diff --git "a/assets/index-D3DuJDE3.js" "b/assets/index-D3DuJDE3.js" new file mode 100644--- /dev/null +++ "b/assets/index-D3DuJDE3.js" @@ -0,0 +1,2883 @@ +var $v=Object.defineProperty;var Sv=(e,t,n)=>t in e?$v(e,t,{enumerable:!0,configurable:!0,writable:!0,value:n}):e[t]=n;var kc=(e,t,n)=>Sv(e,typeof t!="symbol"?t+"":t,n);(function(){const t=document.createElement("link").relList;if(t&&t.supports&&t.supports("modulepreload"))return;for(const i of document.querySelectorAll('link[rel="modulepreload"]'))r(i);new MutationObserver(i=>{for(const s of i)if(s.type==="childList")for(const a of s.addedNodes)a.tagName==="LINK"&&a.rel==="modulepreload"&&r(a)}).observe(document,{childList:!0,subtree:!0});function n(i){const s={};return i.integrity&&(s.integrity=i.integrity),i.referrerPolicy&&(s.referrerPolicy=i.referrerPolicy),i.crossOrigin==="use-credentials"?s.credentials="include":i.crossOrigin==="anonymous"?s.credentials="omit":s.credentials="same-origin",s}function r(i){if(i.ep)return;i.ep=!0;const s=n(i);fetch(i.href,s)}})();/** +* @vue/shared v3.5.29 +* (c) 2018-present Yuxi (Evan) You and Vue contributors +* @license MIT +**/function _u(e){const t=Object.create(null);for(const n of e.split(","))t[n]=1;return n=>n in t}const it={},wi=[],Mn=()=>{},pg=()=>!1,Ka=e=>e.charCodeAt(0)===111&&e.charCodeAt(1)===110&&(e.charCodeAt(2)>122||e.charCodeAt(2)<97),vu=e=>e.startsWith("onUpdate:"),At=Object.assign,xu=(e,t)=>{const n=e.indexOf(t);n>-1&&e.splice(n,1)},Tv=Object.prototype.hasOwnProperty,Je=(e,t)=>Tv.call(e,t),Ie=Array.isArray,_i=e=>Rs(e)==="[object Map]",zi=e=>Rs(e)==="[object Set]",zc=e=>Rs(e)==="[object Date]",Me=e=>typeof e=="function",_t=e=>typeof e=="string",On=e=>typeof e=="symbol",nt=e=>e!==null&&typeof e=="object",fg=e=>(nt(e)||Me(e))&&Me(e.then)&&Me(e.catch),hg=Object.prototype.toString,Rs=e=>hg.call(e),Iv=e=>Rs(e).slice(8,-1),mg=e=>Rs(e)==="[object Object]",$u=e=>_t(e)&&e!=="NaN"&&e[0]!=="-"&&""+parseInt(e,10)===e,ms=_u(",key,ref,ref_for,ref_key,onVnodeBeforeMount,onVnodeMounted,onVnodeBeforeUpdate,onVnodeUpdated,onVnodeBeforeUnmount,onVnodeUnmounted"),Xa=e=>{const t=Object.create(null);return(n=>t[n]||(t[n]=e(n)))},Cv=/-\w/g,br=Xa(e=>e.replace(Cv,t=>t.slice(1).toUpperCase())),Ev=/\B([A-Z])/g,Jr=Xa(e=>e.replace(Ev,"-$1").toLowerCase()),gg=Xa(e=>e.charAt(0).toUpperCase()+e.slice(1)),Mo=Xa(e=>e?`on${gg(e)}`:""),yr=(e,t)=>!Object.is(e,t),Sa=(e,...t)=>{for(let n=0;n{Object.defineProperty(e,t,{configurable:!0,enumerable:!1,writable:r,value:n})},Ya=e=>{const t=parseFloat(e);return isNaN(t)?e:t};let Ac;const Za=()=>Ac||(Ac=typeof globalThis<"u"?globalThis:typeof self<"u"?self:typeof window<"u"?window:typeof global<"u"?global:{});function Fr(e){if(Ie(e)){const t={};for(let n=0;n{if(n){const r=n.split(zv);r.length>1&&(t[r[0].trim()]=r[1].trim())}}),t}function Os(e){let t="";if(_t(e))t=e;else if(Ie(e))for(let n=0;nAi(n,t))}const wg=e=>!!(e&&e.__v_isRef===!0),wt=e=>_t(e)?e:e==null?"":Ie(e)||nt(e)&&(e.toString===hg||!Me(e.toString))?wg(e)?wt(e.value):JSON.stringify(e,_g,2):String(e),_g=(e,t)=>wg(t)?_g(e,t.value):_i(t)?{[`Map(${t.size})`]:[...t.entries()].reduce((n,[r,i],s)=>(n[Ro(r,s)+" =>"]=i,n),{})}:zi(t)?{[`Set(${t.size})`]:[...t.values()].map(n=>Ro(n))}:On(t)?Ro(t):nt(t)&&!Ie(t)&&!mg(t)?String(t):t,Ro=(e,t="")=>{var n;return On(e)?`Symbol(${(n=e.description)!=null?n:t})`:e};/** +* @vue/reactivity v3.5.29 +* (c) 2018-present Yuxi (Evan) You and Vue contributors +* @license MIT +**/let Ft;class Bv{constructor(t=!1){this.detached=t,this._active=!0,this._on=0,this.effects=[],this.cleanups=[],this._isPaused=!1,this.__v_skip=!0,this.parent=Ft,!t&&Ft&&(this.index=(Ft.scopes||(Ft.scopes=[])).push(this)-1)}get active(){return this._active}pause(){if(this._active){this._isPaused=!0;let t,n;if(this.scopes)for(t=0,n=this.scopes.length;t0&&--this._on===0&&(Ft=this.prevScope,this.prevScope=void 0)}stop(t){if(this._active){this._active=!1;let n,r;for(n=0,r=this.effects.length;n0)return;if(ys){let t=ys;for(ys=void 0;t;){const n=t.next;t.next=void 0,t.flags&=-9,t=n}}let e;for(;gs;){let t=gs;for(gs=void 0;t;){const n=t.next;if(t.next=void 0,t.flags&=-9,t.flags&1)try{t.trigger()}catch(r){e||(e=r)}t=n}}if(e)throw e}function Sg(e){for(let t=e.deps;t;t=t.nextDep)t.version=-1,t.prevActiveLink=t.dep.activeLink,t.dep.activeLink=t}function Tg(e){let t,n=e.depsTail,r=n;for(;r;){const i=r.prevDep;r.version===-1?(r===n&&(n=i),Cu(r),Pv(r)):t=r,r.dep.activeLink=r.prevActiveLink,r.prevActiveLink=void 0,r=i}e.deps=t,e.depsTail=n}function Hl(e){for(let t=e.deps;t;t=t.nextDep)if(t.dep.version!==t.version||t.dep.computed&&(Ig(t.dep.computed)||t.dep.version!==t.version))return!0;return!!e._dirty}function Ig(e){if(e.flags&4&&!(e.flags&16)||(e.flags&=-17,e.globalVersion===Ts)||(e.globalVersion=Ts,!e.isSSR&&e.flags&128&&(!e.deps&&!e._dirty||!Hl(e))))return;e.flags|=2;const t=e.dep,n=at,r=bn;at=e,bn=!0;try{Sg(e);const i=e.fn(e._value);(t.version===0||yr(i,e._value))&&(e.flags|=128,e._value=i,t.version++)}catch(i){throw t.version++,i}finally{at=n,bn=r,Tg(e),e.flags&=-3}}function Cu(e,t=!1){const{dep:n,prevSub:r,nextSub:i}=e;if(r&&(r.nextSub=i,e.prevSub=void 0),i&&(i.prevSub=r,e.nextSub=void 0),n.subs===e&&(n.subs=r,!r&&n.computed)){n.computed.flags&=-5;for(let s=n.computed.deps;s;s=s.nextDep)Cu(s,!0)}!t&&!--n.sc&&n.map&&n.map.delete(n.key)}function Pv(e){const{prevDep:t,nextDep:n}=e;t&&(t.nextDep=n,e.prevDep=void 0),n&&(n.prevDep=t,e.nextDep=void 0)}let bn=!0;const Cg=[];function Qn(){Cg.push(bn),bn=!1}function Jn(){const e=Cg.pop();bn=e===void 0?!0:e}function Mc(e){const{cleanup:t}=e;if(e.cleanup=void 0,t){const n=at;at=void 0;try{t()}finally{at=n}}}let Ts=0;class Uv{constructor(t,n){this.sub=t,this.dep=n,this.version=n.version,this.nextDep=this.prevDep=this.nextSub=this.prevSub=this.prevActiveLink=void 0}}class Eu{constructor(t){this.computed=t,this.version=0,this.activeLink=void 0,this.subs=void 0,this.map=void 0,this.key=void 0,this.sc=0,this.__v_skip=!0}track(t){if(!at||!bn||at===this.computed)return;let n=this.activeLink;if(n===void 0||n.sub!==at)n=this.activeLink=new Uv(at,this),at.deps?(n.prevDep=at.depsTail,at.depsTail.nextDep=n,at.depsTail=n):at.deps=at.depsTail=n,Eg(n);else if(n.version===-1&&(n.version=this.version,n.nextDep)){const r=n.nextDep;r.prevDep=n.prevDep,n.prevDep&&(n.prevDep.nextDep=r),n.prevDep=at.depsTail,n.nextDep=void 0,at.depsTail.nextDep=n,at.depsTail=n,at.deps===n&&(at.deps=r)}return n}trigger(t){this.version++,Ts++,this.notify(t)}notify(t){Tu();try{for(let n=this.subs;n;n=n.prevSub)n.sub.notify()&&n.sub.dep.notify()}finally{Iu()}}}function Eg(e){if(e.dep.sc++,e.sub.flags&4){const t=e.dep.computed;if(t&&!e.dep.subs){t.flags|=20;for(let r=t.deps;r;r=r.nextDep)Eg(r)}const n=e.dep.subs;n!==e&&(e.prevSub=n,n&&(n.nextSub=e)),e.dep.subs=e}}const jl=new WeakMap,Hr=Symbol(""),Kl=Symbol(""),Is=Symbol("");function Ot(e,t,n){if(bn&&at){let r=jl.get(e);r||jl.set(e,r=new Map);let i=r.get(n);i||(r.set(n,i=new Eu),i.map=r,i.key=n),i.track()}}function Kn(e,t,n,r,i,s){const a=jl.get(e);if(!a){Ts++;return}const o=l=>{l&&l.trigger()};if(Tu(),t==="clear")a.forEach(o);else{const l=Ie(e),d=l&&$u(n);if(l&&n==="length"){const p=Number(r);a.forEach((f,c)=>{(c==="length"||c===Is||!On(c)&&c>=p)&&o(f)})}else switch((n!==void 0||a.has(void 0))&&o(a.get(n)),d&&o(a.get(Is)),t){case"add":l?d&&o(a.get("length")):(o(a.get(Hr)),_i(e)&&o(a.get(Kl)));break;case"delete":l||(o(a.get(Hr)),_i(e)&&o(a.get(Kl)));break;case"set":_i(e)&&o(a.get(Hr));break}}Iu()}function ci(e){const t=Qe(e);return t===e?t:(Ot(t,"iterate",Is),an(e)?t:t.map(_n))}function Qa(e){return Ot(e=Qe(e),"iterate",Is),e}function hr(e,t){return er(e)?Si(jr(e)?_n(t):t):_n(t)}const Lv={__proto__:null,[Symbol.iterator](){return No(this,Symbol.iterator,e=>hr(this,e))},concat(...e){return ci(this).concat(...e.map(t=>Ie(t)?ci(t):t))},entries(){return No(this,"entries",e=>(e[1]=hr(this,e[1]),e))},every(e,t){return Vn(this,"every",e,t,void 0,arguments)},filter(e,t){return Vn(this,"filter",e,t,n=>n.map(r=>hr(this,r)),arguments)},find(e,t){return Vn(this,"find",e,t,n=>hr(this,n),arguments)},findIndex(e,t){return Vn(this,"findIndex",e,t,void 0,arguments)},findLast(e,t){return Vn(this,"findLast",e,t,n=>hr(this,n),arguments)},findLastIndex(e,t){return Vn(this,"findLastIndex",e,t,void 0,arguments)},forEach(e,t){return Vn(this,"forEach",e,t,void 0,arguments)},includes(...e){return Bo(this,"includes",e)},indexOf(...e){return Bo(this,"indexOf",e)},join(e){return ci(this).join(e)},lastIndexOf(...e){return Bo(this,"lastIndexOf",e)},map(e,t){return Vn(this,"map",e,t,void 0,arguments)},pop(){return Yi(this,"pop")},push(...e){return Yi(this,"push",e)},reduce(e,...t){return Rc(this,"reduce",e,t)},reduceRight(e,...t){return Rc(this,"reduceRight",e,t)},shift(){return Yi(this,"shift")},some(e,t){return Vn(this,"some",e,t,void 0,arguments)},splice(...e){return Yi(this,"splice",e)},toReversed(){return ci(this).toReversed()},toSorted(e){return ci(this).toSorted(e)},toSpliced(...e){return ci(this).toSpliced(...e)},unshift(...e){return Yi(this,"unshift",e)},values(){return No(this,"values",e=>hr(this,e))}};function No(e,t,n){const r=Qa(e),i=r[t]();return r!==e&&!an(e)&&(i._next=i.next,i.next=()=>{const s=i._next();return s.done||(s.value=n(s.value)),s}),i}const Wv=Array.prototype;function Vn(e,t,n,r,i,s){const a=Qa(e),o=a!==e&&!an(e),l=a[t];if(l!==Wv[t]){const f=l.apply(e,s);return o?_n(f):f}let d=n;a!==e&&(o?d=function(f,c){return n.call(this,hr(e,f),c,e)}:n.length>2&&(d=function(f,c){return n.call(this,f,c,e)}));const p=l.call(a,d,r);return o&&i?i(p):p}function Rc(e,t,n,r){const i=Qa(e);let s=n;return i!==e&&(an(e)?n.length>3&&(s=function(a,o,l){return n.call(this,a,o,l,e)}):s=function(a,o,l){return n.call(this,a,hr(e,o),l,e)}),i[t](s,...r)}function Bo(e,t,n){const r=Qe(e);Ot(r,"iterate",Is);const i=r[t](...n);return(i===-1||i===!1)&&Au(n[0])?(n[0]=Qe(n[0]),r[t](...n)):i}function Yi(e,t,n=[]){Qn(),Tu();const r=Qe(e)[t].apply(e,n);return Iu(),Jn(),r}const qv=_u("__proto__,__v_isRef,__isVue"),kg=new Set(Object.getOwnPropertyNames(Symbol).filter(e=>e!=="arguments"&&e!=="caller").map(e=>Symbol[e]).filter(On));function Gv(e){On(e)||(e=String(e));const t=Qe(this);return Ot(t,"has",e),t.hasOwnProperty(e)}class zg{constructor(t=!1,n=!1){this._isReadonly=t,this._isShallow=n}get(t,n,r){if(n==="__v_skip")return t.__v_skip;const i=this._isReadonly,s=this._isShallow;if(n==="__v_isReactive")return!i;if(n==="__v_isReadonly")return i;if(n==="__v_isShallow")return s;if(n==="__v_raw")return r===(i?s?Jv:Og:s?Rg:Mg).get(t)||Object.getPrototypeOf(t)===Object.getPrototypeOf(r)?t:void 0;const a=Ie(t);if(!i){let l;if(a&&(l=Lv[n]))return l;if(n==="hasOwnProperty")return Gv}const o=Reflect.get(t,n,Nt(t)?t:r);if((On(n)?kg.has(n):qv(n))||(i||Ot(t,"get",n),s))return o;if(Nt(o)){const l=a&&$u(n)?o:o.value;return i&&nt(l)?Yl(l):l}return nt(o)?i?Yl(o):$i(o):o}}class Ag extends zg{constructor(t=!1){super(!1,t)}set(t,n,r,i){let s=t[n];const a=Ie(t)&&$u(n);if(!this._isShallow){const d=er(s);if(!an(r)&&!er(r)&&(s=Qe(s),r=Qe(r)),!a&&Nt(s)&&!Nt(r))return d||(s.value=r),!0}const o=a?Number(n)e,na=e=>Reflect.getPrototypeOf(e);function Kv(e,t,n){return function(...r){const i=this.__v_raw,s=Qe(i),a=_i(s),o=e==="entries"||e===Symbol.iterator&&a,l=e==="keys"&&a,d=i[e](...r),p=n?Xl:t?Si:_n;return!t&&Ot(s,"iterate",l?Kl:Hr),At(Object.create(d),{next(){const{value:f,done:c}=d.next();return c?{value:f,done:c}:{value:o?[p(f[0]),p(f[1])]:p(f),done:c}}})}}function ra(e){return function(...t){return e==="delete"?!1:e==="clear"?void 0:this}}function Xv(e,t){const n={get(i){const s=this.__v_raw,a=Qe(s),o=Qe(i);e||(yr(i,o)&&Ot(a,"get",i),Ot(a,"get",o));const{has:l}=na(a),d=t?Xl:e?Si:_n;if(l.call(a,i))return d(s.get(i));if(l.call(a,o))return d(s.get(o));s!==a&&s.get(i)},get size(){const i=this.__v_raw;return!e&&Ot(Qe(i),"iterate",Hr),i.size},has(i){const s=this.__v_raw,a=Qe(s),o=Qe(i);return e||(yr(i,o)&&Ot(a,"has",i),Ot(a,"has",o)),i===o?s.has(i):s.has(i)||s.has(o)},forEach(i,s){const a=this,o=a.__v_raw,l=Qe(o),d=t?Xl:e?Si:_n;return!e&&Ot(l,"iterate",Hr),o.forEach((p,f)=>i.call(s,d(p),d(f),a))}};return At(n,e?{add:ra("add"),set:ra("set"),delete:ra("delete"),clear:ra("clear")}:{add(i){!t&&!an(i)&&!er(i)&&(i=Qe(i));const s=Qe(this);return na(s).has.call(s,i)||(s.add(i),Kn(s,"add",i,i)),this},set(i,s){!t&&!an(s)&&!er(s)&&(s=Qe(s));const a=Qe(this),{has:o,get:l}=na(a);let d=o.call(a,i);d||(i=Qe(i),d=o.call(a,i));const p=l.call(a,i);return a.set(i,s),d?yr(s,p)&&Kn(a,"set",i,s):Kn(a,"add",i,s),this},delete(i){const s=Qe(this),{has:a,get:o}=na(s);let l=a.call(s,i);l||(i=Qe(i),l=a.call(s,i)),o&&o.call(s,i);const d=s.delete(i);return l&&Kn(s,"delete",i,void 0),d},clear(){const i=Qe(this),s=i.size!==0,a=i.clear();return s&&Kn(i,"clear",void 0,void 0),a}}),["keys","values","entries",Symbol.iterator].forEach(i=>{n[i]=Kv(i,e,t)}),n}function ku(e,t){const n=Xv(e,t);return(r,i,s)=>i==="__v_isReactive"?!e:i==="__v_isReadonly"?e:i==="__v_raw"?r:Reflect.get(Je(n,i)&&i in r?n:r,i,s)}const Yv={get:ku(!1,!1)},Zv={get:ku(!1,!0)},Qv={get:ku(!0,!1)};const Mg=new WeakMap,Rg=new WeakMap,Og=new WeakMap,Jv=new WeakMap;function ex(e){switch(e){case"Object":case"Array":return 1;case"Map":case"Set":case"WeakMap":case"WeakSet":return 2;default:return 0}}function tx(e){return e.__v_skip||!Object.isExtensible(e)?0:ex(Iv(e))}function $i(e){return er(e)?e:zu(e,!1,Fv,Yv,Mg)}function nx(e){return zu(e,!1,jv,Zv,Rg)}function Yl(e){return zu(e,!0,Hv,Qv,Og)}function zu(e,t,n,r,i){if(!nt(e)||e.__v_raw&&!(t&&e.__v_isReactive))return e;const s=tx(e);if(s===0)return e;const a=i.get(e);if(a)return a;const o=new Proxy(e,s===2?r:n);return i.set(e,o),o}function jr(e){return er(e)?jr(e.__v_raw):!!(e&&e.__v_isReactive)}function er(e){return!!(e&&e.__v_isReadonly)}function an(e){return!!(e&&e.__v_isShallow)}function Au(e){return e?!!e.__v_raw:!1}function Qe(e){const t=e&&e.__v_raw;return t?Qe(t):e}function rx(e){return!Je(e,"__v_skip")&&Object.isExtensible(e)&&yg(e,"__v_skip",!0),e}const _n=e=>nt(e)?$i(e):e,Si=e=>nt(e)?Yl(e):e;function Nt(e){return e?e.__v_isRef===!0:!1}function bt(e){return Ng(e,!1)}function ia(e){return Ng(e,!0)}function Ng(e,t){return Nt(e)?e:new ix(e,t)}class ix{constructor(t,n){this.dep=new Eu,this.__v_isRef=!0,this.__v_isShallow=!1,this._rawValue=n?t:Qe(t),this._value=n?t:_n(t),this.__v_isShallow=n}get value(){return this.dep.track(),this._value}set value(t){const n=this._rawValue,r=this.__v_isShallow||an(t)||er(t);t=r?t:Qe(t),yr(t,n)&&(this._rawValue=t,this._value=r?t:_n(t),this.dep.trigger())}}function We(e){return Nt(e)?e.value:e}const sx={get:(e,t,n)=>t==="__v_raw"?e:We(Reflect.get(e,t,n)),set:(e,t,n,r)=>{const i=e[t];return Nt(i)&&!Nt(n)?(i.value=n,!0):Reflect.set(e,t,n,r)}};function Bg(e){return jr(e)?e:new Proxy(e,sx)}class ax{constructor(t,n,r){this.fn=t,this.setter=n,this._value=void 0,this.dep=new Eu(this),this.__v_isRef=!0,this.deps=void 0,this.depsTail=void 0,this.flags=16,this.globalVersion=Ts-1,this.next=void 0,this.effect=this,this.__v_isReadonly=!n,this.isSSR=r}notify(){if(this.flags|=16,!(this.flags&8)&&at!==this)return $g(this,!0),!0}get value(){const t=this.dep.track();return Ig(this),t&&(t.version=this.dep.version),this._value}set value(t){this.setter&&this.setter(t)}}function ox(e,t,n=!1){let r,i;return Me(e)?r=e:(r=e.get,i=e.set),new ax(r,i,n)}const sa={},za=new WeakMap;let Wr;function lx(e,t=!1,n=Wr){if(n){let r=za.get(n);r||za.set(n,r=[]),r.push(e)}}function ux(e,t,n=it){const{immediate:r,deep:i,once:s,scheduler:a,augmentJob:o,call:l}=n,d=S=>i?S:an(S)||i===!1||i===0?Xn(S,1):Xn(S);let p,f,c,m,y=!1,w=!1;if(Nt(e)?(f=()=>e.value,y=an(e)):jr(e)?(f=()=>d(e),y=!0):Ie(e)?(w=!0,y=e.some(S=>jr(S)||an(S)),f=()=>e.map(S=>{if(Nt(S))return S.value;if(jr(S))return d(S);if(Me(S))return l?l(S,2):S()})):Me(e)?t?f=l?()=>l(e,2):e:f=()=>{if(c){Qn();try{c()}finally{Jn()}}const S=Wr;Wr=p;try{return l?l(e,3,[m]):e(m)}finally{Wr=S}}:f=Mn,t&&i){const S=f,E=i===!0?1/0:i;f=()=>Xn(S(),E)}const $=Dv(),_=()=>{p.stop(),$&&$.active&&xu($.effects,p)};if(s&&t){const S=t;t=(...E)=>{S(...E),_()}}let v=w?new Array(e.length).fill(sa):sa;const I=S=>{if(!(!(p.flags&1)||!p.dirty&&!S))if(t){const E=p.run();if(i||y||(w?E.some((R,O)=>yr(R,v[O])):yr(E,v))){c&&c();const R=Wr;Wr=p;try{const O=[E,v===sa?void 0:w&&v[0]===sa?[]:v,m];v=E,l?l(t,3,O):t(...O)}finally{Wr=R}}}else p.run()};return o&&o(I),p=new vg(f),p.scheduler=a?()=>a(I,!1):I,m=S=>lx(S,!1,p),c=p.onStop=()=>{const S=za.get(p);if(S){if(l)l(S,4);else for(const E of S)E();za.delete(p)}},t?r?I(!0):v=p.run():a?a(I.bind(null,!0),!0):p.run(),_.pause=p.pause.bind(p),_.resume=p.resume.bind(p),_.stop=_,_}function Xn(e,t=1/0,n){if(t<=0||!nt(e)||e.__v_skip||(n=n||new Map,(n.get(e)||0)>=t))return e;if(n.set(e,t),t--,Nt(e))Xn(e.value,t,n);else if(Ie(e))for(let r=0;r{Xn(r,t,n)});else if(mg(e)){for(const r in e)Xn(e[r],t,n);for(const r of Object.getOwnPropertySymbols(e))Object.prototype.propertyIsEnumerable.call(e,r)&&Xn(e[r],t,n)}return e}/** +* @vue/runtime-core v3.5.29 +* (c) 2018-present Yuxi (Evan) You and Vue contributors +* @license MIT +**/function Ns(e,t,n,r){try{return r?e(...r):e()}catch(i){Ja(i,t,n)}}function Nn(e,t,n,r){if(Me(e)){const i=Ns(e,t,n,r);return i&&fg(i)&&i.catch(s=>{Ja(s,t,n)}),i}if(Ie(e)){const i=[];for(let s=0;s>>1,i=Lt[r],s=Cs(i);s=Cs(n)?Lt.push(e):Lt.splice(cx(t),0,e),e.flags|=1,Ug()}}function Ug(){Aa||(Aa=Dg.then(Wg))}function px(e){Ie(e)?vi.push(...e):mr&&e.id===-1?mr.splice(gi+1,0,e):e.flags&1||(vi.push(e),e.flags|=1),Ug()}function Oc(e,t,n=Cn+1){for(;nCs(n)-Cs(r));if(vi.length=0,mr){mr.push(...t);return}for(mr=t,gi=0;gie.id==null?e.flags&2?-1:1/0:e.id;function Wg(e){try{for(Cn=0;Cn{r._d&&Hc(-1);const s=Ma(t);let a;try{a=e(...i)}finally{Ma(s),r._d&&Hc(1)}return a};return r._n=!0,r._c=!0,r._d=!0,r}function Ze(e,t){if(sn===null)return e;const n=so(sn),r=e.dirs||(e.dirs=[]);for(let i=0;i1)return n&&Me(t)?t.call(r&&r.proxy):t}}const mx=Symbol.for("v-scx"),gx=()=>Ta(mx);function En(e,t,n){return Gg(e,t,n)}function Gg(e,t,n=it){const{immediate:r,deep:i,flush:s,once:a}=n,o=At({},n),l=t&&r||!t&&s!=="post";let d;if(ks){if(s==="sync"){const m=gx();d=m.__watcherHandles||(m.__watcherHandles=[])}else if(!l){const m=()=>{};return m.stop=Mn,m.resume=Mn,m.pause=Mn,m}}const p=Wt;o.call=(m,y,w)=>Nn(m,p,y,w);let f=!1;s==="post"?o.scheduler=m=>{Vt(m,p&&p.suspense)}:s!=="sync"&&(f=!0,o.scheduler=(m,y)=>{y?m():Mu(m)}),o.augmentJob=m=>{t&&(m.flags|=4),f&&(m.flags|=2,p&&(m.id=p.uid,m.i=p))};const c=ux(e,t,o);return ks&&(d?d.push(c):l&&c()),c}function yx(e,t,n){const r=this.proxy,i=_t(e)?e.includes(".")?Vg(r,e):()=>r[e]:e.bind(r,r);let s;Me(t)?s=t:(s=t.handler,n=t);const a=Bs(this),o=Gg(i,s.bind(r),n);return a(),o}function Vg(e,t){const n=t.split(".");return()=>{let r=e;for(let i=0;ie.__isTeleport,_x=Symbol("_leaveCb");function Ru(e,t){e.shapeFlag&6&&e.component?(e.transition=t,Ru(e.component.subTree,t)):e.shapeFlag&128?(e.ssContent.transition=t.clone(e.ssContent),e.ssFallback.transition=t.clone(e.ssFallback)):e.transition=t}function eo(e,t){return Me(e)?At({name:e.name},t,{setup:e}):e}function Fg(e){e.ids=[e.ids[0]+e.ids[2]+++"-",0,0]}function Nc(e,t){let n;return!!((n=Object.getOwnPropertyDescriptor(e,t))&&!n.configurable)}const Ra=new WeakMap;function bs(e,t,n,r,i=!1){if(Ie(e)){e.forEach((w,$)=>bs(w,t&&(Ie(t)?t[$]:t),n,r,i));return}if(ws(r)&&!i){r.shapeFlag&512&&r.type.__asyncResolved&&r.component.subTree.component&&bs(e,t,n,r.component.subTree);return}const s=r.shapeFlag&4?so(r.component):r.el,a=i?null:s,{i:o,r:l}=e,d=t&&t.r,p=o.refs===it?o.refs={}:o.refs,f=o.setupState,c=Qe(f),m=f===it?pg:w=>Nc(p,w)?!1:Je(c,w),y=(w,$)=>!($&&Nc(p,$));if(d!=null&&d!==l){if(Bc(t),_t(d))p[d]=null,m(d)&&(f[d]=null);else if(Nt(d)){const w=t;y(d,w.k)&&(d.value=null),w.k&&(p[w.k]=null)}}if(Me(l))Ns(l,o,12,[a,p]);else{const w=_t(l),$=Nt(l);if(w||$){const _=()=>{if(e.f){const v=w?m(l)?f[l]:p[l]:y()||!e.k?l.value:p[e.k];if(i)Ie(v)&&xu(v,s);else if(Ie(v))v.includes(s)||v.push(s);else if(w)p[l]=[s],m(l)&&(f[l]=p[l]);else{const I=[s];y(l,e.k)&&(l.value=I),e.k&&(p[e.k]=I)}}else w?(p[l]=a,m(l)&&(f[l]=a)):$&&(y(l,e.k)&&(l.value=a),e.k&&(p[e.k]=a))};if(a){const v=()=>{_(),Ra.delete(e)};v.id=-1,Ra.set(e,v),Vt(v,n)}else Bc(e),_()}}}function Bc(e){const t=Ra.get(e);t&&(t.flags|=8,Ra.delete(e))}Za().requestIdleCallback;Za().cancelIdleCallback;const ws=e=>!!e.type.__asyncLoader,Hg=e=>e.type.__isKeepAlive;function vx(e,t){jg(e,"a",t)}function xx(e,t){jg(e,"da",t)}function jg(e,t,n=Wt){const r=e.__wdc||(e.__wdc=()=>{let i=n;for(;i;){if(i.isDeactivated)return;i=i.parent}return e()});if(to(t,r,n),n){let i=n.parent;for(;i&&i.parent;)Hg(i.parent.vnode)&&$x(r,t,n,i),i=i.parent}}function $x(e,t,n,r){const i=to(t,e,r,!0);no(()=>{xu(r[t],i)},n)}function to(e,t,n=Wt,r=!1){if(n){const i=n[e]||(n[e]=[]),s=t.__weh||(t.__weh=(...a)=>{Qn();const o=Bs(n),l=Nn(t,n,e,a);return o(),Jn(),l});return r?i.unshift(s):i.push(s),s}}const tr=e=>(t,n=Wt)=>{(!ks||e==="sp")&&to(e,(...r)=>t(...r),n)},Sx=tr("bm"),Ou=tr("m"),Tx=tr("bu"),Ix=tr("u"),Cx=tr("bum"),no=tr("um"),Ex=tr("sp"),kx=tr("rtg"),zx=tr("rtc");function Ax(e,t=Wt){to("ec",e,t)}const Mx=Symbol.for("v-ndc");function Dc(e,t,n,r){let i;const s=n,a=Ie(e);if(a||_t(e)){const o=a&&jr(e);let l=!1,d=!1;o&&(l=!an(e),d=er(e),e=Qa(e)),i=new Array(e.length);for(let p=0,f=e.length;pt(o,l,void 0,s));else{const o=Object.keys(e);i=new Array(o.length);for(let l=0,d=o.length;le?f0(e)?so(e):Zl(e.parent):null,_s=At(Object.create(null),{$:e=>e,$el:e=>e.vnode.el,$data:e=>e.data,$props:e=>e.props,$attrs:e=>e.attrs,$slots:e=>e.slots,$refs:e=>e.refs,$parent:e=>Zl(e.parent),$root:e=>Zl(e.root),$host:e=>e.ce,$emit:e=>e.emit,$options:e=>Xg(e),$forceUpdate:e=>e.f||(e.f=()=>{Mu(e.update)}),$nextTick:e=>e.n||(e.n=Pg.bind(e.proxy)),$watch:e=>yx.bind(e)}),Do=(e,t)=>e!==it&&!e.__isScriptSetup&&Je(e,t),Rx={get({_:e},t){if(t==="__v_skip")return!0;const{ctx:n,setupState:r,data:i,props:s,accessCache:a,type:o,appContext:l}=e;if(t[0]!=="$"){const c=a[t];if(c!==void 0)switch(c){case 1:return r[t];case 2:return i[t];case 4:return n[t];case 3:return s[t]}else{if(Do(r,t))return a[t]=1,r[t];if(i!==it&&Je(i,t))return a[t]=2,i[t];if(Je(s,t))return a[t]=3,s[t];if(n!==it&&Je(n,t))return a[t]=4,n[t];Ql&&(a[t]=0)}}const d=_s[t];let p,f;if(d)return t==="$attrs"&&Ot(e.attrs,"get",""),d(e);if((p=o.__cssModules)&&(p=p[t]))return p;if(n!==it&&Je(n,t))return a[t]=4,n[t];if(f=l.config.globalProperties,Je(f,t))return f[t]},set({_:e},t,n){const{data:r,setupState:i,ctx:s}=e;return Do(i,t)?(i[t]=n,!0):r!==it&&Je(r,t)?(r[t]=n,!0):Je(e.props,t)||t[0]==="$"&&t.slice(1)in e?!1:(s[t]=n,!0)},has({_:{data:e,setupState:t,accessCache:n,ctx:r,appContext:i,props:s,type:a}},o){let l;return!!(n[o]||e!==it&&o[0]!=="$"&&Je(e,o)||Do(t,o)||Je(s,o)||Je(r,o)||Je(_s,o)||Je(i.config.globalProperties,o)||(l=a.__cssModules)&&l[o])},defineProperty(e,t,n){return n.get!=null?e._.accessCache[t]=0:Je(n,"value")&&this.set(e,t,n.value,null),Reflect.defineProperty(e,t,n)}};function Pc(e){return Ie(e)?e.reduce((t,n)=>(t[n]=null,t),{}):e}let Ql=!0;function Ox(e){const t=Xg(e),n=e.proxy,r=e.ctx;Ql=!1,t.beforeCreate&&Uc(t.beforeCreate,e,"bc");const{data:i,computed:s,methods:a,watch:o,provide:l,inject:d,created:p,beforeMount:f,mounted:c,beforeUpdate:m,updated:y,activated:w,deactivated:$,beforeDestroy:_,beforeUnmount:v,destroyed:I,unmounted:S,render:E,renderTracked:R,renderTriggered:O,errorCaptured:x,serverPrefetch:U,expose:q,inheritAttrs:J,components:se,directives:ce,filters:L}=t;if(d&&Nx(d,r,null),a)for(const te in a){const he=a[te];Me(he)&&(r[te]=he.bind(n))}if(i){const te=i.call(n,n);nt(te)&&(e.data=$i(te))}if(Ql=!0,s)for(const te in s){const he=s[te],Q=Me(he)?he.bind(n,n):Me(he.get)?he.get.bind(n,n):Mn,ve=!Me(he)&&Me(he.set)?he.set.bind(n):Mn,Y=ot({get:Q,set:ve});Object.defineProperty(r,te,{enumerable:!0,configurable:!0,get:()=>Y.value,set:P=>Y.value=P})}if(o)for(const te in o)Kg(o[te],r,n,te);if(l){const te=Me(l)?l.call(n):l;Reflect.ownKeys(te).forEach(he=>{hx(he,te[he])})}p&&Uc(p,e,"c");function ie(te,he){Ie(he)?he.forEach(Q=>te(Q.bind(n))):he&&te(he.bind(n))}if(ie(Sx,f),ie(Ou,c),ie(Tx,m),ie(Ix,y),ie(vx,w),ie(xx,$),ie(Ax,x),ie(zx,R),ie(kx,O),ie(Cx,v),ie(no,S),ie(Ex,U),Ie(q))if(q.length){const te=e.exposed||(e.exposed={});q.forEach(he=>{Object.defineProperty(te,he,{get:()=>n[he],set:Q=>n[he]=Q,enumerable:!0})})}else e.exposed||(e.exposed={});E&&e.render===Mn&&(e.render=E),J!=null&&(e.inheritAttrs=J),se&&(e.components=se),ce&&(e.directives=ce),U&&Fg(e)}function Nx(e,t,n=Mn){Ie(e)&&(e=Jl(e));for(const r in e){const i=e[r];let s;nt(i)?"default"in i?s=Ta(i.from||r,i.default,!0):s=Ta(i.from||r):s=Ta(i),Nt(s)?Object.defineProperty(t,r,{enumerable:!0,configurable:!0,get:()=>s.value,set:a=>s.value=a}):t[r]=s}}function Uc(e,t,n){Nn(Ie(e)?e.map(r=>r.bind(t.proxy)):e.bind(t.proxy),t,n)}function Kg(e,t,n,r){let i=r.includes(".")?Vg(n,r):()=>n[r];if(_t(e)){const s=t[e];Me(s)&&En(i,s)}else if(Me(e))En(i,e.bind(n));else if(nt(e))if(Ie(e))e.forEach(s=>Kg(s,t,n,r));else{const s=Me(e.handler)?e.handler.bind(n):t[e.handler];Me(s)&&En(i,s,e)}}function Xg(e){const t=e.type,{mixins:n,extends:r}=t,{mixins:i,optionsCache:s,config:{optionMergeStrategies:a}}=e.appContext,o=s.get(t);let l;return o?l=o:!i.length&&!n&&!r?l=t:(l={},i.length&&i.forEach(d=>Oa(l,d,a,!0)),Oa(l,t,a)),nt(t)&&s.set(t,l),l}function Oa(e,t,n,r=!1){const{mixins:i,extends:s}=t;s&&Oa(e,s,n,!0),i&&i.forEach(a=>Oa(e,a,n,!0));for(const a in t)if(!(r&&a==="expose")){const o=Bx[a]||n&&n[a];e[a]=o?o(e[a],t[a]):t[a]}return e}const Bx={data:Lc,props:Wc,emits:Wc,methods:ds,computed:ds,beforeCreate:Ut,created:Ut,beforeMount:Ut,mounted:Ut,beforeUpdate:Ut,updated:Ut,beforeDestroy:Ut,beforeUnmount:Ut,destroyed:Ut,unmounted:Ut,activated:Ut,deactivated:Ut,errorCaptured:Ut,serverPrefetch:Ut,components:ds,directives:ds,watch:Px,provide:Lc,inject:Dx};function Lc(e,t){return t?e?function(){return At(Me(e)?e.call(this,this):e,Me(t)?t.call(this,this):t)}:t:e}function Dx(e,t){return ds(Jl(e),Jl(t))}function Jl(e){if(Ie(e)){const t={};for(let n=0;nt==="modelValue"||t==="model-value"?e.modelModifiers:e[`${t}Modifiers`]||e[`${br(t)}Modifiers`]||e[`${Jr(t)}Modifiers`];function qx(e,t,...n){if(e.isUnmounted)return;const r=e.vnode.props||it;let i=n;const s=t.startsWith("update:"),a=s&&Wx(r,t.slice(7));a&&(a.trim&&(i=n.map(p=>_t(p)?p.trim():p)),a.number&&(i=n.map(Ya)));let o,l=r[o=Mo(t)]||r[o=Mo(br(t))];!l&&s&&(l=r[o=Mo(Jr(t))]),l&&Nn(l,e,6,i);const d=r[o+"Once"];if(d){if(!e.emitted)e.emitted={};else if(e.emitted[o])return;e.emitted[o]=!0,Nn(d,e,6,i)}}const Gx=new WeakMap;function Zg(e,t,n=!1){const r=n?Gx:t.emitsCache,i=r.get(e);if(i!==void 0)return i;const s=e.emits;let a={},o=!1;if(!Me(e)){const l=d=>{const p=Zg(d,t,!0);p&&(o=!0,At(a,p))};!n&&t.mixins.length&&t.mixins.forEach(l),e.extends&&l(e.extends),e.mixins&&e.mixins.forEach(l)}return!s&&!o?(nt(e)&&r.set(e,null),null):(Ie(s)?s.forEach(l=>a[l]=null):At(a,s),nt(e)&&r.set(e,a),a)}function ro(e,t){return!e||!Ka(t)?!1:(t=t.slice(2).replace(/Once$/,""),Je(e,t[0].toLowerCase()+t.slice(1))||Je(e,Jr(t))||Je(e,t))}function qc(e){const{type:t,vnode:n,proxy:r,withProxy:i,propsOptions:[s],slots:a,attrs:o,emit:l,render:d,renderCache:p,props:f,data:c,setupState:m,ctx:y,inheritAttrs:w}=e,$=Ma(e);let _,v;try{if(n.shapeFlag&4){const S=i||r,E=S;_=An(d.call(E,S,p,f,m,c,y)),v=o}else{const S=t;_=An(S.length>1?S(f,{attrs:o,slots:a,emit:l}):S(f,null)),v=t.props?o:Vx(o)}}catch(S){vs.length=0,Ja(S,e,1),_=Rn(Ti)}let I=_;if(v&&w!==!1){const S=Object.keys(v),{shapeFlag:E}=I;S.length&&E&7&&(s&&S.some(vu)&&(v=Fx(v,s)),I=Ii(I,v,!1,!0))}return n.dirs&&(I=Ii(I,null,!1,!0),I.dirs=I.dirs?I.dirs.concat(n.dirs):n.dirs),n.transition&&Ru(I,n.transition),_=I,Ma($),_}const Vx=e=>{let t;for(const n in e)(n==="class"||n==="style"||Ka(n))&&((t||(t={}))[n]=e[n]);return t},Fx=(e,t)=>{const n={};for(const r in e)(!vu(r)||!(r.slice(9)in t))&&(n[r]=e[r]);return n};function Hx(e,t,n){const{props:r,children:i,component:s}=e,{props:a,children:o,patchFlag:l}=t,d=s.emitsOptions;if(t.dirs||t.transition)return!0;if(n&&l>=0){if(l&1024)return!0;if(l&16)return r?Gc(r,a,d):!!a;if(l&8){const p=t.dynamicProps;for(let f=0;fObject.create(Jg),t0=e=>Object.getPrototypeOf(e)===Jg;function Kx(e,t,n,r=!1){const i={},s=e0();e.propsDefaults=Object.create(null),n0(e,t,i,s);for(const a in e.propsOptions[0])a in i||(i[a]=void 0);n?e.props=r?i:nx(i):e.type.props?e.props=i:e.props=s,e.attrs=s}function Xx(e,t,n,r){const{props:i,attrs:s,vnode:{patchFlag:a}}=e,o=Qe(i),[l]=e.propsOptions;let d=!1;if((r||a>0)&&!(a&16)){if(a&8){const p=e.vnode.dynamicProps;for(let f=0;f{l=!0;const[c,m]=r0(f,t,!0);At(a,c),m&&o.push(...m)};!n&&t.mixins.length&&t.mixins.forEach(p),e.extends&&p(e.extends),e.mixins&&e.mixins.forEach(p)}if(!s&&!l)return nt(e)&&r.set(e,wi),wi;if(Ie(s))for(let p=0;pe==="_"||e==="_ctx"||e==="$stable",Bu=e=>Ie(e)?e.map(An):[An(e)],Zx=(e,t,n)=>{if(t._n)return t;const r=fx((...i)=>Bu(t(...i)),n);return r._c=!1,r},i0=(e,t,n)=>{const r=e._ctx;for(const i in e){if(Nu(i))continue;const s=e[i];if(Me(s))t[i]=Zx(i,s,r);else if(s!=null){const a=Bu(s);t[i]=()=>a}}},s0=(e,t)=>{const n=Bu(t);e.slots.default=()=>n},a0=(e,t,n)=>{for(const r in t)(n||!Nu(r))&&(e[r]=t[r])},Qx=(e,t,n)=>{const r=e.slots=e0();if(e.vnode.shapeFlag&32){const i=t._;i?(a0(r,t,n),n&&yg(r,"_",i,!0)):i0(t,r)}else t&&s0(e,t)},Jx=(e,t,n)=>{const{vnode:r,slots:i}=e;let s=!0,a=it;if(r.shapeFlag&32){const o=t._;o?n&&o===1?s=!1:a0(i,t,n):(s=!t.$stable,i0(t,i)),a=t}else t&&(s0(e,t),a={default:1});if(s)for(const o in i)!Nu(o)&&a[o]==null&&delete i[o]},Vt=i1;function e1(e){return t1(e)}function t1(e,t){const n=Za();n.__VUE__=!0;const{insert:r,remove:i,patchProp:s,createElement:a,createText:o,createComment:l,setText:d,setElementText:p,parentNode:f,nextSibling:c,setScopeId:m=Mn,insertStaticContent:y}=e,w=(A,N,D,H=null,Z=null,K=null,le=void 0,ae=null,oe=!!N.dynamicChildren)=>{if(A===N)return;A&&!Zi(A,N)&&(H=Xe(A),P(A,Z,K,!0),A=null),N.patchFlag===-2&&(oe=!1,N.dynamicChildren=null);const{type:X,ref:Se,shapeFlag:fe}=N;switch(X){case io:$(A,N,D,H);break;case Ti:_(A,N,D,H);break;case Uo:A==null&&v(N,D,H,le);break;case mn:se(A,N,D,H,Z,K,le,ae,oe);break;default:fe&1?E(A,N,D,H,Z,K,le,ae,oe):fe&6?ce(A,N,D,H,Z,K,le,ae,oe):(fe&64||fe&128)&&X.process(A,N,D,H,Z,K,le,ae,oe,ut)}Se!=null&&Z?bs(Se,A&&A.ref,K,N||A,!N):Se==null&&A&&A.ref!=null&&bs(A.ref,null,K,A,!0)},$=(A,N,D,H)=>{if(A==null)r(N.el=o(N.children),D,H);else{const Z=N.el=A.el;N.children!==A.children&&d(Z,N.children)}},_=(A,N,D,H)=>{A==null?r(N.el=l(N.children||""),D,H):N.el=A.el},v=(A,N,D,H)=>{[A.el,A.anchor]=y(A.children,N,D,H,A.el,A.anchor)},I=({el:A,anchor:N},D,H)=>{let Z;for(;A&&A!==N;)Z=c(A),r(A,D,H),A=Z;r(N,D,H)},S=({el:A,anchor:N})=>{let D;for(;A&&A!==N;)D=c(A),i(A),A=D;i(N)},E=(A,N,D,H,Z,K,le,ae,oe)=>{if(N.type==="svg"?le="svg":N.type==="math"&&(le="mathml"),A==null)R(N,D,H,Z,K,le,ae,oe);else{const X=A.el&&A.el._isVueCE?A.el:null;try{X&&X._beginPatch(),U(A,N,Z,K,le,ae,oe)}finally{X&&X._endPatch()}}},R=(A,N,D,H,Z,K,le,ae)=>{let oe,X;const{props:Se,shapeFlag:fe,transition:xe,dirs:me}=A;if(oe=A.el=a(A.type,K,Se&&Se.is,Se),fe&8?p(oe,A.children):fe&16&&x(A.children,oe,null,H,Z,Po(A,K),le,ae),me&&Mr(A,null,H,"created"),O(oe,A,A.scopeId,le,H),Se){for(const He in Se)He!=="value"&&!ms(He)&&s(oe,He,null,Se[He],K,H);"value"in Se&&s(oe,"value",null,Se.value,K),(X=Se.onVnodeBeforeMount)&&Sn(X,H,A)}me&&Mr(A,null,H,"beforeMount");const ke=n1(Z,xe);ke&&xe.beforeEnter(oe),r(oe,N,D),((X=Se&&Se.onVnodeMounted)||ke||me)&&Vt(()=>{X&&Sn(X,H,A),ke&&xe.enter(oe),me&&Mr(A,null,H,"mounted")},Z)},O=(A,N,D,H,Z)=>{if(D&&m(A,D),H)for(let K=0;K{for(let X=oe;X{const ae=N.el=A.el;let{patchFlag:oe,dynamicChildren:X,dirs:Se}=N;oe|=A.patchFlag&16;const fe=A.props||it,xe=N.props||it;let me;if(D&&Rr(D,!1),(me=xe.onVnodeBeforeUpdate)&&Sn(me,D,N,A),Se&&Mr(N,A,D,"beforeUpdate"),D&&Rr(D,!0),(fe.innerHTML&&xe.innerHTML==null||fe.textContent&&xe.textContent==null)&&p(ae,""),X?q(A.dynamicChildren,X,ae,D,H,Po(N,Z),K):le||he(A,N,ae,null,D,H,Po(N,Z),K,!1),oe>0){if(oe&16)J(ae,fe,xe,D,Z);else if(oe&2&&fe.class!==xe.class&&s(ae,"class",null,xe.class,Z),oe&4&&s(ae,"style",fe.style,xe.style,Z),oe&8){const ke=N.dynamicProps;for(let He=0;He{me&&Sn(me,D,N,A),Se&&Mr(N,A,D,"updated")},H)},q=(A,N,D,H,Z,K,le)=>{for(let ae=0;ae{if(N!==D){if(N!==it)for(const K in N)!ms(K)&&!(K in D)&&s(A,K,N[K],null,Z,H);for(const K in D){if(ms(K))continue;const le=D[K],ae=N[K];le!==ae&&K!=="value"&&s(A,K,ae,le,Z,H)}"value"in D&&s(A,"value",N.value,D.value,Z)}},se=(A,N,D,H,Z,K,le,ae,oe)=>{const X=N.el=A?A.el:o(""),Se=N.anchor=A?A.anchor:o("");let{patchFlag:fe,dynamicChildren:xe,slotScopeIds:me}=N;me&&(ae=ae?ae.concat(me):me),A==null?(r(X,D,H),r(Se,D,H),x(N.children||[],D,Se,Z,K,le,ae,oe)):fe>0&&fe&64&&xe&&A.dynamicChildren&&A.dynamicChildren.length===xe.length?(q(A.dynamicChildren,xe,D,Z,K,le,ae),(N.key!=null||Z&&N===Z.subTree)&&o0(A,N,!0)):he(A,N,D,Se,Z,K,le,ae,oe)},ce=(A,N,D,H,Z,K,le,ae,oe)=>{N.slotScopeIds=ae,A==null?N.shapeFlag&512?Z.ctx.activate(N,D,H,le,oe):L(N,D,H,Z,K,le,oe):ue(A,N,oe)},L=(A,N,D,H,Z,K,le)=>{const ae=A.component=f1(A,H,Z);if(Hg(A)&&(ae.ctx.renderer=ut),m1(ae,!1,le),ae.asyncDep){if(Z&&Z.registerDep(ae,ie,le),!A.el){const oe=ae.subTree=Rn(Ti);_(null,oe,N,D),A.placeholder=oe.el}}else ie(ae,A,N,D,Z,K,le)},ue=(A,N,D)=>{const H=N.component=A.component;if(Hx(A,N,D))if(H.asyncDep&&!H.asyncResolved){te(H,N,D);return}else H.next=N,H.update();else N.el=A.el,H.vnode=N},ie=(A,N,D,H,Z,K,le)=>{const ae=()=>{if(A.isMounted){let{next:fe,bu:xe,u:me,parent:ke,vnode:He}=A;{const qt=l0(A);if(qt){fe&&(fe.el=He.el,te(A,fe,le)),qt.asyncDep.then(()=>{Vt(()=>{A.isUnmounted||X()},Z)});return}}let Le=fe,mt;Rr(A,!1),fe?(fe.el=He.el,te(A,fe,le)):fe=He,xe&&Sa(xe),(mt=fe.props&&fe.props.onVnodeBeforeUpdate)&&Sn(mt,ke,fe,He),Rr(A,!0);const $t=qc(A),gt=A.subTree;A.subTree=$t,w(gt,$t,f(gt.el),Xe(gt),A,Z,K),fe.el=$t.el,Le===null&&jx(A,$t.el),me&&Vt(me,Z),(mt=fe.props&&fe.props.onVnodeUpdated)&&Vt(()=>Sn(mt,ke,fe,He),Z)}else{let fe;const{el:xe,props:me}=N,{bm:ke,m:He,parent:Le,root:mt,type:$t}=A,gt=ws(N);Rr(A,!1),ke&&Sa(ke),!gt&&(fe=me&&me.onVnodeBeforeMount)&&Sn(fe,Le,N),Rr(A,!0);{mt.ce&&mt.ce._hasShadowRoot()&&mt.ce._injectChildStyle($t);const qt=A.subTree=qc(A);w(null,qt,D,H,A,Z,K),N.el=qt.el}if(He&&Vt(He,Z),!gt&&(fe=me&&me.onVnodeMounted)){const qt=N;Vt(()=>Sn(fe,Le,qt),Z)}(N.shapeFlag&256||Le&&ws(Le.vnode)&&Le.vnode.shapeFlag&256)&&A.a&&Vt(A.a,Z),A.isMounted=!0,N=D=H=null}};A.scope.on();const oe=A.effect=new vg(ae);A.scope.off();const X=A.update=oe.run.bind(oe),Se=A.job=oe.runIfDirty.bind(oe);Se.i=A,Se.id=A.uid,oe.scheduler=()=>Mu(Se),Rr(A,!0),X()},te=(A,N,D)=>{N.component=A;const H=A.vnode.props;A.vnode=N,A.next=null,Xx(A,N.props,H,D),Jx(A,N.children,D),Qn(),Oc(A),Jn()},he=(A,N,D,H,Z,K,le,ae,oe=!1)=>{const X=A&&A.children,Se=A?A.shapeFlag:0,fe=N.children,{patchFlag:xe,shapeFlag:me}=N;if(xe>0){if(xe&128){ve(X,fe,D,H,Z,K,le,ae,oe);return}else if(xe&256){Q(X,fe,D,H,Z,K,le,ae,oe);return}}me&8?(Se&16&<(X,Z,K),fe!==X&&p(D,fe)):Se&16?me&16?ve(X,fe,D,H,Z,K,le,ae,oe):lt(X,Z,K,!0):(Se&8&&p(D,""),me&16&&x(fe,D,H,Z,K,le,ae,oe))},Q=(A,N,D,H,Z,K,le,ae,oe)=>{A=A||wi,N=N||wi;const X=A.length,Se=N.length,fe=Math.min(X,Se);let xe;for(xe=0;xeSe?lt(A,Z,K,!0,!1,fe):x(N,D,H,Z,K,le,ae,oe,fe)},ve=(A,N,D,H,Z,K,le,ae,oe)=>{let X=0;const Se=N.length;let fe=A.length-1,xe=Se-1;for(;X<=fe&&X<=xe;){const me=A[X],ke=N[X]=oe?jn(N[X]):An(N[X]);if(Zi(me,ke))w(me,ke,D,null,Z,K,le,ae,oe);else break;X++}for(;X<=fe&&X<=xe;){const me=A[fe],ke=N[xe]=oe?jn(N[xe]):An(N[xe]);if(Zi(me,ke))w(me,ke,D,null,Z,K,le,ae,oe);else break;fe--,xe--}if(X>fe){if(X<=xe){const me=xe+1,ke=mexe)for(;X<=fe;)P(A[X],Z,K,!0),X++;else{const me=X,ke=X,He=new Map;for(X=ke;X<=xe;X++){const vt=N[X]=oe?jn(N[X]):An(N[X]);vt.key!=null&&He.set(vt.key,X)}let Le,mt=0;const $t=xe-ke+1;let gt=!1,qt=0;const vn=new Array($t);for(X=0;X<$t;X++)vn[X]=0;for(X=me;X<=fe;X++){const vt=A[X];if(mt>=$t){P(vt,Z,K,!0);continue}let Ct;if(vt.key!=null)Ct=He.get(vt.key);else for(Le=ke;Le<=xe;Le++)if(vn[Le-ke]===0&&Zi(vt,N[Le])){Ct=Le;break}Ct===void 0?P(vt,Z,K,!0):(vn[Ct-ke]=X+1,Ct>=qt?qt=Ct:gt=!0,w(vt,N[Ct],D,null,Z,K,le,ae,oe),mt++)}const vr=gt?r1(vn):wi;for(Le=vr.length-1,X=$t-1;X>=0;X--){const vt=ke+X,Ct=N[vt],Dn=N[vt+1],ni=vt+1{const{el:K,type:le,transition:ae,children:oe,shapeFlag:X}=A;if(X&6){Y(A.component.subTree,N,D,H);return}if(X&128){A.suspense.move(N,D,H);return}if(X&64){le.move(A,N,D,ut);return}if(le===mn){r(K,N,D);for(let fe=0;feae.enter(K),Z);else{const{leave:fe,delayLeave:xe,afterLeave:me}=ae,ke=()=>{A.ctx.isUnmounted?i(K):r(K,N,D)},He=()=>{K._isLeaving&&K[_x](!0),fe(K,()=>{ke(),me&&me()})};xe?xe(K,ke,He):He()}else r(K,N,D)},P=(A,N,D,H=!1,Z=!1)=>{const{type:K,props:le,ref:ae,children:oe,dynamicChildren:X,shapeFlag:Se,patchFlag:fe,dirs:xe,cacheIndex:me}=A;if(fe===-2&&(Z=!1),ae!=null&&(Qn(),bs(ae,null,D,A,!0),Jn()),me!=null&&(N.renderCache[me]=void 0),Se&256){N.ctx.deactivate(A);return}const ke=Se&1&&xe,He=!ws(A);let Le;if(He&&(Le=le&&le.onVnodeBeforeUnmount)&&Sn(Le,N,A),Se&6)Ue(A.component,D,H);else{if(Se&128){A.suspense.unmount(D,H);return}ke&&Mr(A,null,N,"beforeUnmount"),Se&64?A.type.remove(A,N,D,ut,H):X&&!X.hasOnce&&(K!==mn||fe>0&&fe&64)?lt(X,N,D,!1,!0):(K===mn&&fe&384||!Z&&Se&16)&<(oe,N,D),H&&ee(A)}(He&&(Le=le&&le.onVnodeUnmounted)||ke)&&Vt(()=>{Le&&Sn(Le,N,A),ke&&Mr(A,null,N,"unmounted")},D)},ee=A=>{const{type:N,el:D,anchor:H,transition:Z}=A;if(N===mn){ne(D,H);return}if(N===Uo){S(A);return}const K=()=>{i(D),Z&&!Z.persisted&&Z.afterLeave&&Z.afterLeave()};if(A.shapeFlag&1&&Z&&!Z.persisted){const{leave:le,delayLeave:ae}=Z,oe=()=>le(D,K);ae?ae(A.el,K,oe):oe()}else K()},ne=(A,N)=>{let D;for(;A!==N;)D=c(A),i(A),A=D;i(N)},Ue=(A,N,D)=>{const{bum:H,scope:Z,job:K,subTree:le,um:ae,m:oe,a:X}=A;Fc(oe),Fc(X),H&&Sa(H),Z.stop(),K&&(K.flags|=8,P(le,A,N,D)),ae&&Vt(ae,N),Vt(()=>{A.isUnmounted=!0},N)},lt=(A,N,D,H=!1,Z=!1,K=0)=>{for(let le=K;le{if(A.shapeFlag&6)return Xe(A.component.subTree);if(A.shapeFlag&128)return A.suspense.next();const N=c(A.anchor||A.el),D=N&&N[bx];return D?c(D):N};let Ke=!1;const ft=(A,N,D)=>{let H;A==null?N._vnode&&(P(N._vnode,null,null,!0),H=N._vnode.component):w(N._vnode||null,A,N,null,null,null,D),N._vnode=A,Ke||(Ke=!0,Oc(H),Lg(),Ke=!1)},ut={p:w,um:P,m:Y,r:ee,mt:L,mc:x,pc:he,pbc:q,n:Xe,o:e};return{render:ft,hydrate:void 0,createApp:Lx(ft)}}function Po({type:e,props:t},n){return n==="svg"&&e==="foreignObject"||n==="mathml"&&e==="annotation-xml"&&t&&t.encoding&&t.encoding.includes("html")?void 0:n}function Rr({effect:e,job:t},n){n?(e.flags|=32,t.flags|=4):(e.flags&=-33,t.flags&=-5)}function n1(e,t){return(!e||e&&!e.pendingBranch)&&t&&!t.persisted}function o0(e,t,n=!1){const r=e.children,i=t.children;if(Ie(r)&&Ie(i))for(let s=0;s>1,e[n[o]]0&&(t[r]=n[s-1]),n[s]=r)}}for(s=n.length,a=n[s-1];s-- >0;)n[s]=a,a=t[a];return n}function l0(e){const t=e.subTree.component;if(t)return t.asyncDep&&!t.asyncResolved?t:l0(t)}function Fc(e){if(e)for(let t=0;te.__isSuspense;function i1(e,t){t&&t.pendingBranch?Ie(e)?t.effects.push(...e):t.effects.push(e):px(e)}const mn=Symbol.for("v-fgt"),io=Symbol.for("v-txt"),Ti=Symbol.for("v-cmt"),Uo=Symbol.for("v-stc"),vs=[];let Jt=null;function kn(e=!1){vs.push(Jt=e?null:[])}function s1(){vs.pop(),Jt=vs[vs.length-1]||null}let Es=1;function Hc(e,t=!1){Es+=e,e<0&&Jt&&t&&(Jt.hasOnce=!0)}function a1(e){return e.dynamicChildren=Es>0?Jt||wi:null,s1(),Es>0&&Jt&&Jt.push(e),e}function zn(e,t,n,r,i,s){return a1(M(e,t,n,r,i,s,!0))}function c0(e){return e?e.__v_isVNode===!0:!1}function Zi(e,t){return e.type===t.type&&e.key===t.key}const p0=({key:e})=>e??null,Ia=({ref:e,ref_key:t,ref_for:n})=>(typeof e=="number"&&(e=""+e),e!=null?_t(e)||Nt(e)||Me(e)?{i:sn,r:e,k:t,f:!!n}:e:null);function M(e,t=null,n=null,r=0,i=null,s=e===mn?0:1,a=!1,o=!1){const l={__v_isVNode:!0,__v_skip:!0,type:e,props:t,key:t&&p0(t),ref:t&&Ia(t),scopeId:qg,slotScopeIds:null,children:n,component:null,suspense:null,ssContent:null,ssFallback:null,dirs:null,transition:null,el:null,anchor:null,target:null,targetStart:null,targetAnchor:null,staticCount:0,shapeFlag:s,patchFlag:r,dynamicProps:i,dynamicChildren:null,appContext:null,ctx:sn};return o?(Du(l,n),s&128&&e.normalize(l)):n&&(l.shapeFlag|=_t(n)?8:16),Es>0&&!a&&Jt&&(l.patchFlag>0||s&6)&&l.patchFlag!==32&&Jt.push(l),l}const Rn=o1;function o1(e,t=null,n=null,r=0,i=null,s=!1){if((!e||e===Mx)&&(e=Ti),c0(e)){const o=Ii(e,t,!0);return n&&Du(o,n),Es>0&&!s&&Jt&&(o.shapeFlag&6?Jt[Jt.indexOf(e)]=o:Jt.push(o)),o.patchFlag=-2,o}if(w1(e)&&(e=e.__vccOpts),t){t=l1(t);let{class:o,style:l}=t;o&&!_t(o)&&(t.class=Os(o)),nt(l)&&(Au(l)&&!Ie(l)&&(l=At({},l)),t.style=Fr(l))}const a=_t(e)?1:d0(e)?128:wx(e)?64:nt(e)?4:Me(e)?2:0;return M(e,t,n,r,i,a,s,!0)}function l1(e){return e?Au(e)||t0(e)?At({},e):e:null}function Ii(e,t,n=!1,r=!1){const{props:i,ref:s,patchFlag:a,children:o,transition:l}=e,d=t?d1(i||{},t):i,p={__v_isVNode:!0,__v_skip:!0,type:e.type,props:d,key:d&&p0(d),ref:t&&t.ref?n&&s?Ie(s)?s.concat(Ia(t)):[s,Ia(t)]:Ia(t):s,scopeId:e.scopeId,slotScopeIds:e.slotScopeIds,children:o,target:e.target,targetStart:e.targetStart,targetAnchor:e.targetAnchor,staticCount:e.staticCount,shapeFlag:e.shapeFlag,patchFlag:t&&e.type!==mn?a===-1?16:a|16:a,dynamicProps:e.dynamicProps,dynamicChildren:e.dynamicChildren,appContext:e.appContext,dirs:e.dirs,transition:l,component:e.component,suspense:e.suspense,ssContent:e.ssContent&&Ii(e.ssContent),ssFallback:e.ssFallback&&Ii(e.ssFallback),placeholder:e.placeholder,el:e.el,anchor:e.anchor,ctx:e.ctx,ce:e.ce};return l&&r&&Ru(p,l.clone(p)),p}function u1(e=" ",t=0){return Rn(io,null,e,t)}function An(e){return e==null||typeof e=="boolean"?Rn(Ti):Ie(e)?Rn(mn,null,e.slice()):c0(e)?jn(e):Rn(io,null,String(e))}function jn(e){return e.el===null&&e.patchFlag!==-1||e.memo?e:Ii(e)}function Du(e,t){let n=0;const{shapeFlag:r}=e;if(t==null)t=null;else if(Ie(t))n=16;else if(typeof t=="object")if(r&65){const i=t.default;i&&(i._c&&(i._d=!1),Du(e,i()),i._c&&(i._d=!0));return}else{n=32;const i=t._;!i&&!t0(t)?t._ctx=sn:i===3&&sn&&(sn.slots._===1?t._=1:(t._=2,e.patchFlag|=1024))}else Me(t)?(t={default:t,_ctx:sn},n=32):(t=String(t),r&64?(n=16,t=[u1(t)]):n=8);e.children=t,e.shapeFlag|=n}function d1(...e){const t={};for(let n=0;nWt||sn;let Na,tu;{const e=Za(),t=(n,r)=>{let i;return(i=e[n])||(i=e[n]=[]),i.push(r),s=>{i.length>1?i.forEach(a=>a(s)):i[0](s)}};Na=t("__VUE_INSTANCE_SETTERS__",n=>Wt=n),tu=t("__VUE_SSR_SETTERS__",n=>ks=n)}const Bs=e=>{const t=Wt;return Na(e),e.scope.on(),()=>{e.scope.off(),Na(t)}},jc=()=>{Wt&&Wt.scope.off(),Na(null)};function f0(e){return e.vnode.shapeFlag&4}let ks=!1;function m1(e,t=!1,n=!1){t&&tu(t);const{props:r,children:i}=e.vnode,s=f0(e);Kx(e,r,s,t),Qx(e,i,n||t);const a=s?g1(e,t):void 0;return t&&tu(!1),a}function g1(e,t){const n=e.type;e.accessCache=Object.create(null),e.proxy=new Proxy(e.ctx,Rx);const{setup:r}=n;if(r){Qn();const i=e.setupContext=r.length>1?b1(e):null,s=Bs(e),a=Ns(r,e,0,[e.props,i]),o=fg(a);if(Jn(),s(),(o||e.sp)&&!ws(e)&&Fg(e),o){if(a.then(jc,jc),t)return a.then(l=>{Kc(e,l)}).catch(l=>{Ja(l,e,0)});e.asyncDep=a}else Kc(e,a)}else h0(e)}function Kc(e,t,n){Me(t)?e.type.__ssrInlineRender?e.ssrRender=t:e.render=t:nt(t)&&(e.setupState=Bg(t)),h0(e)}function h0(e,t,n){const r=e.type;e.render||(e.render=r.render||Mn);{const i=Bs(e);Qn();try{Ox(e)}finally{Jn(),i()}}}const y1={get(e,t){return Ot(e,"get",""),e[t]}};function b1(e){const t=n=>{e.exposed=n||{}};return{attrs:new Proxy(e.attrs,y1),slots:e.slots,emit:e.emit,expose:t}}function so(e){return e.exposed?e.exposeProxy||(e.exposeProxy=new Proxy(Bg(rx(e.exposed)),{get(t,n){if(n in t)return t[n];if(n in _s)return _s[n](e)},has(t,n){return n in t||n in _s}})):e.proxy}function w1(e){return Me(e)&&"__vccOpts"in e}const ot=(e,t)=>ox(e,t,ks),_1="3.5.29";/** +* @vue/runtime-dom v3.5.29 +* (c) 2018-present Yuxi (Evan) You and Vue contributors +* @license MIT +**/let nu;const Xc=typeof window<"u"&&window.trustedTypes;if(Xc)try{nu=Xc.createPolicy("vue",{createHTML:e=>e})}catch{}const m0=nu?e=>nu.createHTML(e):e=>e,v1="http://www.w3.org/2000/svg",x1="http://www.w3.org/1998/Math/MathML",Hn=typeof document<"u"?document:null,Yc=Hn&&Hn.createElement("template"),$1={insert:(e,t,n)=>{t.insertBefore(e,n||null)},remove:e=>{const t=e.parentNode;t&&t.removeChild(e)},createElement:(e,t,n,r)=>{const i=t==="svg"?Hn.createElementNS(v1,e):t==="mathml"?Hn.createElementNS(x1,e):n?Hn.createElement(e,{is:n}):Hn.createElement(e);return e==="select"&&r&&r.multiple!=null&&i.setAttribute("multiple",r.multiple),i},createText:e=>Hn.createTextNode(e),createComment:e=>Hn.createComment(e),setText:(e,t)=>{e.nodeValue=t},setElementText:(e,t)=>{e.textContent=t},parentNode:e=>e.parentNode,nextSibling:e=>e.nextSibling,querySelector:e=>Hn.querySelector(e),setScopeId(e,t){e.setAttribute(t,"")},insertStaticContent(e,t,n,r,i,s){const a=n?n.previousSibling:t.lastChild;if(i&&(i===s||i.nextSibling))for(;t.insertBefore(i.cloneNode(!0),n),!(i===s||!(i=i.nextSibling)););else{Yc.innerHTML=m0(r==="svg"?`${e}`:r==="mathml"?`${e}`:e);const o=Yc.content;if(r==="svg"||r==="mathml"){const l=o.firstChild;for(;l.firstChild;)o.appendChild(l.firstChild);o.removeChild(l)}t.insertBefore(o,n)}return[a?a.nextSibling:t.firstChild,n?n.previousSibling:t.lastChild]}},S1=Symbol("_vtc");function T1(e,t,n){const r=e[S1];r&&(t=(t?[t,...r]:[...r]).join(" ")),t==null?e.removeAttribute("class"):n?e.setAttribute("class",t):e.className=t}const Ba=Symbol("_vod"),g0=Symbol("_vsh"),I1={name:"show",beforeMount(e,{value:t},{transition:n}){e[Ba]=e.style.display==="none"?"":e.style.display,n&&t?n.beforeEnter(e):Qi(e,t)},mounted(e,{value:t},{transition:n}){n&&t&&n.enter(e)},updated(e,{value:t,oldValue:n},{transition:r}){!t!=!n&&(r?t?(r.beforeEnter(e),Qi(e,!0),r.enter(e)):r.leave(e,()=>{Qi(e,!1)}):Qi(e,t))},beforeUnmount(e,{value:t}){Qi(e,t)}};function Qi(e,t){e.style.display=t?e[Ba]:"none",e[g0]=!t}const C1=Symbol(""),E1=/(?:^|;)\s*display\s*:/;function k1(e,t,n){const r=e.style,i=_t(n);let s=!1;if(n&&!i){if(t)if(_t(t))for(const a of t.split(";")){const o=a.slice(0,a.indexOf(":")).trim();n[o]==null&&Ca(r,o,"")}else for(const a in t)n[a]==null&&Ca(r,a,"");for(const a in n)a==="display"&&(s=!0),Ca(r,a,n[a])}else if(i){if(t!==n){const a=r[C1];a&&(n+=";"+a),r.cssText=n,s=E1.test(n)}}else t&&e.removeAttribute("style");Ba in e&&(e[Ba]=s?r.display:"",e[g0]&&(r.display="none"))}const Zc=/\s*!important$/;function Ca(e,t,n){if(Ie(n))n.forEach(r=>Ca(e,t,r));else if(n==null&&(n=""),t.startsWith("--"))e.setProperty(t,n);else{const r=z1(e,t);Zc.test(n)?e.setProperty(Jr(r),n.replace(Zc,""),"important"):e[r]=n}}const Qc=["Webkit","Moz","ms"],Lo={};function z1(e,t){const n=Lo[t];if(n)return n;let r=br(t);if(r!=="filter"&&r in e)return Lo[t]=r;r=gg(r);for(let i=0;iWo||(O1.then(()=>Wo=0),Wo=Date.now());function B1(e,t){const n=r=>{if(!r._vts)r._vts=Date.now();else if(r._vts<=n.attached)return;Nn(D1(r,n.value),t,5,[r])};return n.value=e,n.attached=N1(),n}function D1(e,t){if(Ie(t)){const n=e.stopImmediatePropagation;return e.stopImmediatePropagation=()=>{n.call(e),e._stopped=!0},t.map(r=>i=>!i._stopped&&r&&r(i))}else return t}const ip=e=>e.charCodeAt(0)===111&&e.charCodeAt(1)===110&&e.charCodeAt(2)>96&&e.charCodeAt(2)<123,P1=(e,t,n,r,i,s)=>{const a=i==="svg";t==="class"?T1(e,r,a):t==="style"?k1(e,n,r):Ka(t)?vu(t)||M1(e,t,n,r,s):(t[0]==="."?(t=t.slice(1),!0):t[0]==="^"?(t=t.slice(1),!1):U1(e,t,r,a))?(tp(e,t,r),!e.tagName.includes("-")&&(t==="value"||t==="checked"||t==="selected")&&ep(e,t,r,a,s,t!=="value")):e._isVueCE&&(/[A-Z]/.test(t)||!_t(r))?tp(e,br(t),r,s,t):(t==="true-value"?e._trueValue=r:t==="false-value"&&(e._falseValue=r),ep(e,t,r,a))};function U1(e,t,n,r){if(r)return!!(t==="innerHTML"||t==="textContent"||t in e&&ip(t)&&Me(n));if(t==="spellcheck"||t==="draggable"||t==="translate"||t==="autocorrect"||t==="sandbox"&&e.tagName==="IFRAME"||t==="form"||t==="list"&&e.tagName==="INPUT"||t==="type"&&e.tagName==="TEXTAREA")return!1;if(t==="width"||t==="height"){const i=e.tagName;if(i==="IMG"||i==="VIDEO"||i==="CANVAS"||i==="SOURCE")return!1}return ip(t)&&_t(n)?!1:t in e}const Ci=e=>{const t=e.props["onUpdate:modelValue"]||!1;return Ie(t)?n=>Sa(t,n):t};function L1(e){e.target.composing=!0}function sp(e){const t=e.target;t.composing&&(t.composing=!1,t.dispatchEvent(new Event("input")))}const Zn=Symbol("_assign");function ap(e,t,n){return t&&(e=e.trim()),n&&(e=Ya(e)),e}const nn={created(e,{modifiers:{lazy:t,trim:n,number:r}},i){e[Zn]=Ci(i);const s=r||i.props&&i.props.type==="number";gr(e,t?"change":"input",a=>{a.target.composing||e[Zn](ap(e.value,n,s))}),(n||s)&&gr(e,"change",()=>{e.value=ap(e.value,n,s)}),t||(gr(e,"compositionstart",L1),gr(e,"compositionend",sp),gr(e,"change",sp))},mounted(e,{value:t}){e.value=t??""},beforeUpdate(e,{value:t,oldValue:n,modifiers:{lazy:r,trim:i,number:s}},a){if(e[Zn]=Ci(a),e.composing)return;const o=(s||e.type==="number")&&!/^0\d/.test(e.value)?Ya(e.value):e.value,l=t??"";o!==l&&(document.activeElement===e&&e.type!=="range"&&(r&&t===n||i&&e.value.trim()===l)||(e.value=l))}},Or={deep:!0,created(e,t,n){e[Zn]=Ci(n),gr(e,"change",()=>{const r=e._modelValue,i=zs(e),s=e.checked,a=e[Zn];if(Ie(r)){const o=Su(r,i),l=o!==-1;if(s&&!l)a(r.concat(i));else if(!s&&l){const d=[...r];d.splice(o,1),a(d)}}else if(zi(r)){const o=new Set(r);s?o.add(i):o.delete(i),a(o)}else a(y0(e,s))})},mounted:op,beforeUpdate(e,t,n){e[Zn]=Ci(n),op(e,t,n)}};function op(e,{value:t,oldValue:n},r){e._modelValue=t;let i;if(Ie(t))i=Su(t,r.props.value)>-1;else if(zi(t))i=t.has(r.props.value);else{if(t===n)return;i=Ai(t,y0(e,!0))}e.checked!==i&&(e.checked=i)}const Tn={deep:!0,created(e,{value:t,modifiers:{number:n}},r){const i=zi(t);gr(e,"change",()=>{const s=Array.prototype.filter.call(e.options,a=>a.selected).map(a=>n?Ya(zs(a)):zs(a));e[Zn](e.multiple?i?new Set(s):s:s[0]),e._assigning=!0,Pg(()=>{e._assigning=!1})}),e[Zn]=Ci(r)},mounted(e,{value:t}){lp(e,t)},beforeUpdate(e,t,n){e[Zn]=Ci(n)},updated(e,{value:t}){e._assigning||lp(e,t)}};function lp(e,t){const n=e.multiple,r=Ie(t);if(!(n&&!r&&!zi(t))){for(let i=0,s=e.options.length;iString(d)===String(o)):a.selected=Su(t,o)>-1}else a.selected=t.has(o);else if(Ai(zs(a),t)){e.selectedIndex!==i&&(e.selectedIndex=i);return}}!n&&e.selectedIndex!==-1&&(e.selectedIndex=-1)}}function zs(e){return"_value"in e?e._value:e.value}function y0(e,t){const n=t?"_trueValue":"_falseValue";return n in e?e[n]:t}const W1=["ctrl","shift","alt","meta"],q1={stop:e=>e.stopPropagation(),prevent:e=>e.preventDefault(),self:e=>e.target!==e.currentTarget,ctrl:e=>!e.ctrlKey,shift:e=>!e.shiftKey,alt:e=>!e.altKey,meta:e=>!e.metaKey,left:e=>"button"in e&&e.button!==0,middle:e=>"button"in e&&e.button!==1,right:e=>"button"in e&&e.button!==2,exact:(e,t)=>W1.some(n=>e[`${n}Key`]&&!t.includes(n))},up=(e,t)=>{if(!e)return e;const n=e._withMods||(e._withMods={}),r=t.join(".");return n[r]||(n[r]=((i,...s)=>{for(let a=0;a{const t=V1().createApp(...e),{mount:n}=t;return t.mount=r=>{const i=j1(r);if(!i)return;const s=t._component;!Me(s)&&!s.render&&!s.template&&(s.template=i.innerHTML),i.nodeType===1&&(i.textContent="");const a=n(i,!1,H1(i));return i instanceof Element&&(i.removeAttribute("v-cloak"),i.setAttribute("data-v-app","")),a},t});function H1(e){if(e instanceof SVGElement)return"svg";if(typeof MathMLElement=="function"&&e instanceof MathMLElement)return"mathml"}function j1(e){return _t(e)?document.querySelector(e):e}/*! + * ONNX Runtime Web v1.25.0-dev.20260228-6e72d31970 + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. + */var Pu=Object.defineProperty,K1=Object.getOwnPropertyDescriptor,X1=Object.getOwnPropertyNames,Y1=Object.prototype.hasOwnProperty,Z1=(e=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(e,{get:(t,n)=>(typeof require<"u"?require:t)[n]}):e)(function(e){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+e+'" is not supported')}),re=(e,t)=>()=>(e&&(t=e(e=0)),t),Mi=(e,t)=>{for(var n in t)Pu(e,n,{get:t[n],enumerable:!0})},Q1=(e,t,n,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of X1(t))!Y1.call(e,i)&&i!==n&&Pu(e,i,{get:()=>t[i],enumerable:!(r=K1(t,i))||r.enumerable});return e},As=e=>Q1(Pu({},"__esModule",{value:!0}),e),Ji,cr,yi,cp,b0,w0=re(()=>{Ji=new Map,cr=[],yi=(e,t,n)=>{if(t&&typeof t.init=="function"&&typeof t.createInferenceSessionHandler=="function"){let r=Ji.get(e);if(r===void 0)Ji.set(e,{backend:t,priority:n});else{if(r.priority>n)return;if(r.priority===n&&r.backend!==t)throw new Error(`cannot register backend "${e}" using priority ${n}`)}if(n>=0){let i=cr.indexOf(e);i!==-1&&cr.splice(i,1);for(let s=0;s{let t=Ji.get(e);if(!t)return"backend not found.";if(t.initialized)return t.backend;if(t.aborted)return t.error;{let n=!!t.initPromise;try{return n||(t.initPromise=t.backend.init(e)),await t.initPromise,t.initialized=!0,t.backend}catch(r){return n||(t.error=`${r}`,t.aborted=!0),t.error}finally{delete t.initPromise}}},b0=async e=>{let t=e.executionProviders||[],n=t.map(l=>typeof l=="string"?l:l.name),r=n.length===0?cr:n,i,s=[],a=new Set;for(let l of r){let d=await cp(l);typeof d=="string"?s.push({name:l,err:d}):(i||(i=d),i===d&&a.add(l))}if(!i)throw new Error(`no available backend found. ERR: ${s.map(l=>`[${l.name}] ${l.err}`).join(", ")}`);for(let{name:l,err:d}of s)n.includes(l)&&console.warn(`removing requested execution provider "${l}" from session options because it is not available: ${d}`);let o=t.filter(l=>a.has(typeof l=="string"?l:l.name));return[i,new Proxy(e,{get:(l,d)=>d==="executionProviders"?o:Reflect.get(l,d)})]}}),J1=re(()=>{w0()}),_0,e$=re(()=>{_0="1.24.0-dev.20251116-b39e144322"}),qo,Tt,v0=re(()=>{e$(),qo="warning",Tt={wasm:{},webgl:{},webgpu:{},versions:{common:_0},set logLevel(e){if(e!==void 0){if(typeof e!="string"||["verbose","info","warning","error","fatal"].indexOf(e)===-1)throw new Error(`Unsupported logging level: ${e}`);qo=e}},get logLevel(){return qo}},Object.defineProperty(Tt,"logLevel",{enumerable:!0})}),Be,t$=re(()=>{v0(),Be=Tt}),x0,$0,n$=re(()=>{x0=(e,t)=>{let n=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);n.width=e.dims[3],n.height=e.dims[2];let r=n.getContext("2d");if(r!=null){let i,s;(t==null?void 0:t.tensorLayout)!==void 0&&t.tensorLayout==="NHWC"?(i=e.dims[2],s=e.dims[3]):(i=e.dims[3],s=e.dims[2]);let a=(t==null?void 0:t.format)!==void 0?t.format:"RGB",o=t==null?void 0:t.norm,l,d;o===void 0||o.mean===void 0?l=[255,255,255,255]:typeof o.mean=="number"?l=[o.mean,o.mean,o.mean,o.mean]:(l=[o.mean[0],o.mean[1],o.mean[2],0],o.mean[3]!==void 0&&(l[3]=o.mean[3])),o===void 0||o.bias===void 0?d=[0,0,0,0]:typeof o.bias=="number"?d=[o.bias,o.bias,o.bias,o.bias]:(d=[o.bias[0],o.bias[1],o.bias[2],0],o.bias[3]!==void 0&&(d[3]=o.bias[3]));let p=s*i,f=0,c=p,m=p*2,y=-1;a==="RGBA"?(f=0,c=p,m=p*2,y=p*3):a==="RGB"?(f=0,c=p,m=p*2):a==="RBG"&&(f=0,m=p,c=p*2);for(let w=0;w{let n=typeof document<"u"?document.createElement("canvas").getContext("2d"):new OffscreenCanvas(1,1).getContext("2d"),r;if(n!=null){let i,s,a;(t==null?void 0:t.tensorLayout)!==void 0&&t.tensorLayout==="NHWC"?(i=e.dims[2],s=e.dims[1],a=e.dims[3]):(i=e.dims[3],s=e.dims[2],a=e.dims[1]);let o=t!==void 0&&t.format!==void 0?t.format:"RGB",l=t==null?void 0:t.norm,d,p;l===void 0||l.mean===void 0?d=[255,255,255,255]:typeof l.mean=="number"?d=[l.mean,l.mean,l.mean,l.mean]:(d=[l.mean[0],l.mean[1],l.mean[2],255],l.mean[3]!==void 0&&(d[3]=l.mean[3])),l===void 0||l.bias===void 0?p=[0,0,0,0]:typeof l.bias=="number"?p=[l.bias,l.bias,l.bias,l.bias]:(p=[l.bias[0],l.bias[1],l.bias[2],0],l.bias[3]!==void 0&&(p[3]=l.bias[3]));let f=s*i;if(t!==void 0&&(t.format!==void 0&&a===4&&t.format!=="RGBA"||a===3&&t.format!=="RGB"&&t.format!=="BGR"))throw new Error("Tensor format doesn't match input tensor dims");let c=4,m=0,y=1,w=2,$=3,_=0,v=f,I=f*2,S=-1;o==="RGBA"?(_=0,v=f,I=f*2,S=f*3):o==="RGB"?(_=0,v=f,I=f*2):o==="RBG"&&(_=0,I=f,v=f*2),r=n.createImageData(i,s);for(let E=0;E{Uu(),aa=(e,t)=>{if(e===void 0)throw new Error("Image buffer must be defined");if(t.height===void 0||t.width===void 0)throw new Error("Image height and width must be defined");if(t.tensorLayout==="NHWC")throw new Error("NHWC Tensor layout is not supported yet");let{height:n,width:r}=t,i=t.norm??{mean:255,bias:0},s,a;typeof i.mean=="number"?s=[i.mean,i.mean,i.mean,i.mean]:s=[i.mean[0],i.mean[1],i.mean[2],i.mean[3]??255],typeof i.bias=="number"?a=[i.bias,i.bias,i.bias,i.bias]:a=[i.bias[0],i.bias[1],i.bias[2],i.bias[3]??0];let o=t.format!==void 0?t.format:"RGBA",l=t.tensorFormat!==void 0&&t.tensorFormat!==void 0?t.tensorFormat:"RGB",d=n*r,p=l==="RGBA"?new Float32Array(d*4):new Float32Array(d*3),f=4,c=0,m=1,y=2,w=3,$=0,_=d,v=d*2,I=-1;o==="RGB"&&(f=3,c=0,m=1,y=2,w=-1),l==="RGBA"?I=d*3:l==="RBG"?($=0,v=d,_=d*2):l==="BGR"&&(v=0,_=d,$=d*2);for(let S=0;S{let n=typeof HTMLImageElement<"u"&&e instanceof HTMLImageElement,r=typeof ImageData<"u"&&e instanceof ImageData,i=typeof ImageBitmap<"u"&&e instanceof ImageBitmap,s=typeof e=="string",a,o=t??{},l=()=>{if(typeof document<"u")return document.createElement("canvas");if(typeof OffscreenCanvas<"u")return new OffscreenCanvas(1,1);throw new Error("Canvas is not supported")},d=p=>typeof HTMLCanvasElement<"u"&&p instanceof HTMLCanvasElement||p instanceof OffscreenCanvas?p.getContext("2d"):null;if(n){let p=l();p.width=e.width,p.height=e.height;let f=d(p);if(f!=null){let c=e.height,m=e.width;if(t!==void 0&&t.resizedHeight!==void 0&&t.resizedWidth!==void 0&&(c=t.resizedHeight,m=t.resizedWidth),t!==void 0){if(o=t,t.tensorFormat!==void 0)throw new Error("Image input config format must be RGBA for HTMLImageElement");o.tensorFormat="RGBA",o.height=c,o.width=m}else o.tensorFormat="RGBA",o.height=c,o.width=m;f.drawImage(e,0,0),a=f.getImageData(0,0,m,c).data}else throw new Error("Can not access image data")}else if(r){let p,f;if(t!==void 0&&t.resizedWidth!==void 0&&t.resizedHeight!==void 0?(p=t.resizedHeight,f=t.resizedWidth):(p=e.height,f=e.width),t!==void 0&&(o=t),o.format="RGBA",o.height=p,o.width=f,t!==void 0){let c=l();c.width=f,c.height=p;let m=d(c);if(m!=null)m.putImageData(e,0,0),a=m.getImageData(0,0,f,p).data;else throw new Error("Can not access image data")}else a=e.data}else if(i){if(t===void 0)throw new Error("Please provide image config with format for Imagebitmap");let p=l();p.width=e.width,p.height=e.height;let f=d(p);if(f!=null){let c=e.height,m=e.width;return f.drawImage(e,0,0,m,c),a=f.getImageData(0,0,m,c).data,o.height=c,o.width=m,aa(a,o)}else throw new Error("Can not access image data")}else{if(s)return new Promise((p,f)=>{let c=l(),m=d(c);if(!e||!m)return f();let y=new Image;y.crossOrigin="Anonymous",y.src=e,y.onload=()=>{c.width=y.width,c.height=y.height,m.drawImage(y,0,0,c.width,c.height);let w=m.getImageData(0,0,c.width,c.height);o.height=c.height,o.width=c.width,p(aa(w.data,o))}});throw new Error("Input data provided is not supported - aborted tensor creation")}if(a!==void 0)return aa(a,o);throw new Error("Input data provided is not supported - aborted tensor creation")},T0=(e,t)=>{let{width:n,height:r,download:i,dispose:s}=t,a=[1,r,n,4];return new Qt({location:"texture",type:"float32",texture:e,dims:a,download:i,dispose:s})},I0=(e,t)=>{let{dataType:n,dims:r,download:i,dispose:s}=t;return new Qt({location:"gpu-buffer",type:n??"float32",gpuBuffer:e,dims:r,download:i,dispose:s})},C0=(e,t)=>{let{dataType:n,dims:r,download:i,dispose:s}=t;return new Qt({location:"ml-tensor",type:n??"float32",mlTensor:e,dims:r,download:i,dispose:s})},E0=(e,t,n)=>new Qt({location:"cpu-pinned",type:e,data:t,dims:n??[t.length]})}),qr,cs,Go,k0,i$=re(()=>{qr=new Map([["float32",Float32Array],["uint8",Uint8Array],["int8",Int8Array],["uint16",Uint16Array],["int16",Int16Array],["int32",Int32Array],["bool",Uint8Array],["float64",Float64Array],["uint32",Uint32Array],["int4",Uint8Array],["uint4",Uint8Array]]),cs=new Map([[Float32Array,"float32"],[Uint8Array,"uint8"],[Int8Array,"int8"],[Uint16Array,"uint16"],[Int16Array,"int16"],[Int32Array,"int32"],[Float64Array,"float64"],[Uint32Array,"uint32"]]),Go=!1,k0=()=>{if(!Go){Go=!0;let e=typeof BigInt64Array<"u"&&BigInt64Array.from,t=typeof BigUint64Array<"u"&&BigUint64Array.from,n=globalThis.Float16Array,r=typeof n<"u"&&n.from;e&&(qr.set("int64",BigInt64Array),cs.set(BigInt64Array,"int64")),t&&(qr.set("uint64",BigUint64Array),cs.set(BigUint64Array,"uint64")),r?(qr.set("float16",n),cs.set(n,"float16")):qr.set("float16",Uint16Array)}}}),z0,A0,s$=re(()=>{Uu(),z0=e=>{let t=1;for(let n=0;n{switch(e.location){case"cpu":return new Qt(e.type,e.data,t);case"cpu-pinned":return new Qt({location:"cpu-pinned",data:e.data,type:e.type,dims:t});case"texture":return new Qt({location:"texture",texture:e.texture,type:e.type,dims:t});case"gpu-buffer":return new Qt({location:"gpu-buffer",gpuBuffer:e.gpuBuffer,type:e.type,dims:t});case"ml-tensor":return new Qt({location:"ml-tensor",mlTensor:e.mlTensor,type:e.type,dims:t});default:throw new Error(`tensorReshape: tensor location ${e.location} is not supported`)}}}),Qt,Uu=re(()=>{n$(),r$(),i$(),s$(),Qt=class{constructor(e,t,n){k0();let r,i;if(typeof e=="object"&&"location"in e)switch(this.dataLocation=e.location,r=e.type,i=e.dims,e.location){case"cpu-pinned":{let a=qr.get(r);if(!a)throw new TypeError(`unsupported type "${r}" to create tensor from pinned buffer`);if(!(e.data instanceof a))throw new TypeError(`buffer should be of type ${a.name}`);this.cpuData=e.data;break}case"texture":{if(r!=="float32")throw new TypeError(`unsupported type "${r}" to create tensor from texture`);this.gpuTextureData=e.texture,this.downloader=e.download,this.disposer=e.dispose;break}case"gpu-buffer":{if(r!=="float32"&&r!=="float16"&&r!=="int32"&&r!=="int64"&&r!=="uint32"&&r!=="uint8"&&r!=="bool"&&r!=="uint4"&&r!=="int4")throw new TypeError(`unsupported type "${r}" to create tensor from gpu buffer`);this.gpuBufferData=e.gpuBuffer,this.downloader=e.download,this.disposer=e.dispose;break}case"ml-tensor":{if(r!=="float32"&&r!=="float16"&&r!=="int32"&&r!=="int64"&&r!=="uint32"&&r!=="uint64"&&r!=="int8"&&r!=="uint8"&&r!=="bool"&&r!=="uint4"&&r!=="int4")throw new TypeError(`unsupported type "${r}" to create tensor from MLTensor`);this.mlTensorData=e.mlTensor,this.downloader=e.download,this.disposer=e.dispose;break}default:throw new Error(`Tensor constructor: unsupported location '${this.dataLocation}'`)}else{let a,o;if(typeof e=="string")if(r=e,o=n,e==="string"){if(!Array.isArray(t))throw new TypeError("A string tensor's data must be a string array.");a=t}else{let l=qr.get(e);if(l===void 0)throw new TypeError(`Unsupported tensor type: ${e}.`);if(Array.isArray(t)){if(e==="float16"&&l===Uint16Array||e==="uint4"||e==="int4")throw new TypeError(`Creating a ${e} tensor from number array is not supported. Please use ${l.name} as data.`);e==="uint64"||e==="int64"?a=l.from(t,BigInt):a=l.from(t)}else if(t instanceof l)a=t;else if(t instanceof Uint8ClampedArray)if(e==="uint8")a=Uint8Array.from(t);else throw new TypeError("A Uint8ClampedArray tensor's data must be type of uint8");else if(e==="float16"&&t instanceof Uint16Array&&l!==Uint16Array)a=new globalThis.Float16Array(t.buffer,t.byteOffset,t.length);else throw new TypeError(`A ${r} tensor's data must be type of ${l}`)}else if(o=t,Array.isArray(e)){if(e.length===0)throw new TypeError("Tensor type cannot be inferred from an empty array.");let l=typeof e[0];if(l==="string")r="string",a=e;else if(l==="boolean")r="bool",a=Uint8Array.from(e);else throw new TypeError(`Invalid element type of data array: ${l}.`)}else if(e instanceof Uint8ClampedArray)r="uint8",a=Uint8Array.from(e);else{let l=cs.get(e.constructor);if(l===void 0)throw new TypeError(`Unsupported type for tensor data: ${e.constructor}.`);r=l,a=e}if(o===void 0)o=[a.length];else if(!Array.isArray(o))throw new TypeError("A tensor's dims must be a number array");i=o,this.cpuData=a,this.dataLocation="cpu"}let s=z0(i);if(this.cpuData&&s!==this.cpuData.length&&!((r==="uint4"||r==="int4")&&Math.ceil(s/2)===this.cpuData.length))throw new Error(`Tensor's size(${s}) does not match data length(${this.cpuData.length}).`);this.type=r,this.dims=i,this.size=s}static async fromImage(e,t){return S0(e,t)}static fromTexture(e,t){return T0(e,t)}static fromGpuBuffer(e,t){return I0(e,t)}static fromMLTensor(e,t){return C0(e,t)}static fromPinnedBuffer(e,t,n){return E0(e,t,n)}toDataURL(e){return x0(this,e)}toImageData(e){return $0(this,e)}get data(){if(this.ensureValid(),!this.cpuData)throw new Error("The data is not on CPU. Use `getData()` to download GPU data to CPU, or use `texture` or `gpuBuffer` property to access the GPU data directly.");return this.cpuData}get location(){return this.dataLocation}get texture(){if(this.ensureValid(),!this.gpuTextureData)throw new Error("The data is not stored as a WebGL texture.");return this.gpuTextureData}get gpuBuffer(){if(this.ensureValid(),!this.gpuBufferData)throw new Error("The data is not stored as a WebGPU buffer.");return this.gpuBufferData}get mlTensor(){if(this.ensureValid(),!this.mlTensorData)throw new Error("The data is not stored as a WebNN MLTensor.");return this.mlTensorData}async getData(e){switch(this.ensureValid(),this.dataLocation){case"cpu":case"cpu-pinned":return this.data;case"texture":case"gpu-buffer":case"ml-tensor":{if(!this.downloader)throw new Error("The current tensor is not created with a specified data downloader.");if(this.isDownloading)throw new Error("The current tensor is being downloaded.");try{this.isDownloading=!0;let t=await this.downloader();return this.downloader=void 0,this.dataLocation="cpu",this.cpuData=t,e&&this.disposer&&(this.disposer(),this.disposer=void 0),t}finally{this.isDownloading=!1}}default:throw new Error(`cannot get data from location: ${this.dataLocation}`)}}dispose(){if(this.isDownloading)throw new Error("The current tensor is being downloaded.");this.disposer&&(this.disposer(),this.disposer=void 0),this.cpuData=void 0,this.gpuTextureData=void 0,this.gpuBufferData=void 0,this.mlTensorData=void 0,this.downloader=void 0,this.isDownloading=void 0,this.dataLocation="none"}ensureValid(){if(this.dataLocation==="none")throw new Error("The tensor is disposed.")}reshape(e){if(this.ensureValid(),this.downloader||this.disposer)throw new Error("Cannot reshape a tensor that owns GPU resource.");return A0(this,e)}}}),yn,M0=re(()=>{Uu(),yn=Qt}),Da,Vo,Bn,wn,Kr,Xr,R0=re(()=>{v0(),Da=(e,t)=>{(typeof Tt.trace>"u"?!Tt.wasm.trace:!Tt.trace)||console.timeStamp(`${e}::ORT::${t}`)},Vo=(e,t)=>{var i;let n=((i=new Error().stack)==null?void 0:i.split(/\r\n|\r|\n/g))||[],r=!1;for(let s=0;s{(typeof Tt.trace>"u"?!Tt.wasm.trace:!Tt.trace)||Vo("BEGIN",e)},wn=e=>{(typeof Tt.trace>"u"?!Tt.wasm.trace:!Tt.trace)||Vo("END",e)},Kr=e=>{(typeof Tt.trace>"u"?!Tt.wasm.trace:!Tt.trace)||console.time(`ORT::${e}`)},Xr=e=>{(typeof Tt.trace>"u"?!Tt.wasm.trace:!Tt.trace)||console.timeEnd(`ORT::${e}`)}}),O0,a$=re(()=>{w0(),M0(),R0(),O0=class N0{constructor(t){this.handler=t}async run(t,n,r){Bn(),Kr("InferenceSession.run");let i={},s={};if(typeof t!="object"||t===null||t instanceof yn||Array.isArray(t))throw new TypeError("'feeds' must be an object that use input names as keys and OnnxValue as corresponding values.");let a=!0;if(typeof n=="object"){if(n===null)throw new TypeError("Unexpected argument[1]: cannot be null.");if(n instanceof yn)throw new TypeError("'fetches' cannot be a Tensor");if(Array.isArray(n)){if(n.length===0)throw new TypeError("'fetches' cannot be an empty array.");a=!1;for(let d of n){if(typeof d!="string")throw new TypeError("'fetches' must be a string array or an object.");if(this.outputNames.indexOf(d)===-1)throw new RangeError(`'fetches' contains invalid output name: ${d}.`);i[d]=null}if(typeof r=="object"&&r!==null)s=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else{let d=!1,p=Object.getOwnPropertyNames(n);for(let f of this.outputNames)if(p.indexOf(f)!==-1){let c=n[f];(c===null||c instanceof yn)&&(d=!0,a=!1,i[f]=c)}if(d){if(typeof r=="object"&&r!==null)s=r;else if(typeof r<"u")throw new TypeError("'options' must be an object.")}else s=n}}else if(typeof n<"u")throw new TypeError("Unexpected argument[1]: must be 'fetches' or 'options'.");for(let d of this.inputNames)if(typeof t[d]>"u")throw new Error(`input '${d}' is missing in 'feeds'.`);if(a)for(let d of this.outputNames)i[d]=null;let o=await this.handler.run(t,i,s),l={};for(let d in o)if(Object.hasOwnProperty.call(o,d)){let p=o[d];p instanceof yn?l[d]=p:l[d]=new yn(p.type,p.data,p.dims)}return Xr("InferenceSession.run"),wn(),l}async release(){return this.handler.dispose()}static async create(t,n,r,i){Bn(),Kr("InferenceSession.create");let s,a={};if(typeof t=="string"){if(s=t,typeof n=="object"&&n!==null)a=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof Uint8Array){if(s=t,typeof n=="object"&&n!==null)a=n;else if(typeof n<"u")throw new TypeError("'options' must be an object.")}else if(t instanceof ArrayBuffer||typeof SharedArrayBuffer<"u"&&t instanceof SharedArrayBuffer){let p=t,f=0,c=t.byteLength;if(typeof n=="object"&&n!==null)a=n;else if(typeof n=="number"){if(f=n,!Number.isSafeInteger(f))throw new RangeError("'byteOffset' must be an integer.");if(f<0||f>=p.byteLength)throw new RangeError(`'byteOffset' is out of range [0, ${p.byteLength}).`);if(c=t.byteLength-f,typeof r=="number"){if(c=r,!Number.isSafeInteger(c))throw new RangeError("'byteLength' must be an integer.");if(c<=0||f+c>p.byteLength)throw new RangeError(`'byteLength' is out of range (0, ${p.byteLength-f}].`);if(typeof i=="object"&&i!==null)a=i;else if(typeof i<"u")throw new TypeError("'options' must be an object.")}else if(typeof r<"u")throw new TypeError("'byteLength' must be a number.")}else if(typeof n<"u")throw new TypeError("'options' must be an object.");s=new Uint8Array(p,f,c)}else throw new TypeError("Unexpected argument[0]: must be 'path' or 'buffer'.");let[o,l]=await b0(a),d=await o.createInferenceSessionHandler(s,l);return Xr("InferenceSession.create"),wn(),new N0(d)}startProfiling(){this.handler.startProfiling()}endProfiling(){this.handler.endProfiling()}get inputNames(){return this.handler.inputNames}get outputNames(){return this.handler.outputNames}get inputMetadata(){return this.handler.inputMetadata}get outputMetadata(){return this.handler.outputMetadata}}}),Lu,o$=re(()=>{a$(),Lu=O0}),l$=re(()=>{}),u$=re(()=>{}),d$=re(()=>{}),c$=re(()=>{}),p$={};Mi(p$,{InferenceSession:()=>Lu,TRACE:()=>Da,TRACE_EVENT_BEGIN:()=>Kr,TRACE_EVENT_END:()=>Xr,TRACE_FUNC_BEGIN:()=>Bn,TRACE_FUNC_END:()=>wn,Tensor:()=>yn,env:()=>Be,registerBackend:()=>yi});var on=re(()=>{J1(),t$(),o$(),M0(),l$(),u$(),R0(),d$(),c$()}),Wu=re(()=>{}),B0={};Mi(B0,{default:()=>D0});var Fo,Ho,D0,f$=re(()=>{var e;qw(),ei(),qu(),Fo="ort-wasm-proxy-worker",Ho=((e=globalThis.self)==null?void 0:e.name)===Fo,Ho&&(self.onmessage=t=>{let{type:n,in:r}=t.data;try{switch(n){case"init-wasm":Gu(r.wasm).then(()=>{ad(r).then(()=>{postMessage({type:n})},i=>{postMessage({type:n,err:i})})},i=>{postMessage({type:n,err:i})});break;case"init-ep":{let{epName:i,env:s}=r;od(s,i).then(()=>{postMessage({type:n})},a=>{postMessage({type:n,err:a})});break}case"copy-from":{let{buffer:i}=r,s=Va(i);postMessage({type:n,out:s});break}case"create":{let{model:i,options:s}=r;ld(i,s).then(a=>{postMessage({type:n,out:a})},a=>{postMessage({type:n,err:a})});break}case"release":ud(r),postMessage({type:n});break;case"run":{let{sessionId:i,inputIndices:s,inputs:a,outputIndices:o,options:l}=r;dd(i,s,a,o,new Array(o.length).fill(null),l).then(d=>{d.some(p=>p[3]!=="cpu")?postMessage({type:n,err:"Proxy does not support non-cpu tensor location."}):postMessage({type:n,out:d},pd([...a,...d]))},d=>{postMessage({type:n,err:d})});break}case"end-profiling":cd(r),postMessage({type:n});break;default:}}catch(i){postMessage({type:n,err:i})}}),D0=Ho?null:t=>new Worker(t??Yt,{type:"module",name:Fo})}),P0={};Mi(P0,{default:()=>U0});async function pp(e={}){var Cc,Ec;var t=e,n=!!globalThis.window,r=!!globalThis.WorkerGlobalScope,i=r&&((Cc=self.name)==null?void 0:Cc.startsWith("em-pthread"));t.mountExternalData=(u,h)=>{u.startsWith("./")&&(u=u.substring(2)),(t.Zc||(t.Zc=new Map)).set(u,h)},t.unmountExternalData=()=>{delete t.Zc},globalThis.SharedArrayBuffer??new WebAssembly.Memory({initial:0,maximum:0,ae:!0}).buffer.constructor;let s=u=>async(...h)=>{var b;try{if(t.$c)throw Error("Session already started");let g=t.$c={Nd:h[0],errors:[]},T=await u(...h);if(t.$c!==g)throw Error("Session mismatch");(b=t.gd)==null||b.flush();let z=g.errors;if(0G),0{if(u==="webgpu"){[t.gd,t.Dd,t.Hd,t.jd,t.Gd,t.ac,t.Id,t.Kd,t.Ed,t.Fd,t.Jd]=h;let b=t.gd;t.jsepRegisterBuffer=(g,T,z,B)=>b.registerBuffer(g,T,z,B),t.jsepGetBuffer=g=>b.getBuffer(g),t.jsepCreateDownloader=(g,T,z)=>b.createDownloader(g,T,z),t.jsepOnCreateSession=g=>{b.onCreateSession(g)},t.jsepOnReleaseSession=g=>{b.onReleaseSession(g)},t.jsepOnRunStart=g=>b.onRunStart(g),t.Ld=(g,T)=>{b.upload(g,T)}}else if(u==="webnn"){let b=h[0];[t.Zd,t.vd,t.webnnEnsureTensor,t.wd,t.webnnDownloadTensor,t.Yd,t.webnnEnableTraceEvent]=h.slice(1),t.webnnReleaseTensorId=t.vd,t.webnnUploadTensor=t.wd,t.webnnRegisterMLContext=t.Yd,t.webnnOnRunStart=g=>b.onRunStart(g),t.webnnOnRunEnd=b.onRunEnd.bind(b),t.webnnOnReleaseSession=g=>{b.onReleaseSession(g)},t.webnnCreateMLTensorDownloader=(g,T)=>b.createMLTensorDownloader(g,T),t.webnnRegisterMLTensor=(g,T,z,B)=>b.registerMLTensor(g,T,z,B),t.webnnCreateMLContext=g=>b.createMLContext(g),t.webnnRegisterMLConstant=(g,T,z,B,G,de)=>b.registerMLConstant(g,T,z,B,G,t.Zc,de),t.webnnRegisterGraphInput=b.registerGraphInput.bind(b),t.webnnIsGraphInput=b.isGraphInput.bind(b),t.webnnRegisterGraphOutput=b.registerGraphOutput.bind(b),t.webnnIsGraphOutput=b.isGraphOutput.bind(b),t.webnnCreateTemporaryTensor=b.createTemporaryTensor.bind(b),t.webnnIsGraphInputOutputTypeSupported=b.isGraphInputOutputTypeSupported.bind(b)}};let a=()=>{let u=h=>(...b)=>{let g=Mt;return b=h(...b),Mt!=g?new Promise((T,z)=>{li={resolve:T,reject:z}}):b};(()=>{for(let h of["_OrtAppendExecutionProvider","_OrtCreateSession","_OrtRun","_OrtRunWithBinding","_OrtBindInput"])t[h]=u(t[h])})(),s!==void 0&&(t._OrtRun=s(t._OrtRun),t._OrtRunWithBinding=s(t._OrtRunWithBinding)),a=void 0};t.asyncInit=()=>{a==null||a()};var o,l,d=(u,h)=>{throw h},p=import.meta.url,f="";if(n||r){try{f=new URL(".",p).href}catch{}r&&(l=u=>{var h=new XMLHttpRequest;return h.open("GET",u,!1),h.responseType="arraybuffer",h.send(null),new Uint8Array(h.response)}),o=async u=>{if(O(u))return new Promise((b,g)=>{var T=new XMLHttpRequest;T.open("GET",u,!0),T.responseType="arraybuffer",T.onload=()=>{T.status==200||T.status==0&&T.response?b(T.response):g(T.status)},T.onerror=g,T.send(null)});var h=await fetch(u,{credentials:"same-origin"});if(h.ok)return h.arrayBuffer();throw Error(h.status+" : "+h.url)}}var c,m,y,w,$,_,v=console.log.bind(console),I=console.error.bind(console),S=v,E=I,R=!1,O=u=>u.startsWith("file://");function x(){ke.buffer!=q.buffer&&P()}if(i){let u=function(h){try{var b=h.data,g=b.Uc;if(g==="load"){let T=[];self.onmessage=z=>T.push(z),_=()=>{postMessage({Uc:"loaded"});for(let z of T)u(z);self.onmessage=u};for(let z of b.Ad)t[z]&&!t[z].proxy||(t[z]=(...B)=>{postMessage({Uc:"callHandler",zd:z,args:B})},z=="print"&&(S=t[z]),z=="printErr"&&(E=t[z]));ke=b.Vd,P(),m=b.Wd,lt(),ta()}else if(g==="run"){(function(T){var z=(x(),ue)[T+52>>>2>>>0];T=(x(),ue)[T+56>>>2>>>0],Bd(z,z-T),Ae(z)})(b.Tc),So(b.Tc,0,0,1,0,0),fe(),Gi(b.Tc),U||(zd(),U=!0);try{He(b.Pd,b.dd)}catch(T){if(T!="unwind")throw T}}else b.target!=="setimmediate"&&(g==="checkMailbox"?U&&St():g&&(E(`worker: received unknown command ${g}`),E(b)))}catch(T){throw Ad(),T}};var U=!1;self.onunhandledrejection=h=>{throw h.reason||h},self.onmessage=u}var q,J,se,ce,L,ue,ie,te,he,Q,ve,Y=!1;function P(){var u=ke.buffer;t.HEAP8=q=new Int8Array(u),se=new Int16Array(u),t.HEAPU8=J=new Uint8Array(u),ce=new Uint16Array(u),t.HEAP32=L=new Int32Array(u),t.HEAPU32=ue=new Uint32Array(u),ie=new Float32Array(u),te=new Float64Array(u),he=new BigInt64Array(u),Q=new BigUint64Array(u)}function ee(){Y=!0,i?_():Gn.tb()}function ne(u){throw E(u="Aborted("+u+")"),R=!0,u=new WebAssembly.RuntimeError(u+". Build with -sASSERTIONS for more info."),$==null||$(u),u}function Ue(){return{a:{ma:D_,hb:B_,g:$t,J:qt,f:Dn,o:ni,h:Ds,ha:oo,b:lo,T:uo,Ia:xr,n:co,_:ri,Ya:Di,Ea:$r,Ga:Pi,Za:Us,Wa:Ls,Pa:Ws,Va:ii,ka:si,Fa:qs,Ca:Gs,Xa:Vs,Da:Fs,cb:po,ea:fo,xa:ho,va:go,da:k,O:F,H:pe,wa:$e,Z:Kt,ya:Ks,Sa:Xs,Aa:sr,Ja:oi,ta:Un,fa:Ln,Ra:Gi,$a:kt,R:zr,r:Fi,c:ai,ib:lr,y:ur,M:Ys,D:Hi,m:wo,t:Zs,jb:_o,I:qn,S:c_,j:p_,v:f_,q:h_,l:m_,Ma:g_,Na:y_,Oa:b_,Ka:yd,La:bd,ua:wd,eb:__,bb:x_,u:$_,aa:S_,ga:T_,ab:v_,V:I_,_a:C_,Ba:E_,F:w_,U:k_,la:Js,za:A_,gb:z_,fb:M_,Ta:$d,Ua:Sd,Ha:K,$:Td,ja:Id,Qa:Cd,ia:Ed,lb:_v,na:fv,mb:wv,oa:pv,G:rv,d:W_,s:U_,w:P_,B:Z_,pb:uv,K:ev,x:G_,pa:dv,X:hv,ba:lv,nb:bv,ob:yv,ra:iv,qa:ov,qb:sv,N:tv,Y:cv,e:q_,A:V_,k:L_,kb:vv,p:H_,z:j_,C:F_,E:K_,L:Q_,rb:nv,Q:mv,ca:J_,W:gv,sb:Y_,sa:X_,P:av,i:O_,a:ke,db:H}}}async function lt(){function u(g,T){var z=Gn=g.exports;g={};for(let[B,G]of Object.entries(z))typeof G=="function"?(z=Tr(G),g[B]=z):g[B]=G;return Gn=g,Gn=(function(){var B=Gn,G=ge=>ze=>ge(ze)>>>0,de=ge=>()=>ge()>>>0;return(B=Object.assign({},B)).ub=G(B.ub),B.Yb=de(B.Yb),B._b=G(B._b),B.mc=G(B.mc),B.nc=de(B.nc),B.rc=G(B.rc),B})(),oe.push(Gn.$b),kd=(g=Gn).ub,zd=g.vb,t._OrtInit=g.wb,t._OrtGetLastError=g.xb,t._OrtCreateSessionOptions=g.yb,t._OrtAppendExecutionProvider=g.zb,t._OrtAddFreeDimensionOverride=g.Ab,t._OrtAddSessionConfigEntry=g.Bb,t._OrtReleaseSessionOptions=g.Cb,t._OrtCreateSession=g.Db,t._OrtReleaseSession=g.Eb,t._OrtGetInputOutputCount=g.Fb,t._OrtGetInputOutputMetadata=g.Gb,t._OrtFree=g.Hb,t._OrtCreateTensor=g.Ib,t._OrtGetTensorData=g.Jb,t._OrtReleaseTensor=g.Kb,t._OrtCreateRunOptions=g.Lb,t._OrtAddRunConfigEntry=g.Mb,t._OrtReleaseRunOptions=g.Nb,t._OrtCreateBinding=g.Ob,t._OrtBindInput=g.Pb,t._OrtBindOutput=g.Qb,t._OrtClearBoundOutputs=g.Rb,t._OrtReleaseBinding=g.Sb,t._OrtRunWithBinding=g.Tb,t._OrtRun=g.Ub,t._OrtEndProfiling=g.Vb,t._JsepOutput=g.Wb,t._JsepGetNodeName=g.Xb,ea=g.Yb,$n=t._free=g.Zb,Ki=t._malloc=g._b,So=g.bc,Ad=g.cc,Md=g.dc,Rd=g.ec,To=g.fc,Od=g.gc,Nd=g.hc,Ne=g.ic,Xi=g.jc,Bd=g.kc,Ae=g.lc,Io=g.mc,Re=g.nc,Dd=g.oc,Co=g.pc,Pd=g.qc,Ud=g.rc,Ld=g.sc,Eo=g.tc,Wd=g.uc,qd=g.vc,Gd=g.wc,Vd=g.xc,Fd=g.yc,Hd=g.zc,jd=g.Ac,Kd=g.Bc,Xd=g.Cc,Yd=g.Dc,Zd=g.Ec,Qd=g.Fc,Jd=g.Gc,ec=g.Hc,tc=g.Ic,nc=g.Jc,rc=g.Kc,ic=g.Lc,sc=g.Mc,ac=g.Nc,oc=g.Oc,lc=g.Pc,uc=g.Rc,dc=g.Sc,cc=g.bd,pc=g.cd,fc=g.hd,hc=g.ld,mc=g.md,gc=g.nd,yc=g.od,bc=g.pd,wc=g.qd,_c=g.rd,vc=g.sd,xc=g.xd,$c=g.Rd,Sc=g.Sd,Tc=g.Td,Ic=g.Ud,m=T,Gn}var h,b=Ue();return t.instantiateWasm?new Promise(g=>{t.instantiateWasm(b,(T,z)=>{g(u(T,z))})}):i?u(new WebAssembly.Instance(m,Ue()),m):(ve??(ve=t.locateFile?t.locateFile?t.locateFile("ort-wasm-simd-threaded.jsep.wasm",f):f+"ort-wasm-simd-threaded.jsep.wasm":new URL("/assets/ort-wasm-simd-threaded.jsep-CTuMJ5w1.wasm",import.meta.url).href),h=await(async function(g){var T=ve;if(!c&&!O(T))try{var z=fetch(T,{credentials:"same-origin"});return await WebAssembly.instantiateStreaming(z,g)}catch(B){E(`wasm streaming compile failed: ${B}`),E("falling back to ArrayBuffer instantiation")}return(async function(B,G){try{var de=await(async function(ge){if(!c)try{var ze=await o(ge);return new Uint8Array(ze)}catch{}if(ge==ve&&c)ge=new Uint8Array(c);else{if(!l)throw"both async and sync fetching of the wasm failed";ge=l(ge)}return ge})(B);return await WebAssembly.instantiate(de,G)}catch(ge){E(`failed to asynchronously prepare wasm: ${ge}`),ne(ge)}})(T,g)})(b),u(h.instance,h.module))}class Xe{constructor(h){kc(this,"name","ExitStatus");this.message=`Program terminated with exit(${h})`,this.status=h}}var Ke=u=>{u.terminate(),u.onmessage=()=>{}},ft=[],ut=0,ht=null,A=u=>{le.length==0&&(me(),xe(le[0]));var h=le.pop();if(!h)return 6;ae.push(h),X[u.Tc]=h,h.Tc=u.Tc;var b={Uc:"run",Pd:u.Od,dd:u.dd,Tc:u.Tc};return h.postMessage(b,u.ud),0},N=0,D=(u,h,...b)=>{var g,T=16*b.length,z=Re(),B=Io(T),G=B>>>3;for(g of b)typeof g=="bigint"?((x(),he)[G++>>>0]=1n,(x(),he)[G++>>>0]=g):((x(),he)[G++>>>0]=0n,(x(),te)[G++>>>0]=g);return u=Md(u,0,T,B,h),Ae(z),u};function H(u){if(i)return D(0,1,u);if(y=u,!(0{if(y=u,i)throw Z(u),"unwind";H(u)},le=[],ae=[],oe=[],X={},Se=u=>{var h=u.Tc;delete X[h],le.push(u),ae.splice(ae.indexOf(u),1),u.Tc=0,Rd(h)};function fe(){oe.forEach(u=>u())}var xe=u=>new Promise(h=>{u.onmessage=T=>{var z=T.data;if(T=z.Uc,z.ad&&z.ad!=ea()){var B=X[z.ad];B?B.postMessage(z,z.ud):E(`Internal error! Worker sent a message "${T}" to target pthread ${z.ad}, but that thread no longer exists!`)}else T==="checkMailbox"?St():T==="spawnThread"?A(z):T==="cleanupThread"?ir(()=>{Se(X[z.Qd])}):T==="loaded"?(u.loaded=!0,h(u)):z.target==="setimmediate"?u.postMessage(z):T==="uncaughtException"?u.onerror(z.error):T==="callHandler"?t[z.zd](...z.args):T&&E(`worker sent an unknown command ${T}`)},u.onerror=T=>{throw E(`worker sent an error! ${T.filename}:${T.lineno}: ${T.message}`),T};var b,g=[];for(b of[])t.propertyIsEnumerable(b)&&g.push(b);u.postMessage({Uc:"load",Ad:g,Vd:ke,Wd:m})});function me(){var u=new Worker((()=>{let h=URL;return import.meta.url>"file:"&&import.meta.url<"file;"?new h("ort.bundle.min.mjs",import.meta.url):new URL(import.meta.url)})(),{type:"module",workerData:"em-pthread",name:"em-pthread"});le.push(u)}var ke,He=(u,h)=>{N=0,u=Eo(u,h),0>>=0);return(x(),q)[h.Vc+12>>>0]==0&&(vn(h,!0),mt--),vr(h,!1),Le.push(h),Ud(u)}var gt=0,qt=()=>{Ne(0,0);var u=Le.pop();Dd(u.ed),gt=0};function vn(u,h){h=h?1:0,(x(),q)[u.Vc+12>>>0]=h}function vr(u,h){h=h?1:0,(x(),q)[u.Vc+13>>>0]=h}class vt{constructor(h){this.ed=h,this.Vc=h-24}}var Ct=u=>{var h=gt;if(!h)return Xi(0),0;var b=new vt(h);(x(),ue)[b.Vc+16>>>2>>>0]=h;var g=(x(),ue)[b.Vc+4>>>2>>>0];if(!g)return Xi(0),h;for(var T of u){if(T===0||T===g)break;if(Pd(T,g,b.Vc+16))return Xi(T),h}return Xi(g),h};function Dn(){return Ct([])}function ni(u){return Ct([u>>>0])}function Ds(u,h,b,g){return Ct([u>>>0,h>>>0,b>>>0,g>>>0])}var oo=()=>{var u=Le.pop();u||ne("no exception to throw");var h=u.ed;throw(x(),q)[u.Vc+13>>>0]==0&&(Le.push(u),vr(u,!0),vn(u,!1),mt++),Co(h),gt=h};function lo(u,h,b){var g=new vt(u>>>=0);throw h>>>=0,b>>>=0,(x(),ue)[g.Vc+16>>>2>>>0]=0,(x(),ue)[g.Vc+4>>>2>>>0]=h,(x(),ue)[g.Vc+8>>>2>>>0]=b,Co(u),mt++,gt=u}var uo=()=>mt;function Oi(u,h,b,g){return i?D(2,1,u,h,b,g):xr(u,h,b,g)}function xr(u,h,b,g){if(u>>>=0,h>>>=0,b>>>=0,g>>>=0,!globalThis.SharedArrayBuffer)return 6;var T=[];return i&&T.length===0?Oi(u,h,b,g):(u={Od:b,Tc:u,dd:g,ud:T},i?(u.Uc="spawnThread",postMessage(u,T),0):A(u))}function co(u){throw gt||(gt=u>>>0),gt}var Ni=globalThis.TextDecoder&&new TextDecoder,Ps=(u,h,b,g)=>{if(b=h+b,g)return b;for(;u[h]&&!(h>=b);)++h;return h},Bi=(u,h=0,b,g)=>{if(16<(b=Ps(u,h>>>=0,b,g))-h&&u.buffer&&Ni)return Ni.decode(u.buffer instanceof ArrayBuffer?u.subarray(h,b):u.slice(h,b));for(g="";h(T=(240&T)==224?(15&T)<<12|z<<6|B:(7&T)<<18|z<<12|B<<6|63&u[h++])?g+=String.fromCharCode(T):(T-=65536,g+=String.fromCharCode(55296|T>>10,56320|1023&T))}}else g+=String.fromCharCode(T)}return g},dt=(u,h,b)=>(u>>>=0)?Bi((x(),J),u,h,b):"";function ri(u,h,b){return i?D(3,1,u,h,b):0}function Di(u,h){if(i)return D(4,1,u,h)}function $r(u,h){if(i)return D(5,1,u,h)}function Pi(u,h,b){if(i)return D(6,1,u,h,b)}function Us(u,h,b){return i?D(7,1,u,h,b):0}function Ls(u,h){if(i)return D(8,1,u,h)}function Ws(u,h,b){if(i)return D(9,1,u,h,b)}function ii(u,h,b,g){if(i)return D(10,1,u,h,b,g)}function si(u,h,b,g){if(i)return D(11,1,u,h,b,g)}function qs(u,h,b,g){if(i)return D(12,1,u,h,b,g)}function Gs(u){if(i)return D(13,1,u)}function Vs(u,h){if(i)return D(14,1,u,h)}function Fs(u,h,b){if(i)return D(15,1,u,h,b)}var po=()=>ne(""),tn=u=>{u>>>=0;for(var h="";;){var b=(x(),J)[u++>>>0];if(!b)return h;h+=String.fromCharCode(b)}},Ui={},Li={},rr=class extends Error{constructor(u){super(u),this.name="BindingError"}};function ln(u,h,b={}){return(function(g,T,z={}){var B=T.name;if(!g)throw new rr(`type "${B}" must have a positive integer typeid pointer`);if(Li.hasOwnProperty(g)){if(z.Bd)return;throw new rr(`Cannot register type '${B}' twice`)}Li[g]=T,Ui.hasOwnProperty(g)&&(T=Ui[g],delete Ui[g],T.forEach(G=>G()))})(u,h,b)}var Hs=(u,h,b)=>{switch(h){case 1:return b?g=>(x(),q)[g>>>0]:g=>(x(),J)[g>>>0];case 2:return b?g=>(x(),se)[g>>>1>>>0]:g=>(x(),ce)[g>>>1>>>0];case 4:return b?g=>(x(),L)[g>>>2>>>0]:g=>(x(),ue)[g>>>2>>>0];case 8:return b?g=>(x(),he)[g>>>3>>>0]:g=>(x(),Q)[g>>>3>>>0];default:throw new TypeError(`invalid integer width (${h}): ${u}`)}};function fo(u,h,b,g,T){u>>>=0,b>>>=0,h=tn(h>>>0);let z=B=>B;if(g=g===0n){let B=8*b;z=G=>BigInt.asUintN(B,G),T=z(T)}ln(u,{name:h,Qc:z,Xc:(B,G)=>(typeof G=="number"&&(G=BigInt(G)),G),Wc:Hs(h,b,!g),Yc:null})}function ho(u,h,b,g){ln(u>>>=0,{name:h=tn(h>>>0),Qc:function(T){return!!T},Xc:function(T,z){return z?b:g},Wc:function(T){return this.Qc((x(),J)[T>>>0])},Yc:null})}var js=[],un=[0,1,,1,null,1,!0,1,!1,1];function ai(u){9<(u>>>=0)&&--un[u+1]==0&&(un[u]=void 0,js.push(u))}var Bt=u=>{if(!u)throw new rr(`Cannot use deleted val. handle = ${u}`);return un[u]},Ht=u=>{switch(u){case void 0:return 2;case null:return 4;case!0:return 6;case!1:return 8;default:let h=js.pop()||un.length;return un[h]=u,un[h+1]=1,h}};function Wi(u){return this.Qc((x(),ue)[u>>>2>>>0])}var mo={name:"emscripten::val",Qc:u=>{var h=Bt(u);return ai(u),h},Xc:(u,h)=>Ht(h),Wc:Wi,Yc:null};function go(u){return ln(u>>>0,mo)}var C=(u,h)=>{switch(h){case 4:return function(b){return this.Qc((x(),ie)[b>>>2>>>0])};case 8:return function(b){return this.Qc((x(),te)[b>>>3>>>0])};default:throw new TypeError(`invalid float width (${h}): ${u}`)}};function k(u,h,b){b>>>=0,ln(u>>>=0,{name:h=tn(h>>>0),Qc:g=>g,Xc:(g,T)=>T,Wc:C(h,b),Yc:null})}function F(u,h,b,g,T){u>>>=0,b>>>=0,h=tn(h>>>0);let z=G=>G;if(g===0){var B=32-8*b;z=G=>G<>>B,T=z(T)}ln(u,{name:h,Qc:z,Xc:(G,de)=>de,Wc:Hs(h,b,g!==0),Yc:null})}function pe(u,h,b){function g(z){var B=(x(),ue)[z>>>2>>>0];return z=(x(),ue)[z+4>>>2>>>0],new T((x(),q).buffer,z,B)}var T=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,BigInt64Array,BigUint64Array][h];ln(u>>>=0,{name:b=tn(b>>>0),Qc:g,Wc:g},{Bd:!0})}var j=(u,h,b)=>{var g=(x(),J);if(h>>>=0,0=B){if(h>=b)break;g[h++>>>0]=B}else if(2047>=B){if(h+1>=b)break;g[h++>>>0]=192|B>>6,g[h++>>>0]=128|63&B}else if(65535>=B){if(h+2>=b)break;g[h++>>>0]=224|B>>12,g[h++>>>0]=128|B>>6&63,g[h++>>>0]=128|63&B}else{if(h+3>=b)break;g[h++>>>0]=240|B>>18,g[h++>>>0]=128|B>>12&63,g[h++>>>0]=128|B>>6&63,g[h++>>>0]=128|63&B,z++}}g[h>>>0]=0,u=h-T}else u=0;return u},ye=u=>{for(var h=0,b=0;b=g?h++:2047>=g?h+=2:55296<=g&&57343>=g?(h+=4,++b):h+=3}return h};function $e(u,h){ln(u>>>=0,{name:h=tn(h>>>0),Qc(b){var g=(x(),ue)[b>>>2>>>0];return g=dt(b+4,g,!0),$n(b),g},Xc(b,g){g instanceof ArrayBuffer&&(g=new Uint8Array(g));var T=typeof g=="string";if(!(T||ArrayBuffer.isView(g)&&g.BYTES_PER_ELEMENT==1))throw new rr("Cannot pass non-string to std::string");var z=T?ye(g):g.length,B=Ki(4+z+1),G=B+4;return(x(),ue)[B>>>2>>>0]=z,T?j(g,G,z+1):(x(),J).set(g,G>>>0),b!==null&&b.push($n,B),B},Wc:Wi,Yc(b){$n(b)}})}var Oe=globalThis.TextDecoder?new TextDecoder("utf-16le"):void 0,Ge=(u,h,b)=>{if(u>>>=1,16<(h=Ps((x(),ce),u,h/2,b))-u&&Oe)return Oe.decode((x(),ce).slice(u,h));for(b="";u>>0];b+=String.fromCharCode(g)}return b},Ye=(u,h,b)=>{if(b??(b=2147483647),2>b)return 0;var g=h;b=(b-=2)<2*u.length?b/2:u.length;for(var T=0;T>>1>>>0]=z,h+=2}return(x(),se)[h>>>1>>>0]=0,h-g},Dt=u=>2*u.length,jt=(u,h,b)=>{var g="";u>>>=2;for(var T=0;!(T>=h/4);T++){var z=(x(),ue)[u+T>>>0];if(!z&&!b)break;g+=String.fromCodePoint(z)}return g},Et=(u,h,b)=>{if(h>>>=0,b??(b=2147483647),4>b)return 0;var g=h;b=g+b-4;for(var T=0;T>>2>>>0]=z,(h+=4)+4>b)break}return(x(),L)[h>>>2>>>0]=0,h-g},yt=u=>{for(var h=0,b=0;b>>=0,h>>>=0,b=tn(b>>>=0),h===2)var g=Ge,T=Ye,z=Dt;else g=jt,T=Et,z=yt;ln(u,{name:b,Qc:B=>{var G=(x(),ue)[B>>>2>>>0];return G=g(B+4,G*h,!0),$n(B),G},Xc:(B,G)=>{if(typeof G!="string")throw new rr(`Cannot pass non-string to C++ string type ${b}`);var de=z(G),ge=Ki(4+de+h);return(x(),ue)[ge>>>2>>>0]=de/h,T(G,ge+4,de+h),B!==null&&B.push($n,ge),ge},Wc:Wi,Yc(B){$n(B)}})}function Ks(u,h){ln(u>>>=0,{Cd:!0,name:h=tn(h>>>0),Qc:()=>{},Xc:()=>{}})}function Xs(u){So(u>>>0,!r,1,!n,131072,!1),fe()}var ir=u=>{if(!R)try{if(u(),!(0Number((navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./)||[])[2]);function Gi(u){u>>>=0,qi||(Atomics.waitAsync((x(),L),u>>>2,u).value.then(St),u+=128,Atomics.store((x(),L),u>>>2,1))}var St=()=>ir(()=>{var u=ea();u&&(Gi(u),Nd())});function sr(u,h){(u>>>=0)==h>>>0?setTimeout(St):i?postMessage({ad:u,Uc:"checkMailbox"}):(u=X[u])&&u.postMessage({Uc:"checkMailbox"})}var Pn=[];function oi(u,h,b,g,T){for(h>>>=0,T>>>=0,Pn.length=0,b=T>>>3,g=T+g>>>3;b>>0]?(x(),he)[b++>>>0]:(x(),te)[b++>>>0],Pn.push(z)}return(h?ko[h]:N_[u])(...Pn)}var Un=()=>{N=0};function Ln(u){u>>>=0,i?postMessage({Uc:"cleanupThread",Qd:u}):Se(X[u])}function kt(u){}var Sr=u=>{try{u()}catch(h){ne(h)}};function Tr(u){var h=(...b)=>{xn.push(u);try{return u(...b)}finally{R||(xn.pop(),Mt&&zt===1&&xn.length===0&&(zt=0,N+=1,Sr(Sc),typeof Fibers<"u"&&Fibers.ce()))}};return Cr.set(u,h),h}var zt=0,Mt=null,ar=0,xn=[],Wn=new Map,Ir=new Map,Cr=new Map,Er=0,li=null,kr=[],Vi=u=>(function(h){if(!R){if(zt===0){var b=!1,g=!1;h((T=0)=>{if(!R&&(ar=T,b=!0,g)){zt=2,Sr(()=>Tc(Mt)),typeof MainLoop<"u"&&MainLoop.yd&&MainLoop.resume(),T=!1;try{var z=(function(){var de=(x(),L)[Mt+8>>>2>>>0];return de=Ir.get(de),de=Cr.get(de),--N,de()})()}catch(de){z=de,T=!0}var B=!1;if(!Mt){var G=li;G&&(li=null,(T?G.reject:G.resolve)(z),B=!0)}if(T&&!B)throw z}}),g=!0,b||(zt=1,Mt=(function(){var T=Ki(65548),z=T+12;if((x(),ue)[T>>>2>>>0]=z,(x(),ue)[T+4>>>2>>>0]=z+65536,z=xn[0],!Wn.has(z)){var B=Er++;Wn.set(z,B),Ir.set(B,z)}return z=Wn.get(z),(x(),L)[T+8>>>2>>>0]=z,T})(),typeof MainLoop<"u"&&MainLoop.yd&&MainLoop.pause(),Sr(()=>$c(Mt)))}else zt===2?(zt=0,Sr(Ic),$n(Mt),Mt=null,kr.forEach(ir)):ne(`invalid state: ${zt}`);return ar}})(h=>{u().then(h)});function zr(u){return u>>>=0,Vi(async()=>{var h=await Bt(u);return Ht(h)})}var Xt=[],ui=u=>{var h=Xt.length;return Xt.push(u),h},di=(u,h)=>{for(var b=Array(u),g=0;g>>2>>>0],B=Li[z];if(B===void 0)throw u=`parameter ${g}`,z=kd(z),h=tn(z),$n(z),new rr(`${u} has unknown type ${h}`);b[T]=B}return b},yo=(u,h,b)=>{var g=[];return u=u(g,b),g.length&&((x(),ue)[h>>>2>>>0]=Ht(g)),u},bo={},or=u=>{var h=bo[u];return h===void 0?tn(u):h};function Fi(u,h,b){var[g,...T]=di(u,h>>>0);h=g.Xc.bind(g);var z=T.map(de=>de.Wc.bind(de));u--;var B={toValue:Bt};switch(u=z.map((de,ge)=>{var ze=`argFromPtr${ge}`;return B[ze]=de,`${ze}(args${ge?"+"+8*ge:""})`}),b){case 0:var G="toValue(handle)";break;case 2:G="new (toValue(handle))";break;case 3:G="";break;case 1:B.getStringOrSymbol=or,G="toValue(handle)[getStringOrSymbol(methodName)]"}return G+=`(${u})`,g.Cd||(B.toReturnWire=h,B.emval_returnValue=yo,G=`return emval_returnValue(toReturnWire, destructorsRef, ${G})`),G=`return function (handle, methodName, destructorsRef, args) { + ${G} + }`,b=new Function(Object.keys(B),G)(...Object.values(B)),G=`methodCaller<(${T.map(de=>de.name)}) => ${g.name}>`,ui(Object.defineProperty(b,"name",{value:G}))}function lr(u,h){return h>>>=0,(u=Bt(u>>>0))==Bt(h)}function ur(u){return(u>>>=0)?(u=or(u),Ht(globalThis[u])):Ht(globalThis)}function Ys(u){return u=or(u>>>0),Ht(t[u])}function Hi(u,h){return h>>>=0,u=Bt(u>>>0),h=Bt(h),Ht(u[h])}function wo(u){9<(u>>>=0)&&(un[u+1]+=1)}function Zs(u,h,b,g,T){return Xt[u>>>0](h>>>0,b>>>0,g>>>0,T>>>0)}function _o(u,h,b,g,T){return Zs(u>>>0,h>>>0,b>>>0,g>>>0,T>>>0)}function qn(){return Ht([])}function c_(u){u=Bt(u>>>0);for(var h=Array(u.length),b=0;b>>0))}function f_(){return Ht({})}function h_(u){for(var h=Bt(u>>>=0);h.length;){var b=h.pop();h.pop()(b)}ai(u)}function m_(u,h,b){h>>>=0,b>>>=0,u=Bt(u>>>0),h=Bt(h),b=Bt(b),u[h]=b}function g_(u,h){u=-9007199254740992>u||9007199254740992>>=0,u=new Date(1e3*u),(x(),L)[h>>>2>>>0]=u.getUTCSeconds(),(x(),L)[h+4>>>2>>>0]=u.getUTCMinutes(),(x(),L)[h+8>>>2>>>0]=u.getUTCHours(),(x(),L)[h+12>>>2>>>0]=u.getUTCDate(),(x(),L)[h+16>>>2>>>0]=u.getUTCMonth(),(x(),L)[h+20>>>2>>>0]=u.getUTCFullYear()-1900,(x(),L)[h+24>>>2>>>0]=u.getUTCDay(),u=(u.getTime()-Date.UTC(u.getUTCFullYear(),0,1,0,0,0,0))/864e5|0,(x(),L)[h+28>>>2>>>0]=u}var hd=u=>u%4==0&&(u%100!=0||u%400==0),md=[0,31,60,91,121,152,182,213,244,274,305,335],gd=[0,31,59,90,120,151,181,212,243,273,304,334];function y_(u,h){u=-9007199254740992>u||9007199254740992>>=0,u=new Date(1e3*u),(x(),L)[h>>>2>>>0]=u.getSeconds(),(x(),L)[h+4>>>2>>>0]=u.getMinutes(),(x(),L)[h+8>>>2>>>0]=u.getHours(),(x(),L)[h+12>>>2>>>0]=u.getDate(),(x(),L)[h+16>>>2>>>0]=u.getMonth(),(x(),L)[h+20>>>2>>>0]=u.getFullYear()-1900,(x(),L)[h+24>>>2>>>0]=u.getDay();var b=(hd(u.getFullYear())?md:gd)[u.getMonth()]+u.getDate()-1|0;(x(),L)[h+28>>>2>>>0]=b,(x(),L)[h+36>>>2>>>0]=-60*u.getTimezoneOffset(),b=new Date(u.getFullYear(),6,1).getTimezoneOffset();var g=new Date(u.getFullYear(),0,1).getTimezoneOffset();u=0|(b!=g&&u.getTimezoneOffset()==Math.min(g,b)),(x(),L)[h+32>>>2>>>0]=u}function b_(u){u>>>=0;var h=new Date((x(),L)[u+20>>>2>>>0]+1900,(x(),L)[u+16>>>2>>>0],(x(),L)[u+12>>>2>>>0],(x(),L)[u+8>>>2>>>0],(x(),L)[u+4>>>2>>>0],(x(),L)[u>>>2>>>0],0),b=(x(),L)[u+32>>>2>>>0],g=h.getTimezoneOffset(),T=new Date(h.getFullYear(),6,1).getTimezoneOffset(),z=new Date(h.getFullYear(),0,1).getTimezoneOffset(),B=Math.min(z,T);return 0>b?(x(),L)[u+32>>>2>>>0]=+(T!=z&&B==g):0>>2>>>0]=h.getDay(),b=(hd(h.getFullYear())?md:gd)[h.getMonth()]+h.getDate()-1|0,(x(),L)[u+28>>>2>>>0]=b,(x(),L)[u>>>2>>>0]=h.getSeconds(),(x(),L)[u+4>>>2>>>0]=h.getMinutes(),(x(),L)[u+8>>>2>>>0]=h.getHours(),(x(),L)[u+12>>>2>>>0]=h.getDate(),(x(),L)[u+16>>>2>>>0]=h.getMonth(),(x(),L)[u+20>>>2>>>0]=h.getYear(),u=h.getTime(),BigInt(isNaN(u)?-1:u/1e3)}function yd(u,h,b,g,T,z,B){return i?D(16,1,u,h,b,g,T,z,B):-52}function bd(u,h,b,g,T,z){if(i)return D(17,1,u,h,b,g,T,z)}var ji={},w_=()=>performance.timeOrigin+performance.now();function wd(u,h){if(i)return D(18,1,u,h);if(ji[u]&&(clearTimeout(ji[u].id),delete ji[u]),!h)return 0;var b=setTimeout(()=>{delete ji[u],ir(()=>Od(u,performance.timeOrigin+performance.now()))},h);return ji[u]={id:b,be:h},0}function __(u,h,b,g){u>>>=0,h>>>=0,b>>>=0,g>>>=0;var T=new Date().getFullYear(),z=new Date(T,0,1).getTimezoneOffset();T=new Date(T,6,1).getTimezoneOffset();var B=Math.max(z,T);(x(),ue)[u>>>2>>>0]=60*B,(x(),L)[h>>>2>>>0]=+(z!=T),u=(h=G=>{var de=Math.abs(G);return`UTC${0<=G?"-":"+"}${String(Math.floor(de/60)).padStart(2,"0")}${String(de%60).padStart(2,"0")}`})(z),h=h(T),TDate.now();function x_(u,h,b){return b>>>=0,0<=u&&3>=u?(u===0?u=Date.now():u=performance.timeOrigin+performance.now(),u=Math.round(1e6*u),(x(),he)[b>>>3>>>0]=BigInt(u),0):28}var vo=[],_d=(u,h)=>{vo.length=0;for(var b;b=(x(),J)[u++>>>0];){var g=b!=105;h+=(g&=b!=112)&&h%8?4:0,vo.push(b==112?(x(),ue)[h>>>2>>>0]:b==106?(x(),he)[h>>>3>>>0]:b==105?(x(),L)[h>>>2>>>0]:(x(),te)[h>>>3>>>0]),h+=g?8:4}return vo};function $_(u,h,b){return u>>>=0,h=_d(h>>>0,b>>>0),ko[u](...h)}function S_(u,h,b){return u>>>=0,h=_d(h>>>0,b>>>0),ko[u](...h)}var T_=()=>{};function I_(u,h){return E(dt(u>>>0,h>>>0))}var C_=()=>{throw N+=1,"unwind"};function E_(){return 4294901760}var k_=()=>navigator.hardwareConcurrency,Ar={},Qs=u=>{var h;return(h=/\bwasm-function\[\d+\]:(0x[0-9a-f]+)/.exec(u))?+h[1]:(h=/:(\d+):\d+(?:\)|$)/.exec(u))?2147483648|+h[1]:0},vd=u=>{for(var h of u)(u=Qs(h))&&(Ar[u]=h)};function z_(){var u=Error().stack.toString().split(` +`);return u[0]=="Error"&&u.shift(),vd(u),Ar.kd=Qs(u[3]),Ar.Md=u,Ar.kd}function Js(u){if(!(u=Ar[u>>>0]))return 0;var h;if(h=/^\s+at .*\.wasm\.(.*) \(.*\)$/.exec(u))u=h[1];else if(h=/^\s+at (.*) \(.*\)$/.exec(u))u=h[1];else{if(!(h=/^(.+?)@/.exec(u)))return 0;u=h[1]}$n(Js.td??0),h=ye(u)+1;var b=Ki(h);return b&&j(u,b,h),Js.td=b,Js.td}function A_(u){u>>>=0;var h=(x(),J).length;if(u<=h||4294901760=b;b*=2){var g=h*(1+.2/b);g=Math.min(g,u+100663296);e:{g=(Math.min(4294901760,65536*Math.ceil(Math.max(u,g)/65536))-ke.buffer.byteLength+65535)/65536|0;try{ke.grow(g),P();var T=1;break e}catch{}T=void 0}if(T)return!0}return!1}function M_(u,h,b){if(u>>>=0,h>>>=0,Ar.kd==u)var g=Ar.Md;else(g=Error().stack.toString().split(` +`))[0]=="Error"&&g.shift(),vd(g);for(var T=3;g[T]&&Qs(g[T])!=u;)++T;for(u=0;u>>2>>>0]=Qs(g[u+T]);return u}var xo,$o={},xd=()=>{var g;if(!xo){var u,h={USER:"web_user",LOGNAME:"web_user",PATH:"/",PWD:"/",HOME:"/home/web_user",LANG:(((g=globalThis.navigator)==null?void 0:g.language)??"C").replace("-","_")+".UTF-8",_:"./this.program"};for(u in $o)$o[u]===void 0?delete h[u]:h[u]=$o[u];var b=[];for(u in h)b.push(`${u}=${h[u]}`);xo=b}return xo};function $d(u,h){if(i)return D(19,1,u,h);u>>>=0,h>>>=0;var b,g=0,T=0;for(b of xd()){var z=h+g;(x(),ue)[u+T>>>2>>>0]=z,g+=j(b,z,1/0)+1,T+=4}return 0}function Sd(u,h){if(i)return D(20,1,u,h);u>>>=0,h>>>=0;var b=xd();for(var g of((x(),ue)[u>>>2>>>0]=b.length,u=0,b))u+=ye(g)+1;return(x(),ue)[h>>>2>>>0]=u,0}function Td(u){return i?D(21,1,u):52}function Id(u,h,b,g){return i?D(22,1,u,h,b,g):52}function Cd(u,h,b,g){return i?D(23,1,u,h,b,g):70}var R_=[null,[],[]];function Ed(u,h,b,g){if(i)return D(24,1,u,h,b,g);h>>>=0,b>>>=0,g>>>=0;for(var T=0,z=0;z>>2>>>0],G=(x(),ue)[h+4>>>2>>>0];h+=8;for(var de=0;de>>0],qe=R_[ge];ze===0||ze===10?((ge===1?S:E)(Bi(qe)),qe.length=0):qe.push(ze)}T+=G}return(x(),ue)[g>>>2>>>0]=T,0}function O_(u){return u>>>0}i||(function(){for(var u=t.numThreads-1;u--;)me();ft.push(async()=>{var h=(async function(){if(!i)return Promise.all(le.map(xe))})();ut++,await h,--ut==0&&ht&&(h=ht,ht=null,h())})})(),i||(ke=new WebAssembly.Memory({initial:256,maximum:65536,shared:!0}),P()),t.wasmBinary&&(c=t.wasmBinary),t.stackSave=()=>Re(),t.stackRestore=u=>Ae(u),t.stackAlloc=u=>Io(u),t.setValue=function(u,h,b="i8"){switch(b.endsWith("*")&&(b="*"),b){case"i1":case"i8":(x(),q)[u>>>0]=h;break;case"i16":(x(),se)[u>>>1>>>0]=h;break;case"i32":(x(),L)[u>>>2>>>0]=h;break;case"i64":(x(),he)[u>>>3>>>0]=BigInt(h);break;case"float":(x(),ie)[u>>>2>>>0]=h;break;case"double":(x(),te)[u>>>3>>>0]=h;break;case"*":(x(),ue)[u>>>2>>>0]=h;break;default:ne(`invalid type for setValue: ${b}`)}},t.getValue=function(u,h="i8"){switch(h.endsWith("*")&&(h="*"),h){case"i1":case"i8":return(x(),q)[u>>>0];case"i16":return(x(),se)[u>>>1>>>0];case"i32":return(x(),L)[u>>>2>>>0];case"i64":return(x(),he)[u>>>3>>>0];case"float":return(x(),ie)[u>>>2>>>0];case"double":return(x(),te)[u>>>3>>>0];case"*":return(x(),ue)[u>>>2>>>0];default:ne(`invalid type for getValue: ${h}`)}},t.UTF8ToString=dt,t.stringToUTF8=j,t.lengthBytesUTF8=ye;var kd,zd,ea,$n,Ki,So,Ad,Md,Rd,To,Od,Nd,Ne,Xi,Bd,Ae,Io,Re,Dd,Co,Pd,Ud,Ld,Eo,Wd,qd,Gd,Vd,Fd,Hd,jd,Kd,Xd,Yd,Zd,Qd,Jd,ec,tc,nc,rc,ic,sc,ac,oc,lc,uc,dc,cc,pc,fc,hc,mc,gc,yc,bc,wc,_c,vc,xc,$c,Sc,Tc,Ic,Gn,N_=[H,Z,Oi,ri,Di,$r,Pi,Us,Ls,Ws,ii,si,qs,Gs,Vs,Fs,yd,bd,wd,$d,Sd,Td,Id,Cd,Ed],ko={934156:(u,h,b,g,T)=>{if(t===void 0||!t.Zc)return 1;if((u=dt(Number(u>>>0))).startsWith("./")&&(u=u.substring(2)),!(u=t.Zc.get(u)))return 2;if(h=Number(h>>>0),b=Number(b>>>0),g=Number(g>>>0),h+b>u.byteLength)return 3;try{let z=u.subarray(h,h+b);switch(T){case 0:(x(),J).set(z,g>>>0);break;case 1:t.Xd?t.Xd(g,z):t.Ld(g,z);break;default:return 4}return 0}catch{return 4}},934980:(u,h,b)=>{t.wd(u,(x(),J).subarray(h>>>0,h+b>>>0))},935044:()=>t.Zd(),935086:u=>{t.vd(u)},935123:()=>{t.Ed()},935154:()=>{t.Fd()},935183:()=>{t.Jd()},935208:u=>t.Dd(u),935241:u=>t.Hd(u),935273:(u,h,b)=>{t.jd(Number(u),Number(h),Number(b),!0)},935336:(u,h,b)=>{t.jd(Number(u),Number(h),Number(b))},935393:()=>typeof wasmOffsetConverter<"u",935450:u=>{t.ac("Abs",u,void 0)},935501:u=>{t.ac("Neg",u,void 0)},935552:u=>{t.ac("Floor",u,void 0)},935605:u=>{t.ac("Ceil",u,void 0)},935657:u=>{t.ac("Reciprocal",u,void 0)},935715:u=>{t.ac("Sqrt",u,void 0)},935767:u=>{t.ac("Exp",u,void 0)},935818:u=>{t.ac("Erf",u,void 0)},935869:u=>{t.ac("Sigmoid",u,void 0)},935924:(u,h,b)=>{t.ac("HardSigmoid",u,{alpha:h,beta:b})},936003:u=>{t.ac("Log",u,void 0)},936054:u=>{t.ac("Sin",u,void 0)},936105:u=>{t.ac("Cos",u,void 0)},936156:u=>{t.ac("Tan",u,void 0)},936207:u=>{t.ac("Asin",u,void 0)},936259:u=>{t.ac("Acos",u,void 0)},936311:u=>{t.ac("Atan",u,void 0)},936363:u=>{t.ac("Sinh",u,void 0)},936415:u=>{t.ac("Cosh",u,void 0)},936467:u=>{t.ac("Asinh",u,void 0)},936520:u=>{t.ac("Acosh",u,void 0)},936573:u=>{t.ac("Atanh",u,void 0)},936626:u=>{t.ac("Tanh",u,void 0)},936678:u=>{t.ac("Not",u,void 0)},936729:(u,h,b)=>{t.ac("Clip",u,{min:h,max:b})},936798:u=>{t.ac("Clip",u,void 0)},936850:(u,h)=>{t.ac("Elu",u,{alpha:h})},936908:u=>{t.ac("Gelu",u,void 0)},936960:u=>{t.ac("Relu",u,void 0)},937012:(u,h)=>{t.ac("LeakyRelu",u,{alpha:h})},937076:(u,h)=>{t.ac("ThresholdedRelu",u,{alpha:h})},937146:(u,h)=>{t.ac("Cast",u,{to:h})},937204:u=>{t.ac("Add",u,void 0)},937255:u=>{t.ac("Sub",u,void 0)},937306:u=>{t.ac("Mul",u,void 0)},937357:u=>{t.ac("Div",u,void 0)},937408:u=>{t.ac("Pow",u,void 0)},937459:u=>{t.ac("Equal",u,void 0)},937512:u=>{t.ac("Greater",u,void 0)},937567:u=>{t.ac("GreaterOrEqual",u,void 0)},937629:u=>{t.ac("Less",u,void 0)},937681:u=>{t.ac("LessOrEqual",u,void 0)},937740:(u,h,b,g,T)=>{t.ac("ReduceMean",u,{keepDims:!!h,noopWithEmptyAxes:!!b,axes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},937915:(u,h,b,g,T)=>{t.ac("ReduceMax",u,{keepDims:!!h,noopWithEmptyAxes:!!b,axes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},938089:(u,h,b,g,T)=>{t.ac("ReduceMin",u,{keepDims:!!h,noopWithEmptyAxes:!!b,axes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},938263:(u,h,b,g,T)=>{t.ac("ReduceProd",u,{keepDims:!!h,noopWithEmptyAxes:!!b,axes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},938438:(u,h,b,g,T)=>{t.ac("ReduceSum",u,{keepDims:!!h,noopWithEmptyAxes:!!b,axes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},938612:(u,h,b,g,T)=>{t.ac("ReduceL1",u,{keepDims:!!h,noopWithEmptyAxes:!!b,axes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},938785:(u,h,b,g,T)=>{t.ac("ReduceL2",u,{keepDims:!!h,noopWithEmptyAxes:!!b,axes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},938958:(u,h,b,g,T)=>{t.ac("ReduceLogSum",u,{keepDims:!!h,noopWithEmptyAxes:!!b,axes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},939135:(u,h,b,g,T)=>{t.ac("ReduceSumSquare",u,{keepDims:!!h,noopWithEmptyAxes:!!b,axes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},939315:(u,h,b,g,T)=>{t.ac("ReduceLogSumExp",u,{keepDims:!!h,noopWithEmptyAxes:!!b,axes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},939495:u=>{t.ac("Where",u,void 0)},939548:(u,h,b)=>{t.ac("Transpose",u,{perm:h?Array.from((x(),L).subarray(Number(h)>>>0,Number(b)>>>0)):[]})},939672:(u,h,b,g)=>{t.ac("DepthToSpace",u,{blocksize:h,mode:dt(b),format:g?"NHWC":"NCHW"})},939805:(u,h,b,g)=>{t.ac("DepthToSpace",u,{blocksize:h,mode:dt(b),format:g?"NHWC":"NCHW"})},939938:(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt,dr)=>{t.ac("ConvTranspose",u,{format:de?"NHWC":"NCHW",autoPad:h,dilations:[b],group:g,kernelShape:[T],pads:[z,B],strides:[G],wIsConst:()=>!!(x(),q)[ge>>>0],outputPadding:ze?Array.from((x(),L).subarray(Number(ze)>>>0,Number(qe)>>>0)):[],outputShape:et?Array.from((x(),L).subarray(Number(et)>>>0,Number(rt)>>>0)):[],activation:dt(dr)})},940371:(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt)=>{t.ac("ConvTranspose",u,{format:G?"NHWC":"NCHW",autoPad:h,dilations:Array.from((x(),L).subarray(Number(b)>>>0,2+(Number(b)>>>0)>>>0)),group:g,kernelShape:Array.from((x(),L).subarray(Number(T)>>>0,2+(Number(T)>>>0)>>>0)),pads:Array.from((x(),L).subarray(Number(z)>>>0,4+(Number(z)>>>0)>>>0)),strides:Array.from((x(),L).subarray(Number(B)>>>0,2+(Number(B)>>>0)>>>0)),wIsConst:()=>!!(x(),q)[de>>>0],outputPadding:ge?Array.from((x(),L).subarray(Number(ge)>>>0,Number(ze)>>>0)):[],outputShape:qe?Array.from((x(),L).subarray(Number(qe)>>>0,Number(et)>>>0)):[],activation:dt(rt)})},941032:(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt,dr)=>{t.ac("ConvTranspose",u,{format:de?"NHWC":"NCHW",autoPad:h,dilations:[b],group:g,kernelShape:[T],pads:[z,B],strides:[G],wIsConst:()=>!!(x(),q)[ge>>>0],outputPadding:ze?Array.from((x(),L).subarray(Number(ze)>>>0,Number(qe)>>>0)):[],outputShape:et?Array.from((x(),L).subarray(Number(et)>>>0,Number(rt)>>>0)):[],activation:dt(dr)})},941465:(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt)=>{t.ac("ConvTranspose",u,{format:G?"NHWC":"NCHW",autoPad:h,dilations:Array.from((x(),L).subarray(Number(b)>>>0,2+(Number(b)>>>0)>>>0)),group:g,kernelShape:Array.from((x(),L).subarray(Number(T)>>>0,2+(Number(T)>>>0)>>>0)),pads:Array.from((x(),L).subarray(Number(z)>>>0,4+(Number(z)>>>0)>>>0)),strides:Array.from((x(),L).subarray(Number(B)>>>0,2+(Number(B)>>>0)>>>0)),wIsConst:()=>!!(x(),q)[de>>>0],outputPadding:ge?Array.from((x(),L).subarray(Number(ge)>>>0,Number(ze)>>>0)):[],outputShape:qe?Array.from((x(),L).subarray(Number(qe)>>>0,Number(et)>>>0)):[],activation:dt(rt)})},942126:(u,h)=>{t.ac("GlobalAveragePool",u,{format:h?"NHWC":"NCHW"})},942217:(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt)=>{t.ac("AveragePool",u,{format:rt?"NHWC":"NCHW",auto_pad:h,ceil_mode:b,count_include_pad:g,storage_order:T,dilations:z?Array.from((x(),L).subarray(Number(z)>>>0,Number(B)>>>0)):[],kernel_shape:G?Array.from((x(),L).subarray(Number(G)>>>0,Number(de)>>>0)):[],pads:ge?Array.from((x(),L).subarray(Number(ge)>>>0,Number(ze)>>>0)):[],strides:qe?Array.from((x(),L).subarray(Number(qe)>>>0,Number(et)>>>0)):[]})},942696:(u,h)=>{t.ac("GlobalAveragePool",u,{format:h?"NHWC":"NCHW"})},942787:(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt)=>{t.ac("AveragePool",u,{format:rt?"NHWC":"NCHW",auto_pad:h,ceil_mode:b,count_include_pad:g,storage_order:T,dilations:z?Array.from((x(),L).subarray(Number(z)>>>0,Number(B)>>>0)):[],kernel_shape:G?Array.from((x(),L).subarray(Number(G)>>>0,Number(de)>>>0)):[],pads:ge?Array.from((x(),L).subarray(Number(ge)>>>0,Number(ze)>>>0)):[],strides:qe?Array.from((x(),L).subarray(Number(qe)>>>0,Number(et)>>>0)):[]})},943266:(u,h)=>{t.ac("GlobalMaxPool",u,{format:h?"NHWC":"NCHW"})},943353:(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt)=>{t.ac("MaxPool",u,{format:rt?"NHWC":"NCHW",auto_pad:h,ceil_mode:b,count_include_pad:g,storage_order:T,dilations:z?Array.from((x(),L).subarray(Number(z)>>>0,Number(B)>>>0)):[],kernel_shape:G?Array.from((x(),L).subarray(Number(G)>>>0,Number(de)>>>0)):[],pads:ge?Array.from((x(),L).subarray(Number(ge)>>>0,Number(ze)>>>0)):[],strides:qe?Array.from((x(),L).subarray(Number(qe)>>>0,Number(et)>>>0)):[]})},943828:(u,h)=>{t.ac("GlobalMaxPool",u,{format:h?"NHWC":"NCHW"})},943915:(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt)=>{t.ac("MaxPool",u,{format:rt?"NHWC":"NCHW",auto_pad:h,ceil_mode:b,count_include_pad:g,storage_order:T,dilations:z?Array.from((x(),L).subarray(Number(z)>>>0,Number(B)>>>0)):[],kernel_shape:G?Array.from((x(),L).subarray(Number(G)>>>0,Number(de)>>>0)):[],pads:ge?Array.from((x(),L).subarray(Number(ge)>>>0,Number(ze)>>>0)):[],strides:qe?Array.from((x(),L).subarray(Number(qe)>>>0,Number(et)>>>0)):[]})},944390:(u,h,b,g,T)=>{t.ac("Gemm",u,{alpha:h,beta:b,transA:g,transB:T})},944494:u=>{t.ac("MatMul",u,void 0)},944548:(u,h,b,g)=>{t.ac("ArgMax",u,{keepDims:!!h,selectLastIndex:!!b,axis:g})},944656:(u,h,b,g)=>{t.ac("ArgMin",u,{keepDims:!!h,selectLastIndex:!!b,axis:g})},944764:(u,h)=>{t.ac("Softmax",u,{axis:h})},944827:(u,h)=>{t.ac("Concat",u,{axis:h})},944887:(u,h,b,g,T)=>{t.ac("Split",u,{axis:h,numOutputs:b,splitSizes:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},945043:u=>{t.ac("Expand",u,void 0)},945097:(u,h)=>{t.ac("Gather",u,{axis:Number(h)})},945168:(u,h)=>{t.ac("GatherElements",u,{axis:Number(h)})},945247:(u,h)=>{t.ac("GatherND",u,{batch_dims:Number(h)})},945326:(u,h,b,g,T,z,B,G,de,ge,ze)=>{t.ac("Resize",u,{antialias:h,axes:b?Array.from((x(),L).subarray(Number(b)>>>0,Number(g)>>>0)):[],coordinateTransformMode:dt(T),cubicCoeffA:z,excludeOutside:B,extrapolationValue:G,keepAspectRatioPolicy:dt(de),mode:dt(ge),nearestMode:dt(ze)})},945688:(u,h,b,g,T,z,B)=>{t.ac("Slice",u,{starts:h?Array.from((x(),L).subarray(Number(h)>>>0,Number(b)>>>0)):[],ends:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[],axes:z?Array.from((x(),L).subarray(Number(z)>>>0,Number(B)>>>0)):[]})},945952:u=>{t.ac("Tile",u,void 0)},946004:(u,h,b)=>{t.ac("InstanceNormalization",u,{epsilon:h,format:b?"NHWC":"NCHW"})},946118:(u,h,b)=>{t.ac("InstanceNormalization",u,{epsilon:h,format:b?"NHWC":"NCHW"})},946232:u=>{t.ac("Range",u,void 0)},946285:(u,h)=>{t.ac("Einsum",u,{equation:dt(h)})},946366:(u,h,b,g,T)=>{t.ac("Pad",u,{mode:h,value:b,pads:g?Array.from((x(),L).subarray(Number(g)>>>0,Number(T)>>>0)):[]})},946509:(u,h,b,g,T,z)=>{t.ac("BatchNormalization",u,{epsilon:h,momentum:b,spatial:!!T,trainingMode:!!g,format:z?"NHWC":"NCHW"})},946678:(u,h,b,g,T,z)=>{t.ac("BatchNormalization",u,{epsilon:h,momentum:b,spatial:!!T,trainingMode:!!g,format:z?"NHWC":"NCHW"})},946847:(u,h,b)=>{t.ac("CumSum",u,{exclusive:Number(h),reverse:Number(b)})},946944:(u,h,b)=>{t.ac("DequantizeLinear",u,{axis:h,blockSize:b})},947034:(u,h,b,g,T)=>{t.ac("GridSample",u,{align_corners:h,mode:dt(b),padding_mode:dt(g),format:T?"NHWC":"NCHW"})},947204:(u,h,b,g,T)=>{t.ac("GridSample",u,{align_corners:h,mode:dt(b),padding_mode:dt(g),format:T?"NHWC":"NCHW"})},947374:(u,h)=>{t.ac("ScatterND",u,{reduction:dt(h)})},947459:(u,h,b,g,T,z,B,G,de)=>{t.ac("Attention",u,{numHeads:h,isUnidirectional:b,maskFilterValue:g,scale:T,doRotary:z,qkvHiddenSizes:B?Array.from((x(),L).subarray(Number(G)>>>0,Number(G)+B>>>0)):[],pastPresentShareBuffer:!!de})},947731:u=>{t.ac("BiasAdd",u,void 0)},947786:u=>{t.ac("BiasSplitGelu",u,void 0)},947847:u=>{t.ac("FastGelu",u,void 0)},947903:(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt,dr,zo)=>{t.ac("Conv",u,{format:qe?"NHWC":"NCHW",auto_pad:h,dilations:b?Array.from((x(),L).subarray(Number(b)>>>0,Number(g)>>>0)):[],group:T,kernel_shape:z?Array.from((x(),L).subarray(Number(z)>>>0,Number(B)>>>0)):[],pads:G?Array.from((x(),L).subarray(Number(G)>>>0,Number(de)>>>0)):[],strides:ge?Array.from((x(),L).subarray(Number(ge)>>>0,Number(ze)>>>0)):[],w_is_const:()=>!!(x(),q)[Number(et)>>>0],activation:dt(rt),activation_params:dr?Array.from((x(),ie).subarray(Number(dr)>>>0,Number(zo)>>>0)):[]})},948487:u=>{t.ac("Gelu",u,void 0)},948539:(u,h,b,g,T,z,B,G,de)=>{t.ac("GroupQueryAttention",u,{numHeads:h,kvNumHeads:b,scale:g,softcap:T,doRotary:z,rotaryInterleaved:B,smoothSoftmax:G,localWindowSize:de})},948756:(u,h,b,g)=>{t.ac("LayerNormalization",u,{axis:h,epsilon:b,simplified:!!g})},948867:(u,h,b,g)=>{t.ac("LayerNormalization",u,{axis:h,epsilon:b,simplified:!!g})},948978:(u,h,b,g,T,z)=>{t.ac("MatMulNBits",u,{k:h,n:b,accuracyLevel:g,bits:T,blockSize:z})},949105:(u,h,b,g,T,z)=>{t.ac("MultiHeadAttention",u,{numHeads:h,isUnidirectional:b,maskFilterValue:g,scale:T,doRotary:z})},949264:(u,h)=>{t.ac("QuickGelu",u,{alpha:h})},949328:(u,h,b,g,T)=>{t.ac("RotaryEmbedding",u,{interleaved:!!h,numHeads:b,rotaryEmbeddingDim:g,scale:T})},949467:(u,h,b)=>{t.ac("SkipLayerNormalization",u,{epsilon:h,simplified:!!b})},949569:(u,h,b)=>{t.ac("SkipLayerNormalization",u,{epsilon:h,simplified:!!b})},949671:(u,h,b,g)=>{t.ac("GatherBlockQuantized",u,{gatherAxis:h,quantizeAxis:b,blockSize:g})},949792:u=>{t.Id(u)},949826:(u,h)=>t.Kd(Number(u),Number(h),t.$c.Nd,t.$c.errors)};function B_(u,h,b){return Vi(async()=>{await t.Gd(Number(u),Number(h),Number(b))})}function D_(){return typeof wasmOffsetConverter<"u"}function P_(u,h,b,g){var T=Re();try{return Kd(u,h,b,g)}catch(z){if(Ae(T),z!==z+0)throw z;Ne(1,0)}}function U_(u,h,b){var g=Re();try{return Vd(u,h,b)}catch(T){if(Ae(g),T!==T+0)throw T;Ne(1,0)}}function L_(u,h,b){var g=Re();try{Ld(u,h,b)}catch(T){if(Ae(g),T!==T+0)throw T;Ne(1,0)}}function W_(u,h){var b=Re();try{return Eo(u,h)}catch(g){if(Ae(b),g!==g+0)throw g;Ne(1,0)}}function q_(u){var h=Re();try{Wd(u)}catch(b){if(Ae(h),b!==b+0)throw b;Ne(1,0)}}function G_(u,h,b,g,T,z,B){var G=Re();try{return Hd(u,h,b,g,T,z,B)}catch(de){if(Ae(G),de!==de+0)throw de;Ne(1,0)}}function V_(u,h){var b=Re();try{Xd(u,h)}catch(g){if(Ae(b),g!==g+0)throw g;Ne(1,0)}}function F_(u,h,b,g,T,z){var B=Re();try{qd(u,h,b,g,T,z)}catch(G){if(Ae(B),G!==G+0)throw G;Ne(1,0)}}function H_(u,h,b,g){var T=Re();try{jd(u,h,b,g)}catch(z){if(Ae(T),z!==z+0)throw z;Ne(1,0)}}function j_(u,h,b,g,T){var z=Re();try{Gd(u,h,b,g,T)}catch(B){if(Ae(z),B!==B+0)throw B;Ne(1,0)}}function K_(u,h,b,g,T,z,B){var G=Re();try{Zd(u,h,b,g,T,z,B)}catch(de){if(Ae(G),de!==de+0)throw de;Ne(1,0)}}function X_(u,h,b,g,T,z,B){var G=Re();try{Qd(u,h,b,g,T,z,B)}catch(de){if(Ae(G),de!==de+0)throw de;Ne(1,0)}}function Y_(u,h,b,g,T,z,B,G){var de=Re();try{nc(u,h,b,g,T,z,B,G)}catch(ge){if(Ae(de),ge!==ge+0)throw ge;Ne(1,0)}}function Z_(u,h,b,g,T){var z=Re();try{return Yd(u,h,b,g,T)}catch(B){if(Ae(z),B!==B+0)throw B;Ne(1,0)}}function Q_(u,h,b,g,T,z,B,G){var de=Re();try{rc(u,h,b,g,T,z,B,G)}catch(ge){if(Ae(de),ge!==ge+0)throw ge;Ne(1,0)}}function J_(u,h,b,g,T,z,B,G,de,ge,ze,qe){var et=Re();try{Jd(u,h,b,g,T,z,B,G,de,ge,ze,qe)}catch(rt){if(Ae(et),rt!==rt+0)throw rt;Ne(1,0)}}function ev(u,h,b,g,T,z){var B=Re();try{return ec(u,h,b,g,T,z)}catch(G){if(Ae(B),G!==G+0)throw G;Ne(1,0)}}function tv(u,h,b){var g=Re();try{return ic(u,h,b)}catch(T){if(Ae(g),T!==T+0)throw T;return Ne(1,0),0n}}function nv(u,h,b,g,T,z,B,G,de){var ge=Re();try{Fd(u,h,b,g,T,z,B,G,de)}catch(ze){if(Ae(ge),ze!==ze+0)throw ze;Ne(1,0)}}function rv(u){var h=Re();try{return sc(u)}catch(b){if(Ae(h),b!==b+0)throw b;Ne(1,0)}}function iv(u,h,b){var g=Re();try{return ac(u,h,b)}catch(T){if(Ae(g),T!==T+0)throw T;Ne(1,0)}}function sv(u,h){var b=Re();try{return xc(u,h)}catch(g){if(Ae(b),g!==g+0)throw g;return Ne(1,0),0n}}function av(u,h,b,g,T){var z=Re();try{oc(u,h,b,g,T)}catch(B){if(Ae(z),B!==B+0)throw B;Ne(1,0)}}function ov(u){var h=Re();try{return lc(u)}catch(b){if(Ae(h),b!==b+0)throw b;return Ne(1,0),0n}}function lv(u,h,b,g,T,z){var B=Re();try{return hc(u,h,b,g,T,z)}catch(G){if(Ae(B),G!==G+0)throw G;Ne(1,0)}}function uv(u,h,b,g,T,z){var B=Re();try{return mc(u,h,b,g,T,z)}catch(G){if(Ae(B),G!==G+0)throw G;Ne(1,0)}}function dv(u,h,b,g,T,z,B,G){var de=Re();try{return tc(u,h,b,g,T,z,B,G)}catch(ge){if(Ae(de),ge!==ge+0)throw ge;Ne(1,0)}}function cv(u,h,b,g,T){var z=Re();try{return gc(u,h,b,g,T)}catch(B){if(Ae(z),B!==B+0)throw B;return Ne(1,0),0n}}function pv(u,h,b,g){var T=Re();try{return yc(u,h,b,g)}catch(z){if(Ae(T),z!==z+0)throw z;Ne(1,0)}}function fv(u,h,b,g){var T=Re();try{return bc(u,h,b,g)}catch(z){if(Ae(T),z!==z+0)throw z;Ne(1,0)}}function hv(u,h,b,g,T,z,B,G,de,ge,ze,qe){var et=Re();try{return wc(u,h,b,g,T,z,B,G,de,ge,ze,qe)}catch(rt){if(Ae(et),rt!==rt+0)throw rt;Ne(1,0)}}function mv(u,h,b,g,T,z,B,G,de,ge,ze){var qe=Re();try{pc(u,h,b,g,T,z,B,G,de,ge,ze)}catch(et){if(Ae(qe),et!==et+0)throw et;Ne(1,0)}}function gv(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt,dr,zo){var xv=Re();try{fc(u,h,b,g,T,z,B,G,de,ge,ze,qe,et,rt,dr,zo)}catch(Ao){if(Ae(xv),Ao!==Ao+0)throw Ao;Ne(1,0)}}function yv(u,h,b,g){var T=Re();try{return _c(u,h,b,g)}catch(z){if(Ae(T),z!==z+0)throw z;Ne(1,0)}}function bv(u,h,b,g,T){var z=Re();try{return vc(u,h,b,g,T)}catch(B){if(Ae(z),B!==B+0)throw B;Ne(1,0)}}function wv(u,h,b){var g=Re();try{return uc(u,h,b)}catch(T){if(Ae(g),T!==T+0)throw T;Ne(1,0)}}function _v(u,h,b){var g=Re();try{return dc(u,h,b)}catch(T){if(Ae(g),T!==T+0)throw T;Ne(1,0)}}function vv(u,h,b,g){var T=Re();try{cc(u,h,b,g)}catch(z){if(Ae(T),z!==z+0)throw z;Ne(1,0)}}function ta(){if(0{w=u,$=h})}var U0,fp,h$=re(()=>{var e,t;U0=pp,fp=(t=(e=globalThis.self)==null?void 0:e.name)==null?void 0:t.startsWith("em-pthread"),fp&&pp()}),jo,ru,hp,Yt,L0,oa,mp,gp,Ko,yp,Xo,W0,Yo,q0,qu=re(()=>{Wu(),jo=typeof location>"u"?void 0:location.origin,ru=import.meta.url>"file:"&&import.meta.url<"file;",hp=()=>{{if(ru){let e=URL;return new URL(new e("ort.bundle.min.mjs",import.meta.url).href,jo).href}return import.meta.url}},Yt=hp(),L0=()=>{if(Yt&&!Yt.startsWith("blob:"))return Yt.substring(0,Yt.lastIndexOf("/")+1)},oa=(e,t)=>{try{let n=t??Yt;return(n?new URL(e,n):new URL(e)).origin===jo}catch{return!1}},mp=(e,t)=>{let n=t??Yt;try{return(n?new URL(e,n):new URL(e)).href}catch{return}},gp=(e,t)=>`${t??"./"}${e}`,Ko=async e=>{let t=await(await fetch(e,{credentials:"same-origin"})).blob();return URL.createObjectURL(t)},yp=async e=>(await import(e)).default,Xo=(f$(),As(B0)).default,W0=async()=>{if(!Yt)throw new Error("Failed to load proxy worker: cannot determine the script source URL.");if(oa(Yt))return[void 0,Xo()];let e=await Ko(Yt);return[e,Xo(e)]},Yo=(h$(),As(P0)).default,q0=async(e,t,n,r)=>{let i=Yo&&!(e||t);if(i)if(Yt)i=oa(Yt)||r&&!n;else if(r&&!n)i=!0;else throw new Error("cannot determine the script source URL.");if(i)return[void 0,Yo];{let s="ort-wasm-simd-threaded.jsep.mjs",a=e??mp(s,t),o=n&&a&&!oa(a,t),l=o?await Ko(a):a??gp(s,t);return[o?l:void 0,await yp(l)]}}}),Zo,la,es,Qo,bp,wp,_p,Gu,tt,ei=re(()=>{qu(),la=!1,es=!1,Qo=!1,bp=()=>{if(typeof SharedArrayBuffer>"u")return!1;try{return typeof MessageChannel<"u"&&new MessageChannel().port1.postMessage(new SharedArrayBuffer(1)),WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,5,4,1,3,1,1,10,11,1,9,0,65,0,254,16,2,0,26,11]))}catch{return!1}},wp=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,4,1,96,0,0,3,2,1,0,10,30,1,28,0,65,0,253,15,253,12,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,253,186,1,26,11]))}catch{return!1}},_p=()=>{try{return WebAssembly.validate(new Uint8Array([0,97,115,109,1,0,0,0,1,5,1,96,0,1,123,3,2,1,0,10,19,1,17,0,65,1,253,15,65,2,253,15,65,3,253,15,253,147,2,11]))}catch{return!1}},Gu=async e=>{if(la)return Promise.resolve();if(es)throw new Error("multiple calls to 'initializeWebAssembly()' detected.");if(Qo)throw new Error("previous call to 'initializeWebAssembly()' failed.");es=!0;let t=e.initTimeout,n=e.numThreads;if(e.simd!==!1){if(e.simd==="relaxed"){if(!_p())throw new Error("Relaxed WebAssembly SIMD is not supported in the current environment.")}else if(!wp())throw new Error("WebAssembly SIMD is not supported in the current environment.")}let r=bp();n>1&&!r&&(typeof self<"u"&&!self.crossOriginIsolated&&console.warn("env.wasm.numThreads is set to "+n+", but this will not work unless you enable crossOriginIsolated mode. See https://web.dev/cross-origin-isolation-guide/ for more info."),console.warn("WebAssembly multi-threading is not supported in the current environment. Falling back to single-threading."),e.numThreads=n=1);let i=e.wasmPaths,s=typeof i=="string"?i:void 0,a=i==null?void 0:i.mjs,o=(a==null?void 0:a.href)??a,l=i==null?void 0:i.wasm,d=(l==null?void 0:l.href)??l,p=e.wasmBinary,[f,c]=await q0(o,s,n>1,!!p||!!d),m=!1,y=[];if(t>0&&y.push(new Promise(w=>{setTimeout(()=>{m=!0,w()},t)})),y.push(new Promise((w,$)=>{let _={numThreads:n};if(p)_.wasmBinary=p,_.locateFile=v=>v;else if(d||s)_.locateFile=v=>d??s+v;else if(o&&o.indexOf("blob:")!==0)_.locateFile=v=>new URL(v,o).href;else if(f){let v=L0();v&&(_.locateFile=I=>v+I)}c(_).then(v=>{es=!1,la=!0,Zo=v,w(),f&&URL.revokeObjectURL(f)},v=>{es=!1,Qo=!0,$(v)})})),await Promise.race(y),m)throw new Error(`WebAssembly backend initializing failed due to timeout: ${t}ms`)},tt=()=>{if(la&&Zo)return Zo;throw new Error("WebAssembly is not initialized yet.")}}),gn,Pa,je,Vu=re(()=>{ei(),gn=(e,t)=>{let n=tt(),r=n.lengthBytesUTF8(e)+1,i=n._malloc(r);return n.stringToUTF8(e,i,r),t.push(i),i},Pa=(e,t,n,r)=>{if(typeof e=="object"&&e!==null){if(n.has(e))throw new Error("Circular reference in options");n.add(e)}Object.entries(e).forEach(([i,s])=>{let a=t?t+i:i;if(typeof s=="object")Pa(s,a+".",n,r);else if(typeof s=="string"||typeof s=="number")r(a,s.toString());else if(typeof s=="boolean")r(a,s?"1":"0");else throw new Error(`Can't handle extra config type: ${typeof s}`)})},je=e=>{let t=tt(),n=t.stackSave();try{let r=t.PTR_SIZE,i=t.stackAlloc(2*r);t._OrtGetLastError(i,i+r);let s=Number(t.getValue(i,r===4?"i32":"i64")),a=t.getValue(i+r,"*"),o=a?t.UTF8ToString(a):"";throw new Error(`${e} ERROR_CODE: ${s}, ERROR_MESSAGE: ${o}`)}finally{t.stackRestore(n)}}}),G0,m$=re(()=>{ei(),Vu(),G0=e=>{let t=tt(),n=0,r=[],i=e||{};try{if((e==null?void 0:e.logSeverityLevel)===void 0)i.logSeverityLevel=2;else if(typeof e.logSeverityLevel!="number"||!Number.isInteger(e.logSeverityLevel)||e.logSeverityLevel<0||e.logSeverityLevel>4)throw new Error(`log severity level is not valid: ${e.logSeverityLevel}`);if((e==null?void 0:e.logVerbosityLevel)===void 0)i.logVerbosityLevel=0;else if(typeof e.logVerbosityLevel!="number"||!Number.isInteger(e.logVerbosityLevel))throw new Error(`log verbosity level is not valid: ${e.logVerbosityLevel}`);(e==null?void 0:e.terminate)===void 0&&(i.terminate=!1);let s=0;return(e==null?void 0:e.tag)!==void 0&&(s=gn(e.tag,r)),n=t._OrtCreateRunOptions(i.logSeverityLevel,i.logVerbosityLevel,!!i.terminate,s),n===0&&je("Can't create run options."),(e==null?void 0:e.extra)!==void 0&&Pa(e.extra,"",new WeakSet,(a,o)=>{let l=gn(a,r),d=gn(o,r);t._OrtAddRunConfigEntry(n,l,d)!==0&&je(`Can't set a run config entry: ${a} - ${o}.`)}),[n,r]}catch(s){throw n!==0&&t._OrtReleaseRunOptions(n),r.forEach(a=>t._free(a)),s}}}),vp,xp,$p,ts,Sp,V0,g$=re(()=>{ei(),Vu(),vp=e=>{switch(e){case"disabled":return 0;case"basic":return 1;case"extended":return 2;case"layout":return 3;case"all":return 99;default:throw new Error(`unsupported graph optimization level: ${e}`)}},xp=e=>{switch(e){case"sequential":return 0;case"parallel":return 1;default:throw new Error(`unsupported execution mode: ${e}`)}},$p=e=>{e.extra||(e.extra={}),e.extra.session||(e.extra.session={});let t=e.extra.session;t.use_ort_model_bytes_directly||(t.use_ort_model_bytes_directly="1"),e.executionProviders&&e.executionProviders.some(n=>(typeof n=="string"?n:n.name)==="webgpu")&&(e.enableMemPattern=!1)},ts=(e,t,n,r)=>{let i=gn(t,r),s=gn(n,r);tt()._OrtAddSessionConfigEntry(e,i,s)!==0&&je(`Can't set a session config entry: ${t} - ${n}.`)},Sp=async(e,t,n)=>{let r=t.executionProviders;for(let i of r){let s=typeof i=="string"?i:i.name,a=[];switch(s){case"webnn":if(s="WEBNN",typeof i!="string"){let f=i==null?void 0:i.deviceType;f&&ts(e,"deviceType",f,n)}break;case"webgpu":if(s="JS",typeof i!="string"){let f=i;if(f!=null&&f.preferredLayout){if(f.preferredLayout!=="NCHW"&&f.preferredLayout!=="NHWC")throw new Error(`preferredLayout must be either 'NCHW' or 'NHWC': ${f.preferredLayout}`);ts(e,"preferredLayout",f.preferredLayout,n)}}break;case"wasm":case"cpu":continue;default:throw new Error(`not supported execution provider: ${s}`)}let o=gn(s,n),l=a.length,d=0,p=0;if(l>0){d=tt()._malloc(l*tt().PTR_SIZE),n.push(d),p=tt()._malloc(l*tt().PTR_SIZE),n.push(p);for(let f=0;f{let t=tt(),n=0,r=[],i=e||{};$p(i);try{let s=vp(i.graphOptimizationLevel??"all"),a=xp(i.executionMode??"sequential"),o=typeof i.logId=="string"?gn(i.logId,r):0,l=i.logSeverityLevel??2;if(!Number.isInteger(l)||l<0||l>4)throw new Error(`log severity level is not valid: ${l}`);let d=i.logVerbosityLevel??0;if(!Number.isInteger(d)||d<0||d>4)throw new Error(`log verbosity level is not valid: ${d}`);let p=typeof i.optimizedModelFilePath=="string"?gn(i.optimizedModelFilePath,r):0;if(n=t._OrtCreateSessionOptions(s,!!i.enableCpuMemArena,!!i.enableMemPattern,a,!!i.enableProfiling,0,o,l,d,p),n===0&&je("Can't create session options."),i.executionProviders&&await Sp(n,i,r),i.enableGraphCapture!==void 0){if(typeof i.enableGraphCapture!="boolean")throw new Error(`enableGraphCapture must be a boolean value: ${i.enableGraphCapture}`);ts(n,"enableGraphCapture",i.enableGraphCapture.toString(),r)}if(i.freeDimensionOverrides)for(let[f,c]of Object.entries(i.freeDimensionOverrides)){if(typeof f!="string")throw new Error(`free dimension override name must be a string: ${f}`);if(typeof c!="number"||!Number.isInteger(c)||c<0)throw new Error(`free dimension override value must be a non-negative integer: ${c}`);let m=gn(f,r);t._OrtAddFreeDimensionOverride(n,m,c)!==0&&je(`Can't set a free dimension override: ${f} - ${c}.`)}return i.extra!==void 0&&Pa(i.extra,"",new WeakSet,(f,c)=>{ts(n,f,c,r)}),[n,r]}catch(s){throw n!==0&&t._OrtReleaseSessionOptions(n)!==0&&je("Can't release session options."),r.forEach(a=>t._free(a)),s}}}),Gr,Yn,Vr,ao,Ua,Fu,Hu,iu,Te=re(()=>{Gr=e=>{switch(e){case"int8":return 3;case"uint8":return 2;case"bool":return 9;case"int16":return 5;case"uint16":return 4;case"int32":return 6;case"uint32":return 12;case"float16":return 10;case"float32":return 1;case"float64":return 11;case"string":return 8;case"int64":return 7;case"uint64":return 13;case"int4":return 22;case"uint4":return 21;default:throw new Error(`unsupported data type: ${e}`)}},Yn=e=>{switch(e){case 3:return"int8";case 2:return"uint8";case 9:return"bool";case 5:return"int16";case 4:return"uint16";case 6:return"int32";case 12:return"uint32";case 10:return"float16";case 1:return"float32";case 11:return"float64";case 8:return"string";case 7:return"int64";case 13:return"uint64";case 22:return"int4";case 21:return"uint4";default:throw new Error(`unsupported data type: ${e}`)}},Vr=(e,t)=>{let n=[-1,4,1,1,2,2,4,8,-1,1,2,8,4,8,-1,-1,-1,-1,-1,-1,-1,.5,.5][e],r=typeof t=="number"?t:t.reduce((i,s)=>i*s,1);return n>0?Math.ceil(r*n):void 0},ao=e=>{switch(e){case"float16":return typeof Float16Array<"u"&&Float16Array.from?Float16Array:Uint16Array;case"float32":return Float32Array;case"uint8":return Uint8Array;case"int8":return Int8Array;case"uint16":return Uint16Array;case"int16":return Int16Array;case"int32":return Int32Array;case"bool":return Uint8Array;case"float64":return Float64Array;case"uint32":return Uint32Array;case"int64":return BigInt64Array;case"uint64":return BigUint64Array;default:throw new Error(`unsupported type: ${e}`)}},Ua=e=>{switch(e){case"verbose":return 0;case"info":return 1;case"warning":return 2;case"error":return 3;case"fatal":return 4;default:throw new Error(`unsupported logging level: ${e}`)}},Fu=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",Hu=e=>e==="float32"||e==="float16"||e==="int32"||e==="int64"||e==="uint32"||e==="uint64"||e==="int8"||e==="uint8"||e==="bool"||e==="uint4"||e==="int4",iu=e=>{switch(e){case"none":return 0;case"cpu":return 1;case"cpu-pinned":return 2;case"texture":return 3;case"gpu-buffer":return 4;case"ml-tensor":return 5;default:throw new Error(`unsupported data location: ${e}`)}}}),ju,F0=re(()=>{Wu(),ju=async e=>{if(typeof e=="string"){let t=await fetch(e);if(!t.ok)throw new Error(`failed to load external data file: ${e}`);let n=t.headers.get("Content-Length"),r=n?parseInt(n,10):0;if(r<1073741824)return new Uint8Array(await t.arrayBuffer());{if(!t.body)throw new Error(`failed to load external data file: ${e}, no response body.`);let i=t.body.getReader(),s;try{s=new ArrayBuffer(r)}catch(o){if(o instanceof RangeError){let l=Math.ceil(r/65536);s=new WebAssembly.Memory({initial:l,maximum:l}).buffer}else throw o}let a=0;for(;;){let{done:o,value:l}=await i.read();if(o)break;let d=l.byteLength;new Uint8Array(s,a,d).set(l),a+=d}return new Uint8Array(s,0,r)}}else return e instanceof Blob?new Uint8Array(await e.arrayBuffer()):e instanceof Uint8Array?e:new Uint8Array(e)}}),Tp,Ip,Cp,Ep,Ku,kp,Pe,nr=re(()=>{Te(),Tp=["V","I","W","E","F"],Ip=(e,t)=>{console.log(`[${Tp[e]},${new Date().toISOString()}]${t}`)},Ku=(e,t)=>{Cp=e,Ep=t},kp=(e,t)=>{let n=Ua(e),r=Ua(Cp);n>=r&&Ip(n,typeof t=="function"?t():t)},Pe=(...e)=>{Ep&&kp(...e)}}),zp,Ei,W,La,H0,j0,K0,Ce=re(()=>{zp=class{static calcMatMulShape(e,t){return e[1]!==t[0]?void 0:[e[0],t[1]]}},Ei=class{static calcShape(e,t,n=!1){let r=e.length,i=t.length;if(r===0)return t;if(i===0)return e;let s=Math.max(e.length,t.length),a=new Array(s);if(n){if(r<2||i<2)return;let o=zp.calcMatMulShape([e[r-2],e[r-1]],[t[i-2],t[i-1]]);if(o===void 0)return;[a[s-2],a[s-1]]=o}for(let o=n?3:1;o<=s;o++){let l=r-o<0?1:e[r-o],d=i-o<0?1:t[i-o];if(l!==d&&l>1&&d>1)return;let p=Math.max(l,d);if(l&&d)a[s-o]=Math.max(l,d);else{if(p>1)return;a[s-o]=0}}return a}static isValidBroadcast(e,t){let n=e.length,r=t.length;if(n>r)return!1;for(let i=1;i<=n;i++)if(e[n-i]!==1&&e[n-i]!==t[r-i])return!1;return!0}},W=class Ea{static size(t){return Ea.getSizeFromDimensionRange(t,0,t.length)}static convertShape(t,n=4){let r=t.length;if(r===0)return[];let i=new Array(r),s=r-1;for(;s>=0;){if(t[s]%n===0){i[s]=t[s]/n;break}if(n%t[s]!==0)throw new Error("cannot convert shape");i[s]=1,n/=t[s],s--}for(s--;s>=0;s--)i[s]=t[s];return i}static sizeFromDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeFromDimension as Tensor has ${t.length} dimensions.`);return Ea.getSizeFromDimensionRange(t,n,t.length)}static sizeToDimension(t,n){if(n<0||n>t.length)throw new Error(`invalid dimension of ${n} for sizeToDimension as Tensor has ${t.length} dimensions.`);return Ea.getSizeFromDimensionRange(t,0,n)}static getSizeFromDimensionRange(t,n,r){let i=1;for(let s=n;s=0;--i)r[i]=r[i+1]*t[i+1];return r}static normalizeAxis(t,n){if(t<-n&&t>=n)throw new Error("unsupported axis for this operation.");return t<0?t+n:t}static normalizeAxes(t,n){return t.map(r=>this.normalizeAxis(r,n??t.length))}static sortBasedOnPerm(t,n){return n?n.map(r=>t[r]):t.slice().reverse()}static padShape(t,n){let r=t.length;return t.map((i,s)=>i+n[s]+n[s+r])}static areEqual(t,n){return t.length!==n.length?!1:t.every((r,i)=>r===n[i])}},La=class ps{static adjustPoolAttributes(t,n,r,i,s,a){if(!t&&r.length!==n.length-2)throw new Error("length of specified kernel shapes should be 2 less than length of input dimensions");if(t)for(let o=0;o=r.length?r.push(n[o+2]):r[o]=n[o+2];for(let o=0;o=r[o]||a[o+r.length]>=r[o])throw new Error("pads should be smaller than kernel")}}static adjustPadsBasedOnAutoPad(t,n,r,i,s,a,o){if(o){if(s.length!==2*(t.length-2))throw new Error("length of pads should be twice the length of data dimensions");if(n.length!==t.length-2)throw new Error("length of strides should be the length of data dimensions");if(i.length!==t.length-2)throw new Error("length of kernel shapes should be the length of data dimensions");for(let l=0;l{Te(),Xu=(e,t)=>new(ao(t))(e)}),Jo,su,el,Ap,tl,Mp,nl,rl,il,Rp,Y0,y$=re(()=>{Te(),nr(),Jo=new Map([["float32",32],["float16",16],["int32",32],["uint32",32],["int64",64],["uint64",64],["int8",8],["uint8",8],["int4",4],["uint4",4]]),su=(e,t)=>{if(t==="int32")return e;let n=Jo.get(t);if(!n)throw new Error(`WebNN backend does not support data type: ${t}`);let r=n/8;if(e.byteLength%r!==0)throw new Error(`Invalid Uint8Array length - must be a multiple of ${r}.`);let i=e.byteLength/r,s=new(ao(t))(e.buffer,e.byteOffset,i);switch(t){case"int64":case"uint64":{let a=new Int32Array(i);for(let o=0;o2147483647n||l<-2147483648n)throw new Error("Can not convert int64 data to int32 - value out of range.");a[o]=Number(l)}return new Uint8Array(a.buffer)}case"int8":case"uint8":case"uint32":{if(t==="uint32"&&s.some(o=>o>2147483647))throw new Error("Can not convert uint32 data to int32 - value out of range.");let a=Int32Array.from(s,Number);return new Uint8Array(a.buffer)}default:throw new Error(`Unsupported data conversion from ${t} to 'int32'`)}},el=(e,t)=>{if(t==="int32")return e;if(e.byteLength%4!==0)throw new Error("Invalid Uint8Array length - must be a multiple of 4 (int32).");let n=e.byteLength/4,r=new Int32Array(e.buffer,e.byteOffset,n);switch(t){case"int64":{let i=BigInt64Array.from(r,BigInt);return new Uint8Array(i.buffer)}case"uint64":{if(r.some(s=>s<0))throw new Error("Can not convert int32 data to uin64 - negative value found.");let i=BigUint64Array.from(r,BigInt);return new Uint8Array(i.buffer)}case"int8":{if(r.some(s=>s<-128||s>127))throw new Error("Can not convert int32 data to int8 - value out of range.");let i=Int8Array.from(r,Number);return new Uint8Array(i.buffer)}case"uint8":{if(r.some(i=>i<0||i>255))throw new Error("Can not convert int32 data to uint8 - value out of range.");return Uint8Array.from(r,Number)}case"uint32":{if(r.some(s=>s<0))throw new Error("Can not convert int32 data to uint32 - negative value found.");let i=Uint32Array.from(r,Number);return new Uint8Array(i.buffer)}default:throw new Error(`Unsupported data conversion from 'int32' to ${t}`)}},Ap=1,tl=()=>Ap++,Mp=new Map([["int8","int32"],["uint8","int32"],["uint32","int32"],["int64","int32"]]),nl=(e,t)=>{let n=Jo.get(e);if(!n)throw new Error(`WebNN backend does not support data type: ${e}`);return t.length>0?Math.ceil(t.reduce((r,i)=>r*i)*n/8):0},rl=class{constructor(e){this.isDataConverted=!1;let{sessionId:t,context:n,tensor:r,dataType:i,shape:s,fallbackDataType:a}=e;this.sessionId=t,this.mlContext=n,this.mlTensor=r,this.dataType=i,this.tensorShape=s,this.fallbackDataType=a}get tensor(){return this.mlTensor}get type(){return this.dataType}get fallbackType(){return this.fallbackDataType}get shape(){return this.tensorShape}get byteLength(){return nl(this.dataType,this.tensorShape)}destroy(){Pe("verbose",()=>"[WebNN] TensorWrapper.destroy"),this.mlTensor.destroy()}write(e){this.mlContext.writeTensor(this.mlTensor,e)}async read(e){if(this.fallbackDataType){let t=await this.mlContext.readTensor(this.mlTensor),n=el(new Uint8Array(t),this.dataType);if(e){(e instanceof ArrayBuffer?new Uint8Array(e):new Uint8Array(e.buffer,e.byteOffset,e.byteLength)).set(n);return}else return n.buffer}else return e?this.mlContext.readTensor(this.mlTensor,e):this.mlContext.readTensor(this.mlTensor)}canReuseTensor(e,t,n){return this.mlContext===e&&this.dataType===t&&this.tensorShape.length===n.length&&this.tensorShape.every((r,i)=>r===n[i])}setIsDataConverted(e){this.isDataConverted=e}},il=class{constructor(e,t){this.tensorManager=e,this.wrapper=t}get tensorWrapper(){return this.wrapper}releaseTensor(){this.tensorWrapper&&(this.tensorManager.releaseTensor(this.tensorWrapper),this.wrapper=void 0)}async ensureTensor(e,t,n,r){let i=this.tensorManager.getMLContext(e),s=this.tensorManager.getMLOpSupportLimits(e),a;if(!(s!=null&&s.input.dataTypes.includes(t))){if(a=Mp.get(t),!a||(s==null?void 0:s.input.dataTypes.includes(a)))throw new Error(`WebNN backend does not support data type: ${t}`);Pe("verbose",()=>`[WebNN] TensorIdTracker.ensureTensor: fallback dataType from ${t} to ${a}`)}if(this.wrapper){if(this.wrapper.canReuseTensor(i,t,n))return this.wrapper.tensor;if(r){if(this.wrapper.byteLength!==nl(t,n))throw new Error("Unable to copy data to tensor with different size.");this.activeUpload=new Uint8Array(await this.wrapper.read())}this.tensorManager.releaseTensor(this.wrapper)}let o=typeof MLTensorUsage>"u"?void 0:MLTensorUsage.READ|MLTensorUsage.WRITE;return this.wrapper=await this.tensorManager.getCachedTensor(e,t,n,o,!0,!0,a),r&&this.activeUpload&&(this.wrapper.write(this.activeUpload),this.activeUpload=void 0),this.wrapper.tensor}upload(e){let t=e;if(this.wrapper){if(this.wrapper.fallbackType)if(this.wrapper.fallbackType==="int32")t=su(e,this.wrapper.type),this.wrapper.setIsDataConverted(!0);else throw new Error(`Unsupported fallback data type: ${this.wrapper.fallbackType}`);if(e.byteLength===this.wrapper.byteLength){this.wrapper.write(t);return}else Pe("verbose",()=>"Data size does not match tensor size. Releasing tensor."),this.releaseTensor()}this.activeUpload?this.activeUpload.set(t):this.activeUpload=new Uint8Array(t)}async download(e){var t,n;if(this.activeUpload){let r=(t=this.wrapper)!=null&&t.isDataConverted?el(this.activeUpload,(n=this.wrapper)==null?void 0:n.type):this.activeUpload;if(e){e instanceof ArrayBuffer?new Uint8Array(e).set(r):new Uint8Array(e.buffer,e.byteOffset,e.byteLength).set(r);return}else return r.buffer}if(!this.wrapper)throw new Error("Tensor has not been created.");return e?this.wrapper.read(e):this.wrapper.read()}},Rp=class{constructor(e){this.backend=e,this.tensorTrackersById=new Map,this.freeTensors=[],this.externalTensors=new Set}getMLContext(e){let t=this.backend.getMLContext(e);if(!t)throw new Error("MLContext not found for session.");return t}getMLOpSupportLimits(e){return this.backend.getMLOpSupportLimits(e)}reserveTensorId(){let e=tl();return this.tensorTrackersById.set(e,new il(this)),e}releaseTensorId(e){let t=this.tensorTrackersById.get(e);t&&(this.tensorTrackersById.delete(e),t.tensorWrapper&&this.releaseTensor(t.tensorWrapper))}async ensureTensor(e,t,n,r,i){Pe("verbose",()=>`[WebNN] TensorManager.ensureTensor {tensorId: ${t}, dataType: ${n}, shape: ${r}, copyOld: ${i}}`);let s=this.tensorTrackersById.get(t);if(!s)throw new Error("Tensor not found.");return s.ensureTensor(e,n,r,i)}upload(e,t){let n=this.tensorTrackersById.get(e);if(!n)throw new Error("Tensor not found.");n.upload(t)}async download(e,t){Pe("verbose",()=>`[WebNN] TensorManager.download {tensorId: ${e}, dstBuffer: ${t==null?void 0:t.byteLength}}`);let n=this.tensorTrackersById.get(e);if(!n)throw new Error("Tensor not found.");return n.download(t)}releaseTensorsForSession(e){for(let t of this.freeTensors)t.sessionId===e&&t.destroy();this.freeTensors=this.freeTensors.filter(t=>t.sessionId!==e)}registerTensor(e,t,n,r){let i=this.getMLContext(e),s=tl(),a=new rl({sessionId:e,context:i,tensor:t,dataType:n,shape:r});return this.tensorTrackersById.set(s,new il(this,a)),this.externalTensors.add(a),s}async getCachedTensor(e,t,n,r,i,s,a){let o=this.getMLContext(e);for(let[d,p]of this.freeTensors.entries())if(p.canReuseTensor(o,t,n)){Pe("verbose",()=>`[WebNN] Reusing tensor {dataType: ${t}, ${a?`fallbackDataType: ${a},`:""} shape: ${n}`);let f=this.freeTensors.splice(d,1)[0];return f.sessionId=e,f}Pe("verbose",()=>`[WebNN] MLContext.createTensor {dataType: ${t}, ${a?`fallbackDataType: ${a},`:""} shape: ${n}}`);let l=await o.createTensor({dataType:a??t,shape:n,dimensions:n,usage:r,writable:i,readable:s});return new rl({sessionId:e,context:o,tensor:l,dataType:t,shape:n,fallbackDataType:a})}releaseTensor(e){this.externalTensors.has(e)&&this.externalTensors.delete(e),this.freeTensors.push(e)}},Y0=(...e)=>new Rp(...e)}),ns,Op,Z0,b$=re(()=>{Te(),ei(),X0(),y$(),nr(),ns=new Map([[1,"float32"],[10,"float16"],[6,"int32"],[12,"uint32"],[7,"int64"],[13,"uint64"],[22,"int4"],[21,"uint4"],[3,"int8"],[2,"uint8"],[9,"uint8"]]),Op=(e,t)=>{if(e===t)return!0;if(e===void 0||t===void 0)return!1;let n=Object.keys(e).sort(),r=Object.keys(t).sort();return n.length===r.length&&n.every((i,s)=>i===r[s]&&e[i]===t[i])},Z0=class{constructor(e){this.tensorManager=Y0(this),this.mlContextBySessionId=new Map,this.sessionIdsByMLContext=new Map,this.mlContextCache=[],this.sessionGraphInputs=new Map,this.sessionGraphOutputs=new Map,this.temporaryGraphInputs=[],this.temporaryGraphOutputs=[],this.temporarySessionTensorIds=new Map,this.mlOpSupportLimitsBySessionId=new Map,Ku(e.logLevel,!!e.debug)}get currentSessionId(){if(this.activeSessionId===void 0)throw new Error("No active session");return this.activeSessionId}onRunStart(e){Pe("verbose",()=>`[WebNN] onRunStart {sessionId: ${e}}`),this.activeSessionId=e}onRunEnd(e){Pe("verbose",()=>`[WebNN] onRunEnd {sessionId: ${e}}`);let t=this.temporarySessionTensorIds.get(e);if(t){for(let n of t)Pe("verbose",()=>`[WebNN] releasing temporary tensor {tensorId: ${n}}`),this.tensorManager.releaseTensorId(n);this.temporarySessionTensorIds.delete(e),this.activeSessionId=void 0}}async createMLContext(e){if(e instanceof GPUDevice){let n=this.mlContextCache.findIndex(r=>r.gpuDevice===e);if(n!==-1)return this.mlContextCache[n].mlContext;{let r=await navigator.ml.createContext(e);return this.mlContextCache.push({gpuDevice:e,mlContext:r}),r}}else if(e===void 0){let n=this.mlContextCache.findIndex(r=>r.options===void 0&&r.gpuDevice===void 0);if(n!==-1)return this.mlContextCache[n].mlContext;{let r=await navigator.ml.createContext();return this.mlContextCache.push({mlContext:r}),r}}let t=this.mlContextCache.findIndex(n=>Op(n.options,e));if(t!==-1)return this.mlContextCache[t].mlContext;{let n=await navigator.ml.createContext(e);return this.mlContextCache.push({options:e,mlContext:n}),n}}registerMLContext(e,t){this.mlContextBySessionId.set(e,t);let n=this.sessionIdsByMLContext.get(t);n||(n=new Set,this.sessionIdsByMLContext.set(t,n)),n.add(e),this.mlOpSupportLimitsBySessionId.has(e)||this.mlOpSupportLimitsBySessionId.set(e,t.opSupportLimits()),this.temporaryGraphInputs.length>0&&(this.sessionGraphInputs.set(e,this.temporaryGraphInputs),this.temporaryGraphInputs=[]),this.temporaryGraphOutputs.length>0&&(this.sessionGraphOutputs.set(e,this.temporaryGraphOutputs),this.temporaryGraphOutputs=[])}onReleaseSession(e){this.sessionGraphInputs.delete(e),this.sessionGraphOutputs.delete(e);let t=this.mlContextBySessionId.get(e);if(!t)return;this.tensorManager.releaseTensorsForSession(e),this.mlContextBySessionId.delete(e),this.mlOpSupportLimitsBySessionId.delete(e);let n=this.sessionIdsByMLContext.get(t);if(n.delete(e),n.size===0){this.sessionIdsByMLContext.delete(t);let r=this.mlContextCache.findIndex(i=>i.mlContext===t);r!==-1&&this.mlContextCache.splice(r,1)}}getMLContext(e){return this.mlContextBySessionId.get(e)}getMLOpSupportLimits(e){return this.mlOpSupportLimitsBySessionId.get(e)}reserveTensorId(){return this.tensorManager.reserveTensorId()}releaseTensorId(e){Pe("verbose",()=>`[WebNN] releaseTensorId {tensorId: ${e}}`),this.tensorManager.releaseTensorId(e)}async ensureTensor(e,t,n,r,i){let s=ns.get(n);if(!s)throw new Error(`Unsupported ONNX data type: ${n}`);return this.tensorManager.ensureTensor(e??this.currentSessionId,t,s,r,i)}async createTemporaryTensor(e,t,n){Pe("verbose",()=>`[WebNN] createTemporaryTensor {onnxDataType: ${t}, shape: ${n}}`);let r=ns.get(t);if(!r)throw new Error(`Unsupported ONNX data type: ${t}`);let i=this.tensorManager.reserveTensorId();await this.tensorManager.ensureTensor(e,i,r,n,!1);let s=this.temporarySessionTensorIds.get(e);return s?s.push(i):this.temporarySessionTensorIds.set(e,[i]),i}uploadTensor(e,t){if(!tt().shouldTransferToMLTensor)throw new Error("Trying to upload to a MLTensor while shouldTransferToMLTensor is false");Pe("verbose",()=>`[WebNN] uploadTensor {tensorId: ${e}, data: ${t.byteLength}}`),this.tensorManager.upload(e,t)}async downloadTensor(e,t){return this.tensorManager.download(e,t)}createMLTensorDownloader(e,t){return async()=>{let n=await this.tensorManager.download(e);return Xu(n,t)}}registerMLTensor(e,t,n,r){let i=ns.get(n);if(!i)throw new Error(`Unsupported ONNX data type: ${n}`);let s=this.tensorManager.registerTensor(e,t,i,r);return Pe("verbose",()=>`[WebNN] registerMLTensor {tensor: ${t}, dataType: ${i}, dimensions: ${r}} -> {tensorId: ${s}}`),s}registerMLConstant(e,t,n,r,i,s,a=!1){if(!s)throw new Error("External mounted files are not available.");let o=e;e.startsWith("./")&&(o=e.substring(2));let l=s.get(o);if(!l)throw new Error(`File with name ${o} not found in preloaded files.`);if(t+n>l.byteLength)throw new Error("Out of bounds: data offset and length exceed the external file data size.");let d=l.slice(t,t+n).buffer,p;switch(i.dataType){case"float32":p=new Float32Array(d);break;case"float16":p=typeof Float16Array<"u"&&Float16Array.from?new Float16Array(d):new Uint16Array(d);break;case"int32":p=new Int32Array(d);break;case"uint32":p=new Uint32Array(d);break;case"int64":if(a){let f=su(new Uint8Array(d),"int64");p=new Int32Array(f.buffer),i.dataType="int32"}else p=new BigInt64Array(d);break;case"uint64":p=new BigUint64Array(d);break;case"int8":p=new Int8Array(d);break;case"int4":case"uint4":case"uint8":p=new Uint8Array(d);break;default:throw new Error(`Unsupported data type: ${i.dataType} in creating WebNN Constant from external data.`)}return Pe("verbose",()=>`[WebNN] registerMLConstant {dataType: ${i.dataType}, shape: ${i.shape}}} ${a?"(Note: it was int64 data type and registered to int32 as workaround)":""}`),r.constant(i,p)}registerGraphInput(e){this.temporaryGraphInputs.push(e)}registerGraphOutput(e){this.temporaryGraphOutputs.push(e)}isGraphInput(e,t){let n=this.sessionGraphInputs.get(e);return n?n.includes(t):!1}isGraphOutput(e,t){let n=this.sessionGraphOutputs.get(e);return n?n.includes(t):!1}isGraphInputOutputTypeSupported(e,t,n=!0){let r=ns.get(Gr(t)),i=this.mlOpSupportLimitsBySessionId.get(e);return typeof r>"u"?!1:n?!!(i!=null&&i.input.dataTypes.includes(r)):!!(i!=null&&i.output.dataTypes.includes(r))}flush(){}}}),Yu=re(()=>{}),sl,ua,da,Np,Bp,al,au,Dp,Q0,w$=re(()=>{nr(),Yu(),sl=new Map([[64,250],[128,200],[256,200],[512,200],[2048,230],[4096,200],[8192,50],[16384,50],[32768,50],[65536,50],[131072,50],[262144,50],[524288,50],[1048576,50],[2097152,30],[4194304,20],[8388608,10],[12582912,10],[16777216,10],[26214400,15],[33554432,22],[44236800,2],[58982400,6],[67108864,6],[134217728,6],[167772160,6]]),ua=[],da=e=>Math.ceil(Number(e)/16)*16,Np=e=>{for(let t=0;tBp++,au=async(e,t,n,r)=>{let i=da(n),s=e.device.createBuffer({size:i,usage:GPUBufferUsage.COPY_DST|GPUBufferUsage.MAP_READ});try{let a=e.getCommandEncoder();e.endComputePass(),a.copyBufferToBuffer(t,0,s,0,i),e.flush(),await s.mapAsync(GPUMapMode.READ);let o=s.getMappedRange();if(r){let l=r();return l.set(new Uint8Array(o,0,n)),l}else return new Uint8Array(o.slice(0,n))}finally{s.destroy()}},Dp=class{constructor(e){this.backend=e,this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.buffersPending=[],this.capturedPendingBuffers=new Map;for(let[t]of sl)ua.push(t),this.freeBuffers.set(t,[]),this.freeUniformBuffers.set(t,[]);this.sessionCount=0}upload(e,t){let n=t.buffer,r=t.byteOffset,i=t.byteLength,s=da(i),a=this.storageCache.get(e);if(!a)throw new Error("gpu data for uploading does not exist");if(Number(a.originalSize)!==i)throw new Error(`inconsistent data size. gpu data size=${a.originalSize}, data size=${i}`);let o=this.backend.device.createBuffer({mappedAtCreation:!0,size:s,usage:GPUBufferUsage.MAP_WRITE|GPUBufferUsage.COPY_SRC}),l=o.getMappedRange();new Uint8Array(l).set(new Uint8Array(n,r,i)),o.unmap();let d=this.backend.device.createCommandEncoder();d.copyBufferToBuffer(o,0,a.gpuData.buffer,0,s),this.backend.device.queue.submit([d.finish()]),o.destroy(),Pe("verbose",()=>`[WebGPU] GpuDataManager.upload(id=${e})`)}memcpy(e,t){let n=this.storageCache.get(e);if(!n)throw new Error("source gpu data for memcpy does not exist");let r=this.storageCache.get(t);if(!r)throw new Error("destination gpu data for memcpy does not exist");if(n.originalSize!==r.originalSize)throw new Error("inconsistent source and destination gpu data size");let i=da(n.originalSize),s=this.backend.getCommandEncoder();this.backend.endComputePass(),s.copyBufferToBuffer(n.gpuData.buffer,0,r.gpuData.buffer,0,i)}registerExternalBuffer(e,t,n){let r;if(n){if(r=n[0],e===n[1])return Pe("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${r}, buffer is the same, skip.`),r;if(this.backend.capturedCommandList.has(this.backend.currentSessionId))throw new Error(`Registering a different external buffer under graph capture mode is not supported yet. + Please use the previous external buffer!`)}else r=al();return this.storageCache.set(r,{gpuData:{id:r,type:0,buffer:e},originalSize:t}),Pe("verbose",()=>`[WebGPU] GpuDataManager.registerExternalBuffer(size=${t}) => id=${r}, registered.`),r}unregisterExternalBuffer(e){e!==void 0&&(this.storageCache.delete(e),Pe("verbose",()=>`[WebGPU] GpuDataManager.unregisterExternalBuffer() => id=${e}`))}create(e,t=GPUBufferUsage.STORAGE|GPUBufferUsage.COPY_SRC|GPUBufferUsage.COPY_DST){let n=Np(e),r,i=(t&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE,s=(t&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM;if(i||s){let o=(i?this.freeBuffers:this.freeUniformBuffers).get(n);o?o.length>0?r=o.pop():r=this.backend.device.createBuffer({size:n,usage:t}):r=this.backend.device.createBuffer({size:n,usage:t})}else r=this.backend.device.createBuffer({size:n,usage:t});let a={id:al(),type:0,buffer:r};return this.storageCache.set(a.id,{gpuData:a,originalSize:Number(e)}),Pe("verbose",()=>`[WebGPU] GpuDataManager.create(size=${e}) => id=${a.id}`),a}get(e){var t;return(t=this.storageCache.get(e))==null?void 0:t.gpuData}release(e){let t=typeof e=="bigint"?Number(e):e,n=this.storageCache.get(t);if(!n){if(this.storageCache.size===0)return 0;throw new Error("releasing data does not exist")}return Pe("verbose",()=>`[WebGPU] GpuDataManager.release(id=${t}), gpuDataId=${n.gpuData.id}`),this.storageCache.delete(t),this.buffersPending.push(n.gpuData.buffer),n.originalSize}async download(e,t){let n=this.storageCache.get(Number(e));if(!n)throw new Error("data does not exist");await au(this.backend,n.gpuData.buffer,n.originalSize,t)}refreshPendingBuffers(){if(this.buffersPending.length!==0)if(this.backend.sessionStatus==="default"){for(let e of this.buffersPending){let t=sl.get(e.size);if((e.usage&GPUBufferUsage.STORAGE)===GPUBufferUsage.STORAGE){let n=this.freeBuffers.get(e.size)||[];t===void 0||n.length>=t?e.destroy():n.push(e)}else if((e.usage&GPUBufferUsage.UNIFORM)===GPUBufferUsage.UNIFORM){let n=this.freeUniformBuffers.get(e.size)||[];t===void 0||n.length>=t?e.destroy():n.push(e)}else e.destroy()}this.buffersPending=[]}else{let e=this.capturedPendingBuffers.get(this.backend.currentSessionId);e||(e=[],this.capturedPendingBuffers.set(this.backend.currentSessionId,e));for(let t of this.buffersPending)e.push(t);this.buffersPending=[]}}dispose(){this.freeBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.freeUniformBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache.forEach(e=>{e.gpuData.buffer.destroy()}),this.capturedPendingBuffers.forEach(e=>{e.forEach(t=>{t.destroy()})}),this.storageCache=new Map,this.freeBuffers=new Map,this.freeUniformBuffers=new Map,this.capturedPendingBuffers=new Map}onCreateSession(){this.sessionCount+=1}onReleaseSession(e){let t=this.capturedPendingBuffers.get(e);t&&(t.forEach(n=>{n.destroy()}),this.capturedPendingBuffers.delete(e)),this.sessionCount-=1,this.sessionCount===0&&(Pe("warning",()=>"[WebGPU] Clearing webgpu buffer cache"),this.storageCache.forEach(n=>{n.gpuData.buffer.destroy()}),this.storageCache=new Map)}},Q0=(...e)=>new Dp(...e)}),Pp,Fe,pt=re(()=>{Pp=class{constructor(e){Object.assign(this,e)}get cacheKey(){return this.key||(this.key=Object.getOwnPropertyNames(this).sort().map(e=>`${this[e]}`).join(";")),this.key}},Fe=e=>new Pp(e)}),ki,ca,xt,Rt,_e,ct,ou,bi,wr,we,rs,V,be,J0,Zu,Up,ey,Ee=re(()=>{Te(),Ce(),ki=64,ca=(e,t)=>{if(t===3)throw new Error("vec3 has same alignment as vec4, use vec4 instead");switch(Number(e)){case 10:return t>1?`vec${t}`:"f16";case 1:return t>1?`vec${t}`:"f32";case 6:return t>1?`vec${t}`:"i32";case 12:return t>1?`vec${t}`:"u32";case 7:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","i32"];case 13:if(t>1)throw new Error("currently not supported vecX of uint64 yet");return["vec2","u32"];case 9:if(t!==4)throw new Error("bool must be vec4");return["u32","vec4"];case 22:return"i32";case 21:return"u32";default:throw new Error(`Unknown data type: ${e}`)}},xt=(e,t=1)=>{let n=ca(e,t);return typeof n=="string"?n:n[0]},Rt=(e,t=1)=>{let n=ca(e,t);return typeof n=="string"?n:n[1]},_e=(...e)=>{let t=[];return e.forEach(n=>{n.length!==0&&t.push({type:12,data:n},{type:12,data:W.computeStrides(n)})}),t},ct=e=>e%4===0?4:e%2===0?2:1,ou=(e="f32",t,n="0")=>!t||t===1?`${e}(${n})`:`vec${t}<${e}>(${n})`,bi=(e,t,n)=>e==="f32"?n:t===1?`f32(${n})`:`vec${t}(${n})`,wr=(e,t)=>t===4?`(${e}.x + ${e}.y + ${e}.z + ${e}.w)`:t===2?`(${e}.x + ${e}.y)`:t===3?`(${e}.x + ${e}.y + ${e}.z)`:e,we=(e,t,n,r)=>e.startsWith("uniforms.")&&n>4?typeof t=="string"?r==="f16"?`${e}[(${t}) / 8][(${t}) % 8 / 4][(${t}) % 8 % 4]`:`${e}[(${t}) / 4][(${t}) % 4]`:r==="f16"?`${e}[${Math.floor(t/8)}][${Math.floor(t%8/4)}][${t%8%4}]`:`${e}[${Math.floor(t/4)}][${t%4}]`:n>1?`${e}[${t}]`:e,rs=(e,t,n,r,i)=>{let s=typeof n=="number",a=s?n:n.length,o=[...new Array(a).keys()],l=a<2?"u32":a<=4?`vec${a}`:`array`,d=ca(t,i),p=typeof d=="string"?d:d[1],f=typeof d=="string"?d:d[0],c={indices:l,value:p,storage:f,tensor:t},m=Y=>typeof Y=="string"?Y:`${Y}u`,y={offsetToIndices:!1,indicesToOffset:!1,broadcastedIndicesToOffset:!1,set:!1,setByIndices:!1,get:!1,getByIndices:!1},w=s?"uniforms.":"",$=`${w}${e}_shape`,_=`${w}${e}_strides`,v="";for(let Y=0;Y ${c.indices} { + var indices: ${c.indices}; + var current = offset; + ${v} + return indices; + }`,S=Y=>(y.offsetToIndices=!0,a<2?Y:`o2i_${e}(${Y})`),E=[];if(a>=2)for(let Y=a-1;Y>=0;Y--)E.push(`${we(_,Y,a)} * (indices[${Y}])`);let R=a<2?"":` + fn i2o_${e}(indices: ${c.indices}) -> u32 { + return ${E.join("+")}; + }`,O=Y=>(y.indicesToOffset=!0,a<2?Y:`i2o_${e}(${Y})`),x=(...Y)=>a===0?"0u":`${c.indices}(${Y.map(m).join(",")})`,U=(Y,P)=>a<2?`${Y}`:`${we(Y,P,a)}`,q=(Y,P,ee)=>a<2?`${Y}=${ee};`:`${we(Y,P,a)}=${ee};`,J={},se=(Y,P)=>{y.broadcastedIndicesToOffset=!0;let ee=`${P.name}broadcastedIndicesTo${e}Offset`;if(ee in J)return`${ee}(${Y})`;let ne=[];for(let Ue=a-1;Ue>=0;Ue--){let lt=P.indicesGet("outputIndices",Ue+P.rank-a);ne.push(`${U(_,Ue)} * (${lt} % ${U($,Ue)})`)}return J[ee]=`fn ${ee}(outputIndices: ${P.type.indices}) -> u32 { + return ${ne.length>0?ne.join("+"):"0u"}; + }`,`${ee}(${Y})`},ce=(Y,P)=>(()=>{if(c.storage===c.value)return`${e}[${Y}]=${P};`;if(c.storage==="vec2"&&c.value==="i32")return`${e}[${Y}]=vec2(u32(${P}), select(0u, 0xFFFFFFFFu, ${P} < 0));`;if(c.storage==="vec2"&&c.value==="u32")return`${e}[${Y}]=vec2(u32(${P}), 0u);`;if(c.storage==="u32"&&c.value==="vec4")return`${e}[${Y}]=dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(${P}));`;throw new Error(`not supported combination of storage type ${c.storage} and value type ${c.value} yet`)})(),L=Y=>(()=>{if(c.storage===c.value)return`${e}[${Y}]`;if(c.storage==="vec2"&&c.value==="i32")return`i32(${e}[${Y}].x)`;if(c.storage==="vec2"&&c.value==="u32")return`u32(${e}[${Y}].x)`;if(c.storage==="u32"&&c.value==="vec4")return`vec4(bool(${e}[${Y}] & 0xFFu), bool(${e}[${Y}] & 0xFF00u), bool(${e}[${Y}] & 0xFF0000u), bool(${e}[${Y}] & 0xFF000000u))`;throw new Error(`not supported combination of storage type ${c.storage} and value type ${c.value} yet`)})(),ue=a<2?"":` + fn get_${e}ByIndices(indices: ${c.indices}) -> ${p} { + return ${L(`i2o_${e}(indices)`)}; + }`,ie=a<2?"":(()=>{let Y=o.map(ee=>`d${ee}: u32`).join(", "),P=o.map(ee=>`d${ee}`).join(", ");return` + fn get_${e}(${Y}) -> ${p} { + return get_${e}ByIndices(${x(P)}); + }`})(),te=(...Y)=>{if(Y.length!==a)throw new Error(`indices length must be ${a}`);let P=Y.map(m).join(",");return a===0?L("0u"):a===1?L(P[0]):(y.get=!0,y.getByIndices=!0,y.indicesToOffset=!0,`get_${e}(${P})`)},he=Y=>a<2?L(Y):(y.getByIndices=!0,y.indicesToOffset=!0,`get_${e}ByIndices(${Y})`),Q=a<2?"":` + fn set_${e}ByIndices(indices: ${c.indices}, value: ${p}) { + ${ce(`i2o_${e}(indices)`,"value")} + }`,ve=a<2?"":(()=>{let Y=o.map(ee=>`d${ee}: u32`).join(", "),P=o.map(ee=>`d${ee}`).join(", ");return` + fn set_${e}(${Y}, value: ${p}) { + set_${e}ByIndices(${x(P)}, value); + }`})();return{impl:()=>{let Y=[],P=!1;return y.offsetToIndices&&(Y.push(I),P=!0),y.indicesToOffset&&(Y.push(R),P=!0),y.broadcastedIndicesToOffset&&(Object.values(J).forEach(ee=>Y.push(ee)),P=!0),y.set&&(Y.push(ve),P=!0),y.setByIndices&&(Y.push(Q),P=!0),y.get&&(Y.push(ie),P=!0),y.getByIndices&&(Y.push(ue),P=!0),!s&&P&&Y.unshift(`const ${$} = ${c.indices}(${n.join(",")});`,`const ${_} = ${c.indices}(${W.computeStrides(n).join(",")});`),Y.join(` +`)},type:c,offsetToIndices:S,indicesToOffset:O,broadcastedIndicesToOffset:se,indices:x,indicesGet:U,indicesSet:q,set:(...Y)=>{if(Y.length!==a+1)throw new Error(`indices length must be ${a}`);let P=Y[a];if(typeof P!="string")throw new Error("value must be string");let ee=Y.slice(0,a).map(m).join(",");return a===0?ce("0u",P):a===1?ce(ee[0],P):(y.set=!0,y.setByIndices=!0,y.indicesToOffset=!0,`set_${e}(${ee}, ${P})`)},setByOffset:ce,setByIndices:(Y,P)=>a<2?ce(Y,P):(y.setByIndices=!0,y.indicesToOffset=!0,`set_${e}ByIndices(${Y}, ${P});`),get:te,getByOffset:L,getByIndices:he,usage:r,name:e,strides:_,shape:$,rank:a}},V=(e,t,n,r=1)=>rs(e,t,n,"input",r),be=(e,t,n,r=1)=>rs(e,t,n,"output",r),J0=(e,t,n)=>rs(e,t,n,"atomicOutput",1),Zu=(e,t,n,r=1)=>rs(e,t,n,"internal",r),Up=class{constructor(e,t){this.normalizedDispatchGroup=e,this.limits=t,this.internalVariables=[],this.variables=[],this.uniforms=[],this.variableIndex=0}guardAgainstOutOfBoundsWorkgroupSizes(e){return`if (global_idx >= ${typeof e=="number"?`${e}u`:e}) { return; }`}mainStart(e=ki){let t=typeof e=="number"?e:e[0],n=typeof e=="number"?1:e[1],r=typeof e=="number"?1:e[2];if(t>this.limits.maxComputeWorkgroupSizeX||n>this.limits.maxComputeWorkgroupSizeY||r>this.limits.maxComputeWorkgroupSizeZ)throw new Error(`workgroup size [${t}, ${n}, ${r}] exceeds the maximum workgroup size [${this.limits.maxComputeWorkgroupSizeX}, ${this.limits.maxComputeWorkgroupSizeY}, ${this.limits.maxComputeWorkgroupSizeZ}].`);if(t*n*r>this.limits.maxComputeInvocationsPerWorkgroup)throw new Error(`workgroup size [${t}, ${n}, ${r}] exceeds the maximum workgroup invocations ${this.limits.maxComputeInvocationsPerWorkgroup}.`);let i=this.normalizedDispatchGroup[1]===1&&this.normalizedDispatchGroup[2]===1,s=i?`@builtin(global_invocation_id) global_id : vec3, + @builtin(workgroup_id) workgroup_id : vec3, + @builtin(local_invocation_index) local_idx : u32, + @builtin(local_invocation_id) local_id : vec3`:`@builtin(global_invocation_id) global_id : vec3, + @builtin(local_invocation_id) local_id : vec3, + @builtin(local_invocation_index) local_idx : u32, + @builtin(workgroup_id) workgroup_id : vec3, + @builtin(num_workgroups) num_workgroups : vec3`,a=i?`let global_idx = global_id.x; + let workgroup_index = workgroup_id.x;`:`let workgroup_index = workgroup_id.z * num_workgroups[0] * num_workgroups[1] + + workgroup_id.y * num_workgroups[0] + workgroup_id.x; + let global_idx = workgroup_index * ${t*n*r}u + local_idx;`;return`@compute @workgroup_size(${t}, ${n}, ${r}) + fn main(${s}) { + ${a} + `}appendVariableUniforms(e){e.rank!==0&&(e.shape.startsWith("uniforms.")&&this.uniforms.push({name:e.shape.replace("uniforms.",""),type:"u32",length:e.rank}),e.strides.startsWith("uniforms.")&&this.uniforms.push({name:e.strides.replace("uniforms.",""),type:"u32",length:e.rank}))}declareVariable(e,t){if(e.usage==="internal")throw new Error("cannot use internal variable with declareVariable(). use registerInternalVariables() instead.");this.variables.push(e),this.appendVariableUniforms(e);let n=e.usage==="input"?"read":"read_write",r=e.usage==="atomicOutput"?"atomic":e.type.storage;return`@group(0) @binding(${t}) var ${e.name}: array<${r}>;`}declareVariables(...e){return e.map(t=>this.declareVariable(t,this.variableIndex++)).join(` +`)}registerInternalVariable(e){if(e.usage!=="internal")throw new Error("cannot use input or output variable with registerInternalVariable(). use declareVariables() instead.");this.internalVariables.push(e),this.appendVariableUniforms(e)}registerInternalVariables(...e){return e.forEach(t=>this.registerInternalVariable(t)),this}registerUniform(e,t,n=1){return this.uniforms.push({name:e,type:t,length:n}),this}registerUniforms(e){return this.uniforms=this.uniforms.concat(e),this}uniformDeclaration(){if(this.uniforms.length===0)return"";let e=[];for(let{name:t,type:n,length:r}of this.uniforms)if(r&&r>4)n==="f16"?e.push(`@align(16) ${t}:array, ${Math.ceil(r/8)}>`):e.push(`${t}:array, ${Math.ceil(r/4)}>`);else{let i=r==null||r===1?n:`vec${r}<${n}>`;e.push(`${t}:${i}`)}return` + struct Uniforms { ${e.join(", ")} }; + @group(0) @binding(${this.variableIndex}) var uniforms: Uniforms;`}get additionalImplementations(){return this.uniformDeclaration()+this.variables.map(e=>e.impl()).join(` +`)+this.internalVariables.map(e=>e.impl()).join(` +`)}get variablesInfo(){if(this.uniforms.length===0)return;let e=t=>[12,10,1,6][["u32","f16","f32","i32"].indexOf(t)];return this.uniforms.map(t=>[e(t.type),t.length??1])}},ey=(e,t)=>new Up(e,t)}),Lp,ol,Wp,qp,Gp,Vp,en,ty,ny,_r=re(()=>{Te(),Ce(),pt(),Ee(),Lp=(e,t)=>{if(!e||e.length!==1)throw new Error("Transpose requires 1 input.");if(t.length!==0&&t.length!==e[0].dims.length)throw new Error(`perm size ${t.length} does not match input rank ${e[0].dims.length}`)},ol=(e,t)=>t.length!==0?t:[...new Array(e).keys()].reverse(),Wp=(e,t)=>W.sortBasedOnPerm(e,ol(e.length,t)),qp=(e,t,n,r)=>{let i=`fn perm(i: ${r.type.indices}) -> ${n.type.indices} { + var a: ${n.type.indices};`;for(let s=0;s{let n=[],r=[];for(let i=0;i{let n=0;for(let r=0;r{let n=e.dataType,r=e.dims.length,i=ol(r,t),s=Wp(e.dims,i),a=e.dims,o=s,l=r<2||Vp(i,e.dims),d;if(l)return d=y=>{let w=V("input",n,a,4),$=be("output",n,o,4);return` + ${y.registerUniform("output_size","u32").declareVariables(w,$)} + ${y.mainStart()} + ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + output[global_idx] = input[global_idx]; + }`},{name:"TransposeCopy",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let y=W.size(s);return{outputs:[{dims:s,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(y/64/4)},programUniforms:[{type:12,data:Math.ceil(y/4)}]}},getShaderSource:d};let{newShape:p,newPerm:f}=Gp(e.dims,i),c=W.areEqual(f,[2,3,1]),m=W.areEqual(f,[3,1,2]);if(p.length===2||c||m){a=c?[p[0],p[1]*p[2]]:m?[p[0]*p[1],p[2]]:p,o=[a[1],a[0]];let y=16;return d=w=>{let $=V("a",n,a.length),_=be("output",n,o.length);return` + ${w.registerUniform("output_size","u32").declareVariables($,_)} + var tile : array, ${y}>; + ${w.mainStart([y,y,1])} + let stride = (uniforms.output_shape[1] - 1) / ${y} + 1; + let workgroup_id_x = workgroup_index % stride; + let workgroup_id_y = workgroup_index / stride; + let input_col = workgroup_id_y * ${y}u + local_id.x; + let input_row = workgroup_id_x * ${y}u + local_id.y; + if (input_row < uniforms.a_shape[0] && input_col < uniforms.a_shape[1]) { + tile[local_id.y][local_id.x] = ${$.getByIndices(`${$.type.indices}(input_row, input_col)`)}; + } + workgroupBarrier(); + + let output_col = workgroup_id_x * ${y}u + local_id.x; + let output_row = workgroup_id_y * ${y}u + local_id.y; + if (output_row < uniforms.output_shape[0] && output_col < uniforms.output_shape[1]) { + ${_.setByIndices(`${_.type.indices}(output_row, output_col)`,"tile[local_id.x][local_id.y]")} + } + }`},{name:"TransposeShared",shaderCache:{inputDependencies:["type"]},getRunData:()=>{let w=W.size(s);return{outputs:[{dims:s,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(o[1]/y),y:Math.ceil(o[0]/y)},programUniforms:[{type:12,data:w},..._e(a,o)]}},getShaderSource:d}}return d=y=>{let w=V("a",n,a.length),$=be("output",n,o.length);return` + ${y.registerUniform("output_size","u32").declareVariables(w,$)} + + ${qp(i,r,w,$)} + + ${y.mainStart()} + ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${$.offsetToIndices("global_idx")}; + let aIndices = perm(indices); + + ${$.setByOffset("global_idx",w.getByIndices("aIndices"))} + }`},{name:"Transpose",shaderCache:{hint:`${t}`,inputDependencies:["rank"]},getRunData:()=>{let y=W.size(s);return{outputs:[{dims:s,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:[{type:12,data:y},..._e(a,o)]}},getShaderSource:d}},ty=(e,t)=>{Lp(e.inputs,t.perm),e.compute(en(e.inputs[0],t.perm))},ny=e=>Fe({perm:e.perm})}),Fp,Hp,jp,Kp,Xp,Yp,Zp,Qp,Jp,ef,dn,ry,iy,sy,ay,oy,ly,uy,dy,cy,py,_$=re(()=>{Te(),Ce(),Ee(),Qu(),_r(),Fp={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate * candidate",logSumExp:"bestValue + exp(candidate)",l1:"bestValue + abs(candidate)",l2:"bestValue + candidate * candidate",logSum:"bestValue + candidate"},Hp={max:"select(bestValue, candidate, candidate > bestValue)",min:"select(bestValue, candidate, candidate < bestValue)",mean:"bestValue + candidate",sum:"bestValue + candidate",prod:"bestValue * candidate",sumSquare:"bestValue + candidate",logSumExp:"bestValue + candidate",l1:"bestValue + candidate",l2:"bestValue + candidate",logSum:"bestValue + candidate"},jp={max:"_A[offset]",min:"_A[offset]",mean:"0",sum:"0",prod:"1",sumSquare:"0",logSumExp:"0",l1:"0",l2:"0",logSum:"0"},Kp={max:"bestValue",min:"bestValue",sum:"bestValue",prod:"bestValue",sumSquare:"bestValue",logSumExp:"log(bestValue)",l1:"bestValue",l2:"sqrt(bestValue)",logSum:"log(bestValue)"},Xp=(e,t)=>{let n=[];for(let r=t-e;r{let n=[],r=e.length;for(let s=0;se[s]);return[n,i]},Zp=(e,t)=>{let n=e.length+t.length,r=[],i=0;for(let s=0;s{for(let n=0;n{let n=[];if(!Qp(e,t)){for(let r=0;rn.push(r))}return n},ef=(e,t,n,r,i,s,a)=>{let o=n[0].dims,l=W.size(s),d=W.size(a),p=V("_A",n[0].dataType,o),f=be("output",i,s),c=64;l===1&&(c=256);let m=` + var aBestValues : array; + `,y=w=>` + ${w.registerUniform("reduceSize","u32").declareVariables(p,f)} + ${m} + fn DIV_CEIL(a : u32, b : u32) -> u32 { + return ((a - 1u) / b + 1u); + } + ${w.mainStart(c)} + + let outputIndex = global_idx / ${c}; + let offset = outputIndex * uniforms.reduceSize; + + var bestValue = f32(${jp[r]}); + let Length = uniforms.reduceSize; + for (var k = local_idx; k < Length; k = k + ${c}) { + let candidate = f32(${p.getByOffset("offset + k")}); + bestValue = ${Fp[r]}; + } + aBestValues[local_idx] = bestValue; + workgroupBarrier(); + + var reduceSize = min(Length, ${c}u); + for (var currentSize = reduceSize / 2u; reduceSize > 1u; + currentSize = reduceSize / 2u) { + let interval = DIV_CEIL(reduceSize, 2u); + if (local_idx < currentSize) { + let candidate = aBestValues[local_idx + interval]; + bestValue = ${Hp[r]}; + aBestValues[local_idx] = bestValue; + } + reduceSize = interval; + workgroupBarrier(); + } + + if (local_idx == 0u) { + ${f.setByOffset("outputIndex",`${r==="mean"?`${f.type.storage}(bestValue / f32(uniforms.reduceSize))`:`${f.type.storage}(${Kp[r]})`}`)}; + } + }`;return{name:e,shaderCache:{hint:`${t};${c}`,inputDependencies:["type"]},getShaderSource:y,getRunData:()=>({outputs:[{dims:s,dataType:i}],dispatchGroup:{x:l},programUniforms:[{type:12,data:d}]})}},dn=(e,t,n,r)=>{let i=e.inputs.length===1?n:lu(e.inputs,n),s=i.axes;s.length===0&&!i.noopWithEmptyAxes&&(s=e.inputs[0].dims.map((m,y)=>y));let a=W.normalizeAxes(s,e.inputs[0].dims.length),o=a,l=e.inputs[0],d=Jp(o,e.inputs[0].dims.length);d.length>0&&(l=e.compute(en(e.inputs[0],d),{inputs:[0],outputs:[-1]})[0],o=Xp(o.length,l.dims.length));let[p,f]=Yp(l.dims,o),c=p;i.keepDims&&(c=Zp(p,a)),e.compute(ef(t,i.cacheKey,[l],r,e.inputs[0].dataType,c,f),{inputs:[l]})},ry=(e,t)=>{dn(e,"ReduceMeanShared",t,"mean")},iy=(e,t)=>{dn(e,"ReduceL1Shared",t,"l1")},sy=(e,t)=>{dn(e,"ReduceL2Shared",t,"l2")},ay=(e,t)=>{dn(e,"ReduceLogSumExpShared",t,"logSumExp")},oy=(e,t)=>{dn(e,"ReduceMaxShared",t,"max")},ly=(e,t)=>{dn(e,"ReduceMinShared",t,"min")},uy=(e,t)=>{dn(e,"ReduceProdShared",t,"prod")},dy=(e,t)=>{dn(e,"ReduceSumShared",t,"sum")},cy=(e,t)=>{dn(e,"ReduceSumSquareShared",t,"sumSquare")},py=(e,t)=>{dn(e,"ReduceLogSumShared",t,"logSum")}}),cn,tf,Wa,lu,pn,nf,rf,sf,af,of,lf,uf,df,cf,pf,fn,fy,hy,my,gy,yy,by,wy,_y,vy,xy,Qu=re(()=>{Te(),Ce(),pt(),Ee(),_$(),cn=e=>{if(!e||e.length===0||e.length>2)throw new Error("Reduce op requires 1 or 2 inputs.");if(e.length===2&&e[1].dims.length!==1)throw new Error("Invalid axes input dims.")},tf=e=>["","",`var value = ${e.getByIndices("input_indices")};`,""],Wa=(e,t,n,r,i,s,a=!1,o=!1)=>{let l=[],d=n[0].dims,p=d.length,f=W.normalizeAxes(i,p),c=!o&&f.length===0;d.forEach((w,$)=>{c||f.indexOf($)>=0?a&&l.push(1):l.push(w)});let m=l.length,y=W.size(l);return{name:e,shaderCache:t,getShaderSource:w=>{let $=[],_=V("_A",n[0].dataType,p),v=be("output",s,m),I=r(_,v,f),S=I[2];for(let E=0,R=0;E=0?(a&&R++,S=`for(var j${E}: u32 = 0; j${E} < ${d[E]}; j${E}++) { + ${I[2].includes("last_index")?`let last_index = j${E};`:""} + ${_.indicesSet("input_indices",E,`j${E}`)} + ${S} + }`):($.push(`${_.indicesSet("input_indices",E,v.indicesGet("output_indices",R))};`),R++);return` + + ${w.registerUniform("output_size","u32").declareVariables(_,v)} + + ${w.mainStart()} + ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + var input_indices: ${_.type.indices}; + let output_indices = ${v.offsetToIndices("global_idx")}; + + ${$.join(` +`)} + ${I[0]} // init ops for reduce max/min + ${I[1]} + ${S} + ${I[3]} + ${I.length===4?v.setByOffset("global_idx","value"):I.slice(4).join(` +`)} + }`},getRunData:()=>({outputs:[{dims:l,dataType:s}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:[{type:12,data:y},..._e(d,l)]})}},lu=(e,t)=>{let n=[];return e[1].dims[0]>0&&e[1].getBigInt64Array().forEach(r=>n.push(Number(r))),Fe({axes:n,keepDims:t.keepDims,noopWithEmptyAxes:t.noopWithEmptyAxes})},pn=(e,t,n,r)=>{let i=e.inputs,s=i.length===1?n:lu(i,n);e.compute(Wa(t,{hint:s.cacheKey,inputDependencies:["rank"]},[i[0]],s.noopWithEmptyAxes&&s.axes.length===0?tf:r,s.axes,i[0].dataType,s.keepDims,s.noopWithEmptyAxes),{inputs:[0]})},nf=(e,t)=>{cn(e.inputs),pn(e,"ReduceLogSum",t,(n,r)=>[`var value = ${r.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,"value = log(value);"])},rf=(e,t)=>{cn(e.inputs),pn(e,"ReduceL1",t,(n,r)=>[`var value = ${r.type.storage}(0);`,"",`value += abs(${n.getByIndices("input_indices")});`,""])},sf=(e,t)=>{cn(e.inputs),pn(e,"ReduceL2",t,(n,r)=>[`var t = ${r.type.value}(0); var value = ${r.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += (t * t);`,"value = sqrt(value);"])},af=(e,t)=>{cn(e.inputs),pn(e,"ReduceLogSumExp",t,(n,r)=>[`var value = ${r.type.storage}(0);`,"",`value += exp(${n.getByIndices("input_indices")});`,"value = log(value);"])},of=(e,t)=>{cn(e.inputs),pn(e,"ReduceMax",t,(n,r,i)=>{let s=[];for(let a=0;a=0||i.length===0)&&s.push(n.indicesSet("input_indices",a,0));return[`${s.join(` +`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = max(value, ${n.getByIndices("input_indices")});`,""]})},lf=(e,t)=>{cn(e.inputs),pn(e,"ReduceMean",t,(n,r,i)=>{let s=1;for(let a=0;a=0||i.length===0)&&(s*=e.inputs[0].dims[a]);return["var sum = f32(0);","",`sum += f32(${n.getByIndices("input_indices")});`,`let value = ${r.type.value}(sum / ${s});`]})},uf=(e,t)=>{cn(e.inputs),pn(e,"ReduceMin",t,(n,r,i)=>{let s=[];for(let a=0;a=0||i.length===0)&&s.push(`input_indices[${a}] = 0;`);return[`${s.join(` +`)}`,`var value = ${n.getByIndices("input_indices")};`,`value = min(value, ${n.getByIndices("input_indices")});`,""]})},df=(e,t)=>{cn(e.inputs),pn(e,"ReduceProd",t,(n,r)=>[`var value = ${r.type.storage}(1);`,"",`value *= ${n.getByIndices("input_indices")};`,""])},cf=(e,t)=>{cn(e.inputs),pn(e,"ReduceSum",t,(n,r)=>[`var value = ${r.type.storage}(0);`,"",`value += ${n.getByIndices("input_indices")};`,""])},pf=(e,t)=>{cn(e.inputs),pn(e,"ReduceSumSquare",t,(n,r)=>[`var t = ${r.type.value}(0); var value = ${r.type.value}(0);`,"",`t = ${n.getByIndices("input_indices")}; value += t * t;`,""])},fn=(e,t,n)=>{if(t.length===0)return n;let r=1,i=1;for(let s=0;s1024},fy=(e,t)=>{fn(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?lf(e,t):ry(e,t)},hy=(e,t)=>{fn(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?rf(e,t):iy(e,t)},my=(e,t)=>{fn(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?sf(e,t):sy(e,t)},gy=(e,t)=>{fn(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?af(e,t):ay(e,t)},yy=(e,t)=>{fn(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?of(e,t):oy(e,t)},by=(e,t)=>{fn(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?uf(e,t):ly(e,t)},wy=(e,t)=>{fn(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?df(e,t):uy(e,t)},_y=(e,t)=>{fn(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?cf(e,t):dy(e,t)},vy=(e,t)=>{fn(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?pf(e,t):cy(e,t)},xy=(e,t)=>{fn(e.inputs[0].dims,t.axes,t.noopWithEmptyAxes)?nf(e,t):py(e,t)}}),ll,$y,Sy,uu,v$=re(()=>{Te(),pt(),Qu(),ll=e=>{if(!e||e.length===0||e.length>2)throw new Error("ArgMinMaxOp op requires 1 or 2 inputs.");if(e[0].dataType!==1)throw new Error("Invalid input type.")},$y=(e,t)=>{ll(e.inputs);let n=(r,i,s)=>{let a=[];for(let o=0;o=0||s.length===0)&&a.push(`input_indices[${o}] = 0;`);return[`${a.join(` +`)}`,`var value = ${r.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${r.getByIndices("input_indices")} ${t.selectLastIndex>0?"<=":"<"} value) { + value = ${r.getByIndices("input_indices")}; + best_index = i32(last_index); + }`,"",i.setByOffset("global_idx","best_index")]};e.compute(Wa("ArgMin",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},Sy=(e,t)=>{ll(e.inputs);let n=(r,i,s)=>{let a=[];for(let o=0;o=0||s.length===0)&&a.push(`input_indices[${o}] = 0;`);return[`${a.join(` +`)}`,`var value = ${r.getByIndices("input_indices")}; +var best_index : i32 = 0;`,`if (${r.getByIndices("input_indices")} ${t.selectLastIndex>0?">=":">"} value) { + value = ${r.getByIndices("input_indices")}; + best_index = i32(last_index); + }`,"",i.setByOffset("global_idx","best_index")]};e.compute(Wa("argMax",{hint:t.cacheKey,inputDependencies:["rank"]},[e.inputs[0]],n,[t.axis],7,t.keepDims),{inputs:[0]})},uu=e=>Fe(e)}),ff,pa,hf,mf,gf,Ms,yf,Ty,Ju=re(()=>{Te(),Ce(),Yu(),Ee(),ff=(e,t)=>{let n=e[0],r=e[1],i=e[2],s=e[3],a=e[4],o=e[5];if(a&&o)throw new Error("Attention cannot have both past and attention_bias");if(n.dims.length!==3)throw new Error('Input "input" must have 3 dimensions');let l=n.dims[0],d=n.dims[1],p=n.dims[2];if(i.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimensions');if(r.dims.length!==2)throw new Error('Input "weights" is expected to have 2 dimensions');if(r.dims[0]!==p)throw new Error("Input 1 dimension 0 should have same length as dimension 2 of input 0");if(i.dims[0]!==r.dims[1])throw new Error('Input "bias" dimension 0 should have same length as dimension 1 of input "weights"');let f=i.dims[0]/3,c=f,m=c;if(t.qkvHiddenSizes.length>0){if(t.qkvHiddenSizes.length!==3)throw new Error("qkv_hidden_sizes attribute should have 3 elements");for(let I of t.qkvHiddenSizes)if(I%t.numHeads!==0)throw new Error("qkv_hidden_sizes should be divisible by num_heads");f=t.qkvHiddenSizes[0],c=t.qkvHiddenSizes[1],m=t.qkvHiddenSizes[2]}let y=d;if(f!==c)throw new Error("qkv_hidden_sizes first element should be same as the second");if(i.dims[0]!==f+c+m)throw new Error('Input "bias" dimension 0 should have same length as sum of Q/K/V hidden sizes');let w=0;if(a){if(c!==m)throw new Error('Input "past" expect k_hidden_size == v_hidden_size');if(a.dims.length!==5)throw new Error('Input "past" must have 5 dimensions');if(a.dims[0]!==2)throw new Error('Input "past" first dimension must be 2');if(a.dims[1]!==l)throw new Error('Input "past" second dimension must be batch_size');if(a.dims[2]!==t.numHeads)throw new Error('Input "past" third dimension must be num_heads');if(a.dims[4]!==c/t.numHeads)throw new Error('Input "past" fifth dimension must be k_hidden_size / num_heads');t.pastPresentShareBuffer||(w=a.dims[3])}let $=y+w,_=-1,v=0;if(s)throw new Error("Mask not supported");if(a)throw new Error("past is not supported");if(o){if(o.dims.length!==4)throw new Error('Input "attention_bias" must have 4 dimensions');if(o.dims[0]!==l||o.dims[1]!==t.numHeads||o.dims[2]!==d||o.dims[3]!==$)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:l,sequenceLength:d,pastSequenceLength:w,kvSequenceLength:y,totalSequenceLength:$,maxSequenceLength:_,inputHiddenSize:p,hiddenSize:f,vHiddenSize:m,headSize:Math.floor(f/t.numHeads),vHeadSize:Math.floor(m/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:v,scale:t.scale,broadcastResPosBias:!1,passPastInKv:!1,qkvFormat:1}},pa=(e,t,n)=>t&&e?` + let total_sequence_length_input = u32(${t.getByOffset("0")}); + let present_sequence_length = max(total_sequence_length_input, uniforms.past_sequence_length); + let is_subsequent_prompt: bool = sequence_length > 1 && sequence_length != total_sequence_length_input; + let is_first_prompt: bool = is_subsequent_prompt == false && sequence_length == total_sequence_length_input; + total_sequence_length = u32(${e==null?void 0:e.getByOffset("batchIdx")}) + 1; + var past_sequence_length: u32 = 0; + if (is_first_prompt == false) { + past_sequence_length = total_sequence_length - sequence_length; + } + `:` + ${n?"let past_sequence_length = uniforms.past_sequence_length":""}; + let present_sequence_length = total_sequence_length; + `,hf=(e,t,n,r,i,s,a,o)=>{let l=ct(a?1:s),d=64,p=s/l;p{let v=be("x",e.dataType,e.dims,l),I=[v],S=a?V("seq_lens",a.dataType,a.dims):void 0;S&&I.push(S);let E=o?V("total_sequence_length_input",o.dataType,o.dims):void 0;E&&I.push(E);let R=Rt(e.dataType),O=[{name:"batch_size",type:"u32"},{name:"num_heads",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"sequence_length",type:"u32"},{name:"total_sequence_length",type:"u32"},{name:"elements_per_thread",type:"u32"}];return` + var thread_max: array; + var thread_sum: array; + ${_.registerUniforms(O).declareVariables(...I)} + ${_.mainStart([d,1,1])} + let batchIdx = workgroup_id.z / uniforms.num_heads; + let headIdx = workgroup_id.z % uniforms.num_heads; + let sequence_length = uniforms.sequence_length; + var total_sequence_length = uniforms.total_sequence_length; + ${pa(S,E,!1)} + let local_offset = local_idx * uniforms.elements_per_thread; + let offset = (global_idx / ${d}) * uniforms.total_sequence_length + local_offset; + let seq_causal_length = ${a?"u32(past_sequence_length + workgroup_id.y + 1)":"total_sequence_length"}; + var thread_max_vector = ${y}(-3.4028234663852886e+38f); + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + thread_max_vector = max(${y}(x[offset + i]), thread_max_vector); + } + thread_max[local_idx] = ${(()=>{switch(l){case 1:return"thread_max_vector";case 2:return"max(thread_max_vector.x, thread_max_vector.y)";case 4:return"max(max(thread_max_vector.x, thread_max_vector.y), max(thread_max_vector.z, thread_max_vector.w))";default:throw new Error(`Unsupported components: ${l}`)}})()}; + workgroupBarrier(); + + var max_value = f32(-3.4028234663852886e+38f); + for (var i = 0u; i < ${d}; i++) { + max_value = max(thread_max[i], max_value); + } + + var sum_vector = ${y}(0); + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + sum_vector += exp(${y}(x[offset + i]) - max_value); + } + thread_sum[local_idx] = ${(()=>{switch(l){case 1:return"sum_vector";case 2:return"sum_vector.x + sum_vector.y";case 4:return"sum_vector.x + sum_vector.y + sum_vector.z + sum_vector.w";default:throw new Error(`Unsupported components: ${l}`)}})()}; + workgroupBarrier(); + + var sum: f32 = 0; + for (var i = 0u; i < ${d}; i++) { + sum += thread_sum[i]; + } + + if (sum == 0) { + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + x[offset + i] = ${v.type.value}(${R}(1.0) / ${R}(seq_causal_length)); + } + } else { + for (var i: u32 = 0; i < uniforms.elements_per_thread && i + local_offset < seq_causal_length; i++) { + var f32input = ${y}(x[offset + i]); + x[offset + i] = ${v.type.value}(exp(f32input - max_value) / sum); + } + } + ${a?` + for (var total_seq_id: u32 = seq_causal_length; total_seq_id + local_offset < uniforms.total_sequence_length; total_seq_id++) { + x[offset + total_seq_id] = ${v.type.value}(${R}(0)); + }`:""}; + }`};return{name:"AttentionProbsSoftmax",shaderCache:{hint:`${d};${m};${l}`,inputDependencies:w},getShaderSource:$,getRunData:()=>({outputs:[],dispatchGroup:{x:1,y:i,z:t*n},programUniforms:c})}},mf=(e,t,n,r,i,s,a,o,l)=>{let d=a+s.kvSequenceLength,p=[s.batchSize,s.numHeads,s.sequenceLength,d],f=e>1&&r,c=s.kvNumHeads?s.kvNumHeads:s.numHeads,m=f?[s.batchSize,c,d,s.headSize]:void 0,y=s.nReps?s.nReps:1,w=s.scale===0?1/Math.sqrt(s.headSize):s.scale,$=ct(s.headSize),_=s.headSize/$,v=12,I={x:Math.ceil(d/v),y:Math.ceil(s.sequenceLength/v),z:s.batchSize*s.numHeads},S=[{type:12,data:s.sequenceLength},{type:12,data:_},{type:12,data:d},{type:12,data:s.numHeads},{type:12,data:s.headSize},{type:1,data:w},{type:12,data:a},{type:12,data:s.kvSequenceLength},{type:12,data:y}],E=f&&r&&W.size(r.dims)>0,R=["type","type"];E&&R.push("type"),i&&R.push("type"),o&&R.push("type"),l&&R.push("type");let O=[{dims:p,dataType:t.dataType,gpuDataType:0}];f&&O.push({dims:m,dataType:t.dataType,gpuDataType:0});let x=U=>{let q=V("q",t.dataType,t.dims,$),J=V("key",n.dataType,n.dims,$),se=[q,J];if(E){let Q=V("past_key",r.dataType,r.dims,$);se.push(Q)}i&&se.push(V("attention_bias",i.dataType,i.dims));let ce=o?V("seq_lens",o.dataType,o.dims):void 0;ce&&se.push(ce);let L=l?V("total_sequence_length_input",l.dataType,l.dims):void 0;L&&se.push(L);let ue=be("output",t.dataType,p),ie=[ue];f&&ie.push(be("present_key",t.dataType,m,$));let te=Rt(1,$),he=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"alpha",type:"f32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` + const TILE_SIZE = ${v}u; + + var tileQ: array<${q.type.storage}, ${v*v}>; + var tileK: array<${q.type.storage}, ${v*v}>; + ${U.registerUniforms(he).declareVariables(...se,...ie)} + ${U.mainStart([v,v,1])} + // x holds the N and y holds the M + let headIdx = workgroup_id.z % uniforms.num_heads; + let kvHeadIdx = ${y===1?"headIdx":"headIdx / uniforms.n_reps"}; + let kv_num_heads = ${y===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"}; + let batchIdx = workgroup_id.z / uniforms.num_heads; + let m = workgroup_id.y * TILE_SIZE; + let n = workgroup_id.x * TILE_SIZE; + let sequence_length = uniforms.M; + var total_sequence_length = uniforms.N; + ${pa(ce,L,!0)} + let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; + let qOffset = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; + ${E&&f?"let pastKeyOffset = absKvHeadIdx * uniforms.past_sequence_length * uniforms.K;":""}; + let kOffset = absKvHeadIdx * uniforms.kv_sequence_length * uniforms.K; + ${f?"let presentKeyOffset = absKvHeadIdx * uniforms.N * uniforms.K;":""} + var value = ${te}(0); + for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { + if (global_id.y < uniforms.M && w + local_id.x < uniforms.K) { + tileQ[TILE_SIZE * local_id.y + local_id.x] = q[qOffset + local_id.y * uniforms.K + w + local_id.x]; + } + if (n + local_id.y < uniforms.N && w + local_id.x < uniforms.K) { + var idx = TILE_SIZE * local_id.y + local_id.x; + ${E&&f?` + if (n + local_id.y < past_sequence_length) { + tileK[idx] = past_key[pastKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x]; + } else if (n + local_id.y - past_sequence_length < uniforms.kv_sequence_length) { + tileK[idx] = key[kOffset + (n + local_id.y - past_sequence_length) * uniforms.K + w + local_id.x]; + }`:` + if (n + local_id.y < uniforms.kv_sequence_length) { + tileK[idx] = key[kOffset + (n + local_id.y) * uniforms.K + w + local_id.x]; + }`} + ${f?`if (n + local_id.y < present_sequence_length) { + present_key[presentKeyOffset + (n + local_id.y) * uniforms.K + w + local_id.x] = tileK[idx]; + }`:""} + } + workgroupBarrier(); + + for (var k: u32 = 0u; k < TILE_SIZE && w+k < uniforms.K; k++) { + value += ${te}(tileQ[TILE_SIZE * local_id.y + k] * tileK[TILE_SIZE * local_id.x + k]); + } + + workgroupBarrier(); + } + + if (global_id.y < uniforms.M && global_id.x < total_sequence_length) { + let headOffset = workgroup_id.z * uniforms.M * uniforms.N; + let outputIdx = headOffset + global_id.y * uniforms.N + global_id.x; + var sum: f32 = ${(()=>{switch($){case 1:return"value";case 2:return"value.x + value.y";case 4:return"value.x + value.y + value.z + value.w";default:throw new Error(`Unsupported components: ${$}`)}})()}; + output[outputIdx] = ${ue.type.value} (sum * uniforms.alpha) + ${i?"attention_bias[outputIdx]":"0.0"}; + } + }`};return{name:"AttentionProbs",shaderCache:{hint:`${$};${i!==void 0};${r!==void 0};${e}`,inputDependencies:R},getRunData:()=>({outputs:O,dispatchGroup:I,programUniforms:S}),getShaderSource:x}},gf=(e,t,n,r,i,s,a=void 0,o=void 0)=>{let l=s+i.kvSequenceLength,d=i.nReps?i.nReps:1,p=i.vHiddenSize*d,f=e>1&&r,c=i.kvNumHeads?i.kvNumHeads:i.numHeads,m=f?[i.batchSize,c,l,i.headSize]:void 0,y=[i.batchSize,i.sequenceLength,p],w=12,$={x:Math.ceil(i.vHeadSize/w),y:Math.ceil(i.sequenceLength/w),z:i.batchSize*i.numHeads},_=[{type:12,data:i.sequenceLength},{type:12,data:l},{type:12,data:i.vHeadSize},{type:12,data:i.numHeads},{type:12,data:i.headSize},{type:12,data:p},{type:12,data:s},{type:12,data:i.kvSequenceLength},{type:12,data:d}],v=f&&r&&W.size(r.dims)>0,I=["type","type"];v&&I.push("type"),a&&I.push("type"),o&&I.push("type");let S=[{dims:y,dataType:t.dataType,gpuDataType:0}];f&&S.push({dims:m,dataType:t.dataType,gpuDataType:0});let E=R=>{let O=V("probs",t.dataType,t.dims),x=V("v",n.dataType,n.dims),U=[O,x];v&&U.push(V("past_value",r.dataType,r.dims));let q=a?V("seq_lens",a.dataType,a.dims):void 0;a&&U.push(q);let J=o?V("total_sequence_length_input",o.dataType,o.dims):void 0;o&&U.push(J);let se=[be("output",t.dataType,y)];f&&se.push(be("present_value",t.dataType,m));let ce=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"v_hidden_size",type:"u32"},{name:"past_sequence_length",type:"u32"},{name:"kv_sequence_length",type:"u32"},{name:"n_reps",type:"u32"}];return` + const TILE_SIZE = ${w}u; + var tileQ: array<${O.type.value}, ${w*w}>; + var tileV: array<${O.type.value}, ${w*w}>; + ${R.registerUniforms(ce).declareVariables(...U,...se)} + ${R.mainStart([w,w,1])} + let headIdx = workgroup_id.z % uniforms.num_heads; + let batchIdx = workgroup_id.z / uniforms.num_heads; + let kvHeadIdx = ${d===1?"headIdx":"headIdx / uniforms.n_reps"}; + let kv_num_heads = ${d===1?"uniforms.num_heads":"uniforms.num_heads / uniforms.n_reps"}; + let m = global_id.y; + let n = global_id.x; + let sequence_length = uniforms.M; + var total_sequence_length = uniforms.K; + ${pa(q,J,!0)} + let offsetA = workgroup_id.z * uniforms.M * uniforms.K + m * uniforms.K; + let absKvHeadIdx = batchIdx * kv_num_heads + kvHeadIdx; // kvHeadIdx is relative to the batch + ${v&&f?"let pastValueOffset = absKvHeadIdx * uniforms.N * uniforms.past_sequence_length + n;":""}; + let vOffset = absKvHeadIdx * uniforms.N * uniforms.kv_sequence_length + n; + ${f?"let presentValueOffset = absKvHeadIdx * uniforms.N * uniforms.K + n;":""} + var value = ${O.type.storage}(0); + for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { + if (m < uniforms.M && w + local_id.x < uniforms.K) { + tileQ[TILE_SIZE * local_id.y + local_id.x] = probs[offsetA + w + local_id.x]; + } + if (n < uniforms.N && w + local_id.y < uniforms.K) { + var idx = TILE_SIZE * local_id.y + local_id.x; + ${v&&f?` + if (w + local_id.y < past_sequence_length) { + tileV[idx] = past_value[pastValueOffset + (w + local_id.y) * uniforms.N]; + } else if (w + local_id.y - past_sequence_length < uniforms.kv_sequence_length) { + tileV[idx] = v[vOffset + (w + local_id.y - past_sequence_length) * uniforms.N]; + } + `:` + if (w + local_id.y < uniforms.kv_sequence_length) { + tileV[idx] = v[vOffset + (w + local_id.y) * uniforms.N]; + }`} + ${f?` + if (w + local_id.y < present_sequence_length) { + present_value[presentValueOffset + (w + local_id.y) * uniforms.N] = tileV[idx]; + }`:""} + } + workgroupBarrier(); + for (var k: u32 = 0u; k < TILE_SIZE && w+k < total_sequence_length; k++) { + value += tileQ[TILE_SIZE * local_id.y + k] * tileV[TILE_SIZE * k + local_id.x]; + } + workgroupBarrier(); + } + + // we need to transpose output from BNSH_v to BSND_v + if (m < uniforms.M && n < uniforms.N) { + let outputIdx = batchIdx * uniforms.M * uniforms.v_hidden_size + m * uniforms.v_hidden_size + + headIdx * uniforms.N + n; + output[outputIdx] = value; + } + }`};return{name:"AttentionScore",shaderCache:{hint:`${r!==void 0};${e}`,inputDependencies:I},getRunData:()=>({outputs:S,dispatchGroup:$,programUniforms:_}),getShaderSource:E}},Ms=(e,t,n,r,i,s,a,o,l,d,p=void 0,f=void 0)=>{let c=Math.min(e.outputCount,1+(a?1:0)+(o?1:0)),m=c>1?d.pastSequenceLength:0,y=m+d.kvSequenceLength,w=l&&W.size(l.dims)>0?l:void 0,$=[t,n];c>1&&a&&W.size(a.dims)>0&&$.push(a),w&&$.push(w),p&&$.push(p),f&&$.push(f);let _=e.compute(mf(c,t,n,a,w,d,m,p,f),{inputs:$,outputs:c>1?[-1,1]:[-1]})[0];e.compute(hf(_,d.batchSize,d.numHeads,m,d.sequenceLength,y,p,f),{inputs:p&&f?[_,p,f]:[_],outputs:[]});let v=[_,r];c>1&&o&&W.size(o.dims)>0&&v.push(o),p&&v.push(p),f&&v.push(f),e.compute(gf(c,_,r,o,d,m,p,f),{inputs:v,outputs:c>1?[0,2]:[0]})},yf=(e,t)=>{let n=[t.batchSize,t.numHeads,t.sequenceLength,t.headSize],r=t.sequenceLength,i=t.inputHiddenSize,s=t.headSize,a=12,o={x:Math.ceil(t.headSize/a),y:Math.ceil(t.sequenceLength/a),z:t.batchSize*t.numHeads},l=[e.inputs[0],e.inputs[1],e.inputs[2]],d=[{type:12,data:r},{type:12,data:i},{type:12,data:s},{type:12,data:t.numHeads},{type:12,data:t.headSize},{type:12,data:t.hiddenSize},{type:12,data:t.hiddenSize+t.hiddenSize+t.vHiddenSize}],p=f=>{let c=be("output_q",l[0].dataType,n),m=be("output_k",l[0].dataType,n),y=be("output_v",l[0].dataType,n),w=V("input",l[0].dataType,l[0].dims),$=V("weight",l[1].dataType,l[1].dims),_=V("bias",l[2].dataType,l[2].dims),v=w.type.storage,I=[{name:"M",type:"u32"},{name:"K",type:"u32"},{name:"N",type:"u32"},{name:"num_heads",type:"u32"},{name:"head_size",type:"u32"},{name:"hidden_size",type:"u32"},{name:"ldb",type:"u32"}];return` + const TILE_SIZE = ${a}u; + var tileInput: array<${v}, ${a*a}>; + var tileWeightQ: array<${v}, ${a*a}>; + var tileWeightK: array<${v}, ${a*a}>; + var tileWeightV: array<${v}, ${a*a}>; + ${f.registerUniforms(I).declareVariables(w,$,_,c,m,y)} + ${f.mainStart([a,a,1])} + let batchIndex = workgroup_id.z / uniforms.num_heads; + let headNumber = workgroup_id.z % uniforms.num_heads; + let m = global_id.y; + let n = global_id.x; + + let inputOffset = batchIndex * (uniforms.M * uniforms.K) + m * uniforms.K; + let biasOffsetQ = headNumber * uniforms.head_size; + let biasOffsetK = uniforms.hidden_size + biasOffsetQ; + let biasOffsetV = uniforms.hidden_size + biasOffsetK; + + var valueQ = ${v}(0); + var valueK = ${v}(0); + var valueV = ${v}(0); + for (var w: u32 = 0u; w < uniforms.K; w += TILE_SIZE) { + if (m < uniforms.M && w + local_id.x < uniforms.K) { + tileInput[TILE_SIZE * local_id.y + local_id.x] = input[inputOffset + w + local_id.x]; + } + if (n < uniforms.N && w + local_id.y < uniforms.K) { + let offset = n + (w + local_id.y) * uniforms.ldb; + tileWeightQ[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetQ + offset]; + tileWeightK[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetK + offset]; + tileWeightV[TILE_SIZE * local_id.y + local_id.x] = weight[biasOffsetV + offset]; + } + workgroupBarrier(); + for (var k: u32 = 0u; k({outputs:[{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0},{dims:n,dataType:e.inputs[0].dataType,gpuDataType:0}],dispatchGroup:o,programUniforms:d}),getShaderSource:p},{inputs:l,outputs:[-1,-1,-1]})},Ty=(e,t)=>{let n=ff(e.inputs,t),[r,i,s]=yf(e,n);return Ms(e,r,i,s,e.inputs[4],void 0,void 0,void 0,e.inputs[5],n)}}),bf,wf,_f,Iy,x$=re(()=>{on(),Te(),Ce(),pt(),Ee(),bf=(e,t)=>{if(!e||e.length!==5)throw new Error("BatchNormalization requires 5 inputs");let n=(r,i,s)=>{let a=i.length;if(a!==r.length)throw new Error(`${s}: num dimensions != ${a}`);i.forEach((o,l)=>{if(o!==r[l])throw new Error(`${s}: dim[${l}] do not match`)})};if(e[0].dims.length>1){let r=t.format==="NHWC"?t.spatial?e[0].dims.slice(-1):e[0].dims.slice(-1).concat(e[0].dims.slice(1,e[0].dims.length-1)):e[0].dims.slice(1,t.spatial?2:void 0);n(e[1].dims,r,"Invalid input scale"),n(e[2].dims,r,"Invalid input B"),n(e[3].dims,r,"Invalid input mean"),n(e[4].dims,r,"Invalid input var")}else n(e[1].dims,[1],"Invalid input scale"),n(e[2].dims,[1],"Invalid input B"),n(e[3].dims,[1],"Invalid input mean"),n(e[4].dims,[1],"Invalid input var")},wf=(e,t)=>{let{epsilon:n,spatial:r,format:i}=t,s=e[0].dims,a=r?ct(s[s.length-1]):1,o=i==="NHWC"&&s.length>1?a:1,l=W.size(s)/a,d=r,p=d?s.length:s,f=V("x",e[0].dataType,e[0].dims,a),c=V("scale",e[1].dataType,e[1].dims,o),m=V("bias",e[2].dataType,e[2].dims,o),y=V("inputMean",e[3].dataType,e[3].dims,o),w=V("inputVar",e[4].dataType,e[4].dims,o),$=be("y",e[0].dataType,p,a),_=()=>{let I="";if(r)I=`let cOffset = ${s.length===1?"0u":i==="NHWC"?`outputIndices[${s.length-1}] / ${a}`:"outputIndices[1]"};`;else if(i==="NCHW")I=` + ${$.indicesSet("outputIndices","0","0")} + let cOffset = ${$.indicesToOffset("outputIndices")};`;else{I=`var cIndices = ${c.type.indices}(0); + cIndices[0] = outputIndices[${s.length-1}];`;for(let S=1;S` + const epsilon = ${n}; + ${I.registerUniform("outputSize","u32").declareVariables(f,c,m,y,w,$)} + ${I.mainStart()} + ${I.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${$.offsetToIndices(`global_idx * ${a}`)}; + ${_()} + let scale = ${c.getByOffset("cOffset")}; + let bias = ${m.getByOffset("cOffset")}; + let inputMean = ${y.getByOffset("cOffset")}; + let inputVar = ${w.getByOffset("cOffset")}; + let x = ${f.getByOffset("global_idx")}; + let value = (x - inputMean) * inverseSqrt(inputVar + epsilon) * scale + bias; + ${$.setByOffset("global_idx","value")} + }`;return{name:"BatchNormalization",shaderCache:{hint:`${t.epsilon}_${t.format}_${r}_${a}`,inputDependencies:d?["rank","type","type","type","type"]:void 0},getShaderSource:v,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:d?[{type:12,data:l},..._e(s)]:[{type:12,data:l}]})}},_f=e=>Fe(e),Iy=(e,t)=>{let{inputs:n,outputCount:r}=e,i=_f({...t,outputCount:r});if(Be.webgpu.validateInputContent&&bf(n,i),t.trainingMode)throw new Error("BatchNormalization trainingMode is not supported yet.");e.compute(wf(n,i))}}),vf,xf,Cy,$$=re(()=>{Ce(),Ee(),vf=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![320,640,1280].includes(e[0].dims[2]))throw new Error("number of channels should be 320, 640 or 1280");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},xf=e=>{let t=e[0].dims,n=e[0].dims[2],r=W.size(t)/4,i=e[0].dataType,s=V("input",i,t,4),a=V("bias",i,[n],4),o=V("residual",i,t,4),l=be("output",i,t,4);return{name:"BiasAdd",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(r/64)}}),getShaderSource:d=>` + const channels = ${n}u / 4; + ${d.declareVariables(s,a,o,l)} + + ${d.mainStart()} + ${d.guardAgainstOutOfBoundsWorkgroupSizes(r)} + let value = ${s.getByOffset("global_idx")} + + ${a.getByOffset("global_idx % channels")} + ${o.getByOffset("global_idx")}; + ${l.setByOffset("global_idx","value")} + }`}},Cy=e=>{vf(e.inputs),e.compute(xf(e.inputs))}}),$f,Ve,Ey,ky,zy,Ay,My,Ry,Oy,Ny,By,Sf,Dy,Py,Uy,Ly,fs,Wy,ka,qy,Gy,Vy,Fy,Hy,jy,Ky,Xy,Yy,Zy,Qy,Jy,eb,tb,nb,rb,ul,ib,du,cu,sb,ab,ob,Tf,If,lb,ed=re(()=>{Te(),Ce(),pt(),Ee(),$f=(e,t,n,r,i,s,a)=>{let o=Math.ceil(t/4),l="";typeof i=="string"?l=`${i}(a)`:l=i("a");let d=V("inputData",n,[o],4),p=be("outputData",r,[o],4),f=[{name:"vec_size",type:"u32"}];return a&&f.push(...a),` + ${e.registerUniforms(f).declareVariables(d,p)} + + ${s??""} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + + let a = ${d.getByOffset("global_idx")}; + ${p.setByOffset("global_idx",l)} + }`},Ve=(e,t,n,r,i,s=e.dataType,a,o)=>{let l=[{type:12,data:Math.ceil(W.size(e.dims)/4)}];return a&&l.push(...a),{name:t,shaderCache:{hint:i,inputDependencies:["type"]},getShaderSource:d=>$f(d,W.size(e.dims),e.dataType,s,n,r,o),getRunData:d=>({outputs:[{dims:e.dims,dataType:s}],dispatchGroup:{x:Math.ceil(W.size(d[0].dims)/64/4)},programUniforms:l})}},Ey=e=>{e.compute(Ve(e.inputs[0],"Abs","abs"))},ky=e=>{e.compute(Ve(e.inputs[0],"Acos","acos"))},zy=e=>{e.compute(Ve(e.inputs[0],"Acosh","acosh"))},Ay=e=>{e.compute(Ve(e.inputs[0],"Asin","asin"))},My=e=>{e.compute(Ve(e.inputs[0],"Asinh","asinh"))},Ry=e=>{e.compute(Ve(e.inputs[0],"Atan","atan"))},Oy=e=>{e.compute(Ve(e.inputs[0],"Atanh","atanh"))},Ny=e=>Fe(e),By=(e,t)=>{let n;switch(t.to){case 10:n="vec4";break;case 1:n="vec4";break;case 12:n="vec4";break;case 6:n="vec4";break;case 9:n="vec4";break;default:throw new RangeError(`not supported type (specified in attribute 'to' from 'Cast' operator): ${t.to}`)}e.compute(Ve(e.inputs[0],"Cast",n,void 0,t.cacheKey,t.to))},Sf=e=>{let t,n,r=e.length>=2&&e[1].data!==0,i=e.length>=3&&e[2].data!==0;switch(e[0].dataType){case 1:t=r?e[1].getFloat32Array()[0]:-34028234663852886e22,n=i?e[2].getFloat32Array()[0]:34028234663852886e22;break;case 10:t=r?e[1].getUint16Array()[0]:64511,n=i?e[2].getUint16Array()[0]:31743;break;default:throw new Error("Unsupport data type")}return Fe({min:t,max:n})},Dy=(e,t)=>{let n=t||Sf(e.inputs),r=Rt(e.inputs[0].dataType);e.compute(Ve(e.inputs[0],"Clip",i=>`clamp(${i}, vec4<${r}>(uniforms.min), vec4<${r}>(uniforms.max))`,void 0,n.cacheKey,void 0,[{type:e.inputs[0].dataType,data:n.min},{type:e.inputs[0].dataType,data:n.max}],[{name:"min",type:r},{name:"max",type:r}]),{inputs:[0]})},Py=e=>{e.compute(Ve(e.inputs[0],"Ceil","ceil"))},Uy=e=>{e.compute(Ve(e.inputs[0],"Cos","cos"))},Ly=e=>{e.compute(Ve(e.inputs[0],"Cosh","cosh"))},fs=e=>Fe(e),Wy=(e,t)=>{let n=Rt(e.inputs[0].dataType);e.compute(Ve(e.inputs[0],"Elu",r=>`elu_vf32(${r})`,` + const elu_alpha_ = ${n}(${t.alpha}); + + fn elu_f32(a: ${n}) -> ${n} { + return select((exp(a) - 1.0) * elu_alpha_, a, a >= 0.0); + } + + fn elu_vf32(v: vec4<${n}>) -> vec4<${n}> { + return vec4(elu_f32(v.x), elu_f32(v.y), elu_f32(v.z), elu_f32(v.w)); + }`,t.cacheKey))},ka=(e="f32")=>` +const r0: ${e} = 0.3275911; +const r1: ${e} = 0.254829592; +const r2: ${e} = -0.284496736; +const r3: ${e} = 1.421413741; +const r4: ${e} = -1.453152027; +const r5: ${e} = 1.061405429; + +fn erf_vf32(v: vec4<${e}>) -> vec4<${e}> { + let absv = abs(v); + let x = 1.0 / (1.0 + r0 * absv); + return sign(v) * (1.0 - ((((r5 * x + r4) * x + r3) * x + r2) * x + r1) * x * exp(-absv * absv)); +}`,qy=e=>{let t=Rt(e.inputs[0].dataType);e.compute(Ve(e.inputs[0],"Erf",n=>`erf_vf32(${n})`,ka(t)))},Gy=e=>{e.compute(Ve(e.inputs[0],"Exp","exp"))},Vy=e=>{e.compute(Ve(e.inputs[0],"Floor","floor"))},Fy=e=>{let t=Rt(e.inputs[0].dataType);e.compute(Ve(e.inputs[0],"Gelu",n=>`0.5 * ${n} * (1.0 + erf_vf32(${n} * 0.7071067811865475))`,ka(t)))},Hy=(e,t)=>{let n=Rt(e.inputs[0].dataType);e.compute(Ve(e.inputs[0],"LeakyRelu",r=>`select(leaky_relu_alpha_ * ${r}, ${r}, ${r} >= vec4<${n}>(0.0))`,`const leaky_relu_alpha_ = ${n}(${t.alpha});`,t.cacheKey))},jy=e=>{e.compute(Ve(e.inputs[0],"Not",t=>`!${t}`))},Ky=e=>{e.compute(Ve(e.inputs[0],"Neg",t=>`-${t}`))},Xy=e=>{e.compute(Ve(e.inputs[0],"Reciprocal",t=>`1.0/${t}`))},Yy=e=>{let t=Rt(e.inputs[0].dataType);e.compute(Ve(e.inputs[0],"Relu",n=>`select(vec4<${t}>(0.0), ${n}, ${n} > vec4<${t}>(0.0))`))},Zy=e=>{e.compute(Ve(e.inputs[0],"Sigmoid",t=>`(1.0 / (1.0 + exp(-${t})))`))},Qy=e=>Fe(e),Jy=(e,t)=>{let n=Rt(e.inputs[0].dataType);e.compute(Ve(e.inputs[0],"HardSigmoid",r=>`max(vec4<${n}>(0.0), min(vec4<${n}>(1.0), ${t.alpha} * ${r} + vec4<${n}>(${t.beta})))`,void 0,t.cacheKey))},eb=e=>{e.compute(Ve(e.inputs[0],"Sin","sin"))},tb=e=>{e.compute(Ve(e.inputs[0],"Sinh","sinh"))},nb=e=>{e.compute(Ve(e.inputs[0],"Sqrt","sqrt"))},rb=e=>{e.compute(Ve(e.inputs[0],"Tan","tan"))},ul=e=>`sign(${e}) * (1 - exp(-2 * abs(${e}))) / (1 + exp(-2 * abs(${e})))`,ib=e=>{e.compute(Ve(e.inputs[0],"Tanh",ul))},du=(e="f32")=>` +const fast_gelu_a: ${e} = 0.5; +const fast_gelu_b: ${e} = 0.7978845608028654; +const fast_gelu_c: ${e} = 0.035677408136300125; + +fn tanh_v(v: vec4<${e}>) -> vec4<${e}> { + return ${ul("v")}; +} +`,cu=e=>`(fast_gelu_a + fast_gelu_a * tanh_v(${e} * (fast_gelu_c * ${e} * ${e} + fast_gelu_b))) * ${e}`,sb=e=>{let t=Rt(e.inputs[0].dataType);e.compute(Ve(e.inputs[0],"FastGelu",cu,du(t),void 0,e.inputs[0].dataType))},ab=(e,t)=>{let n=Rt(e.inputs[0].dataType);return e.compute(Ve(e.inputs[0],"ThresholdedRelu",r=>`select(vec4<${n}>(0.0), ${r}, ${r} > thresholded_relu_alpha_)`,`const thresholded_relu_alpha_ = vec4<${n}>(${t.alpha});`,t.cacheKey)),0},ob=e=>{e.compute(Ve(e.inputs[0],"Log","log"))},Tf=(e,t)=>` +const alpha = vec4<${e}>(${t}); +const one = ${e}(1.0); +const zero = ${e}(0.0); + +fn quick_gelu_impl(x: vec4<${e}>) -> vec4<${e}> { + let v = x *alpha; + var x1 : vec4<${e}>; + for (var i = 0; i < 4; i = i + 1) { + if (v[i] >= zero) { + x1[i] = one / (one + exp(-v[i])); + } else { + x1[i] = one - one / (one + exp(v[i])); + } + } + return x * x1; +} +`,If=e=>`quick_gelu_impl(${e})`,lb=(e,t)=>{let n=Rt(e.inputs[0].dataType);e.compute(Ve(e.inputs[0],"QuickGelu",If,Tf(n,t.alpha),t.cacheKey,e.inputs[0].dataType))}}),Cf,Ef,ub,S$=re(()=>{Ce(),Ee(),ed(),Cf=e=>{if(e[0].dims.length!==3)throw new Error("input should have 3 dimensions");if(![2560,5120,10240].includes(e[0].dims[2]))throw new Error("hidden state should be 2560, 5120 or 10240");if(e[1].dims.length!==1)throw new Error("bias is expected to have 1 dimensions");if(e[0].dims[2]!==e[1].dims[0])throw new Error("last dimension of input and bias are not the same")},Ef=e=>{let t=e[0].dims.slice();t[2]=t[2]/2;let n=V("input",e[0].dataType,e[0].dims,4),r=V("bias",e[0].dataType,[e[0].dims[2]],4),i=be("output",e[0].dataType,t,4),s=W.size(t)/4,a=xt(e[0].dataType);return{name:"BiasSplitGelu",getRunData:()=>({outputs:[{dims:t,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)}}),getShaderSource:o=>` + const M_SQRT2 = sqrt(2.0); + const halfChannels = ${e[0].dims[2]/4/2}u; + + ${o.declareVariables(n,r,i)} + + ${ka(a)} + + ${o.mainStart()} + ${o.guardAgainstOutOfBoundsWorkgroupSizes(s)} + let biasIdx = global_idx % halfChannels; + let batchIndex = global_idx / halfChannels; + let inputOffset = biasIdx + batchIndex * halfChannels * 2; + let valueLeft = input[inputOffset] + bias[biasIdx]; + let valueRight = input[inputOffset + halfChannels] + bias[biasIdx + halfChannels]; + let geluRight = valueRight * 0.5 * (erf_vf32(valueRight / M_SQRT2) + 1); + + ${i.setByOffset("global_idx","valueLeft * geluRight")} + }`}},ub=e=>{Cf(e.inputs),e.compute(Ef(e.inputs))}}),kf,zf,hn,db,cb,pb,fb,hb,mb,gb,yb,bb,wb,T$=re(()=>{Te(),Ce(),Ee(),kf=(e,t,n,r,i,s,a,o,l,d,p,f)=>{let c,m;typeof o=="string"?c=m=(v,I)=>`${o}((${v}),(${I}))`:typeof o=="function"?c=m=o:(c=o.scalar,m=o.vector);let y=be("outputData",p,r.length,4),w=V("aData",l,t.length,4),$=V("bData",d,n.length,4),_;if(i)if(s){let v=W.size(t)===1,I=W.size(n)===1,S=t.length>0&&t[t.length-1]%4===0,E=n.length>0&&n[n.length-1]%4===0;v||I?_=y.setByOffset("global_idx",m(v?`${w.type.value}(${w.getByOffset("0")}.x)`:w.getByOffset("global_idx"),I?`${$.type.value}(${$.getByOffset("0")}.x)`:$.getByOffset("global_idx"))):_=` + let outputIndices = ${y.offsetToIndices("global_idx * 4u")}; + let offsetA = ${w.broadcastedIndicesToOffset("outputIndices",y)}; + let offsetB = ${$.broadcastedIndicesToOffset("outputIndices",y)}; + ${y.setByOffset("global_idx",m(a||S?w.getByOffset("offsetA / 4u"):`${w.type.value}(${w.getByOffset("offsetA / 4u")}[offsetA % 4u])`,a||E?$.getByOffset("offsetB / 4u"):`${$.type.value}(${$.getByOffset("offsetB / 4u")}[offsetB % 4u])`))} + `}else _=y.setByOffset("global_idx",m(w.getByOffset("global_idx"),$.getByOffset("global_idx")));else{if(!s)throw new Error("no necessary to use scalar implementation for element-wise binary op implementation.");let v=(I,S,E="")=>{let R=`aData[indexA${S}][componentA${S}]`,O=`bData[indexB${S}][componentB${S}]`;return` + let outputIndices${S} = ${y.offsetToIndices(`global_idx * 4u + ${S}u`)}; + let offsetA${S} = ${w.broadcastedIndicesToOffset(`outputIndices${S}`,y)}; + let offsetB${S} = ${$.broadcastedIndicesToOffset(`outputIndices${S}`,y)}; + let indexA${S} = offsetA${S} / 4u; + let indexB${S} = offsetB${S} / 4u; + let componentA${S} = offsetA${S} % 4u; + let componentB${S} = offsetB${S} % 4u; + ${I}[${S}] = ${E}(${c(R,O)}); + `};p===9?_=` + var data = vec4(0); + ${v("data",0,"u32")} + ${v("data",1,"u32")} + ${v("data",2,"u32")} + ${v("data",3,"u32")} + outputData[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:_=` + ${v("outputData[global_idx]",0)} + ${v("outputData[global_idx]",1)} + ${v("outputData[global_idx]",2)} + ${v("outputData[global_idx]",3)} + `}return` + ${e.registerUniform("vec_size","u32").declareVariables(w,$,y)} + + ${f??""} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${_} + }`},zf=(e,t,n,r,i,s,a=n.dataType)=>{let o=n.dims.map(Number),l=r.dims.map(Number),d=!W.areEqual(o,l),p=o,f=W.size(o),c=!1,m=!1,y=[d];if(d){let w=Ei.calcShape(o,l,!1);if(!w)throw new Error("Can't perform binary op on the given tensors");p=w.slice(),f=W.size(p);let $=W.size(o)===1,_=W.size(l)===1,v=o.length>0&&o[o.length-1]%4===0,I=l.length>0&&l[l.length-1]%4===0;y.push($),y.push(_),y.push(v),y.push(I);let S=1;for(let E=1;Ew.toString()).join("_"),inputDependencies:["rank","rank"]},getShaderSource:w=>kf(w,o,l,p,c,d,m,i,n.dataType,r.dataType,a,s),getRunData:()=>({outputs:[{dims:p,dataType:a}],dispatchGroup:{x:Math.ceil(f/64/4)},programUniforms:[{type:12,data:Math.ceil(W.size(p)/4)},..._e(o,l,p)]})}},hn=(e,t,n,r,i,s)=>{e.compute(zf(t,i??"",e.inputs[0],e.inputs[1],n,r,s))},db=e=>{hn(e,"Add",(t,n)=>`${t}+${n}`)},cb=e=>{hn(e,"Div",(t,n)=>`${t}/${n}`)},pb=e=>{hn(e,"Equal",{scalar:(t,n)=>`u32(${t}==${n})`,vector:(t,n)=>`vec4(${t}==${n})`},void 0,void 0,9)},fb=e=>{hn(e,"Mul",(t,n)=>`${t}*${n}`)},hb=e=>{let t=V("input",e.inputs[0].dataType,e.inputs[0].dims).type.value;hn(e,"Pow",{scalar:(n,r)=>`pow_custom(${n},${r})`,vector:(n,r)=>`pow_vector_custom(${n},${r})`},` + fn pow_custom(a : ${t}, b : ${t}) -> ${t} { + if (b == ${t}(0.0)) { + return ${t}(1.0); + } else if (a < ${t}(0.0) && f32(b) != floor(f32(b))) { + return ${t}(pow(f32(a), f32(b))); // NaN + } + return select(sign(a), ${t}(1.0), round(f32(abs(b) % ${t}(2.0))) != 1.0) * ${t}(${t==="i32"?"round":""}(pow(f32(abs(a)), f32(b)))); + } + fn pow_vector_custom(a : vec4<${t}>, b : vec4<${t}>) -> vec4<${t}> { + // TODO: implement vectorized pow + return vec4<${t}>(pow_custom(a.x, b.x), pow_custom(a.y, b.y), pow_custom(a.z, b.z), pow_custom(a.w, b.w)); + } + `)},mb=e=>{hn(e,"Sub",(t,n)=>`${t}-${n}`)},gb=e=>{hn(e,"Greater",{scalar:(t,n)=>`u32(${t}>${n})`,vector:(t,n)=>`vec4(${t}>${n})`},void 0,void 0,9)},yb=e=>{hn(e,"Less",{scalar:(t,n)=>`u32(${t}<${n})`,vector:(t,n)=>`vec4(${t}<${n})`},void 0,void 0,9)},bb=e=>{hn(e,"GreaterOrEqual",{scalar:(t,n)=>`u32(${t}>=${n})`,vector:(t,n)=>`vec4(${t}>=${n})`},void 0,void 0,9)},wb=e=>{hn(e,"LessOrEqual",{scalar:(t,n)=>`u32(${t}<=${n})`,vector:(t,n)=>`vec4(${t}<=${n})`},void 0,void 0,9)}}),Af,Mf,Rf,Of,_b,vb,I$=re(()=>{Te(),Ce(),pt(),Ee(),Af=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");let n=0,r=e[n],i=r.dataType,s=r.dims.length;e.forEach((a,o)=>{if(o!==n){if(a.dataType!==i)throw new Error("input tensors should be one type");if(a.dims.length!==s)throw new Error("input tensors should have the same shape");a.dims.forEach((l,d)=>{if(d!==t&&l!==r.dims[d])throw new Error("non concat dimensions must match")})}})},Mf=(e,t)=>` + fn calculateInputIndex(index: u32) -> u32 { + let sizeInConcatAxis = array(${t}); + for (var i: u32 = 0u; i < ${e}; i += 1u ) { + if (index < sizeInConcatAxis[i]) { + return i; + } + } + return ${e}u; + }`,Rf=(e,t)=>{let n=e.length,r=[];for(let i=0;i{let i=W.size(n),s=new Array(e.length),a=new Array(e.length),o=0,l=[],d=[],p=[{type:12,data:i}];for(let w=0;w`uniforms.sizeInConcatAxis${w}`).join(","),y=w=>` + + ${(()=>{w.registerUniform("outputSize","u32");for(let $=0;$(${m}); + ${c} -= sizeInConcatAxis[inputIndex - 1u]; + } + + ${Rf(a,f)} + }`;return{name:"Concat",shaderCache:{hint:`${t}`,inputDependencies:l},getRunData:()=>({outputs:[{dims:n,dataType:r}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:p}),getShaderSource:y}},_b=(e,t)=>{let n=e.inputs,r=n[0].dims,i=W.normalizeAxis(t.axis,r.length);Af(n,i);let s=r.slice();s[i]=n.reduce((o,l)=>o+(l.dims.length>i?l.dims[i]:0),0);let a=n.filter(o=>W.size(o.dims)>0);e.compute(Of(a,i,s,n[0].dataType),{inputs:a})},vb=e=>Fe({axis:e.axis})}),Yr,Zr,Qr,td,ti=re(()=>{Te(),Ce(),Yr=(e,t,n="f32")=>{switch(e.activation){case"Relu":return`value = max(value, ${t}(0.0));`;case"Sigmoid":return`value = (${t}(1.0) / (${t}(1.0) + exp(-value)));`;case"Clip":return`value = clamp(value, ${t}(${n}(uniforms.clip_min)), ${t}(${n}(uniforms.clip_max)));`;case"HardSigmoid":return`value = max(${t}(0.0), min(${t}(1.0), ${n}(uniforms.alpha) * value + ${n}(uniforms.beta)));`;case"LeakyRelu":return`value = select(${n}(uniforms.alpha) * value, value, value >= ${t}(0.0));`;case"Tanh":return`let e2x = exp(-2.0 * abs(value)); + value = sign(value) * (1.0 - e2x) / (1.0 + e2x); + `;case"":return"";default:throw new Error(`Unsupported activation ${e.activation}`)}},Zr=(e,t)=>{e.activation==="Clip"?t.push({type:1,data:e.clipMax},{type:1,data:e.clipMin}):e.activation==="HardSigmoid"?t.push({type:1,data:e.alpha},{type:1,data:e.beta}):e.activation==="LeakyRelu"&&t.push({type:1,data:e.alpha})},Qr=(e,t)=>{e.activation==="Clip"?t.push({name:"clip_max",type:"f32"},{name:"clip_min",type:"f32"}):e.activation==="HardSigmoid"?t.push({name:"alpha",type:"f32"},{name:"beta",type:"f32"}):e.activation==="LeakyRelu"&&t.push({name:"alpha",type:"f32"})},td=e=>{let t=(e==null?void 0:e.activation)||"";if(t==="HardSigmoid"){let[n,r]=(e==null?void 0:e.activation_params)||[.2,.5];return{activation:t,alpha:n,beta:r}}else if(t==="Clip"){let[n,r]=(e==null?void 0:e.activation_params)||[j0,K0];return{activation:t,clipMax:r,clipMin:n}}else if(t==="LeakyRelu"){let[n]=(e==null?void 0:e.activation_params)||[.01];return{activation:t,alpha:n}}return{activation:t}}}),It,xb,nd=re(()=>{It=(e,t)=>{switch(e){case 1:return t;case 2:return`vec2<${t}>`;case 3:return`vec3<${t}>`;case 4:return`vec4<${t}>`;default:throw new Error(`${e}-component is not supported.`)}},xb=e=>` + ${e?"value = value + getBiasByOutputCoords(coords);":""} + `}),$b,C$=re(()=>{$b=e=>` +fn getIndexFromCoords4D(coords : vec4, shape : vec4) -> i32 { + return dot(coords, vec4( + shape.y * shape.z * shape.w, shape.z * shape.w, shape.w, 1)); +} +fn getOutputIndexFromCoords(coords : vec4) -> i32 { + return dot(coords, vec4( + i32(${e}.x), i32(${e}.y), i32(${e}.z), 1)); +} +`}),xs,rd,id=re(()=>{Te(),Ce(),Ee(),ti(),xs=(e,t,n,r,i)=>{let s=r-n;return` + ${Array.from({length:n}).map((a,o)=>` + if (${we(t.shape,o,t.rank)} != 1) { + ${t.indicesSet(e,o,we(i,o+s,r))} + } else { + ${t.indicesSet(e,o,0)} + }`).join("")} +`},rd=(e,t,n,r,i=!1,s)=>{let a=e[0].dims,o=e[1].dims,l=a[a.length-2],d=o[o.length-1],p=a[a.length-1],f=ct(d),c=ct(p),m=ct(l),y=W.size(n)/f/m,w=e.length>2,$=r?r.slice(0,-2):n.slice(0,-2),_=[W.size($),l,d],v=[{type:12,data:y},{type:12,data:l},{type:12,data:d},{type:12,data:p}];Zr(t,v),v.push(..._e($,a,o)),w&&v.push(..._e(e[2].dims)),v.push(..._e(_));let I=S=>{let E=Zu("batch_dims",e[0].dataType,$.length),R=V("a",e[0].dataType,a.length,c),O=V("b",e[1].dataType,o.length,f),x=be("output",e[0].dataType,_.length,f),U=xt(x.type.tensor),q=Yr(t,x.type.value,U),J=[R,O],se="";if(w){let ue=i?f:1;J.push(V("bias",e[2].dataType,e[2].dims.length,ue)),se=`${i?`value += bias[col / ${ue}];`:`value += ${x.type.value}(bias[row + i]);`}`}let ce=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"}];Qr(t,ce);let L=()=>{let ue=`var a_data: ${R.type.value};`;for(let ie=0;ie; + for (var k: u32 = 0u; k < uniforms.K; k = k + ${c}) { + ${L()} + } + for (var i = 0u; i < ${m}u; i++) { + var value = values[i]; + ${se} + ${q} + let cur_indices = ${x.type.indices}(batch, row + i, col); + let offset = ${x.indicesToOffset("cur_indices")}; + ${x.setByOffset(`offset / ${f}`,"value")}; + } + } + `};return{name:"MatMulNaive",shaderCache:{hint:`${t.activation};${f};${c};${m};${i}`,inputDependencies:w?["rank","rank","rank"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:s?s(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(y/64)},programUniforms:v}),getShaderSource:I}}}),Nf,Bf,pu,dl,Df,fu,Pf,qa,sd=re(()=>{Te(),Ce(),Ee(),ti(),id(),nd(),Nf=(e,t)=>e?` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + kStart + inputRow, + globalRowStart / innerElementSize + inputCol${t?", batchIndices":""}); + `:` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + globalRow + innerRow, + kStart / innerElementSize + inputCol${t?", batchIndices":""}); + `,Bf=(e,t)=>e?` + let ACached0 = mm_Asub[k * innerElementSize][localRow]; + let ACached1 = mm_Asub[k * innerElementSize + 1][localRow]; + let ACached2 = mm_Asub[k * innerElementSize + 2][localRow]; + ${t===3?"":"let ACached3 = mm_Asub[k * innerElementSize + 3][localRow];"} + for (var i = 0; i < rowPerThread; i = i + 1) { + acc[i] = BCached0 * ACached0[i] + acc[i]; + acc[i] = BCached1 * ACached1[i] + acc[i]; + acc[i] = BCached2 * ACached2[i] + acc[i]; + ${t===3?"":"acc[i] = BCached3 * ACached3[i] + acc[i];"} + }`:` + for (var i = 0; i < rowPerThread; i = i + 1) { + let ACached = mm_Asub[tileRow + i][k]; + acc[i] = BCached0 * ACached.x + acc[i]; + acc[i] = BCached1 * ACached.y + acc[i]; + acc[i] = BCached2 * ACached.z + acc[i]; + ${t===3?"":"acc[i] = BCached3 * ACached.w + acc[i];"} + }`,pu=(e,t,n="f32",r,i=!1,s=32,a=!1,o=32)=>{let l=t[1]*e[1],d=t[0]*e[0],p=i?l:s,f=i?s:l,c=p/t[0],m=s/t[1];if(!((i&&c===4&&e[1]===4||!i&&(c===3||c===4))&&p%t[0]===0&&s%t[1]===0&&e[0]===4))throw new Error(`If transposeA ${i} is true, innerElementSize ${c} and workPerThread[1] ${e[1]} must be 4. + Otherwise, innerElementSize ${c} must be 3 or 4. + tileAWidth ${p} must be divisible by workgroupSize[0]${t[0]}. tileInner ${s} must be divisible by workgroupSize[1] ${t[1]}. colPerThread ${e[0]} must be 4.`);return` +var mm_Asub: array, ${p/c}>, ${f}>; +var mm_Bsub: array, ${d/e[0]}>, ${s}>; + +const rowPerThread = ${e[1]}; +const colPerThread = ${e[0]}; +const innerElementSize = ${c}; +const tileInner = ${s}; + +@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) +fn main(@builtin(local_invocation_id) localId : vec3, + @builtin(global_invocation_id) globalId : vec3, + @builtin(workgroup_id) workgroupId : vec3) { + let localRow = i32(localId.y); + let tileRow = localRow * rowPerThread; + let tileCol = i32(localId.x); + + let globalRow =i32(globalId.y) * rowPerThread; + let globalCol = i32(globalId.x); + let batch = ${a?"0":"i32(globalId.z)"}; + ${r?`let batchIndices = ${r.offsetToIndices("u32(batch)")};`:""} + let globalRowStart = i32(workgroupId.y) * ${l}; + + let num_tiles = ${a?`${Math.ceil(o/s)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; + var kStart = ${a?`i32(globalId.z) * ${o}`:"0"}; + + var acc: array, rowPerThread>; + + // Loop over shared dimension. + let tileRowB = localRow * ${m}; + for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let inputRow = tileRow + innerRow; + let inputCol = tileCol; + ${Nf(i,r)} + } + + // Load one tile of B into local memory. + for (var innerRow = 0; innerRow < ${m}; innerRow = innerRow + 1) { + let inputRow = tileRowB + innerRow; + let inputCol = tileCol; + mm_Bsub[inputRow][inputCol] = mm_readB(batch, kStart + inputRow, globalCol${r?", batchIndices":""}); + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + for (var k = 0; k < tileInner / innerElementSize; k = k + 1) { + let BCached0 = mm_Bsub[k * innerElementSize][tileCol]; + let BCached1 = mm_Bsub[k * innerElementSize + 1][tileCol]; + let BCached2 = mm_Bsub[k * innerElementSize + 2][tileCol]; + ${c===3?"":"let BCached3 = mm_Bsub[k * innerElementSize + 3][tileCol];"} + + ${Bf(i,c)} + } + + workgroupBarrier(); + } + + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + mm_write(batch, globalRow + innerRow, globalCol, acc[innerRow]); + } +}`},dl=(e,t)=>e?` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + kStart + inputRow, + globalRowStart + inputCol${t?", batchIndices":""}); + `:` + mm_Asub[inputRow][inputCol] = mm_readA(batch, + globalRowStart + inputRow, + kStart + inputCol${t?", batchIndices":""}); + `,Df=e=>e?"let ACached = mm_Asub[k][tileRow + innerRow];":"let ACached = mm_Asub[tileRow + innerRow][k];",fu=(e,t,n="f32",r,i=!1,s=32,a=!1,o=32,l=!1)=>{let d=e[1]*t[1],p=e[0]*t[0],f=i?d:s,c=i?s:d;if(!(c%t[1]===0&&f%t[0]===0&&s%t[1]===0))throw new Error(`tileAHight ${c} must be divisible by workgroupSize[1]${t[1]}, tileAWidth ${f} must be divisible by workgroupSize[0]${t[0]}, tileInner ${s} must be divisible by workgroupSize[1]${t[1]}`);let m=c/t[1],y=f/t[0],w=s/t[1],$=l?` + let localRow = i32(localId.y); + let localCol = i32(localId.x); + let globalRowStart = i32(workgroupId.y) * ${d}; + let globalColStart = i32(workgroupId.x) * ${p}; + + // Loop over shared dimension. + for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var inputRow = localRow; inputRow < ${c}; inputRow = inputRow + ${t[1]}) { + for (var inputCol = localCol; inputCol < ${f}; inputCol = inputCol + ${t[0]}) { + ${dl(i,r)} + } + } + // Load one tile of B into local memory. + for (var inputRow = localRow; inputRow < ${s}; inputRow = inputRow + ${t[1]}) { + for (var inputCol = localCol; inputCol < ${p}; inputCol = inputCol + ${t[0]}) { + mm_Bsub[inputRow][inputCol] = mm_readB(batch, + kStart + inputRow, + globalColStart + inputCol${r?", batchIndices":""}); + } + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + var BCached : array<${n}, colPerThread>; + for (var k = 0; k < tileInner; k = k + 1) { + for (var inner = 0; inner < colPerThread; inner = inner + 1) { + BCached[inner] = mm_Bsub[k][localCol + inner * ${t[0]}]; + } + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let ACached = ${i?`mm_Asub[k][localRow + innerRow * ${t[1]}];`:`mm_Asub[localRow + innerRow * ${t[1]}][k];`} + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + acc[innerRow][innerCol] = acc[innerRow][innerCol] + + ACached * BCached[innerCol]; + } + } + } + workgroupBarrier(); + } + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + let gRow = globalRowStart + localRow + innerRow * ${t[1]}; + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + let gCol = globalColStart + localCol + innerCol * ${t[0]}; + mm_write(batch, gRow, gCol, acc[innerRow][innerCol]); + } + } + `:` +let tileRow = i32(localId.y) * rowPerThread; +let tileCol = i32(localId.x) * colPerThread; + +let globalRow = i32(globalId.y) * rowPerThread; +let globalCol = i32(globalId.x) * colPerThread; +let globalRowStart = i32(workgroupId.y) * ${d}; + +let tileRowA = i32(localId.y) * ${m}; +let tileColA = i32(localId.x) * ${y}; +let tileRowB = i32(localId.y) * ${w}; +// Loop over shared dimension. +for (var t = 0; t < num_tiles; t = t + 1) { + // Load one tile of A into local memory. + for (var innerRow = 0; innerRow < ${m}; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < ${y}; innerCol = innerCol + 1) { + let inputRow = tileRowA + innerRow; + let inputCol = tileColA + innerCol; + ${dl(i,r)} + } + } + + // Load one tile of B into local memory. + for (var innerRow = 0; innerRow < ${w}; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + let inputRow = tileRowB + innerRow; + let inputCol = tileCol + innerCol; + mm_Bsub[inputRow][inputCol] = mm_readB(batch, + kStart + inputRow, + globalCol + innerCol${r?", batchIndices":""}); + } + } + kStart = kStart + tileInner; + workgroupBarrier(); + + // Compute acc values for a single thread. + var BCached : array<${n}, colPerThread>; + for (var k = 0; k < tileInner; k = k + 1) { + for (var inner = 0; inner < colPerThread; inner = inner + 1) { + BCached[inner] = mm_Bsub[k][tileCol + inner]; + } + + for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + ${Df(i)} + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + acc[innerRow][innerCol] = acc[innerRow][innerCol] + ACached * BCached[innerCol]; + } + } + } + + workgroupBarrier(); +} + +for (var innerRow = 0; innerRow < rowPerThread; innerRow = innerRow + 1) { + for (var innerCol = 0; innerCol < colPerThread; innerCol = innerCol + 1) { + mm_write(batch, globalRow + innerRow, globalCol + innerCol, + acc[innerRow][innerCol]); + } +} +`;return` + var mm_Asub : array, ${c}>; + var mm_Bsub : array, ${s}>; + const rowPerThread = ${e[1]}; + const colPerThread = ${e[0]}; + const tileInner = ${s}; + +@compute @workgroup_size(${t[0]}, ${t[1]}, ${t[2]}) +fn main(@builtin(local_invocation_id) localId : vec3, + @builtin(global_invocation_id) globalId : vec3, + @builtin(workgroup_id) workgroupId : vec3) { + let batch = ${a?"0":"i32(globalId.z)"}; + ${r?`let batchIndices = ${r.offsetToIndices("u32(batch)")};`:""} + let num_tiles = ${a?`${Math.ceil(o/s)}`:"(uniforms.dim_inner - 1) / tileInner + 1"}; + var kStart = ${a?`i32(globalId.z) * ${o}`:"0"}; + + var acc : array, rowPerThread>; + ${$} + } +`},Pf=(e,t,n,r,i=!1)=>{let[s,a,o,l]=r,d=xt(r[0].type.tensor);return` + fn mm_readA(batch: i32, row: i32, colIn: i32, batchIndices: ${s.type.indices}) -> ${It(e,d)} { + var value = ${It(e,d)}(0.0); + let col = colIn * ${e}; + if(row < uniforms.dim_a_outer && col < uniforms.dim_inner) + { + var aIndices: ${a.type.indices}; + ${xs("aIndices",a,a.rank-2,s.rank,"batchIndices")} + ${a.indicesSet("aIndices",a.rank-2,"u32(row)")} + ${a.indicesSet("aIndices",a.rank-1,"u32(colIn)")} + value = ${a.getByIndices("aIndices")}; + } + return value; + } + + fn mm_readB(batch: i32, row: i32, colIn: i32, batchIndices: ${s.type.indices}) -> ${It(e,d)} { + var value = ${It(e,d)}(0.0); + let col = colIn * ${e}; + if(row < uniforms.dim_inner && col < uniforms.dim_b_outer) + { + var bIndices: ${o.type.indices}; + ${xs("bIndices",o,o.rank-2,s.rank,"batchIndices")} + ${o.indicesSet("bIndices",o.rank-2,"u32(row)")} + ${o.indicesSet("bIndices",o.rank-1,"u32(colIn)")} + value = ${o.getByIndices("bIndices")}; + } + return value; + } + + fn mm_write(batch: i32, row: i32, colIn: i32, valueIn: ${It(e,d)}) { + let col = colIn * ${e}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) { + var value = valueIn; + let coords = vec3(batch, row, colIn); + ${t?`value = value + ${i?"bias[colIn]":`${It(e,d)}(bias[row])`};`:""} + ${n} + ${l.setByIndices("vec3(coords)","value")} + } + } + `},qa=(e,t,n,r,i=!1,s)=>{let a=e[0].dims,o=e[1].dims,l=a.slice(0,-2),d=o.slice(0,-2),p=r?r.slice(0,-2):n.slice(0,-2),f=W.size(p),c=a[a.length-2],m=a[a.length-1],y=o[o.length-1],w=m%4===0&&y%4===0,$=c<=8?[4,1,1]:[4,4,1],_=[8,8,1],v=[Math.ceil(y/_[0]/$[0]),Math.ceil(c/_[1]/$[1]),Math.ceil(f/_[2]/$[2])],I=w?4:1,S=[...l,c,m/I],E=S.length,R=[...d,m,y/I],O=R.length,x=[f,c,y/I],U=[{type:6,data:c},{type:6,data:y},{type:6,data:m}];Zr(t,U),U.push(..._e(p,S,R));let q=["rank","rank"],J=e.length>2;J&&(U.push(..._e(e[2].dims)),q.push("rank")),U.push(..._e(x));let se=ce=>{let L=p.length,ue=Zu("batchDims",e[0].dataType,L,1),ie=xt(e[0].dataType),te=V("a",e[0].dataType,E,I),he=V("b",e[1].dataType,O,I),Q=be("result",e[0].dataType,x.length,I),ve=[te,he];if(J){let Ue=i?I:1;ve.push(V("bias",e[2].dataType,e[2].dims.length,Ue))}let Y=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"}];Qr(t,Y);let P=xt(Q.type.tensor),ee=Yr(t,Q.type.value,P),ne=Pf(I,J,ee,[ue,te,he,Q],i);return` + ${ce.registerUniforms(Y).registerInternalVariables(ue).declareVariables(...ve,Q)} + ${ne} + ${w?pu($,_,ie,ue):fu($,_,ie,ue)} + `};return{name:"MatMul",shaderCache:{hint:`${$};${t.activation};${w};${i}`,inputDependencies:q},getRunData:()=>({outputs:[{dims:s?s(n):n,dataType:e[0].dataType}],dispatchGroup:{x:v[0],y:v[1],z:v[2]},programUniforms:U}),getShaderSource:se}}}),Uf,Sb,E$=re(()=>{Te(),nr(),Ee(),ti(),nd(),C$(),sd(),Uf=(e,t,n,r,i=!1,s,a=4,o=4,l=4,d="f32")=>{let p=U=>{switch(U){case 1:return"resData = x[xIndex];";case 3:return`resData = vec3<${d}>(x[xIndex], x[xIndex + 1], x[xIndex + 2]);`;case 4:return"resData = x[xIndex / 4];";default:throw new Error(`innerElementSize ${U} is not supported.`)}},f=U=>{switch(U){case 1:return"return w[row * i32(uniforms.w_shape[3]) + colIn];";case 4:return"return w[row * i32(uniforms.w_shape[3]) / 4 + colIn];";default:throw new Error(`innerElementSize ${U} is not supported.`)}},c=e?` + let coord = vec4(batch, xRow, xCol, xCh); + `:` + let coord = vec4(batch, xCh, xRow, xCol); + `,m=e?` + let coords = vec4( + batch, + row / outWidth, + row % outWidth, + col); + `:` + let coords = vec4( + batch, + row, + col / outWidth, + col % outWidth); + `,y=e?"i32(uniforms.x_shape[1])":"i32(uniforms.x_shape[2])",w=e?"i32(uniforms.x_shape[2])":"i32(uniforms.x_shape[3])",$=e?"row":"col",_=e?"col":"row",v=` + let inChannels = i32(uniforms.w_shape[2]); + let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; + let outRow = ${$} / outWidth; + let outCol = ${$} % outWidth; + + let WRow = ${_} / (i32(uniforms.w_shape[1]) * inChannels); + let WCol = ${_} / inChannels % i32(uniforms.w_shape[1]); + let xRow = outRow * uniforms.stride[0] + uniforms.dilation[0] * WRow - uniforms.pad[0]; + let xCol = outCol * uniforms.stride[1] + uniforms.dilation[1] * WCol - uniforms.pad[1]; + let xCh = ${_} % inChannels; + var resData = ${It(a,d)}(0.0); + // The bounds checking is always needed since we use it to pad zero for + // the 'same' padding type. + if (xRow >= 0 && xRow < ${y} && xCol >= 0 && xCol < ${w}) { + ${c} + let xIndex = getIndexFromCoords4D(coord, vec4(uniforms.x_shape)); + ${p(a)} + } + return resData;`,I=e?t&&r?` + let col = colIn * ${a}; + ${v}`:` + let col = colIn * ${a}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_inner) { + ${v} + } + return ${It(a,d)}(0.0);`:r&&n?` + let col = colIn * ${a}; + ${v}`:` + let col = colIn * ${a}; + if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { + ${v} + } + return ${It(a,d)}(0.0);`,S=e?r&&n?f(o):` + let col = colIn * ${o}; + if (row < uniforms.dim_inner && col < uniforms.dim_b_outer) { + ${f(o)} + } + return ${It(o,d)}(0.0);`:` + let col = colIn * ${o}; + if (row < uniforms.dim_inner && col < uniforms.dim_a_outer) { + ${f(o)} + } + return ${It(o,d)}(0.0);`,E=It(l,d),R=It(e?a:o,d),O=It(e?o:a,d),x=Yr(s,E,d);return` + fn mm_readA(batch: i32, row : i32, colIn : i32) -> ${R} { + ${e?I:S} + } + + fn mm_readB(batch: i32, row : i32, colIn : i32) -> ${O} { + ${e?S:I} + } + + fn mm_write(batch: i32, row : i32, colIn : i32, valueIn : ${E}) { + let col = colIn * ${l}; + if (row < uniforms.dim_a_outer && col < uniforms.dim_b_outer) + { + var value = valueIn; + let outWidth = ${e?"i32(uniforms.result_shape[2])":"i32(uniforms.result_shape[3])"}; + ${m} + ${xb(i)} + ${x} + setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value); + } + }`},Sb=(e,t,n,r,i,s,a,o,l)=>{let d=t.format==="NHWC",p=d?e[0].dims[3]:e[0].dims[1],f=n[0],c=d?n[2]:n[3],m=d?n[1]:n[2],y=d?n[3]:n[1],w=d&&(p%4===0||p%3===0)&&y%4===0,$=d?y:c*m,_=d?c*m:y,v=[8,8,1],I=r<=8?[4,1,1]:[4,4,1],S=[Math.ceil($/v[0]/I[0]),Math.ceil(_/v[1]/I[1]),Math.ceil(f/v[2]/I[2])];Pe("verbose",()=>`[conv2d_mm_webgpu] dispatch = ${S}`);let E=w?d&&p%4!==0?3:4:1,R=v[1]*I[1],O=v[0]*I[0],x=Math.max(v[0]*E,v[1]),U=r%R===0,q=i%O===0,J=s%x===0,se=w?[E,4,4]:[1,1,1],ce=[{type:6,data:r},{type:6,data:i},{type:6,data:s},{type:6,data:[t.pads[0],t.pads[1]]},{type:6,data:t.strides},{type:6,data:t.dilations}];Zr(t,ce),ce.push(..._e(e[0].dims,e[1].dims));let L=["rank","rank"];a&&(ce.push(..._e(e[2].dims)),L.push("rank")),ce.push(..._e(n));let ue=ie=>{let te=[{name:"dim_a_outer",type:"i32"},{name:"dim_b_outer",type:"i32"},{name:"dim_inner",type:"i32"},{name:"pad",type:"i32",length:2},{name:"stride",type:"i32",length:2},{name:"dilation",type:"i32",length:2}];Qr(t,te);let he=w?4:1,Q=xt(e[0].dataType),ve=` + fn setOutputAtIndex(flatIndex : i32, value : ${w?`vec4<${Q}>`:Q}) { + result[flatIndex] = ${w?`vec4<${Q}>`:Q}(value); + } + fn setOutputAtCoords(d0 : i32, d1 : i32, d2 : i32, d3 : i32, value : ${w?`vec4<${Q}>`:Q}) { + let flatIndex = getOutputIndexFromCoords(vec4(d0, d1, d2, d3)); + setOutputAtIndex(flatIndex ${w?"/ 4":""}, value); + }`,Y=V("x",e[0].dataType,e[0].dims.length,E===3?1:E),P=V("w",e[1].dataType,e[1].dims.length,he),ee=[Y,P],ne=be("result",e[0].dataType,n.length,he);if(a){let Ue=V("bias",e[2].dataType,e[2].dims.length,he);ee.push(Ue),ve+=` + fn getBiasByOutputCoords(coords : vec4) -> ${w?`vec4<${Q}>`:Q} { + return bias[coords.${d?"w":"y"}${w?"/ 4":""}]; + }`}return` + ${$b("uniforms.result_strides")} + //struct Uniforms { xShape : vec4, wShape : vec4, outShape : vec4, + // outShapeStrides: vec3, filterDims : vec2, pad : vec2, stride : vec2, + // dilation : vec2, dimAOuter : i32, dimBOuter : i32, dimInner : i32 }; + ${ie.registerUniforms(te).declareVariables(...ee,ne)} + ${ve} + ${Uf(d,U,q,J,a,t,se[0],se[1],se[2],Q)} + ${w?pu(I,v,Q,void 0,!d,x):fu(I,v,Q,void 0,!d,x,!1,void 0,o)}`};return{name:"Conv2DMatMul",shaderCache:{hint:`${t.cacheKey};${E};${w};${U};${q};${J};${R};${O};${x}`,inputDependencies:L},getRunData:()=>({outputs:[{dims:l?l(n):n,dataType:e[0].dataType}],dispatchGroup:{x:S[0],y:S[1],z:S[2]},programUniforms:ce}),getShaderSource:ue}}}),Lf,cl,is,Wf,pl,qf,Tb,Ib,k$=re(()=>{Te(),nr(),Ce(),Ee(),ti(),nd(),Lf=e=>{let t=1;for(let n=0;ntypeof e=="number"?[e,e,e]:e,is=(e,t)=>t<=1?e:e+(e-1)*(t-1),Wf=(e,t,n,r=1)=>{let i=is(t,r);return Math.floor((e[0]*(n-1)-n+i)/2)},pl=(e,t,n,r,i)=>{i==null&&(i=Wf(e,t[0],r[0]));let s=[0,0,0,n];for(let a=0;a<3;a++)e[a]+2*i>=t[a]&&(s[a]=Math.trunc((e[a]-t[a]+2*i)/r[a]+1));return s},qf=(e,t,n,r,i,s,a,o,l,d)=>{let p,f,c,m;if(e==="VALID"&&(e=0),typeof e=="number"){p={top:e,bottom:e,left:e,right:e,front:e,back:e};let y=pl([t,n,r,1],[o,l,d],1,[i,s,a],e);f=y[0],c=y[1],m=y[2]}else if(Array.isArray(e)){if(!e.every((w,$,_)=>w===_[0]))throw Error(`Unsupported padding parameter: ${e}`);p={top:e[0],bottom:e[1],left:e[2],right:e[3],front:e[4],back:e[5]};let y=pl([t,n,r,1],[o,l,d],1,[i,s,a],e[0]);f=y[0],c=y[1],m=y[2]}else if(e==="SAME_UPPER"){f=Math.ceil(t/i),c=Math.ceil(n/s),m=Math.ceil(r/a);let y=(f-1)*i+o-t,w=(c-1)*s+l-n,$=(m-1)*a+d-r,_=Math.floor(y/2),v=y-_,I=Math.floor(w/2),S=w-I,E=Math.floor($/2),R=$-E;p={top:I,bottom:S,left:E,right:R,front:_,back:v}}else throw Error(`Unknown padding parameter: ${e}`);return{padInfo:p,outDepth:f,outHeight:c,outWidth:m}},Tb=(e,t,n,r,i,s=!1,a="channelsLast")=>{let o,l,d,p,f;if(a==="channelsLast")[o,l,d,p,f]=e;else if(a==="channelsFirst")[o,f,l,d,p]=e;else throw new Error(`Unknown dataFormat ${a}`);let[c,,m,y,w]=t,[$,_,v]=cl(n),[I,S,E]=cl(r),R=is(m,I),O=is(y,S),x=is(w,E),{padInfo:U,outDepth:q,outHeight:J,outWidth:se}=qf(i,l,d,p,$,_,v,R,O,x),ce=s?c*f:c,L=[0,0,0,0,0];return a==="channelsFirst"?L=[o,ce,q,J,se]:a==="channelsLast"&&(L=[o,q,J,se,ce]),{batchSize:o,dataFormat:a,inDepth:l,inHeight:d,inWidth:p,inChannels:f,outDepth:q,outHeight:J,outWidth:se,outChannels:ce,padInfo:U,strideDepth:$,strideHeight:_,strideWidth:v,filterDepth:m,filterHeight:y,filterWidth:w,effectiveFilterDepth:R,effectiveFilterHeight:O,effectiveFilterWidth:x,dilationDepth:I,dilationHeight:S,dilationWidth:E,inShape:e,outShape:L,filterShape:t}},Ib=(e,t,n,r,i,s)=>{let a=s==="channelsLast";a?e[0].dims[3]:e[0].dims[1];let o=[64,1,1],l={x:n.map(($,_)=>_)},d=[Math.ceil(Lf(l.x.map($=>n[$]))/o[0]),1,1];Pe("verbose",()=>`[conv3d_naive_webgpu] dispatch = ${d}`);let p=1,f=W.size(n),c=[{type:12,data:f},{type:12,data:r},{type:12,data:i},{type:12,data:t.strides},{type:12,data:t.dilations}];Zr(t,c),c.push(..._e(e[0].dims,e[1].dims));let m=["rank","rank"],y=e.length===3;y&&(c.push(..._e(e[2].dims)),m.push("rank")),c.push(..._e(n));let w=$=>{let _=[{name:"output_size",type:"u32"},{name:"filter_dims",type:"u32",length:r.length},{name:"pads",type:"u32",length:i.length},{name:"strides",type:"u32",length:t.strides.length},{name:"dilations",type:"u32",length:t.dilations.length}];Qr(t,_);let v=1,I=xt(e[0].dataType),S=V("x",e[0].dataType,e[0].dims.length,p),E=V("W",e[1].dataType,e[1].dims.length,v),R=[S,E],O=be("result",e[0].dataType,n.length,v),x="";if(y){let J=V("bias",e[2].dataType,e[2].dims.length,v);R.push(J),x+=` + fn getBiasByOutputCoords(coords : array) -> ${I} { + return bias[${a?we("coords",4,5):we("coords",1,5)}]; + }`}let U=It(p,I),q=Yr(t,U,I);return` + ${x} + fn getX(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { + let aIndices = array(d0, d1, d2, d3, d4); + return ${S.getByIndices("aIndices")}; + } + fn getW(d0 : u32, d1 : u32, d2 : u32, d3 : u32, d4 : u32) -> f32 { + let aIndices = array(d0, d1, d2, d3, d4); + return ${E.getByIndices("aIndices")}; + } + ${$.registerUniforms(_).declareVariables(...R,O)} + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let coords = ${O.offsetToIndices("global_idx")}; + let batch = ${we("coords",0,S.rank)}; + let d2 = ${a?we("coords",S.rank-1,S.rank):we("coords",1,S.rank)}; + let xFRCCorner = vec3(${a?we("coords",1,S.rank):we("coords",2,S.rank)}, + ${a?we("coords",2,S.rank):we("coords",3,S.rank)}, + ${a?we("coords",3,S.rank):we("coords",4,S.rank)}) * uniforms.strides - uniforms.pads; + let xFCorner = xFRCCorner.x; + let xRCorner = xFRCCorner.y; + let xCCorner = xFRCCorner.z; + let xShapeY = ${a?we("uniforms.x_shape",1,S.rank):we("uniforms.x_shape",2,S.rank)}; + let xShapeZ = ${a?we("uniforms.x_shape",2,S.rank):we("uniforms.x_shape",3,S.rank)}; + let xShapeW = ${a?we("uniforms.x_shape",3,S.rank):we("uniforms.x_shape",4,S.rank)}; + let xShapeU = ${a?we("uniforms.x_shape",4,S.rank):we("uniforms.x_shape",1,S.rank)}; + let inputDepthNearestVec4 = (xShapeU / 4) * 4; + let inputDepthVec4Remainder = xShapeU % 4; + + var value = 0.0; + for (var wF = 0u; wF < uniforms.filter_dims[0]; wF++) { + let xF = xFCorner + wF * uniforms.dilations[0]; + if (xF < 0 || xF >= xShapeY) { + continue; + } + + for (var wR = 0u; wR < uniforms.filter_dims[1]; wR++) { + let xR = xRCorner + wR * uniforms.dilations[1]; + if (xR < 0 || xR >= xShapeZ) { + continue; + } + + for (var wC = 0u; wC < uniforms.filter_dims[2]; wC++) { + let xC = xCCorner + wC * uniforms.dilations[2]; + if (xC < 0 || xC >= xShapeW) { + continue; + } + + for (var d1 = 0u; d1 < inputDepthNearestVec4; d1 += 4) { + ${a?`let xValues = vec4( + getX(batch, xF, xR, xC, d1), + getX(batch, xF, xR, xC, d1 + 1), + getX(batch, xF, xR, xC, d1 + 2), + getX(batch, xF, xR, xC, d1 + 3)); + `:`let xValues = vec4( + getX(batch, d1, xF, xR, xC), + getX(batch, d1 + 1, xF, xR, xC), + getX(batch, d1 + 2, xF, xR, xC), + getX(batch, d1 + 3, xF, xR, xC)); + `} + let wValues = vec4( + getW(d2, d1, wF, wR, wC), + getW(d2, d1 + 1, wF, wR, wC), + getW(d2, d1 + 2, wF, wR, wC), + getW(d2, d1 + 3, wF, wR, wC)); + value += dot(xValues, wValues); + } + if (inputDepthVec4Remainder == 1) { + ${a?`value += getX(batch, xF, xR, xC, inputDepthNearestVec4) + * getW(d2, inputDepthNearestVec4, wF, wR, wC);`:`value += getX(batch, inputDepthNearestVec4, xF, xR, xC) + * getW(d2, inputDepthNearestVec4, wF, wR, wC);`} + } else if (inputDepthVec4Remainder == 2) { + ${a?`let xValues = vec2( + getX(batch, xF, xR, xC, inputDepthNearestVec4), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1)); + `:`let xValues = vec2( + getX(batch, inputDepthNearestVec4, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC)); + `} + let wValues = vec2( + getW(d2, inputDepthNearestVec4, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC)); + value += dot(xValues, wValues); + } else if (inputDepthVec4Remainder == 3) { + ${a?`let xValues = vec3( + getX(batch, xF, xR, xC, inputDepthNearestVec4), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 1), + getX(batch, xF, xR, xC, inputDepthNearestVec4 + 2)); + `:`let xValues = vec3( + getX(batch, inputDepthNearestVec4, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 1, xF, xR, xC), + getX(batch, inputDepthNearestVec4 + 2, xF, xR, xC)); + `} + let wValues = vec3( + getW(d2, inputDepthNearestVec4, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 1, wF, wR, wC), + getW(d2, inputDepthNearestVec4 + 2, wF, wR, wC)); + value += dot(xValues, wValues); + } + } + } + } + ${y?"value = value + getBiasByOutputCoords(coords)":""}; + ${q} + result[global_idx] = f32(value); + }`};return{name:"Conv3DNaive",shaderCache:{hint:`${t.cacheKey};${a};${p};${y}`,inputDependencies:m},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:d[0],y:d[1],z:d[2]},programUniforms:c}),getShaderSource:w}}}),Cb,Eb,z$=re(()=>{Te(),Ce(),Ee(),ti(),Cb=(e,t,n,r)=>{let i=e.length>2,s=i?"value += b[output_channel];":"",a=e[0].dims,o=e[1].dims,l=t.format==="NHWC",d=l?n[3]:n[1],p=d/t.group,f=l&&p>=4?ct(d):1,c=W.size(n)/f,m=[{type:12,data:c},{type:12,data:t.dilations},{type:12,data:[t.strides[0],t.strides[1]]},{type:12,data:[t.pads[0],t.pads[1]]},{type:12,data:p}];Zr(t,m),m.push(..._e(a,[o[0],o[1],o[2],o[3]/f]));let y=i?["rank","rank","rank"]:["rank","rank"];m.push(..._e([n[0],n[1],n[2],n[3]/f]));let w=$=>{let _=be("output",e[0].dataType,n.length,f),v=xt(_.type.tensor),I=Yr(t,_.type.value,v),S=V("x",e[0].dataType,a.length),E=V("w",e[1].dataType,o.length,f),R=[S,E];i&&R.push(V("b",e[2].dataType,e[2].dims,f));let O=[{name:"output_size",type:"u32"},{name:"dilations",type:"u32",length:t.dilations.length},{name:"strides",type:"u32",length:2},{name:"pads",type:"u32",length:2},{name:"output_channels_per_group",type:"u32"}];Qr(t,O);let x=l?` + for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[0]; wHeight++) { + let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; + + if (xHeight < 0u || xHeight >= uniforms.x_shape[1]) { + continue; + } + + for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[1]; wWidth++) { + let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; + if (xWidth < 0u || xWidth >= uniforms.x_shape[2]) { + continue; + } + + for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[2]; wInChannel++) { + let input_channel = in_channel_offset + wInChannel; + let xVal = ${S.get("batch","xHeight","xWidth","input_channel")}; + let wVal = ${E.get("wHeight","wWidth","wInChannel","output_channel")}; + value += xVal * wVal; + } + } + } + `:` + for (var wInChannel: u32 = 0u; wInChannel < uniforms.w_shape[1]; wInChannel++) { + let input_channel = in_channel_offset + wInChannel; + for (var wHeight: u32 = 0u; wHeight < uniforms.w_shape[2]; wHeight++) { + let xHeight = xRCCorner.x + wHeight * uniforms.dilations[0]; + + if (xHeight < 0u || xHeight >= uniforms.x_shape[2]) { + continue; + } + + for (var wWidth: u32 = 0u; wWidth < uniforms.w_shape[3]; wWidth++) { + let xWidth = xRCCorner.y + wWidth * uniforms.dilations[1]; + if (xWidth < 0u || xWidth >= uniforms.x_shape[3]) { + continue; + } + + let xVal = ${S.get("batch","input_channel","xHeight","xWidth")}; + let wVal = ${E.get("output_channel","wInChannel","wHeight","wWidth")}; + value += xVal * wVal; + } + } + } + `;return` + ${$.registerUniforms(O).declareVariables(...R,_)} + + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let outputIndices = ${_.offsetToIndices("global_idx")}; + let batch: u32 = outputIndices[0]; + let output_channel: u32 = outputIndices[${l?3:1}]; + let xRCCorner: vec2 = vec2(outputIndices[${l?1:2}], outputIndices[${l?2:3}]) * uniforms.strides - uniforms.pads; + let group_id: u32 = output_channel * ${f} / uniforms.output_channels_per_group; + var in_channel_offset = group_id * uniforms.w_shape[${l?2:1}]; + + var value: ${_.type.value} = ${_.type.value}(0); + ${x} + ${s} + ${I} + ${_.setByOffset("global_idx","value")} + }`};return{name:"GroupedConv",shaderCache:{hint:`${t.cacheKey}_${f}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:r?r(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:m}),getShaderSource:w}},Eb=(e,t,n,r)=>{let i=e.length>2,s=ct(n[3]),a=ct(n[2]),o=W.size(n)/s/a,l=[e[0].dims[0],e[0].dims[1],e[0].dims[2],e[0].dims[3]/s],d=[e[1].dims[0],e[1].dims[1],e[1].dims[2],e[1].dims[3]/s],p=[n[0],n[1],n[2],n[3]/s],f=[{type:12,data:o},{type:6,data:[t.strides[0],t.strides[1]]},{type:6,data:[t.pads[0],t.pads[1]]}];Zr(t,f),f.push(..._e(l,d,p));let c=(a-1)*t.strides[1]+d[1],m=y=>{let w=be("output",e[0].dataType,p.length,s),$=xt(w.type.tensor),_=Yr(t,w.type.value,$),v=V("x",e[0].dataType,l.length,s),I=V("w",e[1].dataType,d.length,s),S=[v,I];i&&S.push(V("b",e[2].dataType,e[2].dims,s));let E=i?"value += b[output_channel];":"",R=[{name:"output_size",type:"u32"},{name:"strides",type:"i32",length:2},{name:"pads",type:"i32",length:2}];return Qr(t,R),` + ${y.registerUniforms(R).declareVariables(...S,w)} + ${y.mainStart()} + ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let width0 = uniforms.output_shape[3]; + let output_channel = global_idx % width0; + var index1 = global_idx / width0; + let width1 = uniforms.output_shape[2] / ${a}u; + let col = (index1 % width1) * ${a}u; + index1 = index1 / width1; + let row = index1 % uniforms.output_shape[1]; + let batch = index1 / uniforms.output_shape[1]; + + let x_corner = vec2(i32(row), i32(col)) * uniforms.strides - uniforms.pads; + + var x_vals: array<${v.type.value}, ${c}>; + var values: array<${w.type.value}, ${a}>; + let input_channel = output_channel; + // Use constant instead of uniform can give better performance for w's height/width. + for (var w_height: u32 = 0u; w_height < ${d[0]}; w_height++) { + let x_height = x_corner.x + i32(w_height); + if (x_height >= 0 && u32(x_height) < uniforms.x_shape[1]) { + for (var i = 0; i < ${c}; i++) { + let x_width = x_corner.y + i; + if (x_width >= 0 && u32(x_width) < uniforms.x_shape[2]) { + x_vals[i] = ${v.get("batch","u32(x_height)","u32(x_width)","input_channel")}; + } else { + x_vals[i] = ${v.type.value}(0); + } + } + for (var w_width: u32 = 0u; w_width < ${d[1]}; w_width++) { + let w_val = ${I.get("w_height","w_width","0","output_channel")}; + for (var i = 0u; i < ${a}u; i++) { + values[i] = fma(x_vals[i * u32(uniforms.strides[1]) + w_width], w_val, values[i]); + } + } + } + } + + for (var i = 0u; i < ${a}u; i++) { + var value = values[i]; + ${E} + ${_} + ${w.set("batch","row","col + i","output_channel","value")}; + } + }`};return{name:"GroupedConv-Vectorize",shaderCache:{hint:`${t.cacheKey};${s};${a};${c};${d[0]};${d[1]}`,inputDependencies:i?["rank","rank","type"]:["rank","rank"]},getRunData:()=>({outputs:[{dims:r?r(n):n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:f}),getShaderSource:m}}}),Gf,fa,Vf,ha,hu,fl,Ff,Hf,mu,A$=re(()=>{Ce(),E$(),k$(),sd(),z$(),ti(),id(),_r(),Gf=(e,t,n,r,i,s)=>{let a=e[0],o=e.slice(s?1:2,s?3:4),l=o.length,d=t[0],p=t.slice(2).map((c,m)=>c+(c-1)*(n[m]-1)),f=o.map((c,m)=>c+r[m]+r[m+l]).map((c,m)=>Math.floor((c-p[m]+i[m])/i[m]));return f.splice(0,0,a),f.splice(s?3:1,0,d),f},fa=[2,3,1,0],Vf=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length>5)throw new Error("greater than 5D is not supported");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],r=e[1].dims[1]*t.group;if(n!==r)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(e.length===3&&(e[2].dims.length!==1||e[1].dims[0]!==e[2].dims[0]))throw new Error("invalid bias");let i=e[0].dims.length-2;if(t.dilations.length!==i)throw new Error(`dilations should be ${i}D`);if(t.strides.length!==i)throw new Error(`strides should be ${i}D`);if(t.pads.length!==i*2)throw new Error(`pads should be ${i*2}D`);if(t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape")},ha=(e,t)=>{let n=e.kernelShape.slice();n.length{let t=td(e),n=e.format,r=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],i=e.dilations,s=e.group,a=e.kernel_shape,o=e.pads,l=e.strides,d=e.w_is_const();return{autoPad:r,format:n,dilations:i,group:s,kernelShape:a,pads:o,strides:l,wIsConst:d,...t,cacheKey:`${e.format};${t.activation};`}},fl=(e,t,n,r)=>{let i=n.format==="NHWC",s=Gf(t[0].dims,t[1].dims,n.dilations,n.pads,n.strides,i);if(n.group!==1){let R=[t[0]];if(i){let O=e.kernelCustomData.wT??e.compute(en(t[1],fa),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=O),R.push(O)}else R.push(t[1]);t.length===3&&R.push(t[2]),!e.adapterInfo.isArchitecture("ampere")&&i&&t[1].dims[0]===n.group&&t[1].dims[1]===1&&n.dilations[0]===1&&n.dilations[1]===1?e.compute(Eb(R,n,s,r),{inputs:R}):e.compute(Cb(R,n,s,r),{inputs:R});return}let a=t.length===3,o=t[0].dims[i?1:2],l=t[0].dims[i?2:3],d=t[0].dims[i?3:1],p=t[1].dims[2],f=t[1].dims[3],c=s[i?1:2],m=s[i?2:3],y=s[i?3:1],w=i&&p===o&&f===l&&n.pads[0]===0&&n.pads[1]===0;if(w||p===1&&f===1&&n.dilations[0]===1&&n.dilations[1]===1&&n.strides[0]===1&&n.strides[1]===1&&n.pads[0]===0&&n.pads[1]===0){let R=s[0],O,x,U,q=[];if(i){let ce=e.kernelCustomData.wT??e.compute(en(t[1],fa),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];if(n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=ce),w){let L=o*l*d;O=t[0].reshape([1,R,L]),x=ce.reshape([1,L,y]),U=[1,R,y]}else O=t[0].reshape([R,o*l,d]),x=ce.reshape([1,d,y]),U=[R,c*m,y];q.push(O),q.push(x)}else O=t[0].reshape([R,d,o*l]),x=t[1].reshape([1,y,d]),U=[R,y,c*m],q.push(x),q.push(O);a&&q.push(t[2]);let J=U[2],se=q[0].dims[q[0].dims.length-1];J<8&&se<8?e.compute(rd(q,n,s,U,i,r),{inputs:q}):e.compute(qa(q,n,s,U,i,r),{inputs:q});return}let $=!0,_=e.kernelCustomData.wT??e.compute(en(t[1],fa),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=_);let v=[t[0],_];a&&v.push(t[2]);let I=i?c*m:y,S=i?y:c*m,E=p*f*d;e.compute(Sb(v,n,s,I,S,E,a,$,r),{inputs:v})},Ff=(e,t)=>{let n=t.format==="NHWC",r=[e.inputs[0].reshape(n?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&r.push(e.inputs[2]);let i=[0,t.pads[0],0,t.pads[1]],s=[1].concat(t.strides),a=[1].concat(t.dilations),o=[1].concat(t.kernelShape),l=ha({...t,pads:i,strides:s,dilations:a,kernelShape:o},r);fl(e,r,l,d=>n?[d[0],d[2],d[3]]:[d[0],d[1],d[3]])},Hf=(e,t,n)=>{let r=n.format==="NHWC"?"channelsLast":"channelsFirst",i=ha(n,t),s=n.autoPad==="NOTSET"?n.pads:n.autoPad,a=Tb(t[0].dims,t[1].dims,n.strides,n.dilations,s,!1,r);e.compute(Ib(t,i,a.outShape,[a.filterDepth,a.filterHeight,a.filterWidth],[a.padInfo.front,a.padInfo.top,a.padInfo.left],r))},mu=(e,t)=>{if(Vf(e.inputs,t),e.inputs[0].dims.length===3)Ff(e,t);else if(e.inputs[0].dims.length===5)Hf(e,e.inputs,t);else{let n=ha(t,e.inputs);fl(e,e.inputs,n)}}}),kb,M$=re(()=>{Te(),nr(),Ce(),Ee(),kb=(e,t,n)=>{let r=e.length>2,i=t.outputShape,s=t.format==="NHWC",a=t.group,o=e[1].dims,l=o[2]/a,d=o[3],p=s?ct(l):1,f=s&&d===1&&l>=4,c=f?Math.floor(l/4)*4:Math.floor(l/p)*p,m=l-c,y=s?ct(d):1,w=s?d===1?p:y:1,$=W.size(i)/y,_=[Math.ceil($/64),1,1];Pe("verbose",()=>`[conv2d_backprop_webgpu] dispatch = ${_}`);let v=["rank","rank"],I=[t.strides[0],t.strides[1]],S=[t.kernelShape[s?1:2],t.kernelShape[s?2:3]],E=[t.dilations[0],t.dilations[1]],R=[S[0]+(t.dilations[0]<=1?0:(t.kernelShape[s?1:2]-1)*(t.dilations[0]-1)),S[1]+(t.dilations[1]<=1?0:(t.kernelShape[s?2:3]-1)*(t.dilations[1]-1))],O=[R[0]-1-Math.floor((t.pads[0]+t.pads[2])/2),R[1]-1-Math.floor((t.pads[1]+t.pads[3])/2)],x=[{type:12,data:$},{type:12,data:I},{type:12,data:S},{type:12,data:E},{type:12,data:R},{type:6,data:O},{type:12,data:c},{type:12,data:l},{type:12,data:d},..._e(e[0].dims,e[1].dims)];r&&(x.push(..._e(e[2].dims)),v.push("rank")),x.push(..._e(i));let U=q=>{let J=[{name:"output_size",type:"u32"},{name:"strides",type:"u32",length:I.length},{name:"filter_dims",type:"u32",length:S.length},{name:"dilations",type:"u32",length:S.length},{name:"effective_filter_dims",type:"u32",length:R.length},{name:"pads",type:"i32",length:O.length},{name:"input_channels_per_group_int",type:"u32"},{name:"input_channels_per_group",type:"u32"},{name:"output_channels_per_group",type:"u32"}],se=xt(e[0].dataType),ce=s?1:2,L=s?2:3,ue=s?3:1,ie=V("W",e[1].dataType,e[1].dims.length,w),te=V("Dy",e[0].dataType,e[0].dims.length,p),he=[te,ie];r&&he.push(V("bias",e[2].dataType,[i[ue]].length,y));let Q=be("result",e[0].dataType,i.length,y),ve=()=>{let ee="";if(f)p===4?ee+=` + let xValue = ${te.getByOffset("x_offset")}; + let wValue = ${ie.getByOffset("w_offset")}; + dotProd = dotProd + dot(xValue, wValue); + x_offset += 1u; + w_offset += 1u;`:p===2?ee+=` + dotProd = dotProd + dot(vec4<${se}>(${te.getByOffset("x_offset")}, ${te.getByOffset("x_offset + 1u")}), vec4<${se}>(${ie.getByOffset("w_offset")}, ${ie.getByOffset("w_offset + 1u")})); + x_offset += 2u; + w_offset += 2u;`:p===1&&(ee+=` + dotProd = dotProd + dot(vec4<${se}>(${te.getByOffset("x_offset")}, ${te.getByOffset("x_offset + 1u")}, ${te.getByOffset("x_offset + 2u")}, ${te.getByOffset("x_offset + 3u")}), vec4<${se}>(${ie.getByOffset("w_offset")}, ${ie.getByOffset("w_offset + 1u")}, ${ie.getByOffset("w_offset + 2u")}, ${ie.getByOffset("w_offset + 3u")})); + x_offset += 4u; + w_offset += 4u;`);else if(ee+=` + let xValue = ${s?te.getByOffset(`${te.indicesToOffset(`${te.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${p}`):te.get("batch","inputChannel","idyR","idyC")}; + `,p===1)ee+=` + let w_offset = ${ie.indicesToOffset(`${ie.type.indices}(u32(wRPerm), u32(wCPerm), inputChannel, wOutChannel)`)}; + let wValue = ${ie.getByOffset(`w_offset / ${w}`)}; + dotProd = dotProd + xValue * wValue;`;else for(let ne=0;ne{if(m===0)return"";if(!f)throw new Error(`packInputAs4 ${f} is not true.`);let ee="";if(p===1){ee+="dotProd = dotProd";for(let ne=0;ne(i32(r), i32(c)) - uniforms.pads; + let dyRCorner = dyCorner.x; + let dyCCorner = dyCorner.y; + let groupId = d1 / uniforms.output_channels_per_group; + let wOutChannel = d1 - groupId * uniforms.output_channels_per_group; + // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1). + // ? = to be determined. : = across all values in that axis. + var dotProd = ${Q.type.value}(0.0); + var wR: u32 = 0; + if (uniforms.dilations.x == 1) { + // Minimum wR >= 0 that satisfies (dyRCorner + wR) % (uniforms.strides.x) == 0 + wR = u32(((dyRCorner + i32(uniforms.strides.x) - 1) / i32(uniforms.strides.x)) * i32(uniforms.strides.x) - dyRCorner); + } + for (; wR < uniforms.effective_filter_dims.x; wR = wR + 1) { + if (wR % uniforms.dilations.x != 0) { + continue; + } + let dyR = (${se}(dyRCorner) + ${se}(wR)) / ${se}(uniforms.strides[0]); + let wRPerm = uniforms.filter_dims.x - 1 - wR / uniforms.dilations.x; + if (dyR < 0.0 || dyR >= ${se}(uniforms.Dy_shape[${ce}]) || fract(dyR) > 0.0 || + wRPerm < 0) { + continue; + } + let idyR: u32 = u32(dyR); + var wC: u32 = 0; + if (uniforms.dilations.y == 1) { + // Minimum wC >= 0 that satisfies (dyCCorner + wC) % (uniforms.strides.y) == 0 + wC = u32(((dyCCorner + i32(uniforms.strides.y) - 1) / i32(uniforms.strides.y)) * i32(uniforms.strides.y) - dyCCorner); + } + for (; wC < uniforms.effective_filter_dims.y; wC = wC + 1) { + if (wC % uniforms.dilations.y != 0) { + continue; + } + let dyC = (${se}(dyCCorner) + ${se}(wC)) / ${se}(uniforms.strides.y); + let wCPerm = uniforms.filter_dims.y - 1 - wC / uniforms.dilations.y; + if (dyC < 0.0 || dyC >= ${se}(uniforms.Dy_shape[${L}]) || + fract(dyC) > 0.0 || wCPerm < 0) { + continue; + } + let idyC: u32 = u32(dyC); + var inputChannel = groupId * uniforms.input_channels_per_group; + ${f?` + var x_offset = ${te.indicesToOffset(`${te.type.indices}(batch, idyR, idyC, inputChannel)`)} / ${p}; + var w_offset = ${ie.indicesToOffset(`${ie.type.indices}(wRPerm, wCPerm, inputChannel, wOutChannel)`)} / ${w}; + `:""} + for (var d2: u32 = 0; d2 < uniforms.input_channels_per_group_int; d2 = d2 + ${f?4:p}) { + ${ve()} + inputChannel = inputChannel + ${f?4:p}; + } + ${Y()} + wC = wC + uniforms.strides.y - 1; + } + wR = wR + uniforms.strides[0] - 1; + } + let value = dotProd${r?` + bias[d1 / ${y}]`:""}; + ${Q.setByOffset("global_idx","value")}; + `;return` + ${q.registerUniforms(J).declareVariables(...he,Q)} + ${q.mainStart()} + ${q.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")}; + ${P}}`};return{name:"ConvTranspose2D",shaderCache:{hint:`${t.cacheKey};${p}${w}${y}${f}${m}`,inputDependencies:v},getRunData:()=>({dispatchGroup:{x:_[0],y:_[1],z:_[2]},outputs:[{dims:n?n(i):i,dataType:e[0].dataType}],programUniforms:x}),getShaderSource:U}}}),jf,Kf,Xf,hl,zb,Yf,ml,Zf,Ab,R$=re(()=>{M$(),ti(),_r(),jf=(e,t,n,r,i,s)=>(e-1)*t+n+(r-1)*i+1-s,Kf=(e,t,n,r,i)=>{let s=Math.floor(e/2);t==="SAME_UPPER"?(n[r]=s,n[i]=e-s):t==="SAME_LOWER"&&(n[r]=e-s,n[i]=s)},Xf=(e,t,n,r,i,s,a,o,l,d)=>{let p=e.length-2,f=d.length===0;l.length{let n=e.kernelShape.slice();if(e.kernelShape.length===0||e.kernelShape.reduce((f,c)=>f*c,1)===0){n.length=0;for(let f=2;ff+c,0)===0){let f=t[0].dims.length-2;l=new Array(f).fill(1)}let d=e.strides.slice();if(d.reduce((f,c)=>f+c,0)===0){let f=t[0].dims.length-2;d=new Array(f).fill(1)}Xf(o,n,l,e.autoPad,e.group,i,d,r,a,s);let p=Object.assign({},e);return Object.assign(p,{kernelShape:n,pads:i,outputPadding:a,outputShape:s,dilations:l,strides:d}),p},zb=e=>{let t=td(e),n=e.format,r=["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][typeof e.autoPad>"u"?0:e.autoPad],i=e.dilations,s=e.group??1,a=e.kernelShape,o=e.pads,l=e.strides,d=e.wIsConst(),p=e.outputPadding,f=e.outputShape;return{autoPad:r,format:n,dilations:i,group:s,kernelShape:a,outputPadding:p,outputShape:f,pads:o,strides:l,wIsConst:d,...t,cacheKey:`${e.format};${t.activation};`}},Yf=(e,t)=>{if(!e||e.length!==2&&e.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(e[0].dims.length!==4&&e[0].dims.length!==3)throw new Error("currently only support 2-dimensional conv");if(e[0].dims.length!==e[1].dims.length)throw new Error("filter does not have same dimension as input");let n=e[0].dims[t.format==="NHWC"?e[0].dims.length-1:1],r=e[1].dims[0];if(n!==r)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let i=e[1].dims[1]*t.group;if(e.length===3&&(e[2].dims.length!==1||e[2].dims[0]!==i))throw new Error("invalid bias");let s=e[0].dims.length-2;if(t.dilations.reduce((a,o)=>a+o,0)>0&&t.dilations.length!==s)throw new Error(`dilations should be ${s}D`);if(t.strides.reduce((a,o)=>a+o,0)>0&&t.strides.length!==s)throw new Error(`strides should be ${s}D`);if(t.pads.reduce((a,o)=>a+o,0)>0&&t.pads.length!==s*2)throw new Error(`pads should be ${s*2}D`);if(t.outputPadding.length!==s&&t.outputPadding.length!==0)throw new Error(`output_padding should be ${s}D`);if(t.kernelShape.reduce((a,o)=>a+o,0)>0&&t.kernelShape.length!==0&&t.kernelShape.length!==e[1].dims.length-2)throw new Error("invalid kernel shape");if(t.outputShape.length!==0&&t.outputShape.length!==e[0].dims.length-2)throw new Error("invalid output shape")},ml=(e,t,n,r)=>{let i=e.kernelCustomData.wT??e.compute(en(t[1],[2,3,0,1]),{inputs:[1],outputs:[n.wIsConst?-2:-1]})[0];n.wIsConst&&!e.kernelCustomData.wT&&(e.kernelCustomData.wT=i);let s=[t[0],i];t.length===3&&s.push(t[2]),e.compute(kb(s,n,r),{inputs:s})},Zf=(e,t)=>{let n=t.format==="NHWC",r=[e.inputs[0].reshape(n?[e.inputs[0].dims[0],1,e.inputs[0].dims[1],e.inputs[0].dims[2]]:[e.inputs[0].dims[0],e.inputs[0].dims[1],1,e.inputs[0].dims[2]]),e.inputs[1].reshape([e.inputs[1].dims[0],e.inputs[1].dims[1],1,e.inputs[1].dims[2]])];e.inputs.length===3&&r.push(e.inputs[2]);let i=t.kernelShape;(i.length===0||i[0]===0)&&(i=[e.inputs[1].dims[2]]);let s=t.dilations;(s.length===0||s[0]===0)&&(s=[1]);let a=t.strides;(a.length===0||a[0]===0)&&(a=[1]);let o=t.pads;o.length===0&&(o=[0,0]),o=[0,o[0],0,o[1]],a=[1].concat(a),s=[1].concat(s),i=[1].concat(i);let l=t.outputPadding;l=[0].concat(l);let d=hl({...t,pads:o,strides:a,dilations:s,kernelShape:i,outputPadding:l},r);ml(e,r,d,p=>n?[p[0],p[2],p[3]]:[p[0],p[1],p[3]])},Ab=(e,t)=>{if(Yf(e.inputs,t),e.inputs[0].dims.length===3)Zf(e,t);else{let n=hl(t,e.inputs);ml(e,e.inputs,n)}}}),Qf,Mb,Rb,O$=re(()=>{Te(),Ce(),pt(),Ee(),Qf=(e,t,n,r)=>{let i=W.size(t),s=t.length,a=V("input",e,s),o=be("output",e,s),l=n.dataType===6?n.getInt32Array()[0]:Number(n.getBigInt64Array()[0]),d=W.normalizeAxis(l,s),p=f=>{let c=` i32(${a.indicesGet("inputIndices","uniforms.axis")}) `,m=we("uniforms.input_shape","uniforms.axis",s),y=r.reverse?c+(r.exclusive?" + 1":""):"0",w=r.reverse?m:c+(r.exclusive?"":" + 1");return` + ${f.registerUniform("outputSize","u32").registerUniform("axis","u32").declareVariables(a,o)} + ${f.mainStart()} + ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var inputIndices = ${o.offsetToIndices("global_idx")}; + var sum = ${o.type.value}(0); + let first : i32 = ${y}; + let last : i32 = ${w}; + for (var i : i32 = first; i < last; i++) { + ${a.indicesSet("inputIndices","uniforms.axis","u32(i)")}; + sum = sum + ${a.getByIndices("inputIndices")}; + } + ${o.setByOffset("global_idx","sum")}; + }`};return{name:"CumSum",shaderCache:{hint:r.cacheKey,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:t,dataType:e}],dispatchGroup:{x:Math.ceil(i/64)},programUniforms:[{type:12,data:i},{type:12,data:d},..._e(t,t)]}),getShaderSource:p}},Mb=(e,t)=>{let n=e.inputs[0].dims,r=e.inputs[0].dataType,i=e.inputs[1];e.compute(Qf(r,n,i,t),{inputs:[0]})},Rb=e=>{let t=e.exclusive===1,n=e.reverse===1;return Fe({exclusive:t,reverse:n})}}),Jf,eh,th,Ob,Nb,N$=re(()=>{Te(),Ce(),pt(),Ee(),Jf=e=>{if(!e||e.length!==1)throw new Error("DepthToSpace requires 1 input.");if(e[0].dims.length!==4)throw new Error("DepthToSpace requires 4D input.")},eh=(e,t,n,r)=>{let i=[];i.push(`fn perm(i: ${r.type.indices}) -> ${n.type.indices} { + var a: ${n.type.indices};`);for(let s=0;s{let n,r,i,s,a,o,l=t.format==="NHWC",d=t.blocksize,p=t.mode==="DCR";l?([n,r,i,s]=e.dims,a=p?[n,r,i,d,d,s/d**2]:[n,r,i,s/d**2,d,d],o=p?[0,1,3,2,4,5]:[0,1,4,2,5,3]):([n,r,i,s]=[e.dims[0],e.dims[2],e.dims[3],e.dims[1]],a=p?[n,d,d,s/d**2,r,i]:[n,s/d**2,d,d,r,i],o=p?[0,3,4,1,5,2]:[0,1,4,2,5,3]);let f=e.reshape(a),c=f.dims.length,m=e.dataType,y=V("a",m,c),w=be("output",m,c),$=_=>` + ${_.registerUniform("output_size","u32").declareVariables(y,w)} + + ${eh(o,c,y,w)} + + ${_.mainStart()} + ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${w.offsetToIndices("global_idx")}; + let aIndices = perm(indices); + + ${w.setByOffset("global_idx",y.getByIndices("aIndices"))} + }`;return{name:"DepthToSpace",shaderCache:{hint:`${e.dims};${t.blocksize};${t.mode}`,inputDependencies:["rank"]},getRunData:_=>{let v=l?[n,r*d,i*d,s/d**2]:[n,s/d**2,r*d,i*d],I=W.size(v),S=f.dims,E=W.sortBasedOnPerm(S,o);return{outputs:[{dims:v,dataType:_[0].dataType}],dispatchGroup:{x:Math.ceil(I/64)},programUniforms:[{type:12,data:I},..._e(S,E)]}},getShaderSource:$}},Ob=(e,t)=>{Jf(e.inputs),e.compute(th(e.inputs[0],t))},Nb=e=>Fe({blocksize:e.blocksize,mode:e.mode,format:e.format})}),ma,ss,gl,nh,rh,ih,sh,yl,ah,Bb,Db,B$=re(()=>{Te(),Ce(),pt(),Ee(),ma="[a-zA-Z]|\\.\\.\\.",ss="("+ma+")+",gl="^"+ss+"$",nh="("+ss+",)*"+ss,rh="^"+nh+"$",ih=class{constructor(e=-1){this.symbolToIndices=new Map,this.inputIndex=e}addSymbol(e,t){let n=this.symbolToIndices.get(e);n===void 0?n=[t]:n.push(t),this.symbolToIndices.set(e,n)}},sh=class{constructor(e,t){var i;this.equation=t,this.hasEllipsis=!1,this.symbolToInfo=new Map,this.lhs=new Array,this.outputDims=[];let[n,r]=t.includes("->")?t.split("->",2):[t,""];if(!n.match(RegExp(rh)))throw new Error("Invalid LHS term");if(n.split(",").forEach((s,a)=>{let o=e[a].dims.slice();if(!s.match(RegExp(gl)))throw new Error("Invalid LHS term");let l=this.processTerm(s,!0,o,a);this.lhs.push(l)}),r==="")r+=[...this.symbolToInfo.entries()].filter(([s,a])=>a.count===1||s==="...").map(([s])=>s).join("");else if(!r.match(RegExp(ss)))throw new Error("Invalid RHS");(i=r.match(RegExp(ma,"g")))==null||i.forEach(s=>{if(s==="...")this.outputDims=this.outputDims.concat(this.ellipsisDims);else{let a=this.symbolToInfo.get(s);if(a===void 0)throw new Error("Invalid RHS symbol");this.outputDims.push(a.dimValue)}}),this.rhs=this.processTerm(r,!1,this.outputDims)}addSymbol(e,t,n){let r=this.symbolToInfo.get(e);if(r!==void 0){if(r.dimValue!==t&&r.count!==1)throw new Error("Dimension mismatch");r.count++,r.inputIndices.push(n)}else r={count:1,dimValue:t,inputIndices:[n]};this.symbolToInfo.set(e,r)}processTerm(e,t,n,r=-1){let i=n.length,s=!1,a=[],o=0;if(!e.match(RegExp(gl))&&!t&&e!=="")throw new Error("Invalid LHS term");let l=e.match(RegExp(ma,"g")),d=new ih(r);return l==null||l.forEach((p,f)=>{if(p==="..."){if(s)throw new Error("Only one ellipsis is allowed per input term");s=!0;let c=i-l.length+1;if(c<0)throw new Error("Ellipsis out of bounds");if(a=n.slice(o,o+c),this.hasEllipsis){if(this.ellipsisDims.length!==a.length||this.ellipsisDims.toString()!==a.toString())throw new Error("Ellipsis dimensions mismatch")}else if(t)this.hasEllipsis=!0,this.ellipsisDims=a;else throw new Error("Ellipsis must be specified in the LHS");for(let m=0;me+"_max",ah=(e,t,n,r)=>{let i=e.map(d=>d.length).map((d,p)=>V(`input${p}`,t,d)),s=W.size(r),a=be("output",t,r.length),o=[...n.symbolToInfo.keys()].filter(d=>!n.rhs.symbolToIndices.has(d)),l=d=>{let p=[],f="var prod = 1.0;",c="var sum = 0.0;",m="sum += prod;",y=[],w=[],$=[],_=[],v=n.symbolToInfo.size===n.rhs.symbolToIndices.size;n.symbolToInfo.forEach((S,E)=>{var R;if(n.rhs.symbolToIndices.has(E)){let O=(R=n.rhs.symbolToIndices.get(E))==null?void 0:R[0];O!==void 0&&n.lhs.forEach((x,U)=>{if(S.inputIndices.includes(U)){let q=x.symbolToIndices.get(E);if(q===void 0)throw new Error("Invalid symbol error");q.forEach(J=>{p.push(`${i[U].indicesSet(`input${U}Indices`,J,a.indicesGet("outputIndices",O))}`)})}})}else n.lhs.forEach((O,x)=>{if(S.inputIndices.includes(x)){let U=O.symbolToIndices.get(E);if(U===void 0)throw new Error("Invalid symbol error");U.forEach(q=>{y.push(`${i[x].indicesSet(`input${x}Indices`,q,`${E}`)}`)}),_.push(`prod *= ${i[x].getByIndices(`input${x}Indices`)};`)}}),w.push(`for(var ${E}: u32 = 0; ${E} < uniforms.${yl(E)}; ${E}++) {`),$.push("}")});let I=v?[...p,`let sum = ${i.map((S,E)=>S.getByIndices(`input${E}Indices`)).join(" * ")};`]:[...p,c,...w,...y,f,..._,m,...$];return` + ${d.registerUniforms(o.map(S=>({name:`${yl(S)}`,type:"u32"}))).registerUniform("outputSize","u32").declareVariables(...i,a)} + + ${d.mainStart()} + ${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + var outputIndices = ${a.offsetToIndices("global_idx")}; + ${i.map((S,E)=>`var input${E}Indices: ${i[E].type.indices};`).join(` +`)} + ${I.join(` +`)}; + ${a.setByOffset("global_idx","sum")}; + }`};return{name:"Einsum",shaderCache:{hint:n.equation,inputDependencies:e.map(()=>"rank")},getRunData:()=>{let d=o.filter(f=>n.symbolToInfo.has(f)).map(f=>{var c;return{type:12,data:((c=n.symbolToInfo.get(f))==null?void 0:c.dimValue)||0}});d.push({type:12,data:s});let p=e.map((f,c)=>[..._e(f)]).reduce((f,c)=>f.concat(c),d);return p.push(..._e(r)),{outputs:[{dims:r,dataType:t}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:p}},getShaderSource:l}},Bb=(e,t)=>{let n=new sh(e.inputs,t.equation),r=n.outputDims,i=e.inputs.map((s,a)=>s.dims);e.compute(ah(i,e.inputs[0].dataType,n,r))},Db=e=>{let t=e.equation.replace(/\s+/g,"");return Fe({equation:t})}}),oh,bl,lh,uh,Pb,D$=re(()=>{Te(),Ce(),Ee(),oh=e=>{if(!e||e.length!==2)throw new Error("Expand requires 2 input.");let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),r=n.length{let n=e.length-t.length,r=[];for(let i=0;ie.length>t.length?bl(e,t):bl(t,e),uh=e=>{let t=e[0].dims,n=Array.from(e[1].getBigInt64Array(),Number),r=lh(t,n),i=e[0].dataType,s=i===9||W.size(t)===1,a=i===9||t.length>0&&t[t.length-1]%4===0?4:1,o=s||r.length>0&&r[r.length-1]%4===0?4:1,l=Math.ceil(W.size(r)/o),d=f=>{let c=V("input",i,t.length,a),m=be("output",i,r.length,o),y;if(i===9){let w=($,_,v="")=>` + let outputIndices${_} = ${m.offsetToIndices(`outputOffset + ${_}u`)}; + let offset${_} = ${c.broadcastedIndicesToOffset(`outputIndices${_}`,m)}; + let index${_} = offset${_} / 4u; + let component${_} = offset${_} % 4u; + ${$}[${_}] = ${v}(${c.getByOffset(`index${_}`)}[component${_}]); + `;y=` + let outputOffset = global_idx * ${o}; + var data = vec4(0); + ${w("data",0,"u32")} + ${w("data",1,"u32")} + ${w("data",2,"u32")} + ${w("data",3,"u32")} + ${m.setByOffset("global_idx","data")} + }`}else y=` + let outputIndices = ${m.offsetToIndices(`global_idx * ${o}`)}; + let inputOffset = ${c.broadcastedIndicesToOffset("outputIndices",m)}; + let data = ${m.type.value}(${c.getByOffset(`inputOffset / ${a}`)}); + ${m.setByOffset("global_idx","data")} + }`;return` + ${f.registerUniform("vec_size","u32").declareVariables(c,m)} + ${f.mainStart()} + ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${y}`},p=[{type:12,data:l},..._e(t,r)];return{name:"Expand",shaderCache:{hint:`${r.length};${a}${o}`,inputDependencies:["rank"]},getShaderSource:d,getRunData:()=>({outputs:[{dims:r,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:p})}},Pb=e=>{oh(e.inputs),e.compute(uh(e.inputs),{inputs:[0]})}}),dh,Ub,P$=re(()=>{Te(),Ce(),Ee(),ed(),dh=e=>{let t=e[0].dataType,n=W.size(e[0].dims),r=W.size(e[1].dims),i=r%4===0,s=a=>{let o=V("x",t,[1],4),l=V("bias",t,[1],4),d=be("y",t,[1],4),p=[{name:"output_vec_size",type:"u32"},{name:"bias_size",type:"u32"}],f=m=>` + let bias${m}_offset: u32 = (global_idx * 4 + ${m}) % uniforms.bias_size; + let bias${m} = ${l.getByOffset(`bias${m}_offset / 4`)}[bias${m}_offset % 4];`,c=i?` + let bias = ${l.getByOffset("global_idx % (uniforms.bias_size / 4)")};`:`${f(0)}${f(1)}${f(2)}${f(3)} + let bias = ${o.type.value}(bias0, bias1, bias2, bias3);`;return`${a.registerUniforms(p).declareVariables(o,l,d)} + + ${du(Rt(t))} + + ${a.mainStart(ki)} + ${a.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_vec_size")} + + let x = ${o.getByOffset("global_idx")}; + ${c} + let x_in = x + bias; + ${d.setByOffset("global_idx",cu("x_in"))} + }`};return{name:"FastGeluWithBias",shaderCache:{hint:`${i}`,inputDependencies:["type","type"]},getShaderSource:s,getRunData:a=>({outputs:[{dims:a[0].dims,dataType:a[0].dataType}],programUniforms:[{type:12,data:Math.ceil(n/4)},{type:12,data:r}],dispatchGroup:{x:Math.ceil(n/ki/4)}})}},Ub=e=>{e.inputs.length<2||W.size(e.inputs[1].dims)===0?sb(e):e.compute(dh(e.inputs))}}),ch,ph,Lb,Wb,U$=re(()=>{Te(),Ce(),pt(),Ee(),ch=e=>{if(!e||e.length!==2)throw new Error("Gather requires 2 inputs.")},ph=(e,t)=>{let n=e[0].dims,r=e[1].dims,i=n.length,s=W.normalizeAxis(t.axis,i),a=n.slice(0);a.splice(s,1,...r);let o=n[s],l=e[0].dataType===9?4:1,d=Math.ceil(W.size(a)/l),p=[{type:12,data:d},{type:6,data:o},{type:12,data:s},..._e(e[0].dims,e[1].dims,a)],f=c=>{let m=V("data",e[0].dataType,e[0].dims.length,l),y=V("inputIndices",e[1].dataType,e[1].dims.length),w=be("output",e[0].dataType,a.length,l),$=v=>{let I=r.length,S=`var indicesIndices${v} = ${y.type.indices}(0);`;for(let E=0;E1?`indicesIndices${v}[${E}]`:`indicesIndices${v}`} = ${a.length>1?`outputIndices${v}[uniforms.axis + ${E}]`:`outputIndices${v}`};`;S+=` + var idx${v} = ${y.getByIndices(`indicesIndices${v}`)}; + if (idx${v} < 0) { + idx${v} = idx${v} + uniforms.axisDimLimit; + } + var dataIndices${v} : ${m.type.indices}; + `;for(let E=0,R=0;E1?`dataIndices${v}[${E}]`:`dataIndices${v}`} = u32(idx${v});`,R+=I):(S+=`${i>1?`dataIndices${v}[${E}]`:`dataIndices${v}`} = ${a.length>1?`outputIndices${v}[${R}]`:`outputIndices${v}`};`,R++);return S},_;if(e[0].dataType===9){let v=(I,S,E="")=>` + let outputIndices${S} = ${w.offsetToIndices(`outputOffset + ${S}u`)}; + ${$(S)}; + let offset${S} = ${m.indicesToOffset(`dataIndices${S}`)}; + let index${S} = offset${S} / 4u; + let component${S} = offset${S} % 4u; + ${I}[${S}] = ${E}(${m.getByOffset(`index${S}`)}[component${S}]); + `;_=` + let outputOffset = global_idx * ${l}; + var value = vec4(0); + ${v("value",0,"u32")} + ${v("value",1,"u32")} + ${v("value",2,"u32")} + ${v("value",3,"u32")} + ${w.setByOffset("global_idx","value")} + `}else _=` + let outputIndices = ${w.offsetToIndices("global_idx")}; + ${$("")}; + let value = ${m.getByIndices("dataIndices")}; + ${w.setByOffset("global_idx","value")}; + `;return` + ${c.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(m,y,w)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + ${_} + }`};return{name:"Gather",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:a,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:p}),getShaderSource:f}},Lb=e=>Fe({axis:e.axis}),Wb=(e,t)=>{let n=e.inputs;ch(n),e.compute(ph(e.inputs,t))}}),fh,qb,Gb,L$=re(()=>{Te(),Ce(),Ee(),fh=(e,t,n,r,i,s,a,o,l)=>{let d=[{type:12,data:s},{type:12,data:r},{type:12,data:i},{type:12,data:n},{type:12,data:a},{type:12,data:o},{type:12,data:l}],p=[s];d.push(..._e(t.dims,p));let f=c=>{let m=V("indices_data",t.dataType,t.dims.length),y=be("input_slice_offsets_data",12,1,1),w=[m,y],$=[{name:"output_size",type:"u32"},{name:"batch_dims",type:"u32"},{name:"input_dims",type:"u32",length:i.length},{name:"sizes_from_slice_dims_data",type:"u32",length:n.length},{name:"num_slices_per_batch",type:"u32"},{name:"input_batch_stride",type:"u32"},{name:"num_slice_dims",type:"u32"}];return` + ${c.registerUniforms($).declareVariables(...w)} + ${c.mainStart()} + ${c.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let batch_idx = global_idx / uniforms.num_slices_per_batch; + let base_offset = batch_idx * uniforms.input_batch_stride; + + let slice_indices_base_offset = global_idx * uniforms.num_slice_dims; + var relative_slice_offset = 0; + for (var dim_idx = 0u; dim_idx < uniforms.num_slice_dims; dim_idx ++) { + var index = i32(indices_data[dim_idx + slice_indices_base_offset].x); + let input_dim_idx = uniforms.batch_dims + dim_idx; + if (index < 0) { + ${i.length===1?"index += i32(uniforms.input_dims);":"index += i32(uniforms.input_dims[input_dim_idx]);"} + } + ${n.length===1?"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data);":"relative_slice_offset += index * i32(uniforms.sizes_from_slice_dims_data[dim_idx]);"} + } + + input_slice_offsets_data[global_idx] = base_offset + u32(relative_slice_offset); + }`};return e.compute({name:"computeSliceOffsets",shaderCache:{hint:`${i.length}_${n.length}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:p,dataType:e.inputs[1].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:d}),getShaderSource:f},{inputs:[t],outputs:[-1]})[0]},qb=(e,t)=>{let n=e.inputs,r=n[0].dims,i=n[0].dataType,s=n[1].dims,a=s[s.length-1],o=W.sizeToDimension(s,s.length-1),l=W.sizeFromDimension(r,t.batchDims+a),d=W.sizeToDimension(r,t.batchDims),p=W.sizeFromDimension(r,t.batchDims),f=o/d,c=new Array(a),m=l;for(let S=0;Sr.length)throw new Error("last dimension of indices must not be larger than rank of input tensor");let $=s.slice(0,-1).concat(r.slice(w)),_=W.size($),v=[{type:12,data:_},{type:12,data:l},..._e(n[0].dims,y.dims,$)],I=S=>{let E=V("data",n[0].dataType,n[0].dims.length),R=V("slice_offsets",12,y.dims.length),O=be("output",n[0].dataType,$.length);return` + ${S.registerUniform("output_size","u32").registerUniform("slice_size","u32").declareVariables(E,R,O)} + ${S.mainStart()} + ${S.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let slice_offset = slice_offsets[global_idx / uniforms.slice_size]; + output[global_idx] = data[u32(slice_offset) + global_idx % uniforms.slice_size]; + }`};e.compute({name:"GatherND",shaderCache:{hint:t.cacheKey,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:$,dataType:i}],dispatchGroup:{x:Math.ceil(_/64)},programUniforms:v}),getShaderSource:I},{inputs:[n[0],y]})},Gb=e=>({batchDims:e.batch_dims,cacheKey:""})}),hh,mh,Vb,Fb,W$=re(()=>{Te(),Ce(),pt(),Ee(),hh=(e,t)=>{if(e.length<3||e.length>4)throw new Error("GatherBlockQuantized requires 3 or 4 inputs.");let n=W.normalizeAxis(t.quantizeAxis,e[0].dims.length),r=t.blockSize,i=e[0],s=e[2],a=e.length===4?e[3]:void 0;if(s.dims.length!==i.dims.length||!i.dims.map((o,l)=>l===n?Math.ceil(o/r)===s.dims[l]:o===s.dims[l]).reduce((o,l)=>o&&l,!0))throw new Error("Scales must have the same rank as the input tensor and the dims should match except on gatherAxis.");if(a){if(a.dataType!==i.dataType)throw new Error("Zero point must have the same data type as the input tensor.");if(a.dims.length!==s.dims.length||!a.dims.map((o,l)=>o===s.dims[l]).reduce((o,l)=>o&&l,!0))throw new Error("Zero point must have the same rank as the input tensor and the dims should match except on quantizeAxis.")}},mh=(e,t)=>{let n=e[0].dims,r=e[1].dims,i=n.length,s=W.normalizeAxis(t.gatherAxis,i),a=W.normalizeAxis(t.quantizeAxis,i),o=n.slice(0);o.splice(s,1,...r);let l=W.size(o),d=e[2].dataType,p=e[0].dataType===22,f=[{type:12,data:l},{type:12,data:a},{type:12,data:s},{type:12,data:t.blockSize},..._e(...e.map((m,y)=>m.dims),o)],c=m=>{let y=V("data",e[0].dataType,e[0].dims.length),w=V("inputIndices",e[1].dataType,e[1].dims.length),$=V("scales",e[2].dataType,e[2].dims.length),_=e.length>3?V("zeroPoint",e[3].dataType,e[3].dims.length):void 0,v=be("output",d,o.length),I=[y,w,$];_&&I.push(_);let S=[{name:"output_size",type:"u32"},{name:"quantize_axis",type:"u32"},{name:"gather_axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${m.registerUniforms(S).declareVariables(...I,v)} + ${m.mainStart()} + let output_indices = ${v.offsetToIndices("global_idx")}; + var indices_indices = ${w.type.indices}(0); + ${r.length>1?` + for (var i: u32 = 0; i < ${r.length}; i++) { + let index = ${v.indicesGet("output_indices","uniforms.gather_axis + i")}; + ${w.indicesSet("indices_indices","i","index")}; + }`:`indices_indices = ${v.indicesGet("output_indices","uniforms.gather_axis")};`}; + var data_indices = ${y.type.indices}(0); + for (var i: u32 = 0; i < uniforms.gather_axis; i++) { + let index = ${v.indicesGet("output_indices","i")}; + ${y.indicesSet("data_indices","i","index")}; + } + var index_from_indices = ${w.getByIndices("indices_indices")}; + if (index_from_indices < 0) { + index_from_indices += ${n[s]}; + } + ${y.indicesSet("data_indices","uniforms.gather_axis","u32(index_from_indices)")}; + for (var i = uniforms.gather_axis + 1; i < ${o.length}; i++) { + let index = ${v.indicesGet("output_indices",`i + ${r.length} - 1`)}; + ${y.indicesSet("data_indices","i","index")}; + } + let data_offset = ${y.indicesToOffset("data_indices")}; + let data_index = data_offset % 8; + // Convert 4-bit packed data to 8-bit packed data. + let packed_4bit_quantized_data = ${y.getByOffset("data_offset / 8")}; + let packed_8bit_quantized_data = (packed_4bit_quantized_data >> (4 * (data_index % 2))) & 0x0f0f0f0f; + let quantized_data_vec = ${p?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_quantized_data)); + let quantized_data = quantized_data_vec[data_index / 2]; + var scale_indices = data_indices; + let quantize_axis_index = ${$.indicesGet("data_indices","uniforms.quantize_axis")} / uniforms.block_size; + ${$.indicesSet("scale_indices","uniforms.quantize_axis","quantize_axis_index")}; + var scale = ${$.getByIndices("scale_indices")}; + ${_?` + let zero_point_indices = scale_indices; + let zero_point_offset = ${_.indicesToOffset("zero_point_indices")}; + let zero_point_index = zero_point_offset % 8; + let packed_4bit_zero_points = ${_.getByOffset("zero_point_offset / 8")}; + let packed_8bit_zero_points = (packed_4bit_zero_points >> (4 * (zero_point_index % 2))) & 0x0f0f0f0f; + let zero_point_vec = ${p?"unpack4xI8":"unpack4xU8"}(u32(packed_8bit_zero_points)); + let zero_point = zero_point_vec[zero_point_index / 2];`:"var zero_point = 0"}; + let dequantized_data = ${Rt(d)}(quantized_data - zero_point) * scale; + ${v.setByOffset("global_idx","dequantized_data")}; + }`};return{name:"GatherBlockQuantized",shaderCache:{hint:`${t.cacheKey};${e.filter((m,y)=>y!==1).map(m=>m.dims.join("_")).join(";")}`,inputDependencies:Array.from({length:e.length},(m,y)=>"rank")},getRunData:()=>({outputs:[{dims:o,dataType:d}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:f}),getShaderSource:c}},Vb=(e,t)=>{let n=e.inputs;hh(n,t),e.compute(mh(e.inputs,t))},Fb=e=>Fe({blockSize:e.blockSize,gatherAxis:e.gatherAxis,quantizeAxis:e.quantizeAxis})}),gh,yh,Hb,jb,q$=re(()=>{Te(),Ce(),pt(),Ee(),gh=e=>{if(!e||e.length!==2)throw new Error("GatherElements requires 2 inputs.");if(e[0].dims.length<1)throw new Error("GatherElements requires that the data input be rank >= 1.");if(e[0].dims.length!==e[1].dims.length)throw new Error(`GatherElements requires that the data input and + indices input tensors be of same rank.`)},yh=(e,t)=>{let n=e[0].dims,r=e[0].dataType,i=n.length,s=e[1].dims,a=e[1].dataType,o=W.normalizeAxis(t.axis,i),l=n[o],d=s.slice(0),p=W.size(d),f=V("input",r,i),c=V("indicesInput",a,s.length),m=be("output",r,d.length),y=[{type:12,data:p},{type:6,data:l},{type:12,data:o}];return y.push(..._e(n,s,d)),{name:"GatherElements",shaderCache:{inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:d,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:y}),getShaderSource:w=>` + ${w.registerUniform("outputSize","u32").registerUniform("axisDimLimit","i32").registerUniform("axis","u32").declareVariables(f,c,m)} + ${w.mainStart()} + ${w.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + let outputIndices = ${m.offsetToIndices("global_idx")}; + + var idx = ${c.getByOffset("global_idx")}; + if (idx < 0) { + idx = idx + uniforms.axisDimLimit; + } + var inputIndices = ${f.type.indices}(outputIndices); + ${f.indicesSet("inputIndices","uniforms.axis","u32(idx)")}; + let value = ${f.getByIndices("inputIndices")}; + + ${m.setByOffset("global_idx","value")}; + }`}},Hb=e=>Fe({axis:e.axis}),jb=(e,t)=>{let n=e.inputs;gh(n),e.compute(yh(e.inputs,t))}}),bh,wh,Kb,Xb,G$=re(()=>{Te(),Ce(),Ee(),bh=e=>{if(!e)throw new Error("Input is missing");if(e.length<2||e.length>3)throw new Error("Invaid input number.");if(e.length===3&&e[2].dims.length>2)throw new Error("Invalid input shape of C");if(e[0].dataType!==e[1].dataType||e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("Input types are mismatched")},wh=(e,t)=>{let n=e[0].dims.slice(),r=e[1].dims.slice(),[i,s,a]=H0.getShapeOfGemmResult(n,t.transA,r,t.transB,e.length===3?e[2].dims:void 0),o=[i,s];if(!o)throw new Error("Can't use gemm on the given tensors");let l=16,d=Math.ceil(s/l),p=Math.ceil(i/l),f=!0,c=W.size(o),m=[{type:12,data:f?d:c},{type:12,data:i},{type:12,data:s},{type:12,data:a},{type:1,data:t.alpha},{type:1,data:t.beta}],y=["type","type"];e.length===3&&(m.push(..._e(e[2].dims)),y.push("rank")),m.push(..._e(o));let w=_=>{let v="";t.transA&&t.transB?v="value += a[k * uniforms.M + m] * b[n * uniforms.K + k];":t.transA&&!t.transB?v="value += a[k * uniforms.M + m] * b[k * uniforms.N + n];":!t.transA&&t.transB?v="value += a[m * uniforms.K + k] * b[n * uniforms.K + k];":!t.transA&&!t.transB&&(v="value += a[m * uniforms.K + k] * b[k * uniforms.N + n];");let I=t.alpha===1?"":"value *= uniforms.alpha;",S=V("a",e[0].dataType,e[0].dims),E=V("b",e[1].dataType,e[1].dims),R=S.type.value,O=null,x=[S,E];e.length===3&&(O=V("c",e[2].dataType,e[2].dims.length),x.push(O));let U=be("output",e[0].dataType,o.length);x.push(U);let q=[{name:"output_size",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}];return` + ${_.registerUniforms(q).declareVariables(...x)} + + ${_.mainStart()} + ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let m = global_idx / uniforms.N; + let n = global_idx % uniforms.N; + + var value = ${R}(0); + for (var k: u32 = 0u; k < uniforms.K; k++) { + ${v} + } + + ${I} + ${O!=null?`let cOffset = ${O.broadcastedIndicesToOffset("vec2(m, n)",U)}; value += ${R}(uniforms.beta) * ${O.getByOffset("cOffset")};`:""} + output[global_idx] = value; + }`},$=_=>{let v=V("a",e[0].dataType,e[0].dims),I=V("b",e[1].dataType,e[1].dims),S=null,E=[v,I];e.length===3&&(S=V("c",e[2].dataType,e[2].dims.length),E.push(S));let R=be("output",e[0].dataType,o.length);E.push(R);let O=[{name:"num_tile_n",type:"u32"},{name:"M",type:"u32"},{name:"N",type:"u32"},{name:"K",type:"u32"},{name:"alpha",type:"f32"},{name:"beta",type:"f32"}],x="",U="";t.transA&&t.transB?(U=` + var col = tile_row_start + local_id.x; + var row = k_start + local_id.y; + if (col < uniforms.M && row < uniforms.K) { + tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col]; + } else { + tile_a[local_id.y][local_id.x] = ${v.type.value}(0); + } + + col = k_start + local_id.x; + row = tile_col_start + local_id.y; + if (col < uniforms.K && row < uniforms.N) { + tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col]; + } else { + tile_b[local_id.y][local_id.x] = ${I.type.value}(0); + } + `,x="value += tile_a[k][local_id.y] * tile_b[local_id.x][k];"):t.transA&&!t.transB?(U=` + var col = tile_row_start + local_id.x; + var row = k_start + local_id.y; + if (col < uniforms.M && row < uniforms.K) { + tile_a[local_id.y][local_id.x] = a[row * uniforms.M + col]; + } else { + tile_a[local_id.y][local_id.x] = ${v.type.value}(0); + } + + col = tile_col_start + local_id.x; + row = k_start + local_id.y; + if (col < uniforms.N && row < uniforms.K) { + tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col]; + } else { + tile_b[local_id.y][local_id.x] = ${I.type.value}(0); + } + `,x="value += tile_a[k][local_id.y] * tile_b[k][local_id.x];"):!t.transA&&t.transB?(U=` + var col = k_start + local_id.x; + var row = tile_row_start + local_id.y; + if (col < uniforms.K && row < uniforms.M) { + tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col]; + } else { + tile_a[local_id.y][local_id.x] = ${v.type.value}(0); + } + + col = k_start + local_id.x; + row = tile_col_start + local_id.y; + if (col < uniforms.K && row < uniforms.N) { + tile_b[local_id.y][local_id.x] = b[row * uniforms.K + col]; + } else { + tile_b[local_id.y][local_id.x] = ${I.type.value}(0); + } + `,x="value += tile_a[local_id.y][k] * tile_b[local_id.x][k];"):!t.transA&&!t.transB&&(U=` + var col = k_start + local_id.x; + var row = tile_row_start + local_id.y; + if (col < uniforms.K && row < uniforms.M) { + tile_a[local_id.y][local_id.x] = a[row * uniforms.K + col]; + } else { + tile_a[local_id.y][local_id.x] = ${v.type.value}(0); + } + + col = tile_col_start + local_id.x; + row = k_start + local_id.y; + if (col < uniforms.N && row < uniforms.K) { + tile_b[local_id.y][local_id.x] = b[row * uniforms.N + col]; + } else { + tile_b[local_id.y][local_id.x] = ${I.type.value}(0); + } + `,x="value += tile_a[local_id.y][k] * tile_b[k][local_id.x];");let q=t.alpha===1?"":"value *= uniforms.alpha;";return` + ${_.registerUniforms(O).declareVariables(...E)} + var tile_a: array, ${l}>; + var tile_b: array, ${l}>; + ${_.mainStart([l,l,1])} + let tile_col_start = (workgroup_index % uniforms.num_tile_n) * ${l}; + let tile_row_start = (workgroup_index / uniforms.num_tile_n) * ${l}; + let num_tiles = (uniforms.K - 1) / ${l} + 1; + var k_start = 0u; + var value = ${R.type.value}(0); + for (var t: u32 = 0u; t < num_tiles; t++) { + ${U} + k_start = k_start + ${l}; + workgroupBarrier(); + + for (var k: u32 = 0u; k < ${l}; k++) { + ${x} + } + workgroupBarrier(); + } + + ${q} + let m = tile_row_start + local_id.y; + let n = tile_col_start + local_id.x; + ${S!=null?`let cOffset = ${S.broadcastedIndicesToOffset("vec2(m, n)",R)}; value += ${R.type.value}(uniforms.beta) * ${S.getByOffset("cOffset")};`:""} + if (m < uniforms.M && n < uniforms.N) { + output[m * uniforms.N + n] = value; + } + }`};return f?{name:"GemmShared",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:d*p},programUniforms:m}),getShaderSource:$}:{name:"Gemm",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:o,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:m}),getShaderSource:w}},Kb=e=>{let t=e.transA,n=e.transB,r=e.alpha,i=e.beta;return{transA:t,transB:n,alpha:r,beta:i,cacheKey:`${e.transA};${e.transB};${e.alpha===1}`}},Xb=(e,t)=>{bh(e.inputs),e.compute(wh(e.inputs,t))}}),In,Fn,Nr,Br,_h,vh,xh,$h,Sh,Th,Ih,Ch,Yb,Zb,V$=re(()=>{Te(),Ce(),pt(),Ee(),[In,Fn,Nr,Br]=[0,1,2,3],_h=e=>{if(e[0].dims.length!==4)throw new Error("only 4-D tensor is supported.");if(e[0].dims.length!==e[1].dims.length)throw new Error("input dimensions must be equal to grid dimensions");if(e[0].dims.length-2!==e[1].dims[e[1].dims.length-1])throw new Error(`last dimension of grid must be equal to ${e[0].dims.length-2}`);if(e[0].dims[0]!==e[1].dims[0])throw new Error("grid batch size must match input batch size")},vh=` + fn gs_get_cubic_coeffs(x: f32) -> vec4 { + let cubic_alpha = -0.75f; + let x_abs = abs(x); + var coeffs: vec4; + coeffs[0] = (((cubic_alpha * (x_abs + 1) - 5 * cubic_alpha) * (x_abs + 1) + 8 * cubic_alpha) * (x_abs + 1) - 4 * cubic_alpha); + coeffs[1] = (((cubic_alpha + 2) * x_abs - (cubic_alpha + 3)) * x_abs * x_abs + 1); + coeffs[2] = (((cubic_alpha + 2) * (1 - x_abs) - (cubic_alpha + 3)) * (1 - x_abs) * (1 - x_abs) + 1); + coeffs[3] = (((cubic_alpha * (2 - x_abs) - 5 * cubic_alpha) * (2 - x_abs) + 8 * cubic_alpha) * (2 - x_abs) - 4 * cubic_alpha); + return coeffs; + } +`,xh=e=>` + fn gs_bicubic_interpolate(p: mat4x4<${e}>, x: f32, y: f32) -> ${e} { + var v: vec4; + var coeffs = gs_get_cubic_coeffs(x); + for (var i = 0; i < 4; i++) { + v[i] = coeffs[0] * p[i][0] + coeffs[1] * p[i][1] + coeffs[2] * p[i][2] + coeffs[3] * p[i][3]; + } + coeffs = gs_get_cubic_coeffs(y); + let pixel = ${e}(coeffs[0] * v[0] + coeffs[1] * v[1] + coeffs[2] * v[2] + coeffs[3] * v[3]); + return pixel; + } +`,$h=e=>` + fn gs_denormalize(n: f32, length: i32) -> f32 { + ${e.alignCorners===0?` + // alignCorners: false => [-1, 1] to [-0.5, length - 0.5] + return ((n + 1.0) * f32(length) - 1.0) / 2.0; + `:` + // alignCorners: true => [-1, 1] to [0, length - 1] + return (n + 1.0) / 2.0 * (f32(length - 1)); + `} + } +`,Sh=e=>` + ${e.paddingMode==="reflection"?` + fn gs_reflect(x: i32, x_min: f32, x_max: f32) -> u32 { + var dx = 0.0; + var fx = f32(x); + let range = x_max - x_min; + if (fx < x_min) { + dx = x_min - fx; + let n = u32(dx / range); + let r = dx - f32(n) * range; + if (n % 2 == 0) { + fx = x_min + r; + } else { + fx = x_max - r; + } + } else if (fx > x_max) { + dx = fx - x_max; + let n = u32(dx / range); + let r = dx - f32(n) * range; + if (n % 2 == 0) { + fx = x_max - r; + } else { + fx = x_min + r; + } + } + return u32(fx); + }`:""} +`,Th=(e,t,n)=>` + fn pixel_at_grid(r: i32, c: i32, H: i32, W: i32, batch: u32, channel: u32, border: vec4) -> ${t} { + var pixel = ${t}(0); + var indices = vec4(0); + indices[${In}] = batch; + indices[${Fn}] = channel;`+(()=>{switch(n.paddingMode){case"zeros":return` + if (r >= 0 && r < H && c >=0 && c < W) { + indices[${Nr}] = u32(r); + indices[${Br}] = u32(c); + } else { + return ${t}(0); + } + `;case"border":return` + indices[${Nr}] = u32(clamp(r, 0, H - 1)); + indices[${Br}] = u32(clamp(c, 0, W - 1)); + `;case"reflection":return` + indices[${Nr}] = gs_reflect(r, border[1], border[3]); + indices[${Br}] = gs_reflect(c, border[0], border[2]); + `;default:throw new Error(`padding mode ${n.paddingMode} is not supported`)}})()+` + return ${e.getByIndices("indices")}; + } +`,Ih=(e,t,n)=>(()=>{switch(n.mode){case"nearest":return` + let result = pixel_at_grid(i32(round(y)), i32(round(x)), H_in, W_in, indices[${In}], indices[${Fn}], border); + `;case"bilinear":return` + let x1 = i32(floor(x)); + let y1 = i32(floor(y)); + let x2 = x1 + 1; + let y2 = y1 + 1; + + let p11 = pixel_at_grid(y1, x1, H_in, W_in, indices[${In}], indices[${Fn}], border); + let p12 = pixel_at_grid(y1, x2, H_in, W_in, indices[${In}], indices[${Fn}], border); + let p21 = pixel_at_grid(y2, x1, H_in, W_in, indices[${In}], indices[${Fn}], border); + let p22 = pixel_at_grid(y2, x2, H_in, W_in, indices[${In}], indices[${Fn}], border); + + let dx2 = ${t}(f32(x2) - x); + let dx1 = ${t}(x - f32(x1)); + let dy2 = ${t}(f32(y2) - y); + let dy1 = ${t}(y - f32(y1)); + let result = dy2 * (dx2 * p11 + dx1 * p12) + dy1 * (dx2 * p21 + dx1 * p22); + `;case"bicubic":return` + let x0 = i32(floor(x)) - 1; + let y0 = i32(floor(y)) - 1; + var p: mat4x4<${t}>; + for (var h = 0; h < 4; h++) { + for (var w = 0; w < 4; w++) { + p[h][w] = pixel_at_grid(h + y0, w + x0, H_in, W_in, indices[${In}], indices[${Fn}], border); + } + } + + let dx = x - f32(x0 + 1); + let dy = y - f32(y0 + 1); + let result = gs_bicubic_interpolate(p, dx, dy); + `;default:throw new Error(`mode ${n.mode} is not supported`)}})()+`${e.setByOffset("global_idx","result")}`,Ch=(e,t)=>{let n=V("x",e[0].dataType,e[0].dims.length),r=[e[1].dims[0],e[1].dims[1],e[1].dims[2]],i=V("grid",e[1].dataType,r.length,2),s=[e[0].dims[0],e[0].dims[1],e[1].dims[1],e[1].dims[2]];t.format==="NHWC"&&(s=[e[0].dims[0],e[1].dims[1],e[1].dims[2],e[0].dims[3]],[In,Fn,Nr,Br]=[0,3,1,2]);let a=be("output",e[0].dataType,s.length),o=n.type.value,l=W.size(s),d=[{type:12,data:l},..._e(e[0].dims,r,s)],p=f=>` + ${f.registerUniform("output_size","u32").declareVariables(n,i,a)} + ${vh} + ${xh(o)} + ${$h(t)} + ${Sh(t)} + ${Th(n,o,t)} + + ${f.mainStart()} + ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let H_in = i32(uniforms.x_shape[${Nr}]); + let W_in = i32(uniforms.x_shape[${Br}]); + + ${t.alignCorners===0?` + let x_min = -0.5; + let x_max = f32(W_in) - 0.5; + let y_min = -0.5; + let y_max = f32(H_in) - 0.5; + `:` + let x_min = 0.0; + let x_max = f32(W_in) - 1.0; + let y_min = 0.0; + let y_max = f32(H_in) - 1.0; + `}; + let border = vec4(x_min, y_min, x_max, y_max); + + let indices = ${a.offsetToIndices("global_idx")}; + var grid_indices = vec3(indices[${In}], indices[${Nr}], indices[${Br}]); + let nxy = ${i.getByIndices("grid_indices")}; + var x = gs_denormalize(f32(nxy[0]), W_in); + var y = gs_denormalize(f32(nxy[1]), H_in); + + ${Ih(a,o,t)} + }`;return{name:"GridSample",shaderCache:{hint:`${t.cacheKey}`,inputDependencies:["type","type"]},getRunData:f=>{let c=W.size(s);return{outputs:[{dims:s,dataType:f[0].dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:d}},getShaderSource:p}},Yb=(e,t)=>{_h(e.inputs),e.compute(Ch(e.inputs,t))},Zb=e=>Fe({alignCorners:e.align_corners,mode:e.mode,paddingMode:e.padding_mode,format:e.format})}),Pt,Eh,Qb,wl,kh,hs,Jb,ew=re(()=>{Te(),Ce(),pt(),Yu(),Ju(),Ee(),_r(),Pt=(e,t)=>e.length>t&&e[t].dims.length>0?e[t]:void 0,Eh=(e,t)=>{let n=e[0],r=Pt(e,1),i=Pt(e,2),s=Pt(e,3),a=Pt(e,4),o=Pt(e,5),l=Pt(e,6),d=Pt(e,7);if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let p=n.dims[0],f=n.dims[1],c=n.dims.length===3?n.dims[2]:t.numHeads*n.dims[4],m=f,y=0,w=0,$=Math.floor(c/t.numHeads);if(l&&d&&W.size(l.dims)&&W.size(d.dims)){if(l.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(l.dims[0]!==p||l.dims[1]!==t.numHeads||l.dims[3]!==$)throw new Error('Input "past_key" shape (batch_size, num_heads, past_sequence_length, head_size)');if(d.dims[0]!==p||d.dims[1]!==t.numHeads||d.dims[3]!==$)throw new Error('Input "past_value" shape (batch_size, num_heads, past_sequence_length, head_size)');if(l.dims[2]!==d.dims[2])throw new Error('Input "past_key" and "past_value" shall have same dim 2 (past_sequence_length)');if(d.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');y=l.dims[2],w=l.dims[2]}else if(l&&W.size(l.dims)||d&&W.size(d.dims))throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let _;if(r&&W.size(r.dims)>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(r.dims.length<3||r.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==r.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(r.dims.length===3){if(r.dims[2]!==n.dims[2])throw new Error('Input "query" and "key" shall have same dim 2 (hidden_size)');_=2,m=r.dims[1]}else if(r.dims.length===5){if(r.dims[2]!==t.numHeads||r.dims[3]!==2||r.dims[4]!==$)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(i)throw new Error('Expect "value" be none when "key" has packed kv format.');_=5,m=r.dims[1]}else{if(r.dims[1]!==t.numHeads||r.dims[3]!==$)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');_=0,m=r.dims[2]}}else{if(n.dims.length!==5)throw new Error('Input "query" is expected to have 5 dimensions when key is empty');if(n.dims[2]!==t.numHeads||n.dims[3]!==3)throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');_=3}if(s&&W.size(s.dims)>0){if(s.dims.length!==1)throw new Error('Input "bias" is expected to have 1 dimension');if(r&&r.dims.length===5&&r.dims[3]===2)throw new Error("bias is not allowed for packed kv.")}let v=y+m,I=0;if(a&&W.size(a.dims)>0){I=8;let O=a.dims;throw O.length===1?O[0]===p?I=1:O[0]===3*p+2&&(I=3):O.length===2&&O[0]===p&&O[1]===v&&(I=5),I===8?new Error('Input "key_padding_mask" shape shall be (batch_size) or (batch_size, total_sequence_length)'):new Error("Mask not supported")}let S=!1,E=c;if(i&&W.size(i.dims)>0){if(i.dims.length!==3&&i.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.dims[0]!==i.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(i.dims.length===3){if(m!==i.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');E=i.dims[2]}else{if(m!==i.dims[2])throw new Error('Input "key" and "value" shall have the same dim 2 (kv_sequence_length)');E=i.dims[1]*i.dims[3],S=!0}}let R=!1;if(a&&W.size(a.dims)>0)throw new Error("Key padding mask is not supported");if(o&&W.size(o.dims)>0){if(o.dims.length!==4)throw new Error('Input "attention_bias" is expected to have 4 dimensions');if(o.dims[0]!==p||o.dims[1]!==t.numHeads||o.dims[2]!==f||o.dims[3]!==v)throw new Error('Expect "attention_bias" shape (batch_size, num_heads, sequence_length, total_sequence_length)')}return{batchSize:p,sequenceLength:f,pastSequenceLength:y,kvSequenceLength:m,totalSequenceLength:v,maxSequenceLength:w,inputHiddenSize:0,hiddenSize:c,vHiddenSize:E,headSize:$,vHeadSize:Math.floor(E/t.numHeads),numHeads:t.numHeads,isUnidirectional:!1,pastPresentShareBuffer:!1,maskFilterValue:t.maskFilterValue,maskType:I,scale:t.scale,broadcastResPosBias:R,passPastInKv:S,qkvFormat:_}},Qb=e=>Fe({...e}),wl=Fe({perm:[0,2,1,3]}),kh=(e,t,n,r,i,s,a)=>{let o=[r,i,s],l=W.size(o),d=[{type:12,data:l},{type:12,data:a},{type:12,data:s}],p=f=>{let c=be("qkv_with_bias",t.dataType,o),m=V("qkv",t.dataType,o),y=V("bias",n.dataType,o),w=[{name:"output_size",type:"u32"},{name:"bias_offset",type:"u32"},{name:"hidden_size",type:"u32"}];return` + ${f.registerUniforms(w).declareVariables(m,y,c)} + ${f.mainStart()} + ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let bias_offset_idx = (global_idx % uniforms.hidden_size) + uniforms.bias_offset; + + qkv_with_bias[global_idx] = qkv[global_idx] + bias[bias_offset_idx]; + }`};return e.compute({name:"MultiHeadAttentionAddBias",shaderCache:{inputDependencies:["type","type"]},getRunData:()=>({outputs:[{dims:o,dataType:t.dataType,gpuDataType:0}],dispatchGroup:{x:Math.ceil(l/64)},programUniforms:d}),getShaderSource:p},{inputs:[t,n],outputs:[-1]})[0]},hs=(e,t,n,r,i,s,a,o)=>{let l=s;if(a&&W.size(a.dims)>0){if(r===1)throw new Error("AddBiasReshape is not implemented. Please export your model with packed QKV or KV");return l=kh(e,s,a,t,r,n*i,o),l=l.reshape([t,r,n,i]),n===1||r===1?l:e.compute(en(l,wl.perm),{inputs:[l],outputs:[-1]})[0]}else return s.dims.length===3&&(l=s.reshape([t,r,n,i])),n===1||r===1?l:e.compute(en(l,wl.perm),{inputs:[l],outputs:[-1]})[0]},Jb=(e,t)=>{let n=Eh(e.inputs,t),r=e.inputs[0],i=Pt(e.inputs,1),s=Pt(e.inputs,2),a=Pt(e.inputs,3),o=Pt(e.inputs,4),l=Pt(e.inputs,5),d=Pt(e.inputs,6),p=Pt(e.inputs,7);if(r.dims.length===5)throw new Error("Packed QKV is not implemented");if((i==null?void 0:i.dims.length)===5)throw new Error("Packed KV is not implemented");let f=i&&s&&i.dims.length===4&&s.dims.length===4,c=hs(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,r,a,0);if(f)return Ms(e,c,i,s,o,void 0,d,p,l,n);if(!i||!s)throw new Error("key and value must be provided");let m=hs(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.headSize,i,a,n.hiddenSize),y=hs(e,n.batchSize,n.numHeads,n.kvSequenceLength,n.vHeadSize,s,a,2*n.hiddenSize);Ms(e,c,m,y,o,void 0,d,p,l,n)}}),zh,Ah,Mh,Rh,gu,tw,nw,rw=re(()=>{Te(),Ce(),pt(),Ee(),zh=e=>{if(!e||e.length<1)throw new Error("too few inputs")},Ah=(e,t)=>{let n=[],r=t.numOutputs;return e[1].dims[0]>0&&(e[1].getBigInt64Array().forEach(i=>n.push(Number(i))),r=n.length),Fe({numOutputs:r,axis:t.axis,splitSizes:n})},Mh=e=>` +fn calculateOutputIndex(index: u32) -> u32 { + for (var i: u32 = 0u; i < ${e}u; i += 1u ) { + if (index < ${we("uniforms.size_in_split_axis","i",e)}) { + return i; + } + } + return ${e}u; +}`,Rh=e=>{let t=e.length,n=[];for(let r=0;r{let n=e[0].dims,r=W.size(n),i=e[0].dataType,s=W.normalizeAxis(t.axis,n.length),a=new Array(t.numOutputs),o=V("input",i,n.length),l=new Array(t.numOutputs),d=[],p=[],f=0,c=[{type:12,data:r}];for(let y=0;y` + ${y.registerUniform("input_size","u32").registerUniform("size_in_split_axis","u32",l.length).declareVariables(o,...a)} + ${Mh(l.length)} + ${Rh(a)} + + ${y.mainStart()} + ${y.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.input_size")} + + var indices = ${o.offsetToIndices("global_idx")}; + var index = ${o.indicesGet("indices",s)}; + let output_number = calculateOutputIndex(index); + if (output_number != 0) { + index -= ${we("uniforms.size_in_split_axis","output_number - 1u",l.length)}; + ${o.indicesSet("indices",s,"index")}; + } + writeBufferData(output_number, indices, global_idx); + }`;return{name:"Split",shaderCache:{hint:t.cacheKey,inputDependencies:["rank"]},getShaderSource:m,getRunData:()=>({outputs:d,dispatchGroup:{x:Math.ceil(r/64)},programUniforms:c})}},tw=(e,t)=>{zh(e.inputs);let n=e.inputs.length===1?t:Ah(e.inputs,t);e.compute(gu(e.inputs,n),{inputs:[0]})},nw=e=>{let t=e.axis,n=e.splitSizes,r=e.numOutputs<0?n.length:e.numOutputs;if(r!==n.length)throw new Error("numOutputs and splitSizes length must be equal");return Fe({axis:t,numOutputs:r,splitSizes:n})}}),Oh,Ga,iw,sw=re(()=>{Te(),Ce(),pt(),Ee(),Oh=(e,t)=>{let[n,r,i,s]=e,{numHeads:a,rotaryEmbeddingDim:o}=t;if(n.dims.length!==3&&n.dims.length!==4)throw new Error(`Input 'x' is expected to have 3 or 4 dimensions, got ${n.dims.length}`);if(!W.areEqual(r.dims,[])&&!W.areEqual(r.dims,[1])&&r.dims.length!==2)throw new Error(`Input 'position_ids' is expected to have 0, 1, or 2 dimensions, got ${r.dims.length}`);if(i.dims.length!==2)throw new Error(`Input 'cos_cache' is expected to have 2 dimensions, got ${i.dims.length}`);if(s.dims.length!==2)throw new Error(`Input 'sin_cache' is expected to have 2 dimensions, got ${s.dims.length}`);if(!W.areEqual(i.dims,s.dims))throw new Error("Inputs 'cos_cache' and 'sin_cache' are expected to have the same shape");if(o>0&&a===0)throw new Error("num_heads must be provided if rotary_embedding_dim is specified");let l=n.dims[0],d=n.dims[n.dims.length-2],p=i.dims[0],f=W.sizeFromDimension(n.dims,1)/d,c=o===0?i.dims[1]*2:f/a;if(o>c)throw new Error("rotary_embedding_dim must be less than or equal to head_size");if(r.dims.length===2){if(l!==r.dims[0])throw new Error(`Input 'position_ids' dimension 0 should be of size batch_size, got ${r.dims[0]}`);if(d!==r.dims[1])throw new Error(`Input 'position_ids' dimension 1 should be of size sequence_length, got ${r.dims[1]}`)}if(c/2!==i.dims[1]&&o/2!==i.dims[1])throw new Error(`Input 'cos_cache' dimension 1 should be same as head_size / 2 or rotary_embedding_dim / 2, got ${i.dims[1]}`);if(d>p)throw new Error("Updating cos_cache and sin_cache in RotaryEmbedding is not currently supported")},Ga=(e,t)=>{let{interleaved:n,numHeads:r,rotaryEmbeddingDim:i,scale:s}=t,a=e[0].dims[0],o=W.sizeFromDimension(e[0].dims,1),l=e[0].dims[e[0].dims.length-2],d=o/l,p=e[2].dims[1],f=i===0?p*2:d/r,c=new Array(a,l,d/f,f-p),m=W.computeStrides(c),y=[{type:1,data:s},{type:12,data:c},{type:12,data:m},...e[0].dims.length===3?new Array({type:12,data:[o,d,f,1]}):[],...e[0].dims.length===4?new Array({type:12,data:[o,f,l*f,1]}):[],..._e(e[0].dims,e[1].dims,e[2].dims,e[3].dims,e[0].dims)],w=$=>{let _=V("input",e[0].dataType,e[0].dims.length),v=V("position_ids",e[1].dataType,e[1].dims.length),I=V("cos_cache",e[2].dataType,e[2].dims.length),S=V("sin_cache",e[3].dataType,e[3].dims.length),E=be("output",e[0].dataType,e[0].dims.length);return $.registerUniforms([{name:"scale",type:"f32"},{name:"global_shape",type:"u32",length:c.length},{name:"global_strides",type:"u32",length:m.length},{name:"input_output_strides",type:"u32",length:m.length}]),` + ${$.declareVariables(_,v,I,S,E)} + + ${$.mainStart(ki)} + let half_rotary_emb_dim = uniforms.${I.name}_shape[1]; + let bsnh = global_idx / uniforms.global_strides % uniforms.global_shape; + let size = uniforms.global_shape[0] * uniforms.global_strides[0]; + ${$.guardAgainstOutOfBoundsWorkgroupSizes("size")} + + if (bsnh[3] < half_rotary_emb_dim) { + let position_ids_idx = + ${v.broadcastedIndicesToOffset("bsnh.xy",be("",v.type.tensor,2))}; + let position_id = + u32(${v.getByOffset("position_ids_idx")}) + select(0, bsnh[1], position_ids_idx == 0); + let i = dot(bsnh, uniforms.input_output_strides) + select(0, bsnh[3], ${n}); + let j = i + select(half_rotary_emb_dim, 1, ${n}); + let re = ${_.getByOffset("i")} * ${I.get("position_id","bsnh[3]")} - + ${_.getByOffset("j")} * ${S.get("position_id","bsnh[3]")}; + ${E.setByOffset("i","re")} + let im = ${_.getByOffset("i")} * ${S.get("position_id","bsnh[3]")} + + ${_.getByOffset("j")} * ${I.get("position_id","bsnh[3]")}; + ${E.setByOffset("j","im")} + } else { + let k = dot(bsnh, uniforms.input_output_strides) + half_rotary_emb_dim; + ${E.setByOffset("k",_.getByOffset("k"))} + } + }`};return{name:"RotaryEmbedding",shaderCache:{hint:Fe({interleaved:n}).cacheKey,inputDependencies:["rank","rank","rank","rank"]},getShaderSource:w,getRunData:()=>({outputs:[{dims:e[0].dims,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(W.size(c)/ki)},programUniforms:y})}},iw=(e,t)=>{Oh(e.inputs,t),e.compute(Ga(e.inputs,t))}}),Nh,Bh,_l,Dh,aw,F$=re(()=>{pt(),Te(),Ju(),ew(),rw(),_r(),sw(),Ee(),Nh=(e,t)=>{if(t.doRotary&&e.length<=7)throw new Error("cos_cache and sin_cache inputs are required if do_rotary is specified");let n=e[0],r=e[1],i=e[2],s=e[3],a=e[4];if(t.doRotary!==0&&e.length<=7)throw new Error("cos_cast and sin_cache are expected if do_rotary attribute is non-zero");if(t.localWindowSize!==-1)throw new Error("Local attention is not supported");if(t.softcap!==0)throw new Error("Softcap is not supported");if(t.rotaryInterleaved!==0)throw new Error("Rotary interleaved is not supported");if(t.smoothSoftmax)throw new Error("Smooth softmax is not supported");if(n.dims.length!==3&&n.dims.length!==5)throw new Error("Input query is expected to have 3 or 5 dimensions");let o=!1,l=n.dims[0],d=n.dims[1],p=n.dims.length===3?o?n.dims[2]/3:n.dims[2]:t.numHeads*n.dims[4],f=d,c=0,m=!r||r.dims.length===0,y=Math.floor(m?p/(t.numHeads+2*t.kvNumHeads):p/t.numHeads);m&&(p=y*t.numHeads);let w=s&&s.dims.length!==0,$=a&&a.dims.length!==0;if(w&&s.dims.length===4&&s.dims[0]===l&&s.dims[1]!==t.kvNumHeads&&s.dims[2]===t.kvNumHeads&&s.dims[3]===y)throw new Error("BSNH pastKey/pastValue is not supported");if(w&&$){if(s.dims.length!==4)throw new Error('Input "past_key" is expected to have 4 dimensions');if(a.dims.length!==4)throw new Error('Input "past_value" is expected to have 4 dimensions');c=s.dims[2]}else if(w||$)throw new Error('Input "past_key" and "past_value" shall be both present or both absent');let _=1;if(r&&r.dims.length>0){if(n.dims.length!==3)throw new Error('Input "query" is expected to have 3 dimensions when key is given');if(r.dims.length<3||r.dims.length>5)throw new Error('Input "key" is expected to have 3, 4, or 5 dimensions');if(n.dims[0]!==r.dims[0])throw new Error('Input "query" and "key" shall have same dim 0 (batch size)');if(r.dims.length===3){if(n.dims[2]%r.dims[2]!==0)throw new Error('Dimension 2 of "query" should be a multiple of "key"');f=r.dims[1]}else if(r.dims.length===5){if(r.dims[2]!==t.numHeads||r.dims[3]!==2||r.dims[4]!==y)throw new Error('Expect "key" shape (batch_size, kv_sequence_length, num_heads, 2, head_size) for packed kv');if(i)throw new Error('Expect "value" be none when "key" has packed kv format.');f=r.dims[1]}else{if(r.dims[1]!==t.numHeads||r.dims[3]!==y)throw new Error('Expect "key" shape (batch_size, num_heads, kv_sequence_length, head_size) for past_key');f=r.dims[2]}}else{if(n.dims.length!==3&&n.dims.length!==5)throw new Error('Input "query" is expected to have 3 or 5 dimensions when key is empty');if(n.dims.length===5&&(n.dims[2]!==t.numHeads||n.dims[3]!==3))throw new Error('Expect "query" shape (batch_size, kv_sequence_length, num_heads, 3, head_size) for packed kv');_=3}let v=0,I=!1,S=t.kvNumHeads?y*t.kvNumHeads:p;if(i&&i.dims.length>0){if(i.dims.length!==3&&i.dims.length!==4)throw new Error('Input "value" is expected to have 3 or 4 dimensions');if(n.dims[0]!==i.dims[0])throw new Error('Input "query" and "value" shall have same dim 0 (batch_size)');if(i.dims.length===3){if(f!==i.dims[1])throw new Error('Input "key" and "value" shall have the same dim 1 (kv_sequence_length)');S=i.dims[2]}else{if(f!==i.dims[2])throw new Error('Input "past_key" and "past_value" shall have the same dim 2 (kv_sequence_length)');S=i.dims[1]*i.dims[3],I=!0}}let E=e.length>4?e[5]:void 0;if(E&&E.dims.length!==1&&E.dims[0]!==l)throw new Error('Input "seqlens" is expected to have 1 dimension and the same dim 0 as batch_size');return{batchSize:l,sequenceLength:d,pastSequenceLength:c,kvSequenceLength:f,totalSequenceLength:-1,maxSequenceLength:-1,inputHiddenSize:0,hiddenSize:p,vHiddenSize:S,headSize:y,vHeadSize:Math.floor(S/t.kvNumHeads),numHeads:t.numHeads,kvNumHeads:t.kvNumHeads,nReps:t.numHeads/t.kvNumHeads,pastPresentShareBuffer:!1,maskType:v,scale:t.scale,broadcastResPosBias:!1,passPastInKv:I,qkvFormat:_}},Bh=Fe({perm:[0,2,1,3]}),_l=(e,t,n)=>{let r=t,i=n.kvNumHeads;return t.dims.length===3&&n.kvSequenceLength!==0&&(r=t.reshape([n.batchSize,n.kvSequenceLength,i,n.headSize]),r=e.compute(en(r,Bh.perm),{inputs:[r],outputs:[-1]})[0]),r},Dh=(e,t,n,r)=>{let i=7,s=["type","type"],a=[e*t],o=e*t,l=[{type:12,data:o},{type:12,data:t},{type:12,data:e}],d=p=>{let f=V("seq_lens",n.dataType,n.dims),c=V("total_seq_lens",r.dataType,r.dims),m=be("pos_ids",i,a),y=[{name:"output_size",type:"u32"},{name:"sequence_length",type:"u32"},{name:"batch_size",type:"u32"}];return` + ${p.registerUniforms(y).declareVariables(f,c,m)} + ${p.mainStart()} + ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let total_sequence_length = u32(${c.getByOffset("0")}); + let is_subsequent_prompt = uniforms.sequence_length > 1 && uniforms.sequence_length != total_sequence_length; + let is_first_prompt = !is_subsequent_prompt && uniforms.sequence_length == total_sequence_length; + let batch_idx = global_idx / uniforms.sequence_length; + let sequence_idx = i32(global_idx % uniforms.sequence_length); + var pos_id: i32 = 0; + let seqlen = ${f.getByOffset("batch_idx")}; + let total_seqlen = seqlen + 1; + if (is_first_prompt) { + if (sequence_idx < total_seqlen) { + pos_id = sequence_idx; + } else { + pos_id = 1; + } + ${m.setByOffset("global_idx","pos_id")} + } else if (is_subsequent_prompt) { + let past_seqlen = total_seqlen - i32(uniforms.sequence_length); + if (past_seqlen + sequence_idx < total_seqlen) { + pos_id = past_seqlen + sequence_idx; + } else { + pos_id = 1; + } + ${m.setByOffset("global_idx","pos_id")} + } else if (global_idx < uniforms.batch_size) { + ${m.setByOffset("global_idx","seqlen")} + }; + } + `};return{name:"GeneratePositionIds",shaderCache:{hint:`${e};${t}`,inputDependencies:s},getRunData:()=>({outputs:[{dims:a,dataType:i}],dispatchGroup:{x:Math.ceil(o/64)},programUniforms:l}),getShaderSource:d}},aw=(e,t)=>{var S;let n=Nh(e.inputs,t);if(e.inputs[0].dims.length===5)throw new Error("Packed QKV is not implemented");if(((S=e.inputs[1])==null?void 0:S.dims.length)===5)throw new Error("Packed KV is not implemented");let r=e.inputs[0],i=e.inputs[1]&&e.inputs[1].dims.length>0?e.inputs[1]:void 0,s=e.inputs[2]&&e.inputs[2].dims.length>0?e.inputs[2]:void 0,a=e.inputs[3]&&e.inputs[3].dims.length!==0?e.inputs[3]:void 0,o=e.inputs[4]&&e.inputs[4].dims.length!==0?e.inputs[4]:void 0,l=e.inputs.length>4?e.inputs[5]:void 0,d=e.inputs.length>5?e.inputs[6]:void 0,p=n.kvNumHeads?n.kvNumHeads:n.numHeads,f=Fe({axis:2,numOutputs:3,splitSizes:[n.numHeads*n.headSize,p*n.headSize,p*n.headSize]}),[c,m,y]=!i&&!s?e.compute(gu([r],f),{inputs:[r],outputs:[-1,-1,-1]}):[r,i,s],w,$;if(t.doRotary){let E=e.compute(Dh(n.batchSize,n.sequenceLength,l,d),{inputs:[l,d],outputs:[-1]})[0],R=e.inputs[7],O=e.inputs[8],x=Fe({interleaved:t.rotaryInterleaved!==0,numHeads:n.numHeads,rotaryEmbeddingDim:0,scale:t.scale}),U=[c,E,R,O],q=[-1];w=e.compute(Ga(U,x),{inputs:U,outputs:q})[0],U.splice(0,1,m);let J=Fe({interleaved:t.rotaryInterleaved!==0,numHeads:n.kvNumHeads,rotaryEmbeddingDim:0,scale:t.scale});$=e.compute(Ga(U,J),{inputs:U,outputs:q})[0]}let _=hs(e,n.batchSize,n.numHeads,n.sequenceLength,n.headSize,t.doRotary?w:c,void 0,0),v=_l(e,t.doRotary?$:m,n),I=_l(e,y,n);Ms(e,_,v,I,void 0,void 0,a,o,void 0,n,l,d)}}),vl,Ph,Uh,ow,H$=re(()=>{Te(),Ce(),_r(),Ee(),vl=(e,t,n,r,i,s,a,o)=>{let l=ct(s),d=l===1?"f32":`vec${l}f`,p=l===1?"vec2f":`mat2x${l}f`,f=i*a,c=64;f===1&&(c=256);let m=[i,a,s/l],y=[i,a,2],w=["rank","type","type"],$=[];$.push(..._e(m,y));let _=v=>{let I=V("x",t.dataType,3,l),S=V("scale",n.dataType,n.dims),E=V("bias",r.dataType,r.dims),R=be("output",1,3,2),O=[I,S,E,R];return` + var workgroup_shared : array<${p}, ${c}>; + const workgroup_size = ${c}u; + ${v.declareVariables(...O)} + ${v.mainStart(c)} + let batch = workgroup_index / uniforms.x_shape[1]; + let channel = workgroup_index % uniforms.x_shape[1]; + let hight = uniforms.x_shape[2]; + // initialize workgroup memory + var sum = ${d}(0); + var squared_sum = ${d}(0); + for (var h = local_idx; h < hight; h += workgroup_size) { + let value = ${d}(${I.get("batch","channel","h")}); + sum += value; + squared_sum += value * value; + } + workgroup_shared[local_idx] = ${p}(sum, squared_sum); + workgroupBarrier(); + + for (var currSize = workgroup_size >> 1; currSize > 0; currSize = currSize >> 1) { + if (local_idx < currSize) { + workgroup_shared[local_idx] = workgroup_shared[local_idx] + workgroup_shared[local_idx + currSize]; + } + workgroupBarrier(); + } + if (local_idx == 0) { + let sum_final = ${wr("workgroup_shared[0][0]",l)} / f32(hight * ${l}); + let squared_sum_final = ${wr("workgroup_shared[0][1]",l)} / f32(hight * ${l}); + + let inv_std_dev = inverseSqrt(squared_sum_final - sum_final * sum_final + f32(${o})); + let channel_scale = inv_std_dev * f32(scale[channel]); + let channel_shift = f32(bias[channel]) - sum_final * channel_scale; + output[workgroup_index] = vec2f(channel_scale, channel_shift); + } + }`};return e.compute({name:"InstanceNormComputeChannelScaleShift",shaderCache:{hint:`${l};${o};${c}`,inputDependencies:w},getRunData:()=>({outputs:[{dims:y,dataType:1}],dispatchGroup:{x:f},programUniforms:$}),getShaderSource:_},{inputs:[t,n,r],outputs:[-1]})[0]},Ph=(e,t,n)=>{let r=t[0].dims,i=r,s=2,a=r[0],o=r[1],l=W.sizeFromDimension(r,s),d=ct(l),p=W.size(i)/d,f=vl(e,t[0],t[1],t[2],a,l,o,n.epsilon),c=[a,o,l/d],m=[a,o],y=["type","none"],w=$=>{let _=V("x",t[0].dataType,c.length,d),v=V("scale_shift",1,m.length,2),I=be("output",t[0].dataType,c.length,d),S=[_,v,I];return` + ${$.registerUniform("output_size","u32").declareVariables(...S)} + ${$.mainStart()} + ${$.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let outputIndices = ${I.offsetToIndices("global_idx")}; + let batch = outputIndices[0]; + let channel = outputIndices[1]; + let scale_shift = ${v.getByIndices("vec2(batch, channel)")}; + let value = ${_.getByOffset("global_idx")} * ${I.type.value}(scale_shift.x) + ${I.type.value}(scale_shift.y); + ${I.setByOffset("global_idx","value")}; + }`};e.compute({name:"InstanceNormalization",shaderCache:{hint:`${d}`,inputDependencies:y},getRunData:()=>({outputs:[{dims:i,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(p/64)},programUniforms:[{type:12,data:p},..._e(c,m,c)]}),getShaderSource:w},{inputs:[t[0],f]})},Uh=(e,t,n)=>{let r=t[0].dims,i=r,s=r[0],a=r[r.length-1],o=W.sizeFromDimension(r,1)/a,l=ct(a),d=W.size(i)/l,p=[{type:12,data:o},{type:12,data:Math.floor(a/l)}],f=["type","type"],c=!1,m=[0,r.length-1];for(let _=0;_r[m[v]])),w=vl(e,y,t[1],t[2],s,o,a,n.epsilon),$=_=>{let v=xt(t[0].dataType),I=l===1?"vec2f":`mat${l}x2f`,S=O=>{let x=O===0?"x":"y",U=l===1?"f32":`vec${l}f`;switch(l){case 1:return`${v}(${U}(scale.${x}))`;case 2:return`vec2<${v}>(${U}(scale[0].${x}, scale[1].${x}))`;case 4:return`vec4<${v}>(${U}(scale[0].${x}, scale[1].${x}, scale[2].${x}, scale[3].${x}))`;default:throw new Error(`Not supported compoents ${l}`)}},E=V("input",t[0].dataType,t[0].dims,l),R=be("output",t[0].dataType,i,l);return` + @group(0) @binding(0) var input : array<${E.type.storage}>; + @group(0) @binding(1) var scale_input : array<${I}>; + @group(0) @binding(2) var output : array<${R.type.storage}>; + struct Uniforms {H: u32, C : u32}; + @group(0) @binding(3) var uniforms: Uniforms; + + ${_.mainStart()} + let current_image_number = global_idx / (uniforms.C * uniforms.H); + let current_channel_number = global_idx % uniforms.C; + + let scale_offset = current_image_number * uniforms.C + current_channel_number; + let scale = scale_input[scale_offset]; + output[global_idx] = fma(input[global_idx], ${S(0)}, ${S(1)}); + }`};e.compute({name:"InstanceNormalizationNHWC",shaderCache:{hint:`${l}`,inputDependencies:f},getRunData:()=>({outputs:[{dims:i,dataType:t[0].dataType}],dispatchGroup:{x:Math.ceil(d/64)},programUniforms:p}),getShaderSource:$},{inputs:[t[0],w]})},ow=(e,t)=>{t.format==="NHWC"?Uh(e,e.inputs,t):Ph(e,e.inputs,t)}}),Lh,Wh,lw,j$=re(()=>{Te(),Ce(),Ee(),Lh=e=>{if(!e||e.length<2)throw new Error("layerNorm requires at least 2 inputs.")},Wh=(e,t,n)=>{let r=t.simplified,i=e[0].dims,s=e[1],a=!r&&e[2],o=i,l=W.normalizeAxis(t.axis,i.length),d=W.sizeToDimension(i,l),p=W.sizeFromDimension(i,l),f=W.size(s.dims),c=a?W.size(a.dims):0;if(f!==p||a&&c!==p)throw new Error(`Size of X.shape()[axis:] == ${p}. + Size of scale and bias (if provided) must match this. + Got scale size of ${f} and bias size of ${c}`);let m=[];for(let E=0;E1,v=n>2,I=E=>{let R=xt(e[0].dataType),O=[V("x",e[0].dataType,e[0].dims,y),V("scale",s.dataType,s.dims,y)];a&&O.push(V("bias",a.dataType,a.dims,y)),O.push(be("output",e[0].dataType,o,y)),_&&O.push(be("mean_data_output",1,m)),v&&O.push(be("inv_std_output",1,m));let x=[{name:"norm_count",type:"u32"},{name:"norm_size",type:"f32"},{name:"norm_size_vectorized",type:"u32"},{name:"epsilon",type:"f32"}];return` + ${E.registerUniforms(x).declareVariables(...O)} + ${E.mainStart()} + ${E.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.norm_count")} + let offset = global_idx * uniforms.norm_size_vectorized; + var mean_vector = ${ou("f32",y)}; + var mean_square_vector = ${ou("f32",y)}; + + for (var h: u32 = 0u; h < uniforms.norm_size_vectorized; h++) { + let value = ${bi(R,y,"x[h + offset]")}; + mean_vector += value; + mean_square_vector += value * value; + } + let mean = ${wr("mean_vector",y)} / uniforms.norm_size; + let inv_std_dev = inverseSqrt(${wr("mean_square_vector",y)} / uniforms.norm_size ${r?"":"- mean * mean"} + uniforms.epsilon); + + for (var j: u32 = 0; j < uniforms.norm_size_vectorized; j++) { + let f32input = ${bi(R,y,"x[j + offset]")}; + let f32scale = ${bi(R,y,"scale[j]")}; + output[j + offset] = ${O[0].type.value}((f32input ${r?"":"- mean"}) * inv_std_dev * f32scale + ${a?`+ ${bi(R,y,"bias[j]")}`:""} + ); + } + + ${_?"mean_data_output[global_idx] = mean":""}; + ${v?"inv_std_output[global_idx] = inv_std_dev":""}; + }`},S=[{dims:o,dataType:e[0].dataType}];return _&&S.push({dims:m,dataType:1}),v&&S.push({dims:m,dataType:1}),{name:"LayerNormalization",shaderCache:{hint:`${y};${n};${r}`,inputDependencies:w},getRunData:()=>({outputs:S,dispatchGroup:{x:Math.ceil(d/64)},programUniforms:$}),getShaderSource:I}},lw=(e,t)=>{Lh(e.inputs),e.compute(Wh(e.inputs,t,e.outputCount))}}),qh,uw,K$=re(()=>{Ce(),id(),sd(),qh=e=>{if(!e||e.length!==2)throw new Error("MatMul requires 2 inputs.");if(e[0].dims[e[0].dims.length-1]!==e[1].dims[e[1].dims.length-2])throw new Error("shared dimension does not match.")},uw=e=>{qh(e.inputs);let t=Ei.calcShape(e.inputs[0].dims,e.inputs[1].dims,!0);if(!t)throw new Error("Can't use matmul on the given tensors");let n=t[t.length-1],r=e.inputs[0].dims[e.inputs[0].dims.length-1];if(n<8&&r<8)e.compute(rd(e.inputs,{activation:""},t));else{let i=t[t.length-2],s=W.size(e.inputs[0].dims.slice(0,-2)),a=W.size(e.inputs[1].dims.slice(0,-2));if(s!==1&&i===1&&a===1){let o=e.inputs[0].reshape([1,s,r]),l=e.inputs[1].reshape([1,r,n]),d=[1,s,n],p=[o,l];e.compute(qa(p,{activation:""},t,d),{inputs:p})}else e.compute(qa(e.inputs,{activation:""},t))}}}),Gh,Vh,Fh,dw,cw,X$=re(()=>{Te(),Ce(),pt(),Ee(),Gh=(e,t)=>{if(e.length<3||e.length>4)throw new Error("MatMulNBits requires 3 or 4 inputs");let n=e[0],r=n.dims.length;if(n.dims[r-1]!==t.k)throw new Error("The last dim of input shape does not match the k value");let i=Math.floor((t.k+t.blockSize-1)/t.blockSize),s=t.blockSize/8*t.bits,a=e[1];if(!W.areEqual(a.dims,[t.n,i,s]))throw new Error("The second inputs must be 3D tensor with shape N X nBlocksPerCol X blobSize");let o=e[2].dims;if(W.size(o)!==t.n*i)throw new Error("scales input size error.");if(e.length===4){let l=e[3].dims,d=t.n*(t.bits===8?i:Math.floor((i*t.bits+7)/8));if(W.size(l)!==d)throw new Error("zeroPoints input size error.")}},Vh=(e,t)=>{let n=e[0].dims,r=n.length,i=n[r-2],s=t.k,a=t.n,o=n.slice(0,r-2),l=W.size(o),d=e[1].dims[2]/4,p=e[0].dataType,f=ct(t.k),c=ct(d),m=ct(a),y=o.concat([i,a]),w=i>1&&a/m%2===0?2:1,$=W.size(y)/m/w,_=64,v=[],I=[l,i,s/f],S=W.convertShape(e[1].dims).slice();S.splice(-1,1,d/c),v.push(..._e(I)),v.push(..._e(S)),v.push(..._e(e[2].dims)),e.length===4&&v.push(..._e(W.convertShape(e[3].dims)));let E=[l,i,a/m];v.push(..._e(E));let R=O=>{let x=I.length,U=V("a",e[0].dataType,x,f),q=V("b",12,S.length,c),J=V("scales",e[2].dataType,e[2].dims.length),se=[U,q,J],ce=e.length===4?V("zero_points",12,e[3].dims.length):void 0;ce&&se.push(ce);let L=E.length,ue=be("output",e[0].dataType,L,m),ie=xt(e[0].dataType),te=(()=>{switch(f){case 1:return`array<${ie}, 8>`;case 2:return`mat4x2<${ie}>`;case 4:return`mat2x4<${ie}>`;default:throw new Error(`${f}-component is not supported.`)}})(),he=Math.floor(32/t.bits),Q=Math.floor(he/8),ve=()=>{let ee="";for(let ne=0;ne0?ne:""} = ${ne===0?U.indicesToOffset(`${U.type.indices}(batch, row, word_offset)`):"input_offset"}; + var a_data${ne>0?ne:""}: ${te}; + for (var j${ne>0?ne:""}: u32 = 0; j${ne>0?ne:""} < ${8/f}; j${ne>0?ne:""}++) { + a_data${ne>0?ne:""}[j${ne>0?ne:""}] = ${U.getByOffset(`input_offset${ne>0?ne:""}`)}; + input_offset${ne>0?ne:""}++; + } + `;for(let Xe=0;Xe> ${ne*16}u; + let byte_lo = half_word & 0xFFu; + let byte_hi = (half_word >> 8u) & 0xFFu; + let spread_word = (byte_lo & 0xFu) | ((byte_lo >> 4u) << 8u) | ((byte_hi & 0xFu) << 16u) | ((byte_hi >> 4u) << 24u); + b_value_lower = unpack4xU8(spread_word & b_mask); + b_value_upper = unpack4xU8((spread_word >> 2u) & b_mask); + }`:`b_value_lower = unpack4xU8((b_value >> ${Ue}u) & b_mask); + b_value_upper = unpack4xU8((b_value >> ${lt}u) & b_mask);`} + b_quantized_values = ${te}(${Array.from({length:4},(Ke,ft)=>`${ie}(b_value_lower[${ft}]), ${ie}(b_value_upper[${ft}])`).join(", ")}); + b_dequantized_values = ${f===1?`${te}(${Array.from({length:8},(Ke,ft)=>`(b_quantized_values[${ft}] - ${ce?`zero_point${Xe}`:"zero_point"}) * scale${Xe}`).join(", ")});`:`(b_quantized_values - ${te}(${Array(8).fill(`${ce?`zero_point${Xe}`:"zero_point"}`).join(",")})) * scale${Xe};`}; + workgroup_shared[local_id.x * ${w} + ${Math.floor(Xe/m)}]${m>1?`[${Xe%m}]`:""} += ${Array.from({length:8/f},(Ke,ft)=>`${f===1?`a_data${ne>0?ne:""}[${ft}] * b_dequantized_values[${ft}]`:`dot(a_data${ne>0?ne:""}[${ft}], b_dequantized_values[${ft}])`}`).join(" + ")}; + `}return ee},Y=()=>{let ee=` + var col_index = col * ${m}; + ${ce?` + let zero_point_values_per_byte: u32 = ${Math.floor(8/t.bits)}u; + let zero_point_bytes_per_col = (nBlocksPerCol + zero_point_values_per_byte - 1u) / zero_point_values_per_byte; + var zero_point_byte_count: u32; + var zero_point_word_index: u32; + var zero_point_byte_offset: u32; + let zero_point_sub_offset: u32 = block % zero_point_values_per_byte; + var zero_point_bits_offset: u32; + var zero_point_word: u32;`:` + // The default zero point is ${Math.pow(2,t.bits-1)} for unsigned ${t.bits}-bit quantization. + let zero_point = ${ie}(${Math.pow(2,t.bits-1).toFixed(1)});`} + `;for(let ne=0;ne> 0x2u; + zero_point_byte_offset = zero_point_byte_count & 0x3u; + zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_sub_offset * ${t.bits}u); + zero_point_word = ${ce.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point${ne} = ${ie}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:""} + col_index += 1;`;return ee},P=()=>{let ee=`col_index = col * ${m};`;for(let ne=0;ne; + var b_value_upper: vec4; + var b_quantized_values: ${te}; + var b_dequantized_values: ${te};`,ee};return` + var workgroup_shared: array<${ue.type.value}, ${w*_}>; + ${O.declareVariables(...se,ue)} + ${O.mainStart([_,1,1])} + let output_indices = ${ue.offsetToIndices(`(global_idx / ${_}) * ${w}`)}; + let col = output_indices[2]; + let row = output_indices[1]; + let batch = output_indices[0]; + let nBlocksPerCol = uniforms.b_shape[1]; + + for (var block = local_id.x; block < nBlocksPerCol; block += ${_}) { + //process one block + var word_offset: u32 = block * ${t.blockSize/f}; + ${Y()} + for (var word: u32 = 0; word < ${d}; word += ${c}) { + ${P()} + for (var i: u32 = 0; i < ${c}; i++) { + ${ve()} + word_offset += ${he/f}; + } + } + } + workgroupBarrier(); + + if (local_id.x < ${w}) { + var output_value: ${ue.type.value} = ${ue.type.value}(0); + var workgroup_shared_offset: u32 = local_id.x; + for (var b: u32 = 0u; b < ${_}u; b++) { + output_value += workgroup_shared[workgroup_shared_offset]; + workgroup_shared_offset += ${w}; + } + ${ue.setByIndices(`${ue.type.indices}(batch, row, col + local_id.x)`,"output_value")}; + } + }`};return{name:"MatMulNBits",shaderCache:{hint:`${t.blockSize};${t.bits};${f};${c};${m};${w};${_}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:y,dataType:p}],dispatchGroup:{x:$},programUniforms:v}),getShaderSource:R}},Fh=(e,t)=>{let n=e[0].dims,r=n.length,i=n[r-2],s=t.k,a=t.n,o=n.slice(0,r-2),l=W.size(o),d=e[1].dims[2]/4,p=e[0].dataType,f=ct(t.k),c=ct(d),m=o.concat([i,a]),y=128,w=a%8===0?8:a%4===0?4:1,$=y/w,_=Math.floor(32/t.bits),v=$*c*_,I=v/f,S=v/t.blockSize,E=W.size(m)/w,R=[],O=[l,i,s/f],x=W.convertShape(e[1].dims).slice();x.splice(-1,1,d/c),R.push(..._e(O)),R.push(..._e(x)),R.push(..._e(e[2].dims)),e.length===4&&R.push(..._e(W.convertShape(e[3].dims)));let U=[l,i,a];R.push(..._e(U));let q=J=>{let se=O.length,ce=V("a",e[0].dataType,se,f),L=V("b",12,x.length,c),ue=V("scales",e[2].dataType,e[2].dims.length),ie=[ce,L,ue],te=e.length===4?V("zero_points",12,e[3].dims.length):void 0;te&&ie.push(te);let he=U.length,Q=be("output",e[0].dataType,he),ve=xt(e[0].dataType),Y=()=>{switch(f){case 1:return` + let a_data0 = vec4<${ve}>(sub_a[word_offset], sub_a[word_offset + 1], sub_a[word_offset + 2], sub_a[word_offset + 3]); + let a_data1 = vec4<${ve}>(sub_a[word_offset + 4], sub_a[word_offset + 5], sub_a[word_offset + 6], sub_a[word_offset + 7]);`;case 2:return` + let a_data0 = vec4<${ve}>(sub_a[word_offset], sub_a[word_offset + 1]); + let a_data1 = vec4<${ve}>(sub_a[word_offset + 2], sub_a[word_offset + 3]);`;case 4:return` + let a_data0 = sub_a[word_offset]; + let a_data1 = sub_a[word_offset + 1];`;default:throw new Error(`${f}-component is not supported.`)}};return` + var sub_a: array<${ce.type.value}, ${I}>; + var inter_results: array, ${w}>; + ${J.declareVariables(...ie,Q)} + ${J.mainStart([$,w,1])} + let output_indices = ${Q.offsetToIndices(`workgroup_index * ${w}`)}; + let col = output_indices[2]; + let row = output_indices[1]; + let batch = output_indices[0]; + let n_blocks_per_col = uniforms.b_shape[1]; + let num_tiles = (n_blocks_per_col - 1) / ${S} + 1; + + // Loop over shared dimension. + for (var tile: u32 = 0; tile < num_tiles; tile += 1) { + let a_col_start = tile * ${I}; + // load one tile A data into shared memory. + for (var a_offset = local_idx; a_offset < ${I}; a_offset += ${y}) + { + let a_col = a_col_start + a_offset; + if (a_col < uniforms.a_shape[2]) + { + sub_a[a_offset] = ${ce.getByIndices(`${ce.type.indices}(batch, row, a_col)`)}; + } else { + sub_a[a_offset] = ${ce.type.value}(0); + } + } + workgroupBarrier(); + + // each thread process one block + let b_row = col + local_id.y; + let block = tile * ${S} + local_id.x; + ${te?` + let zero_point_values_per_byte: u32 = ${Math.floor(8/t.bits)}u; + let zero_point_bytes_per_col = (n_blocks_per_col + zero_point_values_per_byte - 1u) / zero_point_values_per_byte; + let zero_point_byte_count = b_row * zero_point_bytes_per_col + (block / zero_point_values_per_byte); + let zero_point_word_index = zero_point_byte_count >> 0x2u; + let zero_point_byte_offset = zero_point_byte_count & 0x3u; + let zero_point_sub_offset: u32 = block % zero_point_values_per_byte; + let zero_point_bits_offset = (zero_point_byte_offset << 3) + (zero_point_sub_offset * ${t.bits}u); + let zero_point_word = ${te.getByOffset("zero_point_word_index")} >> zero_point_bits_offset; + let zero_point = ${ve}((zero_point_word) & ${t.bits===2?"0x3u":"0xFu"});`:` + // The default zero point is ${Math.pow(2,t.bits-1)} for unsigned ${t.bits}-bit quantization. + let zero_point = ${ve}(${Math.pow(2,t.bits-1).toFixed(1)});`} + let scale = ${ue.getByOffset("b_row * n_blocks_per_col + block")}; + let b_data = ${L.getByIndices(`${L.type.indices}(b_row, block, 0)`)}; + var word_offset = local_id.x * ${t.blockSize/f}; + for (var i: u32 = 0; i < ${c}; i++) { + let b_value = ${c===1?"b_data":"b_data[i]"}; + ${(()=>{let P=Math.floor(_/8),ee="";for(let ne=0;ne> ${ne*16}u; + let byte_lo = half_word & 0xFFu; + let byte_hi = (half_word >> 8u) & 0xFFu; + let spread_word = (byte_lo & 0xFu) | ((byte_lo >> 4u) << 8u) | ((byte_hi & 0xFu) << 16u) | ((byte_hi >> 4u) << 24u); + let b_value_lower = unpack4xU8(spread_word & 0x03030303u); + let b_value_upper = unpack4xU8((spread_word >> 2u) & 0x03030303u);`:` + let b_value_lower = unpack4xU8((b_value >> ${Ue}u) & 0x0F0F0F0Fu); + let b_value_upper = unpack4xU8((b_value >> ${lt}u) & 0x0F0F0F0Fu);`} + let b_quantized_values = mat2x4<${ve}>(${Array.from({length:4},(Xe,Ke)=>`${ve}(b_value_lower[${Ke}]), ${ve}(b_value_upper[${Ke}])`).join(", ")}); + let b_dequantized_values = (b_quantized_values - mat2x4<${ve}>(${Array(8).fill("zero_point").join(",")})) * scale; + inter_results[local_id.y][local_id.x] += ${Array.from({length:2},(Xe,Ke)=>`${`dot(a_data${Ke}, b_dequantized_values[${Ke}])`}`).join(" + ")}; + } + word_offset += ${8/f};`}return ee})()} + } + workgroupBarrier(); + } + + if (local_idx < ${w}) { + var output_value: ${Q.type.value} = ${Q.type.value}(0); + for (var b = 0u; b < ${$}; b++) { + output_value += inter_results[local_idx][b]; + } + if (col + local_idx < uniforms.output_shape[2]) + { + ${Q.setByIndices(`${Q.type.indices}(batch, row, col + local_idx)`,"output_value")} + } + } + }`};return{name:"BlockwiseMatMulNBits32",shaderCache:{hint:`${t.blockSize};${f};${c};${$};${w}`,inputDependencies:Array(e.length).fill("rank")},getRunData:()=>({outputs:[{dims:m,dataType:p}],dispatchGroup:{x:E},programUniforms:R}),getShaderSource:q}},dw=(e,t)=>{Gh(e.inputs,t),t.blockSize===32&&e.adapterInfo.isVendor("intel")&&e.adapterInfo.isArchitecture("gen-12lp")?e.compute(Fh(e.inputs,t)):e.compute(Vh(e.inputs,t))},cw=e=>Fe(e)}),Hh,jh,Kh,Xh,Yh,Zh,Qh,Jh,pw,Y$=re(()=>{Te(),Ce(),Ee(),Hh=e=>{if(!e||e.length<1)throw new Error("Too few inputs");if(e[0].dataType!==1&&e[0].dataType!==10)throw new Error("Input type must be float or float16.");if(e.length>=2){let t=e[0].dims.length*2===e[1].dims[0];if(e.length===4&&(t=e[3].dims[0]*2===e[1].dims[0]),!t)throw new Error("The pads should be a 1D tensor of shape [2 * input_rank] or [2 * num_axes].")}},jh=(e,t,n)=>{let r="";for(let i=t-1;i>=0;--i)r+=` + k = i32(${e.indicesGet("indices",i)}) - ${we("uniforms.pads",i,n)}; + if (k < 0) { + break; + } + if (k >= i32(${we("uniforms.x_shape",i,t)})) { + break; + } + offset += k * i32(${we("uniforms.x_strides",i,t)}); + `;return` + value = ${e.type.value}(uniforms.constant_value); + for (var i = 0; i < 1; i++) { + var offset = 0; + var k = 0; + ${r} + value = x[offset]; + } + `},Kh=(e,t,n)=>{let r="";for(let i=t-1;i>=0;--i)r+=` + k = i32(${e.indicesGet("indices",i)}) - ${we("uniforms.pads",i,n)}; + if (k < 0) { + k = -k; + } + { + let _2n_1 = 2 * (i32(${we("uniforms.x_shape",i,t)}) - 1); + k = k % _2n_1; + if(k >= i32(${we("uniforms.x_shape",i,t)})) { + k = _2n_1 - k; + } + } + offset += k * i32(${we("uniforms.x_strides",i,t)}); + `;return` + var offset = 0; + var k = 0; + ${r} + value = x[offset]; + `},Xh=(e,t,n)=>{let r="";for(let i=t-1;i>=0;--i)r+=` + k = i32(${e.indicesGet("indices",i)}) - ${we("uniforms.pads",i,n)}; + if (k < 0) { + k = 0; + } + if (k >= i32(${we("uniforms.x_shape",i,t)})) { + k = i32(${we("uniforms.x_shape",i,t)}) - 1; + } + offset += k * i32(${we("uniforms.x_strides",i,t)}); + `;return` + var offset = 0; + var k = 0; + ${r} + value = x[offset]; + `},Yh=(e,t,n)=>{let r="";for(let i=t-1;i>=0;--i)r+=` + k = i32(${e.indicesGet("indices",i)}) - ${we("uniforms.pads",i,n)}; + if (k < 0) { + k += i32(${we("uniforms.x_shape",i,t)}]); + } + if (k >= i32(${we("uniforms.x_shape",i,t)})) { + k -= i32(${we("uniforms.x_shape",i,t)}); + } + offset += k * i32(${we("uniforms.x_strides",i,t)}); + `;return` + var offset = 0; + var k = 0; + ${r} + value = x[offset]; + `},Zh=(e,t,n)=>{switch(n.mode){case 0:return jh(e,t,n.pads.length);case 1:return Kh(e,t,n.pads.length);case 2:return Xh(e,t,n.pads.length);case 3:return Yh(e,t,n.pads.length);default:throw new Error("Invalid mode")}},Qh=(e,t)=>{let n=W.padShape(e[0].dims.slice(),t.pads),r=e[0].dims,i=W.size(n),s=[{type:12,data:i},{type:6,data:t.pads}],a=e.length>=3&&e[2].data;t.mode===0&&s.push({type:a?e[2].dataType:1,data:t.value}),s.push(..._e(e[0].dims,n));let o=["rank"],l=d=>{let p=be("output",e[0].dataType,n.length),f=V("x",e[0].dataType,r.length),c=f.type.value,m=Zh(p,r.length,t),y=[{name:"output_size",type:"u32"},{name:"pads",type:"i32",length:t.pads.length}];return t.mode===0&&y.push({name:"constant_value",type:a?c:"f32"}),` + ${d.registerUniforms(y).declareVariables(f,p)} + ${d.mainStart()} + ${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + + let indices = ${p.offsetToIndices("global_idx")}; + + var value = ${c}(0); + ${m} + output[global_idx] = value; + }`};return{name:"Pad",shaderCache:{hint:`${t.mode}${a}`,inputDependencies:o},getRunData:()=>({outputs:[{dims:n,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(W.size(n)/64)},programUniforms:s}),getShaderSource:l}},Jh=(e,t)=>{if(e.length>1){let n=e[1].getBigInt64Array(),r=e.length>=3&&e[2].data?e[2].dataType===10?e[2].getUint16Array()[0]:e[2].getFloat32Array()[0]:0,i=e[0].dims.length,s=new Int32Array(2*i).fill(0);if(e.length>=4){let o=e[3].getBigInt64Array();for(let l=0;ls[Number(l)]=Number(o));let a=[];return s.forEach(o=>a.push(o)),{mode:t.mode,value:r,pads:a}}else return t},pw=(e,t)=>{Hh(e.inputs);let n=Jh(e.inputs,t);e.compute(Qh(e.inputs,n),{inputs:[0]})}}),as,xl,$l,Sl,Tl,em,tm,Il,Cl,fw,hw,El,mw,gw,kl,yw,bw,ww,_w,Z$=re(()=>{on(),Te(),Ce(),Ee(),as=e=>{if(Be.webgpu.validateInputContent&&(!e||e.length!==1))throw new Error("Pool ops requires 1 input.")},xl=(e,t,n)=>{let r=t.format==="NHWC",i=e.dims.slice();r&&i.splice(1,0,i.pop());let s=Object.hasOwnProperty.call(t,"dilations"),a=t.kernelShape.slice(),o=t.strides.slice(),l=s?t.dilations.slice():[],d=t.pads.slice();La.adjustPoolAttributes(n,i,a,o,l,d);let p=La.computePoolOutputShape(n,i,o,l,a,d,t.autoPad),f=Object.assign({},t);s?Object.assign(f,{kernelShape:a,strides:o,pads:d,dilations:l,cacheKey:t.cacheKey}):Object.assign(f,{kernelShape:a,strides:o,pads:d,cacheKey:t.cacheKey});let c=p.slice();return c.push(c.splice(1,1)[0]),[f,r?c:p]},$l=(e,t)=>{let n=t.format==="NHWC",r=W.size(e),i=W.size(t.kernelShape),s=[{type:12,data:r},{type:12,data:i}],a=[{name:"outputSize",type:"u32"},{name:"kernelSize",type:"u32"}];if(t.kernelShape.length<=2){let o=t.kernelShape[t.kernelShape.length-1],l=t.strides[t.strides.length-1],d=t.pads[t.pads.length/2-1],p=t.pads[t.pads.length-1],f=!!(d+p);s.push({type:12,data:o},{type:12,data:l},{type:12,data:d},{type:12,data:p}),a.push({name:"kw",type:"u32"},{name:"sw",type:"u32"},{name:"pwStart",type:"u32"},{name:"pwEnd",type:"u32"});let c=!1;if(t.kernelShape.length===2){let m=t.kernelShape[t.kernelShape.length-2],y=t.strides[t.strides.length-2],w=t.pads[t.pads.length/2-2],$=t.pads[t.pads.length-2];c=!!(w+$),s.push({type:12,data:m},{type:12,data:y},{type:12,data:w},{type:12,data:$}),a.push({name:"kh",type:"u32"},{name:"sh",type:"u32"},{name:"phStart",type:"u32"},{name:"phEnd",type:"u32"})}return[s,a,!0,f,c]}else{if(n)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let o=W.computeStrides(t.kernelShape);s.push({type:12,data:o},{type:12,data:t.pads},{type:12,data:t.strides}),a.push({name:"kernelStrides",type:"u32",length:o.length},{name:"pads",type:"u32",length:t.pads.length},{name:"strides",type:"u32",length:t.strides.length});let l=t.pads.reduce((d,p)=>d+p);return[s,a,!!l,!1,!1]}},Sl=(e,t,n,r,i,s,a,o,l,d,p,f)=>{let c=i.format==="NHWC",m=t.type.value,y=be("output",t.type.tensor,r);if(i.kernelShape.length<=2){let w="",$="",_="",v=n-(c?2:1);if(p?w=` + for (var i: u32 = 0u; i < uniforms.kw; i++) { + xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; + if (xIndices[${v}] < 0 || xIndices[${v}] + >= uniforms.x_shape[${v}]) { + pad++; + continue; + } + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${s} + }`:w=` + for (var i: u32 = 0u; i < uniforms.kw; i++) { + xIndices[${v}] = indices[${v}] * uniforms.sw - uniforms.pwStart + i; + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${s} + }`,i.kernelShape.length===2){let I=n-(c?3:2);f?$=` + for (var j: u32 = 0u; j < uniforms.kh; j++) { + xIndices[${I}] = indices[${I}] * uniforms.sh - uniforms.phStart + j; + if (xIndices[${I}] < 0 || xIndices[${I}] >= uniforms.x_shape[${I}]) { + pad += i32(uniforms.kw); + continue; + } + `:$=` + for (var j: u32 = 0u; j < uniforms.kh; j++) { + xIndices[${I}] = indices[${I}] * uniforms.sh - uniforms.phStart + j; + `,_=` + } + `}return` + ${e.registerUniforms(l).declareVariables(t,y)} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + + let indices = ${y.offsetToIndices("global_idx")}; + var xIndices = ${y.offsetToIndices("global_idx")}; + + var value = ${m}(${o}); + var pad = 0; + ${$} + ${w} + ${_} + ${a} + + output[global_idx] = value; + }`}else{if(c)throw new Error("Pooling with kernelShape.length > 2 is not supported for NHWC format.");let w=i.kernelShape.length,$=i.pads.length,_="";return d?_=` + if (xIndices[j] >= uniforms.x_shape[j]) { + pad++; + isPad = true; + break; + } + } + if (!isPad) { + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${s} + }`:_=` + } + let x_val = x[${t.indicesToOffset("xIndices")}]; + ${s} + `,` + ${e.registerUniforms(l).declareVariables(t,y)} + + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + let indices = ${y.offsetToIndices("global_idx")}; + var xIndices = ${y.offsetToIndices("global_idx")}; + + var offsets: array; + + var value = ${m}(${o}); + var pad = 0; + var isPad = false; + + for (var i: u32 = 0u; i < uniforms.kernelSize; i++) { + var offset = i; + for (var j = 0u; j < ${w-1}u; j++) { + offsets[j] = offset / ${we("uniforms.kernelStrides","j",w)}; + offset -= offsets[j] * ${we("uniforms.kernelStrides","j",w)}; + } + offsets[${w-1}] = offset; + + isPad = false; + for (var j = ${n-w}u; j < ${n}u; j++) { + xIndices[j] = indices[j] * ${we("uniforms.strides",`j - ${n-w}u`,w)} + + offsets[j - ${n-w}u] - ${we("uniforms.pads","j - 2u",$)}; + ${_} + } + ${a} + + output[global_idx] = value; + }`}},Tl=e=>`${e.format};${e.ceilMode};${e.autoPad};${e.kernelShape.length}`,em=e=>`${Tl(e)};${e.countIncludePad}`,tm=e=>`${Tl(e)};${e.storageOrder};${e.dilations}`,Il=e=>({format:e.format,autoPad:["NOTSET","VALID","SAME_UPPER","SAME_LOWER"][e.auto_pad],ceilMode:e.ceil_mode,kernelShape:e.kernel_shape,strides:e.strides,pads:e.pads}),Cl=(e,t,n,r)=>{let[i,s]=xl(t,r,n),a=V("x",t.dataType,t.dims.length),o=a.type.value,l="value += x_val;",d="";i.countIncludePad?d+=`value /= ${o}(uniforms.kernelSize);`:d+=`value /= ${o}(i32(uniforms.kernelSize) - pad);`;let[p,f,c,m,y]=$l(s,i);p.push(..._e(t.dims,s));let w=["rank"];return{name:e,shaderCache:{hint:`${r.cacheKey};${c};${m};${y}`,inputDependencies:w},getRunData:()=>({outputs:[{dims:s,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(W.size(s)/64)},programUniforms:p}),getShaderSource:$=>Sl($,a,t.dims.length,s.length,i,l,d,0,f,c,m,y)}},fw=e=>{let t=e.count_include_pad!==0,n=Il(e);if(n.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");let r={countIncludePad:t,...n,cacheKey:""};return{...r,cacheKey:em(r)}},hw=(e,t)=>{as(e.inputs),e.compute(Cl("AveragePool",e.inputs[0],!1,t))},El={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[]},mw=e=>{let t=e.format;return{format:t,...El,cacheKey:t}},gw=(e,t)=>{as(e.inputs),e.compute(Cl("GlobalAveragePool",e.inputs[0],!0,t))},kl=(e,t,n,r)=>{let[i,s]=xl(t,r,n),a=` + value = max(x_val, value); + `,o="",l=V("x",t.dataType,t.dims.length),d=["rank"],[p,f,c,m,y]=$l(s,i);return p.push(..._e(t.dims,s)),{name:e,shaderCache:{hint:`${r.cacheKey};${c};${m};${y}`,inputDependencies:d},getRunData:()=>({outputs:[{dims:s,dataType:t.dataType}],dispatchGroup:{x:Math.ceil(W.size(s)/64)},programUniforms:p}),getShaderSource:w=>Sl(w,l,t.dims.length,s.length,i,a,o,t.dataType===10?-65504:-1e5,f,c,m,y)}},yw=(e,t)=>{as(e.inputs),e.compute(kl("MaxPool",e.inputs[0],!1,t))},bw=e=>{let t=e.storage_order,n=e.dilations,r=Il(e);if(t!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(r.ceilMode!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");let i={storageOrder:t,dilations:n,...r,cacheKey:""};return{...i,cacheKey:tm(i)}},ww=e=>{let t=e.format;return{format:t,...El,cacheKey:t}},_w=(e,t)=>{as(e.inputs),e.compute(kl("GlobalMaxPool",e.inputs[0],!0,t))}}),nm,rm,vw,xw,Q$=re(()=>{Te(),Ce(),pt(),Ee(),nm=(e,t)=>{if(e.length<2||e.length>3)throw new Error("DequantizeLinear requires 2 or 3 inputs.");if(e.length===3&&e[1].dims===e[2].dims)throw new Error("x-scale and x-zero-point must have the same shape.");if(e.length===3&&e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[0].dataType===6&&e.length>2)throw new Error("In the case of dequantizing int32 there is no zero point.");if(e[1].dims.length!==0&&e[1].dims.length!==1&&e[1].dims.length!==e[0].dims.length)throw new Error("scale input must be a scalar, a 1D tensor, or have the same rank as the input tensor.");if(e.length>2){if(e[0].dataType!==e[2].dataType)throw new Error("x and x-zero-point must have the same data type.");if(e[1].dims.length!==e[2].dims.length)throw new Error("scale and zero-point inputs must have the same rank.");if(!e[1].dims.map((n,r)=>n===e[2].dims[r]).reduce((n,r)=>n&&r,!0))throw new Error("scale and zero-point inputs must have the same shape.")}if(t.blockSize>0){if(e[1].dims.length===0||e[1].dims.length===1&&e[1].dims[0]===1)throw new Error("blockSize must be set only for block quantization.");if(!e[1].dims.map((i,s)=>s===t.axis||i===e[0].dims[s]).reduce((i,s)=>i&&s,!0))throw new Error("For block qunatization, scale input shape to match the input shape except for the axis");if(e[1].dims.length!==e[0].dims.length)throw new Error("For block qunatization the scale input rank must be the same as the x rank.");let n=e[0].dims[t.axis],r=e[1].dims[t.axis];if(t.blockSizeMath.ceil(n/(r-1)-1))throw new Error("blockSize must be with in the range [ceil(dI / Si), ceil(dI / (Si - 1) - 1)].")}},rm=(e,t)=>{let n=W.normalizeAxis(t.axis,e[0].dims.length),r=e[0].dataType,i=r===3,s=e[0].dims,a=e[1].dataType,o=W.size(s),l=r===3||r===2,d=l?[Math.ceil(W.size(e[0].dims)/4)]:e[0].dims,p=e[1].dims,f=e.length>2?e[2]:void 0,c=f?l?[Math.ceil(W.size(f.dims)/4)]:f.dims:void 0,m=p.length===0||p.length===1&&p[0]===1,y=m===!1&&p.length===1,w=ct(o),$=m&&(!l||w===4),_=$?w:1,v=$&&!l?w:1,I=V("input",l?12:r,d.length,v),S=V("scale",a,p.length),E=f?V("zero_point",l?12:r,c.length):void 0,R=be("output",a,s.length,_),O=[I,S];E&&O.push(E);let x=[d,p];f&&x.push(c);let U=[{type:12,data:o/_},{type:12,data:n},{type:12,data:t.blockSize},..._e(...x,s)],q=J=>{let se=[{name:"output_size",type:"u32"},{name:"axis",type:"u32"},{name:"block_size",type:"u32"}];return` + ${J.registerUniforms(se).declareVariables(...O,R)} + ${J.mainStart()} + ${J.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let output_indices = ${R.offsetToIndices("global_idx")}; + + // Set input x + ${l?` + let input = ${I.getByOffset("global_idx / 4")}; + let x_vec = ${i?"unpack4xI8(input)":"unpack4xU8(input)"}; + let x_value = ${_===1?"x_vec[global_idx % 4]":"x_vec"};`:`let x_value = ${I.getByOffset("global_idx")};`}; + + // Set scale input + ${m?`let scale_value= ${S.getByOffset("0")}`:y?` + let scale_index = ${R.indicesGet("output_indices","uniforms.axis")}; + let scale_value= ${S.getByOffset("scale_index")};`:` + var scale_indices: ${S.type.indices} = output_indices; + let index = ${S.indicesGet("scale_indices","uniforms.axis")} / uniforms.block_size; + ${S.indicesSet("scale_indices","uniforms.axis","index")}; + let scale_value= ${S.getByIndices("scale_indices")};`}; + + // Set zero-point input + ${E?m?l?` + let zero_point_input = ${E.getByOffset("0")}; + let zero_point_vec = ${i?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; + let zero_point_value= zero_point_vec[0]`:`let zero_point_value = ${E.getByOffset("0")}`:y?l?` + let zero_point_index = ${R.indicesGet("output_indices","uniforms.axis")}; + let zero_point_input = ${E.getByOffset("zero_point_index / 4")}; + let zero_point_vec = ${i?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; + let zero_point_value = zero_point_vec[zero_point_index % 4]`:` + let zero_point_index = ${R.indicesGet("output_indices","uniforms.axis")}; + let zero_point_value = ${E.getByOffset("zero_point_index")};`:l?` + let zero_point_offset = ${S.indicesToOffset("scale_indices")}; + let zero_point_input = ${E.getByOffset("zero_point_offset / 4")}; + let zero_point_vec = ${i?"unpack4xI8(zero_point_input)":"unpack4xU8(zero_point_input)"}; + let zero_point_value = zero_point_vec[zero_point_offset % 4];`:`let zero_point_value = ${E.getByIndices("scale_indices")};`:`let zero_point_value = ${l?i?"i32":"u32":I.type.value}(0);`}; + // Compute and write output + ${R.setByOffset("global_idx",`${R.type.value}(x_value - zero_point_value) * scale_value`)}; + }`};return{name:"DequantizeLinear",shaderCache:{hint:t.cacheKey,inputDependencies:E?["rank","rank","rank"]:["rank","rank"]},getShaderSource:q,getRunData:()=>({outputs:[{dims:s,dataType:a}],dispatchGroup:{x:Math.ceil(o/_/64),y:1,z:1},programUniforms:U})}},vw=(e,t)=>{nm(e.inputs,t),e.compute(rm(e.inputs,t))},xw=e=>Fe({axis:e.axis,blockSize:e.blockSize})}),im,sm,$w,J$=re(()=>{on(),Te(),Ee(),im=(e,t,n)=>{let r=e===t,i=et&&n>0;if(r||i||s)throw new Error("Range these inputs' contents are invalid.")},sm=(e,t,n,r)=>{let i=Math.abs(Math.ceil((t-e)/n)),s=[i],a=i,o=[{type:12,data:a},{type:r,data:e},{type:r,data:n},..._e(s)],l=d=>{let p=be("output",r,s.length),f=p.type.value,c=[{name:"outputSize",type:"u32"},{name:"start",type:f},{name:"delta",type:f}];return` + ${d.registerUniforms(c).declareVariables(p)} + ${d.mainStart()} + ${d.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + output[global_idx] = uniforms.start + ${f}(global_idx) * uniforms.delta; + }`};return{name:"Range",shaderCache:{hint:`${r}`},getShaderSource:l,getRunData:()=>({outputs:[{dims:s,dataType:r}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:o})}},$w=e=>{let t=0,n=0,r=0;e.inputs[0].dataType===6?(t=e.inputs[0].getInt32Array()[0],n=e.inputs[1].getInt32Array()[0],r=e.inputs[2].getInt32Array()[0]):e.inputs[0].dataType===1&&(t=e.inputs[0].getFloat32Array()[0],n=e.inputs[1].getFloat32Array()[0],r=e.inputs[2].getFloat32Array()[0]),Be.webgpu.validateInputContent&&im(t,n,r),e.compute(sm(t,n,r,e.inputs[0].dataType),{inputs:[]})}}),am,om,Sw,Tw,e2=re(()=>{Te(),Ce(),pt(),Ee(),am=(e,t,n,r)=>{if(e!=="none"&&r!=="i32"&&r!=="u32"&&r!=="f32")throw new Error(`Input ${r} is not supported with reduction ${e}.`);let i=`{ + var oldValue = 0; + loop { + let newValueF32 =`,s=`; + let newValue = bitcast(newValueF32); + let res = atomicCompareExchangeWeak(&${t}, oldValue, newValue); + if res.exchanged { + break; + } + oldValue = res.old_value; + } + }`;switch(e){case"none":return`${t}=${n};`;case"add":return r==="i32"||r==="u32"?`atomicAdd(&${t}, bitcast<${r}>(${n}));`:` + ${i}bitcast<${r}>(oldValue) + (${n})${s}`;case"max":return r==="i32"||r==="u32"?`atomicMax(&${t}, bitcast<${r}>(${n}));`:` + ${i}max(bitcast(oldValue), (${n}))${s}`;case"min":return r==="i32"||r==="u32"?`atomicMin(&${t}, bitcast<${r}>(${n}));`:`${i}min(bitcast<${r}>(oldValue), (${n}))${s}`;case"mul":return`${i}(bitcast<${r}>(oldValue) * (${n}))${s}`;default:throw new Error(`Reduction ${e} is not supported.`)}},om=(e,t)=>{let n=e[0].dims,r=e[1].dims,i=n,s=1,a=Math.ceil(W.sizeToDimension(r,r.length-1)/s),o=r[r.length-1],l=W.sizeFromDimension(n,o),d=[{type:12,data:a},{type:12,data:o},{type:12,data:l},..._e(e[1].dims,e[2].dims,i)],p=f=>{let c=V("indices",e[1].dataType,e[1].dims.length),m=V("updates",e[2].dataType,e[2].dims.length,s),y=t.reduction!=="none"&&t.reduction!==""?J0("output",e[0].dataType,i.length):be("output",e[0].dataType,i.length,s);return` + ${f.registerUniform("output_size","u32").registerUniform("last_index_dimension","u32").registerUniform("num_updates_elements","u32").declareVariables(c,m,y)} + ${f.mainStart()} + ${f.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + var data_offset = 0u; + let indices_start = uniforms.last_index_dimension * global_idx; + let indices_end = indices_start + uniforms.last_index_dimension; + for (var i = indices_start; i < indices_end; i++) { + var index = i32(indices[i].x); + ${e[0].dims.length===1?` + let element_count_dim = uniforms.output_strides; + let dim_value = uniforms.output_shape;`:` + let element_count_dim = uniforms.output_strides[i - indices_start]; + let dim_value = uniforms.output_shape[i - indices_start];`} + if (index >= 0) { + if (index >= i32(dim_value)) { + index = i32(dim_value - 1); + } + } else { + if (index < -i32(dim_value)) { + index = 0; + } else { + index += i32(dim_value); + } + } + data_offset += u32((u32(index) * element_count_dim)); + } + + for (var i = 0u; i < uniforms.num_updates_elements; i++) { + let value = updates[uniforms.num_updates_elements * global_idx + i]; + ${am(t.reduction,"output[data_offset + i]","value",y.type.value)} + } + + }`};return{name:"ScatterND",shaderCache:{hint:`${t.cacheKey}_${t.reduction}`,inputDependencies:["rank","rank"]},getRunData:()=>({outputs:[{dims:i,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(a/64)},programUniforms:d}),getShaderSource:p}},Sw=e=>Fe({reduction:e.reduction}),Tw=(e,t)=>{e.compute(om(e.inputs,t),{inputs:[e.inputs[1],e.inputs[2]],outputs:[]})}}),lm,um,dm,zl,cm,pm,fm,hm,mm,gm,ym,bm,Al,wm,_m,vm,xm,$m,Iw,Cw,t2=re(()=>{Te(),Ce(),pt(),Ee(),lm=(e,t)=>{if(e.every(n=>n>0||(()=>{throw new Error("Resize requires scales input values to be positive")})),e.length>0){if(t.mode==="linear"){if(!(e.length===2||e.length===3||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1||e.length===5&&e[0]===1&&e[1]===1))throw new Error(`For linear mode, Resize requires scales to be 2D, 3D, 4D with either two outermost or one innermost and + one outermost scale values equal to 1, or 5D with two outermost scale values equal to 1`)}else if(t.mode==="cubic"&&!(e.length===2||e.length===4&&e[0]===1&&e[1]===1||e.length===4&&e[0]===1&&e[3]===1))throw new Error("Resize requires scales input size to be 2 or 4 for cubic mode")}},um=(e,t,n)=>{t.every(i=>i>=0&&i{throw new Error("Resize requires axes input values to be positive and less than rank")}));let r=new Array(n).fill(1);return t.forEach((i,s)=>r[i]=e[s]),r},dm=(e,t,n,r,i,s)=>{let[a,o,l]=n>10?[1,2,3]:[-1,e.length>1?1:-1,-1],d=e[0].dims.length;if(a>0&&e.length>a&&e[a].dims.length>0)e[a].getFloat32Array().forEach(p=>s.push(p));else if(t.coordinateTransformMode==="tf_crop_and_resize")throw new Error("Resize requires RoI input to be specified when coordinateTransformMode is tfCropAndResize");if(o>0&&e.length>o&&e[o].dims.length===1&&e[o].dims[0]>0){if(e[o].getFloat32Array().forEach(p=>r.push(p)),r.length!==0&&r.length!==d&&n>=18&&r.length!==t.axes.length)throw new Error("Resize requires scales input size to be same as input rank or axes size for opset 18 and up");lm(r,t),t.axes.length>0&&um(r,t.axes,d).forEach((p,f)=>r[f]=p)}if(l>0&&e.length>l&&e[l].dims.length===1&&e[l].dims[0]>0&&(e[l].getBigInt64Array().forEach(p=>i.push(Number(p))),i.length!==0&&i.length!==d&&n>=18&&i.length!==t.axes.length))throw new Error("Resize requires sizes input size to be same as input rank or axes size for opset 18 and up");if(t.axes.length>0){if(r.length!==0&&r.length!==t.axes.length)throw new Error('Resize requires "scales" input size to be of axes rank when axes attributes is specified');if(i.length!==0&&i.length!==t.axes.length)throw new Error('Resize requires "sizes" input size to be of rank axes rank when axes attributes is specified')}if(typeof r<"u"&&typeof i<"u"&&r.length>0&&i.length>d)throw new Error("Resize requires only of scales or sizes to be specified")},zl=(e,t,n,r)=>` + // The whole part and the fractional part are calculated separately due to inaccuracy of floating + // point division. As an example, f32(21) / f32(7) may evaluate to 2.99... instead of 3, causing an + // offset-by-one error later in floor(). + let big = (${e}) * (${t}); + let whole = ${r}(big / (${n})); + let fract = ${r}(big % (${n})) / ${r}(${n}); + return whole + fract; +`,cm=(e,t)=>`fn getOriginalCoordinateFromResizedCoordinate(xResized: u32, xScale: f32, lengthResized: u32, + lengthOriginal: u32, roiStart: f32, roiEnd: f32) -> ${t} { `+(()=>{switch(e){case"asymmetric":return` + if (xScale < 1.0 || floor(xScale) != xScale) { + return ${t}(xResized) / ${t}(xScale); + } else { + ${zl("xResized","lengthOriginal","lengthResized",t)} + } + `;case"pytorch_half_pixel":return`if (lengthResized > 1) { + return (${t}(xResized) + 0.5) / ${t}(xScale) - 0.5; + } else { + return 0.0; + }`;case"tf_half_pixel_for_nn":return`return (${t}(xResized) + 0.5) / ${t}(xScale);`;case"align_corners":return`if (lengthResized == 1) { + return 0.0; + } else { + ${zl("xResized","lengthOriginal - 1","lengthResized - 1",t)} + }`;case"tf_crop_and_resize":return`if (lengthResized > 1) { + return ${t}(roiStart) * ${t}(lengthOriginal - 1) + + (${t}(xResized) * ${t}(roiEnd - roiStart) * ${t}(lengthOriginal - 1)) / + ${t}(lengthResized - 1); + } else { + return 0.5 * ${t}(roiStart + roiEnd) * ${t}(lengthOriginal - 1); + }`;case"half_pixel_symmetric":return`const outputWidth = ${t}xScale * ${t}(lengthResized); + const adjustment = ${t}(lengthResized) / outputWidth; + const center = ${t}(lengthOriginal) / 2; + const offset = center * (1 - adjustment); + return offset + ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;case"half_pixel":return`return ((${t}(xResized) + 0.5) / ${t}(xScale)) - 0.5;`;default:throw new Error(`Coordinate transform mode ${e} is not supported`)}})()+"}",pm=(e,t,n)=>`fn getNearestPixelFromOriginal(xOriginal: ${n}, isDownSample: bool) -> ${n} {`+(()=>{switch(e){case"round_prefer_ceil":return"if (fract(xOriginal) == 0.5) { return ceil(xOriginal); } else { return round(xOriginal); }";case"floor":return"return floor(xOriginal);";case"ceil":return"return ceil(xOriginal);";case"round_prefer_floor":return"if (fract(xOriginal) == 0.5) { return floor(xOriginal); } else { return round(xOriginal); }";case"simple":default:if(t<11)return"if (isDownSample) { return ceil(xOriginal); } else { return xOriginal; }";throw new Error(`Nearest mode ${e} is not supported`)}})()+"}",fm=(e,t,n)=>{let r=new Array(n).fill(0).concat(new Array(n).fill(1)),i=e.length===0?r:e.slice();return t.length>0?(t.forEach((s,a)=>{r[s]=i[a],r[a+n]=i[t.length+a]}),r):i},hm=(e,t,n,r)=>{let i=[];if(n.length>0)if(r.length>0){if(e.forEach(s=>i.push(s)),Math.max(...r)>e.length)throw new Error("axes is out of bound");r.forEach((s,a)=>i[s]=n[a])}else n.forEach(s=>i.push(s));else{if(t.length===0)throw new Error("Resize requires either scales or sizes.");i=e.map((s,a)=>Math.round(s*t[a]))}return i},mm=(e,t,n)=>{let r=(()=>{switch(n.keepAspectRatioPolicy){case"not_larger":return n.axes.length>0?Math.min(...n.axes.map(s=>t[s]),Number.MAX_VALUE):Math.min(...t,Number.MAX_VALUE);case"not_smaller":return n.axes.length>0?Math.max(...n.axes.map(s=>t[s]),Number.MIN_VALUE):Math.max(...t,Number.MIN_VALUE);default:throw new Error(`Keep aspect ratio policy ${n.keepAspectRatioPolicy} is not supported`)}})();t.fill(1,0,t.length);let i=e.slice();return n.axes.length>0?(n.axes.forEach(s=>t[s]=r),n.axes.forEach(s=>i[s]=Math.round(e[s]*t[s]))):(t.fill(r,0,t.length),i.forEach((s,a)=>i[a]=Math.round(s*t[a]))),i},gm=(e,t,n,r,i)=>` + fn calculateOriginalIndicesFromOutputIndices(output_indices: ${e.type.indices}) -> array<${e.type.value}, ${n.length}> { + var original_indices: array<${e.type.value}, ${n.length}>; + for (var i:u32 = 0; i < ${n.length}; i++) { + var output_index = ${e.indicesGet("output_indices","i")}; + var scale = ${we("uniforms.scales","i",r)}; + var roi_low = ${we("uniforms.roi","i",i)}; + var roi_hi = ${we("uniforms.roi",`i + ${t.length}`,i)}; + if (scale == 1.0) { + original_indices[i] = ${e.type.value}(output_index); + } else { + var input_shape_i = ${we("uniforms.input_shape","i",t.length)}; + var output_shape_i = ${we("uniforms.output_shape","i",n.length)}; + original_indices[i] = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, + input_shape_i, roi_low, roi_hi); + } + } + return original_indices; + }`,ym=(e,t,n,r,i,s,a)=>` + fn calculateInputIndicesFromOutputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + var input_indices: ${e.type.indices}; + for (var i:u32 = 0; i < ${r.length}; i++) { + var output_index = ${t.indicesGet("output_indices","i")}; + var input_index: u32; + var scale = ${we("uniforms.scales","i",i)}; + if (scale == 1.0) { + input_index = output_index; + } else { + var roi_low = ${we("uniforms.roi","i",s)}; + var roi_hi = ${we("uniforms.roi",`i + ${n.length}`,s)}; + var input_shape_i = ${we("uniforms.input_shape","i",n.length)}; + var output_shape_i = ${we("uniforms.output_shape","i",r.length)}; + var original_idx = getOriginalCoordinateFromResizedCoordinate(output_index, scale, output_shape_i, + input_shape_i, roi_low, roi_hi); + if (!${a} || (original_idx >= 0 && original_idx < ${t.type.value}(input_shape_i))) { + if (original_idx < 0) { + input_index = 0; + } else if (original_idx > ${t.type.value}(input_shape_i - 1)) { + input_index = input_shape_i - 1; + } else { + input_index = u32(getNearestPixelFromOriginal(original_idx, scale < 1)); + } + } else { + input_index = u32(original_idx); + } + } + ${e.indicesSet("input_indices","i","input_index")} + } + return input_indices; + }`,bm=(e,t)=>` + fn checkInputIndices(input_indices: ${e.type.indices}) -> bool { + for (var i:u32 = 0; i < ${t.length}; i++) { + var input_index = ${e.indicesGet("input_indices","i")}; + if (input_index < 0 || input_index >= ${we("uniforms.input_shape","i",t.length)}) { + return false; + } + } + return true; + }`,Al=(e,t,n,r)=>e.rank>r?` + ${e.indicesSet("input_indices",t,"channel")}; + ${e.indicesSet("input_indices",n,"batch")}; +`:"",wm=(e,t,n,r,i)=>{let[s,a,o,l]=n.length===2?[-1,0,1,-1]:[0,2,3,1],d=e.type.value;return` + fn getInputValue(batch: u32, channel: u32, row: u32, col: u32) -> ${d} { + var input_indices: ${e.type.indices}; + ${e.indicesSet("input_indices",a,`max(0, min(row, ${n[a]} - 1))`)}; + ${e.indicesSet("input_indices",o,`max(0, min(col, ${n[o]} - 1))`)}; + ${Al(e,l,s,2)} + return ${e.getByIndices("input_indices")}; + } + + fn bilinearInterpolation(output_indices: ${t.type.indices}) -> ${d} { + var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); + var row:${d} = originalIndices[${a}]; + var col:${d} = originalIndices[${o}]; + ${r?`if (row < 0 || row > (${n[a]} - 1) || col < 0 || col > (${n[o]} - 1)) { + return ${i}; + }`:""}; + row = max(0, min(row, ${n[a]} - 1)); + col = max(0, min(col, ${n[o]} - 1)); + var row1: u32 = u32(row); + var col1: u32 = u32(col); + var row2: u32 = u32(row + 1); + var col2: u32 = u32(col + 1); + var channel: u32 = ${n.length>2?`u32(originalIndices[${l}])`:"0"}; + var batch: u32 = ${n.length>2?`u32(originalIndices[${s}])`:"0"}; + var x11: ${d} = getInputValue(batch, channel, row1, col1); + var x12: ${d} = getInputValue(batch, channel, row1, col2); + var x21: ${d} = getInputValue(batch, channel, row2, col1); + var x22: ${d} = getInputValue(batch, channel, row2, col2); + var dx1: ${d} = abs(row - ${d}(row1)); + var dx2: ${d} = abs(${d}(row2) - row); + var dy1: ${d} = abs(col - ${d}(col1)); + var dy2: ${d} = abs(${d}(col2) - col); + if (row1 == row2) { + dx1 = 0.5; + dx2 = 0.5; + } + if (col1 == col2) { + dy1 = 0.5; + dy2 = 0.5; + } + return (x11 * dx2 * dy2 + x12 * dx2 * dy1 + x21 * dx1 * dy2 + x22 * dx1 * dy1); + }`},_m=(e,t,n,r,i,s,a,o,l,d)=>{let p=n.length===2,[f,c]=p?[0,1]:[2,3],m=e.type.value,y=w=>{let $=w===f?"row":"col";return` + fn ${$}CubicInterpolation(input_indices: ${e.type.indices}, output_indices: ${t.type.indices}) -> ${m} { + var output_index = ${t.indicesGet("output_indices",w)}; + var originalIdx: ${m} = getOriginalCoordinateFromResizedCoordinate(output_index, ${i[w]}, + ${r[w]}, ${n[w]}, ${s[w]}, ${s[w]} + ${n.length}); + var fractOriginalIdx: ${m} = originalIdx - floor(originalIdx); + var coefs = getCubicInterpolationCoefs(fractOriginalIdx); + + if (${o} && (originalIdx < 0 || originalIdx > (${n[w]} - 1))) { + return ${l}; + } + var data: array<${m}, 4> = array<${m}, 4>(0.0, 0.0, 0.0, 0.0); + for (var i: i32 = -1; i < 3; i++) { + var ${$}: ${m} = originalIdx + ${m}(i); + if (${$} < 0 || ${$} >= ${n[w]}) { + ${d?`coefs[i + 1] = 0.0; + continue;`:o?`return ${l};`:`${$} = max(0, min(${$}, ${n[w]} - 1));`}; + } + var input_indices_copy: ${e.type.indices} = input_indices; + ${e.indicesSet("input_indices_copy",w,`u32(${$})`)}; + data[i + 1] = ${w===f?e.getByIndices("input_indices_copy"):"rowCubicInterpolation(input_indices_copy, output_indices)"}; + } + return cubicInterpolation1D(data, coefs); + }`};return` + ${y(f)}; + ${y(c)}; + fn getCubicInterpolationCoefs(s: ${m}) -> array<${m}, 4> { + var absS = abs(s); + var coeffs: array<${m}, 4> = array<${m}, 4>(0.0, 0.0, 0.0, 0.0); + var oneMinusAbsS: ${m} = 1.0 - absS; + var twoMinusAbsS: ${m} = 2.0 - absS; + var onePlusAbsS: ${m} = 1.0 + absS; + coeffs[0] = ((${a} * onePlusAbsS - 5 * ${a}) * onePlusAbsS + 8 * ${a}) * onePlusAbsS - 4 * ${a}; + coeffs[1] = ((${a} + 2) * absS - (${a} + 3)) * absS * absS + 1; + coeffs[2] = ((${a} + 2) * oneMinusAbsS - (${a} + 3)) * oneMinusAbsS * oneMinusAbsS + 1; + coeffs[3] = ((${a} * twoMinusAbsS - 5 * ${a}) * twoMinusAbsS + 8 * ${a}) * twoMinusAbsS - 4 * ${a}; + return coeffs; + } + + fn cubicInterpolation1D(x: array<${m}, 4>, coefs: array<${m}, 4>) -> ${m} { + var coefsSum: ${m} = coefs[0] + coefs[1] + coefs[2] + coefs[3]; + return (x[0] * coefs[0] + x[1] * coefs[1]+ x[2] * coefs[2]+ x[3] * coefs[3]) / coefsSum; + } + + fn bicubicInterpolation(output_indices: ${t.type.indices}) -> ${m} { + var input_indices: ${e.type.indices} = output_indices; + return colCubicInterpolation(input_indices, output_indices); + } + `},vm=(e,t,n,r,i)=>{let[s,a,o,l,d]=n.length===3?[-1,0,1,2,-1]:[0,2,3,4,1],p=e.type.value;return` + fn getInputValue(batch: u32, channel: u32, depth:u32, height: u32, width: u32) -> ${p} { + var input_indices: ${e.type.indices}; + ${e.indicesSet("input_indices",a,`max(0, min(depth, ${n[a]} - 1))`)}; + ${e.indicesSet("input_indices",o,`max(0, min(height, ${n[o]} - 1))`)}; + ${e.indicesSet("input_indices",l,`max(0, min(width, ${n[l]} - 1))`)}; + ${Al(e,d,s,3)} + return ${e.getByIndices("input_indices")}; + } + + fn trilinearInterpolation(output_indices: ${t.type.indices}) -> ${p} { + var originalIndices = calculateOriginalIndicesFromOutputIndices(output_indices); + var depth:${p} = originalIndices[${a}]; + var height:${p} = originalIndices[${o}]; + var width:${p} = originalIndices[${l}]; + ${r?`if (depth < 0 || depth > (${n[a]} - 1) || height < 0 || height > (${n[o]} - 1) || width < 0 || (width > ${n[l]} - 1)) { + return ${i}; + }`:""}; + + depth = max(0, min(depth, ${n[a]} - 1)); + height = max(0, min(height, ${n[o]} - 1)); + width = max(0, min(width, ${n[l]} - 1)); + var depth1: u32 = u32(depth); + var height1: u32 = u32(height); + var width1: u32 = u32(width); + var depth2: u32 = u32(depth + 1); + var height2: u32 = u32(height + 1); + var width2: u32 = u32(width + 1); + var channel: u32 = ${n.length>3?`u32(originalIndices[${d}])`:"0"}; + var batch: u32 = ${n.length>3?`u32(originalIndices[${s}])`:"0"}; + + var x111: ${p} = getInputValue(batch, channel, depth1, height1, width1); + var x112: ${p} = getInputValue(batch, channel, depth1, height1, width2); + var x121: ${p} = getInputValue(batch, channel, depth1, height2, width1); + var x122: ${p} = getInputValue(batch, channel, depth1, height2, width2); + var x211: ${p} = getInputValue(batch, channel, depth2, height1, width1); + var x212: ${p} = getInputValue(batch, channel, depth2, height1, width2); + var x221: ${p} = getInputValue(batch, channel, depth2, height2, width1); + var x222: ${p} = getInputValue(batch, channel, depth2, height2, width2); + var dx1: ${p} = abs(depth - ${p}(depth1)); + var dx2: ${p} = abs(${p}(depth2) - depth); + var dy1: ${p} = abs(height - ${p}(height1)); + var dy2: ${p} = abs(${p}(height2) - height); + var dz1: ${p} = abs(width - ${p}(width1)); + var dz2: ${p} = abs(${p}(width2) - width); + if (depth1 == depth2) { + dx1 = 0.5; + dx2 = 0.5; + } + if (height1 == height2) { + dy1 = 0.5; + dy2 = 0.5; + } + if (width1 == width2) { + dz1 = 0.5; + dz2 = 0.5; + } + return (x111 * dx2 * dy2 * dz2 + x112 * dx2 * dy2 * dz1 + x121 * dx2 * dy1 *dz2 + x122 * dx2 * dy1 * dz1 + + x211 * dx1 * dy2 * dz2 + x212 * dx1 * dy2 * dz1 + x221 * dx1 * dy1 *dz2 + x222 * dx1 * dy1 * dz1); + }`},xm=(e,t,n,r,i,s)=>{let a=e.dims,o=fm(s,t.axes,a.length),l=hm(a,r,i,t.axes),d=r.slice();r.length===0&&(d=a.map((v,I)=>v===0?1:l[I]/v),t.keepAspectRatioPolicy!=="stretch"&&(l=mm(a,d,t)));let p=be("output",e.dataType,l.length),f=V("input",e.dataType,a.length),c=W.size(l),m=a.length===l.length&&a.every((v,I)=>v===l[I]),y=t.coordinateTransformMode==="tf_crop_and_resize",w=t.extrapolationValue,$=f.type.value,_=v=>` + ${m?"":` + ${cm(t.coordinateTransformMode,$)}; + ${(()=>{switch(t.mode){case"nearest":return` + ${bm(f,a)}; + ${pm(t.nearestMode,n,$)}; + ${ym(f,p,a,l,d.length,o.length,y)}; + `;case"linear":return` + ${gm(p,a,l,d.length,o.length)}; + ${(()=>{if(a.length===2||a.length===4)return`${wm(f,p,a,y,w)}`;if(a.length===3||a.length===5)return`${vm(f,p,a,y,w)}`;throw Error("Linear mode only supports input dims 2, 3, 4 and 5 are supported in linear mode.")})()}; + `;case"cubic":return` + ${(()=>{if(a.length===2||a.length===4)return`${_m(f,p,a,l,d,o,t.cubicCoeffA,y,t.extrapolationValue,t.excludeOutside)}`;throw Error("Cubic mode only supports input dims 2 and 4 are supported in linear mode.")})()}; + `;default:throw Error("Invalid resize mode")}})()}; + `} + ${v.registerUniform("output_size","u32").registerUniform("scales","f32",d.length).registerUniform("roi","f32",o.length).declareVariables(f,p)} + ${v.mainStart()} + ${v.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + ${m?"output[global_idx] = input[global_idx];":` + let output_indices = ${p.offsetToIndices("global_idx")}; + var input_indices: ${f.type.indices}; + ${(()=>{switch(t.mode){case"nearest":return`input_indices = calculateInputIndicesFromOutputIndices(output_indices); + if (checkInputIndices(input_indices)) { + output[global_idx] = ${f.getByIndices("input_indices")}; + } else { + output[global_idx] = ${t.extrapolationValue}; + }`;case"linear":return`output[global_idx] = ${a.length===2||a.length===4?"bilinearInterpolation":"trilinearInterpolation"}(output_indices);`;case"cubic":return"output[global_idx] = bicubicInterpolation(output_indices);";default:throw Error(`Unsupported resize mode: ${t.mode}`)}})()}; +`} + }`;return{name:"Resize",shaderCache:{hint:`${t.cacheKey}|${n}|${d.length>0?t.mode==="cubic"?d:d.length:""}|${i.length>0?i:""}|${o.length>0?o:""}|${m}|${t.mode==="nearest"?a.length:a}`,inputDependencies:["rank"]},getShaderSource:_,getRunData:()=>({outputs:[{dims:l,dataType:e.dataType}],dispatchGroup:{x:Math.ceil(c/64)},programUniforms:[{type:12,data:c},{type:1,data:d},{type:1,data:o},..._e(a,l)]})}},$m=e=>{let t=e.customDataBuffer;return new Uint32Array(t,t.byteOffset,1)[0]},Iw=(e,t)=>{let n=[],r=[],i=[],s=$m(e);if(t.antialias!==0)throw Error("Only default value (0) for Antialias attribute is supported");dm(e.inputs,t,s,n,r,i),e.compute(xm(e.inputs[0],t,s,n,r,i),{inputs:[0]})},Cw=e=>{let t=e.antialias,n=e.axes,r=e.coordinateTransformMode,i=e.cubicCoeffA,s=e.excludeOutside!==0,a=e.extrapolationValue,o=e.keepAspectRatioPolicy,l=e.mode,d=e.nearestMode===""?"simple":e.nearestMode;return Fe({antialias:t,axes:n,coordinateTransformMode:r,cubicCoeffA:i,excludeOutside:s,extrapolationValue:a,keepAspectRatioPolicy:o,mode:l,nearestMode:d})}}),Sm,Tm,Ew,n2=re(()=>{Te(),Ce(),Ee(),Sm=e=>{if(!e||e.length<3)throw new Error("layerNorm requires at least 3 inputs.");let t=e[0],n=e[1],r=e[2];if(t.dataType!==n.dataType||t.dataType!==r.dataType)throw new Error("All inputs must have the same data type");if(t.dims.length!==3&&t.dims.length!==2)throw new Error("Input must be 2D or 3D");if(n.dims.length!==3&&n.dims.length!==2)throw new Error("Skip must be 2D or 3D");let i=t.dims[t.dims.length-1],s=t.dims[t.dims.length-2];if(n.dims[n.dims.length-1]!==i)throw new Error("Skip must have the same hidden size as input");if(n.dims[n.dims.length-2]!==s)throw new Error("Skip must have the same sequence length as input");if(r.dims.length!==1)throw new Error("Gamma must be 1D");if(r.dims[r.dims.length-1]!==i)throw new Error("Gamma must have the same hidden size as input");if(e.length>3){let a=e[3];if(a.dims.length!==1)throw new Error("Beta must be 1D");if(a.dims[a.dims.length-1]!==i)throw new Error("Beta must have the same hidden size as input")}if(e.length>4){let a=e[4];if(a.dims.length!==1)throw new Error("Bias must be 1D");if(a.dims[a.dims.length-1]!==i)throw new Error("Bias must have the same hidden size as input")}},Tm=(e,t,n,r)=>{let i=t.simplified,s=e[0].dims,a=W.size(s),o=s,l=a,d=s.slice(-1)[0],p=r?s.slice(0,-1).concat(1):[],f=!i&&e.length>3,c=e.length>4,m=r&&n>1,y=r&&n>2,w=n>3,$=64,_=ct(d),v=[{type:12,data:l},{type:12,data:_},{type:12,data:d},{type:1,data:t.epsilon}],I=E=>{let R=[{name:"output_size",type:"u32"},{name:"components",type:"u32"},{name:"hidden_size",type:"u32"},{name:"epsilon",type:"f32"}],O=[V("x",e[0].dataType,e[0].dims,_),V("skip",e[1].dataType,e[1].dims,_),V("gamma",e[2].dataType,e[2].dims,_)];f&&O.push(V("beta",e[3].dataType,e[3].dims,_)),c&&O.push(V("bias",e[4].dataType,e[4].dims,_)),O.push(be("output",e[0].dataType,o,_)),m&&O.push(be("mean_output",1,p)),y&&O.push(be("inv_std_output",1,p)),w&&O.push(be("input_skip_bias_sum",e[0].dataType,o,_));let x=xt(e[0].dataType),U=xt(1,_);return` + + ${E.registerUniforms(R).declareVariables(...O)} + var sum_shared : array<${U}, ${$}>; + var sum_squared_shared : array<${U}, ${$}>; + + ${E.mainStart([$,1,1])} + let ix = local_id.x; + let iy = global_id.x / ${$}; + + let hidden_size_vectorized: u32 = uniforms.hidden_size / uniforms.components; + var stride = hidden_size_vectorized / ${$}; + let offset = ix * stride + iy * hidden_size_vectorized; + let offset1d = stride * ix; + if (ix == ${$-1}) { + stride = hidden_size_vectorized - stride * ix; + } + for (var i: u32 = 0; i < stride; i++) { + let skip_value = skip[offset + i]; + let bias_value = ${c?"bias[offset1d + i]":x+"(0.0)"}; + let input_value = x[offset + i]; + let value = input_value + skip_value + bias_value; + ${w?"input_skip_bias_sum[offset + i] = value;":""} + output[offset + i] = value; + let f32_value = ${bi(x,_,"value")}; + sum_shared[ix] += f32_value; + sum_squared_shared[ix] += f32_value * f32_value; + } + workgroupBarrier(); + + var reduce_size : u32 = ${$}; + for (var curr_size = reduce_size >> 1; curr_size > 0; curr_size = reduce_size >> 1) { + reduce_size = curr_size + (reduce_size & 1); + if (ix < curr_size) { + sum_shared[ix] += sum_shared[ix + reduce_size]; + sum_squared_shared[ix] += sum_squared_shared[ix + reduce_size]; + } + workgroupBarrier(); + } + + let sum = sum_shared[0]; + let square_sum = sum_squared_shared[0]; + let mean = ${wr("sum",_)} / f32(uniforms.hidden_size); + let inv_std_dev = inverseSqrt(${wr("square_sum",_)} / f32(uniforms.hidden_size) ${i?"":"- mean * mean"} + uniforms.epsilon); + ${m?"mean_output[global_idx] = mean;":""} + ${y?"inv_std_output[global_idx] = inv_std_dev;":""} + + for (var i: u32 = 0; i < stride; i++) { + output[offset + i] = (output[offset + i] ${i?"":`- ${x}(mean)`}) * + ${x}(inv_std_dev) * gamma[offset1d + i] + ${f?"+ beta[offset1d + i]":""}; + } + }`},S=[{dims:o,dataType:e[0].dataType}];return n>1&&S.push({dims:p,dataType:1}),n>2&&S.push({dims:p,dataType:1}),n>3&&S.push({dims:s,dataType:e[0].dataType}),{name:"SkipLayerNormalization",shaderCache:{hint:`${_};${m};${y};${w}`,inputDependencies:e.map((E,R)=>"type")},getShaderSource:I,getRunData:()=>({outputs:S,dispatchGroup:{x:Math.ceil(l/d)},programUniforms:v})}},Ew=(e,t)=>{Sm(e.inputs);let n=[0];e.outputCount>1&&n.push(-3),e.outputCount>2&&n.push(-3),e.outputCount>3&&n.push(3),e.compute(Tm(e.inputs,t,e.outputCount,!1),{outputs:n})}}),Im,os,Cm,Ml,Em,km,kw,zw,r2=re(()=>{Te(),Ce(),pt(),Ee(),Im=(e,t)=>{if(!e||e.length<1)throw new Error("too few inputs");if(t.axes.length!==0){if(t.axes.length!==t.starts.length||t.axes.length!==t.ends.length)throw new Error("axes, starts and ends must have the same length")}else if(t.starts.length!==t.ends.length)throw new Error("starts and ends must have the same length");e.slice(1).forEach((n,r)=>{if(e[r+1].dataType!==6&&e[r+1].dataType!==7)throw new Error(`Input ${r} must be an array of int32 or int64`)})},os=(e,t)=>{let n=[];if(e.length>t)if(e[t].dataType===7)e[t].getBigInt64Array().forEach(r=>n.push(Number(r)));else if(e[t].dataType===6)e[t].getInt32Array().forEach(r=>n.push(Number(r)));else throw new Error(`Input ${t} must be an array of int32 or int64`);return n},Cm=(e,t)=>{if(e.length>1){let n=os(e,1),r=os(e,2),i=os(e,3);return i.length===0&&(i=[...Array(e[0].dims.length).keys()]),Fe({starts:n,ends:r,axes:i})}else return t},Ml=(e,t,n,r,i)=>{let s=e;return e<0&&(s+=n[r[t]]),i[t]<0?Math.max(0,Math.min(s,n[r[t]]-1)):Math.max(0,Math.min(s,n[r[t]]))},Em=(e,t,n)=>`fn calculateInputIndices(output_indices: ${t.type.indices}) -> ${e.type.indices} { + var input_indices: ${e.type.indices}; + var carry = 0u; + for (var i = ${n.length-1}; i >= 0; i--) { + let input_shape_i = ${we("uniforms.input_shape","i",n.length)}; + let steps_i = ${we("uniforms.steps","i",n.length)}; + let signs_i = ${we("uniforms.signs","i",n.length)}; + let starts_i = ${we("uniforms.starts","i",n.length)}; + var output_index = ${t.indicesGet("output_indices","i")}; + var input_index = output_index * steps_i + starts_i + carry; + carry = input_index / input_shape_i; + input_index = input_index % input_shape_i; + if (signs_i < 0) { + input_index = input_shape_i - input_index - 1u + starts_i; + } + ${e.indicesSet("input_indices","i","input_index")}; + } + return input_indices; + }`,km=(e,t)=>{let n=e[0].dims,r=W.size(n),i=t.axes.length>0?W.normalizeAxes(t.axes,n.length):[...Array(n.length).keys()],s=os(e,4);s.forEach(_=>_!==0||(()=>{throw new Error("step cannot be 0")})),s.length===0&&(s=Array(i.length).fill(1));let a=t.starts.map((_,v)=>Ml(_,v,n,i,s)),o=t.ends.map((_,v)=>Ml(_,v,n,i,s));if(i.length!==a.length||i.length!==o.length)throw new Error("start, ends and axes should have the same number of elements");if(i.length!==n.length)for(let _=0;_Math.sign(_));s.forEach((_,v,I)=>{if(_<0){let S=(o[v]-a[v])/_,E=a[v],R=E+S*s[v];a[v]=R,o[v]=E,I[v]=-_}});let d=n.slice(0);i.forEach((_,v)=>{d[_]=Math.ceil((o[_]-a[_])/s[_])});let p={dims:d,dataType:e[0].dataType},f=be("output",e[0].dataType,d.length),c=V("input",e[0].dataType,e[0].dims.length),m=W.size(d),y=[{name:"outputSize",type:"u32"},{name:"starts",type:"u32",length:a.length},{name:"signs",type:"i32",length:l.length},{name:"steps",type:"u32",length:s.length}],w=[{type:12,data:m},{type:12,data:a},{type:6,data:l},{type:12,data:s},..._e(e[0].dims,d)],$=_=>` + ${_.registerUniforms(y).declareVariables(c,f)} + ${Em(c,f,n)} + ${_.mainStart()} + ${_.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.outputSize")} + let output_indices = ${f.offsetToIndices("global_idx")}; + let input_indices = calculateInputIndices(output_indices); + ${f.setByOffset("global_idx",c.getByIndices("input_indices"))} + }`;return{name:"Slice",shaderCache:{hint:`${l.length}_${a.length}_${s.length}`,inputDependencies:["rank"]},getShaderSource:$,getRunData:()=>({outputs:[p],dispatchGroup:{x:Math.ceil(r/64)},programUniforms:w})}},kw=(e,t)=>{Im(e.inputs,t);let n=Cm(e.inputs,t);e.compute(km(e.inputs,n),{inputs:[0]})},zw=e=>{let t=e.starts,n=e.ends,r=e.axes;return Fe({starts:t,ends:n,axes:r})}}),zm,Am,Aw,Mw,i2=re(()=>{Te(),Ce(),pt(),_r(),Ee(),zm=e=>{if(!e||e.length!==1)throw new Error("Softmax op requires 1 input.")},Am=(e,t)=>{let n=e.inputs[0],r=n.dims,i=W.size(r),s=r.length,a=W.normalizeAxis(t.axis,s),o=ax),d[a]=s-1,d[s-1]=a,l=e.compute(en(n,d),{inputs:[n],outputs:[-1]})[0]):l=n;let p=l.dims,f=p[s-1],c=i/f,m=ct(f),y=f/m,w=64;c===1&&(w=256);let $=(O,x)=>x===4?`max(max(${O}.x, ${O}.y), max(${O}.z, ${O}.w))`:x===2?`max(${O}.x, ${O}.y)`:x===3?`max(max(${O}.x, ${O}.y), ${O}.z)`:O,_=V("x",l.dataType,l.dims,m),v=be("result",l.dataType,l.dims,m),I=_.type.value,S=xt(l.dataType)==="f32"?`var threadMax = ${I}(-3.4028234663852886e+38f);`:`var threadMax = ${I}(-65504.0h);`,E=O=>` + var rowMaxShared : ${I}; + var rowSumShared : ${I}; + var threadShared : array<${I}, ${w}>; + + fn getValue(row: i32, col: i32, row_stride: i32) -> ${I} { + let index = row * row_stride + col; + return x[index]; + } + + fn setValue(row: i32, col: i32, row_stride: i32, value: ${I}) { + let index = row * row_stride + col; + result[index] = value; + } + ${O.registerUniform("packedCols","i32").declareVariables(_,v)} + ${O.mainStart(w)} + let gindex = i32(global_idx); + let lindex = i32(local_idx); + const wg = ${w}; + let row = gindex / wg; + let cols = uniforms.packedCols; + let row_stride : i32 = uniforms.packedCols; + + // find the rows max + ${S} + for (var col = lindex; col < cols; col += wg) { + let value = getValue(row, col, row_stride); + threadMax = max(threadMax, value); + } + if (lindex < cols) { + threadShared[lindex] = threadMax; + } + workgroupBarrier(); + + var reduceSize = min(cols, wg); + for (var currSize = reduceSize >> 1; currSize > 0; currSize = reduceSize >> 1) { + reduceSize = currSize + (reduceSize & 1); + if (lindex < currSize) { + threadShared[lindex] = max(threadShared[lindex], threadShared[lindex + reduceSize]); + } + workgroupBarrier(); + } + if (lindex == 0) { + rowMaxShared = ${I}(${$("threadShared[0]",m)}); + } + workgroupBarrier(); + + // find the rows sum + var threadSum = ${I}(0.0); + for (var col = lindex; col < cols; col += wg) { + let subExp = exp(getValue(row, col, row_stride) - rowMaxShared); + threadSum += subExp; + } + threadShared[lindex] = threadSum; + workgroupBarrier(); + + for (var currSize = wg >> 1; currSize > 0; currSize = currSize >> 1) { + if (lindex < currSize) { + threadShared[lindex] = threadShared[lindex] + threadShared[lindex + currSize]; + } + workgroupBarrier(); + } + if (lindex == 0) { + rowSumShared = ${I}(${wr("threadShared[0]",m)}); + } + workgroupBarrier(); + + // calculate final value for each element in the row + for (var col = lindex; col < cols; col += wg) { + var value = exp(getValue(row, col, row_stride) - rowMaxShared) / rowSumShared; + // max operation protects against NaN since all values should be >=0 + value = max(value, ${I}(0.0)); + setValue(row, col, row_stride, value); + } + }`,R=e.compute({name:"Softmax",shaderCache:{hint:`${m};${w}`,inputDependencies:["type"]},getRunData:()=>({outputs:[{dims:p,dataType:l.dataType}],dispatchGroup:{x:c},programUniforms:[{type:6,data:y}]}),getShaderSource:E},{inputs:[l],outputs:[o?-1:0]})[0];o&&e.compute(en(R,d),{inputs:[R]})},Aw=(e,t)=>{zm(e.inputs),Am(e,t)},Mw=e=>Fe({axis:e.axis})}),Rl,Mm,Rm,Om,Rw,s2=re(()=>{Te(),Ce(),Ee(),Rl=e=>Array.from(e.getBigInt64Array(),Number),Mm=e=>{if(!e||e.length!==2)throw new Error("Tile requires 2 inputs.");if(e[0].dataType!==1&&e[0].dataType!==10&&e[0].dataType!==6&&e[0].dataType!==12)throw new Error("Tile only support float, float16, int32, and uint32 data types");if(e[1].dataType!==7)throw new Error("Tile `repeats` input should be of int64 data type");if(e[1].dims.length!==1)throw new Error("Tile `repeats` input should be 1-D");if(Rl(e[1]).length!==e[0].dims.length)throw new Error("Tile `repeats` input should have same number of elements as rank of input data tensor")},Rm=(e,t)=>{let n=[];for(let r=0;r{let n=e[0].dims,r=t??Rl(e[1]),i=Rm(n,r),s=W.size(i),a=e[0].dataType,o=V("input",a,n.length),l=be("output",a,i.length),d=p=>` + const inputShape = ${o.indices(...n)}; + ${p.registerUniform("output_size","u32").declareVariables(o,l)} + ${p.mainStart()} + ${p.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size")} + let output_indices = ${l.offsetToIndices("global_idx")}; + var input_indices: ${o.type.indices}; + for (var i = 0; i < ${n.length}; i++) { + let input_dim_i = ${o.indicesGet("uniforms.input_shape","i")}; + let input_dim_value = ${l.indicesGet("output_indices","i")} % input_dim_i; + + ${o.indicesSet("input_indices","i","input_dim_value")} + } + ${l.setByOffset("global_idx",o.getByIndices("input_indices"))} + }`;return{name:"Tile",shaderCache:{hint:`${r}`,inputDependencies:["rank"]},getRunData:()=>({outputs:[{dims:i,dataType:e[0].dataType}],dispatchGroup:{x:Math.ceil(s/64)},programUniforms:[{type:12,data:s},..._e(e[0].dims,i)]}),getShaderSource:d}},Rw=e=>{Mm(e.inputs),e.compute(Om(e.inputs),{inputs:[0]})}}),Nm,Bm,Ow,a2=re(()=>{Te(),Ce(),Ee(),Nm=(e,t,n,r,i)=>{let s=be("output_data",i,n.length,4),a=V("a_data",t[1].dataType,t[1].dims.length,4),o=V("b_data",t[2].dataType,t[2].dims.length,4),l=V("c_data",t[0].dataType,t[0].dims.length,4),d,p=(f,c,m)=>`select(${c}, ${f}, ${m})`;if(!r)d=s.setByOffset("global_idx",p(a.getByOffset("global_idx"),o.getByOffset("global_idx"),l.getByOffset("global_idx")));else{let f=(c,m,y="")=>{let w=`a_data[index_a${m}][component_a${m}]`,$=`b_data[index_b${m}][component_b${m}]`,_=`bool(c_data[index_c${m}] & (0xffu << (component_c${m} * 8)))`;return` + let output_indices${m} = ${s.offsetToIndices(`global_idx * 4u + ${m}u`)}; + let offset_a${m} = ${a.broadcastedIndicesToOffset(`output_indices${m}`,s)}; + let offset_b${m} = ${o.broadcastedIndicesToOffset(`output_indices${m}`,s)}; + let offset_c${m} = ${l.broadcastedIndicesToOffset(`output_indices${m}`,s)}; + let index_a${m} = offset_a${m} / 4u; + let index_b${m} = offset_b${m} / 4u; + let index_c${m} = offset_c${m} / 4u; + let component_a${m} = offset_a${m} % 4u; + let component_b${m} = offset_b${m} % 4u; + let component_c${m} = offset_c${m} % 4u; + ${c}[${m}] = ${y}(${p(w,$,_)}); + `};i===9?d=` + var data = vec4(0); + ${f("data",0,"u32")} + ${f("data",1,"u32")} + ${f("data",2,"u32")} + ${f("data",3,"u32")} + output_data[global_idx] = dot(vec4(0x1, 0x100, 0x10000, 0x1000000), vec4(data));`:d=` + ${f("output_data[global_idx]",0)} + ${f("output_data[global_idx]",1)} + ${f("output_data[global_idx]",2)} + ${f("output_data[global_idx]",3)} + `}return` + ${e.registerUniform("vec_size","u32").declareVariables(l,a,o,s)} + ${e.mainStart()} + ${e.guardAgainstOutOfBoundsWorkgroupSizes("uniforms.vec_size")} + ${d} + }`},Bm=e=>{let t=e[1].dims,n=e[2].dims,r=e[0].dims,i=e[1].dataType,s=!(W.areEqual(t,n)&&W.areEqual(n,r)),a=t,o=W.size(t);if(s){let d=Ei.calcShape(Ei.calcShape(t,n,!1),r,!1);if(!d)throw new Error("Can't perform where op on the given tensors");a=d,o=W.size(a)}let l=Math.ceil(o/4);return{name:"Where",shaderCache:{inputDependencies:["rank","rank","rank"]},getShaderSource:d=>Nm(d,e,a,s,i),getRunData:()=>({outputs:[{dims:a,dataType:i}],dispatchGroup:{x:Math.ceil(o/64/4)},programUniforms:[{type:12,data:l},..._e(r,t,n,a)]})}},Ow=e=>{e.compute(Bm(e.inputs))}}),Nw,o2=re(()=>{v$(),Ju(),x$(),$$(),S$(),T$(),I$(),A$(),R$(),O$(),N$(),B$(),D$(),P$(),U$(),L$(),W$(),q$(),G$(),V$(),F$(),H$(),j$(),K$(),X$(),ew(),Y$(),Z$(),Q$(),J$(),e2(),Qu(),t2(),sw(),n2(),r2(),i2(),rw(),s2(),_r(),ed(),a2(),Nw=new Map([["Abs",[Ey]],["Acos",[ky]],["Acosh",[zy]],["Add",[db]],["ArgMax",[Sy,uu]],["ArgMin",[$y,uu]],["Asin",[Ay]],["Asinh",[My]],["Atan",[Ry]],["Atanh",[Oy]],["Attention",[Ty]],["AveragePool",[hw,fw]],["BatchNormalization",[Iy]],["BiasAdd",[Cy]],["BiasSplitGelu",[ub]],["Cast",[By,Ny]],["Ceil",[Py]],["Clip",[Dy]],["Concat",[_b,vb]],["Conv",[mu,hu]],["ConvTranspose",[Ab,zb]],["Cos",[Uy]],["Cosh",[Ly]],["CumSum",[Mb,Rb]],["DepthToSpace",[Ob,Nb]],["DequantizeLinear",[vw,xw]],["Div",[cb]],["Einsum",[Bb,Db]],["Elu",[Wy,fs]],["Equal",[pb]],["Erf",[qy]],["Exp",[Gy]],["Expand",[Pb]],["FastGelu",[Ub]],["Floor",[Vy]],["FusedConv",[mu,hu]],["Gather",[Wb,Lb]],["GatherElements",[jb,Hb]],["GatherBlockQuantized",[Vb,Fb]],["GatherND",[qb,Gb]],["Gelu",[Fy]],["Gemm",[Xb,Kb]],["GlobalAveragePool",[gw,mw]],["GlobalMaxPool",[_w,ww]],["Greater",[gb]],["GreaterOrEqual",[bb]],["GridSample",[Yb,Zb]],["GroupQueryAttention",[aw]],["HardSigmoid",[Jy,Qy]],["InstanceNormalization",[ow]],["LayerNormalization",[lw]],["LeakyRelu",[Hy,fs]],["Less",[yb]],["LessOrEqual",[wb]],["Log",[ob]],["MatMul",[uw]],["MatMulNBits",[dw,cw]],["MaxPool",[yw,bw]],["Mul",[fb]],["MultiHeadAttention",[Jb,Qb]],["Neg",[Ky]],["Not",[jy]],["Pad",[pw]],["Pow",[hb]],["QuickGelu",[lb,fs]],["Range",[$w]],["Reciprocal",[Xy]],["ReduceMin",[by]],["ReduceMean",[fy]],["ReduceMax",[yy]],["ReduceSum",[_y]],["ReduceProd",[wy]],["ReduceL1",[hy]],["ReduceL2",[my]],["ReduceLogSum",[xy]],["ReduceLogSumExp",[gy]],["ReduceSumSquare",[vy]],["Relu",[Yy]],["Resize",[Iw,Cw]],["RotaryEmbedding",[iw]],["ScatterND",[Tw,Sw]],["Sigmoid",[Zy]],["Sin",[eb]],["Sinh",[tb]],["Slice",[kw,zw]],["SkipLayerNormalization",[Ew]],["Split",[tw,nw]],["Sqrt",[nb]],["Softmax",[Aw,Mw]],["Sub",[mb]],["Tan",[rb]],["Tanh",[ib]],["ThresholdedRelu",[ab,fs]],["Tile",[Rw]],["Transpose",[ty,ny]],["Where",[Ow]]])}),Bw,l2=re(()=>{on(),nr(),Ee(),Bw=class{constructor(e){this.backend=e,this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,t){this.repo.set(e,t)}run(e,t,n,r,i){Bn(e.programInfo.name);let s=this.backend.device,a=this.backend.getComputePassEncoder();this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2);let o=[];for(let d of t)o.push({binding:o.length,resource:{buffer:d.buffer}});for(let d of n)o.push({binding:o.length,resource:{buffer:d.buffer}});i&&o.push({binding:o.length,resource:i});let l=s.createBindGroup({layout:e.computePipeline.getBindGroupLayout(0),entries:o,label:e.programInfo.name});if(this.backend.sessionStatus==="capturing"){let d={kernelId:this.backend.currentKernelId,computePipeline:e.computePipeline,bindGroup:l,dispatchGroup:r};this.backend.capturedCommandList.get(this.backend.currentSessionId).push(d)}a.setPipeline(e.computePipeline),a.setBindGroup(0,l),a.dispatchWorkgroups(...r),this.backend.writeTimestamp(this.backend.pendingDispatchNumber*2+1),this.backend.pendingDispatchNumber++,(this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber||this.backend.queryType==="at-passes")&&this.backend.endComputePass(),this.backend.pendingDispatchNumber>=this.backend.maxDispatchNumber&&this.backend.flush(),wn(e.programInfo.name)}dispose(){}build(e,t){Bn(e.name);let n=this.backend.device,r=[];[{feature:"shader-f16",extension:"f16"},{feature:"subgroups",extension:"subgroups"}].forEach(d=>{n.features.has(d.feature)&&r.push(`enable ${d.extension};`)});let i=ey(t,this.backend.device.limits),s=e.getShaderSource(i),a=`${r.join(` +`)} +${i.additionalImplementations} +${s}`,o=n.createShaderModule({code:a,label:e.name});Pe("verbose",()=>`[WebGPU] ${e.name} shader code: ${a}`);let l=n.createComputePipeline({compute:{module:o,entryPoint:"main"},layout:"auto",label:e.name});return wn(e.name),{programInfo:e,computePipeline:l,uniformVariablesInfo:i.variablesInfo}}normalizeDispatchGroupSize(e){let t=typeof e=="number"?e:e.x,n=typeof e=="number"?1:e.y||1,r=typeof e=="number"?1:e.z||1,i=this.backend.device.limits.maxComputeWorkgroupsPerDimension;if(t<=i&&n<=i&&r<=i)return[t,n,r];let s=t*n*r,a=Math.ceil(Math.sqrt(s));if(a>i){if(a=Math.ceil(Math.cbrt(s)),a>i)throw new Error("Total dispatch size exceeds WebGPU maximum.");return[a,a,a]}else return[a,a,1]}}}),Dw={};Mi(Dw,{WebGpuBackend:()=>Pw});var Dm,Pm,Um,Pw,u2=re(()=>{on(),Te(),nr(),X0(),w$(),o2(),l2(),Dm=(e,t)=>{if(t.length!==e.length)throw new Error(`inputDependencies length ${t.length} is not equal to inputTensors length ${e.length}.`);let n=[];for(let r=0;r{var i,s;let r=e.name;return(i=e.shaderCache)!=null&&i.hint&&(r+="["+e.shaderCache.hint+"]"),r+=":"+n+`:${Dm(t,((s=e.shaderCache)==null?void 0:s.inputDependencies)??new Array(t.length).fill("dims"))}`,r},Um=class{constructor(e){e&&(this.architecture=e.architecture,this.vendor=e.vendor)}isArchitecture(e){return this.architecture===e}isVendor(e){return this.vendor===e}},Pw=class{constructor(){this.currentSessionId=null,this.currentKernelId=null,this.commandEncoder=null,this.computePassEncoder=null,this.maxDispatchNumber=16,this.pendingDispatchNumber=0,this.pendingKernels=[],this.pendingQueries=new Map,this.sessionStatus="default",this.capturedCommandList=new Map,this.capturedPendingKernels=new Map,this.sessionExternalDataMapping=new Map}get currentKernelCustomData(){if(this.currentKernelId===null)throw new Error("currentKernelCustomData(): currentKernelId is null. (should not happen)");let e=this.kernelCustomData.get(this.currentKernelId);return e||(e={},this.kernelCustomData.set(this.currentKernelId,e)),e}async initialize(e,t){this.env=e;let n=[],r={requiredLimits:{maxComputeWorkgroupStorageSize:t.limits.maxComputeWorkgroupStorageSize,maxComputeWorkgroupsPerDimension:t.limits.maxComputeWorkgroupsPerDimension,maxStorageBufferBindingSize:t.limits.maxStorageBufferBindingSize,maxBufferSize:t.limits.maxBufferSize,maxComputeInvocationsPerWorkgroup:t.limits.maxComputeInvocationsPerWorkgroup,maxComputeWorkgroupSizeX:t.limits.maxComputeWorkgroupSizeX,maxComputeWorkgroupSizeY:t.limits.maxComputeWorkgroupSizeY,maxComputeWorkgroupSizeZ:t.limits.maxComputeWorkgroupSizeZ},requiredFeatures:n},i=s=>t.features.has(s)&&n.push(s)&&!0;i("chromium-experimental-timestamp-query-inside-passes")||i("timestamp-query"),i("shader-f16"),i("subgroups"),this.device=await t.requestDevice(r),this.adapterInfo=new Um(t.info||await t.requestAdapterInfo()),this.gpuDataManager=Q0(this),this.programManager=new Bw(this),this.kernels=new Map,this.kernelPersistentData=new Map,this.kernelCustomData=new Map,Ku(e.logLevel,!!e.debug),this.device.onuncapturederror=s=>{s.error instanceof GPUValidationError&&console.error(`An uncaught WebGPU validation error was raised: ${s.error.message}`)},Object.defineProperty(this.env.webgpu,"device",{value:this.device,writable:!1,enumerable:!0,configurable:!1}),Object.defineProperty(this.env.webgpu,"adapter",{value:t,writable:!1,enumerable:!0,configurable:!1}),this.setQueryType()}dispose(){typeof this.querySet<"u"&&this.querySet.destroy(),this.gpuDataManager.dispose()}getCommandEncoder(){return this.commandEncoder||(this.commandEncoder=this.device.createCommandEncoder()),this.commandEncoder}getComputePassEncoder(){if(!this.computePassEncoder){let e=this.getCommandEncoder(),t={};this.queryType==="at-passes"&&(t.timestampWrites={querySet:this.querySet,beginningOfPassWriteIndex:this.pendingDispatchNumber*2,endOfPassWriteIndex:this.pendingDispatchNumber*2+1}),this.computePassEncoder=e.beginComputePass(t)}return this.computePassEncoder}endComputePass(){this.computePassEncoder&&(this.computePassEncoder.end(),this.computePassEncoder=null)}flush(){if(!this.commandEncoder)return;Bn(),this.endComputePass();let e;this.queryType!=="none"&&(this.commandEncoder.resolveQuerySet(this.querySet,0,this.pendingDispatchNumber*2,this.queryResolveBuffer,0),e=this.device.createBuffer({size:this.pendingDispatchNumber*2*8,usage:GPUBufferUsage.MAP_READ|GPUBufferUsage.COPY_DST}),this.pendingQueries.set(e,this.pendingKernels),this.pendingKernels=[],this.commandEncoder.copyBufferToBuffer(this.queryResolveBuffer,0,e,0,this.pendingDispatchNumber*2*8)),this.device.queue.submit([this.commandEncoder.finish()]),this.gpuDataManager.refreshPendingBuffers(),this.commandEncoder=null,this.pendingDispatchNumber=0,this.queryType!=="none"&&e.mapAsync(GPUMapMode.READ).then(()=>{var r;let t=new BigUint64Array(e.getMappedRange()),n=this.pendingQueries.get(e);for(let i=0;i"u"&&(this.queryTimeBase=m);let w=Number(m-this.queryTimeBase),$=Number(y-this.queryTimeBase);if(!Number.isSafeInteger(w)||!Number.isSafeInteger($))throw new RangeError("incorrect timestamp range");if((r=this.env.webgpu.profiling)!=null&&r.ondata)this.env.webgpu.profiling.ondata({version:1,inputsMetadata:f.map(_=>({dims:_.dims,dataType:Yn(_.dataType)})),outputsMetadata:c.map(_=>({dims:_.dims,dataType:Yn(_.dataType)})),kernelId:a,kernelType:l,kernelName:d,programName:p,startTime:w,endTime:$});else{let _="";f.forEach((I,S)=>{_+=`input[${S}]: [${I.dims}] | ${Yn(I.dataType)}, `});let v="";c.forEach((I,S)=>{v+=`output[${S}]: [${I.dims}] | ${Yn(I.dataType)}, `}),console.log(`[profiling] kernel "${a}|${l}|${d}|${p}" ${_}${v}start time: ${w} ns, execution time: ${$-w} ns`)}Da("GPU",`${p}::${m}::${y}`)}e.unmap(),this.pendingQueries.delete(e)}),wn()}run(e,t,n,r,i,s){Bn(e.name);let a=[];for(let v=0;vI):n;if(p.length!==o.length)throw new Error(`Output size ${p.length} must be equal to ${o.length}.`);let f=[],c=[];for(let v=0;v=s)throw new Error(`Invalid output index: ${p[v]}`);if(p[v]===-3)continue;let I=p[v]===-1,S=p[v]===-2,E=I||S?i(o[v].dataType,o[v].dims):r(p[v],o[v].dataType,o[v].dims);if(f.push(E),E.data===0)continue;let R=this.gpuDataManager.get(E.data);if(!R)throw new Error(`no GPU data for output: ${E.data}`);if(I&&this.temporaryData.push(R),S){let O=this.kernelPersistentData.get(this.currentKernelId);O||(O=[],this.kernelPersistentData.set(this.currentKernelId,O)),O.push(R)}c.push(R)}if(a.length!==t.length||c.length!==f.length){if(c.length===0)return wn(e.name),f;throw new Error(`Program ${e.name} has zero-sized tensor(s) in inputs or outputs. This is not supported now.`)}let m;if(d){let v=0,I=[];d.forEach(O=>{let x=typeof O.data=="number"?[O.data]:O.data;if(x.length===0)return;let U=O.type===10?2:4,q,J;O.type===10?(J=x.length>4?16:x.length>2?8:x.length*U,q=x.length>4?16:U*x.length):(J=x.length<=2?x.length*U:16,q=16),v=Math.ceil(v/J)*J,I.push(v);let se=O.type===10?8:4;v+=x.length>4?Math.ceil(x.length/se)*q:x.length*U});let S=16;v=Math.ceil(v/S)*S;let E=new ArrayBuffer(v);d.forEach((O,x)=>{let U=I[x],q=typeof O.data=="number"?[O.data]:O.data;if(O.type===6)new Int32Array(E,U,q.length).set(q);else if(O.type===12)new Uint32Array(E,U,q.length).set(q);else if(O.type===10)new Uint16Array(E,U,q.length).set(q);else if(O.type===1)new Float32Array(E,U,q.length).set(q);else throw new Error(`Unsupported uniform type: ${Yn(O.type)}`)});let R=this.gpuDataManager.create(v,GPUBufferUsage.COPY_DST|GPUBufferUsage.UNIFORM);this.device.queue.writeBuffer(R.buffer,0,E,0,v),this.gpuDataManager.release(R.id),m={offset:0,size:v,buffer:R.buffer}}let y=this.programManager.normalizeDispatchGroupSize(l),w=y[1]===1&&y[2]===1,$=Pm(e,t,w),_=this.programManager.getArtifact($);if(_||(_=this.programManager.build(e,y),this.programManager.setArtifact($,_),Pe("info",()=>`[artifact] key: ${$}, programName: ${e.name}`)),d&&_.uniformVariablesInfo){if(d.length!==_.uniformVariablesInfo.length)throw new Error(`Uniform variables count mismatch: expect ${_.uniformVariablesInfo.length}, got ${d.length} in program "${_.programInfo.name}".`);for(let v=0;v`[ProgramManager] run "${e.name}" (key=${$}) with ${y[0]}x${y[1]}x${y[2]}`),this.queryType!=="none"||this.sessionStatus==="capturing"){let v={kernelId:this.currentKernelId,programName:_.programInfo.name,inputTensorViews:t,outputTensorViews:f};this.pendingKernels.push(v),this.sessionStatus==="capturing"&&this.capturedPendingKernels.get(this.currentSessionId).push(v)}return this.programManager.run(_,a,c,y,m),wn(e.name),f}upload(e,t){this.gpuDataManager.upload(e,t)}memcpy(e,t){this.gpuDataManager.memcpy(e,t)}async download(e,t){await this.gpuDataManager.download(e,t)}alloc(e){return this.gpuDataManager.create(e).id}free(e){return this.gpuDataManager.release(e)}createKernel(e,t,n,r){let i=Nw.get(e);if(!i)throw new Error(`kernel not implemented: ${e}`);let s={kernelType:e,kernelName:r,kernelEntry:i[0],attributes:[i[1],n]};this.kernels.set(t,s)}releaseKernel(e){let t=this.kernelPersistentData.get(e);if(t){for(let n of t)this.gpuDataManager.release(n.id);this.kernelPersistentData.delete(e)}this.kernelCustomData.delete(e),this.kernels.delete(e)}computeKernel(e,t,n){let r=this.kernels.get(e);if(!r)throw new Error(`kernel not created: ${e}`);let i=r.kernelType,s=r.kernelName,a=r.kernelEntry,o=r.attributes;if(this.currentKernelId!==null)throw new Error(`kernel "[${i}] ${s}" is not allowed to be called recursively`);this.currentKernelId=e,o[0]&&(o[1]=o[0](o[1]),o[0]=void 0),Pe("info",()=>`[WebGPU] Start to run kernel "[${i}] ${s}"...`);let l=this.env.debug;this.temporaryData=[];try{return l&&this.device.pushErrorScope("validation"),a(t,o[1]),0}catch(d){return n.push(Promise.resolve(`[WebGPU] Kernel "[${i}] ${s}" failed. ${d}`)),1}finally{l&&n.push(this.device.popErrorScope().then(d=>d?`GPU validation error for kernel "[${i}] ${s}": ${d.message}`:null));for(let d of this.temporaryData)this.gpuDataManager.release(d.id);this.temporaryData=[],this.currentKernelId=null}}registerBuffer(e,t,n,r){let i=this.sessionExternalDataMapping.get(e);i||(i=new Map,this.sessionExternalDataMapping.set(e,i));let s=i.get(t),a=this.gpuDataManager.registerExternalBuffer(n,r,s);return i.set(t,[a,n]),a}unregisterBuffers(e){let t=this.sessionExternalDataMapping.get(e);t&&(t.forEach(n=>this.gpuDataManager.unregisterExternalBuffer(n[0])),this.sessionExternalDataMapping.delete(e))}getBuffer(e){let t=this.gpuDataManager.get(e);if(!t)throw new Error(`no GPU data for buffer: ${e}`);return t.buffer}createDownloader(e,t,n){return async()=>{let r=await au(this,e,t);return Xu(r.buffer,n)}}writeTimestamp(e){this.queryType==="inside-passes"&&this.computePassEncoder.writeTimestamp(this.querySet,e)}setQueryType(){var e;this.queryType="none",(((e=this.env.webgpu.profiling)==null?void 0:e.mode)==="default"||(typeof this.env.trace>"u"?this.env.wasm.trace:this.env.trace))&&(this.device.features.has("chromium-experimental-timestamp-query-inside-passes")?this.queryType="inside-passes":this.device.features.has("timestamp-query")&&(this.queryType="at-passes"),this.queryType!=="none"&&typeof this.querySet>"u"&&(this.querySet=this.device.createQuerySet({type:"timestamp",count:this.maxDispatchNumber*2}),this.queryResolveBuffer=this.device.createBuffer({size:this.maxDispatchNumber*2*8,usage:GPUBufferUsage.COPY_SRC|GPUBufferUsage.QUERY_RESOLVE})))}captureBegin(){Pe("info","captureBegin"),this.capturedCommandList.get(this.currentSessionId)||this.capturedCommandList.set(this.currentSessionId,[]),this.capturedPendingKernels.get(this.currentSessionId)||this.capturedPendingKernels.set(this.currentSessionId,[]),this.flush(),this.sessionStatus="capturing"}captureEnd(){Pe("info","captureEnd"),this.flush(),this.sessionStatus="default"}replay(){Pe("info","replay"),this.sessionStatus="replaying";let e=this.capturedCommandList.get(this.currentSessionId),t=this.capturedPendingKernels.get(this.currentSessionId),n=e.length;this.pendingKernels=[];for(let r=0;r=this.maxDispatchNumber||this.queryType==="at-passes")&&this.endComputePass(),this.pendingDispatchNumber>=this.maxDispatchNumber&&this.flush()}this.flush(),this.sessionStatus="default"}onCreateSession(){this.gpuDataManager.onCreateSession()}onReleaseSession(e){this.unregisterBuffers(e),this.capturedCommandList.has(e)&&this.capturedCommandList.delete(e),this.capturedPendingKernels.has(e)&&this.capturedPendingKernels.delete(e),this.gpuDataManager.onReleaseSession(e)}onRunStart(e){this.currentSessionId=e,this.setQueryType()}}}),Uw={};Mi(Uw,{init:()=>Lw});var ga,Lm,Lw,d2=re(()=>{Te(),nr(),Ce(),b$(),ga=class Ww{constructor(t,n,r,i){this.module=t,this.dataType=n,this.data=r,this.dims=i}getFloat32Array(){if(this.dataType!==1)throw new Error("Invalid data type");let t=W.size(this.dims);return t===0?new Float32Array:new Float32Array(this.module.HEAP8.buffer,this.data,t)}getBigInt64Array(){if(this.dataType!==7)throw new Error("Invalid data type");let t=W.size(this.dims);return t===0?new BigInt64Array:new BigInt64Array(this.module.HEAP8.buffer,this.data,t)}getInt32Array(){if(this.dataType!==6)throw new Error("Invalid data type");let t=W.size(this.dims);return t===0?new Int32Array:new Int32Array(this.module.HEAP8.buffer,this.data,t)}getUint16Array(){if(this.dataType!==10&&this.dataType!==4)throw new Error("Invalid data type");let t=W.size(this.dims);return t===0?new Uint16Array:new Uint16Array(this.module.HEAP8.buffer,this.data,t)}reshape(t){if(W.size(t)!==W.size(this.dims))throw new Error("Invalid new shape");return new Ww(this.module,this.dataType,this.data,t)}},Lm=class{constructor(e,t,n){this.module=e,this.backend=t,this.customDataOffset=0,this.customDataSize=0,this.adapterInfo=t.adapterInfo;let r=e.PTR_SIZE,i=n/e.PTR_SIZE,s=r===4?"i32":"i64";this.opKernelContext=Number(e.getValue(r*i++,s));let a=Number(e.getValue(r*i++,s));this.outputCount=Number(e.getValue(r*i++,s)),this.customDataOffset=Number(e.getValue(r*i++,"*")),this.customDataSize=Number(e.getValue(r*i++,s));let o=[];for(let l=0;ltypeof o=="number"?this.inputs[o]:o))??this.inputs,r=(t==null?void 0:t.outputs)??[],i=(o,l,d)=>new ga(this.module,l,this.output(o,d),d),s=(o,l)=>{let d=Vr(o,l);if(!d)throw new Error(`Unsupported data type: ${o}`);let p=d>0?this.backend.gpuDataManager.create(d).id:0;return new ga(this.module,o,p,l)};return this.backend.run(e,n,r,i,s,this.outputCount)}output(e,t){let n=this.module.stackSave();try{let r=this.module.PTR_SIZE,i=r===4?"i32":"i64",s=this.module.stackAlloc((1+t.length)*r);this.module.setValue(s,t.length,i);for(let a=0;a{let i=t.jsepInit;if(!i)throw new Error("Failed to initialize JSEP. The WebAssembly module is not built with JSEP support.");if(e==="webgpu"){let s=(u2(),As(Dw)).WebGpuBackend,a=new s;await a.initialize(n,r),i("webgpu",[a,o=>a.alloc(Number(o)),o=>a.free(o),(o,l,d,p=!1)=>{if(p)Pe("verbose",()=>`[WebGPU] jsepCopyGpuToGpu: src=${Number(o)}, dst=${Number(l)}, size=${Number(d)}`),a.memcpy(Number(o),Number(l));else{Pe("verbose",()=>`[WebGPU] jsepCopyCpuToGpu: dataOffset=${Number(o)}, gpuDataId=${Number(l)}, size=${Number(d)}`);let f=t.HEAPU8.subarray(Number(o>>>0),Number(o>>>0)+Number(d));a.upload(Number(l),f)}},async(o,l,d)=>{Pe("verbose",()=>`[WebGPU] jsepCopyGpuToCpu: gpuDataId=${o}, dataOffset=${l}, size=${d}`),await a.download(Number(o),()=>t.HEAPU8.subarray(Number(l)>>>0,Number(l+d)>>>0))},(o,l,d)=>a.createKernel(o,Number(l),d,t.UTF8ToString(t._JsepGetNodeName(Number(l)))),o=>a.releaseKernel(o),(o,l,d,p)=>{Pe("verbose",()=>`[WebGPU] jsepRun: sessionHandle=${d}, kernel=${o}, contextDataOffset=${l}`);let f=new Lm(t,a,Number(l));return a.computeKernel(Number(o),f,p)},()=>a.captureBegin(),()=>a.captureEnd(),()=>a.replay()])}else{let s=new Z0(n);i("webnn",[s,()=>s.reserveTensorId(),a=>s.releaseTensorId(a),async(a,o,l,d,p)=>s.ensureTensor(a,o,l,d,p),(a,o)=>{s.uploadTensor(a,o)},async(a,o)=>s.downloadTensor(a,o),(a,o)=>s.registerMLContext(a,o),!!n.trace])}}}),Wm,ad,od,pr,qm,Ol,Va,ld,ud,Nl,dd,cd,pd,qw=re(()=>{on(),m$(),g$(),Te(),ei(),Vu(),F0(),Wm=(e,t)=>{tt()._OrtInit(e,t)!==0&&je("Can't initialize onnxruntime.")},ad=async e=>{Wm(e.wasm.numThreads,Ua(e.logLevel))},od=async(e,t)=>{var r,i;(i=(r=tt()).asyncInit)==null||i.call(r);let n=e.webgpu.adapter;if(t==="webgpu"){if(typeof navigator>"u"||!navigator.gpu)throw new Error("WebGPU is not supported in current environment");if(n){if(typeof n.limits!="object"||typeof n.features!="object"||typeof n.requestDevice!="function")throw new Error("Invalid GPU adapter set in `env.webgpu.adapter`. It must be a GPUAdapter object.")}else{let s=e.webgpu.powerPreference;if(s!==void 0&&s!=="low-power"&&s!=="high-performance")throw new Error(`Invalid powerPreference setting: "${s}"`);let a=e.webgpu.forceFallbackAdapter;if(a!==void 0&&typeof a!="boolean")throw new Error(`Invalid forceFallbackAdapter setting: "${a}"`);if(n=await navigator.gpu.requestAdapter({powerPreference:s,forceFallbackAdapter:a}),!n)throw new Error('Failed to get GPU adapter. You may need to enable flag "--enable-unsafe-webgpu" if you are using Chrome.')}}if(t==="webnn"&&(typeof navigator>"u"||!navigator.ml))throw new Error("WebNN is not supported in current environment");{let s=(d2(),As(Uw)).init;t==="webgpu"&&await s("webgpu",tt(),e,n),t==="webnn"&&await s("webnn",tt(),e)}},pr=new Map,qm=e=>{let t=tt(),n=t.stackSave();try{let r=t.PTR_SIZE,i=t.stackAlloc(2*r);t._OrtGetInputOutputCount(e,i,i+r)!==0&&je("Can't get session input/output count.");let s=r===4?"i32":"i64";return[Number(t.getValue(i,s)),Number(t.getValue(i+r,s))]}finally{t.stackRestore(n)}},Ol=(e,t)=>{let n=tt(),r=n.stackSave(),i=0;try{let s=n.PTR_SIZE,a=n.stackAlloc(2*s);n._OrtGetInputOutputMetadata(e,t,a,a+s)!==0&&je("Can't get session input/output metadata.");let o=Number(n.getValue(a,"*"));i=Number(n.getValue(a+s,"*"));let l=n.HEAP32[i/4];if(l===0)return[o,0];let d=n.HEAPU32[i/4+1],p=[];for(let f=0;f{let t=tt(),n=t._malloc(e.byteLength);if(n===0)throw new Error(`Can't create a session. failed to allocate a buffer of size ${e.byteLength}.`);return t.HEAPU8.set(e,n),[n,e.byteLength]},ld=async(e,t)=>{var f,c,m,y;let n,r,i=tt();Array.isArray(e)?[n,r]=e:e.buffer===i.HEAPU8.buffer?[n,r]=[e.byteOffset,e.byteLength]:[n,r]=Va(e);let s=0,a=0,o=0,l=[],d=[],p=[];try{if([a,l]=await V0(t),(t==null?void 0:t.externalData)&&i.mountExternalData){let x=[];for(let U of t.externalData){let q=typeof U=="string"?U:U.path;x.push(ju(typeof U=="string"?U:U.data).then(J=>{i.mountExternalData(q,J)}))}await Promise.all(x)}for(let x of(t==null?void 0:t.executionProviders)??[])if((typeof x=="string"?x:x.name)==="webnn"){if(i.shouldTransferToMLTensor=!1,typeof x!="string"){let U=x,q=U==null?void 0:U.context,J=U==null?void 0:U.gpuDevice,se=U==null?void 0:U.deviceType,ce=U==null?void 0:U.powerPreference;q?i.currentContext=q:J?i.currentContext=await i.webnnCreateMLContext(J):i.currentContext=await i.webnnCreateMLContext({deviceType:se,powerPreference:ce})}else i.currentContext=await i.webnnCreateMLContext();break}s=await i._OrtCreateSession(n,r,a),(f=i.webgpuOnCreateSession)==null||f.call(i,s),s===0&&je("Can't create a session."),(c=i.jsepOnCreateSession)==null||c.call(i),i.currentContext&&(i.webnnRegisterMLContext(s,i.currentContext),i.currentContext=void 0,i.shouldTransferToMLTensor=!0);let[w,$]=qm(s),_=!!(t!=null&&t.enableGraphCapture),v=[],I=[],S=[],E=[],R=[];for(let x=0;xx==="gpu-buffer"||x==="ml-tensor"||x==="ml-tensor-cpu-output")&&(o=i._OrtCreateBinding(s),o===0&&je("Can't create IO binding."),O={handle:o,outputPreferredLocations:R,outputPreferredLocationsEncoded:R.map(x=>x==="ml-tensor-cpu-output"?"ml-tensor":x).map(x=>iu(x))}),pr.set(s,[s,d,p,O,_,!1]),[s,v,I,S,E]}catch(w){throw d.forEach($=>i._OrtFree($)),p.forEach($=>i._OrtFree($)),o!==0&&i._OrtReleaseBinding(o)!==0&&je("Can't release IO binding."),s!==0&&i._OrtReleaseSession(s)!==0&&je("Can't release session."),w}finally{i._free(n),a!==0&&i._OrtReleaseSessionOptions(a)!==0&&je("Can't release session options."),l.forEach(w=>i._free(w)),(y=i.unmountExternalData)==null||y.call(i)}},ud=e=>{var l,d,p;let t=tt(),n=pr.get(e);if(!n)throw new Error(`cannot release session. invalid session id: ${e}`);let[r,i,s,a,o]=n;a&&(o&&t._OrtClearBoundOutputs(a.handle)!==0&&je("Can't clear bound outputs."),t._OrtReleaseBinding(a.handle)!==0&&je("Can't release IO binding.")),(l=t.jsepOnReleaseSession)==null||l.call(t,e),(d=t.webnnOnReleaseSession)==null||d.call(t,e),(p=t.webgpuOnReleaseSession)==null||p.call(t,e),i.forEach(f=>t._OrtFree(f)),s.forEach(f=>t._OrtFree(f)),t._OrtReleaseSession(r)!==0&&je("Can't release session."),pr.delete(e)},Nl=async(e,t,n,r,i,s,a=!1)=>{if(!e){t.push(0);return}let o=tt(),l=o.PTR_SIZE,d=e[0],p=e[1],f=e[3],c=f,m,y;if(d==="string"&&(f==="gpu-buffer"||f==="ml-tensor"))throw new Error("String tensor is not supported on GPU.");if(a&&f!=="gpu-buffer")throw new Error(`External buffer must be provided for input/output index ${s} when enableGraphCapture is true.`);if(f==="gpu-buffer"){let _=e[2].gpuBuffer;y=Vr(Gr(d),p);{let v=o.jsepRegisterBuffer;if(!v)throw new Error('Tensor location "gpu-buffer" is not supported without using WebGPU.');m=v(r,s,_,y)}}else if(f==="ml-tensor"){let _=e[2].mlTensor;y=Vr(Gr(d),p);let v=o.webnnRegisterMLTensor;if(!v)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');m=v(r,_,Gr(d),p)}else{let _=e[2];if(Array.isArray(_)){y=l*_.length,m=o._malloc(y),n.push(m);for(let v=0;v<_.length;v++){if(typeof _[v]!="string")throw new TypeError(`tensor data at index ${v} is not a string`);o.setValue(m+v*l,gn(_[v],n),"*")}}else{let v=o.webnnIsGraphInput,I=o.webnnIsGraphOutput;if(d!=="string"&&v&&I){let S=o.UTF8ToString(i);if(v(r,S)||I(r,S)){let E=Gr(d);y=Vr(E,p),c="ml-tensor";let R=o.webnnCreateTemporaryTensor,O=o.webnnUploadTensor;if(!R||!O)throw new Error('Tensor location "ml-tensor" is not supported without using WebNN.');let x=await R(r,E,p);O(x,new Uint8Array(_.buffer,_.byteOffset,_.byteLength)),m=x}else y=_.byteLength,m=o._malloc(y),n.push(m),o.HEAPU8.set(new Uint8Array(_.buffer,_.byteOffset,y),m)}else y=_.byteLength,m=o._malloc(y),n.push(m),o.HEAPU8.set(new Uint8Array(_.buffer,_.byteOffset,y),m)}}let w=o.stackSave(),$=o.stackAlloc(4*p.length);try{p.forEach((v,I)=>o.setValue($+I*l,v,l===4?"i32":"i64"));let _=o._OrtCreateTensor(Gr(d),m,y,$,p.length,iu(c));_===0&&je(`Can't create tensor for input/output. session=${r}, index=${s}.`),t.push(_)}finally{o.stackRestore(w)}},dd=async(e,t,n,r,i,s)=>{var se,ce,L,ue;let a=tt(),o=a.PTR_SIZE,l=pr.get(e);if(!l)throw new Error(`cannot run inference. invalid session id: ${e}`);let d=l[0],p=l[1],f=l[2],c=l[3],m=l[4],y=l[5],w=t.length,$=r.length,_=0,v=[],I=[],S=[],E=[],R=[],O=a.stackSave(),x=a.stackAlloc(w*o),U=a.stackAlloc(w*o),q=a.stackAlloc($*o),J=a.stackAlloc($*o);try{[_,v]=G0(s),Kr("wasm prepareInputOutputTensor");for(let Q=0;QN*D,1);ne=Yn(Xe);let A=c==null?void 0:c.outputPreferredLocations[r[Q]];if(ne==="string"){if(A==="gpu-buffer"||A==="ml-tensor")throw new Error("String tensor is not supported on GPU.");let N=[];for(let D=0;D0){let N=a.jsepGetBuffer;if(!N)throw new Error('preferredLocation "gpu-buffer" is not supported without using WebGPU.');let D=N(Ue),H=Vr(Xe,ht);if(H===void 0||!Fu(ne))throw new Error(`Unsupported data type: ${ne}`);ee=!0,te.push([ne,ut,{gpuBuffer:D,download:a.jsepCreateDownloader(D,H,ne),dispose:()=>{a._OrtReleaseTensor(ve)!==0&&je("Can't release tensor.")}},"gpu-buffer"])}else if(A==="ml-tensor"&&ht>0){let N=a.webnnEnsureTensor,D=a.webnnIsGraphInputOutputTypeSupported;if(!N||!D)throw new Error('preferredLocation "ml-tensor" is not supported without using WebNN.');if(Vr(Xe,ht)===void 0||!Hu(ne))throw new Error(`Unsupported data type: ${ne}`);if(!D(e,ne,!1))throw new Error(`preferredLocation "ml-tensor" for ${ne} output is not supported by current WebNN Context.`);let H=await N(e,Ue,Xe,ut,!1);ee=!0,te.push([ne,ut,{mlTensor:H,download:a.webnnCreateMLTensorDownloader(Ue,ne),dispose:()=>{a.webnnReleaseTensorId(Ue),a._OrtReleaseTensor(ve)}},"ml-tensor"])}else if(A==="ml-tensor-cpu-output"&&ht>0){let N=a.webnnCreateMLTensorDownloader(Ue,ne)(),D=te.length;ee=!0,he.push((async()=>{let H=[D,await N];return a.webnnReleaseTensorId(Ue),a._OrtReleaseTensor(ve),H})()),te.push([ne,ut,[],"cpu"])}else{let N=ao(ne),D=new N(ht);new Uint8Array(D.buffer,D.byteOffset,D.byteLength).set(a.HEAPU8.subarray(Ue,Ue+D.byteLength)),te.push([ne,ut,D,"cpu"])}}finally{a.stackRestore(Y),ne==="string"&&Ue&&a._free(Ue),ee||a._OrtReleaseTensor(ve)}}c&&!m&&(a._OrtClearBoundOutputs(c.handle)!==0&&je("Can't clear bound outputs."),pr.set(e,[d,p,f,c,m,!1]));for(let[Q,ve]of await Promise.all(he))te[Q][2]=ve;return Xr("wasm ProcessOutputTensor"),te}finally{(ue=a.webnnOnRunEnd)==null||ue.call(a,d),a.stackRestore(O),I.forEach(ie=>a._OrtReleaseTensor(ie)),S.forEach(ie=>a._OrtReleaseTensor(ie)),E.forEach(ie=>a._free(ie)),_!==0&&a._OrtReleaseRunOptions(_),v.forEach(ie=>a._free(ie))}},cd=e=>{let t=tt(),n=pr.get(e);if(!n)throw new Error("invalid session id");let r=n[0],i=t._OrtEndProfiling(r);i===0&&je("Can't get an profile file name."),t._OrtFree(i)},pd=e=>{let t=[];for(let n of e){let r=n[2];!Array.isArray(r)&&"buffer"in r&&t.push(r.buffer)}return t}}),fr,Gt,pi,ls,us,ya,Bl,ba,Dr,Pr,Gm,Gw,Vw,Fw,Hw,jw,Kw,Xw,Yw=re(()=>{on(),qw(),ei(),qu(),fr=()=>!!Be.wasm.proxy&&typeof document<"u",pi=!1,ls=!1,us=!1,ba=new Map,Dr=(e,t)=>{let n=ba.get(e);n?n.push(t):ba.set(e,[t])},Pr=()=>{if(pi||!ls||us||!Gt)throw new Error("worker not ready")},Gm=e=>{switch(e.data.type){case"init-wasm":pi=!1,e.data.err?(us=!0,Bl[1](e.data.err)):(ls=!0,Bl[0]()),ya&&(URL.revokeObjectURL(ya),ya=void 0);break;case"init-ep":case"copy-from":case"create":case"release":case"run":case"end-profiling":{let t=ba.get(e.data.type);e.data.err?t.shift()[1](e.data.err):t.shift()[0](e.data.out);break}}},Gw=async()=>{if(!ls){if(pi)throw new Error("multiple calls to 'initWasm()' detected.");if(us)throw new Error("previous call to 'initWasm()' failed.");if(pi=!0,fr())return new Promise((e,t)=>{Gt==null||Gt.terminate(),W0().then(([n,r])=>{try{Gt=r,Gt.onerror=s=>t(s),Gt.onmessage=Gm,Bl=[e,t];let i={type:"init-wasm",in:Be};!i.in.wasm.wasmPaths&&(n||ru)&&(i.in.wasm.wasmPaths={wasm:new URL("/assets/ort-wasm-simd-threaded.jsep-CTuMJ5w1.wasm",import.meta.url).href}),Gt.postMessage(i),ya=n}catch(i){t(i)}},t)});try{await Gu(Be.wasm),await ad(Be),ls=!0}catch(e){throw us=!0,e}finally{pi=!1}}},Vw=async e=>{if(fr())return Pr(),new Promise((t,n)=>{Dr("init-ep",[t,n]);let r={type:"init-ep",in:{epName:e,env:Be}};Gt.postMessage(r)});await od(Be,e)},Fw=async e=>fr()?(Pr(),new Promise((t,n)=>{Dr("copy-from",[t,n]);let r={type:"copy-from",in:{buffer:e}};Gt.postMessage(r,[e.buffer])})):Va(e),Hw=async(e,t)=>{if(fr()){if(t!=null&&t.preferredOutputLocation)throw new Error('session option "preferredOutputLocation" is not supported for proxy.');return Pr(),new Promise((n,r)=>{Dr("create",[n,r]);let i={type:"create",in:{model:e,options:{...t}}},s=[];e instanceof Uint8Array&&s.push(e.buffer),Gt.postMessage(i,s)})}else return ld(e,t)},jw=async e=>{if(fr())return Pr(),new Promise((t,n)=>{Dr("release",[t,n]);let r={type:"release",in:e};Gt.postMessage(r)});ud(e)},Kw=async(e,t,n,r,i,s)=>{if(fr()){if(n.some(a=>a[3]!=="cpu"))throw new Error("input tensor on GPU is not supported for proxy.");if(i.some(a=>a))throw new Error("pre-allocated output tensor is not supported for proxy.");return Pr(),new Promise((a,o)=>{Dr("run",[a,o]);let l=n,d={type:"run",in:{sessionId:e,inputIndices:t,inputs:l,outputIndices:r,options:s}};Gt.postMessage(d,pd(l))})}else return dd(e,t,n,r,i,s)},Xw=async e=>{if(fr())return Pr(),new Promise((t,n)=>{Dr("end-profiling",[t,n]);let r={type:"end-profiling",in:e};Gt.postMessage(r)});cd(e)}}),Dl,Vm,Zw,c2=re(()=>{on(),Yw(),Te(),Wu(),F0(),Dl=(e,t)=>{switch(e.location){case"cpu":return[e.type,e.dims,e.data,"cpu"];case"gpu-buffer":return[e.type,e.dims,{gpuBuffer:e.gpuBuffer},"gpu-buffer"];case"ml-tensor":return[e.type,e.dims,{mlTensor:e.mlTensor},"ml-tensor"];default:throw new Error(`invalid data location: ${e.location} for ${t()}`)}},Vm=e=>{switch(e[3]){case"cpu":return new yn(e[0],e[2],e[1]);case"gpu-buffer":{let t=e[0];if(!Fu(t))throw new Error(`not supported data type: ${t} for deserializing GPU tensor`);let{gpuBuffer:n,download:r,dispose:i}=e[2];return yn.fromGpuBuffer(n,{dataType:t,dims:e[1],download:r,dispose:i})}case"ml-tensor":{let t=e[0];if(!Hu(t))throw new Error(`not supported data type: ${t} for deserializing MLTensor tensor`);let{mlTensor:n,download:r,dispose:i}=e[2];return yn.fromMLTensor(n,{dataType:t,dims:e[1],download:r,dispose:i})}default:throw new Error(`invalid data location: ${e[3]}`)}},Zw=class{async fetchModelAndCopyToWasmMemory(e){return Fw(await ju(e))}async loadModel(e,t){Bn();let n;typeof e=="string"?n=await this.fetchModelAndCopyToWasmMemory(e):n=e,[this.sessionId,this.inputNames,this.outputNames,this.inputMetadata,this.outputMetadata]=await Hw(n,t),wn()}async dispose(){return jw(this.sessionId)}async run(e,t,n){Bn();let r=[],i=[];Object.entries(e).forEach(f=>{let c=f[0],m=f[1],y=this.inputNames.indexOf(c);if(y===-1)throw new Error(`invalid input '${c}'`);r.push(m),i.push(y)});let s=[],a=[];Object.entries(t).forEach(f=>{let c=f[0],m=f[1],y=this.outputNames.indexOf(c);if(y===-1)throw new Error(`invalid output '${c}'`);s.push(m),a.push(y)});let o=r.map((f,c)=>Dl(f,()=>`input "${this.inputNames[i[c]]}"`)),l=s.map((f,c)=>f?Dl(f,()=>`output "${this.outputNames[a[c]]}"`):null),d=await Kw(this.sessionId,i,o,a,l,n),p={};for(let f=0;fbu,initializeFlags:()=>yu,wasmBackend:()=>Jw});var yu,bu,Jw,p2=re(()=>{on(),Yw(),c2(),yu=()=>{(typeof Be.wasm.initTimeout!="number"||Be.wasm.initTimeout<0)&&(Be.wasm.initTimeout=0);let e=Be.wasm.simd;if(typeof e!="boolean"&&e!==void 0&&e!=="fixed"&&e!=="relaxed"&&(console.warn(`Property "env.wasm.simd" is set to unknown value "${e}". Reset it to \`false\` and ignore SIMD feature checking.`),Be.wasm.simd=!1),typeof Be.wasm.proxy!="boolean"&&(Be.wasm.proxy=!1),typeof Be.wasm.trace!="boolean"&&(Be.wasm.trace=!1),typeof Be.wasm.numThreads!="number"||!Number.isInteger(Be.wasm.numThreads)||Be.wasm.numThreads<=0)if(typeof self<"u"&&!self.crossOriginIsolated)Be.wasm.numThreads=1;else{let t=typeof navigator>"u"?Z1("node:os").cpus().length:navigator.hardwareConcurrency;Be.wasm.numThreads=Math.min(4,Math.ceil((t||1)/2))}},bu=class{async init(e){yu(),await Gw(),await Vw(e)}async createInferenceSessionHandler(e,t){let n=new Zw;return await n.loadModel(e,t),n}},Jw=new bu});on();on();on();var f2="1.25.0-dev.20260228-6e72d31970";{let e=(p2(),As(Qw)).wasmBackend;yi("webgpu",e,5),yi("webnn",e,5),yi("cpu",e,10),yi("wasm",e,10)}Object.defineProperty(Be.versions,"web",{value:f2,enumerable:!0});/** +* @license +* Copyright 2021 Google LLC. All Rights Reserved. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +* ============================================================================= +*//** + * @license + * Copyright 2020 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + *//** + * @license + * Copyright 2019 Google LLC. All Rights Reserved. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============================================================================= + */const Ur=32;function Fa(e,t){if(!t)return;if(Array.isArray(t))return t[0];const n=e==null?void 0:e[0];if(n)return t[n]}function Fm(e,t,n){if(e)return Array.isArray(e)?e[n]:e[t]}function fi(e){if(!e)return"";const t=e/(1024*1024),n=Math.round(t*10)/10;return Number.isInteger(n)?`${n}MB`:`${n.toFixed(1)}MB`}function wa(e){return e<1e3?`${Math.round(e)}ms`:`${(e/1e3).toFixed(1)}s`}function Pl(e,t){return!e||!t?null:`${(e/1e3/t).toFixed(2)}s/tile`}function h2(e){return JSON.stringify(e,null,2)}function rn(e){return Number.isFinite(e??NaN)?Math.round(e*1e4)/1e4:e}function De(e,t,n){return Math.min(n,Math.max(t,e))}function Hm(e,t){if(t<=1)return 0;const n=(t-1)*2,r=(e%n+n)%n;return rr.trim()).filter(Boolean).map(r=>Number.parseFloat(r));for(;n.lengthNumber.isFinite(r)?r:0)}function g2(e){switch(e){case void 0:case null:case"":case"unknown":return"unknown";case"float":case"float32":case"tensor(float)":return"float32";case"float16":case"tensor(float16)":return"float16";case"uint8":case"tensor(uint8)":return"uint8";case"double":case"float64":case"tensor(double)":return"float64";default:return e}}function Ha(e){if(!e||typeof e!="object")return{type:"unknown",dimensions:[],dimensionLabels:[]};const t=e,n=y2(t);return{type:typeof t.type=="string"?t.type:"unknown",dimensions:n.map(w2),dimensionLabels:n.map(b2)}}function y2(e){var n;const t=e.dimensions??e.dims??e.shape??((n=e.tensorShape)==null?void 0:n.dimensions)??[];return Array.isArray(t)?t:Array.from(t)}function b2(e){if(typeof e=="string")return/^\d+$/.test(e.trim())?null:e.trim();if(e&&typeof e=="object"){const t=e;for(const n of["dimParam","dim_param","param","name","label"]){const r=t[n];if(typeof r=="string"&&r.trim())return r.trim()}}return null}function w2(e){if(typeof e=="number"&&Number.isFinite(e))return e;if(typeof e=="bigint"){const t=Number(e);return Number.isSafeInteger(t)?t:String(e)}if(typeof e=="string"){const t=e.trim();if(/^\d+$/.test(t)){const n=Number(t);if(Number.isSafeInteger(n))return n}return t||"dynamic"}return e==null?"dynamic":String(e)}function _a(e){if(!e)return!1;const t=e.toLowerCase();return t.includes("width")||t==="w"||t.endsWith("_w")}function va(e){if(!e)return!1;const t=e.toLowerCase();return t.includes("height")||t==="h"||t.endsWith("_h")}function wu(e){const t=(e==null?void 0:e.dimensions)??[];return typeof t[1]=="number"&&[1,3,4].includes(t[1])?"nchw":typeof t[3]=="number"&&[1,3,4].includes(t[3])?"nhwc":null}function fd(e,t){const n=(e==null?void 0:e.dimensionLabels)??[];if(n.length<4)return"hw";if(t==="nchw"){if(_a(n[2])&&va(n[3]))return"wh";if(va(n[2])&&_a(n[3]))return"hw"}if(t==="nhwc"){if(_a(n[1])&&va(n[2]))return"wh";if(va(n[1])&&_a(n[2]))return"hw"}return"hw"}function e_(e,t,n="hw"){return e.length<4?null:t==="nchw"&&Number.isFinite(e[2])&&Number.isFinite(e[3])?n==="wh"?{width:e[2],height:e[3]}:{width:e[3],height:e[2]}:t==="nhwc"&&Number.isFinite(e[1])&&Number.isFinite(e[2])?n==="wh"?{width:e[1],height:e[2]}:{width:e[2],height:e[1]}:null}function t_(e,t,n){const r=(e==null?void 0:e.dimensions)??[],i=Number(n.naturalWidth)||0,s=Number(n.naturalHeight)||0,a=n.requestedWidth&&n.requestedWidth>0?n.requestedWidth:null,o=n.requestedHeight&&n.requestedHeight>0?n.requestedHeight:null;if(a&&o)return{width:a,height:o,fixed:!1};if(a&&i>0&&s>0)return{width:a,height:Math.max(1,Math.round(s*a/i)),fixed:!1};if(o&&i>0&&s>0)return{width:Math.max(1,Math.round(i*o/s)),height:o,fixed:!1};const l=n.inferredFixedInputSize??e_(r,t,fd(e,t));return l?{width:l.width,height:l.height,fixed:!0}:{width:a||i,height:o||s,fixed:!1}}function _2(e){return e.fixed?{padRight:0,padBottom:0,multiple:null}:{padRight:(Ur-e.width%Ur)%Ur,padBottom:(Ur-e.height%Ur)%Ur,multiple:Ur}}function n_(e){const t="naturalWidth"in e?e.naturalWidth||e.width||0:e.width||0,n="naturalHeight"in e?e.naturalHeight||e.height||0:e.height||0;return{width:t,height:n}}function Km(e,t,n,r){const i=Ha(Fa(e==null?void 0:e.inputNames,e==null?void 0:e.inputMetadata)),s=wu(i)??n.layout,a=n_(t);return t_(i,s,{requestedWidth:Number.parseInt(n.width,10)||null,requestedHeight:Number.parseInt(n.height,10)||null,naturalWidth:a.width,naturalHeight:a.height,inferredFixedInputSize:r})}async function Xm(e,t,n,r,i={}){const s=Ha(Fa(t.inputNames,t.inputMetadata)),a=wu(s)??n.layout,o=fd(s,a),l=n_(e),d=t_(s,a,{requestedWidth:typeof i.requestedWidth=="number"?i.requestedWidth:Number.parseInt(n.width,10)||null,requestedHeight:typeof i.requestedHeight=="number"?i.requestedHeight:Number.parseInt(n.height,10)||null,naturalWidth:l.width,naturalHeight:l.height,inferredFixedInputSize:r}),p=d.width,f=d.height,c=_2({width:p,height:f,fixed:d.fixed}),m=p+c.padRight,y=f+c.padBottom,w=n.channelOrder,$=n.normalize,_=n.preResizeAlgorithm,v=jm(n.mean,3),I=jm(n.std,3).map(ie=>ie||1),S=g2(s.type),E=S==="unknown"?"float32":S;if(E!=="float32")throw new Error(`This inspector currently supports float32 image inputs only. Model expects ${E}.`);const R=document.createElement("canvas");R.width=l.width,R.height=l.height;const O=R.getContext("2d",{willReadFrequently:!0});if(!O)throw new Error("2D canvas context is unavailable.");O.drawImage(e,0,0,l.width,l.height);const U=(await Ss(R,p,f,_)).getContext("2d",{willReadFrequently:!0});if(!U)throw new Error("2D canvas context is unavailable.");const q=U.getImageData(0,0,p,f).data,J=3,se=new Float32Array(m*y*J),ce=w==="rgb"?[0,1,2]:[2,1,0],L=m*y>=1e6;for(let ie=0;ies&&(s=d),a+=d,o+=1)}return{min:rn(i),max:rn(s),mean:o?rn(a/o):null}}function v2(e){const t=e==null?void 0:e.data,n=$s(t);return{name:"",type:(e==null?void 0:e.type)??"unknown",dims:(e==null?void 0:e.dims)??[],count:(t==null?void 0:t.length)??0,min:n.min,max:n.max,mean:n.mean,sample:t?Array.from({length:Math.min(t.length,12)},(r,i)=>rn(Number(t[i]))??0):[]}}function x2(e){const t=Array.isArray(e==null?void 0:e.dims)?e.dims:[];if(t.length===4){if(typeof t[1]=="number"&&[1,3,4].includes(t[1]))return{width:t[3],height:t[2],channels:t[1],layout:"nchw"};if(typeof t[3]=="number"&&[1,3,4].includes(t[3]))return{width:t[2],height:t[1],channels:t[3],layout:"nhwc"}}if(t.length===3){if(typeof t[0]=="number"&&[1,3,4].includes(t[0]))return{width:t[2],height:t[1],channels:t[0],layout:"chw"};if(typeof t[2]=="number"&&[1,3,4].includes(t[2]))return{width:t[1],height:t[0],channels:t[2],layout:"hwc"}}return null}function $2(e,t,n){if(!n)return{width:e,height:t};const r=Number(n.paddedWidth)||e,i=Number(n.paddedHeight)||t,s=e/Math.max(1,r),a=t/Math.max(1,i),o=Number(n.inputWidth)||r,l=Number(n.inputHeight)||i;return{width:De(Math.round(o*s),1,e),height:De(Math.round(l*a),1,t)}}function r_(e){const t=$s(e),n=t.min!==null&&t.max!==null&&t.min>=-.25&&t.max<=1.25;let r=t.min??0,i=t.max??1;return n?(r=0,i=1):r>=0&&i>1.25&&i<=255&&(r=0,i=255),{sourceMin:r,sourceMax:i}}function Ll(e,t,n){if(!Number.isFinite(e))return 0;if(n<=1)return De(Math.round(e*255),0,255);const r=(e-t)/Math.max(1e-6,n-t);return De(Math.round(r*255),0,255)}async function Ym(e,t=null,n=null){const r=x2(e);if(!r)return null;const i=e==null?void 0:e.data;if(!(i!=null&&i.length))return null;const s=$2(r.width,r.height,t),a=document.createElement("canvas");a.width=s.width,a.height=s.height;const o=a.getContext("2d");if(!o)return null;const l=o.createImageData(s.width,s.height),{sourceMin:d,sourceMax:p}=n??r_(i),f=r.width*r.height,c=Math.max(1,r.channels),m=r.layout==="nchw"||r.layout==="chw",y=Math.min(1,c-1),w=Math.min(2,c-1),$=s.height>=512||s.width*s.height>=1e6;for(let _=0;_{e.toBlob(i=>{if(!i){r(new Error("Failed to encode canvas as an image."));return}n(URL.createObjectURL(i))},t)})}function S2(e){const t=document.createElement("canvas");t.width=e.width,t.height=e.height;const n=t.getContext("2d");if(!n)throw new Error("2D canvas context is unavailable.");return n.drawImage(e,0,0),t}function i_(e,t,n){const r=De(n,0,.2),i=Math.max(0,Math.floor(t*r)),s=Math.max(0,Math.floor(t*.5)),a=Math.max(0,Math.ceil(t*(1-r))-1),o=ql(e,i),l=ql(e,s),d=ql(e,a);return{low:o,mid:l,high:Math.max(o+1,d)}}function ql(e,t){let n=0;for(let r=0;rt)return r;return e.length-1}function Zm(e,t){const n=[new Uint32Array(256),new Uint32Array(256),new Uint32Array(256)];let r=0;for(let i=0;ii_(i,r,t))}function Qm(e,t){const n=new Uint32Array(256);let r=0;for(let i=0;i=.999||r<=.001||r>=.999?1:De(Math.log(r)/Math.log(n),.35,2.5)}async function I2(e,t,n){const r=De(n.strength,0,1);if(r<=0)return e;const i=e.getContext("2d",{willReadFrequently:!0}),s=t.getContext("2d",{willReadFrequently:!0});if(!i||!s)throw new Error("2D canvas context is unavailable.");const a=i.getImageData(0,0,e.width,e.height),o=s.getImageData(0,0,t.width,t.height),l=Zm(a.data,n.clipFraction),d=Zm(o.data,n.clipFraction),p=Qm(a.data,n.clipFraction),f=Qm(o.data,n.clipFraction),c=T2(p,f),m=e.width*e.height>=1e6;for(let y=0;y0&&y%48===0&&await Zt()}return i.putImageData(a,0,0),e}function Jm(e){const t=De(e,0,1);return t*t*(3-2*t)}function C2(e){return e-Math.floor(e)}function Gl(e,t,n){return e+(t-e)*n}function xa(e,t,n){return C2(Math.sin(e*127.1+t*311.7+n*74.7)*43758.5453123)*2-1}function $a(e,t,n,r){const i=e/Math.max(.001,n),s=t/Math.max(.001,n),a=Math.floor(i),o=Math.floor(s),l=Jm(i-a),d=Jm(s-o),p=xa(a,o,r),f=xa(a+1,o,r),c=xa(a,o+1,r),m=xa(a+1,o+1,r),y=Gl(p,f,l),w=Gl(c,m,l);return Gl(y,w,d)}async function E2(e,t){const n=De(t.amount,0,1);if(n<=0)return e;const r=e.getContext("2d",{willReadFrequently:!0});if(!r)throw new Error("2D canvas context is unavailable.");const i=r.getImageData(0,0,e.width,e.height),s=e.width*e.height>=1e6,a=Math.max(.5,t.size),o=n*52,l=e.width*.173+e.height*.117+a*4.91+o*.29+(t.monochrome?1.7:8.3);for(let d=0;d0&&d%48===0&&await Zt()}return r.putImageData(i,0,0),e}function s_(e){const t=Array.from(e.entries()).filter(([,r])=>Math.abs(r)>1e-8);if(!t.length)return{offsets:[0],weights:[1]};const n=t.reduce((r,[,i])=>r+i,0);return Math.abs(n)<=1e-8?{offsets:t.map(([r])=>r),weights:t.map(()=>1/t.length)}:{offsets:t.map(([r])=>r),weights:t.map(([,r])=>r/n)}}function k2(e,t){const n=e/Math.max(1,t);return Array.from({length:t},(r,i)=>{const s=i*n,a=s+n,o=Math.floor(s),l=Math.ceil(a)-1,d=new Map;for(let p=o;p<=l;p+=1){const f=Math.max(0,Math.min(a,p+1)-Math.max(s,p));if(f<=0)continue;const c=De(p,0,e-1);d.set(c,(d.get(c)??0)+f)}return s_(d)})}function z2(e,t){const n=e/Math.max(1,t);return Array.from({length:t},(r,i)=>{const s=(i+.5)*n-.5;return{offsets:[De(Math.round(s),0,e-1)],weights:[1]}})}function A2(e){const n=Math.abs(e);return n>=2?0:n<1?((-.5+2)*n-(-.5+3))*n*n+1:((-.5*n-5*-.5)*n+8*-.5)*n-4*-.5}function M2(e,t){const n=Math.abs(e);if(n===0)return 1;if(n>=t)return 0;const r=Math.PI*n;return t*Math.sin(r)*Math.sin(r/t)/(r*r)}function R2(e,t,n){const r=e/Math.max(1,t),i=Math.max(1,r),s=n==="lanczos"?3:2,a=s*i;return Array.from({length:t},(o,l)=>{const d=(l+.5)*r-.5,p=Math.ceil(d-a),f=Math.floor(d+a),c=new Map;for(let m=p;m<=f;m+=1){const y=Math.abs((m-d)/i),w=n==="lanczos"?M2(y,s):A2(y);if(w===0)continue;const $=De(m,0,e-1);c.set($,(c.get($)??0)+w/i)}return s_(c)})}function eg(e,t,n){return n==="nearest"?z2(e,t):n==="area"?k2(e,t):R2(e,t,n)}async function Ss(e,t,n,r){const i=Math.max(1,Math.round(t)),s=Math.max(1,Math.round(n));if(i===e.width&&s===e.height)return e;const a=e.getContext("2d",{willReadFrequently:!0});if(!a)throw new Error("2D canvas context is unavailable.");const l=a.getImageData(0,0,e.width,e.height).data,d=eg(e.width,i,r),p=eg(e.height,s,r),f=new Float32Array(i*e.height*4),c=i*s>=1e6||e.width*e.height>=1e6;for(let $=0;$0&&$%32===0&&await Zt()}const m=new Uint8ClampedArray(i*s*4);for(let $=0;$1e-8){const q=255/x;m[S]=De(Math.round(E*q),0,255),m[S+1]=De(Math.round(R*q),0,255),m[S+2]=De(Math.round(O*q),0,255)}else m[S]=0,m[S+1]=0,m[S+2]=0;m[S+3]=U}c&&$>0&&$%32===0&&await Zt()}const y=document.createElement("canvas");y.width=i,y.height=s;const w=y.getContext("2d");if(!w)throw new Error("2D canvas context is unavailable.");return w.putImageData(new ImageData(m,i,s),0,0),y}async function tg(e,t,n,r){return Ss(e,t,n,r)}function O2(e,t){t==null||t("Session metadata debug.",{inputNames:e.inputNames,outputNames:e.outputNames,inputMetadataKeys:e.inputMetadata?Object.keys(e.inputMetadata):null,outputMetadataKeys:e.outputMetadata?Object.keys(e.outputMetadata):null,sampleInputMetadata:Fa(e.inputNames,e.inputMetadata),sampleOutputMetadata:Fa(e.outputNames,e.outputMetadata)});const n=(e.inputNames||[]).map((i,s)=>({name:i,...Ha(Fm(e.inputMetadata,i,s))})),r=(e.outputNames||[]).map((i,s)=>({name:i,...Ha(Fm(e.outputMetadata,i,s))}));return{inputCount:n.length,outputCount:r.length,inputs:n,outputs:r,primaryInput:n[0]||null,primaryOutput:r[0]||null}}function ng(e,t){const n=e instanceof Error?e.message:String(e);return t==="webgl"&&n.includes("int64 is not supported")?"WebGL EP cannot handle int64 tensors by itself. Use WebGPU or WASM for this model.":null}function N2(e){const t=e instanceof Error?e.message:String(e);return t.includes("[WebGPU]")||t.includes("Failed to generate kernel")}function B2(e){const t=e instanceof Error?e.message:String(e);return t.includes("Got invalid dimensions for input")&&t.includes("Expected:")?[t,"Hint: this model has fixed input dimensions. The app will retry once when the expected size is explicit."].join(` + +`):t.includes("ReshapeHelper")||t.includes("The input tensor cannot be reshaped to the requested shape")?[t,"Hint: this model appears to have fixed internal shape assumptions that do not match the current image size."].join(` + +`):N2(e)?[t,"Hint: this looks like a WebGPU kernel/provider issue. Try WASM or a different model export."].join(` + +`):t}async function rg(){const e=navigator,t={secureContext:typeof isSecureContext=="boolean"?isSecureContext:null,crossOriginIsolated:typeof crossOriginIsolated=="boolean"?crossOriginIsolated:null,hasSharedArrayBuffer:typeof SharedArrayBuffer=="function",hasNavigatorGpu:!!e.gpu,userAgent:typeof navigator=="object"?navigator.userAgent:null};if(!e.gpu)return t;try{let n=await e.gpu.requestAdapter({powerPreference:"high-performance"});if(n||(n=await e.gpu.requestAdapter()),t.adapter=n?"available":"null",!n)return t;if(typeof n.requestAdapterInfo=="function"){const r=await n.requestAdapterInfo();t.adapterInfo={vendor:r.vendor,architecture:r.architecture,device:r.device,description:r.description}}t.features=Array.from(n.features,r=>String(r)).slice(0,12)}catch(n){t.error=n instanceof Error?n.message:String(n)}return t}function ig(){const e={hasWebGl:!1,hasWebGl2:!1,unmaskedVendor:null,unmaskedRenderer:null,vendor:null,renderer:null,isSoftwareRenderer:null,error:null};try{const t=document.createElement("canvas"),n=t.getContext("webgl2");e.hasWebGl2=!!n;const r=n||t.getContext("webgl")||t.getContext("experimental-webgl");if(!r)return e;e.hasWebGl=!0;const i=r.getExtension("WEBGL_debug_renderer_info");i&&(e.unmaskedVendor=r.getParameter(i.UNMASKED_VENDOR_WEBGL),e.unmaskedRenderer=r.getParameter(i.UNMASKED_RENDERER_WEBGL)),e.vendor=r.getParameter(r.VENDOR),e.renderer=r.getParameter(r.RENDERER);const s=String(e.unmaskedRenderer||e.renderer||"");e.isSoftwareRenderer=/swiftshader|llvmpipe|software/i.test(s)}catch(t){e.error=t instanceof Error?t.message:String(t)}return e}function Vl(e){const t=e,n=[t==null?void 0:t.executionProvider,t==null?void 0:t.provider];for(const r of n)if(typeof r=="string"&&r.trim())return r.trim();return null}function Zt(){return new Promise(e=>{if(typeof requestAnimationFrame=="function"){requestAnimationFrame(()=>e());return}setTimeout(e,0)})}function hi(e){return new Promise((t,n)=>{const r=new Image,i=URL.createObjectURL(e);r.onload=()=>{URL.revokeObjectURL(i),t(r)},r.onerror=()=>{URL.revokeObjectURL(i),n(new Error("Failed to decode image file."))},r.src=i})}function D2(e,t,n){const r=$i({splitPercent:50,zoom:1,originX:50,originY:50,dragging:!1,panning:!1,panStartX:0,panStartY:0,panOriginStartX:50,panOriginStartY:50}),i=ot(()=>{const m=r.splitPercent/100,y=r.originX/100,w=Math.max(1,r.zoom),$=(m-(1-w)*y)/w;return rn(De($,0,1)*100)??50}),s=ot(()=>({"--split":`${r.splitPercent}%`,"--split-adjusted":`${i.value}%`,"--zoom":String(r.zoom),"--origin-x":`${r.originX}%`,"--origin-y":`${r.originY}%`}));function a(m){var y,w,$,_;if(!(!t.value||!e.value)){if(m.button===2&&r.zoom>1){m.preventDefault(),r.panning=!0,r.panStartX=m.clientX,r.panStartY=m.clientY,r.panOriginStartX=r.originX,r.panOriginStartY=r.originY,(w=(y=e.value).setPointerCapture)==null||w.call(y,m.pointerId),e.value.style.cursor="grabbing";return}m.button===0&&n.value&&(r.dragging=!0,(_=($=e.value).setPointerCapture)==null||_.call($,m.pointerId),o(m))}}function o(m){var $;const y=($=e.value)==null?void 0:$.getBoundingClientRect();if(!(y!=null&&y.width))return;const w=(m.clientX-y.left)/y.width;r.splitPercent=De(w*100,0,100)}function l(m){var S;const y=(S=e.value)==null?void 0:S.getBoundingClientRect();if(!(y!=null&&y.width)||!y.height)return;const w=m.clientX-r.panStartX,$=m.clientY-r.panStartY,_=r.zoom,v=w/y.width/_*100,I=$/y.height/_*100;r.originX=De(r.panOriginStartX-v,0,100),r.originY=De(r.panOriginStartY-I,0,100)}function d(m){if(r.panning){l(m);return}r.dragging&&o(m)}function p(){if(r.panning){r.panning=!1,e.value&&(e.value.style.cursor="");return}r.dragging=!1}function f(m){r.zoom>1&&m.preventDefault()}function c(m){var q;if(!t.value)return;const y=(q=e.value)==null?void 0:q.getBoundingClientRect();if(!(y!=null&&y.width)||!y.height)return;m.preventDefault();const w=(m.clientX-y.left)/y.width,$=(m.clientY-y.top)/y.height,_=r.zoom,v=r.originX/100,I=r.originY/100,S=(w-v*(1-_))/_,E=($-I*(1-_))/_,R=m.deltaY<0?1.12:.9,O=De(rn(_*R)??_,1,6);let x=v,U=I;O!==1&&(x=(w-S*O)/(1-O),U=($-E*O)/(1-O)),r.originX=De(x*100,0,100),r.originY=De(U*100,0,100),r.zoom=O}return Ou(()=>{window.addEventListener("pointermove",d),window.addEventListener("pointerup",p)}),no(()=>{window.removeEventListener("pointermove",d),window.removeEventListener("pointerup",p)}),{styleVars:s,zoomScale:ot(()=>r.zoom),zoomLabel:ot(()=>`${Math.round(r.zoom*100)}%`),startCompareDrag:a,preventContextMenu:f,handleWheel:c}}const P2={class:"flex min-h-0 flex-1 flex-col gap-2"},U2=["src","alt","hidden"],L2=["src","hidden"],W2=["hidden"],q2=["hidden"],G2={class:"text-[11px] text-stone-500"},V2=["hidden"],F2={class:"font-semibold text-stone-800"},H2={class:"text-[11px] text-stone-500"},j2={class:"absolute bottom-3 right-3 min-w-14 rounded-lg border border-stone-300 bg-white/95 px-2.5 py-1.5 text-center text-xs font-medium text-stone-600 shadow-sm"},K2=["hidden"],X2={class:"relative overflow-hidden rounded-xl border border-stone-300 bg-white/85 px-4 py-2.5 text-xs text-stone-700 shadow-sm"},Y2={class:"relative flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between"},Z2={class:"flex flex-wrap gap-x-4 gap-y-2"},Q2={class:"flex items-center gap-1.5"},J2={class:"text-stone-800"},e3={class:"flex items-center gap-1.5"},t3={class:"text-stone-800"},n3={class:"flex items-center gap-1.5"},r3={class:"text-stone-800"},i3={class:"flex items-center gap-1.5"},s3={class:"text-stone-800"},a3={class:"flex flex-wrap items-center gap-x-4 gap-y-2"},o3={class:"flex items-center gap-1.5"},l3={class:"text-stone-800"},u3={class:"flex items-center gap-1.5"},d3={class:"text-stone-800"},c3=["disabled"],p3=eo({__name:"ComparePanel",props:{baseImageUrl:{},overlayImageUrl:{},baseLabel:{},baseResolution:{},overlayResolution:{},emptyMessage:{},resultInfo:{},canDownload:{type:Boolean},statusTone:{}},emits:["download"],setup(e,{emit:t}){const n=e,r=t,i=bt(null),s=ot(()=>!!n.baseImageUrl),a=ot(()=>!!n.overlayImageUrl),{handleWheel:o,preventContextMenu:l,startCompareDrag:d,styleVars:p,zoomLabel:f,zoomScale:c}=D2(i,s,a),m=ot(()=>{const S=n.resultInfo.progress.match(/(\d+(?:\.\d+)?)%/),E=S?Number.parseFloat(S[1]):0,R=Number.isFinite(E)?Math.min(100,Math.max(0,E)):0,O=n.resultInfo.tiles.match(/^(\d+)\s*\/\s*(\d+)$/);return(O?Number.parseInt(O[2],10):0)===1&&R<100?`${Math.max(R,33.3333)}%`:`${R}%`}),y=ot(()=>n.statusTone==="success"?"bg-emerald-400/80":n.statusTone==="error"?"bg-rose-400/80":n.statusTone==="busy"?"bg-amber-400/80":"bg-stone-300/90"),w=["linear-gradient(45deg, #f0f0f0 25%, transparent 25%) -16px 0 / 32px 32px","linear-gradient(-45deg, #f0f0f0 25%, transparent 25%) -16px 0 / 32px 32px","linear-gradient(45deg, transparent 75%, #f0f0f0 75%) -16px 0 / 32px 32px","linear-gradient(-45deg, transparent 75%, #f0f0f0 75%) -16px 0 / 32px 32px","#fff"].join(", "),$=ot(()=>({...p.value,background:w})),_=ot(()=>({transform:"scale(var(--zoom))",transformOrigin:"var(--origin-x) var(--origin-y)",imageRendering:c.value>1?"pixelated":"auto"})),v=ot(()=>({..._.value,clipPath:"inset(0 calc(100% - var(--split-adjusted)) 0 0)"})),I={left:"calc(var(--split) - 1px)",background:"linear-gradient(180deg, transparent, #666 18%, #333 50%, #666 82%, transparent)",boxShadow:"0 0 0 1px rgba(0,0,0,0.1), 0 0 12px rgba(0,0,0,0.2)"};return(S,E)=>(kn(),zn("section",P2,[M("div",{ref_key:"compareStageRef",ref:i,class:"relative flex-1 overflow-hidden rounded-xl border border-stone-300 bg-white touch-none",style:Fr($.value),onPointerdown:E[0]||(E[0]=(...R)=>We(d)&&We(d)(...R)),onContextmenu:E[1]||(E[1]=(...R)=>We(l)&&We(l)(...R)),onWheel:E[2]||(E[2]=(...R)=>We(o)&&We(o)(...R))},[M("img",{src:e.baseImageUrl,class:"pointer-events-none absolute inset-0 h-full w-full select-none object-contain",style:Fr(_.value),alt:`${e.baseLabel} image`,hidden:!s.value},null,12,U2),M("img",{src:e.overlayImageUrl,class:"pointer-events-none absolute inset-0 h-full w-full select-none object-contain",style:Fr(v.value),alt:"Upscaled output",hidden:!a.value},null,12,L2),M("div",{class:"pointer-events-none absolute inset-y-0 w-0.5",style:I,hidden:!(s.value&&a.value)},null,8,W2),M("div",{class:"pointer-events-none absolute left-3 top-3 flex flex-col gap-0.5 rounded-lg border border-stone-300 bg-white/95 px-2.5 py-1.5 text-xs text-stone-600 shadow-sm",hidden:!a.value},[E[4]||(E[4]=M("div",{class:"font-semibold text-stone-800"},"Upscaled",-1)),M("div",G2,wt(e.overlayResolution),1)],8,q2),M("div",{class:"pointer-events-none absolute right-3 top-3 flex flex-col gap-0.5 rounded-lg border border-stone-300 bg-white/95 px-2.5 py-1.5 text-xs text-stone-600 shadow-sm",hidden:!s.value},[M("div",F2,wt(e.baseLabel),1),M("div",H2,wt(e.baseResolution),1)],8,V2),M("div",j2,wt(We(f)),1),M("div",{class:"absolute inset-0 grid place-items-center bg-white/50 p-8 text-center text-sm text-stone-500",hidden:s.value},wt(e.emptyMessage),9,K2)],36),M("div",X2,[M("div",{class:Os(["pointer-events-none absolute left-0 top-0 h-2 transition-[width] duration-300",y.value]),style:Fr({width:m.value})},null,6),M("div",Y2,[M("div",Z2,[M("div",Q2,[E[5]||(E[5]=M("span",{class:"font-medium text-stone-500"},"Progress:",-1)),M("span",J2,wt(e.resultInfo.progress),1)]),M("div",e3,[E[6]||(E[6]=M("span",{class:"font-medium text-stone-500"},"Tiles:",-1)),M("span",t3,wt(e.resultInfo.tiles),1)]),M("div",n3,[E[7]||(E[7]=M("span",{class:"font-medium text-stone-500"},"Avg/tile:",-1)),M("span",r3,wt(e.resultInfo.speed),1)]),M("div",i3,[E[8]||(E[8]=M("span",{class:"font-medium text-stone-500"},"Run time:",-1)),M("span",s3,wt(e.resultInfo.totalTime),1)])]),M("div",a3,[M("div",o3,[E[9]||(E[9]=M("span",{class:"font-medium text-stone-500"},"Resolution:",-1)),M("span",l3,wt(e.resultInfo.resolution),1)]),M("div",u3,[E[10]||(E[10]=M("span",{class:"font-medium text-stone-500"},"Size:",-1)),M("span",d3,wt(e.resultInfo.size),1)]),M("button",{type:"button",class:"inline-flex items-center justify-center rounded-md bg-white px-2.5 py-1.5 text-xs font-semibold text-stone-900 ring-1 ring-inset ring-stone-300 transition hover:bg-stone-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-400 disabled:cursor-not-allowed disabled:bg-stone-100 disabled:text-stone-400 disabled:ring-stone-200",disabled:!e.canDownload,onClick:E[3]||(E[3]=R=>r("download"))}," Download ",8,c3)])])])]))}}),f3={class:"w-full lg:w-80 lg:shrink-0"},h3={class:"flex flex-col gap-3 rounded-xl border border-stone-300 bg-white p-4 shadow-sm"},m3={class:"flex flex-col gap-2"},g3={class:"flex items-start justify-between gap-3"},y3={class:"mt-1 text-[11px] text-stone-500"},b3=["disabled"],w3=["disabled"],_3={class:"flex flex-col gap-2.5"},v3={class:"flex flex-col gap-1"},x3={class:"relative"},$3=["disabled"],S3=["label"],T3=["value"],I3={class:"flex flex-col gap-1"},C3={class:"flex flex-col gap-1.5"},E3=["disabled"],k3=["disabled"],z3={class:"flex min-w-0 flex-col gap-0.5 rounded-xl border border-stone-300 bg-stone-50 px-3 py-2.5"},A3={class:"min-w-0 break-words text-sm font-semibold text-stone-900"},M3={class:"min-w-0 break-words text-[11px] text-stone-600"},R3={class:"flex flex-col gap-2.5"},O3={class:"flex items-center justify-between rounded-lg border border-stone-200 bg-stone-50 px-3 py-2"},N3=["disabled"],B3=["disabled"],D3={class:"flex flex-col gap-1"},P3={class:"flex flex-col gap-1.5"},U3=["disabled"],L3=["disabled"],W3={class:"m-0 min-h-4 text-xs text-stone-500"},q3={class:"flex flex-col gap-1"},G3={class:"relative"},V3=["disabled"],F3={class:"flex flex-col gap-1"},H3=["disabled"],j3={class:"flex items-center justify-between rounded-lg border border-stone-200 bg-stone-50 px-3 py-2"},K3=["disabled"],X3={class:"flex flex-col gap-1"},Y3=["disabled"],Z3={class:"flex flex-col gap-1"},Q3=["disabled"],J3={class:"flex flex-col gap-1"},eS={class:"relative"},tS=["disabled"],nS={class:"flex flex-col gap-1"},rS=["disabled"],iS={class:"flex flex-col gap-1"},sS={class:"relative"},aS=["disabled"],oS={class:"flex items-center justify-between rounded-lg border border-stone-200 bg-stone-50 px-3 py-2"},lS=["disabled"],uS={class:"flex flex-col gap-1"},dS={class:"relative"},cS=["disabled"],pS={class:"group"},fS={class:"flex flex-col gap-2.5 pt-2"},hS={class:"flex items-center justify-between rounded-lg border border-stone-200 bg-stone-50 px-3 py-2"},mS=["disabled"],gS={class:"flex flex-col gap-1"},yS=["disabled"],bS={class:"flex flex-col gap-1"},wS=["disabled"],_S={class:"flex items-center justify-between rounded-lg border border-stone-200 bg-stone-50 px-3 py-2"},vS=["disabled"],xS={class:"flex flex-col gap-1"},$S=["disabled"],SS={class:"flex flex-col gap-1"},TS=["disabled"],IS={class:"flex items-center justify-between rounded-lg border border-stone-200 bg-stone-50 px-3 py-2"},CS=["disabled"],ES={class:"flex flex-col gap-1"},kS=["disabled"],zS={class:"flex flex-col gap-1"},AS=["disabled"],MS={class:"flex flex-col gap-1"},RS={class:"relative"},OS=["disabled"],NS={class:"flex flex-col gap-1"},BS={class:"relative"},DS=["disabled"],PS={class:"flex flex-col gap-1"},US={class:"relative"},LS=["disabled"],WS={class:"flex flex-col gap-1"},qS={class:"relative"},GS=["disabled"],VS={class:"flex flex-col gap-1"},FS={class:"relative"},HS=["disabled"],jS={class:"flex items-center justify-between rounded-lg border border-stone-200 bg-stone-50 px-3 py-2"},KS=["disabled"],XS={class:"flex flex-col gap-1"},YS=["disabled"],ZS={class:"flex flex-col gap-1"},QS=["disabled"],JS=eo({__name:"ControlPanel",props:{controls:{},onnxVersionText:{},statusText:{},statusTone:{},modelStatusLabel:{},modelStatusMeta:{},imageStatusText:{},predefinedModelGroups:{},isRunning:{type:Boolean},isModelLoading:{type:Boolean}},emits:["modelFile","imageFile","reload","run","cancel","reset"],setup(e,{emit:t}){const n=e,r=t,i=bt(null),s=bt(null),a=ot(()=>n.isRunning||n.isModelLoading);function o(){var f;(f=i.value)==null||f.click()}function l(){var f;(f=s.value)==null||f.click()}function d(f){var m;const c=f.target;r("modelFile",((m=c.files)==null?void 0:m[0])||null),c.value=""}function p(f){var m;const c=f.target;r("imageFile",((m=c.files)==null?void 0:m[0])||null),c.value=""}return(f,c)=>(kn(),zn("aside",f3,[M("section",h3,[M("div",m3,[M("div",g3,[M("div",null,[c[33]||(c[33]=M("h1",{class:"m-0 text-lg font-semibold text-stone-900"},"ONNX in Browser",-1)),M("div",y3,wt(e.onnxVersionText),1)]),M("button",{type:"button",class:"shrink-0 text-[11px] font-normal text-stone-500 underline decoration-stone-300 underline-offset-2 transition hover:text-stone-700 disabled:cursor-not-allowed disabled:text-stone-300 disabled:decoration-stone-200",disabled:a.value,onClick:c[0]||(c[0]=m=>r("reset"))}," Reset ",8,b3)]),M("div",{class:Os(["w-full px-3 py-2 text-center text-xs font-medium select-none",e.statusTone==="success"&&"bg-emerald-100 text-emerald-900",e.statusTone==="error"&&"bg-rose-100 text-rose-900",e.statusTone==="busy"&&"bg-amber-100 text-amber-900",!e.statusTone&&"bg-stone-100 text-stone-600"])},wt(e.statusText),3),e.isRunning?(kn(),zn("button",{key:1,type:"button",class:"inline-flex w-full items-center justify-center rounded-md bg-stone-700 px-3.5 py-2.5 text-sm font-semibold text-white transition hover:bg-stone-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 disabled:cursor-not-allowed disabled:bg-stone-300 disabled:text-stone-500",onClick:c[2]||(c[2]=m=>r("cancel"))}," Cancel ")):(kn(),zn("button",{key:0,type:"button",class:"inline-flex w-full items-center justify-center rounded-md bg-stone-900 px-3.5 py-2.5 text-sm font-semibold text-white transition hover:bg-stone-800 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 disabled:cursor-not-allowed disabled:bg-stone-300 disabled:text-stone-500",disabled:e.isModelLoading,onClick:c[1]||(c[1]=m=>r("run"))}," Run ",8,w3))]),M("div",_3,[M("label",v3,[c[36]||(c[36]=M("span",{class:"text-xs font-medium text-stone-600"},"Model",-1)),M("div",x3,[Ze(M("select",{"onUpdate:modelValue":c[3]||(c[3]=m=>n.controls.selectedPredefinedModelId=m),disabled:a.value,class:"w-full appearance-none rounded-lg border border-stone-300 bg-white px-3 py-2 pr-11 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},[c[34]||(c[34]=M("option",{value:""},"-- Select a model or choose a file --",-1)),(kn(!0),zn(mn,null,Dc(e.predefinedModelGroups,m=>(kn(),zn("optgroup",{key:m.label,label:m.label},[(kn(!0),zn(mn,null,Dc(m.options,y=>(kn(),zn("option",{key:y.value,value:y.value},wt(y.label),9,T3))),128))],8,S3))),128))],8,$3),[[Tn,n.controls.selectedPredefinedModelId]]),c[35]||(c[35]=M("svg",{class:"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-500",viewBox:"0 0 16 16",fill:"none","aria-hidden":"true"},[M("path",{d:"M4 6l4 4 4-4",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"1.5"})],-1))])]),M("label",I3,[c[37]||(c[37]=M("span",{class:"flex w-full items-center justify-between gap-2 text-xs font-medium text-stone-600"},[M("span",null,"Model file"),M("a",{class:"font-normal text-stone-500 underline decoration-stone-300 underline-offset-2 transition hover:text-stone-700",href:"https://huggingface.co/spaces/notaneimu/pth2onnx-converter",target:"_blank",rel:"noopener noreferrer"}," Convert from pth ")],-1)),M("div",C3,[M("input",{ref_key:"modelInputRef",ref:i,class:"sr-only",type:"file",accept:".onnx,application/octet-stream",disabled:a.value,onChange:d},null,40,E3),M("button",{type:"button",class:"inline-flex w-full items-center justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-stone-900 ring-1 ring-inset ring-stone-300 transition hover:bg-stone-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-400 disabled:cursor-not-allowed disabled:bg-stone-100 disabled:text-stone-400",disabled:a.value,onClick:o}," Choose model file ",8,k3)])]),M("div",z3,[M("span",A3,wt(e.modelStatusLabel),1),M("span",M3,wt(e.modelStatusMeta),1)]),M("div",R3,[M("label",O3,[c[38]||(c[38]=M("span",{class:"text-xs font-medium text-stone-600"},"Auto loading",-1)),Ze(M("input",{"onUpdate:modelValue":c[4]||(c[4]=m=>n.controls.autoLoadModel=m),type:"checkbox",disabled:a.value,class:"h-4 w-4 accent-stone-700 disabled:opacity-60"},null,8,N3),[[Or,n.controls.autoLoadModel]])]),M("button",{type:"button",class:"inline-flex w-full items-center justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-stone-900 ring-1 ring-inset ring-stone-300 transition hover:bg-stone-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-400 disabled:cursor-not-allowed disabled:bg-stone-100 disabled:text-stone-400",disabled:a.value,onClick:c[5]||(c[5]=m=>r("reload"))}," Load model ",8,B3)]),M("label",D3,[c[39]||(c[39]=M("span",{class:"text-xs font-medium text-stone-600"},"Image",-1)),M("div",P3,[M("input",{ref_key:"imageInputRef",ref:s,class:"sr-only",type:"file",accept:"image/*",disabled:a.value,onChange:p},null,40,U3),M("button",{type:"button",class:"inline-flex w-full items-center justify-center rounded-md bg-white px-3 py-2 text-sm font-semibold text-stone-900 ring-1 ring-inset ring-stone-300 transition hover:bg-stone-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-400 disabled:cursor-not-allowed disabled:bg-stone-100 disabled:text-stone-400",disabled:a.value,onClick:l}," Choose image file ",8,L3),M("p",W3,wt(e.imageStatusText),1)])]),M("label",q3,[c[42]||(c[42]=M("span",{class:"text-xs font-medium text-stone-600"},"Execution provider",-1)),M("div",G3,[Ze(M("select",{"onUpdate:modelValue":c[6]||(c[6]=m=>n.controls.provider=m),disabled:a.value,class:"w-full appearance-none rounded-lg border border-stone-300 bg-white px-3 py-2 pr-11 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},[...c[40]||(c[40]=[M("option",{value:"webgpu"},"WebGPU (default)",-1),M("option",{value:"wasm"},"WASM",-1),M("option",{value:"webgl"},"WebGL",-1)])],8,V3),[[Tn,n.controls.provider]]),c[41]||(c[41]=M("svg",{class:"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-500",viewBox:"0 0 16 16",fill:"none","aria-hidden":"true"},[M("path",{d:"M4 6l4 4 4-4",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"1.5"})],-1))])]),M("label",F3,[c[43]||(c[43]=M("span",{class:"text-xs font-medium text-stone-600"},"Tile size",-1)),Ze(M("input",{"onUpdate:modelValue":c[7]||(c[7]=m=>n.controls.tileSize=m),type:"number",min:"1",step:"1",placeholder:"(empty disables tiling)",disabled:a.value,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,H3),[[nn,n.controls.tileSize]])]),M("label",j3,[c[44]||(c[44]=M("span",{class:"text-xs font-medium text-stone-600"},"Tile blending",-1)),Ze(M("input",{"onUpdate:modelValue":c[8]||(c[8]=m=>n.controls.tileBlendingEnabled=m),type:"checkbox",disabled:a.value,class:"h-4 w-4 accent-stone-700 disabled:opacity-60"},null,8,K3),[[Or,n.controls.tileBlendingEnabled]])]),M("label",X3,[c[45]||(c[45]=M("span",{class:"text-xs font-medium text-stone-600"},"Initial width",-1)),Ze(M("input",{"onUpdate:modelValue":c[9]||(c[9]=m=>n.controls.width=m),type:"number",min:"1",step:"1",placeholder:"auto",disabled:a.value,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,Y3),[[nn,n.controls.width]])]),M("label",Z3,[c[46]||(c[46]=M("span",{class:"text-xs font-medium text-stone-600"},"Initial height",-1)),Ze(M("input",{"onUpdate:modelValue":c[10]||(c[10]=m=>n.controls.height=m),type:"number",min:"1",step:"1",placeholder:"auto",disabled:a.value,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,Q3),[[nn,n.controls.height]])]),M("label",J3,[c[49]||(c[49]=M("span",{class:"text-xs font-medium text-stone-600"},"Pre resize algorithm",-1)),M("div",eS,[Ze(M("select",{"onUpdate:modelValue":c[11]||(c[11]=m=>n.controls.preResizeAlgorithm=m),disabled:a.value,class:"w-full appearance-none rounded-lg border border-stone-300 bg-white px-3 py-2 pr-11 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},[...c[47]||(c[47]=[M("option",{value:"lanczos"},"Lanczos",-1),M("option",{value:"area"},"Area",-1),M("option",{value:"bicubic"},"Bicubic",-1),M("option",{value:"nearest"},"Nearest neighbor",-1)])],8,tS),[[Tn,n.controls.preResizeAlgorithm]]),c[48]||(c[48]=M("svg",{class:"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-500",viewBox:"0 0 16 16",fill:"none","aria-hidden":"true"},[M("path",{d:"M4 6l4 4 4-4",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"1.5"})],-1))]),c[50]||(c[50]=M("p",{class:"m-0 text-xs italic leading-snug text-stone-500"},"Used when resizing the source image to the model input size.",-1))]),M("label",nS,[c[51]||(c[51]=M("span",{class:"text-xs font-medium text-stone-600"},"Final scale",-1)),Ze(M("input",{"onUpdate:modelValue":c[12]||(c[12]=m=>n.controls.outputScale=m),type:"number",min:"1",max:"4",step:"0.1",placeholder:"off (keep model output)",disabled:a.value,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,rS),[[nn,n.controls.outputScale]]),c[52]||(c[52]=M("p",{class:"m-0 text-xs italic leading-snug text-stone-500"},"Optional downscale after inference. Uses the input size as 1x and caps at the model output.",-1))]),M("label",iS,[c[55]||(c[55]=M("span",{class:"text-xs font-medium text-stone-600"},"Post resize algorithm",-1)),M("div",sS,[Ze(M("select",{"onUpdate:modelValue":c[13]||(c[13]=m=>n.controls.outputScaleAlgorithm=m),disabled:a.value,class:"w-full appearance-none rounded-lg border border-stone-300 bg-white px-3 py-2 pr-11 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},[...c[53]||(c[53]=[M("option",{value:"lanczos"},"Lanczos",-1),M("option",{value:"area"},"Area",-1),M("option",{value:"bicubic"},"Bicubic",-1),M("option",{value:"nearest"},"Nearest neighbor",-1)])],8,aS),[[Tn,n.controls.outputScaleAlgorithm]]),c[54]||(c[54]=M("svg",{class:"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-500",viewBox:"0 0 16 16",fill:"none","aria-hidden":"true"},[M("path",{d:"M4 6l4 4 4-4",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"1.5"})],-1))]),c[56]||(c[56]=M("p",{class:"m-0 text-xs italic leading-snug text-stone-500"},"Used only for the optional output resize after inference.",-1))]),M("label",oS,[c[57]||(c[57]=M("span",{class:"text-xs font-medium text-stone-600"},"Compare with classic upscale",-1)),Ze(M("input",{"onUpdate:modelValue":c[14]||(c[14]=m=>n.controls.compareWithClassicUpscale=m),type:"checkbox",disabled:a.value,class:"h-4 w-4 accent-stone-700 disabled:opacity-60"},null,8,lS),[[Or,n.controls.compareWithClassicUpscale]])]),M("label",uS,[c[60]||(c[60]=M("span",{class:"text-xs font-medium text-stone-600"},"Classic compare algorithm",-1)),M("div",dS,[Ze(M("select",{"onUpdate:modelValue":c[15]||(c[15]=m=>n.controls.classicCompareAlgorithm=m),disabled:a.value||!n.controls.compareWithClassicUpscale,class:"w-full appearance-none rounded-lg border border-stone-300 bg-white px-3 py-2 pr-11 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},[...c[58]||(c[58]=[M("option",{value:"lanczos"},"Lanczos",-1),M("option",{value:"area"},"Area",-1),M("option",{value:"bicubic"},"Bicubic",-1),M("option",{value:"nearest"},"Nearest neighbor",-1)])],8,cS),[[Tn,n.controls.classicCompareAlgorithm]]),c[59]||(c[59]=M("svg",{class:"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-500",viewBox:"0 0 16 16",fill:"none","aria-hidden":"true"},[M("path",{d:"M4 6l4 4 4-4",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"1.5"})],-1))]),c[61]||(c[61]=M("p",{class:"m-0 text-xs italic leading-snug text-stone-500"},"Builds a non-AI baseline upscaled to the same final resolution as the model result.",-1))]),M("details",pS,[c[93]||(c[93]=M("summary",{class:"flex cursor-pointer list-none items-center justify-between py-1 text-sm font-semibold text-stone-700"},[M("span",null,"Additional params"),M("span",{class:"text-xs text-stone-500 group-open:hidden"},"▶"),M("span",{class:"hidden text-xs text-stone-500 group-open:inline"},"▼")],-1)),M("div",fS,[M("label",hS,[c[62]||(c[62]=M("span",{class:"text-xs font-medium text-stone-600"},"Color correct final image",-1)),Ze(M("input",{"onUpdate:modelValue":c[16]||(c[16]=m=>n.controls.colorCorrectionEnabled=m),type:"checkbox",disabled:a.value,class:"h-4 w-4 accent-stone-700 disabled:opacity-60"},null,8,mS),[[Or,n.controls.colorCorrectionEnabled]])]),M("label",gS,[c[63]||(c[63]=M("span",{class:"text-xs font-medium text-stone-600"},"Color correction strength (%)",-1)),Ze(M("input",{"onUpdate:modelValue":c[17]||(c[17]=m=>n.controls.colorCorrectionStrength=m),type:"number",min:"0",max:"100",step:"5",placeholder:"100",disabled:a.value||!n.controls.colorCorrectionEnabled,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,yS),[[nn,n.controls.colorCorrectionStrength]]),c[64]||(c[64]=M("p",{class:"m-0 text-xs italic leading-snug text-stone-500"},"Matches RGB color balance and black/white levels back toward the source image.",-1))]),M("label",bS,[c[65]||(c[65]=M("span",{class:"text-xs font-medium text-stone-600"},"Black/white clip (%)",-1)),Ze(M("input",{"onUpdate:modelValue":c[18]||(c[18]=m=>n.controls.colorCorrectionClip=m),type:"number",min:"0",max:"10",step:"0.1",placeholder:"0.6",disabled:a.value||!n.controls.colorCorrectionEnabled,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,wS),[[nn,n.controls.colorCorrectionClip]]),c[66]||(c[66]=M("p",{class:"m-0 text-xs italic leading-snug text-stone-500"},"Ignores tiny outliers when matching source black and white points.",-1))]),M("label",_S,[c[67]||(c[67]=M("span",{class:"text-xs font-medium text-stone-600"},"Add film grain",-1)),Ze(M("input",{"onUpdate:modelValue":c[19]||(c[19]=m=>n.controls.filmGrainEnabled=m),type:"checkbox",disabled:a.value,class:"h-4 w-4 accent-stone-700 disabled:opacity-60"},null,8,vS),[[Or,n.controls.filmGrainEnabled]])]),M("label",xS,[c[68]||(c[68]=M("span",{class:"text-xs font-medium text-stone-600"},"Film grain amount (%)",-1)),Ze(M("input",{"onUpdate:modelValue":c[20]||(c[20]=m=>n.controls.filmGrainAmount=m),type:"number",min:"0",max:"100",step:"1",placeholder:"6",disabled:a.value||!n.controls.filmGrainEnabled,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,$S),[[nn,n.controls.filmGrainAmount]])]),M("label",SS,[c[69]||(c[69]=M("span",{class:"text-xs font-medium text-stone-600"},"Film grain size (px)",-1)),Ze(M("input",{"onUpdate:modelValue":c[21]||(c[21]=m=>n.controls.filmGrainSize=m),type:"number",min:"0.5",max:"8",step:"0.1",placeholder:"1.2",disabled:a.value||!n.controls.filmGrainEnabled,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,TS),[[nn,n.controls.filmGrainSize]])]),M("label",IS,[c[70]||(c[70]=M("span",{class:"text-xs font-medium text-stone-600"},"Monochrome grain",-1)),Ze(M("input",{"onUpdate:modelValue":c[22]||(c[22]=m=>n.controls.filmGrainMonochrome=m),type:"checkbox",disabled:a.value||!n.controls.filmGrainEnabled,class:"h-4 w-4 accent-stone-700 disabled:opacity-60"},null,8,CS),[[Or,n.controls.filmGrainMonochrome]])]),M("label",ES,[c[71]||(c[71]=M("span",{class:"text-xs font-medium text-stone-600"},"Seam blend width (input px)",-1)),Ze(M("input",{"onUpdate:modelValue":c[23]||(c[23]=m=>n.controls.seamBlendWidth=m),type:"number",min:"0",step:"1",placeholder:"auto",disabled:a.value||!n.controls.tileBlendingEnabled,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,kS),[[nn,n.controls.seamBlendWidth]]),c[72]||(c[72]=M("p",{class:"m-0 text-xs italic leading-snug text-stone-500"},"Blank uses auto seam width. Set 0 to disable seam feather blending.",-1))]),M("label",zS,[c[73]||(c[73]=M("span",{class:"text-xs font-medium text-stone-600"},"Seam correction strength (%)",-1)),Ze(M("input",{"onUpdate:modelValue":c[24]||(c[24]=m=>n.controls.seamCorrectionStrength=m),type:"number",min:"0",max:"300",step:"5",placeholder:"100",disabled:a.value||!n.controls.tileBlendingEnabled,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,AS),[[nn,n.controls.seamCorrectionStrength]]),c[74]||(c[74]=M("p",{class:"m-0 text-xs italic leading-snug text-stone-500"},"0 disables exposure matching. 100 is default, higher values apply stronger correction.",-1))]),M("label",MS,[c[77]||(c[77]=M("span",{class:"text-xs font-medium text-stone-600"},"Graph optimization",-1)),M("div",RS,[Ze(M("select",{"onUpdate:modelValue":c[25]||(c[25]=m=>n.controls.optLevel=m),disabled:a.value,class:"w-full appearance-none rounded-lg border border-stone-300 bg-white px-3 py-2 pr-11 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},[...c[75]||(c[75]=[M("option",{value:"all"},"All (default)",-1),M("option",{value:"extended"},"Extended",-1),M("option",{value:"basic"},"Basic",-1),M("option",{value:"disabled"},"Disabled",-1)])],8,OS),[[Tn,n.controls.optLevel]]),c[76]||(c[76]=M("svg",{class:"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-500",viewBox:"0 0 16 16",fill:"none","aria-hidden":"true"},[M("path",{d:"M4 6l4 4 4-4",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"1.5"})],-1))])]),M("label",NS,[c[80]||(c[80]=M("span",{class:"text-xs font-medium text-stone-600"},"WebGPU layout",-1)),M("div",BS,[Ze(M("select",{"onUpdate:modelValue":c[26]||(c[26]=m=>n.controls.webgpuLayout=m),disabled:a.value,class:"w-full appearance-none rounded-lg border border-stone-300 bg-white px-3 py-2 pr-11 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},[...c[78]||(c[78]=[M("option",{value:"NCHW"},"NCHW (default)",-1),M("option",{value:"NHWC"},"NHWC",-1)])],8,DS),[[Tn,n.controls.webgpuLayout]]),c[79]||(c[79]=M("svg",{class:"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-500",viewBox:"0 0 16 16",fill:"none","aria-hidden":"true"},[M("path",{d:"M4 6l4 4 4-4",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"1.5"})],-1))])]),M("label",PS,[c[83]||(c[83]=M("span",{class:"text-xs font-medium text-stone-600"},"WebGPU validation",-1)),M("div",US,[Ze(M("select",{"onUpdate:modelValue":c[27]||(c[27]=m=>n.controls.webgpuValidation=m),disabled:a.value,class:"w-full appearance-none rounded-lg border border-stone-300 bg-white px-3 py-2 pr-11 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},[...c[81]||(c[81]=[M("option",{value:"basic"},"Basic (default)",-1),M("option",{value:"full"},"Full",-1),M("option",{value:"wgpuOnly"},"WebGPU-only",-1),M("option",{value:"disabled"},"Disabled",-1)])],8,LS),[[Tn,n.controls.webgpuValidation]]),c[82]||(c[82]=M("svg",{class:"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-500",viewBox:"0 0 16 16",fill:"none","aria-hidden":"true"},[M("path",{d:"M4 6l4 4 4-4",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"1.5"})],-1))])]),M("label",WS,[c[86]||(c[86]=M("span",{class:"text-xs font-medium text-stone-600"},"Input layout",-1)),M("div",qS,[Ze(M("select",{"onUpdate:modelValue":c[28]||(c[28]=m=>n.controls.layout=m),disabled:a.value,class:"w-full appearance-none rounded-lg border border-stone-300 bg-white px-3 py-2 pr-11 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},[...c[84]||(c[84]=[M("option",{value:"nchw"},"NCHW",-1),M("option",{value:"nhwc"},"NHWC",-1)])],8,GS),[[Tn,n.controls.layout]]),c[85]||(c[85]=M("svg",{class:"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-500",viewBox:"0 0 16 16",fill:"none","aria-hidden":"true"},[M("path",{d:"M4 6l4 4 4-4",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"1.5"})],-1))])]),M("label",VS,[c[89]||(c[89]=M("span",{class:"text-xs font-medium text-stone-600"},"Channel order",-1)),M("div",FS,[Ze(M("select",{"onUpdate:modelValue":c[29]||(c[29]=m=>n.controls.channelOrder=m),disabled:a.value,class:"w-full appearance-none rounded-lg border border-stone-300 bg-white px-3 py-2 pr-11 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},[...c[87]||(c[87]=[M("option",{value:"rgb"},"RGB",-1),M("option",{value:"bgr"},"BGR",-1)])],8,HS),[[Tn,n.controls.channelOrder]]),c[88]||(c[88]=M("svg",{class:"pointer-events-none absolute right-3 top-1/2 h-4 w-4 -translate-y-1/2 text-stone-500",viewBox:"0 0 16 16",fill:"none","aria-hidden":"true"},[M("path",{d:"M4 6l4 4 4-4",stroke:"currentColor","stroke-linecap":"round","stroke-linejoin":"round","stroke-width":"1.5"})],-1))])]),M("label",jS,[c[90]||(c[90]=M("span",{class:"text-xs font-medium text-stone-600"},"Scale pixels to 0..1",-1)),Ze(M("input",{"onUpdate:modelValue":c[30]||(c[30]=m=>n.controls.normalize=m),type:"checkbox",disabled:a.value,class:"h-4 w-4 accent-stone-700 disabled:opacity-60"},null,8,KS),[[Or,n.controls.normalize]])]),M("label",XS,[c[91]||(c[91]=M("span",{class:"text-xs font-medium text-stone-600"},"Mean (comma separated)",-1)),Ze(M("input",{"onUpdate:modelValue":c[31]||(c[31]=m=>n.controls.mean=m),type:"text",disabled:a.value,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,YS),[[nn,n.controls.mean]])]),M("label",ZS,[c[92]||(c[92]=M("span",{class:"text-xs font-medium text-stone-600"},"Std (comma separated)",-1)),Ze(M("input",{"onUpdate:modelValue":c[32]||(c[32]=m=>n.controls.std=m),type:"text",disabled:a.value,class:"w-full rounded-lg border border-stone-300 bg-white px-3 py-2 text-sm text-stone-800 outline-none transition focus:border-stone-500 disabled:cursor-not-allowed disabled:opacity-60"},null,8,QS),[[nn,n.controls.std]])])])])])])]))}}),eT={class:"rounded-xl border border-stone-300 bg-white p-4 shadow-sm"},tT={class:"flex items-center gap-2"},nT={class:"ml-1 text-xs text-stone-500"},rT={class:"pt-3"},iT={id:"logOutput",class:"max-h-52 overflow-auto rounded-lg border border-stone-200 bg-stone-50 p-3 text-xs leading-6 whitespace-pre-wrap break-words text-stone-700"},sT=eo({__name:"LogPanel",props:{logOutput:{}},emits:["copy","clear"],setup(e,{emit:t}){const n=t,r=bt(!0);return(i,s)=>(kn(),zn("section",eT,[M("div",{class:"-m-4 flex cursor-pointer items-center justify-between rounded-xl p-4 transition hover:bg-stone-50",onClick:s[2]||(s[2]=a=>r.value=!r.value)},[s[3]||(s[3]=M("h2",{class:"m-0 text-sm font-semibold text-stone-700"},"Log",-1)),M("div",tT,[M("button",{type:"button",class:"inline-flex items-center justify-center rounded-md bg-white px-2.5 py-1.5 text-xs font-semibold text-stone-900 ring-1 ring-inset ring-stone-300 transition hover:bg-stone-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-400",onClick:s[0]||(s[0]=up(a=>n("copy"),["stop"]))}," Copy "),M("button",{type:"button",class:"inline-flex items-center justify-center rounded-md bg-white px-2.5 py-1.5 text-xs font-semibold text-stone-700 ring-1 ring-inset ring-stone-200 transition hover:bg-stone-50 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-400",onClick:s[1]||(s[1]=up(a=>n("clear"),["stop"]))}," Clear "),M("span",nT,wt(r.value?"▶":"▼"),1)])]),Ze(M("div",rT,[M("pre",iT,wt(e.logOutput),1)],512),[[I1,!r.value]])]))}}),aT="/assets/ort-wasm-simd-threaded-Cl9nwGIC.mjs",oT="/assets/ort-wasm-simd-threaded-yYL6DyeX.wasm",lT="/assets/ort-wasm-simd-threaded.jsep-DbX7tCCQ.mjs",uT="/assets/ort-wasm-simd-threaded.jsep-CTuMJ5w1.wasm",dT=[{id:"1x-itf-skindiffdetail-lite-v1",name:"1x ITF SkinDiffDetail Lite v1",file:"1x-ITF-SkinDiffDetail-Lite-v1.onnx",scale:1,size:20205357},{id:"1x-kim2091-dejpeg-v0",name:"1x Kim2091 DeJpeg v0",file:"1x-Kim2091-DeJpeg-v0.onnx",scale:1,size:9234496,recommended:!0},{id:"1x-restoration-jpg-esrgan-jpg40-60-vyls",name:"1x Restoration JPG ESRGAN JPG 40-60 Vyls",file:"1x-Restoration-jpg-ESRGAN_64nf_23nb-jpg40_60-vyls.onnx",scale:1,size:66870328},{id:"1x-restoration-jpg-esrgan-jpg60-80-vyls",name:"1x Restoration JPG ESRGAN JPG 60-80 Vyls",file:"1x-Restoration-jpg-ESRGAN_64nf_23nb-jpg60_80-vyls.onnx",scale:1,size:66870328},{id:"1x-superscale-rplksr-s",name:"1x SuperScale RPLKSR S",file:"1x-SuperScale_RPLKSR_S.onnx",scale:1,size:954e4},{id:"1x-superscale-span",name:"1x SuperScale SPAN",file:"1x-SuperScale_SPAN.onnx",scale:1,size:164e4},{id:"2x-realesrgan-x2plus",name:"2x Real-ESRGAN x2plus",file:"2x-realesrgan-x2plus.onnx",scale:2,size:67191666},{id:"2x-nomosuni-compact-otf-medium",name:"2x NomosUni Compact OTF Medium",file:"2xNomosUni_compact_otf_medium.onnx",scale:2,size:2411189},{id:"4x-clearreality-v1-fp32",name:"4x ClearReality V1 FP32",file:"4x-ClearRealityV1-fp32-opset17.onnx",scale:4,size:1718947,recommended:!0},{id:"4x-clearreality-v1-soft-fp32",name:"4x ClearReality V1 Soft FP32",file:"4x-ClearRealityV1_Soft-fp32-opset17.onnx",scale:4,size:1718947},{id:"4x-ultrasharp-fp32",name:"4x UltraSharp FP32",file:"4x-UltraSharp-fp32-opset17.onnx",scale:4,size:67002795},{id:"4x-ultrasharp-v2-fp32",name:"4x UltraSharp V2 FP32",file:"4x-UltraSharpV2_fp32_op17.onnx",scale:4,size:51800517},{id:"upscayl-digital-art-4x",name:"4x Upscayl Digital Art",file:"upscayl-digital-art-4x.onnx",scale:4,size:17970916},{id:"upscayl-high-fidelity-4x",name:"4x Upscayl High Fidelity",file:"upscayl-high-fidelity-4x.onnx",scale:4,size:67167470},{id:"upscayl-lite-4x",name:"4x Upscayl Lite",file:"upscayl-lite-4x.onnx",scale:4,size:4868758},{id:"upscayl-remacri-4x",name:"4x Upscayl Remacri",file:"upscayl-remacri-4x.onnx",scale:4,size:67167470},{id:"upscayl-standard-4x",name:"4x Upscayl Standard",file:"upscayl-standard-4x.onnx",scale:4,size:67167470},{id:"upscayl-ultramix-balanced-4x",name:"4x Upscayl Ultramix Balanced",file:"upscayl-ultramix-balanced-4x.onnx",scale:4,size:67167470},{id:"upscayl-ultrasharp-4x",name:"4x Upscayl UltraSharp",file:"upscayl-ultrasharp-4x.onnx",scale:4,size:67167470},{id:"4x-lsdir",name:"4x LSDIR",file:"4xLSDIR.onnx",scale:4,size:67167471},{id:"4x-lsdir-compact-v2",name:"4x LSDIR Compact V2",file:"4xLSDIRCompactv2.onnx",scale:4,size:2494279},{id:"4x-nomos2-hq-dat2-fp32",name:"4x Nomos2 HQ DAT2 FP32",file:"4xNomos2_hq_dat2_fp32.onnx",scale:4,size:52855546},{id:"4x-nomos8k-dat",name:"4x Nomos8k DAT",file:"4xNomos8kDAT.onnx",scale:4,size:89983270,recommended:!0},{id:"4x-nomoswebphoto-esrgan-fp32",name:"4x NomosWebPhoto ESRGAN FP32",file:"4xNomosWebPhoto_esrgan_fp32_opset17.onnx",scale:4,size:67003044},{id:"4x-purephoto-realplskr",name:"4x PurePhoto RealPLSKR",file:"4xPurePhoto-RealPLSKR.onnx",scale:4,size:29943222,recommended:!0},{id:"4x-purephoto-span",name:"4x PurePhoto SPAN",file:"4xPurePhoto-Span.onnx",scale:4,size:1721421,recommended:!0},{id:"4x-spankendata-fp32",name:"4x SPAN Kendata FP32",file:"4xSPANkendata_fp32.onnx",scale:4,size:1718947},{id:"4x-foolhardy-remacri",name:"4x Foolhardy Remacri",file:"4x_foolhardy_Remacri.onnx",scale:4,size:67167471},{id:"4x-nmkd-siax-200k",name:"4x NMKD-Siax 200k",file:"4x_NMKD-Siax_200k.onnx",scale:4,size:67167471},{id:"realesr-general-wdn-x4v3",name:"RealESR-General WDN x4v3",file:"realesr-general-wdn-x4v3.onnx",scale:4,size:4868759,recommended:!0},{id:"realesrgan-x4plus",name:"RealESRGAN x4plus",file:"RealESRGAN_x4plus.onnx",scale:4,size:67167471},{id:"epoch895-omnisr",name:"epoch895 OmniSR",file:"epoch895_OmniSR.onnx",scale:4,size:5557453},{id:"1x-refocus-cleanly",name:"1x ReFocus Cleanly",file:"1x-ReFocus-Cleanly.onnx",scale:1,size:66899149},{id:"4x-fuzzybox",name:"4x FuzzyBox",file:"4x_FuzzyBox.onnx",scale:4,size:67213722},{id:"4x-fsdedither",name:"4x FSDedither",file:"4xFSDedither.onnx",scale:4,size:67213722},{id:"1x-desharpen",name:"1x DeSharpen",file:"1x_DeSharpen.onnx",scale:1,size:66899149},{id:"4x-uniscalev2-soft",name:"4x UniScaleV2 Soft",file:"4x-UniScaleV2_Soft.onnx",scale:4,size:66899149},{id:"4x-uniscalev2-sharp",name:"4x UniScaleV2 Sharp",file:"4x-UniScaleV2_Sharp.onnx",scale:4,size:66899149},{id:"4x-uniscalev2-moderate",name:"4x UniScaleV2 Moderate",file:"4x-UniScaleV2_Moderate.onnx",scale:4,size:66899149},{id:"4x-nomoswebphoto-realplksr",name:"4x NomosWebPhoto RealPLKSR",file:"4xNomosWebPhoto_RealPLKSR.onnx",scale:4,size:29989274},{id:"4x-realwebphoto-v4-dat2-fp32-opset17",name:"4x RealWebPhoto v4 DAT2 FP32 opset17",file:"4xRealWebPhoto_v4_dat2_fp32_opset17.onnx",scale:4,size:48758784},{id:"002-lightweightsr-div2k-s64w8-swinir-s-x4",name:"002 lightweightSR DIV2K s64w8 SwinIR-S x4",file:"002_lightweightSR_DIV2K_s64w8_SwinIR-S_x4.onnx",scale:4,size:8283750}],cT={models:dT},pT="onnx-web-inspector",Lr="assets";function fT(){let e=null;async function t(){if(!("indexedDB"in window))throw new Error("IndexedDB is not available in this browser.");return e||(e=new Promise((s,a)=>{const o=window.indexedDB.open(pT,1);o.onupgradeneeded=()=>{o.result.createObjectStore(Lr,{keyPath:"key"})},o.onsuccess=()=>s(o.result),o.onerror=()=>a(o.error||new Error("Failed to open IndexedDB."))})),e}async function n(s,a){const o=await t();await new Promise((l,d)=>{const p=o.transaction(Lr,"readwrite");p.oncomplete=()=>l(),p.onerror=()=>d(p.error||new Error("Failed to save asset.")),p.objectStore(Lr).put({key:s,name:a.name,size:a.size,type:a.type,blob:a.blob,savedAt:Date.now()})})}async function r(s){const a=await t(),o=await new Promise((d,p)=>{const c=a.transaction(Lr,"readonly").objectStore(Lr).get(s);c.onsuccess=()=>d(c.result||null),c.onerror=()=>p(c.error||new Error("Failed to load saved asset."))}),l=o==null?void 0:o.blob;return l instanceof Blob?{name:typeof(o==null?void 0:o.name)=="string"?o.name:`${s}.bin`,size:typeof(o==null?void 0:o.size)=="number"?o.size:l.size,type:typeof(o==null?void 0:o.type)=="string"?o.type:l.type,blob:l,saved:!0}:null}async function i(s){const a=await t();await new Promise((o,l)=>{const d=a.transaction(Lr,"readwrite");d.oncomplete=()=>o(),d.onerror=()=>l(d.error||new Error("Failed to delete asset.")),d.objectStore(Lr).delete(s)})}return{persistAsset:n,loadPersistedAsset:r,deletePersistedAsset:i}}const hT="https://huggingface.co/huggingworld/onnx-image-models/resolve/main/",Fl="onnx-web-selected-model",sg="onnx-web-auto-load-model",ag="onnx-web-control-settings",mT=10,gT=24,yT=.1,bT=4,wT=24,_T=.75,vT=18,xT=.75,$T=64,a_="100",o_="100",l_="0.6",u_="6",d_="1.2",ST=300,TT=10,IT=8,og=180,lg=["lanczos","area","bicubic","nearest"],CT={lanczos:"Lanczos",area:"Area",bicubic:"Bicubic",nearest:"Nearest neighbor"},st={selectedPredefinedModelId:"",autoLoadModel:!0,provider:"webgpu",tileSize:"200",tileBlendingEnabled:!1,seamBlendWidth:"",seamCorrectionStrength:a_,width:"600",height:"",outputScale:"",preResizeAlgorithm:"lanczos",outputScaleAlgorithm:"lanczos",colorCorrectionEnabled:!1,colorCorrectionStrength:o_,colorCorrectionClip:l_,filmGrainEnabled:!1,filmGrainAmount:u_,filmGrainSize:d_,filmGrainMonochrome:!0,compareWithClassicUpscale:!1,classicCompareAlgorithm:"lanczos",optLevel:"all",webgpuLayout:"NCHW",webgpuValidation:"basic",layout:"nchw",channelOrder:"rgb",normalize:!0,mean:"0,0,0",std:"1,1,1"};function ug(e,t){if(t<=1)return 0;const n=(t-1)*2,r=(e%n+n)%n;return r0&&v0&&__!==0);for(let _=0;_0&&v0&&_=1||y.data[I+3]===0){y.data[I]=E,y.data[I+1]=R,y.data[I+2]=O,y.data[I+3]=255;continue}y.data[I]=mi(y.data[I]*(1-S)+E*S),y.data[I+1]=mi(y.data[I+1]*(1-S)+R*S),y.data[I+2]=mi(y.data[I+2]*(1-S)+O*S),y.data[I+3]=255}return t.putImageData(y,o,l),w}function UT(e){let t=Math.round(e.sourceX),n=Math.round(e.sourceY),r=Math.floor(e.sourceWidth),i=Math.floor(e.sourceHeight),s=Math.round(e.destX),a=Math.round(e.destY);if(r<=0||i<=0)return null;if(s<0){const o=-s;s=0,t+=o,r-=o}if(a<0){const o=-a;a=0,n+=o,i-=o}if(t<0){const o=-t;t=0,s+=o,r-=o}if(n<0){const o=-n;n=0,a+=o,i-=o}return r=Math.min(r,e.maxSourceWidth-t,e.maxDestWidth-s),i=Math.min(i,e.maxSourceHeight-n,e.maxDestHeight-a),r<=0||i<=0?null:{sourceX:t,sourceY:n,sourceWidth:r,sourceHeight:i,destX:s,destY:a}}function LT(){const{deletePersistedAsset:e,loadPersistedAsset:t,persistAsset:n}=fT(),r=$i({...st}),i=bt("Idle"),s=bt(""),a=bt("Loading ONNX Runtime..."),o=bt(["[log] Ready."]),l=bt(cT.models??[]),d=ia(null),p=ia(null),f=bt(null),c=bt(null),m=bt(null),y=bt(null),w=bt(null),$=ia(null),_=$i({resolution:"—",size:"—",progress:"—",tiles:"—",speed:"—",totalTime:"—"}),v=ia(null),I=bt(null),S=bt(null),E=bt(null),R=bt(!1),O=bt(!1),x=bt(!1);let U=null,q=null,J=null,se=0,ce=0,L=0,ue=0,ie=!1,te=null,he=null,Q=null,ve=null,Y=null;function P(C,k){const F=new Date().toLocaleTimeString();o.value.push(k?`[${F}] ${C} +${h2(k)}`:`[${F}] ${C}`)}function ee(C,k=""){i.value=C,s.value=k}function ne(){_.resolution="—",_.size="—",_.progress="—",_.tiles="—",_.speed="—",_.totalTime="—"}function Ue(){ce+=1,D(),K(null),ne()}function lt(C){return String(C??"").trim()}function Xe(C,k,F="asset"){return{name:C.name||F,size:C.size,type:C.type||"",saved:k,blob:C}}function Ke(C){if(!(!C||!C.startsWith("blob:")))try{URL.revokeObjectURL(C)}catch{}}function ft(C){Ke(m.value),m.value=C?URL.createObjectURL(C):null}function ut(C){Ke(y.value),y.value=C}function ht(C){var k;Ke(((k=w.value)==null?void 0:k.url)??null),w.value=C}function A(){ve=null}function N(){L+=1,ht(null),Y=null}function D(){q&&(clearTimeout(q),q=null),J&&(clearTimeout(J),J=null)}function H(C,k=0,F=0){if(Q=null,!C||k<=0||F<=0){he=null;return}he={canvas:C,sourceWidth:k,sourceHeight:F}}async function Z(C,k,F){const pe=lt(r.outputScale),j=r.outputScaleAlgorithm;if(Q&&Q.renderCanvas===C&&Q.sourceWidth===k&&Q.sourceHeight===F&&Q.outputScale===pe&&Q.outputScaleAlgorithm===j)return Q.canvas;const ye=m2(r.outputScale),$e=ye?Math.max(1,Math.round(k*ye)):null,Oe=ye?Math.max(1,Math.round(F*ye)):null,Ge=$e?Math.min(C.width,$e):C.width,Ye=Oe?Math.min(C.height,Oe):C.height,Dt=Ge{const k=C.reason;P("Unhandled promise rejection.",{error:k instanceof Error?k.message:String(k),stack:k instanceof Error?k.stack:null})}),window.addEventListener("error",C=>{P("Unhandled error event.",{message:C.message||"Unknown error",filename:C.filename||null,lineno:C.lineno||null,colno:C.colno||null})})}async function vn(){const C=await rg();P("WebGPU startup diagnostics.",C),P("WebGL startup diagnostics.",ig()),r.provider==="webgpu"&&C.adapter!=="available"&&P("WebGPU appears unavailable. WebGPU inference may fail.",{reason:C.error||"requestAdapter returned null"})}function vr(C){if(!C)return;const k=C.dimensions;if(typeof k[1]=="number"&&[1,3,4].includes(k[1])){r.layout="nchw";return}typeof k[3]=="number"&&[1,3,4].includes(k[3])&&(r.layout="nhwc")}async function vt(C){gt(C);const k=C==="webgpu"?[{name:"webgpu",preferredLayout:r.webgpuLayout,validationMode:r.webgpuValidation}]:[C],F=performance.now(),pe=new Uint8Array(await d.value.blob.arrayBuffer()),j=await Lu.create(pe,{executionProviders:k,graphOptimizationLevel:r.optLevel}),ye=Vl(j);if(ye&&ye!==C)throw await j.release(),new Error(`Requested ${C} session, but ONNX Runtime prepared ${ye}. Refusing provider fallback.`);return v.value=j,I.value=C,S.value=O2(j,P),vr(S.value.primaryInput),C==="webgpu"&&Ps(),P("Model session prepared.",{provider:C,requestedProviders:k,executionProvider:Vl(j),graphOptimizationLevel:r.optLevel}),{durationMs:performance.now()-F,providers:k,metadata:S.value}}async function Ct(){if(!d.value){ee("Select a model","error"),P("Model load skipped because no file was selected.");return}await le(),ee("Loading model…","busy"),P("Starting model load.",{name:d.value.name,size:d.value.size,provider:r.provider});try{r.provider==="webgpu"?(P("WebGPU diagnostics.",await rg()),await Zt()):r.provider==="webgl"&&(P("WebGL diagnostics.",ig()),await Zt());const{durationMs:C,providers:k,metadata:F}=await vt(r.provider);ee("Model ready","success"),P("Model session created.",{durationMs:rn(C),requestedProviders:k,inputs:F==null?void 0:F.inputs,outputs:F==null?void 0:F.outputs,executionProvider:Vl(v.value)})}catch(C){await le(),ee("Model load failed","error"),P("Model load failed.",{error:C instanceof Error?C.message:String(C),hint:ng(C,r.provider)})}}async function Dn(){if(O.value){P("Model load already in progress.");return}O.value=!0;try{await Ct()}finally{O.value=!1}}async function ni(C){if(O.value){P("Model load already in progress.");return}O.value=!0;try{await oe(),await e("model").catch(ye=>{P("Failed to delete saved model from storage.",{error:ye instanceof Error?ye.message:String(ye)})}),ee("Loading model…","busy"),P("Fetching model from server.",{name:C.name,file:C.file,provider:r.provider});const k=`${hT}${encodeURIComponent(C.file)}`,F=await fetch(k);if(!F.ok)throw new Error(`Failed to fetch model: HTTP ${F.status}`);const pe=new Blob([await F.arrayBuffer()]),j=new File([pe],C.file,{type:"application/octet-stream"});if(d.value={...Xe(j,!1,C.file),predefined:!0,modelId:C.id,scale:C.scale},!r.autoLoadModel){ee("Model selected","success"),P("Model selected. Use Load model to prepare it.");return}await Ct()}catch(k){await le(),d.value=null,ee("Model load failed","error"),P("Model load failed.",{error:k instanceof Error?k.message:String(k),hint:ng(k,r.provider)})}finally{O.value=!1}}async function Ds(){const C=r.selectedPredefinedModelId;if(!C){X(null);return}const k=l.value.find(F=>F.id===C);k&&(X(C),P("Selected model from the list.",{id:k.id,name:k.name,file:k.file,scale:k.scale}),await ni(k))}async function oo(C){if(C){await oe(),r.selectedPredefinedModelId="",X(null),d.value=Xe(C,!1,C.name),P("Selected ONNX model file.",{name:C.name,size:C.size,type:C.type||"unknown"});try{await n("model",d.value),d.value.saved=!0}catch(k){P("Browser storage save failed. The file still works for this session.",{key:"model",error:k instanceof Error?k.message:String(k)})}if(r.autoLoadModel){await Dn();return}ee("Model selected","success"),P("Model selected. Use Load model to prepare it.")}}async function lo(C){if(C){p.value=Xe(C,!1,C.name),f.value=null,A(),ae(),K(null),ne(),ft(C),Oi(C),xr(),P("Selected image file.",{name:C.name,size:C.size,type:C.type||"unknown"});try{await n("image",p.value),p.value.saved=!0}catch(k){P("Browser storage save failed. The file still works for this session.",{key:"image",error:k instanceof Error?k.message:String(k)})}}}async function uo(){try{const[C,k]=await Promise.all([t("model"),t("image")]);C&&(d.value=C,P("Restored saved ONNX model from browser storage.",{name:C.name,size:C.size})),k&&(p.value=k,f.value=null,A(),ae(),ft(k.blob),Oi(k.blob),xr(),P("Restored saved image from browser storage.",{name:k.name,size:k.size})),C&&r.autoLoadModel?await Dn():C&&ee("Model selected","success")}catch(C){P("Browser storage restore failed. Falling back to manual file selection.",{error:C instanceof Error?C.message:String(C)})}}async function Oi(C){const k=ue+=1;try{const F=await hi(C);if(k!==ue)return;f.value={width:F.naturalWidth||F.width||0,height:F.naturalHeight||F.height||0}}catch(F){if(k!==ue)return;f.value=null,P("Failed to read image dimensions.",{error:F instanceof Error?F.message:String(F)})}}function xr(){if(!p.value){ae();return}U&&clearTimeout(U),U=setTimeout(()=>{U=null,co()},150)}async function co(){const C=p.value;if(!C){ae();return}const k=se+=1;try{const F=await hi(C.blob);if(k!==se)return;const pe={width:F.naturalWidth||F.width||0,height:F.naturalHeight||F.height||0},ye=lt(r.width)===""&<(r.height)===""?{width:pe.width,height:pe.height,fixed:!1}:Km(v.value,F,r,E.value);if(!ye.width||!ye.height){ut(null),c.value=null;return}const $e=ye.width!==pe.width||ye.height!==pe.height;if(c.value={width:ye.width,height:ye.height,sourceWidth:pe.width,sourceHeight:pe.height,resized:$e,fixed:ye.fixed},!$e){ut(null);return}await Zt();const Oe=document.createElement("canvas");Oe.width=pe.width,Oe.height=pe.height;const Ge=Oe.getContext("2d");if(!Ge)throw new Error("2D canvas context is unavailable.");Ge.drawImage(F,0,0,pe.width,pe.height);const Ye=await Ss(Oe,ye.width,ye.height,r.preResizeAlgorithm),Dt=await Wl(Ye);if(k!==se){Ke(Dt);return}ut(Dt)}catch(F){if(k!==se)return;ut(null),c.value=null,P("Failed to build model input preview.",{error:F instanceof Error?F.message:String(F)})}}async function Ni(){var k;const C=await Promise.resolve((k=Be.webgpu)==null?void 0:k.device);return!C||typeof C!="object"?null:C}async function Ps(){if(ie)return;const C=await Ni();!C||typeof C.addEventListener!="function"||(C.addEventListener("uncapturederror",k=>{const F=k.error;P("WebGPU uncaptured error.",{name:(F==null?void 0:F.name)||null,message:(F==null?void 0:F.message)||String(F)})}),C.lost&&typeof C.lost.then=="function"&&C.lost.then(k=>{P("WebGPU device lost.",{reason:(k==null?void 0:k.reason)||null,message:(k==null?void 0:k.message)||null})}),ie=!0)}async function Bi(C){const k=await Ni(),F=k==null?void 0:k.pushErrorScope,pe=k==null?void 0:k.popErrorScope;if(!(I.value==="webgpu"&&k&&typeof F=="function"&&typeof pe=="function"))return await C();F.call(k,"validation"),F.call(k,"out-of-memory"),F.call(k,"internal");try{return await C()}finally{const ye=await pe.call(k),$e=await pe.call(k),Ge=[await pe.call(k),$e,ye].filter(Boolean).map(Ye=>({name:Ye.name||null,message:Ye.message||String(Ye)}));Ge.length&&P("WebGPU error scope captured errors.",{errors:Ge})}}async function dt(C){if(!C)return null;try{const k=await fetch(C);return fi((await k.blob()).size)}catch{return null}}async function ri(C,k,F){const pe=r.colorCorrectionEnabled&&!!p.value,j=r.filmGrainEnabled;let $e=await Z(C,k,F);if((pe||j)&&($e=S2($e)),(pe||j)&&(ee("Finishing…","busy"),_.progress="100% (finishing…)",await Zt()),pe&&p.value){const Oe=await hi(p.value.blob),Ge=await Di(Oe,k,F),Ye=Ge.width===$e.width&&Ge.height===$e.height?Ge:await tg(Ge,$e.width,$e.height,r.outputScaleAlgorithm),Dt=MT(r.colorCorrectionStrength),jt=RT(r.colorCorrectionClip);await I2($e,Ye,{strength:Dt,clipFraction:jt}),P("Applied final-image color correction.",{strengthPercent:Math.round(Dt*100),clipPercent:rn(jt*100),referenceSize:`${Ye.width}x${Ye.height}`})}if(j){const Oe=OT(r.filmGrainAmount),Ge=NT(r.filmGrainSize);await E2($e,{amount:Oe,size:Ge,monochrome:r.filmGrainMonochrome}),P("Applied film grain.",{amountPercent:Math.round(Oe*100),size:Ge,monochrome:r.filmGrainMonochrome})}return{url:await Wl($e),width:$e.width,height:$e.height}}async function Di(C,k,F){const pe=p.value;if(pe&&ve&&ve.blob===pe.blob&&ve.targetWidth===k&&ve.targetHeight===F&&ve.preResizeAlgorithm===r.preResizeAlgorithm)return ve.canvas;const j=C.naturalWidth||C.width||0,ye=C.naturalHeight||C.height||0,$e=document.createElement("canvas");$e.width=j,$e.height=ye;const Oe=$e.getContext("2d");if(!Oe)throw new Error("2D canvas context is unavailable.");Oe.drawImage(C,0,0,j,ye);const Ge=j===k&&ye===F?$e:await Ss($e,k,F,r.preResizeAlgorithm);return pe&&(ve={blob:pe.blob,targetWidth:k,targetHeight:F,preResizeAlgorithm:r.preResizeAlgorithm,canvas:Ge}),Ge}async function $r(C,k,F){if(Y=C,!r.compareWithClassicUpscale||!C||!p.value){ht(null);return}const pe=++L;try{const j=F??await hi(p.value.blob);if(pe!==L)return;const ye=await Di(j,C.targetWidth,C.targetHeight);if(pe!==L)return;const $e=ye.width===C.compareWidth&&ye.height===C.compareHeight?ye:await Ss(ye,C.compareWidth,C.compareHeight,r.classicCompareAlgorithm);if(pe!==L)return;const Oe=await Wl($e);if(pe!==L){Ke(Oe);return}ht({url:Oe,width:$e.width,height:$e.height}),P("Updated classic comparison preview.",{trigger:k,sourceSize:`${C.targetWidth}x${C.targetHeight}`,outputSize:`${$e.width}x${$e.height}`,preResizeAlgorithm:r.preResizeAlgorithm,classicCompareAlgorithm:r.classicCompareAlgorithm})}catch(j){if(pe!==L)return;ht(null),P("Failed to build classic comparison preview.",{trigger:k,error:j instanceof Error?j.message:String(j)})}}function Pi(C){!x.value||!Y||!$.value||R.value||O.value||(J&&(clearTimeout(J),J=null),J=setTimeout(()=>{J=null,$r(Y,C)},og))}async function Us(C){const k=he,F=$.value;if(!k||!F||R.value||O.value)return;const pe=++ce;ee("Updating preview…","busy");const j=await ri(k.canvas,k.sourceWidth,k.sourceHeight);if(pe!==ce){Ke(j.url);return}const ye=$.value;if(!ye){Ke(j.url);return}K({...ye,preview:j}),await $r({targetWidth:k.sourceWidth,targetHeight:k.sourceHeight,compareWidth:j.width,compareHeight:j.height},C),_.resolution=`${j.width}x${j.height}`,_.size=await dt(j.url)||"—",_.progress="100%",ee("Preview updated","success"),P("Updated output preview from cached render.",{trigger:C,outputScale:lt(r.outputScale)||null,outputScaleAlgorithm:r.outputScaleAlgorithm,outputSize:`${j.width}x${j.height}`})}function Ls(C){!x.value||!$.value||!he||R.value||O.value||(q&&(clearTimeout(q),q=null),q=setTimeout(()=>{q=null,Us(C)},og))}function Ws(C){const k=I.value||r.provider;if(!["webgpu","webgl","wasm"].includes(k))return!1;const F=Ul(r.tileSize);if(!F)return!1;const pe=ii(C),j=si();return j?j.width!==j.height||F!==j.width?!1:pe.width>j.width||pe.height>j.height:pe.width>F||pe.height>F}function ii(C){const k=C.naturalWidth||C.width||0,F=C.naturalHeight||C.height||0,pe=Number.parseInt(r.width,10)||0,j=Number.parseInt(r.height,10)||0;return pe>0&&j>0?{width:pe,height:j}:pe>0&&k>0&&F>0?{width:pe,height:Math.max(1,Math.round(F*pe/k))}:j>0&&k>0&&F>0?{width:Math.max(1,Math.round(k*j/F)),height:j}:{width:k,height:F}}function si(){var F;if(E.value)return E.value;const C=(F=S.value)==null?void 0:F.primaryInput;if(!C)return null;const k=typeof C.dimensions[1]=="number"&&[1,3,4].includes(C.dimensions[1])?"nchw":typeof C.dimensions[3]=="number"&&[1,3,4].includes(C.dimensions[3])?"nhwc":r.layout;return e_(C.dimensions,k,fd(C,k))}function qs(C){const k=si();if(!k)return null;const F=ii(C);if(F.width===k.width&&F.height===k.height)return null;const j=Ul(r.tileSize);return j?k.width!==k.height?`Model requires fixed ${k.width}x${k.height} input. Tiling only works for exact-size runs with this model.`:j!==k.width?`Model requires ${k.width}x${k.height} input. Set tile size to ${k.width} or use an exact-size image.`:null:`Model requires ${k.width}x${k.height} input, but current size is ${F.width}x${F.height}.`}async function Gs(C){if(!v.value||!p.value||!d.value)return;const k=d.value,F=p.value,pe=ii(C),j=Ul(r.tileSize);if(!j)throw new Error("Tiled inference is disabled because tile size is not set.");const ye=pe.width,$e=pe.height,Oe=Math.ceil(ye/j),Ge=Math.ceil($e/j),Ye=Oe*Ge,Dt=v.value.inputNames[0],Et=si()?0:kT(j),yt=r.tileBlendingEnabled,Kt=yt?zT(r.seamBlendWidth):null,Ks=yt?AT(r.seamCorrectionStrength):0,Xs=yt?Math.round(Ks*100):0;_.progress="0%",_.tiles=`0/${Ye}`,_.speed="—",_.totalTime="—",P("Using tiled inference to reduce memory usage.",{provider:I.value||r.provider,tileSize:j,tilePadding:Et,paddedSourceSize:`${Oe*j}x${Ge*j}`,tilesWide:Oe,tilesHigh:Ge,seamBlend:{enabled:yt,widthInputPx:Kt,mode:yt?Kt===null?"auto":Kt===0?"off":"manual":"disabled",correctionStrengthPercent:Xs}});const ir=document.createElement("canvas");ir.width=ye,ir.height=$e;const qi=ir.getContext("2d",{willReadFrequently:!0});if(!qi)throw new Error("2D canvas context is unavailable.");qi.drawImage(C,0,0,ye,$e);const Gi=qi.getImageData(0,0,ye,$e).data;let St=null,sr=null,Pn=[],oi=null,Un=1,Ln=1,kt=0;const Sr=performance.now();for(let Mt=0;Mtv.value.run({[Dt]:kr.tensor}));Pn.length||(Pn=Object.keys(Vi));const zr=Vi[Pn[0]];oi||(oi=r_(zr.data));const Xt=await Ym(zr,kr.outputCrop,oi);if(!Xt)throw new Error("Tiled inference output is not renderable as an image tensor.");if(!St&&(Un=Math.max(1,Math.round(Xt.width/Ir)),Ln=Math.max(1,Math.round(Xt.height/Cr)),St=document.createElement("canvas"),St.width=ye*Un,St.height=$e*Ln,sr=St.getContext("2d",{willReadFrequently:!0}),!sr))throw new Error("2D canvas context is unavailable.");if(!sr)throw new Error("2D canvas context is unavailable.");const ui=Et*Un,di=Et*Ln,yo=Math.max(1,Math.min(j,ye-Wn)),bo=Math.max(1,Math.min(j,$e-ar)),or=Math.max(1,Math.min(yo*Un,Xt.width-ui)),Fi=Math.max(1,Math.min(bo*Ln,Xt.height-di));let lr=0,ur=0,Ys=[0,0,0];if(yt){const Zs=Kt===null?null:Kt*Un,_o=Kt===null?null:Kt*Ln;lr=xn>0?dg(ui,Zs):0,ur=Mt>0?dg(di,_o):0;const qn=St?UT({sourceX:ui-lr,sourceY:di-ur,sourceWidth:or+lr,sourceHeight:Fi+ur,destX:Wn*Un-lr,destY:ar*Ln-ur,maxSourceWidth:Xt.width,maxSourceHeight:Xt.height,maxDestWidth:St.width,maxDestHeight:St.height}):null;Ys=sr&&qn?PT({outputCtx:sr,renderedCanvas:Xt.canvas,sourceX:qn.sourceX,sourceY:qn.sourceY,sourceWidth:qn.sourceWidth,sourceHeight:qn.sourceHeight,destX:qn.destX,destY:qn.destY,leftBlend:lr,topBlend:ur,correctionStrength:Ks}):[0,0,0]}else sr.drawImage(Xt.canvas,ui,di,or,Fi,Wn*Un,ar*Ln,or,Fi);P(`Tile ${kt+1}/${Ye} processed.`,{tile:{column:xn+1,row:Mt+1,left:Wn,top:ar,coreSize:`${j}x${j}`,paddedInputSize:`${Ir}x${Cr}`,tilePadding:Et,seamBlend:{enabled:yt,left:lr,top:ur,widthInputPx:Kt,correctionStrengthPercent:Xs,colorOffset:Ys}},inputTensor:{dims:kr.tensor.dims,type:kr.tensor.type,stats:$s(kr.tensor.data)},outputTensor:{dims:zr.dims,type:zr.type,stats:$s(zr.data),byteScale:oi}}),kt+=1;const Hi=performance.now()-Sr,wo=Hi/kt*Ye;_.progress=`${Math.round(kt/Ye*100)}%`,_.tiles=`${kt}/${Ye}`,_.speed=Pl(Hi,kt)||"—",_.totalTime=ktv.value.run({[ye]:j.tensor})),Ge=performance.now()-$e,Ye=Object.keys(Oe),Dt=Ye.map(Kt=>({...v2(Oe[Kt]),name:Kt})),jt=Oe[Ye[0]],Et=await Ym(jt,j.outputCrop);H((Et==null?void 0:Et.canvas)??null,pe.width,pe.height);const yt=Et?await ri(Et.canvas,pe.width,pe.height):null;K({createdAt:new Date().toLocaleString(),modelName:C.name,imageName:k.name,durationMs:rn(Ge)??Ge,inputSummary:j.summary,outputs:Dt,primaryOutputName:Ye[0]||"n/a",preview:yt}),await $r(yt?{targetWidth:pe.width,targetHeight:pe.height,compareWidth:yt.width,compareHeight:yt.height}:null,"run",F),_.resolution=yt?`${yt.width}x${yt.height}`:`${pe.width}x${pe.height}`,_.size=await dt((yt==null?void 0:yt.url)??null)||"—",_.progress="100%",_.tiles="1/1",_.speed=Pl(Ge,1)||"—",_.totalTime=wa(Ge),ee("Run complete","success"),P("Run completed.",{durationMs:rn(Ge),provider:I.value||r.provider,outputCount:Ye.length,outputs:Dt})}async function Fs(){if(O.value){ee("Model loading…","busy"),P("Run skipped because model loading is still in progress.");return}if(!v.value){ee("Load a model","error"),P("Run skipped because no model is ready.");return}if(!p.value){ee("Select an image","error"),P("Run skipped because no image is selected.");return}if(I.value&&I.value!==r.provider){ee("Load model","error"),P("Run skipped because the loaded session provider does not match the selected provider.",{loadedProvider:I.value,selectedProvider:r.provider});return}if(R.value){P("Run already in progress.");return}D(),te=new AbortController,R.value=!0,ee("Running…","busy");try{const C=await hi(p.value.blob),k=qs(C);if(k){ee("Run failed","error"),P("Run blocked.",{error:k});return}await Vs()}catch(C){(C instanceof Error?C.message:String(C))==="AbortError"?(ee("Run cancelled","error"),P("Run cancelled by user.")):(ee("Run failed","error"),P("Run failed.",{error:B2(C)}))}finally{te=null,R.value=!1}}function po(){if(!R.value||!te){P("No inference is currently running.");return}P("Cancelling run..."),te.abort()}async function tn(){var k,F,pe;const C=(F=(k=$.value)==null?void 0:k.preview)==null?void 0:F.url;if(!C){P("No result image available to download.");return}try{const ye=await(await fetch(C)).blob(),$e=URL.createObjectURL(ye),Oe=document.createElement("a"),Ye=(((pe=$.value)==null?void 0:pe.imageName)||"image").replace(/\.[^/.]+$/,"");Oe.href=$e,Oe.download=`${Ye}-upscaled.png`,document.body.appendChild(Oe),Oe.click(),document.body.removeChild(Oe),URL.revokeObjectURL($e),P("Downloaded result image.",{filename:Oe.download})}catch(j){P("Failed to download result image.",{error:j instanceof Error?j.message:String(j)})}}async function Ui(){try{await navigator.clipboard.writeText(o.value.join(` + +`)),P("Copied full log to clipboard.")}catch(C){P("Clipboard copy failed.",{error:C instanceof Error?C.message:String(C)}),ee("Clipboard unavailable","error")}}function Li(){o.value=["[log] Cleared."]}async function rr(){if(R.value||O.value){P("Reset skipped because a run is already in progress.");return}await oe(),d.value=null,p.value=null,f.value=null,ue+=1,A(),ft(null);const C=await Promise.allSettled([e("model"),e("image")]),k=["model","image"];for(const[F,pe]of C.entries())pe.status==="rejected"&&P(`Failed to clear saved ${k[F]}.`,{error:pe.reason instanceof Error?pe.reason.message:String(pe.reason)});mt(),X(null),fe(!0),He(),ee("Idle"),P("Reset all settings, models, and images.")}async function ln(){Le()||xe(),qt(),$t(),await vn(),await uo();const k=r.selectedPredefinedModelId||Se();!d.value&&k&&l.value.find(pe=>pe.id===k)&&(r.selectedPredefinedModelId=k,await Ds()),!v.value&&!d.value&&ee("Idle"),x.value=!0,He(),p.value&&xr()}En(r,()=>{x.value&&He()},{deep:!0,flush:"sync"}),En(()=>r.autoLoadModel,(C,k)=>{C!==k&&(fe(C),x.value&&P(`Auto load model ${C?"enabled":"disabled"}.`))}),En(()=>r.selectedPredefinedModelId,(C,k)=>{!x.value||C===k||Ds()}),En(()=>[r.provider,r.optLevel,r.webgpuLayout,r.webgpuValidation],(C,k)=>{!x.value||!d.value||R.value||O.value||C.join("|")!==k.join("|")&&(r.autoLoadModel?(P("Model configuration changed, reloading model..."),Dn()):(ee("Load model","busy"),P("Model settings changed. Use Load model to apply them.")))}),En(()=>{var C;return[((C=p.value)==null?void 0:C.blob)??null,r.width,r.height,r.preResizeAlgorithm]},()=>{x.value&&(lt(r.width)===""&<(r.height)===""&&(E.value=null),A(),xr(),Pi("inputResize"))}),En(()=>[r.outputScale,r.outputScaleAlgorithm,r.colorCorrectionEnabled,r.colorCorrectionStrength,r.colorCorrectionClip,r.filmGrainEnabled,r.filmGrainAmount,r.filmGrainSize,r.filmGrainMonochrome],(C,k)=>{if(!x.value||C.every((pe,j)=>pe===k[j]))return;const F=C[1]!==k[1]?"outputScaleAlgorithm":C[0]!==k[0]?"outputScale":C[2]!==k[2]?"colorCorrectionToggle":C[3]!==k[3]?"colorCorrectionStrength":C[4]!==k[4]?"colorCorrectionClip":C[5]!==k[5]?"filmGrainToggle":C[6]!==k[6]?"filmGrainAmount":C[7]!==k[7]?"filmGrainSize":"filmGrainMonochrome";Ls(F)}),En(()=>[r.compareWithClassicUpscale,r.classicCompareAlgorithm],(C,k)=>{if(!x.value||C[0]===k[0]&&C[1]===k[1])return;if((C[0]!==k[0]||C[1]!==k[1])&&A(),!C[0]){ht(null);return}const F=C[1]!==k[1]?"classicCompareAlgorithm":"compareToggle";Pi(F)}),no(()=>{var C,k,F;U&&clearTimeout(U),D(),Ke(m.value),Ke(y.value),Ke(((k=(C=$.value)==null?void 0:C.preview)==null?void 0:k.url)??null),Ke(((F=w.value)==null?void 0:F.url)??null)});const Hs=ot(()=>{var C;return((C=d.value)==null?void 0:C.name)||"No model selected"}),fo=ot(()=>{if(!d.value)return"";const C=d.value.predefined?"From list":"From file",k=O.value?"loading":v.value?"ready":"selected";return[C,fi(d.value.size),k].filter(Boolean).join(" • ")}),ho=ot(()=>{if(!p.value)return"No image selected.";const C=f.value?` (${f.value.width}x${f.value.height})`:"";return`Selected: ${p.value.name}${C}`}),js=ot(()=>{const C=l.value.filter(j=>j.recommended),k=new Set(C.map(j=>j.id)),F=new Map;for(const j of l.value){if(k.has(j.id))continue;const ye=j.scale??1;F.set(ye,[...F.get(ye)??[],j])}const pe=Array.from(F.entries()).sort(([j],[ye])=>j-ye).map(([j,ye])=>({label:`${j}x Models`,options:ye.map($e=>({value:$e.id,label:fi($e.size)?`${$e.name} (${fi($e.size)})`:$e.name}))}));return C.length?[{label:"Recommended Models",options:C.map(j=>({value:j.id,label:fi(j.size)?`${j.name} (${fi(j.size)})`:j.name}))},...pe]:pe}),un=ot(()=>{var C;return r.compareWithClassicUpscale&&!!((C=w.value)!=null&&C.url)}),ai=ot(()=>{var C;return un.value?((C=w.value)==null?void 0:C.url)||"":y.value||m.value||""}),Bt=ot(()=>{var C,k;return((k=(C=$.value)==null?void 0:C.preview)==null?void 0:k.url)||""}),Ht=ot(()=>un.value?`Classic ${CT[r.classicCompareAlgorithm]} upscale`:y.value?"Model input":"Original"),Wi=ot(()=>{if(un.value&&w.value)return`${w.value.width}x${w.value.height}`;const C=c.value||f.value;return C?`${C.width}x${C.height}`:"—"}),mo=ot(()=>{var k;const C=(k=$.value)==null?void 0:k.preview;return C?`${C.width}x${C.height}`:"—"}),go=ot(()=>ai.value?Bt.value?"":"Run inference to overlay the upscaled output on top of the input image.":"Choose an image to start comparing original vs upscaled.");return{controls:r,statusText:i,statusTone:s,onnxVersionText:a,logLines:o,resultInfo:_,isRunning:R,isModelLoading:O,modelStatusLabel:Hs,modelStatusMeta:fo,imageStatusText:ho,predefinedModelGroups:js,baseImageUrl:ai,overlayImageUrl:Bt,baseLabel:Ht,baseResolution:Wi,overlayResolution:mo,compareEmptyMessage:go,canDownload:ot(()=>{var C,k;return!!((k=(C=$.value)==null?void 0:C.preview)!=null&&k.url)}),initialize:ln,handleModelFileSelected:oo,handleImageFileSelected:lo,reloadModel:Dn,runInference:Fs,cancelInference:po,downloadResult:tn,resetAll:rr,copyLog:Ui,clearLog:Li}}const WT={class:"min-h-screen w-full bg-stone-200 p-2 text-stone-800 sm:p-4"},qT={class:"mx-auto flex flex-col gap-4"},GT={class:"flex flex-col gap-4 lg:flex-row lg:items-start"},VT={class:"flex min-h-[420px] min-w-0 flex-1 flex-col lg:sticky lg:top-4 lg:h-[calc(100vh-2rem)] lg:min-h-0 lg:self-start"},FT=eo({__name:"App",setup(e){const t=LT(),n=ot(()=>t.logLines.value.join(` + +`));return Ou(()=>{t.initialize()}),(r,i)=>(kn(),zn("div",WT,[M("div",qT,[M("div",GT,[Rn(JS,{controls:We(t).controls,"onnx-version-text":We(t).onnxVersionText.value,"status-text":We(t).statusText.value,"status-tone":We(t).statusTone.value,"model-status-label":We(t).modelStatusLabel.value,"model-status-meta":We(t).modelStatusMeta.value,"image-status-text":We(t).imageStatusText.value,"predefined-model-groups":We(t).predefinedModelGroups.value,"is-running":We(t).isRunning.value,"is-model-loading":We(t).isModelLoading.value,onModelFile:We(t).handleModelFileSelected,onImageFile:We(t).handleImageFileSelected,onReload:We(t).reloadModel,onRun:We(t).runInference,onCancel:We(t).cancelInference,onReset:We(t).resetAll},null,8,["controls","onnx-version-text","status-text","status-tone","model-status-label","model-status-meta","image-status-text","predefined-model-groups","is-running","is-model-loading","onModelFile","onImageFile","onReload","onRun","onCancel","onReset"]),M("main",VT,[Rn(p3,{"base-image-url":We(t).baseImageUrl.value,"overlay-image-url":We(t).overlayImageUrl.value,"base-label":We(t).baseLabel.value,"base-resolution":We(t).baseResolution.value,"overlay-resolution":We(t).overlayResolution.value,"empty-message":We(t).compareEmptyMessage.value,"result-info":We(t).resultInfo,"can-download":We(t).canDownload.value,"status-tone":We(t).statusTone.value,onDownload:We(t).downloadResult},null,8,["base-image-url","overlay-image-url","base-label","base-resolution","overlay-resolution","empty-message","result-info","can-download","status-tone","onDownload"])])]),Rn(sT,{"log-output":n.value,onCopy:We(t).copyLog,onClear:We(t).clearLog},null,8,["log-output","onCopy","onClear"])])]))}});F1(FT).mount("#app");