@slexisvn/mlfw 0.1.4 → 0.1.5
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 +1 -1
- package/dist/index.node.js +1 -1
- package/package.json +1 -1
package/dist/index.node.js
CHANGED
|
@@ -244,7 +244,7 @@ fn rnn(@builtin(local_invocation_id) lid : vec3<u32>) {
|
|
|
244
244
|
`)}printFunction(e,t=null){let r=t===null;r&&(t=[]),this.valueNames.clear(),this._nextValueId=0;let s=e.args.map((i,a)=>`${this._nameValue(i)}: ${Gn(i.type)}`),o=e.outputTypes.map(i=>Gn(i)).join(", ");t.push(`${this._indentPrefix()}func @${e.name}(${s.join(", ")}) -> (${o}) {`),this.indent++;for(let i of e.body)this.printBlock(i,t,i===e.entryBlock);return this.indent--,t.push(`${this._indentPrefix()}}`),r?t.join(`
|
|
245
245
|
`):void 0}printBlock(e,t,r=!1){if(!r&&e.arguments.length>0){let s=e.arguments.map(o=>`${this._nameValue(o)}: ${Gn(o.type)}`);t.push(`${this._indentPrefix()}^bb(${s.join(", ")}):`)}for(let s of e)this.printOperation(s,t)}printOperation(e,t=null){let r=t===null;r&&(t=[]);let s=this._indentPrefix(),o=s;if(e.numResults>0){let i=[];for(let a=0;a<e.numResults;a++)i.push(this._nameValue(e.getResult(a)));o+=i.join(", ")+" = "}if(o+=e.opName,e.numOperands>0){let i=[];for(let a=0;a<e.numOperands;a++)i.push(this._valueName(e.getOperand(a)));o+="("+i.join(", ")+")"}else o+="()";if(e.attributes.size>0){let i=[];for(let[a,u]of TG(e.attributes))i.push(`${a} = ${__(u)}`);o+=" {"+i.join(", ")+"}"}if(e.numResults>0){let i=[];for(let a=0;a<e.numResults;a++)i.push(Gn(e.getResult(a).type));o+=" : "+i.join(", ")}t.push(o);for(let i=0;i<e.regions.length;i++){let a=e.regions[i];t.push(`${s}{`),this.indent++;for(let u of a){if(u.arguments.length>0){let p=u.arguments.map(c=>`${this._nameValue(c)}: ${Gn(c.type)}`);t.push(`${this._indentPrefix()}^bb(${p.join(", ")}):`)}for(let p of u)this.printOperation(p,t)}this.indent--,t.push(`${s}}`)}return r?t.join(`
|
|
246
246
|
`):void 0}_nameValue(e){if(this.valueNames.has(e))return this.valueNames.get(e);let t=`%${this._nextValueId++}`;return this.valueNames.set(e,t),t}_valueName(e){return this.valueNames.has(e)?this.valueNames.get(e):"%?"}_indentPrefix(){let e="";for(let t=0;t<this.indent;t++)e+=this.indentStr;return e}};l(TG,"sortedEntries");l(__,"formatAttrValue");l(xG,"printModule")});var VG,B_,eE=M(()=>{"use strict";Re();po();VG={eq:l((n,e)=>n===e,"eq"),ne:l((n,e)=>n!==e,"ne"),gt:l((n,e)=>n>e,"gt"),ge:l((n,e)=>n>=e,"ge"),lt:l((n,e)=>n<e,"lt"),le:l((n,e)=>n<=e,"le")},B_=class{static{l(this,"ShapeEnv")}_symbols;_guards;_bindings;_nextId;constructor(){this._symbols=new Map,this._guards=[],this._bindings=new Map,this._nextId=0}allocate(e,t,r){let s=`s${this._nextId++}`;return this._symbols.set(s,{hint:r,inputIdx:e,dimIdx:t}),s}produceShapeSpec(e,t,r){let s=new Array(t.length),o=new Array(t.length);for(let i=0;i<t.length;i++)if(r&&r.has(i)){let a=this.allocate(e,i,t[i]);s[i]=X,o[i]=a}else{let a=this.allocate(e,i,t[i]);this.guardRelation(a,"eq",t[i]),s[i]=t[i],o[i]=t[i]}return{irShape:s,symShape:o}}guardRelation(e,t,r){this._guards.push({lhs:e,op:t,rhs:r})}guardDivisible(e,t){this._guards.push({type:"divisible",sym:e,divisor:t})}bindInputShapes(e){this._bindings.clear();for(let[t,r]of this._symbols)this._bindings.set(t,e[r.inputIdx].shape[r.dimIdx])}evaluateGuards(){for(let e=0;e<this._guards.length;e++){let t=this._guards[e];if("type"in t){if(this._resolve(t.sym)%t.divisor!==0)return{passed:!1,failedGuard:t};continue}let r=this._resolve(t.lhs),s=this._resolve(t.rhs);if(!VG[t.op](r,s))return{passed:!1,failedGuard:t}}return{passed:!0,failedGuard:null}}resolveSymbolicShape(e){let t=new Array(e.length);for(let r=0;r<e.length;r++)t[r]=this._resolve(e[r]);return t}_resolve(e){return typeof e=="number"?e:typeof e=="string"?this._bindings.get(e):e instanceof Pe?Pe.evaluate(e,this._bindings):e}get symbols(){return this._symbols}get guards(){return this._guards}get bindings(){return this._bindings}}});function As(){return R_}var R_,M_,_u=M(()=>{"use strict";_s();Di();qs();Re();Ct();yu();eE();sS();R_=null;l(As,"getActiveTracer");M_=class{static{l(this,"Tracer")}_name;_shapeEnv;_inputTypes;_inputSymShapes;_outputTypes;_outputSymShapes;_inputs;_func;_builder;_module;_capturedParams;_capturedParamOrder;constructor(e){this._name=e||"traced",this._shapeEnv=new B_,this._inputTypes=[],this._inputSymShapes=[],this._outputTypes=[],this._outputSymShapes=[],this._inputs=[],this._func=null,this._builder=null,this._module=null,this._capturedParams=new Map,this._capturedParamOrder=[]}get shapeEnv(){return this._shapeEnv}createInput(e,t,r){let s=this._inputTypes.length,{irShape:o,symShape:i}=this._shapeEnv.produceShapeSpec(s,e,r);if(r)for(let u=0;u<i.length;u++)typeof i[u]=="string"&&this._shapeEnv.guardRelation(i[u],"gt",0);let a=new I(o,t);return this._inputTypes.push(a),this._inputSymShapes.push(i),{shape:o,dtype:t,tensorType:a}}_initGraph(){this._func=new yn(this._name,this._inputTypes,[]),this._func.inputTypes=[...this._func.inputTypes],this._builder=new $n(this._func),this._module=new Mn(this._name);let e=[],t=this._func.args;for(let r=0;r<t.length;r++){let s=t[r],o=this._inputTypes[r];s.symbolicShape=this._inputSymShapes[r];let i=new bt(s,o.shape,o.dtype,this,this._inputSymShapes[r]);e.push(i)}return this._inputs=e,e}recordOp(e,t,r){let s=[];for(let u of t)u instanceof bt&&s.push(u.irValue);let o=this._requireBuilder(),i=Ry(o,e,s,r),a=[];for(let u=0;u<i.numResults;u++){let p=i.getResult(u),c=p.type,f=this._propagateSymbolicShape(e,i,t,c,u);p.symbolicShape=f,a.push(new bt(p,c.shape,c.dtype,this,f))}return a.length===1?a[0]:a}_propagateSymbolicShape(e,t,r,s,o=0){let i=r.filter(c=>c instanceof bt),a=ne.get(t.opName||e);if(a&&a.propagateSymbolicShapes){let c=new Map;for(let d of i)c.set(d.irValue,d.symbolicShape);let f=a.propagateSymbolicShapes(t,c);if(f&&f[o])return f[o]}let u=s.shape,p=new Array(u.length);for(let c=0;c<u.length;c++){if(u[c]!==X){p[c]=u[c];continue}let f=null;for(let d of i){let h=d.symbolicShape;if(!h)continue;let m=u.length-h.length,g=c-m;if(g>=0&&g<h.length&&typeof h[g]=="string"){f=h[g];break}}p[c]=f!==null?f:X}return p}scan(e,t,r){let s=l(d=>d instanceof bt?d.irValue:this.captureConstant(d).irValue,"toIr"),o=e.map(s),i=t.map(s),u=this._requireBuilder().scanOp(o,i,(d,h,m)=>{let g=this._requireBuilder();this._builder=d;try{let y=l(w=>new bt(w,w.type.shape,w.type.dtype,this,[...w.type.shape]),"wrap"),[_,b]=r(m.map(y),h.map(y));return[_.map(w=>w.irValue),b.map(w=>w.irValue)]}finally{this._builder=g}}),p=t.length,c=[],f=[];for(let d=0;d<u.numResults;d++){let h=u.getResult(d),m=new bt(h,h.type.shape,h.type.dtype,this,[...h.type.shape]);d<p?c.push(m):f.push(m)}return[c,f]}captureConstant(e){let t=this._capturedParams.get(e);if(t)return t;if(e.shape.length===0&&e.data){let u=e.data[0],c=this._requireBuilder().scalarConstant(u,e.dtype).getResult(0),f=new bt(c,[],e.dtype,this,[]);return this._capturedParams.set(e,f),f}let r=new I(e.shape,e.dtype),s=this._requireFunc();s.inputTypes.push(r);let i=s.entryBlock.addArgument(r),a=new bt(i,e.shape,e.dtype,this,[...e.shape]);return this._capturedParams.set(e,a),this._capturedParamOrder.push(e),a}get capturedParams(){return this._capturedParamOrder}markOutput(e){e instanceof bt&&(this._requireBuilder().returnOp([e.irValue]),this._outputSymShapes=[e.symbolicShape]),this._outputTypes=[new I(e.shape,e.dtype)]}markOutputs(e){let t=e.map(r=>r.irValue);this._requireBuilder().returnOp(t),this._outputTypes=e.map(r=>new I(r.shape,r.dtype)),this._outputSymShapes=e.map(r=>r.symbolicShape)}get outputSymShapes(){return this._outputSymShapes}getGraphModule(){let e=this._requireFunc(),t=this._requireModule();return e.outputTypes=Object.freeze(this._outputTypes),Object.isFrozen(e.inputTypes)||(e.inputTypes=Object.freeze(e.inputTypes)),t.addFunction(e),t}activate(){R_=this}deactivate(){R_===this&&(R_=null)}_requireBuilder(){if(!this._builder)throw new Error("Tracer graph has not been initialized");return this._builder}_requireFunc(){if(!this._func)throw new Error("Tracer graph has not been initialized");return this._func}_requireModule(){if(!this._module)throw new Error("Tracer graph has not been initialized");return this._module}}});function gE(){return[...mE]}function F_(n,e,t,r=mE){return r.filter(s=>s.phase===n).sort((s,o)=>s.priority-o.priority).map(s=>s.factory(e,t)).filter(Boolean)}var mE,ev=M(()=>{"use strict";mE=[];l(gE,"snapshotGraphPasses");l(F_,"graphPassesForPhase")});function yE(n){return n instanceof Map?n:n&&typeof n=="object"?new Map(Object.entries(n)):new Map}var z_,_E=M(()=>{"use strict";ev();l(yE,"toMap");z_=class{static{l(this,"CompilerContext")}constructor({loweringRules:e=null,codegenEntries:t=null,graphPasses:r=null}={}){this.loweringRules=yE(e),this.codegenEntries=yE(t),this.graphPasses=r||gE()}get hasOverrides(){return this.loweringRules.size>0||this.codegenEntries.size>0}getLoweringRule(e){return this.loweringRules.get(e)||null}getCodegenEntry(e){return this.codegenEntries.get(e)||null}passesForPhase(e,t,r){return F_(e,t,r,this.graphPasses)}}});function V_(n){return new tv().print(n)}var tv,nv=M(()=>{"use strict";tr();tv=class{static{l(this,"TensorIRPrinter")}constructor(){this.indent=0,this.out=[]}print(e){return this.out=[],this.visit(e),this.out.join("")}push(e){this.out.push(e)}newline(){this.out.push(`
|
|
247
|
-
`+" ".repeat(this.indent))}visit(e){if(!e)return;let t="visit"+e.type;this[t]?this[t](e):this.push(`[UnknownNode: ${e.type}]`)}visitPrimFunc(e){this.push(`prim_func ${e.name}(${e.params.map(t=>t.name).join(", ")}) {`),this.indent++,this.newline();for(let[t,r]of e.bufferMap)this.push(`${r.name} = buffer_map(${t.name}, shape=[${r.shape.join(",")}], dtype=${r.dtype})`),this.newline();this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitSeqNode(e){for(let t=0;t<e.stmts.length;t++)this.visit(e.stmts[t]),t<e.stmts.length-1&&this.newline()}visitForNode(e){let t=e.kind==="serial"?"":`@${e.kind} `,r=e.threadTag?`[${e.threadTag}] `:"";this.push(`for ${e.loopVar.name} in 0..`),this.visit(e.extent),this.push(` ${t}${r}{`),this.indent++,this.newline(),this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitBlockNode(e){this.push(`block ${e.name} {`),this.indent++,this.newline();for(let t of e.iterVars)this.push(`bind ${t.iterVar.name} = `),this.visit(t.binding),this.newline();e.reads.length>0&&(this.push("reads(["),this.push(e.reads.map(t=>`${t.buffer.name}[...]`).join(", ")),this.push("])"),this.newline()),e.writes.length>0&&(this.push("writes(["),this.push(e.writes.map(t=>`${t.buffer.name}[...]`).join(", ")),this.push("])"),this.newline()),e.initBody&&(this.push("init {"),this.indent++,this.newline(),this.visit(e.initBody),this.indent--,this.newline(),this.push("}"),this.newline()),this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitBufferStoreNode(e){this.push(`${e.buffer.name}[`);for(let t=0;t<e.indices.length;t++)this.visit(e.indices[t]),t<e.indices.length-1&&this.push(", ");this.push("] = "),this.visit(e.value)}visitBufferLoadNode(e){this.push(`${e.buffer.name}[`);for(let t=0;t<e.indices.length;t++)this.visit(e.indices[t]),t<e.indices.length-1&&this.push(", ");this.push("]")}visitIfThenElseNode(e){this.push("if ("),this.visit(e.condition),this.push(") {"),this.indent++,this.newline(),this.visit(e.thenBody),this.indent--,this.newline(),this.push("}"),e.elseBody&&(this.push(" else {"),this.indent++,this.newline(),this.visit(e.elseBody),this.indent--,this.newline(),this.push("}"))}visitLetStmtNode(e){this.push(`let ${e.variable.name} = `),this.visit(e.value),this.newline(),this.visit(e.body)}visitAllocateNode(e){this.push(`allocate ${e.buffer.name}[${e.buffer.shape.join(", ")}] (${e.scope}) {`),this.indent++,this.newline(),this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitEvaluateNode(e){this.push("evaluate "),this.visit(e.value)}visitMathOpNode(e){this.push("("),this.visit(e.a),e.b&&(this.push(` ${e.op} `),this.visit(e.b)),this.push(")")}visitCompareNode(e){this.push("("),this.visit(e.a),this.push(` ${Ku(e.direction)} `),this.visit(e.b),this.push(")")}visitCallExternNode(e){this.push(`${e.externName}(`);for(let t=0;t<e.args.length;t++)this.visit(e.args[t]),t<e.args.length-1&&this.push(", ");this.push(")")}visitVariableNode(e){this.push(e.name)}visitIntImmNode(e){this.push(e.value.toString())}visitFloatImmNode(e){this.push(e.value.toString())}visitCastNode(e){this.push(`cast<${e.toDtype}>(`),this.visit(e.expr),this.push(")")}};l(V_,"printTensorIR")});var G_,bE=M(()=>{"use strict";nn();nv();G_=class{static{l(this,"TirPassManager")}constructor(){this.passes=[],this.trace=null,this.verifyHook=null}addPass(e){this.passes.push(e)}setTrace(e){this.trace=e}setVerifyHook(e){this.verifyHook=e}run(e,t){for(let r of this.passes)this._runPass(r,e,t);return e}_runPass(e,t,r){let s=r.trace;e.trace=s,s.phaseStart(e.phase);let o=performance.now();e.begin(r);for(let i=0;i<t.length;i++){let a=t[i];if(!r.failed.has(a.name))try{let u=e.run(a,r);u&&u!==a&&(t[i]=u)}catch(u){if(r.errors.push(new Yn(e.phase,a.name,u.message)),r.failed.add(a.name),s.errorEvent(e.phase,a.name,u.message),!r.resilient)break}}if(e.end(r),s.phaseEnd(e.phase,performance.now()-o),e.snapshotPoint&&s.shouldSnapshot(e.snapshotPoint))for(let i of t)r.failed.has(i.name)||s.irDump(e.snapshotPoint+":"+i.name,V_(i));this.verifyHook&&this._verifyFuncs(t,r),e.trace=null}_verifyFuncs(e,t){for(let r of e){if(t.failed.has(r.name))continue;let s=this.verifyHook(r);if(s&&s.length>0){let o=s.join("; ");if(t.resilient)t.errors.push(new Yn("verification",r.name,o)),t.failed.add(r.name);else throw new Error("TensorIR verification failed for "+r.name+": "+o)}}}}});function wE(n){let e=new Wu;return e.add(new Zu),e.add(new Qu),e.add(new Xu(n)),e.add(new el),e.add(new tl(n)),e.add(new nl),e.add(new rl),e.add(new Vh),e.add(new Ju),e.add(new Kh),e.add(new Hh),e.add(new Yh),e.add(new Jh),n&&(e.add(new Wh(n)),e.add(new qh(n)),e.add(new Uh(n))),e}var p6,f6,j_,TE=M(()=>{"use strict";fn();lp();Rx();sl();l(wE,"buildAlgebraicPatterns");p6=wE(!1),f6=wE(!0),j_=class extends Ue{static{l(this,"AlgebraicSimplificationPass")}constructor(e={}){super("algebraic_simplify"),this.preservedAnalyses=new Set,this.patterns=e.fastMath?f6:p6}run(e,t){return new Ul(this.patterns).applyPatterns(e,10,this.trace)}}});function xE(n){let e=ne.get(n);return!!(e&&e.isConstant)}function d6(n,e){return!f0(e)||typeof n!="number"?!0:Number.isInteger(n)&&Number.isSafeInteger(n)}function SE(n,e,t){if(t.has(n))return t.get(n);let r=h6(n,e,t);return t.set(n,r),r}function h6(n,e,t){let r=n.definingOp;if(!r)return;if(xE(r.opName))return r.getAttr("value");if(e.has(r))return;e.add(r);let s=ne.get(r.opName);if(!s||!s.fold||s.hasSideEffects||r.regions.length>0)return;let o=new Array(r.numOperands),i=new Array(r.numOperands);for(let a=0;a<r.numOperands;a++){let u=SE(r.getOperand(a),e,t);if(u===void 0)return;o[a]=u,i[a]=r.getOperand(a).definingOp}try{return s.fold(o,r.attributes,i)}catch{return}}var q_,vE=M(()=>{"use strict";fn();_s();Ct();nn();Re();l(xE,"isConstantProducer");l(d6,"isFoldResultRepresentable");l(SE,"resolveConstantValue");l(h6,"computeConstantValue");q_=class extends Ue{static{l(this,"ConstantFoldPass")}constructor(){super("constant_fold")}run(e,t){let r=!1,s=0,o=new $n(e),i=new Map;for(let a of[...e.opsRecursive()]){if(!a.parentBlock||xE(a.opName))continue;let u=ne.get(a.opName);if(!u||a.regions.length>0||u.hasSideEffects||u.getMemoryEffects&&u.getMemoryEffects(a).length>0||!u.fold||a.numOperands===0)continue;let p=new Array(a.numOperands),c=new Array(a.numOperands),f=!0;for(let d=0;d<a.numOperands;d++){let h=SE(a.getOperand(d),new Set,i);if(h===void 0){f=!1;break}p[d]=h,c[d]=a.getOperand(d).definingOp}if(f)try{let d=u.fold(p,a.attributes,c);if(d===void 0||!d6(d,a.getResult(0).type.dtype))continue;o.block=a.parentBlock,o.setInsertionPoint(a);let h=o.constant(d,a.getResult(0).type);a.replaceAllResultsWith([h.getResult(0)]),a.erase(),r=!0,s++}catch(d){this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,foldError:a.opName,message:d.message,level:le.DEBUG})}}return this.trace&&this.trace.level>=le.DEBUG&&s>0&&this.trace.emit({type:"pass_detail",passName:this.name,foldedCount:s,level:le.DEBUG}),r?oe.CHANGED:oe.UNCHANGED}}});var U_,kE=M(()=>{"use strict";fn();Ct();nn();U_=class extends Ue{static{l(this,"CSEPass")}constructor(){super("cse"),this.preservedAnalyses=new Set}run(e,t){let r=!1,s=0,o=typeof e.blocksRecursive=="function"?[...e.blocksRecursive()]:[...e.body];for(let i of o){let a=new Map;for(let u of[...i.ops()]){if(!u.parentBlock||u.regions&&u.regions.length>0)continue;let p=ne.get(u.opName);if(p&&p.hasSideEffects||p&&p.getMemoryEffects&&p.getMemoryEffects(u).length>0)continue;let c=u.structuralHash();if(!a.has(c)){a.set(c,[u]);continue}let f=a.get(c),d=!1;for(let h of f)if(h.parentBlock&&h.structuralEquals(u)){let m=[];for(let g=0;g<h.numResults;g++)m.push(h.getResult(g));u.replaceAllResultsWith(m),u.erase(),r=!0,s++,d=!0;break}d||f.push(u)}}return this.trace&&this.trace.level>=le.DEBUG&&s>0&&this.trace.emit({type:"pass_detail",passName:this.name,eliminated:s,level:le.DEBUG}),r?oe.CHANGED:oe.UNCHANGED}}});function sv(n,e){let t=rv[n],r=rv[e];return t===void 0||r===void 0?!1:n===We.REDUCTION?e===We.ELEMENTWISE:e===We.REDUCTION?t<=rv[We.INJECTIVE]:!0}function wc(n){let e=!1,t=!1,r=!1,s=!1;for(let o of n){let i=ne.get(o.opName);if(!i||i.isOpaque){r=!0;continue}i.isReduction?e=!0:i.isInjective?t=!0:i.isBroadcast?s=!0:i.isElementwise||(r=!0)}return r?We.OPAQUE:e?We.REDUCTION:t?We.INJECTIVE:s?We.BROADCAST:We.ELEMENTWISE}function Ou(n){let e=ne.get(n.opName);return!e||e.isOpaque?We.OPAQUE:e.isReduction?We.REDUCTION:e.isInjective?We.INJECTIVE:e.isBroadcast?We.BROADCAST:e.isElementwise?We.ELEMENTWISE:We.OPAQUE}function NE(n,e){if(n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(!(n[t]===X||e[t]===X)&&n[t]!==e[t])return!1;return!0}var We,rv,_c,bc,Bu=M(()=>{"use strict";Re();Ct();$a();Ir();We=Object.freeze({ELEMENTWISE:"kElementwise",BROADCAST:"kBroadcast",REDUCTION:"kReduction",INJECTIVE:"kInjective",OPAQUE:"kOpaque",HORIZONTAL:"kHorizontal"}),rv={[We.ELEMENTWISE]:0,[We.BROADCAST]:1,[We.INJECTIVE]:2,[We.REDUCTION]:3};l(sv,"canFusePatterns");l(wc,"classifyFusionKind");l(Ou,"classifyOpPattern");_c=Object.freeze({legal:!0}),bc=class{static{l(this,"FusionLegality")}constructor(e={}){this.maxFusionSize=e.maxFusionSize||512,this.maxSharedMemory=e.maxSharedMemory||49152,this.libraryOps=e.libraryOps||new Set,this.allowReductionFusion=e.allowReductionFusion!==!1,this._lowerableCache=new Map}isOpLowerable(e){let t=this._lowerableCache.get(e);return t===void 0&&(t=Xt(e)||ni(e)||C0(e),this._lowerableCache.set(e,t)),t}canFuse(e,t){if(!e||!t)return{legal:!1,reason:"null op"};if(e===t)return{legal:!1,reason:"same op"};if(e.regions.length>0&&e.opName!=="fusion"&&e.opName!=="reduce")return{legal:!1,reason:"producer has control flow regions"};if(t.regions.length>0&&t.opName!=="fusion"&&t.opName!=="reduce")return{legal:!1,reason:"consumer has control flow regions"};let r=ne.get(e.opName),s=ne.get(t.opName);if(!r)return{legal:!1,reason:"unknown producer op"};if(!s)return{legal:!1,reason:"unknown consumer op"};if(!this.isOpLowerable(e.opName))return{legal:!1,reason:`producer op '${e.opName}' has no lowering rule`};if(!this.isOpLowerable(t.opName))return{legal:!1,reason:`consumer op '${t.opName}' has no lowering rule`};if(r.isOpaque)return{legal:!1,reason:"producer is opaque (use EpilogueFusionPass for dot/conv epilogues)"};if(s.isOpaque)return{legal:!1,reason:"consumer is opaque"};let o=Ou(e),i=Ou(t);return sv(o,i)?(o===We.REDUCTION||i===We.REDUCTION)&&!this.allowReductionFusion?{legal:!1,reason:"reduction fusion disabled by target"}:o===We.ELEMENTWISE&&i===We.ELEMENTWISE?this._checkElementwisePair(e,t):(o===We.BROADCAST||o===We.REDUCTION)&&i===We.ELEMENTWISE?_c:this._checkProducerConsumerShapes(e,t):{legal:!1,reason:`cannot fuse pattern ${o} -> ${i}`}}canMergeGroups(e,t){if(e.size+t.size>this.maxFusionSize)return{legal:!1,reason:"merged group exceeds max fusion size"};let r=0;for(let s of e.ops){let o=ne.get(s.opName);if(o&&o.isReduction&&r++,o&&o.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}for(let s of t.ops){let o=ne.get(s.opName);if(o&&o.isReduction&&r++,o&&o.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}return r>1?{legal:!1,reason:"merged group would contain multiple reductions"}:_c}_checkElementwisePair(e,t){let r=this._getOutputShape(e),s=this._getOutputShape(t);return r&&s&&!NE(r,s)?{legal:!1,reason:`elementwise shape mismatch: [${r}] vs [${s}]`}:_c}_checkProducerConsumerShapes(e,t){let r=this._getOutputShape(e);if(!r)return _c;for(let s=0;s<t.numOperands;s++)if(t.getOperand(s).definingOp===e){let o=t.getOperand(s).type;if(o instanceof I)return NE(r,o.shape)?_c:{legal:!1,reason:`shape mismatch on data edge: [${r}] vs [${o.shape}]`}}return _c}_getOutputShape(e){for(let t=0;t<e.numResults;t++){let r=e.getResult(t).type;if(r instanceof I)return r.shape}return null}};l(NE,"shapesCompatible")});function Ru(n){return am(n,"throw")}function kd(n,{labelOf:e,sameLabel:t=l((i,a)=>i===a,"sameLabel"),canMerge:r=l(()=>!0,"canMerge"),onAttach:s=l(()=>{},"onAttach"),sort:o=Ru}){let i=o(n),a=new Map,u=new Map,p=[],c=0,f=l((m,g)=>{if(m===g)return!0;let y=[g],_=new Set;for(;y.length>0;){let b=y.pop();if(b===m)return!0;if(_.has(b))continue;_.add(b);let w=u.get(b);if(w)for(let x of w)y.push(x)}return!1},"isUpstreamOf"),d=l(m=>{let g=new Set;for(let y=0;y<m.numOperands;y++){let _=m.getOperand(y).definingOp;if(!_)continue;let b=a.get(_);b&&g.add(b)}return g},"operandParts"),h=l((m,g)=>{for(let y of d(m)){if(y===g)continue;let _=u.get(g);_||(_=new Set,u.set(g,_)),_.add(y)}},"recordEdges");for(let m of i){let g=e(m);if(g==null)continue;let y=!1;for(let _=0;_<m.numOperands;_++){let b=m.getOperand(_).definingOp;if(!b)continue;let w=a.get(b);if(!w||!t(w.label,g)||!r(w,m,g))continue;let x=!1;for(let S of d(m))if(S!==w&&f(w,S)){x=!0;break}if(!x){w.ops.push(m),w.opSet.add(m),s(w,m),a.set(m,w),h(m,w),y=!0;break}}if(!y){let _={id:c++,label:g,ops:[m],opSet:new Set([m])};s(_,m),p.push(_),a.set(m,_),h(m,_)}}return{partitions:p,opToPart:a,preds:u}}function pa(n,e,{pullConstants:t=!1,isConstant:r=l(()=>!1,"isConstant")}={}){let s=[],o=new Set,i=[],a=new Set,u=[],p=new Set;for(let c of e){for(let f=0;f<c.numOperands;f++){let d=c.getOperand(f),h=d.definingOp;if(!(h&&n.has(h))){if(t&&h&&r(h)){p.has(h)||(p.add(h),u.push(h));continue}o.has(d)||(o.add(d),s.push(d))}}for(let f=0;f<c.numResults;f++){let d=c.getResult(f);if(a.has(d))continue;let h=!1;for(let m of d.uses())if(!n.has(m.user)){h=!0;break}h&&(a.add(d),i.push(d))}}return{inputs:s,outputs:i,constDefs:u}}function ov(n,e){let t=new Map,r=new Map;for(let a of n)t.set(a,0),r.set(a,[]);for(let a of n){let u=e.get(a);if(u)for(let p of u)r.has(p)&&(r.get(p).push(a),t.set(a,t.get(a)+1))}let s=[];for(let a of n)t.get(a)===0&&s.push(a);let o=[],i=0;for(;i<s.length;){let a=s[i++];o.push(a);for(let u of r.get(a)){let p=t.get(u)-1;t.set(u,p),p===0&&s.push(u)}}return o.length===n.length?o:null}var Nd=M(()=>{"use strict";Da();l(Ru,"topoSortOps");l(kd,"buildPartitions");l(pa,"computePartitionIO");l(ov,"topoSortPartitions")});function m6(n){for(let e=0;e<n.numResults;e++){let t=n.getResult(e).type;if(t instanceof I)return t.shape.join(",")}return null}var wi,W_,K_=M(()=>{"use strict";Re();Ct();Bu();$a();Nd();wi=class{static{l(this,"FusionGroup")}constructor(e){this.id=e,this.ops=[],this.opSet=new Set,this.kind=null,this._inputValues=null,this._outputValues=null,this.minTopoPos=1/0,this.maxTopoPos=-1/0}addOp(e,t){this.opSet.has(e)||(this.ops.push(e),this.opSet.add(e),this._inputValues=null,this._outputValues=null,t!==void 0&&(t<this.minTopoPos&&(this.minTopoPos=t),t>this.maxTopoPos&&(this.maxTopoPos=t)))}hasOp(e){return this.opSet.has(e)}merge(e){for(let t of e.ops)this.addOp(t);e.minTopoPos<this.minTopoPos&&(this.minTopoPos=e.minTopoPos),e.maxTopoPos>this.maxTopoPos&&(this.maxTopoPos=e.maxTopoPos)}computeIO(){if(this._inputValues&&this._outputValues)return;let{inputs:e,outputs:t}=pa(this.opSet,this.ops);this._inputValues=e,this._outputValues=t}getInputValues(){return this.computeIO(),this._inputValues}getOutputValues(){return this.computeIO(),this._outputValues}get size(){return this.ops.length}classifyKind(){return this.kind=wc(this.ops),this.kind}allOpsInlineFusable(){for(let e of this.ops){let t=ne.get(e.opName);if(!t)return!1;if(!(t.isReduction||t.isConstant)&&!ni(e.opName))return!1}return!0}};l(m6,"outputShapeKey");W_=class{static{l(this,"FusionGroupBuilder")}constructor(e){this.legality=e,this._nextId=0,this._topoIndex=null}buildProducerConsumerGroups(e){this._topoIndex=new Map;let t=0;for(let i of e.ops())this._topoIndex.set(i,t++);let r=[],s=new Map;for(let i of e.ops()){let a=ne.get(i.opName);if(!a||a.isConstant||a.isTerminator||a.isReduction)continue;let u=this._topoIndex.get(i);for(let p=0;p<i.numOperands;p++){let c=i.getOperand(p).definingOp;if(!c)continue;let f=ne.get(c.opName);if(!f||f.isConstant||f.isReduction)continue;let d=s.get(i),h=s.get(c);if(d&&d===h||!this.legality.canFuse(c,i).legal)continue;let g=this._topoIndex.get(c);if(!d&&!h){let y=new wi(this._nextId++);y.addOp(c,g),y.addOp(i,u),s.set(c,y),s.set(i,y)}else if(d&&!h)d.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(d,c)&&(d.addOp(c,g),s.set(c,d));else if(!d&&h)h.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(h,i)&&(h.addOp(i,u),s.set(i,h));else if(this.legality.canMergeGroups(d,h).legal&&!this._mergeWouldCreateCycle(d,h)){d.merge(h);for(let _ of h.ops)s.set(_,d)}}}let o=new Set;for(let i of s.values())o.has(i)||i.size<2||(o.add(i),i.classifyKind(),r.push(i));return r}_wouldCreateCycle(e,t){for(let r=0;r<t.numOperands;r++){let s=t.getOperand(r).definingOp;if(!(!s||e.hasOp(s)||this._topoIndex.get(s)<e.minTopoPos)&&this._dependsOnGroup(s,e))return!0}return!1}_dependsOnOps(e,t,r){let s=new Set,o=[e];for(s.add(e);o.length>0;){let i=o.pop();for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a).definingOp;if(!u||s.has(u))continue;if(t.has(u))return!0;this._topoIndex.get(u)<r||(s.add(u),o.push(u))}}return!1}_dependsOnGroup(e,t){return this._dependsOnOps(e,t.opSet,t.minTopoPos)}_mergeWouldCreateCycle(e,t){let r=Math.min(e.minTopoPos,t.minTopoPos),s=Math.max(e.maxTopoPos,t.maxTopoPos),o=new Set([...e.opSet,...t.opSet]);for(let i of o)for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a).definingOp;if(!u||o.has(u))continue;let p=this._topoIndex.get(u);if(!(p<r||p>s)&&this._dependsOnOps(u,o,r))return!0}return!1}_bucketable(e,t){return t&&!t.isConstant&&!t.isTerminator&&!t.isOpaque&&e.numResults>0&&m6(e)!==null}buildHorizontalGroups(e){let t=[...e.ops()];this._topoIndex=new Map;for(let u=0;u<t.length;u++)this._topoIndex.set(t[u],u);let r=[],s=new Map,o=this.legality.maxFusionSize||t.length,i=new Map,a=0;for(let u=0;u<t.length;u++){let p=t[u];if(s.has(p))continue;let c=ne.get(p.opName);if(!this._bucketable(p,c))continue;let f=p.getResult(0).type;a++,i.set(p,a);let d=new wi(this._nextId++);d.addOp(p,u),s.set(p,d);let h=Math.min(t.length,u+1+o);for(let m=u+1;m<h;m++){let g=t[m],y=!1;for(let b=0;b<g.numOperands;b++){let w=g.getOperand(b).definingOp;if(w&&i.get(w)===a){y=!0;break}}if(y){i.set(g,a);continue}if(d.size>=o||s.has(g))continue;let _=ne.get(g.opName);this._bucketable(g,_)&&f.equals(g.getResult(0).type)&&(this._sharesInput(p,g)||c.isElementwise&&_.isElementwise)&&(d.addOp(g,m),s.set(g,d),i.set(g,a))}d.size>=2&&(d.kind=We.HORIZONTAL,r.push(d))}return r}buildAllGroups(e){let t=this.buildProducerConsumerGroups(e),r=this.buildHorizontalGroups(e),s=new Map;for(let c of t)for(let f of c.ops)s.set(f,c);let o=new Set(s.keys()),i=[];for(let c of r){let f=!1;for(let d of c.ops)if(o.has(d)){f=!0;break}f||i.push(c)}let a=[...e.ops()],u=[];for(let c of a)for(let f=0;f<c.numOperands;f++){let d=c.getOperand(f).definingOp;d&&u.push(c,d)}for(let c of i)for(let f of c.ops)s.set(f,c);if(!this._condensedHasCycle(a,u,s))return[...t,...i];for(let c of i)for(let f of c.ops)s.delete(f);let p=[...t];for(let c of i){for(let f of c.ops)s.set(f,c);if(this._condensedHasCycle(a,u,s)){for(let f of c.ops)s.delete(f);continue}p.push(c)}return p}_condensedHasCycle(e,t,r){let s=l(f=>r.get(f)||f,"repOf"),o=new Map,i=new Set;for(let f of e)i.add(s(f));for(let f=0;f<t.length;f+=2){let d=s(t[f]),h=s(t[f+1]);if(h===d)continue;i.add(h),i.add(d);let m=o.get(h);m||(m=new Set,o.set(h,m)),m.add(d)}let a=0,u=1,p=2,c=new Map;for(let f of i)c.set(f,a);for(let f of i){if(c.get(f)!==a)continue;let d=[f];for(;d.length>0;){let h=d[d.length-1],m=c.get(h);if(m===a){c.set(h,u);let g=o.get(h);if(g)for(let y of g){let _=c.get(y);if(_===u)return!0;_===a&&d.push(y)}}else m===u&&c.set(h,p),d.pop()}}return!1}_sharesInput(e,t){if(e.numOperands===0||t.numOperands===0)return!1;let r=new Set;for(let s=0;s<e.numOperands;s++)r.add(e.getOperand(s));for(let s=0;s<t.numOperands;s++)if(r.has(t.getOperand(s)))return!0;return!1}_hasDependency(e,t){for(let r=0;r<e.numResults;r++){let s=e.getResult(r);for(let o=0;o<t.numOperands;o++)if(t.getOperand(o)===s)return!0}return!1}}});var g6,fa,H_=M(()=>{"use strict";Re();Ct();g6=Object.freeze({memory:1,launch:1e3}),fa=class{static{l(this,"FusionCostModel")}constructor(e={}){this.memoryBandwidthGBs=e.memoryBandwidthGBs||900,this.computeTFLOPs=e.computeTFLOPs||15,this.launchOverheadUs=e.launchOverheadUs||5,this.minBenefitRatio=e.minBenefitRatio||1.05,this.maxRegistersPerThread=e.maxRegistersPerThread||255,this.maxSharedMemory=e.maxSharedMemory||49152,this.maxCodeSizeOps=e.maxCodeSizeOps||256,this.libraryOps=e.libraryOps||new Set,this.registerBytesPerOp=e.registerBytesPerOp||8,this.policy=e.policy||null,this.benefitWeights={...g6,...e.benefitWeights||{}}}edgeBenefit(e){let t=this.benefitWeights;return t.launch*this.launchOverheadUs+t.memory*e}estimateOpCost(e){let t=this.estimateFLOPs(e),r=this.estimateBytes(e);return{flops:t,bytes:r,arithmeticIntensity:r>0?t/r:0}}estimateFLOPs(e){let t=ne.get(e.opName);if(t&&t.getFlops)return t.getFlops(e);let r=1;for(let s=0;s<e.numResults;s++){let o=e.getResult(s).type;if(o instanceof I){let i=o.numel();i!==X&&(r=i);break}}if(t&&t.isReduction&&e.numOperands>0){let s=e.getOperand(0).type;if(s instanceof I){let o=s.numel();if(o!==X)return o}}return r}estimateBytes(e){let t=0;for(let r=0;r<e.numOperands;r++){let s=e.getOperand(r).type;if(s instanceof I){let o=s.sizeInBytes();o!==X&&(t+=o)}}for(let r=0;r<e.numResults;r++){let s=e.getResult(r).type;if(s instanceof I){let o=s.sizeInBytes();o!==X&&(t+=o)}}return t}estimateGroupCost(e){let t=e.opSet,r=new Map,s=0,o=0,i=0,a=0,u=!1,p=!1,c=0,f=0,d=0,h=0,m=new Set;for(let x of e.ops){let S=this.estimateFLOPs(x);r.set(x,S),s+=S,o+=this.estimateBytes(x),this.libraryOps.has(x.opName)&&a++;let T=ne.get(x.opName);if(T){let v=this._outputElements(x);T.isReduction&&(u=!0,c+=v),T.isElementwise&&(p=!0,f+=v)}for(let v=0;v<x.numResults;v++){let A=x.getResult(v);m.add(A);let N=0,k=!1;for(let O of A.uses())t.has(O.user)?N++:k=!0;N>1&&(i+=(N-1)*S),(N>0||k)&&d++}for(let v=0;v<x.numOperands;v++){let A=x.getOperand(v);if(!m.has(A))continue;let N=!1;for(let k of A.uses())if(t.has(k.user)&&k.user!==x){N=!0;break}N||d--}d>h&&(h=d)}let g=e.getInputValues(),y=e.getOutputValues(),_=0;for(let x of g)if(x.type instanceof I){let S=x.type.sizeInBytes();S!==X&&(_+=S)}for(let x of y)if(x.type instanceof I){let S=x.type.sizeInBytes();S!==X&&(_+=S)}let b=0;for(let x of e.ops)for(let S=0;S<x.numResults;S++){let T=x.getResult(S),v=0;for(let A of T.uses())if(t.has(A.user)&&(v++,v>1))break;if(v>1){let A=T.type;if(A instanceof I){let N=A.sizeInBytes();N!==X&&(b+=N)}}}let w=0;return u&&p&&f>0&&c>0&&(w=Math.abs(f-c)),{unfusedFLOPs:s,unfusedBytes:o,fusedFLOPs:s+i,fusedBytes:_,recomputeCost:i,memorySaved:o-_,launchSaved:(e.size-1)*this.launchOverheadUs,registerPressure:h*this.registerBytesPerOp,sharedMemoryUsage:b,parallelismLoss:w,libraryCallLoss:a}}shouldFuse(e){if(e.size<2)return{fuse:!1,reason:"group too small"};if(this.policy&&typeof this.policy.shouldFuse=="function"){let r=this.policy.shouldFuse(e,this);if(r)return r}let t=this.estimateGroupCost(e);if(t.libraryCallLoss>0)return{fuse:!1,reason:"fusion would lose library call opportunity",cost:t};if(t.registerPressure>this.maxRegistersPerThread)return{fuse:!1,reason:`register pressure ${t.registerPressure} exceeds limit ${this.maxRegistersPerThread}`,cost:t};if(t.sharedMemoryUsage>this.maxSharedMemory)return{fuse:!1,reason:`shared memory ${t.sharedMemoryUsage} exceeds limit ${this.maxSharedMemory}`,cost:t};if(e.size>this.maxCodeSizeOps)return{fuse:!1,reason:`code size ${e.size} exceeds limit ${this.maxCodeSizeOps}`,cost:t};if(t.memorySaved<=0&&t.launchSaved<=0)return{fuse:!1,reason:"no memory or launch benefit",cost:t};if(t.parallelismLoss>0){let r=t.memorySaved+t.launchSaved*1e3;if(t.parallelismLoss>r*this.minBenefitRatio)return{fuse:!1,reason:"parallelism loss outweighs fusion benefit",cost:t}}return{fuse:!0,reason:`saves ${t.memorySaved} bytes, ${t.launchSaved}us launch`,cost:t}}_outputElements(e){for(let t=0;t<e.numResults;t++){let r=e.getResult(t).type;if(r instanceof I){let s=r.numel();if(s!==X)return s}}return 0}}});function Tc(n){let e=null;for(let t of n.ops())e=t;return e&&e.opName==="yield"?e:null}function xc(n){let e=0,t=n.regions[0]?.entryBlock;if(!t)return 0;for(let r of t.ops())r.opName!=="yield"&&e++;return e}function Sc(n){let e=0,t=n.regions[0]?.entryBlock;if(!t)return 0;for(let r of t.ops()){let s=ne.get(r.opName);s&&s.isReduction&&e++}return e}function vc(n){let e=n.regions[0]?.entryBlock;if(!e)return!1;for(let t of e.ops())if(t.opName!=="yield"&&!ni(t.opName))return!1;return!0}function y6(n){let e=new Map,t=0;for(let r=n.firstOp;r;r=r._next)e.set(r,t++);return e}function iv(n){let e=y6(n);return(t,r)=>{let s=e.get(t),o=e.get(r);return s!==void 0&&o!==void 0&&s<o}}function kc(n,e){let t=Qo(n.ops,g=>n.hasOp(g),"null");if(t===null||t.length===0)return null;n._inputValues=null,n._outputValues=null;let r=n.getInputValues(),s=n.getOutputValues(),o=r.map(g=>g.type),i=s.map(g=>g.type),a=new qt,u=new On(o);a.addBlock(u);let p=new Map;for(let g=0;g<r.length;g++)p.set(r[g],u.arguments[g]);for(let g of t)u.pushOp(g.clone(p));let c=s.map(g=>{let y=p.get(g);if(y===void 0)throw new Error("Fusion materialization: output value not found in valueMap");return y});u.pushOp(new Me("yield",c,[]));let f=new Me("fusion",r,i,{fusion_kind:n.kind||e},[a]),d=t[0].parentBlock;if(!d)return null;let h=iv(d),m=null;for(let g of r){let y=g.definingOp;!y||n.hasOp(y)||(!m||!h(y,m))&&(m=y)}m&&m.parentBlock===d?d.insertAfter(f,m):d.insertBefore(f,t[0]);for(let g=0;g<s.length;g++)s[g].replaceAllUsesWith(f.getResult(g));for(let g of t)g.dropAllOperands(),g.parentBlock&&g.parentBlock.removeOp(g);return f}var Nc=M(()=>{"use strict";Ct();$a();yr();Ea();Da();l(Tc,"getYieldOp");l(xc,"countInnerOps");l(Sc,"countReductions");l(vc,"allInnerOpsFusable");l(y6,"blockPositionIndex");l(iv,"makeComesBefore");l(kc,"materializeFusionGroup")});var Y_,AE=M(()=>{"use strict";fn();Bu();K_();H_();Nc();nn();Y_=class extends Ue{static{l(this,"FusionPass")}constructor(e={}){super("FusionPass");let t=e.target||{};this.legality=new bc({maxFusionSize:t.maxFusionSize||e.maxFusionSize,maxSharedMemory:t.sharedMemoryBytes||e.maxSharedMemory,libraryOps:t.libraryOps||e.libraryOps,allowReductionFusion:e.allowReductionFusion}),this.costModel=new fa({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,policy:t.getAttr?t.getAttr("fusionPolicy"):null,...e.cost}),this.groupBuilder=new W_(this.legality)}run(e,t){let r=this.groupBuilder.buildAllGroups(e),s=[];for(let i of r)this._createsCycle(e,i)?this._traceDecision(i,!1,"fusing would create a dependency cycle"):s.push(i);let o=[];for(let i of s){if(!i.allOpsInlineFusable()){this._traceDecision(i,!1,"group contains ops without inline fusion support");continue}let a=this.costModel.shouldFuse(i);this._traceDecision(i,a.fuse,a.reason),a.fuse&&o.push(i)}if(o.length===0)return oe.UNCHANGED;for(let i of o)kc(i,We.ELEMENTWISE);return oe.CHANGED}_traceDecision(e,t,r){if(!this.trace||this.trace.level<le.DEBUG)return;let s=e.ops.map(o=>o.opName);this.trace.emit({type:"fusion_decision",passName:this.name,groupSize:e.ops.length,ops:s,anchor:s[s.length-1]||null,fuse:t,reason:r||null,level:le.DEBUG}),this.trace.explain("fusion",s.join("+"),t?"fused":"not-fused",r||null,{groupSize:s.length})}_createsCycle(e,t){let r=t.opSet,s=t.getOutputValues(),o=new Set,i=[];for(let u of s)for(let p of u.uses())r.has(p.user)||(i.push(p.user),o.add(p.user));let a=0;for(;a<i.length;){let u=i[a++];if(r.has(u))return!0;for(let p=0;p<u.numResults;p++)for(let c of u.getResult(p).uses())o.has(c.user)||(o.add(c.user),i.push(c.user))}return!1}}});function J_(n){return cl(n.opName)||_n(n.opName)}function OE(n){if(J_(n))return!0;let e=ne.get(n.opName);return e!==null&&e.isElementwise}function b6(n,e){let t=n.getOperand(0).definingOp;return t&&e.has(t)&&!J_(t)?n.getOperand(1).definingOp:t}function w6(n,e){if(!n.parentBlock||n.parentBlock!==e.parentBlock)return!1;let t=n.parentBlock.firstOp;for(;t;){if(t===n)return!0;if(t===e)return!1;t=t._next}return!1}function T6(n,e){for(let t of n)if(t!==e){for(let r=0;r<t.numResults;r++)for(let s of t.getResult(r).uses())if(!n.has(s.user))return!0}return!1}function x6(n,e){let t=ne.get(n.opName),r=t&&t.getAttr("epilogueTag");return r?r(n,e):"activation"}function S6(n){let e=[],t=new Set,r=new Set;function s(d){if(!(r.has(d)||d===n)){r.add(d);for(let h=0;h<d.numOperands;h++){let m=d.getOperand(h).definingOp;m&&m!==n&&!r.has(m)&&J_(m)&&s(m)}e.push(d),t.add(d)}}l(s,"absorb");let o=[],i=n.getResult(0);for(let d of i.uses())OE(d.user)&&o.push(d.user);for(;o.length>0;){let d=o.pop();if(!r.has(d)){s(d);for(let h=0;h<d.numResults;h++)for(let m of d.getResult(h).uses())!r.has(m.user)&&OE(m.user)&&o.push(m.user)}}let a=[];for(let d of e)J_(d)||a.push(x6(d,t));let u=e[e.length-1];for(let d=e.length-1;d>=0;d--){let h=e[d];for(let m=0;m<h.numResults;m++){for(let g of h.getResult(m).uses())if(!t.has(g.user)){u=h,d=-1;break}if(d<0)break}}let p=new Set;for(let d=0;d<n.numOperands;d++)p.add(n.getOperand(d));let c=[],f=new Set;for(let d of e)for(let h=0;h<d.numOperands;h++){let m=d.getOperand(h);if(f.has(m))continue;f.add(m);let g=m.definingOp;g!==n&&(g&&t.has(g)||p.has(m)||c.push(m))}return{chain:e,chainSet:t,tags:a,lastOp:u,extras:c}}function v6(n){let e=null,t=null,r=null,s=null,o=new Set;for(let i=0;i<2;i++){let a=n.getOperand(i),u=a.definingOp;if(!u||u.opName!=="convert")continue;let p=!1;for(let f of a.uses())if(f.user!==n){p=!0;break}if(p)continue;let c=u.getAttr("target_dtype")||u.getResult(0).type.dtype;i===0?(e=c,r=u.getOperand(0)):(t=c,s=u.getOperand(0)),o.add(u)}return{lhsCast:e,rhsCast:t,lhsInput:r,rhsInput:s,removed:o}}var _6,Z_,BE=M(()=>{"use strict";fn();yr();Ct();Ir();nn();l(J_,"isPassthrough");l(OE,"isEpilogueCandidate");_6=new Map([["add",(n,e)=>e.has(b6(n,e))?"residual_add":"bias"],["sub",()=>"bias"],["mul",()=>"scale"],["maximum",()=>"relu"],["clamp",()=>"clamp"],["neg",()=>"neg"],["exp",()=>"exp"],["tanh",()=>"tanh"],["sqrt",()=>"sqrt"],["abs",()=>"abs"],["log",()=>"log"]]);l(b6,"resolveOtherOperand");for(let[n,e]of _6)ne.has(n)&&ne.registerOpAttr(n,"epilogueTag",e);l(w6,"comesBefore");l(T6,"hasEscapingUse");l(x6,"classifyTag");l(S6,"collectChainAndAnalyze");l(v6,"collectPrologue");Z_=class extends Ue{static{l(this,"EpilogueFusionPass")}constructor(e={}){super("EpilogueFusionPass"),this.maxEpilogueOps=e.maxEpilogueOps||16,this.target=e.target||null}run(e){if(this.target&&!this.target.enableEpilogueFusion)return oe.UNCHANGED;let t=!1,r=[];for(let s of e.ops()){let o=ne.get(s.opName);o&&o.isOutEWiseFusable&&r.push(s)}for(let s of r){let o=S6(s);if(o.chain.length>this.maxEpilogueOps)continue;let i=v6(s),a=o.chain.length>0,u=i.lhsCast!==null||i.rhsCast!==null;if(!a&&!u)continue;let{chain:p,tags:c,extras:f}=o,d=0;for(let v of c)v==="bias"||v==="residual_add"||v==="scale"?d++:v==="clamp"&&(d+=2);if(d!==f.length)continue;let h=i.lhsInput||s.getOperand(0),m=i.rhsInput||s.getOperand(1),g=[h,m,...f],y=a?o.lastOp:s,_=y.getResult(0).type,b=new Map(s.attributes);b.set("epilogue_ops",p.map(v=>v.opName)),b.set("epilogue_tags",c),b.set("num_dot_operands",2),b.set("num_extra_inputs",f.length),i.lhsCast&&b.set("lhs_prologue_cast",i.lhsCast),i.rhsCast&&b.set("rhs_prologue_cast",i.rhsCast);let w=new Me("fused_dot_epilogue",g,[_],b),x=s.parentBlock;if(!x)continue;let S=new Set(p);S.add(s);for(let v of i.removed)S.add(v);if(T6(S,y))continue;let T=null;for(let v of g){let A=v.definingOp;!A||S.has(A)||(!T||!w6(A,T))&&(T=A)}T&&T.parentBlock===x?x.insertAfter(w,T):x.insertBefore(w,s),y.getResult(0).replaceAllUsesWith(w.getResult(0));for(let v=p.length-1;v>=0;v--)p[v].dropAllOperands(),p[v].parentBlock&&p[v].parentBlock.removeOp(p[v]);s.dropAllOperands(),s.parentBlock&&s.parentBlock.removeOp(s);for(let v of i.removed)v.dropAllOperands(),v.parentBlock&&v.parentBlock.removeOp(v);t=!0}return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,dotsFound:r.length,changed:t,level:le.DEBUG}),t?oe.CHANGED:oe.UNCHANGED}}});var Q_,RE=M(()=>{"use strict";fn();yr();Ea();Re();Ct();nn();Bu();Nc();Q_=class extends Ue{static{l(this,"FusionMergerPass")}constructor(e={}){super("FusionMergerPass"),this.maxFusionSize=e.maxFusionSize||512,this.maxReductions=e.maxReductions||1,this.launchOverheadUs=e.launchOverheadUs||5,this.minMemorySavings=e.minMemorySavings||0}run(e){let t=[];for(let u of e.ops())u.opName==="fusion"&&t.push(u);if(t.length<2)return oe.UNCHANGED;let r=new Set(t),s=this._buildProducerConsumerEdges(t,r);if(s.length===0)return oe.UNCHANGED;let o=!1,i=0,a=new Set;for(let{producer:u,consumer:p,sharedResults:c}of s)a.has(u)||a.has(p)||this._canMerge(u,p)&&this._shouldMerge(u,p,c)&&(this._mergeCreatesCycle(u,p,r)||(this._merge(u,p,c),a.add(u),a.add(p),o=!0,i++));return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:t.length,edges:s.length,mergeCount:i,level:le.DEBUG}),o?oe.CHANGED:oe.UNCHANGED}_buildProducerConsumerEdges(e,t){let r=[],s=new Map;for(let o of e)for(let i=0;i<o.numResults;i++){let a=o.getResult(i);for(let u of a.uses()){if(!t.has(u.user))continue;let p=u.user;if(p===o)continue;let c=`${o.id}|${p.id}`,f=s.get(c);f||(f={producer:o,consumer:p,sharedResults:new Map},s.set(c,f),r.push(f)),f.sharedResults.set(i,u.operandIndex)}}return r}_canMerge(e,t){let r=xc(e),s=xc(t);if(r+s>this.maxFusionSize)return!1;let o=0;return o+=Sc(e),o+=Sc(t),!(o>this.maxReductions||!vc(e)||!vc(t))}_shouldMerge(e,t,r){let s=0;for(let[u]of r){let p=e.getResult(u);if(p.type instanceof I){let c=p.type.sizeInBytes();c!==X&&(s+=c)}}let o=!0;for(let u=0;u<e.numResults;u++){let p=e.getResult(u);for(let c of p.uses())if(c.user!==t){o=!1;break}if(!o)break}if(o)return!0;let i=this._estimateRecomputeCost(e);return s+this.launchOverheadUs*1e3>i}_mergeCreatesCycle(e,t,r){let s=new Set,o=[];for(let a=0;a<t.numOperands;a++){let p=t.getOperand(a).definingOp;p&&p!==e&&p.opName!=="constant"&&(s.has(p)||(s.add(p),o.push(p)))}let i=0;for(;i<o.length;){let a=o[i++];if(a===e)return!0;for(let u=0;u<a.numOperands;u++){let p=a.getOperand(u).definingOp;p&&!s.has(p)&&(s.add(p),o.push(p))}}return!1}_merge(e,t,r){let s=e.regions[0].entryBlock,o=t.regions[0].entryBlock,i=Tc(s),a=Tc(o);if(!i||!a)return;let u=new Map;for(let[N,k]of r)u.set(k,N);let p=[],c=new Map;for(let N=0;N<e.numOperands;N++)c.set(N,p.length),p.push(e.getOperand(N));let f=new Map;for(let N=0;N<t.numOperands;N++){if(u.has(N))continue;let k=t.getOperand(N),O=-1;for(let R=0;R<p.length;R++)if(p[R]===k){O=R;break}O>=0?f.set(N,O):(f.set(N,p.length),p.push(k))}let d=p.map(N=>N.type),h=new qt,m=new On(d);h.addBlock(m);let g=new Map;for(let N=0;N<s.arguments.length;N++)g.set(s.arguments[N],m.arguments[c.get(N)]);for(let N of s.ops())N.opName!=="yield"&&m.pushOp(N.clone(g));for(let N=0;N<i.numOperands;N++){let k=i.getOperand(N),O=g.get(k)||k;for(let[R,B]of u)B===N&&g.set(o.arguments[R],O)}for(let N=0;N<o.arguments.length;N++){if(u.has(N))continue;let k=f.get(N);k!==void 0&&g.set(o.arguments[N],m.arguments[k])}for(let N of o.ops())N.opName!=="yield"&&m.pushOp(N.clone(g));let y=[],_=[],b=[];for(let N=0;N<e.numResults;N++){let k=!1;for(let O of e.getResult(N).uses())if(O.user!==t){k=!0;break}if(k){let O=i.getOperand(N),R=g.get(O)||O;b.push({resultIdx:N,value:R}),y.push(R),_.push(e.getResult(N).type)}}for(let N=0;N<t.numResults;N++){let k=a.getOperand(N),O=g.get(k)||k;y.push(O),_.push(t.getResult(N).type)}let w=new Me("yield",y,[]);m.pushOp(w);let x=[];for(let N of m.ops())N.opName!=="yield"&&x.push(N);let S=wc(x),T=new Me("fusion",p,_,{fusion_kind:S},[h]),v=t;if(!v.parentBlock)return;v.parentBlock.insertBefore(T,v);let A=0;for(let{resultIdx:N}of b)e.getResult(N).replaceAllUsesWith(T.getResult(A)),A++;for(let N=0;N<t.numResults;N++)t.getResult(N).replaceAllUsesWith(T.getResult(A)),A++;e.dropAllOperands(),e.parentBlock&&e.parentBlock.removeOp(e),t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t)}_estimateRecomputeCost(e){let t=0,r=e.regions[0]?.entryBlock;if(!r)return 0;for(let s of r.ops()){if(s.opName==="yield")continue;let o=ne.get(s.opName);if(o&&o.getFlops)t+=o.getFlops(s);else for(let i=0;i<s.numResults;i++){let a=s.getResult(i).type;if(a instanceof I){let u=a.numel();u!==X&&(t+=u);break}}}return t}}});function k6(n,e){let t=Math.min(n.id,e.id),r=Math.max(n.id,e.id);return`${t}|${r}`}var Ad,ME=M(()=>{"use strict";fn();yr();Ea();Re();Bu();nn();Nc();Ad=class extends Ue{static{l(this,"MultiOutputFusionPass")}constructor(e={}){super("MultiOutputFusionPass"),this.maxFusionSize=e.maxFusionSize||512,this.maxReductions=e.maxReductions||1,this.minSharedInputBytes=e.minSharedInputBytes||0,this.maxOutputs=e.maxOutputs||8,this.maxConsumersPerInput=e.maxConsumersPerInput||64}run(e){let t=[];for(let a of e.ops())a.opName==="fusion"&&t.push(a);if(t.length<2)return oe.UNCHANGED;let r=this._findCandidates(t);if(r.length===0)return oe.UNCHANGED;let s=!1,o=0,i=new Set;for(let{left:a,right:u,sharedInputs:p,sharedBytes:c}of r)i.has(a)||i.has(u)||this._canMerge(a,u)&&(this._mergeCreatesCycle(a,u)||(this._mergeMultiOutput(a,u,p),i.add(a),i.add(u),s=!0,o++));return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:t.length,candidates:r.length,mergeCount:o,level:le.DEBUG}),s?oe.CHANGED:oe.UNCHANGED}_findCandidates(e){let t=new Map;for(let o of e)for(let i=0;i<o.numOperands;i++){let u=o.getOperand(i).id,p=t.get(u);p||(p=[],t.set(u,p)),p.push(o)}let r=new Map;for(let[o,i]of t){if(i.length<2)continue;let a=Math.min(i.length,this.maxConsumersPerInput);for(let u=0;u<a;u++)for(let p=u+1;p<a;p++){let c=i[u],f=i[p];if(c===f)continue;let d=k6(c,f),h=r.get(d);h||(h={left:c,right:f,sharedInputs:new Set,sharedBytes:0},r.set(d,h)),h.sharedInputs.add(o)}}let s=[];for(let o of r.values()){let i=0;for(let a of o.sharedInputs){let u=t.get(a);if(!u||u.length===0)continue;let p=u[0];for(let c=0;c<p.numOperands;c++){let f=p.getOperand(c);if(f.id===a&&f.type instanceof I){let d=f.type.sizeInBytes();d!==X&&(i+=d);break}}}o.sharedBytes=i,i>=this.minSharedInputBytes&&s.push(o)}return s.sort((o,i)=>i.sharedBytes-o.sharedBytes),s}_canMerge(e,t){if(this._hasProducerConsumerEdge(e,t)||this._hasProducerConsumerEdge(t,e))return!1;let r=xc(e),s=xc(t);if(r+s>this.maxFusionSize||e.numResults+t.numResults>this.maxOutputs)return!1;let i=0;return i+=Sc(e),i+=Sc(t),!(i>this.maxReductions||!vc(e)||!vc(t))}_mergeCreatesCycle(e,t){let r=new Set,s=[];for(let i=0;i<e.numResults;i++){let a=e.getResult(i);for(let u of a.uses())u.user!==t&&!r.has(u.user)&&(r.add(u.user),s.push(u.user))}let o=0;for(;o<s.length;){let i=s[o++];if(i===t)return!0;for(let a=0;a<i.numResults;a++)for(let u of i.getResult(a).uses())r.has(u.user)||(r.add(u.user),s.push(u.user))}r.clear(),s.length=0;for(let i=0;i<t.numResults;i++){let a=t.getResult(i);for(let u of a.uses())u.user!==e&&!r.has(u.user)&&(r.add(u.user),s.push(u.user))}for(o=0;o<s.length;){let i=s[o++];if(i===e)return!0;for(let a=0;a<i.numResults;a++)for(let u of i.getResult(a).uses())r.has(u.user)||(r.add(u.user),s.push(u.user))}return!1}_mergeMultiOutput(e,t,r){let s=e.regions[0].entryBlock,o=t.regions[0].entryBlock,i=Tc(s),a=Tc(o);if(!i||!a)return;let u=[],p=new Map,c=new Map;for(let k=0;k<e.numOperands;k++){let O=e.getOperand(k),R=O.id,B=p.get(R);B===void 0&&(B=u.length,p.set(R,B),u.push(O)),c.set(k,B)}let f=new Map;for(let k=0;k<t.numOperands;k++){let O=t.getOperand(k),R=O.id,B=p.get(R);B===void 0&&(B=u.length,p.set(R,B),u.push(O)),f.set(k,B)}let d=u.map(k=>k.type),h=new qt,m=new On(d);h.addBlock(m);let g=new Map;for(let k=0;k<s.arguments.length;k++)g.set(s.arguments[k],m.arguments[c.get(k)]);for(let k of s.ops())k.opName!=="yield"&&m.pushOp(k.clone(g));for(let k=0;k<o.arguments.length;k++)g.set(o.arguments[k],m.arguments[f.get(k)]);for(let k of o.ops())k.opName!=="yield"&&m.pushOp(k.clone(g));let y=[],_=[];for(let k=0;k<i.numOperands;k++){let O=i.getOperand(k);y.push(g.get(O)||O),_.push(e.getResult(k).type)}for(let k=0;k<a.numOperands;k++){let O=a.getOperand(k);y.push(g.get(O)||O),_.push(t.getResult(k).type)}let b=new Me("yield",y,[]);m.pushOp(b);let w=[];for(let k of m.ops())k.opName!=="yield"&&w.push(k);let x=wc(w),S=new Me("fusion",u,_,{fusion_kind:x},[h]),T=e.parentBlock;if(!T)return;let v=iv(T),A=null;for(let k of u){let O=k.definingOp;!O||O===e||O===t||(!A||!v(O,A))&&(A=O)}A&&A.parentBlock===T?T.insertAfter(S,A):T.insertBefore(S,e);for(let k=0;k<e.numResults;k++)e.getResult(k).replaceAllUsesWith(S.getResult(k));let N=e.numResults;for(let k=0;k<t.numResults;k++)t.getResult(k).replaceAllUsesWith(S.getResult(N+k));e.dropAllOperands(),e.parentBlock&&e.parentBlock.removeOp(e),t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t)}_hasProducerConsumerEdge(e,t){for(let r=0;r<e.numResults;r++)for(let s of e.getResult(r).uses())if(s.user===t)return!0;return!1}};l(k6,"pairKey")});var av,ft,ro=M(()=>{"use strict";av=class{static{l(this,"UseDefResult")}constructor(e,t,r,s,o){this.topologicalOrder=e,this.valueToOp=t,this.opUsers=r,this.depth=s,this.height=o}},ft=class{static get name(){return"use_def"}static get depKey(){return"useDef"}static get dependencies(){return[]}static compute(e){let t=[],r=new Map,s=new Map,o=new Set,i=new Set,a=l(c=>{if(o.has(c))return;i.add(c);let f=[{op:c,i:0}];for(;f.length>0;){let d=f[f.length-1],h=d.op;if(d.i<h.numOperands){let m=h.getOperand(d.i).definingOp;if(d.i++,m&&!o.has(m)){if(i.has(m))throw new Error("Cycle detected in UseDefAnalysis");i.add(m),f.push({op:m,i:0})}continue}i.delete(h),o.add(h),t.push(h),f.pop()}},"visit");for(let c of e.ops()){for(let f=0;f<c.numResults;f++)r.set(c.getResult(f),c);s.set(c,new Set)}let u=new Map,p=new Map;for(let c of e.ops())a(c);for(let c of t){let f=0;for(let d=0;d<c.numOperands;d++){let h=c.getOperand(d).definingOp;if(h){s.get(h).add(c);let m=u.get(h)||0;m+1>f&&(f=m+1)}}u.set(c,f)}for(let c=t.length-1;c>=0;c--){let f=t[c],d=0,h=s.get(f)||new Set;for(let m of h){let g=p.get(m)||0;g+1>d&&(d=g+1)}p.set(f,d)}return new av(t,r,s,u,p)}}});var uv,X_,IE=M(()=>{"use strict";ro();Ir();uv=class{static{l(this,"DominanceResult")}constructor(e){this.idom=e}postDominates(e,t){let r=t;for(;r;){if(r===e)return!0;r=this.idom.get(r)}return!1}immediatePDom(e){return this.idom.get(e)||null}pathToPDom(e){let t=[],r=this.idom.get(e);for(;r&&(t.push(r),r!==this.idom.get(r));)r=this.idom.get(r);return t}},X_=class{static get name(){return"post_dominance"}static get depKey(){return"postDominance"}static get dependencies(){return[ft]}static compute(e,t={}){let s=(t.useDef||ft.compute(e)).topologicalOrder,o=new Map;for(let m of s)o.set(m,[]);for(let m of s)for(let g=0;g<m.numResults;g++){let y=m.getResult(g);for(let _ of y.uses()){let b=_.user;o.has(b)&&o.get(m).push(b)}}let i=new Set;for(let m of s)Xt(m.opName)&&i.add(m);let a={opName:"__pdom_root__"},u=Math.max(1,Math.ceil(Math.log2(s.length+2))+1),p=new Map,c=new Map,f=new Map;c.set(a,0),f.set(a,new Array(u).fill(a));let d=l((m,g)=>{p.set(m,g),c.set(m,c.get(g)+1);let y=new Array(u);y[0]=g;for(let _=1;_<u;_++)y[_]=f.get(y[_-1])[_-1];f.set(m,y)},"link"),h=l((m,g)=>{if(c.get(m)<c.get(g)){let _=m;m=g,g=_}let y=c.get(m)-c.get(g);for(let _=0;_<u;_++)y>>_&1&&(m=f.get(m)[_]);if(m===g)return m;for(let _=u-1;_>=0;_--)f.get(m)[_]!==f.get(g)[_]&&(m=f.get(m)[_],g=f.get(g)[_]);return f.get(m)[0]},"lca");for(let m=s.length-1;m>=0;m--){let g=s[m];if(i.has(g)){d(g,a);continue}let y=null;for(let _ of o.get(g))p.has(_)&&(y=y===null?_:h(y,_));y!==null&&d(g,y)}for(let[m,g]of p)g===a&&p.delete(m);return new uv(p,s)}}});function EE(n){return Xt(n)||_n(n)}var eb,LE=M(()=>{"use strict";fn();Ct();Bu();K_();H_();IE();nn();ro();Nc();Ir();l(EE,"isSkipOp");eb=class extends Ue{static{l(this,"DominatorFusionPass")}constructor(e={}){super("DominatorFusionPass"),this.requiredAnalyses=[ft];let t=e.target||{};this.maxFusionSize=t.maxFusionSize||e.maxFusionSize||512,this.maxReductions=e.maxReductions||1,this.libraryOps=t.libraryOps||e.libraryOps||new Set,this.costModel=new fa({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,...e.cost})}run(e,t){let r=t?t.getAnalysis(ft,e):ft.compute(e),s=X_.compute(e,{useDef:r}),o=r.topologicalOrder,i=this._buildGroups(o,s);if(i.length===0)return oe.UNCHANGED;let a=[];for(let u of i){if(!u.allOpsInlineFusable()){this._explain(u,!1,"group contains ops without inline fusion support");continue}let p=this.costModel.shouldFuse(u);this._explain(u,p.fuse,p.reason),p.fuse&&a.push(u)}if(a.length===0)return oe.UNCHANGED;for(let u of a)kc(u,We.ELEMENTWISE);return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,groupsBuilt:i.length,groupsFused:a.length,level:le.DEBUG}),oe.CHANGED}_explain(e,t,r){if(!this.trace||!this.trace.explainsEnabled)return;let s=e.ops.map(o=>o.opName);this.trace.explain("fusion",s.join("+"),t?"fused":"not-fused",r||null,{groupSize:s.length,strategy:"dominator"})}_buildGroups(e,t){let r=new Map,s=[],o=0;for(let p=e.length-1;p>=0;p--){let c=e[p];if(EE(c.opName))continue;let f=Ou(c);if(f===We.OPAQUE||this.libraryOps.has(c.opName))continue;let d=t.immediatePDom(c);if(!d||EE(d.opName))continue;let h=Ou(d);if(h===We.OPAQUE||this.libraryOps.has(d.opName)||!this._canFusePatterns(f,h)||!this._pathAllFusable(c,d,e,t))continue;let m=r.get(d);if(m){if(m.size+1>this.maxFusionSize||!this._checkReductionLimit(m,c))continue;m.addOp(c),r.set(c,m),this._absorbIntermediates(c,d,m,r,e,t)}else{let g=r.get(c);if(g){if(g.size+1>this.maxFusionSize||!this._checkReductionLimit(g,d))continue;g.addOp(d),r.set(d,g),this._absorbIntermediates(c,d,g,r,e,t)}else{let y=new wi(o++);if(y.addOp(c),y.addOp(d),!this._checkGroupReductions(y))continue;r.set(c,y),r.set(d,y),s.push(y),this._absorbIntermediates(c,d,y,r,e,t)}}}let i=new Set,a=[];for(let p of s)i.has(p)||p.size<2||(i.add(p),p.classifyKind(),a.push(p));for(let p of r.values())i.has(p)||p.size<2||(i.add(p),p.classifyKind(),a.push(p));let u=[];for(let p of a){if(!this._checkGroupReductions(p)){this._explain(p,!1,`group exceeds the ${this.maxReductions}-reduction limit`);continue}if(this._createsCycle(p)){this._explain(p,!1,"fusing would create a dependency cycle");continue}u.push(p)}return u}_createsCycle(e){let t=new Set;for(let o of e.getInputValues())o.definingOp&&!e.hasOp(o.definingOp)&&t.add(o.definingOp);if(t.size===0)return!1;let r=new Set,s=[];for(let o of e.getOutputValues())for(let i of o.uses())e.hasOp(i.user)||s.push(i.user);for(;s.length>0;){let o=s.pop();if(!r.has(o)){if(r.add(o),t.has(o))return!0;for(let i=0;i<o.numResults;i++)for(let a of o.getResult(i).uses())e.hasOp(a.user)||s.push(a.user)}}return!1}_canFusePatterns(e,t){return sv(e,t)}_pathAllFusable(e,t,r,s){let o=new Set,i=[e];for(o.add(e),o.add(t);i.length>0;){let a=i.pop();for(let u=0;u<a.numResults;u++)for(let p of a.getResult(u).uses()){let c=p.user;if(c===t||o.has(c)||!s.postDominates(t,c))continue;if(o.add(c),Ou(c)===We.OPAQUE||this.libraryOps.has(c.opName))return!1;i.push(c)}}return!0}_absorbIntermediates(e,t,r,s,o,i){let a=new Set,u=[e];for(a.add(e),a.add(t);u.length>0;){let p=u.pop();for(let c=0;c<p.numResults;c++)for(let f of p.getResult(c).uses()){let d=f.user;d===t||a.has(d)||!i.postDominates(t,d)||(a.add(d),Ou(d)===We.OPAQUE||this.libraryOps.has(d.opName))||(r.size<this.maxFusionSize&&this._checkReductionLimit(r,d)&&(r.addOp(d),s.set(d,r)),u.push(d))}}}_checkGroupReductions(e){let t=0;for(let r of e.ops){let s=ne.get(r.opName);s&&s.isReduction&&t++}return t<=this.maxReductions}_checkReductionLimit(e,t){let r=ne.get(t.opName);if(!r||!r.isReduction)return!0;let s=0;for(let o of e.ops){let i=ne.get(o.opName);i&&i.isReduction&&s++}return s<this.maxReductions}}});var tb,CE=M(()=>{"use strict";tb=class{static{l(this,"GraphCycles")}constructor(e,t){this._n=e,this._parent=new Int32Array(e),this._rank=new Int32Array(e),this._nodeAtRank=new Int32Array(e),this._out=new Array(e),this._in=new Array(e);for(let r=0;r<e;r++)this._parent[r]=r,this._rank[r]=r,this._nodeAtRank[r]=r,this._out[r]=new Set,this._in[r]=new Set;if(t)for(let[r,s]of t)r!==s&&(this._out[r].add(s),this._in[s].add(r))}find(e){let t=this._parent,r=e;for(;t[r]!==r;)r=t[r];for(;t[e]!==r;){let s=t[e];t[e]=r,e=s}return r}wouldCreateCycle(e,t){let r=this.find(e),s=this.find(t);if(r===s)return!1;let o=this._rank[r]<this._rank[s]?r:s,i=o===r?s:r;return this._hasIntermediatePath(o,i)}_hasIntermediatePath(e,t){let r=this._rank[t],s=new Set([e]),o=[e];for(;o.length>0;){let i=o.pop();for(let a of this._out[i]){let u=this.find(a);if(!(u===i||u===e)){if(u===t){if(i!==e)return!0;continue}this._rank[u]>=r||s.has(u)||(s.add(u),o.push(u))}}}return!1}merge(e,t){let r=this.find(e),s=this.find(t);if(r===s)return r;let o=this._rank[r]<this._rank[s]?r:s,i=o===r?s:r,a=this._rank[o],u=this._rank[i],p=this._out[r].size+this._in[r].size,c=this._out[s].size+this._in[s].size,f=p>=c?r:s,d=f===r?s:r,h=this._out[f],m=this._in[f];for(let g of this._in[d]){let y=this.find(g);y===f||y===d||(this._out[y].delete(d),this._out[y].add(f),m.add(y))}for(let g of this._out[d]){let y=this.find(g);y===f||y===d||(this._in[y].delete(d),this._in[y].add(f),h.add(y))}return h.delete(d),m.delete(d),h.delete(f),m.delete(f),this._parent[d]=f,this._nodeAtRank[this._rank[d]]=-1,this._reorder(a,u),f}_reorder(e,t){let r=[],s=[];for(let c=e;c<=t;c++){let f=this._nodeAtRank[c];if(!(f<0)){if(this.find(f)!==f){this._nodeAtRank[c]=-1;continue}r.push(c),s.push(f)}}if(s.length<=1){for(let c=0;c<s.length;c++)this._rank[s[c]]=r[c],this._nodeAtRank[r[c]]=s[c];return}let o=new Set(s),i=new Map;for(let c of s)i.set(c,0);for(let c of s)for(let f of this._out[c]){let d=this.find(f);d!==c&&o.has(d)&&i.set(d,i.get(d)+1)}let a=[];for(let c of s)i.get(c)===0&&a.push(c);let u=[],p=0;for(;p<a.length;){let c=a[p++];u.push(c);for(let f of this._out[c]){let d=this.find(f);if(d!==c&&o.has(d)){let h=i.get(d)-1;i.set(d,h),h===0&&a.push(d)}}}for(let c=0;c<u.length;c++){let f=u[c],d=r[c];this._rank[f]=d,this._nodeAtRank[d]=f}}}});var nb,DE=M(()=>{"use strict";nb=class{static{l(this,"MaxHeap")}constructor(){this._items=[]}get size(){return this._items.length}isEmpty(){return this._items.length===0}push(e,t){let r=this._items;r.push({priority:e,value:t});let s=r.length-1;for(;s>0;){let o=s-1>>1;if(r[o].priority>=r[s].priority)break;let i=r[o];r[o]=r[s],r[s]=i,s=o}}pop(){let e=this._items,t=e.length;if(t===0)return;let r=e[0],s=e.pop();if(t>1){e[0]=s;let o=e.length,i=0;for(;;){let a=i*2+1,u=a+1,p=i;if(a<o&&e[a].priority>e[p].priority&&(p=a),u<o&&e[u].priority>e[p].priority&&(p=u),p===i)break;let c=e[p];e[p]=e[i],e[i]=c,i=p}}return r.value}}});function N6(n){let e=ne.get(n.opName);return!e||e.isConstant||e.isTerminator||e.isOpaque?!1:e.isReduction?!0:ni(n.opName)}var rb,PE=M(()=>{"use strict";fn();Ct();Re();Bu();K_();H_();ro();CE();DE();Nc();$a();nn();l(N6,"isFusibleOp");rb=class extends Ue{static{l(this,"PriorityFusionPass")}constructor(e={}){super("PriorityFusionPass"),this.requiredAnalyses=[ft];let t=e.target||{};this.maxReductions=e.maxReductions??1,this.legality=new bc({maxFusionSize:t.maxFusionSize||e.maxFusionSize,maxSharedMemory:t.sharedMemoryBytes||e.maxSharedMemory,libraryOps:t.libraryOps||e.libraryOps,allowReductionFusion:e.allowReductionFusion});let r=t.getAttr&&t.getAttr("fusionBenefitWeights")||e.benefitWeights;this.costModel=new fa({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,policy:t.getAttr?t.getAttr("fusionPolicy"):null,benefitWeights:r,...e.cost})}run(e,t){let s=(t?t.getAnalysis(ft,e):ft.compute(e)).topologicalOrder,o=s.length;if(o===0)return oe.UNCHANGED;let i=new Map;for(let T=0;T<o;T++)i.set(s[T],T);let a=[];for(let T=0;T<o;T++){let v=s[T];for(let A=0;A<v.numOperands;A++){let N=v.getOperand(A).definingOp;N&&i.has(N)&&a.push([i.get(N),T])}}let u=new tb(o,a),p=new Int32Array(o),c=new Map;for(let T=0;T<o;T++)if(N6(s[T])){let v=new wi(T);v.addOp(s[T]),c.set(T,v)}let f=new nb,d=l((T,v)=>{let A=new wi(-1);for(let N of T.ops)A.addOp(N);for(let N of v.ops)A.addOp(N);return A},"mergedGroup"),h=l(T=>{if(T.size<2||!T.allOpsInlineFusable())return!1;let v=0;for(let A of T.ops){let N=ne.get(A.opName);N&&N.isReduction&&v++}return v>this.maxReductions||(T.classifyKind(),T.kind===We.OPAQUE)?!1:this.costModel.shouldFuse(T).fuse===!0},"legalMerge"),m=l((T,v)=>{let A=0;for(let N=0;N<v.numOperands;N++){let k=v.getOperand(N);if(k.definingOp===T&&k.type instanceof I){let O=k.type.sizeInBytes();O!==X&&(A+=O)}}return A},"edgeBytes"),g=l((T,v)=>{let A=u.find(i.get(T)),N=u.find(i.get(v));if(A===N||!c.has(A)||!c.has(N)||!this.legality.canFuse(T,v).legal)return;let k=this.costModel.edgeBenefit(m(T,v));f.push(k,{a:A,b:N,va:p[A],vb:p[N],prodOp:T,consOp:v})},"pushCandidate"),y=l((T,v)=>{let A=new Set;for(let N of T.ops){for(let k=0;k<N.numResults;k++)for(let O of N.getResult(k).uses()){let R=i.get(O.user);if(R===void 0)continue;let B=u.find(R);if(B===v||!c.has(B))continue;let E=v*o+B;A.has(E)||(A.add(E),g(N,O.user))}for(let k=0;k<N.numOperands;k++){let O=N.getOperand(k).definingOp;if(!O)continue;let R=i.get(O);if(R===void 0)continue;let B=u.find(R);if(B===v||!c.has(B))continue;let E=B*o+v;A.has(E)||(A.add(E),g(O,N))}}},"reEval"),_=new Set;for(let T=0;T<o;T++){if(!c.has(T))continue;let v=s[T];for(let A=0;A<v.numOperands;A++){let N=v.getOperand(A).definingOp;if(!N)continue;let k=i.get(N);if(k===void 0||!c.has(k))continue;let O=k*o+T;_.has(O)||(_.add(O),g(N,v))}}let b=0;for(;!f.isEmpty();){let T=f.pop(),v=u.find(T.a),A=u.find(T.b);if(v===A||p[v]!==T.va||p[A]!==T.vb)continue;let N=c.get(v),k=c.get(A);if(!N||!k||!this.legality.canFuse(T.prodOp,T.consOp).legal)continue;let O=d(N,k);if(!h(O)||u.wouldCreateCycle(v,A))continue;let R=u.merge(v,A),B=N.size>=k.size?N:k,E=B===N?k:N;B.merge(E),c.delete(v),c.delete(A),c.set(R,B),p[R]=(p[v]>p[A]?p[v]:p[A])+1,b++,y(B,R)}if(b===0)return oe.UNCHANGED;let w=[],x=new Set;for(let T of c.values())x.has(T)||T.size<2||(x.add(T),w.push(T));let S=0;for(let T of w)T.classifyKind(),this._explain(T),kc(T,We.ELEMENTWISE)&&S++;return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,ops:o,merges:b,fusions:S,level:le.DEBUG}),S>0?oe.CHANGED:oe.UNCHANGED}_explain(e){if(!this.trace||!this.trace.explainsEnabled)return;let t=e.ops.map(r=>r.opName);this.trace.explain("fusion",t.join("+"),"fused",null,{groupSize:t.length,strategy:"priority"})}}});function A6(n,e){return n===e?!0:!n||!e?!1:typeof n.equals=="function"?n.equals(e):!1}var da,sb,$E=M(()=>{"use strict";Re();da=class{static{l(this,"LayoutPreference")}constructor(e,t,r=0){this.inputs=e,this.outputs=t,this.cost=r}},sb=class{static{l(this,"LayoutPolicy")}constructor(e){this.target=e,this._rules=new Map,this._initDefaultRules()}registerRule(e,t){this._rules.set(e,t)}getPreference(e){let t=this._rules.get(e.opName);return t?t(e,this.target):null}estimateConversionCost(e,t,r){if(!(r instanceof I)||A6(e,t))return 0;let s=r.numel();return s<0?1024:s*2}estimateBenefit(e,t,r){if(!(t instanceof I))return 0;let s=t.numel();if(s<0)return 0;let o=e.opName;if(o==="dot"||o==="conv"||o==="matmul")return s*4*r;if(o==="reduce")return s*2*r;let i=this.target.cacheLineBytes||64;return s*4<=i*4?0:Math.floor(s*.5)}_initDefaultRules(){this._rules.set("conv",(e,t)=>{let s=e.getOperand(0).type?.rank||4;if(t.preferredConvLayout)return new da([t.preferredConvLayout,null],[t.preferredConvLayout]);if(t.isGPU()&&s===4){let o=new Gt([0,2,3,1]);return new da([o,null],[o])}if(t.isCPU()&&s===4){let o=new Gt([0,2,3,1]);return new da([o,null],[o])}return null}),this._rules.set("dot",(e,t)=>{let r=e.getOperand(0).type,s=e.getOperand(1).type;if(!r||!s)return null;let o=Gt.rowMajor(r.rank);if(t.isCPU()&&s.rank===2){let a=Gt.columnMajor(s.rank);return new da([o,a],[o])}let i=Gt.rowMajor(s.rank);return new da([o,i],[o])}),this._rules.set("reduce",(e,t)=>{let r=e.getResult(0).type;return r?new da([null],[Gt.rowMajor(r.rank)]):null})}};l(A6,"layoutEquals")});function FE(n,e){let t=new Map,r=null,s=0;for(let i=0;i<n.numOperands;i++){let a=n.getOperand(i),u=e.get(a);if(!u)continue;let p=u.hash?u.hash():0,c=(t.get(p)||0)+1;t.set(p,c),c>s&&(s=c,r=u)}if(r)return r;let o=n.getResult(0);return o&&o.type instanceof I?Gt.rowMajor(o.type.rank):Gt.rowMajor(1)}function O6(n,e){return n===e?!0:!n||!e?!1:typeof n.equals=="function"?n.equals(e):!1}function zE(n){if(n instanceof Gt)return n;if(n&&typeof n.toLayout=="function")try{return n.toLayout()}catch{}return n&&n.baseOrder&&!n.isBlocked?.()?new Gt(n.baseOrder):n&&n.order?new Gt(n.order):null}var lv,ob,VE=M(()=>{"use strict";ro();Re();Ct();Bn();Ir();lv=class{static{l(this,"LayoutAnalysisResult")}constructor(e,t,r){this.assignments=e,this.conversions=t,this.totalCost=r}},ob=class{static get name(){return"layout"}static get depKey(){return"layout"}static get dependencies(){return[ft]}static compute(e,t,r){let s=t.useDef,o=new Map,i=s.topologicalOrder;for(let p of e.args)p.type instanceof I&&o.set(p,p.type.layout||Gt.rowMajor(p.type.rank));for(let p=0;p<i.length;p++){let c=i[p];if(Xt(c.opName))continue;let f=r?r.getPreference(c):null;if(f&&f.outputs.length>0){for(let m=0;m<c.numResults;m++){let g=c.getResult(m);if(!(g.type instanceof I))continue;let y=f.outputs[m]||null;y?o.set(g,y):o.set(g,FE(c,o))}continue}let d=ne.get(c.opName),h=d&&d.hasTrait(Y.ELEMENTWISE);for(let m=0;m<c.numResults;m++){let g=c.getResult(m);g.type instanceof I&&(h?o.set(g,FE(c,o)):o.set(g,Gt.rowMajor(g.type.rank)))}}let a=[],u=0;for(let p=0;p<i.length;p++){let c=i[p],f=r?r.getPreference(c):null;if(f)for(let d=0;d<c.numOperands;d++){let h=c.getOperand(d);if(!(h.type instanceof I))continue;let m=o.get(h);if(!m)continue;let g=f.inputs[d];if(!g||O6(m,g))continue;let y=zE(m),_=zE(g);!y||!_||y.equals(_)||(a.push({value:h,consumer:c,operandIdx:d,from:y,to:_}),r&&(u+=r.estimateConversionCost(y,_,h.type)))}}return new lv(o,a,u)}};l(FE,"resolveFromInputs");l(O6,"layoutEquals");l(zE,"toPlainLayout")});function B6(n,e,t){let r=n.id,s=e.hash?e.hash():0,o=t.hash?t.hash():0;return`${r}:${s}:${o}`}var ib,GE=M(()=>{"use strict";fn();yr();Re();$E();VE();ro();nn();ib=class extends Ue{static{l(this,"LayoutTransformPass")}constructor(e={}){super("LayoutTransformPass"),this.target=e.target||null,this._policy=null}run(e,t){if(!this.target)return oe.UNCHANGED;this._policy||(this._policy=new sb(this.target));let r=t?t.getAnalysis(ft,e):ft.compute(e),s=ob.compute(e,{useDef:r},this._policy);if(s.conversions.length===0)return oe.UNCHANGED;let o=new Map;for(let p of s.conversions){let{value:c,consumer:f,operandIdx:d,from:h,to:m}=p,g=B6(c,h,m),y=o.get(g);y||(y={value:c,from:h,to:m,consumers:[],cost:this._policy.estimateConversionCost(h,m,c.type),benefit:0},o.set(g,y)),y.consumers.push({consumer:f,operandIdx:d});let _=this.target.layoutAwareOps&&this.target.layoutAwareOps.has(f.opName);y.benefit+=_?this._policy.estimateBenefit(f,c.type,1):0}let i=0,a=0,u=[];for(let p of o.values())p.benefit<p.cost||(u.push(p),i+=p.cost,a+=p.benefit);if(u.length===0||i>a)return oe.UNCHANGED;for(let p of u){let c=p.from instanceof Gt?p.from.order:Array.from({length:p.value.type.rank},(y,_)=>_),f=p.to instanceof Gt?p.to.order:Array.from({length:p.value.type.rank},(y,_)=>_),d=new I(p.value.type.shape,p.value.type.dtype,p.to),h=new Me("layout_transform",[p.value],[d],{src_layout:[...c],dst_layout:[...f]}),m=p.value.definingOp;m&&m.parentBlock?m.parentBlock.insertAfter(h,m):p.consumers[0].consumer.parentBlock&&p.consumers[0].consumer.parentBlock.insertBefore(h,p.consumers[0].consumer);let g=h.getResult(0);for(let y of p.consumers)y.consumer.replaceOperand(y.operandIdx,g)}return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,conversions:s.conversions.length,uniqueTransforms:u.length,level:le.DEBUG}),oe.CHANGED}};l(B6,"valueLayoutKey")});function jE(n,e){if(n===e)return!0;if(typeof n=="number"&&typeof e=="number")return n===e;if(typeof n!=typeof e||n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(n[t]!==e[t])return!1;return!0}function qE(n){let e=0;for(let t=0;t<n.length;t++)e=(e<<5)-e+n.charCodeAt(t)&2147483647;return e}function I6(n){if(typeof n=="number"){let t=new Float64Array([n]),r=new Uint32Array(t.buffer);return(r[0]^r[1])&2147483647}let e=n.length;for(let t=0;t<Math.min(n.length,8);t++){let r=new Float64Array([n[t]]),s=new Uint32Array(r.buffer);e=(e^(s[0]^s[1]))*16777619&2147483647}return e}var Pr,R6,M6,vr,cv=M(()=>{"use strict";Re();Pr=Object.freeze({PER_TENSOR_SYMMETRIC:"per_tensor_symmetric",PER_TENSOR_ASYMMETRIC:"per_tensor_asymmetric",PER_CHANNEL:"per_channel",PER_GROUP:"per_group"}),R6=new Set(Object.values(Pr)),M6=new Set(["i8","ui8"]),vr=class n{static{l(this,"QuantizationParams")}constructor(e){this.scheme=e.scheme,this.scale=e.scale,this.zeroPoint=e.zeroPoint,this.axis=e.axis??null,this.groupSize=e.groupSize??null,this.dtype=e.dtype||"i8",this.numBits=e.numBits||fo(this.dtype)*8,this._hash=null}clampRange(){if(this.isSymmetric()){let r=2**(this.numBits-1)-1;return[-r,r]}if(this.dtype==="ui8")return[0,2**this.numBits-1];let e=-(2**(this.numBits-1)),t=2**(this.numBits-1)-1;return[e,t]}quantize(e){let[t,r]=this.clampRange(),s=Math.round(e/this.getScalarScale()+this.getScalarZeroPoint());return Math.max(t,Math.min(r,s))}dequantize(e){return(e-this.getScalarZeroPoint())*this.getScalarScale()}quantizeArray(e){let[t,r]=this.clampRange(),s=new Array(e.length);if(this.isPerChannel())throw new Error("Use quantizeArrayPerChannel(floatArr, shape) for per-channel quantization");let o=this.getScalarScale(),i=this.getScalarZeroPoint();for(let a=0;a<e.length;a++)s[a]=Math.max(t,Math.min(r,Math.round(e[a]/o+i)));return s}dequantizeArray(e){let t=new Array(e.length);if(this.isPerChannel())throw new Error("Use dequantizeArrayPerChannel(intArr, shape) for per-channel dequantization");let r=this.getScalarScale(),s=this.getScalarZeroPoint();for(let o=0;o<e.length;o++)t[o]=(e[o]-s)*r;return t}_channelStride(e){let t=1;for(let r=this.axis+1;r<e.length;r++)t*=e[r];return t}quantizeArrayPerChannel(e,t){let[r,s]=this.clampRange(),o=this._channelStride(t),i=t[this.axis],a=new Array(e.length);for(let u=0;u<e.length;u++){let p=Math.floor(u/o)%i,c=this.getScaleForChannel(p),f=this.getZeroPointForChannel(p);a[u]=Math.max(r,Math.min(s,Math.round(e[u]/c+f)))}return a}dequantizeArrayPerChannel(e,t){let r=this._channelStride(t),s=t[this.axis],o=new Array(e.length);for(let i=0;i<e.length;i++){let a=Math.floor(i/r)%s;o[i]=(e[i]-this.getZeroPointForChannel(a))*this.getScaleForChannel(a)}return o}quantizeArrayPerGroup(e){let[t,r]=this.clampRange(),s=new Array(e.length);for(let o=0;o<e.length;o++){let i=Math.floor(o/this.groupSize),a=this.scale[i],u=this.zeroPoint[i];s[o]=Math.max(t,Math.min(r,Math.round(e[o]/a+u)))}return s}dequantizeArrayPerGroup(e){let t=new Array(e.length);for(let r=0;r<e.length;r++){let s=Math.floor(r/this.groupSize);t[r]=(e[r]-this.zeroPoint[s])*this.scale[s]}return t}getScaleForGroup(e){return this.scale[e]}getZeroPointForGroup(e){return this.zeroPoint[e]}getScaleForChannel(e){return this.isPerChannel()?this.scale[e]:this.getScalarScale()}getZeroPointForChannel(e){return this.isPerChannel()?this.zeroPoint[e]:this.getScalarZeroPoint()}getScalarScale(){return typeof this.scale=="number"?this.scale:this.scale[0]}getScalarZeroPoint(){return typeof this.zeroPoint=="number"?this.zeroPoint:this.zeroPoint[0]}numChannels(){return this.isPerChannel()?typeof this.scale=="number"?1:this.scale.length:1}isPerChannel(){return this.scheme===Pr.PER_CHANNEL}isPerGroup(){return this.scheme===Pr.PER_GROUP}isSymmetric(){return this.scheme===Pr.PER_TENSOR_SYMMETRIC}equals(e){return this===e?!0:!(!(e instanceof n)||this.scheme!==e.scheme||this.dtype!==e.dtype||this.numBits!==e.numBits||this.axis!==e.axis||this.groupSize!==e.groupSize||!jE(this.scale,e.scale)||!jE(this.zeroPoint,e.zeroPoint))}hash(){if(this._hash!==null)return this._hash;let e=2166136261;return e=(e^qE(this.scheme))*16777619&2147483647,e=(e^qE(this.dtype))*16777619&2147483647,e=(e^this.numBits)*16777619&2147483647,e=(e^I6(this.scale))*16777619&2147483647,this._hash=e,e}serialize(){let e=this.isPerChannel()||this.isPerGroup();return{scheme:this.scheme,scale:e?[...this.scale]:this.scale,zeroPoint:e?[...this.zeroPoint]:this.zeroPoint,axis:this.axis,groupSize:this.groupSize,dtype:this.dtype,numBits:this.numBits}}static deserialize(e){return new n(e)}static fromRange(e,t,r,s="i8",o=8){if(r===Pr.PER_TENSOR_SYMMETRIC){let d=Math.max(Math.abs(e),Math.abs(t)),h=2**(o-1)-1,m=d/h||1e-10;return new n({scheme:r,scale:m,zeroPoint:0,dtype:s,numBits:o})}let[i,a]=s==="ui8"?[0,2**o-1]:[-(2**(o-1)),2**(o-1)-1],p=(t-e||1e-10)/(a-i),c=Math.round(i-e/p),f=Math.max(i,Math.min(a,c));return new n({scheme:r,scale:p,zeroPoint:f,dtype:s,numBits:o})}static fromRangePerChannel(e,t,r,s="i8",o=8){let i=e.length,a=new Float64Array(i),u=new Int32Array(i),p=2**(o-1)-1;for(let c=0;c<i;c++){let f=Math.max(Math.abs(e[c]),Math.abs(t[c]));a[c]=f/p||1e-10,u[c]=0}return new n({scheme:Pr.PER_CHANNEL,scale:a,zeroPoint:u,axis:r,dtype:s,numBits:o})}static defaultForActivation(e,t="i8",r=8){return n.fromRange(-6,6,e,t,r)}static fromConstantArray(e,t,r="i8",s=8){let o=1/0,i=-1/0;for(let a=0;a<e.length;a++)e[a]<o&&(o=e[a]),e[a]>i&&(i=e[a]);return isFinite(o)||(o=-1),isFinite(i)||(i=1),o===i&&(o-=.5,i+=.5),n.fromRange(o,i,t,r,s)}static fromConstantArrayPerChannel(e,t,r,s="i8",o=8){let i=t[r],a=1;for(let c=r+1;c<t.length;c++)a*=t[c];let u=new Array(i).fill(1/0),p=new Array(i).fill(-1/0);for(let c=0;c<e.length;c++){let f=Math.floor(c/a)%i,d=e[c];d<u[f]&&(u[f]=d),d>p[f]&&(p[f]=d)}for(let c=0;c<i;c++)isFinite(u[c])||(u[c]=-1),isFinite(p[c])||(p[c]=1),u[c]===p[c]&&(u[c]-=.5,p[c]+=.5);return n.fromRangePerChannel(u,p,r,s,o)}static fromConstantArrayPerGroup(e,t,r="i8",s=4){let o=Math.ceil(e.length/t),i=new Float64Array(o),a=new Int32Array(o),u=2**(s-1)-1;for(let p=0;p<o;p++){let c=p*t,f=Math.min(c+t,e.length),d=0;for(let h=c;h<f;h++){let m=Math.abs(e[h]);m>d&&(d=m)}i[p]=d/u||1e-10,a[p]=0}return new n({scheme:Pr.PER_GROUP,scale:i,zeroPoint:a,groupSize:t,dtype:r,numBits:s})}static isQuantizableDtype(e){return M6.has(e)}static isValidScheme(e){return R6.has(e)}};l(jE,"scaleEquals");l(qE,"hashStr");l(I6,"hashScaleValue")});function D6(n){for(let e=0;e<n.numOperands;e++){let t=n.getOperand(e).definingOp;if(t&&t.opName==="constant")return!0}return!1}function P6(n,e,t){for(let r=0;r<n.numOperands;r++){let s=n.getOperand(r);if(!e.has(s)&&(!(s.type instanceof I)||!jt(s.type.dtype)))return!1}return n.numOperands>0}var E6,L6,C6,ab,ub,UE=M(()=>{"use strict";fn();yr();Re();Ct();ro();cv();nn();Ir();E6=new Set(["softmax","sqrt","div","rsqrt","log","exp","tanh"]),L6=new Set(["dot","conv","add","mul","sub"]),C6=new Map([["dot","quantized_dot"],["conv","quantized_conv"]]);for(let[n,e]of C6)ne.has(n)&&ne.registerOpAttr(n,"quantizedVariant",e);ab=class{static{l(this,"QuantizationConfig")}constructor(e={}){this.scheme=e.scheme||Pr.PER_TENSOR_SYMMETRIC,this.calibration=e.calibration||null,this.targetDtype=e.targetDtype||"i8",this.excludeOps=e.excludeOps||E6,this.quantizableOps=e.quantizableOps||L6,this.sensitivityThreshold=e.sensitivityThreshold||0,this.sensitivityResult=e.sensitivityResult||null,this.weightOnly=e.weightOnly||!1,this.target=e.target||null}},ub=class extends Ue{static{l(this,"QuantizationPass")}constructor(e={}){super("QuantizationPass"),this.requiredAnalyses=[ft],this.config=e instanceof ab?e:new ab(e)}run(e,t){let s=(t?t.getAnalysis(ft,e):ft.compute(e)).topologicalOrder,o=new Set;this._paramsByValue=new Map;let i=this.config,a=!1;if(i.target&&!i.target.supportsInt8)return oe.UNCHANGED;for(let p=0;p<s.length;p++){let c=s[p];if(Xt(c.opName))continue;if(i.excludeOps.has(c.opName)||!i.quantizableOps.has(c.opName)){for(let h=0;h<c.numOperands;h++)o.has(c.getOperand(h))&&(a=this._insertDequantBefore(c,h,c.getOperand(h),i)||a);continue}if(i.sensitivityResult&&i.sensitivityThreshold>0&&i.sensitivityResult.isSensitive(c,i.sensitivityThreshold)||i.weightOnly&&!D6(c))continue;let f=ne.get(c.opName),d=f?f.getAttr("quantizedVariant"):null;if(d&&P6(c,o,i)){i.scheme===Pr.PER_CHANNEL&&this._canPerChannelDot(c,o)?a=this._replacePerChannelDot(c,i)||a:a=this._replaceWithNativeQuantized(c,d,o,i)||a;continue}a=this._insertDequantQuantBoundary(c,o,i)||a}let u=e.getReturnOp();if(u)for(let p=0;p<u.numOperands;p++){let c=u.getOperand(p);o.has(c)&&(a=this._insertDequantBefore(u,p,c,i)||a)}return this.trace&&this.trace.level>=le.DEBUG&&a&&this.trace.emit({type:"pass_detail",passName:this.name,opsProcessed:s.length,changed:a,level:le.DEBUG}),a?oe.CHANGED:oe.UNCHANGED}_getQuantParams(e,t){let r=fo(t.targetDtype)*8;if(t.calibration&&t.calibration.hasData(e))return t.calibration.getQuantParams(e,t.scheme,t.targetDtype);let s=e.definingOp;if(s&&s.opName==="constant"){let o=s.getAttr("value");if(typeof o=="number")return vr.fromRange(-Math.abs(o)||-1,Math.abs(o)||1,t.scheme,t.targetDtype,r);if(o&&typeof o.length=="number")return vr.fromConstantArray(o,t.scheme,t.targetDtype,r)}return e.type instanceof I&&jt(e.type.dtype)?vr.defaultForActivation(t.scheme,t.targetDtype,r):null}_resolveQuantParams(e,t){let r=this._paramsByValue.get(e);if(r)return r;let s=e.definingOp;return s&&s.opName==="quantize"?new vr({scheme:s.getAttr("scheme")||t.scheme,scale:s.getAttr("scale"),zeroPoint:s.getAttr("zero_point")||0,dtype:s.getAttr("target_dtype")||t.targetDtype}):this._getQuantParams(e,t)}_insertQuantizeAfter(e,t,r){let s=e.getResult(t),o=this._getQuantParams(s,r);if(!o)return null;let i=new I(s.type.shape,r.targetDtype),a=new Me("quantize",[s],[i],{scale:o.getScalarScale(),zero_point:o.getScalarZeroPoint(),scheme:r.scheme,target_dtype:r.targetDtype,...o.axis!==null?{axis:o.axis}:{}});e.parentBlock&&e.parentBlock.insertAfter(a,e);let u=a.getResult(0);return this._paramsByValue.set(u,o),u}_insertDequantBefore(e,t,r,s){let o=this._resolveQuantParams(r,s);if(!o)return!1;let i="f32",a=new I(r.type.shape,i),u=new Me("dequantize",[r],[a],{scale:o.getScalarScale(),zero_point:o.getScalarZeroPoint(),scheme:s.scheme,target_dtype:i});return e.parentBlock&&(e.parentBlock.insertBefore(u,e),e.replaceOperand(t,u.getResult(0))),!0}_insertDequantQuantBoundary(e,t,r){let s=!1;for(let o=0;o<e.numOperands;o++){let i=e.getOperand(o);t.has(i)&&(s=this._insertDequantBefore(e,o,i,r)||s)}for(let o=0;o<e.numResults;o++){let i=e.getResult(o);if(!(i.type instanceof I)||!jt(i.type.dtype))continue;let a=this._insertQuantizeAfter(e,o,r);if(a){let u=[...i.uses()];for(let p of u)p.user!==a.definingOp&&p.user.replaceOperand(p.operandIndex,a);t.add(a),s=!0}}return s}_replaceWithNativeQuantized(e,t,r,s){let o=[],i={};for(let[f,d]of e.attributes||[])i[f]=d;for(let f=0;f<e.numOperands;f++){let d=e.getOperand(f);if(r.has(d)){o.push(d);let _=this._resolveQuantParams(d,s);if(_){let b=f===0?"lhs":"rhs";if(t==="quantized_dot")i[`${b}_scale`]=_.getScalarScale(),i[`${b}_zero_point`]=_.getScalarZeroPoint();else{let w=f===0?"input":"kernel";i[`${w}_scale`]=_.getScalarScale(),i[`${w}_zero_point`]=_.getScalarZeroPoint()}}continue}let h=this._getQuantParams(d,s);if(!h)return!1;let m=new I(d.type.shape,s.targetDtype),g=new Me("quantize",[d],[m],{scale:h.getScalarScale(),zero_point:h.getScalarZeroPoint(),scheme:s.scheme,target_dtype:s.targetDtype});e.parentBlock&&e.parentBlock.insertBefore(g,e),o.push(g.getResult(0));let y=f===0?t==="quantized_dot"?"lhs":"input":t==="quantized_dot"?"rhs":"kernel";i[`${y}_scale`]=h.getScalarScale(),i[`${y}_zero_point`]=h.getScalarZeroPoint()}let a=i.lhs_scale||i.input_scale||1,u=i.rhs_scale||i.kernel_scale||1;i.output_scale=a*u,i.output_zero_point=0;let p=new I(e.getResult(0).type.shape,"i32"),c=new Me(t,o,[p],i);if(e.parentBlock){e.parentBlock.insertBefore(c,e);let f=new I(e.getResult(0).type.shape,e.getResult(0).type.dtype),d=i.output_scale||1,h=new Me("dequantize",[c.getResult(0)],[f],{scale:d,zero_point:i.output_zero_point||0,scheme:s.scheme,target_dtype:e.getResult(0).type.dtype});e.parentBlock.insertBefore(h,e),e.replaceAllResultsWith([h.getResult(0)]),e.erase()}return!0}_canPerChannelDot(e,t){if(e.opName!=="dot")return!1;let r=e.getOperand(0),s=e.getOperand(1);if(t.has(r)||t.has(s)||!(r.type instanceof I)||r.type.shape.length!==2||!(s.type instanceof I)||s.type.shape.length!==2)return!1;let o=s.definingOp;if(!o||o.opName!=="constant")return!1;let i=o.getAttr("value");if(!i||typeof i=="number"||typeof i.length!="number")return!1;let a=e.getAttr("rhs_contracting")||[],u=e.getAttr("lhs_contracting")||[];return!(a.length!==1||u.length!==1||(e.getAttr("rhs_batch")||[]).length!==0||(e.getAttr("lhs_batch")||[]).length!==0)}_activationParams(e,t){let r=fo(t.targetDtype)*8,s=Pr.PER_TENSOR_SYMMETRIC;return t.calibration&&t.calibration.hasData(e)?t.calibration.getQuantParams(e,s,t.targetDtype):vr.defaultForActivation(s,t.targetDtype,r)}_replacePerChannelDot(e,t){let r=e.getOperand(0),s=e.getOperand(1),o=s.type.shape,i=s.definingOp.getAttr("value"),a=fo(t.targetDtype)*8,p=e.getAttr("rhs_contracting")[0]===0?1:0,c=vr.fromConstantArrayPerChannel([...i],o,p,t.targetDtype,a),f=c.quantizeArrayPerChannel([...i],o),d=this._activationParams(r,t),h=d.getScalarScale(),m=d.getScalarZeroPoint(),g=e.parentBlock;if(!g)return!1;let y=new I(o,t.targetDtype),_=new Me("constant",[],[y],{value:f,tensor_type:y});g.insertBefore(_,e);let b=new I(r.type.shape,t.targetDtype),w=new Me("quantize",[r],[b],{scale:h,zero_point:m,scheme:Pr.PER_TENSOR_SYMMETRIC,target_dtype:t.targetDtype});g.insertBefore(w,e);let x=e.getResult(0).type.shape,S={};for(let[q,D]of e.attributes||[])S[q]=D;S.lhs_scale=h,S.lhs_zero_point=m,S.rhs_scale=1,S.rhs_zero_point=0,S.output_scale=1,S.output_zero_point=0;let T=new I(x,"i32"),v=new Me("quantized_dot",[w.getResult(0),_.getResult(0)],[T],S);g.insertBefore(v,e);let A=new I(x,"f32"),N=new Me("convert",[v.getResult(0)],[A],{target_dtype:"f32"});g.insertBefore(N,e);let k=o[p],O=new Array(k);for(let q=0;q<k;q++)O[q]=h*c.getScaleForChannel(q);let R=new Set(e.getAttr("lhs_contracting")||[]),B=0;for(let q=0;q<r.type.shape.length;q++)R.has(q)||B++;let E=B,F=new I([k],"f32"),C=new Me("constant",[],[F],{value:O,tensor_type:F});g.insertBefore(C,e);let H=new Me("broadcast_in_dim",[C.getResult(0)],[A],{broadcast_dimensions:[E],result_shape:x});g.insertBefore(H,e);let G=new Me("mul",[N.getResult(0),H.getResult(0)],[A],{});return g.insertBefore(G,e),e.replaceAllResultsWith([G.getResult(0)]),e.erase(),!0}};l(D6,"hasConstantOperand");l(P6,"allOperandsCanQuantize")});var pv,lb,WE=M(()=>{"use strict";ro();Re();pv=class{static{l(this,"LivenessResult")}constructor(e,t,r,s,o,i,a){this.liveIn=e,this.liveOut=t,this.intervals=r,this.opIndex=s,this.peakPressure=o,this.peakOp=i,this.pressureAtOp=a}interfere(e,t){if(e===t)return!0;let r=this.intervals.get(e),s=this.intervals.get(t);return r&&s?r.start<=s.end&&s.start<=r.end:!1}liveAtOp(e){return this.liveIn.get(e)||new Set}intervalOf(e){return this.intervals.get(e)||null}},lb=class n{static get name(){return"liveness"}static get depKey(){return"liveness"}static get dependencies(){return[ft]}static buildIntervals(e,t){let r=new Map;for(let o=0;o<t.length;o++)r.set(t[o],o);let s=new Map;for(let o of e.args)s.set(o,{start:-1,end:-1});for(let o=0;o<t.length;o++){let i=t[o];for(let a=0;a<i.numResults;a++)s.set(i.getResult(a),{start:o,end:o})}for(let o=0;o<t.length;o++){let i=t[o];for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a),p=s.get(u);p&&p.end<o&&(p.end=o)}}return{intervals:s,opIndex:r}}static compute(e,t={}){let r=t.useDef||ft.compute(e),s=r.topologicalOrder,{intervals:o,opIndex:i}=n.buildIntervals(e,s),a=new Map,u=new Map;for(let d of s)a.set(d,new Set),u.set(d,new Set);for(let d=s.length-1;d>=0;d--){let h=s[d],m=u.get(h),g=r.opUsers.get(h);if(g)for(let _ of g){let b=a.get(_);if(b)for(let w of b)m.add(w)}let y=a.get(h);for(let _ of m)y.add(_);for(let _=0;_<h.numResults;_++)y.delete(h.getResult(_));for(let _=0;_<h.numOperands;_++)y.add(h.getOperand(_))}for(let[d,h]of i){let m=a.get(d);for(let g of m){let y=o.get(g);y&&y.end<h&&(y.end=h)}}let p=0,c=null,f=new Map;for(let d=0;d<s.length;d++){let h=s[d],m=a.get(h),g=0;for(let y of m)if(y.type instanceof I){let _=y.type.sizeInBytes();_!==X&&(g+=_)}f.set(h,g),g>p&&(p=g,c=h)}return new pv(a,u,o,i,p,c,f)}}});var cb,pb,KE=M(()=>{"use strict";fn();yr();Re();Ct();nn();ro();WE();Ir();cb=class{static{l(this,"RematerializationConfig")}constructor(e={}){this.memoryBudget=e.memoryBudget||1/0,this.maxIterations=e.maxIterations||100,this.maxRecomputeCost=e.maxRecomputeCost||1/0,this.excludeOps=e.excludeOps||new Set}},pb=class extends Ue{static{l(this,"RematerializationPass")}constructor(e={}){super("RematerializationPass"),this.config=e instanceof cb?e:new cb(e)}run(e,t){if(this.config.memoryBudget===1/0)return oe.UNCHANGED;let r=!1,s=0,o=0;for(;s<this.config.maxIterations;){let i=ft.compute(e),{peakPressure:a,candidates:u,opIndex:p}=this._analyzeIntervalPressure(e,i);if(o=a,a<=this.config.memoryBudget||u.length===0)break;u.sort((c,f)=>f.score-c.score),this._rematerialize(e,u[0],p),r=!0,s++}return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,iterations:s,peakPressure:o,budget:this.config.memoryBudget,changed:r,level:le.DEBUG}),r?oe.CHANGED:oe.UNCHANGED}_analyzeIntervalPressure(e,t){let r=t.topologicalOrder,{intervals:s,opIndex:o}=lb.buildIntervals(e,r),i=[];for(let[m,g]of s){if(!(m.type instanceof I))continue;let y=m.type.sizeInBytes();y===X||y<=0||(i.push({idx:g.start,delta:y,value:m}),i.push({idx:g.end+1,delta:-y,value:m}))}i.sort((m,g)=>m.idx-g.idx||m.delta-g.delta);let a=0,u=0,p=0,c=new Set,f=new Set,d=0;for(let m=-1;m<=r.length;m++){for(;d<i.length&&i[d].idx<=m;)a+=i[d].delta,i[d].delta>0&&i[d].value&&f.add(i[d].value),i[d].delta<0&&i[d].value&&f.delete(i[d].value),d++;if(a>u){u=a,p=m,c.clear();for(let g of f)c.add(g)}}let h=[];for(let m of c){if(!this._canRematerialize(m))continue;let g=m.definingOp,y=this._computeMemorySaved(m);if(y<=0)continue;let _=this._estimateRecomputeCost(g);_>=this.config.maxRecomputeCost||_!==0&&h.push({value:m,definingOp:g,memorySaved:y,recomputeCost:_,score:y/_})}return{peakPressure:u,peakIdx:p,candidates:h,opIndex:o}}_canRematerialize(e){if(e.isBlockArgument())return!1;let t=e.definingOp;if(!t||Xt(t.opName)||_n(t.opName)||this.config.excludeOps.has(t.opName)||t.regions.length>0||t.hasSideEffects()||e.useCount<=1)return!1;for(let r=0;r<t.numOperands;r++){let s=t.getOperand(r);if(s.definingOp&&s.definingOp.hasSideEffects())return!1}return!0}_computeMemorySaved(e){if(!(e.type instanceof I))return 0;let t=e.type.sizeInBytes();return t===X?0:t}_operandExtensionCost(e,t,r){let s=0;for(let o=0;o<e.numOperands;o++){let i=e.getOperand(o),a=0;for(let u of i.uses()){let p=r.get(u.user)||0;p>a&&(a=p)}if(t>a&&i.type instanceof I){let u=i.type.sizeInBytes();u!==X&&(s+=u)}}return s}_estimateRecomputeCost(e){let t=ne.get(e.opName);if(!t||t.isOpaque)return 1/0;if(t.getFlops){let r=t.getFlops(e);if(r>0)return r}if(t.isElementwise||t.isBroadcast||t.isView){let r=0;for(let s=0;s<e.numResults;s++){let o=e.getResult(s).type;if(o instanceof I){let i=o.numel();if(i!==X){r=i;break}}}return r||1}if(t.isReduction)for(let r=0;r<e.numOperands;r++){let s=e.getOperand(r).type;if(s instanceof I){let o=s.numel();if(o!==X)return o}}return 1/0}_rematerialize(e,t,r){let{value:s,definingOp:o}=t,i=[];for(let c of s.uses())i.push({user:c.user,operandIndex:c.operandIndex});if(i.length<=1)return;i.sort((c,f)=>(r.get(c.user)||0)-(r.get(f.user)||0));let a=r.get(i[i.length-1].user)||0,u=this._operandExtensionCost(o,a,r),p=this._computeMemorySaved(s);if(!(u>=p))for(let c=1;c<i.length;c++){let f=i[c],d=f.user;if(!d.parentBlock)continue;let h=new Me(o.opName,[...o.operands],o.results.map(m=>m.type),new Map(o.attributes));d.parentBlock.insertBefore(h,d),d.replaceOperand(f.operandIndex,h.getResult(s.resultIndex))}}}});function $o(n){return _n(n.opName)}function HE(n){let e=0;for(let t=0;t<n.numResults;t++){let r=n.getResult(t).type;if(!r||!r.isFullyStatic)continue;let s=r.sizeInBytes();s>e&&(e=s)}return e}function F6(n){let e=n.definingOp;if(e&&e.opName==="broadcast"){let t=e.getOperand(0);e=t&&t.definingOp}if(e&&$o(e)){let t=e.getAttr("value");if(typeof t=="number")return t}return 0}function fv(n){if(n.opName!=="dot"&&n.opName!=="cublas_gemm")return null;let e=n.getOperand(0).type,t=n.getOperand(1).type,r=n.getResult(0).type;if(!e||!t||!r||e.dtype!=="f32"||t.dtype!=="f32"||r.dtype!=="f32")return null;let s=n.getAttr("lhs_batch")||[],o=n.getAttr("rhs_batch")||[];if(s.length>0||o.length>0)return null;let i=n.getAttr("lhs_contracting")||[],a=n.getAttr("rhs_contracting")||[];if(i.length!==1||a.length!==1||t.rank!==2||i[0]!==e.rank-1||a[0]!==0&&a[0]!==1||!e.isFullyStatic||!t.isFullyStatic||!r.isFullyStatic)return null;let u=n.getOperand(0).definingOp,p=n.getOperand(1).definingOp;if(u&&$o(u)||p&&$o(p))return null;let c=a[0]===1,f=e.shape[e.rank-1];if((c?t.shape[1]:t.shape[0])!==f)return null;let h=1;for(let g=0;g<e.rank-1;g++)h*=e.shape[g];let m=c?t.shape[0]:t.shape[1];return h<=0||m<=0||f<=0?null:{M:h,N:m,K:f,transB:c}}function YE(n){return{labelOf:l(e=>n.get(e),"labelOf"),canMerge:l((e,t)=>Math.max(e.maxBuf||0,HE(t))<=$6,"canMerge"),onAttach:l((e,t)=>{e.maxBuf=Math.max(e.maxBuf||0,HE(t))},"onAttach")}}function fb(n,e,t){let r=n.opSet,s=Ru(n.ops),{inputs:o,outputs:i,constDefs:a}=pa(r,s,{pullConstants:!0,isConstant:$o});for(let d of o)if(!d.type||!d.type.isFullyStatic)return null;for(let d of i)if(!d.type||!d.type.isFullyStatic)return null;let u=new yn(e,o.map(d=>d.type),i.map(d=>d.type)),p=new Map;for(let d=0;d<o.length;d++)p.set(o[d],u.args[d]);for(let d of a)u.entryBlock.pushOp(d.clone(p));for(let d of s)u.entryBlock.pushOp(d.clone(p));let c=i.map(d=>p.get(d));if(c.some(d=>d===void 0))return null;u.entryBlock.pushOp(new Me("return",c,[]));let f=n.ops.length===1&&t.has(n.ops[0])?n.ops[0]:null;return{part:n,subFunc:u,inputs:o,outputs:i,dotOp:f}}function Od(n){if(z6.has(n.opName))return!0;if(n.regions)for(let e of n.regions){let t=e.entryBlock;if(t){for(let r of t.ops())if(Od(r))return!0}}return!1}function V6(n){let e=0;for(let t=0;t<n.numResults;t++){let r=n.getResult(t).type;if(!r||!r.isFullyStatic)continue;let s=1;for(let o of r.shape)s*=o;s>e&&(e=s)}return e}function JE(n,e=256){if(n.functionCount!==1)return!1;let t=n.functions().next().value,r=new Map,s=l(o=>{if(!o)return 0;let i=r.get(o);if(i!==void 0)return i;r.set(o,0);let a=Od(o)?V6(o):0;for(let u=0;u<o.numOperands;u++){let p=s(o.getOperand(u).definingOp);p>a&&(a=p)}return r.set(o,a),a},"maxBoundaryInSubtree");for(let o of t.ops())if(!(Xt(o.opName)||!Od(o))){for(let i=0;i<o.numOperands;i++)if(s(o.getOperand(i).definingOp)>e)return!0}return!1}function ZE(n,e,t){let r=new Map,s=0,o=l(h=>{let m=r.get(h);return m===void 0&&(m=s++,r.set(h,m)),m},"getSlot");for(let h of n.args)o(h);for(let h of t)for(let m of h.outputs)o(m);let i=[];for(let h of n.args)i.push(o(h));let a=[],u=new Set;for(let h=0;h<e.numOperands;h++){let m=e.getOperand(h),g=i.length,y=m.isBlockArgument&&m.isBlockArgument(),_=m.definingOp&&$o(m.definingOp);if(!y&&!_&&r.has(m)){let b=r.get(m);if(!u.has(b)){u.add(b),i.push(b);continue}i.push(s++),a.push({pos:g,kind:"copy",srcSlot:b});continue}if(y){i.push(s++),a.push({pos:g,kind:"copy",srcSlot:o(m)});continue}if(_){i.push(s++),a.push({pos:g,kind:"const",value:F6(m)});continue}return null}let p=[];for(let h of t){let m=[];for(let y of h.inputs){let _=r.get(y);if(_===void 0)return null;m.push(_)}let g=h.outputs.map(y=>r.get(y));p.push({name:h.subFunc.name,inputSlots:m,outputSlots:g})}let c=new Set(i),f=[],d=new Set;for(let[h,m]of r)if(!(c.has(m)||d.has(m))){if(d.add(m),!h.type||!h.type.isFullyStatic)return null;f.push({slot:m,shape:[...h.type.shape],dtype:h.type.dtype})}return{plan:{numSlots:s,argSlots:i,intermediates:f,steps:p,returnFixups:a}}}function ha(n,e=2){if(n.functionCount!==1)return null;let t=n.functions().next().value,r=t.getReturnOp();if(!r)return null;let s=[],o=new Map,i=0;for(let g of t.ops())Xt(g.opName)||$o(g)||(Od(g)?o.set(g,"boundary#"+i++):o.set(g,"native"),s.push(g));if(i<e||s.length===0)return null;let{partitions:a,preds:u}=kd(s,YE(o));if(a.length<2)return null;let p=ov(a,u);if(!p)return null;let c=t.name,f=[],d=new Map,h=0;for(let g of p){let y=fb(g,c+"_p"+h++,d);if(!y)return null;f.push(y)}let m=ZE(t,r,f);if(!m)return null;n.removeFunction(t.name);for(let g of f)n.addFunction(g.subFunc);return{plan:m.plan}}function QE(n){if(n.functionCount!==1)return null;let e=n.functions().next().value,t=e.getReturnOp();if(!t)return null;let r=[],s=new Map,o=new Map,i=0,a=0;for(let y of e.ops()){if(Xt(y.opName)||$o(y))continue;let _=fv(y);_?(s.set(y,"cublas#"+i),o.set(y,_),i++):Od(y)?s.set(y,"boundary#"+a++):s.set(y,"native"),r.push(y)}if(i+a===0||r.length===0)return null;let{partitions:u,preds:p}=kd(r,YE(s));if(u.length<2)return null;let c=ov(u,p);if(!c)return null;let f=e.name,d=[],h=0;for(let y of c){let _=fb(y,f+"_p"+h++,o);if(!_)return null;d.push(_)}let m=ZE(e,t,d);if(!m)return null;let g=new Map;for(let y of d){if(!y.dotOp)continue;let _=o.get(y.dotOp),b=y.inputs.indexOf(y.dotOp.getOperand(0)),w=y.inputs.indexOf(y.dotOp.getOperand(1)),x=y.outputs.indexOf(y.dotOp.getResult(0));b<0||w<0||x<0||g.set(y.subFunc.name,{M:_.M,N:_.N,K:_.K,transB:_.transB,aIdx:b,bIdx:w,cIdx:y.inputs.length+x})}if(g.size===0)return null;n.removeFunction(e.name);for(let y of d)n.addFunction(y.subFunc);return{plan:m.plan,cublasInfos:g}}var $6,z6,db=M(()=>{"use strict";qs();yr();Nd();Ir();l($o,"isConstantOp");$6=32*1024;l(HE,"maxResultBytes");l(F6,"constScalarOf");l(fv,"cublasDotInfo");l(YE,"bufferLimitedConfig");l(fb,"materializePartition");z6=new Set(["dot","fused_dot_epilogue","cublas_gemm","reduce","conv","quantized_conv","scaled_dot_product_attention"]);l(Od,"containsBoundaryOp");l(V6,"maxBoundaryResultBytes");l(JE,"hasDependentBoundaries");l(ZE,"buildExecutionPlan");l(ha,"splitGraphForNative");l(QE,"splitGraphForCublas")});function eL(n){return XE.push(n),n}var XE,hb,tL=M(()=>{"use strict";fn();XE=[];l(eL,"registerBackendOpRewrite");hb=class extends Ue{static{l(this,"BackendOpRewritePass")}constructor(e={}){super(e.name||"BackendOpRewritePass"),this.config=e,this.rewrites=e.rewrites||XE}run(e){let t=!1;for(let r of[...e.ops()]){let s=r.parentBlock;if(s){for(let o of this.rewrites)if(o.match(r,this.config)){o.build(r,s,this.config),t=!0;break}}}return t?oe.CHANGED:oe.UNCHANGED}}});var G6,mb,nL=M(()=>{"use strict";yr();db();tL();G6=eL({name:"dot->cublas_gemm",match:l(n=>n.opName==="dot"&&n.numOperands===2&&fv(n)!==null,"match"),build:l((n,e)=>{let t=n.results.map(s=>s.type),r=new Me("cublas_gemm",[n.getOperand(0),n.getOperand(1)],t,new Map(n.attributes),null);e.insertBefore(r,n),n.replaceAllResultsWith(r.results),n.erase()},"build")}),mb=class extends hb{static{l(this,"CublasRewritePass")}constructor(e={}){super({...e,name:"CublasRewritePass",rewrites:[G6]})}}});function rL(n,e,{cudaMatmulChain:t=!1,context:r=null}={}){let s=r?a=>r.passesForPhase(a,n,e):a=>F_(a,n,e),o=[];for(let a of s("pre"))o.push(a);if(o.push(new ql(e)),o.push(new jl("canonicalize",[new au,new j_({fastMath:n.optimization.fastMath}),new q_,new U_,new Co],n.optimization.maxSimplifyIterations)),n.optimization.layout&&e&&(o.push(new ib({target:e})),o.push(new Co)),n.quantization.enabled&&(o.push(new ub({...n.quantization,target:e})),o.push(new au),o.push(new Co)),n.matmulBackend!=="cublas"&&(n.fusion.epilogue!==void 0?n.fusion.epilogue:e&&e.enableEpilogueFusion)&&(o.push(new Z_({target:e})),o.push(new Co)),n.fusion.enabled){let a=n.fusion,u=a.launchOverheadUs??j6;a.strategy==="dominator"?o.push(new eb({target:e,...a})):a.strategy==="priority"?(o.push(new rb({target:e,cost:{launchOverheadUs:u},...a})),o.push(new Ad({maxFusionSize:e?.maxFusionSize,...a}))):(o.push(new Y_({target:e,cost:{launchOverheadUs:u},...a})),o.push(new Q_({maxFusionSize:e?.maxFusionSize,...a})),o.push(new Ad({maxFusionSize:e?.maxFusionSize,...a}))),o.push(new Co)}if(n.matmulBackend==="cublas"&&o.push(new mb),n.optimization.rematerialization){let a={...n.optimization.rematConfig};a.memoryBudget===void 0&&e&&e.memoryBudgetBytes>0&&(a.memoryBudget=e.memoryBudgetBytes),o.push(new pb(a))}for(let a of s("post"))o.push(a);return o}var j6,sL=M(()=>{"use strict";_y();Mx();TE();vE();kE();Ex();AE();BE();RE();ME();LE();PE();GE();UE();Bx();KE();nL();ev();j6=5;l(rL,"buildGraphPipeline")});var ar,ma=M(()=>{"use strict";ar=class{static{l(this,"PrimFuncPass")}constructor(e,t=null){this.name=e,this.phase=t||e,this.snapshotPoint=null,this.trace=null}begin(e){}run(e,t){throw new Error("PrimFuncPass.run not implemented")}end(e){}}});var gb,dv,Mu,hv=M(()=>{"use strict";Fe();gb=["iterCount","depth","parallelLoops","vectorizedLoops","unrolledLoops","threadBoundLoops","serialLoops","threadBlockSize","gridSize","underReduction","numMathOps","numExternCalls","numReads","numWrites","stride1Accesses","stridedAccesses","reuseCount","touchedBytes","arithmeticIntensity","vectorized","parallelized","innermostExtent"],dv=class{static{l(this,"ScheduleFeatures")}constructor(e){this.numLoops=e.numLoops||0,this.numBlocks=e.numBlocks||0,this.totalIterations=e.totalIterations||0,this.maxLoopDepth=e.maxLoopDepth||0,this.numParallelLoops=e.numParallelLoops||0,this.numVectorizedLoops=e.numVectorizedLoops||0,this.numUnrolledLoops=e.numUnrolledLoops||0,this.numThreadBound=e.numThreadBound||0,this.numSerialLoops=e.numSerialLoops||0,this.totalBufferBytes=e.totalBufferBytes||0,this.numBufferReads=e.numBufferReads||0,this.numBufferWrites=e.numBufferWrites||0,this.numMathOps=e.numMathOps||0,this.numExternCalls=e.numExternCalls||0,this.arithmeticIntensity=e.arithmeticIntensity||0,this.innermostExtent=e.innermostExtent||0,this.outermostExtent=e.outermostExtent||0,this.hasReduction=e.hasReduction||!1,this.reductionDepth=e.reductionDepth||0,this.threadBlockSize=e.threadBlockSize||0,this.gridSize=e.gridSize||0,this.strideOneAccesses=e.strideOneAccesses||0,this.nonStrideOneAccesses=e.nonStrideOneAccesses||0}},Mu=class n{static{l(this,"FeatureExtractor")}static extract(e){let t={numLoops:0,numBlocks:0,totalIterations:1,maxLoopDepth:0,currentDepth:0,numParallelLoops:0,numVectorizedLoops:0,numUnrolledLoops:0,numThreadBound:0,numSerialLoops:0,totalBufferBytes:0,buffersSeen:new Set,numBufferReads:0,numBufferWrites:0,numMathOps:0,numExternCalls:0,hasReduction:!1,reductionDepth:0,threadBlockSize:1,gridSize:1,innermostExtent:0,outermostExtent:0,loopExtents:[],strideOneAccesses:0,nonStrideOneAccesses:0};n._visitIterative(e.body,t);let r=t.totalBufferBytes,s=t.numMathOps+t.numExternCalls;return t.arithmeticIntensity=r>0?s/r:0,new dv(t)}static extractStatements(e){let t=[],r=[],s=0,o=[{node:e.body,action:"enter"}];for(;o.length>0;){let{node:i,action:a}=o.pop();if(i){if(a==="leaveFor"){r.pop();continue}if(a==="leaveBlock"){s--;continue}switch(i.type){case"ForNode":r.push(i),o.push({node:null,action:"leaveFor"}),o.push({node:i.body,action:"enter"});break;case"BlockNode":i.initBody&&(s++,o.push({node:null,action:"leaveBlock"}),o.push({node:i.initBody,action:"enter"})),o.push({node:i.body,action:"enter"});break;case"SeqNode":for(let u=i.stmts.length-1;u>=0;u--)o.push({node:i.stmts[u],action:"enter"});break;case"AllocateNode":case"LetStmtNode":o.push({node:i.body,action:"enter"});break;case"IfThenElseNode":i.elseBody&&o.push({node:i.elseBody,action:"enter"}),o.push({node:i.thenBody,action:"enter"});break;case"BufferStoreNode":t.push(n._statementVector(i,r,s));break}}}return t}static _statementVector(e,t,r){let s=1,o=0,i=0,a=0,u=0,p=0,c=1,f=1;for(let T of t){let v=T.extent&&T.extent.type==="IntImmNode"?T.extent.value:1;switch(s*=v,T.kind){case z.PARALLEL:o++;break;case z.VECTORIZED:i++;break;case z.UNROLLED:a++;break;case z.THREAD_BINDING:u++,T.threadTag&&T.threadTag.startsWith("threadIdx")?c*=v:T.threadTag&&T.threadTag.startsWith("blockIdx")&&(f*=v);break;default:p++;break}}let d={math:0,extern:0};n._countExpr(e.value,d);let h=t.map(T=>T.loopVar.name),m=[{buffer:e.buffer,indices:e.indices}];n._collectLoads(e.value,m);let g=0,y=0,_=0,b=0;for(let T of m){let v=T.buffer&&T.buffer.sizeInBytes?T.buffer.sizeInBytes():0;v>0&&(b+=v);let A=T.indices&&T.indices.length>0?T.indices[T.indices.length-1]:null;A&&A.type==="VariableNode"?g++:y++;let N=new Set;if(T.indices)for(let k of T.indices)n._collectVars(k,N);for(let k of h)N.has(k)||_++}let w=t.length>0?t[t.length-1]:null,x=w&&w.extent&&w.extent.type==="IntImmNode"?w.extent.value:0,S={iterCount:s,depth:t.length,parallelLoops:o,vectorizedLoops:i,unrolledLoops:a,threadBoundLoops:u,serialLoops:p,threadBlockSize:c,gridSize:f,underReduction:r>0?1:0,numMathOps:d.math,numExternCalls:d.extern,numReads:m.length-1,numWrites:1,stride1Accesses:g,stridedAccesses:y,reuseCount:_,touchedBytes:b,arithmeticIntensity:b>0?(d.math+d.extern)/b:0,vectorized:i>0?1:0,parallelized:o+u>0?1:0,innermostExtent:x};return gb.map(T=>S[T]||0)}static _countExpr(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"MathOpNode":t.math++,n._countExpr(e.a,t),e.b&&n._countExpr(e.b,t);break;case"CompareNode":t.math++,n._countExpr(e.a,t),n._countExpr(e.b,t);break;case"CallExternNode":t.extern++;for(let r of e.args)n._countExpr(r,t);break;default:break}}static _collectLoads(e,t){if(!(!e||typeof e!="object")){if(e.type==="BufferLoadNode"){t.push({buffer:e.buffer,indices:e.indices});return}if(e.a&&n._collectLoads(e.a,t),e.b&&n._collectLoads(e.b,t),e.args)for(let r of e.args)n._collectLoads(r,t)}}static _collectVars(e,t){if(!(!e||typeof e!="object")){if(e.type==="VariableNode"){t.add(e.name);return}if(e.a&&n._collectVars(e.a,t),e.b&&n._collectVars(e.b,t),e.args)for(let r of e.args)n._collectVars(r,t);if(e.indices)for(let r of e.indices)n._collectVars(r,t)}}static _visitIterative(e,t){let r=[{node:e,action:"enter"}];for(;r.length>0;){let{node:s,action:o}=r.pop();if(s){if(o==="leave_for"){t.currentDepth--;continue}switch(s.type){case"ForNode":{t.numLoops++,t.currentDepth++,t.currentDepth>t.maxLoopDepth&&(t.maxLoopDepth=t.currentDepth);let i=s.extent.type==="IntImmNode"?s.extent.value:1;switch(t.loopExtents.push(i),t.numLoops===1&&(t.outermostExtent=i),t.innermostExtent=i,s.kind){case z.PARALLEL:t.numParallelLoops++;break;case z.VECTORIZED:t.numVectorizedLoops++;break;case z.UNROLLED:t.numUnrolledLoops++;break;case z.THREAD_BINDING:t.numThreadBound++,s.threadTag&&s.threadTag.startsWith("threadIdx")?t.threadBlockSize*=i:s.threadTag&&s.threadTag.startsWith("blockIdx")&&(t.gridSize*=i);break;default:t.numSerialLoops++;break}t.totalIterations*=i,r.push({node:null,action:"leave_for"}),r.push({node:s.body,action:"enter"});break}case"BlockNode":t.numBlocks++,s.initBody&&(t.hasReduction=!0,t.reductionDepth=t.currentDepth);for(let i of s.reads)n._visitBuffer(i.buffer,t);for(let i of s.writes)n._visitBuffer(i.buffer,t);r.push({node:s.body,action:"enter"}),s.initBody&&r.push({node:s.initBody,action:"enter"});break;case"SeqNode":for(let i=s.stmts.length-1;i>=0;i--)r.push({node:s.stmts[i],action:"enter"});break;case"AllocateNode":n._visitBuffer(s.buffer,t),r.push({node:s.body,action:"enter"});break;case"IfThenElseNode":s.elseBody&&r.push({node:s.elseBody,action:"enter"}),r.push({node:s.thenBody,action:"enter"});break;case"LetStmtNode":r.push({node:s.body,action:"enter"});break;case"BufferStoreNode":t.numBufferWrites++,n._visitBuffer(s.buffer,t),n._checkStride(s.buffer,s.indices,t),n._visitExpr(s.value,t);break}}}}static _visitBuffer(e,t){if(!e||t.buffersSeen.has(e))return;t.buffersSeen.add(e);let r=e.sizeInBytes();r>0&&(t.totalBufferBytes+=r)}static _visitExpr(e,t){if(e)switch(e.type){case"MathOpNode":t.numMathOps++,n._visitExpr(e.a,t),e.b&&n._visitExpr(e.b,t);break;case"CallExternNode":t.numExternCalls++;for(let r of e.args)n._visitExpr(r,t);break;case"BufferLoadNode":t.numBufferReads++,n._checkStride(e.buffer,e.indices,t);break;case"CompareNode":t.numMathOps++,n._visitExpr(e.a,t),n._visitExpr(e.b,t);break;default:break}}static _checkStride(e,t,r){if(!e||!t||t.length===0)return;let s=t[t.length-1];for(;s&&s.type==="MathOpNode"&&s.b&&s.b.type==="IntImmNode";)if(s.op==="+"&&s.b.value===0)s=s.a;else if(s.op==="*"&&s.b.value===1)s=s.a;else break;s&&s.type==="VariableNode"?r.strideOneAccesses++:r.nonStrideOneAccesses++}}});function oL(n,e){if(n.length===0)return 0;let t=0;for(let r of n)t+=e[r];return t/n.length}function mv(n,e,t,r,s,o,i){let a=t.length;if(r>=s||a<o*2)return{leaf:oL(t,e)};let u=n[0].length,p=0,c=0,f=new Set(t);for(let g of t){let y=e[g];p+=y,c+=y*y}let d=null,h=null;for(let g=0;g<u;g++){let y=i[g].filter(w=>f.has(w)),_=0,b=0;for(let w=0;w<y.length-1;w++){let x=e[y[w]];_+=x,b+=x*x;let S=n[y[w]][g],T=n[y[w+1]][g];if(S===T)continue;let v=w+1,A=a-v;if(v<o||A<o)continue;let N=p-_,k=c-b,O=b-_*_/v+(k-N*N/A);(!d||O<d.cost)&&(d={f:g,thr:(S+T)/2,sIdx:w,cost:O},h=y)}}if(!d)return{leaf:oL(t,e)};let m=h;return{f:d.f,thr:d.thr,left:mv(n,e,m.slice(0,d.sIdx+1),r+1,s,o,i),right:mv(n,e,m.slice(d.sIdx+1),r+1,s,o,i)}}function q6(n){let e=n.length,t=e>0?n[0].length:0,r=new Array(t),s=[];for(let o=0;o<e;o++)s.push(o);for(let o=0;o<t;o++)r[o]=s.slice().sort((i,a)=>n[i][o]-n[a][o]);return r}function iL(n,e){for(;n&&n.leaf===void 0;)n=e[n.f]<=n.thr?n.left:n.right;return n?n.leaf:0}var Bd,aL=M(()=>{"use strict";l(oL,"leafValue");l(mv,"buildTree");l(q6,"buildFeatureOrder");l(iL,"predictTree");Bd=class n{static{l(this,"GradientBoostedTrees")}constructor(e={}){this.numTrees=e.numTrees??60,this.maxDepth=e.maxDepth??3,this.lr=e.lr??.1,this.minSamples=e.minSamples??1,this.trees=null,this.base=0}fit(e,t){let r=e.length;if(r===0)return;let s=0;for(let p of t)s+=p;s/=r;let o=new Array(r).fill(s),i=[];for(let p=0;p<r;p++)i.push(p);let a=q6(e),u=[];for(let p=0;p<this.numTrees;p++){let c=new Array(r);for(let d=0;d<r;d++)c[d]=t[d]-o[d];let f=mv(e,c,i,0,this.maxDepth,this.minSamples,a);for(let d=0;d<r;d++)o[d]+=this.lr*iL(f,e[d]);u.push(f)}this.trees=u,this.base=s}predict(e){if(!this.trees)return 0;let t=this.base;for(let r of this.trees)t+=this.lr*iL(r,e);return t}serialize(){return{trees:this.trees,base:this.base,lr:this.lr}}static deserialize(e){let t=new n({lr:e.lr});return t.trees=e.trees,t.base=e.base,t}}});function uL(n){let e=n[0].length,t=new Array(e+1).fill(0);for(let r of n)for(let s=0;s<e;s++){let o=r[s]||0;K6.has(s)?o>t[s]&&(t[s]=o):t[s]+=o}for(let r of H6)r<e&&(t[r]/=n.length);return t[e]=n.length,t}var U6,W6,K6,H6,gv,Y6,yb,Ac,Oc,yv=M(()=>{"use strict";hv();aL();U6=new Set(["depth","threadBlockSize","gridSize","underReduction","vectorized","parallelized","innermostExtent"]),W6=new Set(["arithmeticIntensity"]),K6=new Set(gb.map((n,e)=>U6.has(n)?e:-1).filter(n=>n>=0)),H6=new Set(gb.map((n,e)=>W6.has(n)?e:-1).filter(n=>n>=0));l(uL,"aggregateStatements");gv=class{static{l(this,"CostEstimate")}constructor(e,t){this.score=e,this.breakdown=t}},Y6={parallelism:2,vectorization:1.5,memoryCoalescing:2,occupancy:1,arithmeticIntensity:1,loopOverhead:-.5,codeSize:-.3},yb=class{static{l(this,"AnalyticalCostModel")}constructor(e,t={}){this.target=e,this._weights={...Y6,...e&&e.costModelWeights?e.costModelWeights:{},...t.weights||{}}}estimate(e){let t=Mu.extract(e);return this.estimateFromFeatures(t)}score(e){return this.estimate(e).score}estimateFromFeatures(e){let t=this._scoreParallelism(e),r=this._scoreVectorization(e),s=this._scoreMemoryAccess(e),o=this._scoreOccupancy(e),i=this._scoreIntensity(e),a=this._scoreOverhead(e),u=this._scoreCodeSize(e),p=this._weights,c=p.parallelism*t+p.vectorization*r+p.memoryCoalescing*s+p.occupancy*o+p.arithmeticIntensity*i+p.loopOverhead*a+p.codeSize*u;return new gv(c,{parallelism:t,vectorization:r,memoryCoalescing:s,occupancy:o,arithmeticIntensity:i,loopOverhead:a,codeSize:u})}_scoreParallelism(e){if(this.target.isGPU()){let r=e.threadBlockSize*e.gridSize,s=this.target.maxParallelism();return Math.min(1,r/Math.max(s*.1,1))}return e.numParallelLoops/Math.max(e.numLoops,1)}_scoreVectorization(e){return e.numLoops===0?0:this.target.isGPU()?e.strideOneAccesses/Math.max(e.strideOneAccesses+e.nonStrideOneAccesses,1):e.numVectorizedLoops>0?Math.min(1,e.innermostExtent/this.target.vectorWidth):0}_scoreMemoryAccess(e){let t=e.strideOneAccesses+e.nonStrideOneAccesses;return t===0?1:e.strideOneAccesses/t}_scoreOccupancy(e){if(!this.target.isGPU())return 1;if(e.threadBlockSize===0)return 0;let t=this.target.warpSize,r=Math.ceil(e.threadBlockSize/t),s=Math.floor(this.target.maxThreadsPerBlock/t);return Math.min(1,r/s)}_scoreIntensity(e){let t=this.target.supportsFloat16?1.5:1;return Math.min(1,e.arithmeticIntensity*10*t)}_scoreOverhead(e){return e.numSerialLoops/Math.max(e.numLoops,1)}_scoreCodeSize(e){return Math.min(1,(e.numMathOps+e.numExternCalls)/256)}compare(e,t){return this.estimate(e).score-this.estimate(t).score}},Ac=class n{static{l(this,"LearnedCostModel")}constructor(e=null,t={}){this.opts={numTrees:t.numTrees??60,maxDepth:t.maxDepth??3,lr:t.lr??.1,minSamples:t.minSamples??1},this._gbt=e?Bd.deserialize(e):null,this._X=[],this._Y=[]}addSample(e,t){!e||e.length===0||Number.isFinite(t)&&(this._X.push(uL(e)),this._Y.push(t))}train(){if(this._X.length===0)return;let e=new Bd(this.opts);e.fit(this._X,this._Y),this._gbt=e}predict(e){return!this._gbt||!e||e.length===0?0:this._gbt.predict(uL(e))}get trained(){return this._gbt!==null}get sampleCount(){return this._X.length}serialize(){return{gbt:this._gbt?this._gbt.serialize():null,numSamples:this._X.length}}static deserialize(e){return new n(e&&e.gbt?e.gbt:null)}},Oc=class{static{l(this,"GuidedCostModel")}constructor(e,t,r={}){this.analytical=e,this.learned=t,this.confidenceSamples=r.confidenceSamples??8}_learnedConfident(){return this.learned&&this.learned.trained&&this.learned.sampleCount>=this.confidenceSamples}score(e){return this._learnedConfident()?this.learned.predict(Mu.extractStatements(e)):this.analytical.score(e)}}});function*lL(n){let e=[n];for(;e.length>0;){let t=e.pop();if(t){if(yield t,t.body&&e.push(t.body),t.stmts)for(let r of t.stmts)e.push(r);t.thenBody&&e.push(t.thenBody),t.elseBody&&e.push(t.elseBody),t.initBody&&e.push(t.initBody)}}}function Rd(n,e){for(let t of lL(n))if(t.type==="BlockNode"&&t.name===e)return t;return null}function Fo(n){let e=[];for(let t of lL(n))t.type==="BlockNode"&&e.push(t.name);return e}function Md(n,e){let t=Tt(n,e);if(!t)return{spatial:0,reduction:0,reads:0,hasReduction:!1};let r=0,s=0;for(let o of t.loops)t.reductionLoopVars.has(o.loopVar.name)?s++:r++;return{spatial:r,reduction:s,reads:t.readBuffers.length,hasReduction:t.hasReduction}}var Bc=M(()=>{"use strict";Do();l(lL,"walkStmts");l(Rd,"findBlock");l(Fo,"collectAllBlockNames");l(Md,"analyzeBlockStructure")});function pL(n){return n.isGPU()?Z6:J6}function _v(n){let e=0,t=0;for(let[r,s]of n.order)r==="S"?e=Math.max(e,s+1):t=Math.max(t,s+1);return{spatialLevels:e,reductionLevels:t}}var J6,Z6,cL,bv=M(()=>{"use strict";J6={name:"mlt_cpu",order:[["S",0],["S",1],["S",2],["S",3],["R",0]],roles:{S0:"parallelize",S3:"vectorize"}},Z6={name:"mlt_gpu",order:[["S",0],["S",1],["S",2],["R",0]],roles:{S0:"blockIdx",S1:"threadIdx",S2:"unroll"}},cL={name:"ssrsrs_cpu",order:[["S",0],["S",1],["R",0],["S",2],["R",1],["S",3]],roles:{S0:"parallelize",S3:"vectorize",R1:"unroll"}};l(pL,"getTileStructure");l(_v,"levelCounts")});var Os,$r,_b=M(()=>{"use strict";Os=class{static{l(this,"SearchVariable")}constructor(e,t){this.name=e,this.candidates=t}sample(e){return this.candidates[e(this.candidates.length)]}},$r=class{static{l(this,"ScheduleSketch")}constructor(e,t,r){this.name=e,this.variables=t,this._apply=r}instantiate(e){return(t,r,s)=>{this._apply(t,r,s,e)}}sampleParams(e){let t={};for(let r of this.variables)t[r.name]=r.sample(e);return t}}});function Q6(n){let e=[];for(let t=1;t*t<=n;t++){if(n%t!==0)continue;e.push(t);let r=n/t;r!==t&&e.push(r)}return e.sort((t,r)=>t-r),e}function X6(n,e){let t=Math.min(n.length,e.length);for(let r=0;r<t;r++)if(n[r]!==e[r])return n[r]-e[r];return n.length-e.length}function e4(n,e){let t=new Set,r=[];n.sort(X6);for(let i of n){let a=i.join(",");t.has(a)||(t.add(a),r.push(i))}if(r.length<=e)return r;let s=new Set,o=[];for(let i=0;i<e;i++){let a=Math.floor(i*(r.length-1)/(e-1));s.has(a)||(s.add(a),o.push(r[a]))}return o}function wv(n,e,t=48){if(!Number.isFinite(n)||n<1||e<=1)return[[n]];let r=[],s=t*8,o=l((i,a,u)=>{if(!(r.length>=s)){if(a===e-1){r.push([...u,i]);return}for(let p of Q6(i))if(u.push(p),o(i/p,a+1,u),u.pop(),r.length>=s)return}},"rec");return o(n,0,[]),e4(r,t)}var fL=M(()=>{"use strict";l(Q6,"divisorsOf");l(X6,"cmpTuple");l(e4,"selectDiverse");l(wv,"enumerateFactorizations")});function bb(n){return n.extent&&n.extent.type==="IntImmNode"?n.extent.value:null}function hL(n,e,t,r){let s=[],o=t;for(let i=0;i<r.length-1;i++){let a=n.getLoops(e).find(f=>f.loopVar.name===o);if(!a)return null;let u=1;for(let f=i+1;f<r.length;f++)u*=r[f];let[p,c]=n.split(a,u);s.push(p.loopVar.name),o=c.loopVar.name}return s.push(o),s}function t4(n,e,t,r,s){let o=l(i=>n.getLoops(e).find(a=>a.loopVar.name===i),"find");for(let[i,a]of t.order){let u=t.roles[i+a];if(!u)continue;let p=i==="S"?r:s;if(u==="parallelize"){let c=p[0]&&a<p[0].length?o(p[0][a]):null;c&&n.parallelize(c)}else if(u==="vectorize"){let c=p[p.length-1],f=c&&a<c.length?o(c[a]):null;f&&n.vectorize(f)}else if(u==="unroll")for(let c of p){let f=a<c.length?o(c[a]):null;f&&n.unroll(f)}else(u==="blockIdx"||u==="threadIdx")&&p.forEach((c,f)=>{if(f>=dL.length)return;let d=a<c.length?o(c[a]):null;d&&n.bindThread(d,`${u}.${dL[f]}`)})}}function mL(n,e,t,r,s,o){let i=[];for(let f=0;f<r.length;f++){let d=hL(n,e,r[f],o[`s${f}`]);if(!d)return;i.push(d)}let a=[];for(let f=0;f<s.length;f++){let d=hL(n,e,s[f],o[`r${f}`]);if(!d)return;a.push(d)}let u=[];for(let[f,d]of t.order){let h=f==="S"?i:a;for(let m of h)d<m.length&&u.push(m[d])}let p=new Map(n.getLoops(e).map(f=>[f.loopVar.name,f])),c=u.map(f=>p.get(f)).filter(Boolean);c.length>=2&&n.reorder(...c),t4(n,e,t,i,a)}function gL(n){let e=n.loops.filter(r=>!n.reductionLoopVars.has(r.loopVar.name)),t=n.loops.filter(r=>n.reductionLoopVars.has(r.loopVar.name));return{spatialLoops:e,reductionLoops:t}}function yL(n,e,t,r){let s=[];return n.forEach((o,i)=>s.push(new Os(`s${i}`,wv(bb(o),t)))),e.forEach((o,i)=>s.push(new Os(`r${i}`,wv(bb(o),r)))),s}function _L(n,e){let{spatialLevels:t,reductionLevels:r}=_v(e),{spatialLoops:s,reductionLoops:o}=gL(n);if(s.length===0)return null;for(let p of[...s,...o])if(bb(p)===null)return null;let i=yL(s,o,t,r),a=s.map(p=>p.loopVar.name),u=o.map(p=>p.loopVar.name);return new $r(e.name,i,(p,c,f,d)=>{mL(p,c,e,a,u,d)})}function bL(n,e){let{spatialLevels:t,reductionLevels:r}=_v(e),{spatialLoops:s,reductionLoops:o}=gL(n);if(s.length===0||o.length===0)return null;for(let p of[...s,...o])if(bb(p)===null)return null;let i=yL(s,o,t,r),a=s.map(p=>p.loopVar.name),u=o.map(p=>p.loopVar.name);return new $r(e.name,i,(p,c,f,d)=>{p.decomposeReduction(c),mL(p,`${c}_upd`,e,a,u,d)})}var dL,wL=M(()=>{"use strict";_b();fL();bv();dL=["x","y","z"];l(bb,"staticExtent");l(hL,"multiLevelSplit");l(t4,"applyRoles");l(mL,"tileBlock");l(gL,"splitLoops");l(yL,"tilingVariables");l(_L,"createMultiLevelTilingSketch");l(bL,"createSSRSRSTilingSketch")});function xL(n){return Math.min(n&&n.maxThreadsPerBlock||256,256)}function r4(n){let e=new Set;for(let t=2;t*t<=n;t++){if(n%t!==0)continue;e.add(t);let r=n/t;r>1&&r<n&&e.add(r)}return[...e].sort((t,r)=>t-r)}function SL(n){return new $r("fused",[],(e,t,r,s)=>{e.fuseConsumer(t,n);let o=e.getLoops(t);o.length>0&&e.parallelize(o[0])})}function vL(n){let e=n.loops.filter(i=>n.reductionLoopVars.has(i.loopVar.name));if(e.length!==1)return null;let t=e[0],r=t.extent&&t.extent.type==="IntImmNode"?t.extent.value:null;if(r===null)return null;let s=r4(r);if(s.length===0)return null;let o=t.loopVar.name;return new $r("rfactor",[new Os("rf_factor",s)],(i,a,u,p)=>{i.rfactor(a,o,p.rf_factor);let c=i.getLoops(`${a}_rf_p`);c.length>0&&i.parallelize(c[0])})}function kL(){return new $r("elementwise_cpu",[new Os("vector_width",n4)],(n,e,t,r)=>{let s=n.getLoops(e);if(s.length===0)return;if(s.length===1){let a=s[0].extent;if(a.type==="IntImmNode"&&a.value>=r.vector_width*2){let[u,p]=n.split(s[0],r.vector_width);n.parallelize(u),n.vectorize(p)}else n.parallelize(s[0]);return}n.parallelize(s[0]);let o=s[s.length-1],i=o.extent;if(i.type==="IntImmNode"&&i.value>=r.vector_width){let[,a]=n.split(o,r.vector_width);n.vectorize(a)}})}function NL(){return new $r("elementwise_gpu",[new Os("block_size",TL)],(n,e,t,r)=>{let s=n.getLoops(e);if(s.length===0)return;let o=s[0];for(let c=1;c<s.length;c++){let d=n.getLoops(e).find(h=>h.loopVar.name===s[c].loopVar.name);d&&o.body===d&&(o=n.fuseLoops(o,d))}let i=o.extent;if(i.type!=="IntImmNode"){n.bindThread(o,"threadIdx.x");return}let a=i.value,u=Math.min(t.maxThreadsPerBlock,1024);if(eS(n.func)&&a<=u){n.bindThread(o,"threadIdx.x");return}let p=Math.min(r.block_size,xL(t));if(a>p){let[c,f]=n.split(o,p);n.bindThread(c,"blockIdx.x"),n.bindThread(f,"threadIdx.x")}else n.bindThread(o,"threadIdx.x")})}function AL(){return new $r("reduction_cpu",[],(n,e,t,r)=>{let s=n.getLoops(e);s.length>0&&n.parallelize(s[0])})}function OL(){return new $r("reduction_gpu",[new Os("block_size",TL)],(n,e,t,r)=>{let s=n.getLoops(e);if(s.length===0)return;let o=Tt(n.func,e),i=s.filter(c=>!o||!Xl(c,o));if(i.length===0)return;let a=i[0];for(let c=1;c<i.length;c++){let d=n.getLoops(e).find(h=>h.loopVar.name===i[c].loopVar.name);d&&a.body===d&&(a=n.fuseLoops(a,d))}let u=a.extent;if(u.type!=="IntImmNode"){n.bindThread(a,"threadIdx.x");return}let p=Math.min(r.block_size,xL(t));if(u.value>p){let[c,f]=n.split(a,p);n.bindThread(c,"blockIdx.x"),n.bindThread(f,"threadIdx.x")}else n.bindThread(a,"threadIdx.x")})}var TL,n4,BL=M(()=>{"use strict";Do();_b();TL=[32,64,128,256,512,1024],n4=[1,2,4,8,16];l(xL,"gpuThreadCap");l(r4,"reductionDivisors");l(SL,"createFusedTilingSketch");l(vL,"createRfactorSketch");l(kL,"createElementwiseCPUSketch");l(NL,"createElementwiseGPUSketch");l(AL,"createReductionCPUSketch");l(OL,"createReductionGPUSketch")});function wb(n){return Ay(n,wb,(e,t,r)=>{switch(e.type){case"PrimFunc":return t.name=e.name,t.params=e.params,t.body=r(e.body),t.bufferMap=new Map(e.bufferMap),t.shapeParams=e.shapeParams,t.shapeParamMap=e.shapeParamMap instanceof Map?new Map(e.shapeParamMap):e.shapeParamMap,t._setChild("body",t.body),t;case"AllocateNode":return t.buffer=e.buffer,t.scope=e.scope,t.body=r(e.body),t._setChild("body",t.body),t;case"LetStmtNode":return t.variable=e.variable,t.value=r(e.value),t.body=r(e.body),t._setChild("body",t.body),t;case"WhileNode":return t.condVar=e.condVar,t.condBody=r(e.condBody),t.loopBody=r(e.loopBody),t._setChild("condBody",t.condBody),t._setChild("loopBody",t.loopBody),t;default:for(let s of Object.keys(e)){if(s==="_parent"||s==="_parentKey"||s==="_parentIdx")continue;let o=e[s];o instanceof Map?t[s]=new Map(o):Array.isArray(o)?t[s]=o.map(r):typeof o=="object"&&o!==null&&o.type?t[s]=r(o):t[s]=o}return t}})}function Ti(n){return wb(n)}function Iu(n){return wb(n)}function Tv(n){return wb(n)}function RL(n,e,t){let r=t.get(e);if(!r)return null;let s=[],o=r._parent;for(;o&&o!==n;)o.type==="ForNode"&&s.push(o),o=o._parent;s.reverse();let i=Tv(r);for(let p=s.length-1;p>=0;p--){let c=s[p],f=new he(c.loopVar,Tv(c.min),Tv(c.extent),c.kind,i,c.threadTag);f._setChild("body",i),i=f}let a=new Map;for(let p of r.reads)a.set(p.buffer.name,p.buffer);for(let p of r.writes)a.set(p.buffer.name,p.buffer);let u=[];for(let p of n.params)a.has(p.name)&&u.push(p);return new Pi("__tune_"+e,u,i,a,[])}var Id=M(()=>{"use strict";Fe();Gx();l(wb,"deepCloneIR");l(Ti,"clonePrimFunc");l(Iu,"cloneTensorIR");l(Tv,"cloneBlockSubtree");l(RL,"extractBlockMini")});function kv(n,e){let t=[];for(let r=1;r<=e;r*=2)r>=n&&t.push(r);return t}function Cd(n){if(!n)return null;let e=[];for(let t of n){if(!t||t.type!=="VariableNode")return null;e.push(t.name)}return e}function s4(n,e){let t=[n];for(;t.length>0;){let r=t.pop();if(!(!r||typeof r!="object")){if(r.type==="BufferStoreNode"&&r.buffer&&r.buffer.name===e&&r.value&&r.value.type==="MathOpNode"&&r.value.op==="+")return r;if(r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody)}}return null}function Ld(n){if(!n||n.broadcastDims||n.offset&&n.offset!==0)return!1;let e=n.shape,t=n.strides;if(!t||t.length!==e.length)return!1;let r=1;for(let s=e.length-1;s>=0;s--){if(typeof e[s]!="number"||e[s]<=0||t[s]!==r)return!1;r*=e[s]}return!0}function Nv(n,e){if(!Tt(n,e))return null;let r=Rd(n.body,e);if(!r||r.reads.length<2||r.writes.length<1)return null;let s=r.writes[0].buffer;if(!s||s.shape.length<2)return null;let o=s.shape.length,i=s4(r.body,s.name);if(!i)return null;let a=Cd(i.indices);if(!a||a.length!==o)return null;let u=i.value,p=l(T=>T&&T.type==="BufferLoadNode"&&T.buffer&&T.buffer.name===s.name,"isCLoad"),c=p(u.a)?u.b:p(u.b)?u.a:null;if(!c||c.type!=="MathOpNode"||c.op!=="*")return null;let f=[c.a,c.b];if(!f.every(T=>T&&T.type==="BufferLoadNode"&&T.buffer))return null;let d=a[o-1],h=a.slice(0,o-1),m=null,g=null;for(let T of f){let v=Cd(T.indices);if(!v||v.length!==o)continue;let A=!0;for(let N=0;N<o-1;N++)if(v[N]!==h[N]){A=!1;break}if(A){m=T,g=v[o-1];break}}if(!m||g==null)return null;let y=f[0]===m?f[1]:f[0],_=Cd(y.indices);if(!_)return null;let b=m.buffer,w=y.buffer;if(b.shape.length!==o||b.dtype!=="f32"||w.dtype!=="f32"||s.dtype!=="f32")return null;for(let T=0;T<o-1;T++)if(b.shape[T]!==s.shape[T])return null;let x=s.shape[o-1],S=b.shape[o-1];if(![x,S].every(T=>typeof T=="number"&&T>0))return null;if(_.length===2&&w.shape.length===2){let T;if(_[0]===g&&_[1]===d)T=!1;else if(_[0]===d&&_[1]===g)T=!0;else return null;if(T){if(w.shape[0]!==x||w.shape[1]!==S)return null}else if(w.shape[0]!==S||w.shape[1]!==x)return null;let v=1;for(let A=0;A<o-1;A++){let N=s.shape[A];if(typeof N!="number"||N<=0)return null;v*=N}return o===2?{A:b,B:w,C:s,M:v,N:x,K:S,transB:T,batch:1}:!Ld(b)||!Ld(s)?null:{A:new me(b.name,[v,S],b.dtype,b.scope),B:w,C:new me(s.name,[v,x],s.dtype,s.scope),M:v,N:x,K:S,transB:T,batch:1}}if(o>=3&&_.length===o&&w.shape.length===o){let T=a.slice(0,o-2);for(let B=0;B<o-2;B++)if(_[B]!==T[B])return null;let v;if(_[o-2]===g&&_[o-1]===d)v=!1;else if(_[o-2]===d&&_[o-1]===g)v=!0;else return null;for(let B=0;B<o-2;B++)if(w.shape[B]!==s.shape[B])return null;if(v){if(w.shape[o-2]!==x||w.shape[o-1]!==S)return null}else if(w.shape[o-2]!==S||w.shape[o-1]!==x)return null;let A=s.shape[o-2];if(typeof A!="number"||A<=0)return null;let N=1;for(let B=0;B<o-2;B++){let E=s.shape[B];if(typeof E!="number"||E<=0)return null;N*=E}if(!Ld(b)||!Ld(w)||!Ld(s))return null;let k=new me(b.name,[N,A,S],b.dtype,b.scope),O=new me(w.name,v?[N,x,S]:[N,S,x],w.dtype,w.scope),R=new me(s.name,[N,A,x],s.dtype,s.scope);return{A:k,B:O,C:R,M:A,N:x,K:S,transB:v,batch:N}}return null}function CL(n,e,t=32){let r=n.maxThreadsPerBlock||1024,s=n.warpSize||32,o=n.sharedMemoryBytes||49152,i=n.registersPerThread||255,a=4,{M:u,N:p,K:c}=e,f=kv(2,8),d=kv(2,s/2),h=kv(s/4,s/2),m=2*s,g=Math.min(r,8*s),y=4*s,_=new Set,b=[];for(let w of f)for(let x of f)if(!(w*x>y)&&!(w*x+w+x+s>i))for(let S of d)for(let T of d){let v=T*S;if(v<m||v>g||v%s!==0)continue;let A=w*S,N=x*T;if(!(A>u||N>p||A>y||N>y))for(let k of h){if(k>c||(A*k+k*N)*a>o)continue;let R=`${A}_${N}_${k}_${w}_${x}`;_.has(R)||(_.add(R),b.push({BM:A,BN:N,BK:k,TM:w,TN:x,threads:v}))}}return b.sort((w,x)=>EL(x,s)-EL(w,s)),b.slice(0,t)}function EL(n,e){let t=n.TM*n.TN,r=-Math.abs(n.TM-n.TN),s=-Math.abs(Math.log2(n.BM)-Math.log2(n.BN)),o=n.threads>=4*e&&n.threads<=8*e?1:0,i=n.BK===e/4?1:0;return o*100+t*4+r*6+s*4+i}function Tb(n,e){if(!e)return null;let t=CL(n,e,256);return t.find(s=>s.BM===64&&s.BN===64&&s.BK===8&&s.TM===4&&s.TN===4)||(t.length>0?t[0]:null)}function o4(n,e,t,r){let s=Iu(n.storeValue);return Gi(s,o=>o.type==="BufferLoadNode"&&o.buffer?o.buffer.name===n.inputName?e:n.scalarConsts.has(o.buffer.name)?Iu(n.scalarConsts.get(o.buffer.name)):o:o.type==="VariableNode"?o.name===n.iv0?Iu(t):o.name===n.iv1?Iu(r):o:o)}function Av(n,e,t=null){let{A:r,B:s,C:o,M:i,N:a,K:u,transB:p}=n,c=n.batch||1,{BM:f,BN:d,BK:h,TM:m,TN:g}=e,y=d/g,_=f/m,b=y*_,w=Math.ceil(u/h),x=f*h,S=h*d,T=Math.ceil(x/b),v=Math.ceil(S/b),A=i%f!==0,N=a%d!==0,k=u%h!==0,O=x%b!==0,R=S%b!==0,B=new me("rb_As",[x],"f32","shared"),E=new me("rb_Bs",[S],"f32","shared"),F=new me("rb_acc",[m*g],"f32","local"),C=new me("rb_af",[m],"f32","local"),H=new me("rb_bf",[g],"f32","local"),G=Jt("rb_bx"),q=Jt("rb_by"),D=Jt("rb_tx"),V=Jt("rb_ty"),ue=Jt("rb_tid"),ce=Jt("rb_brow"),Le=Jt("rb_bcol"),j=Jt("rb_k0"),W=Jt("rb_bz"),ie=c>1,je=l((Ot,lo)=>ie?[W,Ot,lo]:[Ot,lo],"gIdx"),ke=l((Ot,lo)=>Jn(Fr(Ot,nt(g)),lo),"accIdx"),rt=Jt("rb_im"),zt=Jt("rb_in"),on=xi(rt,m,xi(zt,g,new J(F,[ke(rt,zt)],xv()))),Zt=Jt("rb_la"),it=Jt("rb_aidx"),ye=Jn(ce,ML(it,nt(h))),Xe=Jn(j,IL(it,nt(h))),Nn=new U(r,je(ye,Xe));if(A||k){let Ot=A?so(ye,nt(i)):null;k&&(Ot=Ot?Sv(Ot,so(Xe,nt(u))):so(Xe,nt(u))),Nn=new Se(Ot,Nn,xv())}let Qt=new J(B,[it],Nn);O&&(Qt=new Se(so(it,nt(x)),Qt));let an=vv(Zt,T,new et(it,Jn(ue,Fr(Zt,nt(b))),Qt)),un=Jt("rb_lb"),mn=Jt("rb_bidx"),cr=Jn(j,ML(mn,nt(d))),Vn=Jn(Le,IL(mn,nt(d))),Or=new U(s,p?je(Vn,cr):je(cr,Vn));if(k||N){let Ot=k?so(cr,nt(u)):null;N&&(Ot=Ot?Sv(Ot,so(Vn,nt(a))):so(Vn,nt(a))),Or=new Se(Ot,Or,xv())}let pr=new J(E,[mn],Or);R&&(pr=new Se(so(mn,nt(S)),pr));let yt=vv(un,v,new et(mn,Jn(ue,Fr(un,nt(b))),pr)),fr=Jt("rb_kk"),xt=Jt("rb_fi"),Vr=Jt("rb_fj"),Ds=xi(xt,m,new J(C,[xt],new U(B,[Jn(Fr(Jn(Fr(V,nt(m)),xt),nt(h)),fr)]))),uo=xi(Vr,g,new J(H,[Vr],new U(E,[Jn(Fr(fr,nt(d)),Jn(Fr(D,nt(g)),Vr))]))),Gr=Jt("rb_mi"),dr=Jt("rb_ni"),Br=xi(Gr,m,xi(dr,g,new J(F,[ke(Gr,dr)],Jn(new U(F,[ke(Gr,dr)]),Fr(new U(C,[Gr]),new U(H,[dr])))))),Uo=xi(fr,h,new te([Ds,uo,Br])),jr=Jt("rb_kt"),qr=new et(j,Fr(jr,nt(h)),new te([an,yt,new Fn,Uo,new Fn])),Ps=vv(jr,w,qr),ms=Jt("rb_wm"),va=Jt("rb_wn"),Oi=Jn(Jn(ce,Fr(V,nt(m))),ms),Bi=Jn(Jn(Le,Fr(D,nt(g))),va),ka=new U(F,[ke(ms,va)]),tp=t?t.outBuffer:o,zu=t?o4(t,ka,Oi,Bi):ka,Na=new J(tp,je(Oi,Bi),zu);if(A||N){let Ot=A?so(Oi,nt(i)):null;N&&(Ot=Ot?Sv(Ot,so(Bi,nt(a))):so(Bi,nt(a))),Na=new Se(Ot,Na)}let Aa=xi(ms,m,xi(va,g,Na)),Wo=new te([on,Ps,Aa]),Oa=new qe(F,"local",new qe(C,"local",new qe(H,"local",Wo))),Ri=new et(ue,Jn(Fr(V,nt(y)),D),new et(ce,Fr(q,nt(f)),new et(Le,Fr(G,nt(d)),Oa))),Ko=Math.ceil(a/d),Mi=Math.ceil(i/f),Ba=Ed(q,"blockIdx.y",Mi,Ed(G,"blockIdx.x",Ko,Ed(V,"threadIdx.y",_,Ed(D,"threadIdx.x",y,Ri))));return ie&&(Ba=Ed(W,"blockIdx.z",c,Ba)),new qe(B,"shared",new qe(E,"shared",Ba))}function i4(n){let e=new Os("config_index",n.map((r,s)=>s)),t=new $r("matmul_register_block_gpu",[e],(r,s,o,i)=>{let a=Nv(r.func,s);if(!a)return;let u=n[i.config_index];if(!u)return;let p=Av(a,u);r.func.body=p,r.func._setChild&&r.func._setChild("body",p),r.func.gpuRegisterBlocked=!0});return t.configs=n,t.enumerate=()=>n.map((r,s)=>({config_index:s})),t}function a4(n){let e=Fo(n.body),t=null;for(let s of e){let o=Md(n,s);if(o.hasReduction&&o.spatial>=2&&o.reads>=2){if(t)return null;t=s}}if(!t)return null;let r=Nv(n,t);if(!r)return null;for(let s of e){if(s===t)continue;let o=Tt(n,s);if(!o||o.hasReduction||o.readBuffers.length>0)return null;for(let i of o.writeBuffers)if(i!==r.C.name)return null}return{reductionBlock:t,dims:r}}function u4(n){let e=new Map,t=new Map;Us(n,r=>{if(r.type==="BufferStoreNode"&&r.buffer){let s=r.buffer.name;t.set(s,(t.get(s)||0)+1),(!r.indices||r.indices.length===0)&&r.value&&(r.value.type==="FloatImmNode"||r.value.type==="IntImmNode")&&e.set(s,r.value)}});for(let r of[...e.keys()])(t.get(r)||0)!==1&&e.delete(r);return e}function l4(n,e){let t=null;return Us(n,r=>{if(r.type==="BufferStoreNode"&&r.buffer&&r.buffer.name===e)return t=r,Om}),t}function c4(n,e){let t=null,r=0;return Us(n,s=>{s.type==="BufferLoadNode"&&s.buffer&&s.buffer.name===e&&(r++,t=s)}),r===1?t:null}function DL(n){let e=Fo(n.body),t=null;for(let y of e){let _=Md(n,y);if(_.hasReduction&&_.spatial>=2&&_.reads>=2){if(t)return null;t=y}}if(!t)return null;let r=Nv(n,t);if(!r)return null;let s=r.C.name,o=new Set;for(let y of e){let _=Tt(n,y);if(!_)return null;for(let b of _.writeBuffers)o.add(b)}let i=[];for(let y of e){if(y===t)continue;let _=Tt(n,y);if(_.hasReduction)return null;if(_.readBuffers.length===0){if(_.writeBuffers.every(b=>b===s))continue;return null}i.push({name:y,info:_})}if(i.length===0)return{reductionBlock:t,dims:r,epilogue:null};if(i.length!==1||(r.batch||1)!==1)return null;let a=i[0];if(a.info.writeBuffers.length!==1)return null;let u=a.info.writeBuffers[0],p=u4(n.body),c=0;for(let y of a.info.readBuffers){if(y===s){c++;continue}if(o.has(y)&&!p.has(y))return null}if(c!==1)return null;let f=Rd(n.body,a.name);if(!f)return null;let d=l4(f.body,u);if(!d)return null;let h=Cd(d.indices);if(!h||h.length!==2)return null;let m=c4(d.value,s);if(!m)return null;let g=Cd(m.indices);return!g||g.length!==h.length||g.some((y,_)=>y!==h[_])?null:{reductionBlock:t,dims:r,epilogue:{outBuffer:d.buffer,storeValue:d.value,inputName:s,iv0:h[0],iv1:h[1],scalarConsts:p}}}function PL(n,e,t){let r=a4(n);if(!r)return null;let s=LL.get(n);if(s===void 0){let o=CL(t,r.dims);s=o.length>0?i4(o):null,LL.set(n,s)}return s?e===r.reductionBlock?[s]:[]:null}var nt,xv,Jt,Jn,Fr,ML,IL,so,Sv,vv,xi,Ed,LL,xb=M(()=>{"use strict";Fe();ei();Do();_b();Bc();ts();Id();nt=l(n=>new L(n),"I"),xv=l(()=>new de(0),"FZERO"),Jt=l(n=>new Oe(n,"i32"),"IV"),Jn=l((n,e)=>qn("+",n,e),"ADD"),Fr=l((n,e)=>qn("*",n,e),"MUL"),ML=l((n,e)=>qn("//",n,e),"DIV"),IL=l((n,e)=>qn("%",n,e),"MOD"),so=l((n,e)=>new Ke("lt",n,e),"LT"),Sv=l((n,e)=>new P("&&",n,e),"AND"),vv=l((n,e,t)=>new he(n,nt(0),nt(e),z.SERIAL,t),"forS"),xi=l((n,e,t)=>new he(n,nt(0),nt(e),z.UNROLLED,t),"forU"),Ed=l((n,e,t,r)=>new he(n,nt(0),nt(t),z.THREAD_BINDING,r,e),"forT");l(kv,"pow2Range");l(Cd,"plainVars");l(s4,"findAccStore");l(Ld,"isContiguousRowMajor");l(Nv,"matmulTileDims");l(CL,"enumerateRegisterBlockConfigs");l(EL,"goodness");l(Tb,"pickFixedConfig");l(o4,"foldEpilogue");l(Av,"buildRegisterBlockedMatmul");l(i4,"createMatmulRegisterBlockGPUSketch");l(a4,"analyzePureMatmul");l(u4,"collectScalarConstBuffers");l(l4,"findEpilogueStore");l(c4,"singleBufferLoad");l(DL,"analyzeMatmulEpilogue");LL=new WeakMap;l(PL,"richMatmulSketches")});function p4(n,e){let t=[n];for(;t.length;){let r=t.pop();if(!(!r||typeof r!="object")&&(r.type==="BlockNode"&&e(r),r.body&&t.push(r.body),r.initBody&&t.push(r.initBody),r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),Array.isArray(r.stmts)))for(let s of r.stmts)t.push(s)}}function Pd(n){let e=[];p4(n.body,r=>{e.push({name:r.name,node:r,reads:(r.reads||[]).map(s=>s.buffer&&s.buffer.name).filter(Boolean),writes:(r.writes||[]).map(s=>s.buffer&&s.buffer.name).filter(Boolean)})});let t=new Map;for(let r of e)for(let s of r.reads)t.has(s)||t.set(s,[]),t.get(s).push(r.name);return{blocks:e,consumersByBuf:t,byName:new Map(e.map(r=>[r.name,r]))}}function Ov(n){let e=[];for(let t of n){if(!t||t.type!=="VariableNode")return null;e.push(t.name)}return e}function Dd(n,e,t){if(!n||typeof n!="object")return t;n.type==="BufferLoadNode"&&n.buffer&&n.buffer.name===e&&t.push(n);for(let r of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])n[r]&&Dd(n[r],e,t);if(Array.isArray(n.args))for(let r of n.args)Dd(r,e,t);if(Array.isArray(n.indices))for(let r of n.indices)Dd(r,e,t);if(Array.isArray(n.stmts))for(let r of n.stmts)Dd(r,e,t);return t}function Rc(n,e){if(!n||typeof n!="object")return e;n.type==="BufferLoadNode"&&n.buffer&&n.buffer.name&&e.add(n.buffer.name);for(let t of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])n[t]&&Rc(n[t],e);if(Array.isArray(n.args))for(let t of n.args)Rc(t,e);if(Array.isArray(n.indices))for(let t of n.indices)Rc(t,e);if(Array.isArray(n.stmts))for(let t of n.stmts)Rc(t,e);return e}function Sb(n,e,t,r){let s=e.byName.get(t);if(!s||s.writes.length!==1)return null;let o=s.writes[0],i=[...new Set((e.consumersByBuf.get(o)||[]).filter(w=>w!==t))];if(i.length!==1)return null;let a=i[0],u=r(n,t),p=r(n,a);if(!u||!p||p.hasReduction)return null;let c=e.byName.get(a);if(!c||c.writes.length!==1||c.writes[0]===o)return null;let f=new Set(s.reads);if(Rc(s.node.body,f),Rc(s.node.initBody,f),f.has(c.writes[0]))return null;let d=s.node.body&&s.node.body.type==="BufferStoreNode"?s.node.body:null,h=c.node.body&&c.node.body.type==="BufferStoreNode"?c.node.body:null;if(!d||!h)return null;let m=u.loops.filter(w=>!u.reductionLoopVars.has(w.loopVar.name)).map(w=>w.loopVar.name),g=Ov(d.indices);if(!g||g.join(",")!==m.join(","))return null;let y=p.loops.map(w=>w.loopVar.name);if(y.length!==m.length)return null;let _=Ov(h.indices);if(!_||_.join(",")!==y.join(","))return null;let b=Dd(h.value,o,[]);if(b.length===0)return null;for(let w of b){let x=Ov(w.indices);if(!x||x.join(",")!==_.join(","))return null}return a}var Bv=M(()=>{"use strict";l(p4,"walkBlocks");l(Pd,"buildBlockDAG");l(Ov,"indexVarNames");l(Dd,"collectLoads");l(Rc,"collectLoadedBuffers");l(Sb,"findFusibleConsumer")});function Rv(n){return n.isGPU()?OL():AL()}function f4(n){return n.isGPU()?NL():kL()}function d4(n,e,t,r){let s=Tt(n,e);if(!s)return[Rv(t)];let o=[],i=_L(s,pL(t));if(i&&o.push(i),t.kind===Dt.CPU){let a=bL(s,cL);a&&o.push(a);let u=vL(s);u&&o.push(u);let p=r?Sb(n,r,e,Tt):null;p&&o.push(SL(p))}return o.push(Rv(t)),o}function Mv(n,{priority:e=100}={}){vb.some(t=>t.derive===n.derive&&t.matches===n.matches)||(vb.push({matches:n.matches,derive:n.derive,priority:e}),vb.sort((t,r)=>t.priority-r.priority))}function $L(n,e,t,r={}){if(r.richGpu&&t.isGPU()){let i=PL(n,e,t);if(i!==null)return i}if(t.kind!==Dt.CPU&&!t.isGPU())return[];let s=Md(n,e),o=r.dag||Pd(n);for(let i of vb)if(i.matches(s,t))return i.derive(n,e,t,o);return[]}var vb,FL=M(()=>{"use strict";yo();Do();Bc();bv();wL();BL();xb();Bv();l(Rv,"reductionSketch");l(f4,"elementwiseSketch");l(d4,"deriveMultiLevel");vb=[];l(Mv,"registerSketchRule");Mv({matches:l(n=>n.hasReduction&&n.spatial>=1&&n.reads>=2,"matches"),derive:d4},{priority:10});Mv({matches:l(n=>n.hasReduction,"matches"),derive:l((n,e,t)=>[Rv(t)],"derive")},{priority:20});Mv({matches:l(()=>!0,"matches"),derive:l((n,e,t)=>[f4(t)],"derive")},{priority:30});l($L,"deriveSketches")});function Iv(n,e,t,r,s={}){return $L(n,e,t,s)}var zL=M(()=>{"use strict";FL();l(Iv,"getSketchesForBlock")});function kb(n){let e=new Map,t=[n];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="BlockNode"&&e.set(r.name,r),r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),r.initBody&&t.push(r.initBody)}}return e}function Nb(n,e,t,r=null){let s=r||kb(n.body),o=s.get(e)||null,i=[];if(o){let a=[];for(let p of o.reads)a.push(`${p.buffer.shape.join("x")}:${p.buffer.dtype}`);for(let p of o.writes)a.push(`${p.buffer.shape.join("x")}:${p.buffer.dtype}`);i.push(a.join(","));let u=[];if(sn(o.body,u),o.initBody&&sn(o.initBody,u),i.push(u.join(";")),o.writes.length===1){let p=o.writes[0].buffer.name,c=[];for(let f of h4(s).get(p)||[]){if(f===o)continue;let d=[];sn(f.body,d),f.initBody&&sn(f.initBody,d),c.push(d.join(";"))}c.length>0&&(c.sort(),i.push(`consumers:${c.join("|")}`))}}return i.push(t.name),i.push(t.kind),m4(i.join("|"))}function h4(n){if(n.__readersByBuffer)return n.__readersByBuffer;let e=new Map;for(let t of n.values()){if(!t.reads)continue;let r=new Set;for(let s of t.reads)s.buffer&&r.add(s.buffer.name);for(let s of r){let o=e.get(s);o||(o=[],e.set(s,o)),o.push(t)}}return n.__readersByBuffer=e,e}function sn(n,e){if(!(!n||typeof n!="object"))switch(n.type){case"BufferStoreNode":if(e.push("store"),n.indices)for(let t of n.indices)sn(t,e);sn(n.value,e);return;case"BufferLoadNode":if(e.push(`load:${n.buffer.shape.join("x")}:${n.buffer.dtype}`),n.indices)for(let t of n.indices)sn(t,e);return;case"MathOpNode":e.push(`math:${n.op}`),sn(n.a,e),n.b&&sn(n.b,e);return;case"CallExternNode":e.push(`call:${n.externName}`);for(let t of n.args)sn(t,e);return;case"CompareNode":e.push(`cmp:${n.direction}`),sn(n.a,e),sn(n.b,e);return;case"CastNode":e.push(`cast:${n.fromDtype}->${n.toDtype}`),sn(n.expr,e);return;case"IfThenElseNode":sn(n.condition,e),sn(n.thenBody,e),n.elseBody&&sn(n.elseBody,e);return;case"SeqNode":for(let t of n.stmts)sn(t,e);return;case"ForNode":sn(n.body,e);return;case"BlockNode":n.initBody&&sn(n.initBody,e),sn(n.body,e);return;case"LetStmtNode":sn(n.value,e),sn(n.body,e);return;default:return}}function m4(n){let e=2166136261;for(let t=0;t<n.length;t++)e^=n.charCodeAt(t),e=e*16777619>>>0;return e.toString(16).padStart(8,"0")}var Ev=M(()=>{"use strict";l(kb,"buildBlockMap");l(Nb,"computeWorkloadKey");l(h4,"readersByBuffer");l(sn,"collectBlockOps");l(m4,"fnv1a")});function g4(n,e){let t=n.medianMs!=null?1:0,r=e.medianMs!=null?1:0;return t!==r?r-t:t===1?n.medianMs-e.medianMs:e.score-n.score}var VL,$d,Fd,GL=M(()=>{"use strict";Ev();VL="mlfw-codegen-1",$d=class{static{l(this,"TuningRecord")}constructor(e,t,r,s,o,i){this.workloadKey=e,this.sketchName=t,this.params=r,this.score=s,this.traceData=o,this.version=i,this.timestamp=Date.now(),this.medianMs=null,this.minMs=null}};l(g4,"rankRecords");Fd=class n{static{l(this,"TuningDatabase")}constructor(e=1){this.version=e,this._records=new Map}computeWorkloadKey(e,t,r,s=null){return Nb(e,t,r,s)}store(e,t){let r=this._records.get(e);r||(r=[],this._records.set(e,r)),r.push(t),r.sort(g4),r.length>10&&(r.length=10)}lookup(e){let t=this._records.get(e);return!t||t.length===0?null:t[0]}lookupTopK(e,t=5){let r=this._records.get(e);return r?r.slice(0,t):[]}has(e){return this._records.has(e)&&this._records.get(e).length>0}get size(){let e=0;for(let[,t]of this._records)e+=t.length;return e}serialize(){let e=[];for(let[t,r]of this._records)for(let s of r)e.push({workloadKey:s.workloadKey,sketchName:s.sketchName,params:s.params,score:s.score,traceData:s.traceData,version:s.version,timestamp:s.timestamp,medianMs:s.medianMs,minMs:s.minMs});return{version:this.version,codegenVersion:VL,entries:e}}static deserialize(e){let t=new n(e.version);if(e.codegenVersion!==void 0&&e.codegenVersion!==VL)return t;for(let r of e.entries){let s=new $d(r.workloadKey,r.sketchName,r.params,r.score,r.traceData,r.version);s.timestamp=r.timestamp,s.medianMs=r.medianMs??null,s.minMs=r.minMs??null,t.store(r.workloadKey,s)}return t}saveToFile(e,t){return t.writeFile(e,JSON.stringify(this.serialize())),e}static loadFromFile(e,t){return t.exists(e)?n.deserialize(JSON.parse(t.readFile(e))):new n}clear(){this._records.clear()}}});function ga(n,e){if(!n)return 0;if(n.type==="ForNode"){let t=n.extent&&n.extent.type==="IntImmNode"?n.extent.value:1,r=n.threadTag?e:e*t;return ga(n.body,r)}if(n.type==="BlockNode")return Math.max(e,ga(n.body,e),n.initBody?ga(n.initBody,e):0);if(n.type==="SeqNode"){let t=e;for(let r of n.stmts)t=Math.max(t,ga(r,e));return t}return n.type==="IfThenElseNode"?Math.max(ga(n.thenBody,e),n.elseBody?ga(n.elseBody,e):e):n.type==="AllocateNode"||n.type==="LetStmtNode"?ga(n.body,e):e}function jL(n,e=.1){let t=n.slice().sort((g,y)=>g-y),r=t.length;if(r===0)return{median:0,min:0,trimmedMean:0,cv:0};let s=t[r>>1],o=t[0],i=Math.floor(r*e),a=i,u=r-i>a?r-i:r,p=0,c=0;for(let g=a;g<u;g++)p+=t[g],c++;let f=c>0?p/c:s,d=0;for(let g=a;g<u;g++){let y=t[g]-f;d+=y*y}let h=c>1?Math.sqrt(d/(c-1)):0,m=f>0?h/f:0;return{median:s,min:o,trimmedMean:f,cv:m}}var y4,Ab,Ob,qL=M(()=>{"use strict";Op();y4=1e6;l(ga,"maxSerialTripCount");Ab=class{static{l(this,"BenchmarkResult")}constructor(e,t,r,s,o=null,i=0){this.medianMs=e,this.minMs=t,this.trimmedMeanMs=o??e,this.cv=i,this.samples=r,this._totalBytes=s}get throughputGBs(){return this.minMs<=0||!this._totalBytes?0:this._totalBytes/(this.minMs*1e6)}};l(jL,"robustStats");Ob=class{static{l(this,"BenchmarkRunner")}constructor(e,t={}){this.target=e,this.warmup=t.warmup??3,this.repeat=t.repeat??10,this.minRepeatMs=t.minRepeatMs??0,this.maxCv=t.maxCv??0,this.maxReMeasures=t.maxReMeasures??1,this.measurer=t.measurer||null,this._warn=t.warn||null,this._bufferCache=new Map}_record(e,t){this._warn&&this._warn(e,null,t)}_getOrAllocBuffers(e){let t=0,r=[];for(let[,i]of e.bufferMap){let a=Math.max(i.numel(),1);r.push(a);let u=i.sizeInBytes();u>0&&(t+=u)}let s=r.join(","),o=this._bufferCache.get(s);o||(o=r.map(i=>new Float32Array(i)),this._bufferCache.set(s,o));for(let i of o)for(let a=0;a<i.length;a++)i[a]=Math.random()*2-1;return{buffers:o,totalBytes:t}}run(e){if(!e||!e.body)return null;if(!this.target.isCPU())return this.measurer?this._runMeasured(e):null;let t=new _o(this.target),r;try{r=t.compile(e)}catch(p){return this._record("benchmark-compile",p),null}if(r.metadata.kind!=="js")return null;let s;try{s=new Function("return "+r.source)()}catch(p){return this._record("benchmark-construct-fn",p),null}let{buffers:o,totalBytes:i}=this._getOrAllocBuffers(e);for(let p=0;p<this.warmup;p++)try{s(...o)}catch(c){return this._record("benchmark-warmup-run",c),null}let a=[],u=null;for(let p=0;p<=this.maxReMeasures&&(this._collect(s,o,a),u=jL(a),!(this.maxCv<=0||u.cv<=this.maxCv));p++);return new Ab(u.median,u.min,a,i,u.trimmedMean,u.cv)}_runMeasured(e){if(e.shapeParams&&e.shapeParams.length>0||ga(e.body,1)>y4)return null;let t;try{t=new _o(this.target).compile(e)}catch(a){return this._record("measured-compile",a),null}let r=[],s=0;for(let[,a]of e.bufferMap){let u=Math.max(a.sizeInBytes(),1);r.push(u),s+=u}let o;try{o=this.measurer(t,r,[],{warmup:this.warmup,repeat:this.repeat})}catch(a){return this._record("measurer",a),null}if(!o||o.length===0)return null;let i=jL(o);return new Ab(i.median,i.min,o,s,i.trimmedMean,i.cv)}_collect(e,t,r){let s=0,o=this.repeat*3;for(let i=0;i<o&&(i<this.repeat||s<this.minRepeatMs);i++){let a=performance.now();e(...t);let u=performance.now()-a;r.push(u),s+=u}}}});var Bb,UL=M(()=>{"use strict";Bb=class{static{l(this,"Deadline")}constructor(e=1/0,t=null){this._clock=t||(()=>performance.now()),this._endAt=e===1/0||e==null?1/0:this._clock()+e}get expired(){return this._clock()>=this._endAt}remainingMs(){return this._endAt===1/0?1/0:Math.max(0,this._endAt-this._clock())}}});function WL(n){let e=null;for(let w of n.ops())if(w.opName==="conv"){if(e)return null;e=w}else if(w.opName!=="return")return null;if(!e||(e.getAttr("groups")||1)!==1)return null;let t=e.getAttr("input_layout"),r=e.getAttr("kernel_layout");if(t&&t!=="NCHW"||r&&r!=="OIHW")return null;let s=e.getOperand(0).type,o=e.getOperand(1).type,i=e.getResult(0).type;if(!s||!o||!i||s.shape.length!==4||o.shape.length!==4||i.shape.length!==4||s.dtype!=="f32"||o.dtype!=="f32"||i.dtype!=="f32")return null;let a=e.getAttr("strides")||[1,1],u=e.getAttr("padding")||[[0,0],[0,0]],p=e.getAttr("dilation")||[1,1],c=s.shape[0],f=s.shape[1],d=s.shape[2],h=s.shape[3],m=o.shape[0],g=o.shape[2],y=o.shape[3],_=i.shape[2],b=i.shape[3];return![c,f,d,h,m,g,y,_,b].every(w=>typeof w=="number"&&w>0)||o.shape[1]!==f||i.shape[1]!==m||i.shape[0]!==c?null:{N:c,Cin:f,H:d,W:h,O:m,Kh:g,Kw:y,Oh:_,Ow:b,sH:a[0],sW:a[1],pH:u[0][0],pW:u[1][0],dH:p[0],dW:p[1]}}function _4(n,e,t){let{weight:r,input:s,output:o}=n,{N:i,Cin:a,H:u,W:p,O:c,Kh:f,Kw:d,Oh:h,Ow:m,sH:g,sW:y,pH:_,pW:b,dH:w,dW:x}=e,{BM:S,BN:T,BK:v,TM:A,TN:N}=t,k=c,O=i*h*m,R=a*f*d,B=f*d,E=h*m,F=T/N,C=S/A,H=F*C,G=Math.ceil(R/v),q=S*v,D=v*T,V=Math.ceil(q/H),ue=Math.ceil(D/H),ce=new me("ig_As",[q],"f32","shared"),Le=new me("ig_Bs",[D],"f32","shared"),j=new me("ig_acc",[A*N],"f32","local"),W=new me("ig_af",[A],"f32","local"),ie=new me("ig_bf",[N],"f32","local"),je=Be("ig_bx"),ke=Be("ig_by"),rt=Be("ig_tx"),zt=Be("ig_ty"),on=Be("ig_tid"),Zt=Be("ig_brow"),it=Be("ig_bcol"),ye=Be("ig_k0"),Xe=l((Hr,Yw)=>Te(xe(Hr,$(N)),Yw),"accIdx"),Nn=Be("ig_im"),Qt=Be("ig_in"),an=kn(Nn,A,kn(Qt,N,new J(j,[Xe(Nn,Qt)],zd()))),un=k%S!==0,mn=R%v!==0,cr=O%T!==0,Vn=q%H!==0,Or=D%H!==0,pr=Be("ig_la"),yt=Be("ig_aidx"),fr=Te(Zt,Dn(yt,$(v))),xt=Te(ye,hn(yt,$(v))),Vr=Dn(xt,$(B)),Ds=hn(xt,$(B)),uo=Dn(Ds,$(d)),Gr=hn(Ds,$(d)),dr=new U(r,[fr,Vr,uo,Gr]);if(un||mn){let Hr=un?kr(fr,$(k)):null;mn&&(Hr=Hr?zo(Hr,kr(xt,$(R))):kr(xt,$(R))),dr=new Se(Hr,dr,zd())}let Br=new J(ce,[yt],dr);Vn&&(Br=new Se(kr(yt,$(q)),Br));let Uo=Rb(pr,V,new et(yt,Te(on,xe(pr,$(H))),Br)),jr=Be("ig_lb"),qr=Be("ig_bidx"),Ps=Te(ye,Dn(qr,$(T))),ms=Te(it,hn(qr,$(T))),va=Dn(Ps,$(B)),Oi=hn(Ps,$(B)),Bi=Dn(Oi,$(d)),ka=hn(Oi,$(d)),tp=Dn(ms,$(E)),zu=hn(ms,$(E)),Na=Dn(zu,$(m)),Aa=hn(zu,$(m)),Wo=Mb(Te(xe(Na,$(g)),xe(Bi,$(w))),$(_)),Oa=Mb(Te(xe(Aa,$(y)),xe(ka,$(x))),$(b)),Ri=new U(s,[tp,va,Wo,Oa]),Ko=zo(zo(Ib(Wo,$(0)),kr(Wo,$(u))),zo(Ib(Oa,$(0)),kr(Oa,$(p))));mn&&(Ko=zo(Ko,kr(Ps,$(R)))),cr&&(Ko=zo(Ko,kr(ms,$(O)))),Ri=new Se(Ko,Ri,zd());let Mi=new J(Le,[qr],Ri);Or&&(Mi=new Se(kr(qr,$(D)),Mi));let Ba=Rb(jr,ue,new et(qr,Te(on,xe(jr,$(H))),Mi)),Ot=Be("ig_kk"),lo=Be("ig_fi"),np=Be("ig_fj"),Vw=kn(lo,A,new J(W,[lo],new U(ce,[Te(xe(Te(xe(zt,$(A)),lo),$(v)),Ot)]))),Gw=kn(np,N,new J(ie,[np],new U(Le,[Te(xe(Ot,$(T)),Te(xe(rt,$(N)),np))]))),Vu=Be("ig_mi"),Gu=Be("ig_ni"),jw=kn(Vu,A,kn(Gu,N,new J(j,[Xe(Vu,Gu)],Te(new U(j,[Xe(Vu,Gu)]),xe(new U(W,[Vu]),new U(ie,[Gu])))))),qw=kn(Ot,v,new te([Vw,Gw,jw])),$s=Be("ig_kt"),gs=new et(ye,xe($s,$(v)),new te([Uo,Ba,new Fn,qw,new Fn])),Rr=Rb($s,G,gs),hr=Be("ig_wm"),Ur=Be("ig_wn"),Ii=Te(Te(Zt,xe(zt,$(A))),hr),co=Te(Te(it,xe(rt,$(N))),Ur),Wr=Dn(co,$(E)),Kr=hn(co,$(E)),Ho=Dn(Kr,$(m)),Qn=hn(Kr,$(m)),Ei=new J(o,[Wr,Ii,Ho,Qn],new U(j,[Xe(hr,Ur)]));if(un||cr){let Hr=un?kr(Ii,$(k)):null;cr&&(Hr=Hr?zo(Hr,kr(co,$(O))):kr(co,$(O))),Ei=new Se(Hr,Ei)}let Yo=kn(hr,A,kn(Ur,N,Ei)),Uw=new te([an,Rr,Yo]),vh=new qe(j,"local",new qe(W,"local",new qe(ie,"local",Uw))),Ww=new et(on,Te(xe(zt,$(F)),rt),new et(Zt,xe(ke,$(S)),new et(it,xe(je,$(T)),vh))),Kw=Math.ceil(O/T),Hw=Math.ceil(k/S),kh=ya(ke,"blockIdx.y",Hw,ya(je,"blockIdx.x",Kw,ya(zt,"threadIdx.y",C,ya(rt,"threadIdx.x",F,Ww))));return new qe(ce,"shared",new qe(Le,"shared",kh))}function KL(n,e,t){if(!HL(n,{M:e.O,N:e.N*e.Oh*e.Ow,K:e.Cin*e.Kh*e.Kw},t))return!1;let{BM:r,BN:s,BK:o,TM:i,TN:a}=t,u=e.O,p=e.N*e.Oh*e.Ow,c=e.Cin*e.Kh*e.Kw;if(u%r!==0||p%s!==0||c%o!==0||[r,s,o,i,a].some(d=>d%Lv!==0))return!1;let f=r/i*(s/a);return!(r*o%(Lv*f)!==0||o*s%f!==0)}function b4(n,e,t){let{weight:r,input:s,output:o}=n,{N:i,Cin:a,H:u,W:p,O:c,Kh:f,Kw:d,Oh:h,Ow:m,sH:g,sW:y,pH:_,pW:b,dH:w,dW:x}=e,{BM:S,BN:T,BK:v,TM:A,TN:N}=t,k=Lv,O=c,R=i*h*m,B=a*f*d,E=f*d,F=h*m,C=u*p,H=a*u*p,G=T/N,q=S/A,D=G*q,V=B/v,ue=S*v,ce=v*T,Le=ue/D,j=ce/D,W=Le/k,ie=v/k,je=(h-1)*g+(f-1)*w-_,ke=(m-1)*y+(d-1)*x-b,rt=_>0||je>=u,zt=b>0||ke>=p,on=new me(r.name,[O*B>0?O*B:1],r.dtype,r.scope),Zt=new me(s.name,[i*H>0?i*H:1],s.dtype,s.scope),it=new me("iv_As",[2*ue],"f32","shared");it.align16=!0;let ye=new me("iv_Bs",[2*ce],"f32","shared");ye.align16=!0;let Xe=new me("iv_acc",[A*N],"f32","local"),Nn=new me("iv_af",[A],"f32","local");Nn.align16=!0;let Qt=new me("iv_bf",[N],"f32","local");Qt.align16=!0;let an=new me("iv_ra",[Le],"f32","local");an.align16=!0;let un=new me("iv_rb",[j],"f32","local"),mn=Be("iv_bx"),cr=Be("iv_by"),Vn=Be("iv_tx"),Or=Be("iv_ty"),pr=Be("iv_tid"),yt=Be("iv_brow"),fr=Be("iv_bcol"),xt=l(($s,gs)=>Te(xe($s,$(N)),gs),"accIdx"),Vr=0,Ds=Be("iv_im"),uo=Be("iv_in"),Gr=kn(Ds,A,kn(uo,N,new J(Xe,[xt(Ds,uo)],zd()))),dr=l($s=>{let gs=Vr++,Rr=Be("iv_la"+gs),hr=Be("iv_c"+gs),Ur=Dn(hr,$(ie)),Ii=xe(hn(hr,$(ie)),$(k)),co=kn(Rr,W,new et(hr,Te(pr,xe(Rr,$(D))),new al(an,xe(Rr,$(k)),on,Te(xe(Te(yt,Ur),$(B)),Te($s,Ii)),k))),Wr=Be("iv_lb"+gs),Kr=Be("iv_e"+gs),Ho=Dn(Kr,$(T)),Qn=hn(Kr,$(T)),Ei=Te(fr,Qn),Yo=Te($s,Ho),Uw=Dn(Yo,$(E)),vh=hn(Yo,$(E)),Ww=Dn(vh,$(d)),Kw=hn(vh,$(d)),Hw=Dn(Ei,$(F)),kh=hn(Ei,$(F)),Hr=Dn(kh,$(m)),Yw=hn(kh,$(m)),Jw=Mb(Te(xe(Hr,$(g)),xe(Ww,$(w))),$(_)),Zw=Mb(Te(xe(Yw,$(y)),xe(Kw,$(x))),$(b)),GD=Te(Te(Te(xe(Hw,$(H)),xe(Uw,$(C))),xe(Jw,$(p))),Zw),Qw=new U(Zt,[GD]),ju=null;if(rt&&(ju=zo(Ib(Jw,$(0)),kr(Jw,$(u)))),zt){let hN=zo(Ib(Zw,$(0)),kr(Zw,$(p)));ju=ju?zo(ju,hN):hN}ju&&(Qw=new Se(ju,Qw,zd()));let jD=kn(Wr,j,new et(Kr,Te(pr,xe(Wr,$(D))),new J(un,[Wr],Qw)));return new te([co,jD])},"prefetch"),Br=l(($s,gs)=>{let Rr=Vr++,hr=Be("iv_la"+Rr),Ur=Be("iv_c"+Rr),Ii=Dn(Ur,$(ie)),co=xe(hn(Ur,$(ie)),$(k)),Wr=[];for(let Yo=0;Yo<k;Yo++)Wr.push(new J(it,[Te($s,Te(xe(Te(co,$(Yo)),$(S)),Ii))],new U(an,[Te(xe(hr,$(k)),$(Yo))])));let Kr=kn(hr,W,new et(Ur,Te(pr,xe(hr,$(D))),new te(Wr))),Ho=Be("iv_lb"+Rr),Qn=Be("iv_e"+Rr),Ei=kn(Ho,j,new et(Qn,Te(pr,xe(Ho,$(D))),new J(ye,[Te(gs,Qn)],new U(un,[Ho]))));return new te([Kr,Ei])},"commit"),Uo=l(($s,gs)=>{let Rr=Vr++,hr=Be("iv_kk"+Rr),Ur=[],Ii=Te($s,Te(xe(hr,$(S)),xe(Or,$(A))));for(let Qn=0;Qn<A/k;Qn++)Ur.push(new al(Nn,$(Qn*k),it,Te(Ii,$(Qn*k)),k));let co=Te(gs,Te(xe(hr,$(T)),xe(Vn,$(N))));for(let Qn=0;Qn<N/k;Qn++)Ur.push(new al(Qt,$(Qn*k),ye,Te(co,$(Qn*k)),k));let Wr=Be("iv_mi"+Rr),Kr=Be("iv_ni"+Rr),Ho=kn(Wr,A,kn(Kr,N,new J(Xe,[xt(Wr,Kr)],Te(new U(Xe,[xt(Wr,Kr)]),xe(new U(Nn,[Wr]),new U(Qt,[Kr]))))));return kn(hr,v,new te([...Ur,Ho]))},"computeMMA"),jr=Be("iv_kt"),qr=Be("iv_p"),Ps=Be("iv_pN"),ms=xe(qr,$(ue)),va=xe(qr,$(ce)),Oi=xe(Ps,$(ue)),Bi=xe(Ps,$(ce)),ka=kr(jr,$(V-1)),tp=new te([dr($(0)),Br($(0),$(0)),new Fn]),zu=new et(qr,hn(jr,$(2)),new et(Ps,hn(Te(jr,$(1)),$(2)),new te([new Se(ka,dr(xe(Te(jr,$(1)),$(v)))),Uo(ms,va),new Se(ka,new te([Br(Oi,Bi),new Fn]))]))),Na=Rb(jr,V,zu),Aa=Be("iv_wm"),Wo=Be("iv_wn"),Oa=Te(Te(yt,xe(Or,$(A))),Aa),Ri=Te(Te(fr,xe(Vn,$(N))),Wo),Ko=Dn(Ri,$(F)),Mi=hn(Ri,$(F)),Ba=Dn(Mi,$(m)),Ot=hn(Mi,$(m)),lo=new J(o,[Ko,Oa,Ba,Ot],new U(Xe,[xt(Aa,Wo)])),np=kn(Aa,A,kn(Wo,N,lo)),Vw=new te([Gr,tp,Na,np]),Gw=new qe(Xe,"local",new qe(Nn,"local",new qe(Qt,"local",new qe(an,"local",new qe(un,"local",Vw))))),Vu=new et(pr,Te(xe(Or,$(G)),Vn),new et(yt,xe(cr,$(S)),new et(fr,xe(mn,$(T)),Gw))),Gu=R/T,jw=O/S,qw=ya(cr,"blockIdx.y",jw,ya(mn,"blockIdx.x",Gu,ya(Or,"threadIdx.y",q,ya(Vn,"threadIdx.x",G,Vu))));return new qe(it,"shared",new qe(ye,"shared",qw))}function HL(n,e,t){let{BM:r,BN:s,BK:o,TM:i,TN:a}=t;if(![r,s,o,i,a].every(m=>typeof m=="number"&&m>0)||r%i!==0||s%a!==0)return!1;let u=s/a,p=r/i,c=u*p,f=n.warpSize||32;return!(c%f!==0||c>(n.maxThreadsPerBlock||1024)||(r*o+o*s)*4*2>(n.sharedMemoryBytes||49152)||i*a+i+a+f>(n.registersPerThread||255))}function T4(n,e){for(let t of w4)if(KL(n,e,t))return t;return null}function YL(n,e,t){let r=n.func,s=r.convInfo;if(!s)return!1;let i=Fo(r.body).find(_=>/^conv_acc_/.test(_));if(!i)return!1;let a=Rd(r.body,i);if(!a||a.reads.length<2||a.writes.length<1)return!1;let u=a.reads[0].buffer,p=a.reads[1].buffer,c=a.writes[0].buffer;if(!u||!p||!c)return!1;let f=s.O,d=s.N*s.Oh*s.Ow,h=s.Cin*s.Kh*s.Kw;if(h<128||d<64)return!1;let m=t&&t.convConfig,g=t&&t.convNoVec,y=null;if(!g){let _=m?KL(e,s,m)?m:null:T4(e,s);_&&(y=b4({weight:p,input:u,output:c},s,_))}if(!y){let _=m?HL(e,{M:f,N:d,K:h},m)?m:null:Tb(e,{M:f,N:d,K:h});if(!_)return!1;y=_4({weight:p,input:u,output:c},s,_)}return n.func.body=y,n.func._setChild&&n.func._setChild("body",y),n.func.gpuRegisterBlocked=!0,!0}var $,zd,Be,Te,Mb,xe,Dn,hn,kr,Ib,zo,Rb,kn,ya,Lv,w4,Cv=M(()=>{"use strict";Fe();ei();Bc();xb();$=l(n=>new L(n),"I"),zd=l(()=>new de(0),"FZERO"),Be=l(n=>new Oe(n,"i32"),"IV"),Te=l((n,e)=>new P("+",n,e),"ADD"),Mb=l((n,e)=>new P("-",n,e),"SUB"),xe=l((n,e)=>new P("*",n,e),"MUL"),Dn=l((n,e)=>new P("//",n,e),"DIV"),hn=l((n,e)=>new P("%",n,e),"MOD"),kr=l((n,e)=>new Ke("lt",n,e),"LT"),Ib=l((n,e)=>new Ke("ge",n,e),"GE"),zo=l((n,e)=>new P("&&",n,e),"AND"),Rb=l((n,e,t)=>new he(n,$(0),$(e),z.SERIAL,t),"forS"),kn=l((n,e,t)=>new he(n,$(0),$(e),z.UNROLLED,t),"forU"),ya=l((n,e,t,r)=>new he(n,$(0),$(t),z.THREAD_BINDING,r,e),"forT");l(WL,"detectPureConv");l(_4,"buildImplicitGemmConv");Lv=4;l(KL,"vectorizableConvConfig");l(b4,"buildVectorizedImplicitGemmConv");l(HL,"validConvConfig");w4=[{BM:128,BN:64,BK:8,TM:8,TN:8},{BM:64,BN:64,BK:8,TM:8,TN:8},{BM:64,BN:64,BK:8,TM:4,TN:8},{BM:64,BN:32,BK:8,TM:8,TN:8},{BM:32,BN:64,BK:8,TM:4,TN:8}];l(T4,"pickVectorizedConvConfig");l(YL,"applyImplicitGemmConv")});function JL(n,e,t,r,s,o,i,a){let u=new me(n,[t,r],e.dtype,"shared"),p=new J(u,[s,o],new U(e,[i,a]));return{tile:u,fill:p}}function v4(n,e,t){let{A:r,B:s,C:o,M:i,N:a,K:u,transB:p}=n,c=u/t,f=Mc("ts_by"),d=Mc("ts_bx"),h=Mc("ts_ty"),m=Mc("ts_tx"),g=Mc("ts_ko"),y=Mc("ts_ki"),_=new me("ts_acc",[1],"f32","local"),b=Ic(Eb(f,ds(e)),h),w=Ic(Eb(d,ds(e)),m),x=Eb(g,ds(t)),S=JL("ts_As",r,e,t,h,m,b,Ic(x,m)),T=JL("ts_Bs",s,t,e,h,m,p?w:Ic(x,h),p?Ic(x,h):w),v=S4(y,t,new J(_,[ds(0)],Ic(new U(_,[ds(0)]),Eb(new U(S.tile,[h,y]),new U(T.tile,[y,m]))))),A=new te([S.fill,T.fill,new Fn,v,new Fn]),N=x4(g,c,A),k=new J(_,[ds(0)],new de(0)),O=new J(o,[b,w],new U(_,[ds(0)])),R=new qe(_,"local",new te([k,N,O]));return Lb(f,"blockIdx.y",i/e,Lb(d,"blockIdx.x",a/e,new qe(S.tile,"shared",new qe(T.tile,"shared",Lb(h,"threadIdx.y",e,Lb(m,"threadIdx.x",e,R))))))}function k4(n,e){let{M:t,N:r,K:s}=e,o=16,i=16;return o*o>(n.maxThreadsPerBlock||1024)||(o*i+i*o)*4>(n.sharedMemoryBytes||49152)||t%o!==0||r%o!==0||s%i!==0||t<o||r<o||s<i?null:{BS:o,BK:i}}function N4(n,e){let r=Fo(n.func.body).filter(o=>/^q?conv_(init|acc)_/.test(o));if(r.length===0)return!1;let s=new to(e);for(let o of r)s.applyToBlock(n,o);return!0}function A4(n,e,t={}){if(!e.isGPU())return!1;let r=DL(n.func);if(!r)return!1;let s=r.dims;if(t&&t.primitiveMatmul&&(s.batch||1)===1&&!r.epilogue){let a=k4(e,s);if(a){let u=v4(s,a.BS,a.BK);return n.func.body=u,n.func._setChild&&n.func._setChild("body",u),n.func.gpuRegisterBlocked=!0,!0}}let o=Tb(e,s);if(!o)return!1;let i=Av(s,o,r.epilogue);return n.func.body=i,n.func._setChild&&n.func._setChild("body",i),n.func.gpuRegisterBlocked=!0,!0}function Cb(n,e,t={}){if(!e.isGPU()||e.isWebGPU&&e.isWebGPU())return!1;let r=A4(n,e,t);return r||(r=YL(n,e,t)),r||(r=N4(n,e)),r}var ds,Mc,Ic,Eb,x4,S4,Lb,Dv=M(()=>{"use strict";xb();Bc();Cv();Do();Fe();ei();ds=l(n=>new L(n),"I"),Mc=l(n=>new Oe(n,"i32"),"IV"),Ic=l((n,e)=>new P("+",n,e),"ADD"),Eb=l((n,e)=>new P("*",n,e),"MUL"),x4=l((n,e,t)=>new he(n,ds(0),ds(e),z.SERIAL,t),"forS"),S4=l((n,e,t)=>new he(n,ds(0),ds(e),z.UNROLLED,t),"forU"),Lb=l((n,e,t,r)=>new he(n,ds(0),ds(t),z.THREAD_BINDING,r,e),"forT");l(JL,"stageTileToShared");l(v4,"buildTiledSharedMatmul");l(k4,"pickTiledConfig");l(N4,"applyDeterministicGpuConv");l(A4,"applyDeterministicGpuMatmul");l(Cb,"applyDeterministicGpuSchedule")});function Pv(n){return n*1664525+1013904223&2147483647}function B4(n,e,t){let r={...n};for(let s of e.variables)t.rngFloat()<t.mutationRate&&(r[s.name]=s.sample(o=>t.rng(o)));return r}function ZL(n={}){return n.strategy==="random"?new $v({numTrials:n.numTrials,seed:n.seed,deadline:n.deadline}):new Fv({populationSize:n.populationSize,numGenerations:n.numGenerations,mutationRate:n.mutationRate,eliteRatio:n.eliteRatio,seed:n.seed,deadline:n.deadline})}var Db,$v,Fv,O4,QL=M(()=>{"use strict";l(Pv,"nextLcg");Db=class{static{l(this,"SearchCandidate")}constructor(e,t,r){this.sketchName=e,this.params=t,this.score=r}},$v=class{static{l(this,"RandomSearch")}constructor(e={}){this.numTrials=e.numTrials||64,this.seed=e.seed||42,this.deadline=e.deadline||null,this._rngState=this.seed}_rng(e){return this._rngState=Pv(this._rngState),this._rngState%e}_expired(){return this.deadline?this.deadline.expired:!1}search(e,t){let r=[];for(let s of e){if(this._expired())break;for(let o=0;o<this.numTrials&&!this._expired();o++){let i=s.sampleParams(u=>this._rng(u)),a=t(s,i);a&&r.push(new Db(s.name,i,a.score))}}return r.sort((s,o)=>o.score-s.score),{candidates:r,population:null}}},Fv=class{static{l(this,"EvolutionarySearch")}constructor(e={}){this.populationSize=e.populationSize||32,this.numGenerations=e.numGenerations||10,this.mutationRate=e.mutationRate||.3,this.eliteRatio=e.eliteRatio||.2,this.seed=e.seed||42,this.deadline=e.deadline||null,this._rngState=this.seed}_rng(e){return this._rngState=Pv(this._rngState),this._rngState%e}_rngFloat(){return this._rngState=Pv(this._rngState),this._rngState/2147483647}_expired(){return this.deadline?this.deadline.expired:!1}search(e,t,r=null){let s=r&&r.length?r:this._initPopulation(e),o=new Map,i=l((u,p)=>{let c=u.name+"|"+JSON.stringify(p);if(o.has(c))return o.get(c);let f=t(u,p);return o.set(c,f),f},"evalMemo");for(let u=0;u<this.numGenerations&&!this._expired();u++){let p=[];for(let h of s){let m=i(h.sketch,h.params);m&&p.push({...h,score:m.score})}if(p.sort((h,m)=>m.score-h.score),p.length===0)break;let c=Math.max(1,Math.floor(p.length*this.eliteRatio)),f=p.slice(0,c),d=f.map(h=>({sketch:h.sketch,params:{...h.params}}));for(;d.length<this.populationSize;){let h=f[this._rng(f.length)],m=f[this._rng(f.length)];if(h.sketch!==m.sketch){let g=this._rngFloat()<.5?h:m;d.push(this._mutate(g.sketch,{...g.params}))}else d.push(this._mutate(h.sketch,this._crossover(h,m)))}s=d}let a=[];for(let u of s){let p=i(u.sketch,u.params);p&&a.push(new Db(u.sketch.name,u.params,p.score))}return a.sort((u,p)=>p.score-u.score),{candidates:a,population:s}}_initPopulation(e){let t=[];for(let r=0;r<this.populationSize;r++){let s=e[this._rng(e.length)],o=s.sampleParams(i=>this._rng(i));t.push({sketch:s,params:o})}return t}_crossover(e,t){if(e.sketch!==t.sketch)return{...e.params};let r={};for(let s of e.sketch.variables)r[s.name]=this._rngFloat()<.5?e.params[s.name]:t.params[s.name];return r}_mutate(e,t){let r={rngFloat:l(()=>this._rngFloat(),"rngFloat"),rng:l(i=>this._rng(i),"rng"),mutationRate:this.mutationRate},s=[B4,...O4],o=t;for(let i of s)o=i(o,e,r);return{sketch:e,params:o}}},O4=[];l(B4,"defaultResampleMutator");l(ZL,"createSearchStrategy")});function $b(n){let e={"threadIdx.x":1,"threadIdx.y":1,"threadIdx.z":1},t=new Set,r=l(s=>{if(!(!s||typeof s!="object"||t.has(s))){if(t.add(s),s.type==="ForNode"&&e[s.threadTag]!==void 0){let o=s.extent&&s.extent.type==="IntImmNode"?s.extent.value:1;o>e[s.threadTag]&&(e[s.threadTag]=o)}for(let o in s){let i=s[o];Array.isArray(i)?i.forEach(r):i&&typeof i=="object"&&r(i)}}},"visit");return r(n.body),R4.reduce((s,o)=>s*e[o],1)}var R4,Pb,XL=M(()=>{"use strict";du();Px();hv();Id();QL();yv();R4=["threadIdx.x","threadIdx.y","threadIdx.z"];l($b,"gpuThreadBlockSize");Pb=class{static{l(this,"BlockTuningSession")}constructor(e){this.target=e.target,this.primFunc=e.primFunc,this.blockName=e.blockName,this.sketches=e.sketches,this.benchmarkRunner=e.benchmarkRunner||null,this.config=e.config,this.deadline=e.deadline||null,this._warn=e.warn||(()=>{}),this._warnedEvalSketches=new Set;let t=this.sketches.some(o=>o.name==="fused");t?(this.learnedModel=new Ac,this.costModel=new Oc(e.costModel.analytical,this.learnedModel)):(this.costModel=e.costModel,this.learnedModel=e.learnedModel);let r=t?null:RL(e.primFunc,e.blockName,e.blockMap);this.evalFunc=r||e.primFunc,this.evalBlockName=e.blockName,this.sketchByName=new Map;for(let o of this.sketches)this.sketchByName.set(o.name,o);let s=this.sketches.length===1&&typeof this.sketches[0].enumerate=="function";this.enumSketch=s?this.sketches[0]:null,this.enumParams=s?this.sketches[0].enumerate():null,this.strategy=ZL({...this.config,deadline:this.deadline}),this.population=null,this._best=null,this.plateaued=!1}runRound(){let e=this._best?this._best.measuredScore:-1/0,t=this._produceCandidates();if(t.length===0)return this.plateaued=!0,0;if(this.benchmarkRunner)this._measureAndLearn(t);else{let s=t[0];this._consider({sketchName:s.sketchName,params:s.params,score:s.score,measuredScore:s.score}),this.plateaued=!0}let r=this._best?this._best.measuredScore:-1/0;return Math.max(0,r-e)}best(){return this._best?{sketchName:this._best.sketchName,params:this._best.params,score:this._best.score,medianMs:this._best.medianMs??null,minMs:this._best.minMs??null}:null}bestTrace(){if(!this._best)return null;let e=this.sketchByName.get(this._best.sketchName);if(!e)return null;try{let t=new Cn(Ti(this.primFunc));return e.instantiate(this._best.params)(t,this.blockName,this.target),t.trace.serialize()}catch(t){return this._warn("best-trace",this.blockName,t),null}}_produceCandidates(){if(this.enumSketch){let s=[];for(let o of this.enumParams){let i=this._evaluate(this.enumSketch,o);i&&s.push({sketchName:this.enumSketch.name,params:o,score:i.score})}return s.sort((o,i)=>i.score-o.score),s}let e=l((s,o)=>this._evaluate(s,o),"evaluator"),{candidates:t,population:r}=this.strategy.search(this.sketches,e,this.population);return this.population=r,t}_evaluate(e,t){try{let r=Ti(this.evalFunc),s=new Cn(r);if(e.instantiate(t)(s,this.evalBlockName,this.target),Yl.validate(r).length>0)return null;let i=this.target.maxThreadsPerBlock;return this.target.isGPU&&this.target.isGPU()&&i&&$b(r)>i?null:{score:this.costModel.score(r)}}catch(r){return this._warnedEvalSketches.has(e.name)||(this._warnedEvalSketches.add(e.name),this._warn("evaluate-candidate",this.blockName,r)),null}}_measureAndLearn(e){let t=e.slice(0,this.config.topKForBenchmark);for(let r of t){if(this.deadline&&this.deadline.expired)break;let s=this._measure(r);if(!s)continue;let o=-s.result.medianMs;this.learnedModel.addSample(s.features,o),this._consider({sketchName:r.sketchName,params:r.params,score:o,measuredScore:o,medianMs:s.result.medianMs,minMs:s.result.minMs})}this.learnedModel.train()}_measure(e){let t=this.sketchByName.get(e.sketchName);if(!t)return null;let r,s;try{r=Ti(this.primFunc),t.instantiate(e.params)(new Cn(r),this.blockName,this.target),s=Ti(this.evalFunc),t.instantiate(e.params)(new Cn(s),this.evalBlockName,this.target)}catch(i){return this._warn("measure-candidate",this.blockName,i),null}let o=this.benchmarkRunner.run(r);return o?{result:o,features:Mu.extractStatements(s)}:null}_consider(e){(!this._best||e.measuredScore>this._best.measuredScore)&&(this._best=e)}}});var zv,Fb,e2=M(()=>{"use strict";zv=class{static{l(this,"GradientSchedulerPolicy")}pick(e){let t=e.filter(i=>!i.plateaued);if(t.length===0)return null;let r=t.filter(i=>i.rounds===0);if(r.length>0)return r[0];let s=null,o=-1/0;for(let i of t){let a=i.gainEwma!==void 0?i.gainEwma:i.lastGain,u=i.weight*a;u>o&&(o=u,s=i)}return s}},Fb=class{static{l(this,"TaskScheduler")}constructor(e=null){this.policy=e||new zv}run(e,t,r={}){let s=r.maxRoundsPerTask??8,o=r.plateauPatience??2,i=r.gainEwmaAlpha??.5;for(let a of e)a.rounds=0,a.lastGain=0,a.gainEwma=0,a.stale=0,a.plateaued=!1;for(;!(t&&t.expired);){let a=this.policy.pick(e);if(!a)break;let u=a.session.runRound();a.rounds++,a.lastGain=u,a.gainEwma=i*u+(1-i)*a.gainEwma,u<=0?a.stale++:a.stale=0,(a.session.plateaued||a.stale>=o||a.rounds>=s)&&(a.plateaued=!0)}}}});function M4(n){if(n.isCPU())return null;let e=B1(n.kind);if(!e)throw new Error("hardwareMeasure requested for target '"+n.kind+"' but no measurer is registered for it; the corresponding runtime must be loaded (Node: import '#io/cuda_runtime') before compiling");return e}var zb,Vb,t2=M(()=>{"use strict";du();yv();zL();GL();qL();UL();Ev();Bc();Bv();Do();Dv();XL();Id();Fe();e2();Hm();l(M4,"resolveMeasurer");zb=class{static{l(this,"AutotuneConfig")}constructor(e={}){this.strategy=e.strategy||"evolutionary",this.numTrials=e.numTrials||64,this.populationSize=e.populationSize||32,this.numGenerations=e.numGenerations||10,this.mutationRate=e.mutationRate,this.eliteRatio=e.eliteRatio,this.seed=e.seed||42,this.timeBudgetMs=e.timeBudgetMs||3e4,this.clock=e.clock||null,this.tuningDB=e.tuningDB||null,this.useTuningDB=e.useTuningDB!==!1,this.measurer=e.measurer||null,this.hardwareMeasure=e.hardwareMeasure??!1,this.enableBenchmark=e.enableBenchmark??(this.hardwareMeasure||!!e.measurer),this.benchmarkWarmup=e.benchmarkWarmup??3,this.benchmarkRepeat=e.benchmarkRepeat??10,this.benchmarkMaxCv=e.benchmarkMaxCv??0,this.topKForBenchmark=e.topKForBenchmark??5,this.maxRoundsPerTask=e.maxRoundsPerTask??8,this.plateauPatience=e.plateauPatience??2,this.schedulerPolicy=e.schedulerPolicy||null,this.onWarning=e.onWarning||null}},Vb=class{static{l(this,"Autotuner")}constructor(e,t={},r=null){this.target=e,this.config=t instanceof zb?t:new zb(t),this.trace=r,this._funcName=null,this.config.hardwareMeasure&&(this.config.measurer=M4(e)),this.analyticalModel=new yb(e),this.learnedModel=new Ac,this.costModel=new Oc(this.analyticalModel,this.learnedModel),this.db=this.config.tuningDB instanceof Fd?this.config.tuningDB:new Fd;let s=l((o,i,a)=>this._warn(o,i,a),"warn");this.benchmarkRunner=this.config.enableBenchmark?new Ob(e,{warmup:this.config.benchmarkWarmup,repeat:this.config.benchmarkRepeat,maxCv:this.config.benchmarkMaxCv,measurer:this.config.measurer,warn:s}):null,this.scheduler=new Fb(this.config.schedulerPolicy)}_warn(e,t,r){let s=r&&r.message?r.message:String(r);if(this.config.onWarning)try{this.config.onWarning({stage:e,func:this._funcName,block:t||null,message:s,error:r})}catch(o){}this.trace&&this.trace.warn("autotune",this._funcName,`${e}${t?" ["+t+"]":""}: ${s}`)}tune(e,t=null){this._funcName=e.name;let r=t?[t]:Fo(e.body),s=kb(e.body),o=Pd(e),i=new Bb(this.config.timeBudgetMs,this.config.clock),a=new Map,u=new Map;for(let f of r){let d=Nb(e,f,this.target,s);u.set(f,d);let h=a.get(d);if(h){h.weight++;continue}if(this.config.useTuningDB&&this.db.has(d)){a.set(d,{key:d,kind:"cache",cached:this.db.lookup(d),weight:1});continue}let m=Iv(e,f,this.target,s,{richGpu:this.config.richGpu??!!this.config.measurer,dag:o});if(m.length===0){a.set(d,{key:d,kind:"empty",weight:1});continue}let g=new Pb({target:this.target,primFunc:e,blockName:f,blockMap:s,sketches:m,costModel:this.costModel,learnedModel:this.learnedModel,benchmarkRunner:this.benchmarkRunner,config:this.config,deadline:i,warn:l((y,_,b)=>this._warn(y,_,b),"warn")});a.set(d,{key:d,kind:"session",session:g,weight:1})}let p=[...a.values()].filter(f=>f.kind==="session");p.length>0&&this.scheduler.run(p,i,this.config);let c=new Map;for(let f of r){let d=a.get(u.get(f));if(d.kind==="cache"){c.set(f,{sketchName:d.cached.sketchName,params:d.cached.params,score:d.cached.score,fromCache:!0});continue}if(d.kind==="empty")continue;let h=d.session.best();if(h){if(this.config.useTuningDB&&!d.stored){let m=new $d(d.key,h.sketchName,h.params,h.score,d.session.bestTrace(),this.db.version);m.medianMs=h.medianMs||null,m.minMs=h.minMs||null,this.db.store(d.key,m),d.stored=!0}c.set(f,{sketchName:h.sketchName,params:h.params,score:h.score,fromCache:!1,medianMs:h.medianMs,minMs:h.minMs})}}return c}tuneAndApply(e,t=null){let r=this.tune(e,t);if(r.size>0){let s=this._applyBestSchedule(e,r);if(s)return{func:s.func,results:r,applied:!0}}return{func:e,results:r,applied:!1}}_applyBestSchedule(e,t){let r=this._buildDefaultSchedule(e),s=!!r&&this._scheduleIsValid(r),o=s&&this._isStrongBackendSchedule(r),i=this._buildTunedSchedule(e,t),a=!!i&&this._scheduleIsValid(i),u=a&&this._isStrongBackendSchedule(i),p=!o||u&&this.config.measurer!=null;return a&&p?(this._adoptSchedule(e,i),{func:e}):(i&&!a?this._warn("tuned-schedule-invalid",null,new Error("tuned schedule exceeds target thread-block limit; falling back to default")):a&&o&&this._warn("baseline-preferred",null,new Error("cost-model-only tuning cannot displace the deterministic GPU schedule without hardware measurement; keeping the deterministic kernel")),s?(this._adoptSchedule(e,r),{func:e}):a?(this._adoptSchedule(e,i),{func:e}):(this._warn("no-valid-schedule",null,new Error("neither tuned nor default schedule is valid; leaving function unscheduled")),null))}_isStrongBackendSchedule(e){return!!e&&e.gpuRegisterBlocked===!0}_buildTunedSchedule(e,t){try{let r=Ti(e),s=new Cn(r),o=kb(r.body),i=Pd(r),a=new Set,u=[];for(let c of t)if(c[1].sketchName==="fused"){let f=Sb(r,i,c[0],Tt);f&&a.add(f),u.unshift(c)}else u.push(c);let p=new Set;for(let[c,f]of u)if(!(a.has(c)||p.has(f))&&(p.add(f),!(!f.sketchName||!f.params)))try{let h=Iv(r,c,this.target,o,{richGpu:this.config.richGpu??!!this.config.measurer,dag:i}).find(m=>m.name===f.sketchName);h&&this._fitsThreadBlock(r,c,h,f.params)&&h.instantiate(f.params)(s,c,this.target)}catch(d){this._warn("apply-tuned-block",c,d);continue}return this._scheduleResidualBlocks(s,a),r}catch(r){return this._warn("build-tuned-schedule",null,r),null}}_scheduleResidualBlocks(e,t){let r=null;for(let s of Fo(e.func.body))if(!(t.has(s)||this._blockIsParallelized(e,s))){r||(r=new to(this.target));try{r.applyToBlock(e,s)}catch(o){this._warn("residual-block",s,o)}}}_blockIsParallelized(e,t){let r;try{r=e.getLoops(t)}catch(s){return this._warn("block-loops",t,s),!0}for(let s of r)if(s.kind===z.THREAD_BINDING||s.kind===z.PARALLEL||s.kind===z.VECTORIZED)return!0;return!1}_scheduleIsValid(e){return!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock?!0:$b(e)<=this.target.maxThreadsPerBlock}_buildDefaultSchedule(e){try{let t=Ti(e),r=new Cn(t);return Cb(r,this.target,this.config)||new to(this.target).applyToAllBlocks(r),t}catch(t){return this._warn("build-default-schedule",null,t),null}}_adoptSchedule(e,t){Object.assign(e,t),e._setChild("body",e.body)}_fitsThreadBlock(e,t,r,s){if(!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock)return!0;try{let o=Ti(e);return r.instantiate(s)(new Cn(o),t,this.target),$b(o)<=this.target.maxThreadsPerBlock}catch(o){return this._warn("fits-thread-block",t,o),!1}}}});var Gb,n2=M(()=>{"use strict";ma();du();Do();t2();Dv();Gb=class extends ar{static{l(this,"SchedulePass")}constructor(e){super("SchedulePass","scheduling"),this.config=e,this.target=e.target,this.snapshotPoint="afterScheduling",this._autotuner=null,this._policy=null}begin(e){let t=this.config.scheduling;t.autotune?this._autotuner=new Vb(this.target,t,e.trace):t.enabled&&(this._policy=new to(this.target,null,e.trace))}run(e,t){let r=this.config.scheduling,s=t.trace;if(r.autotune){if(e.cublasInfo||e._tensorIntrin)return;let o=performance.now(),i=this._autotuner.tuneAndApply(e),a=performance.now()-o,u=0,p=0;if(i&&i.results){p=i.results.size;for(let[c,f]of i.results)f.fromCache&&u++,s.explainsEnabled&&s.explain("schedule",c,f.sketchName,`autotuned: best of search${f.fromCache?" (cached)":""}, score ${f.score!=null?f.score.toFixed(3):"n/a"}`,{target:this.target.name,params:f.params})}s.autotuneStats(e.name,{durationMs:a,blockCount:p,applied:!!(i&&i.applied),cacheHits:u})}else if(r.enabled||r.gpuTiling){if(e.cublasInfo||e._tensorIntrin)return;let o=performance.now(),i=new Cn(e);!Cb(i,this.target,r)&&r.enabled&&this._policy.applyToAllBlocks(i),s.functionEvent("scheduling",e.name,{durationMs:performance.now()-o})}}}});function r2(n){let e={analyzer:new Xs,simp:null};e.simp=new Sy(e.analyzer);let t=hs(n.body,e);return n.body=t,n._setChild("body",t),n}function I4(n,e,t){let r=n.analyzer.getVarBound(e);return t&&t.type==="IntImmNode"&&t.value>0?n.analyzer.bind(e,0,t.value-1):n.analyzer.setVarBound(e,null),r}function hs(n,e){if(!n||typeof n!="object")return n;switch(n.type){case"ForNode":{let t=I4(e,n.loopVar.name,n.extent),r=hs(n.body,e);e.analyzer.setVarBound(n.loopVar.name,t);let s=new he(n.loopVar,n.min,n.extent,n.kind,r,n.threadTag);return n.annotations&&(s.annotations=n.annotations),s}case"BlockNode":{let t=[];for(let i of n.iterVars)i.iterVar&&(t.push([i.iterVar.name,e.analyzer.getVarBound(i.iterVar.name)]),e.analyzer.setVarBound(i.iterVar.name,i.binding?cu(e.analyzer,i.binding):null));let r=n.iterVars.map(E4(e)),s=hs(n.body,e),o=n.initBody?hs(n.initBody,e):null;for(let[i,a]of t)e.analyzer.setVarBound(i,a);return new fe(n.name,r,n.reads,n.writes,s,o)}case"SeqNode":return new te(n.stmts.map(t=>hs(t,e)));case"IfThenElseNode":{let t=ur(n.condition,e);return _i(e.analyzer,t)?hs(n.thenBody,e):lu(e.analyzer,t)?n.elseBody?hs(n.elseBody,e):new te([]):new Se(t,hs(n.thenBody,e),n.elseBody?hs(n.elseBody,e):null)}case"BufferStoreNode":return new J(n.buffer,n.indices.map(t=>ur(t,e)),ur(n.value,e));case"LetStmtNode":return new et(n.variable,ur(n.value,e),hs(n.body,e));case"AllocateNode":return new qe(n.buffer,n.scope,hs(n.body,e));case"WhileNode":return new $i(n.condVar,hs(n.condBody,e),hs(n.loopBody,e));case"EvaluateNode":return new il(ur(n.value,e));default:return n}}function E4(n){return e=>{if(!e.iterVar||!e.binding)return e;let t=ur(e.binding,n);return new Pa(e.iterVar,t,e.kind)}}function ur(n,e){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"IntImmNode":case"FloatImmNode":case"VariableNode":return n;case"BufferLoadNode":return new U(n.buffer,n.indices.map(t=>ur(t,e)));case"MathOpNode":{let t=ur(n.a,e),r=n.b?ur(n.b,e):null;return e.simp.simplify(new P(n.op,t,r))}case"CompareNode":{let t=ur(n.a,e),r=ur(n.b,e);return e.simp.simplify(new Ke(n.direction,t,r))}case"CastNode":return new ut(ur(n.expr,e),n.fromDtype,n.toDtype);case"CallExternNode":return new Ne(n.externName,n.args.map(t=>ur(t,e)),n.dtype);case"IfThenElseNode":{let t=ur(n.condition,e),r=ur(n.thenBody,e),s=n.elseBody?ur(n.elseBody,e):null;return _i(e.analyzer,t)?r:s!==null&&lu(e.analyzer,t)?s:new Se(t,r,s)}default:return n}}var s2=M(()=>{"use strict";Hl();wf();Fe();l(r2,"simplifyPrimFunc");l(I4,"bindLoopVar");l(hs,"simplifyStmt");l(E4,"simplifyIterVar");l(ur,"simplifyExpr")});var jb,o2=M(()=>{"use strict";ma();s2();jb=class extends ar{static{l(this,"SimplifyPass")}constructor(){super("SimplifyPass","simplify")}run(e,t){let r=performance.now();return r2(e),t.trace.functionEvent("simplify",e.name,{durationMs:performance.now()-r}),e}}});function i2(n){return!!n&&typeof n=="object"&&typeof n.name=="string"&&n.dtype!==void 0&&n.shape!==void 0&&n.type===void 0}var Vv,Gv,L4,qb,a2=M(()=>{"use strict";Vv=class{static{l(this,"BufferInterval")}constructor(e,t,r,s){this.buffer=e,this.firstUse=t,this.lastUse=r,this.scope=s}get size(){return this.buffer.sizeInBytes()}overlaps(e){return this.firstUse<=e.lastUse&&e.firstUse<=this.lastUse}},Gv=class{static{l(this,"BufferLivenessResult")}constructor(e,t,r){this.intervals=e,this.stmtOrder=t,this.paramBuffers=r}isParam(e){return this.paramBuffers.has(e)}getTemporaries(){let e=[];for(let[t,r]of this.intervals)this.paramBuffers.has(t)||e.push(r);return e}interfere(e,t){let r=this.intervals.get(e),s=this.intervals.get(t);return!r||!s?!1:r.overlaps(s)}},L4=new Set(["_parent","_parentKey","_parentIdx"]);l(i2,"isBuffer");qb=class{static{l(this,"BufferLiveness")}static analyze(e){let t=new Map,r=[],s=0,o=new Set;for(let[,f]of e.bufferMap)o.add(f);let i=[],a=l(f=>{if(!f)return;let d=t.get(f);d?(s<d.firstUse&&(d.firstUse=s),s>d.lastUse&&(d.lastUse=s)):(d=new Vv(f,s,s,f.scope),t.set(f,d)),i.push(f)},"touch"),u=l((f,d)=>{if(!(!f||typeof f!="object"||d.has(f))){if(d.add(f),i2(f)){a(f);return}i2(f.buffer)&&a(f.buffer);for(let h of Object.keys(f)){if(L4.has(h)||h==="buffer")continue;let m=f[h];if(!(!m||typeof m!="object"))if(Array.isArray(m))for(let g of m)u(g,d);else u(m,d)}}},"touchAll"),p=l((f,d)=>{for(let h=f;h<i.length;h++){let m=t.get(i[h]);m&&d>m.lastUse&&(m.lastUse=d)}},"extendRegion"),c=l(f=>{if(f)switch(f.type){case"SeqNode":for(let d of f.stmts)c(d);break;case"ForNode":{let d=s,h=i.length;u(f.min,new Set),u(f.extent,new Set),c(f.body);let m=s>d?s-1:d;p(h,m);break}case"WhileNode":{let d=s,h=i.length;u(f.condVar,new Set),c(f.condBody),c(f.loopBody);let m=s>d?s-1:d;p(h,m);break}case"BlockNode":r.push({idx:s,node:f});for(let d of f.reads)a(d.buffer);for(let d of f.writes)a(d.buffer);u(f.body,new Set),f.initBody&&u(f.initBody,new Set),s++;break;case"AllocateNode":a(f.buffer),c(f.body);break;case"IfThenElseNode":{let d=s,h=i.length;u(f.condition,new Set),c(f.thenBody),f.elseBody&&c(f.elseBody);let m=s>d?s-1:d;p(h,m);break}case"LetStmtNode":u(f.value,new Set),c(f.body);break;case"EvaluateNode":u(f.value,new Set);break;default:u(f,new Set);break}},"walk");return c(e.body),new Gv(t,r,o)}}});function C4(n,e){if(n.shape.length!==e.shape.length)return!1;for(let t=0;t<n.shape.length;t++)if(n.shape[t]!==e.shape[t])return!1;return!0}function Ec(n,e){if(n===e)return!0;if(!n||!e||typeof n!="object"||typeof e!="object"||n.type!==e.type)return!1;switch(n.type){case"VariableNode":return n.name===e.name;case"IntImmNode":return n.value===e.value;case"FloatImmNode":return n.value===e.value;case"MathOpNode":return n.op===e.op&&Ec(n.a,e.a)&&Ec(n.b,e.b);case"CompareNode":return n.direction===e.direction&&Ec(n.a,e.a)&&Ec(n.b,e.b);case"CastNode":return n.toDtype===e.toDtype&&Ec(n.expr,e.expr);case"BufferLoadNode":return n.buffer===e.buffer&&qv(n.indices,e.indices);default:return!1}}function qv(n,e){if(n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(!Ec(n[t],e[t]))return!1;return!0}function u2(n,e){n&&Us(n,e)}function D4(n,e,t){let r=[],s=[];for(let p of[n.body,n.initBody])p&&u2(p,c=>{c.type==="BufferStoreNode"&&c.buffer===t?r.push(c):c.type==="BufferLoadNode"&&c.buffer===e&&s.push(c)});if(r.length!==1||s.length===0)return!1;let o=r[0],i=o.indices;for(let p of s)if(!qv(p.indices,i))return!1;let a=new Set;for(let p of[o.value,...o.indices])p&&u2(p,c=>{c.type==="BufferLoadNode"&&c.buffer===e&&a.add(c)});let u=!0;for(let p of s)if(!a.has(p)){u=!1;break}if(!u){let p=o.value;if(!(p&&p.type==="BufferLoadNode"&&p.buffer===e&&qv(p.indices,i)))return!1}return!0}function P4(n,e){if(n)for(let t of yl(n,r=>r.type==="BlockNode",{kinds:"stmt"}))e.push(t)}var jv,Ub,l2=M(()=>{"use strict";ts();jv=class{static{l(this,"InplaceCandidate")}constructor(e,t,r){this.srcBuffer=e,this.dstBuffer=t,this.reason=r}},Ub=class{static{l(this,"InplaceAnalysis")}static analyze(e,t,r=new Set){let s=[],o=[];P4(e.body,o);let i=new Map;for(let p of t.stmtOrder)i.set(p.node,p.idx);let a=new Map;for(let p of t.stmtOrder)for(let c of p.node.reads){let f=a.get(c.buffer);(f===void 0||p.idx>f)&&a.set(c.buffer,p.idx)}let u=new Set;for(let p of o){let c=i.get(p);if(c!==void 0)for(let f of p.writes){let d=f.buffer;if(!(t.isParam(d)&&!r.has(d)))for(let h of p.reads){let m=h.buffer;if(m===d||u.has(m)||t.isParam(m)&&!r.has(m)||!C4(m,d)||m.dtype!==d.dtype||m.scope!==d.scope)continue;let g=t.intervals.get(m),y=t.intervals.get(d);if(!(!g||!y)&&g.lastUse<=y.firstUse){let _=a.get(m);if(_===void 0||_<=c){if(!D4(p,m,d))continue;s.push(new jv(m,d,`${m.name} last used at ${g.lastUse}, ${d.name} first used at ${y.firstUse}`)),u.add(m);break}}}}}return s}};l(C4,"shapesMatch");l(Ec,"exprEqual");l(qv,"indexListEqual");l(u2,"walkNodes");l(D4,"isInplaceComputeSafe");l(P4,"collectBlocks")});function c2(n,e){let t=0,r=n.length;for(;t<r;){let s=t+r>>1;n[s].offset<=e.offset?t=s+1:r=s}n.splice(t,0,e)}function p2(n){return{best:null,consider(e,t,r){return t<r?null:n==="best-fit"?((this.best===null||t<this.best.gap)&&(this.best={offset:e,gap:t}),null):e},result(e){return this.best!==null?this.best.offset:e}}}var Uv,Wv,Wb,f2=M(()=>{"use strict";l(c2,"insertByOffset");l(p2,"gapSelector");Uv=class{static{l(this,"MemoryBlock")}constructor(e,t,r){this.offset=e,this.size=t,this.buffer=r}get end(){return this.offset+this.size}overlaps(e){return this.offset<e.end&&e.offset<this.end}},Wv=class{static{l(this,"MemoryPool")}constructor(e,t=64,r="best-fit"){this.scope=e,this.alignment=t,this.strategy=r,this.blocks=[],this.peakUsage=0}allocate(e,t){let r=this._align(e),s=this._findFreeOffset(r);return this.placeAt(s,r,t)}placeAt(e,t,r){let s=this._align(t),o=new Uv(e,s,r);c2(this.blocks,o);let i=e+s;return i>this.peakUsage&&(this.peakUsage=i),o}_align(e){return Math.ceil(e/this.alignment)*this.alignment}_findFreeOffset(e){let t=this.blocks,r=0,s=p2(this.strategy);for(let o of t){let i=this._align(r),a=o.offset-i,u=s.consider(i,a,e);if(u!==null)return u;o.end>r&&(r=o.end)}return s.result(this._align(r))}fragmentation(){if(this.peakUsage===0)return 0;let e=this.blocks.reduce((t,r)=>t+r.size,0);return Math.max(0,1-e/this.peakUsage)}release(e){let t=this.blocks.indexOf(e);t>=0&&this.blocks.splice(t,1)}},Wb=class{static{l(this,"BufferAssignment")}constructor(){this.assignments=new Map,this.pools=new Map,this.inplaceMap=new Map,this.effLastUse=new Map}assign(e,t=[],r=64,s="best-fit"){for(let c of t)this.inplaceMap.set(c.dstBuffer,c.srcBuffer);let o=new Map;for(let c of e)o.set(c.buffer,c);let i=new Map;for(let c of e)i.set(c.buffer,c.lastUse);let a=new Map;for(let[c,f]of this.inplaceMap)!o.has(c)||!o.has(f)||(a.has(f)||a.set(f,[]),a.get(f).push(c));if(a.size>0){let c=new Map;for(let f of e){let d=f.buffer;if(c.get(d)===1)continue;let h=[d];for(;h.length;){let m=h[h.length-1],g=a.get(m);if(c.get(m)===void 0){if(c.set(m,0),g)for(let y of g)c.get(y)===void 0&&h.push(y);continue}if(h.pop(),c.get(m)!==1){if(g){let y=i.get(m);for(let _ of g){let b=i.get(_);b>y&&(y=b)}i.set(m,y)}c.set(m,1)}}}}this.effLastUse=i;let u=[...e].sort((c,f)=>{let d=c.size,h=f.size,m=d>0,g=h>0;if(m&&g){let y=h-d;if(y!==0)return y}else if(m!==g)return m?-1:1;return c.firstUse-f.firstUse}),p=new Map;for(let c of u){let f=c.buffer;if(this.inplaceMap.has(f)){let w=this.inplaceMap.get(f),x=this.assignments.get(w);if(x){this.assignments.set(f,{offset:x.offset,size:c.size,scope:c.scope,pool:x.pool,inplaceOf:w});continue}}let d=c.size;if(d===0)continue;if(d<0){this.assignments.set(f,{offset:0,size:0,scope:c.scope,pool:c.scope,inplaceOf:null,isDynamic:!0});continue}let h=c.scope,m=this.pools.get(h);m||(m=new Wv(h,r),this.pools.set(h,m)),p.has(h)||p.set(h,[]);let g=p.get(h),y=i.get(f)??c.lastUse,_=this._interferenceOffset(g,c.firstUse,y,m._align(d),r,s),b=m.placeAt(_,d,f);c2(g,{firstUse:c.firstUse,lastUseEff:y,offset:b.offset,size:b.size}),this.assignments.set(f,{offset:b.offset,size:b.size,scope:h,pool:h,inplaceOf:null})}return this}_interferenceOffset(e,t,r,s,o,i){let a=[];for(let c of e)c.firstUse<=r&&t<=c.lastUseEff&&a.push([c.offset,c.offset+c.size]);let u=0,p=p2(i);for(let[c,f]of a){let d=Math.ceil(u/o)*o,h=c-d,m=p.consider(d,h,s);if(m!==null)return m;f>u&&(u=f)}return p.result(Math.ceil(u/o)*o)}getOffset(e){let t=this.assignments.get(e);return t?t.offset:-1}getAssignment(e){return this.assignments.get(e)||null}peakMemory(e=null){if(e){let r=this.pools.get(e);return r?r.peakUsage:0}let t=0;for(let[,r]of this.pools)t+=r.peakUsage;return t}}});function Hb(n,e,t){if(!n||typeof n!="object"||t.has(n))return!1;if(t.add(n),n.type==="BufferLoadNode"&&(!e||n.buffer===e))return!0;for(let r of Object.keys(n)){if(r==="_parent"||r==="_parentKey"||r==="_parentIdx")continue;let s=n[r];if(!(!s||typeof s!="object")){if(Array.isArray(s)){for(let o of s)if(Hb(o,e,t))return!0}else if(Hb(s,e,t))return!0}}return!1}function d2(n){let e=new Set,t=new Map,r=new Set,s=[n.body];for(;s.length>0;){let o=s.pop();if(!(!o||typeof o!="object"||r.has(o))){if(r.add(o),o.type==="BufferStoreNode"&&o.buffer){let i=o.buffer;for(let u of o.indices)if(Hb(u,null,new Set)){e.add(i);break}Hb(o.value,i,new Set)&&e.add(i);let a=t.get(i);a||(a=[],t.set(i,a)),a.push(o.value)}for(let i of Object.keys(o)){if(i==="_parent"||i==="_parentKey"||i==="_parentIdx")continue;let a=o[i];if(Array.isArray(a))for(let u of a)u&&typeof u=="object"&&s.push(u);else a&&typeof a=="object"&&s.push(a)}}}for(let[o,i]of t){let a=l(u=>u&&(u.type==="IntImmNode"||u.type==="FloatImmNode")&&u.value===0,"isConstZero");i.every(a)&&e.add(o),i.length===1&&i[0]&&(i[0].type==="IntImmNode"||i[0].type==="FloatImmNode")&&e.add(o)}return e}function $4(n,e){let t=new Set,r=[n];for(;r.length>0;){let s=r.pop();if(!(!s||typeof s!="object"||t.has(s))){t.add(s);for(let o of Object.keys(s)){if(o==="_parent"||o==="_parentKey"||o==="_parentIdx")continue;let i=s[o];if(i&&typeof i=="object"&&e.has(i)){s[o]=e.get(i);continue}if(Array.isArray(i)){for(let a=0;a<i.length;a++){let u=i[a];u&&typeof u=="object"&&e.has(u)?i[a]=e.get(u):u&&typeof u=="object"&&r.push(u)}continue}i&&typeof i=="object"&&r.push(i)}}}}var Kv,Kb,h2=M(()=>{"use strict";a2();l2();f2();Fe();K0();Kv=class{static{l(this,"MemoryPlan")}constructor(e,t,r){this.assignment=e,this.liveness=t,this.inplaceCandidates=r,this.aliasMap=new Map}peakMemory(e=null){return this.assignment.peakMemory(e)}getReport(){let e=new Map;for(let[s,o]of this.assignment.pools)e.set(s,{peakUsage:o.peakUsage,numBuffers:0,numReused:0});for(let[s,o]of this.assignment.assignments){let i=e.get(o.scope);i&&(i.numBuffers++,o.inplaceOf&&i.numReused++)}let t=this.liveness.getTemporaries().length,r=this.inplaceCandidates.length;return{peakMemory:this.assignment.peakMemory(),scopeBreakdown:e,totalTemporaries:t,totalInplace:r,materializedReuse:this.aliasMap.size,assignments:this.assignment.assignments}}},Kb=class{static{l(this,"MemoryPlanner")}constructor(e={}){this.alignment=e.alignment||64,this.enableInplace=e.enableInplace!==!1,this.allocStrategy=e.allocStrategy||"best-fit",this.poolAllocation=e.poolAllocation||!1}plan(e){let t=qb.analyze(e),r=t.getTemporaries(),s=[];this.enableInplace&&(s=Ub.analyze(e,t));let o=new Wb;return o.assign(r,s,this.alignment,this.allocStrategy),new Kv(o,t,s)}planAndRewrite(e){let t=this.plan(e);return{func:this._insertAllocations(e,t),plan:t}}_insertAllocations(e,t){let r=t.liveness.getTemporaries();if(r.length===0)return e;let s=new Map;this.poolAllocation?this._assignPoolOffsets(e,t,r):(s=this._buildReuseAliases(r,t,e),s.size>0&&$4(e.body,s)),t.aliasMap=s;let o=[...r].sort((u,p)=>p.firstUse-u.firstUse),i=e.body,a=new Set;for(let u of o){let p=u.buffer;if(s.has(p))continue;let c=t.assignment.getAssignment(p);c&&(c.inplaceOf||a.has(p)||(a.add(p),i=new qe(p,c.isDynamic?"dynamic":c.scope,i)))}return e.body=i,e._setChild("body",i),e}_assignPoolOffsets(e,t,r){let s=d2(e);for(let o of r){let i=o.buffer;if(s.has(i)||i.scope!=="global")continue;let a=t.assignment.getAssignment(i);!a||a.inplaceOf||a.isDynamic||a.size>0&&(i.poolByteOffset=a.offset)}}_buildReuseAliases(e,t,r){let s=d2(r),o=new Set(t.assignment.inplaceMap.values()),i=t.assignment.effLastUse,a=l(c=>i.get(c.buffer)??c.lastUse,"lastUseOf"),u=new Map;for(let c of e){let f=c.buffer,d=t.assignment.getAssignment(f);if(!d||d.inplaceOf||d.isDynamic||o.has(f)||f.numel()<=0||s.has(f))continue;let h=`${f.scope}|${f.dtype}|${f.shape.join(",")}|${f.strides.join(",")}`,m=u.get(h);m||(m=[],u.set(h,m)),m.push(c)}let p=new Map;for(let c of u.values()){if(c.length<2)continue;c.sort((d,h)=>d.firstUse-h.firstUse||a(d)-a(h));let f=new Tl((d,h)=>d.lastUse-h.lastUse);for(let d of c){let h=f.peek();h&&h.lastUse<d.firstUse?(f.pop(),h.lastUse=a(d),f.push(h),p.set(d.buffer,h.rep)):f.push({rep:d.buffer,lastUse:a(d)})}}return p}};l(Hb,"exprLoadsBuffer");l(d2,"collectFreshZeroDependent");l($4,"rewriteBufferAliases")});var Yb,m2=M(()=>{"use strict";ma();h2();Yb=class extends ar{static{l(this,"MemoryPlanPass")}constructor(e){super("MemoryPlanPass","memoryPlanning"),this.config=e,this._planner=null}begin(e){let t=this.config.memory.alignment||this.config.target?.cacheLineSizeBytes||64;this._planner=new Kb({alignment:t,enableInplace:this.config.memory.inplaceReuse,allocStrategy:this.config.memory.allocStrategy,poolAllocation:this.config.memory.poolAllocation})}run(e,t){if(e.gpuRegisterBlocked)return;let r=performance.now(),{plan:s}=this._planner.planAndRewrite(e),o=s.getReport();t.trace.memoryStats(e.name,{durationMs:performance.now()-r,peakMemory:o.peakMemory,totalTemporaries:o.totalTemporaries,totalInplace:o.totalInplace})}}});function Lc(n){return n&&n.type==="IntImmNode"?n.value:null}function F4(n){if(!n||n.type!=="ForNode"||!g2.has(n.kind)||Lc(n.min)!==0)return null;let e=Lc(n.extent);if(e===null)return null;let t=n.body;if(!t||t.type!=="ForNode"||!g2.has(t.kind)||Lc(t.min)!==0)return null;let r=Lc(t.extent);if(r===null||r<=0)return null;let s=t.body;if(!s||s.type!=="IfThenElseNode"||s.elseBody)return null;let o=z4(s.condition,n.loopVar.name,t.loopVar.name,r);if(o===null||o<=0)return null;let i=Math.floor(o/r),a=o-i*r;if(a===0||i<1||e!==i+1)return null;let u=new Xs;if(u.bind(n.loopVar.name,0,i-1),u.bind(t.loopVar.name,0,r-1),!_i(u,s.condition))return null;let p=s.thenBody,c=new he(t.loopVar,new L(0),new L(r),t.kind,p,t.threadTag),f=new he(n.loopVar,new L(0),new L(i),n.kind,c,n.threadTag),d=Gi(Iu(p),g=>g.type==="VariableNode"&&g.name===n.loopVar.name?new L(i):g),h=new Oe(t.loopVar.name,t.loopVar.dtype),m=new he(h,new L(0),new L(a),t.kind,d,t.threadTag);return new te([f,m])}function z4(n,e,t,r){if(!n)return null;let s,o;if(n.type==="MathOpNode"&&n.op==="<")s=n.a,o=n.b;else if(n.type==="CompareNode"&&n.direction==="lt")s=n.a,o=n.b;else return null;let i=Lc(o);if(i===null||!s||s.type!=="MathOpNode"||s.op!=="+")return null;let a=s.a,u=s.b;return!u||u.type!=="VariableNode"||u.name!==t||!a||a.type!=="MathOpNode"||a.op!=="*"||!a.a||a.a.type!=="VariableNode"||a.a.name!==e||Lc(a.b)!==r?null:i}var g2,Jb,y2=M(()=>{"use strict";ma();Fe();ts();Id();Hl();wf();g2=new Set([z.SERIAL,z.PARALLEL,z.VECTORIZED,z.UNROLLED]),Jb=class extends ar{static{l(this,"LoopPartitionPass")}constructor(){super("LoopPartitionPass","loopPartition")}run(e,t){let r=Gi(e.body,s=>F4(s)||s);return r&&r!==e.body&&(e.body=r,e._setChild("body",r)),e}};l(Lc,"intImm");l(F4,"partitionLoop");l(z4,"matchFlatGuard")});function Zb(n){let e=n.body;if(!e||e.type!=="BlockNode")return null;let t=e.body;if(!t||t.type!=="BufferStoreNode")return null;let r=t,s=r.value;if(!s||s.type!=="MathOpNode"||!V4.has(s.op))return null;let o=null,i=null;if(s.a&&s.a.type==="BufferLoadNode"&&s.a.buffer===r.buffer?(o=s.a,i=s.b):s.b&&s.b.type==="BufferLoadNode"&&s.b.buffer===r.buffer&&(o=s.b,i=s.a),!o)return null;let a=Hv(r.indices),u=Hv(o.indices);if(a!==u||a.includes("?"))return null;let p=r.indices.map(d=>{if(d.type!=="VariableNode")return d;for(let h of e.iterVars)if(h.iterVar&&h.iterVar.name===d.name&&h.binding)return h.binding;return d}),c=n.loopVar.name,f=Hv(p);return f.includes("?")||f.includes("$"+c)?null:{store:r,loadSide:o,valueSide:i,outerIndices:p,block:e,op:r.value.op}}function Hv(n){return n.map(Yv).join(",")}function Yv(n){return n?n.type==="VariableNode"?"$"+n.name:n.type==="IntImmNode"?String(n.value):n.type==="MathOpNode"?"("+Yv(n.a)+n.op+(n.b?Yv(n.b):"")+")":"?":"?"}var V4,Jv=M(()=>{"use strict";V4=new Set(["+","*","max","min"]);l(Zb,"detectAccumulator");l(Hv,"indicesKey");l(Yv,"exprKey")});function Vo(n){if(!(!n||typeof n!="object"||!n.type))switch(n.type){case"ForNode":{let e=Zb(n);n.accumulator=e,e||Vo(n.body);return}case"BlockNode":Vo(n.body),n.initBody&&Vo(n.initBody);return;case"SeqNode":for(let e of n.stmts)Vo(e);return;case"LetStmtNode":case"AllocateNode":Vo(n.body);return;case"IfThenElseNode":Vo(n.thenBody),n.elseBody&&Vo(n.elseBody);return;case"WhileNode":Vo(n.condBody),Vo(n.loopBody);return;default:return}}var Qb,_2=M(()=>{"use strict";ma();Jv();Qb=class extends ar{static{l(this,"AccumulatorDetectionPass")}constructor(){super("AccumulatorDetectionPass","accumulatorDetect")}run(e,t){return Vo(e.body),e}};l(Vo,"annotateStmt")});function j4(n){let e=[],t=[n.body];for(;t.length>0;){let s=t.pop();if(s){if(s.type==="BlockNode"){e.push(s),t.push(s.body);continue}if(s.body&&t.push(s.body),s.stmts)for(let o of s.stmts)t.push(o);s.thenBody&&t.push(s.thenBody),s.elseBody&&t.push(s.elseBody)}}if(e.length===0)return null;let r=null;for(let s of e){if(!s.name.includes("matmul"))return null;s.reads.length>=2&&s.writes.length>=1&&(r=s)}return r}function q4(n){let e=j4(n);if(!e)return null;let t=e.reads[0].buffer,r=e.reads[1].buffer,s=e.writes[0].buffer;if(!b2.has(t.dtype)||!b2.has(r.dtype)||s.dtype!=="f32"||t.shape.length!==2||r.shape.length!==2||s.shape.length!==2)return null;let o=s.shape[0],i=s.shape[1],a=t.shape[1];if(![o,i,a].every(p=>typeof p=="number"&&p>0&&p%G4===0))return null;let u=new Set;for(let[,p]of n.bufferMap)u.add(p.name);return!u.has(t.name)||!u.has(r.name)||!u.has(s.name)?null:{M:o,N:i,K:a,a:t.name,b:r.name,c:s.name}}var G4,b2,Xb,w2=M(()=>{"use strict";ma();du();G4=16,b2=new Set(["f16","bf16"]);l(j4,"findMatmulBlock");l(q4,"detectWmmaMatmul");Xb=class extends ar{static{l(this,"AutoTensorizePass")}constructor(e){super("AutoTensorizePass","scheduling"),this.config=e,this.target=e.target}run(e,t){if(e.cublasInfo||e._tensorIntrin||!this.target||!this.target.isGPU())return;let r=q4(e);r&&(new Cn(e).tensorize("wmma_16x16x16_f16f16f32",r),t&&t.trace&&t.trace.explainsEnabled&&t.trace.explain("tensorize",e.name,"wmma_16x16x16_f16f16f32",`auto-tensorized ${r.M}x${r.N}x${r.K} f16 GEMM`,{target:this.target.name}))}}});function U4(n){let e=[],t=new Map,r=new Map,s=[{node:n,block:null}];for(;s.length>0;){let{node:o,block:i}=s.pop();if(o.type==="BufferLoadNode"&&o.buffer)t.set(o.buffer.name,(t.get(o.buffer.name)||0)+1),i&&i.reads.add(o.buffer.name);else if(o.type==="BufferStoreNode"&&o.buffer&&i){i.writes.add(o.buffer.name);let u=r.get(o.buffer.name);u||(u=new Set,r.set(o.buffer.name,u)),u.add(i.name)}let a=i;o.type==="BlockNode"&&(a={name:o.name,writes:new Set,reads:new Set,hasInit:o.initBody!=null},e.push(a));for(let u of ln(o))s.push({node:u,block:a})}return{blocks:e,loadCount:t,storeWriters:r}}var ew,T2=M(()=>{"use strict";ma();du();Do();ts();l(U4,"analyzeFunc");ew=class extends ar{static{l(this,"InlineReindexPass")}constructor(e){super("InlineReindexPass","scheduling"),this.config=e,this.target=e.target,this.snapshotPoint="afterInlineReindex"}run(e,t){if(!this.target.isGPU()||this.target.isWebGPU()||e.cublasInfo||e._tensorIntrin||tS(e))return;let r=this.config.scheduling;if(!(r.enabled||r.gpuTiling||r.autotune))return;let s=new Set;for(let[,c]of e.bufferMap)s.add(c.name);let{blocks:o,loadCount:i,storeWriters:a}=U4(e.body),u=new Cn(e),p=!1;for(let c of o){if(c.hasInit||c.writes.size===0)continue;let f=[...c.writes];if(f.every(d=>!s.has(d)&&a.get(d).size===1)&&f.some(d=>(i.get(d)||0)>0)&&[...c.reads].every(d=>(a.get(d)?a.get(d).size:0)<=1))try{u.computeInlineBlock(c.name),p=!0}catch{}}p&&Xx(e)}}});function Zv(n,e,t=W4){return t.filter(r=>r.phase===n).sort((r,s)=>r.priority-s.priority).map(r=>r.factory(e)).filter(Boolean)}var W4,x2=M(()=>{"use strict";W4=[];l(Zv,"tirPassesForPhase")});function S2(n){let e=[];for(let t of Zv("pre",n))e.push(t);e.push(new ew(n)),n.optimization.tensorize&&e.push(new Xb(n)),e.push(new Gb(n)),n.optimization.loopPartition&&e.push(new Jb),e.push(new jb),e.push(new Yb(n)),n.optimization.detectAccumulators&&e.push(new Qb);for(let t of Zv("post",n))e.push(t);return e}var v2=M(()=>{"use strict";n2();o2();m2();y2();_2();w2();T2();x2();l(S2,"buildTirPipeline")});var Vd,k2=M(()=>{"use strict";Fe();Vd=class{static{l(this,"TensorVerifier")}constructor(){this.errors=[],this.boundVars=new Set}verify(e){if(this.errors=[],this.boundVars.clear(),!(e instanceof Pi))return this.errors.push("Expected PrimFunc at root"),this.errors;for(let t of e.params)this.boundVars.add(t.name);if(e.shapeParams)for(let t of e.shapeParams)this.boundVars.add(t.name);return this.visit(e.body),this.errors}visit(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r)switch(r.type){case"SeqNode":for(let s=r.stmts.length-1;s>=0;s--)t.push(r.stmts[s]);break;case"ForNode":this.boundVars.has(r.loopVar.name)&&this.errors.push(`Loop variable ${r.loopVar.name} already bound`),this.boundVars.add(r.loopVar.name),t.push({type:"_unbind",name:r.loopVar.name}),t.push(r.body);break;case"BlockNode":for(let s of r.iterVars)s.iterVar&&(this.boundVars.has(s.iterVar.name)&&this.errors.push(`Block variable ${s.iterVar.name} already bound`),this.boundVars.add(s.iterVar.name));for(let s=r.iterVars.length-1;s>=0;s--)r.iterVars[s].iterVar&&t.push({type:"_unbind",name:r.iterVars[s].iterVar.name});t.push(r.body),r.initBody&&t.push(r.initBody);break;case"AllocateNode":r.buffer||this.errors.push("Allocate missing buffer"),t.push(r.body);break;case"LetStmtNode":this._visitExpr(r.value),this.boundVars.add(r.variable.name),t.push({type:"_unbind",name:r.variable.name}),t.push(r.body);break;case"IfThenElseNode":this._visitExpr(r.condition),r.elseBody&&t.push(r.elseBody),t.push(r.thenBody);break;case"WhileNode":r.condVar||this.errors.push("WhileNode missing condition variable"),t.push(r.loopBody),t.push(r.condBody);break;case"BufferStoreNode":if(r.buffer||this.errors.push("BufferStore missing buffer"),(!r.indices||r.indices.length!==r.buffer.shape.length)&&this.errors.push(`BufferStore rank mismatch for ${r.buffer?r.buffer.name:"unknown"}`),r.indices)for(let s of r.indices)this._visitExpr(s);this._visitExpr(r.value);break;case"EvaluateNode":this._visitExpr(r.value);break;case"_unbind":this.boundVars.delete(r.name);break;default:this._visitExpr(r);break}}}_visitExpr(e){if(e)switch(e.type){case"BufferLoadNode":if(e.buffer||this.errors.push("BufferLoad missing buffer"),(!e.indices||e.indices.length!==e.buffer.shape.length)&&this.errors.push(`BufferLoad rank mismatch for ${e.buffer?e.buffer.name:"unknown"}`),e.indices)for(let t of e.indices)this._visitExpr(t);break;case"MathOpNode":this._visitExpr(e.a),e.b&&this._visitExpr(e.b);break;case"CompareNode":this._visitExpr(e.a),this._visitExpr(e.b);break;case"CastNode":this._visitExpr(e.expr);break;case"CallExternNode":e.externName||this.errors.push("CallExtern missing function name");for(let t of e.args)this._visitExpr(t);break;case"IfThenElseNode":this._visitExpr(e.condition),this._visitExpr(e.thenBody),e.elseBody&&this._visitExpr(e.elseBody);break;case"VariableNode":this.boundVars.has(e.name)||this.errors.push(`Unbound variable used: ${e.name}`);break;case"BlockRealizeNode":e.binding&&this._visitExpr(e.binding);break;case"IntImmNode":case"FloatImmNode":break}}}});function Qv(n){let e=[];n.functionCount===0&&e.push(new Et("Module has no functions"));for(let t of n)Gd(t,e);return e}function Gd(n,e=[]){if(!n.entryBlock)return e.push(new Et("Function has no entry block",null,n)),e;n.entryBlock.arguments.length!==n.inputTypes.length&&e.push(new Et(`Entry block has ${n.entryBlock.arguments.length} args, expected ${n.inputTypes.length}`,null,n));for(let s=0;s<n.entryBlock.arguments.length;s++){let o=n.entryBlock.arguments[s],i=n.inputTypes[s];i&&!o.type.equals(i)&&e.push(new Et(`Block arg ${s} type ${Gn(o.type)} != expected ${Gn(i)}`,null,n))}let t=new Set;for(let s of n.entryBlock.arguments)t.add(s);for(let s of n.body)N2(s,t);for(let s of n.body)A2(s,n,t,e);let r=n.getReturnOp();if(!r)e.push(new Et("Missing return op",null,n));else{r.numOperands!==n.outputTypes.length&&e.push(new Et(`Return has ${r.numOperands} operands, function declares ${n.outputTypes.length} outputs`,r,n));for(let s=0;s<Math.min(r.numOperands,n.outputTypes.length);s++){let o=r.getOperand(s).type,i=n.outputTypes[s];i instanceof I&&o instanceof I&&(o.dtype!==i.dtype&&e.push(new Et(`Return operand ${s} dtype ${o.dtype} != expected ${i.dtype}`,r,n)),o.shapeCompatible(i)||e.push(new Et(`Return operand ${s} shape incompatible: ${Gn(o)} vs ${Gn(i)}`,r,n)))}}return e}function N2(n,e){for(let t of n.arguments)e.add(t);for(let t of n)for(let r=0;r<t.numResults;r++){let s=t.getResult(r);s&&e.add(s)}}function K4(n,e,t){let r=new Set;for(let u of n)r.add(u);let s=1,o=2,i=new Map,a=new Set;for(let u of n){if(i.get(u)!==void 0)continue;let p=[{op:u,i:0}];for(i.set(u,s);p.length>0;){let c=p[p.length-1],f=c.op;if(c.i<f.numOperands){let d=f.getOperand(c.i);c.i++;let h=d&&d.definingOp;if(!h||!r.has(h))continue;let m=i.get(h);m===s?a.has(h)||(a.add(h),t.push(new Et("participates in a value dependency cycle",h,e))):m===void 0&&(i.set(h,s),p.push({op:h,i:0}));continue}i.set(f,o),p.pop()}}}function A2(n,e,t,r){K4(n,e,r);for(let s of n)H4(s,e,t,r);if(n.size>0){let s=n.lastOp;if(n.parentRegion&&n.parentRegion.parentOp){let o=n.parentRegion.parentOp.opName,i=ne.get(o);i&&i.hasRegions&&(s.isTerminator()||r.push(new Et(`Block in ${o} region must end with terminator, got '${s.opName}'`,s,e)))}}}function H4(n,e,t,r){for(let o=0;o<n.numOperands;o++){let i=n.getOperand(o);if(!i){r.push(new Et(`Operand ${o} is null`,n,e));continue}if(!(i instanceof Li)){r.push(new Et(`Operand ${o} is not a Value`,n,e));continue}t.has(i)||r.push(new Et(`Operand ${o} used before definition`,n,e))}for(let o=0;o<n.numResults;o++){let i=n.getResult(o);i||r.push(new Et(`Result ${o} is null`,n,e)),i&&i.definingOp!==n&&r.push(new Et(`Result ${o} definingOp mismatch`,n,e))}let s=ne.get(n.opName);if(!s){!["return","yield"].includes(n.opName)&&!ne.has(n.opName)&&r.push(new Et(`Unknown op '${n.opName}'`,n,e));return}s.numOperands>=0&&n.numOperands!==s.numOperands&&r.push(new Et(`'${n.opName}' expects ${s.numOperands} operands, got ${n.numOperands}`,n,e)),s.numResults>=0&&n.numResults!==s.numResults&&r.push(new Et(`'${n.opName}' expects ${s.numResults} results, got ${n.numResults}`,n,e));for(let o of s.attrs)o.required&&!n.hasAttr(o.name)&&r.push(new Et(`'${n.opName}' missing required attribute '${o.name}'`,n,e));if(s.hasRegions&&s.numRegions>0&&n.numRegions!==s.numRegions&&r.push(new Et(`'${n.opName}' expects ${s.numRegions} regions, got ${n.numRegions}`,n,e)),s.verify){let o=s.verify(n);if(o)for(let i of o)r.push(new Et(i,n,e))}if(s.inferResultTypes&&n.numResults>0){let o=[];for(let a=0;a<n.numOperands;a++)o.push(n.getOperand(a).type);let i=s.inferResultTypes(o,n.attributes,n.results.map(a=>a.type));if(i)for(let a=0;a<Math.min(i.length,n.numResults);a++){let u=n.getResult(a).type,p=i[a];p instanceof I&&u instanceof I&&(u.dtype!==p.dtype&&r.push(new Et(`Result ${a} dtype ${u.dtype} != inferred ${p.dtype}`,n,e)),u.shapeCompatible(p)||r.push(new Et(`Result ${a} shape ${Gn(u)} incompatible with inferred ${Gn(p)}`,n,e)))}}for(let o of n.regions){let i=new Set(t);for(let a of o)N2(a,i);for(let a of o)A2(a,e,i,r)}}var Et,O2=M(()=>{"use strict";Re();Mh();Ct();Et=class{static{l(this,"VerificationError")}constructor(e,t=null,r=null){this.message=e,this.op=t,this.func=r}toString(){let e="";return this.func&&(e+=`[${this.func.name}] `),this.op&&(e+=`op '${this.op.opName}' (id=${this.op.id}): `),e+this.message}};l(Qv,"verifyModule");l(Gd,"verifyFunction");l(N2,"collectScopeDefs");l(K4,"detectCycles");l(A2,"verifyBlock");l(H4,"verifyOperation")});var tw,Xv,Cc,ek,tk=M(()=>{"use strict";cv();Re();tw=class{static{l(this,"ValueObserver")}constructor(){this.min=1/0,this.max=-1/0,this.count=0,this.histogram=null}update(e){for(let t=0;t<e.length;t++){let r=e[t];r<this.min&&(this.min=r),r>this.max&&(this.max=r)}this.count+=e.length,this.histogram&&this.histogram.update(e)}enableHistogram(e=2048){this.histogram=new Xv(e)}},Xv=class{static{l(this,"HistogramCollector")}constructor(e=2048){this.numBins=e,this.bins=new Float64Array(e),this.rangeMin=0,this.rangeMax=0,this.initialized=!1,this.totalCount=0}update(e){this.initialized||this._initRange(e);let t=this.rangeMax-this.rangeMin;if(t<=0)return;for(let s=0;s<e.length;s++){let o=e[s];if(o<this.rangeMin||o>this.rangeMax){this._expandAndRebucket(e);return}}let r=this.numBins/t;for(let s=0;s<e.length;s++){let o=Math.min(this.numBins-1,Math.floor((e[s]-this.rangeMin)*r));this.bins[o]++}this.totalCount+=e.length}_initRange(e){let t=1/0,r=-1/0;for(let s=0;s<e.length;s++)e[s]<t&&(t=e[s]),e[s]>r&&(r=e[s]);t===r&&(r=t+1),this.rangeMin=t,this.rangeMax=r,this.initialized=!0}_expandAndRebucket(e){let t=this.rangeMin,r=this.rangeMax;for(let c=0;c<e.length;c++)e[c]<t&&(t=e[c]),e[c]>r&&(r=e[c]);let s=new Float64Array(this.bins),o=this.rangeMin,a=this.rangeMax-o;this.rangeMin=t,this.rangeMax=r;let u=r-t,p=this.numBins/u;if(this.bins.fill(0),a>0){let c=a/this.numBins;for(let f=0;f<this.numBins;f++){if(s[f]===0)continue;let d=o+(f+.5)*c,h=Math.min(this.numBins-1,Math.floor((d-t)*p));this.bins[h]+=s[f]}}for(let c=0;c<e.length;c++){let f=Math.min(this.numBins-1,Math.floor((e[c]-t)*p));this.bins[f]++}this.totalCount+=e.length}computePercentileThreshold(e){let t=this.totalCount*e,r=0,s=(this.rangeMax-this.rangeMin)/this.numBins;for(let o=0;o<this.numBins;o++)if(r+=this.bins[o],r>=t)return this.rangeMin+(o+1)*s;return this.rangeMax}computeEntropyThreshold(e){if(this.totalCount===0)return this.rangeMax;let t=new Float64Array(this.numBins);for(let i=0;i<this.numBins;i++)t[i]=this.bins[i]/this.totalCount;let r=this.numBins,s=1/0;for(let i=e;i<=this.numBins;i++){let a=i/e,u=new Float64Array(i);for(let c=0;c<e;c++){let f=Math.floor(c*a),d=Math.min(i,Math.floor((c+1)*a)),h=0;for(let g=f;g<d;g++)h+=t[g];let m=d-f;if(m>0){let g=h/m;for(let y=f;y<d;y++)u[y]=g}}let p=0;for(let c=0;c<i;c++)t[c]>0&&u[c]>0&&(p+=t[c]*Math.log(t[c]/u[c]));p<s&&(s=p,r=i)}let o=(this.rangeMax-this.rangeMin)/this.numBins;return this.rangeMin+r*o}},Cc=class{static{l(this,"CalibrationCollector")}constructor(e="minmax"){this.observers=new Map,this.mode=e}attach(e){for(let t of e.ops())for(let r=0;r<t.numResults;r++){let s=t.getResult(r);if(s.type instanceof I&&jt(s.type.dtype)){let o=new tw;(this.mode==="entropy"||this.mode==="percentile")&&o.enableHistogram(),this.observers.set(s,o)}}for(let t of e.args)if(t.type instanceof I&&jt(t.type.dtype)){let r=new tw;(this.mode==="entropy"||this.mode==="percentile")&&r.enableHistogram(),this.observers.set(t,r)}}observe(e,t){let r=this.observers.get(e);r&&r.update(t)}getResult(){return new ek(this.observers,this.mode)}},ek=class{static{l(this,"CalibrationResult")}constructor(e,t){this._observers=e,this._mode=t}getRange(e){let t=this._observers.get(e);return!t||t.count===0?null:{min:t.min,max:t.max}}getQuantParams(e,t,r){let s=this.getRange(e);if(!s)return null;let o=t===Pr.PER_TENSOR_ASYMMETRIC;if(this._mode==="percentile"&&this._observers.get(e).histogram){if(o)return vr.fromRange(s.min,s.max,t,r);let a=this._observers.get(e).histogram.computePercentileThreshold(.999);return vr.fromRange(-a,a,t,r)}if(this._mode==="entropy"&&this._observers.get(e).histogram){if(o)return vr.fromRange(s.min,s.max,t,r);let i=this._observers.get(e).histogram,a=r==="ui8"?256:255,u=i.computeEntropyThreshold(a);return vr.fromRange(-u,u,t,r)}return vr.fromRange(s.min,s.max,t,r)}hasData(e){let t=this._observers.get(e);return t&&t.count>0}values(){return this._observers.keys()}}});function J4(n,e){let t=[],r=new Set;for(let s of n.ops())if(e.has(s.opName))for(let o=0;o<s.numOperands;o++){let i=s.getOperand(o);if(r.has(i)||!(i.type instanceof I)||!jt(i.type.dtype))continue;let a=i.definingOp;a&&a.opName==="constant"||(r.add(i),t.push(i))}return t}function Z4(n,e){let t=e.map(h=>h.type),r=new yn(n.name,n.inputTypes,[...n.outputTypes,...t]),s=new Map,o=n.entryBlock.arguments,i=r.entryBlock.arguments;for(let h=0;h<o.length;h++)s.set(o[h],i[h]);let a=n.entryBlock.opsArray(),u=new Set(a),p=Qo(a,h=>u.has(h),"ignore"),c=new Map;for(let h of p)h.opName!=="return"&&c.set(h,h.clone(s));for(let h of a)h.opName!=="return"&&r.entryBlock.pushOp(c.get(h));let f=n.getReturnOp(),d=[];if(f)for(let h of f.operands)d.push(s.get(h)||h);for(let h of e)d.push(s.get(h));return r.entryBlock.pushOp(new Me("return",d,[],{})),r}function B2(n,e,t,r={}){if(e.isGPU&&e.isGPU())throw new Error("collectCalibration: synchronous calibration is unavailable for async (GPU) targets; precompute calibration on a CPU/WASM target or supply quantization.calibration directly");if(!t||t.length===0)throw new Error("collectCalibration: at least one calibration batch is required");let s=r.quantizableOps||Y4,o=r.mode||"minmax",i=J4(n,s),a=new Cc(o);a.attach(n);let u=a.getResult();if(i.length===0)return u;let p=Z4(n,i),c=new Mn("__calibrate__");c.addFunction(p);let f=n.outputTypes.length,h=(r.compileFn||Q4)(c,e);for(let m of t){let g=Array.isArray(m)?m:[m],y=p.outputTypes.map(_=>new Float32Array(Math.max(1,_.numel())));h.run(n.name,...g,...y);for(let _=0;_<i.length;_++)a.observe(i[_],y[f+_])}return u}function Q4(n,e){throw new Error("collectCalibration: opts.compileFn is required (pass the compileModule function to avoid a circular import)")}var Y4,R2=M(()=>{"use strict";qs();Di();yr();Re();Da();tk();Y4=new Set(["dot","conv","add","mul","sub"]);l(J4,"activationTargets");l(Z4,"buildCaptureFunction");l(B2,"collectCalibration");l(Q4,"defaultCompileFn")});function rk(n){let e=0;for(let t=0;t<n.numResults;t++){let r=n.getResult(t).type;r instanceof I&&r.isFullyStatic&&(e+=r.sizeInBytes())}return e}var nk,Dc,sk,nw,M2=M(()=>{"use strict";Ct();Re();Nd();nk=class{static{l(this,"Partition")}constructor(e,t){this.id=e,this.target=t,this.ops=[],this.opSet=new Set,this._inputValues=null,this._outputValues=null,this._memoryBytes=0}addOp(e){this.opSet.has(e)||(this.ops.push(e),this.opSet.add(e),this._inputValues=null,this._outputValues=null,this._memoryBytes+=rk(e))}hasOp(e){return this.opSet.has(e)}merge(e){for(let t of e.ops)this.addOp(t)}computeIO(){if(this._inputValues&&this._outputValues)return;let{inputs:e,outputs:t}=pa(this.opSet,this.ops);this._inputValues=e,this._outputValues=t}getInputValues(){return this.computeIO(),this._inputValues}getOutputValues(){return this.computeIO(),this._outputValues}get size(){return this.ops.length}get memoryBytes(){return this._memoryBytes}};l(rk,"estimateOpMemory");Dc=class{static{l(this,"PartitionerConfig")}constructor(e={}){this.targets=e.targets||[],this.defaultTarget=e.defaultTarget||null,this.opTargetOverrides=e.opTargetOverrides||new Map,this.memoryLimits=e.memoryLimits||new Map,this.minPartitionSize=e.minPartitionSize||1,this.costWeights={transferCost:1,loadBalance:.5,...e.costWeights||{}}}},sk=class{static{l(this,"PartitionResult")}constructor(e,t,r){this.partitions=e,this.opToPartition=t,this.transferEdges=r}getPartition(e){return this.opToPartition.get(e)||null}getPartitionsForTarget(e){return this.partitions.filter(t=>t.target===e||t.target.name===e.name)}get numPartitions(){return this.partitions.length}},nw=class{static{l(this,"GraphPartitioner")}constructor(e){this.config=e instanceof Dc?e:new Dc(e),this._supportCache=new Map,this._buildSupportMap()}partition(e){let t=this._collectPartitionableOps(e),r=this._assignTargets(t),s=this._buildPartitions(t,r),o=this._mergeSmallPartitions(s,r),i=this._computeTransferEdges(o),a=new Map;for(let u of o)for(let p of u.ops)a.set(p,u);return new sk(o,a,i)}_buildSupportMap(){for(let e of this.config.targets){let t=new Set;for(let r of ne.names())this._targetSupportsOp(e,r)&&t.add(r);this._supportCache.set(e,t)}}_targetSupportsOp(e,t){if(e.hasLibraryOp(t))return!0;let r=ne.get(t);return r?r.isConstant||r.isTerminator?!0:e.isGPU()?r.isElementwise||r.isReduction||r.isBroadcast||r.isInjective||r.getAttr("gpuCapable")===!0:e.isCPU()?!0:e.isWasm()?!r.isOpaque&&t!=="custom_call":!1:!1}_collectPartitionableOps(e){let t=[];for(let r of e.ops()){let s=ne.get(r.opName);!s||s.isTerminator||t.push(r)}return t}_assignTargets(e){let t=new Map;for(let r of e){let s=r.getAttr("device");if(s){let a=this._resolveDeviceAttr(s);if(a){t.set(r,a);continue}}let o=this.config.opTargetOverrides.get(r.opName);if(o){t.set(r,o);continue}let i=this._selectBestTarget(r);t.set(r,i)}return t}_resolveDeviceAttr(e){return typeof e=="string"?this.config.targets.find(t=>t.name===e||t.kind===e)||null:e}_selectBestTarget(e){let t=this.config.defaultTarget||this.config.targets[0],r=-1/0;for(let s of this.config.targets){let o=this._supportCache.get(s);if(!o||!o.has(e.opName))continue;let i=this._scoreTargetForOp(s,e);i>r&&(r=i,t=s)}return t}_scoreTargetForOp(e,t){let r=ne.get(t.opName);if(!r)return 0;let s=0;if(e.hasLibraryOp(t.opName)&&(s+=100),r.isReduction||r.isElementwise){let o=0;for(let i=0;i<t.numOperands;i++){let a=t.getOperand(i).type;a instanceof I&&a.isFullyStatic&&(o+=a.numel())}e.isGPU()&&o>1024?s+=50:e.isCPU()&&o<=1024&&(s+=30)}return s+=e.computeTFLOPs*10,s}_buildPartitions(e,t){let{partitions:r}=kd(e,{sort:l(i=>this._topologicalSort(i),"sort"),labelOf:l(i=>t.get(i),"labelOf"),sameLabel:l((i,a)=>i===a||i.name===a.name,"sameLabel"),canMerge:l((i,a,u)=>this._fitsMemoryLimit(i,a,u),"canMerge"),onAttach:l((i,a)=>{i.memoryBytes=(i.memoryBytes||0)+rk(a)},"onAttach")}),s=new Map;for(let i of r){let a=new nk(i.id,i.label);for(let u of i.ops)a.addOp(u);s.has(i.label.name)||s.set(i.label.name,[]),s.get(i.label.name).push(a)}let o=[];for(let i of s.values())for(let a of i)o.push(a);return o}_fitsMemoryLimit(e,t,r){let s=this.config.memoryLimits.get(r.name);if(!s)return!0;let o=rk(t);return e.memoryBytes+o<=s}_mergeSmallPartitions(e,t){if(e.length<=1)return e;let r=new Map;for(let h of e)for(let m of h.ops)r.set(m,h);let s=new Set,o=new Map,i=new Map;for(let h of e)s.has(h)||o.set(h,new Set);for(let h of e){if(s.has(h))continue;let m=o.get(h);for(let g of h.ops)for(let y=0;y<g.numResults;y++)for(let _ of g.getResult(y).uses()){let b=r.get(_.user);b&&b!==h&&m.add(b)}}let a=l((h,m)=>{let g=o.get(h);for(let y of o.get(m))y!==h&&g.add(y);g.delete(m);for(let[y,_]of o)_.has(m)&&(_.delete(m),y!==h&&_.add(h));o.delete(m),i=new Map},"mergeSucc"),u=l(h=>{let m=i.get(h);if(m)return m;m=new Set;let g=[...o.get(h)];for(;g.length>0;){let y=g.pop();if(m.has(y))continue;m.add(y);let _=o.get(y);if(_)for(let b of _)g.push(b)}return i.set(h,m),m},"reachOf"),p=l((h,m)=>{for(let g of o.get(h))if(g!==m&&u(g).has(m))return!0;return!1},"pathThroughIntermediate"),c=l((h,m)=>p(h,m)||p(m,h),"mergeCreatesCycle"),f=[];for(let h=0;h<e.length;h++){let m=e[h];if(s.has(m))continue;if(m.size>=this.config.minPartitionSize){f.push(m);continue}let g=-1,y=-1/0;for(let _=0;_<e.length;_++){if(h===_)continue;let b=e[_];if(s.has(b)||b.target.name!==m.target.name||c(m,b))continue;let w=this.config.costWeights.transferCost*this._mergeScore(m,b);w>y&&(y=w,g=_)}if(g>=0){let _=e[g];a(_,m),_.merge(m);for(let b of m.ops)r.set(b,_);s.add(m)}else f.push(m)}let d=new Set;for(let h of f)for(let m of h.ops)d.add(m);for(let h of e){let m=!1;for(let g of h.ops)if(!d.has(g)){m=!0;break}if(m){f.push(h);for(let g of h.ops)d.add(g)}}return f}_mergeScore(e,t){let r=0,s=new Set;for(let o of e.ops)for(let i=0;i<o.numResults;i++)s.add(o.getResult(i));for(let o of t.ops)for(let i=0;i<o.numOperands;i++)s.has(o.getOperand(i))&&r++;return r}_computeTransferEdges(e){let t=[],r=new Set,s=new Map;for(let o of e)for(let i of o.ops)s.set(i,o);for(let o of e)for(let i of o.ops)for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a).definingOp;if(!u)continue;let p=s.get(u);if(!p||p===o)continue;let c=i.getOperand(a),f=`${p.id}|${o.id}|${c.id}`;r.has(f)||(r.add(f),t.push({src:p,dst:o,value:c,sizeBytes:c.type instanceof I&&c.type.isFullyStatic?c.type.sizeInBytes():0}))}return t}_topologicalSort(e){let t=new Set(e),r=new Map,s=new Map;for(let u of e)r.set(u,0),s.set(u,[]);for(let u of e)for(let p=0;p<u.numOperands;p++){let c=u.getOperand(p).definingOp;c&&t.has(c)&&(s.get(c).push(u),r.set(u,r.get(u)+1))}let o=[];for(let u of e)r.get(u)===0&&o.push(u);let i=[],a=0;for(;a<o.length;){let u=o[a++];i.push(u);for(let p of s.get(u)){let c=r.get(p)-1;r.set(p,c),c===0&&o.push(p)}}return i}}});var rw,sw,I2=M(()=>{"use strict";fn();yr();qs();M2();Nd();nn();rw=class extends Ue{static{l(this,"GraphPartitionPass")}constructor(e={}){super("GraphPartitionPass"),this.partitionerConfig=new Dc(e),this.partitionResult=null}run(e,t){if(this.partitionerConfig.targets.length<2)return oe.UNCHANGED;let r=new nw(this.partitionerConfig);return this.partitionResult=r.partition(e),this.partitionResult.numPartitions<=1?oe.UNCHANGED:(this._annotateOps(),this._insertTransferOps(e),this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,numPartitions:this.partitionResult.numPartitions,transferEdges:this.partitionResult.transferEdges.length,level:le.DEBUG}),e.bumpVersion(),oe.CHANGED)}_annotateOps(){for(let e of this.partitionResult.partitions)for(let t of e.ops)t.setAttr("partition_id",e.id),t.setAttr("partition_target",e.target.name)}_insertTransferOps(e){let t=e.entryBlock,r=this._buildOrderIndex(t),{useMap:s,firstInPart:o}=this._buildInsertionIndex(t);for(let i of this.partitionResult.transferEdges){let a=i.value,u=i.src.target.name,p=i.dst.target.name;if(u===p)continue;let c=new Me("copy_to_device",[a],[a.type],{src_device:u,dst_device:p});c.setAttr("partition_id",i.dst.id),c.setAttr("partition_target",p);let f=s.get(i.dst),d=f&&f.get(a)||o.get(i.dst)||null,h=a.definingOp;if(d&&h&&r.has(h)&&r.has(d)&&r.get(h)>=r.get(d))t.insertAfter(c,h);else if(d)t.insertBefore(c,d);else if(h&&r.has(h))t.insertAfter(c,h);else{let g=e.getReturnOp();g?t.insertBefore(c,g):t.pushOp(c)}let m=c.getResult(0);for(let g of i.dst.ops)for(let y=0;y<g.numOperands;y++)g.getOperand(y)===a&&g.replaceOperand(y,m)}}_buildInsertionIndex(e){let t=[...new Set(this.partitionResult.transferEdges.map(o=>o.dst))],r=new Map,s=new Map;for(let o of t)r.set(o,new Map);for(let o of e.ops())for(let i of t){if(!i.hasOp(o))continue;s.has(i)||s.set(i,o);let a=r.get(i);for(let u=0;u<o.numOperands;u++){let p=o.getOperand(u);a.has(p)||a.set(p,o)}}return{useMap:r,firstInPart:s}}_buildOrderIndex(e){let t=new Map,r=0;for(let s of e.ops())t.set(s,r++);return t}},sw=class extends Ue{static{l(this,"PartitionMaterializationPass")}constructor(e={}){super("PartitionMaterializationPass"),this.targets=e.targets||[]}run(e,t){let r=this._collectPartitions(e);if(r.size<=1)return oe.UNCHANGED;let s=this._getModule(e);if(!s)return oe.UNCHANGED;let o=this._materializePartitions(e,r);for(let i of o)s.addFunction(i);return this._rewriteOriginalFunction(e,o,r),this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,partitions:r.size,subFunctions:o.length,level:le.DEBUG}),e.bumpVersion(),oe.CHANGED}_collectPartitions(e){let t=new Map;for(let r of e.ops()){let s=r.getAttr("partition_id");s!==void 0&&(t.has(s)||t.set(s,{id:s,target:r.getAttr("partition_target"),ops:[]}),t.get(s).ops.push(r))}return t}_getModule(e){return e._module||null}_materializePartitions(e,t){let r=[];for(let[s,o]of t){let i=new Set(o.ops),{inputs:a,outputs:u}=pa(i,o.ops),p=a.map(_=>_.type),c=u.map(_=>_.type),f=`${e.name}_partition_${s}`,d=new yn(f,p,c);d._partitionTarget=o.target;let h=new Map;for(let _=0;_<a.length;_++)h.set(a[_],d.args[_]);let m=Ru(o.ops);for(let _ of m)d.entryBlock.pushOp(_.clone(h));let g=u.map(_=>h.get(_)||_),y=new Me("return",g,[]);d.entryBlock.pushOp(y),r.push(d)}return r}_rewriteOriginalFunction(e,t,r){for(let s of t)e.setAttr?.(`sub_${s.name}`,s._partitionTarget)}}});function jd(n){return ip(n,-1)}function X4(n,e,t){let r=t.sharedMemoryBytes||16384,s=0;for(let u of e.entryBlock.ops())if(u.opName!=="yield")for(let p=0;p<u.numResults;p++){let c=u.getResult(p).type;if(!c||!c.shape)continue;let f=jd(c.shape);if(f<0)return!0;let d=f*Rn(c.dtype);d>s&&(s=d)}let o=n.getAttr("num_carry"),i=n.getAttr("num_xs"),a=0;for(let u=0;u<o;u++){let p=n.getOperand(i+u).type,c=p&&p.shape?jd(p.shape):-1;c>0&&(a+=c*Rn(p.dtype))}return 3*s+2*a>r}function ej(n,e){let r=n.regions[0].entryBlock,s=n.getAttr("num_carry"),o=[],i=null;for(let b of r.ops()){if(b.opName==="yield"){i=b;continue}o.push(b)}if(!i)return null;let a=new Set(o),u=new Set(r.arguments),p=[],c=new Set,f=[],d=new Set;for(let b of o)for(let w=0;w<b.numOperands;w++){let x=b.getOperand(w),S=x.definingOp;if(!u.has(x)&&!(S&&a.has(S))){if(S&&$o(S)){d.has(S)||(d.add(S),f.push(S));continue}c.has(x)||(c.add(x),p.push(x))}}let h=[...r.arguments,...p];for(let b of h)if(!b.type||!b.type.isFullyStatic)return null;let m=[];for(let b=0;b<i.numOperands;b++)m.push(i.getOperand(b));let g=new yn(e,h.map(b=>b.type),m.map(b=>b.type)),y=new Map;for(let b=0;b<h.length;b++)y.set(h[b],g.args[b]);for(let b of f)g.entryBlock.pushOp(b.clone(y));for(let b of o)g.entryBlock.pushOp(b.clone(y));let _=m.map(b=>y.get(b));return _.some(b=>b===void 0)?null:(g.entryBlock.pushOp(new Me("return",_,[])),{bodyFunc:g,captured:p,numCarry:s,numYs:m.length-s})}function tj(n,e,t,r,s){let{getSlot:o,newSlot:i,steps:a,addedFuncs:u}=r,p=new Mn(n.name+"_mod");p.addFunction(n);let c=ha(p,s);if(c){let f=c.plan,d=e.length,h=new Map,m=!0;for(let g of f.returnFixups||[]){if(g.kind!=="copy"){m=!1;break}h.set(g.pos,g.srcSlot)}if(m){let g=l(x=>{let S=d+x;return h.has(S)?h.get(S):f.argSlots[S]},"retSlot"),y=new Map;for(let x=0;x<d;x++)y.set(f.argSlots[x],o(e[x]));for(let x of f.intermediates)y.set(x.slot,i(x.shape,x.dtype));for(let x=0;x<t.length;x++){let S=g(x);y.has(S)||y.set(S,o(t[x]))}let _=l(x=>{let S=y.get(x);return S===void 0?null:S},"mapSlot"),b=[],w=!0;for(let x of f.steps){let S=x.inputSlots.map(_),T=x.outputSlots.map(_);if(S.includes(null)||T.includes(null)){w=!1;break}b.push({name:x.name,inputSlots:S,outputSlots:T})}if(w){for(let x of b)a.push(x);for(let x of p.functions())u.push(x);return!0}}}return a.push({name:n.name,inputSlots:e.map(o),outputSlots:t.map(o)}),u.push(n),!0}function E2(n,e,t){let r=fb({ops:n,opSet:new Set(n)},e,new Map);return r?tj(r.subFunc,r.inputs,r.outputs,t,1):!1}function nj(n,e,t){let{getSlot:r,newSlot:s,steps:o,scanLoops:i,addedFuncs:a}=t,u=n.regions[0];if(!u||!u.entryBlock)return!1;let p=ej(n,e);if(!p)return!1;let{bodyFunc:c,captured:f,numCarry:d,numYs:h}=p,m=n.getAttr("num_xs"),g=new Mn(e+"_mod");g.addFunction(c);let y=ha(g,2),_=y?y.plan:null,b=[],w=[];for(let D=0;D<d;D++){let V=n.getOperand(m+D).type;b.push(V.shape),w.push(V.dtype)}let x=[],S=[];for(let D=0;D<m;D++){let V=n.getOperand(D).type;x.push(V.shape.slice(1)),S.push(V.dtype)}let T=[],v=[];for(let D=0;D<h;D++){let V=n.getResult(d+D).type;T.push(V.shape.slice(1)),v.push(V.dtype)}let A=b.map((D,V)=>s(D,w[V])),N=b.map((D,V)=>s(D,w[V])),k=x.map((D,V)=>s(D,S[V])),O=[],R=[],B=[],E=[];for(let D=0;D<d;D++)R.push(r(n.getOperand(m+D)));for(let D=0;D<d;D++)B.push(r(n.getResult(D)));for(let D of f)E.push(r(D));let F=[],C=[];for(let D=0;D<m;D++)F.push(r(n.getOperand(D)));for(let D=0;D<h;D++)C.push(r(n.getResult(d+D)));let H=o.length;if(!_)O=T.map((D,V)=>s(D,v[V])),o.push({name:c.name,inputSlots:[...k,...A,...E],outputSlots:[...N,...O]}),a.push(c);else{let D=m+d+f.length,V=new Map;for(let j of _.returnFixups||[]){if(j.kind!=="copy")return!1;V.set(j.pos,j.srcSlot)}let ue=l(j=>{let W=D+j;return V.has(W)?V.get(W):_.argSlots[W]},"bodyReturnSlot"),ce=new Map;for(let j=0;j<m;j++)ce.set(_.argSlots[j],k[j]);for(let j=0;j<d;j++)ce.set(_.argSlots[m+j],A[j]);for(let j=0;j<f.length;j++)ce.set(_.argSlots[m+d+j],E[j]);for(let j of _.intermediates)ce.set(j.slot,s(j.shape,j.dtype));for(let j=0;j<d;j++){let W=ue(j);ce.has(W)||ce.set(W,N[j])}for(let j=0;j<h;j++){let W=ue(d+j),ie=ce.get(W);ie===void 0&&(ie=s(T[j],v[j]),ce.set(W,ie)),O.push(ie)}let Le=l(j=>{let W=ce.get(j);return W===void 0?null:W},"mapSlot");for(let j of _.steps){let W=j.inputSlots.map(Le),ie=j.outputSlots.map(Le);if(W.includes(null)||ie.includes(null))return!1;o.push({name:j.name,inputSlots:W,outputSlots:ie})}for(let j of g.functions())a.push(j)}let G=o.length,q=n.getOperand(0).type.shape[0];return typeof q!="number"||q<0?!1:(i.push({T:q,loopStart:H,loopEnd:G,carry:b.map((D,V)=>({a:A[V],b:N[V],initSlot:R[V],finalSlot:B[V],bytes:jd(D)*Rn(w[V])})),xs:k.map((D,V)=>({xtSlot:D,xsSlot:F[V],stepBytes:jd(x[V])*Rn(S[V])})),ys:O.map((D,V)=>({ytSlot:D,ysSlot:C[V],stepBytes:jd(T[V])*Rn(v[V])}))}),!0)}function ok(n,e,t=!1){if(!e||typeof e.isWebGPU!="function"||!e.isWebGPU()||n.functionCount!==1)return null;let r=n.functions().next().value,s=r.getReturnOp();if(!s)return null;let o=[];for(let R of r.ops())R.opName==="scan"&&o.push(R);if(o.length===0)return null;let i=new Set,a=l(R=>{if(!(!R||i.has(R))){i.add(R);for(let B=0;B<R.numOperands;B++)a(R.getOperand(B).definingOp)}},"visit");for(let R=0;R<s.numOperands;R++)a(s.getOperand(R).definingOp);for(let R of o)if(!i.has(R))return null;if(!t){let R=!1;for(let B of o){let E=B.regions[0];if(E&&E.entryBlock&&X4(B,E,e)){R=!0;break}}if(!R)return null}let u=[];for(let R of i)$o(R)||Xt(R.opName)||u.push(R);let p=Ru(u),c=new Map,f=0,d=l(R=>{let B=c.get(R);return B===void 0&&(B=f++,c.set(R,B)),B},"getSlot");for(let R of r.args)d(R);let h=[],m=l((R,B)=>{let E=f++;return h.push({slot:E,shape:[...R],dtype:B}),E},"newSlot"),g=[],y=[],_=[],b={getSlot:d,newSlot:m,steps:g,scanLoops:y,addedFuncs:_},w=new Set(o),x=[],S=0,T=0;for(let R of p)if(w.has(R)){if(x.length&&!E2(x,r.name+"_seg"+S++,b)||(x=[],!nj(R,r.name+"_scan"+T++,b)))return null}else x.push(R);if(x.length&&!E2(x,r.name+"_seg"+S++,b))return null;let v=r.args.map(d),A=[],N=new Set;for(let R=0;R<s.numOperands;R++){let B=s.getOperand(R),E=v.length;if(c.has(B)&&!(B.isBlockArgument&&B.isBlockArgument())){let F=d(B);N.has(F)?(v.push(f++),A.push({pos:E,kind:"copy",srcSlot:F})):(N.add(F),v.push(F))}else if(B.isBlockArgument&&B.isBlockArgument())v.push(f++),A.push({pos:E,kind:"copy",srcSlot:d(B)});else return null}let k=new Set(v),O=new Set;for(let[R,B]of c)if(!(k.has(B)||O.has(B))){if(O.add(B),!R.type||!R.type.isFullyStatic)return null;h.some(E=>E.slot===B)||h.push({slot:B,shape:[...R.type.shape],dtype:R.type.dtype})}n.removeFunction(r.name);for(let R of _)n.addFunction(R);return{plan:{numSlots:f,argSlots:v,intermediates:h,steps:g,returnFixups:A,scanLoops:y}}}var L2=M(()=>{"use strict";yr();qs();Di();db();Ir();tr();Re();l(jd,"numel");l(X4,"isScanOversized");l(ej,"buildScanBodyFunction");l(tj,"inlineNativeSplit");l(E2,"emitSegment");l(nj,"emitScanLoop");l(ok,"splitGraphForScan")});function qd(n){ik.push(n),ik.sort((e,t)=>(e.priority??100)-(t.priority??100))}function rj(n){for(let e of ik)if(e.applies(n))return e;return null}function C2(n,e){let t=rj(e);return t?t.run(n,e):null}var ik,D2=M(()=>{"use strict";db();L2();ik=[];l(qd,"registerGraphSplitStrategy");l(rj,"selectGraphSplitStrategy");l(C2,"splitGraph");qd({name:"cublas",priority:10,applies:l(n=>n.config.matmulBackend==="cublas","applies"),run:l(n=>QE(n),"run")});qd({name:"cuda-attention",priority:15,applies:l(n=>n.cudaAttention,"applies"),run:l(n=>ha(n,1),"run")});qd({name:"cuda-native-matmul-chain",priority:20,applies:l(n=>n.cudaMatmulChain,"applies"),run:l(n=>ha(n),"run")});qd({name:"cuda-native-conv-chain",priority:25,applies:l(n=>n.cudaConvChain,"applies"),run:l(n=>ha(n),"run")});qd({name:"webgpu",priority:30,applies:l(n=>n.isWebGPU,"applies"),run:l((n,e)=>{let t=ok(n,e.target);return!t&&JE(n,e.target.maxThreadsPerBlock||256)&&(t=ok(n,e.target,!0),t||(t=ha(n,2))),t},"run")})});function P2(n,e){let t=new Am;for(let[,r]of n.bufferMap)t.paramBuffers.add(r.name);return sj(n.body,t,e),ij(n,t,e),aj(n.body,t),t}function sj(n,e,t){let r=[n];for(;r.length>0;){let s=r.pop();if(!(!s||typeof s!="object")){switch(s.type){case"ForNode":if(s.loopVar&&e.locals.set(s.loopVar.name,"i32"),s.kind===z.THREAD_BINDING&&s.threadTag){let o=s.extent&&s.extent.type==="IntImmNode"?s.extent.value:0,i=!s.extent||s.extent.type!=="IntImmNode",a={varName:s.loopVar.name,extent:o,isDynamic:i,extentNode:s.extent};e.threadBindings.has(s.threadTag)?e.threadBindings.get(s.threadTag).push(a):e.threadBindings.set(s.threadTag,[a])}break;case"LetStmtNode":s.variable&&e.locals.set(s.variable.name,Vi(s.variable.dtype));break;case"BufferStoreNode":case"BufferLoadNode":s.buffer&&e.usedBuffers.set(s.buffer.name,s.buffer);break;case"AllocateNode":s.buffer&&e.allocatedBuffers.add(s.buffer.name),s.scope==="shared"&&s.buffer&&e.sharedBuffers.push(s.buffer);break;case"CallExternNode":s.externName&&!n1(s.externName)&&e.externCalls.set(s.externName,{argCount:s.args?s.args.length:0,dtype:s.dtype||"f32"});break;case"BlockNode":if(s.reads)for(let o of s.reads)o.buffer&&e.usedBuffers.set(o.buffer.name,o.buffer);if(s.writes)for(let o of s.writes)o.buffer&&e.usedBuffers.set(o.buffer.name,o.buffer);if(s.iterVars)for(let o of s.iterVars)o.iterVar&&e.locals.set(o.iterVar.name,Vi(o.iterVar.dtype)),o.binding&&typeof o.binding=="object"&&o.binding.type&&r.push(o.binding);break}if(s.body&&r.push(s.body),s.value&&typeof s.value=="object"&&s.value.type&&r.push(s.value),s.stmts)for(let o of s.stmts)r.push(o);if(s.thenBody&&r.push(s.thenBody),s.elseBody&&r.push(s.elseBody),s.initBody&&r.push(s.initBody),s.condBody&&r.push(s.condBody),s.loopBody&&r.push(s.loopBody),s.condition&&typeof s.condition=="object"&&s.condition.type&&r.push(s.condition),s.a&&typeof s.a=="object"&&s.a.type&&r.push(s.a),s.b&&typeof s.b=="object"&&s.b.type&&r.push(s.b),s.expr&&typeof s.expr=="object"&&s.expr.type&&r.push(s.expr),s.args)for(let o of s.args)typeof o=="object"&&o!==null&&o.type&&r.push(o);if(s.indices)for(let o of s.indices)typeof o=="object"&&o!==null&&o.type&&r.push(o)}}}function ij(n,e,t){let r=e.memoryLayout.alignment,s=0,o=l(i=>{let a=i.shape.some(c=>typeof c!="number"||c<0),u=i.numel();if(!a&&u>=0)return u*Rn(i.dtype);let p=1;for(let c of i.shape)p*=typeof c=="number"&&c>0?c:1;return Math.max(oj,p*Rn(i.dtype))},"bufBytes");for(let[,i]of n.bufferMap)s=Math.ceil(s/r)*r,e.memoryLayout.bufferOffsets.set(i.name,s),s+=o(i);for(let[i,a]of e.usedBuffers)e.memoryLayout.bufferOffsets.has(i)||(s=Math.ceil(s/r)*r,e.memoryLayout.bufferOffsets.set(i,s),s+=o(a));e.memoryLayout.totalBytes=s}function aj(n,e){let t=new Map;for(let r of yl(n,s=>s.type==="BufferStoreNode"&&s.buffer&&!e.paramBuffers.has(s.buffer.name))){let s=r.buffer.name;t.has(s)||t.set(s,[]),t.get(s).push(r.value)}for(let[r,s]of t)if(s.every(i=>i&&i.type==="IntImmNode"&&i.value===0||i&&i.type==="FloatImmNode"&&i.value===0)&&e.zeroBuffers.add(r),s.length===1&&s[0]){let i=s[0];(i.type==="IntImmNode"||i.type==="FloatImmNode")&&e.constantBuffers.set(r,i.value)}}var oj,$2=M(()=>{"use strict";Sp();tr();Fe();ts();l(P2,"scanMetadata");l(sj,"walkTree");oj=65536;l(ij,"computeMemoryLayout");l(aj,"detectZeroBuffers")});function Ud(n,e,t){let r=typeof n.offset=="number"?n.offset:0;if(e.length===0)return new L(r);if(e.length===1)return r===0?e[0]:new P("+",e[0],new L(r));let s=[];r!==0&&s.push(new L(r));for(let o=0;o<e.length;o++){let i=e[o];if(i.type==="IntImmNode"&&i.value===0)continue;let a=n.strides[o];if(a===1)s.push(i);else if(typeof a=="number"&&a>=0)s.push(new P("*",i,new L(a)));else{let u=uj(n,o,t);s.push(new P("*",i,u))}}return s.length===0?new L(0):s.reduce((o,i)=>new P("+",o,i))}function uj(n,e,t){let r=[];for(let s=e+1;s<n.shape.length;s++){let o=n.shape[s];typeof o=="number"&&o>=0?r.push(new L(o)):r.push(lj(n,s,t))}return r.length===0?new L(1):r.reduce((s,o)=>new P("*",s,o))}function lj(n,e,t){let r=n.shape[e];if(r instanceof Pe)return ll(r,s=>new Oe(Uu(s),"index"));if(t){let s=`${n.name}:${e}`,o=t.get(s);if(o)return new Oe(o.name,"index")}return new L(1)}var F2=M(()=>{"use strict";Fe();po();R0();l(Ud,"flattenIndex");l(uj,"computeDynamicStride");l(lj,"resolveShapeParam")});function z2(n,e){let t=P2(n,e),r={shapeParamMap:n.shapeParamMap,accCounter:0,metadata:t},s=Bs(n.body,r);return new km(n.name,n.params,s,n.bufferMap,n.shapeParams,n.shapeParamMap,t)}function Bs(n,e){if(!n||typeof n!="object")return n;switch(n.type){case"ForNode":return cj(n,e);case"BlockNode":return pj(n,e);case"SeqNode":return fj(n,e);case"BufferStoreNode":return dj(n,e);case"LetStmtNode":return hj(n,e);case"AllocateNode":return mj(n,e);case"IfThenElseNode":return gj(n,e);case"WhileNode":return yj(n,e);case"EvaluateNode":return new il(Nr(n.value,e));case"SyncThreadsNode":return n;default:return n}}function cj(n,e){let t=n.accumulator!==void 0?n.accumulator:Zb(n);if(t)return _j(n,t,e);let r=Bs(n.body,e);return new he(n.loopVar,n.min,n.extent,n.kind,r,n.threadTag)}function pj(n,e){let t=[];for(let o of n.iterVars)o.iterVar&&o.binding&&t.push({name:o.iterVar.name,dtype:o.iterVar.dtype,expr:Nr(o.binding,e)});let r=Bs(n.body,e),s=n.initBody?Bs(n.initBody,e):null;return t.length>0?s?(r=new te([new gl(t,s),new gl(t,r)]),s=null):r=new gl(t,r):s&&(r=new te([s,r])),r}function fj(n,e){let t=[];for(let r of n.stmts)t.push(Bs(r,e));return new te(t)}function dj(n,e){let t=Ud(n.buffer,n.indices,e.shapeParamMap),r=Nr(n.value,e),s=n.buffer.dtype||br(n.value);return new xp(n.buffer,t,r,s)}function hj(n,e){return new et(n.variable,Nr(n.value,e),Bs(n.body,e))}function mj(n,e){return new qe(n.buffer,n.scope,Bs(n.body,e))}function gj(n,e){return new Se(Nr(n.condition,e),Bs(n.thenBody,e),n.elseBody?Bs(n.elseBody,e):null)}function yj(n,e){return new $i(n.condVar,Bs(n.condBody,e),Bs(n.loopBody,e))}function Nr(n,e){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"BufferLoadNode":{let t=Ud(n.buffer,n.indices,e.shapeParamMap),r=new Tp(n.buffer,t,n.buffer.dtype);return si(r),r}case"MathOpNode":{let t=Nr(n.a,e),r=n.b?Nr(n.b,e):null,s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),si(s),s}case"CompareNode":{let t=Nr(n.a,e),r=Nr(n.b,e),s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),si(s),s}case"CastNode":{let t=Nr(n.expr,e),r={...n,expr:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),si(r),r}case"CallExternNode":{let t=n.args.map(s=>Nr(s,e)),r={...n,args:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),si(r),r}case"IfThenElseNode":{let t=Nr(n.condition,e),r=Nr(n.thenBody,e),s=n.elseBody?Nr(n.elseBody,e):null,o={...n,condition:t,thenBody:r,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),si(o),o}default:return si(n),n}}function _j(n,e,t){let r=`_acc_${t.accCounter++}`,s=br(e.loadSide);t.metadata.locals.set(r,s);let o=new Map;for(let h of e.block.iterVars)h.iterVar&&h.binding&&o.set(h.iterVar.name,h.binding);let i=o.size>0?zr(e.valueSide,o):e.valueSide,a=Ud(e.store.buffer,e.outerIndices,t.shapeParamMap),u=new Tp(e.store.buffer,a,e.store.buffer.dtype);si(u);let p=Nr(i,t),c=Ud(e.store.buffer,e.outerIndices,t.shapeParamMap),f=new xp(e.store.buffer,c,null,e.store.buffer.dtype),d=e.block.initBody?o.size>0?Wd(e.block.initBody,o):e.block.initBody:null;return new Nm({localName:r,dtype:s,op:e.op,initLoad:u,loopVar:n.loopVar,extent:n.extent,loopKind:n.kind,body:p,flushStore:f,initBody:d?Bs(d,t):null})}function zr(n,e){if(!n||typeof n!="object"||!n.type)return n;if(n.type==="VariableNode"&&e.has(n.name))return e.get(n.name);if(n.type==="BufferLoadNode"){let t=n.indices.map(o=>zr(o,e));if(!t.some((o,i)=>o!==n.indices[i]))return n;let s={...n,indices:t};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="MathOpNode"){let t=zr(n.a,e),r=n.b?zr(n.b,e):null;if(t===n.a&&r===n.b)return n;let s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="CompareNode"){let t=zr(n.a,e),r=zr(n.b,e);if(t===n.a&&r===n.b)return n;let s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="CastNode"){let t=zr(n.expr,e);if(t===n.expr)return n;let r={...n,expr:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}if(n.type==="CallExternNode"){let t=n.args.map(o=>zr(o,e));if(!t.some((o,i)=>o!==n.args[i]))return n;let s={...n,args:t};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="IfThenElseNode"){let t=zr(n.condition,e),r=zr(n.thenBody,e),s=n.elseBody?zr(n.elseBody,e):null;if(t===n.condition&&r===n.thenBody&&s===n.elseBody)return n;let o={...n,condition:t,thenBody:r,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),o}return n}function Wd(n,e){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"BufferStoreNode":{let t=n.indices.map(o=>zr(o,e)),r=zr(n.value,e);if(t.every((o,i)=>o===n.indices[i])&&r===n.value)return n;let s={...n,indices:t,value:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}case"SeqNode":{let t=n.stmts.map(s=>Wd(s,e)),r={...n,stmts:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}case"ForNode":{let t=Wd(n.body,e);if(t===n.body)return n;let r={...n,body:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}case"IfThenElseNode":{let t=zr(n.condition,e),r=Wd(n.thenBody,e),s=n.elseBody?Wd(n.elseBody,e):null,o={...n,condition:t,thenBody:r,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),o}default:return n}}var V2=M(()=>{"use strict";Sp();$2();F2();Fe();Jv();l(z2,"lowerToLIR");l(Bs,"lowerStmt");l(cj,"lowerForNode");l(pj,"lowerBlockNode");l(fj,"lowerSeqNode");l(dj,"lowerBufferStore");l(hj,"lowerLetStmt");l(mj,"lowerAllocate");l(gj,"lowerIfThenElse");l(yj,"lowerWhile");l(Nr,"lowerExpr");l(_j,"lowerAccumulator");l(zr,"substituteVars");l(Wd,"substituteVarsStmt")});function G2(n){let e=[],t={errors:e,boundVars:new Set,path:[],bufferNames:new Set};if(!n||n.type!=="LIRFunc")return e.push(new Go("root must be LIRFunc")),e;for(let[,r]of n.bufferMap)t.bufferNames.add(r.name);for(let r of n.shapeParams)t.boundVars.add(r.name);if(n.metadata)for(let[r]of n.metadata.locals)t.boundVars.add(r);return Rs(n.body,t),e}function Rs(n,e){if(!(!n||typeof n!="object")){switch(e.path.push(n.type),n.type){case"ForNode":bj(n,e);break;case"SeqNode":for(let t of n.stmts)Rs(t,e);break;case"LIRFlatStoreNode":wj(n,e);break;case"LIRAccumulatorNode":Tj(n,e);break;case"LIRBindingsNode":xj(n,e);break;case"LetStmtNode":{Pn(n.value,e);let t=e.boundVars.has(n.variable.name);e.boundVars.add(n.variable.name),Rs(n.body,e),t||e.boundVars.delete(n.variable.name);break}case"AllocateNode":{let t=n.buffer?e.bufferNames.has(n.buffer.name):!0;n.buffer&&e.bufferNames.add(n.buffer.name),Rs(n.body,e),n.buffer&&!t&&e.bufferNames.delete(n.buffer.name);break}case"IfThenElseNode":Pn(n.condition,e),Rs(n.thenBody,e),n.elseBody&&Rs(n.elseBody,e);break;case"WhileNode":Rs(n.condBody,e),Rs(n.loopBody,e);break;case"EvaluateNode":Pn(n.value,e);break;default:break}e.path.pop()}}function bj(n,e){n.extent?Pn(n.extent,e):e.errors.push(new Go("ForNode missing extent",[...e.path]));let t=!0;n.loopVar?(t=e.boundVars.has(n.loopVar.name),e.boundVars.add(n.loopVar.name)):e.errors.push(new Go("ForNode missing loopVar",[...e.path])),Rs(n.body,e),n.loopVar&&!t&&e.boundVars.delete(n.loopVar.name)}function wj(n,e){n.buffer||e.errors.push(new Go("LIRFlatStoreNode missing buffer",[...e.path])),Pn(n.offsetExpr,e),Pn(n.value,e)}function Tj(n,e){n.localName||e.errors.push(new Go("LIRAccumulatorNode missing localName",[...e.path])),n.dtype||e.errors.push(new Go("LIRAccumulatorNode missing dtype",[...e.path]));let t=n.localName?e.boundVars.has(n.localName):!0;n.localName&&e.boundVars.add(n.localName),Pn(n.initLoad,e),Pn(n.body,e),Rs(n.flushStore,e),n.initBody&&Rs(n.initBody,e),n.localName&&!t&&e.boundVars.delete(n.localName)}function xj(n,e){let t=[];for(let r of n.bindings)Pn(r.expr,e),e.boundVars.has(r.name)||t.push(r.name),e.boundVars.add(r.name);Rs(n.body,e);for(let r of t)e.boundVars.delete(r)}function Pn(n,e){if(!(!n||typeof n!="object"||!n.type))switch(n.type){case"LIRFlatLoadNode":n.buffer||e.errors.push(new Go("LIRFlatLoadNode missing buffer",[...e.path])),Pn(n.offsetExpr,e);break;case"MathOpNode":Pn(n.a,e),n.b&&Pn(n.b,e);break;case"CompareNode":Pn(n.a,e),Pn(n.b,e);break;case"CastNode":Pn(n.expr,e);break;case"CallExternNode":if(n.args)for(let t of n.args)Pn(t,e);break;case"IfThenElseNode":Pn(n.condition,e),Pn(n.thenBody,e),n.elseBody&&Pn(n.elseBody,e);break;case"VariableNode":n.name!==void 0&&!e.boundVars.has(n.name)&&e.errors.push(new Go(`unbound variable '${n.name}'`,[...e.path]));break;case"IntImmNode":case"FloatImmNode":break;default:break}}var Go,j2=M(()=>{"use strict";Go=class{static{l(this,"LIRVerificationError")}constructor(e,t){this.message=e,this.nodePath=t||[]}toString(){let e=this.nodePath.length>0?` at ${this.nodePath.join(" > ")}`:"";return`LIR verification: ${this.message}${e}`}};l(G2,"verifyLIR");l(Rs,"verifyStmt");l(bj,"verifyForNode");l(wj,"verifyFlatStore");l(Tj,"verifyAccumulator");l(xj,"verifyBindings");l(Pn,"verifyExpr")});function q2(n,e,t={}){return new Si({target:e,...t}).compileFunction(n)}var ow,ak,Si,iw=M(()=>{"use strict";Di();qs();_E();_y();bE();sL();v2();$a();Op();fy();k2();O2();tk();R2();I2();D2();Cv();nn();RS();nv();V2();j2();nn();ow=class{static{l(this,"CompilerConfig")}constructor(e={}){this.target=e.target,this.verify=e.verify!==!1,this.verifyMode=e.verify==="full"?"full":"normal",this.errorMode=e.errorMode||"strict";let t=this.target&&typeof this.target.isWebGPU=="function"&&this.target.isWebGPU(),s=this.target&&typeof this.target.isGPU=="function"&&this.target.isGPU()&&!t;this.fusion={enabled:!0,strategy:"priority",epilogue:void 0,...e.fusion},this.scheduling={enabled:t,autotune:!1,gpuTiling:s,...e.scheduling},this.matmulBackend=e.matmulBackend||"native",this.quantization={enabled:!1,...e.quantization},this.optimization={layout:!1,rematerialization:!1,rematConfig:{},fastMath:!1,maxSimplifyIterations:8,loopPartition:!1,detectAccumulators:!1,tensorize:!1,...e.optimization},this.memory={alignment:64,inplaceReuse:!0,allocStrategy:"best-fit",poolAllocation:!1,...e.memory},this.partition={enabled:!1,targets:[],defaultTarget:null,opTargetOverrides:new Map,memoryLimits:new Map,minPartitionSize:1,costWeights:{},...e.partition},this.passContext=e.passContext||null,this.loweringRules=e.loweringRules||null,this.codegenEntries=e.codegenEntries||null;let o=e.trace||{};this.trace={level:o.level??le.SILENT,sink:o.sink??null,irSnapshot:{afterGraphPasses:!1,afterLowering:!1,afterScheduling:!1,...o.irSnapshot||{}}}}get usePartition(){return this.partition.enabled&&this.partition.targets.length>=2}},ak=class{static{l(this,"CompilationResult")}constructor(e,t,r){this.module=e,this.trace=t,this.errors=r||[]}get succeeded(){return this.errors.length===0}get failedFunctions(){let e=new Set;for(let t of this.errors)t.funcName&&e.add(t.funcName);return e}run(e,...t){return this.module.run(e,...t)}async runAsync(e,...t){return this.module.runAsync(e,...t)}isAsync(e){return this.module.isAsync(e)}getSource(e){return this.module.getKernelSource(e)}listKernels(){return this.module.listKernels()}},Si=class n{static{l(this,"Compiler")}constructor(e){if(this.config=e instanceof ow?e:new ow(e),!this.config.target)throw new Error("Compiler requires a target");this.context=new z_({loweringRules:this.config.loweringRules,codegenEntries:this.config.codegenEntries})}compile(e){let t=new yy(this.config.trace),r=this.config.errorMode==="resilient",s=[],o=new Set,i=performance.now();t.phaseStart("compile");let a={compiler:this,trace:t,errors:s,failed:o,resilient:r,original:e,working:r?wA(e):e,cudaMatmulChain:!1,split:null,primFuncs:null,lirFuncs:null,runtimeModule:null};for(let u of this._compilePhases())u.when&&!u.when(a)||u.run(a);if(t.phaseEnd("compile",performance.now()-i),!r&&s.length>0)throw new Error(s[0].toString());return new ak(a.runtimeModule,t,s)}_compilePhases(){return[{name:"verify:pre",when:l(e=>e.compiler.config.verify,"when"),run:l(e=>e.compiler._verifyGraph(e.working,"before graph passes",e.trace,e.errors,e.failed,e.resilient),"run")},{name:"calibrate",when:l(e=>{let t=e.compiler.config.quantization;return t.enabled&&t.calibrationData&&!t.calibration},"when"),run:l(e=>e.compiler._runCalibration(e.working,e.trace),"run")},{name:"graphPasses",run:l(e=>{e.cudaMatmulChain=e.compiler._runGraphPasses(e.working,e.original,e.trace,e.errors,e.failed,e.resilient)},"run")},{name:"partition",when:l(e=>e.compiler.config.usePartition,"when"),run:l(e=>e.compiler._runPartitioning(e.working,e.trace),"run")},{name:"split",run:l(e=>{let t=e.compiler.config,r=typeof t.target.isWebGPU=="function"&&t.target.isWebGPU(),s=typeof t.target.isGPU=="function"&&t.target.isGPU()&&!r,o=0,i=0;for(let p of e.working)for(let c of p.ops())c.opName==="conv"||c.opName==="quantized_conv"?o++:c.opName==="scaled_dot_product_attention"&&i++;let a=s&&o>=2,u=s&&i>0;e.split=C2(e.working,{config:t,target:t.target,cudaMatmulChain:e.cudaMatmulChain,cudaConvChain:a,cudaAttention:u,isWebGPU:r})},"run")},{name:"verify:post",when:l(e=>e.compiler.config.verify,"when"),run:l(e=>e.compiler._verifyGraph(e.working,"after graph passes",e.trace,e.errors,e.failed,e.resilient),"run")},{name:"lowering",run:l(e=>{if(e.primFuncs=e.compiler._lowerAll(e.working,e.trace,e.errors,e.failed,e.resilient),e.compiler.config.matmulBackend==="cublas")for(let t of e.primFuncs)t.cublasInfo=e.split&&e.split.cublasInfos?e.split.cublasInfos.get(t.name)||null:A1(t)},"run")},{name:"tirPasses",run:l(e=>e.compiler._runTirPasses(e),"run")},{name:"verify:tensor",when:l(e=>e.compiler.config.verify,"when"),run:l(e=>e.compiler._verifyAll(e.primFuncs,e.errors,e.failed,e.resilient),"run")},{name:"lirLowering",run:l(e=>{e.lirFuncs=e.compiler._lowerToLIR(e.primFuncs,e.trace,e.errors,e.failed,e.resilient)},"run")},{name:"codegen",run:l(e=>{e.runtimeModule=e.compiler._codegen(e.lirFuncs,e.trace,e.errors,e.failed,e.resilient),e.split&&(e.runtimeModule.executionPlan=e.split.plan)},"run")}]}compileFunction(e){let t=new Mn("single");return t.addFunction(e),this.compile(t)}calibrate(e,t="minmax"){let r=new Cc(t);for(let s of e)r.attach(s);return r}_runCalibration(e,t){let r=this.config.quantization,s=e.functionNames()[0],o=e.getFunction(s);if(!o)return;t.phaseStart("calibrate");let i=performance.now(),a=this.config.target,u=l((c,f)=>new n({target:f,verify:this.config.verify}).compile(c),"compileFn"),p=B2(o,a,r.calibrationData,{mode:r.calibrationMode||"minmax",quantizableOps:r.quantizableOps,compileFn:u});this.config.quantization={...r,calibration:p},t.phaseEnd("calibrate",performance.now()-i)}_runGraphPasses(e,t,r,s,o,i){let a=new iu,u=0;for(let m of e)for(let g of m.ops())g.opName==="dot"&&u++;let p=this.config.target,c=(p.getAttr&&p.getAttr("matmulChainThreshold"))??(p.kind==="cuda"?2:1/0),f=u>=c;for(let m of rL(this.config,this.config.target,{cudaMatmulChain:f,context:this.context}))a.addPass(m);a.setTrace(r),this.config.verifyMode==="full"&&a.setVerifyHook((m,g)=>(g?Qv(m):Gd(m)).map(_=>_.toString())),r.phaseStart("graphPasses");let d=performance.now(),h=a.run(e,{errorMode:i?"resilient":"strict",passContext:this.config.passContext});if(h.errors){for(let m of h.errors)s.push(m),r.errorEvent(m.phase,m.funcName,m.message,m.passName);if(h.failedFunctions){for(let m of h.failedFunctions)if(o.add(m),i&&t&&t!==e){let g=t.getFunction(m);g&&e.addFunction(um(g))}}}if(r.phaseEnd("graphPasses",performance.now()-d),r.shouldSnapshot("afterGraphPasses")){let m=new If;r.irDump("afterGraphPasses",m.printModule(e))}return f}_runPartitioning(e,t){let r=new iu;r.addPass(new rw(this.config.partition)),r.addPass(new sw({targets:this.config.partition.targets})),r.setTrace(t),t.phaseStart("partition");let s=performance.now();r.run(e),t.phaseEnd("partition",performance.now()-s)}_eachFunc(e,t,r,s,o,i,a){for(let u of e)if(!o.has(u.name))try{a(u)}catch(p){if(s.push(new Yn(t,u.name,p.message)),o.add(u.name),r.errorEvent(t,u.name,p.message),!i)break}}_lowerAll(e,t,r,s,o){t.phaseStart("lowering");let i=performance.now(),a=[];return this._eachFunc(e,"lowering",t,r,s,o,u=>{let p=performance.now(),c=wm(u,this.config.target,this.context);this.config.target.isGPU&&this.config.target.isGPU()&&!(this.config.target.isWebGPU&&this.config.target.isWebGPU())&&(c.convInfo=WL(u)),t.functionEvent("lowering",u.name,{durationMs:performance.now()-p}),a.push(c),t.shouldSnapshot("afterLowering")&&t.irDump("afterLowering:"+u.name,V_(c))}),t.phaseEnd("lowering",performance.now()-i),a}_runTirPasses(e){let t=new G_;for(let r of S2(this.config))t.addPass(r);if(t.setTrace(e.trace),this.config.verifyMode==="full"){let r=new Vd;t.setVerifyHook(s=>r.verify(s))}t.run(e.primFuncs,{trace:e.trace,errors:e.errors,failed:e.failed,resilient:e.resilient})}_verifyGraph(e,t,r,s,o,i){if(i){for(let u of e){if(o.has(u.name))continue;let p=Gd?Gd(u):[];if(p.length>0){let c=p.map(f=>f.toString()).join("; ");s.push(new Yn("verification",u.name,c)),o.add(u.name),r.errorEvent("verification",u.name,c)}}return}let a=Qv(e);if(a.length>0)throw new Error("Graph verification failed ("+t+"): "+a.map(u=>u.toString()).join("; "))}_verifyAll(e,t,r,s){let o=new Vd;for(let i of e){if(r.has(i.name))continue;let a=o.verify(i);if(a.length>0){let u=a.join("; ");if(s)t.push(new Yn("verification",i.name,u)),r.add(i.name);else throw new Error("TensorIR verification failed for "+i.name+": "+u)}}}_lowerToLIR(e,t,r,s,o){t.phaseStart("lirLowering");let i=performance.now(),a=[];return this._eachFunc(e,"lirLowering",t,r,s,o,u=>{let p=performance.now(),c=z2(u,this.config.target);if(u.cublasInfo&&(c.cublasInfo=u.cublasInfo),u.gpuRegisterBlocked&&(c.gpuRegisterBlocked=!0),this.config.verifyMode==="full"){let f=G2(c);if(f.length>0)throw new Error("LIR verification failed: "+f.map(d=>d.toString()).join("; "))}t.functionEvent("lirLowering",u.name,{durationMs:performance.now()-p}),a.push(c)}),t.phaseEnd("lirLowering",performance.now()-i),a}_codegen(e,t,r,s,o){t.phaseStart("codegen");let i=performance.now(),a=new Gl("compiled"),u=this.config.usePartition,p={matmulBackend:this.config.matmulBackend,context:this.context},c=new Map,f=l(h=>(c.has(h.name)||c.set(h.name,new _o(h,p)),c.get(h.name)),"getBackend"),d=u?null:new _o(this.config.target,p);return this._eachFunc(e,"codegen",t,r,s,o,h=>{let m=performance.now(),g;if(u){let _=h._partitionTarget,b=_?this.config.partition.targets.find(w=>w.name===_):this.config.target;g=f(b||this.config.target)}else g=d;let y=g.compile(h);a.addCompiledKernel(y),h.shapeParamMap&&h.shapeParamMap.size>0&&a.setShapeParamMap(h.name,h.shapeParamMap,h.bufferMap),t.codegenStats(h.name,{durationMs:performance.now()-m,sourceSize:y.source.length,targetName:y.target.name})}),t.phaseEnd("codegen",performance.now()-i),a}};l(q2,"compileGraph")});function vj(n){return typeof n=="object"&&n!==null}function kj(n){return vj(n)&&"_impl"in n}function Nj(n){return(e,...t)=>{let r=As();if(!r)throw new Error(`TRACING dispatch key active but no tracer is set for op '${n}'`);if(Sj.has(n)){let c=Lt.findOp(n);return Lt.redispatch(c,e,...t)}let s=[],o={},i=Ey(n),a=0,u=l(c=>c instanceof bt||kj(c),"isTensor"),p=l(c=>{c instanceof bt||c.isSymbolic?s.push(c):s.push(r.captureConstant(c))},"pushTensor");for(let c of t)if(Array.isArray(c)&&c.length>0&&u(c[0]))for(let f of c)p(f);else u(c)?p(c):i&&(c!=null&&a<i.length&&(o[i[a]]=c),a++);return r.recordOp(n,s,o)}}function W2(){let n=Lt.listOps();for(let e of n){let t=Lt.findOp(e);if(!t)continue;let r=t.name,s=Mr.fromUnboxed(Nj(r));t.entry.registerKernel(48,s)}}var Sj,K2=M(()=>{"use strict";An();rp();Ma();_u();yu();Ly();Sj=new Set(["scatter","repeat","tile","split","chunk","roll","flip","cumsum","sort","argsort","topk"]);l(vj,"_isObject");l(kj,"_hasImpl");l(Nj,"_tracingKernel");l(W2,"registerTracingDispatch")});function Oj(n){return n&&n.shape&&n.shape.length>=2&&Aj.has(n.dtype)}function H2(n,e,t=Oj){let r=n.graph.functions().next().value;if(!r)return n;let s=r.entryBlock,o=n.numUserInputs,i=n.capturedParams,a=[];for(let f=0;f<i.length;f++){let d=o+f,h=s.getArgument(d);h&&t(i[f],f,h)&&a.push({j:f,argIndex:d,arg:h,param:i[f]})}if(a.length===0)return n;for(let f of a){let d=e(f.param),h=f.arg.type,m=Me,g=new m("constant",[],[h],{value:d,tensor_type:h}),y=s.firstOp;y?s.insertBefore(g,y):s.pushOp(g),f.arg.replaceAllUsesWith(g.getResult(0))}let u=new Set(a.map(f=>f.argIndex));s.removeArguments(u),r.inputTypes=Object.freeze(r.inputTypes.filter((f,d)=>!u.has(d)));let p=new Set(a.map(f=>f.j)),c=i.filter((f,d)=>!p.has(d));return{...n,capturedParams:c}}var Aj,Y2=M(()=>{"use strict";yr();Aj=new Set(["f16","f32","f64"]);l(Oj,"defaultWeightPredicate");l(H2,"foldWeightParams")});function Kd(n,e,t){let r=t.get(e.id);if(r)return r;let s=n.scalarConstant(0,e.type.dtype).getResult(0);return n.broadcast(s,e.type.shape,[]).getResult(0)}var jo,Hd=M(()=>{"use strict";l(Kd,"gradOrZero");jo=class{static{l(this,"GradAccumulator")}constructor(e){this._builder=e,this._pending=new Map,this._reduced=new Map}accumulate(e,t){if(!t)return;let r=this._pending.get(e);r||(r=[],this._pending.set(e,r)),r.push(t),this._reduced.delete(e)}_treeReduce(e){let t=e;for(;t.length>1;){let r=[];for(let s=0;s<t.length;s+=2)s+1<t.length?r.push(this._builder.add(t[s],t[s+1]).getResult(0)):r.push(t[s]);t=r}return t[0]}get(e){if(this._reduced.has(e))return this._reduced.get(e);let t=this._pending.get(e);if(!t||t.length===0)return null;let r=this._treeReduce(t);return this._reduced.set(e,r),r}has(e){let t=this._pending.get(e);return!!t&&t.length>0}}});function pe(n,e){uk.set(n,e)}function Eu(n){return uk.get(n)||null}function cw(n){let e=uk.get(n);if(e)return e;if(lk.has(n))return null;throw new Error(`autodiff: op '${n}' is on the gradient path but has no VJP rule and is not a registered gradient barrier. Register one with registerVJPRule('${n}', ...) or registerGradientBarrier('${n}').`)}function ck(n,e){J2.set(n,e)}function pw(n){return J2.get(n)||null}function pk(n){lk.add(n)}function fw(n){return lk.has(n)}var uk,lk,J2,Is=M(()=>{"use strict";uk=new Map,lk=new Set;l(pe,"registerVJPRule");l(Eu,"getVJPRule");l(cw,"requireVJPRuleOrBarrier");J2=new Map;l(ck,"registerRegionVJP");l(pw,"getRegionVJP");l(pk,"registerGradientBarrier");l(fw,"isGradientBarrier")});var vi,fk=M(()=>{"use strict";vi=new Set(["scan","if"])});function Yd(n){let e=new Set(n.arguments.map(i=>i.id)),t=l(i=>{for(let a of i.ops()){for(let u of a.results)e.add(u.id);for(let u of a.regions||[])for(let p of u.blocks){for(let c of p.arguments)e.add(c.id);t(p)}}},"addLocals");t(n);let r=new Set,s=[],o=l(i=>{for(let a of i.ops()){for(let u of a.operands)e.has(u.id)||r.has(u.id)||u.definingOp&&u.definingOp.opName==="constant"||(r.add(u.id),s.push(u));for(let u of a.regions||[])for(let p of u.blocks)o(p)}},"scan");return o(n),s}function dw(n,e){let t=n.scalarConstant(0,e.type.dtype).getResult(0);return n.broadcast(t,e.type.shape,[]).getResult(0)}function Z2(n,e,t){let r=e.type.shape,s=r.map((a,u)=>u===0?t:0),o=r.map((a,u)=>u===0?t+1:a),i=n.slice(e,s,o).getResult(0);return n.reshape(i,r.slice(1)).getResult(0)}function Rj(n,e,t){let r=t.slice(1),s=e.map(o=>n.reshape(o,[1,...r]).getResult(0));return s.length===1?n.reshape(s[0],t).getResult(0):n.concat(s,0).getResult(0)}function hw(n,e,t,r,s,o,i=new Map,a=[]){let u=new Map,p=new Map;for(let _=0;_<e.arguments.length;_++)u.set(e.arguments[_].id,t[_]),p.set(e.arguments[_],t[_]);for(let[_,b]of r)u.set(_,b);for(let _ of a)r.has(_.id)&&p.set(_,r.get(_.id));let c=[],f=null;for(let _ of e.ops())_.opName==="yield"?f=_:c.push(_);let d=l(_=>{if(u.has(_.id))return u.get(_.id);let b=_.definingOp;if(b&&b.opName==="constant"){let w=i.get(_.id);return w===void 0&&(w=n._buildOp("constant",[],[_.type],new Map(b.attributes),null).getResult(0),i.set(_.id,w)),u.set(_.id,w),p.set(_,w),w}return _},"matOperand");for(let _ of c){if(vi.has(_.opName)){for(let S of _.operands)d(S),p.set(S,u.get(S.id)??S);let x=_.clone(p);n.block.pushOp(x);for(let S=0;S<_.numResults;S++)u.set(_.getResult(S).id,x.getResult(S)),p.set(_.getResult(S),x.getResult(S));continue}let b=_.operands.map(d),w=n._buildOp(_.opName,b,_.results.map(x=>x.type),new Map(_.attributes),null);for(let x=0;x<_.numResults;x++)u.set(_.getResult(x).id,w.getResult(x)),p.set(_.getResult(x),w.getResult(x))}let h=f.operands.map(_=>u.get(_.id));if(o)return{forwardYields:h};let m=new jo(n);for(let _=0;_<f.operands.length;_++)s[_]&&m.accumulate(f.operands[_].id,s[_]);for(let _=c.length-1;_>=0;_--){let b=c[_];if(b.opName==="constant")continue;let w=b.results.map(v=>m.get(v.id));if(w.every(v=>v===null))continue;if(vi.has(b.opName)){let v=pw(b.opName);v&&v(b,{accumulator:m,builder:n,materialize:d,needsGrad:Bj,scanCheckpoint:null});continue}let x=cw(b.opName);if(!x)continue;let S={builder:n,op:b,operands:b.operands.map(d),results:b.results.map(v=>u.get(v.id)),gradOutputs:w,attrs:b.attributes,full:l((v,A)=>n.broadcast(n.scalarConstant(v,A.dtype).getResult(0),A.shape,[]).getResult(0),"full")},T=x(S);if(T)for(let v=0;v<b.numOperands;v++)T[v]&&m.accumulate(b.getOperand(v).id,dk(n,T[v],b.getOperand(v).type.shape))}let g=e.arguments.map(_=>m.get(_.id)),y=new Map;for(let _ of r.keys())y.set(_,m.get(_));return{forwardYields:h,gradArgs:g,gradFree:y}}function Mj(n,e,t,r,s){let o=n.regions[0].blocks[0],i=n.regions[1].blocks[0],a=r(n.getOperand(0)),u=[];for(let y=0;y<n.numResults;y++)u.push(e.get(n.getResult(y).id));let p=Yd(o),c=Yd(i),f=new Map(p.map(y=>[y.id,r(y)])),d=new Map(c.map(y=>[y.id,r(y)])),{gradFree:h}=hw(t,o,[],f,u,!1,new Map,p),{gradFree:m}=hw(t,i,[],d,u,!1,new Map,c),g=new Map;for(let y of p)g.set(y.id,y);for(let y of c)g.set(y.id,y);for(let[y,_]of g){if(!s.has(y))continue;let b=h.get(y),w=m.get(y);if(!b&&!w)continue;let x=dw(t,_),S=t.broadcast(a,_.type.shape,[]).getResult(0);e.accumulate(y,t.select(S,b??x,w??x).getResult(0))}}function Ij(n,e){if(!n||e<=1)return null;if(n==="sqrt"||n===!0)return Math.max(1,Math.ceil(Math.sqrt(e)));if(typeof n=="number"&&n>=1){let t=Math.floor(n);return t>=e?null:t}return null}function Ej(n,e,t,r,s,o=null){let i=n.regions[0].blocks[0],a=n.getAttr("num_carry"),u=n.getAttr("num_xs"),p=n.numResults-a,c=[];for(let k=0;k<u;k++)c.push(n.getOperand(k));let f=[];for(let k=0;k<a;k++)f.push(n.getOperand(u+k));let d=c[0].type.shape[0],h=Yd(i),m=c.map(r),g=f.map(r),y=new Map(h.map(k=>[k.id,r(k)])),_=new Map,b=l(k=>m.map(O=>Z2(t,O,k)),"sliceX"),w=l((k,O)=>hw(t,i,[...k,...O],y,null,!0,_,h).forwardYields.slice(0,a),"stepForward"),x=[];for(let k=0;k<p;k++)x.push(e.get(n.getResult(a+k).id));let S=[];for(let k=0;k<a;k++){let O=e.get(n.getResult(k).id);S.push(O??dw(t,g[k]))}let T=new Map,v=m.map(()=>new Array(d)),A=l((k,O,R)=>{let B=[...O,...R],E=x.map(G=>G===null?null:Z2(t,G,k)),F=[...S,...E],{gradArgs:C,gradFree:H}=hw(t,i,B,y,F,!1,_,h);for(let G=0;G<u;G++)v[G][k]=C[G]??dw(t,O[G]);S=[];for(let G=0;G<a;G++)S.push(C[u+G]??dw(t,R[G]));for(let[G,q]of H){if(!q)continue;let D=T.get(G);T.set(G,D?t.add(D,q).getResult(0):q)}},"backwardStep"),N=Ij(o,d);if(N){let k=Math.ceil(d/N),O=new Array(k),R=g;O[0]=R;for(let B=0;B<d;B++){R=w(b(B),R);let E=(B+1)/N;Number.isInteger(E)&&E<k&&(O[E]=R)}for(let B=k-1;B>=0;B--){let E=B*N,F=Math.min(E+N,d),C=[],H=[O[B]],G=O[B];for(let q=E;q<F;q++){let D=b(q);C.push(D),G=w(D,G),H.push(G)}for(let q=F-1;q>=E;q--)A(q,C[q-E],H[q-E])}}else{let k=g,O=[k],R=[];for(let B=0;B<d;B++){let E=b(B);R.push(E),k=w(E,k),O.push(k)}for(let B=d-1;B>=0;B--)A(B,R[B],O[B])}for(let k=0;k<u;k++)s.has(c[k].id)&&e.accumulate(c[k].id,Rj(t,v[k],m[k].type.shape));for(let k=0;k<a;k++)s.has(f[k].id)&&e.accumulate(f[k].id,S[k]);for(let[k,O]of T)O&&s.has(k)&&e.accumulate(k,O)}var Bj,Q2=M(()=>{"use strict";Hd();Is();Jd();fk();Bj={has:l(()=>!0,"has")};ck("scan",(n,e)=>Ej(n,e.accumulator,e.builder,e.materialize,e.needsGrad,e.scanCheckpoint));ck("if",(n,e)=>Mj(n,e.accumulator,e.builder,e.materialize,e.needsGrad));l(Yd,"regionFreeVars");l(dw,"zeroLike");l(Z2,"sliceStep");l(Rj,"stackSteps");l(hw,"diffBodyStep");l(Mj,"buildCondBackward");l(Ij,"resolveSegmentLength");l(Ej,"buildScanBackward")});function Zd(n,{accumulator:e,builder:t,needsGrad:r,resolveValue:s,handleRegionOp:o=null}){for(let i=n.length-1;i>=0;i--){let a=n[i];if(a.opName==="return"||a.opName==="constant"||!a.results.some(g=>r.has(g.id)))continue;let p=[];for(let g=0;g<a.numResults;g++)p.push(e.get(a.getResult(g).id));if(p.every(g=>g===null)||o&&o(a))continue;let c=cw(a.opName);if(!c)continue;let f=new Array(a.numOperands);for(let g=0;g<a.numOperands;g++)f[g]=s(a.getOperand(g));let d=new Array(a.numResults);for(let g=0;g<a.numResults;g++)d[g]=s(a.getResult(g));let h=l((g,y)=>t.broadcast(t.scalarConstant(g,y.dtype).getResult(0),y.shape,[]).getResult(0),"full"),m=c({builder:t,op:a,operands:f,results:d,gradOutputs:p,attrs:a.attributes,full:h});if(m)for(let g=0;g<a.numOperands;g++){if(g>=m.length||!m[g])continue;let y=a.getOperand(g);r.has(y.id)&&e.accumulate(y.id,dk(t,m[g],y.type.shape))}}}function X2(n){let e=[];for(let t of n.regions)t.blocks[0]&&e.push(...Yd(t.blocks[0]));return e}function dk(n,e,t){let r=e.type.shape;if(r.length===t.length&&r.every((u,p)=>u===t[p]))return e;let s=r.length-t.length,o=[];for(let u=0;u<s;u++)o.push(u);for(let u=0;u<t.length;u++)t[u]===1&&r[s+u]!==1&&o.push(s+u);let i=e;if(o.length>0){let u=n.scalarConstant(0,e.type.dtype).getResult(0);i=n.reduce(e,u,o,"sum").getResult(0)}let a=i.type.shape;return a.length===t.length&&a.every((u,p)=>u===t[p])||(i=n.reshape(i,t).getResult(0)),i}var Lj,Qd,Jd=M(()=>{"use strict";qs();_s();ro();Hd();Is();Q2();fk();Lj=new Set(["neg","abs","sign","floor","ceil"]);l(Zd,"backpropOps");l(X2,"regionControlFlowFreeVars");l(dk,"reduceGradToOperandShape");Qd=class{static{l(this,"BackwardGraphBuilder")}constructor(e={}){this._rematPolicy=e.rematPolicy||null,this._checkpointPolicy=e.checkpointPolicy||null,this._scanCheckpoint=e.scanCheckpoint||null}build(e){if(this._checkpointPolicy)return this._buildCheckpointed(e);let r=ft.compute(e).topologicalOrder,s=e.getReturnOp();if(!s)throw new Error("Forward function has no return op");let o=s.operands,i=e.args,a=this._computeGradReachability(e,r),{savedValues:u,savedValueIndices:p}=this._identifySavedValues(r,a,i),c=o.map(T=>T.type),f=u.map(T=>T.type),d=[...c,...f],h=[];for(let T=0;T<i.length;T++)a.has(i[T].id)&&h.push(i[T].type);let m=new yn(`backward_${e.name}`,d,h),g=new $n(m),y=m.args,_=y.slice(0,c.length),b=y.slice(c.length),w=new Map;for(let T=0;T<u.length;T++)w.set(u[T].id,b[T]);for(let T=0;T<i.length;T++)p.has(i[T].id)&&w.set(i[T].id,b[p.get(i[T].id)]);let x=new jo(g);for(let T=0;T<o.length;T++){let v=o[T];x.accumulate(v.id,_[T])}Zd(r,{accumulator:x,builder:g,needsGrad:a,resolveValue:l(T=>this._materialize(T,w,g),"resolveValue"),handleRegionOp:l(T=>{let v=pw(T.opName);return v?(v(T,{accumulator:x,builder:g,materialize:l(A=>this._materialize(A,w,g),"materialize"),needsGrad:a,scanCheckpoint:this._scanCheckpoint}),!0):!1},"handleRegionOp")});let S=[];for(let T=0;T<i.length;T++)a.has(i[T].id)&&S.push(Kd(g,i[T],x));return g.returnOp(S),{backwardFunc:m,savedValues:u,gradInputIndices:this._getGradInputIndices(i,a)}}_materialize(e,t,r){if(t.has(e.id))return t.get(e.id);if(!e.definingOp)return e;let s=new Set([e.id]),o=[{val:e,i:0}];for(;o.length>0;){let i=o[o.length-1],a=i.val,u=a.definingOp;if(t.has(a.id)||!u){s.delete(a.id),o.pop();continue}if(i.i<u.numOperands){let d=u.getOperand(i.i);i.i++,d.definingOp&&!t.has(d.id)&&!s.has(d.id)&&(s.add(d.id),o.push({val:d,i:0}));continue}let p=new Array(u.numOperands);for(let d=0;d<u.numOperands;d++){let h=u.getOperand(d);p[d]=t.has(h.id)?t.get(h.id):h}let c=u.results.map(d=>d.type),f=r._buildOp(u.opName,p,c,new Map(u.attributes),null);for(let d=0;d<u.numResults;d++)t.set(u.getResult(d).id,f.getResult(d));s.delete(a.id),o.pop()}return t.has(e.id)?t.get(e.id):e}_computeGradReachability(e,t){let r=new Set,s=e.getReturnOp();for(let o of s.operands)r.add(o.id);for(let o=t.length-1;o>=0;o--){let i=t[o];if(!(i.opName==="return"||!i.results.some(u=>r.has(u.id)))){if(vi.has(i.opName)){for(let u=0;u<i.numOperands;u++)r.add(i.getOperand(u).id);for(let u of X2(i))r.add(u.id);continue}if(Eu(i.opName)&&!fw(i.opName))for(let u=0;u<i.numOperands;u++)r.add(i.getOperand(u).id)}}return r}_identifySavedValues(e,t,r){let s=[],o=new Map,i=new Set(r.map(f=>f.id));for(let f of e)if(!(f.opName==="return"||f.opName==="constant"||!Eu(f.opName)||!f.results.some(m=>t.has(m.id)))){for(let m=0;m<f.numOperands;m++){let g=f.getOperand(m);i.has(g.id)&&!o.has(g.id)&&(o.set(g.id,s.length),s.push(g))}for(let m=0;m<f.numResults;m++){let g=f.getResult(m);t.has(g.id)&&!o.has(g.id)&&this._shouldSaveResult(f)&&(o.set(g.id,s.length),s.push(g))}}let a=new Set(o.keys()),u=new Map(r.map(f=>[f.id,f])),p=new Set,c=l(f=>{if(a.has(f.id)||p.has(f.id))return;p.add(f.id);let d=f.definingOp;if(!d){i.has(f.id)&&!o.has(f.id)&&(o.set(f.id,s.length),s.push(u.get(f.id)));return}for(let h=0;h<d.numOperands;h++)c(d.getOperand(h))},"collect");for(let f of e)if(!(f.opName==="return"||f.opName==="constant")&&f.results.some(d=>t.has(d.id))){if(vi.has(f.opName)){for(let d of f.operands)c(d);for(let d of X2(f))c(d);continue}if(Eu(f.opName)){for(let d=0;d<f.numOperands;d++)c(f.getOperand(d));for(let d=0;d<f.numResults;d++)c(f.getResult(d))}}return{savedValues:s,savedValueIndices:o}}_shouldSaveResult(e){return this._rematPolicy?!this._rematPolicy.shouldRematerialize(e):!Lj.has(e.opName)}_getGradInputIndices(e,t){let r=[];for(let s=0;s<e.length;s++)t.has(e[s].id)&&r.push(s);return r}_buildCheckpointed(e){let r=ft.compute(e).topologicalOrder,s=e.getReturnOp();if(!s)throw new Error("Forward function has no return op");let o=s.operands,i=e.args,a=this._computeGradReachability(e,r),u=this._checkpointPolicy.segment(r,e);for(let O of u)for(let R of O.ops)if(vi.has(R.opName))throw new Error(`Checkpointed backward does not support region control-flow op '${R.opName}'; build the backward without a checkpointPolicy, which differentiates scan/if via buildScanBackward/buildCondBackward.`);let p=new Set,c=[],f=new Map,d=new Map;for(let O of r)for(let R=0;R<O.numResults;R++){let B=O.getResult(R);d.set(B.id,B)}let h=new Set(i.map(O=>O.id));for(let O of i)a.has(O.id)&&!p.has(O.id)&&(p.add(O.id),f.set(O.id,c.length),c.push(O));for(let O of u){for(let R of O.boundaryInputs)if(!h.has(R)&&!p.has(R)){p.add(R);let B=d.get(R)||null;B&&(f.set(R,c.length),c.push(B))}for(let R of O.boundaryOutputs)if(!p.has(R)){p.add(R);let B=d.get(R)||null;B&&(f.set(R,c.length),c.push(B))}}for(let O of o)p.has(O.id)||(p.add(O.id),f.set(O.id,c.length),c.push(O));let m=o.map(O=>O.type),g=c.map(O=>O.type),y=[...m,...g],_=[];for(let O=0;O<i.length;O++)a.has(i[O].id)&&_.push(i[O].type);let b=new yn(`backward_${e.name}`,y,_),w=new $n(b),x=b.args,S=x.slice(0,m.length),T=x.slice(m.length),v=new Map;for(let O=0;O<c.length;O++)v.set(c[O].id,T[O]);let A=new jo(w);for(let O=0;O<o.length;O++)A.accumulate(o[O].id,S[O]);let N=new Map;for(let O of r)if(O.opName==="constant"){let R=O.getResult(0).type,B=w._buildOp("constant",[],[R],new Map(O.attributes),null);N.set(O.getResult(0).id,B.getResult(0))}for(let O=u.length-1;O>=0;O--){let R=u[O],B=new Map;for(let E of R.ops){let F=new Array(E.numOperands);for(let G=0;G<E.numOperands;G++){let q=E.getOperand(G),D=B.get(q.id)||v.get(q.id)||N.get(q.id);F[G]=D||q}let C=E.results.map(G=>G.type),H=w._buildOp(E.opName,F,C,new Map(E.attributes),null);for(let G=0;G<E.numResults;G++)B.set(E.getResult(G).id,H.getResult(G))}Zd(R.ops,{accumulator:A,builder:w,needsGrad:a,resolveValue:l(E=>B.get(E.id)||v.get(E.id)||N.get(E.id)||E,"resolveValue")})}let k=[];for(let O=0;O<i.length;O++)a.has(i[O].id)&&k.push(Kd(w,i[O],A));return w.returnOp(k),{backwardFunc:b,savedValues:c,gradInputIndices:this._getGradInputIndices(i,a)}}}});var Lu,mw=M(()=>{"use strict";Lu=class{static{l(this,"RematPolicy")}constructor(e={}){this._maxRematDepth=e.maxRematDepth||1,this._sizeThreshold=e.sizeThreshold||1024*1024,this._alwaysRemat=new Set(e.alwaysRemat||["neg","abs","sign","floor","ceil","exp","log","sqrt","rsqrt","sin","cos","tanh"]),this._neverRemat=new Set(e.neverRemat||["matmul","dot","conv","reduce","custom_call","pool2d"])}shouldRematerialize(e){if(this._alwaysRemat.has(e.opName))return!0;if(this._neverRemat.has(e.opName))return!1;let t=e.numResults>0?e.getResult(0).type:null;return!t||!t.shape||t.numel()>this._sizeThreshold?!1:this._isElementwise(e)}_isElementwise(e){if(e.numOperands===0||e.numResults===0)return!1;let t=e.getResult(0).type.shape;for(let r=0;r<e.numOperands;r++){let s=e.getOperand(r).type;if(!s||!s.shape||s.shape.length!==t.length)return!1;for(let o=0;o<t.length;o++)if(s.shape[o]!==t[o])return!1}return!0}}});var Xd,hk=M(()=>{"use strict";qs();_s();ro();Hd();Is();mw();Jd();Xd=class{static{l(this,"JointGraphBuilder")}constructor(e={}){this._rematPolicy=e.rematPolicy||new Lu(e.remat||{}),this._checkpointPolicy=e.checkpointPolicy||null}build(e){if(this._checkpointPolicy)return this._buildCheckpointed(e);let t=this._buildScaffold(e);return Zd(t.topoOrder,{accumulator:t.accumulator,builder:t.builder,needsGrad:t.needsGrad,resolveValue:l(r=>t.valueMap.get(r.id)||r,"resolveValue")}),this._finish(t)}_buildCheckpointed(e){let t=this._buildScaffold(e),r=this._checkpointPolicy.segment(t.topoOrder,e);for(let s=r.length-1;s>=0;s--){let o=r[s],i=new Map;for(let a of o.ops){let u=new Array(a.numOperands);for(let f=0;f<a.numOperands;f++){let d=a.getOperand(f);u[f]=i.get(d.id)||t.valueMap.get(d.id)||d}let p=a.results.map(f=>f.type),c=t.builder._buildOp(a.opName,u,p,new Map(a.attributes),null);for(let f=0;f<a.numResults;f++)i.set(a.getResult(f).id,c.getResult(f))}Zd(o.ops,{accumulator:t.accumulator,builder:t.builder,needsGrad:t.needsGrad,resolveValue:l(a=>i.get(a.id)||t.valueMap.get(a.id)||a,"resolveValue")})}return this._finish(t)}_buildScaffold(e){let r=ft.compute(e).topologicalOrder;this._assertNoRegionControlFlow(r);let s=e.getReturnOp();if(!s)throw new Error("Forward function has no return op");let o=s.operands,i=e.args,a=o.map(w=>w.type),u=[...e.inputTypes,...a],p=[...e.outputTypes,...e.inputTypes],c=new yn(`joint_${e.name}`,u,p),f=new $n(c),d=c.args,h=d.slice(0,e.inputTypes.length),m=d.slice(e.inputTypes.length),g=new Map;for(let w=0;w<i.length;w++)g.set(i[w].id,h[w]);for(let w of r){if(w.opName==="return")continue;let x=new Array(w.numOperands);for(let v=0;v<w.numOperands;v++){let A=w.getOperand(v);x[v]=g.get(A.id)||A}let S=w.results.map(v=>v.type),T=f._buildOp(w.opName,x,S,new Map(w.attributes),null);for(let v=0;v<w.numResults;v++)g.set(w.getResult(v).id,T.getResult(v))}let y=o.map(w=>g.get(w.id)),_=this._computeGradReachability(e,r),b=new jo(f);for(let w=0;w<o.length;w++)b.accumulate(o[w].id,m[w]);return{topoOrder:r,forwardInputs:i,forwardOutputs:o,fwdOutputValues:y,valueMap:g,builder:f,needsGrad:_,accumulator:b,jointFunc:c}}_finish(e){let t=[];for(let r=0;r<e.forwardInputs.length;r++)t.push(Kd(e.builder,e.forwardInputs[r],e.accumulator));return e.builder.returnOp([...e.fwdOutputValues,...t]),{jointFunc:e.jointFunc,numForwardOutputs:e.forwardOutputs.length,numGradInputs:t.length}}_computeGradReachability(e,t){let r=new Set,s=e.getReturnOp();for(let o of s.operands)r.add(o.id);for(let o=t.length-1;o>=0;o--){let i=t[o];if(!(i.opName==="return"||!i.results.some(u=>r.has(u.id)))&&Eu(i.opName)&&!fw(i.opName))for(let u=0;u<i.numOperands;u++)r.add(i.getOperand(u).id)}return r}_assertNoRegionControlFlow(e){for(let t of e)if(vi.has(t.opName))throw new Error(`JointGraphBuilder does not support region control-flow op '${t.opName}'; use BackwardGraphBuilder (separate mode) without a checkpointPolicy, which differentiates scan/if.`)}}});var eC=M(()=>{"use strict"});function tC(n,e){let t=n.gradOutputs[0],[r,s]=n.operands,o=n.full(0,r.type),i=n.builder.compare(r,s,e).getResult(0),a=n.builder.select(i,t,o).getResult(0),u=n.builder.select(i,o,t).getResult(0);return[a,u]}function nC(n){let e=n.gradOutputs[0],[t]=n.operands,r=n.full(0,e.type),s=n.builder.where(t,e,r).getResult(0),o=n.builder.where(t,r,e).getResult(0);return[null,s,o]}var rC=M(()=>{"use strict";Is();pe("add",n=>{let e=n.gradOutputs[0];return[e,e]});pe("sub",n=>{let e=n.gradOutputs[0],t=n.builder.neg(e).getResult(0);return[e,t]});l(tC,"_minMaxVJP");pe("maximum",n=>tC(n,"ge"));pe("minimum",n=>tC(n,"le"));pe("clamp",n=>{let e=n.gradOutputs[0],[t,r,s]=n.operands,o=n.full(0,r.type),i=n.builder.compare(r,t,"ge").getResult(0),a=n.builder.where(i,e,o).getResult(0),u=n.builder.compare(r,s,"le").getResult(0);return[null,n.builder.where(u,a,o).getResult(0),null]});l(nC,"_whereVJP");pe("where",nC);pe("select",nC);pe("mul",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder.mul(e,r).getResult(0),o=n.builder.mul(e,t).getResult(0);return[s,o]});pe("div",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder.div(e,r).getResult(0),o=n.builder.mul(r,r).getResult(0),i=n.builder.neg(t).getResult(0),a=n.builder.mul(e,i).getResult(0),u=n.builder.div(a,o).getResult(0);return[s,u]});pe("neg",n=>{let e=n.gradOutputs[0];return[n.builder.neg(e).getResult(0)]});pe("pow",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.full(1,t.type),o=n.builder.sub(r,s).getResult(0),i=n.builder.pow(t,o).getResult(0),a=n.builder.mul(r,i).getResult(0),u=n.builder.mul(e,a).getResult(0),p=n.builder.pow(t,r).getResult(0),c=n.builder.log(t).getResult(0),f=n.builder.mul(p,c).getResult(0),d=n.builder.mul(e,f).getResult(0);return[u,d]})});function sC(n,e){let t=n.full(2/Math.sqrt(Math.PI),e.type),r=n.builder.mul(e,e).getResult(0),s=n.builder.neg(r).getResult(0),o=n.builder.exp(s).getResult(0);return n.builder.mul(t,o).getResult(0)}function oC(n,e){let t=n.builder,r=n.full(1,e.type),s=t.add(e,n.full(ii,e.type)).getResult(0),o=t.div(r,s).getResult(0),i=t.log(s).getResult(0),a=t.sub(i,t.mul(n.full(.5,e.type),o).getResult(0)).getResult(0),u=t.mul(o,o).getResult(0),p=u;for(let c of Ga)a=t.add(a,t.mul(n.full(c,e.type),p).getResult(0)).getResult(0),p=t.mul(p,u).getResult(0);for(let c=0;c<ii;c++){let f=t.add(e,n.full(c,e.type)).getResult(0);a=t.sub(a,t.div(r,f).getResult(0)).getResult(0)}return a}var iC=M(()=>{"use strict";Is();Yi();pe("exp",n=>{let e=n.gradOutputs[0],t=n.results[0];return[n.builder.mul(e,t).getResult(0)]});pe("log",n=>{let e=n.gradOutputs[0],[t]=n.operands;return[n.builder.div(e,t).getResult(0)]});pe("sqrt",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.full(2,t.type),s=n.builder.mul(r,t).getResult(0);return[n.builder.div(e,s).getResult(0)]});pe("tanh",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.mul(t,t).getResult(0),s=n.builder.sub(n.full(1,t.type),r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("sigmoid",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.sub(n.full(1,t.type),t).getResult(0),s=n.builder.mul(t,r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("relu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(0,t.type),s=n.builder.compare(t,r,"gt").getResult(0);return[n.builder.select(s,e,r).getResult(0)]});pe("gelu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(1.702,t.type),s=n.full(1,t.type),o=n.builder.mul(r,t).getResult(0),i=n.builder.sigmoid(o).getResult(0),a=n.builder.sub(s,i).getResult(0),u=n.builder.mul(o,a).getResult(0),p=n.builder.add(s,u).getResult(0),c=n.builder.mul(i,p).getResult(0);return[n.builder.mul(e,c).getResult(0)]});pe("silu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.sigmoid(t).getResult(0),s=n.builder.sub(n.full(1,t.type),r).getResult(0),o=n.builder.mul(t,s).getResult(0),i=n.builder.add(r,n.builder.mul(r,o).getResult(0)).getResult(0);return[n.builder.mul(e,i).getResult(0)]});pe("sin",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.cos(t).getResult(0);return[n.builder.mul(e,r).getResult(0)]});pe("cos",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.sin(t).getResult(0),s=n.builder.neg(r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("abs",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.sign(t).getResult(0);return[n.builder.mul(e,r).getResult(0)]});l(sC,"_erfDerivIR");l(oC,"_digammaIR");pe("erf",n=>{let e=n.gradOutputs[0],[t]=n.operands;return[n.builder.mul(e,sC(n,t)).getResult(0)]});pe("erfc",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.neg(sC(n,t)).getResult(0);return[n.builder.mul(e,r).getResult(0)]});pe("lgamma",n=>{let e=n.gradOutputs[0],[t]=n.operands;return[n.builder.mul(e,oC(n,t)).getResult(0)]});pe("gamma",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.results[0],s=n.builder.mul(r,oC(n,t)).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("log2",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(Math.LN2,t.type),s=n.builder.mul(t,r).getResult(0);return[n.builder.div(e,s).getResult(0)]});pe("log10",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(Math.LN10,t.type),s=n.builder.mul(t,r).getResult(0);return[n.builder.div(e,s).getResult(0)]});pe("exp2",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.full(Math.LN2,t.type),s=n.builder.mul(t,r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("square",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(2,t.type),s=n.builder.mul(r,t).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("reciprocal",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.mul(t,t).getResult(0),s=n.builder.neg(r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("rsqrt",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.mul(n.builder.mul(t,t).getResult(0),t).getResult(0),s=n.full(-.5,t.type),o=n.builder.mul(s,r).getResult(0);return[n.builder.mul(e,o).getResult(0)]})});var aC=M(()=>{"use strict";Is();pe("dot",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.op.getAttr("lhs_contracting"),o=n.op.getAttr("rhs_contracting"),i=n.op.getAttr("lhs_batch")||[],a=n.op.getAttr("rhs_batch")||[],u=t.type.rank,p=r.type.rank,c=e.type.rank,f=[];for(let _=0;_<u;_++)!s.includes(_)&&!i.includes(_)&&f.push(_);let d=[];for(let _=0;_<p;_++)!o.includes(_)&&!a.includes(_)&&d.push(_);let h=[];for(let _=i.length;_<i.length+d.length;_++)h.push(_+f.length);let m=[];for(let _=i.length;_<i.length+f.length;_++)m.push(_);let g=n.builder.dot(e,r,h,d,Array.from({length:i.length},(_,b)=>b),a).getResult(0),y=n.builder.dot(t,e,f,m,i,Array.from({length:i.length},(_,b)=>b)).getResult(0);return[g,y]});pe("matmul",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=l(p=>{let c=Array.from({length:p},(f,d)=>d);return c[p-2]=p-1,c[p-1]=p-2,c},"swapLastTwo"),o=n.builder.transpose(r,s(r.type.rank)).getResult(0),i=n.builder.matmul(e,o).getResult(0),a=n.builder.transpose(t,s(t.type.rank)).getResult(0),u=n.builder.matmul(a,e).getResult(0);return[i,u]});pe("conv",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder,o=n.op.getAttr("strides"),i=n.op.getAttr("padding"),a=n.op.getAttr("dilation")||o.map(()=>1),u=n.op.getAttr("groups")||1,p=n.op.getAttr("input_layout"),c=n.op.getAttr("kernel_layout");if(!(o.length===2&&o.every(A=>A===1)&&a.every(A=>A===1)&&u===1&&p==="NCHW"&&c==="OIHW"))throw new Error("conv VJP supports only 2D stride-1 dilation-1 groups-1 NCHW/OIHW conv");let d=r.type.shape,h=d[2],m=d[3],[g,y]=i,_=s.reverse(s.transpose(r,[1,0,2,3]).getResult(0),[2,3]).getResult(0),b=[[h-1-g[0],h-1-g[1]],[m-1-y[0],m-1-y[1]]],w=s.conv(e,_,[1,1],b).getResult(0),x=s.transpose(t,[1,0,2,3]).getResult(0),S=s.transpose(e,[1,0,2,3]).getResult(0),T=s.conv(x,S,[1,1],[g,y]).getResult(0),v=s.transpose(T,[1,0,2,3]).getResult(0);return[w,v]})});var uC=M(()=>{"use strict";Is();pe("reduce",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("reduce_type"),s=n.op.getAttr("dimensions"),o=t.type.shape,i=t.type.dtype,a=new Set(s),u=o.map((f,d)=>a.has(d)?1:f),p=o.map((f,d)=>d),c=n.builder.reshape(e,u).getResult(0);if(r==="sum")return[n.builder.broadcast(c,o,p).getResult(0),null];if(r==="mean"){let f=1;for(let y=0;y<o.length;y++)a.has(y)&&(f*=o[y]);let d=n.builder.broadcast(c,o,p).getResult(0),h=n.builder.scalarConstant(f,i).getResult(0),m=n.builder.broadcast(h,o,[]).getResult(0);return[n.builder.div(d,m).getResult(0),null]}if(r==="max"||r==="min"){let f=n.builder.reshape(n.results[0],u).getResult(0),d=n.builder.broadcast(f,o,p).getResult(0),h=n.builder.compare(t,d,"eq").getResult(0),m=n.builder.convert(h,i).getResult(0),g=n.builder.broadcast(c,o,p).getResult(0);return[n.builder.mul(g,m).getResult(0),null]}throw r==="prod"?new Error("reduce VJP for reduce_type='prod' is not implemented; provide a gradient rule or avoid differentiating reduce_prod"):new Error(`reduce VJP: unsupported reduce_type '${r}' on the gradient path (would silently drop the gradient)`)})});var lC=M(()=>{"use strict";Is();pe("reshape",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=t.type.shape;return[n.builder.reshape(e,r).getResult(0)]});pe("reverse",n=>[n.builder.reverse(n.gradOutputs[0],n.op.getAttr("dimensions")).getResult(0)]);pe("transpose",n=>{let e=n.gradOutputs[0],t=n.op.getAttr("permutation"),r=new Array(t.length);for(let s=0;s<t.length;s++)r[t[s]]=s;return[n.builder.transpose(e,r).getResult(0)]});pe("broadcast_in_dim",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("broadcast_dimensions"),s=t.type.shape,o=n.results[0].type.shape,i=t.type.dtype,a=[];for(let f=0;f<o.length;f++)if(!r.includes(f))a.push(f);else{let d=r.indexOf(f);s[d]===1&&o[f]!==1&&a.push(f)}if(a.length===0)return[e];let u=n.builder.scalarConstant(0,i).getResult(0),p=n.builder.reduce(e,u,a,"sum").getResult(0),c=p.type.shape;return c.length!==s.length||!c.every((f,d)=>f===s[d])?[n.builder.reshape(p,s).getResult(0)]:[p]});pe("slice",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("starts"),s=t.type.shape,o=e.type.shape,i=n.op.getAttr("strides")||s.map(()=>1),a=t.type.dtype,u=[...r],p=new Array(s.length),c=new Array(s.length);for(let d=0;d<s.length;d++)c[d]=i[d]-1,p[d]=s[d]-r[d]-(o[d]-1)*i[d]-1;let f=n.builder.scalarConstant(0,a).getResult(0);return[n.builder.pad(e,f,u,p,c).getResult(0)]});pe("concat",n=>{let e=n.gradOutputs[0],t=n.op.getAttr("dimension"),r=n.operands,s=[],o=0;for(let i=0;i<r.length;i++){let a=r[i].type.shape,u=new Array(a.length).fill(0),p=[...e.type.shape];u[t]=o,p[t]=o+a[t],s.push(n.builder.slice(e,u,p).getResult(0)),o+=a[t]}return s});pe("gather",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder.scalarConstant(0,t.type.dtype).getResult(0),o=n.builder.broadcast(s,t.type.shape,[]).getResult(0);return[n.builder.scatterAdd(o,r,e,{updateWindowDims:n.op.getAttr("offset_dims"),insertedWindowDims:n.op.getAttr("collapsed_slice_dims"),scatterDimsToOperandDims:n.op.getAttr("start_index_map"),indexVectorDim:n.op.getAttr("index_vector_dim")}).getResult(0),null]});pe("scatter",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.op.getAttr("inserted_window_dims"),o=t.type.shape.map((a,u)=>s.includes(u)?1:a),i=n.builder.gather(e,r,{offsetDims:n.op.getAttr("update_window_dims"),collapsedSliceDims:s,startIndexMap:n.op.getAttr("scatter_dims_to_operand_dims"),indexVectorDim:n.op.getAttr("index_vector_dim"),sliceSizes:o}).getResult(0);return[e,null,i]});pe("pad",n=>{let e=n.gradOutputs[0],t=n.op.getAttr("low"),r=n.op.getAttr("high"),[s]=n.operands,o=s.type.shape,i=[...t],a=new Array(o.length);for(let u=0;u<o.length;u++)a[u]=t[u]+o[u];return[n.builder.slice(e,i,a).getResult(0),null]})});var cC=M(()=>{"use strict";Is();_s();pe("softmax",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.op.getAttr("axis"),s=t.type.dtype,o=t.type.shape,i=n.builder.mul(e,t).getResult(0),a=n.builder.scalarConstant(0,s).getResult(0),u=n.builder.reduce(i,a,[r],"sum").getResult(0),p=Xo(o.length,r),c=n.builder.broadcast(u,o,p).getResult(0),f=n.builder.sub(e,c).getResult(0);return[n.builder.mul(t,f).getResult(0)]});pe("log_softmax",n=>{let e=n.gradOutputs[0],t=n.results[0],[r]=n.operands,s=n.op.getAttr("axis"),o=t.type.dtype,i=t.type.shape,a=n.builder.exp(t).getResult(0),u=n.builder.scalarConstant(0,o).getResult(0),p=n.builder.reduce(e,u,[s],"sum").getResult(0),c=Xo(i.length,s),f=n.builder.broadcast(p,i,c).getResult(0),d=n.builder.mul(a,f).getResult(0);return[n.builder.sub(e,d).getResult(0)]});pe("layer_norm",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.op.getAttr("axis"),o=n.op.getAttr("epsilon"),i=t.type.dtype,a=t.type.shape,u=[s<0?a.length+s:s],p=a[u[0]],c=Xo(a.length,u[0]),f=n.builder.scalarConstant(0,i).getResult(0),d=n.builder.reduce(t,f,u,"mean").getResult(0),h=n.builder.broadcast(d,a,c).getResult(0),m=n.builder.sub(t,h).getResult(0),g=n.builder.mul(m,m).getResult(0),y=n.builder.reduce(g,f,u,"mean").getResult(0),_=n.full(o,y.type),b=n.builder.add(y,_).getResult(0),w=n.builder.rsqrt(b).getResult(0),x=n.builder.broadcast(w,a,c).getResult(0),S=[];for(let ue=0;ue<a.length;ue++)u.includes(ue)&&S.push(ue);let T=n.builder.broadcast(r,a,S).getResult(0),v=n.builder.mul(e,T).getResult(0),A=n.full(p,t.type),N=n.builder.mul(A,v).getResult(0),k=n.builder.reduce(v,f,u,"sum").getResult(0),O=n.builder.broadcast(k,a,c).getResult(0),R=n.builder.mul(m,x).getResult(0),B=n.builder.mul(v,R).getResult(0),E=n.builder.reduce(B,f,u,"sum").getResult(0),F=n.builder.broadcast(E,a,c).getResult(0),C=n.builder.mul(R,F).getResult(0),H=n.builder.sub(N,n.builder.add(O,C).getResult(0)).getResult(0),G=n.builder.div(x,A).getResult(0),q=n.builder.mul(G,H).getResult(0),D=n.builder.reduce(n.builder.mul(e,R).getResult(0),f,c,"sum").getResult(0),V=n.builder.reduce(e,f,c,"sum").getResult(0);return[q,D,V]});pe("scaled_dot_product_attention",n=>{let e=n.gradOutputs[0],[t,r,s]=n.operands,o=n.op.getAttr("scale");if(n.op.getAttr("causal"))throw new Error("causal scaled_dot_product_attention VJP not supported");let i=n.builder,a=t.type.dtype,u=t.type.rank,p=[];for(let N=0;N<u;N++)p.push(N);p[u-2]=u-1,p[u-1]=u-2;let c=l(N=>i.transpose(N,p).getResult(0),"lastT"),f=i.matmul(t,c(r)).getResult(0),d=i.mul(f,n.full(o,f.type)).getResult(0),h=i.softmax(d,u-1).getResult(0),m=i.matmul(c(h),e).getResult(0),g=i.matmul(e,c(s)).getResult(0),y=i.scalarConstant(0,a).getResult(0),_=i.mul(g,h).getResult(0),b=i.reduce(_,y,[u-1],"sum").getResult(0),w=[];for(let N=0;N<u-1;N++)w.push(N);let x=i.broadcast(b,h.type.shape,w).getResult(0),S=i.mul(h,i.sub(g,x).getResult(0)).getResult(0),T=i.mul(S,n.full(o,S.type)).getResult(0),v=i.matmul(T,r).getResult(0),A=i.matmul(c(T),t).getResult(0);return[v,A,m]});pe("pool2d",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder,s=n.op.getAttr("pool_type"),o=n.op.getAttr("kernel_size"),i=n.op.getAttr("strides"),a=n.op.getAttr("padding"),u=n.op.getAttr("layout")||"NCHW",p=a.every(v=>v[0]===0&&v[1]===0),c=i[0]===o[0]&&i[1]===o[1];if(u!=="NCHW"||!p||!c||s!=="avg"&&s!=="max")throw new Error("pool2d VJP supports only non-overlapping (stride=kernel) avg/max pooling without padding, NCHW");let[f,d,h,m]=e.type.shape,[g,y]=o,_=t.type.shape,b=l(v=>r.reshape(r.broadcast(v,[f,d,h,g,m,y],[0,1,2,4]).getResult(0),_).getResult(0),"upsample"),w=b(e);if(s==="avg"){let v=n.full(g*y,t.type);return[r.div(w,v).getResult(0)]}let x=b(n.results[0]),S=r.compare(t,x,"eq").getResult(0),T=n.full(0,t.type);return[r.select(S,w,T).getResult(0)]});pe("batch_norm",n=>{let e=n.gradOutputs[0],[t,r,,s,o]=n.operands,i=n.op.getAttr("axis"),a=n.op.getAttr("epsilon"),u=t.type.dtype,p=t.type.shape,c=n.builder,f=Xo(p.length,i),d=c.scalarConstant(0,u).getResult(0),h=n.full(a,o.type),m=c.rsqrt(c.add(o,h).getResult(0)).getResult(0),g=c.broadcast(m,p,[i]).getResult(0),y=c.broadcast(s,p,[i]).getResult(0),_=c.broadcast(r,p,[i]).getResult(0),b=c.sub(t,y).getResult(0),w=c.mul(b,g).getResult(0),x=c.mul(e,_).getResult(0),S=c.mul(x,g).getResult(0),T=c.reduce(c.mul(e,w).getResult(0),d,f,"sum").getResult(0),v=c.reduce(e,d,f,"sum").getResult(0),A=c.neg(c.reduce(S,d,f,"sum").getResult(0)).getResult(0),N=c.mul(c.mul(g,g).getResult(0),g).getResult(0),k=n.full(-.5,t.type),O=c.mul(c.mul(c.mul(x,b).getResult(0),N).getResult(0),k).getResult(0),R=c.reduce(O,d,f,"sum").getResult(0);return[S,T,v,A,R]});pe("elu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.results[0],s=n.op.getAttr("alpha")??1,o=n.full(0,t.type),i=n.full(1,t.type),a=n.full(s,t.type),u=n.builder.compare(t,o,"gt").getResult(0),p=n.builder.add(r,a).getResult(0),c=n.builder.select(u,i,p).getResult(0);return[n.builder.mul(e,c).getResult(0)]});pe("leaky_relu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("negative_slope")??.01,s=n.full(0,t.type),o=n.full(1,t.type),i=n.full(r,t.type),a=n.builder.compare(t,s,"gt").getResult(0),u=n.builder.select(a,o,i).getResult(0);return[n.builder.mul(e,u).getResult(0)]});pe("celu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("alpha")??1,s=n.full(0,t.type),o=n.full(1,t.type),i=n.builder.compare(t,s,"gt").getResult(0),a=n.full(r,t.type),u=n.builder.div(t,a).getResult(0),p=n.builder.exp(u).getResult(0),c=n.builder.select(i,o,p).getResult(0);return[n.builder.mul(e,c).getResult(0)]});pe("selu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=1.0507009873554805,s=1.6732632423543772,o=n.full(0,t.type),i=n.full(r,t.type),a=n.builder.compare(t,o,"gt").getResult(0),u=n.full(s,t.type),p=n.builder.exp(t).getResult(0),c=n.builder.mul(u,p).getResult(0),f=n.builder.select(a,i,n.builder.mul(i,c).getResult(0)).getResult(0);return[n.builder.mul(e,f).getResult(0)]});pe("hardswish",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(-3,t.type),s=n.full(3,t.type),o=n.full(0,t.type),i=n.full(1,t.type),a=n.full(2,t.type),u=n.full(6,t.type),p=n.builder.compare(t,r,"le").getResult(0),c=n.builder.compare(t,s,"ge").getResult(0),f=n.builder.add(n.builder.mul(a,t).getResult(0),s).getResult(0),d=n.builder.div(f,u).getResult(0),h=n.builder.select(p,o,n.builder.select(c,i,d).getResult(0)).getResult(0);return[n.builder.mul(e,h).getResult(0)]});pe("hardsigmoid",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(-3,t.type),s=n.full(3,t.type),o=n.full(0,t.type),i=n.full(1/6,t.type),a=n.builder.compare(t,r,"le").getResult(0),u=n.builder.compare(t,s,"ge").getResult(0),p=n.builder.select(a,o,n.builder.select(u,o,i).getResult(0)).getResult(0);return[n.builder.mul(e,p).getResult(0)]});pe("embedding",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=r.type.rank,o=n.full(0,t.type);return[n.builder.scatter(o,r,e,{updateWindowDims:[s],insertedWindowDims:[0],scatterDimsToOperandDims:[0],indexVectorDim:s}).getResult(0),null]})});var pC=M(()=>{"use strict";Is();pk("stop_gradient");pe("stop_gradient",()=>[null]);for(let n of["compare","logical_not","argmax","argmin","iota"])pk(n)});var fC=M(()=>{"use strict";Is();Hd();Jd();hk();mw();eC();rC();iC();aC();uC();lC();cC();pC()});var dC={};at(dC,{compileWithBackward:()=>yw});function gw(n){return typeof n.then=="function"}function yw(n,e,t={}){let r=t.target??go(),s=t.mode||"separate",o=t.rematPolicy||new Lu(t.remat||{}),i={target:r,verify:!1,...t,backward:void 0,mode:void 0,rematPolicy:void 0,remat:void 0},a=t.dynamic_shapes||null,u=[],p=null,c=null,f=null;function d(k){let O=bw((...B)=>n.forward(...B),k,{name:n.constructor.name||"compiled",dynamicShapes:a}),R=l(B=>{let E=B.graph.functions().next().value,F=s==="joint"?m(E,B,o):h(E,B,o);return F.shapeEnv=B.shapeEnv,F.outputSymShapes=B.outputSymShapes,F},"finish");return gw(O)?O.then(R):R(O)}l(d,"_compile");function h(k,O,R){let B=new Qd({rematPolicy:R}),{backwardFunc:E,savedValues:F,gradInputIndices:C}=B.build(k),H=k.getReturnOp(),G=[...H.operands],q=G.length,D=new Map(k.args.map((ke,rt)=>[ke.id,rt])),V=new Map(G.map((ke,rt)=>[ke.id,rt])),ue=[],ce=new Map;for(let ke of F)D.has(ke.id)||V.has(ke.id)||ce.has(ke.id)||(ce.set(ke.id,q+ue.length),ue.push(ke));ue.length>0&&(H.erase(),new $n(k).returnOp([...G,...ue]),k.outputTypes=Object.freeze([...G.map(ke=>ke.type),...ue.map(ke=>ke.type)]));let Le=F.map(ke=>D.has(ke.id)?{kind:"arg",index:D.get(ke.id)}:V.has(ke.id)?{kind:"output",index:V.get(ke.id)}:{kind:"output",index:ce.get(ke.id)}),j=new Mn("forward");j.addFunction(k);let W=new Si(i).compile(j),ie=new Mn("backward");ie.addFunction(E);let je=new Si(i).compile(ie);return{mode:"separate",fwdResult:W,bwdResult:je,forwardFunc:k,backwardFunc:E,savedValues:F,savedSources:Le,numRealOutputs:q,gradInputIndices:C,capturedParams:O.capturedParams,numUserInputs:O.numUserInputs,outputTypes:O.outputTypes,shapeEnv:O.shapeEnv,outputSymShapes:O.outputSymShapes}}l(h,"_compileSeparate");function m(k,O,R){let B=new Xd({rematPolicy:R}),{jointFunc:E,numForwardOutputs:F,numGradInputs:C}=B.build(k),H=new Mn("joint");return H.addFunction(E),{mode:"joint",result:new Si(i).compile(H),jointFunc:E,numForwardOutputs:F,numGradInputs:C,capturedParams:O.capturedParams,numUserInputs:O.numUserInputs,outputTypes:O.outputTypes,inputTypes:k.inputTypes,shapeEnv:O.shapeEnv,outputSymShapes:O.outputSymShapes}}l(m,"_compileJoint");function g(k,O){return k.outputSymShapes&&k.shapeEnv?k.shapeEnv.resolveSymbolicShape(k.outputSymShapes[O]):[...k.outputTypes[O].shape]}l(g,"_resolveOutputShape");function y(k,O,R){let B=k.module||k;return B.executionPlan?B.runPlanAsync(B.executionPlan,R):k.isAsync(O)?k.runAsync(O,...R):(k.run(O,...R),null)}l(y,"_runK");function _(k,O){let B=k.fwdResult.listKernels()[0],E=O.length>0?O[0].device:"cpu",F=O.map(j=>Qe(j)),H=k.capturedParams.map(j=>Qe(j)),G=k.forwardFunc.outputTypes,q=k.numRealOutputs,D=new Array(G.length),V=new Array(G.length);for(let j=0;j<G.length;j++){let W=j<q?g(k,j):[...G[j].shape],ie=G[j].dtype,je=bn(W),ke=St(ie);D[j]=new ke(Math.max(je,1)),V[j]=W}let ue=[...F,...H,...D],ce=l(()=>({results:q===1?Ee(D[0],V[0],G[0].dtype,E):Array.from({length:q},(W,ie)=>Ee(D[ie],V[ie],G[ie].dtype,E)),inputArrays:F,paramArrays:H,outputArrays:D,device:E}),"build"),Le=y(k.fwdResult,B,ue);return Le?Le.then(ce):ce()}l(_,"_executeSeparateForward");function b(k,O,R){let E=k.bwdResult.listKernels()[0],F=O.map(ie=>Qe(ie)),C=k.savedValues,H=k.savedSources,G=[...R.inputArrays,...R.paramArrays],q=new Array(C.length);for(let ie=0;ie<C.length;ie++){let je=H[ie];q[ie]=je.kind==="arg"?G[je.index]:R.outputArrays[je.index]}let D=k.backwardFunc,V=D.outputTypes.length,ue=new Array(V),ce=new Array(V);for(let ie=0;ie<V;ie++){let je=[...D.outputTypes[ie].shape],ke=D.outputTypes[ie].dtype,rt=bn(je),zt=St(ke);ue[ie]=new zt(Math.max(rt,1)),ce[ie]=je}let Le=[...F,...q,...ue],j=l(()=>ue.map((ie,je)=>Ee(ie,ce[je],D.outputTypes[je].dtype,R.device)),"build"),W=y(k.bwdResult,E,Le);return W?W.then(j):j()}l(b,"_executeSeparateBackward");function w(k){for(let O=0;O<u.length;O++){let R=u[O];R.shapeEnv.bindInputShapes(k);let{passed:B}=R.shapeEnv.evaluateGuards();if(B)return R}return null}l(w,"_findCachedEntry");function x(k,O){if(c=k,k.mode==="joint")return v(k,O);let R=_(k,O);return gw(R)?R.then(B=>(p=B,B.results)):(p=R,R.results)}l(x,"_forwardWith");function S(k){let O=w(k);if(O)return x(O,k);let R=d(k);return gw(R)?R.then(B=>(u.push(B),B.shapeEnv.bindInputShapes(k),x(B,k))):(u.push(R),R.shapeEnv.bindInputShapes(k),x(R,k))}l(S,"_runForward");function T(...k){return f?f.then(()=>S(k)):S(k)}l(T,"compiledForward");function v(k,O){let B=k.result.listKernels()[0],E=O.length>0?O[0].device:"cpu",F=O.map(W=>Qe(W)),H=k.capturedParams.map(W=>Qe(W)),G=k.jointFunc,q=G.outputTypes.length,D=new Array(q),V=new Array(q);for(let W=0;W<q;W++){let ie=[...G.outputTypes[W].shape],je=G.outputTypes[W].dtype,ke=bn(ie),rt=St(je);D[W]=new rt(Math.max(ke,1)),V[W]=ie}let ue=new Array(k.numForwardOutputs);for(let W=0;W<k.numForwardOutputs;W++){let ie=k.outputTypes[W],je=bn(ie.shape),ke=St(ie.dtype);ue[W]=new ke(Math.max(je,1))}p={inputArrays:F,paramArrays:H,gradOutputArrays:ue,outputArrays:D,outputShapes:V,device:E,compiled:k};let ce=[...F,...H,...ue,...D],Le=l(()=>{let W=[];for(let ie=0;ie<k.numForwardOutputs;ie++)W.push(Ee(D[ie],V[ie],G.outputTypes[ie].dtype,E));return W.length===1?W[0]:W},"build"),j=y(k.result,B,ce);return j?j.then(Le):Le()}l(v,"_executeJointForward");let A=T;A.backward=function(...k){if(!c||!p)throw new Error("Must run forward before backward");return c.mode==="joint"?N(c,k,p):b(c,k,p)};function N(k,O,R){let{inputArrays:B,paramArrays:E,outputArrays:F,outputShapes:C,device:H}=R,G=O.map(W=>Qe(W));for(let W=0;W<G.length;W++)R.gradOutputArrays[W].set(G[W]);let q=k.jointFunc,D=q.outputTypes.length,V=new Array(D),ue=new Array(D);for(let W=0;W<D;W++){let ie=[...q.outputTypes[W].shape],je=q.outputTypes[W].dtype,ke=bn(ie),rt=St(je);V[W]=new rt(Math.max(ke,1)),ue[W]=ie}let ce=[...B,...E,...G,...V],Le=l(()=>{let W=[];for(let ie=k.numForwardOutputs;ie<D;ie++)W.push(Ee(V[ie],ue[ie],q.outputTypes[ie].dtype,H));return W},"build"),j=y(k.result,k.result.listKernels()[0],ce);return j?j.then(Le):Le()}if(l(N,"_executeJointBackward"),A.original=n,A.backwardGraph=()=>{if(u.length===0)return null;let k=u[0];return k.mode==="joint"?k.jointFunc:k.backwardFunc},A.forwardGraph=()=>{if(u.length===0)return null;let k=u[0];return k.mode==="joint"?k.jointFunc:k.forwardFunc},A.capturedParams=()=>u.length?u[0].capturedParams:[],e){let k=d(e);gw(k)?f=k.then(O=>(u.push(O),f=null,O)):u.push(k)}return A}var _w=M(()=>{"use strict";mk();iw();yo();Di();Jd();_s();hk();mw();fC();wn();rs();ss();l(gw,"_isThenable");l(yw,"compileWithBackward")});function Cu(n){return typeof n.then=="function"}function Cj(){hC||(W2(),hC=!0)}function Dj(n,e){if(!n)return new Array(e.length).fill(null);let t=new Array(e.length);for(let r=0;r<e.length;r++){let s=n[r];if(s===!0){let o=new Set;for(let i=0;i<e[r].shape.length;i++)o.add(i);t[r]=o}else s instanceof Set?t[r]=s:t[r]=null}return t}function bw(n,e,t){Cj();let r=t?.name||n.name||"traced",s=new M_(r),o=Dj(t?.dynamicShapes,e);for(let f=0;f<e.length;f++)s.createInput(e[f].shape,e[f].dtype,o[f]);let i=e.length,a=s._initGraph();function u(f){Array.isArray(f)?s.markOutputs(f):s.markOutput(f),s.deactivate();let d=s.getGraphModule(),h=d.functions().next().value;return{graph:d,capturedParams:[...s.capturedParams],numUserInputs:i,outputTypes:h.outputTypes,shapeEnv:s.shapeEnv,outputSymShapes:s.outputSymShapes}}l(u,"_finalize"),s.activate();let p=Jr.fromKey(48),c=NN(p,()=>n(...a));if(Cu(c))return c.then(f=>u(f),f=>{throw s.deactivate(),f});try{return u(c)}catch(f){throw s.deactivate(),f}}function mC(n,e,t){let r=bw(n,e,t);return Cu(r)?r.then(s=>s.graph):r.graph}function Pj(n,e,t){let r=n.result.listKernels();if(r.length===0)throw new Error("No kernels compiled");let s=r[0],o=e.length>0?e[0].device:"cpu",i=new Array(e.length);for(let g=0;g<e.length;g++)i[g]=Qe(e[g]);let a=n.capturedParams,u=new Array(a.length);for(let g=0;g<a.length;g++)u[g]=Qe(a[g]);let p=n.outputTypes,c=n.outputSymShapes,f=new Array(p.length),d=new Array(p.length);for(let g=0;g<p.length;g++){let y=c&&t?t.resolveSymbolicShape(c[g]):p[g].shape,_=p[g].dtype,b=bn(y),w=St(_);f[g]=new w(Math.max(b,1)),d[g]=[...y]}let h=new Array(i.length+u.length+f.length),m=0;for(let g=0;g<i.length;g++)h[m++]=new Lo(i[g],e[g].shape,e[g].dtype);for(let g=0;g<u.length;g++){let y=new Lo(u[g],a[g].shape,a[g].dtype),_=a[g]._impl;_&&(y.resident={key:_.storage.rawData,version:_.version}),h[m++]=y}for(let g=0;g<f.length;g++)h[m++]=new Lo(f[g],d[g],p[g].dtype);return{funcName:s,device:o,outputTypes:p,outputArrays:f,outputShapes:d,allArgs:h}}function gk(n,e,t,r){if(e.length===1)return Ee(t[0],r[0],e[0].dtype,n);let s=new Array(e.length);for(let o=0;o<e.length;o++)s[o]=Ee(t[o],r[o],e[o].dtype,n);return s}function $j(n,e,t){let{funcName:r,device:s,outputTypes:o,outputArrays:i,outputShapes:a,allArgs:u}=Pj(n,e,t),p=n.result.module.executionPlan;return p?n.result.module.runPlanAsync(p,u,{resident:!0}).then(()=>gk(s,o,i,a)):n.result.isAsync(r)?n.result.runAsync(r,...u).then(()=>gk(s,o,i,a)):(n.result.run(r,...u),gk(s,o,i,a))}function Fj(n,e){let t=[];for(let r of n.result.listKernels()){let s=n.result.getSource(r);s!==null&&t.push(`// ---- compiled entry ${e}, kernel ${r} ----
|
|
247
|
+
`+" ".repeat(this.indent))}visit(e){if(!e)return;let t="visit"+e.type;this[t]?this[t](e):this.push(`[UnknownNode: ${e.type}]`)}visitPrimFunc(e){this.push(`prim_func ${e.name}(${e.params.map(t=>t.name).join(", ")}) {`),this.indent++,this.newline();for(let[t,r]of e.bufferMap)this.push(`${r.name} = buffer_map(${t.name}, shape=[${r.shape.join(",")}], dtype=${r.dtype})`),this.newline();this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitSeqNode(e){for(let t=0;t<e.stmts.length;t++)this.visit(e.stmts[t]),t<e.stmts.length-1&&this.newline()}visitForNode(e){let t=e.kind==="serial"?"":`@${e.kind} `,r=e.threadTag?`[${e.threadTag}] `:"";this.push(`for ${e.loopVar.name} in 0..`),this.visit(e.extent),this.push(` ${t}${r}{`),this.indent++,this.newline(),this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitBlockNode(e){this.push(`block ${e.name} {`),this.indent++,this.newline();for(let t of e.iterVars)this.push(`bind ${t.iterVar.name} = `),this.visit(t.binding),this.newline();e.reads.length>0&&(this.push("reads(["),this.push(e.reads.map(t=>`${t.buffer.name}[...]`).join(", ")),this.push("])"),this.newline()),e.writes.length>0&&(this.push("writes(["),this.push(e.writes.map(t=>`${t.buffer.name}[...]`).join(", ")),this.push("])"),this.newline()),e.initBody&&(this.push("init {"),this.indent++,this.newline(),this.visit(e.initBody),this.indent--,this.newline(),this.push("}"),this.newline()),this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitBufferStoreNode(e){this.push(`${e.buffer.name}[`);for(let t=0;t<e.indices.length;t++)this.visit(e.indices[t]),t<e.indices.length-1&&this.push(", ");this.push("] = "),this.visit(e.value)}visitBufferLoadNode(e){this.push(`${e.buffer.name}[`);for(let t=0;t<e.indices.length;t++)this.visit(e.indices[t]),t<e.indices.length-1&&this.push(", ");this.push("]")}visitIfThenElseNode(e){this.push("if ("),this.visit(e.condition),this.push(") {"),this.indent++,this.newline(),this.visit(e.thenBody),this.indent--,this.newline(),this.push("}"),e.elseBody&&(this.push(" else {"),this.indent++,this.newline(),this.visit(e.elseBody),this.indent--,this.newline(),this.push("}"))}visitLetStmtNode(e){this.push(`let ${e.variable.name} = `),this.visit(e.value),this.newline(),this.visit(e.body)}visitAllocateNode(e){this.push(`allocate ${e.buffer.name}[${e.buffer.shape.join(", ")}] (${e.scope}) {`),this.indent++,this.newline(),this.visit(e.body),this.indent--,this.newline(),this.push("}")}visitEvaluateNode(e){this.push("evaluate "),this.visit(e.value)}visitMathOpNode(e){this.push("("),this.visit(e.a),e.b&&(this.push(` ${e.op} `),this.visit(e.b)),this.push(")")}visitCompareNode(e){this.push("("),this.visit(e.a),this.push(` ${Ku(e.direction)} `),this.visit(e.b),this.push(")")}visitCallExternNode(e){this.push(`${e.externName}(`);for(let t=0;t<e.args.length;t++)this.visit(e.args[t]),t<e.args.length-1&&this.push(", ");this.push(")")}visitVariableNode(e){this.push(e.name)}visitIntImmNode(e){this.push(e.value.toString())}visitFloatImmNode(e){this.push(e.value.toString())}visitCastNode(e){this.push(`cast<${e.toDtype}>(`),this.visit(e.expr),this.push(")")}};l(V_,"printTensorIR")});var G_,bE=M(()=>{"use strict";nn();nv();G_=class{static{l(this,"TirPassManager")}constructor(){this.passes=[],this.trace=null,this.verifyHook=null}addPass(e){this.passes.push(e)}setTrace(e){this.trace=e}setVerifyHook(e){this.verifyHook=e}run(e,t){for(let r of this.passes)this._runPass(r,e,t);return e}_runPass(e,t,r){let s=r.trace;e.trace=s,s.phaseStart(e.phase);let o=performance.now();e.begin(r);for(let i=0;i<t.length;i++){let a=t[i];if(!r.failed.has(a.name))try{let u=e.run(a,r);u&&u!==a&&(t[i]=u)}catch(u){if(r.errors.push(new Yn(e.phase,a.name,u.message)),r.failed.add(a.name),s.errorEvent(e.phase,a.name,u.message),!r.resilient)break}}if(e.end(r),s.phaseEnd(e.phase,performance.now()-o),e.snapshotPoint&&s.shouldSnapshot(e.snapshotPoint))for(let i of t)r.failed.has(i.name)||s.irDump(e.snapshotPoint+":"+i.name,V_(i));this.verifyHook&&this._verifyFuncs(t,r),e.trace=null}_verifyFuncs(e,t){for(let r of e){if(t.failed.has(r.name))continue;let s=this.verifyHook(r);if(s&&s.length>0){let o=s.join("; ");if(t.resilient)t.errors.push(new Yn("verification",r.name,o)),t.failed.add(r.name);else throw new Error("TensorIR verification failed for "+r.name+": "+o)}}}}});function wE(n){let e=new Wu;return e.add(new Zu),e.add(new Qu),e.add(new Xu(n)),e.add(new el),e.add(new tl(n)),e.add(new nl),e.add(new rl),e.add(new Vh),e.add(new Ju),e.add(new Kh),e.add(new Hh),e.add(new Yh),e.add(new Jh),n&&(e.add(new Wh(n)),e.add(new qh(n)),e.add(new Uh(n))),e}var p6,f6,j_,TE=M(()=>{"use strict";fn();lp();Rx();sl();l(wE,"buildAlgebraicPatterns");p6=wE(!1),f6=wE(!0),j_=class extends Ue{static{l(this,"AlgebraicSimplificationPass")}constructor(e={}){super("algebraic_simplify"),this.preservedAnalyses=new Set,this.patterns=e.fastMath?f6:p6}run(e,t){return new Ul(this.patterns).applyPatterns(e,10,this.trace)}}});function xE(n){let e=ne.get(n);return!!(e&&e.isConstant)}function d6(n,e){return!f0(e)||typeof n!="number"?!0:Number.isInteger(n)&&Number.isSafeInteger(n)}function SE(n,e,t){if(t.has(n))return t.get(n);let r=h6(n,e,t);return t.set(n,r),r}function h6(n,e,t){let r=n.definingOp;if(!r)return;if(xE(r.opName))return r.getAttr("value");if(e.has(r))return;e.add(r);let s=ne.get(r.opName);if(!s||!s.fold||s.hasSideEffects||r.regions.length>0)return;let o=new Array(r.numOperands),i=new Array(r.numOperands);for(let a=0;a<r.numOperands;a++){let u=SE(r.getOperand(a),e,t);if(u===void 0)return;o[a]=u,i[a]=r.getOperand(a).definingOp}try{return s.fold(o,r.attributes,i)}catch{return}}var q_,vE=M(()=>{"use strict";fn();_s();Ct();nn();Re();l(xE,"isConstantProducer");l(d6,"isFoldResultRepresentable");l(SE,"resolveConstantValue");l(h6,"computeConstantValue");q_=class extends Ue{static{l(this,"ConstantFoldPass")}constructor(){super("constant_fold")}run(e,t){let r=!1,s=0,o=new $n(e),i=new Map;for(let a of[...e.opsRecursive()]){if(!a.parentBlock||xE(a.opName))continue;let u=ne.get(a.opName);if(!u||a.regions.length>0||u.hasSideEffects||u.getMemoryEffects&&u.getMemoryEffects(a).length>0||!u.fold||a.numOperands===0)continue;let p=new Array(a.numOperands),c=new Array(a.numOperands),f=!0;for(let d=0;d<a.numOperands;d++){let h=SE(a.getOperand(d),new Set,i);if(h===void 0){f=!1;break}p[d]=h,c[d]=a.getOperand(d).definingOp}if(f)try{let d=u.fold(p,a.attributes,c);if(d===void 0||!d6(d,a.getResult(0).type.dtype))continue;o.block=a.parentBlock,o.setInsertionPoint(a);let h=o.constant(d,a.getResult(0).type);a.replaceAllResultsWith([h.getResult(0)]),a.erase(),r=!0,s++}catch(d){this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,foldError:a.opName,message:d.message,level:le.DEBUG})}}return this.trace&&this.trace.level>=le.DEBUG&&s>0&&this.trace.emit({type:"pass_detail",passName:this.name,foldedCount:s,level:le.DEBUG}),r?oe.CHANGED:oe.UNCHANGED}}});var U_,kE=M(()=>{"use strict";fn();Ct();nn();U_=class extends Ue{static{l(this,"CSEPass")}constructor(){super("cse"),this.preservedAnalyses=new Set}run(e,t){let r=!1,s=0,o=typeof e.blocksRecursive=="function"?[...e.blocksRecursive()]:[...e.body];for(let i of o){let a=new Map;for(let u of[...i.ops()]){if(!u.parentBlock||u.regions&&u.regions.length>0)continue;let p=ne.get(u.opName);if(p&&p.hasSideEffects||p&&p.getMemoryEffects&&p.getMemoryEffects(u).length>0)continue;let c=u.structuralHash();if(!a.has(c)){a.set(c,[u]);continue}let f=a.get(c),d=!1;for(let h of f)if(h.parentBlock&&h.structuralEquals(u)){let m=[];for(let g=0;g<h.numResults;g++)m.push(h.getResult(g));u.replaceAllResultsWith(m),u.erase(),r=!0,s++,d=!0;break}d||f.push(u)}}return this.trace&&this.trace.level>=le.DEBUG&&s>0&&this.trace.emit({type:"pass_detail",passName:this.name,eliminated:s,level:le.DEBUG}),r?oe.CHANGED:oe.UNCHANGED}}});function sv(n,e){let t=rv[n],r=rv[e];return t===void 0||r===void 0?!1:n===We.REDUCTION?e===We.ELEMENTWISE:e===We.REDUCTION?t<=rv[We.INJECTIVE]:!0}function wc(n){let e=!1,t=!1,r=!1,s=!1;for(let o of n){let i=ne.get(o.opName);if(!i||i.isOpaque){r=!0;continue}i.isReduction?e=!0:i.isInjective?t=!0:i.isBroadcast?s=!0:i.isElementwise||(r=!0)}return r?We.OPAQUE:e?We.REDUCTION:t?We.INJECTIVE:s?We.BROADCAST:We.ELEMENTWISE}function Ou(n){let e=ne.get(n.opName);return!e||e.isOpaque?We.OPAQUE:e.isReduction?We.REDUCTION:e.isInjective?We.INJECTIVE:e.isBroadcast?We.BROADCAST:e.isElementwise?We.ELEMENTWISE:We.OPAQUE}function NE(n,e){if(n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(!(n[t]===X||e[t]===X)&&n[t]!==e[t])return!1;return!0}var We,rv,_c,bc,Bu=M(()=>{"use strict";Re();Ct();$a();Ir();We=Object.freeze({ELEMENTWISE:"kElementwise",BROADCAST:"kBroadcast",REDUCTION:"kReduction",INJECTIVE:"kInjective",OPAQUE:"kOpaque",HORIZONTAL:"kHorizontal"}),rv={[We.ELEMENTWISE]:0,[We.BROADCAST]:1,[We.INJECTIVE]:2,[We.REDUCTION]:3};l(sv,"canFusePatterns");l(wc,"classifyFusionKind");l(Ou,"classifyOpPattern");_c=Object.freeze({legal:!0}),bc=class{static{l(this,"FusionLegality")}constructor(e={}){this.maxFusionSize=e.maxFusionSize||512,this.maxSharedMemory=e.maxSharedMemory||49152,this.libraryOps=e.libraryOps||new Set,this.allowReductionFusion=e.allowReductionFusion!==!1,this._lowerableCache=new Map}isOpLowerable(e){let t=this._lowerableCache.get(e);return t===void 0&&(t=Xt(e)||ni(e)||C0(e),this._lowerableCache.set(e,t)),t}canFuse(e,t){if(!e||!t)return{legal:!1,reason:"null op"};if(e===t)return{legal:!1,reason:"same op"};if(e.regions.length>0&&e.opName!=="fusion"&&e.opName!=="reduce")return{legal:!1,reason:"producer has control flow regions"};if(t.regions.length>0&&t.opName!=="fusion"&&t.opName!=="reduce")return{legal:!1,reason:"consumer has control flow regions"};let r=ne.get(e.opName),s=ne.get(t.opName);if(!r)return{legal:!1,reason:"unknown producer op"};if(!s)return{legal:!1,reason:"unknown consumer op"};if(!this.isOpLowerable(e.opName))return{legal:!1,reason:`producer op '${e.opName}' has no lowering rule`};if(!this.isOpLowerable(t.opName))return{legal:!1,reason:`consumer op '${t.opName}' has no lowering rule`};if(r.isOpaque)return{legal:!1,reason:"producer is opaque (use EpilogueFusionPass for dot/conv epilogues)"};if(s.isOpaque)return{legal:!1,reason:"consumer is opaque"};let o=Ou(e),i=Ou(t);return sv(o,i)?(o===We.REDUCTION||i===We.REDUCTION)&&!this.allowReductionFusion?{legal:!1,reason:"reduction fusion disabled by target"}:o===We.ELEMENTWISE&&i===We.ELEMENTWISE?this._checkElementwisePair(e,t):(o===We.BROADCAST||o===We.REDUCTION)&&i===We.ELEMENTWISE?_c:this._checkProducerConsumerShapes(e,t):{legal:!1,reason:`cannot fuse pattern ${o} -> ${i}`}}canMergeGroups(e,t){if(e.size+t.size>this.maxFusionSize)return{legal:!1,reason:"merged group exceeds max fusion size"};let r=0;for(let s of e.ops){let o=ne.get(s.opName);if(o&&o.isReduction&&r++,o&&o.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}for(let s of t.ops){let o=ne.get(s.opName);if(o&&o.isReduction&&r++,o&&o.isOpaque)return{legal:!1,reason:"opaque op in merge (use EpilogueFusionPass)"}}return r>1?{legal:!1,reason:"merged group would contain multiple reductions"}:_c}_checkElementwisePair(e,t){let r=this._getOutputShape(e),s=this._getOutputShape(t);return r&&s&&!NE(r,s)?{legal:!1,reason:`elementwise shape mismatch: [${r}] vs [${s}]`}:_c}_checkProducerConsumerShapes(e,t){let r=this._getOutputShape(e);if(!r)return _c;for(let s=0;s<t.numOperands;s++)if(t.getOperand(s).definingOp===e){let o=t.getOperand(s).type;if(o instanceof I)return NE(r,o.shape)?_c:{legal:!1,reason:`shape mismatch on data edge: [${r}] vs [${o.shape}]`}}return _c}_getOutputShape(e){for(let t=0;t<e.numResults;t++){let r=e.getResult(t).type;if(r instanceof I)return r.shape}return null}};l(NE,"shapesCompatible")});function Ru(n){return am(n,"throw")}function kd(n,{labelOf:e,sameLabel:t=l((i,a)=>i===a,"sameLabel"),canMerge:r=l(()=>!0,"canMerge"),onAttach:s=l(()=>{},"onAttach"),sort:o=Ru}){let i=o(n),a=new Map,u=new Map,p=[],c=0,f=l((m,g)=>{if(m===g)return!0;let y=[g],_=new Set;for(;y.length>0;){let b=y.pop();if(b===m)return!0;if(_.has(b))continue;_.add(b);let w=u.get(b);if(w)for(let x of w)y.push(x)}return!1},"isUpstreamOf"),d=l(m=>{let g=new Set;for(let y=0;y<m.numOperands;y++){let _=m.getOperand(y).definingOp;if(!_)continue;let b=a.get(_);b&&g.add(b)}return g},"operandParts"),h=l((m,g)=>{for(let y of d(m)){if(y===g)continue;let _=u.get(g);_||(_=new Set,u.set(g,_)),_.add(y)}},"recordEdges");for(let m of i){let g=e(m);if(g==null)continue;let y=!1;for(let _=0;_<m.numOperands;_++){let b=m.getOperand(_).definingOp;if(!b)continue;let w=a.get(b);if(!w||!t(w.label,g)||!r(w,m,g))continue;let x=!1;for(let S of d(m))if(S!==w&&f(w,S)){x=!0;break}if(!x){w.ops.push(m),w.opSet.add(m),s(w,m),a.set(m,w),h(m,w),y=!0;break}}if(!y){let _={id:c++,label:g,ops:[m],opSet:new Set([m])};s(_,m),p.push(_),a.set(m,_),h(m,_)}}return{partitions:p,opToPart:a,preds:u}}function pa(n,e,{pullConstants:t=!1,isConstant:r=l(()=>!1,"isConstant")}={}){let s=[],o=new Set,i=[],a=new Set,u=[],p=new Set;for(let c of e){for(let f=0;f<c.numOperands;f++){let d=c.getOperand(f),h=d.definingOp;if(!(h&&n.has(h))){if(t&&h&&r(h)){p.has(h)||(p.add(h),u.push(h));continue}o.has(d)||(o.add(d),s.push(d))}}for(let f=0;f<c.numResults;f++){let d=c.getResult(f);if(a.has(d))continue;let h=!1;for(let m of d.uses())if(!n.has(m.user)){h=!0;break}h&&(a.add(d),i.push(d))}}return{inputs:s,outputs:i,constDefs:u}}function ov(n,e){let t=new Map,r=new Map;for(let a of n)t.set(a,0),r.set(a,[]);for(let a of n){let u=e.get(a);if(u)for(let p of u)r.has(p)&&(r.get(p).push(a),t.set(a,t.get(a)+1))}let s=[];for(let a of n)t.get(a)===0&&s.push(a);let o=[],i=0;for(;i<s.length;){let a=s[i++];o.push(a);for(let u of r.get(a)){let p=t.get(u)-1;t.set(u,p),p===0&&s.push(u)}}return o.length===n.length?o:null}var Nd=M(()=>{"use strict";Da();l(Ru,"topoSortOps");l(kd,"buildPartitions");l(pa,"computePartitionIO");l(ov,"topoSortPartitions")});function m6(n){for(let e=0;e<n.numResults;e++){let t=n.getResult(e).type;if(t instanceof I)return t.shape.join(",")}return null}var wi,W_,K_=M(()=>{"use strict";Re();Ct();Bu();$a();Nd();wi=class{static{l(this,"FusionGroup")}constructor(e){this.id=e,this.ops=[],this.opSet=new Set,this.kind=null,this._inputValues=null,this._outputValues=null,this.minTopoPos=1/0,this.maxTopoPos=-1/0}addOp(e,t){this.opSet.has(e)||(this.ops.push(e),this.opSet.add(e),this._inputValues=null,this._outputValues=null,t!==void 0&&(t<this.minTopoPos&&(this.minTopoPos=t),t>this.maxTopoPos&&(this.maxTopoPos=t)))}hasOp(e){return this.opSet.has(e)}merge(e){for(let t of e.ops)this.addOp(t);e.minTopoPos<this.minTopoPos&&(this.minTopoPos=e.minTopoPos),e.maxTopoPos>this.maxTopoPos&&(this.maxTopoPos=e.maxTopoPos)}computeIO(){if(this._inputValues&&this._outputValues)return;let{inputs:e,outputs:t}=pa(this.opSet,this.ops);this._inputValues=e,this._outputValues=t}getInputValues(){return this.computeIO(),this._inputValues}getOutputValues(){return this.computeIO(),this._outputValues}get size(){return this.ops.length}classifyKind(){return this.kind=wc(this.ops),this.kind}allOpsInlineFusable(){for(let e of this.ops){let t=ne.get(e.opName);if(!t)return!1;if(!(t.isReduction||t.isConstant)&&!ni(e.opName))return!1}return!0}};l(m6,"outputShapeKey");W_=class{static{l(this,"FusionGroupBuilder")}constructor(e){this.legality=e,this._nextId=0,this._topoIndex=null}buildProducerConsumerGroups(e){this._topoIndex=new Map;let t=0;for(let i of e.ops())this._topoIndex.set(i,t++);let r=[],s=new Map;for(let i of e.ops()){let a=ne.get(i.opName);if(!a||a.isConstant||a.isTerminator||a.isReduction)continue;let u=this._topoIndex.get(i);for(let p=0;p<i.numOperands;p++){let c=i.getOperand(p).definingOp;if(!c)continue;let f=ne.get(c.opName);if(!f||f.isConstant||f.isReduction)continue;let d=s.get(i),h=s.get(c);if(d&&d===h||!this.legality.canFuse(c,i).legal)continue;let g=this._topoIndex.get(c);if(!d&&!h){let y=new wi(this._nextId++);y.addOp(c,g),y.addOp(i,u),s.set(c,y),s.set(i,y)}else if(d&&!h)d.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(d,c)&&(d.addOp(c,g),s.set(c,d));else if(!d&&h)h.size<this.legality.maxFusionSize&&!this._wouldCreateCycle(h,i)&&(h.addOp(i,u),s.set(i,h));else if(this.legality.canMergeGroups(d,h).legal&&!this._mergeWouldCreateCycle(d,h)){d.merge(h);for(let _ of h.ops)s.set(_,d)}}}let o=new Set;for(let i of s.values())o.has(i)||i.size<2||(o.add(i),i.classifyKind(),r.push(i));return r}_wouldCreateCycle(e,t){for(let r=0;r<t.numOperands;r++){let s=t.getOperand(r).definingOp;if(!(!s||e.hasOp(s)||this._topoIndex.get(s)<e.minTopoPos)&&this._dependsOnGroup(s,e))return!0}return!1}_dependsOnOps(e,t,r){let s=new Set,o=[e];for(s.add(e);o.length>0;){let i=o.pop();for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a).definingOp;if(!u||s.has(u))continue;if(t.has(u))return!0;this._topoIndex.get(u)<r||(s.add(u),o.push(u))}}return!1}_dependsOnGroup(e,t){return this._dependsOnOps(e,t.opSet,t.minTopoPos)}_mergeWouldCreateCycle(e,t){let r=Math.min(e.minTopoPos,t.minTopoPos),s=Math.max(e.maxTopoPos,t.maxTopoPos),o=new Set([...e.opSet,...t.opSet]);for(let i of o)for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a).definingOp;if(!u||o.has(u))continue;let p=this._topoIndex.get(u);if(!(p<r||p>s)&&this._dependsOnOps(u,o,r))return!0}return!1}_bucketable(e,t){return t&&!t.isConstant&&!t.isTerminator&&!t.isOpaque&&e.numResults>0&&m6(e)!==null}buildHorizontalGroups(e){let t=[...e.ops()];this._topoIndex=new Map;for(let u=0;u<t.length;u++)this._topoIndex.set(t[u],u);let r=[],s=new Map,o=this.legality.maxFusionSize||t.length,i=new Map,a=0;for(let u=0;u<t.length;u++){let p=t[u];if(s.has(p))continue;let c=ne.get(p.opName);if(!this._bucketable(p,c))continue;let f=p.getResult(0).type;a++,i.set(p,a);let d=new wi(this._nextId++);d.addOp(p,u),s.set(p,d);let h=Math.min(t.length,u+1+o);for(let m=u+1;m<h;m++){let g=t[m],y=!1;for(let b=0;b<g.numOperands;b++){let w=g.getOperand(b).definingOp;if(w&&i.get(w)===a){y=!0;break}}if(y){i.set(g,a);continue}if(d.size>=o||s.has(g))continue;let _=ne.get(g.opName);this._bucketable(g,_)&&f.equals(g.getResult(0).type)&&(this._sharesInput(p,g)||c.isElementwise&&_.isElementwise)&&(d.addOp(g,m),s.set(g,d),i.set(g,a))}d.size>=2&&(d.kind=We.HORIZONTAL,r.push(d))}return r}buildAllGroups(e){let t=this.buildProducerConsumerGroups(e),r=this.buildHorizontalGroups(e),s=new Map;for(let c of t)for(let f of c.ops)s.set(f,c);let o=new Set(s.keys()),i=[];for(let c of r){let f=!1;for(let d of c.ops)if(o.has(d)){f=!0;break}f||i.push(c)}let a=[...e.ops()],u=[];for(let c of a)for(let f=0;f<c.numOperands;f++){let d=c.getOperand(f).definingOp;d&&u.push(c,d)}for(let c of i)for(let f of c.ops)s.set(f,c);if(!this._condensedHasCycle(a,u,s))return[...t,...i];for(let c of i)for(let f of c.ops)s.delete(f);let p=[...t];for(let c of i){for(let f of c.ops)s.set(f,c);if(this._condensedHasCycle(a,u,s)){for(let f of c.ops)s.delete(f);continue}p.push(c)}return p}_condensedHasCycle(e,t,r){let s=l(f=>r.get(f)||f,"repOf"),o=new Map,i=new Set;for(let f of e)i.add(s(f));for(let f=0;f<t.length;f+=2){let d=s(t[f]),h=s(t[f+1]);if(h===d)continue;i.add(h),i.add(d);let m=o.get(h);m||(m=new Set,o.set(h,m)),m.add(d)}let a=0,u=1,p=2,c=new Map;for(let f of i)c.set(f,a);for(let f of i){if(c.get(f)!==a)continue;let d=[f];for(;d.length>0;){let h=d[d.length-1],m=c.get(h);if(m===a){c.set(h,u);let g=o.get(h);if(g)for(let y of g){let _=c.get(y);if(_===u)return!0;_===a&&d.push(y)}}else m===u&&c.set(h,p),d.pop()}}return!1}_sharesInput(e,t){if(e.numOperands===0||t.numOperands===0)return!1;let r=new Set;for(let s=0;s<e.numOperands;s++)r.add(e.getOperand(s));for(let s=0;s<t.numOperands;s++)if(r.has(t.getOperand(s)))return!0;return!1}_hasDependency(e,t){for(let r=0;r<e.numResults;r++){let s=e.getResult(r);for(let o=0;o<t.numOperands;o++)if(t.getOperand(o)===s)return!0}return!1}}});var g6,fa,H_=M(()=>{"use strict";Re();Ct();g6=Object.freeze({memory:1,launch:1e3}),fa=class{static{l(this,"FusionCostModel")}constructor(e={}){this.memoryBandwidthGBs=e.memoryBandwidthGBs||900,this.computeTFLOPs=e.computeTFLOPs||15,this.launchOverheadUs=e.launchOverheadUs||5,this.minBenefitRatio=e.minBenefitRatio||1.05,this.maxRegistersPerThread=e.maxRegistersPerThread||255,this.maxSharedMemory=e.maxSharedMemory||49152,this.maxCodeSizeOps=e.maxCodeSizeOps||256,this.libraryOps=e.libraryOps||new Set,this.registerBytesPerOp=e.registerBytesPerOp||8,this.policy=e.policy||null,this.benefitWeights={...g6,...e.benefitWeights||{}}}edgeBenefit(e){let t=this.benefitWeights;return t.launch*this.launchOverheadUs+t.memory*e}estimateOpCost(e){let t=this.estimateFLOPs(e),r=this.estimateBytes(e);return{flops:t,bytes:r,arithmeticIntensity:r>0?t/r:0}}estimateFLOPs(e){let t=ne.get(e.opName);if(t&&t.getFlops)return t.getFlops(e);let r=1;for(let s=0;s<e.numResults;s++){let o=e.getResult(s).type;if(o instanceof I){let i=o.numel();i!==X&&(r=i);break}}if(t&&t.isReduction&&e.numOperands>0){let s=e.getOperand(0).type;if(s instanceof I){let o=s.numel();if(o!==X)return o}}return r}estimateBytes(e){let t=0;for(let r=0;r<e.numOperands;r++){let s=e.getOperand(r).type;if(s instanceof I){let o=s.sizeInBytes();o!==X&&(t+=o)}}for(let r=0;r<e.numResults;r++){let s=e.getResult(r).type;if(s instanceof I){let o=s.sizeInBytes();o!==X&&(t+=o)}}return t}estimateGroupCost(e){let t=e.opSet,r=new Map,s=0,o=0,i=0,a=0,u=!1,p=!1,c=0,f=0,d=0,h=0,m=new Set;for(let x of e.ops){let S=this.estimateFLOPs(x);r.set(x,S),s+=S,o+=this.estimateBytes(x),this.libraryOps.has(x.opName)&&a++;let T=ne.get(x.opName);if(T){let v=this._outputElements(x);T.isReduction&&(u=!0,c+=v),T.isElementwise&&(p=!0,f+=v)}for(let v=0;v<x.numResults;v++){let A=x.getResult(v);m.add(A);let N=0,k=!1;for(let O of A.uses())t.has(O.user)?N++:k=!0;N>1&&(i+=(N-1)*S),(N>0||k)&&d++}for(let v=0;v<x.numOperands;v++){let A=x.getOperand(v);if(!m.has(A))continue;let N=!1;for(let k of A.uses())if(t.has(k.user)&&k.user!==x){N=!0;break}N||d--}d>h&&(h=d)}let g=e.getInputValues(),y=e.getOutputValues(),_=0;for(let x of g)if(x.type instanceof I){let S=x.type.sizeInBytes();S!==X&&(_+=S)}for(let x of y)if(x.type instanceof I){let S=x.type.sizeInBytes();S!==X&&(_+=S)}let b=0;for(let x of e.ops)for(let S=0;S<x.numResults;S++){let T=x.getResult(S),v=0;for(let A of T.uses())if(t.has(A.user)&&(v++,v>1))break;if(v>1){let A=T.type;if(A instanceof I){let N=A.sizeInBytes();N!==X&&(b+=N)}}}let w=0;return u&&p&&f>0&&c>0&&(w=Math.abs(f-c)),{unfusedFLOPs:s,unfusedBytes:o,fusedFLOPs:s+i,fusedBytes:_,recomputeCost:i,memorySaved:o-_,launchSaved:(e.size-1)*this.launchOverheadUs,registerPressure:h*this.registerBytesPerOp,sharedMemoryUsage:b,parallelismLoss:w,libraryCallLoss:a}}shouldFuse(e){if(e.size<2)return{fuse:!1,reason:"group too small"};if(this.policy&&typeof this.policy.shouldFuse=="function"){let r=this.policy.shouldFuse(e,this);if(r)return r}let t=this.estimateGroupCost(e);if(t.libraryCallLoss>0)return{fuse:!1,reason:"fusion would lose library call opportunity",cost:t};if(t.registerPressure>this.maxRegistersPerThread)return{fuse:!1,reason:`register pressure ${t.registerPressure} exceeds limit ${this.maxRegistersPerThread}`,cost:t};if(t.sharedMemoryUsage>this.maxSharedMemory)return{fuse:!1,reason:`shared memory ${t.sharedMemoryUsage} exceeds limit ${this.maxSharedMemory}`,cost:t};if(e.size>this.maxCodeSizeOps)return{fuse:!1,reason:`code size ${e.size} exceeds limit ${this.maxCodeSizeOps}`,cost:t};if(t.memorySaved<=0&&t.launchSaved<=0)return{fuse:!1,reason:"no memory or launch benefit",cost:t};if(t.parallelismLoss>0){let r=t.memorySaved+t.launchSaved*1e3;if(t.parallelismLoss>r*this.minBenefitRatio)return{fuse:!1,reason:"parallelism loss outweighs fusion benefit",cost:t}}return{fuse:!0,reason:`saves ${t.memorySaved} bytes, ${t.launchSaved}us launch`,cost:t}}_outputElements(e){for(let t=0;t<e.numResults;t++){let r=e.getResult(t).type;if(r instanceof I){let s=r.numel();if(s!==X)return s}}return 0}}});function Tc(n){let e=null;for(let t of n.ops())e=t;return e&&e.opName==="yield"?e:null}function xc(n){let e=0,t=n.regions[0]?.entryBlock;if(!t)return 0;for(let r of t.ops())r.opName!=="yield"&&e++;return e}function Sc(n){let e=0,t=n.regions[0]?.entryBlock;if(!t)return 0;for(let r of t.ops()){let s=ne.get(r.opName);s&&s.isReduction&&e++}return e}function vc(n){let e=n.regions[0]?.entryBlock;if(!e)return!1;for(let t of e.ops())if(t.opName!=="yield"&&!ni(t.opName))return!1;return!0}function y6(n){let e=new Map,t=0;for(let r=n.firstOp;r;r=r._next)e.set(r,t++);return e}function iv(n){let e=y6(n);return(t,r)=>{let s=e.get(t),o=e.get(r);return s!==void 0&&o!==void 0&&s<o}}function kc(n,e){let t=Qo(n.ops,g=>n.hasOp(g),"null");if(t===null||t.length===0)return null;n._inputValues=null,n._outputValues=null;let r=n.getInputValues(),s=n.getOutputValues(),o=r.map(g=>g.type),i=s.map(g=>g.type),a=new qt,u=new On(o);a.addBlock(u);let p=new Map;for(let g=0;g<r.length;g++)p.set(r[g],u.arguments[g]);for(let g of t)u.pushOp(g.clone(p));let c=s.map(g=>{let y=p.get(g);if(y===void 0)throw new Error("Fusion materialization: output value not found in valueMap");return y});u.pushOp(new Me("yield",c,[]));let f=new Me("fusion",r,i,{fusion_kind:n.kind||e},[a]),d=t[0].parentBlock;if(!d)return null;let h=iv(d),m=null;for(let g of r){let y=g.definingOp;!y||n.hasOp(y)||(!m||!h(y,m))&&(m=y)}m&&m.parentBlock===d?d.insertAfter(f,m):d.insertBefore(f,t[0]);for(let g=0;g<s.length;g++)s[g].replaceAllUsesWith(f.getResult(g));for(let g of t)g.dropAllOperands(),g.parentBlock&&g.parentBlock.removeOp(g);return f}var Nc=M(()=>{"use strict";Ct();$a();yr();Ea();Da();l(Tc,"getYieldOp");l(xc,"countInnerOps");l(Sc,"countReductions");l(vc,"allInnerOpsFusable");l(y6,"blockPositionIndex");l(iv,"makeComesBefore");l(kc,"materializeFusionGroup")});var Y_,AE=M(()=>{"use strict";fn();Bu();K_();H_();Nc();nn();Y_=class extends Ue{static{l(this,"FusionPass")}constructor(e={}){super("FusionPass");let t=e.target||{};this.legality=new bc({maxFusionSize:t.maxFusionSize||e.maxFusionSize,maxSharedMemory:t.sharedMemoryBytes||e.maxSharedMemory,libraryOps:t.libraryOps||e.libraryOps,allowReductionFusion:e.allowReductionFusion}),this.costModel=new fa({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,policy:t.getAttr?t.getAttr("fusionPolicy"):null,...e.cost}),this.groupBuilder=new W_(this.legality)}run(e,t){let r=this.groupBuilder.buildAllGroups(e),s=[];for(let i of r)this._createsCycle(e,i)?this._traceDecision(i,!1,"fusing would create a dependency cycle"):s.push(i);let o=[];for(let i of s){if(!i.allOpsInlineFusable()){this._traceDecision(i,!1,"group contains ops without inline fusion support");continue}let a=this.costModel.shouldFuse(i);this._traceDecision(i,a.fuse,a.reason),a.fuse&&o.push(i)}if(o.length===0)return oe.UNCHANGED;for(let i of o)kc(i,We.ELEMENTWISE);return oe.CHANGED}_traceDecision(e,t,r){if(!this.trace||this.trace.level<le.DEBUG)return;let s=e.ops.map(o=>o.opName);this.trace.emit({type:"fusion_decision",passName:this.name,groupSize:e.ops.length,ops:s,anchor:s[s.length-1]||null,fuse:t,reason:r||null,level:le.DEBUG}),this.trace.explain("fusion",s.join("+"),t?"fused":"not-fused",r||null,{groupSize:s.length})}_createsCycle(e,t){let r=t.opSet,s=t.getOutputValues(),o=new Set,i=[];for(let u of s)for(let p of u.uses())r.has(p.user)||(i.push(p.user),o.add(p.user));let a=0;for(;a<i.length;){let u=i[a++];if(r.has(u))return!0;for(let p=0;p<u.numResults;p++)for(let c of u.getResult(p).uses())o.has(c.user)||(o.add(c.user),i.push(c.user))}return!1}}});function J_(n){return cl(n.opName)||_n(n.opName)}function OE(n){if(J_(n))return!0;let e=ne.get(n.opName);return e!==null&&e.isElementwise}function b6(n,e){let t=n.getOperand(0).definingOp;return t&&e.has(t)&&!J_(t)?n.getOperand(1).definingOp:t}function w6(n,e){if(!n.parentBlock||n.parentBlock!==e.parentBlock)return!1;let t=n.parentBlock.firstOp;for(;t;){if(t===n)return!0;if(t===e)return!1;t=t._next}return!1}function T6(n,e){for(let t of n)if(t!==e){for(let r=0;r<t.numResults;r++)for(let s of t.getResult(r).uses())if(!n.has(s.user))return!0}return!1}function x6(n,e){let t=ne.get(n.opName),r=t&&t.getAttr("epilogueTag");return r?r(n,e):"activation"}function S6(n){let e=[],t=new Set,r=new Set;function s(d){if(!(r.has(d)||d===n)){r.add(d);for(let h=0;h<d.numOperands;h++){let m=d.getOperand(h).definingOp;m&&m!==n&&!r.has(m)&&J_(m)&&s(m)}e.push(d),t.add(d)}}l(s,"absorb");let o=[],i=n.getResult(0);for(let d of i.uses())OE(d.user)&&o.push(d.user);for(;o.length>0;){let d=o.pop();if(!r.has(d)){s(d);for(let h=0;h<d.numResults;h++)for(let m of d.getResult(h).uses())!r.has(m.user)&&OE(m.user)&&o.push(m.user)}}let a=[];for(let d of e)J_(d)||a.push(x6(d,t));let u=e[e.length-1];for(let d=e.length-1;d>=0;d--){let h=e[d];for(let m=0;m<h.numResults;m++){for(let g of h.getResult(m).uses())if(!t.has(g.user)){u=h,d=-1;break}if(d<0)break}}let p=new Set;for(let d=0;d<n.numOperands;d++)p.add(n.getOperand(d));let c=[],f=new Set;for(let d of e)for(let h=0;h<d.numOperands;h++){let m=d.getOperand(h);if(f.has(m))continue;f.add(m);let g=m.definingOp;g!==n&&(g&&t.has(g)||p.has(m)||c.push(m))}return{chain:e,chainSet:t,tags:a,lastOp:u,extras:c}}function v6(n){let e=null,t=null,r=null,s=null,o=new Set;for(let i=0;i<2;i++){let a=n.getOperand(i),u=a.definingOp;if(!u||u.opName!=="convert")continue;let p=!1;for(let f of a.uses())if(f.user!==n){p=!0;break}if(p)continue;let c=u.getAttr("target_dtype")||u.getResult(0).type.dtype;i===0?(e=c,r=u.getOperand(0)):(t=c,s=u.getOperand(0)),o.add(u)}return{lhsCast:e,rhsCast:t,lhsInput:r,rhsInput:s,removed:o}}var _6,Z_,BE=M(()=>{"use strict";fn();yr();Ct();Ir();nn();l(J_,"isPassthrough");l(OE,"isEpilogueCandidate");_6=new Map([["add",(n,e)=>e.has(b6(n,e))?"residual_add":"bias"],["sub",()=>"bias"],["mul",()=>"scale"],["maximum",()=>"relu"],["clamp",()=>"clamp"],["neg",()=>"neg"],["exp",()=>"exp"],["tanh",()=>"tanh"],["sqrt",()=>"sqrt"],["abs",()=>"abs"],["log",()=>"log"]]);l(b6,"resolveOtherOperand");for(let[n,e]of _6)ne.has(n)&&ne.registerOpAttr(n,"epilogueTag",e);l(w6,"comesBefore");l(T6,"hasEscapingUse");l(x6,"classifyTag");l(S6,"collectChainAndAnalyze");l(v6,"collectPrologue");Z_=class extends Ue{static{l(this,"EpilogueFusionPass")}constructor(e={}){super("EpilogueFusionPass"),this.maxEpilogueOps=e.maxEpilogueOps||16,this.target=e.target||null}run(e){if(this.target&&!this.target.enableEpilogueFusion)return oe.UNCHANGED;let t=!1,r=[];for(let s of e.ops()){let o=ne.get(s.opName);o&&o.isOutEWiseFusable&&r.push(s)}for(let s of r){let o=S6(s);if(o.chain.length>this.maxEpilogueOps)continue;let i=v6(s),a=o.chain.length>0,u=i.lhsCast!==null||i.rhsCast!==null;if(!a&&!u)continue;let{chain:p,tags:c,extras:f}=o,d=0;for(let v of c)v==="bias"||v==="residual_add"||v==="scale"?d++:v==="clamp"&&(d+=2);if(d!==f.length)continue;let h=i.lhsInput||s.getOperand(0),m=i.rhsInput||s.getOperand(1),g=[h,m,...f],y=a?o.lastOp:s,_=y.getResult(0).type,b=new Map(s.attributes);b.set("epilogue_ops",p.map(v=>v.opName)),b.set("epilogue_tags",c),b.set("num_dot_operands",2),b.set("num_extra_inputs",f.length),i.lhsCast&&b.set("lhs_prologue_cast",i.lhsCast),i.rhsCast&&b.set("rhs_prologue_cast",i.rhsCast);let w=new Me("fused_dot_epilogue",g,[_],b),x=s.parentBlock;if(!x)continue;let S=new Set(p);S.add(s);for(let v of i.removed)S.add(v);if(T6(S,y))continue;let T=null;for(let v of g){let A=v.definingOp;!A||S.has(A)||(!T||!w6(A,T))&&(T=A)}T&&T.parentBlock===x?x.insertAfter(w,T):x.insertBefore(w,s),y.getResult(0).replaceAllUsesWith(w.getResult(0));for(let v=p.length-1;v>=0;v--)p[v].dropAllOperands(),p[v].parentBlock&&p[v].parentBlock.removeOp(p[v]);s.dropAllOperands(),s.parentBlock&&s.parentBlock.removeOp(s);for(let v of i.removed)v.dropAllOperands(),v.parentBlock&&v.parentBlock.removeOp(v);t=!0}return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,dotsFound:r.length,changed:t,level:le.DEBUG}),t?oe.CHANGED:oe.UNCHANGED}}});var Q_,RE=M(()=>{"use strict";fn();yr();Ea();Re();Ct();nn();Bu();Nc();Q_=class extends Ue{static{l(this,"FusionMergerPass")}constructor(e={}){super("FusionMergerPass"),this.maxFusionSize=e.maxFusionSize||512,this.maxReductions=e.maxReductions||1,this.launchOverheadUs=e.launchOverheadUs||5,this.minMemorySavings=e.minMemorySavings||0}run(e){let t=[];for(let u of e.ops())u.opName==="fusion"&&t.push(u);if(t.length<2)return oe.UNCHANGED;let r=new Set(t),s=this._buildProducerConsumerEdges(t,r);if(s.length===0)return oe.UNCHANGED;let o=!1,i=0,a=new Set;for(let{producer:u,consumer:p,sharedResults:c}of s)a.has(u)||a.has(p)||this._canMerge(u,p)&&this._shouldMerge(u,p,c)&&(this._mergeCreatesCycle(u,p,r)||(this._merge(u,p,c),a.add(u),a.add(p),o=!0,i++));return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:t.length,edges:s.length,mergeCount:i,level:le.DEBUG}),o?oe.CHANGED:oe.UNCHANGED}_buildProducerConsumerEdges(e,t){let r=[],s=new Map;for(let o of e)for(let i=0;i<o.numResults;i++){let a=o.getResult(i);for(let u of a.uses()){if(!t.has(u.user))continue;let p=u.user;if(p===o)continue;let c=`${o.id}|${p.id}`,f=s.get(c);f||(f={producer:o,consumer:p,sharedResults:new Map},s.set(c,f),r.push(f)),f.sharedResults.set(i,u.operandIndex)}}return r}_canMerge(e,t){let r=xc(e),s=xc(t);if(r+s>this.maxFusionSize)return!1;let o=0;return o+=Sc(e),o+=Sc(t),!(o>this.maxReductions||!vc(e)||!vc(t))}_shouldMerge(e,t,r){let s=0;for(let[u]of r){let p=e.getResult(u);if(p.type instanceof I){let c=p.type.sizeInBytes();c!==X&&(s+=c)}}let o=!0;for(let u=0;u<e.numResults;u++){let p=e.getResult(u);for(let c of p.uses())if(c.user!==t){o=!1;break}if(!o)break}if(o)return!0;let i=this._estimateRecomputeCost(e);return s+this.launchOverheadUs*1e3>i}_mergeCreatesCycle(e,t,r){let s=new Set,o=[];for(let a=0;a<t.numOperands;a++){let p=t.getOperand(a).definingOp;p&&p!==e&&p.opName!=="constant"&&(s.has(p)||(s.add(p),o.push(p)))}let i=0;for(;i<o.length;){let a=o[i++];if(a===e)return!0;for(let u=0;u<a.numOperands;u++){let p=a.getOperand(u).definingOp;p&&!s.has(p)&&(s.add(p),o.push(p))}}return!1}_merge(e,t,r){let s=e.regions[0].entryBlock,o=t.regions[0].entryBlock,i=Tc(s),a=Tc(o);if(!i||!a)return;let u=new Map;for(let[N,k]of r)u.set(k,N);let p=[],c=new Map;for(let N=0;N<e.numOperands;N++)c.set(N,p.length),p.push(e.getOperand(N));let f=new Map;for(let N=0;N<t.numOperands;N++){if(u.has(N))continue;let k=t.getOperand(N),O=-1;for(let R=0;R<p.length;R++)if(p[R]===k){O=R;break}O>=0?f.set(N,O):(f.set(N,p.length),p.push(k))}let d=p.map(N=>N.type),h=new qt,m=new On(d);h.addBlock(m);let g=new Map;for(let N=0;N<s.arguments.length;N++)g.set(s.arguments[N],m.arguments[c.get(N)]);for(let N of s.ops())N.opName!=="yield"&&m.pushOp(N.clone(g));for(let N=0;N<i.numOperands;N++){let k=i.getOperand(N),O=g.get(k)||k;for(let[R,B]of u)B===N&&g.set(o.arguments[R],O)}for(let N=0;N<o.arguments.length;N++){if(u.has(N))continue;let k=f.get(N);k!==void 0&&g.set(o.arguments[N],m.arguments[k])}for(let N of o.ops())N.opName!=="yield"&&m.pushOp(N.clone(g));let y=[],_=[],b=[];for(let N=0;N<e.numResults;N++){let k=!1;for(let O of e.getResult(N).uses())if(O.user!==t){k=!0;break}if(k){let O=i.getOperand(N),R=g.get(O)||O;b.push({resultIdx:N,value:R}),y.push(R),_.push(e.getResult(N).type)}}for(let N=0;N<t.numResults;N++){let k=a.getOperand(N),O=g.get(k)||k;y.push(O),_.push(t.getResult(N).type)}let w=new Me("yield",y,[]);m.pushOp(w);let x=[];for(let N of m.ops())N.opName!=="yield"&&x.push(N);let S=wc(x),T=new Me("fusion",p,_,{fusion_kind:S},[h]),v=t;if(!v.parentBlock)return;v.parentBlock.insertBefore(T,v);let A=0;for(let{resultIdx:N}of b)e.getResult(N).replaceAllUsesWith(T.getResult(A)),A++;for(let N=0;N<t.numResults;N++)t.getResult(N).replaceAllUsesWith(T.getResult(A)),A++;e.dropAllOperands(),e.parentBlock&&e.parentBlock.removeOp(e),t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t)}_estimateRecomputeCost(e){let t=0,r=e.regions[0]?.entryBlock;if(!r)return 0;for(let s of r.ops()){if(s.opName==="yield")continue;let o=ne.get(s.opName);if(o&&o.getFlops)t+=o.getFlops(s);else for(let i=0;i<s.numResults;i++){let a=s.getResult(i).type;if(a instanceof I){let u=a.numel();u!==X&&(t+=u);break}}}return t}}});function k6(n,e){let t=Math.min(n.id,e.id),r=Math.max(n.id,e.id);return`${t}|${r}`}var Ad,ME=M(()=>{"use strict";fn();yr();Ea();Re();Bu();nn();Nc();Ad=class extends Ue{static{l(this,"MultiOutputFusionPass")}constructor(e={}){super("MultiOutputFusionPass"),this.maxFusionSize=e.maxFusionSize||512,this.maxReductions=e.maxReductions||1,this.minSharedInputBytes=e.minSharedInputBytes||0,this.maxOutputs=e.maxOutputs||8,this.maxConsumersPerInput=e.maxConsumersPerInput||64}run(e){let t=[];for(let a of e.ops())a.opName==="fusion"&&t.push(a);if(t.length<2)return oe.UNCHANGED;let r=this._findCandidates(t);if(r.length===0)return oe.UNCHANGED;let s=!1,o=0,i=new Set;for(let{left:a,right:u,sharedInputs:p,sharedBytes:c}of r)i.has(a)||i.has(u)||this._canMerge(a,u)&&(this._mergeCreatesCycle(a,u)||(this._mergeMultiOutput(a,u,p),i.add(a),i.add(u),s=!0,o++));return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,fusionOps:t.length,candidates:r.length,mergeCount:o,level:le.DEBUG}),s?oe.CHANGED:oe.UNCHANGED}_findCandidates(e){let t=new Map;for(let o of e)for(let i=0;i<o.numOperands;i++){let u=o.getOperand(i).id,p=t.get(u);p||(p=[],t.set(u,p)),p.push(o)}let r=new Map;for(let[o,i]of t){if(i.length<2)continue;let a=Math.min(i.length,this.maxConsumersPerInput);for(let u=0;u<a;u++)for(let p=u+1;p<a;p++){let c=i[u],f=i[p];if(c===f)continue;let d=k6(c,f),h=r.get(d);h||(h={left:c,right:f,sharedInputs:new Set,sharedBytes:0},r.set(d,h)),h.sharedInputs.add(o)}}let s=[];for(let o of r.values()){let i=0;for(let a of o.sharedInputs){let u=t.get(a);if(!u||u.length===0)continue;let p=u[0];for(let c=0;c<p.numOperands;c++){let f=p.getOperand(c);if(f.id===a&&f.type instanceof I){let d=f.type.sizeInBytes();d!==X&&(i+=d);break}}}o.sharedBytes=i,i>=this.minSharedInputBytes&&s.push(o)}return s.sort((o,i)=>i.sharedBytes-o.sharedBytes),s}_canMerge(e,t){if(this._hasProducerConsumerEdge(e,t)||this._hasProducerConsumerEdge(t,e))return!1;let r=xc(e),s=xc(t);if(r+s>this.maxFusionSize||e.numResults+t.numResults>this.maxOutputs)return!1;let i=0;return i+=Sc(e),i+=Sc(t),!(i>this.maxReductions||!vc(e)||!vc(t))}_mergeCreatesCycle(e,t){let r=new Set,s=[];for(let i=0;i<e.numResults;i++){let a=e.getResult(i);for(let u of a.uses())u.user!==t&&!r.has(u.user)&&(r.add(u.user),s.push(u.user))}let o=0;for(;o<s.length;){let i=s[o++];if(i===t)return!0;for(let a=0;a<i.numResults;a++)for(let u of i.getResult(a).uses())r.has(u.user)||(r.add(u.user),s.push(u.user))}r.clear(),s.length=0;for(let i=0;i<t.numResults;i++){let a=t.getResult(i);for(let u of a.uses())u.user!==e&&!r.has(u.user)&&(r.add(u.user),s.push(u.user))}for(o=0;o<s.length;){let i=s[o++];if(i===e)return!0;for(let a=0;a<i.numResults;a++)for(let u of i.getResult(a).uses())r.has(u.user)||(r.add(u.user),s.push(u.user))}return!1}_mergeMultiOutput(e,t,r){let s=e.regions[0].entryBlock,o=t.regions[0].entryBlock,i=Tc(s),a=Tc(o);if(!i||!a)return;let u=[],p=new Map,c=new Map;for(let k=0;k<e.numOperands;k++){let O=e.getOperand(k),R=O.id,B=p.get(R);B===void 0&&(B=u.length,p.set(R,B),u.push(O)),c.set(k,B)}let f=new Map;for(let k=0;k<t.numOperands;k++){let O=t.getOperand(k),R=O.id,B=p.get(R);B===void 0&&(B=u.length,p.set(R,B),u.push(O)),f.set(k,B)}let d=u.map(k=>k.type),h=new qt,m=new On(d);h.addBlock(m);let g=new Map;for(let k=0;k<s.arguments.length;k++)g.set(s.arguments[k],m.arguments[c.get(k)]);for(let k of s.ops())k.opName!=="yield"&&m.pushOp(k.clone(g));for(let k=0;k<o.arguments.length;k++)g.set(o.arguments[k],m.arguments[f.get(k)]);for(let k of o.ops())k.opName!=="yield"&&m.pushOp(k.clone(g));let y=[],_=[];for(let k=0;k<i.numOperands;k++){let O=i.getOperand(k);y.push(g.get(O)||O),_.push(e.getResult(k).type)}for(let k=0;k<a.numOperands;k++){let O=a.getOperand(k);y.push(g.get(O)||O),_.push(t.getResult(k).type)}let b=new Me("yield",y,[]);m.pushOp(b);let w=[];for(let k of m.ops())k.opName!=="yield"&&w.push(k);let x=wc(w),S=new Me("fusion",u,_,{fusion_kind:x},[h]),T=e.parentBlock;if(!T)return;let v=iv(T),A=null;for(let k of u){let O=k.definingOp;!O||O===e||O===t||(!A||!v(O,A))&&(A=O)}A&&A.parentBlock===T?T.insertAfter(S,A):T.insertBefore(S,e);for(let k=0;k<e.numResults;k++)e.getResult(k).replaceAllUsesWith(S.getResult(k));let N=e.numResults;for(let k=0;k<t.numResults;k++)t.getResult(k).replaceAllUsesWith(S.getResult(N+k));e.dropAllOperands(),e.parentBlock&&e.parentBlock.removeOp(e),t.dropAllOperands(),t.parentBlock&&t.parentBlock.removeOp(t)}_hasProducerConsumerEdge(e,t){for(let r=0;r<e.numResults;r++)for(let s of e.getResult(r).uses())if(s.user===t)return!0;return!1}};l(k6,"pairKey")});var av,ft,ro=M(()=>{"use strict";av=class{static{l(this,"UseDefResult")}constructor(e,t,r,s,o){this.topologicalOrder=e,this.valueToOp=t,this.opUsers=r,this.depth=s,this.height=o}},ft=class{static get name(){return"use_def"}static get depKey(){return"useDef"}static get dependencies(){return[]}static compute(e){let t=[],r=new Map,s=new Map,o=new Set,i=new Set,a=l(c=>{if(o.has(c))return;i.add(c);let f=[{op:c,i:0}];for(;f.length>0;){let d=f[f.length-1],h=d.op;if(d.i<h.numOperands){let m=h.getOperand(d.i).definingOp;if(d.i++,m&&!o.has(m)){if(i.has(m))throw new Error("Cycle detected in UseDefAnalysis");i.add(m),f.push({op:m,i:0})}continue}i.delete(h),o.add(h),t.push(h),f.pop()}},"visit");for(let c of e.ops()){for(let f=0;f<c.numResults;f++)r.set(c.getResult(f),c);s.set(c,new Set)}let u=new Map,p=new Map;for(let c of e.ops())a(c);for(let c of t){let f=0;for(let d=0;d<c.numOperands;d++){let h=c.getOperand(d).definingOp;if(h){s.get(h).add(c);let m=u.get(h)||0;m+1>f&&(f=m+1)}}u.set(c,f)}for(let c=t.length-1;c>=0;c--){let f=t[c],d=0,h=s.get(f)||new Set;for(let m of h){let g=p.get(m)||0;g+1>d&&(d=g+1)}p.set(f,d)}return new av(t,r,s,u,p)}}});var uv,X_,IE=M(()=>{"use strict";ro();Ir();uv=class{static{l(this,"DominanceResult")}constructor(e){this.idom=e}postDominates(e,t){let r=t;for(;r;){if(r===e)return!0;r=this.idom.get(r)}return!1}immediatePDom(e){return this.idom.get(e)||null}pathToPDom(e){let t=[],r=this.idom.get(e);for(;r&&(t.push(r),r!==this.idom.get(r));)r=this.idom.get(r);return t}},X_=class{static get name(){return"post_dominance"}static get depKey(){return"postDominance"}static get dependencies(){return[ft]}static compute(e,t={}){let s=(t.useDef||ft.compute(e)).topologicalOrder,o=new Map;for(let m of s)o.set(m,[]);for(let m of s)for(let g=0;g<m.numResults;g++){let y=m.getResult(g);for(let _ of y.uses()){let b=_.user;o.has(b)&&o.get(m).push(b)}}let i=new Set;for(let m of s)Xt(m.opName)&&i.add(m);let a={opName:"__pdom_root__"},u=Math.max(1,Math.ceil(Math.log2(s.length+2))+1),p=new Map,c=new Map,f=new Map;c.set(a,0),f.set(a,new Array(u).fill(a));let d=l((m,g)=>{p.set(m,g),c.set(m,c.get(g)+1);let y=new Array(u);y[0]=g;for(let _=1;_<u;_++)y[_]=f.get(y[_-1])[_-1];f.set(m,y)},"link"),h=l((m,g)=>{if(c.get(m)<c.get(g)){let _=m;m=g,g=_}let y=c.get(m)-c.get(g);for(let _=0;_<u;_++)y>>_&1&&(m=f.get(m)[_]);if(m===g)return m;for(let _=u-1;_>=0;_--)f.get(m)[_]!==f.get(g)[_]&&(m=f.get(m)[_],g=f.get(g)[_]);return f.get(m)[0]},"lca");for(let m=s.length-1;m>=0;m--){let g=s[m];if(i.has(g)){d(g,a);continue}let y=null;for(let _ of o.get(g))p.has(_)&&(y=y===null?_:h(y,_));y!==null&&d(g,y)}for(let[m,g]of p)g===a&&p.delete(m);return new uv(p,s)}}});function EE(n){return Xt(n)||_n(n)}var eb,LE=M(()=>{"use strict";fn();Ct();Bu();K_();H_();IE();nn();ro();Nc();Ir();l(EE,"isSkipOp");eb=class extends Ue{static{l(this,"DominatorFusionPass")}constructor(e={}){super("DominatorFusionPass"),this.requiredAnalyses=[ft];let t=e.target||{};this.maxFusionSize=t.maxFusionSize||e.maxFusionSize||512,this.maxReductions=e.maxReductions||1,this.libraryOps=t.libraryOps||e.libraryOps||new Set,this.costModel=new fa({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,...e.cost})}run(e,t){let r=t?t.getAnalysis(ft,e):ft.compute(e),s=X_.compute(e,{useDef:r}),o=r.topologicalOrder,i=this._buildGroups(o,s);if(i.length===0)return oe.UNCHANGED;let a=[];for(let u of i){if(!u.allOpsInlineFusable()){this._explain(u,!1,"group contains ops without inline fusion support");continue}let p=this.costModel.shouldFuse(u);this._explain(u,p.fuse,p.reason),p.fuse&&a.push(u)}if(a.length===0)return oe.UNCHANGED;for(let u of a)kc(u,We.ELEMENTWISE);return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,groupsBuilt:i.length,groupsFused:a.length,level:le.DEBUG}),oe.CHANGED}_explain(e,t,r){if(!this.trace||!this.trace.explainsEnabled)return;let s=e.ops.map(o=>o.opName);this.trace.explain("fusion",s.join("+"),t?"fused":"not-fused",r||null,{groupSize:s.length,strategy:"dominator"})}_buildGroups(e,t){let r=new Map,s=[],o=0;for(let p=e.length-1;p>=0;p--){let c=e[p];if(EE(c.opName))continue;let f=Ou(c);if(f===We.OPAQUE||this.libraryOps.has(c.opName))continue;let d=t.immediatePDom(c);if(!d||EE(d.opName))continue;let h=Ou(d);if(h===We.OPAQUE||this.libraryOps.has(d.opName)||!this._canFusePatterns(f,h)||!this._pathAllFusable(c,d,e,t))continue;let m=r.get(d);if(m){if(m.size+1>this.maxFusionSize||!this._checkReductionLimit(m,c))continue;m.addOp(c),r.set(c,m),this._absorbIntermediates(c,d,m,r,e,t)}else{let g=r.get(c);if(g){if(g.size+1>this.maxFusionSize||!this._checkReductionLimit(g,d))continue;g.addOp(d),r.set(d,g),this._absorbIntermediates(c,d,g,r,e,t)}else{let y=new wi(o++);if(y.addOp(c),y.addOp(d),!this._checkGroupReductions(y))continue;r.set(c,y),r.set(d,y),s.push(y),this._absorbIntermediates(c,d,y,r,e,t)}}}let i=new Set,a=[];for(let p of s)i.has(p)||p.size<2||(i.add(p),p.classifyKind(),a.push(p));for(let p of r.values())i.has(p)||p.size<2||(i.add(p),p.classifyKind(),a.push(p));let u=[];for(let p of a){if(!this._checkGroupReductions(p)){this._explain(p,!1,`group exceeds the ${this.maxReductions}-reduction limit`);continue}if(this._createsCycle(p)){this._explain(p,!1,"fusing would create a dependency cycle");continue}u.push(p)}return u}_createsCycle(e){let t=new Set;for(let o of e.getInputValues())o.definingOp&&!e.hasOp(o.definingOp)&&t.add(o.definingOp);if(t.size===0)return!1;let r=new Set,s=[];for(let o of e.getOutputValues())for(let i of o.uses())e.hasOp(i.user)||s.push(i.user);for(;s.length>0;){let o=s.pop();if(!r.has(o)){if(r.add(o),t.has(o))return!0;for(let i=0;i<o.numResults;i++)for(let a of o.getResult(i).uses())e.hasOp(a.user)||s.push(a.user)}}return!1}_canFusePatterns(e,t){return sv(e,t)}_pathAllFusable(e,t,r,s){let o=new Set,i=[e];for(o.add(e),o.add(t);i.length>0;){let a=i.pop();for(let u=0;u<a.numResults;u++)for(let p of a.getResult(u).uses()){let c=p.user;if(c===t||o.has(c)||!s.postDominates(t,c))continue;if(o.add(c),Ou(c)===We.OPAQUE||this.libraryOps.has(c.opName))return!1;i.push(c)}}return!0}_absorbIntermediates(e,t,r,s,o,i){let a=new Set,u=[e];for(a.add(e),a.add(t);u.length>0;){let p=u.pop();for(let c=0;c<p.numResults;c++)for(let f of p.getResult(c).uses()){let d=f.user;d===t||a.has(d)||!i.postDominates(t,d)||(a.add(d),Ou(d)===We.OPAQUE||this.libraryOps.has(d.opName))||(r.size<this.maxFusionSize&&this._checkReductionLimit(r,d)&&(r.addOp(d),s.set(d,r)),u.push(d))}}}_checkGroupReductions(e){let t=0;for(let r of e.ops){let s=ne.get(r.opName);s&&s.isReduction&&t++}return t<=this.maxReductions}_checkReductionLimit(e,t){let r=ne.get(t.opName);if(!r||!r.isReduction)return!0;let s=0;for(let o of e.ops){let i=ne.get(o.opName);i&&i.isReduction&&s++}return s<this.maxReductions}}});var tb,CE=M(()=>{"use strict";tb=class{static{l(this,"GraphCycles")}constructor(e,t){this._n=e,this._parent=new Int32Array(e),this._rank=new Int32Array(e),this._nodeAtRank=new Int32Array(e),this._out=new Array(e),this._in=new Array(e);for(let r=0;r<e;r++)this._parent[r]=r,this._rank[r]=r,this._nodeAtRank[r]=r,this._out[r]=new Set,this._in[r]=new Set;if(t)for(let[r,s]of t)r!==s&&(this._out[r].add(s),this._in[s].add(r))}find(e){let t=this._parent,r=e;for(;t[r]!==r;)r=t[r];for(;t[e]!==r;){let s=t[e];t[e]=r,e=s}return r}wouldCreateCycle(e,t){let r=this.find(e),s=this.find(t);if(r===s)return!1;let o=this._rank[r]<this._rank[s]?r:s,i=o===r?s:r;return this._hasIntermediatePath(o,i)}_hasIntermediatePath(e,t){let r=this._rank[t],s=new Set([e]),o=[e];for(;o.length>0;){let i=o.pop();for(let a of this._out[i]){let u=this.find(a);if(!(u===i||u===e)){if(u===t){if(i!==e)return!0;continue}this._rank[u]>=r||s.has(u)||(s.add(u),o.push(u))}}}return!1}merge(e,t){let r=this.find(e),s=this.find(t);if(r===s)return r;let o=this._rank[r]<this._rank[s]?r:s,i=o===r?s:r,a=this._rank[o],u=this._rank[i],p=this._out[r].size+this._in[r].size,c=this._out[s].size+this._in[s].size,f=p>=c?r:s,d=f===r?s:r,h=this._out[f],m=this._in[f];for(let g of this._in[d]){let y=this.find(g);y===f||y===d||(this._out[y].delete(d),this._out[y].add(f),m.add(y))}for(let g of this._out[d]){let y=this.find(g);y===f||y===d||(this._in[y].delete(d),this._in[y].add(f),h.add(y))}return h.delete(d),m.delete(d),h.delete(f),m.delete(f),this._parent[d]=f,this._nodeAtRank[this._rank[d]]=-1,this._reorder(a,u),f}_reorder(e,t){let r=[],s=[];for(let c=e;c<=t;c++){let f=this._nodeAtRank[c];if(!(f<0)){if(this.find(f)!==f){this._nodeAtRank[c]=-1;continue}r.push(c),s.push(f)}}if(s.length<=1){for(let c=0;c<s.length;c++)this._rank[s[c]]=r[c],this._nodeAtRank[r[c]]=s[c];return}let o=new Set(s),i=new Map;for(let c of s)i.set(c,0);for(let c of s)for(let f of this._out[c]){let d=this.find(f);d!==c&&o.has(d)&&i.set(d,i.get(d)+1)}let a=[];for(let c of s)i.get(c)===0&&a.push(c);let u=[],p=0;for(;p<a.length;){let c=a[p++];u.push(c);for(let f of this._out[c]){let d=this.find(f);if(d!==c&&o.has(d)){let h=i.get(d)-1;i.set(d,h),h===0&&a.push(d)}}}for(let c=0;c<u.length;c++){let f=u[c],d=r[c];this._rank[f]=d,this._nodeAtRank[d]=f}}}});var nb,DE=M(()=>{"use strict";nb=class{static{l(this,"MaxHeap")}constructor(){this._items=[]}get size(){return this._items.length}isEmpty(){return this._items.length===0}push(e,t){let r=this._items;r.push({priority:e,value:t});let s=r.length-1;for(;s>0;){let o=s-1>>1;if(r[o].priority>=r[s].priority)break;let i=r[o];r[o]=r[s],r[s]=i,s=o}}pop(){let e=this._items,t=e.length;if(t===0)return;let r=e[0],s=e.pop();if(t>1){e[0]=s;let o=e.length,i=0;for(;;){let a=i*2+1,u=a+1,p=i;if(a<o&&e[a].priority>e[p].priority&&(p=a),u<o&&e[u].priority>e[p].priority&&(p=u),p===i)break;let c=e[p];e[p]=e[i],e[i]=c,i=p}}return r.value}}});function N6(n){let e=ne.get(n.opName);return!e||e.isConstant||e.isTerminator||e.isOpaque?!1:e.isReduction?!0:ni(n.opName)}var rb,PE=M(()=>{"use strict";fn();Ct();Re();Bu();K_();H_();ro();CE();DE();Nc();$a();nn();l(N6,"isFusibleOp");rb=class extends Ue{static{l(this,"PriorityFusionPass")}constructor(e={}){super("PriorityFusionPass"),this.requiredAnalyses=[ft];let t=e.target||{};this.maxReductions=e.maxReductions??1,this.legality=new bc({maxFusionSize:t.maxFusionSize||e.maxFusionSize,maxSharedMemory:t.sharedMemoryBytes||e.maxSharedMemory,libraryOps:t.libraryOps||e.libraryOps,allowReductionFusion:e.allowReductionFusion});let r=t.getAttr&&t.getAttr("fusionBenefitWeights")||e.benefitWeights;this.costModel=new fa({memoryBandwidthGBs:t.memoryBandwidthGBs,computeTFLOPs:t.computeTFLOPs,maxRegistersPerThread:t.registersPerThread,maxSharedMemory:t.sharedMemoryBytes,libraryOps:t.libraryOps,policy:t.getAttr?t.getAttr("fusionPolicy"):null,benefitWeights:r,...e.cost})}run(e,t){let s=(t?t.getAnalysis(ft,e):ft.compute(e)).topologicalOrder,o=s.length;if(o===0)return oe.UNCHANGED;let i=new Map;for(let T=0;T<o;T++)i.set(s[T],T);let a=[];for(let T=0;T<o;T++){let v=s[T];for(let A=0;A<v.numOperands;A++){let N=v.getOperand(A).definingOp;N&&i.has(N)&&a.push([i.get(N),T])}}let u=new tb(o,a),p=new Int32Array(o),c=new Map;for(let T=0;T<o;T++)if(N6(s[T])){let v=new wi(T);v.addOp(s[T]),c.set(T,v)}let f=new nb,d=l((T,v)=>{let A=new wi(-1);for(let N of T.ops)A.addOp(N);for(let N of v.ops)A.addOp(N);return A},"mergedGroup"),h=l(T=>{if(T.size<2||!T.allOpsInlineFusable())return!1;let v=0;for(let A of T.ops){let N=ne.get(A.opName);N&&N.isReduction&&v++}return v>this.maxReductions||(T.classifyKind(),T.kind===We.OPAQUE)?!1:this.costModel.shouldFuse(T).fuse===!0},"legalMerge"),m=l((T,v)=>{let A=0;for(let N=0;N<v.numOperands;N++){let k=v.getOperand(N);if(k.definingOp===T&&k.type instanceof I){let O=k.type.sizeInBytes();O!==X&&(A+=O)}}return A},"edgeBytes"),g=l((T,v)=>{let A=u.find(i.get(T)),N=u.find(i.get(v));if(A===N||!c.has(A)||!c.has(N)||!this.legality.canFuse(T,v).legal)return;let k=this.costModel.edgeBenefit(m(T,v));f.push(k,{a:A,b:N,va:p[A],vb:p[N],prodOp:T,consOp:v})},"pushCandidate"),y=l((T,v)=>{let A=new Set;for(let N of T.ops){for(let k=0;k<N.numResults;k++)for(let O of N.getResult(k).uses()){let R=i.get(O.user);if(R===void 0)continue;let B=u.find(R);if(B===v||!c.has(B))continue;let E=v*o+B;A.has(E)||(A.add(E),g(N,O.user))}for(let k=0;k<N.numOperands;k++){let O=N.getOperand(k).definingOp;if(!O)continue;let R=i.get(O);if(R===void 0)continue;let B=u.find(R);if(B===v||!c.has(B))continue;let E=B*o+v;A.has(E)||(A.add(E),g(O,N))}}},"reEval"),_=new Set;for(let T=0;T<o;T++){if(!c.has(T))continue;let v=s[T];for(let A=0;A<v.numOperands;A++){let N=v.getOperand(A).definingOp;if(!N)continue;let k=i.get(N);if(k===void 0||!c.has(k))continue;let O=k*o+T;_.has(O)||(_.add(O),g(N,v))}}let b=0;for(;!f.isEmpty();){let T=f.pop(),v=u.find(T.a),A=u.find(T.b);if(v===A||p[v]!==T.va||p[A]!==T.vb)continue;let N=c.get(v),k=c.get(A);if(!N||!k||!this.legality.canFuse(T.prodOp,T.consOp).legal)continue;let O=d(N,k);if(!h(O)||u.wouldCreateCycle(v,A))continue;let R=u.merge(v,A),B=N.size>=k.size?N:k,E=B===N?k:N;B.merge(E),c.delete(v),c.delete(A),c.set(R,B),p[R]=(p[v]>p[A]?p[v]:p[A])+1,b++,y(B,R)}if(b===0)return oe.UNCHANGED;let w=[],x=new Set;for(let T of c.values())x.has(T)||T.size<2||(x.add(T),w.push(T));let S=0;for(let T of w)T.classifyKind(),this._explain(T),kc(T,We.ELEMENTWISE)&&S++;return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,ops:o,merges:b,fusions:S,level:le.DEBUG}),S>0?oe.CHANGED:oe.UNCHANGED}_explain(e){if(!this.trace||!this.trace.explainsEnabled)return;let t=e.ops.map(r=>r.opName);this.trace.explain("fusion",t.join("+"),"fused",null,{groupSize:t.length,strategy:"priority"})}}});function A6(n,e){return n===e?!0:!n||!e?!1:typeof n.equals=="function"?n.equals(e):!1}var da,sb,$E=M(()=>{"use strict";Re();da=class{static{l(this,"LayoutPreference")}constructor(e,t,r=0){this.inputs=e,this.outputs=t,this.cost=r}},sb=class{static{l(this,"LayoutPolicy")}constructor(e){this.target=e,this._rules=new Map,this._initDefaultRules()}registerRule(e,t){this._rules.set(e,t)}getPreference(e){let t=this._rules.get(e.opName);return t?t(e,this.target):null}estimateConversionCost(e,t,r){if(!(r instanceof I)||A6(e,t))return 0;let s=r.numel();return s<0?1024:s*2}estimateBenefit(e,t,r){if(!(t instanceof I))return 0;let s=t.numel();if(s<0)return 0;let o=e.opName;if(o==="dot"||o==="conv"||o==="matmul")return s*4*r;if(o==="reduce")return s*2*r;let i=this.target.cacheLineBytes||64;return s*4<=i*4?0:Math.floor(s*.5)}_initDefaultRules(){this._rules.set("conv",(e,t)=>{let s=e.getOperand(0).type?.rank||4;if(t.preferredConvLayout)return new da([t.preferredConvLayout,null],[t.preferredConvLayout]);if(t.isGPU()&&s===4){let o=new Gt([0,2,3,1]);return new da([o,null],[o])}if(t.isCPU()&&s===4){let o=new Gt([0,2,3,1]);return new da([o,null],[o])}return null}),this._rules.set("dot",(e,t)=>{let r=e.getOperand(0).type,s=e.getOperand(1).type;if(!r||!s)return null;let o=Gt.rowMajor(r.rank);if(t.isCPU()&&s.rank===2){let a=Gt.columnMajor(s.rank);return new da([o,a],[o])}let i=Gt.rowMajor(s.rank);return new da([o,i],[o])}),this._rules.set("reduce",(e,t)=>{let r=e.getResult(0).type;return r?new da([null],[Gt.rowMajor(r.rank)]):null})}};l(A6,"layoutEquals")});function FE(n,e){let t=new Map,r=null,s=0;for(let i=0;i<n.numOperands;i++){let a=n.getOperand(i),u=e.get(a);if(!u)continue;let p=u.hash?u.hash():0,c=(t.get(p)||0)+1;t.set(p,c),c>s&&(s=c,r=u)}if(r)return r;let o=n.getResult(0);return o&&o.type instanceof I?Gt.rowMajor(o.type.rank):Gt.rowMajor(1)}function O6(n,e){return n===e?!0:!n||!e?!1:typeof n.equals=="function"?n.equals(e):!1}function zE(n){if(n instanceof Gt)return n;if(n&&typeof n.toLayout=="function")try{return n.toLayout()}catch{}return n&&n.baseOrder&&!n.isBlocked?.()?new Gt(n.baseOrder):n&&n.order?new Gt(n.order):null}var lv,ob,VE=M(()=>{"use strict";ro();Re();Ct();Bn();Ir();lv=class{static{l(this,"LayoutAnalysisResult")}constructor(e,t,r){this.assignments=e,this.conversions=t,this.totalCost=r}},ob=class{static get name(){return"layout"}static get depKey(){return"layout"}static get dependencies(){return[ft]}static compute(e,t,r){let s=t.useDef,o=new Map,i=s.topologicalOrder;for(let p of e.args)p.type instanceof I&&o.set(p,p.type.layout||Gt.rowMajor(p.type.rank));for(let p=0;p<i.length;p++){let c=i[p];if(Xt(c.opName))continue;let f=r?r.getPreference(c):null;if(f&&f.outputs.length>0){for(let m=0;m<c.numResults;m++){let g=c.getResult(m);if(!(g.type instanceof I))continue;let y=f.outputs[m]||null;y?o.set(g,y):o.set(g,FE(c,o))}continue}let d=ne.get(c.opName),h=d&&d.hasTrait(Y.ELEMENTWISE);for(let m=0;m<c.numResults;m++){let g=c.getResult(m);g.type instanceof I&&(h?o.set(g,FE(c,o)):o.set(g,Gt.rowMajor(g.type.rank)))}}let a=[],u=0;for(let p=0;p<i.length;p++){let c=i[p],f=r?r.getPreference(c):null;if(f)for(let d=0;d<c.numOperands;d++){let h=c.getOperand(d);if(!(h.type instanceof I))continue;let m=o.get(h);if(!m)continue;let g=f.inputs[d];if(!g||O6(m,g))continue;let y=zE(m),_=zE(g);!y||!_||y.equals(_)||(a.push({value:h,consumer:c,operandIdx:d,from:y,to:_}),r&&(u+=r.estimateConversionCost(y,_,h.type)))}}return new lv(o,a,u)}};l(FE,"resolveFromInputs");l(O6,"layoutEquals");l(zE,"toPlainLayout")});function B6(n,e,t){let r=n.id,s=e.hash?e.hash():0,o=t.hash?t.hash():0;return`${r}:${s}:${o}`}var ib,GE=M(()=>{"use strict";fn();yr();Re();$E();VE();ro();nn();ib=class extends Ue{static{l(this,"LayoutTransformPass")}constructor(e={}){super("LayoutTransformPass"),this.target=e.target||null,this._policy=null}run(e,t){if(!this.target)return oe.UNCHANGED;this._policy||(this._policy=new sb(this.target));let r=t?t.getAnalysis(ft,e):ft.compute(e),s=ob.compute(e,{useDef:r},this._policy);if(s.conversions.length===0)return oe.UNCHANGED;let o=new Map;for(let p of s.conversions){let{value:c,consumer:f,operandIdx:d,from:h,to:m}=p,g=B6(c,h,m),y=o.get(g);y||(y={value:c,from:h,to:m,consumers:[],cost:this._policy.estimateConversionCost(h,m,c.type),benefit:0},o.set(g,y)),y.consumers.push({consumer:f,operandIdx:d});let _=this.target.layoutAwareOps&&this.target.layoutAwareOps.has(f.opName);y.benefit+=_?this._policy.estimateBenefit(f,c.type,1):0}let i=0,a=0,u=[];for(let p of o.values())p.benefit<p.cost||(u.push(p),i+=p.cost,a+=p.benefit);if(u.length===0||i>a)return oe.UNCHANGED;for(let p of u){let c=p.from instanceof Gt?p.from.order:Array.from({length:p.value.type.rank},(y,_)=>_),f=p.to instanceof Gt?p.to.order:Array.from({length:p.value.type.rank},(y,_)=>_),d=new I(p.value.type.shape,p.value.type.dtype,p.to),h=new Me("layout_transform",[p.value],[d],{src_layout:[...c],dst_layout:[...f]}),m=p.value.definingOp;m&&m.parentBlock?m.parentBlock.insertAfter(h,m):p.consumers[0].consumer.parentBlock&&p.consumers[0].consumer.parentBlock.insertBefore(h,p.consumers[0].consumer);let g=h.getResult(0);for(let y of p.consumers)y.consumer.replaceOperand(y.operandIdx,g)}return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,conversions:s.conversions.length,uniqueTransforms:u.length,level:le.DEBUG}),oe.CHANGED}};l(B6,"valueLayoutKey")});function jE(n,e){if(n===e)return!0;if(typeof n=="number"&&typeof e=="number")return n===e;if(typeof n!=typeof e||n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(n[t]!==e[t])return!1;return!0}function qE(n){let e=0;for(let t=0;t<n.length;t++)e=(e<<5)-e+n.charCodeAt(t)&2147483647;return e}function I6(n){if(typeof n=="number"){let t=new Float64Array([n]),r=new Uint32Array(t.buffer);return(r[0]^r[1])&2147483647}let e=n.length;for(let t=0;t<Math.min(n.length,8);t++){let r=new Float64Array([n[t]]),s=new Uint32Array(r.buffer);e=(e^(s[0]^s[1]))*16777619&2147483647}return e}var Pr,R6,M6,vr,cv=M(()=>{"use strict";Re();Pr=Object.freeze({PER_TENSOR_SYMMETRIC:"per_tensor_symmetric",PER_TENSOR_ASYMMETRIC:"per_tensor_asymmetric",PER_CHANNEL:"per_channel",PER_GROUP:"per_group"}),R6=new Set(Object.values(Pr)),M6=new Set(["i8","ui8"]),vr=class n{static{l(this,"QuantizationParams")}constructor(e){this.scheme=e.scheme,this.scale=e.scale,this.zeroPoint=e.zeroPoint,this.axis=e.axis??null,this.groupSize=e.groupSize??null,this.dtype=e.dtype||"i8",this.numBits=e.numBits||fo(this.dtype)*8,this._hash=null}clampRange(){if(this.isSymmetric()){let r=2**(this.numBits-1)-1;return[-r,r]}if(this.dtype==="ui8")return[0,2**this.numBits-1];let e=-(2**(this.numBits-1)),t=2**(this.numBits-1)-1;return[e,t]}quantize(e){let[t,r]=this.clampRange(),s=Math.round(e/this.getScalarScale()+this.getScalarZeroPoint());return Math.max(t,Math.min(r,s))}dequantize(e){return(e-this.getScalarZeroPoint())*this.getScalarScale()}quantizeArray(e){let[t,r]=this.clampRange(),s=new Array(e.length);if(this.isPerChannel())throw new Error("Use quantizeArrayPerChannel(floatArr, shape) for per-channel quantization");let o=this.getScalarScale(),i=this.getScalarZeroPoint();for(let a=0;a<e.length;a++)s[a]=Math.max(t,Math.min(r,Math.round(e[a]/o+i)));return s}dequantizeArray(e){let t=new Array(e.length);if(this.isPerChannel())throw new Error("Use dequantizeArrayPerChannel(intArr, shape) for per-channel dequantization");let r=this.getScalarScale(),s=this.getScalarZeroPoint();for(let o=0;o<e.length;o++)t[o]=(e[o]-s)*r;return t}_channelStride(e){let t=1;for(let r=this.axis+1;r<e.length;r++)t*=e[r];return t}quantizeArrayPerChannel(e,t){let[r,s]=this.clampRange(),o=this._channelStride(t),i=t[this.axis],a=new Array(e.length);for(let u=0;u<e.length;u++){let p=Math.floor(u/o)%i,c=this.getScaleForChannel(p),f=this.getZeroPointForChannel(p);a[u]=Math.max(r,Math.min(s,Math.round(e[u]/c+f)))}return a}dequantizeArrayPerChannel(e,t){let r=this._channelStride(t),s=t[this.axis],o=new Array(e.length);for(let i=0;i<e.length;i++){let a=Math.floor(i/r)%s;o[i]=(e[i]-this.getZeroPointForChannel(a))*this.getScaleForChannel(a)}return o}quantizeArrayPerGroup(e){let[t,r]=this.clampRange(),s=new Array(e.length);for(let o=0;o<e.length;o++){let i=Math.floor(o/this.groupSize),a=this.scale[i],u=this.zeroPoint[i];s[o]=Math.max(t,Math.min(r,Math.round(e[o]/a+u)))}return s}dequantizeArrayPerGroup(e){let t=new Array(e.length);for(let r=0;r<e.length;r++){let s=Math.floor(r/this.groupSize);t[r]=(e[r]-this.zeroPoint[s])*this.scale[s]}return t}getScaleForGroup(e){return this.scale[e]}getZeroPointForGroup(e){return this.zeroPoint[e]}getScaleForChannel(e){return this.isPerChannel()?this.scale[e]:this.getScalarScale()}getZeroPointForChannel(e){return this.isPerChannel()?this.zeroPoint[e]:this.getScalarZeroPoint()}getScalarScale(){return typeof this.scale=="number"?this.scale:this.scale[0]}getScalarZeroPoint(){return typeof this.zeroPoint=="number"?this.zeroPoint:this.zeroPoint[0]}numChannels(){return this.isPerChannel()?typeof this.scale=="number"?1:this.scale.length:1}isPerChannel(){return this.scheme===Pr.PER_CHANNEL}isPerGroup(){return this.scheme===Pr.PER_GROUP}isSymmetric(){return this.scheme===Pr.PER_TENSOR_SYMMETRIC}equals(e){return this===e?!0:!(!(e instanceof n)||this.scheme!==e.scheme||this.dtype!==e.dtype||this.numBits!==e.numBits||this.axis!==e.axis||this.groupSize!==e.groupSize||!jE(this.scale,e.scale)||!jE(this.zeroPoint,e.zeroPoint))}hash(){if(this._hash!==null)return this._hash;let e=2166136261;return e=(e^qE(this.scheme))*16777619&2147483647,e=(e^qE(this.dtype))*16777619&2147483647,e=(e^this.numBits)*16777619&2147483647,e=(e^I6(this.scale))*16777619&2147483647,this._hash=e,e}serialize(){let e=this.isPerChannel()||this.isPerGroup();return{scheme:this.scheme,scale:e?[...this.scale]:this.scale,zeroPoint:e?[...this.zeroPoint]:this.zeroPoint,axis:this.axis,groupSize:this.groupSize,dtype:this.dtype,numBits:this.numBits}}static deserialize(e){return new n(e)}static fromRange(e,t,r,s="i8",o=8){if(r===Pr.PER_TENSOR_SYMMETRIC){let d=Math.max(Math.abs(e),Math.abs(t)),h=2**(o-1)-1,m=d/h||1e-10;return new n({scheme:r,scale:m,zeroPoint:0,dtype:s,numBits:o})}let[i,a]=s==="ui8"?[0,2**o-1]:[-(2**(o-1)),2**(o-1)-1],p=(t-e||1e-10)/(a-i),c=Math.round(i-e/p),f=Math.max(i,Math.min(a,c));return new n({scheme:r,scale:p,zeroPoint:f,dtype:s,numBits:o})}static fromRangePerChannel(e,t,r,s="i8",o=8){let i=e.length,a=new Float64Array(i),u=new Int32Array(i),p=2**(o-1)-1;for(let c=0;c<i;c++){let f=Math.max(Math.abs(e[c]),Math.abs(t[c]));a[c]=f/p||1e-10,u[c]=0}return new n({scheme:Pr.PER_CHANNEL,scale:a,zeroPoint:u,axis:r,dtype:s,numBits:o})}static defaultForActivation(e,t="i8",r=8){return n.fromRange(-6,6,e,t,r)}static fromConstantArray(e,t,r="i8",s=8){let o=1/0,i=-1/0;for(let a=0;a<e.length;a++)e[a]<o&&(o=e[a]),e[a]>i&&(i=e[a]);return isFinite(o)||(o=-1),isFinite(i)||(i=1),o===i&&(o-=.5,i+=.5),n.fromRange(o,i,t,r,s)}static fromConstantArrayPerChannel(e,t,r,s="i8",o=8){let i=t[r],a=1;for(let c=r+1;c<t.length;c++)a*=t[c];let u=new Array(i).fill(1/0),p=new Array(i).fill(-1/0);for(let c=0;c<e.length;c++){let f=Math.floor(c/a)%i,d=e[c];d<u[f]&&(u[f]=d),d>p[f]&&(p[f]=d)}for(let c=0;c<i;c++)isFinite(u[c])||(u[c]=-1),isFinite(p[c])||(p[c]=1),u[c]===p[c]&&(u[c]-=.5,p[c]+=.5);return n.fromRangePerChannel(u,p,r,s,o)}static fromConstantArrayPerGroup(e,t,r="i8",s=4){let o=Math.ceil(e.length/t),i=new Float64Array(o),a=new Int32Array(o),u=2**(s-1)-1;for(let p=0;p<o;p++){let c=p*t,f=Math.min(c+t,e.length),d=0;for(let h=c;h<f;h++){let m=Math.abs(e[h]);m>d&&(d=m)}i[p]=d/u||1e-10,a[p]=0}return new n({scheme:Pr.PER_GROUP,scale:i,zeroPoint:a,groupSize:t,dtype:r,numBits:s})}static isQuantizableDtype(e){return M6.has(e)}static isValidScheme(e){return R6.has(e)}};l(jE,"scaleEquals");l(qE,"hashStr");l(I6,"hashScaleValue")});function D6(n){for(let e=0;e<n.numOperands;e++){let t=n.getOperand(e).definingOp;if(t&&t.opName==="constant")return!0}return!1}function P6(n,e,t){for(let r=0;r<n.numOperands;r++){let s=n.getOperand(r);if(!e.has(s)&&(!(s.type instanceof I)||!jt(s.type.dtype)))return!1}return n.numOperands>0}var E6,L6,C6,ab,ub,UE=M(()=>{"use strict";fn();yr();Re();Ct();ro();cv();nn();Ir();E6=new Set(["softmax","sqrt","div","rsqrt","log","exp","tanh"]),L6=new Set(["dot","conv","add","mul","sub"]),C6=new Map([["dot","quantized_dot"],["conv","quantized_conv"]]);for(let[n,e]of C6)ne.has(n)&&ne.registerOpAttr(n,"quantizedVariant",e);ab=class{static{l(this,"QuantizationConfig")}constructor(e={}){this.scheme=e.scheme||Pr.PER_TENSOR_SYMMETRIC,this.calibration=e.calibration||null,this.targetDtype=e.targetDtype||"i8",this.excludeOps=e.excludeOps||E6,this.quantizableOps=e.quantizableOps||L6,this.sensitivityThreshold=e.sensitivityThreshold||0,this.sensitivityResult=e.sensitivityResult||null,this.weightOnly=e.weightOnly||!1,this.target=e.target||null}},ub=class extends Ue{static{l(this,"QuantizationPass")}constructor(e={}){super("QuantizationPass"),this.requiredAnalyses=[ft],this.config=e instanceof ab?e:new ab(e)}run(e,t){let s=(t?t.getAnalysis(ft,e):ft.compute(e)).topologicalOrder,o=new Set;this._paramsByValue=new Map;let i=this.config,a=!1;if(i.target&&!i.target.supportsInt8)return oe.UNCHANGED;for(let p=0;p<s.length;p++){let c=s[p];if(Xt(c.opName))continue;if(i.excludeOps.has(c.opName)||!i.quantizableOps.has(c.opName)){for(let h=0;h<c.numOperands;h++)o.has(c.getOperand(h))&&(a=this._insertDequantBefore(c,h,c.getOperand(h),i)||a);continue}if(i.sensitivityResult&&i.sensitivityThreshold>0&&i.sensitivityResult.isSensitive(c,i.sensitivityThreshold)||i.weightOnly&&!D6(c))continue;let f=ne.get(c.opName),d=f?f.getAttr("quantizedVariant"):null;if(d&&P6(c,o,i)){i.scheme===Pr.PER_CHANNEL&&this._canPerChannelDot(c,o)?a=this._replacePerChannelDot(c,i)||a:a=this._replaceWithNativeQuantized(c,d,o,i)||a;continue}a=this._insertDequantQuantBoundary(c,o,i)||a}let u=e.getReturnOp();if(u)for(let p=0;p<u.numOperands;p++){let c=u.getOperand(p);o.has(c)&&(a=this._insertDequantBefore(u,p,c,i)||a)}return this.trace&&this.trace.level>=le.DEBUG&&a&&this.trace.emit({type:"pass_detail",passName:this.name,opsProcessed:s.length,changed:a,level:le.DEBUG}),a?oe.CHANGED:oe.UNCHANGED}_getQuantParams(e,t){let r=fo(t.targetDtype)*8;if(t.calibration&&t.calibration.hasData(e))return t.calibration.getQuantParams(e,t.scheme,t.targetDtype);let s=e.definingOp;if(s&&s.opName==="constant"){let o=s.getAttr("value");if(typeof o=="number")return vr.fromRange(-Math.abs(o)||-1,Math.abs(o)||1,t.scheme,t.targetDtype,r);if(o&&typeof o.length=="number")return vr.fromConstantArray(o,t.scheme,t.targetDtype,r)}return e.type instanceof I&&jt(e.type.dtype)?vr.defaultForActivation(t.scheme,t.targetDtype,r):null}_resolveQuantParams(e,t){let r=this._paramsByValue.get(e);if(r)return r;let s=e.definingOp;return s&&s.opName==="quantize"?new vr({scheme:s.getAttr("scheme")||t.scheme,scale:s.getAttr("scale"),zeroPoint:s.getAttr("zero_point")||0,dtype:s.getAttr("target_dtype")||t.targetDtype}):this._getQuantParams(e,t)}_insertQuantizeAfter(e,t,r){let s=e.getResult(t),o=this._getQuantParams(s,r);if(!o)return null;let i=new I(s.type.shape,r.targetDtype),a=new Me("quantize",[s],[i],{scale:o.getScalarScale(),zero_point:o.getScalarZeroPoint(),scheme:r.scheme,target_dtype:r.targetDtype,...o.axis!==null?{axis:o.axis}:{}});e.parentBlock&&e.parentBlock.insertAfter(a,e);let u=a.getResult(0);return this._paramsByValue.set(u,o),u}_insertDequantBefore(e,t,r,s){let o=this._resolveQuantParams(r,s);if(!o)return!1;let i="f32",a=new I(r.type.shape,i),u=new Me("dequantize",[r],[a],{scale:o.getScalarScale(),zero_point:o.getScalarZeroPoint(),scheme:s.scheme,target_dtype:i});return e.parentBlock&&(e.parentBlock.insertBefore(u,e),e.replaceOperand(t,u.getResult(0))),!0}_insertDequantQuantBoundary(e,t,r){let s=!1;for(let o=0;o<e.numOperands;o++){let i=e.getOperand(o);t.has(i)&&(s=this._insertDequantBefore(e,o,i,r)||s)}for(let o=0;o<e.numResults;o++){let i=e.getResult(o);if(!(i.type instanceof I)||!jt(i.type.dtype))continue;let a=this._insertQuantizeAfter(e,o,r);if(a){let u=[...i.uses()];for(let p of u)p.user!==a.definingOp&&p.user.replaceOperand(p.operandIndex,a);t.add(a),s=!0}}return s}_replaceWithNativeQuantized(e,t,r,s){let o=[],i={};for(let[f,d]of e.attributes||[])i[f]=d;for(let f=0;f<e.numOperands;f++){let d=e.getOperand(f);if(r.has(d)){o.push(d);let _=this._resolveQuantParams(d,s);if(_){let b=f===0?"lhs":"rhs";if(t==="quantized_dot")i[`${b}_scale`]=_.getScalarScale(),i[`${b}_zero_point`]=_.getScalarZeroPoint();else{let w=f===0?"input":"kernel";i[`${w}_scale`]=_.getScalarScale(),i[`${w}_zero_point`]=_.getScalarZeroPoint()}}continue}let h=this._getQuantParams(d,s);if(!h)return!1;let m=new I(d.type.shape,s.targetDtype),g=new Me("quantize",[d],[m],{scale:h.getScalarScale(),zero_point:h.getScalarZeroPoint(),scheme:s.scheme,target_dtype:s.targetDtype});e.parentBlock&&e.parentBlock.insertBefore(g,e),o.push(g.getResult(0));let y=f===0?t==="quantized_dot"?"lhs":"input":t==="quantized_dot"?"rhs":"kernel";i[`${y}_scale`]=h.getScalarScale(),i[`${y}_zero_point`]=h.getScalarZeroPoint()}let a=i.lhs_scale||i.input_scale||1,u=i.rhs_scale||i.kernel_scale||1;i.output_scale=a*u,i.output_zero_point=0;let p=new I(e.getResult(0).type.shape,"i32"),c=new Me(t,o,[p],i);if(e.parentBlock){e.parentBlock.insertBefore(c,e);let f=new I(e.getResult(0).type.shape,e.getResult(0).type.dtype),d=i.output_scale||1,h=new Me("dequantize",[c.getResult(0)],[f],{scale:d,zero_point:i.output_zero_point||0,scheme:s.scheme,target_dtype:e.getResult(0).type.dtype});e.parentBlock.insertBefore(h,e),e.replaceAllResultsWith([h.getResult(0)]),e.erase()}return!0}_canPerChannelDot(e,t){if(e.opName!=="dot")return!1;let r=e.getOperand(0),s=e.getOperand(1);if(t.has(r)||t.has(s)||!(r.type instanceof I)||r.type.shape.length!==2||!(s.type instanceof I)||s.type.shape.length!==2)return!1;let o=s.definingOp;if(!o||o.opName!=="constant")return!1;let i=o.getAttr("value");if(!i||typeof i=="number"||typeof i.length!="number")return!1;let a=e.getAttr("rhs_contracting")||[],u=e.getAttr("lhs_contracting")||[];return!(a.length!==1||u.length!==1||(e.getAttr("rhs_batch")||[]).length!==0||(e.getAttr("lhs_batch")||[]).length!==0)}_activationParams(e,t){let r=fo(t.targetDtype)*8,s=Pr.PER_TENSOR_SYMMETRIC;return t.calibration&&t.calibration.hasData(e)?t.calibration.getQuantParams(e,s,t.targetDtype):vr.defaultForActivation(s,t.targetDtype,r)}_replacePerChannelDot(e,t){let r=e.getOperand(0),s=e.getOperand(1),o=s.type.shape,i=s.definingOp.getAttr("value"),a=fo(t.targetDtype)*8,p=e.getAttr("rhs_contracting")[0]===0?1:0,c=vr.fromConstantArrayPerChannel([...i],o,p,t.targetDtype,a),f=c.quantizeArrayPerChannel([...i],o),d=this._activationParams(r,t),h=d.getScalarScale(),m=d.getScalarZeroPoint(),g=e.parentBlock;if(!g)return!1;let y=new I(o,t.targetDtype),_=new Me("constant",[],[y],{value:f,tensor_type:y});g.insertBefore(_,e);let b=new I(r.type.shape,t.targetDtype),w=new Me("quantize",[r],[b],{scale:h,zero_point:m,scheme:Pr.PER_TENSOR_SYMMETRIC,target_dtype:t.targetDtype});g.insertBefore(w,e);let x=e.getResult(0).type.shape,S={};for(let[q,D]of e.attributes||[])S[q]=D;S.lhs_scale=h,S.lhs_zero_point=m,S.rhs_scale=1,S.rhs_zero_point=0,S.output_scale=1,S.output_zero_point=0;let T=new I(x,"i32"),v=new Me("quantized_dot",[w.getResult(0),_.getResult(0)],[T],S);g.insertBefore(v,e);let A=new I(x,"f32"),N=new Me("convert",[v.getResult(0)],[A],{target_dtype:"f32"});g.insertBefore(N,e);let k=o[p],O=new Array(k);for(let q=0;q<k;q++)O[q]=h*c.getScaleForChannel(q);let R=new Set(e.getAttr("lhs_contracting")||[]),B=0;for(let q=0;q<r.type.shape.length;q++)R.has(q)||B++;let E=B,F=new I([k],"f32"),C=new Me("constant",[],[F],{value:O,tensor_type:F});g.insertBefore(C,e);let H=new Me("broadcast_in_dim",[C.getResult(0)],[A],{broadcast_dimensions:[E],result_shape:x});g.insertBefore(H,e);let G=new Me("mul",[N.getResult(0),H.getResult(0)],[A],{});return g.insertBefore(G,e),e.replaceAllResultsWith([G.getResult(0)]),e.erase(),!0}};l(D6,"hasConstantOperand");l(P6,"allOperandsCanQuantize")});var pv,lb,WE=M(()=>{"use strict";ro();Re();pv=class{static{l(this,"LivenessResult")}constructor(e,t,r,s,o,i,a){this.liveIn=e,this.liveOut=t,this.intervals=r,this.opIndex=s,this.peakPressure=o,this.peakOp=i,this.pressureAtOp=a}interfere(e,t){if(e===t)return!0;let r=this.intervals.get(e),s=this.intervals.get(t);return r&&s?r.start<=s.end&&s.start<=r.end:!1}liveAtOp(e){return this.liveIn.get(e)||new Set}intervalOf(e){return this.intervals.get(e)||null}},lb=class n{static get name(){return"liveness"}static get depKey(){return"liveness"}static get dependencies(){return[ft]}static buildIntervals(e,t){let r=new Map;for(let o=0;o<t.length;o++)r.set(t[o],o);let s=new Map;for(let o of e.args)s.set(o,{start:-1,end:-1});for(let o=0;o<t.length;o++){let i=t[o];for(let a=0;a<i.numResults;a++)s.set(i.getResult(a),{start:o,end:o})}for(let o=0;o<t.length;o++){let i=t[o];for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a),p=s.get(u);p&&p.end<o&&(p.end=o)}}return{intervals:s,opIndex:r}}static compute(e,t={}){let r=t.useDef||ft.compute(e),s=r.topologicalOrder,{intervals:o,opIndex:i}=n.buildIntervals(e,s),a=new Map,u=new Map;for(let d of s)a.set(d,new Set),u.set(d,new Set);for(let d=s.length-1;d>=0;d--){let h=s[d],m=u.get(h),g=r.opUsers.get(h);if(g)for(let _ of g){let b=a.get(_);if(b)for(let w of b)m.add(w)}let y=a.get(h);for(let _ of m)y.add(_);for(let _=0;_<h.numResults;_++)y.delete(h.getResult(_));for(let _=0;_<h.numOperands;_++)y.add(h.getOperand(_))}for(let[d,h]of i){let m=a.get(d);for(let g of m){let y=o.get(g);y&&y.end<h&&(y.end=h)}}let p=0,c=null,f=new Map;for(let d=0;d<s.length;d++){let h=s[d],m=a.get(h),g=0;for(let y of m)if(y.type instanceof I){let _=y.type.sizeInBytes();_!==X&&(g+=_)}f.set(h,g),g>p&&(p=g,c=h)}return new pv(a,u,o,i,p,c,f)}}});var cb,pb,KE=M(()=>{"use strict";fn();yr();Re();Ct();nn();ro();WE();Ir();cb=class{static{l(this,"RematerializationConfig")}constructor(e={}){this.memoryBudget=e.memoryBudget||1/0,this.maxIterations=e.maxIterations||100,this.maxRecomputeCost=e.maxRecomputeCost||1/0,this.excludeOps=e.excludeOps||new Set}},pb=class extends Ue{static{l(this,"RematerializationPass")}constructor(e={}){super("RematerializationPass"),this.config=e instanceof cb?e:new cb(e)}run(e,t){if(this.config.memoryBudget===1/0)return oe.UNCHANGED;let r=!1,s=0,o=0;for(;s<this.config.maxIterations;){let i=ft.compute(e),{peakPressure:a,candidates:u,opIndex:p}=this._analyzeIntervalPressure(e,i);if(o=a,a<=this.config.memoryBudget||u.length===0)break;u.sort((c,f)=>f.score-c.score),this._rematerialize(e,u[0],p),r=!0,s++}return this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,iterations:s,peakPressure:o,budget:this.config.memoryBudget,changed:r,level:le.DEBUG}),r?oe.CHANGED:oe.UNCHANGED}_analyzeIntervalPressure(e,t){let r=t.topologicalOrder,{intervals:s,opIndex:o}=lb.buildIntervals(e,r),i=[];for(let[m,g]of s){if(!(m.type instanceof I))continue;let y=m.type.sizeInBytes();y===X||y<=0||(i.push({idx:g.start,delta:y,value:m}),i.push({idx:g.end+1,delta:-y,value:m}))}i.sort((m,g)=>m.idx-g.idx||m.delta-g.delta);let a=0,u=0,p=0,c=new Set,f=new Set,d=0;for(let m=-1;m<=r.length;m++){for(;d<i.length&&i[d].idx<=m;)a+=i[d].delta,i[d].delta>0&&i[d].value&&f.add(i[d].value),i[d].delta<0&&i[d].value&&f.delete(i[d].value),d++;if(a>u){u=a,p=m,c.clear();for(let g of f)c.add(g)}}let h=[];for(let m of c){if(!this._canRematerialize(m))continue;let g=m.definingOp,y=this._computeMemorySaved(m);if(y<=0)continue;let _=this._estimateRecomputeCost(g);_>=this.config.maxRecomputeCost||_!==0&&h.push({value:m,definingOp:g,memorySaved:y,recomputeCost:_,score:y/_})}return{peakPressure:u,peakIdx:p,candidates:h,opIndex:o}}_canRematerialize(e){if(e.isBlockArgument())return!1;let t=e.definingOp;if(!t||Xt(t.opName)||_n(t.opName)||this.config.excludeOps.has(t.opName)||t.regions.length>0||t.hasSideEffects()||e.useCount<=1)return!1;for(let r=0;r<t.numOperands;r++){let s=t.getOperand(r);if(s.definingOp&&s.definingOp.hasSideEffects())return!1}return!0}_computeMemorySaved(e){if(!(e.type instanceof I))return 0;let t=e.type.sizeInBytes();return t===X?0:t}_operandExtensionCost(e,t,r){let s=0;for(let o=0;o<e.numOperands;o++){let i=e.getOperand(o),a=0;for(let u of i.uses()){let p=r.get(u.user)||0;p>a&&(a=p)}if(t>a&&i.type instanceof I){let u=i.type.sizeInBytes();u!==X&&(s+=u)}}return s}_estimateRecomputeCost(e){let t=ne.get(e.opName);if(!t||t.isOpaque)return 1/0;if(t.getFlops){let r=t.getFlops(e);if(r>0)return r}if(t.isElementwise||t.isBroadcast||t.isView){let r=0;for(let s=0;s<e.numResults;s++){let o=e.getResult(s).type;if(o instanceof I){let i=o.numel();if(i!==X){r=i;break}}}return r||1}if(t.isReduction)for(let r=0;r<e.numOperands;r++){let s=e.getOperand(r).type;if(s instanceof I){let o=s.numel();if(o!==X)return o}}return 1/0}_rematerialize(e,t,r){let{value:s,definingOp:o}=t,i=[];for(let c of s.uses())i.push({user:c.user,operandIndex:c.operandIndex});if(i.length<=1)return;i.sort((c,f)=>(r.get(c.user)||0)-(r.get(f.user)||0));let a=r.get(i[i.length-1].user)||0,u=this._operandExtensionCost(o,a,r),p=this._computeMemorySaved(s);if(!(u>=p))for(let c=1;c<i.length;c++){let f=i[c],d=f.user;if(!d.parentBlock)continue;let h=new Me(o.opName,[...o.operands],o.results.map(m=>m.type),new Map(o.attributes));d.parentBlock.insertBefore(h,d),d.replaceOperand(f.operandIndex,h.getResult(s.resultIndex))}}}});function $o(n){return _n(n.opName)}function HE(n){let e=0;for(let t=0;t<n.numResults;t++){let r=n.getResult(t).type;if(!r||!r.isFullyStatic)continue;let s=r.sizeInBytes();s>e&&(e=s)}return e}function F6(n){let e=n.definingOp;if(e&&e.opName==="broadcast"){let t=e.getOperand(0);e=t&&t.definingOp}if(e&&$o(e)){let t=e.getAttr("value");if(typeof t=="number")return t}return 0}function fv(n){if(n.opName!=="dot"&&n.opName!=="cublas_gemm")return null;let e=n.getOperand(0).type,t=n.getOperand(1).type,r=n.getResult(0).type;if(!e||!t||!r||e.dtype!=="f32"||t.dtype!=="f32"||r.dtype!=="f32")return null;let s=n.getAttr("lhs_batch")||[],o=n.getAttr("rhs_batch")||[];if(s.length>0||o.length>0)return null;let i=n.getAttr("lhs_contracting")||[],a=n.getAttr("rhs_contracting")||[];if(i.length!==1||a.length!==1||t.rank!==2||i[0]!==e.rank-1||a[0]!==0&&a[0]!==1||!e.isFullyStatic||!t.isFullyStatic||!r.isFullyStatic)return null;let u=n.getOperand(0).definingOp,p=n.getOperand(1).definingOp;if(u&&$o(u)||p&&$o(p))return null;let c=a[0]===1,f=e.shape[e.rank-1];if((c?t.shape[1]:t.shape[0])!==f)return null;let h=1;for(let g=0;g<e.rank-1;g++)h*=e.shape[g];let m=c?t.shape[0]:t.shape[1];return h<=0||m<=0||f<=0?null:{M:h,N:m,K:f,transB:c}}function YE(n){return{labelOf:l(e=>n.get(e),"labelOf"),canMerge:l((e,t)=>Math.max(e.maxBuf||0,HE(t))<=$6,"canMerge"),onAttach:l((e,t)=>{e.maxBuf=Math.max(e.maxBuf||0,HE(t))},"onAttach")}}function fb(n,e,t){let r=n.opSet,s=Ru(n.ops),{inputs:o,outputs:i,constDefs:a}=pa(r,s,{pullConstants:!0,isConstant:$o});for(let d of o)if(!d.type||!d.type.isFullyStatic)return null;for(let d of i)if(!d.type||!d.type.isFullyStatic)return null;let u=new yn(e,o.map(d=>d.type),i.map(d=>d.type)),p=new Map;for(let d=0;d<o.length;d++)p.set(o[d],u.args[d]);for(let d of a)u.entryBlock.pushOp(d.clone(p));for(let d of s)u.entryBlock.pushOp(d.clone(p));let c=i.map(d=>p.get(d));if(c.some(d=>d===void 0))return null;u.entryBlock.pushOp(new Me("return",c,[]));let f=n.ops.length===1&&t.has(n.ops[0])?n.ops[0]:null;return{part:n,subFunc:u,inputs:o,outputs:i,dotOp:f}}function Od(n){if(z6.has(n.opName))return!0;if(n.regions)for(let e of n.regions){let t=e.entryBlock;if(t){for(let r of t.ops())if(Od(r))return!0}}return!1}function V6(n){let e=0;for(let t=0;t<n.numResults;t++){let r=n.getResult(t).type;if(!r||!r.isFullyStatic)continue;let s=1;for(let o of r.shape)s*=o;s>e&&(e=s)}return e}function JE(n,e=256){if(n.functionCount!==1)return!1;let t=n.functions().next().value,r=new Map,s=l(o=>{if(!o)return 0;let i=r.get(o);if(i!==void 0)return i;r.set(o,0);let a=Od(o)?V6(o):0;for(let u=0;u<o.numOperands;u++){let p=s(o.getOperand(u).definingOp);p>a&&(a=p)}return r.set(o,a),a},"maxBoundaryInSubtree");for(let o of t.ops())if(!(Xt(o.opName)||!Od(o))){for(let i=0;i<o.numOperands;i++)if(s(o.getOperand(i).definingOp)>e)return!0}return!1}function ZE(n,e,t){let r=new Map,s=0,o=l(h=>{let m=r.get(h);return m===void 0&&(m=s++,r.set(h,m)),m},"getSlot");for(let h of n.args)o(h);for(let h of t)for(let m of h.outputs)o(m);let i=[];for(let h of n.args)i.push(o(h));let a=[],u=new Set;for(let h=0;h<e.numOperands;h++){let m=e.getOperand(h),g=i.length,y=m.isBlockArgument&&m.isBlockArgument(),_=m.definingOp&&$o(m.definingOp);if(!y&&!_&&r.has(m)){let b=r.get(m);if(!u.has(b)){u.add(b),i.push(b);continue}i.push(s++),a.push({pos:g,kind:"copy",srcSlot:b});continue}if(y){i.push(s++),a.push({pos:g,kind:"copy",srcSlot:o(m)});continue}if(_){i.push(s++),a.push({pos:g,kind:"const",value:F6(m)});continue}return null}let p=[];for(let h of t){let m=[];for(let y of h.inputs){let _=r.get(y);if(_===void 0)return null;m.push(_)}let g=h.outputs.map(y=>r.get(y));p.push({name:h.subFunc.name,inputSlots:m,outputSlots:g})}let c=new Set(i),f=[],d=new Set;for(let[h,m]of r)if(!(c.has(m)||d.has(m))){if(d.add(m),!h.type||!h.type.isFullyStatic)return null;f.push({slot:m,shape:[...h.type.shape],dtype:h.type.dtype})}return{plan:{numSlots:s,argSlots:i,intermediates:f,steps:p,returnFixups:a}}}function ha(n,e=2){if(n.functionCount!==1)return null;let t=n.functions().next().value,r=t.getReturnOp();if(!r)return null;let s=[],o=new Map,i=0;for(let g of t.ops())Xt(g.opName)||$o(g)||(Od(g)?o.set(g,"boundary#"+i++):o.set(g,"native"),s.push(g));if(i<e||s.length===0)return null;let{partitions:a,preds:u}=kd(s,YE(o));if(a.length<2)return null;let p=ov(a,u);if(!p)return null;let c=t.name,f=[],d=new Map,h=0;for(let g of p){let y=fb(g,c+"_p"+h++,d);if(!y)return null;f.push(y)}let m=ZE(t,r,f);if(!m)return null;n.removeFunction(t.name);for(let g of f)n.addFunction(g.subFunc);return{plan:m.plan}}function QE(n){if(n.functionCount!==1)return null;let e=n.functions().next().value,t=e.getReturnOp();if(!t)return null;let r=[],s=new Map,o=new Map,i=0,a=0;for(let y of e.ops()){if(Xt(y.opName)||$o(y))continue;let _=fv(y);_?(s.set(y,"cublas#"+i),o.set(y,_),i++):Od(y)?s.set(y,"boundary#"+a++):s.set(y,"native"),r.push(y)}if(i+a===0||r.length===0)return null;let{partitions:u,preds:p}=kd(r,YE(s));if(u.length<2)return null;let c=ov(u,p);if(!c)return null;let f=e.name,d=[],h=0;for(let y of c){let _=fb(y,f+"_p"+h++,o);if(!_)return null;d.push(_)}let m=ZE(e,t,d);if(!m)return null;let g=new Map;for(let y of d){if(!y.dotOp)continue;let _=o.get(y.dotOp),b=y.inputs.indexOf(y.dotOp.getOperand(0)),w=y.inputs.indexOf(y.dotOp.getOperand(1)),x=y.outputs.indexOf(y.dotOp.getResult(0));b<0||w<0||x<0||g.set(y.subFunc.name,{M:_.M,N:_.N,K:_.K,transB:_.transB,aIdx:b,bIdx:w,cIdx:y.inputs.length+x})}if(g.size===0)return null;n.removeFunction(e.name);for(let y of d)n.addFunction(y.subFunc);return{plan:m.plan,cublasInfos:g}}var $6,z6,db=M(()=>{"use strict";qs();yr();Nd();Ir();l($o,"isConstantOp");$6=32*1024;l(HE,"maxResultBytes");l(F6,"constScalarOf");l(fv,"cublasDotInfo");l(YE,"bufferLimitedConfig");l(fb,"materializePartition");z6=new Set(["dot","fused_dot_epilogue","cublas_gemm","reduce","conv","quantized_conv","scaled_dot_product_attention"]);l(Od,"containsBoundaryOp");l(V6,"maxBoundaryResultBytes");l(JE,"hasDependentBoundaries");l(ZE,"buildExecutionPlan");l(ha,"splitGraphForNative");l(QE,"splitGraphForCublas")});function eL(n){return XE.push(n),n}var XE,hb,tL=M(()=>{"use strict";fn();XE=[];l(eL,"registerBackendOpRewrite");hb=class extends Ue{static{l(this,"BackendOpRewritePass")}constructor(e={}){super(e.name||"BackendOpRewritePass"),this.config=e,this.rewrites=e.rewrites||XE}run(e){let t=!1;for(let r of[...e.ops()]){let s=r.parentBlock;if(s){for(let o of this.rewrites)if(o.match(r,this.config)){o.build(r,s,this.config),t=!0;break}}}return t?oe.CHANGED:oe.UNCHANGED}}});var G6,mb,nL=M(()=>{"use strict";yr();db();tL();G6=eL({name:"dot->cublas_gemm",match:l(n=>n.opName==="dot"&&n.numOperands===2&&fv(n)!==null,"match"),build:l((n,e)=>{let t=n.results.map(s=>s.type),r=new Me("cublas_gemm",[n.getOperand(0),n.getOperand(1)],t,new Map(n.attributes),null);e.insertBefore(r,n),n.replaceAllResultsWith(r.results),n.erase()},"build")}),mb=class extends hb{static{l(this,"CublasRewritePass")}constructor(e={}){super({...e,name:"CublasRewritePass",rewrites:[G6]})}}});function rL(n,e,{cudaMatmulChain:t=!1,context:r=null}={}){let s=r?a=>r.passesForPhase(a,n,e):a=>F_(a,n,e),o=[];for(let a of s("pre"))o.push(a);if(o.push(new ql(e)),o.push(new jl("canonicalize",[new au,new j_({fastMath:n.optimization.fastMath}),new q_,new U_,new Co],n.optimization.maxSimplifyIterations)),n.optimization.layout&&e&&(o.push(new ib({target:e})),o.push(new Co)),n.quantization.enabled&&(o.push(new ub({...n.quantization,target:e})),o.push(new au),o.push(new Co)),n.fusion.enabled&&n.matmulBackend!=="cublas"&&e&&e.enableEpilogueFusion&&(o.push(new Z_({target:e})),o.push(new Co)),n.fusion.enabled){let a=n.fusion,u=a.launchOverheadUs??j6;a.strategy==="dominator"?o.push(new eb({target:e,...a})):a.strategy==="priority"?(o.push(new rb({target:e,cost:{launchOverheadUs:u},...a})),o.push(new Ad({maxFusionSize:e?.maxFusionSize,...a}))):(o.push(new Y_({target:e,cost:{launchOverheadUs:u},...a})),o.push(new Q_({maxFusionSize:e?.maxFusionSize,...a})),o.push(new Ad({maxFusionSize:e?.maxFusionSize,...a}))),o.push(new Co)}if(n.matmulBackend==="cublas"&&o.push(new mb),n.optimization.rematerialization){let a={...n.optimization.rematConfig};a.memoryBudget===void 0&&e&&e.memoryBudgetBytes>0&&(a.memoryBudget=e.memoryBudgetBytes),o.push(new pb(a))}for(let a of s("post"))o.push(a);return o}var j6,sL=M(()=>{"use strict";_y();Mx();TE();vE();kE();Ex();AE();BE();RE();ME();LE();PE();GE();UE();Bx();KE();nL();ev();j6=5;l(rL,"buildGraphPipeline")});var ar,ma=M(()=>{"use strict";ar=class{static{l(this,"PrimFuncPass")}constructor(e,t=null){this.name=e,this.phase=t||e,this.snapshotPoint=null,this.trace=null}begin(e){}run(e,t){throw new Error("PrimFuncPass.run not implemented")}end(e){}}});var gb,dv,Mu,hv=M(()=>{"use strict";Fe();gb=["iterCount","depth","parallelLoops","vectorizedLoops","unrolledLoops","threadBoundLoops","serialLoops","threadBlockSize","gridSize","underReduction","numMathOps","numExternCalls","numReads","numWrites","stride1Accesses","stridedAccesses","reuseCount","touchedBytes","arithmeticIntensity","vectorized","parallelized","innermostExtent"],dv=class{static{l(this,"ScheduleFeatures")}constructor(e){this.numLoops=e.numLoops||0,this.numBlocks=e.numBlocks||0,this.totalIterations=e.totalIterations||0,this.maxLoopDepth=e.maxLoopDepth||0,this.numParallelLoops=e.numParallelLoops||0,this.numVectorizedLoops=e.numVectorizedLoops||0,this.numUnrolledLoops=e.numUnrolledLoops||0,this.numThreadBound=e.numThreadBound||0,this.numSerialLoops=e.numSerialLoops||0,this.totalBufferBytes=e.totalBufferBytes||0,this.numBufferReads=e.numBufferReads||0,this.numBufferWrites=e.numBufferWrites||0,this.numMathOps=e.numMathOps||0,this.numExternCalls=e.numExternCalls||0,this.arithmeticIntensity=e.arithmeticIntensity||0,this.innermostExtent=e.innermostExtent||0,this.outermostExtent=e.outermostExtent||0,this.hasReduction=e.hasReduction||!1,this.reductionDepth=e.reductionDepth||0,this.threadBlockSize=e.threadBlockSize||0,this.gridSize=e.gridSize||0,this.strideOneAccesses=e.strideOneAccesses||0,this.nonStrideOneAccesses=e.nonStrideOneAccesses||0}},Mu=class n{static{l(this,"FeatureExtractor")}static extract(e){let t={numLoops:0,numBlocks:0,totalIterations:1,maxLoopDepth:0,currentDepth:0,numParallelLoops:0,numVectorizedLoops:0,numUnrolledLoops:0,numThreadBound:0,numSerialLoops:0,totalBufferBytes:0,buffersSeen:new Set,numBufferReads:0,numBufferWrites:0,numMathOps:0,numExternCalls:0,hasReduction:!1,reductionDepth:0,threadBlockSize:1,gridSize:1,innermostExtent:0,outermostExtent:0,loopExtents:[],strideOneAccesses:0,nonStrideOneAccesses:0};n._visitIterative(e.body,t);let r=t.totalBufferBytes,s=t.numMathOps+t.numExternCalls;return t.arithmeticIntensity=r>0?s/r:0,new dv(t)}static extractStatements(e){let t=[],r=[],s=0,o=[{node:e.body,action:"enter"}];for(;o.length>0;){let{node:i,action:a}=o.pop();if(i){if(a==="leaveFor"){r.pop();continue}if(a==="leaveBlock"){s--;continue}switch(i.type){case"ForNode":r.push(i),o.push({node:null,action:"leaveFor"}),o.push({node:i.body,action:"enter"});break;case"BlockNode":i.initBody&&(s++,o.push({node:null,action:"leaveBlock"}),o.push({node:i.initBody,action:"enter"})),o.push({node:i.body,action:"enter"});break;case"SeqNode":for(let u=i.stmts.length-1;u>=0;u--)o.push({node:i.stmts[u],action:"enter"});break;case"AllocateNode":case"LetStmtNode":o.push({node:i.body,action:"enter"});break;case"IfThenElseNode":i.elseBody&&o.push({node:i.elseBody,action:"enter"}),o.push({node:i.thenBody,action:"enter"});break;case"BufferStoreNode":t.push(n._statementVector(i,r,s));break}}}return t}static _statementVector(e,t,r){let s=1,o=0,i=0,a=0,u=0,p=0,c=1,f=1;for(let T of t){let v=T.extent&&T.extent.type==="IntImmNode"?T.extent.value:1;switch(s*=v,T.kind){case z.PARALLEL:o++;break;case z.VECTORIZED:i++;break;case z.UNROLLED:a++;break;case z.THREAD_BINDING:u++,T.threadTag&&T.threadTag.startsWith("threadIdx")?c*=v:T.threadTag&&T.threadTag.startsWith("blockIdx")&&(f*=v);break;default:p++;break}}let d={math:0,extern:0};n._countExpr(e.value,d);let h=t.map(T=>T.loopVar.name),m=[{buffer:e.buffer,indices:e.indices}];n._collectLoads(e.value,m);let g=0,y=0,_=0,b=0;for(let T of m){let v=T.buffer&&T.buffer.sizeInBytes?T.buffer.sizeInBytes():0;v>0&&(b+=v);let A=T.indices&&T.indices.length>0?T.indices[T.indices.length-1]:null;A&&A.type==="VariableNode"?g++:y++;let N=new Set;if(T.indices)for(let k of T.indices)n._collectVars(k,N);for(let k of h)N.has(k)||_++}let w=t.length>0?t[t.length-1]:null,x=w&&w.extent&&w.extent.type==="IntImmNode"?w.extent.value:0,S={iterCount:s,depth:t.length,parallelLoops:o,vectorizedLoops:i,unrolledLoops:a,threadBoundLoops:u,serialLoops:p,threadBlockSize:c,gridSize:f,underReduction:r>0?1:0,numMathOps:d.math,numExternCalls:d.extern,numReads:m.length-1,numWrites:1,stride1Accesses:g,stridedAccesses:y,reuseCount:_,touchedBytes:b,arithmeticIntensity:b>0?(d.math+d.extern)/b:0,vectorized:i>0?1:0,parallelized:o+u>0?1:0,innermostExtent:x};return gb.map(T=>S[T]||0)}static _countExpr(e,t){if(!(!e||typeof e!="object"))switch(e.type){case"MathOpNode":t.math++,n._countExpr(e.a,t),e.b&&n._countExpr(e.b,t);break;case"CompareNode":t.math++,n._countExpr(e.a,t),n._countExpr(e.b,t);break;case"CallExternNode":t.extern++;for(let r of e.args)n._countExpr(r,t);break;default:break}}static _collectLoads(e,t){if(!(!e||typeof e!="object")){if(e.type==="BufferLoadNode"){t.push({buffer:e.buffer,indices:e.indices});return}if(e.a&&n._collectLoads(e.a,t),e.b&&n._collectLoads(e.b,t),e.args)for(let r of e.args)n._collectLoads(r,t)}}static _collectVars(e,t){if(!(!e||typeof e!="object")){if(e.type==="VariableNode"){t.add(e.name);return}if(e.a&&n._collectVars(e.a,t),e.b&&n._collectVars(e.b,t),e.args)for(let r of e.args)n._collectVars(r,t);if(e.indices)for(let r of e.indices)n._collectVars(r,t)}}static _visitIterative(e,t){let r=[{node:e,action:"enter"}];for(;r.length>0;){let{node:s,action:o}=r.pop();if(s){if(o==="leave_for"){t.currentDepth--;continue}switch(s.type){case"ForNode":{t.numLoops++,t.currentDepth++,t.currentDepth>t.maxLoopDepth&&(t.maxLoopDepth=t.currentDepth);let i=s.extent.type==="IntImmNode"?s.extent.value:1;switch(t.loopExtents.push(i),t.numLoops===1&&(t.outermostExtent=i),t.innermostExtent=i,s.kind){case z.PARALLEL:t.numParallelLoops++;break;case z.VECTORIZED:t.numVectorizedLoops++;break;case z.UNROLLED:t.numUnrolledLoops++;break;case z.THREAD_BINDING:t.numThreadBound++,s.threadTag&&s.threadTag.startsWith("threadIdx")?t.threadBlockSize*=i:s.threadTag&&s.threadTag.startsWith("blockIdx")&&(t.gridSize*=i);break;default:t.numSerialLoops++;break}t.totalIterations*=i,r.push({node:null,action:"leave_for"}),r.push({node:s.body,action:"enter"});break}case"BlockNode":t.numBlocks++,s.initBody&&(t.hasReduction=!0,t.reductionDepth=t.currentDepth);for(let i of s.reads)n._visitBuffer(i.buffer,t);for(let i of s.writes)n._visitBuffer(i.buffer,t);r.push({node:s.body,action:"enter"}),s.initBody&&r.push({node:s.initBody,action:"enter"});break;case"SeqNode":for(let i=s.stmts.length-1;i>=0;i--)r.push({node:s.stmts[i],action:"enter"});break;case"AllocateNode":n._visitBuffer(s.buffer,t),r.push({node:s.body,action:"enter"});break;case"IfThenElseNode":s.elseBody&&r.push({node:s.elseBody,action:"enter"}),r.push({node:s.thenBody,action:"enter"});break;case"LetStmtNode":r.push({node:s.body,action:"enter"});break;case"BufferStoreNode":t.numBufferWrites++,n._visitBuffer(s.buffer,t),n._checkStride(s.buffer,s.indices,t),n._visitExpr(s.value,t);break}}}}static _visitBuffer(e,t){if(!e||t.buffersSeen.has(e))return;t.buffersSeen.add(e);let r=e.sizeInBytes();r>0&&(t.totalBufferBytes+=r)}static _visitExpr(e,t){if(e)switch(e.type){case"MathOpNode":t.numMathOps++,n._visitExpr(e.a,t),e.b&&n._visitExpr(e.b,t);break;case"CallExternNode":t.numExternCalls++;for(let r of e.args)n._visitExpr(r,t);break;case"BufferLoadNode":t.numBufferReads++,n._checkStride(e.buffer,e.indices,t);break;case"CompareNode":t.numMathOps++,n._visitExpr(e.a,t),n._visitExpr(e.b,t);break;default:break}}static _checkStride(e,t,r){if(!e||!t||t.length===0)return;let s=t[t.length-1];for(;s&&s.type==="MathOpNode"&&s.b&&s.b.type==="IntImmNode";)if(s.op==="+"&&s.b.value===0)s=s.a;else if(s.op==="*"&&s.b.value===1)s=s.a;else break;s&&s.type==="VariableNode"?r.strideOneAccesses++:r.nonStrideOneAccesses++}}});function oL(n,e){if(n.length===0)return 0;let t=0;for(let r of n)t+=e[r];return t/n.length}function mv(n,e,t,r,s,o,i){let a=t.length;if(r>=s||a<o*2)return{leaf:oL(t,e)};let u=n[0].length,p=0,c=0,f=new Set(t);for(let g of t){let y=e[g];p+=y,c+=y*y}let d=null,h=null;for(let g=0;g<u;g++){let y=i[g].filter(w=>f.has(w)),_=0,b=0;for(let w=0;w<y.length-1;w++){let x=e[y[w]];_+=x,b+=x*x;let S=n[y[w]][g],T=n[y[w+1]][g];if(S===T)continue;let v=w+1,A=a-v;if(v<o||A<o)continue;let N=p-_,k=c-b,O=b-_*_/v+(k-N*N/A);(!d||O<d.cost)&&(d={f:g,thr:(S+T)/2,sIdx:w,cost:O},h=y)}}if(!d)return{leaf:oL(t,e)};let m=h;return{f:d.f,thr:d.thr,left:mv(n,e,m.slice(0,d.sIdx+1),r+1,s,o,i),right:mv(n,e,m.slice(d.sIdx+1),r+1,s,o,i)}}function q6(n){let e=n.length,t=e>0?n[0].length:0,r=new Array(t),s=[];for(let o=0;o<e;o++)s.push(o);for(let o=0;o<t;o++)r[o]=s.slice().sort((i,a)=>n[i][o]-n[a][o]);return r}function iL(n,e){for(;n&&n.leaf===void 0;)n=e[n.f]<=n.thr?n.left:n.right;return n?n.leaf:0}var Bd,aL=M(()=>{"use strict";l(oL,"leafValue");l(mv,"buildTree");l(q6,"buildFeatureOrder");l(iL,"predictTree");Bd=class n{static{l(this,"GradientBoostedTrees")}constructor(e={}){this.numTrees=e.numTrees??60,this.maxDepth=e.maxDepth??3,this.lr=e.lr??.1,this.minSamples=e.minSamples??1,this.trees=null,this.base=0}fit(e,t){let r=e.length;if(r===0)return;let s=0;for(let p of t)s+=p;s/=r;let o=new Array(r).fill(s),i=[];for(let p=0;p<r;p++)i.push(p);let a=q6(e),u=[];for(let p=0;p<this.numTrees;p++){let c=new Array(r);for(let d=0;d<r;d++)c[d]=t[d]-o[d];let f=mv(e,c,i,0,this.maxDepth,this.minSamples,a);for(let d=0;d<r;d++)o[d]+=this.lr*iL(f,e[d]);u.push(f)}this.trees=u,this.base=s}predict(e){if(!this.trees)return 0;let t=this.base;for(let r of this.trees)t+=this.lr*iL(r,e);return t}serialize(){return{trees:this.trees,base:this.base,lr:this.lr}}static deserialize(e){let t=new n({lr:e.lr});return t.trees=e.trees,t.base=e.base,t}}});function uL(n){let e=n[0].length,t=new Array(e+1).fill(0);for(let r of n)for(let s=0;s<e;s++){let o=r[s]||0;K6.has(s)?o>t[s]&&(t[s]=o):t[s]+=o}for(let r of H6)r<e&&(t[r]/=n.length);return t[e]=n.length,t}var U6,W6,K6,H6,gv,Y6,yb,Ac,Oc,yv=M(()=>{"use strict";hv();aL();U6=new Set(["depth","threadBlockSize","gridSize","underReduction","vectorized","parallelized","innermostExtent"]),W6=new Set(["arithmeticIntensity"]),K6=new Set(gb.map((n,e)=>U6.has(n)?e:-1).filter(n=>n>=0)),H6=new Set(gb.map((n,e)=>W6.has(n)?e:-1).filter(n=>n>=0));l(uL,"aggregateStatements");gv=class{static{l(this,"CostEstimate")}constructor(e,t){this.score=e,this.breakdown=t}},Y6={parallelism:2,vectorization:1.5,memoryCoalescing:2,occupancy:1,arithmeticIntensity:1,loopOverhead:-.5,codeSize:-.3},yb=class{static{l(this,"AnalyticalCostModel")}constructor(e,t={}){this.target=e,this._weights={...Y6,...e&&e.costModelWeights?e.costModelWeights:{},...t.weights||{}}}estimate(e){let t=Mu.extract(e);return this.estimateFromFeatures(t)}score(e){return this.estimate(e).score}estimateFromFeatures(e){let t=this._scoreParallelism(e),r=this._scoreVectorization(e),s=this._scoreMemoryAccess(e),o=this._scoreOccupancy(e),i=this._scoreIntensity(e),a=this._scoreOverhead(e),u=this._scoreCodeSize(e),p=this._weights,c=p.parallelism*t+p.vectorization*r+p.memoryCoalescing*s+p.occupancy*o+p.arithmeticIntensity*i+p.loopOverhead*a+p.codeSize*u;return new gv(c,{parallelism:t,vectorization:r,memoryCoalescing:s,occupancy:o,arithmeticIntensity:i,loopOverhead:a,codeSize:u})}_scoreParallelism(e){if(this.target.isGPU()){let r=e.threadBlockSize*e.gridSize,s=this.target.maxParallelism();return Math.min(1,r/Math.max(s*.1,1))}return e.numParallelLoops/Math.max(e.numLoops,1)}_scoreVectorization(e){return e.numLoops===0?0:this.target.isGPU()?e.strideOneAccesses/Math.max(e.strideOneAccesses+e.nonStrideOneAccesses,1):e.numVectorizedLoops>0?Math.min(1,e.innermostExtent/this.target.vectorWidth):0}_scoreMemoryAccess(e){let t=e.strideOneAccesses+e.nonStrideOneAccesses;return t===0?1:e.strideOneAccesses/t}_scoreOccupancy(e){if(!this.target.isGPU())return 1;if(e.threadBlockSize===0)return 0;let t=this.target.warpSize,r=Math.ceil(e.threadBlockSize/t),s=Math.floor(this.target.maxThreadsPerBlock/t);return Math.min(1,r/s)}_scoreIntensity(e){let t=this.target.supportsFloat16?1.5:1;return Math.min(1,e.arithmeticIntensity*10*t)}_scoreOverhead(e){return e.numSerialLoops/Math.max(e.numLoops,1)}_scoreCodeSize(e){return Math.min(1,(e.numMathOps+e.numExternCalls)/256)}compare(e,t){return this.estimate(e).score-this.estimate(t).score}},Ac=class n{static{l(this,"LearnedCostModel")}constructor(e=null,t={}){this.opts={numTrees:t.numTrees??60,maxDepth:t.maxDepth??3,lr:t.lr??.1,minSamples:t.minSamples??1},this._gbt=e?Bd.deserialize(e):null,this._X=[],this._Y=[]}addSample(e,t){!e||e.length===0||Number.isFinite(t)&&(this._X.push(uL(e)),this._Y.push(t))}train(){if(this._X.length===0)return;let e=new Bd(this.opts);e.fit(this._X,this._Y),this._gbt=e}predict(e){return!this._gbt||!e||e.length===0?0:this._gbt.predict(uL(e))}get trained(){return this._gbt!==null}get sampleCount(){return this._X.length}serialize(){return{gbt:this._gbt?this._gbt.serialize():null,numSamples:this._X.length}}static deserialize(e){return new n(e&&e.gbt?e.gbt:null)}},Oc=class{static{l(this,"GuidedCostModel")}constructor(e,t,r={}){this.analytical=e,this.learned=t,this.confidenceSamples=r.confidenceSamples??8}_learnedConfident(){return this.learned&&this.learned.trained&&this.learned.sampleCount>=this.confidenceSamples}score(e){return this._learnedConfident()?this.learned.predict(Mu.extractStatements(e)):this.analytical.score(e)}}});function*lL(n){let e=[n];for(;e.length>0;){let t=e.pop();if(t){if(yield t,t.body&&e.push(t.body),t.stmts)for(let r of t.stmts)e.push(r);t.thenBody&&e.push(t.thenBody),t.elseBody&&e.push(t.elseBody),t.initBody&&e.push(t.initBody)}}}function Rd(n,e){for(let t of lL(n))if(t.type==="BlockNode"&&t.name===e)return t;return null}function Fo(n){let e=[];for(let t of lL(n))t.type==="BlockNode"&&e.push(t.name);return e}function Md(n,e){let t=Tt(n,e);if(!t)return{spatial:0,reduction:0,reads:0,hasReduction:!1};let r=0,s=0;for(let o of t.loops)t.reductionLoopVars.has(o.loopVar.name)?s++:r++;return{spatial:r,reduction:s,reads:t.readBuffers.length,hasReduction:t.hasReduction}}var Bc=M(()=>{"use strict";Do();l(lL,"walkStmts");l(Rd,"findBlock");l(Fo,"collectAllBlockNames");l(Md,"analyzeBlockStructure")});function pL(n){return n.isGPU()?Z6:J6}function _v(n){let e=0,t=0;for(let[r,s]of n.order)r==="S"?e=Math.max(e,s+1):t=Math.max(t,s+1);return{spatialLevels:e,reductionLevels:t}}var J6,Z6,cL,bv=M(()=>{"use strict";J6={name:"mlt_cpu",order:[["S",0],["S",1],["S",2],["S",3],["R",0]],roles:{S0:"parallelize",S3:"vectorize"}},Z6={name:"mlt_gpu",order:[["S",0],["S",1],["S",2],["R",0]],roles:{S0:"blockIdx",S1:"threadIdx",S2:"unroll"}},cL={name:"ssrsrs_cpu",order:[["S",0],["S",1],["R",0],["S",2],["R",1],["S",3]],roles:{S0:"parallelize",S3:"vectorize",R1:"unroll"}};l(pL,"getTileStructure");l(_v,"levelCounts")});var Os,$r,_b=M(()=>{"use strict";Os=class{static{l(this,"SearchVariable")}constructor(e,t){this.name=e,this.candidates=t}sample(e){return this.candidates[e(this.candidates.length)]}},$r=class{static{l(this,"ScheduleSketch")}constructor(e,t,r){this.name=e,this.variables=t,this._apply=r}instantiate(e){return(t,r,s)=>{this._apply(t,r,s,e)}}sampleParams(e){let t={};for(let r of this.variables)t[r.name]=r.sample(e);return t}}});function Q6(n){let e=[];for(let t=1;t*t<=n;t++){if(n%t!==0)continue;e.push(t);let r=n/t;r!==t&&e.push(r)}return e.sort((t,r)=>t-r),e}function X6(n,e){let t=Math.min(n.length,e.length);for(let r=0;r<t;r++)if(n[r]!==e[r])return n[r]-e[r];return n.length-e.length}function e4(n,e){let t=new Set,r=[];n.sort(X6);for(let i of n){let a=i.join(",");t.has(a)||(t.add(a),r.push(i))}if(r.length<=e)return r;let s=new Set,o=[];for(let i=0;i<e;i++){let a=Math.floor(i*(r.length-1)/(e-1));s.has(a)||(s.add(a),o.push(r[a]))}return o}function wv(n,e,t=48){if(!Number.isFinite(n)||n<1||e<=1)return[[n]];let r=[],s=t*8,o=l((i,a,u)=>{if(!(r.length>=s)){if(a===e-1){r.push([...u,i]);return}for(let p of Q6(i))if(u.push(p),o(i/p,a+1,u),u.pop(),r.length>=s)return}},"rec");return o(n,0,[]),e4(r,t)}var fL=M(()=>{"use strict";l(Q6,"divisorsOf");l(X6,"cmpTuple");l(e4,"selectDiverse");l(wv,"enumerateFactorizations")});function bb(n){return n.extent&&n.extent.type==="IntImmNode"?n.extent.value:null}function hL(n,e,t,r){let s=[],o=t;for(let i=0;i<r.length-1;i++){let a=n.getLoops(e).find(f=>f.loopVar.name===o);if(!a)return null;let u=1;for(let f=i+1;f<r.length;f++)u*=r[f];let[p,c]=n.split(a,u);s.push(p.loopVar.name),o=c.loopVar.name}return s.push(o),s}function t4(n,e,t,r,s){let o=l(i=>n.getLoops(e).find(a=>a.loopVar.name===i),"find");for(let[i,a]of t.order){let u=t.roles[i+a];if(!u)continue;let p=i==="S"?r:s;if(u==="parallelize"){let c=p[0]&&a<p[0].length?o(p[0][a]):null;c&&n.parallelize(c)}else if(u==="vectorize"){let c=p[p.length-1],f=c&&a<c.length?o(c[a]):null;f&&n.vectorize(f)}else if(u==="unroll")for(let c of p){let f=a<c.length?o(c[a]):null;f&&n.unroll(f)}else(u==="blockIdx"||u==="threadIdx")&&p.forEach((c,f)=>{if(f>=dL.length)return;let d=a<c.length?o(c[a]):null;d&&n.bindThread(d,`${u}.${dL[f]}`)})}}function mL(n,e,t,r,s,o){let i=[];for(let f=0;f<r.length;f++){let d=hL(n,e,r[f],o[`s${f}`]);if(!d)return;i.push(d)}let a=[];for(let f=0;f<s.length;f++){let d=hL(n,e,s[f],o[`r${f}`]);if(!d)return;a.push(d)}let u=[];for(let[f,d]of t.order){let h=f==="S"?i:a;for(let m of h)d<m.length&&u.push(m[d])}let p=new Map(n.getLoops(e).map(f=>[f.loopVar.name,f])),c=u.map(f=>p.get(f)).filter(Boolean);c.length>=2&&n.reorder(...c),t4(n,e,t,i,a)}function gL(n){let e=n.loops.filter(r=>!n.reductionLoopVars.has(r.loopVar.name)),t=n.loops.filter(r=>n.reductionLoopVars.has(r.loopVar.name));return{spatialLoops:e,reductionLoops:t}}function yL(n,e,t,r){let s=[];return n.forEach((o,i)=>s.push(new Os(`s${i}`,wv(bb(o),t)))),e.forEach((o,i)=>s.push(new Os(`r${i}`,wv(bb(o),r)))),s}function _L(n,e){let{spatialLevels:t,reductionLevels:r}=_v(e),{spatialLoops:s,reductionLoops:o}=gL(n);if(s.length===0)return null;for(let p of[...s,...o])if(bb(p)===null)return null;let i=yL(s,o,t,r),a=s.map(p=>p.loopVar.name),u=o.map(p=>p.loopVar.name);return new $r(e.name,i,(p,c,f,d)=>{mL(p,c,e,a,u,d)})}function bL(n,e){let{spatialLevels:t,reductionLevels:r}=_v(e),{spatialLoops:s,reductionLoops:o}=gL(n);if(s.length===0||o.length===0)return null;for(let p of[...s,...o])if(bb(p)===null)return null;let i=yL(s,o,t,r),a=s.map(p=>p.loopVar.name),u=o.map(p=>p.loopVar.name);return new $r(e.name,i,(p,c,f,d)=>{p.decomposeReduction(c),mL(p,`${c}_upd`,e,a,u,d)})}var dL,wL=M(()=>{"use strict";_b();fL();bv();dL=["x","y","z"];l(bb,"staticExtent");l(hL,"multiLevelSplit");l(t4,"applyRoles");l(mL,"tileBlock");l(gL,"splitLoops");l(yL,"tilingVariables");l(_L,"createMultiLevelTilingSketch");l(bL,"createSSRSRSTilingSketch")});function xL(n){return Math.min(n&&n.maxThreadsPerBlock||256,256)}function r4(n){let e=new Set;for(let t=2;t*t<=n;t++){if(n%t!==0)continue;e.add(t);let r=n/t;r>1&&r<n&&e.add(r)}return[...e].sort((t,r)=>t-r)}function SL(n){return new $r("fused",[],(e,t,r,s)=>{e.fuseConsumer(t,n);let o=e.getLoops(t);o.length>0&&e.parallelize(o[0])})}function vL(n){let e=n.loops.filter(i=>n.reductionLoopVars.has(i.loopVar.name));if(e.length!==1)return null;let t=e[0],r=t.extent&&t.extent.type==="IntImmNode"?t.extent.value:null;if(r===null)return null;let s=r4(r);if(s.length===0)return null;let o=t.loopVar.name;return new $r("rfactor",[new Os("rf_factor",s)],(i,a,u,p)=>{i.rfactor(a,o,p.rf_factor);let c=i.getLoops(`${a}_rf_p`);c.length>0&&i.parallelize(c[0])})}function kL(){return new $r("elementwise_cpu",[new Os("vector_width",n4)],(n,e,t,r)=>{let s=n.getLoops(e);if(s.length===0)return;if(s.length===1){let a=s[0].extent;if(a.type==="IntImmNode"&&a.value>=r.vector_width*2){let[u,p]=n.split(s[0],r.vector_width);n.parallelize(u),n.vectorize(p)}else n.parallelize(s[0]);return}n.parallelize(s[0]);let o=s[s.length-1],i=o.extent;if(i.type==="IntImmNode"&&i.value>=r.vector_width){let[,a]=n.split(o,r.vector_width);n.vectorize(a)}})}function NL(){return new $r("elementwise_gpu",[new Os("block_size",TL)],(n,e,t,r)=>{let s=n.getLoops(e);if(s.length===0)return;let o=s[0];for(let c=1;c<s.length;c++){let d=n.getLoops(e).find(h=>h.loopVar.name===s[c].loopVar.name);d&&o.body===d&&(o=n.fuseLoops(o,d))}let i=o.extent;if(i.type!=="IntImmNode"){n.bindThread(o,"threadIdx.x");return}let a=i.value,u=Math.min(t.maxThreadsPerBlock,1024);if(eS(n.func)&&a<=u){n.bindThread(o,"threadIdx.x");return}let p=Math.min(r.block_size,xL(t));if(a>p){let[c,f]=n.split(o,p);n.bindThread(c,"blockIdx.x"),n.bindThread(f,"threadIdx.x")}else n.bindThread(o,"threadIdx.x")})}function AL(){return new $r("reduction_cpu",[],(n,e,t,r)=>{let s=n.getLoops(e);s.length>0&&n.parallelize(s[0])})}function OL(){return new $r("reduction_gpu",[new Os("block_size",TL)],(n,e,t,r)=>{let s=n.getLoops(e);if(s.length===0)return;let o=Tt(n.func,e),i=s.filter(c=>!o||!Xl(c,o));if(i.length===0)return;let a=i[0];for(let c=1;c<i.length;c++){let d=n.getLoops(e).find(h=>h.loopVar.name===i[c].loopVar.name);d&&a.body===d&&(a=n.fuseLoops(a,d))}let u=a.extent;if(u.type!=="IntImmNode"){n.bindThread(a,"threadIdx.x");return}let p=Math.min(r.block_size,xL(t));if(u.value>p){let[c,f]=n.split(a,p);n.bindThread(c,"blockIdx.x"),n.bindThread(f,"threadIdx.x")}else n.bindThread(a,"threadIdx.x")})}var TL,n4,BL=M(()=>{"use strict";Do();_b();TL=[32,64,128,256,512,1024],n4=[1,2,4,8,16];l(xL,"gpuThreadCap");l(r4,"reductionDivisors");l(SL,"createFusedTilingSketch");l(vL,"createRfactorSketch");l(kL,"createElementwiseCPUSketch");l(NL,"createElementwiseGPUSketch");l(AL,"createReductionCPUSketch");l(OL,"createReductionGPUSketch")});function wb(n){return Ay(n,wb,(e,t,r)=>{switch(e.type){case"PrimFunc":return t.name=e.name,t.params=e.params,t.body=r(e.body),t.bufferMap=new Map(e.bufferMap),t.shapeParams=e.shapeParams,t.shapeParamMap=e.shapeParamMap instanceof Map?new Map(e.shapeParamMap):e.shapeParamMap,t._setChild("body",t.body),t;case"AllocateNode":return t.buffer=e.buffer,t.scope=e.scope,t.body=r(e.body),t._setChild("body",t.body),t;case"LetStmtNode":return t.variable=e.variable,t.value=r(e.value),t.body=r(e.body),t._setChild("body",t.body),t;case"WhileNode":return t.condVar=e.condVar,t.condBody=r(e.condBody),t.loopBody=r(e.loopBody),t._setChild("condBody",t.condBody),t._setChild("loopBody",t.loopBody),t;default:for(let s of Object.keys(e)){if(s==="_parent"||s==="_parentKey"||s==="_parentIdx")continue;let o=e[s];o instanceof Map?t[s]=new Map(o):Array.isArray(o)?t[s]=o.map(r):typeof o=="object"&&o!==null&&o.type?t[s]=r(o):t[s]=o}return t}})}function Ti(n){return wb(n)}function Iu(n){return wb(n)}function Tv(n){return wb(n)}function RL(n,e,t){let r=t.get(e);if(!r)return null;let s=[],o=r._parent;for(;o&&o!==n;)o.type==="ForNode"&&s.push(o),o=o._parent;s.reverse();let i=Tv(r);for(let p=s.length-1;p>=0;p--){let c=s[p],f=new he(c.loopVar,Tv(c.min),Tv(c.extent),c.kind,i,c.threadTag);f._setChild("body",i),i=f}let a=new Map;for(let p of r.reads)a.set(p.buffer.name,p.buffer);for(let p of r.writes)a.set(p.buffer.name,p.buffer);let u=[];for(let p of n.params)a.has(p.name)&&u.push(p);return new Pi("__tune_"+e,u,i,a,[])}var Id=M(()=>{"use strict";Fe();Gx();l(wb,"deepCloneIR");l(Ti,"clonePrimFunc");l(Iu,"cloneTensorIR");l(Tv,"cloneBlockSubtree");l(RL,"extractBlockMini")});function kv(n,e){let t=[];for(let r=1;r<=e;r*=2)r>=n&&t.push(r);return t}function Cd(n){if(!n)return null;let e=[];for(let t of n){if(!t||t.type!=="VariableNode")return null;e.push(t.name)}return e}function s4(n,e){let t=[n];for(;t.length>0;){let r=t.pop();if(!(!r||typeof r!="object")){if(r.type==="BufferStoreNode"&&r.buffer&&r.buffer.name===e&&r.value&&r.value.type==="MathOpNode"&&r.value.op==="+")return r;if(r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody)}}return null}function Ld(n){if(!n||n.broadcastDims||n.offset&&n.offset!==0)return!1;let e=n.shape,t=n.strides;if(!t||t.length!==e.length)return!1;let r=1;for(let s=e.length-1;s>=0;s--){if(typeof e[s]!="number"||e[s]<=0||t[s]!==r)return!1;r*=e[s]}return!0}function Nv(n,e){if(!Tt(n,e))return null;let r=Rd(n.body,e);if(!r||r.reads.length<2||r.writes.length<1)return null;let s=r.writes[0].buffer;if(!s||s.shape.length<2)return null;let o=s.shape.length,i=s4(r.body,s.name);if(!i)return null;let a=Cd(i.indices);if(!a||a.length!==o)return null;let u=i.value,p=l(T=>T&&T.type==="BufferLoadNode"&&T.buffer&&T.buffer.name===s.name,"isCLoad"),c=p(u.a)?u.b:p(u.b)?u.a:null;if(!c||c.type!=="MathOpNode"||c.op!=="*")return null;let f=[c.a,c.b];if(!f.every(T=>T&&T.type==="BufferLoadNode"&&T.buffer))return null;let d=a[o-1],h=a.slice(0,o-1),m=null,g=null;for(let T of f){let v=Cd(T.indices);if(!v||v.length!==o)continue;let A=!0;for(let N=0;N<o-1;N++)if(v[N]!==h[N]){A=!1;break}if(A){m=T,g=v[o-1];break}}if(!m||g==null)return null;let y=f[0]===m?f[1]:f[0],_=Cd(y.indices);if(!_)return null;let b=m.buffer,w=y.buffer;if(b.shape.length!==o||b.dtype!=="f32"||w.dtype!=="f32"||s.dtype!=="f32")return null;for(let T=0;T<o-1;T++)if(b.shape[T]!==s.shape[T])return null;let x=s.shape[o-1],S=b.shape[o-1];if(![x,S].every(T=>typeof T=="number"&&T>0))return null;if(_.length===2&&w.shape.length===2){let T;if(_[0]===g&&_[1]===d)T=!1;else if(_[0]===d&&_[1]===g)T=!0;else return null;if(T){if(w.shape[0]!==x||w.shape[1]!==S)return null}else if(w.shape[0]!==S||w.shape[1]!==x)return null;let v=1;for(let A=0;A<o-1;A++){let N=s.shape[A];if(typeof N!="number"||N<=0)return null;v*=N}return o===2?{A:b,B:w,C:s,M:v,N:x,K:S,transB:T,batch:1}:!Ld(b)||!Ld(s)?null:{A:new me(b.name,[v,S],b.dtype,b.scope),B:w,C:new me(s.name,[v,x],s.dtype,s.scope),M:v,N:x,K:S,transB:T,batch:1}}if(o>=3&&_.length===o&&w.shape.length===o){let T=a.slice(0,o-2);for(let B=0;B<o-2;B++)if(_[B]!==T[B])return null;let v;if(_[o-2]===g&&_[o-1]===d)v=!1;else if(_[o-2]===d&&_[o-1]===g)v=!0;else return null;for(let B=0;B<o-2;B++)if(w.shape[B]!==s.shape[B])return null;if(v){if(w.shape[o-2]!==x||w.shape[o-1]!==S)return null}else if(w.shape[o-2]!==S||w.shape[o-1]!==x)return null;let A=s.shape[o-2];if(typeof A!="number"||A<=0)return null;let N=1;for(let B=0;B<o-2;B++){let E=s.shape[B];if(typeof E!="number"||E<=0)return null;N*=E}if(!Ld(b)||!Ld(w)||!Ld(s))return null;let k=new me(b.name,[N,A,S],b.dtype,b.scope),O=new me(w.name,v?[N,x,S]:[N,S,x],w.dtype,w.scope),R=new me(s.name,[N,A,x],s.dtype,s.scope);return{A:k,B:O,C:R,M:A,N:x,K:S,transB:v,batch:N}}return null}function CL(n,e,t=32){let r=n.maxThreadsPerBlock||1024,s=n.warpSize||32,o=n.sharedMemoryBytes||49152,i=n.registersPerThread||255,a=4,{M:u,N:p,K:c}=e,f=kv(2,8),d=kv(2,s/2),h=kv(s/4,s/2),m=2*s,g=Math.min(r,8*s),y=4*s,_=new Set,b=[];for(let w of f)for(let x of f)if(!(w*x>y)&&!(w*x+w+x+s>i))for(let S of d)for(let T of d){let v=T*S;if(v<m||v>g||v%s!==0)continue;let A=w*S,N=x*T;if(!(A>u||N>p||A>y||N>y))for(let k of h){if(k>c||(A*k+k*N)*a>o)continue;let R=`${A}_${N}_${k}_${w}_${x}`;_.has(R)||(_.add(R),b.push({BM:A,BN:N,BK:k,TM:w,TN:x,threads:v}))}}return b.sort((w,x)=>EL(x,s)-EL(w,s)),b.slice(0,t)}function EL(n,e){let t=n.TM*n.TN,r=-Math.abs(n.TM-n.TN),s=-Math.abs(Math.log2(n.BM)-Math.log2(n.BN)),o=n.threads>=4*e&&n.threads<=8*e?1:0,i=n.BK===e/4?1:0;return o*100+t*4+r*6+s*4+i}function Tb(n,e){if(!e)return null;let t=CL(n,e,256);return t.find(s=>s.BM===64&&s.BN===64&&s.BK===8&&s.TM===4&&s.TN===4)||(t.length>0?t[0]:null)}function o4(n,e,t,r){let s=Iu(n.storeValue);return Gi(s,o=>o.type==="BufferLoadNode"&&o.buffer?o.buffer.name===n.inputName?e:n.scalarConsts.has(o.buffer.name)?Iu(n.scalarConsts.get(o.buffer.name)):o:o.type==="VariableNode"?o.name===n.iv0?Iu(t):o.name===n.iv1?Iu(r):o:o)}function Av(n,e,t=null){let{A:r,B:s,C:o,M:i,N:a,K:u,transB:p}=n,c=n.batch||1,{BM:f,BN:d,BK:h,TM:m,TN:g}=e,y=d/g,_=f/m,b=y*_,w=Math.ceil(u/h),x=f*h,S=h*d,T=Math.ceil(x/b),v=Math.ceil(S/b),A=i%f!==0,N=a%d!==0,k=u%h!==0,O=x%b!==0,R=S%b!==0,B=new me("rb_As",[x],"f32","shared"),E=new me("rb_Bs",[S],"f32","shared"),F=new me("rb_acc",[m*g],"f32","local"),C=new me("rb_af",[m],"f32","local"),H=new me("rb_bf",[g],"f32","local"),G=Jt("rb_bx"),q=Jt("rb_by"),D=Jt("rb_tx"),V=Jt("rb_ty"),ue=Jt("rb_tid"),ce=Jt("rb_brow"),Le=Jt("rb_bcol"),j=Jt("rb_k0"),W=Jt("rb_bz"),ie=c>1,je=l((Ot,lo)=>ie?[W,Ot,lo]:[Ot,lo],"gIdx"),ke=l((Ot,lo)=>Jn(Fr(Ot,nt(g)),lo),"accIdx"),rt=Jt("rb_im"),zt=Jt("rb_in"),on=xi(rt,m,xi(zt,g,new J(F,[ke(rt,zt)],xv()))),Zt=Jt("rb_la"),it=Jt("rb_aidx"),ye=Jn(ce,ML(it,nt(h))),Xe=Jn(j,IL(it,nt(h))),Nn=new U(r,je(ye,Xe));if(A||k){let Ot=A?so(ye,nt(i)):null;k&&(Ot=Ot?Sv(Ot,so(Xe,nt(u))):so(Xe,nt(u))),Nn=new Se(Ot,Nn,xv())}let Qt=new J(B,[it],Nn);O&&(Qt=new Se(so(it,nt(x)),Qt));let an=vv(Zt,T,new et(it,Jn(ue,Fr(Zt,nt(b))),Qt)),un=Jt("rb_lb"),mn=Jt("rb_bidx"),cr=Jn(j,ML(mn,nt(d))),Vn=Jn(Le,IL(mn,nt(d))),Or=new U(s,p?je(Vn,cr):je(cr,Vn));if(k||N){let Ot=k?so(cr,nt(u)):null;N&&(Ot=Ot?Sv(Ot,so(Vn,nt(a))):so(Vn,nt(a))),Or=new Se(Ot,Or,xv())}let pr=new J(E,[mn],Or);R&&(pr=new Se(so(mn,nt(S)),pr));let yt=vv(un,v,new et(mn,Jn(ue,Fr(un,nt(b))),pr)),fr=Jt("rb_kk"),xt=Jt("rb_fi"),Vr=Jt("rb_fj"),Ds=xi(xt,m,new J(C,[xt],new U(B,[Jn(Fr(Jn(Fr(V,nt(m)),xt),nt(h)),fr)]))),uo=xi(Vr,g,new J(H,[Vr],new U(E,[Jn(Fr(fr,nt(d)),Jn(Fr(D,nt(g)),Vr))]))),Gr=Jt("rb_mi"),dr=Jt("rb_ni"),Br=xi(Gr,m,xi(dr,g,new J(F,[ke(Gr,dr)],Jn(new U(F,[ke(Gr,dr)]),Fr(new U(C,[Gr]),new U(H,[dr])))))),Uo=xi(fr,h,new te([Ds,uo,Br])),jr=Jt("rb_kt"),qr=new et(j,Fr(jr,nt(h)),new te([an,yt,new Fn,Uo,new Fn])),Ps=vv(jr,w,qr),ms=Jt("rb_wm"),va=Jt("rb_wn"),Oi=Jn(Jn(ce,Fr(V,nt(m))),ms),Bi=Jn(Jn(Le,Fr(D,nt(g))),va),ka=new U(F,[ke(ms,va)]),tp=t?t.outBuffer:o,zu=t?o4(t,ka,Oi,Bi):ka,Na=new J(tp,je(Oi,Bi),zu);if(A||N){let Ot=A?so(Oi,nt(i)):null;N&&(Ot=Ot?Sv(Ot,so(Bi,nt(a))):so(Bi,nt(a))),Na=new Se(Ot,Na)}let Aa=xi(ms,m,xi(va,g,Na)),Wo=new te([on,Ps,Aa]),Oa=new qe(F,"local",new qe(C,"local",new qe(H,"local",Wo))),Ri=new et(ue,Jn(Fr(V,nt(y)),D),new et(ce,Fr(q,nt(f)),new et(Le,Fr(G,nt(d)),Oa))),Ko=Math.ceil(a/d),Mi=Math.ceil(i/f),Ba=Ed(q,"blockIdx.y",Mi,Ed(G,"blockIdx.x",Ko,Ed(V,"threadIdx.y",_,Ed(D,"threadIdx.x",y,Ri))));return ie&&(Ba=Ed(W,"blockIdx.z",c,Ba)),new qe(B,"shared",new qe(E,"shared",Ba))}function i4(n){let e=new Os("config_index",n.map((r,s)=>s)),t=new $r("matmul_register_block_gpu",[e],(r,s,o,i)=>{let a=Nv(r.func,s);if(!a)return;let u=n[i.config_index];if(!u)return;let p=Av(a,u);r.func.body=p,r.func._setChild&&r.func._setChild("body",p),r.func.gpuRegisterBlocked=!0});return t.configs=n,t.enumerate=()=>n.map((r,s)=>({config_index:s})),t}function a4(n){let e=Fo(n.body),t=null;for(let s of e){let o=Md(n,s);if(o.hasReduction&&o.spatial>=2&&o.reads>=2){if(t)return null;t=s}}if(!t)return null;let r=Nv(n,t);if(!r)return null;for(let s of e){if(s===t)continue;let o=Tt(n,s);if(!o||o.hasReduction||o.readBuffers.length>0)return null;for(let i of o.writeBuffers)if(i!==r.C.name)return null}return{reductionBlock:t,dims:r}}function u4(n){let e=new Map,t=new Map;Us(n,r=>{if(r.type==="BufferStoreNode"&&r.buffer){let s=r.buffer.name;t.set(s,(t.get(s)||0)+1),(!r.indices||r.indices.length===0)&&r.value&&(r.value.type==="FloatImmNode"||r.value.type==="IntImmNode")&&e.set(s,r.value)}});for(let r of[...e.keys()])(t.get(r)||0)!==1&&e.delete(r);return e}function l4(n,e){let t=null;return Us(n,r=>{if(r.type==="BufferStoreNode"&&r.buffer&&r.buffer.name===e)return t=r,Om}),t}function c4(n,e){let t=null,r=0;return Us(n,s=>{s.type==="BufferLoadNode"&&s.buffer&&s.buffer.name===e&&(r++,t=s)}),r===1?t:null}function DL(n){let e=Fo(n.body),t=null;for(let y of e){let _=Md(n,y);if(_.hasReduction&&_.spatial>=2&&_.reads>=2){if(t)return null;t=y}}if(!t)return null;let r=Nv(n,t);if(!r)return null;let s=r.C.name,o=new Set;for(let y of e){let _=Tt(n,y);if(!_)return null;for(let b of _.writeBuffers)o.add(b)}let i=[];for(let y of e){if(y===t)continue;let _=Tt(n,y);if(_.hasReduction)return null;if(_.readBuffers.length===0){if(_.writeBuffers.every(b=>b===s))continue;return null}i.push({name:y,info:_})}if(i.length===0)return{reductionBlock:t,dims:r,epilogue:null};if(i.length!==1||(r.batch||1)!==1)return null;let a=i[0];if(a.info.writeBuffers.length!==1)return null;let u=a.info.writeBuffers[0],p=u4(n.body),c=0;for(let y of a.info.readBuffers){if(y===s){c++;continue}if(o.has(y)&&!p.has(y))return null}if(c!==1)return null;let f=Rd(n.body,a.name);if(!f)return null;let d=l4(f.body,u);if(!d)return null;let h=Cd(d.indices);if(!h||h.length!==2)return null;let m=c4(d.value,s);if(!m)return null;let g=Cd(m.indices);return!g||g.length!==h.length||g.some((y,_)=>y!==h[_])?null:{reductionBlock:t,dims:r,epilogue:{outBuffer:d.buffer,storeValue:d.value,inputName:s,iv0:h[0],iv1:h[1],scalarConsts:p}}}function PL(n,e,t){let r=a4(n);if(!r)return null;let s=LL.get(n);if(s===void 0){let o=CL(t,r.dims);s=o.length>0?i4(o):null,LL.set(n,s)}return s?e===r.reductionBlock?[s]:[]:null}var nt,xv,Jt,Jn,Fr,ML,IL,so,Sv,vv,xi,Ed,LL,xb=M(()=>{"use strict";Fe();ei();Do();_b();Bc();ts();Id();nt=l(n=>new L(n),"I"),xv=l(()=>new de(0),"FZERO"),Jt=l(n=>new Oe(n,"i32"),"IV"),Jn=l((n,e)=>qn("+",n,e),"ADD"),Fr=l((n,e)=>qn("*",n,e),"MUL"),ML=l((n,e)=>qn("//",n,e),"DIV"),IL=l((n,e)=>qn("%",n,e),"MOD"),so=l((n,e)=>new Ke("lt",n,e),"LT"),Sv=l((n,e)=>new P("&&",n,e),"AND"),vv=l((n,e,t)=>new he(n,nt(0),nt(e),z.SERIAL,t),"forS"),xi=l((n,e,t)=>new he(n,nt(0),nt(e),z.UNROLLED,t),"forU"),Ed=l((n,e,t,r)=>new he(n,nt(0),nt(t),z.THREAD_BINDING,r,e),"forT");l(kv,"pow2Range");l(Cd,"plainVars");l(s4,"findAccStore");l(Ld,"isContiguousRowMajor");l(Nv,"matmulTileDims");l(CL,"enumerateRegisterBlockConfigs");l(EL,"goodness");l(Tb,"pickFixedConfig");l(o4,"foldEpilogue");l(Av,"buildRegisterBlockedMatmul");l(i4,"createMatmulRegisterBlockGPUSketch");l(a4,"analyzePureMatmul");l(u4,"collectScalarConstBuffers");l(l4,"findEpilogueStore");l(c4,"singleBufferLoad");l(DL,"analyzeMatmulEpilogue");LL=new WeakMap;l(PL,"richMatmulSketches")});function p4(n,e){let t=[n];for(;t.length;){let r=t.pop();if(!(!r||typeof r!="object")&&(r.type==="BlockNode"&&e(r),r.body&&t.push(r.body),r.initBody&&t.push(r.initBody),r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),Array.isArray(r.stmts)))for(let s of r.stmts)t.push(s)}}function Pd(n){let e=[];p4(n.body,r=>{e.push({name:r.name,node:r,reads:(r.reads||[]).map(s=>s.buffer&&s.buffer.name).filter(Boolean),writes:(r.writes||[]).map(s=>s.buffer&&s.buffer.name).filter(Boolean)})});let t=new Map;for(let r of e)for(let s of r.reads)t.has(s)||t.set(s,[]),t.get(s).push(r.name);return{blocks:e,consumersByBuf:t,byName:new Map(e.map(r=>[r.name,r]))}}function Ov(n){let e=[];for(let t of n){if(!t||t.type!=="VariableNode")return null;e.push(t.name)}return e}function Dd(n,e,t){if(!n||typeof n!="object")return t;n.type==="BufferLoadNode"&&n.buffer&&n.buffer.name===e&&t.push(n);for(let r of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])n[r]&&Dd(n[r],e,t);if(Array.isArray(n.args))for(let r of n.args)Dd(r,e,t);if(Array.isArray(n.indices))for(let r of n.indices)Dd(r,e,t);if(Array.isArray(n.stmts))for(let r of n.stmts)Dd(r,e,t);return t}function Rc(n,e){if(!n||typeof n!="object")return e;n.type==="BufferLoadNode"&&n.buffer&&n.buffer.name&&e.add(n.buffer.name);for(let t of["a","b","expr","value","condition","thenBody","elseBody","body","initBody"])n[t]&&Rc(n[t],e);if(Array.isArray(n.args))for(let t of n.args)Rc(t,e);if(Array.isArray(n.indices))for(let t of n.indices)Rc(t,e);if(Array.isArray(n.stmts))for(let t of n.stmts)Rc(t,e);return e}function Sb(n,e,t,r){let s=e.byName.get(t);if(!s||s.writes.length!==1)return null;let o=s.writes[0],i=[...new Set((e.consumersByBuf.get(o)||[]).filter(w=>w!==t))];if(i.length!==1)return null;let a=i[0],u=r(n,t),p=r(n,a);if(!u||!p||p.hasReduction)return null;let c=e.byName.get(a);if(!c||c.writes.length!==1||c.writes[0]===o)return null;let f=new Set(s.reads);if(Rc(s.node.body,f),Rc(s.node.initBody,f),f.has(c.writes[0]))return null;let d=s.node.body&&s.node.body.type==="BufferStoreNode"?s.node.body:null,h=c.node.body&&c.node.body.type==="BufferStoreNode"?c.node.body:null;if(!d||!h)return null;let m=u.loops.filter(w=>!u.reductionLoopVars.has(w.loopVar.name)).map(w=>w.loopVar.name),g=Ov(d.indices);if(!g||g.join(",")!==m.join(","))return null;let y=p.loops.map(w=>w.loopVar.name);if(y.length!==m.length)return null;let _=Ov(h.indices);if(!_||_.join(",")!==y.join(","))return null;let b=Dd(h.value,o,[]);if(b.length===0)return null;for(let w of b){let x=Ov(w.indices);if(!x||x.join(",")!==_.join(","))return null}return a}var Bv=M(()=>{"use strict";l(p4,"walkBlocks");l(Pd,"buildBlockDAG");l(Ov,"indexVarNames");l(Dd,"collectLoads");l(Rc,"collectLoadedBuffers");l(Sb,"findFusibleConsumer")});function Rv(n){return n.isGPU()?OL():AL()}function f4(n){return n.isGPU()?NL():kL()}function d4(n,e,t,r){let s=Tt(n,e);if(!s)return[Rv(t)];let o=[],i=_L(s,pL(t));if(i&&o.push(i),t.kind===Dt.CPU){let a=bL(s,cL);a&&o.push(a);let u=vL(s);u&&o.push(u);let p=r?Sb(n,r,e,Tt):null;p&&o.push(SL(p))}return o.push(Rv(t)),o}function Mv(n,{priority:e=100}={}){vb.some(t=>t.derive===n.derive&&t.matches===n.matches)||(vb.push({matches:n.matches,derive:n.derive,priority:e}),vb.sort((t,r)=>t.priority-r.priority))}function $L(n,e,t,r={}){if(r.richGpu&&t.isGPU()){let i=PL(n,e,t);if(i!==null)return i}if(t.kind!==Dt.CPU&&!t.isGPU())return[];let s=Md(n,e),o=r.dag||Pd(n);for(let i of vb)if(i.matches(s,t))return i.derive(n,e,t,o);return[]}var vb,FL=M(()=>{"use strict";yo();Do();Bc();bv();wL();BL();xb();Bv();l(Rv,"reductionSketch");l(f4,"elementwiseSketch");l(d4,"deriveMultiLevel");vb=[];l(Mv,"registerSketchRule");Mv({matches:l(n=>n.hasReduction&&n.spatial>=1&&n.reads>=2,"matches"),derive:d4},{priority:10});Mv({matches:l(n=>n.hasReduction,"matches"),derive:l((n,e,t)=>[Rv(t)],"derive")},{priority:20});Mv({matches:l(()=>!0,"matches"),derive:l((n,e,t)=>[f4(t)],"derive")},{priority:30});l($L,"deriveSketches")});function Iv(n,e,t,r,s={}){return $L(n,e,t,s)}var zL=M(()=>{"use strict";FL();l(Iv,"getSketchesForBlock")});function kb(n){let e=new Map,t=[n];for(;t.length>0;){let r=t.pop();if(r){if(r.type==="BlockNode"&&e.set(r.name,r),r.body&&t.push(r.body),r.stmts)for(let s of r.stmts)t.push(s);r.thenBody&&t.push(r.thenBody),r.elseBody&&t.push(r.elseBody),r.initBody&&t.push(r.initBody)}}return e}function Nb(n,e,t,r=null){let s=r||kb(n.body),o=s.get(e)||null,i=[];if(o){let a=[];for(let p of o.reads)a.push(`${p.buffer.shape.join("x")}:${p.buffer.dtype}`);for(let p of o.writes)a.push(`${p.buffer.shape.join("x")}:${p.buffer.dtype}`);i.push(a.join(","));let u=[];if(sn(o.body,u),o.initBody&&sn(o.initBody,u),i.push(u.join(";")),o.writes.length===1){let p=o.writes[0].buffer.name,c=[];for(let f of h4(s).get(p)||[]){if(f===o)continue;let d=[];sn(f.body,d),f.initBody&&sn(f.initBody,d),c.push(d.join(";"))}c.length>0&&(c.sort(),i.push(`consumers:${c.join("|")}`))}}return i.push(t.name),i.push(t.kind),m4(i.join("|"))}function h4(n){if(n.__readersByBuffer)return n.__readersByBuffer;let e=new Map;for(let t of n.values()){if(!t.reads)continue;let r=new Set;for(let s of t.reads)s.buffer&&r.add(s.buffer.name);for(let s of r){let o=e.get(s);o||(o=[],e.set(s,o)),o.push(t)}}return n.__readersByBuffer=e,e}function sn(n,e){if(!(!n||typeof n!="object"))switch(n.type){case"BufferStoreNode":if(e.push("store"),n.indices)for(let t of n.indices)sn(t,e);sn(n.value,e);return;case"BufferLoadNode":if(e.push(`load:${n.buffer.shape.join("x")}:${n.buffer.dtype}`),n.indices)for(let t of n.indices)sn(t,e);return;case"MathOpNode":e.push(`math:${n.op}`),sn(n.a,e),n.b&&sn(n.b,e);return;case"CallExternNode":e.push(`call:${n.externName}`);for(let t of n.args)sn(t,e);return;case"CompareNode":e.push(`cmp:${n.direction}`),sn(n.a,e),sn(n.b,e);return;case"CastNode":e.push(`cast:${n.fromDtype}->${n.toDtype}`),sn(n.expr,e);return;case"IfThenElseNode":sn(n.condition,e),sn(n.thenBody,e),n.elseBody&&sn(n.elseBody,e);return;case"SeqNode":for(let t of n.stmts)sn(t,e);return;case"ForNode":sn(n.body,e);return;case"BlockNode":n.initBody&&sn(n.initBody,e),sn(n.body,e);return;case"LetStmtNode":sn(n.value,e),sn(n.body,e);return;default:return}}function m4(n){let e=2166136261;for(let t=0;t<n.length;t++)e^=n.charCodeAt(t),e=e*16777619>>>0;return e.toString(16).padStart(8,"0")}var Ev=M(()=>{"use strict";l(kb,"buildBlockMap");l(Nb,"computeWorkloadKey");l(h4,"readersByBuffer");l(sn,"collectBlockOps");l(m4,"fnv1a")});function g4(n,e){let t=n.medianMs!=null?1:0,r=e.medianMs!=null?1:0;return t!==r?r-t:t===1?n.medianMs-e.medianMs:e.score-n.score}var VL,$d,Fd,GL=M(()=>{"use strict";Ev();VL="mlfw-codegen-1",$d=class{static{l(this,"TuningRecord")}constructor(e,t,r,s,o,i){this.workloadKey=e,this.sketchName=t,this.params=r,this.score=s,this.traceData=o,this.version=i,this.timestamp=Date.now(),this.medianMs=null,this.minMs=null}};l(g4,"rankRecords");Fd=class n{static{l(this,"TuningDatabase")}constructor(e=1){this.version=e,this._records=new Map}computeWorkloadKey(e,t,r,s=null){return Nb(e,t,r,s)}store(e,t){let r=this._records.get(e);r||(r=[],this._records.set(e,r)),r.push(t),r.sort(g4),r.length>10&&(r.length=10)}lookup(e){let t=this._records.get(e);return!t||t.length===0?null:t[0]}lookupTopK(e,t=5){let r=this._records.get(e);return r?r.slice(0,t):[]}has(e){return this._records.has(e)&&this._records.get(e).length>0}get size(){let e=0;for(let[,t]of this._records)e+=t.length;return e}serialize(){let e=[];for(let[t,r]of this._records)for(let s of r)e.push({workloadKey:s.workloadKey,sketchName:s.sketchName,params:s.params,score:s.score,traceData:s.traceData,version:s.version,timestamp:s.timestamp,medianMs:s.medianMs,minMs:s.minMs});return{version:this.version,codegenVersion:VL,entries:e}}static deserialize(e){let t=new n(e.version);if(e.codegenVersion!==void 0&&e.codegenVersion!==VL)return t;for(let r of e.entries){let s=new $d(r.workloadKey,r.sketchName,r.params,r.score,r.traceData,r.version);s.timestamp=r.timestamp,s.medianMs=r.medianMs??null,s.minMs=r.minMs??null,t.store(r.workloadKey,s)}return t}saveToFile(e,t){return t.writeFile(e,JSON.stringify(this.serialize())),e}static loadFromFile(e,t){return t.exists(e)?n.deserialize(JSON.parse(t.readFile(e))):new n}clear(){this._records.clear()}}});function ga(n,e){if(!n)return 0;if(n.type==="ForNode"){let t=n.extent&&n.extent.type==="IntImmNode"?n.extent.value:1,r=n.threadTag?e:e*t;return ga(n.body,r)}if(n.type==="BlockNode")return Math.max(e,ga(n.body,e),n.initBody?ga(n.initBody,e):0);if(n.type==="SeqNode"){let t=e;for(let r of n.stmts)t=Math.max(t,ga(r,e));return t}return n.type==="IfThenElseNode"?Math.max(ga(n.thenBody,e),n.elseBody?ga(n.elseBody,e):e):n.type==="AllocateNode"||n.type==="LetStmtNode"?ga(n.body,e):e}function jL(n,e=.1){let t=n.slice().sort((g,y)=>g-y),r=t.length;if(r===0)return{median:0,min:0,trimmedMean:0,cv:0};let s=t[r>>1],o=t[0],i=Math.floor(r*e),a=i,u=r-i>a?r-i:r,p=0,c=0;for(let g=a;g<u;g++)p+=t[g],c++;let f=c>0?p/c:s,d=0;for(let g=a;g<u;g++){let y=t[g]-f;d+=y*y}let h=c>1?Math.sqrt(d/(c-1)):0,m=f>0?h/f:0;return{median:s,min:o,trimmedMean:f,cv:m}}var y4,Ab,Ob,qL=M(()=>{"use strict";Op();y4=1e6;l(ga,"maxSerialTripCount");Ab=class{static{l(this,"BenchmarkResult")}constructor(e,t,r,s,o=null,i=0){this.medianMs=e,this.minMs=t,this.trimmedMeanMs=o??e,this.cv=i,this.samples=r,this._totalBytes=s}get throughputGBs(){return this.minMs<=0||!this._totalBytes?0:this._totalBytes/(this.minMs*1e6)}};l(jL,"robustStats");Ob=class{static{l(this,"BenchmarkRunner")}constructor(e,t={}){this.target=e,this.warmup=t.warmup??3,this.repeat=t.repeat??10,this.minRepeatMs=t.minRepeatMs??0,this.maxCv=t.maxCv??0,this.maxReMeasures=t.maxReMeasures??1,this.measurer=t.measurer||null,this._warn=t.warn||null,this._bufferCache=new Map}_record(e,t){this._warn&&this._warn(e,null,t)}_getOrAllocBuffers(e){let t=0,r=[];for(let[,i]of e.bufferMap){let a=Math.max(i.numel(),1);r.push(a);let u=i.sizeInBytes();u>0&&(t+=u)}let s=r.join(","),o=this._bufferCache.get(s);o||(o=r.map(i=>new Float32Array(i)),this._bufferCache.set(s,o));for(let i of o)for(let a=0;a<i.length;a++)i[a]=Math.random()*2-1;return{buffers:o,totalBytes:t}}run(e){if(!e||!e.body)return null;if(!this.target.isCPU())return this.measurer?this._runMeasured(e):null;let t=new _o(this.target),r;try{r=t.compile(e)}catch(p){return this._record("benchmark-compile",p),null}if(r.metadata.kind!=="js")return null;let s;try{s=new Function("return "+r.source)()}catch(p){return this._record("benchmark-construct-fn",p),null}let{buffers:o,totalBytes:i}=this._getOrAllocBuffers(e);for(let p=0;p<this.warmup;p++)try{s(...o)}catch(c){return this._record("benchmark-warmup-run",c),null}let a=[],u=null;for(let p=0;p<=this.maxReMeasures&&(this._collect(s,o,a),u=jL(a),!(this.maxCv<=0||u.cv<=this.maxCv));p++);return new Ab(u.median,u.min,a,i,u.trimmedMean,u.cv)}_runMeasured(e){if(e.shapeParams&&e.shapeParams.length>0||ga(e.body,1)>y4)return null;let t;try{t=new _o(this.target).compile(e)}catch(a){return this._record("measured-compile",a),null}let r=[],s=0;for(let[,a]of e.bufferMap){let u=Math.max(a.sizeInBytes(),1);r.push(u),s+=u}let o;try{o=this.measurer(t,r,[],{warmup:this.warmup,repeat:this.repeat})}catch(a){return this._record("measurer",a),null}if(!o||o.length===0)return null;let i=jL(o);return new Ab(i.median,i.min,o,s,i.trimmedMean,i.cv)}_collect(e,t,r){let s=0,o=this.repeat*3;for(let i=0;i<o&&(i<this.repeat||s<this.minRepeatMs);i++){let a=performance.now();e(...t);let u=performance.now()-a;r.push(u),s+=u}}}});var Bb,UL=M(()=>{"use strict";Bb=class{static{l(this,"Deadline")}constructor(e=1/0,t=null){this._clock=t||(()=>performance.now()),this._endAt=e===1/0||e==null?1/0:this._clock()+e}get expired(){return this._clock()>=this._endAt}remainingMs(){return this._endAt===1/0?1/0:Math.max(0,this._endAt-this._clock())}}});function WL(n){let e=null;for(let w of n.ops())if(w.opName==="conv"){if(e)return null;e=w}else if(w.opName!=="return")return null;if(!e||(e.getAttr("groups")||1)!==1)return null;let t=e.getAttr("input_layout"),r=e.getAttr("kernel_layout");if(t&&t!=="NCHW"||r&&r!=="OIHW")return null;let s=e.getOperand(0).type,o=e.getOperand(1).type,i=e.getResult(0).type;if(!s||!o||!i||s.shape.length!==4||o.shape.length!==4||i.shape.length!==4||s.dtype!=="f32"||o.dtype!=="f32"||i.dtype!=="f32")return null;let a=e.getAttr("strides")||[1,1],u=e.getAttr("padding")||[[0,0],[0,0]],p=e.getAttr("dilation")||[1,1],c=s.shape[0],f=s.shape[1],d=s.shape[2],h=s.shape[3],m=o.shape[0],g=o.shape[2],y=o.shape[3],_=i.shape[2],b=i.shape[3];return![c,f,d,h,m,g,y,_,b].every(w=>typeof w=="number"&&w>0)||o.shape[1]!==f||i.shape[1]!==m||i.shape[0]!==c?null:{N:c,Cin:f,H:d,W:h,O:m,Kh:g,Kw:y,Oh:_,Ow:b,sH:a[0],sW:a[1],pH:u[0][0],pW:u[1][0],dH:p[0],dW:p[1]}}function _4(n,e,t){let{weight:r,input:s,output:o}=n,{N:i,Cin:a,H:u,W:p,O:c,Kh:f,Kw:d,Oh:h,Ow:m,sH:g,sW:y,pH:_,pW:b,dH:w,dW:x}=e,{BM:S,BN:T,BK:v,TM:A,TN:N}=t,k=c,O=i*h*m,R=a*f*d,B=f*d,E=h*m,F=T/N,C=S/A,H=F*C,G=Math.ceil(R/v),q=S*v,D=v*T,V=Math.ceil(q/H),ue=Math.ceil(D/H),ce=new me("ig_As",[q],"f32","shared"),Le=new me("ig_Bs",[D],"f32","shared"),j=new me("ig_acc",[A*N],"f32","local"),W=new me("ig_af",[A],"f32","local"),ie=new me("ig_bf",[N],"f32","local"),je=Be("ig_bx"),ke=Be("ig_by"),rt=Be("ig_tx"),zt=Be("ig_ty"),on=Be("ig_tid"),Zt=Be("ig_brow"),it=Be("ig_bcol"),ye=Be("ig_k0"),Xe=l((Hr,Yw)=>Te(xe(Hr,$(N)),Yw),"accIdx"),Nn=Be("ig_im"),Qt=Be("ig_in"),an=kn(Nn,A,kn(Qt,N,new J(j,[Xe(Nn,Qt)],zd()))),un=k%S!==0,mn=R%v!==0,cr=O%T!==0,Vn=q%H!==0,Or=D%H!==0,pr=Be("ig_la"),yt=Be("ig_aidx"),fr=Te(Zt,Dn(yt,$(v))),xt=Te(ye,hn(yt,$(v))),Vr=Dn(xt,$(B)),Ds=hn(xt,$(B)),uo=Dn(Ds,$(d)),Gr=hn(Ds,$(d)),dr=new U(r,[fr,Vr,uo,Gr]);if(un||mn){let Hr=un?kr(fr,$(k)):null;mn&&(Hr=Hr?zo(Hr,kr(xt,$(R))):kr(xt,$(R))),dr=new Se(Hr,dr,zd())}let Br=new J(ce,[yt],dr);Vn&&(Br=new Se(kr(yt,$(q)),Br));let Uo=Rb(pr,V,new et(yt,Te(on,xe(pr,$(H))),Br)),jr=Be("ig_lb"),qr=Be("ig_bidx"),Ps=Te(ye,Dn(qr,$(T))),ms=Te(it,hn(qr,$(T))),va=Dn(Ps,$(B)),Oi=hn(Ps,$(B)),Bi=Dn(Oi,$(d)),ka=hn(Oi,$(d)),tp=Dn(ms,$(E)),zu=hn(ms,$(E)),Na=Dn(zu,$(m)),Aa=hn(zu,$(m)),Wo=Mb(Te(xe(Na,$(g)),xe(Bi,$(w))),$(_)),Oa=Mb(Te(xe(Aa,$(y)),xe(ka,$(x))),$(b)),Ri=new U(s,[tp,va,Wo,Oa]),Ko=zo(zo(Ib(Wo,$(0)),kr(Wo,$(u))),zo(Ib(Oa,$(0)),kr(Oa,$(p))));mn&&(Ko=zo(Ko,kr(Ps,$(R)))),cr&&(Ko=zo(Ko,kr(ms,$(O)))),Ri=new Se(Ko,Ri,zd());let Mi=new J(Le,[qr],Ri);Or&&(Mi=new Se(kr(qr,$(D)),Mi));let Ba=Rb(jr,ue,new et(qr,Te(on,xe(jr,$(H))),Mi)),Ot=Be("ig_kk"),lo=Be("ig_fi"),np=Be("ig_fj"),Vw=kn(lo,A,new J(W,[lo],new U(ce,[Te(xe(Te(xe(zt,$(A)),lo),$(v)),Ot)]))),Gw=kn(np,N,new J(ie,[np],new U(Le,[Te(xe(Ot,$(T)),Te(xe(rt,$(N)),np))]))),Vu=Be("ig_mi"),Gu=Be("ig_ni"),jw=kn(Vu,A,kn(Gu,N,new J(j,[Xe(Vu,Gu)],Te(new U(j,[Xe(Vu,Gu)]),xe(new U(W,[Vu]),new U(ie,[Gu])))))),qw=kn(Ot,v,new te([Vw,Gw,jw])),$s=Be("ig_kt"),gs=new et(ye,xe($s,$(v)),new te([Uo,Ba,new Fn,qw,new Fn])),Rr=Rb($s,G,gs),hr=Be("ig_wm"),Ur=Be("ig_wn"),Ii=Te(Te(Zt,xe(zt,$(A))),hr),co=Te(Te(it,xe(rt,$(N))),Ur),Wr=Dn(co,$(E)),Kr=hn(co,$(E)),Ho=Dn(Kr,$(m)),Qn=hn(Kr,$(m)),Ei=new J(o,[Wr,Ii,Ho,Qn],new U(j,[Xe(hr,Ur)]));if(un||cr){let Hr=un?kr(Ii,$(k)):null;cr&&(Hr=Hr?zo(Hr,kr(co,$(O))):kr(co,$(O))),Ei=new Se(Hr,Ei)}let Yo=kn(hr,A,kn(Ur,N,Ei)),Uw=new te([an,Rr,Yo]),vh=new qe(j,"local",new qe(W,"local",new qe(ie,"local",Uw))),Ww=new et(on,Te(xe(zt,$(F)),rt),new et(Zt,xe(ke,$(S)),new et(it,xe(je,$(T)),vh))),Kw=Math.ceil(O/T),Hw=Math.ceil(k/S),kh=ya(ke,"blockIdx.y",Hw,ya(je,"blockIdx.x",Kw,ya(zt,"threadIdx.y",C,ya(rt,"threadIdx.x",F,Ww))));return new qe(ce,"shared",new qe(Le,"shared",kh))}function KL(n,e,t){if(!HL(n,{M:e.O,N:e.N*e.Oh*e.Ow,K:e.Cin*e.Kh*e.Kw},t))return!1;let{BM:r,BN:s,BK:o,TM:i,TN:a}=t,u=e.O,p=e.N*e.Oh*e.Ow,c=e.Cin*e.Kh*e.Kw;if(u%r!==0||p%s!==0||c%o!==0||[r,s,o,i,a].some(d=>d%Lv!==0))return!1;let f=r/i*(s/a);return!(r*o%(Lv*f)!==0||o*s%f!==0)}function b4(n,e,t){let{weight:r,input:s,output:o}=n,{N:i,Cin:a,H:u,W:p,O:c,Kh:f,Kw:d,Oh:h,Ow:m,sH:g,sW:y,pH:_,pW:b,dH:w,dW:x}=e,{BM:S,BN:T,BK:v,TM:A,TN:N}=t,k=Lv,O=c,R=i*h*m,B=a*f*d,E=f*d,F=h*m,C=u*p,H=a*u*p,G=T/N,q=S/A,D=G*q,V=B/v,ue=S*v,ce=v*T,Le=ue/D,j=ce/D,W=Le/k,ie=v/k,je=(h-1)*g+(f-1)*w-_,ke=(m-1)*y+(d-1)*x-b,rt=_>0||je>=u,zt=b>0||ke>=p,on=new me(r.name,[O*B>0?O*B:1],r.dtype,r.scope),Zt=new me(s.name,[i*H>0?i*H:1],s.dtype,s.scope),it=new me("iv_As",[2*ue],"f32","shared");it.align16=!0;let ye=new me("iv_Bs",[2*ce],"f32","shared");ye.align16=!0;let Xe=new me("iv_acc",[A*N],"f32","local"),Nn=new me("iv_af",[A],"f32","local");Nn.align16=!0;let Qt=new me("iv_bf",[N],"f32","local");Qt.align16=!0;let an=new me("iv_ra",[Le],"f32","local");an.align16=!0;let un=new me("iv_rb",[j],"f32","local"),mn=Be("iv_bx"),cr=Be("iv_by"),Vn=Be("iv_tx"),Or=Be("iv_ty"),pr=Be("iv_tid"),yt=Be("iv_brow"),fr=Be("iv_bcol"),xt=l(($s,gs)=>Te(xe($s,$(N)),gs),"accIdx"),Vr=0,Ds=Be("iv_im"),uo=Be("iv_in"),Gr=kn(Ds,A,kn(uo,N,new J(Xe,[xt(Ds,uo)],zd()))),dr=l($s=>{let gs=Vr++,Rr=Be("iv_la"+gs),hr=Be("iv_c"+gs),Ur=Dn(hr,$(ie)),Ii=xe(hn(hr,$(ie)),$(k)),co=kn(Rr,W,new et(hr,Te(pr,xe(Rr,$(D))),new al(an,xe(Rr,$(k)),on,Te(xe(Te(yt,Ur),$(B)),Te($s,Ii)),k))),Wr=Be("iv_lb"+gs),Kr=Be("iv_e"+gs),Ho=Dn(Kr,$(T)),Qn=hn(Kr,$(T)),Ei=Te(fr,Qn),Yo=Te($s,Ho),Uw=Dn(Yo,$(E)),vh=hn(Yo,$(E)),Ww=Dn(vh,$(d)),Kw=hn(vh,$(d)),Hw=Dn(Ei,$(F)),kh=hn(Ei,$(F)),Hr=Dn(kh,$(m)),Yw=hn(kh,$(m)),Jw=Mb(Te(xe(Hr,$(g)),xe(Ww,$(w))),$(_)),Zw=Mb(Te(xe(Yw,$(y)),xe(Kw,$(x))),$(b)),GD=Te(Te(Te(xe(Hw,$(H)),xe(Uw,$(C))),xe(Jw,$(p))),Zw),Qw=new U(Zt,[GD]),ju=null;if(rt&&(ju=zo(Ib(Jw,$(0)),kr(Jw,$(u)))),zt){let hN=zo(Ib(Zw,$(0)),kr(Zw,$(p)));ju=ju?zo(ju,hN):hN}ju&&(Qw=new Se(ju,Qw,zd()));let jD=kn(Wr,j,new et(Kr,Te(pr,xe(Wr,$(D))),new J(un,[Wr],Qw)));return new te([co,jD])},"prefetch"),Br=l(($s,gs)=>{let Rr=Vr++,hr=Be("iv_la"+Rr),Ur=Be("iv_c"+Rr),Ii=Dn(Ur,$(ie)),co=xe(hn(Ur,$(ie)),$(k)),Wr=[];for(let Yo=0;Yo<k;Yo++)Wr.push(new J(it,[Te($s,Te(xe(Te(co,$(Yo)),$(S)),Ii))],new U(an,[Te(xe(hr,$(k)),$(Yo))])));let Kr=kn(hr,W,new et(Ur,Te(pr,xe(hr,$(D))),new te(Wr))),Ho=Be("iv_lb"+Rr),Qn=Be("iv_e"+Rr),Ei=kn(Ho,j,new et(Qn,Te(pr,xe(Ho,$(D))),new J(ye,[Te(gs,Qn)],new U(un,[Ho]))));return new te([Kr,Ei])},"commit"),Uo=l(($s,gs)=>{let Rr=Vr++,hr=Be("iv_kk"+Rr),Ur=[],Ii=Te($s,Te(xe(hr,$(S)),xe(Or,$(A))));for(let Qn=0;Qn<A/k;Qn++)Ur.push(new al(Nn,$(Qn*k),it,Te(Ii,$(Qn*k)),k));let co=Te(gs,Te(xe(hr,$(T)),xe(Vn,$(N))));for(let Qn=0;Qn<N/k;Qn++)Ur.push(new al(Qt,$(Qn*k),ye,Te(co,$(Qn*k)),k));let Wr=Be("iv_mi"+Rr),Kr=Be("iv_ni"+Rr),Ho=kn(Wr,A,kn(Kr,N,new J(Xe,[xt(Wr,Kr)],Te(new U(Xe,[xt(Wr,Kr)]),xe(new U(Nn,[Wr]),new U(Qt,[Kr]))))));return kn(hr,v,new te([...Ur,Ho]))},"computeMMA"),jr=Be("iv_kt"),qr=Be("iv_p"),Ps=Be("iv_pN"),ms=xe(qr,$(ue)),va=xe(qr,$(ce)),Oi=xe(Ps,$(ue)),Bi=xe(Ps,$(ce)),ka=kr(jr,$(V-1)),tp=new te([dr($(0)),Br($(0),$(0)),new Fn]),zu=new et(qr,hn(jr,$(2)),new et(Ps,hn(Te(jr,$(1)),$(2)),new te([new Se(ka,dr(xe(Te(jr,$(1)),$(v)))),Uo(ms,va),new Se(ka,new te([Br(Oi,Bi),new Fn]))]))),Na=Rb(jr,V,zu),Aa=Be("iv_wm"),Wo=Be("iv_wn"),Oa=Te(Te(yt,xe(Or,$(A))),Aa),Ri=Te(Te(fr,xe(Vn,$(N))),Wo),Ko=Dn(Ri,$(F)),Mi=hn(Ri,$(F)),Ba=Dn(Mi,$(m)),Ot=hn(Mi,$(m)),lo=new J(o,[Ko,Oa,Ba,Ot],new U(Xe,[xt(Aa,Wo)])),np=kn(Aa,A,kn(Wo,N,lo)),Vw=new te([Gr,tp,Na,np]),Gw=new qe(Xe,"local",new qe(Nn,"local",new qe(Qt,"local",new qe(an,"local",new qe(un,"local",Vw))))),Vu=new et(pr,Te(xe(Or,$(G)),Vn),new et(yt,xe(cr,$(S)),new et(fr,xe(mn,$(T)),Gw))),Gu=R/T,jw=O/S,qw=ya(cr,"blockIdx.y",jw,ya(mn,"blockIdx.x",Gu,ya(Or,"threadIdx.y",q,ya(Vn,"threadIdx.x",G,Vu))));return new qe(it,"shared",new qe(ye,"shared",qw))}function HL(n,e,t){let{BM:r,BN:s,BK:o,TM:i,TN:a}=t;if(![r,s,o,i,a].every(m=>typeof m=="number"&&m>0)||r%i!==0||s%a!==0)return!1;let u=s/a,p=r/i,c=u*p,f=n.warpSize||32;return!(c%f!==0||c>(n.maxThreadsPerBlock||1024)||(r*o+o*s)*4*2>(n.sharedMemoryBytes||49152)||i*a+i+a+f>(n.registersPerThread||255))}function T4(n,e){for(let t of w4)if(KL(n,e,t))return t;return null}function YL(n,e,t){let r=n.func,s=r.convInfo;if(!s)return!1;let i=Fo(r.body).find(_=>/^conv_acc_/.test(_));if(!i)return!1;let a=Rd(r.body,i);if(!a||a.reads.length<2||a.writes.length<1)return!1;let u=a.reads[0].buffer,p=a.reads[1].buffer,c=a.writes[0].buffer;if(!u||!p||!c)return!1;let f=s.O,d=s.N*s.Oh*s.Ow,h=s.Cin*s.Kh*s.Kw;if(h<128||d<64)return!1;let m=t&&t.convConfig,g=t&&t.convNoVec,y=null;if(!g){let _=m?KL(e,s,m)?m:null:T4(e,s);_&&(y=b4({weight:p,input:u,output:c},s,_))}if(!y){let _=m?HL(e,{M:f,N:d,K:h},m)?m:null:Tb(e,{M:f,N:d,K:h});if(!_)return!1;y=_4({weight:p,input:u,output:c},s,_)}return n.func.body=y,n.func._setChild&&n.func._setChild("body",y),n.func.gpuRegisterBlocked=!0,!0}var $,zd,Be,Te,Mb,xe,Dn,hn,kr,Ib,zo,Rb,kn,ya,Lv,w4,Cv=M(()=>{"use strict";Fe();ei();Bc();xb();$=l(n=>new L(n),"I"),zd=l(()=>new de(0),"FZERO"),Be=l(n=>new Oe(n,"i32"),"IV"),Te=l((n,e)=>new P("+",n,e),"ADD"),Mb=l((n,e)=>new P("-",n,e),"SUB"),xe=l((n,e)=>new P("*",n,e),"MUL"),Dn=l((n,e)=>new P("//",n,e),"DIV"),hn=l((n,e)=>new P("%",n,e),"MOD"),kr=l((n,e)=>new Ke("lt",n,e),"LT"),Ib=l((n,e)=>new Ke("ge",n,e),"GE"),zo=l((n,e)=>new P("&&",n,e),"AND"),Rb=l((n,e,t)=>new he(n,$(0),$(e),z.SERIAL,t),"forS"),kn=l((n,e,t)=>new he(n,$(0),$(e),z.UNROLLED,t),"forU"),ya=l((n,e,t,r)=>new he(n,$(0),$(t),z.THREAD_BINDING,r,e),"forT");l(WL,"detectPureConv");l(_4,"buildImplicitGemmConv");Lv=4;l(KL,"vectorizableConvConfig");l(b4,"buildVectorizedImplicitGemmConv");l(HL,"validConvConfig");w4=[{BM:128,BN:64,BK:8,TM:8,TN:8},{BM:64,BN:64,BK:8,TM:8,TN:8},{BM:64,BN:64,BK:8,TM:4,TN:8},{BM:64,BN:32,BK:8,TM:8,TN:8},{BM:32,BN:64,BK:8,TM:4,TN:8}];l(T4,"pickVectorizedConvConfig");l(YL,"applyImplicitGemmConv")});function JL(n,e,t,r,s,o,i,a){let u=new me(n,[t,r],e.dtype,"shared"),p=new J(u,[s,o],new U(e,[i,a]));return{tile:u,fill:p}}function v4(n,e,t){let{A:r,B:s,C:o,M:i,N:a,K:u,transB:p}=n,c=u/t,f=Mc("ts_by"),d=Mc("ts_bx"),h=Mc("ts_ty"),m=Mc("ts_tx"),g=Mc("ts_ko"),y=Mc("ts_ki"),_=new me("ts_acc",[1],"f32","local"),b=Ic(Eb(f,ds(e)),h),w=Ic(Eb(d,ds(e)),m),x=Eb(g,ds(t)),S=JL("ts_As",r,e,t,h,m,b,Ic(x,m)),T=JL("ts_Bs",s,t,e,h,m,p?w:Ic(x,h),p?Ic(x,h):w),v=S4(y,t,new J(_,[ds(0)],Ic(new U(_,[ds(0)]),Eb(new U(S.tile,[h,y]),new U(T.tile,[y,m]))))),A=new te([S.fill,T.fill,new Fn,v,new Fn]),N=x4(g,c,A),k=new J(_,[ds(0)],new de(0)),O=new J(o,[b,w],new U(_,[ds(0)])),R=new qe(_,"local",new te([k,N,O]));return Lb(f,"blockIdx.y",i/e,Lb(d,"blockIdx.x",a/e,new qe(S.tile,"shared",new qe(T.tile,"shared",Lb(h,"threadIdx.y",e,Lb(m,"threadIdx.x",e,R))))))}function k4(n,e){let{M:t,N:r,K:s}=e,o=16,i=16;return o*o>(n.maxThreadsPerBlock||1024)||(o*i+i*o)*4>(n.sharedMemoryBytes||49152)||t%o!==0||r%o!==0||s%i!==0||t<o||r<o||s<i?null:{BS:o,BK:i}}function N4(n,e){let r=Fo(n.func.body).filter(o=>/^q?conv_(init|acc)_/.test(o));if(r.length===0)return!1;let s=new to(e);for(let o of r)s.applyToBlock(n,o);return!0}function A4(n,e,t={}){if(!e.isGPU())return!1;let r=DL(n.func);if(!r)return!1;let s=r.dims;if(t&&t.primitiveMatmul&&(s.batch||1)===1&&!r.epilogue){let a=k4(e,s);if(a){let u=v4(s,a.BS,a.BK);return n.func.body=u,n.func._setChild&&n.func._setChild("body",u),n.func.gpuRegisterBlocked=!0,!0}}let o=Tb(e,s);if(!o)return!1;let i=Av(s,o,r.epilogue);return n.func.body=i,n.func._setChild&&n.func._setChild("body",i),n.func.gpuRegisterBlocked=!0,!0}function Cb(n,e,t={}){if(!e.isGPU()||e.isWebGPU&&e.isWebGPU())return!1;let r=A4(n,e,t);return r||(r=YL(n,e,t)),r||(r=N4(n,e)),r}var ds,Mc,Ic,Eb,x4,S4,Lb,Dv=M(()=>{"use strict";xb();Bc();Cv();Do();Fe();ei();ds=l(n=>new L(n),"I"),Mc=l(n=>new Oe(n,"i32"),"IV"),Ic=l((n,e)=>new P("+",n,e),"ADD"),Eb=l((n,e)=>new P("*",n,e),"MUL"),x4=l((n,e,t)=>new he(n,ds(0),ds(e),z.SERIAL,t),"forS"),S4=l((n,e,t)=>new he(n,ds(0),ds(e),z.UNROLLED,t),"forU"),Lb=l((n,e,t,r)=>new he(n,ds(0),ds(t),z.THREAD_BINDING,r,e),"forT");l(JL,"stageTileToShared");l(v4,"buildTiledSharedMatmul");l(k4,"pickTiledConfig");l(N4,"applyDeterministicGpuConv");l(A4,"applyDeterministicGpuMatmul");l(Cb,"applyDeterministicGpuSchedule")});function Pv(n){return n*1664525+1013904223&2147483647}function B4(n,e,t){let r={...n};for(let s of e.variables)t.rngFloat()<t.mutationRate&&(r[s.name]=s.sample(o=>t.rng(o)));return r}function ZL(n={}){return n.strategy==="random"?new $v({numTrials:n.numTrials,seed:n.seed,deadline:n.deadline}):new Fv({populationSize:n.populationSize,numGenerations:n.numGenerations,mutationRate:n.mutationRate,eliteRatio:n.eliteRatio,seed:n.seed,deadline:n.deadline})}var Db,$v,Fv,O4,QL=M(()=>{"use strict";l(Pv,"nextLcg");Db=class{static{l(this,"SearchCandidate")}constructor(e,t,r){this.sketchName=e,this.params=t,this.score=r}},$v=class{static{l(this,"RandomSearch")}constructor(e={}){this.numTrials=e.numTrials||64,this.seed=e.seed||42,this.deadline=e.deadline||null,this._rngState=this.seed}_rng(e){return this._rngState=Pv(this._rngState),this._rngState%e}_expired(){return this.deadline?this.deadline.expired:!1}search(e,t){let r=[];for(let s of e){if(this._expired())break;for(let o=0;o<this.numTrials&&!this._expired();o++){let i=s.sampleParams(u=>this._rng(u)),a=t(s,i);a&&r.push(new Db(s.name,i,a.score))}}return r.sort((s,o)=>o.score-s.score),{candidates:r,population:null}}},Fv=class{static{l(this,"EvolutionarySearch")}constructor(e={}){this.populationSize=e.populationSize||32,this.numGenerations=e.numGenerations||10,this.mutationRate=e.mutationRate||.3,this.eliteRatio=e.eliteRatio||.2,this.seed=e.seed||42,this.deadline=e.deadline||null,this._rngState=this.seed}_rng(e){return this._rngState=Pv(this._rngState),this._rngState%e}_rngFloat(){return this._rngState=Pv(this._rngState),this._rngState/2147483647}_expired(){return this.deadline?this.deadline.expired:!1}search(e,t,r=null){let s=r&&r.length?r:this._initPopulation(e),o=new Map,i=l((u,p)=>{let c=u.name+"|"+JSON.stringify(p);if(o.has(c))return o.get(c);let f=t(u,p);return o.set(c,f),f},"evalMemo");for(let u=0;u<this.numGenerations&&!this._expired();u++){let p=[];for(let h of s){let m=i(h.sketch,h.params);m&&p.push({...h,score:m.score})}if(p.sort((h,m)=>m.score-h.score),p.length===0)break;let c=Math.max(1,Math.floor(p.length*this.eliteRatio)),f=p.slice(0,c),d=f.map(h=>({sketch:h.sketch,params:{...h.params}}));for(;d.length<this.populationSize;){let h=f[this._rng(f.length)],m=f[this._rng(f.length)];if(h.sketch!==m.sketch){let g=this._rngFloat()<.5?h:m;d.push(this._mutate(g.sketch,{...g.params}))}else d.push(this._mutate(h.sketch,this._crossover(h,m)))}s=d}let a=[];for(let u of s){let p=i(u.sketch,u.params);p&&a.push(new Db(u.sketch.name,u.params,p.score))}return a.sort((u,p)=>p.score-u.score),{candidates:a,population:s}}_initPopulation(e){let t=[];for(let r=0;r<this.populationSize;r++){let s=e[this._rng(e.length)],o=s.sampleParams(i=>this._rng(i));t.push({sketch:s,params:o})}return t}_crossover(e,t){if(e.sketch!==t.sketch)return{...e.params};let r={};for(let s of e.sketch.variables)r[s.name]=this._rngFloat()<.5?e.params[s.name]:t.params[s.name];return r}_mutate(e,t){let r={rngFloat:l(()=>this._rngFloat(),"rngFloat"),rng:l(i=>this._rng(i),"rng"),mutationRate:this.mutationRate},s=[B4,...O4],o=t;for(let i of s)o=i(o,e,r);return{sketch:e,params:o}}},O4=[];l(B4,"defaultResampleMutator");l(ZL,"createSearchStrategy")});function $b(n){let e={"threadIdx.x":1,"threadIdx.y":1,"threadIdx.z":1},t=new Set,r=l(s=>{if(!(!s||typeof s!="object"||t.has(s))){if(t.add(s),s.type==="ForNode"&&e[s.threadTag]!==void 0){let o=s.extent&&s.extent.type==="IntImmNode"?s.extent.value:1;o>e[s.threadTag]&&(e[s.threadTag]=o)}for(let o in s){let i=s[o];Array.isArray(i)?i.forEach(r):i&&typeof i=="object"&&r(i)}}},"visit");return r(n.body),R4.reduce((s,o)=>s*e[o],1)}var R4,Pb,XL=M(()=>{"use strict";du();Px();hv();Id();QL();yv();R4=["threadIdx.x","threadIdx.y","threadIdx.z"];l($b,"gpuThreadBlockSize");Pb=class{static{l(this,"BlockTuningSession")}constructor(e){this.target=e.target,this.primFunc=e.primFunc,this.blockName=e.blockName,this.sketches=e.sketches,this.benchmarkRunner=e.benchmarkRunner||null,this.config=e.config,this.deadline=e.deadline||null,this._warn=e.warn||(()=>{}),this._warnedEvalSketches=new Set;let t=this.sketches.some(o=>o.name==="fused");t?(this.learnedModel=new Ac,this.costModel=new Oc(e.costModel.analytical,this.learnedModel)):(this.costModel=e.costModel,this.learnedModel=e.learnedModel);let r=t?null:RL(e.primFunc,e.blockName,e.blockMap);this.evalFunc=r||e.primFunc,this.evalBlockName=e.blockName,this.sketchByName=new Map;for(let o of this.sketches)this.sketchByName.set(o.name,o);let s=this.sketches.length===1&&typeof this.sketches[0].enumerate=="function";this.enumSketch=s?this.sketches[0]:null,this.enumParams=s?this.sketches[0].enumerate():null,this.strategy=ZL({...this.config,deadline:this.deadline}),this.population=null,this._best=null,this.plateaued=!1}runRound(){let e=this._best?this._best.measuredScore:-1/0,t=this._produceCandidates();if(t.length===0)return this.plateaued=!0,0;if(this.benchmarkRunner)this._measureAndLearn(t);else{let s=t[0];this._consider({sketchName:s.sketchName,params:s.params,score:s.score,measuredScore:s.score}),this.plateaued=!0}let r=this._best?this._best.measuredScore:-1/0;return Math.max(0,r-e)}best(){return this._best?{sketchName:this._best.sketchName,params:this._best.params,score:this._best.score,medianMs:this._best.medianMs??null,minMs:this._best.minMs??null}:null}bestTrace(){if(!this._best)return null;let e=this.sketchByName.get(this._best.sketchName);if(!e)return null;try{let t=new Cn(Ti(this.primFunc));return e.instantiate(this._best.params)(t,this.blockName,this.target),t.trace.serialize()}catch(t){return this._warn("best-trace",this.blockName,t),null}}_produceCandidates(){if(this.enumSketch){let s=[];for(let o of this.enumParams){let i=this._evaluate(this.enumSketch,o);i&&s.push({sketchName:this.enumSketch.name,params:o,score:i.score})}return s.sort((o,i)=>i.score-o.score),s}let e=l((s,o)=>this._evaluate(s,o),"evaluator"),{candidates:t,population:r}=this.strategy.search(this.sketches,e,this.population);return this.population=r,t}_evaluate(e,t){try{let r=Ti(this.evalFunc),s=new Cn(r);if(e.instantiate(t)(s,this.evalBlockName,this.target),Yl.validate(r).length>0)return null;let i=this.target.maxThreadsPerBlock;return this.target.isGPU&&this.target.isGPU()&&i&&$b(r)>i?null:{score:this.costModel.score(r)}}catch(r){return this._warnedEvalSketches.has(e.name)||(this._warnedEvalSketches.add(e.name),this._warn("evaluate-candidate",this.blockName,r)),null}}_measureAndLearn(e){let t=e.slice(0,this.config.topKForBenchmark);for(let r of t){if(this.deadline&&this.deadline.expired)break;let s=this._measure(r);if(!s)continue;let o=-s.result.medianMs;this.learnedModel.addSample(s.features,o),this._consider({sketchName:r.sketchName,params:r.params,score:o,measuredScore:o,medianMs:s.result.medianMs,minMs:s.result.minMs})}this.learnedModel.train()}_measure(e){let t=this.sketchByName.get(e.sketchName);if(!t)return null;let r,s;try{r=Ti(this.primFunc),t.instantiate(e.params)(new Cn(r),this.blockName,this.target),s=Ti(this.evalFunc),t.instantiate(e.params)(new Cn(s),this.evalBlockName,this.target)}catch(i){return this._warn("measure-candidate",this.blockName,i),null}let o=this.benchmarkRunner.run(r);return o?{result:o,features:Mu.extractStatements(s)}:null}_consider(e){(!this._best||e.measuredScore>this._best.measuredScore)&&(this._best=e)}}});var zv,Fb,e2=M(()=>{"use strict";zv=class{static{l(this,"GradientSchedulerPolicy")}pick(e){let t=e.filter(i=>!i.plateaued);if(t.length===0)return null;let r=t.filter(i=>i.rounds===0);if(r.length>0)return r[0];let s=null,o=-1/0;for(let i of t){let a=i.gainEwma!==void 0?i.gainEwma:i.lastGain,u=i.weight*a;u>o&&(o=u,s=i)}return s}},Fb=class{static{l(this,"TaskScheduler")}constructor(e=null){this.policy=e||new zv}run(e,t,r={}){let s=r.maxRoundsPerTask??8,o=r.plateauPatience??2,i=r.gainEwmaAlpha??.5;for(let a of e)a.rounds=0,a.lastGain=0,a.gainEwma=0,a.stale=0,a.plateaued=!1;for(;!(t&&t.expired);){let a=this.policy.pick(e);if(!a)break;let u=a.session.runRound();a.rounds++,a.lastGain=u,a.gainEwma=i*u+(1-i)*a.gainEwma,u<=0?a.stale++:a.stale=0,(a.session.plateaued||a.stale>=o||a.rounds>=s)&&(a.plateaued=!0)}}}});function M4(n){if(n.isCPU())return null;let e=B1(n.kind);if(!e)throw new Error("hardwareMeasure requested for target '"+n.kind+"' but no measurer is registered for it; the corresponding runtime must be loaded (Node: import '#io/cuda_runtime') before compiling");return e}var zb,Vb,t2=M(()=>{"use strict";du();yv();zL();GL();qL();UL();Ev();Bc();Bv();Do();Dv();XL();Id();Fe();e2();Hm();l(M4,"resolveMeasurer");zb=class{static{l(this,"AutotuneConfig")}constructor(e={}){this.strategy=e.strategy||"evolutionary",this.numTrials=e.numTrials||64,this.populationSize=e.populationSize||32,this.numGenerations=e.numGenerations||10,this.mutationRate=e.mutationRate,this.eliteRatio=e.eliteRatio,this.seed=e.seed||42,this.timeBudgetMs=e.timeBudgetMs||3e4,this.clock=e.clock||null,this.tuningDB=e.tuningDB||null,this.useTuningDB=e.useTuningDB!==!1,this.measurer=e.measurer||null,this.hardwareMeasure=e.hardwareMeasure??!1,this.enableBenchmark=e.enableBenchmark??(this.hardwareMeasure||!!e.measurer),this.benchmarkWarmup=e.benchmarkWarmup??3,this.benchmarkRepeat=e.benchmarkRepeat??10,this.benchmarkMaxCv=e.benchmarkMaxCv??0,this.topKForBenchmark=e.topKForBenchmark??5,this.maxRoundsPerTask=e.maxRoundsPerTask??8,this.plateauPatience=e.plateauPatience??2,this.schedulerPolicy=e.schedulerPolicy||null,this.onWarning=e.onWarning||null}},Vb=class{static{l(this,"Autotuner")}constructor(e,t={},r=null){this.target=e,this.config=t instanceof zb?t:new zb(t),this.trace=r,this._funcName=null,this.config.hardwareMeasure&&(this.config.measurer=M4(e)),this.analyticalModel=new yb(e),this.learnedModel=new Ac,this.costModel=new Oc(this.analyticalModel,this.learnedModel),this.db=this.config.tuningDB instanceof Fd?this.config.tuningDB:new Fd;let s=l((o,i,a)=>this._warn(o,i,a),"warn");this.benchmarkRunner=this.config.enableBenchmark?new Ob(e,{warmup:this.config.benchmarkWarmup,repeat:this.config.benchmarkRepeat,maxCv:this.config.benchmarkMaxCv,measurer:this.config.measurer,warn:s}):null,this.scheduler=new Fb(this.config.schedulerPolicy)}_warn(e,t,r){let s=r&&r.message?r.message:String(r);if(this.config.onWarning)try{this.config.onWarning({stage:e,func:this._funcName,block:t||null,message:s,error:r})}catch(o){}this.trace&&this.trace.warn("autotune",this._funcName,`${e}${t?" ["+t+"]":""}: ${s}`)}tune(e,t=null){this._funcName=e.name;let r=t?[t]:Fo(e.body),s=kb(e.body),o=Pd(e),i=new Bb(this.config.timeBudgetMs,this.config.clock),a=new Map,u=new Map;for(let f of r){let d=Nb(e,f,this.target,s);u.set(f,d);let h=a.get(d);if(h){h.weight++;continue}if(this.config.useTuningDB&&this.db.has(d)){a.set(d,{key:d,kind:"cache",cached:this.db.lookup(d),weight:1});continue}let m=Iv(e,f,this.target,s,{richGpu:this.config.richGpu??!!this.config.measurer,dag:o});if(m.length===0){a.set(d,{key:d,kind:"empty",weight:1});continue}let g=new Pb({target:this.target,primFunc:e,blockName:f,blockMap:s,sketches:m,costModel:this.costModel,learnedModel:this.learnedModel,benchmarkRunner:this.benchmarkRunner,config:this.config,deadline:i,warn:l((y,_,b)=>this._warn(y,_,b),"warn")});a.set(d,{key:d,kind:"session",session:g,weight:1})}let p=[...a.values()].filter(f=>f.kind==="session");p.length>0&&this.scheduler.run(p,i,this.config);let c=new Map;for(let f of r){let d=a.get(u.get(f));if(d.kind==="cache"){c.set(f,{sketchName:d.cached.sketchName,params:d.cached.params,score:d.cached.score,fromCache:!0});continue}if(d.kind==="empty")continue;let h=d.session.best();if(h){if(this.config.useTuningDB&&!d.stored){let m=new $d(d.key,h.sketchName,h.params,h.score,d.session.bestTrace(),this.db.version);m.medianMs=h.medianMs||null,m.minMs=h.minMs||null,this.db.store(d.key,m),d.stored=!0}c.set(f,{sketchName:h.sketchName,params:h.params,score:h.score,fromCache:!1,medianMs:h.medianMs,minMs:h.minMs})}}return c}tuneAndApply(e,t=null){let r=this.tune(e,t);if(r.size>0){let s=this._applyBestSchedule(e,r);if(s)return{func:s.func,results:r,applied:!0}}return{func:e,results:r,applied:!1}}_applyBestSchedule(e,t){let r=this._buildDefaultSchedule(e),s=!!r&&this._scheduleIsValid(r),o=s&&this._isStrongBackendSchedule(r),i=this._buildTunedSchedule(e,t),a=!!i&&this._scheduleIsValid(i),u=a&&this._isStrongBackendSchedule(i),p=!o||u&&this.config.measurer!=null;return a&&p?(this._adoptSchedule(e,i),{func:e}):(i&&!a?this._warn("tuned-schedule-invalid",null,new Error("tuned schedule exceeds target thread-block limit; falling back to default")):a&&o&&this._warn("baseline-preferred",null,new Error("cost-model-only tuning cannot displace the deterministic GPU schedule without hardware measurement; keeping the deterministic kernel")),s?(this._adoptSchedule(e,r),{func:e}):a?(this._adoptSchedule(e,i),{func:e}):(this._warn("no-valid-schedule",null,new Error("neither tuned nor default schedule is valid; leaving function unscheduled")),null))}_isStrongBackendSchedule(e){return!!e&&e.gpuRegisterBlocked===!0}_buildTunedSchedule(e,t){try{let r=Ti(e),s=new Cn(r),o=kb(r.body),i=Pd(r),a=new Set,u=[];for(let c of t)if(c[1].sketchName==="fused"){let f=Sb(r,i,c[0],Tt);f&&a.add(f),u.unshift(c)}else u.push(c);let p=new Set;for(let[c,f]of u)if(!(a.has(c)||p.has(f))&&(p.add(f),!(!f.sketchName||!f.params)))try{let h=Iv(r,c,this.target,o,{richGpu:this.config.richGpu??!!this.config.measurer,dag:i}).find(m=>m.name===f.sketchName);h&&this._fitsThreadBlock(r,c,h,f.params)&&h.instantiate(f.params)(s,c,this.target)}catch(d){this._warn("apply-tuned-block",c,d);continue}return this._scheduleResidualBlocks(s,a),r}catch(r){return this._warn("build-tuned-schedule",null,r),null}}_scheduleResidualBlocks(e,t){let r=null;for(let s of Fo(e.func.body))if(!(t.has(s)||this._blockIsParallelized(e,s))){r||(r=new to(this.target));try{r.applyToBlock(e,s)}catch(o){this._warn("residual-block",s,o)}}}_blockIsParallelized(e,t){let r;try{r=e.getLoops(t)}catch(s){return this._warn("block-loops",t,s),!0}for(let s of r)if(s.kind===z.THREAD_BINDING||s.kind===z.PARALLEL||s.kind===z.VECTORIZED)return!0;return!1}_scheduleIsValid(e){return!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock?!0:$b(e)<=this.target.maxThreadsPerBlock}_buildDefaultSchedule(e){try{let t=Ti(e),r=new Cn(t);return Cb(r,this.target,this.config)||new to(this.target).applyToAllBlocks(r),t}catch(t){return this._warn("build-default-schedule",null,t),null}}_adoptSchedule(e,t){Object.assign(e,t),e._setChild("body",e.body)}_fitsThreadBlock(e,t,r,s){if(!this.target.isGPU||!this.target.isGPU()||!this.target.maxThreadsPerBlock)return!0;try{let o=Ti(e);return r.instantiate(s)(new Cn(o),t,this.target),$b(o)<=this.target.maxThreadsPerBlock}catch(o){return this._warn("fits-thread-block",t,o),!1}}}});var Gb,n2=M(()=>{"use strict";ma();du();Do();t2();Dv();Gb=class extends ar{static{l(this,"SchedulePass")}constructor(e){super("SchedulePass","scheduling"),this.config=e,this.target=e.target,this.snapshotPoint="afterScheduling",this._autotuner=null,this._policy=null}begin(e){let t=this.config.scheduling;t.autotune?this._autotuner=new Vb(this.target,t,e.trace):t.enabled&&(this._policy=new to(this.target,null,e.trace))}run(e,t){let r=this.config.scheduling,s=t.trace;if(r.autotune){if(e.cublasInfo||e._tensorIntrin)return;let o=performance.now(),i=this._autotuner.tuneAndApply(e),a=performance.now()-o,u=0,p=0;if(i&&i.results){p=i.results.size;for(let[c,f]of i.results)f.fromCache&&u++,s.explainsEnabled&&s.explain("schedule",c,f.sketchName,`autotuned: best of search${f.fromCache?" (cached)":""}, score ${f.score!=null?f.score.toFixed(3):"n/a"}`,{target:this.target.name,params:f.params})}s.autotuneStats(e.name,{durationMs:a,blockCount:p,applied:!!(i&&i.applied),cacheHits:u})}else if(r.enabled||r.gpuTiling){if(e.cublasInfo||e._tensorIntrin)return;let o=performance.now(),i=new Cn(e);!Cb(i,this.target,r)&&r.enabled&&this._policy.applyToAllBlocks(i),s.functionEvent("scheduling",e.name,{durationMs:performance.now()-o})}}}});function r2(n){let e={analyzer:new Xs,simp:null};e.simp=new Sy(e.analyzer);let t=hs(n.body,e);return n.body=t,n._setChild("body",t),n}function I4(n,e,t){let r=n.analyzer.getVarBound(e);return t&&t.type==="IntImmNode"&&t.value>0?n.analyzer.bind(e,0,t.value-1):n.analyzer.setVarBound(e,null),r}function hs(n,e){if(!n||typeof n!="object")return n;switch(n.type){case"ForNode":{let t=I4(e,n.loopVar.name,n.extent),r=hs(n.body,e);e.analyzer.setVarBound(n.loopVar.name,t);let s=new he(n.loopVar,n.min,n.extent,n.kind,r,n.threadTag);return n.annotations&&(s.annotations=n.annotations),s}case"BlockNode":{let t=[];for(let i of n.iterVars)i.iterVar&&(t.push([i.iterVar.name,e.analyzer.getVarBound(i.iterVar.name)]),e.analyzer.setVarBound(i.iterVar.name,i.binding?cu(e.analyzer,i.binding):null));let r=n.iterVars.map(E4(e)),s=hs(n.body,e),o=n.initBody?hs(n.initBody,e):null;for(let[i,a]of t)e.analyzer.setVarBound(i,a);return new fe(n.name,r,n.reads,n.writes,s,o)}case"SeqNode":return new te(n.stmts.map(t=>hs(t,e)));case"IfThenElseNode":{let t=ur(n.condition,e);return _i(e.analyzer,t)?hs(n.thenBody,e):lu(e.analyzer,t)?n.elseBody?hs(n.elseBody,e):new te([]):new Se(t,hs(n.thenBody,e),n.elseBody?hs(n.elseBody,e):null)}case"BufferStoreNode":return new J(n.buffer,n.indices.map(t=>ur(t,e)),ur(n.value,e));case"LetStmtNode":return new et(n.variable,ur(n.value,e),hs(n.body,e));case"AllocateNode":return new qe(n.buffer,n.scope,hs(n.body,e));case"WhileNode":return new $i(n.condVar,hs(n.condBody,e),hs(n.loopBody,e));case"EvaluateNode":return new il(ur(n.value,e));default:return n}}function E4(n){return e=>{if(!e.iterVar||!e.binding)return e;let t=ur(e.binding,n);return new Pa(e.iterVar,t,e.kind)}}function ur(n,e){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"IntImmNode":case"FloatImmNode":case"VariableNode":return n;case"BufferLoadNode":return new U(n.buffer,n.indices.map(t=>ur(t,e)));case"MathOpNode":{let t=ur(n.a,e),r=n.b?ur(n.b,e):null;return e.simp.simplify(new P(n.op,t,r))}case"CompareNode":{let t=ur(n.a,e),r=ur(n.b,e);return e.simp.simplify(new Ke(n.direction,t,r))}case"CastNode":return new ut(ur(n.expr,e),n.fromDtype,n.toDtype);case"CallExternNode":return new Ne(n.externName,n.args.map(t=>ur(t,e)),n.dtype);case"IfThenElseNode":{let t=ur(n.condition,e),r=ur(n.thenBody,e),s=n.elseBody?ur(n.elseBody,e):null;return _i(e.analyzer,t)?r:s!==null&&lu(e.analyzer,t)?s:new Se(t,r,s)}default:return n}}var s2=M(()=>{"use strict";Hl();wf();Fe();l(r2,"simplifyPrimFunc");l(I4,"bindLoopVar");l(hs,"simplifyStmt");l(E4,"simplifyIterVar");l(ur,"simplifyExpr")});var jb,o2=M(()=>{"use strict";ma();s2();jb=class extends ar{static{l(this,"SimplifyPass")}constructor(){super("SimplifyPass","simplify")}run(e,t){let r=performance.now();return r2(e),t.trace.functionEvent("simplify",e.name,{durationMs:performance.now()-r}),e}}});function i2(n){return!!n&&typeof n=="object"&&typeof n.name=="string"&&n.dtype!==void 0&&n.shape!==void 0&&n.type===void 0}var Vv,Gv,L4,qb,a2=M(()=>{"use strict";Vv=class{static{l(this,"BufferInterval")}constructor(e,t,r,s){this.buffer=e,this.firstUse=t,this.lastUse=r,this.scope=s}get size(){return this.buffer.sizeInBytes()}overlaps(e){return this.firstUse<=e.lastUse&&e.firstUse<=this.lastUse}},Gv=class{static{l(this,"BufferLivenessResult")}constructor(e,t,r){this.intervals=e,this.stmtOrder=t,this.paramBuffers=r}isParam(e){return this.paramBuffers.has(e)}getTemporaries(){let e=[];for(let[t,r]of this.intervals)this.paramBuffers.has(t)||e.push(r);return e}interfere(e,t){let r=this.intervals.get(e),s=this.intervals.get(t);return!r||!s?!1:r.overlaps(s)}},L4=new Set(["_parent","_parentKey","_parentIdx"]);l(i2,"isBuffer");qb=class{static{l(this,"BufferLiveness")}static analyze(e){let t=new Map,r=[],s=0,o=new Set;for(let[,f]of e.bufferMap)o.add(f);let i=[],a=l(f=>{if(!f)return;let d=t.get(f);d?(s<d.firstUse&&(d.firstUse=s),s>d.lastUse&&(d.lastUse=s)):(d=new Vv(f,s,s,f.scope),t.set(f,d)),i.push(f)},"touch"),u=l((f,d)=>{if(!(!f||typeof f!="object"||d.has(f))){if(d.add(f),i2(f)){a(f);return}i2(f.buffer)&&a(f.buffer);for(let h of Object.keys(f)){if(L4.has(h)||h==="buffer")continue;let m=f[h];if(!(!m||typeof m!="object"))if(Array.isArray(m))for(let g of m)u(g,d);else u(m,d)}}},"touchAll"),p=l((f,d)=>{for(let h=f;h<i.length;h++){let m=t.get(i[h]);m&&d>m.lastUse&&(m.lastUse=d)}},"extendRegion"),c=l(f=>{if(f)switch(f.type){case"SeqNode":for(let d of f.stmts)c(d);break;case"ForNode":{let d=s,h=i.length;u(f.min,new Set),u(f.extent,new Set),c(f.body);let m=s>d?s-1:d;p(h,m);break}case"WhileNode":{let d=s,h=i.length;u(f.condVar,new Set),c(f.condBody),c(f.loopBody);let m=s>d?s-1:d;p(h,m);break}case"BlockNode":r.push({idx:s,node:f});for(let d of f.reads)a(d.buffer);for(let d of f.writes)a(d.buffer);u(f.body,new Set),f.initBody&&u(f.initBody,new Set),s++;break;case"AllocateNode":a(f.buffer),c(f.body);break;case"IfThenElseNode":{let d=s,h=i.length;u(f.condition,new Set),c(f.thenBody),f.elseBody&&c(f.elseBody);let m=s>d?s-1:d;p(h,m);break}case"LetStmtNode":u(f.value,new Set),c(f.body);break;case"EvaluateNode":u(f.value,new Set);break;default:u(f,new Set);break}},"walk");return c(e.body),new Gv(t,r,o)}}});function C4(n,e){if(n.shape.length!==e.shape.length)return!1;for(let t=0;t<n.shape.length;t++)if(n.shape[t]!==e.shape[t])return!1;return!0}function Ec(n,e){if(n===e)return!0;if(!n||!e||typeof n!="object"||typeof e!="object"||n.type!==e.type)return!1;switch(n.type){case"VariableNode":return n.name===e.name;case"IntImmNode":return n.value===e.value;case"FloatImmNode":return n.value===e.value;case"MathOpNode":return n.op===e.op&&Ec(n.a,e.a)&&Ec(n.b,e.b);case"CompareNode":return n.direction===e.direction&&Ec(n.a,e.a)&&Ec(n.b,e.b);case"CastNode":return n.toDtype===e.toDtype&&Ec(n.expr,e.expr);case"BufferLoadNode":return n.buffer===e.buffer&&qv(n.indices,e.indices);default:return!1}}function qv(n,e){if(n.length!==e.length)return!1;for(let t=0;t<n.length;t++)if(!Ec(n[t],e[t]))return!1;return!0}function u2(n,e){n&&Us(n,e)}function D4(n,e,t){let r=[],s=[];for(let p of[n.body,n.initBody])p&&u2(p,c=>{c.type==="BufferStoreNode"&&c.buffer===t?r.push(c):c.type==="BufferLoadNode"&&c.buffer===e&&s.push(c)});if(r.length!==1||s.length===0)return!1;let o=r[0],i=o.indices;for(let p of s)if(!qv(p.indices,i))return!1;let a=new Set;for(let p of[o.value,...o.indices])p&&u2(p,c=>{c.type==="BufferLoadNode"&&c.buffer===e&&a.add(c)});let u=!0;for(let p of s)if(!a.has(p)){u=!1;break}if(!u){let p=o.value;if(!(p&&p.type==="BufferLoadNode"&&p.buffer===e&&qv(p.indices,i)))return!1}return!0}function P4(n,e){if(n)for(let t of yl(n,r=>r.type==="BlockNode",{kinds:"stmt"}))e.push(t)}var jv,Ub,l2=M(()=>{"use strict";ts();jv=class{static{l(this,"InplaceCandidate")}constructor(e,t,r){this.srcBuffer=e,this.dstBuffer=t,this.reason=r}},Ub=class{static{l(this,"InplaceAnalysis")}static analyze(e,t,r=new Set){let s=[],o=[];P4(e.body,o);let i=new Map;for(let p of t.stmtOrder)i.set(p.node,p.idx);let a=new Map;for(let p of t.stmtOrder)for(let c of p.node.reads){let f=a.get(c.buffer);(f===void 0||p.idx>f)&&a.set(c.buffer,p.idx)}let u=new Set;for(let p of o){let c=i.get(p);if(c!==void 0)for(let f of p.writes){let d=f.buffer;if(!(t.isParam(d)&&!r.has(d)))for(let h of p.reads){let m=h.buffer;if(m===d||u.has(m)||t.isParam(m)&&!r.has(m)||!C4(m,d)||m.dtype!==d.dtype||m.scope!==d.scope)continue;let g=t.intervals.get(m),y=t.intervals.get(d);if(!(!g||!y)&&g.lastUse<=y.firstUse){let _=a.get(m);if(_===void 0||_<=c){if(!D4(p,m,d))continue;s.push(new jv(m,d,`${m.name} last used at ${g.lastUse}, ${d.name} first used at ${y.firstUse}`)),u.add(m);break}}}}}return s}};l(C4,"shapesMatch");l(Ec,"exprEqual");l(qv,"indexListEqual");l(u2,"walkNodes");l(D4,"isInplaceComputeSafe");l(P4,"collectBlocks")});function c2(n,e){let t=0,r=n.length;for(;t<r;){let s=t+r>>1;n[s].offset<=e.offset?t=s+1:r=s}n.splice(t,0,e)}function p2(n){return{best:null,consider(e,t,r){return t<r?null:n==="best-fit"?((this.best===null||t<this.best.gap)&&(this.best={offset:e,gap:t}),null):e},result(e){return this.best!==null?this.best.offset:e}}}var Uv,Wv,Wb,f2=M(()=>{"use strict";l(c2,"insertByOffset");l(p2,"gapSelector");Uv=class{static{l(this,"MemoryBlock")}constructor(e,t,r){this.offset=e,this.size=t,this.buffer=r}get end(){return this.offset+this.size}overlaps(e){return this.offset<e.end&&e.offset<this.end}},Wv=class{static{l(this,"MemoryPool")}constructor(e,t=64,r="best-fit"){this.scope=e,this.alignment=t,this.strategy=r,this.blocks=[],this.peakUsage=0}allocate(e,t){let r=this._align(e),s=this._findFreeOffset(r);return this.placeAt(s,r,t)}placeAt(e,t,r){let s=this._align(t),o=new Uv(e,s,r);c2(this.blocks,o);let i=e+s;return i>this.peakUsage&&(this.peakUsage=i),o}_align(e){return Math.ceil(e/this.alignment)*this.alignment}_findFreeOffset(e){let t=this.blocks,r=0,s=p2(this.strategy);for(let o of t){let i=this._align(r),a=o.offset-i,u=s.consider(i,a,e);if(u!==null)return u;o.end>r&&(r=o.end)}return s.result(this._align(r))}fragmentation(){if(this.peakUsage===0)return 0;let e=this.blocks.reduce((t,r)=>t+r.size,0);return Math.max(0,1-e/this.peakUsage)}release(e){let t=this.blocks.indexOf(e);t>=0&&this.blocks.splice(t,1)}},Wb=class{static{l(this,"BufferAssignment")}constructor(){this.assignments=new Map,this.pools=new Map,this.inplaceMap=new Map,this.effLastUse=new Map}assign(e,t=[],r=64,s="best-fit"){for(let c of t)this.inplaceMap.set(c.dstBuffer,c.srcBuffer);let o=new Map;for(let c of e)o.set(c.buffer,c);let i=new Map;for(let c of e)i.set(c.buffer,c.lastUse);let a=new Map;for(let[c,f]of this.inplaceMap)!o.has(c)||!o.has(f)||(a.has(f)||a.set(f,[]),a.get(f).push(c));if(a.size>0){let c=new Map;for(let f of e){let d=f.buffer;if(c.get(d)===1)continue;let h=[d];for(;h.length;){let m=h[h.length-1],g=a.get(m);if(c.get(m)===void 0){if(c.set(m,0),g)for(let y of g)c.get(y)===void 0&&h.push(y);continue}if(h.pop(),c.get(m)!==1){if(g){let y=i.get(m);for(let _ of g){let b=i.get(_);b>y&&(y=b)}i.set(m,y)}c.set(m,1)}}}}this.effLastUse=i;let u=[...e].sort((c,f)=>{let d=c.size,h=f.size,m=d>0,g=h>0;if(m&&g){let y=h-d;if(y!==0)return y}else if(m!==g)return m?-1:1;return c.firstUse-f.firstUse}),p=new Map;for(let c of u){let f=c.buffer;if(this.inplaceMap.has(f)){let w=this.inplaceMap.get(f),x=this.assignments.get(w);if(x){this.assignments.set(f,{offset:x.offset,size:c.size,scope:c.scope,pool:x.pool,inplaceOf:w});continue}}let d=c.size;if(d===0)continue;if(d<0){this.assignments.set(f,{offset:0,size:0,scope:c.scope,pool:c.scope,inplaceOf:null,isDynamic:!0});continue}let h=c.scope,m=this.pools.get(h);m||(m=new Wv(h,r),this.pools.set(h,m)),p.has(h)||p.set(h,[]);let g=p.get(h),y=i.get(f)??c.lastUse,_=this._interferenceOffset(g,c.firstUse,y,m._align(d),r,s),b=m.placeAt(_,d,f);c2(g,{firstUse:c.firstUse,lastUseEff:y,offset:b.offset,size:b.size}),this.assignments.set(f,{offset:b.offset,size:b.size,scope:h,pool:h,inplaceOf:null})}return this}_interferenceOffset(e,t,r,s,o,i){let a=[];for(let c of e)c.firstUse<=r&&t<=c.lastUseEff&&a.push([c.offset,c.offset+c.size]);let u=0,p=p2(i);for(let[c,f]of a){let d=Math.ceil(u/o)*o,h=c-d,m=p.consider(d,h,s);if(m!==null)return m;f>u&&(u=f)}return p.result(Math.ceil(u/o)*o)}getOffset(e){let t=this.assignments.get(e);return t?t.offset:-1}getAssignment(e){return this.assignments.get(e)||null}peakMemory(e=null){if(e){let r=this.pools.get(e);return r?r.peakUsage:0}let t=0;for(let[,r]of this.pools)t+=r.peakUsage;return t}}});function Hb(n,e,t){if(!n||typeof n!="object"||t.has(n))return!1;if(t.add(n),n.type==="BufferLoadNode"&&(!e||n.buffer===e))return!0;for(let r of Object.keys(n)){if(r==="_parent"||r==="_parentKey"||r==="_parentIdx")continue;let s=n[r];if(!(!s||typeof s!="object")){if(Array.isArray(s)){for(let o of s)if(Hb(o,e,t))return!0}else if(Hb(s,e,t))return!0}}return!1}function d2(n){let e=new Set,t=new Map,r=new Set,s=[n.body];for(;s.length>0;){let o=s.pop();if(!(!o||typeof o!="object"||r.has(o))){if(r.add(o),o.type==="BufferStoreNode"&&o.buffer){let i=o.buffer;for(let u of o.indices)if(Hb(u,null,new Set)){e.add(i);break}Hb(o.value,i,new Set)&&e.add(i);let a=t.get(i);a||(a=[],t.set(i,a)),a.push(o.value)}for(let i of Object.keys(o)){if(i==="_parent"||i==="_parentKey"||i==="_parentIdx")continue;let a=o[i];if(Array.isArray(a))for(let u of a)u&&typeof u=="object"&&s.push(u);else a&&typeof a=="object"&&s.push(a)}}}for(let[o,i]of t){let a=l(u=>u&&(u.type==="IntImmNode"||u.type==="FloatImmNode")&&u.value===0,"isConstZero");i.every(a)&&e.add(o),i.length===1&&i[0]&&(i[0].type==="IntImmNode"||i[0].type==="FloatImmNode")&&e.add(o)}return e}function $4(n,e){let t=new Set,r=[n];for(;r.length>0;){let s=r.pop();if(!(!s||typeof s!="object"||t.has(s))){t.add(s);for(let o of Object.keys(s)){if(o==="_parent"||o==="_parentKey"||o==="_parentIdx")continue;let i=s[o];if(i&&typeof i=="object"&&e.has(i)){s[o]=e.get(i);continue}if(Array.isArray(i)){for(let a=0;a<i.length;a++){let u=i[a];u&&typeof u=="object"&&e.has(u)?i[a]=e.get(u):u&&typeof u=="object"&&r.push(u)}continue}i&&typeof i=="object"&&r.push(i)}}}}var Kv,Kb,h2=M(()=>{"use strict";a2();l2();f2();Fe();K0();Kv=class{static{l(this,"MemoryPlan")}constructor(e,t,r){this.assignment=e,this.liveness=t,this.inplaceCandidates=r,this.aliasMap=new Map}peakMemory(e=null){return this.assignment.peakMemory(e)}getReport(){let e=new Map;for(let[s,o]of this.assignment.pools)e.set(s,{peakUsage:o.peakUsage,numBuffers:0,numReused:0});for(let[s,o]of this.assignment.assignments){let i=e.get(o.scope);i&&(i.numBuffers++,o.inplaceOf&&i.numReused++)}let t=this.liveness.getTemporaries().length,r=this.inplaceCandidates.length;return{peakMemory:this.assignment.peakMemory(),scopeBreakdown:e,totalTemporaries:t,totalInplace:r,materializedReuse:this.aliasMap.size,assignments:this.assignment.assignments}}},Kb=class{static{l(this,"MemoryPlanner")}constructor(e={}){this.alignment=e.alignment||64,this.enableInplace=e.enableInplace!==!1,this.allocStrategy=e.allocStrategy||"best-fit",this.poolAllocation=e.poolAllocation||!1}plan(e){let t=qb.analyze(e),r=t.getTemporaries(),s=[];this.enableInplace&&(s=Ub.analyze(e,t));let o=new Wb;return o.assign(r,s,this.alignment,this.allocStrategy),new Kv(o,t,s)}planAndRewrite(e){let t=this.plan(e);return{func:this._insertAllocations(e,t),plan:t}}_insertAllocations(e,t){let r=t.liveness.getTemporaries();if(r.length===0)return e;let s=new Map;this.poolAllocation?this._assignPoolOffsets(e,t,r):(s=this._buildReuseAliases(r,t,e),s.size>0&&$4(e.body,s)),t.aliasMap=s;let o=[...r].sort((u,p)=>p.firstUse-u.firstUse),i=e.body,a=new Set;for(let u of o){let p=u.buffer;if(s.has(p))continue;let c=t.assignment.getAssignment(p);c&&(c.inplaceOf||a.has(p)||(a.add(p),i=new qe(p,c.isDynamic?"dynamic":c.scope,i)))}return e.body=i,e._setChild("body",i),e}_assignPoolOffsets(e,t,r){let s=d2(e);for(let o of r){let i=o.buffer;if(s.has(i)||i.scope!=="global")continue;let a=t.assignment.getAssignment(i);!a||a.inplaceOf||a.isDynamic||a.size>0&&(i.poolByteOffset=a.offset)}}_buildReuseAliases(e,t,r){let s=d2(r),o=new Set(t.assignment.inplaceMap.values()),i=t.assignment.effLastUse,a=l(c=>i.get(c.buffer)??c.lastUse,"lastUseOf"),u=new Map;for(let c of e){let f=c.buffer,d=t.assignment.getAssignment(f);if(!d||d.inplaceOf||d.isDynamic||o.has(f)||f.numel()<=0||s.has(f))continue;let h=`${f.scope}|${f.dtype}|${f.shape.join(",")}|${f.strides.join(",")}`,m=u.get(h);m||(m=[],u.set(h,m)),m.push(c)}let p=new Map;for(let c of u.values()){if(c.length<2)continue;c.sort((d,h)=>d.firstUse-h.firstUse||a(d)-a(h));let f=new Tl((d,h)=>d.lastUse-h.lastUse);for(let d of c){let h=f.peek();h&&h.lastUse<d.firstUse?(f.pop(),h.lastUse=a(d),f.push(h),p.set(d.buffer,h.rep)):f.push({rep:d.buffer,lastUse:a(d)})}}return p}};l(Hb,"exprLoadsBuffer");l(d2,"collectFreshZeroDependent");l($4,"rewriteBufferAliases")});var Yb,m2=M(()=>{"use strict";ma();h2();Yb=class extends ar{static{l(this,"MemoryPlanPass")}constructor(e){super("MemoryPlanPass","memoryPlanning"),this.config=e,this._planner=null}begin(e){let t=this.config.memory.alignment||this.config.target?.cacheLineSizeBytes||64;this._planner=new Kb({alignment:t,enableInplace:this.config.memory.inplaceReuse,allocStrategy:this.config.memory.allocStrategy,poolAllocation:this.config.memory.poolAllocation})}run(e,t){if(e.gpuRegisterBlocked)return;let r=performance.now(),{plan:s}=this._planner.planAndRewrite(e),o=s.getReport();t.trace.memoryStats(e.name,{durationMs:performance.now()-r,peakMemory:o.peakMemory,totalTemporaries:o.totalTemporaries,totalInplace:o.totalInplace})}}});function Lc(n){return n&&n.type==="IntImmNode"?n.value:null}function F4(n){if(!n||n.type!=="ForNode"||!g2.has(n.kind)||Lc(n.min)!==0)return null;let e=Lc(n.extent);if(e===null)return null;let t=n.body;if(!t||t.type!=="ForNode"||!g2.has(t.kind)||Lc(t.min)!==0)return null;let r=Lc(t.extent);if(r===null||r<=0)return null;let s=t.body;if(!s||s.type!=="IfThenElseNode"||s.elseBody)return null;let o=z4(s.condition,n.loopVar.name,t.loopVar.name,r);if(o===null||o<=0)return null;let i=Math.floor(o/r),a=o-i*r;if(a===0||i<1||e!==i+1)return null;let u=new Xs;if(u.bind(n.loopVar.name,0,i-1),u.bind(t.loopVar.name,0,r-1),!_i(u,s.condition))return null;let p=s.thenBody,c=new he(t.loopVar,new L(0),new L(r),t.kind,p,t.threadTag),f=new he(n.loopVar,new L(0),new L(i),n.kind,c,n.threadTag),d=Gi(Iu(p),g=>g.type==="VariableNode"&&g.name===n.loopVar.name?new L(i):g),h=new Oe(t.loopVar.name,t.loopVar.dtype),m=new he(h,new L(0),new L(a),t.kind,d,t.threadTag);return new te([f,m])}function z4(n,e,t,r){if(!n)return null;let s,o;if(n.type==="MathOpNode"&&n.op==="<")s=n.a,o=n.b;else if(n.type==="CompareNode"&&n.direction==="lt")s=n.a,o=n.b;else return null;let i=Lc(o);if(i===null||!s||s.type!=="MathOpNode"||s.op!=="+")return null;let a=s.a,u=s.b;return!u||u.type!=="VariableNode"||u.name!==t||!a||a.type!=="MathOpNode"||a.op!=="*"||!a.a||a.a.type!=="VariableNode"||a.a.name!==e||Lc(a.b)!==r?null:i}var g2,Jb,y2=M(()=>{"use strict";ma();Fe();ts();Id();Hl();wf();g2=new Set([z.SERIAL,z.PARALLEL,z.VECTORIZED,z.UNROLLED]),Jb=class extends ar{static{l(this,"LoopPartitionPass")}constructor(){super("LoopPartitionPass","loopPartition")}run(e,t){let r=Gi(e.body,s=>F4(s)||s);return r&&r!==e.body&&(e.body=r,e._setChild("body",r)),e}};l(Lc,"intImm");l(F4,"partitionLoop");l(z4,"matchFlatGuard")});function Zb(n){let e=n.body;if(!e||e.type!=="BlockNode")return null;let t=e.body;if(!t||t.type!=="BufferStoreNode")return null;let r=t,s=r.value;if(!s||s.type!=="MathOpNode"||!V4.has(s.op))return null;let o=null,i=null;if(s.a&&s.a.type==="BufferLoadNode"&&s.a.buffer===r.buffer?(o=s.a,i=s.b):s.b&&s.b.type==="BufferLoadNode"&&s.b.buffer===r.buffer&&(o=s.b,i=s.a),!o)return null;let a=Hv(r.indices),u=Hv(o.indices);if(a!==u||a.includes("?"))return null;let p=r.indices.map(d=>{if(d.type!=="VariableNode")return d;for(let h of e.iterVars)if(h.iterVar&&h.iterVar.name===d.name&&h.binding)return h.binding;return d}),c=n.loopVar.name,f=Hv(p);return f.includes("?")||f.includes("$"+c)?null:{store:r,loadSide:o,valueSide:i,outerIndices:p,block:e,op:r.value.op}}function Hv(n){return n.map(Yv).join(",")}function Yv(n){return n?n.type==="VariableNode"?"$"+n.name:n.type==="IntImmNode"?String(n.value):n.type==="MathOpNode"?"("+Yv(n.a)+n.op+(n.b?Yv(n.b):"")+")":"?":"?"}var V4,Jv=M(()=>{"use strict";V4=new Set(["+","*","max","min"]);l(Zb,"detectAccumulator");l(Hv,"indicesKey");l(Yv,"exprKey")});function Vo(n){if(!(!n||typeof n!="object"||!n.type))switch(n.type){case"ForNode":{let e=Zb(n);n.accumulator=e,e||Vo(n.body);return}case"BlockNode":Vo(n.body),n.initBody&&Vo(n.initBody);return;case"SeqNode":for(let e of n.stmts)Vo(e);return;case"LetStmtNode":case"AllocateNode":Vo(n.body);return;case"IfThenElseNode":Vo(n.thenBody),n.elseBody&&Vo(n.elseBody);return;case"WhileNode":Vo(n.condBody),Vo(n.loopBody);return;default:return}}var Qb,_2=M(()=>{"use strict";ma();Jv();Qb=class extends ar{static{l(this,"AccumulatorDetectionPass")}constructor(){super("AccumulatorDetectionPass","accumulatorDetect")}run(e,t){return Vo(e.body),e}};l(Vo,"annotateStmt")});function j4(n){let e=[],t=[n.body];for(;t.length>0;){let s=t.pop();if(s){if(s.type==="BlockNode"){e.push(s),t.push(s.body);continue}if(s.body&&t.push(s.body),s.stmts)for(let o of s.stmts)t.push(o);s.thenBody&&t.push(s.thenBody),s.elseBody&&t.push(s.elseBody)}}if(e.length===0)return null;let r=null;for(let s of e){if(!s.name.includes("matmul"))return null;s.reads.length>=2&&s.writes.length>=1&&(r=s)}return r}function q4(n){let e=j4(n);if(!e)return null;let t=e.reads[0].buffer,r=e.reads[1].buffer,s=e.writes[0].buffer;if(!b2.has(t.dtype)||!b2.has(r.dtype)||s.dtype!=="f32"||t.shape.length!==2||r.shape.length!==2||s.shape.length!==2)return null;let o=s.shape[0],i=s.shape[1],a=t.shape[1];if(![o,i,a].every(p=>typeof p=="number"&&p>0&&p%G4===0))return null;let u=new Set;for(let[,p]of n.bufferMap)u.add(p.name);return!u.has(t.name)||!u.has(r.name)||!u.has(s.name)?null:{M:o,N:i,K:a,a:t.name,b:r.name,c:s.name}}var G4,b2,Xb,w2=M(()=>{"use strict";ma();du();G4=16,b2=new Set(["f16","bf16"]);l(j4,"findMatmulBlock");l(q4,"detectWmmaMatmul");Xb=class extends ar{static{l(this,"AutoTensorizePass")}constructor(e){super("AutoTensorizePass","scheduling"),this.config=e,this.target=e.target}run(e,t){if(e.cublasInfo||e._tensorIntrin||!this.target||!this.target.isGPU())return;let r=q4(e);r&&(new Cn(e).tensorize("wmma_16x16x16_f16f16f32",r),t&&t.trace&&t.trace.explainsEnabled&&t.trace.explain("tensorize",e.name,"wmma_16x16x16_f16f16f32",`auto-tensorized ${r.M}x${r.N}x${r.K} f16 GEMM`,{target:this.target.name}))}}});function U4(n){let e=[],t=new Map,r=new Map,s=[{node:n,block:null}];for(;s.length>0;){let{node:o,block:i}=s.pop();if(o.type==="BufferLoadNode"&&o.buffer)t.set(o.buffer.name,(t.get(o.buffer.name)||0)+1),i&&i.reads.add(o.buffer.name);else if(o.type==="BufferStoreNode"&&o.buffer&&i){i.writes.add(o.buffer.name);let u=r.get(o.buffer.name);u||(u=new Set,r.set(o.buffer.name,u)),u.add(i.name)}let a=i;o.type==="BlockNode"&&(a={name:o.name,writes:new Set,reads:new Set,hasInit:o.initBody!=null},e.push(a));for(let u of ln(o))s.push({node:u,block:a})}return{blocks:e,loadCount:t,storeWriters:r}}var ew,T2=M(()=>{"use strict";ma();du();Do();ts();l(U4,"analyzeFunc");ew=class extends ar{static{l(this,"InlineReindexPass")}constructor(e){super("InlineReindexPass","scheduling"),this.config=e,this.target=e.target,this.snapshotPoint="afterInlineReindex"}run(e,t){if(!this.target.isGPU()||this.target.isWebGPU()||e.cublasInfo||e._tensorIntrin||tS(e))return;let r=this.config.scheduling;if(!(r.enabled||r.gpuTiling||r.autotune))return;let s=new Set;for(let[,c]of e.bufferMap)s.add(c.name);let{blocks:o,loadCount:i,storeWriters:a}=U4(e.body),u=new Cn(e),p=!1;for(let c of o){if(c.hasInit||c.writes.size===0)continue;let f=[...c.writes];if(f.every(d=>!s.has(d)&&a.get(d).size===1)&&f.some(d=>(i.get(d)||0)>0)&&[...c.reads].every(d=>(a.get(d)?a.get(d).size:0)<=1))try{u.computeInlineBlock(c.name),p=!0}catch{}}p&&Xx(e)}}});function Zv(n,e,t=W4){return t.filter(r=>r.phase===n).sort((r,s)=>r.priority-s.priority).map(r=>r.factory(e)).filter(Boolean)}var W4,x2=M(()=>{"use strict";W4=[];l(Zv,"tirPassesForPhase")});function S2(n){let e=[];for(let t of Zv("pre",n))e.push(t);e.push(new ew(n)),n.optimization.tensorize&&e.push(new Xb(n)),e.push(new Gb(n)),n.optimization.loopPartition&&e.push(new Jb),e.push(new jb),e.push(new Yb(n)),n.optimization.detectAccumulators&&e.push(new Qb);for(let t of Zv("post",n))e.push(t);return e}var v2=M(()=>{"use strict";n2();o2();m2();y2();_2();w2();T2();x2();l(S2,"buildTirPipeline")});var Vd,k2=M(()=>{"use strict";Fe();Vd=class{static{l(this,"TensorVerifier")}constructor(){this.errors=[],this.boundVars=new Set}verify(e){if(this.errors=[],this.boundVars.clear(),!(e instanceof Pi))return this.errors.push("Expected PrimFunc at root"),this.errors;for(let t of e.params)this.boundVars.add(t.name);if(e.shapeParams)for(let t of e.shapeParams)this.boundVars.add(t.name);return this.visit(e.body),this.errors}visit(e){let t=[e];for(;t.length>0;){let r=t.pop();if(r)switch(r.type){case"SeqNode":for(let s=r.stmts.length-1;s>=0;s--)t.push(r.stmts[s]);break;case"ForNode":this.boundVars.has(r.loopVar.name)&&this.errors.push(`Loop variable ${r.loopVar.name} already bound`),this.boundVars.add(r.loopVar.name),t.push({type:"_unbind",name:r.loopVar.name}),t.push(r.body);break;case"BlockNode":for(let s of r.iterVars)s.iterVar&&(this.boundVars.has(s.iterVar.name)&&this.errors.push(`Block variable ${s.iterVar.name} already bound`),this.boundVars.add(s.iterVar.name));for(let s=r.iterVars.length-1;s>=0;s--)r.iterVars[s].iterVar&&t.push({type:"_unbind",name:r.iterVars[s].iterVar.name});t.push(r.body),r.initBody&&t.push(r.initBody);break;case"AllocateNode":r.buffer||this.errors.push("Allocate missing buffer"),t.push(r.body);break;case"LetStmtNode":this._visitExpr(r.value),this.boundVars.add(r.variable.name),t.push({type:"_unbind",name:r.variable.name}),t.push(r.body);break;case"IfThenElseNode":this._visitExpr(r.condition),r.elseBody&&t.push(r.elseBody),t.push(r.thenBody);break;case"WhileNode":r.condVar||this.errors.push("WhileNode missing condition variable"),t.push(r.loopBody),t.push(r.condBody);break;case"BufferStoreNode":if(r.buffer||this.errors.push("BufferStore missing buffer"),(!r.indices||r.indices.length!==r.buffer.shape.length)&&this.errors.push(`BufferStore rank mismatch for ${r.buffer?r.buffer.name:"unknown"}`),r.indices)for(let s of r.indices)this._visitExpr(s);this._visitExpr(r.value);break;case"EvaluateNode":this._visitExpr(r.value);break;case"_unbind":this.boundVars.delete(r.name);break;default:this._visitExpr(r);break}}}_visitExpr(e){if(e)switch(e.type){case"BufferLoadNode":if(e.buffer||this.errors.push("BufferLoad missing buffer"),(!e.indices||e.indices.length!==e.buffer.shape.length)&&this.errors.push(`BufferLoad rank mismatch for ${e.buffer?e.buffer.name:"unknown"}`),e.indices)for(let t of e.indices)this._visitExpr(t);break;case"MathOpNode":this._visitExpr(e.a),e.b&&this._visitExpr(e.b);break;case"CompareNode":this._visitExpr(e.a),this._visitExpr(e.b);break;case"CastNode":this._visitExpr(e.expr);break;case"CallExternNode":e.externName||this.errors.push("CallExtern missing function name");for(let t of e.args)this._visitExpr(t);break;case"IfThenElseNode":this._visitExpr(e.condition),this._visitExpr(e.thenBody),e.elseBody&&this._visitExpr(e.elseBody);break;case"VariableNode":this.boundVars.has(e.name)||this.errors.push(`Unbound variable used: ${e.name}`);break;case"BlockRealizeNode":e.binding&&this._visitExpr(e.binding);break;case"IntImmNode":case"FloatImmNode":break}}}});function Qv(n){let e=[];n.functionCount===0&&e.push(new Et("Module has no functions"));for(let t of n)Gd(t,e);return e}function Gd(n,e=[]){if(!n.entryBlock)return e.push(new Et("Function has no entry block",null,n)),e;n.entryBlock.arguments.length!==n.inputTypes.length&&e.push(new Et(`Entry block has ${n.entryBlock.arguments.length} args, expected ${n.inputTypes.length}`,null,n));for(let s=0;s<n.entryBlock.arguments.length;s++){let o=n.entryBlock.arguments[s],i=n.inputTypes[s];i&&!o.type.equals(i)&&e.push(new Et(`Block arg ${s} type ${Gn(o.type)} != expected ${Gn(i)}`,null,n))}let t=new Set;for(let s of n.entryBlock.arguments)t.add(s);for(let s of n.body)N2(s,t);for(let s of n.body)A2(s,n,t,e);let r=n.getReturnOp();if(!r)e.push(new Et("Missing return op",null,n));else{r.numOperands!==n.outputTypes.length&&e.push(new Et(`Return has ${r.numOperands} operands, function declares ${n.outputTypes.length} outputs`,r,n));for(let s=0;s<Math.min(r.numOperands,n.outputTypes.length);s++){let o=r.getOperand(s).type,i=n.outputTypes[s];i instanceof I&&o instanceof I&&(o.dtype!==i.dtype&&e.push(new Et(`Return operand ${s} dtype ${o.dtype} != expected ${i.dtype}`,r,n)),o.shapeCompatible(i)||e.push(new Et(`Return operand ${s} shape incompatible: ${Gn(o)} vs ${Gn(i)}`,r,n)))}}return e}function N2(n,e){for(let t of n.arguments)e.add(t);for(let t of n)for(let r=0;r<t.numResults;r++){let s=t.getResult(r);s&&e.add(s)}}function K4(n,e,t){let r=new Set;for(let u of n)r.add(u);let s=1,o=2,i=new Map,a=new Set;for(let u of n){if(i.get(u)!==void 0)continue;let p=[{op:u,i:0}];for(i.set(u,s);p.length>0;){let c=p[p.length-1],f=c.op;if(c.i<f.numOperands){let d=f.getOperand(c.i);c.i++;let h=d&&d.definingOp;if(!h||!r.has(h))continue;let m=i.get(h);m===s?a.has(h)||(a.add(h),t.push(new Et("participates in a value dependency cycle",h,e))):m===void 0&&(i.set(h,s),p.push({op:h,i:0}));continue}i.set(f,o),p.pop()}}}function A2(n,e,t,r){K4(n,e,r);for(let s of n)H4(s,e,t,r);if(n.size>0){let s=n.lastOp;if(n.parentRegion&&n.parentRegion.parentOp){let o=n.parentRegion.parentOp.opName,i=ne.get(o);i&&i.hasRegions&&(s.isTerminator()||r.push(new Et(`Block in ${o} region must end with terminator, got '${s.opName}'`,s,e)))}}}function H4(n,e,t,r){for(let o=0;o<n.numOperands;o++){let i=n.getOperand(o);if(!i){r.push(new Et(`Operand ${o} is null`,n,e));continue}if(!(i instanceof Li)){r.push(new Et(`Operand ${o} is not a Value`,n,e));continue}t.has(i)||r.push(new Et(`Operand ${o} used before definition`,n,e))}for(let o=0;o<n.numResults;o++){let i=n.getResult(o);i||r.push(new Et(`Result ${o} is null`,n,e)),i&&i.definingOp!==n&&r.push(new Et(`Result ${o} definingOp mismatch`,n,e))}let s=ne.get(n.opName);if(!s){!["return","yield"].includes(n.opName)&&!ne.has(n.opName)&&r.push(new Et(`Unknown op '${n.opName}'`,n,e));return}s.numOperands>=0&&n.numOperands!==s.numOperands&&r.push(new Et(`'${n.opName}' expects ${s.numOperands} operands, got ${n.numOperands}`,n,e)),s.numResults>=0&&n.numResults!==s.numResults&&r.push(new Et(`'${n.opName}' expects ${s.numResults} results, got ${n.numResults}`,n,e));for(let o of s.attrs)o.required&&!n.hasAttr(o.name)&&r.push(new Et(`'${n.opName}' missing required attribute '${o.name}'`,n,e));if(s.hasRegions&&s.numRegions>0&&n.numRegions!==s.numRegions&&r.push(new Et(`'${n.opName}' expects ${s.numRegions} regions, got ${n.numRegions}`,n,e)),s.verify){let o=s.verify(n);if(o)for(let i of o)r.push(new Et(i,n,e))}if(s.inferResultTypes&&n.numResults>0){let o=[];for(let a=0;a<n.numOperands;a++)o.push(n.getOperand(a).type);let i=s.inferResultTypes(o,n.attributes,n.results.map(a=>a.type));if(i)for(let a=0;a<Math.min(i.length,n.numResults);a++){let u=n.getResult(a).type,p=i[a];p instanceof I&&u instanceof I&&(u.dtype!==p.dtype&&r.push(new Et(`Result ${a} dtype ${u.dtype} != inferred ${p.dtype}`,n,e)),u.shapeCompatible(p)||r.push(new Et(`Result ${a} shape ${Gn(u)} incompatible with inferred ${Gn(p)}`,n,e)))}}for(let o of n.regions){let i=new Set(t);for(let a of o)N2(a,i);for(let a of o)A2(a,e,i,r)}}var Et,O2=M(()=>{"use strict";Re();Mh();Ct();Et=class{static{l(this,"VerificationError")}constructor(e,t=null,r=null){this.message=e,this.op=t,this.func=r}toString(){let e="";return this.func&&(e+=`[${this.func.name}] `),this.op&&(e+=`op '${this.op.opName}' (id=${this.op.id}): `),e+this.message}};l(Qv,"verifyModule");l(Gd,"verifyFunction");l(N2,"collectScopeDefs");l(K4,"detectCycles");l(A2,"verifyBlock");l(H4,"verifyOperation")});var tw,Xv,Cc,ek,tk=M(()=>{"use strict";cv();Re();tw=class{static{l(this,"ValueObserver")}constructor(){this.min=1/0,this.max=-1/0,this.count=0,this.histogram=null}update(e){for(let t=0;t<e.length;t++){let r=e[t];r<this.min&&(this.min=r),r>this.max&&(this.max=r)}this.count+=e.length,this.histogram&&this.histogram.update(e)}enableHistogram(e=2048){this.histogram=new Xv(e)}},Xv=class{static{l(this,"HistogramCollector")}constructor(e=2048){this.numBins=e,this.bins=new Float64Array(e),this.rangeMin=0,this.rangeMax=0,this.initialized=!1,this.totalCount=0}update(e){this.initialized||this._initRange(e);let t=this.rangeMax-this.rangeMin;if(t<=0)return;for(let s=0;s<e.length;s++){let o=e[s];if(o<this.rangeMin||o>this.rangeMax){this._expandAndRebucket(e);return}}let r=this.numBins/t;for(let s=0;s<e.length;s++){let o=Math.min(this.numBins-1,Math.floor((e[s]-this.rangeMin)*r));this.bins[o]++}this.totalCount+=e.length}_initRange(e){let t=1/0,r=-1/0;for(let s=0;s<e.length;s++)e[s]<t&&(t=e[s]),e[s]>r&&(r=e[s]);t===r&&(r=t+1),this.rangeMin=t,this.rangeMax=r,this.initialized=!0}_expandAndRebucket(e){let t=this.rangeMin,r=this.rangeMax;for(let c=0;c<e.length;c++)e[c]<t&&(t=e[c]),e[c]>r&&(r=e[c]);let s=new Float64Array(this.bins),o=this.rangeMin,a=this.rangeMax-o;this.rangeMin=t,this.rangeMax=r;let u=r-t,p=this.numBins/u;if(this.bins.fill(0),a>0){let c=a/this.numBins;for(let f=0;f<this.numBins;f++){if(s[f]===0)continue;let d=o+(f+.5)*c,h=Math.min(this.numBins-1,Math.floor((d-t)*p));this.bins[h]+=s[f]}}for(let c=0;c<e.length;c++){let f=Math.min(this.numBins-1,Math.floor((e[c]-t)*p));this.bins[f]++}this.totalCount+=e.length}computePercentileThreshold(e){let t=this.totalCount*e,r=0,s=(this.rangeMax-this.rangeMin)/this.numBins;for(let o=0;o<this.numBins;o++)if(r+=this.bins[o],r>=t)return this.rangeMin+(o+1)*s;return this.rangeMax}computeEntropyThreshold(e){if(this.totalCount===0)return this.rangeMax;let t=new Float64Array(this.numBins);for(let i=0;i<this.numBins;i++)t[i]=this.bins[i]/this.totalCount;let r=this.numBins,s=1/0;for(let i=e;i<=this.numBins;i++){let a=i/e,u=new Float64Array(i);for(let c=0;c<e;c++){let f=Math.floor(c*a),d=Math.min(i,Math.floor((c+1)*a)),h=0;for(let g=f;g<d;g++)h+=t[g];let m=d-f;if(m>0){let g=h/m;for(let y=f;y<d;y++)u[y]=g}}let p=0;for(let c=0;c<i;c++)t[c]>0&&u[c]>0&&(p+=t[c]*Math.log(t[c]/u[c]));p<s&&(s=p,r=i)}let o=(this.rangeMax-this.rangeMin)/this.numBins;return this.rangeMin+r*o}},Cc=class{static{l(this,"CalibrationCollector")}constructor(e="minmax"){this.observers=new Map,this.mode=e}attach(e){for(let t of e.ops())for(let r=0;r<t.numResults;r++){let s=t.getResult(r);if(s.type instanceof I&&jt(s.type.dtype)){let o=new tw;(this.mode==="entropy"||this.mode==="percentile")&&o.enableHistogram(),this.observers.set(s,o)}}for(let t of e.args)if(t.type instanceof I&&jt(t.type.dtype)){let r=new tw;(this.mode==="entropy"||this.mode==="percentile")&&r.enableHistogram(),this.observers.set(t,r)}}observe(e,t){let r=this.observers.get(e);r&&r.update(t)}getResult(){return new ek(this.observers,this.mode)}},ek=class{static{l(this,"CalibrationResult")}constructor(e,t){this._observers=e,this._mode=t}getRange(e){let t=this._observers.get(e);return!t||t.count===0?null:{min:t.min,max:t.max}}getQuantParams(e,t,r){let s=this.getRange(e);if(!s)return null;let o=t===Pr.PER_TENSOR_ASYMMETRIC;if(this._mode==="percentile"&&this._observers.get(e).histogram){if(o)return vr.fromRange(s.min,s.max,t,r);let a=this._observers.get(e).histogram.computePercentileThreshold(.999);return vr.fromRange(-a,a,t,r)}if(this._mode==="entropy"&&this._observers.get(e).histogram){if(o)return vr.fromRange(s.min,s.max,t,r);let i=this._observers.get(e).histogram,a=r==="ui8"?256:255,u=i.computeEntropyThreshold(a);return vr.fromRange(-u,u,t,r)}return vr.fromRange(s.min,s.max,t,r)}hasData(e){let t=this._observers.get(e);return t&&t.count>0}values(){return this._observers.keys()}}});function J4(n,e){let t=[],r=new Set;for(let s of n.ops())if(e.has(s.opName))for(let o=0;o<s.numOperands;o++){let i=s.getOperand(o);if(r.has(i)||!(i.type instanceof I)||!jt(i.type.dtype))continue;let a=i.definingOp;a&&a.opName==="constant"||(r.add(i),t.push(i))}return t}function Z4(n,e){let t=e.map(h=>h.type),r=new yn(n.name,n.inputTypes,[...n.outputTypes,...t]),s=new Map,o=n.entryBlock.arguments,i=r.entryBlock.arguments;for(let h=0;h<o.length;h++)s.set(o[h],i[h]);let a=n.entryBlock.opsArray(),u=new Set(a),p=Qo(a,h=>u.has(h),"ignore"),c=new Map;for(let h of p)h.opName!=="return"&&c.set(h,h.clone(s));for(let h of a)h.opName!=="return"&&r.entryBlock.pushOp(c.get(h));let f=n.getReturnOp(),d=[];if(f)for(let h of f.operands)d.push(s.get(h)||h);for(let h of e)d.push(s.get(h));return r.entryBlock.pushOp(new Me("return",d,[],{})),r}function B2(n,e,t,r={}){if(e.isGPU&&e.isGPU())throw new Error("collectCalibration: synchronous calibration is unavailable for async (GPU) targets; precompute calibration on a CPU/WASM target or supply quantization.calibration directly");if(!t||t.length===0)throw new Error("collectCalibration: at least one calibration batch is required");let s=r.quantizableOps||Y4,o=r.mode||"minmax",i=J4(n,s),a=new Cc(o);a.attach(n);let u=a.getResult();if(i.length===0)return u;let p=Z4(n,i),c=new Mn("__calibrate__");c.addFunction(p);let f=n.outputTypes.length,h=(r.compileFn||Q4)(c,e);for(let m of t){let g=Array.isArray(m)?m:[m],y=p.outputTypes.map(_=>new Float32Array(Math.max(1,_.numel())));h.run(n.name,...g,...y);for(let _=0;_<i.length;_++)a.observe(i[_],y[f+_])}return u}function Q4(n,e){throw new Error("collectCalibration: opts.compileFn is required (pass the compileModule function to avoid a circular import)")}var Y4,R2=M(()=>{"use strict";qs();Di();yr();Re();Da();tk();Y4=new Set(["dot","conv","add","mul","sub"]);l(J4,"activationTargets");l(Z4,"buildCaptureFunction");l(B2,"collectCalibration");l(Q4,"defaultCompileFn")});function rk(n){let e=0;for(let t=0;t<n.numResults;t++){let r=n.getResult(t).type;r instanceof I&&r.isFullyStatic&&(e+=r.sizeInBytes())}return e}var nk,Dc,sk,nw,M2=M(()=>{"use strict";Ct();Re();Nd();nk=class{static{l(this,"Partition")}constructor(e,t){this.id=e,this.target=t,this.ops=[],this.opSet=new Set,this._inputValues=null,this._outputValues=null,this._memoryBytes=0}addOp(e){this.opSet.has(e)||(this.ops.push(e),this.opSet.add(e),this._inputValues=null,this._outputValues=null,this._memoryBytes+=rk(e))}hasOp(e){return this.opSet.has(e)}merge(e){for(let t of e.ops)this.addOp(t)}computeIO(){if(this._inputValues&&this._outputValues)return;let{inputs:e,outputs:t}=pa(this.opSet,this.ops);this._inputValues=e,this._outputValues=t}getInputValues(){return this.computeIO(),this._inputValues}getOutputValues(){return this.computeIO(),this._outputValues}get size(){return this.ops.length}get memoryBytes(){return this._memoryBytes}};l(rk,"estimateOpMemory");Dc=class{static{l(this,"PartitionerConfig")}constructor(e={}){this.targets=e.targets||[],this.defaultTarget=e.defaultTarget||null,this.opTargetOverrides=e.opTargetOverrides||new Map,this.memoryLimits=e.memoryLimits||new Map,this.minPartitionSize=e.minPartitionSize||1,this.costWeights={transferCost:1,loadBalance:.5,...e.costWeights||{}}}},sk=class{static{l(this,"PartitionResult")}constructor(e,t,r){this.partitions=e,this.opToPartition=t,this.transferEdges=r}getPartition(e){return this.opToPartition.get(e)||null}getPartitionsForTarget(e){return this.partitions.filter(t=>t.target===e||t.target.name===e.name)}get numPartitions(){return this.partitions.length}},nw=class{static{l(this,"GraphPartitioner")}constructor(e){this.config=e instanceof Dc?e:new Dc(e),this._supportCache=new Map,this._buildSupportMap()}partition(e){let t=this._collectPartitionableOps(e),r=this._assignTargets(t),s=this._buildPartitions(t,r),o=this._mergeSmallPartitions(s,r),i=this._computeTransferEdges(o),a=new Map;for(let u of o)for(let p of u.ops)a.set(p,u);return new sk(o,a,i)}_buildSupportMap(){for(let e of this.config.targets){let t=new Set;for(let r of ne.names())this._targetSupportsOp(e,r)&&t.add(r);this._supportCache.set(e,t)}}_targetSupportsOp(e,t){if(e.hasLibraryOp(t))return!0;let r=ne.get(t);return r?r.isConstant||r.isTerminator?!0:e.isGPU()?r.isElementwise||r.isReduction||r.isBroadcast||r.isInjective||r.getAttr("gpuCapable")===!0:e.isCPU()?!0:e.isWasm()?!r.isOpaque&&t!=="custom_call":!1:!1}_collectPartitionableOps(e){let t=[];for(let r of e.ops()){let s=ne.get(r.opName);!s||s.isTerminator||t.push(r)}return t}_assignTargets(e){let t=new Map;for(let r of e){let s=r.getAttr("device");if(s){let a=this._resolveDeviceAttr(s);if(a){t.set(r,a);continue}}let o=this.config.opTargetOverrides.get(r.opName);if(o){t.set(r,o);continue}let i=this._selectBestTarget(r);t.set(r,i)}return t}_resolveDeviceAttr(e){return typeof e=="string"?this.config.targets.find(t=>t.name===e||t.kind===e)||null:e}_selectBestTarget(e){let t=this.config.defaultTarget||this.config.targets[0],r=-1/0;for(let s of this.config.targets){let o=this._supportCache.get(s);if(!o||!o.has(e.opName))continue;let i=this._scoreTargetForOp(s,e);i>r&&(r=i,t=s)}return t}_scoreTargetForOp(e,t){let r=ne.get(t.opName);if(!r)return 0;let s=0;if(e.hasLibraryOp(t.opName)&&(s+=100),r.isReduction||r.isElementwise){let o=0;for(let i=0;i<t.numOperands;i++){let a=t.getOperand(i).type;a instanceof I&&a.isFullyStatic&&(o+=a.numel())}e.isGPU()&&o>1024?s+=50:e.isCPU()&&o<=1024&&(s+=30)}return s+=e.computeTFLOPs*10,s}_buildPartitions(e,t){let{partitions:r}=kd(e,{sort:l(i=>this._topologicalSort(i),"sort"),labelOf:l(i=>t.get(i),"labelOf"),sameLabel:l((i,a)=>i===a||i.name===a.name,"sameLabel"),canMerge:l((i,a,u)=>this._fitsMemoryLimit(i,a,u),"canMerge"),onAttach:l((i,a)=>{i.memoryBytes=(i.memoryBytes||0)+rk(a)},"onAttach")}),s=new Map;for(let i of r){let a=new nk(i.id,i.label);for(let u of i.ops)a.addOp(u);s.has(i.label.name)||s.set(i.label.name,[]),s.get(i.label.name).push(a)}let o=[];for(let i of s.values())for(let a of i)o.push(a);return o}_fitsMemoryLimit(e,t,r){let s=this.config.memoryLimits.get(r.name);if(!s)return!0;let o=rk(t);return e.memoryBytes+o<=s}_mergeSmallPartitions(e,t){if(e.length<=1)return e;let r=new Map;for(let h of e)for(let m of h.ops)r.set(m,h);let s=new Set,o=new Map,i=new Map;for(let h of e)s.has(h)||o.set(h,new Set);for(let h of e){if(s.has(h))continue;let m=o.get(h);for(let g of h.ops)for(let y=0;y<g.numResults;y++)for(let _ of g.getResult(y).uses()){let b=r.get(_.user);b&&b!==h&&m.add(b)}}let a=l((h,m)=>{let g=o.get(h);for(let y of o.get(m))y!==h&&g.add(y);g.delete(m);for(let[y,_]of o)_.has(m)&&(_.delete(m),y!==h&&_.add(h));o.delete(m),i=new Map},"mergeSucc"),u=l(h=>{let m=i.get(h);if(m)return m;m=new Set;let g=[...o.get(h)];for(;g.length>0;){let y=g.pop();if(m.has(y))continue;m.add(y);let _=o.get(y);if(_)for(let b of _)g.push(b)}return i.set(h,m),m},"reachOf"),p=l((h,m)=>{for(let g of o.get(h))if(g!==m&&u(g).has(m))return!0;return!1},"pathThroughIntermediate"),c=l((h,m)=>p(h,m)||p(m,h),"mergeCreatesCycle"),f=[];for(let h=0;h<e.length;h++){let m=e[h];if(s.has(m))continue;if(m.size>=this.config.minPartitionSize){f.push(m);continue}let g=-1,y=-1/0;for(let _=0;_<e.length;_++){if(h===_)continue;let b=e[_];if(s.has(b)||b.target.name!==m.target.name||c(m,b))continue;let w=this.config.costWeights.transferCost*this._mergeScore(m,b);w>y&&(y=w,g=_)}if(g>=0){let _=e[g];a(_,m),_.merge(m);for(let b of m.ops)r.set(b,_);s.add(m)}else f.push(m)}let d=new Set;for(let h of f)for(let m of h.ops)d.add(m);for(let h of e){let m=!1;for(let g of h.ops)if(!d.has(g)){m=!0;break}if(m){f.push(h);for(let g of h.ops)d.add(g)}}return f}_mergeScore(e,t){let r=0,s=new Set;for(let o of e.ops)for(let i=0;i<o.numResults;i++)s.add(o.getResult(i));for(let o of t.ops)for(let i=0;i<o.numOperands;i++)s.has(o.getOperand(i))&&r++;return r}_computeTransferEdges(e){let t=[],r=new Set,s=new Map;for(let o of e)for(let i of o.ops)s.set(i,o);for(let o of e)for(let i of o.ops)for(let a=0;a<i.numOperands;a++){let u=i.getOperand(a).definingOp;if(!u)continue;let p=s.get(u);if(!p||p===o)continue;let c=i.getOperand(a),f=`${p.id}|${o.id}|${c.id}`;r.has(f)||(r.add(f),t.push({src:p,dst:o,value:c,sizeBytes:c.type instanceof I&&c.type.isFullyStatic?c.type.sizeInBytes():0}))}return t}_topologicalSort(e){let t=new Set(e),r=new Map,s=new Map;for(let u of e)r.set(u,0),s.set(u,[]);for(let u of e)for(let p=0;p<u.numOperands;p++){let c=u.getOperand(p).definingOp;c&&t.has(c)&&(s.get(c).push(u),r.set(u,r.get(u)+1))}let o=[];for(let u of e)r.get(u)===0&&o.push(u);let i=[],a=0;for(;a<o.length;){let u=o[a++];i.push(u);for(let p of s.get(u)){let c=r.get(p)-1;r.set(p,c),c===0&&o.push(p)}}return i}}});var rw,sw,I2=M(()=>{"use strict";fn();yr();qs();M2();Nd();nn();rw=class extends Ue{static{l(this,"GraphPartitionPass")}constructor(e={}){super("GraphPartitionPass"),this.partitionerConfig=new Dc(e),this.partitionResult=null}run(e,t){if(this.partitionerConfig.targets.length<2)return oe.UNCHANGED;let r=new nw(this.partitionerConfig);return this.partitionResult=r.partition(e),this.partitionResult.numPartitions<=1?oe.UNCHANGED:(this._annotateOps(),this._insertTransferOps(e),this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,numPartitions:this.partitionResult.numPartitions,transferEdges:this.partitionResult.transferEdges.length,level:le.DEBUG}),e.bumpVersion(),oe.CHANGED)}_annotateOps(){for(let e of this.partitionResult.partitions)for(let t of e.ops)t.setAttr("partition_id",e.id),t.setAttr("partition_target",e.target.name)}_insertTransferOps(e){let t=e.entryBlock,r=this._buildOrderIndex(t),{useMap:s,firstInPart:o}=this._buildInsertionIndex(t);for(let i of this.partitionResult.transferEdges){let a=i.value,u=i.src.target.name,p=i.dst.target.name;if(u===p)continue;let c=new Me("copy_to_device",[a],[a.type],{src_device:u,dst_device:p});c.setAttr("partition_id",i.dst.id),c.setAttr("partition_target",p);let f=s.get(i.dst),d=f&&f.get(a)||o.get(i.dst)||null,h=a.definingOp;if(d&&h&&r.has(h)&&r.has(d)&&r.get(h)>=r.get(d))t.insertAfter(c,h);else if(d)t.insertBefore(c,d);else if(h&&r.has(h))t.insertAfter(c,h);else{let g=e.getReturnOp();g?t.insertBefore(c,g):t.pushOp(c)}let m=c.getResult(0);for(let g of i.dst.ops)for(let y=0;y<g.numOperands;y++)g.getOperand(y)===a&&g.replaceOperand(y,m)}}_buildInsertionIndex(e){let t=[...new Set(this.partitionResult.transferEdges.map(o=>o.dst))],r=new Map,s=new Map;for(let o of t)r.set(o,new Map);for(let o of e.ops())for(let i of t){if(!i.hasOp(o))continue;s.has(i)||s.set(i,o);let a=r.get(i);for(let u=0;u<o.numOperands;u++){let p=o.getOperand(u);a.has(p)||a.set(p,o)}}return{useMap:r,firstInPart:s}}_buildOrderIndex(e){let t=new Map,r=0;for(let s of e.ops())t.set(s,r++);return t}},sw=class extends Ue{static{l(this,"PartitionMaterializationPass")}constructor(e={}){super("PartitionMaterializationPass"),this.targets=e.targets||[]}run(e,t){let r=this._collectPartitions(e);if(r.size<=1)return oe.UNCHANGED;let s=this._getModule(e);if(!s)return oe.UNCHANGED;let o=this._materializePartitions(e,r);for(let i of o)s.addFunction(i);return this._rewriteOriginalFunction(e,o,r),this.trace&&this.trace.level>=le.DEBUG&&this.trace.emit({type:"pass_detail",passName:this.name,partitions:r.size,subFunctions:o.length,level:le.DEBUG}),e.bumpVersion(),oe.CHANGED}_collectPartitions(e){let t=new Map;for(let r of e.ops()){let s=r.getAttr("partition_id");s!==void 0&&(t.has(s)||t.set(s,{id:s,target:r.getAttr("partition_target"),ops:[]}),t.get(s).ops.push(r))}return t}_getModule(e){return e._module||null}_materializePartitions(e,t){let r=[];for(let[s,o]of t){let i=new Set(o.ops),{inputs:a,outputs:u}=pa(i,o.ops),p=a.map(_=>_.type),c=u.map(_=>_.type),f=`${e.name}_partition_${s}`,d=new yn(f,p,c);d._partitionTarget=o.target;let h=new Map;for(let _=0;_<a.length;_++)h.set(a[_],d.args[_]);let m=Ru(o.ops);for(let _ of m)d.entryBlock.pushOp(_.clone(h));let g=u.map(_=>h.get(_)||_),y=new Me("return",g,[]);d.entryBlock.pushOp(y),r.push(d)}return r}_rewriteOriginalFunction(e,t,r){for(let s of t)e.setAttr?.(`sub_${s.name}`,s._partitionTarget)}}});function jd(n){return ip(n,-1)}function X4(n,e,t){let r=t.sharedMemoryBytes||16384,s=0;for(let u of e.entryBlock.ops())if(u.opName!=="yield")for(let p=0;p<u.numResults;p++){let c=u.getResult(p).type;if(!c||!c.shape)continue;let f=jd(c.shape);if(f<0)return!0;let d=f*Rn(c.dtype);d>s&&(s=d)}let o=n.getAttr("num_carry"),i=n.getAttr("num_xs"),a=0;for(let u=0;u<o;u++){let p=n.getOperand(i+u).type,c=p&&p.shape?jd(p.shape):-1;c>0&&(a+=c*Rn(p.dtype))}return 3*s+2*a>r}function ej(n,e){let r=n.regions[0].entryBlock,s=n.getAttr("num_carry"),o=[],i=null;for(let b of r.ops()){if(b.opName==="yield"){i=b;continue}o.push(b)}if(!i)return null;let a=new Set(o),u=new Set(r.arguments),p=[],c=new Set,f=[],d=new Set;for(let b of o)for(let w=0;w<b.numOperands;w++){let x=b.getOperand(w),S=x.definingOp;if(!u.has(x)&&!(S&&a.has(S))){if(S&&$o(S)){d.has(S)||(d.add(S),f.push(S));continue}c.has(x)||(c.add(x),p.push(x))}}let h=[...r.arguments,...p];for(let b of h)if(!b.type||!b.type.isFullyStatic)return null;let m=[];for(let b=0;b<i.numOperands;b++)m.push(i.getOperand(b));let g=new yn(e,h.map(b=>b.type),m.map(b=>b.type)),y=new Map;for(let b=0;b<h.length;b++)y.set(h[b],g.args[b]);for(let b of f)g.entryBlock.pushOp(b.clone(y));for(let b of o)g.entryBlock.pushOp(b.clone(y));let _=m.map(b=>y.get(b));return _.some(b=>b===void 0)?null:(g.entryBlock.pushOp(new Me("return",_,[])),{bodyFunc:g,captured:p,numCarry:s,numYs:m.length-s})}function tj(n,e,t,r,s){let{getSlot:o,newSlot:i,steps:a,addedFuncs:u}=r,p=new Mn(n.name+"_mod");p.addFunction(n);let c=ha(p,s);if(c){let f=c.plan,d=e.length,h=new Map,m=!0;for(let g of f.returnFixups||[]){if(g.kind!=="copy"){m=!1;break}h.set(g.pos,g.srcSlot)}if(m){let g=l(x=>{let S=d+x;return h.has(S)?h.get(S):f.argSlots[S]},"retSlot"),y=new Map;for(let x=0;x<d;x++)y.set(f.argSlots[x],o(e[x]));for(let x of f.intermediates)y.set(x.slot,i(x.shape,x.dtype));for(let x=0;x<t.length;x++){let S=g(x);y.has(S)||y.set(S,o(t[x]))}let _=l(x=>{let S=y.get(x);return S===void 0?null:S},"mapSlot"),b=[],w=!0;for(let x of f.steps){let S=x.inputSlots.map(_),T=x.outputSlots.map(_);if(S.includes(null)||T.includes(null)){w=!1;break}b.push({name:x.name,inputSlots:S,outputSlots:T})}if(w){for(let x of b)a.push(x);for(let x of p.functions())u.push(x);return!0}}}return a.push({name:n.name,inputSlots:e.map(o),outputSlots:t.map(o)}),u.push(n),!0}function E2(n,e,t){let r=fb({ops:n,opSet:new Set(n)},e,new Map);return r?tj(r.subFunc,r.inputs,r.outputs,t,1):!1}function nj(n,e,t){let{getSlot:r,newSlot:s,steps:o,scanLoops:i,addedFuncs:a}=t,u=n.regions[0];if(!u||!u.entryBlock)return!1;let p=ej(n,e);if(!p)return!1;let{bodyFunc:c,captured:f,numCarry:d,numYs:h}=p,m=n.getAttr("num_xs"),g=new Mn(e+"_mod");g.addFunction(c);let y=ha(g,2),_=y?y.plan:null,b=[],w=[];for(let D=0;D<d;D++){let V=n.getOperand(m+D).type;b.push(V.shape),w.push(V.dtype)}let x=[],S=[];for(let D=0;D<m;D++){let V=n.getOperand(D).type;x.push(V.shape.slice(1)),S.push(V.dtype)}let T=[],v=[];for(let D=0;D<h;D++){let V=n.getResult(d+D).type;T.push(V.shape.slice(1)),v.push(V.dtype)}let A=b.map((D,V)=>s(D,w[V])),N=b.map((D,V)=>s(D,w[V])),k=x.map((D,V)=>s(D,S[V])),O=[],R=[],B=[],E=[];for(let D=0;D<d;D++)R.push(r(n.getOperand(m+D)));for(let D=0;D<d;D++)B.push(r(n.getResult(D)));for(let D of f)E.push(r(D));let F=[],C=[];for(let D=0;D<m;D++)F.push(r(n.getOperand(D)));for(let D=0;D<h;D++)C.push(r(n.getResult(d+D)));let H=o.length;if(!_)O=T.map((D,V)=>s(D,v[V])),o.push({name:c.name,inputSlots:[...k,...A,...E],outputSlots:[...N,...O]}),a.push(c);else{let D=m+d+f.length,V=new Map;for(let j of _.returnFixups||[]){if(j.kind!=="copy")return!1;V.set(j.pos,j.srcSlot)}let ue=l(j=>{let W=D+j;return V.has(W)?V.get(W):_.argSlots[W]},"bodyReturnSlot"),ce=new Map;for(let j=0;j<m;j++)ce.set(_.argSlots[j],k[j]);for(let j=0;j<d;j++)ce.set(_.argSlots[m+j],A[j]);for(let j=0;j<f.length;j++)ce.set(_.argSlots[m+d+j],E[j]);for(let j of _.intermediates)ce.set(j.slot,s(j.shape,j.dtype));for(let j=0;j<d;j++){let W=ue(j);ce.has(W)||ce.set(W,N[j])}for(let j=0;j<h;j++){let W=ue(d+j),ie=ce.get(W);ie===void 0&&(ie=s(T[j],v[j]),ce.set(W,ie)),O.push(ie)}let Le=l(j=>{let W=ce.get(j);return W===void 0?null:W},"mapSlot");for(let j of _.steps){let W=j.inputSlots.map(Le),ie=j.outputSlots.map(Le);if(W.includes(null)||ie.includes(null))return!1;o.push({name:j.name,inputSlots:W,outputSlots:ie})}for(let j of g.functions())a.push(j)}let G=o.length,q=n.getOperand(0).type.shape[0];return typeof q!="number"||q<0?!1:(i.push({T:q,loopStart:H,loopEnd:G,carry:b.map((D,V)=>({a:A[V],b:N[V],initSlot:R[V],finalSlot:B[V],bytes:jd(D)*Rn(w[V])})),xs:k.map((D,V)=>({xtSlot:D,xsSlot:F[V],stepBytes:jd(x[V])*Rn(S[V])})),ys:O.map((D,V)=>({ytSlot:D,ysSlot:C[V],stepBytes:jd(T[V])*Rn(v[V])}))}),!0)}function ok(n,e,t=!1){if(!e||typeof e.isWebGPU!="function"||!e.isWebGPU()||n.functionCount!==1)return null;let r=n.functions().next().value,s=r.getReturnOp();if(!s)return null;let o=[];for(let R of r.ops())R.opName==="scan"&&o.push(R);if(o.length===0)return null;let i=new Set,a=l(R=>{if(!(!R||i.has(R))){i.add(R);for(let B=0;B<R.numOperands;B++)a(R.getOperand(B).definingOp)}},"visit");for(let R=0;R<s.numOperands;R++)a(s.getOperand(R).definingOp);for(let R of o)if(!i.has(R))return null;if(!t){let R=!1;for(let B of o){let E=B.regions[0];if(E&&E.entryBlock&&X4(B,E,e)){R=!0;break}}if(!R)return null}let u=[];for(let R of i)$o(R)||Xt(R.opName)||u.push(R);let p=Ru(u),c=new Map,f=0,d=l(R=>{let B=c.get(R);return B===void 0&&(B=f++,c.set(R,B)),B},"getSlot");for(let R of r.args)d(R);let h=[],m=l((R,B)=>{let E=f++;return h.push({slot:E,shape:[...R],dtype:B}),E},"newSlot"),g=[],y=[],_=[],b={getSlot:d,newSlot:m,steps:g,scanLoops:y,addedFuncs:_},w=new Set(o),x=[],S=0,T=0;for(let R of p)if(w.has(R)){if(x.length&&!E2(x,r.name+"_seg"+S++,b)||(x=[],!nj(R,r.name+"_scan"+T++,b)))return null}else x.push(R);if(x.length&&!E2(x,r.name+"_seg"+S++,b))return null;let v=r.args.map(d),A=[],N=new Set;for(let R=0;R<s.numOperands;R++){let B=s.getOperand(R),E=v.length;if(c.has(B)&&!(B.isBlockArgument&&B.isBlockArgument())){let F=d(B);N.has(F)?(v.push(f++),A.push({pos:E,kind:"copy",srcSlot:F})):(N.add(F),v.push(F))}else if(B.isBlockArgument&&B.isBlockArgument())v.push(f++),A.push({pos:E,kind:"copy",srcSlot:d(B)});else return null}let k=new Set(v),O=new Set;for(let[R,B]of c)if(!(k.has(B)||O.has(B))){if(O.add(B),!R.type||!R.type.isFullyStatic)return null;h.some(E=>E.slot===B)||h.push({slot:B,shape:[...R.type.shape],dtype:R.type.dtype})}n.removeFunction(r.name);for(let R of _)n.addFunction(R);return{plan:{numSlots:f,argSlots:v,intermediates:h,steps:g,returnFixups:A,scanLoops:y}}}var L2=M(()=>{"use strict";yr();qs();Di();db();Ir();tr();Re();l(jd,"numel");l(X4,"isScanOversized");l(ej,"buildScanBodyFunction");l(tj,"inlineNativeSplit");l(E2,"emitSegment");l(nj,"emitScanLoop");l(ok,"splitGraphForScan")});function qd(n){ik.push(n),ik.sort((e,t)=>(e.priority??100)-(t.priority??100))}function rj(n){for(let e of ik)if(e.applies(n))return e;return null}function C2(n,e){let t=rj(e);return t?t.run(n,e):null}var ik,D2=M(()=>{"use strict";db();L2();ik=[];l(qd,"registerGraphSplitStrategy");l(rj,"selectGraphSplitStrategy");l(C2,"splitGraph");qd({name:"cublas",priority:10,applies:l(n=>n.config.matmulBackend==="cublas","applies"),run:l(n=>QE(n),"run")});qd({name:"cuda-attention",priority:15,applies:l(n=>n.cudaAttention,"applies"),run:l(n=>ha(n,1),"run")});qd({name:"cuda-native-matmul-chain",priority:20,applies:l(n=>n.cudaMatmulChain,"applies"),run:l(n=>ha(n),"run")});qd({name:"cuda-native-conv-chain",priority:25,applies:l(n=>n.cudaConvChain,"applies"),run:l(n=>ha(n),"run")});qd({name:"webgpu",priority:30,applies:l(n=>n.isWebGPU,"applies"),run:l((n,e)=>{let t=ok(n,e.target);return!t&&JE(n,e.target.maxThreadsPerBlock||256)&&(t=ok(n,e.target,!0),t||(t=ha(n,2))),t},"run")})});function P2(n,e){let t=new Am;for(let[,r]of n.bufferMap)t.paramBuffers.add(r.name);return sj(n.body,t,e),ij(n,t,e),aj(n.body,t),t}function sj(n,e,t){let r=[n];for(;r.length>0;){let s=r.pop();if(!(!s||typeof s!="object")){switch(s.type){case"ForNode":if(s.loopVar&&e.locals.set(s.loopVar.name,"i32"),s.kind===z.THREAD_BINDING&&s.threadTag){let o=s.extent&&s.extent.type==="IntImmNode"?s.extent.value:0,i=!s.extent||s.extent.type!=="IntImmNode",a={varName:s.loopVar.name,extent:o,isDynamic:i,extentNode:s.extent};e.threadBindings.has(s.threadTag)?e.threadBindings.get(s.threadTag).push(a):e.threadBindings.set(s.threadTag,[a])}break;case"LetStmtNode":s.variable&&e.locals.set(s.variable.name,Vi(s.variable.dtype));break;case"BufferStoreNode":case"BufferLoadNode":s.buffer&&e.usedBuffers.set(s.buffer.name,s.buffer);break;case"AllocateNode":s.buffer&&e.allocatedBuffers.add(s.buffer.name),s.scope==="shared"&&s.buffer&&e.sharedBuffers.push(s.buffer);break;case"CallExternNode":s.externName&&!n1(s.externName)&&e.externCalls.set(s.externName,{argCount:s.args?s.args.length:0,dtype:s.dtype||"f32"});break;case"BlockNode":if(s.reads)for(let o of s.reads)o.buffer&&e.usedBuffers.set(o.buffer.name,o.buffer);if(s.writes)for(let o of s.writes)o.buffer&&e.usedBuffers.set(o.buffer.name,o.buffer);if(s.iterVars)for(let o of s.iterVars)o.iterVar&&e.locals.set(o.iterVar.name,Vi(o.iterVar.dtype)),o.binding&&typeof o.binding=="object"&&o.binding.type&&r.push(o.binding);break}if(s.body&&r.push(s.body),s.value&&typeof s.value=="object"&&s.value.type&&r.push(s.value),s.stmts)for(let o of s.stmts)r.push(o);if(s.thenBody&&r.push(s.thenBody),s.elseBody&&r.push(s.elseBody),s.initBody&&r.push(s.initBody),s.condBody&&r.push(s.condBody),s.loopBody&&r.push(s.loopBody),s.condition&&typeof s.condition=="object"&&s.condition.type&&r.push(s.condition),s.a&&typeof s.a=="object"&&s.a.type&&r.push(s.a),s.b&&typeof s.b=="object"&&s.b.type&&r.push(s.b),s.expr&&typeof s.expr=="object"&&s.expr.type&&r.push(s.expr),s.args)for(let o of s.args)typeof o=="object"&&o!==null&&o.type&&r.push(o);if(s.indices)for(let o of s.indices)typeof o=="object"&&o!==null&&o.type&&r.push(o)}}}function ij(n,e,t){let r=e.memoryLayout.alignment,s=0,o=l(i=>{let a=i.shape.some(c=>typeof c!="number"||c<0),u=i.numel();if(!a&&u>=0)return u*Rn(i.dtype);let p=1;for(let c of i.shape)p*=typeof c=="number"&&c>0?c:1;return Math.max(oj,p*Rn(i.dtype))},"bufBytes");for(let[,i]of n.bufferMap)s=Math.ceil(s/r)*r,e.memoryLayout.bufferOffsets.set(i.name,s),s+=o(i);for(let[i,a]of e.usedBuffers)e.memoryLayout.bufferOffsets.has(i)||(s=Math.ceil(s/r)*r,e.memoryLayout.bufferOffsets.set(i,s),s+=o(a));e.memoryLayout.totalBytes=s}function aj(n,e){let t=new Map;for(let r of yl(n,s=>s.type==="BufferStoreNode"&&s.buffer&&!e.paramBuffers.has(s.buffer.name))){let s=r.buffer.name;t.has(s)||t.set(s,[]),t.get(s).push(r.value)}for(let[r,s]of t)if(s.every(i=>i&&i.type==="IntImmNode"&&i.value===0||i&&i.type==="FloatImmNode"&&i.value===0)&&e.zeroBuffers.add(r),s.length===1&&s[0]){let i=s[0];(i.type==="IntImmNode"||i.type==="FloatImmNode")&&e.constantBuffers.set(r,i.value)}}var oj,$2=M(()=>{"use strict";Sp();tr();Fe();ts();l(P2,"scanMetadata");l(sj,"walkTree");oj=65536;l(ij,"computeMemoryLayout");l(aj,"detectZeroBuffers")});function Ud(n,e,t){let r=typeof n.offset=="number"?n.offset:0;if(e.length===0)return new L(r);if(e.length===1)return r===0?e[0]:new P("+",e[0],new L(r));let s=[];r!==0&&s.push(new L(r));for(let o=0;o<e.length;o++){let i=e[o];if(i.type==="IntImmNode"&&i.value===0)continue;let a=n.strides[o];if(a===1)s.push(i);else if(typeof a=="number"&&a>=0)s.push(new P("*",i,new L(a)));else{let u=uj(n,o,t);s.push(new P("*",i,u))}}return s.length===0?new L(0):s.reduce((o,i)=>new P("+",o,i))}function uj(n,e,t){let r=[];for(let s=e+1;s<n.shape.length;s++){let o=n.shape[s];typeof o=="number"&&o>=0?r.push(new L(o)):r.push(lj(n,s,t))}return r.length===0?new L(1):r.reduce((s,o)=>new P("*",s,o))}function lj(n,e,t){let r=n.shape[e];if(r instanceof Pe)return ll(r,s=>new Oe(Uu(s),"index"));if(t){let s=`${n.name}:${e}`,o=t.get(s);if(o)return new Oe(o.name,"index")}return new L(1)}var F2=M(()=>{"use strict";Fe();po();R0();l(Ud,"flattenIndex");l(uj,"computeDynamicStride");l(lj,"resolveShapeParam")});function z2(n,e){let t=P2(n,e),r={shapeParamMap:n.shapeParamMap,accCounter:0,metadata:t},s=Bs(n.body,r);return new km(n.name,n.params,s,n.bufferMap,n.shapeParams,n.shapeParamMap,t)}function Bs(n,e){if(!n||typeof n!="object")return n;switch(n.type){case"ForNode":return cj(n,e);case"BlockNode":return pj(n,e);case"SeqNode":return fj(n,e);case"BufferStoreNode":return dj(n,e);case"LetStmtNode":return hj(n,e);case"AllocateNode":return mj(n,e);case"IfThenElseNode":return gj(n,e);case"WhileNode":return yj(n,e);case"EvaluateNode":return new il(Nr(n.value,e));case"SyncThreadsNode":return n;default:return n}}function cj(n,e){let t=n.accumulator!==void 0?n.accumulator:Zb(n);if(t)return _j(n,t,e);let r=Bs(n.body,e);return new he(n.loopVar,n.min,n.extent,n.kind,r,n.threadTag)}function pj(n,e){let t=[];for(let o of n.iterVars)o.iterVar&&o.binding&&t.push({name:o.iterVar.name,dtype:o.iterVar.dtype,expr:Nr(o.binding,e)});let r=Bs(n.body,e),s=n.initBody?Bs(n.initBody,e):null;return t.length>0?s?(r=new te([new gl(t,s),new gl(t,r)]),s=null):r=new gl(t,r):s&&(r=new te([s,r])),r}function fj(n,e){let t=[];for(let r of n.stmts)t.push(Bs(r,e));return new te(t)}function dj(n,e){let t=Ud(n.buffer,n.indices,e.shapeParamMap),r=Nr(n.value,e),s=n.buffer.dtype||br(n.value);return new xp(n.buffer,t,r,s)}function hj(n,e){return new et(n.variable,Nr(n.value,e),Bs(n.body,e))}function mj(n,e){return new qe(n.buffer,n.scope,Bs(n.body,e))}function gj(n,e){return new Se(Nr(n.condition,e),Bs(n.thenBody,e),n.elseBody?Bs(n.elseBody,e):null)}function yj(n,e){return new $i(n.condVar,Bs(n.condBody,e),Bs(n.loopBody,e))}function Nr(n,e){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"BufferLoadNode":{let t=Ud(n.buffer,n.indices,e.shapeParamMap),r=new Tp(n.buffer,t,n.buffer.dtype);return si(r),r}case"MathOpNode":{let t=Nr(n.a,e),r=n.b?Nr(n.b,e):null,s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),si(s),s}case"CompareNode":{let t=Nr(n.a,e),r=Nr(n.b,e),s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),si(s),s}case"CastNode":{let t=Nr(n.expr,e),r={...n,expr:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),si(r),r}case"CallExternNode":{let t=n.args.map(s=>Nr(s,e)),r={...n,args:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),si(r),r}case"IfThenElseNode":{let t=Nr(n.condition,e),r=Nr(n.thenBody,e),s=n.elseBody?Nr(n.elseBody,e):null,o={...n,condition:t,thenBody:r,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),si(o),o}default:return si(n),n}}function _j(n,e,t){let r=`_acc_${t.accCounter++}`,s=br(e.loadSide);t.metadata.locals.set(r,s);let o=new Map;for(let h of e.block.iterVars)h.iterVar&&h.binding&&o.set(h.iterVar.name,h.binding);let i=o.size>0?zr(e.valueSide,o):e.valueSide,a=Ud(e.store.buffer,e.outerIndices,t.shapeParamMap),u=new Tp(e.store.buffer,a,e.store.buffer.dtype);si(u);let p=Nr(i,t),c=Ud(e.store.buffer,e.outerIndices,t.shapeParamMap),f=new xp(e.store.buffer,c,null,e.store.buffer.dtype),d=e.block.initBody?o.size>0?Wd(e.block.initBody,o):e.block.initBody:null;return new Nm({localName:r,dtype:s,op:e.op,initLoad:u,loopVar:n.loopVar,extent:n.extent,loopKind:n.kind,body:p,flushStore:f,initBody:d?Bs(d,t):null})}function zr(n,e){if(!n||typeof n!="object"||!n.type)return n;if(n.type==="VariableNode"&&e.has(n.name))return e.get(n.name);if(n.type==="BufferLoadNode"){let t=n.indices.map(o=>zr(o,e));if(!t.some((o,i)=>o!==n.indices[i]))return n;let s={...n,indices:t};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="MathOpNode"){let t=zr(n.a,e),r=n.b?zr(n.b,e):null;if(t===n.a&&r===n.b)return n;let s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="CompareNode"){let t=zr(n.a,e),r=zr(n.b,e);if(t===n.a&&r===n.b)return n;let s={...n,a:t,b:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="CastNode"){let t=zr(n.expr,e);if(t===n.expr)return n;let r={...n,expr:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}if(n.type==="CallExternNode"){let t=n.args.map(o=>zr(o,e));if(!t.some((o,i)=>o!==n.args[i]))return n;let s={...n,args:t};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}if(n.type==="IfThenElseNode"){let t=zr(n.condition,e),r=zr(n.thenBody,e),s=n.elseBody?zr(n.elseBody,e):null;if(t===n.condition&&r===n.thenBody&&s===n.elseBody)return n;let o={...n,condition:t,thenBody:r,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),o}return n}function Wd(n,e){if(!n||typeof n!="object"||!n.type)return n;switch(n.type){case"BufferStoreNode":{let t=n.indices.map(o=>zr(o,e)),r=zr(n.value,e);if(t.every((o,i)=>o===n.indices[i])&&r===n.value)return n;let s={...n,indices:t,value:r};return Object.setPrototypeOf(s,Object.getPrototypeOf(n)),s}case"SeqNode":{let t=n.stmts.map(s=>Wd(s,e)),r={...n,stmts:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}case"ForNode":{let t=Wd(n.body,e);if(t===n.body)return n;let r={...n,body:t};return Object.setPrototypeOf(r,Object.getPrototypeOf(n)),r}case"IfThenElseNode":{let t=zr(n.condition,e),r=Wd(n.thenBody,e),s=n.elseBody?Wd(n.elseBody,e):null,o={...n,condition:t,thenBody:r,elseBody:s};return Object.setPrototypeOf(o,Object.getPrototypeOf(n)),o}default:return n}}var V2=M(()=>{"use strict";Sp();$2();F2();Fe();Jv();l(z2,"lowerToLIR");l(Bs,"lowerStmt");l(cj,"lowerForNode");l(pj,"lowerBlockNode");l(fj,"lowerSeqNode");l(dj,"lowerBufferStore");l(hj,"lowerLetStmt");l(mj,"lowerAllocate");l(gj,"lowerIfThenElse");l(yj,"lowerWhile");l(Nr,"lowerExpr");l(_j,"lowerAccumulator");l(zr,"substituteVars");l(Wd,"substituteVarsStmt")});function G2(n){let e=[],t={errors:e,boundVars:new Set,path:[],bufferNames:new Set};if(!n||n.type!=="LIRFunc")return e.push(new Go("root must be LIRFunc")),e;for(let[,r]of n.bufferMap)t.bufferNames.add(r.name);for(let r of n.shapeParams)t.boundVars.add(r.name);if(n.metadata)for(let[r]of n.metadata.locals)t.boundVars.add(r);return Rs(n.body,t),e}function Rs(n,e){if(!(!n||typeof n!="object")){switch(e.path.push(n.type),n.type){case"ForNode":bj(n,e);break;case"SeqNode":for(let t of n.stmts)Rs(t,e);break;case"LIRFlatStoreNode":wj(n,e);break;case"LIRAccumulatorNode":Tj(n,e);break;case"LIRBindingsNode":xj(n,e);break;case"LetStmtNode":{Pn(n.value,e);let t=e.boundVars.has(n.variable.name);e.boundVars.add(n.variable.name),Rs(n.body,e),t||e.boundVars.delete(n.variable.name);break}case"AllocateNode":{let t=n.buffer?e.bufferNames.has(n.buffer.name):!0;n.buffer&&e.bufferNames.add(n.buffer.name),Rs(n.body,e),n.buffer&&!t&&e.bufferNames.delete(n.buffer.name);break}case"IfThenElseNode":Pn(n.condition,e),Rs(n.thenBody,e),n.elseBody&&Rs(n.elseBody,e);break;case"WhileNode":Rs(n.condBody,e),Rs(n.loopBody,e);break;case"EvaluateNode":Pn(n.value,e);break;default:break}e.path.pop()}}function bj(n,e){n.extent?Pn(n.extent,e):e.errors.push(new Go("ForNode missing extent",[...e.path]));let t=!0;n.loopVar?(t=e.boundVars.has(n.loopVar.name),e.boundVars.add(n.loopVar.name)):e.errors.push(new Go("ForNode missing loopVar",[...e.path])),Rs(n.body,e),n.loopVar&&!t&&e.boundVars.delete(n.loopVar.name)}function wj(n,e){n.buffer||e.errors.push(new Go("LIRFlatStoreNode missing buffer",[...e.path])),Pn(n.offsetExpr,e),Pn(n.value,e)}function Tj(n,e){n.localName||e.errors.push(new Go("LIRAccumulatorNode missing localName",[...e.path])),n.dtype||e.errors.push(new Go("LIRAccumulatorNode missing dtype",[...e.path]));let t=n.localName?e.boundVars.has(n.localName):!0;n.localName&&e.boundVars.add(n.localName),Pn(n.initLoad,e),Pn(n.body,e),Rs(n.flushStore,e),n.initBody&&Rs(n.initBody,e),n.localName&&!t&&e.boundVars.delete(n.localName)}function xj(n,e){let t=[];for(let r of n.bindings)Pn(r.expr,e),e.boundVars.has(r.name)||t.push(r.name),e.boundVars.add(r.name);Rs(n.body,e);for(let r of t)e.boundVars.delete(r)}function Pn(n,e){if(!(!n||typeof n!="object"||!n.type))switch(n.type){case"LIRFlatLoadNode":n.buffer||e.errors.push(new Go("LIRFlatLoadNode missing buffer",[...e.path])),Pn(n.offsetExpr,e);break;case"MathOpNode":Pn(n.a,e),n.b&&Pn(n.b,e);break;case"CompareNode":Pn(n.a,e),Pn(n.b,e);break;case"CastNode":Pn(n.expr,e);break;case"CallExternNode":if(n.args)for(let t of n.args)Pn(t,e);break;case"IfThenElseNode":Pn(n.condition,e),Pn(n.thenBody,e),n.elseBody&&Pn(n.elseBody,e);break;case"VariableNode":n.name!==void 0&&!e.boundVars.has(n.name)&&e.errors.push(new Go(`unbound variable '${n.name}'`,[...e.path]));break;case"IntImmNode":case"FloatImmNode":break;default:break}}var Go,j2=M(()=>{"use strict";Go=class{static{l(this,"LIRVerificationError")}constructor(e,t){this.message=e,this.nodePath=t||[]}toString(){let e=this.nodePath.length>0?` at ${this.nodePath.join(" > ")}`:"";return`LIR verification: ${this.message}${e}`}};l(G2,"verifyLIR");l(Rs,"verifyStmt");l(bj,"verifyForNode");l(wj,"verifyFlatStore");l(Tj,"verifyAccumulator");l(xj,"verifyBindings");l(Pn,"verifyExpr")});function q2(n,e,t={}){return new Si({target:e,...t}).compileFunction(n)}var ow,ak,Si,iw=M(()=>{"use strict";Di();qs();_E();_y();bE();sL();v2();$a();Op();fy();k2();O2();tk();R2();I2();D2();Cv();nn();RS();nv();V2();j2();nn();ow=class{static{l(this,"CompilerConfig")}constructor(e={}){this.target=e.target,this.verify=e.verify!==!1,this.verifyMode=e.verify==="full"?"full":"normal",this.errorMode=e.errorMode||"strict";let t=this.target&&typeof this.target.isWebGPU=="function"&&this.target.isWebGPU(),s=this.target&&typeof this.target.isGPU=="function"&&this.target.isGPU()&&!t;this.fusion={enabled:!0,strategy:"priority",...e.fusion},this.scheduling={enabled:t,autotune:!1,gpuTiling:s,...e.scheduling},this.matmulBackend=e.matmulBackend||"native",this.quantization={enabled:!1,...e.quantization},this.optimization={layout:!1,rematerialization:!1,rematConfig:{},fastMath:!1,maxSimplifyIterations:8,loopPartition:!1,detectAccumulators:!1,tensorize:!1,...e.optimization},this.memory={alignment:64,inplaceReuse:!0,allocStrategy:"best-fit",poolAllocation:!1,...e.memory},this.partition={enabled:!1,targets:[],defaultTarget:null,opTargetOverrides:new Map,memoryLimits:new Map,minPartitionSize:1,costWeights:{},...e.partition},this.passContext=e.passContext||null,this.loweringRules=e.loweringRules||null,this.codegenEntries=e.codegenEntries||null;let o=e.trace||{};this.trace={level:o.level??le.SILENT,sink:o.sink??null,irSnapshot:{afterGraphPasses:!1,afterLowering:!1,afterScheduling:!1,...o.irSnapshot||{}}}}get usePartition(){return this.partition.enabled&&this.partition.targets.length>=2}},ak=class{static{l(this,"CompilationResult")}constructor(e,t,r){this.module=e,this.trace=t,this.errors=r||[]}get succeeded(){return this.errors.length===0}get failedFunctions(){let e=new Set;for(let t of this.errors)t.funcName&&e.add(t.funcName);return e}run(e,...t){return this.module.run(e,...t)}async runAsync(e,...t){return this.module.runAsync(e,...t)}isAsync(e){return this.module.isAsync(e)}getSource(e){return this.module.getKernelSource(e)}listKernels(){return this.module.listKernels()}},Si=class n{static{l(this,"Compiler")}constructor(e){if(this.config=e instanceof ow?e:new ow(e),!this.config.target)throw new Error("Compiler requires a target");this.context=new z_({loweringRules:this.config.loweringRules,codegenEntries:this.config.codegenEntries})}compile(e){let t=new yy(this.config.trace),r=this.config.errorMode==="resilient",s=[],o=new Set,i=performance.now();t.phaseStart("compile");let a={compiler:this,trace:t,errors:s,failed:o,resilient:r,original:e,working:r?wA(e):e,cudaMatmulChain:!1,split:null,primFuncs:null,lirFuncs:null,runtimeModule:null};for(let u of this._compilePhases())u.when&&!u.when(a)||u.run(a);if(t.phaseEnd("compile",performance.now()-i),!r&&s.length>0)throw new Error(s[0].toString());return new ak(a.runtimeModule,t,s)}_compilePhases(){return[{name:"verify:pre",when:l(e=>e.compiler.config.verify,"when"),run:l(e=>e.compiler._verifyGraph(e.working,"before graph passes",e.trace,e.errors,e.failed,e.resilient),"run")},{name:"calibrate",when:l(e=>{let t=e.compiler.config.quantization;return t.enabled&&t.calibrationData&&!t.calibration},"when"),run:l(e=>e.compiler._runCalibration(e.working,e.trace),"run")},{name:"graphPasses",run:l(e=>{e.cudaMatmulChain=e.compiler._runGraphPasses(e.working,e.original,e.trace,e.errors,e.failed,e.resilient)},"run")},{name:"partition",when:l(e=>e.compiler.config.usePartition,"when"),run:l(e=>e.compiler._runPartitioning(e.working,e.trace),"run")},{name:"split",run:l(e=>{let t=e.compiler.config,r=typeof t.target.isWebGPU=="function"&&t.target.isWebGPU(),s=typeof t.target.isGPU=="function"&&t.target.isGPU()&&!r,o=0,i=0;for(let p of e.working)for(let c of p.ops())c.opName==="conv"||c.opName==="quantized_conv"?o++:c.opName==="scaled_dot_product_attention"&&i++;let a=s&&o>=2,u=s&&i>0;e.split=C2(e.working,{config:t,target:t.target,cudaMatmulChain:e.cudaMatmulChain,cudaConvChain:a,cudaAttention:u,isWebGPU:r})},"run")},{name:"verify:post",when:l(e=>e.compiler.config.verify,"when"),run:l(e=>e.compiler._verifyGraph(e.working,"after graph passes",e.trace,e.errors,e.failed,e.resilient),"run")},{name:"lowering",run:l(e=>{if(e.primFuncs=e.compiler._lowerAll(e.working,e.trace,e.errors,e.failed,e.resilient),e.compiler.config.matmulBackend==="cublas")for(let t of e.primFuncs)t.cublasInfo=e.split&&e.split.cublasInfos?e.split.cublasInfos.get(t.name)||null:A1(t)},"run")},{name:"tirPasses",run:l(e=>e.compiler._runTirPasses(e),"run")},{name:"verify:tensor",when:l(e=>e.compiler.config.verify,"when"),run:l(e=>e.compiler._verifyAll(e.primFuncs,e.errors,e.failed,e.resilient),"run")},{name:"lirLowering",run:l(e=>{e.lirFuncs=e.compiler._lowerToLIR(e.primFuncs,e.trace,e.errors,e.failed,e.resilient)},"run")},{name:"codegen",run:l(e=>{e.runtimeModule=e.compiler._codegen(e.lirFuncs,e.trace,e.errors,e.failed,e.resilient),e.split&&(e.runtimeModule.executionPlan=e.split.plan)},"run")}]}compileFunction(e){let t=new Mn("single");return t.addFunction(e),this.compile(t)}calibrate(e,t="minmax"){let r=new Cc(t);for(let s of e)r.attach(s);return r}_runCalibration(e,t){let r=this.config.quantization,s=e.functionNames()[0],o=e.getFunction(s);if(!o)return;t.phaseStart("calibrate");let i=performance.now(),a=this.config.target,u=l((c,f)=>new n({target:f,verify:this.config.verify}).compile(c),"compileFn"),p=B2(o,a,r.calibrationData,{mode:r.calibrationMode||"minmax",quantizableOps:r.quantizableOps,compileFn:u});this.config.quantization={...r,calibration:p},t.phaseEnd("calibrate",performance.now()-i)}_runGraphPasses(e,t,r,s,o,i){let a=new iu,u=0;for(let m of e)for(let g of m.ops())g.opName==="dot"&&u++;let p=this.config.target,c=(p.getAttr&&p.getAttr("matmulChainThreshold"))??(p.kind==="cuda"?2:1/0),f=u>=c;for(let m of rL(this.config,this.config.target,{cudaMatmulChain:f,context:this.context}))a.addPass(m);a.setTrace(r),this.config.verifyMode==="full"&&a.setVerifyHook((m,g)=>(g?Qv(m):Gd(m)).map(_=>_.toString())),r.phaseStart("graphPasses");let d=performance.now(),h=a.run(e,{errorMode:i?"resilient":"strict",passContext:this.config.passContext});if(h.errors){for(let m of h.errors)s.push(m),r.errorEvent(m.phase,m.funcName,m.message,m.passName);if(h.failedFunctions){for(let m of h.failedFunctions)if(o.add(m),i&&t&&t!==e){let g=t.getFunction(m);g&&e.addFunction(um(g))}}}if(r.phaseEnd("graphPasses",performance.now()-d),r.shouldSnapshot("afterGraphPasses")){let m=new If;r.irDump("afterGraphPasses",m.printModule(e))}return f}_runPartitioning(e,t){let r=new iu;r.addPass(new rw(this.config.partition)),r.addPass(new sw({targets:this.config.partition.targets})),r.setTrace(t),t.phaseStart("partition");let s=performance.now();r.run(e),t.phaseEnd("partition",performance.now()-s)}_eachFunc(e,t,r,s,o,i,a){for(let u of e)if(!o.has(u.name))try{a(u)}catch(p){if(s.push(new Yn(t,u.name,p.message)),o.add(u.name),r.errorEvent(t,u.name,p.message),!i)break}}_lowerAll(e,t,r,s,o){t.phaseStart("lowering");let i=performance.now(),a=[];return this._eachFunc(e,"lowering",t,r,s,o,u=>{let p=performance.now(),c=wm(u,this.config.target,this.context);this.config.target.isGPU&&this.config.target.isGPU()&&!(this.config.target.isWebGPU&&this.config.target.isWebGPU())&&(c.convInfo=WL(u)),t.functionEvent("lowering",u.name,{durationMs:performance.now()-p}),a.push(c),t.shouldSnapshot("afterLowering")&&t.irDump("afterLowering:"+u.name,V_(c))}),t.phaseEnd("lowering",performance.now()-i),a}_runTirPasses(e){let t=new G_;for(let r of S2(this.config))t.addPass(r);if(t.setTrace(e.trace),this.config.verifyMode==="full"){let r=new Vd;t.setVerifyHook(s=>r.verify(s))}t.run(e.primFuncs,{trace:e.trace,errors:e.errors,failed:e.failed,resilient:e.resilient})}_verifyGraph(e,t,r,s,o,i){if(i){for(let u of e){if(o.has(u.name))continue;let p=Gd?Gd(u):[];if(p.length>0){let c=p.map(f=>f.toString()).join("; ");s.push(new Yn("verification",u.name,c)),o.add(u.name),r.errorEvent("verification",u.name,c)}}return}let a=Qv(e);if(a.length>0)throw new Error("Graph verification failed ("+t+"): "+a.map(u=>u.toString()).join("; "))}_verifyAll(e,t,r,s){let o=new Vd;for(let i of e){if(r.has(i.name))continue;let a=o.verify(i);if(a.length>0){let u=a.join("; ");if(s)t.push(new Yn("verification",i.name,u)),r.add(i.name);else throw new Error("TensorIR verification failed for "+i.name+": "+u)}}}_lowerToLIR(e,t,r,s,o){t.phaseStart("lirLowering");let i=performance.now(),a=[];return this._eachFunc(e,"lirLowering",t,r,s,o,u=>{let p=performance.now(),c=z2(u,this.config.target);if(u.cublasInfo&&(c.cublasInfo=u.cublasInfo),u.gpuRegisterBlocked&&(c.gpuRegisterBlocked=!0),this.config.verifyMode==="full"){let f=G2(c);if(f.length>0)throw new Error("LIR verification failed: "+f.map(d=>d.toString()).join("; "))}t.functionEvent("lirLowering",u.name,{durationMs:performance.now()-p}),a.push(c)}),t.phaseEnd("lirLowering",performance.now()-i),a}_codegen(e,t,r,s,o){t.phaseStart("codegen");let i=performance.now(),a=new Gl("compiled"),u=this.config.usePartition,p={matmulBackend:this.config.matmulBackend,context:this.context},c=new Map,f=l(h=>(c.has(h.name)||c.set(h.name,new _o(h,p)),c.get(h.name)),"getBackend"),d=u?null:new _o(this.config.target,p);return this._eachFunc(e,"codegen",t,r,s,o,h=>{let m=performance.now(),g;if(u){let _=h._partitionTarget,b=_?this.config.partition.targets.find(w=>w.name===_):this.config.target;g=f(b||this.config.target)}else g=d;let y=g.compile(h);a.addCompiledKernel(y),h.shapeParamMap&&h.shapeParamMap.size>0&&a.setShapeParamMap(h.name,h.shapeParamMap,h.bufferMap),t.codegenStats(h.name,{durationMs:performance.now()-m,sourceSize:y.source.length,targetName:y.target.name})}),t.phaseEnd("codegen",performance.now()-i),a}};l(q2,"compileGraph")});function vj(n){return typeof n=="object"&&n!==null}function kj(n){return vj(n)&&"_impl"in n}function Nj(n){return(e,...t)=>{let r=As();if(!r)throw new Error(`TRACING dispatch key active but no tracer is set for op '${n}'`);if(Sj.has(n)){let c=Lt.findOp(n);return Lt.redispatch(c,e,...t)}let s=[],o={},i=Ey(n),a=0,u=l(c=>c instanceof bt||kj(c),"isTensor"),p=l(c=>{c instanceof bt||c.isSymbolic?s.push(c):s.push(r.captureConstant(c))},"pushTensor");for(let c of t)if(Array.isArray(c)&&c.length>0&&u(c[0]))for(let f of c)p(f);else u(c)?p(c):i&&(c!=null&&a<i.length&&(o[i[a]]=c),a++);return r.recordOp(n,s,o)}}function W2(){let n=Lt.listOps();for(let e of n){let t=Lt.findOp(e);if(!t)continue;let r=t.name,s=Mr.fromUnboxed(Nj(r));t.entry.registerKernel(48,s)}}var Sj,K2=M(()=>{"use strict";An();rp();Ma();_u();yu();Ly();Sj=new Set(["scatter","repeat","tile","split","chunk","roll","flip","cumsum","sort","argsort","topk"]);l(vj,"_isObject");l(kj,"_hasImpl");l(Nj,"_tracingKernel");l(W2,"registerTracingDispatch")});function Oj(n){return n&&n.shape&&n.shape.length>=2&&Aj.has(n.dtype)}function H2(n,e,t=Oj){let r=n.graph.functions().next().value;if(!r)return n;let s=r.entryBlock,o=n.numUserInputs,i=n.capturedParams,a=[];for(let f=0;f<i.length;f++){let d=o+f,h=s.getArgument(d);h&&t(i[f],f,h)&&a.push({j:f,argIndex:d,arg:h,param:i[f]})}if(a.length===0)return n;for(let f of a){let d=e(f.param),h=f.arg.type,m=Me,g=new m("constant",[],[h],{value:d,tensor_type:h}),y=s.firstOp;y?s.insertBefore(g,y):s.pushOp(g),f.arg.replaceAllUsesWith(g.getResult(0))}let u=new Set(a.map(f=>f.argIndex));s.removeArguments(u),r.inputTypes=Object.freeze(r.inputTypes.filter((f,d)=>!u.has(d)));let p=new Set(a.map(f=>f.j)),c=i.filter((f,d)=>!p.has(d));return{...n,capturedParams:c}}var Aj,Y2=M(()=>{"use strict";yr();Aj=new Set(["f16","f32","f64"]);l(Oj,"defaultWeightPredicate");l(H2,"foldWeightParams")});function Kd(n,e,t){let r=t.get(e.id);if(r)return r;let s=n.scalarConstant(0,e.type.dtype).getResult(0);return n.broadcast(s,e.type.shape,[]).getResult(0)}var jo,Hd=M(()=>{"use strict";l(Kd,"gradOrZero");jo=class{static{l(this,"GradAccumulator")}constructor(e){this._builder=e,this._pending=new Map,this._reduced=new Map}accumulate(e,t){if(!t)return;let r=this._pending.get(e);r||(r=[],this._pending.set(e,r)),r.push(t),this._reduced.delete(e)}_treeReduce(e){let t=e;for(;t.length>1;){let r=[];for(let s=0;s<t.length;s+=2)s+1<t.length?r.push(this._builder.add(t[s],t[s+1]).getResult(0)):r.push(t[s]);t=r}return t[0]}get(e){if(this._reduced.has(e))return this._reduced.get(e);let t=this._pending.get(e);if(!t||t.length===0)return null;let r=this._treeReduce(t);return this._reduced.set(e,r),r}has(e){let t=this._pending.get(e);return!!t&&t.length>0}}});function pe(n,e){uk.set(n,e)}function Eu(n){return uk.get(n)||null}function cw(n){let e=uk.get(n);if(e)return e;if(lk.has(n))return null;throw new Error(`autodiff: op '${n}' is on the gradient path but has no VJP rule and is not a registered gradient barrier. Register one with registerVJPRule('${n}', ...) or registerGradientBarrier('${n}').`)}function ck(n,e){J2.set(n,e)}function pw(n){return J2.get(n)||null}function pk(n){lk.add(n)}function fw(n){return lk.has(n)}var uk,lk,J2,Is=M(()=>{"use strict";uk=new Map,lk=new Set;l(pe,"registerVJPRule");l(Eu,"getVJPRule");l(cw,"requireVJPRuleOrBarrier");J2=new Map;l(ck,"registerRegionVJP");l(pw,"getRegionVJP");l(pk,"registerGradientBarrier");l(fw,"isGradientBarrier")});var vi,fk=M(()=>{"use strict";vi=new Set(["scan","if"])});function Yd(n){let e=new Set(n.arguments.map(i=>i.id)),t=l(i=>{for(let a of i.ops()){for(let u of a.results)e.add(u.id);for(let u of a.regions||[])for(let p of u.blocks){for(let c of p.arguments)e.add(c.id);t(p)}}},"addLocals");t(n);let r=new Set,s=[],o=l(i=>{for(let a of i.ops()){for(let u of a.operands)e.has(u.id)||r.has(u.id)||u.definingOp&&u.definingOp.opName==="constant"||(r.add(u.id),s.push(u));for(let u of a.regions||[])for(let p of u.blocks)o(p)}},"scan");return o(n),s}function dw(n,e){let t=n.scalarConstant(0,e.type.dtype).getResult(0);return n.broadcast(t,e.type.shape,[]).getResult(0)}function Z2(n,e,t){let r=e.type.shape,s=r.map((a,u)=>u===0?t:0),o=r.map((a,u)=>u===0?t+1:a),i=n.slice(e,s,o).getResult(0);return n.reshape(i,r.slice(1)).getResult(0)}function Rj(n,e,t){let r=t.slice(1),s=e.map(o=>n.reshape(o,[1,...r]).getResult(0));return s.length===1?n.reshape(s[0],t).getResult(0):n.concat(s,0).getResult(0)}function hw(n,e,t,r,s,o,i=new Map,a=[]){let u=new Map,p=new Map;for(let _=0;_<e.arguments.length;_++)u.set(e.arguments[_].id,t[_]),p.set(e.arguments[_],t[_]);for(let[_,b]of r)u.set(_,b);for(let _ of a)r.has(_.id)&&p.set(_,r.get(_.id));let c=[],f=null;for(let _ of e.ops())_.opName==="yield"?f=_:c.push(_);let d=l(_=>{if(u.has(_.id))return u.get(_.id);let b=_.definingOp;if(b&&b.opName==="constant"){let w=i.get(_.id);return w===void 0&&(w=n._buildOp("constant",[],[_.type],new Map(b.attributes),null).getResult(0),i.set(_.id,w)),u.set(_.id,w),p.set(_,w),w}return _},"matOperand");for(let _ of c){if(vi.has(_.opName)){for(let S of _.operands)d(S),p.set(S,u.get(S.id)??S);let x=_.clone(p);n.block.pushOp(x);for(let S=0;S<_.numResults;S++)u.set(_.getResult(S).id,x.getResult(S)),p.set(_.getResult(S),x.getResult(S));continue}let b=_.operands.map(d),w=n._buildOp(_.opName,b,_.results.map(x=>x.type),new Map(_.attributes),null);for(let x=0;x<_.numResults;x++)u.set(_.getResult(x).id,w.getResult(x)),p.set(_.getResult(x),w.getResult(x))}let h=f.operands.map(_=>u.get(_.id));if(o)return{forwardYields:h};let m=new jo(n);for(let _=0;_<f.operands.length;_++)s[_]&&m.accumulate(f.operands[_].id,s[_]);for(let _=c.length-1;_>=0;_--){let b=c[_];if(b.opName==="constant")continue;let w=b.results.map(v=>m.get(v.id));if(w.every(v=>v===null))continue;if(vi.has(b.opName)){let v=pw(b.opName);v&&v(b,{accumulator:m,builder:n,materialize:d,needsGrad:Bj,scanCheckpoint:null});continue}let x=cw(b.opName);if(!x)continue;let S={builder:n,op:b,operands:b.operands.map(d),results:b.results.map(v=>u.get(v.id)),gradOutputs:w,attrs:b.attributes,full:l((v,A)=>n.broadcast(n.scalarConstant(v,A.dtype).getResult(0),A.shape,[]).getResult(0),"full")},T=x(S);if(T)for(let v=0;v<b.numOperands;v++)T[v]&&m.accumulate(b.getOperand(v).id,dk(n,T[v],b.getOperand(v).type.shape))}let g=e.arguments.map(_=>m.get(_.id)),y=new Map;for(let _ of r.keys())y.set(_,m.get(_));return{forwardYields:h,gradArgs:g,gradFree:y}}function Mj(n,e,t,r,s){let o=n.regions[0].blocks[0],i=n.regions[1].blocks[0],a=r(n.getOperand(0)),u=[];for(let y=0;y<n.numResults;y++)u.push(e.get(n.getResult(y).id));let p=Yd(o),c=Yd(i),f=new Map(p.map(y=>[y.id,r(y)])),d=new Map(c.map(y=>[y.id,r(y)])),{gradFree:h}=hw(t,o,[],f,u,!1,new Map,p),{gradFree:m}=hw(t,i,[],d,u,!1,new Map,c),g=new Map;for(let y of p)g.set(y.id,y);for(let y of c)g.set(y.id,y);for(let[y,_]of g){if(!s.has(y))continue;let b=h.get(y),w=m.get(y);if(!b&&!w)continue;let x=dw(t,_),S=t.broadcast(a,_.type.shape,[]).getResult(0);e.accumulate(y,t.select(S,b??x,w??x).getResult(0))}}function Ij(n,e){if(!n||e<=1)return null;if(n==="sqrt"||n===!0)return Math.max(1,Math.ceil(Math.sqrt(e)));if(typeof n=="number"&&n>=1){let t=Math.floor(n);return t>=e?null:t}return null}function Ej(n,e,t,r,s,o=null){let i=n.regions[0].blocks[0],a=n.getAttr("num_carry"),u=n.getAttr("num_xs"),p=n.numResults-a,c=[];for(let k=0;k<u;k++)c.push(n.getOperand(k));let f=[];for(let k=0;k<a;k++)f.push(n.getOperand(u+k));let d=c[0].type.shape[0],h=Yd(i),m=c.map(r),g=f.map(r),y=new Map(h.map(k=>[k.id,r(k)])),_=new Map,b=l(k=>m.map(O=>Z2(t,O,k)),"sliceX"),w=l((k,O)=>hw(t,i,[...k,...O],y,null,!0,_,h).forwardYields.slice(0,a),"stepForward"),x=[];for(let k=0;k<p;k++)x.push(e.get(n.getResult(a+k).id));let S=[];for(let k=0;k<a;k++){let O=e.get(n.getResult(k).id);S.push(O??dw(t,g[k]))}let T=new Map,v=m.map(()=>new Array(d)),A=l((k,O,R)=>{let B=[...O,...R],E=x.map(G=>G===null?null:Z2(t,G,k)),F=[...S,...E],{gradArgs:C,gradFree:H}=hw(t,i,B,y,F,!1,_,h);for(let G=0;G<u;G++)v[G][k]=C[G]??dw(t,O[G]);S=[];for(let G=0;G<a;G++)S.push(C[u+G]??dw(t,R[G]));for(let[G,q]of H){if(!q)continue;let D=T.get(G);T.set(G,D?t.add(D,q).getResult(0):q)}},"backwardStep"),N=Ij(o,d);if(N){let k=Math.ceil(d/N),O=new Array(k),R=g;O[0]=R;for(let B=0;B<d;B++){R=w(b(B),R);let E=(B+1)/N;Number.isInteger(E)&&E<k&&(O[E]=R)}for(let B=k-1;B>=0;B--){let E=B*N,F=Math.min(E+N,d),C=[],H=[O[B]],G=O[B];for(let q=E;q<F;q++){let D=b(q);C.push(D),G=w(D,G),H.push(G)}for(let q=F-1;q>=E;q--)A(q,C[q-E],H[q-E])}}else{let k=g,O=[k],R=[];for(let B=0;B<d;B++){let E=b(B);R.push(E),k=w(E,k),O.push(k)}for(let B=d-1;B>=0;B--)A(B,R[B],O[B])}for(let k=0;k<u;k++)s.has(c[k].id)&&e.accumulate(c[k].id,Rj(t,v[k],m[k].type.shape));for(let k=0;k<a;k++)s.has(f[k].id)&&e.accumulate(f[k].id,S[k]);for(let[k,O]of T)O&&s.has(k)&&e.accumulate(k,O)}var Bj,Q2=M(()=>{"use strict";Hd();Is();Jd();fk();Bj={has:l(()=>!0,"has")};ck("scan",(n,e)=>Ej(n,e.accumulator,e.builder,e.materialize,e.needsGrad,e.scanCheckpoint));ck("if",(n,e)=>Mj(n,e.accumulator,e.builder,e.materialize,e.needsGrad));l(Yd,"regionFreeVars");l(dw,"zeroLike");l(Z2,"sliceStep");l(Rj,"stackSteps");l(hw,"diffBodyStep");l(Mj,"buildCondBackward");l(Ij,"resolveSegmentLength");l(Ej,"buildScanBackward")});function Zd(n,{accumulator:e,builder:t,needsGrad:r,resolveValue:s,handleRegionOp:o=null}){for(let i=n.length-1;i>=0;i--){let a=n[i];if(a.opName==="return"||a.opName==="constant"||!a.results.some(g=>r.has(g.id)))continue;let p=[];for(let g=0;g<a.numResults;g++)p.push(e.get(a.getResult(g).id));if(p.every(g=>g===null)||o&&o(a))continue;let c=cw(a.opName);if(!c)continue;let f=new Array(a.numOperands);for(let g=0;g<a.numOperands;g++)f[g]=s(a.getOperand(g));let d=new Array(a.numResults);for(let g=0;g<a.numResults;g++)d[g]=s(a.getResult(g));let h=l((g,y)=>t.broadcast(t.scalarConstant(g,y.dtype).getResult(0),y.shape,[]).getResult(0),"full"),m=c({builder:t,op:a,operands:f,results:d,gradOutputs:p,attrs:a.attributes,full:h});if(m)for(let g=0;g<a.numOperands;g++){if(g>=m.length||!m[g])continue;let y=a.getOperand(g);r.has(y.id)&&e.accumulate(y.id,dk(t,m[g],y.type.shape))}}}function X2(n){let e=[];for(let t of n.regions)t.blocks[0]&&e.push(...Yd(t.blocks[0]));return e}function dk(n,e,t){let r=e.type.shape;if(r.length===t.length&&r.every((u,p)=>u===t[p]))return e;let s=r.length-t.length,o=[];for(let u=0;u<s;u++)o.push(u);for(let u=0;u<t.length;u++)t[u]===1&&r[s+u]!==1&&o.push(s+u);let i=e;if(o.length>0){let u=n.scalarConstant(0,e.type.dtype).getResult(0);i=n.reduce(e,u,o,"sum").getResult(0)}let a=i.type.shape;return a.length===t.length&&a.every((u,p)=>u===t[p])||(i=n.reshape(i,t).getResult(0)),i}var Lj,Qd,Jd=M(()=>{"use strict";qs();_s();ro();Hd();Is();Q2();fk();Lj=new Set(["neg","abs","sign","floor","ceil"]);l(Zd,"backpropOps");l(X2,"regionControlFlowFreeVars");l(dk,"reduceGradToOperandShape");Qd=class{static{l(this,"BackwardGraphBuilder")}constructor(e={}){this._rematPolicy=e.rematPolicy||null,this._checkpointPolicy=e.checkpointPolicy||null,this._scanCheckpoint=e.scanCheckpoint||null}build(e){if(this._checkpointPolicy)return this._buildCheckpointed(e);let r=ft.compute(e).topologicalOrder,s=e.getReturnOp();if(!s)throw new Error("Forward function has no return op");let o=s.operands,i=e.args,a=this._computeGradReachability(e,r),{savedValues:u,savedValueIndices:p}=this._identifySavedValues(r,a,i),c=o.map(T=>T.type),f=u.map(T=>T.type),d=[...c,...f],h=[];for(let T=0;T<i.length;T++)a.has(i[T].id)&&h.push(i[T].type);let m=new yn(`backward_${e.name}`,d,h),g=new $n(m),y=m.args,_=y.slice(0,c.length),b=y.slice(c.length),w=new Map;for(let T=0;T<u.length;T++)w.set(u[T].id,b[T]);for(let T=0;T<i.length;T++)p.has(i[T].id)&&w.set(i[T].id,b[p.get(i[T].id)]);let x=new jo(g);for(let T=0;T<o.length;T++){let v=o[T];x.accumulate(v.id,_[T])}Zd(r,{accumulator:x,builder:g,needsGrad:a,resolveValue:l(T=>this._materialize(T,w,g),"resolveValue"),handleRegionOp:l(T=>{let v=pw(T.opName);return v?(v(T,{accumulator:x,builder:g,materialize:l(A=>this._materialize(A,w,g),"materialize"),needsGrad:a,scanCheckpoint:this._scanCheckpoint}),!0):!1},"handleRegionOp")});let S=[];for(let T=0;T<i.length;T++)a.has(i[T].id)&&S.push(Kd(g,i[T],x));return g.returnOp(S),{backwardFunc:m,savedValues:u,gradInputIndices:this._getGradInputIndices(i,a)}}_materialize(e,t,r){if(t.has(e.id))return t.get(e.id);if(!e.definingOp)return e;let s=new Set([e.id]),o=[{val:e,i:0}];for(;o.length>0;){let i=o[o.length-1],a=i.val,u=a.definingOp;if(t.has(a.id)||!u){s.delete(a.id),o.pop();continue}if(i.i<u.numOperands){let d=u.getOperand(i.i);i.i++,d.definingOp&&!t.has(d.id)&&!s.has(d.id)&&(s.add(d.id),o.push({val:d,i:0}));continue}let p=new Array(u.numOperands);for(let d=0;d<u.numOperands;d++){let h=u.getOperand(d);p[d]=t.has(h.id)?t.get(h.id):h}let c=u.results.map(d=>d.type),f=r._buildOp(u.opName,p,c,new Map(u.attributes),null);for(let d=0;d<u.numResults;d++)t.set(u.getResult(d).id,f.getResult(d));s.delete(a.id),o.pop()}return t.has(e.id)?t.get(e.id):e}_computeGradReachability(e,t){let r=new Set,s=e.getReturnOp();for(let o of s.operands)r.add(o.id);for(let o=t.length-1;o>=0;o--){let i=t[o];if(!(i.opName==="return"||!i.results.some(u=>r.has(u.id)))){if(vi.has(i.opName)){for(let u=0;u<i.numOperands;u++)r.add(i.getOperand(u).id);for(let u of X2(i))r.add(u.id);continue}if(Eu(i.opName)&&!fw(i.opName))for(let u=0;u<i.numOperands;u++)r.add(i.getOperand(u).id)}}return r}_identifySavedValues(e,t,r){let s=[],o=new Map,i=new Set(r.map(f=>f.id));for(let f of e)if(!(f.opName==="return"||f.opName==="constant"||!Eu(f.opName)||!f.results.some(m=>t.has(m.id)))){for(let m=0;m<f.numOperands;m++){let g=f.getOperand(m);i.has(g.id)&&!o.has(g.id)&&(o.set(g.id,s.length),s.push(g))}for(let m=0;m<f.numResults;m++){let g=f.getResult(m);t.has(g.id)&&!o.has(g.id)&&this._shouldSaveResult(f)&&(o.set(g.id,s.length),s.push(g))}}let a=new Set(o.keys()),u=new Map(r.map(f=>[f.id,f])),p=new Set,c=l(f=>{if(a.has(f.id)||p.has(f.id))return;p.add(f.id);let d=f.definingOp;if(!d){i.has(f.id)&&!o.has(f.id)&&(o.set(f.id,s.length),s.push(u.get(f.id)));return}for(let h=0;h<d.numOperands;h++)c(d.getOperand(h))},"collect");for(let f of e)if(!(f.opName==="return"||f.opName==="constant")&&f.results.some(d=>t.has(d.id))){if(vi.has(f.opName)){for(let d of f.operands)c(d);for(let d of X2(f))c(d);continue}if(Eu(f.opName)){for(let d=0;d<f.numOperands;d++)c(f.getOperand(d));for(let d=0;d<f.numResults;d++)c(f.getResult(d))}}return{savedValues:s,savedValueIndices:o}}_shouldSaveResult(e){return this._rematPolicy?!this._rematPolicy.shouldRematerialize(e):!Lj.has(e.opName)}_getGradInputIndices(e,t){let r=[];for(let s=0;s<e.length;s++)t.has(e[s].id)&&r.push(s);return r}_buildCheckpointed(e){let r=ft.compute(e).topologicalOrder,s=e.getReturnOp();if(!s)throw new Error("Forward function has no return op");let o=s.operands,i=e.args,a=this._computeGradReachability(e,r),u=this._checkpointPolicy.segment(r,e);for(let O of u)for(let R of O.ops)if(vi.has(R.opName))throw new Error(`Checkpointed backward does not support region control-flow op '${R.opName}'; build the backward without a checkpointPolicy, which differentiates scan/if via buildScanBackward/buildCondBackward.`);let p=new Set,c=[],f=new Map,d=new Map;for(let O of r)for(let R=0;R<O.numResults;R++){let B=O.getResult(R);d.set(B.id,B)}let h=new Set(i.map(O=>O.id));for(let O of i)a.has(O.id)&&!p.has(O.id)&&(p.add(O.id),f.set(O.id,c.length),c.push(O));for(let O of u){for(let R of O.boundaryInputs)if(!h.has(R)&&!p.has(R)){p.add(R);let B=d.get(R)||null;B&&(f.set(R,c.length),c.push(B))}for(let R of O.boundaryOutputs)if(!p.has(R)){p.add(R);let B=d.get(R)||null;B&&(f.set(R,c.length),c.push(B))}}for(let O of o)p.has(O.id)||(p.add(O.id),f.set(O.id,c.length),c.push(O));let m=o.map(O=>O.type),g=c.map(O=>O.type),y=[...m,...g],_=[];for(let O=0;O<i.length;O++)a.has(i[O].id)&&_.push(i[O].type);let b=new yn(`backward_${e.name}`,y,_),w=new $n(b),x=b.args,S=x.slice(0,m.length),T=x.slice(m.length),v=new Map;for(let O=0;O<c.length;O++)v.set(c[O].id,T[O]);let A=new jo(w);for(let O=0;O<o.length;O++)A.accumulate(o[O].id,S[O]);let N=new Map;for(let O of r)if(O.opName==="constant"){let R=O.getResult(0).type,B=w._buildOp("constant",[],[R],new Map(O.attributes),null);N.set(O.getResult(0).id,B.getResult(0))}for(let O=u.length-1;O>=0;O--){let R=u[O],B=new Map;for(let E of R.ops){let F=new Array(E.numOperands);for(let G=0;G<E.numOperands;G++){let q=E.getOperand(G),D=B.get(q.id)||v.get(q.id)||N.get(q.id);F[G]=D||q}let C=E.results.map(G=>G.type),H=w._buildOp(E.opName,F,C,new Map(E.attributes),null);for(let G=0;G<E.numResults;G++)B.set(E.getResult(G).id,H.getResult(G))}Zd(R.ops,{accumulator:A,builder:w,needsGrad:a,resolveValue:l(E=>B.get(E.id)||v.get(E.id)||N.get(E.id)||E,"resolveValue")})}let k=[];for(let O=0;O<i.length;O++)a.has(i[O].id)&&k.push(Kd(w,i[O],A));return w.returnOp(k),{backwardFunc:b,savedValues:c,gradInputIndices:this._getGradInputIndices(i,a)}}}});var Lu,mw=M(()=>{"use strict";Lu=class{static{l(this,"RematPolicy")}constructor(e={}){this._maxRematDepth=e.maxRematDepth||1,this._sizeThreshold=e.sizeThreshold||1024*1024,this._alwaysRemat=new Set(e.alwaysRemat||["neg","abs","sign","floor","ceil","exp","log","sqrt","rsqrt","sin","cos","tanh"]),this._neverRemat=new Set(e.neverRemat||["matmul","dot","conv","reduce","custom_call","pool2d"])}shouldRematerialize(e){if(this._alwaysRemat.has(e.opName))return!0;if(this._neverRemat.has(e.opName))return!1;let t=e.numResults>0?e.getResult(0).type:null;return!t||!t.shape||t.numel()>this._sizeThreshold?!1:this._isElementwise(e)}_isElementwise(e){if(e.numOperands===0||e.numResults===0)return!1;let t=e.getResult(0).type.shape;for(let r=0;r<e.numOperands;r++){let s=e.getOperand(r).type;if(!s||!s.shape||s.shape.length!==t.length)return!1;for(let o=0;o<t.length;o++)if(s.shape[o]!==t[o])return!1}return!0}}});var Xd,hk=M(()=>{"use strict";qs();_s();ro();Hd();Is();mw();Jd();Xd=class{static{l(this,"JointGraphBuilder")}constructor(e={}){this._rematPolicy=e.rematPolicy||new Lu(e.remat||{}),this._checkpointPolicy=e.checkpointPolicy||null}build(e){if(this._checkpointPolicy)return this._buildCheckpointed(e);let t=this._buildScaffold(e);return Zd(t.topoOrder,{accumulator:t.accumulator,builder:t.builder,needsGrad:t.needsGrad,resolveValue:l(r=>t.valueMap.get(r.id)||r,"resolveValue")}),this._finish(t)}_buildCheckpointed(e){let t=this._buildScaffold(e),r=this._checkpointPolicy.segment(t.topoOrder,e);for(let s=r.length-1;s>=0;s--){let o=r[s],i=new Map;for(let a of o.ops){let u=new Array(a.numOperands);for(let f=0;f<a.numOperands;f++){let d=a.getOperand(f);u[f]=i.get(d.id)||t.valueMap.get(d.id)||d}let p=a.results.map(f=>f.type),c=t.builder._buildOp(a.opName,u,p,new Map(a.attributes),null);for(let f=0;f<a.numResults;f++)i.set(a.getResult(f).id,c.getResult(f))}Zd(o.ops,{accumulator:t.accumulator,builder:t.builder,needsGrad:t.needsGrad,resolveValue:l(a=>i.get(a.id)||t.valueMap.get(a.id)||a,"resolveValue")})}return this._finish(t)}_buildScaffold(e){let r=ft.compute(e).topologicalOrder;this._assertNoRegionControlFlow(r);let s=e.getReturnOp();if(!s)throw new Error("Forward function has no return op");let o=s.operands,i=e.args,a=o.map(w=>w.type),u=[...e.inputTypes,...a],p=[...e.outputTypes,...e.inputTypes],c=new yn(`joint_${e.name}`,u,p),f=new $n(c),d=c.args,h=d.slice(0,e.inputTypes.length),m=d.slice(e.inputTypes.length),g=new Map;for(let w=0;w<i.length;w++)g.set(i[w].id,h[w]);for(let w of r){if(w.opName==="return")continue;let x=new Array(w.numOperands);for(let v=0;v<w.numOperands;v++){let A=w.getOperand(v);x[v]=g.get(A.id)||A}let S=w.results.map(v=>v.type),T=f._buildOp(w.opName,x,S,new Map(w.attributes),null);for(let v=0;v<w.numResults;v++)g.set(w.getResult(v).id,T.getResult(v))}let y=o.map(w=>g.get(w.id)),_=this._computeGradReachability(e,r),b=new jo(f);for(let w=0;w<o.length;w++)b.accumulate(o[w].id,m[w]);return{topoOrder:r,forwardInputs:i,forwardOutputs:o,fwdOutputValues:y,valueMap:g,builder:f,needsGrad:_,accumulator:b,jointFunc:c}}_finish(e){let t=[];for(let r=0;r<e.forwardInputs.length;r++)t.push(Kd(e.builder,e.forwardInputs[r],e.accumulator));return e.builder.returnOp([...e.fwdOutputValues,...t]),{jointFunc:e.jointFunc,numForwardOutputs:e.forwardOutputs.length,numGradInputs:t.length}}_computeGradReachability(e,t){let r=new Set,s=e.getReturnOp();for(let o of s.operands)r.add(o.id);for(let o=t.length-1;o>=0;o--){let i=t[o];if(!(i.opName==="return"||!i.results.some(u=>r.has(u.id)))&&Eu(i.opName)&&!fw(i.opName))for(let u=0;u<i.numOperands;u++)r.add(i.getOperand(u).id)}return r}_assertNoRegionControlFlow(e){for(let t of e)if(vi.has(t.opName))throw new Error(`JointGraphBuilder does not support region control-flow op '${t.opName}'; use BackwardGraphBuilder (separate mode) without a checkpointPolicy, which differentiates scan/if.`)}}});var eC=M(()=>{"use strict"});function tC(n,e){let t=n.gradOutputs[0],[r,s]=n.operands,o=n.full(0,r.type),i=n.builder.compare(r,s,e).getResult(0),a=n.builder.select(i,t,o).getResult(0),u=n.builder.select(i,o,t).getResult(0);return[a,u]}function nC(n){let e=n.gradOutputs[0],[t]=n.operands,r=n.full(0,e.type),s=n.builder.where(t,e,r).getResult(0),o=n.builder.where(t,r,e).getResult(0);return[null,s,o]}var rC=M(()=>{"use strict";Is();pe("add",n=>{let e=n.gradOutputs[0];return[e,e]});pe("sub",n=>{let e=n.gradOutputs[0],t=n.builder.neg(e).getResult(0);return[e,t]});l(tC,"_minMaxVJP");pe("maximum",n=>tC(n,"ge"));pe("minimum",n=>tC(n,"le"));pe("clamp",n=>{let e=n.gradOutputs[0],[t,r,s]=n.operands,o=n.full(0,r.type),i=n.builder.compare(r,t,"ge").getResult(0),a=n.builder.where(i,e,o).getResult(0),u=n.builder.compare(r,s,"le").getResult(0);return[null,n.builder.where(u,a,o).getResult(0),null]});l(nC,"_whereVJP");pe("where",nC);pe("select",nC);pe("mul",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder.mul(e,r).getResult(0),o=n.builder.mul(e,t).getResult(0);return[s,o]});pe("div",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder.div(e,r).getResult(0),o=n.builder.mul(r,r).getResult(0),i=n.builder.neg(t).getResult(0),a=n.builder.mul(e,i).getResult(0),u=n.builder.div(a,o).getResult(0);return[s,u]});pe("neg",n=>{let e=n.gradOutputs[0];return[n.builder.neg(e).getResult(0)]});pe("pow",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.full(1,t.type),o=n.builder.sub(r,s).getResult(0),i=n.builder.pow(t,o).getResult(0),a=n.builder.mul(r,i).getResult(0),u=n.builder.mul(e,a).getResult(0),p=n.builder.pow(t,r).getResult(0),c=n.builder.log(t).getResult(0),f=n.builder.mul(p,c).getResult(0),d=n.builder.mul(e,f).getResult(0);return[u,d]})});function sC(n,e){let t=n.full(2/Math.sqrt(Math.PI),e.type),r=n.builder.mul(e,e).getResult(0),s=n.builder.neg(r).getResult(0),o=n.builder.exp(s).getResult(0);return n.builder.mul(t,o).getResult(0)}function oC(n,e){let t=n.builder,r=n.full(1,e.type),s=t.add(e,n.full(ii,e.type)).getResult(0),o=t.div(r,s).getResult(0),i=t.log(s).getResult(0),a=t.sub(i,t.mul(n.full(.5,e.type),o).getResult(0)).getResult(0),u=t.mul(o,o).getResult(0),p=u;for(let c of Ga)a=t.add(a,t.mul(n.full(c,e.type),p).getResult(0)).getResult(0),p=t.mul(p,u).getResult(0);for(let c=0;c<ii;c++){let f=t.add(e,n.full(c,e.type)).getResult(0);a=t.sub(a,t.div(r,f).getResult(0)).getResult(0)}return a}var iC=M(()=>{"use strict";Is();Yi();pe("exp",n=>{let e=n.gradOutputs[0],t=n.results[0];return[n.builder.mul(e,t).getResult(0)]});pe("log",n=>{let e=n.gradOutputs[0],[t]=n.operands;return[n.builder.div(e,t).getResult(0)]});pe("sqrt",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.full(2,t.type),s=n.builder.mul(r,t).getResult(0);return[n.builder.div(e,s).getResult(0)]});pe("tanh",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.mul(t,t).getResult(0),s=n.builder.sub(n.full(1,t.type),r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("sigmoid",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.sub(n.full(1,t.type),t).getResult(0),s=n.builder.mul(t,r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("relu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(0,t.type),s=n.builder.compare(t,r,"gt").getResult(0);return[n.builder.select(s,e,r).getResult(0)]});pe("gelu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(1.702,t.type),s=n.full(1,t.type),o=n.builder.mul(r,t).getResult(0),i=n.builder.sigmoid(o).getResult(0),a=n.builder.sub(s,i).getResult(0),u=n.builder.mul(o,a).getResult(0),p=n.builder.add(s,u).getResult(0),c=n.builder.mul(i,p).getResult(0);return[n.builder.mul(e,c).getResult(0)]});pe("silu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.sigmoid(t).getResult(0),s=n.builder.sub(n.full(1,t.type),r).getResult(0),o=n.builder.mul(t,s).getResult(0),i=n.builder.add(r,n.builder.mul(r,o).getResult(0)).getResult(0);return[n.builder.mul(e,i).getResult(0)]});pe("sin",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.cos(t).getResult(0);return[n.builder.mul(e,r).getResult(0)]});pe("cos",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.sin(t).getResult(0),s=n.builder.neg(r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("abs",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.sign(t).getResult(0);return[n.builder.mul(e,r).getResult(0)]});l(sC,"_erfDerivIR");l(oC,"_digammaIR");pe("erf",n=>{let e=n.gradOutputs[0],[t]=n.operands;return[n.builder.mul(e,sC(n,t)).getResult(0)]});pe("erfc",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder.neg(sC(n,t)).getResult(0);return[n.builder.mul(e,r).getResult(0)]});pe("lgamma",n=>{let e=n.gradOutputs[0],[t]=n.operands;return[n.builder.mul(e,oC(n,t)).getResult(0)]});pe("gamma",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.results[0],s=n.builder.mul(r,oC(n,t)).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("log2",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(Math.LN2,t.type),s=n.builder.mul(t,r).getResult(0);return[n.builder.div(e,s).getResult(0)]});pe("log10",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(Math.LN10,t.type),s=n.builder.mul(t,r).getResult(0);return[n.builder.div(e,s).getResult(0)]});pe("exp2",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.full(Math.LN2,t.type),s=n.builder.mul(t,r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("square",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(2,t.type),s=n.builder.mul(r,t).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("reciprocal",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.mul(t,t).getResult(0),s=n.builder.neg(r).getResult(0);return[n.builder.mul(e,s).getResult(0)]});pe("rsqrt",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.builder.mul(n.builder.mul(t,t).getResult(0),t).getResult(0),s=n.full(-.5,t.type),o=n.builder.mul(s,r).getResult(0);return[n.builder.mul(e,o).getResult(0)]})});var aC=M(()=>{"use strict";Is();pe("dot",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.op.getAttr("lhs_contracting"),o=n.op.getAttr("rhs_contracting"),i=n.op.getAttr("lhs_batch")||[],a=n.op.getAttr("rhs_batch")||[],u=t.type.rank,p=r.type.rank,c=e.type.rank,f=[];for(let _=0;_<u;_++)!s.includes(_)&&!i.includes(_)&&f.push(_);let d=[];for(let _=0;_<p;_++)!o.includes(_)&&!a.includes(_)&&d.push(_);let h=[];for(let _=i.length;_<i.length+d.length;_++)h.push(_+f.length);let m=[];for(let _=i.length;_<i.length+f.length;_++)m.push(_);let g=n.builder.dot(e,r,h,d,Array.from({length:i.length},(_,b)=>b),a).getResult(0),y=n.builder.dot(t,e,f,m,i,Array.from({length:i.length},(_,b)=>b)).getResult(0);return[g,y]});pe("matmul",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=l(p=>{let c=Array.from({length:p},(f,d)=>d);return c[p-2]=p-1,c[p-1]=p-2,c},"swapLastTwo"),o=n.builder.transpose(r,s(r.type.rank)).getResult(0),i=n.builder.matmul(e,o).getResult(0),a=n.builder.transpose(t,s(t.type.rank)).getResult(0),u=n.builder.matmul(a,e).getResult(0);return[i,u]});pe("conv",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder,o=n.op.getAttr("strides"),i=n.op.getAttr("padding"),a=n.op.getAttr("dilation")||o.map(()=>1),u=n.op.getAttr("groups")||1,p=n.op.getAttr("input_layout"),c=n.op.getAttr("kernel_layout");if(!(o.length===2&&o.every(A=>A===1)&&a.every(A=>A===1)&&u===1&&p==="NCHW"&&c==="OIHW"))throw new Error("conv VJP supports only 2D stride-1 dilation-1 groups-1 NCHW/OIHW conv");let d=r.type.shape,h=d[2],m=d[3],[g,y]=i,_=s.reverse(s.transpose(r,[1,0,2,3]).getResult(0),[2,3]).getResult(0),b=[[h-1-g[0],h-1-g[1]],[m-1-y[0],m-1-y[1]]],w=s.conv(e,_,[1,1],b).getResult(0),x=s.transpose(t,[1,0,2,3]).getResult(0),S=s.transpose(e,[1,0,2,3]).getResult(0),T=s.conv(x,S,[1,1],[g,y]).getResult(0),v=s.transpose(T,[1,0,2,3]).getResult(0);return[w,v]})});var uC=M(()=>{"use strict";Is();pe("reduce",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("reduce_type"),s=n.op.getAttr("dimensions"),o=t.type.shape,i=t.type.dtype,a=new Set(s),u=o.map((f,d)=>a.has(d)?1:f),p=o.map((f,d)=>d),c=n.builder.reshape(e,u).getResult(0);if(r==="sum")return[n.builder.broadcast(c,o,p).getResult(0),null];if(r==="mean"){let f=1;for(let y=0;y<o.length;y++)a.has(y)&&(f*=o[y]);let d=n.builder.broadcast(c,o,p).getResult(0),h=n.builder.scalarConstant(f,i).getResult(0),m=n.builder.broadcast(h,o,[]).getResult(0);return[n.builder.div(d,m).getResult(0),null]}if(r==="max"||r==="min"){let f=n.builder.reshape(n.results[0],u).getResult(0),d=n.builder.broadcast(f,o,p).getResult(0),h=n.builder.compare(t,d,"eq").getResult(0),m=n.builder.convert(h,i).getResult(0),g=n.builder.broadcast(c,o,p).getResult(0);return[n.builder.mul(g,m).getResult(0),null]}throw r==="prod"?new Error("reduce VJP for reduce_type='prod' is not implemented; provide a gradient rule or avoid differentiating reduce_prod"):new Error(`reduce VJP: unsupported reduce_type '${r}' on the gradient path (would silently drop the gradient)`)})});var lC=M(()=>{"use strict";Is();pe("reshape",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=t.type.shape;return[n.builder.reshape(e,r).getResult(0)]});pe("reverse",n=>[n.builder.reverse(n.gradOutputs[0],n.op.getAttr("dimensions")).getResult(0)]);pe("transpose",n=>{let e=n.gradOutputs[0],t=n.op.getAttr("permutation"),r=new Array(t.length);for(let s=0;s<t.length;s++)r[t[s]]=s;return[n.builder.transpose(e,r).getResult(0)]});pe("broadcast_in_dim",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("broadcast_dimensions"),s=t.type.shape,o=n.results[0].type.shape,i=t.type.dtype,a=[];for(let f=0;f<o.length;f++)if(!r.includes(f))a.push(f);else{let d=r.indexOf(f);s[d]===1&&o[f]!==1&&a.push(f)}if(a.length===0)return[e];let u=n.builder.scalarConstant(0,i).getResult(0),p=n.builder.reduce(e,u,a,"sum").getResult(0),c=p.type.shape;return c.length!==s.length||!c.every((f,d)=>f===s[d])?[n.builder.reshape(p,s).getResult(0)]:[p]});pe("slice",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("starts"),s=t.type.shape,o=e.type.shape,i=n.op.getAttr("strides")||s.map(()=>1),a=t.type.dtype,u=[...r],p=new Array(s.length),c=new Array(s.length);for(let d=0;d<s.length;d++)c[d]=i[d]-1,p[d]=s[d]-r[d]-(o[d]-1)*i[d]-1;let f=n.builder.scalarConstant(0,a).getResult(0);return[n.builder.pad(e,f,u,p,c).getResult(0)]});pe("concat",n=>{let e=n.gradOutputs[0],t=n.op.getAttr("dimension"),r=n.operands,s=[],o=0;for(let i=0;i<r.length;i++){let a=r[i].type.shape,u=new Array(a.length).fill(0),p=[...e.type.shape];u[t]=o,p[t]=o+a[t],s.push(n.builder.slice(e,u,p).getResult(0)),o+=a[t]}return s});pe("gather",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.builder.scalarConstant(0,t.type.dtype).getResult(0),o=n.builder.broadcast(s,t.type.shape,[]).getResult(0);return[n.builder.scatterAdd(o,r,e,{updateWindowDims:n.op.getAttr("offset_dims"),insertedWindowDims:n.op.getAttr("collapsed_slice_dims"),scatterDimsToOperandDims:n.op.getAttr("start_index_map"),indexVectorDim:n.op.getAttr("index_vector_dim")}).getResult(0),null]});pe("scatter",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.op.getAttr("inserted_window_dims"),o=t.type.shape.map((a,u)=>s.includes(u)?1:a),i=n.builder.gather(e,r,{offsetDims:n.op.getAttr("update_window_dims"),collapsedSliceDims:s,startIndexMap:n.op.getAttr("scatter_dims_to_operand_dims"),indexVectorDim:n.op.getAttr("index_vector_dim"),sliceSizes:o}).getResult(0);return[e,null,i]});pe("pad",n=>{let e=n.gradOutputs[0],t=n.op.getAttr("low"),r=n.op.getAttr("high"),[s]=n.operands,o=s.type.shape,i=[...t],a=new Array(o.length);for(let u=0;u<o.length;u++)a[u]=t[u]+o[u];return[n.builder.slice(e,i,a).getResult(0),null]})});var cC=M(()=>{"use strict";Is();_s();pe("softmax",n=>{let e=n.gradOutputs[0],t=n.results[0],r=n.op.getAttr("axis"),s=t.type.dtype,o=t.type.shape,i=n.builder.mul(e,t).getResult(0),a=n.builder.scalarConstant(0,s).getResult(0),u=n.builder.reduce(i,a,[r],"sum").getResult(0),p=Xo(o.length,r),c=n.builder.broadcast(u,o,p).getResult(0),f=n.builder.sub(e,c).getResult(0);return[n.builder.mul(t,f).getResult(0)]});pe("log_softmax",n=>{let e=n.gradOutputs[0],t=n.results[0],[r]=n.operands,s=n.op.getAttr("axis"),o=t.type.dtype,i=t.type.shape,a=n.builder.exp(t).getResult(0),u=n.builder.scalarConstant(0,o).getResult(0),p=n.builder.reduce(e,u,[s],"sum").getResult(0),c=Xo(i.length,s),f=n.builder.broadcast(p,i,c).getResult(0),d=n.builder.mul(a,f).getResult(0);return[n.builder.sub(e,d).getResult(0)]});pe("layer_norm",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=n.op.getAttr("axis"),o=n.op.getAttr("epsilon"),i=t.type.dtype,a=t.type.shape,u=[s<0?a.length+s:s],p=a[u[0]],c=Xo(a.length,u[0]),f=n.builder.scalarConstant(0,i).getResult(0),d=n.builder.reduce(t,f,u,"mean").getResult(0),h=n.builder.broadcast(d,a,c).getResult(0),m=n.builder.sub(t,h).getResult(0),g=n.builder.mul(m,m).getResult(0),y=n.builder.reduce(g,f,u,"mean").getResult(0),_=n.full(o,y.type),b=n.builder.add(y,_).getResult(0),w=n.builder.rsqrt(b).getResult(0),x=n.builder.broadcast(w,a,c).getResult(0),S=[];for(let ue=0;ue<a.length;ue++)u.includes(ue)&&S.push(ue);let T=n.builder.broadcast(r,a,S).getResult(0),v=n.builder.mul(e,T).getResult(0),A=n.full(p,t.type),N=n.builder.mul(A,v).getResult(0),k=n.builder.reduce(v,f,u,"sum").getResult(0),O=n.builder.broadcast(k,a,c).getResult(0),R=n.builder.mul(m,x).getResult(0),B=n.builder.mul(v,R).getResult(0),E=n.builder.reduce(B,f,u,"sum").getResult(0),F=n.builder.broadcast(E,a,c).getResult(0),C=n.builder.mul(R,F).getResult(0),H=n.builder.sub(N,n.builder.add(O,C).getResult(0)).getResult(0),G=n.builder.div(x,A).getResult(0),q=n.builder.mul(G,H).getResult(0),D=n.builder.reduce(n.builder.mul(e,R).getResult(0),f,c,"sum").getResult(0),V=n.builder.reduce(e,f,c,"sum").getResult(0);return[q,D,V]});pe("scaled_dot_product_attention",n=>{let e=n.gradOutputs[0],[t,r,s]=n.operands,o=n.op.getAttr("scale");if(n.op.getAttr("causal"))throw new Error("causal scaled_dot_product_attention VJP not supported");let i=n.builder,a=t.type.dtype,u=t.type.rank,p=[];for(let N=0;N<u;N++)p.push(N);p[u-2]=u-1,p[u-1]=u-2;let c=l(N=>i.transpose(N,p).getResult(0),"lastT"),f=i.matmul(t,c(r)).getResult(0),d=i.mul(f,n.full(o,f.type)).getResult(0),h=i.softmax(d,u-1).getResult(0),m=i.matmul(c(h),e).getResult(0),g=i.matmul(e,c(s)).getResult(0),y=i.scalarConstant(0,a).getResult(0),_=i.mul(g,h).getResult(0),b=i.reduce(_,y,[u-1],"sum").getResult(0),w=[];for(let N=0;N<u-1;N++)w.push(N);let x=i.broadcast(b,h.type.shape,w).getResult(0),S=i.mul(h,i.sub(g,x).getResult(0)).getResult(0),T=i.mul(S,n.full(o,S.type)).getResult(0),v=i.matmul(T,r).getResult(0),A=i.matmul(c(T),t).getResult(0);return[v,A,m]});pe("pool2d",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.builder,s=n.op.getAttr("pool_type"),o=n.op.getAttr("kernel_size"),i=n.op.getAttr("strides"),a=n.op.getAttr("padding"),u=n.op.getAttr("layout")||"NCHW",p=a.every(v=>v[0]===0&&v[1]===0),c=i[0]===o[0]&&i[1]===o[1];if(u!=="NCHW"||!p||!c||s!=="avg"&&s!=="max")throw new Error("pool2d VJP supports only non-overlapping (stride=kernel) avg/max pooling without padding, NCHW");let[f,d,h,m]=e.type.shape,[g,y]=o,_=t.type.shape,b=l(v=>r.reshape(r.broadcast(v,[f,d,h,g,m,y],[0,1,2,4]).getResult(0),_).getResult(0),"upsample"),w=b(e);if(s==="avg"){let v=n.full(g*y,t.type);return[r.div(w,v).getResult(0)]}let x=b(n.results[0]),S=r.compare(t,x,"eq").getResult(0),T=n.full(0,t.type);return[r.select(S,w,T).getResult(0)]});pe("batch_norm",n=>{let e=n.gradOutputs[0],[t,r,,s,o]=n.operands,i=n.op.getAttr("axis"),a=n.op.getAttr("epsilon"),u=t.type.dtype,p=t.type.shape,c=n.builder,f=Xo(p.length,i),d=c.scalarConstant(0,u).getResult(0),h=n.full(a,o.type),m=c.rsqrt(c.add(o,h).getResult(0)).getResult(0),g=c.broadcast(m,p,[i]).getResult(0),y=c.broadcast(s,p,[i]).getResult(0),_=c.broadcast(r,p,[i]).getResult(0),b=c.sub(t,y).getResult(0),w=c.mul(b,g).getResult(0),x=c.mul(e,_).getResult(0),S=c.mul(x,g).getResult(0),T=c.reduce(c.mul(e,w).getResult(0),d,f,"sum").getResult(0),v=c.reduce(e,d,f,"sum").getResult(0),A=c.neg(c.reduce(S,d,f,"sum").getResult(0)).getResult(0),N=c.mul(c.mul(g,g).getResult(0),g).getResult(0),k=n.full(-.5,t.type),O=c.mul(c.mul(c.mul(x,b).getResult(0),N).getResult(0),k).getResult(0),R=c.reduce(O,d,f,"sum").getResult(0);return[S,T,v,A,R]});pe("elu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.results[0],s=n.op.getAttr("alpha")??1,o=n.full(0,t.type),i=n.full(1,t.type),a=n.full(s,t.type),u=n.builder.compare(t,o,"gt").getResult(0),p=n.builder.add(r,a).getResult(0),c=n.builder.select(u,i,p).getResult(0);return[n.builder.mul(e,c).getResult(0)]});pe("leaky_relu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("negative_slope")??.01,s=n.full(0,t.type),o=n.full(1,t.type),i=n.full(r,t.type),a=n.builder.compare(t,s,"gt").getResult(0),u=n.builder.select(a,o,i).getResult(0);return[n.builder.mul(e,u).getResult(0)]});pe("celu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.op.getAttr("alpha")??1,s=n.full(0,t.type),o=n.full(1,t.type),i=n.builder.compare(t,s,"gt").getResult(0),a=n.full(r,t.type),u=n.builder.div(t,a).getResult(0),p=n.builder.exp(u).getResult(0),c=n.builder.select(i,o,p).getResult(0);return[n.builder.mul(e,c).getResult(0)]});pe("selu",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=1.0507009873554805,s=1.6732632423543772,o=n.full(0,t.type),i=n.full(r,t.type),a=n.builder.compare(t,o,"gt").getResult(0),u=n.full(s,t.type),p=n.builder.exp(t).getResult(0),c=n.builder.mul(u,p).getResult(0),f=n.builder.select(a,i,n.builder.mul(i,c).getResult(0)).getResult(0);return[n.builder.mul(e,f).getResult(0)]});pe("hardswish",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(-3,t.type),s=n.full(3,t.type),o=n.full(0,t.type),i=n.full(1,t.type),a=n.full(2,t.type),u=n.full(6,t.type),p=n.builder.compare(t,r,"le").getResult(0),c=n.builder.compare(t,s,"ge").getResult(0),f=n.builder.add(n.builder.mul(a,t).getResult(0),s).getResult(0),d=n.builder.div(f,u).getResult(0),h=n.builder.select(p,o,n.builder.select(c,i,d).getResult(0)).getResult(0);return[n.builder.mul(e,h).getResult(0)]});pe("hardsigmoid",n=>{let e=n.gradOutputs[0],[t]=n.operands,r=n.full(-3,t.type),s=n.full(3,t.type),o=n.full(0,t.type),i=n.full(1/6,t.type),a=n.builder.compare(t,r,"le").getResult(0),u=n.builder.compare(t,s,"ge").getResult(0),p=n.builder.select(a,o,n.builder.select(u,o,i).getResult(0)).getResult(0);return[n.builder.mul(e,p).getResult(0)]});pe("embedding",n=>{let e=n.gradOutputs[0],[t,r]=n.operands,s=r.type.rank,o=n.full(0,t.type);return[n.builder.scatter(o,r,e,{updateWindowDims:[s],insertedWindowDims:[0],scatterDimsToOperandDims:[0],indexVectorDim:s}).getResult(0),null]})});var pC=M(()=>{"use strict";Is();pk("stop_gradient");pe("stop_gradient",()=>[null]);for(let n of["compare","logical_not","argmax","argmin","iota"])pk(n)});var fC=M(()=>{"use strict";Is();Hd();Jd();hk();mw();eC();rC();iC();aC();uC();lC();cC();pC()});var dC={};at(dC,{compileWithBackward:()=>yw});function gw(n){return typeof n.then=="function"}function yw(n,e,t={}){let r=t.target??go(),s=t.mode||"separate",o=t.rematPolicy||new Lu(t.remat||{}),i={target:r,verify:!1,...t,backward:void 0,mode:void 0,rematPolicy:void 0,remat:void 0},a=t.dynamic_shapes||null,u=[],p=null,c=null,f=null;function d(k){let O=bw((...B)=>n.forward(...B),k,{name:n.constructor.name||"compiled",dynamicShapes:a}),R=l(B=>{let E=B.graph.functions().next().value,F=s==="joint"?m(E,B,o):h(E,B,o);return F.shapeEnv=B.shapeEnv,F.outputSymShapes=B.outputSymShapes,F},"finish");return gw(O)?O.then(R):R(O)}l(d,"_compile");function h(k,O,R){let B=new Qd({rematPolicy:R}),{backwardFunc:E,savedValues:F,gradInputIndices:C}=B.build(k),H=k.getReturnOp(),G=[...H.operands],q=G.length,D=new Map(k.args.map((ke,rt)=>[ke.id,rt])),V=new Map(G.map((ke,rt)=>[ke.id,rt])),ue=[],ce=new Map;for(let ke of F)D.has(ke.id)||V.has(ke.id)||ce.has(ke.id)||(ce.set(ke.id,q+ue.length),ue.push(ke));ue.length>0&&(H.erase(),new $n(k).returnOp([...G,...ue]),k.outputTypes=Object.freeze([...G.map(ke=>ke.type),...ue.map(ke=>ke.type)]));let Le=F.map(ke=>D.has(ke.id)?{kind:"arg",index:D.get(ke.id)}:V.has(ke.id)?{kind:"output",index:V.get(ke.id)}:{kind:"output",index:ce.get(ke.id)}),j=new Mn("forward");j.addFunction(k);let W=new Si(i).compile(j),ie=new Mn("backward");ie.addFunction(E);let je=new Si(i).compile(ie);return{mode:"separate",fwdResult:W,bwdResult:je,forwardFunc:k,backwardFunc:E,savedValues:F,savedSources:Le,numRealOutputs:q,gradInputIndices:C,capturedParams:O.capturedParams,numUserInputs:O.numUserInputs,outputTypes:O.outputTypes,shapeEnv:O.shapeEnv,outputSymShapes:O.outputSymShapes}}l(h,"_compileSeparate");function m(k,O,R){let B=new Xd({rematPolicy:R}),{jointFunc:E,numForwardOutputs:F,numGradInputs:C}=B.build(k),H=new Mn("joint");return H.addFunction(E),{mode:"joint",result:new Si(i).compile(H),jointFunc:E,numForwardOutputs:F,numGradInputs:C,capturedParams:O.capturedParams,numUserInputs:O.numUserInputs,outputTypes:O.outputTypes,inputTypes:k.inputTypes,shapeEnv:O.shapeEnv,outputSymShapes:O.outputSymShapes}}l(m,"_compileJoint");function g(k,O){return k.outputSymShapes&&k.shapeEnv?k.shapeEnv.resolveSymbolicShape(k.outputSymShapes[O]):[...k.outputTypes[O].shape]}l(g,"_resolveOutputShape");function y(k,O,R){let B=k.module||k;return B.executionPlan?B.runPlanAsync(B.executionPlan,R):k.isAsync(O)?k.runAsync(O,...R):(k.run(O,...R),null)}l(y,"_runK");function _(k,O){let B=k.fwdResult.listKernels()[0],E=O.length>0?O[0].device:"cpu",F=O.map(j=>Qe(j)),H=k.capturedParams.map(j=>Qe(j)),G=k.forwardFunc.outputTypes,q=k.numRealOutputs,D=new Array(G.length),V=new Array(G.length);for(let j=0;j<G.length;j++){let W=j<q?g(k,j):[...G[j].shape],ie=G[j].dtype,je=bn(W),ke=St(ie);D[j]=new ke(Math.max(je,1)),V[j]=W}let ue=[...F,...H,...D],ce=l(()=>({results:q===1?Ee(D[0],V[0],G[0].dtype,E):Array.from({length:q},(W,ie)=>Ee(D[ie],V[ie],G[ie].dtype,E)),inputArrays:F,paramArrays:H,outputArrays:D,device:E}),"build"),Le=y(k.fwdResult,B,ue);return Le?Le.then(ce):ce()}l(_,"_executeSeparateForward");function b(k,O,R){let E=k.bwdResult.listKernels()[0],F=O.map(ie=>Qe(ie)),C=k.savedValues,H=k.savedSources,G=[...R.inputArrays,...R.paramArrays],q=new Array(C.length);for(let ie=0;ie<C.length;ie++){let je=H[ie];q[ie]=je.kind==="arg"?G[je.index]:R.outputArrays[je.index]}let D=k.backwardFunc,V=D.outputTypes.length,ue=new Array(V),ce=new Array(V);for(let ie=0;ie<V;ie++){let je=[...D.outputTypes[ie].shape],ke=D.outputTypes[ie].dtype,rt=bn(je),zt=St(ke);ue[ie]=new zt(Math.max(rt,1)),ce[ie]=je}let Le=[...F,...q,...ue],j=l(()=>ue.map((ie,je)=>Ee(ie,ce[je],D.outputTypes[je].dtype,R.device)),"build"),W=y(k.bwdResult,E,Le);return W?W.then(j):j()}l(b,"_executeSeparateBackward");function w(k){for(let O=0;O<u.length;O++){let R=u[O];R.shapeEnv.bindInputShapes(k);let{passed:B}=R.shapeEnv.evaluateGuards();if(B)return R}return null}l(w,"_findCachedEntry");function x(k,O){if(c=k,k.mode==="joint")return v(k,O);let R=_(k,O);return gw(R)?R.then(B=>(p=B,B.results)):(p=R,R.results)}l(x,"_forwardWith");function S(k){let O=w(k);if(O)return x(O,k);let R=d(k);return gw(R)?R.then(B=>(u.push(B),B.shapeEnv.bindInputShapes(k),x(B,k))):(u.push(R),R.shapeEnv.bindInputShapes(k),x(R,k))}l(S,"_runForward");function T(...k){return f?f.then(()=>S(k)):S(k)}l(T,"compiledForward");function v(k,O){let B=k.result.listKernels()[0],E=O.length>0?O[0].device:"cpu",F=O.map(W=>Qe(W)),H=k.capturedParams.map(W=>Qe(W)),G=k.jointFunc,q=G.outputTypes.length,D=new Array(q),V=new Array(q);for(let W=0;W<q;W++){let ie=[...G.outputTypes[W].shape],je=G.outputTypes[W].dtype,ke=bn(ie),rt=St(je);D[W]=new rt(Math.max(ke,1)),V[W]=ie}let ue=new Array(k.numForwardOutputs);for(let W=0;W<k.numForwardOutputs;W++){let ie=k.outputTypes[W],je=bn(ie.shape),ke=St(ie.dtype);ue[W]=new ke(Math.max(je,1))}p={inputArrays:F,paramArrays:H,gradOutputArrays:ue,outputArrays:D,outputShapes:V,device:E,compiled:k};let ce=[...F,...H,...ue,...D],Le=l(()=>{let W=[];for(let ie=0;ie<k.numForwardOutputs;ie++)W.push(Ee(D[ie],V[ie],G.outputTypes[ie].dtype,E));return W.length===1?W[0]:W},"build"),j=y(k.result,B,ce);return j?j.then(Le):Le()}l(v,"_executeJointForward");let A=T;A.backward=function(...k){if(!c||!p)throw new Error("Must run forward before backward");return c.mode==="joint"?N(c,k,p):b(c,k,p)};function N(k,O,R){let{inputArrays:B,paramArrays:E,outputArrays:F,outputShapes:C,device:H}=R,G=O.map(W=>Qe(W));for(let W=0;W<G.length;W++)R.gradOutputArrays[W].set(G[W]);let q=k.jointFunc,D=q.outputTypes.length,V=new Array(D),ue=new Array(D);for(let W=0;W<D;W++){let ie=[...q.outputTypes[W].shape],je=q.outputTypes[W].dtype,ke=bn(ie),rt=St(je);V[W]=new rt(Math.max(ke,1)),ue[W]=ie}let ce=[...B,...E,...G,...V],Le=l(()=>{let W=[];for(let ie=k.numForwardOutputs;ie<D;ie++)W.push(Ee(V[ie],ue[ie],q.outputTypes[ie].dtype,H));return W},"build"),j=y(k.result,k.result.listKernels()[0],ce);return j?j.then(Le):Le()}if(l(N,"_executeJointBackward"),A.original=n,A.backwardGraph=()=>{if(u.length===0)return null;let k=u[0];return k.mode==="joint"?k.jointFunc:k.backwardFunc},A.forwardGraph=()=>{if(u.length===0)return null;let k=u[0];return k.mode==="joint"?k.jointFunc:k.forwardFunc},A.capturedParams=()=>u.length?u[0].capturedParams:[],e){let k=d(e);gw(k)?f=k.then(O=>(u.push(O),f=null,O)):u.push(k)}return A}var _w=M(()=>{"use strict";mk();iw();yo();Di();Jd();_s();hk();mw();fC();wn();rs();ss();l(gw,"_isThenable");l(yw,"compileWithBackward")});function Cu(n){return typeof n.then=="function"}function Cj(){hC||(W2(),hC=!0)}function Dj(n,e){if(!n)return new Array(e.length).fill(null);let t=new Array(e.length);for(let r=0;r<e.length;r++){let s=n[r];if(s===!0){let o=new Set;for(let i=0;i<e[r].shape.length;i++)o.add(i);t[r]=o}else s instanceof Set?t[r]=s:t[r]=null}return t}function bw(n,e,t){Cj();let r=t?.name||n.name||"traced",s=new M_(r),o=Dj(t?.dynamicShapes,e);for(let f=0;f<e.length;f++)s.createInput(e[f].shape,e[f].dtype,o[f]);let i=e.length,a=s._initGraph();function u(f){Array.isArray(f)?s.markOutputs(f):s.markOutput(f),s.deactivate();let d=s.getGraphModule(),h=d.functions().next().value;return{graph:d,capturedParams:[...s.capturedParams],numUserInputs:i,outputTypes:h.outputTypes,shapeEnv:s.shapeEnv,outputSymShapes:s.outputSymShapes}}l(u,"_finalize"),s.activate();let p=Jr.fromKey(48),c=NN(p,()=>n(...a));if(Cu(c))return c.then(f=>u(f),f=>{throw s.deactivate(),f});try{return u(c)}catch(f){throw s.deactivate(),f}}function mC(n,e,t){let r=bw(n,e,t);return Cu(r)?r.then(s=>s.graph):r.graph}function Pj(n,e,t){let r=n.result.listKernels();if(r.length===0)throw new Error("No kernels compiled");let s=r[0],o=e.length>0?e[0].device:"cpu",i=new Array(e.length);for(let g=0;g<e.length;g++)i[g]=Qe(e[g]);let a=n.capturedParams,u=new Array(a.length);for(let g=0;g<a.length;g++)u[g]=Qe(a[g]);let p=n.outputTypes,c=n.outputSymShapes,f=new Array(p.length),d=new Array(p.length);for(let g=0;g<p.length;g++){let y=c&&t?t.resolveSymbolicShape(c[g]):p[g].shape,_=p[g].dtype,b=bn(y),w=St(_);f[g]=new w(Math.max(b,1)),d[g]=[...y]}let h=new Array(i.length+u.length+f.length),m=0;for(let g=0;g<i.length;g++)h[m++]=new Lo(i[g],e[g].shape,e[g].dtype);for(let g=0;g<u.length;g++){let y=new Lo(u[g],a[g].shape,a[g].dtype),_=a[g]._impl;_&&(y.resident={key:_.storage.rawData,version:_.version}),h[m++]=y}for(let g=0;g<f.length;g++)h[m++]=new Lo(f[g],d[g],p[g].dtype);return{funcName:s,device:o,outputTypes:p,outputArrays:f,outputShapes:d,allArgs:h}}function gk(n,e,t,r){if(e.length===1)return Ee(t[0],r[0],e[0].dtype,n);let s=new Array(e.length);for(let o=0;o<e.length;o++)s[o]=Ee(t[o],r[o],e[o].dtype,n);return s}function $j(n,e,t){let{funcName:r,device:s,outputTypes:o,outputArrays:i,outputShapes:a,allArgs:u}=Pj(n,e,t),p=n.result.module.executionPlan;return p?n.result.module.runPlanAsync(p,u,{resident:!0}).then(()=>gk(s,o,i,a)):n.result.isAsync(r)?n.result.runAsync(r,...u).then(()=>gk(s,o,i,a)):(n.result.run(r,...u),gk(s,o,i,a))}function Fj(n,e){let t=[];for(let r of n.result.listKernels()){let s=n.result.getSource(r);s!==null&&t.push(`// ---- compiled entry ${e}, kernel ${r} ----
|
|
248
248
|
${s}`)}return t.length>0?t.join(`
|
|
249
249
|
|
|
250
250
|
`):null}function zj(n,e,t={}){if(t?.backward)return yw(n,e,t);let r=t?.target??go(),s={target:r,verify:!1,...t},o=t?.dynamic_shapes||null,i=t?.shapeBuckets||null,a=t?.foldWeights??t?.quantization?.foldWeights??!1,u=[];function p(w,x,S){if(!w||typeof w!="object"||w.repro)return w;try{w.repro={name:n.constructor?.name||"compiled",phase:S,target:r?.name,inputs:(x||[]).map(T=>({shape:T.shape,dtype:T.dtype})),config:{fusion:s.fusion,scheduling:s.scheduling,optimization:s.optimization,quantization:s.quantization,dynamicShapes:!!o}}}catch{return w}return w}l(p,"_attachRepro");function c(w){let x=a?H2(w,Qe):w;return{result:new Si(s).compile(x.graph),graph:x.graph,capturedParams:x.capturedParams,numUserInputs:x.numUserInputs,outputTypes:x.outputTypes,shapeEnv:x.shapeEnv,outputSymShapes:x.outputSymShapes}}l(c,"_finalize");function f(w,x){try{let S=bw((...T)=>n.forward(...T),w,{name:n.constructor.name||"compiled",dynamicShapes:x});return Cu(S)?S.then(c,T=>{throw p(T,w,"compile")}):c(S)}catch(S){throw p(S,w,"compile")}}l(f,"_compileWith");function d(w){return f(w,o)}l(d,"_compile");function h(w){return w.map((x,S)=>({shape:x,dtype:e[S].dtype}))}l(h,"_bucketInputs");function m(w){for(let x=0;x<u.length;x++){let S=u[x];S.shapeEnv.bindInputShapes(w);let{passed:T}=S.shapeEnv.evaluateGuards();if(T)return S}return null}l(m,"_findCachedEntry");function g(w,x){try{let S=$j(w,x,w.shapeEnv);return Cu(S)?S.then(void 0,T=>{throw p(T,x,"run")}):S}catch(S){throw p(S,x,"run")}}l(g,"_execute");function y(...w){let x=m(w);if(!x){let S=d(w);if(Cu(S))return S.then(T=>(u.push(T),T.shapeEnv.bindInputShapes(w),g(T,w)));x=S,u.push(x),x.shapeEnv.bindInputShapes(w)}return g(x,w)}l(y,"compiledForward");let _=null;if(e){let w=[];if(i)for(let x of i)w.push(f(h(x),null));if(w.push(d(e)),w.some(x=>Cu(x)))_=Promise.all(w).then(x=>{for(let S of x)u.push(S)});else for(let x of w)Cu(x)||u.push(x)}let b=y;return b.original=n,b.graph=w=>mC((...S)=>n.forward(...S),w||e,{name:n.constructor.name||"compiled",dynamicShapes:o}),b.source=()=>u.length===0?null:Fj(u[0],0),b.kernels=()=>u.length===0?[]:u[0].result.listKernels(),b.result=()=>u.length>0?u[0].result:null,b._ready=_,b}var hC,mk=M(()=>{"use strict";_u();K2();An();s0();iw();yo();wn();fy();rs();ss();Y2();_w();hC=!1;l(Cu,"_isThenable");l(Cj,"_ensureTracing");l(Dj,"_normalizeDynamicShapes");l(bw,"_traceCore");l(mC,"trace");l(Pj,"_prepareExecution");l(gk,"_wrapOutputs");l($j,"executeCompiled");l(Fj,"sourceForEntry");l(zj,"compile")});ys();An();wn();Ly();sr();di();Xa();Un();ss();rs();function tc(n,e){let t=En(n),r=Wn.allocate(0,e,Ol),s=new Pt(r,0,n,t,e,Ol);return new Ze(s)}l(tc,"_metaTensor");function W3(n,e,t){let r=Bl(e.shape,t.shape);if(!r)throw new Error(`Incompatible shapes: [${e.shape}] vs [${t.shape}]`);let s=yg(e.dtype,t.dtype);return tc(r,s)}l(W3,"_metaBinary");function K3(n,e){return tc([...e.shape],e.dtype)}l(K3,"_metaUnary");function H3(n,e,t,r){let s=[...e.shape],o=t!=null?Array.isArray(t)?t:[t]:Array.from({length:s.length},(u,p)=>p);if(o.length===s.length||t===void 0)return tc(r?s.map(()=>1):[],e.dtype);let i=[],a=new Set(o.map(u=>u<0?s.length+u:u));for(let u=0;u<s.length;u++)a.has(u)?r&&i.push(1):i.push(s[u]);return tc(i,e.dtype)}l(H3,"_metaReduction");function Y3(n,e,t){let r=Tg(e.shape,t.shape);if(r===null)throw new Error("metaMatmul: unsupported shapes");return tc(r,e.dtype)}l(Y3,"metaMatmul");function J3(n,e){return tc([...e.shape],e.dtype)}l(J3,"metaClone");function fS(n,e){return Object.fromEntries(n.map(t=>[t,e]))}l(fS,"mapKernel");var BM=Object.freeze({...fS(["add","sub","mul","div","pow","rem","maximum","minimum"],W3),...fS(["neg","exp","log","sqrt","rsqrt","abs","sin","cos","tanh","erf","erfc","lgamma","gamma","sigmoid","relu","gelu","silu","sign","floor","ceil"],K3),...fS(["sum","mean","max","min","prod"],H3),matmul:Y3,clone:J3});GT();be();kt();os();ss();function Z3(n,e,t,r){let s=Ve(n.shape,{dtype:n.dtype}),o=nu(s,e,t,r),i=nu(s,e,t,tn(r.shape,{dtype:n.dtype}));return or(Ml(i,Ve(i.shape,{dtype:i.dtype})),o,n)}l(Z3,"scatterKernel");function Q3(n,e){let t=n.shape,r=t.length;if(e.length<r)throw new Error("repeat: reps length must be >= tensor rank");let s=e.length-r,o=s>0?[...Array(s).fill(1),...t]:t.slice(),i=[],a=[],u=[];for(let p=0;p<o.length;p++)i.push(1,o[p]),a.push(e[p],o[p]),u.push(e[p]*o[p]);return Hn(El(Hn(n,i),a),u)}l(Q3,"repeatKernel");function X3(n,e){let t=n.shape.length,r=e.length<t?[...Array(t-e.length).fill(1),...e]:e;return Cl(n,r)}l(X3,"tileKernel");function eV(n,e,t){let r=[],s=n.shape.length,o=t<0?s+t:t,i=0;for(let a of e)r.push(ir(n,o,i,a)),i+=a;return r}l(eV,"splitKernel");function tV(n,e,t){let r=n.shape.length,s=t<0?r+t:t,o=n.shape[s],i=Math.ceil(o/e);return hi(n,i,s)}l(tV,"chunkKernel");function nV(n,e,t=0){let r=n.shape.length,s=Ks(t,r),o=n.shape[s],i=(e%o+o)%o;return i===0?n:ru([ir(n,s,o-i,i),ir(n,s,0,o-i)],s)}l(nV,"rollKernel");function rV(n,e){let t=Array.isArray(e)?e:[e],r=n.shape.length,s=n;for(let o of t){let i=Ks(o,r),a=s.shape[i],u=new Array(a);for(let p=0;p<a;p++)u[p]=a-1-p;s=ra(s,i,Wt(u,{dtype:"i32"}))}return s}l(rV,"flipKernel");function sV(n,e=0){let t=n.shape.length,r=Ks(e,t),s=n.shape[r],o=n;for(let i=1;i<s;i*=2){let a=[...o.shape];a[r]=i;let u=ru([Ve(a,{dtype:o.dtype}),ir(o,r,0,s-i)],r);o=ae(o,u)}return o}l(sV,"cumsumKernel");function oV(n){let e=1;for(;e<n;)e<<=1;return e}l(oV,"nextPow2");function Py(n,e,t){let r=n.shape.length,s=r-1,o=n.shape[s],i=oV(o),a=e?-1/0:1/0,u=n;if(i>o){let f=new Array(r).fill(0),d=new Array(r).fill(0);d[s]=i-o,u=tu(n,f,d,a)}let p=new Array(r).fill(1);p[s]=i;let c=null;if(t){let f=new Array(i);for(let d=0;d<i;d++)f[d]=d;c=Hn(Wt(f,{dtype:"i32"}),p)}for(let f=2;f<=i;f<<=1)for(let d=f>>1;d>=1;d>>=1){let h=new Array(i),m=new Array(i);for(let x=0;x<i;x++){let S=x^d;h[x]=S;let T=(x&f)===0,v=e?!T:T;S>x?m[x]=v?1:0:m[x]=v?0:1}let g=Wt(h,{dtype:"i32"}),y=ra(u,s,g),_=Sg(u,y),b=ta(u,y),w=Hn(Wt(m,{dtype:"f32"}),p);if(t){let x=ra(c,s,g),S=or(w,Hp(_,u),Hp(b,u));c=or(S,c,x)}u=or(w,_,b)}return i>o&&(u=ir(u,s,0,o),t&&(c=ir(c,s,0,o))),t?{values:u,indices:c}:u}l(Py,"bitonicLastDim");function iV(n,e=-1,t=!1){let r=n.shape.length,s=Ks(e,r);if(s===r-1)return Py(n,t,!1);let o=vt(n,s,r-1),i=Py(o,t,!1);return vt(i,s,r-1)}l(iV,"sortKernel");function RM(n,e,t){let r=n.shape.length,s=Ks(e,r);if(s===r-1)return Py(n,t,!0);let o=vt(n,s,r-1),i=Py(o,t,!0);return{values:vt(i.values,s,r-1),indices:vt(i.indices,s,r-1)}}l(RM,"sortWithIndices");function aV(n,e=-1,t=!1){return RM(n,e,t).indices}l(aV,"argsortKernel");function uV(n,e,t=-1,r=!0){let s=n.shape.length,o=Ks(t,s),{values:i,indices:a}=RM(n,o,r);return[ir(i,o,0,e),ir(a,o,0,e)]}l(uV,"topkKernel");var MM=Object.freeze({scatter:l((n,e,t,r,s)=>Z3(e,t,r,s),"scatter"),repeat:l((n,e,t)=>Q3(e,t),"repeat"),tile:l((n,e,t)=>X3(e,t),"tile"),split:l((n,e,t,r)=>eV(e,t,r),"split"),chunk:l((n,e,t,r)=>tV(e,t,r),"chunk"),roll:l((n,e,t,r)=>nV(e,t,r),"roll"),flip:l((n,e,t)=>rV(e,t),"flip"),cumsum:l((n,e,t)=>sV(e,t),"cumsum"),sort:l((n,e,t,r)=>iV(e,t,r),"sort"),argsort:l((n,e,t,r)=>aV(e,t,r),"argsort"),topk:l((n,e,t,r,s)=>uV(e,t,r,s),"topk")});var lV=3,IM=!1,cV=[0,1,2,3,5];function EM(){if(IM)return;IM=!0;let n=new Vt("mlc","DEF");for(let t of bM())n.def(t);let e=new Vt("mlc","IMPL");for(let[t,r]of Object.entries(BM))e.impl(t,lV,r);for(let[t,r]of Object.entries({...bB,...MM}))for(let s of cV)e.impl(t,s,r);pS()}l(EM,"registerNativeOps");ys();An();ey();os();wn();Un();var pV=new Set(["cpu","wasm"]);function dS(n){let e=new Float64Array(n.length);for(let t=0;t<n.length;t++)e[t]=Number(n[t]);return e}l(dS,"float64From");function LM(n){if(!pV.has(n.device.type))throw new Error(`linalg/ml: host-readable device required (cpu or wasm), got '${n.device.type}'`)}l(LM,"requireHost");function Ye(n){if(n.ndim!==2)throw new Error(`linalg/ml: expected a 2-D matrix, got ${n.ndim}-D`);LM(n);let[e,t]=n.shape;return{data:dS(Qe(n)),rows:e,cols:t}}l(Ye,"hostMatrix");function $t(n){if(LM(n),n.ndim===1)return{data:dS(Qe(n)),rows:n.shape[0],cols:1,wasVector:!0};if(n.ndim===2)return{data:dS(Qe(n)),rows:n.shape[0],cols:n.shape[1],wasVector:!1};throw new Error(`linalg/ml: expected a 1-D or 2-D right-hand side, got ${n.ndim}-D`)}l($t,"hostColumns");function Q(n,e,t,r=Ws){return Wt(n,{shape:e,dtype:t,device:r})}l(Q,"toHostTensor");os();function CM(n,e){let t=Float64Array.from(n),r=new Int32Array(e);for(let o=0;o<e;o++)r[o]=o;let s=1;for(let o=0;o<e;o++){let i=o,a=Math.abs(t[o*e+o]);for(let p=o+1;p<e;p++){let c=Math.abs(t[p*e+o]);c>a&&(a=c,i=p)}if(a===0)throw new Error("linalg: matrix is singular");if(i!==o){for(let c=0;c<e;c++){let f=t[o*e+c];t[o*e+c]=t[i*e+c],t[i*e+c]=f}let p=r[o];r[o]=r[i],r[i]=p,s=-s}let u=t[o*e+o];for(let p=o+1;p<e;p++){let c=t[p*e+o]/u;t[p*e+o]=c;for(let f=o+1;f<e;f++)t[p*e+f]-=c*t[o*e+f]}}return{lu:t,piv:r,sign:s}}l(CM,"luFactor");function fV(n,e,t,r,s){let o=new Float64Array(t*s);for(let i=0;i<t;i++)for(let a=0;a<s;a++)o[i*s+a]=r[e[i]*s+a];for(let i=0;i<t;i++)for(let a=0;a<s;a++){let u=o[i*s+a];for(let p=0;p<i;p++)u-=n[i*t+p]*o[p*s+a];o[i*s+a]=u}for(let i=t-1;i>=0;i--){let a=n[i*t+i];for(let u=0;u<s;u++){let p=o[i*s+u];for(let c=i+1;c<t;c++)p-=n[i*t+c]*o[c*s+u];o[i*s+u]=p/a}}return o}l(fV,"luSolve");function hS(n,e,t,r){let{lu:s,piv:o}=CM(n,e);return fV(s,o,e,t,r)}l(hS,"solveHost");function DM(n,e){let t;try{t=CM(n,e)}catch{return 0}let r=t.sign;for(let s=0;s<e;s++)r*=t.lu[s*e+s];return r}l(DM,"detHost");function PM(n,e){let t=new Float64Array(e*e);for(let r=0;r<e;r++)for(let s=0;s<=r;s++){let o=n[r*e+s];for(let i=0;i<s;i++)o-=t[r*e+i]*t[s*e+i];if(r===s){if(o<=0)throw new Error("linalg.cholesky: matrix is not positive definite");t[r*e+s]=Math.sqrt(o)}else t[r*e+s]=o/t[s*e+s]}return t}l(PM,"choleskyHost");Xp();function Sf(n,e,t){let r=t?.tol??1e-12,s=t?.maxSweeps??100,o=Float64Array.from(n),i=new Float64Array(e*e);for(let c=0;c<e;c++)i[c*e+c]=1;for(let c=0;c<s;c++){let f=0;for(let d=0;d<e;d++)for(let h=d+1;h<e;h++)f+=o[d*e+h]*o[d*e+h];if(Math.sqrt(f)<r)break;for(let d=0;d<e;d++)for(let h=d+1;h<e;h++){let m=o[d*e+h];if(Math.abs(m)<1e-300)continue;let g=(o[h*e+h]-o[d*e+d])/(2*m),y=Math.sign(g||1)/(Math.abs(g)+Math.sqrt(g*g+1)),_=1/Math.sqrt(y*y+1),b=y*_;for(let w=0;w<e;w++){let x=o[w*e+d],S=o[w*e+h];o[w*e+d]=_*x-b*S,o[w*e+h]=b*x+_*S}for(let w=0;w<e;w++){let x=o[d*e+w],S=o[h*e+w];o[d*e+w]=_*x-b*S,o[h*e+w]=b*x+_*S}for(let w=0;w<e;w++){let x=i[w*e+d],S=i[w*e+h];i[w*e+d]=_*x-b*S,i[w*e+h]=b*x+_*S}}}let a=Array.from({length:e},(c,f)=>f).sort((c,f)=>o[c*e+c]-o[f*e+f]),u=new Float64Array(e),p=new Float64Array(e*e);for(let c=0;c<e;c++){let f=a[c];u[c]=o[f*e+f];for(let d=0;d<e;d++)p[d*e+c]=i[d*e+f]}return{values:u,vectors:p}}l(Sf,"eighHost");Xp();function mV(n,e,t,r){if(r){let o=new Float64Array(t*t);for(let i=0;i<t;i++)for(let a=i;a<t;a++){let u=0;for(let p=0;p<e;p++)u+=n[p*t+i]*n[p*t+a];o[i*t+a]=u,o[a*t+i]=u}return o}let s=new Float64Array(e*e);for(let o=0;o<e;o++)for(let i=o;i<e;i++){let a=0;for(let u=0;u<t;u++)a+=n[o*t+u]*n[i*t+u];s[o*e+i]=a,s[i*e+o]=a}return s}l(mV,"gram");function gV(n,e,t,r,s){let o=new Float64Array(e*s);for(let i=0;i<e;i++)for(let a=0;a<s;a++){let u=0;for(let p=0;p<t;p++)u+=n[i*t+p]*r[a*t+p];o[i*s+a]=u}return o}l(gV,"matmulRows");function mu(n,e,t,r,s=mV,o=gV){let i=r?.tol??1e-12,a=Math.min(e,t),u=new Float64Array(e*a),p=new Float64Array(a),c=new Float64Array(t*a);if(t<=e){let{values:f,vectors:d}=Sf(s(n,e,t,!0),t,r),h=new Float64Array(a*t);for(let g=0;g<a;g++){let y=t-1-g;p[g]=Math.sqrt(Math.max(f[y],0));for(let _=0;_<t;_++){let b=d[_*t+y];c[_*a+g]=b,h[g*t+_]=b}}let m=o(n,e,t,h,a);for(let g=0;g<a;g++)if(p[g]>i)for(let y=0;y<e;y++)u[y*a+g]=m[y*a+g]/p[g]}else{let{values:f,vectors:d}=Sf(s(n,e,t,!1),e,r),h=new Float64Array(a*e);for(let y=0;y<a;y++){let _=e-1-y;p[y]=Math.sqrt(Math.max(f[_],0));for(let b=0;b<e;b++){let w=d[b*e+_];u[b*a+y]=w,h[y*e+b]=w}}let m=new Float64Array(t*e);for(let y=0;y<e;y++)for(let _=0;_<t;_++)m[_*e+y]=n[y*t+_];let g=o(m,t,e,h,a);for(let y=0;y<a;y++)if(p[y]>i)for(let _=0;_<t;_++)c[_*a+y]=g[_*a+y]/p[y]}return{U:u,S:p,V:c,k:a}}l(mu,"svdHost");Xp();function $M(n,e,t,r,s,o){let i=o?.rcond??1e-12,{U:a,S:u,V:p,k:c}=mu(n,e,t,o),f=i*(u.length?u[0]:0),d=new Float64Array(c*s);for(let m=0;m<c;m++){let g=u[m];for(let y=0;y<s;y++){let _=0;for(let b=0;b<e;b++)_+=a[b*c+m]*r[b*s+y];d[m*s+y]=g>f?_/g:0}}let h=new Float64Array(t*s);for(let m=0;m<t;m++)for(let g=0;g<s;g++){let y=0;for(let _=0;_<c;_++)y+=p[m*c+_]*d[_*s+g];h[m*s+g]=y}return h}l($M,"lstsqHost");function FM(n,e,t,r){let s=r?.rcond??1e-12,{U:o,S:i,V:a,k:u}=mu(n,e,t,r),p=s*(i.length?i[0]:0),c=new Float64Array(t*e);for(let f=0;f<t;f++)for(let d=0;d<e;d++){let h=0;for(let m=0;m<u;m++){let g=i[m];g>p&&(h+=a[f*u+m]*(1/g)*o[d*u+m])}c[f*e+d]=h}return c}l(FM,"pinvHost");function zM(n,e,t){let r=new Float64Array(t);for(let i=0;i<t;i++){let a=0;for(let u=0;u<e;u++)a+=n[u*t+i];r[i]=a/e}let s=e>1?e-1:1,o=new Float64Array(t*t);for(let i=0;i<t;i++)for(let a=i;a<t;a++){let u=0;for(let c=0;c<e;c++)u+=(n[c*t+i]-r[i])*(n[c*t+a]-r[a]);let p=u/s;o[i*t+a]=p,o[a*t+i]=p}return o}l(zM,"covHost");function vf(n,e){let t=Ye(n);if(t.rows!==t.cols)throw new Error(`linalg.${e}: matrix must be square`);return t}l(vf,"squareMatrix");function VM(n,e){let{data:t,rows:r}=vf(e,"cholesky");return Q(PM(t,r),[r,r],e.dtype,e.device)}l(VM,"cpuCholesky");function GM(n,e,t){let{data:r,rows:s}=vf(e,"solve"),o=$t(t);if(o.rows!==s)throw new Error("linalg.solve: right-hand side rows must match matrix");let i=hS(r,s,o.data,o.cols);return Q(i,o.wasVector?[s]:[s,o.cols],e.dtype,e.device)}l(GM,"cpuSolve");function jM(n,e,t){let{data:r,rows:s,cols:o}=Ye(e),i=$t(t);if(i.rows!==s)throw new Error("linalg.lstsq: right-hand side rows must match matrix");let a=$M(r,s,o,i.data,i.cols);return Q(a,i.wasVector?[o]:[o,i.cols],e.dtype,e.device)}l(jM,"cpuLstsq");function qM(n,e){let{data:t,rows:r}=vf(e,"inv"),s=new Float64Array(r*r);for(let i=0;i<r;i++)s[i*r+i]=1;let o=hS(t,r,s,r);return Q(o,[r,r],e.dtype,e.device)}l(qM,"cpuInv");function UM(n,e){let{data:t,rows:r,cols:s}=Ye(e);return Q(FM(t,r,s),[s,r],e.dtype,e.device)}l(UM,"cpuPinv");function WM(n,e){let{data:t,rows:r}=vf(e,"det");return Wp(DM(t,r),{dtype:e.dtype})}l(WM,"cpuDet");function KM(n,e){let{data:t,rows:r,cols:s}=Ye(e);return Q(zM(t,r,s),[s,s],e.dtype,e.device)}l(KM,"cpuCov");function HM(n,e){let{data:t,rows:r}=vf(e,"eigh"),{values:s,vectors:o}=Sf(t,r);return[Q(s,[r],e.dtype,e.device),Q(o,[r,r],e.dtype,e.device)]}l(HM,"cpuEigh");function $y(n,e){let{data:t,rows:r,cols:s}=Ye(e),{U:o,S:i,V:a,k:u}=mu(t,r,s);return[Q(o,[r,u],e.dtype,e.device),Q(i,[u],e.dtype,e.device),Q(a,[s,u],e.dtype,e.device)]}l($y,"cpuSvd");var yV={svd:$y,eigh:HM,cholesky:VM,inv:qM,pinv:UM,det:WM,cov:KM,solve:GM,lstsq:jM},_V=[0,2],YM=!1;function JM(){if(YM)return;YM=!0,Pl();let n=new Vt("mlc","IMPL");for(let[e,t]of Object.entries(yV))for(let r of _V)n.impl(e,r,t)}l(JM,"registerCpuLinalg");ys();An();ys();var bV=["kmeans(Tensor x, int n_clusters, int max_iter, int n_init, int seed) -> (Tensor, Tensor, Tensor)","kmeans_predict(Tensor x, Tensor centers) -> Tensor","knn_predict(Tensor x_train, Tensor y_train, Tensor x_query, int n_neighbors, bool classify) -> Tensor","gaussian_nb_fit(Tensor x, Tensor y) -> (Tensor, Tensor, Tensor, Tensor)","gaussian_nb_predict(Tensor x, Tensor means, Tensor variances, Tensor priors, Tensor classes) -> Tensor","elastic_net(Tensor x, Tensor y, float alpha, float l1_ratio, int max_iter, float tol, bool fit_intercept) -> (Tensor, Tensor)","decision_tree_fit(Tensor x, Tensor y, int max_depth, int min_split, int min_leaf, int max_features, bool classify, int seed) -> (Tensor, Tensor, Tensor, Tensor, Tensor)","decision_tree_predict(Tensor x, Tensor feature, Tensor threshold, Tensor left, Tensor right, Tensor value) -> Tensor"],ZM=!1;function Fy(){if(ZM)return;ZM=!0;let n=new Vt("mlc","DEF");for(let e of bV)n.def(e)}l(Fy,"ensureMlSchemas");function xr(n){let e=(n??2654435769)>>>0;return l(function(){e|=0,e=e+1831565813|0;let r=Math.imul(e^e>>>15,1|e);return r=r+Math.imul(r^r>>>7,61|r)^r,((r^r>>>14)>>>0)/4294967296},"next")}l(xr,"makeRng");function nc(n,e){return Math.floor(n()*e)}l(nc,"randInt");function gS(n,e){let t=Array.from({length:n},(r,s)=>s);for(let r=n-1;r>0;r--){let s=nc(e,r+1),o=t[r];t[r]=t[s],t[s]=o}return t}l(gS,"shuffledIndices");function yS(n,e,t,r,s){let o=0;for(let i=0;i<s;i++){let a=n[e*s+i]-t[r*s+i];o+=a*a}return o}l(yS,"sqDist");function _S(n,e,t,r,s){let o=new Float64Array(r*t),i=nc(s,e);for(let u=0;u<t;u++)o[u]=n[i*t+u];let a=new Float64Array(e).fill(1/0);for(let u=1;u<r;u++){let p=0;for(let d=0;d<e;d++){let h=yS(n,d,o,u-1,t);h<a[d]&&(a[d]=h),p+=a[d]}let c=s()*p,f=e-1;for(let d=0;d<e;d++)if(c-=a[d],c<=0){f=d;break}for(let d=0;d<t;d++)o[u*t+d]=n[f*t+d]}return o}l(_S,"kmeansPlusPlus");function QM(n,e,t,r,s,o){let i=0;for(let a=0;a<e;a++){let u=0,p=1/0;for(let c=0;c<s;c++){let f=yS(n,a,r,c,t);f<p&&(p=f,u=c)}o[a]=u,i+=p}return i}l(QM,"assign");function bS(n,e,t,r,s,o){let i=new Float64Array(r*t),a=new Int32Array(r);for(let p=0;p<e;p++){let c=s[p];a[c]++;for(let f=0;f<t;f++)i[c*t+f]+=n[p*t+f]}let u=!1;for(let p=0;p<r;p++)if(a[p]!==0)for(let c=0;c<t;c++){let f=i[p*t+c]/a[p];f!==o[p*t+c]&&(u=!0),o[p*t+c]=f}return u}l(bS,"updateCenters");function wV(n,e,t,r,s,o){let i=_S(n,e,t,r,o),a=new Int32Array(e),u=1/0;for(let p=0;p<s&&(u=QM(n,e,t,i,r,a),!!bS(n,e,t,r,a,i));p++);return u=QM(n,e,t,i,r,a),{centers:i,labels:a,inertia:u}}l(wV,"lloyd");function zy(n,e,t,r,s,o){let{data:i,rows:a,cols:u}=Ye(e),p=null;for(let f=0;f<s;f++){let d=xr(o+f*2654435769),h=wV(i,a,u,t,r,d);(!p||h.inertia<p.inertia)&&(p=h)}let c=new Float64Array(a);for(let f=0;f<a;f++)c[f]=p.labels[f];return[Q(p.centers,[t,u],e.dtype,e.device),Q(c,[a],e.dtype,e.device),Q(new Float64Array([p.inertia]),[1],e.dtype,e.device)]}l(zy,"cpuKmeans");function Vy(n,e,t){let{data:r,rows:s,cols:o}=Ye(e),i=Ye(t),a=new Float64Array(s);for(let u=0;u<s;u++){let p=0,c=1/0;for(let f=0;f<i.rows;f++){let d=yS(r,u,i.data,f,o);d<c&&(c=d,p=f)}a[u]=p}return Q(a,[s],e.dtype,e.device)}l(Vy,"cpuKmeansPredict");function wS(n,e,t,r){for(let s=0;s<t;s++)e[s]=s;for(let s=0;s<r;s++){let o=s;for(let a=s+1;a<t;a++)n[e[a]]<n[e[o]]&&(o=a);let i=e[s];e[s]=e[o],e[o]=i}}l(wS,"topKSelect");function TS(n,e,t,r){if(r){let o=new Map,i=n[e[0]],a=0;for(let u=0;u<t;u++){let p=n[e[u]],c=(o.get(p)||0)+1;o.set(p,c),c>a&&(a=c,i=p)}return i}let s=0;for(let o=0;o<t;o++)s+=n[e[o]];return s/t}l(TS,"knnVote");function Gy(n,e,t,r,s,o){let i=Ye(e),a=Ye(r),u=$t(t),p=i.cols,c=Math.min(s,i.rows),f=new Float64Array(a.rows),d=new Float64Array(i.rows),h=new Int32Array(i.rows);for(let m=0;m<a.rows;m++){for(let g=0;g<i.rows;g++){let y=0;for(let _=0;_<p;_++){let b=a.data[m*p+_]-i.data[g*p+_];y+=b*b}d[g]=y}wS(d,h,i.rows,c),f[m]=TS(u.data,h,c,o)}return Q(f,[a.rows],r.dtype,r.device)}l(Gy,"cpuKnnPredict");be();os();function ks(n,e){let t=Wt(Int32Array.from(e),{shape:[e.length],dtype:"i32",device:n.device});return ct("index_select",n,t,0)}l(ks,"takeRows");function rc(n){return Ye(n)}l(rc,"matrixOf");function Yt(n){let e=$t(n);if(!e.wasVector&&e.cols!==1)throw new Error("ml: expected a 1-D target or single-column matrix");return{data:e.data,n:e.rows}}l(Yt,"vectorOf");function kf(n,e,t,r){return Q(n,[e,t],r??"f32")}l(kf,"matrix");function xS(n,e,t){return Q(n,[e],t??"f32")}l(xS,"vector");function Ns(n,e){let t=[],r=new Map,s=new Int32Array(e);for(let o=0;o<e;o++){let i=n[o],a=r.get(i);a===void 0&&(a=t.length,r.set(i,a),t.push(i)),s[o]=a}return{y:s,classes:t}}l(Ns,"encodeLabels");var TV=1e-9;function XM(n,e,t){let{data:r,rows:s,cols:o}=Ye(e),i=$t(t),{y:a,classes:u}=Ns(i.data,i.rows),p=u.length,c=new Float64Array(p*o),f=new Float64Array(p*o),d=new Float64Array(p),h=new Int32Array(p);for(let _=0;_<s;_++){let b=a[_];h[b]++;for(let w=0;w<o;w++)c[b*o+w]+=r[_*o+w]}for(let _=0;_<p;_++){for(let b=0;b<o;b++)c[_*o+b]/=h[_];d[_]=h[_]/s}for(let _=0;_<s;_++){let b=a[_];for(let w=0;w<o;w++){let x=r[_*o+w]-c[b*o+w];f[b*o+w]+=x*x}}let m=0;for(let _=0;_<p;_++)for(let b=0;b<o;b++)f[_*o+b]/=h[_],f[_*o+b]>m&&(m=f[_*o+b]);let g=TV*m;for(let _=0;_<p*o;_++)f[_]+=g;let y=new Float64Array(p);for(let _=0;_<p;_++)y[_]=u[_];return[Q(c,[p,o],e.dtype,e.device),Q(f,[p,o],e.dtype,e.device),Q(d,[p],e.dtype,e.device),Q(y,[p],e.dtype,e.device)]}l(XM,"cpuGaussianNbFit");function eI(n,e,t,r,s,o){let{data:i,rows:a,cols:u}=Ye(e),p=Ye(t),c=Ye(r),f=$t(s),d=$t(o),h=p.rows,m=new Float64Array(a);for(let g=0;g<a;g++){let y=0,_=-1/0;for(let b=0;b<h;b++){let w=Math.log(f.data[b]);for(let x=0;x<u;x++){let S=c.data[b*u+x],T=i[g*u+x]-p.data[b*u+x];w+=-.5*(Math.log(2*Math.PI*S)+T*T/S)}w>_&&(_=w,y=b)}m[g]=d.data[y]}return Q(m,[a],e.dtype,e.device)}l(eI,"cpuGaussianNbPredict");function xV(n,e){return n>e?n-e:n<-e?n+e:0}l(xV,"softThreshold");function SS(n,e,t,r,s){let o=new Float64Array(t),i=0;if(s){for(let c=0;c<t;c++){let f=0;for(let d=0;d<e;d++)f+=n[d*t+c];o[c]=f/e}for(let c=0;c<e;c++)i+=r[c];i/=e}let a=new Float64Array(e*t);for(let c=0;c<e;c++)for(let f=0;f<t;f++)a[c*t+f]=n[c*t+f]-o[f];let u=new Float64Array(e);for(let c=0;c<e;c++)u[c]=r[c]-i;let p=new Float64Array(t);for(let c=0;c<t;c++){let f=0;for(let d=0;d<e;d++)f+=a[d*t+c]*a[d*t+c];p[c]=f/e}return{xc:a,r:u,z:p,meanX:o,meanY:i}}l(SS,"elasticNetPrep");function vS(n,e,t,r){let s=e;for(let o=0;o<r;o++)s-=n[o]*t[o];return s}l(vS,"elasticNetIntercept");function SV(n,e,t,r,s,o,i,a,u,p){for(let c=0;c<u;c++){let f=0;for(let d=0;d<t;d++){if(s[d]===0)continue;let h=0;for(let _=0;_<e;_++)h+=n[_*t+d]*r[_];let m=h/e+o[d]*s[d],g=xV(m,i)/(s[d]+a),y=g-o[d];if(y!==0){for(let _=0;_<e;_++)r[_]-=y*n[_*t+d];o[d]=g,Math.abs(y)>f&&(f=Math.abs(y))}}if(f<p)break}}l(SV,"coordinateDescent");function jy(n,e,t,r,s,o,i,a){let{data:u,rows:p,cols:c}=Ye(e),f=$t(t),{xc:d,r:h,z:m,meanX:g,meanY:y}=SS(u,p,c,f.data,a),_=new Float64Array(c),b=r*s,w=r*(1-s);SV(d,p,c,h,m,_,b,w,o,i);let x=vS(g,y,_,c);return[Q(_,[c],e.dtype,e.device),Q(new Float64Array([x]),[1],e.dtype,e.device)]}l(jy,"cpuElasticNet");function vV(n,e,t){if(e<=0||e>=n)return Array.from({length:n},(s,o)=>o);let r=Array.from({length:n},(s,o)=>o);for(let s=n-1;s>n-1-e;s--){let o=Math.floor(t()*(s+1)),i=r[s];r[s]=r[o],r[o]=i}return r.slice(n-e)}l(vV,"chooseFeatures");function kV(n,e,t,r,s,o,i){let a=t.length,u=null;for(let p of r){let c=t.slice().sort((m,g)=>n[m*e+p]-n[g*e+p]),f=new Float64Array(s),d=new Float64Array(s);for(let m of c)d[o[m]]++;let h=0;for(let m=0;m<a-1;m++){let g=o[c[m]];f[g]++,d[g]--,h++;let y=a-h,_=n[c[m]*e+p],b=n[c[m+1]*e+p];if(_===b||h<i||y<i)continue;let w=1,x=1;for(let T=0;T<s;T++){let v=f[T]/h,A=d[T]/y;w-=v*v,x-=A*A}let S=(h*w+y*x)/a;(!u||S<u.impurity)&&(u={feature:p,threshold:(_+b)/2,impurity:S})}}return u}l(kV,"bestSplitClassify");function NV(n,e,t,r,s,o){let i=t.length,a=null;for(let u of r){let p=t.slice().sort((g,y)=>n[g*e+u]-n[y*e+u]),c=0,f=0;for(let g of p)c+=s[g],f+=s[g]*s[g];let d=0,h=0,m=0;for(let g=0;g<i-1;g++){let y=s[p[g]];d+=y,h+=y*y,m++;let _=i-m,b=n[p[g]*e+u],w=n[p[g+1]*e+u];if(b===w||m<o||_<o)continue;let x=h-d*d/m,S=c-d,v=f-h-S*S/_,A=x+v;(!a||A<a.impurity)&&(a={feature:u,threshold:(b+w)/2,impurity:A})}}return a}l(NV,"bestSplitRegress");function tI(n,e,t,r,s,o,i,a,u){let p=Ye(e),c=p.data,f=p.cols,d=$t(t),h=xr(u),m=null,g=null,y=0;if(a){let N=Ns(d.data,d.rows);m=N.y,g=N.classes,y=g.length}let _=[],b=[],w=[],x=[],S=[],T=l(N=>{if(a){let O=new Float64Array(y);for(let B of N)O[m[B]]++;let R=0;for(let B=1;B<y;B++)O[B]>O[R]&&(R=B);return g[R]}let k=0;for(let O of N)k+=d.data[O];return k/N.length},"leafValue"),v=l(N=>{let k=a?m:d.data,O=k[N[0]];for(let R of N)if(k[R]!==O)return!1;return!0},"isPure"),A=l((N,k)=>{let O=_.length;if(_.push(-1),b.push(0),w.push(-1),x.push(-1),S.push(0),k>=r||N.length<s||v(N))return S[O]=T(N),O;let R=vV(f,i,h),B=a?kV(c,f,N,R,y,m,o):NV(c,f,N,R,d.data,o);if(!B)return S[O]=T(N),O;let E=[],F=[];for(let C of N)c[C*f+B.feature]<=B.threshold?E.push(C):F.push(C);return _[O]=B.feature,b[O]=B.threshold,w[O]=A(E,k+1),x[O]=A(F,k+1),O},"build");return A(Array.from({length:p.rows},(N,k)=>k),0),[Q(Float64Array.from(_),[_.length],e.dtype,e.device),Q(Float64Array.from(b),[b.length],e.dtype,e.device),Q(Float64Array.from(w),[w.length],e.dtype,e.device),Q(Float64Array.from(x),[x.length],e.dtype,e.device),Q(Float64Array.from(S),[S.length],e.dtype,e.device)]}l(tI,"cpuDecisionTreeFit");function nI(n,e,t,r,s,o,i){let a=Ye(e),u=$t(t).data,p=$t(r).data,c=$t(s).data,f=$t(o).data,d=$t(i).data,h=new Float64Array(a.rows),m=a.cols;for(let g=0;g<a.rows;g++){let y=0;for(;u[y]!==-1;)a.data[g*m+u[y]]<=p[y]?y=c[y]:y=f[y];h[g]=d[y]}return Q(h,[a.rows],e.dtype,e.device)}l(nI,"cpuDecisionTreePredict");var AV={kmeans:zy,kmeans_predict:Vy,knn_predict:Gy,gaussian_nb_fit:XM,gaussian_nb_predict:eI,elastic_net:jy,decision_tree_fit:tI,decision_tree_predict:nI},OV=[0,2],BV=[1,5];function RV(n){return()=>{throw new Error(`ml.${n}: scalar-iterative algorithm runs on CPU/WASM only; GPU/WebGPU not supported (no performance benefit)`)}}l(RV,"unsupported");var rI=!1;function sI(){if(rI)return;rI=!0,Fy();let n=new Vt("mlc","IMPL");for(let[e,t]of Object.entries(AV)){for(let r of OV)n.impl(e,r,t);for(let r of BV)n.impl(e,r,RV(e))}}l(sI,"registerCpuMl");ys();An();ys();var MV=["qr(Tensor input) -> (Tensor, Tensor)","fft(Tensor input) -> Tensor","ifft(Tensor input) -> Tensor"],oI=!1;function iI(){if(oI)return;oI=!0;let n=new Vt("mlc","DEF");for(let e of MV)n.def(e)}l(iI,"ensureNumericSchemas");wn();function aI(n,e,t){let r=Math.min(e,t),s=Float64Array.from(n),o=[];for(let u=0;u<r;u++){let p=0;for(let h=u;h<e;h++)p+=s[h*t+u]*s[h*t+u];if(p=Math.sqrt(p),p===0){o.push(null);continue}let c=s[u*t+u]>=0?-p:p,f=new Float64Array(e-u);f[0]=s[u*t+u]-c;for(let h=u+1;h<e;h++)f[h-u]=s[h*t+u];let d=0;for(let h=0;h<f.length;h++)d+=f[h]*f[h];if(d===0){o.push(null);continue}o.push(f);for(let h=u;h<t;h++){let m=0;for(let y=0;y<f.length;y++)m+=f[y]*s[(u+y)*t+h];let g=2*m/d;for(let y=0;y<f.length;y++)s[(u+y)*t+h]-=g*f[y]}}let i=new Float64Array(e*r);for(let u=0;u<r;u++)i[u*r+u]=1;for(let u=r-1;u>=0;u--){let p=o[u];if(!p)continue;let c=0;for(let f=0;f<p.length;f++)c+=p[f]*p[f];for(let f=0;f<r;f++){let d=0;for(let m=0;m<p.length;m++)d+=p[m]*i[(u+m)*r+f];let h=2*d/c;for(let m=0;m<p.length;m++)i[(u+m)*r+f]-=h*p[m]}}let a=new Float64Array(r*t);for(let u=0;u<r;u++)for(let p=u;p<t;p++)a[u*t+p]=s[u*t+p];return{Q:i,R:a,k:r}}l(aI,"qrHost");function IV(n,e){let t=n.length;for(let r=1,s=0;r<t;r++){let o=t>>1;for(;s&o;o>>=1)s^=o;s^=o,r<s&&([n[r],n[s]]=[n[s],n[r]],[e[r],e[s]]=[e[s],e[r]])}}l(IV,"bitReversePermute");function qy(n,e,t){let r=n.length;IV(n,e);for(let s=2;s<=r;s<<=1){let o=(t?1:-1)*2*Math.PI/s,i=Math.cos(o),a=Math.sin(o);for(let u=0;u<r;u+=s){let p=1,c=0,f=s>>1;for(let d=0;d<f;d++){let h=n[u+d],m=e[u+d],g=n[u+d+f]*p-e[u+d+f]*c,y=n[u+d+f]*c+e[u+d+f]*p;n[u+d]=h+g,e[u+d]=m+y,n[u+d+f]=h-g,e[u+d+f]=m-y;let _=p*i-c*a;c=p*a+c*i,p=_}}}if(t)for(let s=0;s<r;s++)n[s]/=r,e[s]/=r}l(qy,"fftRadix2");function EV(n){let e=1;for(;e<n;)e<<=1;return e}l(EV,"nextPow2");function LV(n,e,t){let r=n.length,s=EV(2*r-1),o=t?1:-1,i=new Float64Array(r),a=new Float64Array(r);for(let d=0;d<r;d++){let h=d*d%(2*r),m=o*Math.PI*h/r;i[d]=Math.cos(m),a[d]=Math.sin(m)}let u=new Float64Array(s),p=new Float64Array(s);for(let d=0;d<r;d++)u[d]=n[d]*i[d]-e[d]*a[d],p[d]=n[d]*a[d]+e[d]*i[d];let c=new Float64Array(s),f=new Float64Array(s);c[0]=i[0],f[0]=-a[0];for(let d=1;d<r;d++)c[d]=i[d],f[d]=-a[d],c[s-d]=i[d],f[s-d]=-a[d];qy(u,p,!1),qy(c,f,!1);for(let d=0;d<s;d++){let h=u[d]*c[d]-p[d]*f[d];p[d]=u[d]*f[d]+p[d]*c[d],u[d]=h}qy(u,p,!0);for(let d=0;d<r;d++)n[d]=u[d]*i[d]-p[d]*a[d],e[d]=u[d]*a[d]+p[d]*i[d];if(t)for(let d=0;d<r;d++)n[d]/=r,e[d]/=r}l(LV,"bluestein");function uI(n,e,t){let r=n.length;r!==0&&((r&r-1)===0?qy(n,e,t):LV(n,e,t))}l(uI,"fftHost");function lI(n,e){let{data:t,rows:r,cols:s}=Ye(e),{Q:o,R:i,k:a}=aI(t,r,s);return[Q(o,[r,a],e.dtype,e.device),Q(i,[a,s],e.dtype,e.device)]}l(lI,"cpuQr");function CV(n){let e=Qe(n);if(n.ndim===1){let t=n.shape[0];return{re:Float64Array.from(e),im:new Float64Array(t),n:t}}if(n.ndim===2&&n.shape[1]===2){let t=n.shape[0],r=new Float64Array(t),s=new Float64Array(t);for(let o=0;o<t;o++)r[o]=e[2*o],s[o]=e[2*o+1];return{re:r,im:s,n:t}}throw new Error(`fft: expected a 1-D real signal or [n, 2] complex tensor, got shape [${n.shape}]`)}l(CV,"splitComplex");function DV(n,e,t){let r=new Float64Array(2*t);for(let s=0;s<t;s++)r[2*s]=n[s],r[2*s+1]=e[s];return r}l(DV,"interleave");function cI(n,e){let{re:t,im:r,n:s}=CV(n);return uI(t,r,e),Q(DV(t,r,s),[s,2],n.dtype,n.device)}l(cI,"transform");function pI(n,e){return cI(e,!1)}l(pI,"cpuFft");function fI(n,e){return cI(e,!0)}l(fI,"cpuIfft");var PV={qr:lI,fft:pI,ifft:fI},$V=[0,2],dI=!1;function hI(){if(dI)return;dI=!0,iI();let n=new Vt("mlc","IMPL");for(let[e,t]of Object.entries(PV))for(let r of $V)n.impl(e,r,t)}l(hI,"registerCpuNumeric");ys();An();ey();Q0();var pt=8,NS=4,sc=2,FV=sc*pt,gu=Math.log2(sc),zV=65536,mI=FV,VV=64*1024*1024;function gI(n,e){return Math.ceil(n/e)*e}l(gI,"alignUp");var kS=class{static{l(this,"SimdModule")}constructor(e,t){this._module=e,this._exportName=t,this._instantiate()}_instantiate(){this._instance=new WebAssembly.Instance(this._module,{}),this._memory=this._instance.exports.memory,this._fn=this._instance.exports[this._exportName],this._top=0}reset(){this._top=0}reclaim(){this._memory.buffer.byteLength>VV?this._instantiate():this._top=0}_ensure(e){let t=this._memory.buffer.byteLength;e<=t||this._memory.grow(Math.ceil((e-t)/zV))}alloc(e){let t=gI(this._top,mI);return this._top=t+gI(e,mI),this._ensure(this._top),t}allocF64(e){return this.alloc(e*pt)}allocI32(e){return this.alloc(e*NS)}f64(e,t){return new Float64Array(this._memory.buffer,e,t)}i32(e,t){return new Int32Array(this._memory.buffer,e,t)}writeF64(e,t){this.f64(e,t.length).set(t)}run(...e){this._fn(...e)}},yI=new Map;function Po(n,e,t){let r=yI.get(n);return r||(r=new kS(new WebAssembly.Module(Wm(e())),t),yI.set(n,r)),r.reclaim(),r}l(Po,"simdModule");var GV=1e308,jV=1,qV=32768;function Nf(n,e,t,r){let s=e.map(([i,a])=>`(param $${i} ${a})`).join(" "),o=t.map(([i,a])=>`(local $${i} ${a})`).join(" ");return`(module (memory (export "memory") ${jV} ${qV})
|