Files
Robot_Arm/webapp/vendor/ort/ort.min.js
T

1828 lines
436 KiB
JavaScript
Raw Normal View History

/*!
* ONNX Runtime Web v1.20.1
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
"use strict";var ort=(()=>{var Ic=Object.create;var yr=Object.defineProperty;var _c=Object.getOwnPropertyDescriptor;var Sc=Object.getOwnPropertyNames;var Oc=Object.getPrototypeOf,Ac=Object.prototype.hasOwnProperty;var Bn=(i=>typeof require<"u"?require:typeof Proxy<"u"?new Proxy(i,{get:(e,o)=>(typeof require<"u"?require:e)[o]}):i)(function(i){if(typeof require<"u")return require.apply(this,arguments);throw Error('Dynamic require of "'+i+'" is not supported')});var x=(i,e)=>()=>(i&&(e=i(i=0)),e);var tt=(i,e)=>()=>(e||i((e={exports:{}}).exports,e),e.exports),We=(i,e)=>{for(var o in e)yr(i,o,{get:e[o],enumerable:!0})},oi=(i,e,o,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let r of Sc(e))!Ac.call(i,r)&&r!==o&&yr(i,r,{get:()=>e[r],enumerable:!(t=_c(e,r))||t.enumerable});return i};var Ae=(i,e,o)=>(o=i!=null?Ic(Oc(i)):{},oi(e||!i||!i.__esModule?yr(o,"default",{value:i,enumerable:!0}):o,i)),gr=i=>oi(yr({},"__esModule",{value:!0}),i);var xr,se,ue,Pc,Tr,wr=x(()=>{"use strict";xr=new Map,se=[],ue=(i,e,o)=>{if(e&&typeof e.init=="function"&&typeof e.createInferenceSessionHandler=="function"){let t=xr.get(i);if(t===void 0)xr.set(i,{backend:e,priority:o});else{if(t.priority>o)return;if(t.priority===o&&t.backend!==e)throw new Error(`cannot register backend "${i}" using priority ${o}`)}if(o>=0){let r=se.indexOf(i);r!==-1&&se.splice(r,1);for(let n=0;n<se.length;n++)if(xr.get(se[n]).priority<=o){se.splice(n,0,i);return}se.push(i)}return}throw new TypeError("not a valid backend")},Pc=async i=>{let e=xr.get(i);if(!e)return"backend not found.";if(e.initialized)return e.backend;if(e.aborted)return e.error;{let o=!!e.initPromise;try{return o||(e.initPromise=e.backend.init(i)),await e.initPromise,e.initialized=!0,e.backend}catch(t){return o||(e.error=`${t}`,e.aborted=!0),e.error}finally{delete e.initPromise}}},Tr=async i=>{let e=i.executionProviders||[],o=e.map(u=>typeof u=="string"?u:u.name),t=o.length===0?se:o,r,n=[],s=new Set;for(let u of t){let l=await Pc(u);typeof l=="string"?n.push({name:u,err:l}):(r||(r=l),r===l&&s.add(u))}if(!r)throw new Error(`no available backend found. ERR: ${n.map(u=>`[${u.name}] ${u.err}`).join(", ")}`);for(let{name:u,err:l}of n)o.includes(u)&&console.warn(`removing requested execution provider "${u}" from session options because it is not available: ${l}`);let a=e.filter(u=>s.has(typeof u=="string"?u:u.name));return[r,new Proxy(i,{get:(u,l)=>l==="executionProviders"?a:Reflect.get(u,l)})]}});var ii=x(()=>{"use strict";wr()});var ai,si=x(()=>{"use strict";ai="1.20.1"});var ui,Tt,Fn=x(()=>{"use strict";si();ui="warning",Tt={wasm:{},webgl:{},webgpu:{},versions:{common:ai},set logLevel(i){if(i!==void 0){if(typeof i!="string"||["verbose","info","warning","error","fatal"].indexOf(i)===-1)throw new Error(`Unsupported logging level: ${i}`);ui=i}},get logLevel(){return ui}};Object.defineProperty(Tt,"logLevel",{enumerable:!0})});var $,li=x(()=>{"use strict";Fn();$=Tt});var fi,ci,pi=x(()=>{"use strict";fi=(i,e)=>{let o=typeof document<"u"?document.createElement("canvas"):new OffscreenCanvas(1,1);o.width=i.dims[3],o.height=i.dims[2];let t=o.getContext("2d");if(t!=null){let r,n;e?.tensorLayout!==void 0&&e.tensorLayout==="NHWC"?(r=i.dims[2],n=i.dims[3]):(r=i.dims[3],n=i.dims[2]);let s=e?.format!==void 0?e.format:"RGB",a=e?.norm,u,l;a===void 0||a.mean===void 0?u=[255,255,255,255]:typeof a.mean=="number"?u=[a.mean,a.mean,a.mean,a.mean]:(u=[a.mean[0],a.mean[1],a.mean[2],0],a.mean[3]!==void 0&&(u[3]=a.mean[3])),a===void 0||a.bias===void 0?l=[0,0,0,0]:typeof a.bias=="number"?l=[a.bias,a.bias,a.bias,a.bias]:(l=[a.bias[0],a.bias[1],a.bias[2],0],a.bias[3]!==void 0&&(l[3]=a.bias[3]));let f=n*r,c=0,d=f,m=f*2,y=-1;s==="RGBA"?(c=0,d=f,m=f*2,y=f*3):s==="RGB"?(c=0,d=f,m=f*2):s==="RBG"&&(c=0,m=f,d=f*2);for(let g=0;g<n;g++)for(let T=0;T<r;T++){let _=(i.data[c++]-l[0])*u[0],w=(i.data[d++]-l[1])*u[1],v=(i.data[m++]-l[2])*u[2],L=y===-1?255:(i.data[y++]-l[3])*u[3];t.fillStyle="rgba("+_+","+w+","+v+","+L+")",t.fillRect(T,g,1,1)}if("toDataURL"in o)return o.toDataURL();throw new Error("toDataURL is not supported")}else throw new Error("Can n
precision highp float;
${e.attribute} vec3 position;
${e.attribute} vec2 textureCoord;
${e.varyingVertex} vec2 TexCoords;
void main()
{
gl_Position = vec4(position, 1.0);
TexCoords = textureCoord;
}`}function es(i){let e=D(i);return`${e.version}
precision highp float;
precision highp int;
precision highp sampler2D;
${e.varyingFrag} vec2 TexCoords;
${e.outputDeclaration}
const vec2 halfCR = vec2(0.5, 0.5);
// Custom vector types to handle higher dimenalities.
struct ivec5
{
int x;
int y;
int z;
int w;
int u;
};
struct ivec6
{
int x;
int y;
int z;
int w;
int u;
int v;
};
int imod(int x, int y) {
return x - y * (x / y);
}
`}function rs(i,e){let o=D(i);return`
void main() {
int indices[${e}];
toVec(TexCoords, indices);
vec4 result = vec4(process(indices));
${o.output} = result;
}
`}var qc,jc,X=x(()=>{"use strict";qc={version:"",attribute:"attribute",varyingVertex:"varying",varyingFrag:"varying",texture2D:"texture2D",output:"gl_FragColor",outputDeclaration:""},jc={version:"#version 300 es",attribute:"in",varyingVertex:"out",varyingFrag:"in",texture2D:"texture",output:"outputColor",outputDeclaration:"out vec4 outputColor;"}});var R=x(()=>{"use strict"});async function uo(i,e=t=>0,o){return new Promise((t,r)=>{let n=0,s=()=>{if(i()){t();return}n++;let a=e(n);if(o!=null&&n>=o){r();return}setTimeout(s,a)};s()})}function Rr(i){return $e(typeof i<"u"&&i.length!==0,()=>"empty string found for sampler name"),"get"+i.charAt(0).toUpperCase()+i.slice(1)}function ns(i){return $e(typeof i<"u"&&i.length!==0,()=>"empty string found for sampler name"),"get"+i.charAt(0).toUpperCase()+i.slice(1)+"AtOutCoords"}function ke(i,e){let o=JSON.parse(JSON.stringify(i));return o=e,o}function Be(i,e){return e.map(o=>i[o]).join(", ")}function ht(i){if(i<=1)return"int";if(i===2)return"ivec2";if(i===3)return"ivec3";if(i===4)return"ivec4";if(i===5)return"ivec5";if(i===6)return"ivec6";throw Error(`GPU for rank ${i} is not yet supported`)}function Gt(i=6){return["x","y","z","w","u","v"].slice(0,i)}var Vt=x(()=>{"use strict";U()});function Xc(i,e){return Gt(e).map(o=>`${i}.${o}`)}function Fe(i,e){return e===1?[i]:Xc(i,e)}function Ut(){return`
float getChannel(vec4 frag, int dim) {
int modCoord = imod(dim, 2);
return modCoord == 0 ? frag.r : frag.g;
}
float getChannel(vec4 frag, vec2 innerDims) {
vec2 modCoord = mod(innerDims, 2.);
return modCoord.x == 0. ?
(modCoord.y == 0. ? frag.r : frag.g) :
(modCoord.y == 0. ? frag.b : frag.a);
}
`}var ge=x(()=>{"use strict";Vt()});function Jc(i,e,o){if(i===0)return"false";if(i===1)return`rc > ${e[0]}`;let t="";for(let r=i-2;r<i;r++)t+=`${o[r]} >= ${e[r-i+2]}`,r<i-1&&(t+="||");return t}function Zc(i,e){let o=i.length;if(o===0)return"getA(), 0, 0, 0";if(o===1)return`getA(rc),
rc + 1 >= ${i[0]} ? 0. : getA(rc + 1),
0, 0`;let t="r, c",r="r, cp1",n="rp1, c",s="rp1, cp1",a="";if(o>2)for(let u=0;u<o-2;++u)a=a+`${e[u]},`;return`getA(${a}${t}),
rEdge ? 0. : getA(${a}${n}),
cEdge ? 0. : getA(${a}${r}),
rEdge || cEdge ? 0. : getA(${a}${s})`}function Yc(i,e,o,t){return i===0||i===1?"":`
int r = ${e[i-2]};
int c = ${e[i-1]};
int rp1 = ${e[i-2]} + 1;
int cp1 = ${e[i-1]} + 1;
bool rEdge = rp1 >= ${t};
bool cEdge = cp1 >= ${o};
`}var os,Kc,is,as=x(()=>{"use strict";X();R();Vt();ge();os={name:"pack",inputNames:["A"],inputTypes:[1]},Kc=(i,e)=>{let o=D(i.session.backend.glContext.version),t=e.dims,r=t.length,n=e.dims.length,s=ht(n),a=Fe("rc",n),u=Yc(n,a,t[t.length-2],t[t.length-1]),l;r===0?l=[1,1]:r===1?l=[t[0],1]:l=[t[n-1],t[n-2]];let f=Jc(n,l,a),c=Zc(t,a),d=`
void main() {
${s} rc = getOutputCoords();
if(${f}) {
${o.output} = vec4(0);
} else {
${u}
${o.output} = vec4(${c});
}
}
`;return{...os,hasMain:!0,output:{dims:e.dims,type:e.type,textureType:2},shaderSource:d}},is=(i,e)=>({...os,get:()=>Kc(i,e)})});function lo(i){if(i.length===0)return[1,1,1];let e=1;for(let o=0;o<i.length-2;++o)e*=i[o];return[e,i.length>1?i[i.length-2]:1,i[i.length-1]]}function us(i,e){let o=!1;return i.length===0||e.length===0?o=!0:i.length<2||e.length<2?o=i[i.length-1]===e[e.length-1]:o=i[i.length-1]===e[e.length-1]&&i[i.length-2]===e[e.length-2],o}function ep(i){let e=A.computeStrides(i),o=["b","r","c"],t="index";return`
ivec3 inputCoordsFromReshapedOutCoords(int index) {
${e.map((n,s)=>{let a=`int ${o[s]} = ${t} / ${n}`,u=s===e.length-1?`int ${o[s+1]} = ${t} - ${o[s]} * ${n}`:`index -= ${o[s]} * ${n}`;return`${a}; ${u};`}).join("")}
return ivec3(b, r, c);
}
`}function rp(i){let e=A.computeStrides(i);return`
int getFlattenedIndex(ivec3 coords) {
// reverse y, z order
return coords.x * ${e[0]} + coords.z * ${e[1]} + coords.y;
}
`}var Qc,tp,ss,ls=x(()=>{"use strict";U();X();R();ge();Qc=i=>({name:"Reshape (packed)",inputTypes:[2],inputNames:["A"],cacheHint:`${i}`}),tp=(i,e,o,t)=>{let r=e.dims,n=t,s="";for(let l=0;l<4;l++){let f="";switch(l){case 0:f="outputCoords = rc;";break;case 1:f="outputCoords = ivec3(rc.x, rc.y+1, rc.z);";break;case 2:f="outputCoords = ivec3(rc.x, rc.y, rc.z+1);";break;case 3:f="outputCoords = ivec3(rc.x, rc.y+1, rc.z+1);";break;default:throw new Error}s+=`
${f}
${l>0?"if(outputCoords.y < rows && outputCoords.z < cols){":""}
int flattenedIndex = getFlattenedIndex(outputCoords);
ivec3 inputRC = inputCoordsFromReshapedOutCoords(flattenedIndex);
vec2 innerDims = vec2(float(inputRC.y),float(inputRC.z));
result[${l}] = getChannel(getA(inputRC.x, inputRC.y, inputRC.z), innerDims);
${l>0?"}":""}
`}let a=D(i.session.backend.glContext.version),u=`
${ep(r)}
${rp(n)}
${Ut()}
void main() {
ivec3 rc = getOutputCoords();
vec4 result = vec4(0.0);
ivec3 outputCoords;
int rows = ${n[2]};
int cols = ${n[1]};
${s}
${a.output} = result;
}
`;return{...o,output:{dims:n,type:e.type,textureType:2},shaderSource:u,hasMain:!0}},ss=(i,e,o)=>{let t=Qc(o);return{...t,get:()=>tp(i,e,t,o)}}});var fo,fs=x(()=>{"use strict";X();R();fo=(i,e)=>{let o=e.shape,t=D(i.session.backend.glContext.version),r=`
const float FLOAT_MAX = 1.70141184e38;
const float FLOAT_MIN = 1.17549435e-38;
bool isNaN(float val) {
return (val < 1.0 || 0.0 < val || val == 0.0) ? false : true;
}
highp vec4 encodeAsUint8(highp float v) {
if (isNaN(v)) {
return vec4(255, 255, 255, 255);
}
highp float av = abs(v);
if(av < FLOAT_MIN) {
return vec4(0.0, 0.0, 0.0, 0.0);
} else if(v > FLOAT_MAX) {
return vec4(0.0, 0.0, 128.0, 127.0) / 255.0;
} else if(v < -FLOAT_MAX) {
return vec4(0.0, 0.0, 128.0, 255.0) / 255.0;
}
highp vec4 c = vec4(0,0,0,0);
highp float e = floor(log2(av));
highp float m = exp2(fract(log2(av))) - 1.0;
c[2] = floor(128.0 * m);
m -= c[2] / 128.0;
c[1] = floor(32768.0 * m);
m -= c[1] / 32768.0;
c[0] = floor(8388608.0 * m);
highp float ebias = e + 127.0;
c[3] = floor(ebias / 2.0);
ebias -= c[3] * 2.0;
c[2] += floor(ebias) * 128.0;
c[3] += 128.0 * step(0.0, -v);
return c / 255.0;
}
void main() {
float value = ${t.texture2D}(X,TexCoords).r;
${t.output} = encodeAsUint8(value);
}`,n={name:"Uint8Encode",inputTypes:[0],inputNames:["X"],output:{dims:o,type:e.tensor.type,textureType:3},shaderSource:r,hasMain:!0};return i.executeProgram(n,[e.tensor])}});function op(i,e){if(i===1)return"rc";let o="";for(let t=0;t<i;t++)o+=e[t],t<i-1&&(o+=",");return o}var cs,np,ps,ds=x(()=>{"use strict";X();R();Vt();ge();cs={name:"unpack",inputNames:["A"],inputTypes:[2]},np=(i,e)=>{let o=e.dims.length,t=Fe("rc",o),r=t.slice(-2),n=ht(o),s=Ut(),u=e.dims.length===0?"":op(o,t),l=o<=1?"rc":`vec2(${r.join(",")})`,f=D(i.session.backend.glContext.version),c=`
${s}
void main() {
${n} rc = getOutputCoords();
// Sample the texture with the coords to get the rgba channel value.
vec4 packedInput = getA(${u});
${f.output} = vec4(getChannel(packedInput, ${l}), 0, 0, 0);
}
`;return{...cs,hasMain:!0,output:{dims:e.dims,type:e.type,textureType:0},shaderSource:c}},ps=(i,e)=>({...cs,get:()=>np(i,e)})});var Gr,Qe,Mr,tr=x(()=>{"use strict";It();Gr=class{constructor(e,o=1){if(o===1)this.internalFormat=e.R32F,this.format=e.RED,this.textureType=e.FLOAT,this.channelSize=o;else if(o===4)this.internalFormat=e.RGBA32F,this.format=e.RGBA,this.textureType=e.FLOAT,this.channelSize=o;else throw new Error(`Invalid number of channels: ${o}`)}encode(e,o){let t,r;return e.constructor!==Float32Array&&(z.warning("Encoder","data was not of type Float32; creating new Float32Array"),r=new Float32Array(e)),o*this.channelSize>e.length?(z.warning("Encoder","Source data too small. Allocating larger array"),r=e,t=this.allocate(o*this.channelSize),r.forEach((n,s)=>t[s]=n)):(r=e,t=r),t}allocate(e){return new Float32Array(e*4)}decode(e,o){return this.channelSize===1?e.filter((r,n)=>n%4===0).subarray(0,o):e.subarray(0,o)}},Qe=class{constructor(e,o=1,t){if(o!==1&&o!==4)throw new Error(`Invalid number of channels: ${o}`);this.internalFormat=e.RGBA,this.format=e.RGBA,this.channelSize=o,this.textureType=t||e.FLOAT}encode(e,o){let t=e;return this.channelSize===1&&(z.verbose("Encoder","Exploding into a larger array"),t=this.allocate(o),e.forEach((r,n)=>t[n*4]=r)),t}allocate(e){return new Float32Array(e*4)}decode(e,o){return this.channelSize===1?e.filter((r,n)=>n%4===0).subarray(0,o):e.subarray(0,o)}},Mr=class{constructor(e,o=1){this.channelSize=4;if(o===1)this.internalFormat=e.ALPHA,this.format=e.ALPHA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=o;else if(o===4)this.internalFormat=e.RGBA,this.format=e.RGBA,this.textureType=e.UNSIGNED_BYTE,this.channelSize=o;else throw new Error(`Invalid number of channels: ${o}`)}encode(e,o){return new Uint8Array(e.buffer,e.byteOffset,e.byteLength)}allocate(e){return new Uint8Array(e*this.channelSize)}decode(e,o){if(e instanceof Uint8Array)return e.subarray(0,o);throw new Error(`Invalid array type: ${e.constructor}`)}}});var er,hs,co,ms=x(()=>{"use strict";U();R();er=(i,e,o)=>{let t=o===0||o===1?1:4,r=o===2,n=o===1||o===2,s=o===4?e.length-1:void 0,a=o===4?e.map((u,l)=>l===e.length-1?u*4:u):void 0;return co(i,e,t,a,{isPacked:r,reverseWH:n,breakAxis:s})},hs=(i,e,o)=>{let t=er(i,e,o);return[t.width,t.height]},co=(i,e,o=1,t,r)=>{let n=!!(r&&r.isPacked),[s,a]=i.computeTextureWH(n&&t||e,r),u=e.length,l=e.slice(0);if(u===0&&(l=[1]),o===1)t=e;else if(n){if(o!==4)throw new Error("a packed texture must be 4-channel");t=e,u>0&&(l[u-1]=Math.ceil(l[u-1]/2)),u>1&&(l[u-2]=Math.ceil(l[u-2]/2))}else if(!t)throw new Error("Unpacked shape is needed when using channels > 1");return{width:s,height:a,channels:o,isPacked:n,shape:l,strides:A.computeStrides(l),unpackedShape:t,reversedWH:r&&r.reverseWH}}});var ap,Vr,ys=x(()=>{"use strict";It();ye();U();as();ls();fs();ds();tr();ms();R();ap=(i,e)=>{let o=e.map(r=>`${r.unpackedShape.join(",")};${r.width}x${r.height}`).join("_"),t=i.name;return i.cacheHint&&(t+="["+i.cacheHint+"]"),t+=":"+o,t},Vr=class{constructor(e){this.session=e;this.packedTextureDataCache=new Map,this.unpackedTextureDataCache=new Map}calculateTextureWidthAndHeight(e,o){return hs(this.session.layoutStrategy,e,o)}executeProgram(e,o){if(o.length<e.inputNames.length)throw new Error(`Input size mustn't be less than ${e.inputNames.length}.`);if(e.inputNames.length!==e.inputTypes.length)throw new Error("input names size does not match input types");let t=[];for(let l=0;l<e.inputNames.length;++l)t[l]=this.getOrCreateTextureData(o[l],e.inputTypes[l]);let r=ap(e,t),n=this.session.programManager.getArtifact(r),s=n?n.programInfo:typeof e.get=="function"?e.get():e,a=er(this.session.layoutStrategy,s.output.dims,s.output.textureType),u=this.createTextureData(a,s.output.type);return n||(n=this.session.programManager.build(s,t,u),this.session.programManager.setArtifact(r,n)),this.runProgram(n,t,u),u}run(e,o){return this.executeProgram(e,o).tensor}runProgram(e,o,t){for(let r=0;r<o.length;++r)if(!!o[r].isPacked!=(e.programInfo.inputTypes[r]===2))throw new Error(`input[${r}] property packed inconsistent`);if(!!t.is
float process(int[${r}] indices) {
vec2 position = offsetToCoords(indices[1], ${n}, ${s});
float scale = getColorAsFloat(${t.texture2D}(Scale, position));
float mean = getColorAsFloat(${t.texture2D}(Mean, position));
float variance = getColorAsFloat(${t.texture2D}(Variance, position));
float b = getColorAsFloat(${t.texture2D}(B, position));
return scale * ( (_A(indices) - mean) / sqrt(variance + float(${o.epsilon})) ) + b;
}`;return{...gs,output:{dims:e[0].dims,type:e[0].type,textureType:0},shaderSource:a}},up=i=>{if(!i||i.length!==5)throw new Error("BatchNormalization requires 5 inputs.");let e=i[0],o=i[1],t=i[2],r=i[3],n=i[4];if(e.dims.length<3||o.dims.length!==1||t.dims.length!==1||r.dims.length!==1||n.dims.length!==1)throw new Error("invalid input shape.");if(o.dims[0]!==e.dims[1]||t.dims[0]!==e.dims[1]||r.dims[0]!==e.dims[1]||n.dims[0]!==e.dims[1])throw new Error("invalid input shape.");if(e.type!=="float32"&&e.type!=="float64"||o.type!=="float32"&&o.type!=="float64"||t.type!=="float32"&&t.type!=="float64"||r.type!=="float32"&&r.type!=="float64"||n.type!=="float32"&&n.type!=="float64")throw new Error("invalid input tensor types.")}});var Ur,At,O,rr,zr,Xt=x(()=>{"use strict";Ur=class{constructor(e,o,t,r){this.glContext=e;this.programInfo=o;this.inputTextureLayouts=t;this.outputTextureLayout=r}},At=class{constructor(e){this.context=e}},O=class{constructor(e,o){this.routineBody=e;this.dependencies=o}},rr=class{constructor(e,o,t){this.name=e;t?this.dependencies=t:this.dependencies=[],o&&(this.routineBody=o)}addDependency(e){e&&this.dependencies.push(e)}},zr=class{static returnOrderedNodes(e){if(!e||e.length===0)return[];if(e.length===1)return e;let o=new Set,t=new Set,r=new Array;return this.createOrderedNodes(e,o,t,r),r}static createOrderedNodes(e,o,t,r){for(let n=0;n<e.length;++n)this.dfsTraverse(e[n],o,t,r)}static dfsTraverse(e,o,t,r){if(!e||t.has(e.name))return;if(o.has(e.name))throw new Error("Cyclic dependency detected. Can't topologically sort routines needed for shader.");o.add(e.name);let n=e.dependencies;if(n&&n.length>0)for(let s=0;s<n.length;++s)this.dfsTraverse(n[s],o,t,r);r.push(e),t.add(e.name),o.delete(e.name)}}});function fp(){let i="add_";return{body:`
float ${i}(float a, float b) {
return a + b;
}
vec4 ${i}(vec4 v1, vec4 v2) {
return v1 + v2;
}
`,name:i,type:0}}function cp(){let i="div_";return{body:`
float ${i}(float a, float b) {
return a / b;
}
vec4 ${i}(vec4 v1, vec4 v2) {
return v1 / v2;
}
`,name:i,type:0}}function pp(){let i="mul_";return{body:`
float ${i}(float a, float b) {
return a * b;
}
vec4 ${i}(vec4 v1, vec4 v2) {
return v1 * v2;
}
`,name:i,type:0}}function dp(){let i="sub_";return{body:`
float ${i}(float a, float b) {
return a - b;
}
vec4 ${i}(vec4 v1, vec4 v2) {
return v1 - v2;
}
`,name:i,type:0}}function hp(){let i="equal_";return{body:`
float ${i}(float a, float b) {
return float(a == b);
}
vec4 ${i}(vec4 v1, vec4 v2) {
return vec4(equal(v1, v2));
}
`,name:i,type:0}}function mp(){let i="greater_";return{body:`
float ${i}(float a, float b) {
return float(a > b);
}
vec4 ${i}(vec4 v1, vec4 v2) {
return vec4( v1.r > v2.r ,
v1.g > v2.g,
v1.b > v2.b,
v1.a > v2.a );
}
`,name:i,type:0}}function bp(){let i="less_";return{body:`
float ${i}(float a, float b) {
return float(a < b);
}
vec4 ${i}(vec4 v1, vec4 v2) {
return vec4( v1.r < v2.r ,
v1.g < v2.g,
v1.b < v2.b,
v1.a < v2.a );
}
`,name:i,type:0}}function yp(){let i="and_";return{body:`
float ${i}(float a, float b) {
return float( bool(a) && bool(b) );
}
vec4 ${i}(vec4 v1, vec4 v2) {
bvec4 b1 = bvec4(v1);
bvec4 b2 = bvec4(v2);
return vec4( b1.r && b2.r ,
b1.g && b2.g,
b1.b && b2.b,
b1.a && b2.a );
}
`,name:i,type:0}}function gp(){let i="or_";return{body:`
float ${i}(float a, float b) {
return float( bool(a) || bool(b) );
}
vec4 ${i}(vec4 v1, vec4 v2) {
bvec4 b1 = bvec4(v1);
bvec4 b2 = bvec4(v2);
return vec4( b1.r || b2.r ,
b1.g || b2.g,
b1.b || b2.b,
b1.a || b2.a );
}
`,name:i,type:0}}function xp(){let i="xor_";return{body:`
float ${i}(float a, float b) {
return float( bool(a) ^^ bool(b) );
}
vec4 ${i}(vec4 v1, vec4 v2) {
bvec4 b1 = bvec4(v1);
bvec4 b2 = bvec4(v2);
return vec4( b1.r ^^ b2.r ,
b1.g ^^ b2.g,
b1.b ^^ b2.b,
b1.a ^^ b2.a );
}
`,name:i,type:0}}function Tp(){return vp("pow")}function wp(){let i="prelu_";return{body:`
float ${i}(float a, float b) {
return a < 0.0 ? a * b: a;
}
vec4 ${i}(vec4 v1, vec4 v2) {
return vec4(
v1.r < 0.0 ? v1.r * v2.r: v1.r,
v1.g < 0.0 ? v1.g * v2.g: v1.g,
v1.b < 0.0 ? v1.b * v2.b: v1.b,
v1.a < 0.0 ? v1.a * v2.a: v1.a
);
}
`,name:i,type:0}}function vp(i){let e=`${i}_`;return{body:`
float ${e}(float a, float b) {
return ${i}(a, b);
}
vec4 ${e}(vec4 v1, vec4 v2) {
return ${i}(v1, v2);
}
`,name:e,type:0}}var Pt,Ip,vs,Is,_s,Ss,Os,As,Ps,Es,Ds,Ls,$s,ks,Bs=x(()=>{"use strict";U();Xt();X();R();Pt=(i,e,o,t=e[0].type,r)=>{let n=i.session.pack?2:0;return{name:o.name,inputNames:["A","B"],inputTypes:[n,n],cacheHint:r,get:()=>Ip(i,e,o,t)}},Ip=(i,e,o,t=e[0].type)=>{let r=i.session.pack?2:0,n=!A.areEqual(e[0].dims,e[1].dims),s=e[0].dims,a=i.session.pack;if(n){let f=dt.calcShape(e[0].dims,e[1].dims,!1);if(!f)throw new Error("Can't perform binary op on the given tensors");s=f;let c=s.length,d=e[0].dims.length!==0?e[0].dims.length:1,m=e[1].dims.length!==0?e[1].dims.length:1,y=e[0].dims.length!==0?"bcastIndices_A(indices, aindices);":"aindices[0] = 0;",g=e[1].dims.length!==0?"bcastIndices_B(indices, bindices);":"bindices[0] = 0;",T=D(i.session.backend.glContext.version),_=a?`
${o.body}
void main() {
vec4 a = getAAtOutCoords();
vec4 b = getBAtOutCoords();
vec4 result = ${o.name}(a, b);
${T.output} = result;
}`:`
${o.body}
float process(int indices[${c}]) {
int aindices[${d}];
int bindices[${m}];
${y}
${g}
return ${o.name}(_A(aindices), _B(bindices));
}`;return{name:o.name,inputNames:["A","B"],inputTypes:[r,r],output:{dims:s,type:t,textureType:r},shaderSource:_,hasMain:a}}let u=D(i.session.backend.glContext.version),l=`
${o.body}
void main() {
vec4 v1 = ${u.texture2D}(A, TexCoords);
vec4 v2 = ${u.texture2D}(B, TexCoords);
vec4 result = ${o.name}(v1, v2);
${u.output} = result;
}
`;return{name:o.name,inputNames:["A","B"],inputTypes:[r,r],output:{dims:e[0].dims,type:t,textureType:r},shaderSource:l,hasMain:!0}},vs=(i,e)=>[i.run(Pt(i,e,fp()),e)],Is=(i,e)=>[i.run(Pt(i,e,yp(),"bool"),e)],_s=(i,e)=>[i.run(Pt(i,e,cp()),e)],Ss=(i,e)=>[i.run(Pt(i,e,hp(),"bool"),e)],Os=(i,e)=>[i.run(Pt(i,e,mp(),"bool"),e)],As=(i,e)=>[i.run(Pt(i,e,bp(),"bool"),e)],Ps=(i,e)=>[i.run(Pt(i,e,pp()),e)],Es=(i,e)=>[i.run(Pt(i,e,gp(),"bool"),e)],Ds=(i,e)=>[i.run(Pt(i,e,Tp()),e)],Ls=(i,e)=>[i.run(Pt(i,e,wp()),e)],$s=(i,e)=>[i.run(Pt(i,e,dp()),e)],ks=(i,e)=>[i.run(Pt(i,e,xp(),"bool"),e)]});var Fs,Ns,Sp,Cs=x(()=>{"use strict";U();Fs=(i,e,o)=>(Sp(e),[i.cast(e[0],o)]),Ns=i=>ut.tensorDataTypeFromProto(i.attributes.getInt("to")),Sp=i=>{if(!i||i.length!==1)throw new Error("Cast requires 1 input.");if(i[0].type==="string")throw new Error("Invalid input type.")}});var Op,Ap,Rs,Wr,Gs=x(()=>{"use strict";X();R();Vt();ge();Op=(i,e)=>({name:"Concat (packed)",inputNames:Array.from({length:i},(o,t)=>`X${t}`),inputTypes:Array(i).fill(2),cacheHint:e}),Ap=(i,e,o,t)=>{let r=o[0].dims.slice();if(t>=r.length||t<-1*r.length)throw new Error("axis specified for concat doesn't match input dimensionality");t<0&&(t=r.length+t);let n=r.slice(0);for(let B=1;B<o.length;B++){let Q=o[B].dims.slice();for(let pt=0;pt<r.length;pt++)if(pt===t)n[t]+=Q[pt];else if(r[pt]!==Q[pt])throw new Error("non concat dimensions must match")}let s=n.length,a=Fe("coords",s),u=ht(s),l=Ut(),f=o.map(B=>B.dims),c=Gt(s),d=new Array(f.length-1);d[0]=f[0][t];for(let B=1;B<d.length;B++)d[B]=d[B-1]+f[B][t];let m=c[t],y=c.slice(-2),g=c.join(),T=`if (${m} < ${d[0]}) {
return getChannel(
getX0(${g}), vec2(${y.join()}));
}`;for(let B=1;B<d.length;B++){let Q=d[B-1];T+=`
if (${m} < ${d[B]} && ${m} >= ${d[B-1]}) {
return getChannel(
getX${B}(${Wr(c,m,Q)}),
vec2(${Wr(y,m,Q)}));
}`}let _=d.length,w=d[d.length-1];T+=`
return getChannel(
getX${_}(${Wr(c,m,w)}),
vec2(${Wr(y,m,w)}));`;let v=D(i.session.backend.glContext.version),L=`
${l}
float getValue(${c.map(B=>"int "+B)}) {
${T}
}
void main() {
${u} coords = getOutputCoords();
int lastDim = coords.${c[s-1]};
coords.${c[s-1]} = coords.${c[s-2]};
coords.${c[s-2]} = lastDim;
vec4 result = vec4(getValue(${a}), 0., 0., 0.);
${a[s-1]} = ${a[s-1]} + 1;
if (${a[s-1]} < ${n[s-1]}) {
result.g = getValue(${a});
}
${a[s-2]} = ${a[s-2]} + 1;
if (${a[s-2]} < ${n[s-2]}) {
result.a = getValue(${a});
}
${a[s-1]} = ${a[s-1]} - 1;
if (${a[s-2]} < ${n[s-2]} &&
${a[s-1]} < ${n[s-1]}) {
result.b = getValue(${a});
}
${v.output} = result;
}
`;return{...e,output:{dims:n,type:o[0].type,textureType:2},shaderSource:L,hasMain:!0}},Rs=(i,e,o)=>{let t=Op(e.length,o.cacheKey);return{...t,get:()=>Ap(i,t,e,o.axis)}},Wr=(i,e,o)=>{let t=i.indexOf(e);return i.map((n,s)=>s===t?`${n} - ${o}`:n).join()}});var Ms,Pp,Ep,Dp,Vs,Lp,$p,kp,Us,Bp,zs=x(()=>{"use strict";at();R();Gs();Ms=(i,e,o)=>(Bp(e),i.session.pack&&e[0].dims.length>1?[i.run(Rs(i,e,o),e)]:[i.run(Dp(i,e,o),e)]),Pp=(i,e)=>({name:"Concat",inputNames:Array.from({length:i},(o,t)=>`X${t}`),inputTypes:Array(i).fill(0),cacheHint:e}),Ep=(i,e,o,t)=>{let r=o[0].dims.slice();if(t>=r.length||t<-1*r.length)throw new Error("axis specified for concat doesn't match input dimensionality");t<0&&(t=r.length+t);let n=r.slice(0);for(let m=1;m<o.length;m++){let y=o[m].dims.slice();for(let g=0;g<r.length;g++)if(g===t)n[t]+=y[g];else if(r[g]!==y[g])throw new Error("non concat dimensions must match")}let s=n.length,a=new Array(o.length),u=0;for(let m=0;m<a.length;++m)u+=o[m].dims[t],a[m]=u;let l="";o.length<5?l=Vs(a):l=Lp(a);let f=$p(o.length,s),c=kp(a),d=`
${f}
${c}
${l}
float process(int indices[${s}]) {
int textureIndex = getTextureWhereDataResides (indices[${t}]);
if(textureIndex != 0) {
indices[${t}] = indices[${t}] - int(getSizeInConcatAxisValueFromIndex(textureIndex-int(1)));
}
return fetchDataFromCorrectTexture(textureIndex, indices);
}`;return{...e,output:{dims:n,type:o[0].type,textureType:0},shaderSource:d}},Dp=(i,e,o)=>{let t=Pp(e.length,o.cacheKey);return{...t,get:()=>Ep(i,t,e,o.axis)}},Vs=i=>`int getTextureWhereDataResides(int index) {
${i.map((o,t)=>`if(index<${o}) {return ${t};}
`).join("")}
}`,Lp=i=>Vs(i),$p=(i,e)=>{let o=[`float fetchDataFromCorrectTexture(int textureIndex, int indices[${e}]) {`];for(let t=0;t<i;++t)t===0?o.push(` if (textureIndex == ${t}) { return _X${t}(indices); }`):t===i-1?o.push(` else { return _X${t}(indices); }`):o.push(` else if (textureIndex == ${t}) { return _X${t}(indices); }`);return o.push(" }"),o.join(`
`)},kp=i=>{let e=["int getSizeInConcatAxisValueFromIndex(int index) {"];for(let o=0;o<i.length;++o)o===0?e.push(` if (index == ${o}) { return ${i[o]}; }`):o===i.length-1?e.push(` else { return ${i[o]}; }`):e.push(` else if (index == ${o}) { return ${i[o]}; }`);return e.push(" }"),e.join(`
`)},Us=i=>F({axis:i.attributes.getInt("axis")}),Bp=i=>{if(!i||i.length<1)throw new Error("too few inputs");let e=i[0].type,o=i[0].dims.length;if(e==="string")throw new Error("string tensor is not supported yet");for(let t of i){if(t.type!==e)throw new Error("input tensors should be one type");if(t.dims.length!==o)throw new Error("input tensors should have the same shape")}}});function Fp(){return Et("abs")}function Np(){return Et("acos")}function Cp(){return Et("asin")}function Rp(){return Et("atan")}function Gp(){return Et("ceil")}function Mp(){return Et("cos")}function Vp(i){let e="elu";return{body:`
const float alpha = float(${i});
float ${e}_(float a) {
return a >= 0.0 ? a: (exp(a) - 1.0) * alpha;
}
vec4 ${e}_(vec4 v) {
return vec4(${e}_(v.x), ${e}_(v.y), ${e}_(v.z), ${e}_(v.w));
}
`,name:e,type:0}}function Up(){return Et("exp")}function zp(){return Et("floor")}function ho(i,e){let o="clip";return{body:`
const float min = float(${i});
const float max = float(${e});
float ${o}_(float a) {
return clamp(a, min, max);
}
vec4 ${o}_(vec4 v) {
return clamp(v, min, max);
}
`,name:o,type:0}}function Wp(){let i="indentity";return{body:`
float ${i}_(float a) {
return a;
}
vec4 ${i}_(vec4 v) {
return v;
}
`,name:i,type:0}}function Hp(i){let e="leakyRelu";return{body:`
const float alpha = float(${i});
float ${e}_(float a) {
return a < 0.0 ? a * alpha : a;
}
vec4 ${e}_(vec4 v) {
return vec4(${e}_(v.x), ${e}_(v.y), ${e}_(v.z), ${e}_(v.w));
}
`,name:e,type:0}}function qp(){return Et("log")}function jp(){let i="neg";return{body:`
float ${i}_(float a) {
return -a;
}
vec4 ${i}_(vec4 v) {
return -v;
}
`,name:i,type:0}}function Xp(){let i="not";return{body:`
float ${i}_(float a) {
return float( ! bool(a) );
}
bool ${i}_(bool a) {
return !a;
}
vec4 ${i}_(vec4 v) {
return vec4(!bool(v.x), !bool(v.y), !bool(v.z), !bool(v.w));
}
bvec4 ${i}_(bvec4 v) {
return bvec4(!v.x, !v.y, !v.z, !v.w);
}
`,name:i,type:0}}function Kp(){return Et("sin")}function mo(){let i="relu";return{body:`
float ${i}_(float a) {
return max( a, 0.0 );
}
vec4 ${i}_(vec4 v) {
return max( v, 0.0 );
}
`,name:i,type:0}}function bo(){let i="sigmoid";return{body:`
float ${i}_(float a) {
return 1.0 / (1.0 + exp(-a));
}
vec4 ${i}_(vec4 v) {
return 1.0 / (1.0 + exp(-v));
}
`,name:i,type:0}}function Jp(){return Et("sqrt")}function Zp(){return Et("tan")}function Yp(){let i="tanh";return{body:`
float ${i}_(float a) {
a = clamp(a, -10., 10.);
a = exp(2.*a);
return (a - 1.) / (a + 1.);
}
vec4 ${i}_(vec4 v) {
v = clamp(v, -10., 10.);
v = exp(2.*v);
return (v - 1.) / (v + 1.);
}
`,name:i,type:0}}function Et(i){return{body:`
float ${i}_(float a) {
return ${i}(a);
}
vec4 ${i}_(vec4 v) {
return ${i}(v);
}
`,name:i,type:0}}var Qp,Z,Ws,Hs,qs,js,yo,Xs,Ks,td,Js,Zs,Ys,Qs,tu,eu,go,ru,nu,ou,iu,au,su,uu,lu,fu,cu,pu,xo=x(()=>{"use strict";at();U();Xt();X();R();Qp=(i,e,o,t)=>{let r=i.session.pack?2:0,n=D(i.session.backend.glContext.version);return{...e,output:{dims:o.dims,type:o.type,textureType:r},shaderSource:`
${t.body}
void main() {
vec4 v = ${n.texture2D}(A, TexCoords);
v = ${t.name}_(v);
${n.output} = v;
}
`,hasMain:!0}},Z=(i,e,o,t)=>{let r=i.session.pack?2:0,n={name:o.name,inputTypes:[r],inputNames:["A"],cacheHint:t};return{...n,get:()=>Qp(i,n,e,o)}},Ws=(i,e)=>[i.run(Z(i,e[0],Fp()),e)],Hs=(i,e)=>[i.run(Z(i,e[0],Np()),e)],qs=(i,e)=>[i.run(Z(i,e[0],Cp()),e)],js=(i,e)=>[i.run(Z(i,e[0],Rp()),e)],yo=(i,e,o)=>[i.run(Z(i,e[0],ho(o.min,o.max),o.cacheKey),e)],Xs=i=>F({min:i.attributes.getFloat("min",me),max:i.attributes.getFloat("max",be)}),Ks=(i,e)=>{let o=td(i,e);return yo(i,[e[0]],o)},td=(i,e)=>{if(e.length>=3&&(!i.session.isInitializer(e[1].dataId)||!i.session.isInitializer(e[2].dataId)))throw new Error("dynamic clip attributes are not allowed");let o=e.length>=3?e[1].numberData[0]:me,t=e.length>=3?e[2].numberData[0]:be;return F({min:o,max:t})},Js=(i,e)=>[i.run(Z(i,e[0],Gp()),e)],Zs=(i,e)=>[i.run(Z(i,e[0],Mp()),e)],Ys=(i,e,o)=>[i.run(Z(i,e[0],Vp(o.alpha),o.cacheKey),e)],Qs=i=>F({alpha:i.attributes.getFloat("alpha",1)}),tu=(i,e)=>[i.run(Z(i,e[0],Up()),e)],eu=(i,e)=>[i.run(Z(i,e[0],zp()),e)],go=(i,e)=>[i.run(Z(i,e[0],Wp()),e)],ru=(i,e,o)=>[i.run(Z(i,e[0],Hp(o.alpha),o.cacheKey),e)],nu=i=>F({alpha:i.attributes.getFloat("alpha",.01)}),ou=(i,e)=>[i.run(Z(i,e[0],qp()),e)],iu=(i,e)=>[i.run(Z(i,e[0],jp()),e)],au=(i,e)=>[i.run(Z(i,e[0],Xp()),e)],su=(i,e)=>[i.run(Z(i,e[0],mo()),e)],uu=(i,e)=>[i.run(Z(i,e[0],bo()),e)],lu=(i,e)=>[i.run(Z(i,e[0],Kp()),e)],fu=(i,e)=>[i.run(Z(i,e[0],Jp()),e)],cu=(i,e)=>[i.run(Z(i,e[0],Zp()),e)],pu=(i,e)=>[i.run(Z(i,e[0],Yp()),e)]});function zt(i){let e;switch(i.activation){case"Relu":e=mo();break;case"Sigmoid":e=bo();break;case"Clip":e=ho(i.clipMin,i.clipMax);break;default:return{activationFunction:"",applyActivation:""}}let o=e.name,t=e.body,r=`value = ${o}_(value);`;return{activationFunction:t,applyActivation:r}}var Ne,xe=x(()=>{"use strict";U();xo();Ne=i=>{let e=i.getString("activation","");if(e==="Clip"){let[o,t]=i.getFloats("activation_params",[me,be]);return{activation:e,clipMax:t,clipMin:o,activationCacheKey:`${e}:${o},${t}`}}return{activation:e,activationCacheKey:e}}});var rd,nd,du,hu=x(()=>{"use strict";It();X();R();Hr();xe();rd=(i,e)=>({name:"GroupedConv",inputNames:i?["X","W","Bias"]:["X","W"],inputTypes:i?[0,0,0]:[0,0],cacheHint:e}),nd=(i,e,o,t)=>{let n=e.length>2?"value += getBias(output_channel);":"",s=e[0].dims.slice(),a=e[1].dims.slice(),u=a[0]/t.group;z.verbose("GroupedConv",`autpPad:${t.autoPad}, dilations:${t.dilations}, group:${t.group}, kernelShape:${t.kernelShape}, pads:${t.pads}, strides:${t.strides}`);let l=Ce(s,a,t.dilations,t.pads,t.strides),f=D(i.session.backend.glContext.version),{activationFunction:c,applyActivation:d}=zt(t),m=`
const ivec2 strides = ivec2(${t.strides[0]}, ${t.strides[1]});
const ivec2 pads = ivec2(${t.pads[0]}, ${t.pads[1]});
${c}
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
int output_channel = coords.y;
ivec2 xRCCorner = coords.zw * strides - pads;
int group_id = output_channel / ${u};
float value = 0.0;
for (int wInChannel = 0; wInChannel < ${a[1]}; wInChannel++) {
int input_channel = group_id * ${a[1]} + wInChannel;
for (int wHeight = 0; wHeight < ${a[2]}; wHeight++) {
int xHeight = xRCCorner.x + wHeight * ${t.dilations[0]};
if (xHeight < 0 || xHeight >= ${s[2]}) {
continue;
}
for (int wWidth = 0; wWidth < ${a[3]}; wWidth++) {
int xWidth = xRCCorner.y + wWidth * ${t.dilations[1]};
if (xWidth < 0 || xWidth >= ${s[3]}) {
continue;
}
float xVal = getX(batch, input_channel, xWidth, xHeight);
float wVal = getW(output_channel, wInChannel, wWidth, wHeight);
value += xVal*wVal;
}
}
}
${n}
${d}
${f.output} = vec4(value, .0, .0, .0);
}
`;return{...o,output:{dims:l,type:e[0].type,textureType:0},shaderSource:m,hasMain:!0}},du=(i,e,o)=>{let t=rd(e.length>2,o.cacheKey);return{...t,get:()=>nd(i,e,t,o)}}});var od,id,mu,bu=x(()=>{"use strict";X();R();ge();od=i=>({name:"Im2Col (packed)",inputNames:["A"],inputTypes:[2],cacheHint:i}),id=(i,e,o,t,r,n)=>{let s=o.dims,a=t.dims,u=2,l=3,f=r.length,c=[a[1]*a[2]*a[3],r[2]*r[3]],d=a[2]*a[3],m=Ut(),y=D(i.session.backend.glContext.version),g="";for(let _=0;_<=1;_++)for(let w=0;w<=1;w++)g+=`
blockIndex = rc.x + ${w};
pos = rc.y + ${_};
if(blockIndex < ${c[1]} && pos < ${c[0]}) {
offsetY = int(blockIndex / (${r[f-1]})) * ${n.strides[0]} -
${n.pads[0]};
d0 = offsetY + ${n.dilations[0]} * (imod(pos, ${d}) / ${a[2]});
if(d0 < ${s[u]} && d0 >= 0) {
offsetX = imod(blockIndex, ${r[f-1]}) * ${n.strides[1]} -
${n.pads[1]};
d1 = offsetX + ${n.dilations[1]} * imod(imod(pos, ${d}), ${a[2]});
if(d1 < ${s[l]} && d1 >= 0) {
ch = int(float(pos)/ ${d}.);
innerDims = vec2(d0, d1);
result[${_*2+w}] = getChannel(
getA(0, ch, int(innerDims.x),
int(innerDims.y)), innerDims);
}
}
}
`;let T=`
${m}
void main() {
ivec2 rc = getOutputCoords();
vec4 result = vec4(0.0);
int blockIndex, pos, offsetY, d0, offsetX, d1, ch;
vec2 innerDims;
${g}
${y.output} = result;
}
`;return{...e,output:{dims:c,type:o.type,textureType:2},shaderSource:T,hasMain:!0}},mu=(i,e,o,t,r)=>{let n=od(r.cacheKey);return{...n,get:()=>id(i,n,e,o,t,r)}}});function sd(i,e,o){let t=e[0].dims,r=e[1].dims,n=dt.calcShape(t,r,!0);if(!n)throw new Error("Can't use matmul on the given tensors");let s=ht(n.length),a=Gt(),{activationFunction:u,applyActivation:l}=zt(o),f=e.length>2,c=f?"value += getBiasForMatmul();":"",d=f?`${wo(s,a,e[2].dims,n,!1)}`:"",m=n.length,y=t.length,g=r.length,T=t[t.length-1],_=`
${u}
${d}
float process(int indices[${m}]) {
int a[${y}];
int b[${g}];
bcastMatmulIndices_A(indices, a);
bcastMatmulIndices_B(indices, b);
float value;
for (int k=0; k<${T}; ++k) {
a[${y-1}] = k;
b[${g-2}] = k;
value += _A(a) * _B(b);
}
${c}
${l}
return value;
}`;return{...i,output:{dims:n,type:e[0].type,textureType:0},shaderSource:_}}function To(i,e){let o=ad(i.length>2,e.activationCacheKey);return{...o,get:()=>sd(o,i,e)}}function wo(i,e,o,t,r){let n="",s=o.length,a=t.length,u=a-s;a<2&&s>0?n="coords":n=o.map((g,T)=>`coords.${e[T+u]}`).join(", ");let f=dt.getBroadcastDims(o,t).map(g=>`coords.${e[g+u]} = 0;`).join(`
`),d=A.size(o)===1,m="vec4(outputValue.xx, outputValue.yy)";return d&&(m="vec4(outputValue.x)"),r?`
vec4 getBiasForMatmul() {
${i} coords = getOutputCoords();
${f}
vec4 outputValue = getBias(${n});
return ${m};
}`:`
float getBiasForMatmul() {
${i} coords = getOutputCoords();
${f}
return getBias(coords.x);
}`}var yu,gu,ad,ud,qr=x(()=>{"use strict";U();R();Vt();xe();vo();yu=(i,e,o)=>(ud(e),i.session.pack?[i.run(jr(i,e,o),e)]:[i.run(To(e,o),e)]),gu=i=>Ne(i.attributes),ad=(i,e)=>({name:"MatMul",inputNames:i?["A","B","Bias"]:["A","B"],inputTypes:i?[0,0,0]:[0,0],cacheHint:e});ud=i=>{if(!i||i.length!==2)throw new Error("MatMul requires 2 inputs.");if(i[0].dims[i[0].dims.length-1]!==i[1].dims[i[1].dims.length-2])throw new Error("shared dimension does not match.");if(i[0].type!=="float32"&&i[0].type!=="float64"||i[1].type!=="float32"&&i[1].type!=="float64")throw new Error("inputs should be float type");if(i[0].type!==i[1].type)throw new Error("inputs types should match")}});function cd(i,e,o,t){let r=[],n=[],s=o[0].dims,a=o[1].dims,u=s.length,l=a.length,f=t.length,c=f-u,d=f-l;r=s.map((v,L)=>`coords.${e[L+c]}`),r[u-1]="i*2",r.join(", "),n=a.map((v,L)=>`coords.${e[L+d]}`),n[l-2]="i*2",n.join(", ");let m=dt.getBroadcastDims(s,t),y=dt.getBroadcastDims(a,t),g=m.map(v=>`coords.${e[v+c]} = 0;`).join(`
`),T=y.map(v=>`coords.${e[v+d]} = 0;`).join(`
`),_=`int lastDim = coords.${e[f-1]};
coords.${e[f-1]} = coords.${e[f-2]};
coords.${e[f-2]} = lastDim;`;return`
vec4 getAAtOutCoordsMatmul(int i) {
${i} coords = getOutputCoords();
${_}
${g}
vec4 outputValue = getA(${r});
return outputValue;
}
vec4 getBAtOutCoordsMatmul(int i) {
${i} coords = getOutputCoords();
${_}
${T}
vec4 outputValue = getB(${n});
return outputValue;
}`}function pd(i,e){let o="";for(let t=0;t<e-2;t++)o+=`rc.${i[t]}, `;return o+=`rc.${i[e-2]}, i*2`,o}function dd(i,e){let o="";for(let t=0;t<e-2;t++)o+=`rc.${i[t]}, `;return o+=`i*2, rc.${i[e-1]}`,o}var ld,fd,jr,vo=x(()=>{"use strict";U();X();R();Vt();xe();qr();ld=(i,e)=>({name:"MatMul (packed)",inputNames:i?["A","B","Bias"]:["A","B"],inputTypes:i?[2,2,2]:[2,2],cacheHint:e}),fd=(i,e,o,t)=>{let r=o.length>2,n=r?"value += getBiasForMatmul();":"",s=o[0].dims,a=o[1].dims,u=dt.calcShape(s,a,!0),l=!A.areEqual(o[0].dims,o[1].dims);if(!u)throw new Error("Can't use matmul on the given tensors");let f=s[s.length-1],c=Math.ceil(f/2),d=s.length,m=a.length,y=D(i.session.backend.glContext.version),g=ht(u.length),T=u.length,_=Gt(),{activationFunction:w,applyActivation:v}=zt(t),L=r?`${wo(g,_,o[2].dims,u,!0)}`:"",B=l?`${cd(g,_,o,u)}`:"",Q=l?"getAAtOutCoordsMatmul(i)":`getA(${pd(_,d)})`,pt=l?"getBAtOutCoordsMatmul(i)":`getB(${dd(_,m)})`,Ht=l?"":`${g} rc =
getOutputCoords(); int lastDim = rc.${_[T-1]}; rc.${_[T-1]} =
rc.${_[T-2]}; rc.${_[T-2]} = lastDim;
`,wt=`
${B}
${L}
${w}
void main() {
${Ht}
vec4 value = vec4(0);
for (int i = 0; i < ${c}; i++) {
vec4 a = ${Q};
vec4 b = ${pt};
value += (a.rrbb * b.rgrg);
value += (a.ggaa * b.baba);
}
${n}
${v}
${y.output} = value;
}`;return{...e,output:{dims:u,type:o[0].type,textureType:2},shaderSource:wt,hasMain:!0}},jr=(i,e,o)=>{let t=ld(e.length>2,o.activationCacheKey);return{...t,get:()=>fd(i,t,e,o)}}});var xu,Tu=x(()=>{"use strict";Hr();bu();vo();xu=(i,e,o)=>{let t=e[0].dims,r=e[1].dims,n=Ce(t,r,o.dilations,o.pads,o.strides),s=i.run(mu(i,e[0],e[1],n,o),[e[0]]),a=i.reshapePacked(e[1],[r[0],r[1]*r[2]*r[3]]),u=e.length===3?[a,s,e[2]]:[a,s],l=i.run(jr(i,u,o),u);return i.reshapePacked(l,n)}});var hd,md,wu,Io,_o=x(()=>{"use strict";R();hd=i=>({name:"Im2Col",inputNames:["X"],inputTypes:[0],cacheHint:i}),md=(i,e,o,t,r,n)=>{let s=o.dims,a=t.dims,u=r.length,l=Io(s,a,r,4),f=`
const int XC = ${s[1]};
const int XH = ${s[2]};
const int XW = ${s[3]};
const int KH = ${n.kernelShape[0]};
const int KW = ${n.kernelShape[1]};
const int dilationH = ${n.dilations[0]};
const int dilationW = ${n.dilations[1]};
const int strideH = ${n.strides[0]};
const int strideW = ${n.strides[1]};
const int padH = ${n.pads[0]};
const int padW = ${n.pads[1]};
const int KHKW = KH*KW;
const int XCKHKW = XC * KHKW;
const int outputChannels = 4;
vec4 process(int indices[${u}]) {
int b = indices[0]; // batch size
int oh = indices[1] * strideH - padH; //output height
int ow = indices[2] * strideW - padW; //output width
int p = indices[3] * outputChannels; //patch
vec4 value = vec4(0.0);
for(int i=0; i < outputChannels; ++i) {
if(p < XCKHKW) {
int patchC = p / KHKW;
int patchH = (p - patchC*KHKW) / KW;
int patchW = (p - patchC*KHKW) - patchH * KW;
int xh2 = oh + patchH * dilationH;
int xw2 = ow + patchW * dilationW;
int x[${s.length}];
x[0] = b;
x[1] = patchC;
x[2] = xh2;
x[3] = xw2;
if(xh2 >= 0 &&
xh2 < XH &&
xw2 >= 0 &&
xw2 < XW) {
value[i] = _X(x);
}
}
++p;
}
return value;
}
`;return{...e,output:{dims:l,type:o.type,textureType:4},shaderSource:f}},wu=(i,e,o,t,r)=>{let n=hd(r.cacheKey);return{...n,get:()=>md(i,n,e,o,t,r)}},Io=(i,e,o,t=4)=>[o[0],o[2],o[3],Math.ceil(i[1]*e[2]*e[3]/t)]});var bd,yd,vu,Iu=x(()=>{"use strict";U();X();R();xe();_o();bd=(i,e)=>({name:"ConvDotProduct",inputNames:i?["Im2Col","K","B"]:["Im2Col","K"],inputTypes:i?[0,4,0]:[0,4],cacheKey:e.activationCacheKey}),yd=(i,e,o,t,r)=>{let n=o[0].dims,s=o[1].dims,a=[s[0],Math.ceil(n[1]*s[2]*s[3]/4)],u=Io(n,s,t),[l,f]=i.calculateTextureWidthAndHeight(a,4),c=A.computeStrides(u),[d,m]=i.calculateTextureWidthAndHeight(u,4),y=t.length,g=o.length<3?"0.0":"_B(b)",T=Math.ceil(n[1]*s[2]*s[3]/4),{activationFunction:_,applyActivation:w}=zt(r),v=D(i.session.backend.glContext.version),L=`
${_}
float process(int indices[${y}]) {
int b[1];
b[0] = indices[1];
int im2col[4];
im2col[0] = indices[0];
im2col[1] = indices[2];
im2col[2] = indices[3];
int im2colOffset = im2col[0] * ${c[0]} + im2col[1] * ${c[1]} + im2col[2] * ${c[2]};
int kernelOffset = indices[1] * ${a[1]};
float value = ${g};
for (int i = 0; i < ${T}; ++i) {
vec2 im2colCoords = offsetToCoords(im2colOffset, ${d}, ${m});
vec2 kernelCoords = offsetToCoords(kernelOffset, ${l}, ${f});
value += dot(${v.texture2D}(Im2Col, im2colCoords), ${v.texture2D}(K, kernelCoords));
++im2colOffset;
++kernelOffset;
}
${w}
return value;
}`;return{...e,output:{dims:t,type:o[0].type,textureType:0},shaderSource:L}},vu=(i,e,o,t)=>{let r=bd(e.length>2,t);return{...r,get:()=>yd(i,r,e,o,t)}}});var Ce,So,gd,xd,Td,wd,Oo,vd,Hr=x(()=>{"use strict";at();U();hu();Tu();Iu();xe();_o();qr();Ce=(i,e,o,t,r)=>{let n=i[0],s=i.slice(2),a=s.length,u=e[0],f=e.slice(2).map((y,g)=>y+(y-1)*(o[g]-1)),d=s.map((y,g)=>y+t[g]+t[g+a]).map((y,g)=>Math.floor((y-f[g]+r[g])/r[g]));return[n,u].concat(...d)},So=(i,e,o)=>(vd(e,o),gd(i,e,o)),gd=(i,e,o)=>{let t=wd(o,e),r=i.session.pack,n=t.kernelShape[0]===1&&t.kernelShape[1]===1;return t.group>1?[i.run(du(i,e,t),e)]:n&&r?[xd(i,e,t)]:r&&e[0].dims.length===4&&e[0].dims[0]===1&&!n?[xu(i,e,t)]:[Td(i,e,t)]},xd=(i,e,o)=>{let t=e[0].dims,r=e[1].dims,n=Ce(t,r,o.dilations,o.pads,o.strides),s=i.reshapeUnpacked(e[0],[t[1],t[2]*t[3]]),a=i.reshapeUnpacked(e[1],[r[0],r[1]]),u=e.length>2?[a,s,e[2]]:[a,s],l=i.run(To(u,o),u);return i.reshapeUnpacked(l,n)},Td=(i,e,o)=>{let t=e[0].dims,r=e[1].dims,n=Ce(t,r,o.dilations,o.pads,o.strides),s=i.run(wu(i,e[0],e[1],n,o),[e[0]]),a=e.length===3?[s,e[1],e[2]]:[s,e[1]];return i.run(vu(i,e,n,o),a)},wd=(i,e)=>{let o=i.kernelShape.slice();if(i.kernelShape.length===0)for(let n=2;n<e[1].dims.length;++n)o.push(e[1].dims[n]);let t=i.pads.slice();he.adjustPadsBasedOnAutoPad(e[0].dims,i.strides,i.dilations,o,t,i.autoPad);let r=Object.assign({},i);return Object.assign(r,{kernelShape:o,pads:t,cacheKey:i.cacheKey}),r},Oo=i=>{let e=i.attributes,o=Ne(e),t=e.getString("auto_pad","NOTSET"),r=e.getInts("dilations",[1,1]),n=e.getInt("group",1),s=e.getInts("kernel_shape",[]),a=e.getInts("pads",[0,0,0,0]),u=e.getInts("strides",[1,1]);return F({autoPad:t,dilations:r,group:n,kernelShape:s,pads:a,strides:u,...o})},vd=(i,e)=>{if(!i||i.length!==2&&i.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(i[0].dims.length!==4||i[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let o=i[0].dims[1],t=i[1].dims[1]*e.group;if(o!==t)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");if(i.length===3&&(i[2].dims.length!==1||i[1].dims[0]!==i[2].dims[0]))throw new Error("invalid bias");let r=i[0].dims.length-2;if(e.dilations.length!==r)throw new Error(`dilations should be ${r}D`);if(e.strides.length!==r)throw new Error(`strides should be ${r}D`);if(e.pads.length!==r*2)throw new Error(`pads should be ${r*2}D`);if(e.kernelShape.length!==0&&e.kernelShape.length!==i[1].dims.length-2)throw new Error("invalid kernel shape");if(i[0].type!=="float32"||i[1].type!=="float32")throw new Error("Conv input(X,W) should be float tensor");if(i.length===3&&i[2].type!=="float32")throw new Error("Conv input(bias) should be float tensor")}});var Id,_d,Sd,_u,Od,Ad,Pd,Ed,Dd,Ld,Su,$d,Ou=x(()=>{"use strict";at();X();R();xe();Id=(i,e,o,t,r,n)=>(i-1)*e+o+(t-1)*r+1-n,_d=(i,e,o,t,r)=>{let n=Math.floor(i/2);e==="SAME_UPPER"?(o[t]=n,o[r]=i-n):e==="SAME_LOWER"&&(o[t]=i-n,o[r]=n)},Sd=(i,e,o,t,r,n,s,a)=>{let u=i.length-2,l=a.length===0;for(let f=0;f<u;++f){let c=l?i[f+2]*n[f]:a[f],d=Id(i[f+2],n[f],r[f],e[f],o[f],c);_d(d,t,r,f,f+u),l&&a.push(n[f]*(i[f+2]-1)+s[f]+(e[f]-1)*o[f]+1-r[f]-r[f+u])}},_u=(i,e,o)=>($d(e,o),Od(i,e,o)),Od=(i,e,o)=>{let t=Ld(o,e);return[Dd(i,e,t)]},Ad=(i,e)=>({name:"ConvTranspose",inputNames:i?["X","W","B"]:["X","W"],inputTypes:i?[0,0,0]:[0,0],cacheHint:e}),Pd=(i,e,o,t)=>{let n=e.length>2?"getB(output_channel)":"0.0",s=e[0].dims,a=e[1].dims,u=a[1],l=a[0]/t.group,f=[e[0].dims[0],e[1].dims[1]*t.group,...t.outputShape],c=D(i.session.backend.glContext.version),{activationFunction:d,applyActivation:m}=zt(t),y=`
const ivec2 strides = ivec2(${t.strides[0]}, ${t.strides[1]});
const ivec2 pads = ivec2(${t.pads[0]}, ${t.pads[1]});
${d}
void main() {
ivec4 coords = getOutputCoords();
int batch = coords.x;
int output_channel = coords.y;
ivec2 loc = coords.zw + pads;
int group_id = output_channel / ${u};
int wOutChannel = output_channel - group_id * ${u};
float value = ${n};
for (int inChannelOffset = 0; inChannelOffset < ${l}; inChannelOffset++) {
int input_channel = group_id * ${l} + inChannelOffset;
for (int wWOff = 0; wWOff < ${a[2]}; wWOff++) {
for (int wHOff = 0; wHOff < ${a[3]}; wHOff++) {
ivec2 wOff = ivec2(wWOff * ${t.dilations[0]}, wHOff * ${t.dilations[1]});
ivec2 wLoc = loc - wOff;
ivec2 wLocIn = wLoc / strides;
if (
wLocIn * strides == wLoc &&
wLocIn.x >= 0 && wLocIn.x < ${s[2]} &&
wLocIn.y >= 0 && wLocIn.y < ${s[3]}
) {
float xVal = getX(batch, input_channel, wLocIn.y, wLocIn.x);
float wVal = getW(input_channel, wOutChannel, wHOff, wWOff);
value += xVal * wVal;
}
}
}
}
${m}
${c.output} = vec4(value, .0, .0, .0);
}
`;return{...o,output:{dims:f,type:e[0].type,textureType:0},shaderSource:y,hasMain:!0}},Ed=(i,e,o)=>{let t=Ad(e.length>2,o.cacheKey);return{...t,get:()=>Pd(i,e,t,o)}},Dd=(i,e,o)=>i.run(Ed(i,e,o),e),Ld=(i,e)=>{let o=i.kernelShape.slice();if(i.kernelShape.length===0)for(let a=2;a<e[1].dims.length;++a)o.push(e[1].dims[a]);let t=i.pads.slice(),r=i.outputShape.slice(),n=e[0].dims;Sd(n,o,i.dilations,i.autoPad,t,i.strides,i.outputPadding,r);let s=Object.assign({},i);return Object.assign(s,{kernelShape:o,pads:t,outputShape:r,cacheKey:i.cacheKey}),s},Su=i=>{let e=i.attributes,o=Ne(e),t=e.getString("auto_pad","NOTSET"),r=e.getInts("dilations",[1,1]),n=e.getInt("group",1),s=e.getInts("kernel_shape",[]),a=e.getInts("output_padding",[0,0]),u=e.getInts("output_shape",[]),l=e.getInts("pads",[0,0,0,0]),f=e.getInts("strides",[1,1]);return F({autoPad:t,dilations:r,group:n,kernelShape:s,outputPadding:a,outputShape:u,pads:l,strides:f,...o})},$d=(i,e)=>{if(!i||i.length!==2&&i.length!==3)throw new Error("Conv requires 2 or 3 inputs");if(i[0].dims.length!==4||i[1].dims.length!==4)throw new Error("currently only support 2-dimensional conv");let o=i[0].dims[1],t=i[1].dims[0];if(o!==t)throw new Error("FILTER_IN_CHANNEL should be equal to DATA_CHANNEL");let r=i[1].dims[1]*e.group;if(i.length===3&&(i[2].dims.length!==1||i[2].dims[0]!==r))throw new Error("invalid bias");let n=i[0].dims.length-2;if(e.dilations.length!==n)throw new Error(`dilations should be ${n}D`);if(e.strides.length!==n)throw new Error(`strides should be ${n}D`);if(e.pads.length!==n*2)throw new Error(`pads should be ${n*2}D`);if(e.outputPadding.length!==n)throw new Error(`output_padding should be ${n}D`);if(e.kernelShape.length!==0&&e.kernelShape.length!==i[1].dims.length-2)throw new Error("invalid kernel shape");if(e.outputShape.length!==0&&e.outputShape.length!==i[0].dims.length-2)throw new Error("invalid output shape");if(i[0].type!=="float32"||i[1].type!=="float32")throw new Error("ConvTranspose input(X,W) should be float tensor");if(i.length===3&&i[2].type!=="float32")throw new Error("ConvTranspose input(bias) should be float tensor")}});var Au,Te,Pu,kd,Eu,Bd,Fd,Nd,Xr=x(()=>{"use strict";at();U();R();Au={name:"Transpose",inputNames:["A"],inputTypes:[0]},Te=(i,e,o)=>(Nd(e),[i.run({...Au,cacheHint:o.cacheKey,get:()=>kd(i,e[0],o.perm)},e)]),Pu=i=>F({perm:i.attributes.getInts("perm",[])}),kd=(i,e,o)=>{let t=e.dims;o=Eu(t,o);let r=Bd(t,o),n=t.length,s=`
${Fd("perm",o,n)}
float process(int indices[${n}]) {
int a[${n}];
perm(a, indices);
return _A(a);
}`;return{...Au,output:{dims:r,type:e.type,textureType:0},shaderSource:s}},Eu=(i,e)=>(e&&e.length!==i.length&&(e=[...i.keys()].reverse()),e),Bd=(i,e)=>(e=Eu(i,e),A.sortBasedOnPerm(i,e)),Fd=(i,e,o)=>{let t=[];t.push(`void ${i}(out int a[${o}], int src[${o}]) {`);for(let r=0;r<o;++r)t.push(` a[${e[r]}]=src[${r}];`);return t.push(" }"),t.join(`
`)},Nd=i=>{if(!i||i.length!==1)throw new Error("Transpose requires 1 input.");if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("input should be float tensor")}});var Du,Lu,Cd,$u=x(()=>{"use strict";Xr();Du=(i,e,o)=>{Cd(e);let t=o.blocksize,r=t*t,n=o.mode==="DCR"?[0,3,4,1,5,2]:[0,1,4,2,5,3],s=o.mode==="DCR"?[e[0].dims[0],t,t,e[0].dims[1]/r,e[0].dims[2],e[0].dims[3]]:[e[0].dims[0],e[0].dims[1]/r,t,t,e[0].dims[2],e[0].dims[3]],a=i.reshapeUnpacked(e[0],s),u={perm:n,cacheKey:`${n}`},[l]=Te(i,[a],u),f=[e[0].dims[0],e[0].dims[1]/r,e[0].dims[2]*t,e[0].dims[3]*t];return[i.reshapeUnpacked(l,f)]},Lu=i=>{let e=i.attributes.getInt("blocksize");if(e<1)throw new Error(`blocksize must be >= 1, but got : ${e} for DepthToSpace`);let o=i.attributes.getString("mode","DCR");if(o!=="DCR"&&o!=="CRD")throw new Error(`unrecognized mode: ${o} for DepthToSpace`);return{mode:o,blocksize:e}},Cd=i=>{if(i.length!==1)throw new Error(`DepthToSpace expect 1 inputs, but got ${i.length}`);if(i[0].type==="string"||i[0].dims.length!==4)throw new TypeError("DepthToSpace input should be a 4-D numeric tensor")}});var ku,Bu,Rd,Fu=x(()=>{"use strict";U();ku=(i,e,o)=>{Rd(e,o);let t=A.flattenShape(e[0].dims,o);return[i.reshapeUnpacked(e[0],t)]},Bu=i=>i.attributes.getInt("axis",1),Rd=(i,e)=>{if(!i||i.length!==1)throw new Error("Flatten requires 1 input.");let o=i[0].dims.length;if(o===0)throw new Error("scalar tensor is not supported.");if(e<-o||e>o)throw new Error("Invalid axis");if(i[0].type==="string")throw new Error("string tensor is not supported.")}});var re,nr=x(()=>{"use strict";re=["float32","float64","int32","int16","int8","uint16","uint32","uint8"]});var Nu,Cu,Gd,Md,Vd,Ud,Ru=x(()=>{"use strict";at();nr();U();R();Nu=(i,e,o)=>(Ud(e,o.axis),[i.run(Vd(i,e,o),e)]),Cu=i=>F({axis:i.attributes.getInt("axis",0)}),Gd={name:"Gather",inputNames:["A","B"],inputTypes:[0,0]},Md=(i,e,o,t)=>{let r=o[0].dims.slice(),n=o[1].dims.slice(),s=new Array(r.length+n.length-1);t=A.normalizeAxis(t,r.length);let a=[];for(let d=0;d<s.length;d++)d<t?(s[d]=r[d],a.push(`inputIdx[${d}] = outputIdx[${d}];`)):d<t+n.length?(s[d]=n[d-t],a.push(`indexDataIdx[${d-t}] = outputIdx[${d}];`)):(s[d]=r[d-n.length+1],a.push(`inputIdx[${d-n.length+1}] = outputIdx[${d}];`));let u=s.length||1,l=r.length,f=n.length||1,c=`
float process(int outputIdx[${u}]) {
int inputIdx[${l}];
int indexDataIdx[${f}];
indexDataIdx[0] = 0;
${a.join(`
`)}
int idx = int(_B(indexDataIdx));
inputIdx[${t}] = idx < 0 ? idx + ${r[t]} : idx;
return _A(inputIdx);
}`;return{...e,output:{dims:s,type:o[0].type,textureType:0},shaderSource:c}},Vd=(i,e,o)=>{let t={...Gd,cacheHint:o.cacheKey};return{...t,get:()=>Md(i,t,e,o.axis)}},Ud=(i,e)=>{if(!i||i.length!==2)throw new Error("Gather requires 2 inputs.");let o=i[0].dims.length;if(o<1)throw new Error("Invalid input shape.");if(e<-o||e>o-1)throw new Error("Invalid axis.");if(re.indexOf(i[0].type)===-1)throw new Error("Invaid input type.");if(i[1].type!=="int32"&&i[1].type!=="int16")throw new Error("Invaid input type.")}});var Ao,Gu,Mu,Vu,zd,Wd,Hd,Uu=x(()=>{"use strict";at();U();R();Ao=(i,e,o)=>(Hd(e,o),[i.run(zd(e,o),e)]),Gu=(i,e)=>{let o=i.attributes.getInt("transA",0)!==0,t=i.attributes.getInt("transB",0)!==0,r=i.attributes.getFloat("alpha",1),n=i.attributes.getFloat("beta",1);return F({transA:o,transB:t,alpha:r,beta:n,isOptionalC:e})},Mu=i=>Gu(i,!1),Vu=i=>Gu(i,!0),zd=(i,e)=>{let o={name:"Gemm",inputNames:i.length===3?["A","B","C"]:["A","B"],inputTypes:i.length===3?[0,0,0]:[0,0],key:e.cacheKey};return{...o,get:()=>Wd(o,i,e)}},Wd=(i,e,o)=>{let t=e[0].dims.slice(),r=e[1].dims.slice(),[n,s]=Cr.getShapeOfGemmResult(t,o.transA,r,o.transB,e.length===3?e[2].dims:void 0),a=[n,s];if(!a)throw new Error("Can't use gemm on the given tensors");let u=t[t.length-1],l="";o.transA&&(u=t[0]),o.transA&&o.transB?l="value += _A_T(a) * _B_T(b);":o.transA&&!o.transB?l="value += _A_T(a) * _B(b);":!o.transA&&o.transB?l="value += _A(a) * _B_T(b);":!o.transA&&!o.transB&&(l="value += _A(a) * _B(b);");let f=a.length,c=e.length===3?`int c[${e[2].dims.length}];`:"",d=e.length===3?"bcastIndices_C(indices, c);":"",m=e.length===3?"value += beta * _C(c);":"",y=`
float process(int indices[${f}]) {
int a[${f}];
int b[${f}];
${c}
copyVec(indices, a);
copyVec(indices, b);
${d}
float value = 0.0;
for (int k=0; k<${u}; ++k) {
a[${f-1}] = k;
b[${f-2}] = k;
${l}
}
value = value * alpha;
${m}
return value;
}`;return{...i,output:{dims:a,type:e[0].type,textureType:0},variables:[{name:"alpha",type:"float",data:o.alpha},{name:"beta",type:"float",data:o.beta}],shaderSource:y}},Hd=(i,e)=>{if(!i)throw new Error("Input is missing");if(e.isOptionalC&&(i.length<2||i.length>3))throw new Error("Invaid input shape.");if(!e.isOptionalC&&i.length!==3)throw new Error("Gemm requires 3 inputs");if(i.length===3&&i[2].dims.length!==1&&i[2].dims.length!==2)throw new Error("Invalid input shape of C");if(i[0].type!=="float32"&&i[0].type!=="float64"||i[1].type!=="float32"&&i[1].type!=="float64"||i.length===3&&i[2].type!=="float32"&&i[2].type!=="float64")throw new Error("Invalid input type.");if(i[0].type!==i[1].type||i.length===3&&i[0].type!==i[2].type)throw new Error("Input types are mismatched")}});var zu,Wu,qd,jd,Xd,Kd,Jd,Hu=x(()=>{"use strict";at();R();zu=(i,e,o)=>(Jd(e),[i.run(Xd(i,e,o),e)]),Wu=i=>{let e=i.attributes.getFloat("scale"),o=i.attributes.getFloats("bias");return F({scale:e,bias:o})},qd={name:"ImageScaler",inputNames:["X"],inputTypes:[0]},jd=(i,e,o,t)=>{let r=o[0].dims.slice(),n=r.length,a=`
${Kd(t.bias.length)}
float process(int indices[${n}]) {
return _X(indices) * scale + getBias(bias, indices[1]);
}`;return{...e,output:{dims:r,type:o[0].type,textureType:0},variables:[{name:"bias",type:"float",arrayLength:t.bias.length,data:t.bias},{name:"scale",type:"float",data:t.scale}],shaderSource:a}},Xd=(i,e,o)=>{let t={...qd,cacheHint:o.cacheKey};return{...t,get:()=>jd(i,t,e,o)}},Kd=i=>{let e=[`float getBias(float bias[${i}], int channel) {`];for(let o=0;o<i;++o)o===0?e.push(` if (channel == ${o}) { return bias[${o}]; }`):o===i-1?e.push(` else { return bias[${o}]; }`):e.push(` else if (channel == ${o}) { return bias[${o}]; }`);return e.push(" }"),e.join(`
`)},Jd=i=>{if(!i||i.length!==1)throw new Error("ImageScaler requires 1 input.");if(i[0].dims.length!==4)throw new Error("Invalid input shape.");if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("Invalid input type.")}});var ju,Xu,qu,Zd,Yd,Qd,th,eh,rh,Ku=x(()=>{"use strict";X();R();ju=(i,e,o)=>{rh(e);let t=i.run(Yd(e[0]),e);return[i.run(eh(i,e[0],o,t.dims),[e[0],t,e[1],e[2]])]},Xu=i=>i.attributes.getFloat("epsilon",1e-5),qu={name:"InstanceNormalization_MeanAndVariance",inputNames:["X"],inputTypes:[0]},Zd=(i,e)=>{let o=e.dims.slice(),t=o[1],r=o[2]*o[3],n=[o[0],t],s=`
vec4 process(int[2] indices) {
vec4 v = vec4(0.0);
int a[4];
a[0] = indices[0];
a[1] = indices[1];
float temp = 0.0;
for(int a2=0; a2<${o[2]}; a2++) {
a[2] = a2;
for(int a3=0; a3<${o[3]}; a3++) {
a[3] = a3;
float x = _X(a);
temp += x;
}
}
float mean = temp / float(${r});
temp = 0.0;
for(int a2=0; a2<${o[2]}; a2++) {
a[2] = a2;
for(int a3=0; a3<${o[3]}; a3++) {
a[3] = a3;
float x = _X(a);
temp += (x - mean) * (x - mean);
}
}
v.r = mean;
v.g = temp / float(${r});
return v;
}`;return{...i,output:{dims:n,type:e.type,textureType:4},shaderSource:s}},Yd=i=>({...qu,get:()=>Zd(qu,i)}),Qd={name:"InstanceNormalization_ComputeOutput",inputNames:["X","MeanAndVariance","Scale","B"],inputTypes:[0,4,0,0]},th=(i,e,o,t,r)=>{let n=D(i.session.backend.glContext.version),[s,a]=i.calculateTextureWidthAndHeight(r,4),[u,l]=[s/4,a],f=`
vec4 get_MeanAndVariance(int[2] mv) {
int offset = indicesToOffset_MeanAndVariance(mv);
vec2 coords = offsetToCoords(offset, ${u}, ${l});
return ${n.texture2D}(MeanAndVariance, coords);
}
float process(int[4] indices) {
int mv[2];
mv[0] = indices[0];
mv[1] = indices[1];
vec4 mean_and_variance = get_MeanAndVariance(mv);
float mean = mean_and_variance.r;
float variance = mean_and_variance.g;
int sb[1];
sb[0] = indices[1];
float scale = _Scale(sb);
float b = _B(sb);
return scale * (_X(indices) - mean) / sqrt(variance + epsilon) + b;
}`;return{...e,output:{dims:o.dims,type:o.type,textureType:0},variables:[{name:"epsilon",type:"float",data:t}],shaderSource:f}},eh=(i,e,o,t)=>{let r={...Qd,cacheHint:`${o}`};return{...r,get:()=>th(i,r,e,o,t)}},rh=i=>{if(!i||i.length!==3)throw new Error("InstanceNormalization requires 3 inputs.");let e=i[0],o=i[1],t=i[2];if(e.dims.length<3||o.dims.length!==1||t.dims.length!==1)throw new Error("Invalid input shape.");if(o.dims[0]!==e.dims[1]||t.dims[0]!==e.dims[1])throw new Error("Input shapes are mismatched.");if(e.type!=="float32"&&e.type!=="float64"||o.type!=="float32"&&o.type!=="float64"||t.type!=="float32"&&t.type!=="float64")throw new Error("Invalid input type.");if(i[0].dims.length!==4)throw new Error("Only support 4-D input shape.")}});function nh(i,e){let o=i[0].dims[1],t=i[0].dims.length,r=-Math.floor((e.size-1)/2),n=Math.ceil((e.size-1)/2),s=`float(${e.alpha}) / float(${e.size})`,a=`float(${e.bias})`,u=`float(${e.beta})`,l=`
float process(int indices[${t}]) {
int c = indices[1];
float x = _X(indices);
float square_sum = 0.0;
for (int i = ${r}; i <= ${n}; i++) {
int idx = c + i;
if (c >= 0 && c < ${o}) {
indices[1] = idx;
float j = _X(indices);
square_sum += j * j;
}
}
return x / pow(${a} + ${s} * square_sum, ${u});
}`;return{...Yu,cacheHint:e.cacheKey,output:{dims:i[0].dims,type:i[0].type,textureType:0},shaderSource:l}}function oh(i,e){return{...Yu,cacheHint:e.cacheKey,get:()=>nh(i,e)}}var Ju,Zu,Yu,ih,Qu=x(()=>{"use strict";at();R();Ju=(i,e,o)=>(ih(e),[i.run(oh(e,o),e)]),Zu=i=>{let e=i.attributes.getFloat("alpha",1e-4),o=i.attributes.getFloat("beta",.75),t=i.attributes.getFloat("bias",1),r=i.attributes.getInt("size");return F({alpha:e,beta:o,bias:t,size:r})},Yu={name:"LRN",inputNames:["X"],inputTypes:[0]};ih=i=>{if(!i||i.length!==1)throw new Error("LRN requires 1 input.");if(i[0].dims.length!==4)throw new Error('currently only support LRN for input with "NCHW" format');if(i[0].type!=="float32")throw new Error("input should be float type")}});var ah,Po,tl,el,rl,sh,uh,lh,fh,ch,ph,dh,hh,nl=x(()=>{"use strict";at();U();X();R();ah={name:"Pad",inputNames:["A"],inputTypes:[0]},Po=(i,e,o)=>(lh(e),[i.run({...ah,cacheHint:o.cacheKey,get:()=>uh(i,e[0],o)},e)]),tl=i=>{let e=i.attributes.getString("mode","constant"),o=i.attributes.getFloat("value",0),t=i.attributes.getInts("pads");return F({mode:e,value:o,pads:t})},el=(i,e,o)=>{fh(e);let t=sh(i,e,o);return Po(i,[e[0]],t)},rl=i=>i.attributes.getString("mode","constant"),sh=(i,e,o)=>{if(!i.session.isInitializer(e[1].dataId)||e.length>=3&&!i.session.isInitializer(e[2].dataId))throw new Error("dynamic pad attributes are not allowed");let t=Array.from(e[1].integerData),r=e.length>=3?e[2].floatData[0]:0;return F({mode:o,pads:t,value:r})},uh=(i,e,o)=>{let t=A.padShape(e.dims.slice(),o.pads),r=t.length,s=`
${ch(i,e,o)}
float process(int[${r}] indices) {
return padA(indices);
}`;return{name:"Pad",inputNames:["A"],inputTypes:[0],output:{dims:t,type:e.type,textureType:0},shaderSource:s}},lh=i=>{if(!i||i.length!==1)throw new Error("Pad requires 1 input");if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("Invalid input type.")},fh=i=>{if(!i||i.length!==2&&i.length!==3)throw new Error("Pad requires 2 or 3 inputs");if(i[1].type!=="int32")throw new Error("Invalid input type.");if(i.length>=3&&i[2].type==="string")throw new Error("Invalid input type.")},ch=(i,e,o)=>{let t=D(i.session.backend.glContext.version),[r,n]=i.calculateTextureWidthAndHeight(e.dims,0),s=A.computeStrides(e.dims);switch(o.mode){case"constant":return ph(t,e.dims,s,r,n,o.pads,o.value);case"reflect":return dh(t,e.dims,s,r,n,o.pads);case"edge":return hh(t,e.dims,s,r,n,o.pads);default:throw new Error("Invalid mode")}},ph=(i,e,o,t,r,n,s)=>{let a=e.length,u="";for(let l=a-1;l>=0;--l)u+=`
k = m[${l}] - ${n[l]};
if (k < 0) return constant;
if (k >= ${e[l]}) return constant;
offset += k * ${o[l]};
`;return`
float padA(int m[${a}]) {
const float constant = float(${s});
int offset = 0;
int k = 0;
${u}
vec2 coords = offsetToCoords(offset, ${t}, ${r});
float value = getColorAsFloat(${i.texture2D}(A, coords));
return value;
}
`},dh=(i,e,o,t,r,n)=>{let s=e.length,a="";for(let u=s-1;u>=0;--u)a+=`
k = m[${u}] - ${n[u]};
if (k < 0) { k = -k; }
{
const int _2n_1 = ${2*(e[u]-1)};
k = int( mod( float(k), float(_2n_1) ) ) ;
if(k >= ${e[u]}) { k = _2n_1 - k; }
}
offset += k * ${o[u]};
`;return`
float padA(int m[${s}]) {
int offset = 0;
int k = 0;
${a}
vec2 coords = offsetToCoords(offset, ${t}, ${r});
float value = getColorAsFloat(${i.texture2D}(A, coords));
return value;
}
`},hh=(i,e,o,t,r,n)=>{let s=e.length,a="";for(let u=s-1;u>=0;--u)a+=`
k = m[${u}] - ${n[u]};
if (k < 0) k = 0;
if (k >= ${e[u]}) k = ${e[u]-1};
offset += k * ${o[u]};
`;return`
float padA(int m[${s}]) {
int offset = 0;
int k = 0;
${a}
vec2 coords = offsetToCoords(offset, ${t}, ${r});
float value = getColorAsFloat(${i.texture2D}(A, coords));
return value;
}
`}});var il,al,sl,ul,ll,fl,cl,pl,dl,mh,ol,hl,Jr,ml,Kr,bh,bl=x(()=>{"use strict";at();U();R();il=(i,e,o)=>{Jr(e);let t={name:"AveragePool",inputNames:["X"],inputTypes:[0],cacheHint:o.cacheKey};return[i.run({...t,get:()=>sl(e,t,!1,o)},e)]},al=i=>{let e=i.attributes.getString("auto_pad","NOTSET"),o=i.attributes.getInt("ceil_mode",0),t=i.attributes.getInt("count_include_pad",0)!==0,r=i.attributes.getInts("kernel_shape"),n=i.attributes.getInts("strides",[]),s=i.attributes.getInts("pads",[]);if(o!==0)throw new Error("using ceil() in shape computation is not yet supported for AveragePool");return F({autoPad:e,ceilMode:o,countIncludePad:t,kernelShape:r,strides:n,pads:s})},sl=(i,e,o,t)=>{let[r,n]=dl(i,t,o),s=A.size(r.kernelShape),a="value += _X(x);",u="";r.countIncludePad?u+=`value /= float(${s});`:u+=`value /= float(${s} - pad);`;let f=`
${ml(i[0].dims,r,a,u,"0.0")}
`;return{...e,output:{dims:n,type:i[0].type,textureType:0},shaderSource:f}},ul=(i,e,o)=>{Jr(e);let t={name:"GlobalAveragePool",inputNames:["X"],inputTypes:[0],cacheHint:`${o.countIncludePad}`};return[i.run({...t,get:()=>sl(e,t,!0,o)},e)]},ll=i=>{let e=i.attributes.getInt("count_include_pad",0)!==0;return F({autoPad:"",ceilMode:0,countIncludePad:e,kernelShape:[],strides:[],pads:[]})},fl=(i,e,o)=>{Jr(e);let t={name:"MaxPool",inputNames:["X"],inputTypes:[0],cacheHint:o.cacheKey};return[i.run({...t,get:()=>pl(e,t,!1,o)},e)]},cl=i=>{let e=i.attributes.getString("auto_pad","NOTSET"),o=i.attributes.getInt("ceil_mode",0),t=i.attributes.getInts("kernel_shape"),r=i.attributes.getInts("strides",[]),n=i.attributes.getInts("pads",[]),s=i.attributes.getInt("storage_order",0),a=i.attributes.getInts("dilations",[]);if(s!==0)throw new Error("column major storage order is not yet supported for MaxPool");if(o!==0)throw new Error("using ceil() in shape computation is not yet supported for MaxPool");return F({autoPad:e,ceilMode:o,countIncludePad:!1,kernelShape:t,strides:r,pads:n,storageOrder:s,dilations:a})},pl=(i,e,o,t)=>{let[r,n]=dl(i,t,o),s=`
value = max(_X(x), value);
`,a="",l=`
${ml(i[0].dims,r,s,a,"-1e5")}
`;return{...e,output:{dims:n,type:i[0].type,textureType:0},shaderSource:l}},dl=(i,e,o)=>{let t=i[0].dims.slice(),r=Object.hasOwnProperty.call(e,"dilations"),n=e.kernelShape.slice(),s=e.strides.slice(),a=r?e.dilations.slice():[],u=e.pads.slice();he.adjustPoolAttributes(o,t,n,s,a,u);let l=he.computePoolOutputShape(o,t,s,a,n,u,e.autoPad),f=Object.assign({},e);return r?Object.assign(f,{kernelShape:n,strides:s,pads:u,dilations:a,cacheKey:e.cacheKey}):Object.assign(f,{kernelShape:n,strides:s,pads:u,cacheKey:e.cacheKey}),[f,l]},mh={autoPad:"",ceilMode:0,countIncludePad:!1,kernelShape:[],strides:[],pads:[],storageOrder:0,dilations:[],cacheKey:""},ol={name:"GlobalMaxPool",inputNames:["X"],inputTypes:[0]},hl=(i,e)=>(Jr(e),[i.run({...ol,get:()=>pl(e,ol,!0,mh)},e)]),Jr=i=>{if(!i||i.length!==1)throw new Error("Pool ops requires 1 input.");if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("Invalid input type.")},ml=(i,e,o,t,r)=>{let n=i.length;if(e.kernelShape.length<=2){let s=e.kernelShape[e.kernelShape.length-1],a=e.strides[e.strides.length-1],u=e.pads[e.pads.length/2-1],l=e.pads[e.pads.length-1],f=i[n-1],c="",d="",m="";if(u+l!==0?c=`
for (int i = 0; i < ${s}; i++) {
x[${n} - 1] = indices[${n} - 1] * ${a} - ${u} + i;
if (x[${n} - 1] < 0 || x[${n} - 1] >= ${f}) {
pad++;
continue;
}
${o}
}`:c=`
for (int i = 0; i < ${s}; i++) {
x[${n} - 1] = indices[${n} - 1] * ${a} - ${u} + i;
${o}
}`,e.kernelShape.length===2){let g=e.kernelShape[e.kernelShape.length-2],T=e.strides[e.strides.length-2],_=e.pads[e.pads.length/2-2],w=e.pads[e.pads.length-2],v=i[n-2];_+w!==0?d=`
for (int j = 0; j < ${g}; j++) {
x[${n} - 2] = indices[${n} - 2] * ${T} - ${_} + j;
if (x[${n} - 2] < 0 || x[${n} - 2] >= ${v}) {
pad+= ${s};
continue;
}
`:d=`
for (int j = 0; j < ${g}; j++) {
x[${n} - 2] = indices[${n} - 2] * ${T} - ${_} + j;
`,m=`
}
`}return`
float process(int indices[${n}]) {
int x[${n}];
copyVec(indices, x);
float value = ${r};
int pad = 0;
${d}
${c}
${m}
${t}
return value;
}
`}else{let s=A.size(e.kernelShape),a=A.computeStrides(e.kernelShape),u=a.length,l=e.pads.length,f=bh(u),c=Kr(i,"inputDims"),d=Kr(e.pads,"pads"),m=Kr(a,"kernelStrides"),y=Kr(e.strides,"strides"),g=e.pads.reduce((w,v)=>w+v),T="";return g?T=`
if (x[j] >= inputDims[j] || x[j] < 0) {
pad++;
isPad = true;
break;
}
}
if (!isPad) {
${o}
}`:T=`
}
${o}
`,`
${f}
float process(int indices[${n}]) {
int x[${n}];
copyVec(indices, x);
int offset[${u}];
int pads[${l}];
int inputDims[${n}];
int kernelStrides[${u}];
int strides[${u}];
${d}
${c}
${y}
${m}
float value = ${r};
int pad = 0;
bool isPad = false;
for (int i = 0; i < ${s}; i++) {
offsetToIndices(i, kernelStrides, offset);
isPad = false;
for (int j = ${n} - ${u}; j < ${n}; j++) {
x[j] = indices[j] * strides[j - ${n} + ${u}]
+ offset[j - ${n} + ${u}] - pads[j - 2];
${T}
}
${t}
return value;
}
`}},Kr=(i,e)=>{let o="";for(let t=0;t<i.length;t++)o+=`
${e}[${t}] = ${i[t]};
`;return o},bh=i=>`
void offsetToIndices(int offset, int[${i}] strides, out int[${i}] indices) {
if (${i} == 0) {
return;
}
for (int i = 0; i < ${i} - 1; ++i) {
indices[i] = offset / strides[i];
offset -= indices[i] * strides[i];
}
indices[${i} - 1] = offset;
}`});var we,ne,yh,gh,yl,gl,xl,Tl,wl,vl,Il,_l=x(()=>{"use strict";at();nr();U();R();we=(i,e,o,t,r)=>{gh(e);let n={name:t,inputNames:["A"],inputTypes:[0]};return[i.run({...n,cacheHint:o.cacheKey,get:()=>yh(i,e,o,t,r,n)},e)]},ne=i=>{let e=i.attributes.getInts("axes",[]),o=i.attributes.getInt("keepdims",1)===1;return F({axes:e,keepDims:o})},yh=(i,e,o,t,r,n)=>{let s=[],a=e[0].dims.length||1,u=[],l=A.normalizeAxes(o.axes,e[0].dims.length),f=r(e,l),c=f[1];for(let y=0;y<e[0].dims.length;y++)l.indexOf(y)>=0||l.length===0?(o.keepDims&&s.push(1),c=`
for(int j${y} = 0; j${y} < ${e[0].dims[y]}; j${y}++) {
inputIdx[${y}] = j${y};
${c}
}`):(u.push(`inputIdx[${y}] = outputIdx[${s.length}];`),s.push(e[0].dims[y]));let m=`
float process(int outputIdx[${s.length||1}]) {
float value; // final result
int inputIdx[${a}]; // addressing input data
${u.join(`
`)}
${f[0]} // init ops for reduce max/min
${c}
${f[2]} // final computation for reduce mean
return value;
}`;return{...n,output:{dims:s,type:e[0].type,textureType:0},shaderSource:m}},gh=i=>{if(!i||i.length!==1)throw new Error("Reduce op requires 1 input.");if(re.indexOf(i[0].type)===-1)throw new Error("Invalid input type.")},yl=(i,e,o)=>we(i,e,o,"ReduceSum",()=>["value = 0.0;","value += _A(inputIdx);",""]),gl=(i,e,o)=>we(i,e,o,"ReduceMean",(r,n)=>{let s=1;for(let a=0;a<r[0].dims.length;a++)(n.indexOf(a)>=0||n.length===0)&&(s*=r[0].dims[a]);return["value = 0.0;","value += _A(inputIdx);",`value /= ${s}.;`]}),xl=(i,e,o)=>we(i,e,o,"ReduceMax",(r,n)=>{let s=[];for(let a=0;a<r[0].dims.length;a++)(n.indexOf(a)>=0||n.length===0)&&s.push(`inputIdx[${a}] = 0;`);return[`${s.join(`
`)}
value = _A(inputIdx);`,"value = max(value, _A(inputIdx));",""]}),Tl=(i,e,o)=>we(i,e,o,"ReduceMin",(r,n)=>{let s=[];for(let a=0;a<r[0].dims.length;a++)(n.indexOf(a)>=0||n.length===0)&&s.push(`inputIdx[${a}] = 0;`);return[`${s.join(`
`)}
value = _A(inputIdx);`,"value = min(value, _A(inputIdx));",""]}),wl=(i,e,o)=>we(i,e,o,"ReduceProd",()=>["value = 1.0;","value *= _A(inputIdx);",""]),vl=(i,e,o)=>we(i,e,o,"ReduceLogSum",()=>["value = 0.0;","value += _A(inputIdx);","value = log(value);"]),Il=(i,e,o)=>we(i,e,o,"ReduceLogSumSquare",()=>["float t; value = 0.0;","t = _A(inputIdx); value += t * t;",""])});var Sl,Ol=x(()=>{"use strict";U();Sl=(i,e)=>{let o=A.calculateReshapedDims(e[0].dims,e[1].integerData);return i.session.pack?[i.reshapePacked(e[0],o)]:[i.reshapeUnpacked(e[0],o)]}});var Al,Eo,Pl,El,or,xh,Do,Zr,Lo=x(()=>{"use strict";at();X();R();Al={name:"Upsample",inputNames:["X"],inputTypes:[0]},Eo=(i,e,o)=>(Do(e,o),[i.run({...Al,cacheHint:o.cacheKey,get:()=>xh(i,e,o)},e)]),Pl=i=>or(i,7),El=i=>or(i,9),or=(i,e)=>{let o=e>=10,t=i.attributes.getString("mode","nearest");if(t!=="nearest"&&t!=="linear"&&(e<11||t!=="cubic"))throw new Error(`unrecognized mode: ${t}`);let r=[];e<9&&(r=i.attributes.getFloats("scales"),Zr(r,t,o));let n=i.attributes.getFloat("extrapolation_value",0),s=e>10?i.attributes.getString("coordinate_transformation_mode","half_pixel"):"asymmetric";if(["asymmetric","pytorch_half_pixel","tf_half_pixel_for_nn","align_corners","tf_crop_and_resize","half_pixel"].indexOf(s)===-1)throw new Error(`coordinate_transform_mode '${s}' is not supported`);let a=s==="tf_crop_and_resize",u=a,l=t==="nearest"&&e>=11?i.attributes.getString("nearest_mode","round_prefer_floor"):"";if(["round_prefer_floor","round_prefer_ceil","floor","ceil",""].indexOf(l)===-1)throw new Error(`nearest_mode '${l}' is not supported`);let f=i.attributes.getFloat("cubic_coeff_a",-.75),c=i.attributes.getInt("exclude_outside",0)!==0;if(c&&t!=="cubic")throw new Error("exclude_outside can be set to 1 only when mode is CUBIC.");let d=e<11?!0:t==="nearest"&&s==="asymmetric"&&l==="floor",m=0,y=0,g=0;return e>10?i.inputs.length>2?(m=1,y=2,g=3):(y=1,g=2):e===9&&(y=1),F({opset:e,isResize:o,mode:t,scales:r,extrapolationValue:n,coordinateTransformMode:s,useExtrapolation:u,needRoiInput:a,nearestMode:l,cubicCoefficientA:f,excludeOutside:c,useNearest2xOptimization:d,roiInputIdx:m,scalesInputIdx:y,sizesInputIdx:g})},xh=(i,e,o)=>{let t=D(i.session.backend.glContext.version),[r,n]=i.calculateTextureWidthAndHeight(e[0].dims,0),s=e[0].dims.map((g,T)=>Math.floor(g*o.scales[T])),[a,u]=i.calculateTextureWidthAndHeight(s,0),l=s.length,f=new Array(l),c=new Array(l),d=`
int output_pitches[${l}];
int input_pitches[${l}];
`;for(let g=l-1;g>=0;g--)f[g]=g===l-1?1:f[g+1]*s[g+1],c[g]=g===l-1?1:c[g+1]*e[0].dims[g+1],d+=`
output_pitches[${g}] = ${f[g]};
input_pitches[${g}] = ${c[g]};
`;let m=`
float getInputFloat(int index) {
vec2 coords = offsetToCoords(index, ${r}, ${n});
float value = getColorAsFloat(${t.texture2D}(X, coords));
return value;
}
`,y=o.mode==="nearest"?`
${m}
float process(int indices[${l}]) {
int input_index = 0;
int output_index = coordsToOffset(TexCoords, ${a}, ${u});
${d}
int d, m;
for (int dim = 0; dim < ${l}; ++dim) {
d = output_index / output_pitches[dim];
m = output_index - d * output_pitches[dim];
output_index = m;
if (scales[dim] != 1 && d > 0) {
int d2 = d / scales[dim];
m = d - d2 * scales[dim];
d = d2;
}
input_index += input_pitches[dim] * d;
}
return getInputFloat(input_index);
}`:l===4?`
${m}
float process(int indices[4]) {
int input_index = 0;
int output_index = coordsToOffset(TexCoords, ${a}, ${u});
${d}
int m;
int index_of_dim0, index_of_dim1, index_of_dim2, index_of_dim3;
index_of_dim0 = output_index / output_pitches[0];
m = output_index - index_of_dim0 * output_pitches[0];
index_of_dim1 = m / output_pitches[1];
m = m - index_of_dim1 * output_pitches[1];
index_of_dim2 = m / output_pitches[2];
m = m - index_of_dim2 * output_pitches[2];
index_of_dim3 = m;
int index_of_input_dim2, index_of_input_dim3, x_offset, y_offset;
index_of_input_dim2 = index_of_dim2 / scales[2];
y_offset = index_of_dim2 - index_of_input_dim2 * scales[2];
index_of_input_dim3 = index_of_dim3 / scales[3];
x_offset = index_of_dim3 - index_of_input_dim3 * scales[3];
input_index = index_of_dim0 * input_pitches[0] +
index_of_dim1 * input_pitches[1] +
index_of_input_dim2 * input_pitches[2] +
index_of_input_dim3;
float x00 = getInputFloat(input_index);
float x10, x01, x11;
bool end_of_dim2 = false;
if (index_of_input_dim2 == (${e[0].dims[2]} - 1)) {
// It's the end in dimension 2
x01 = x00;
end_of_dim2 = true;
} else {
x01 = getInputFloat(input_index + input_pitches[2]);
}
if (index_of_input_dim3 == (input_pitches[2] - 1)) {
// It's the end in dimension 3
x10 = x00;
x11 = x01;
}
else {
x10 = getInputFloat(input_index + 1);
x11 = end_of_dim2 ? x10 : getInputFloat(input_index + input_pitches[2] + 1);
}
float y0 = x00 + float(y_offset) * (x01 - x00) / float(scales[2]);
float y1 = x10 + float(y_offset) * (x11 - x10) / float(scales[2]);
return y0 + float(x_offset) * (y1 - y0) / float(scales[3]);
}`:`
${m}
float process(int indices[2]) {
int input_index = 0;
int output_index = coordsToOffset(TexCoords, ${a}, ${u});
${d}
int m;
int index_of_dim0, index_of_dim1;
index_of_dim0 = output_index / output_pitches[0];
m = output_index - index_of_dim0 * output_pitches[0];
index_of_dim1 = m;
int index_of_input_dim0, index_of_input_dim1, x_offset, y_offset;
index_of_input_dim0 = index_of_dim0 / scales[0];
y_offset = index_of_dim0 - index_of_input_dim0 * scales[0];
index_of_input_dim1 = index_of_dim1 / scales[1];
x_offset = index_of_dim1 - index_of_input_dim1 * scales[1];
input_index = index_of_input_dim0 * input_pitches[0] + index_of_input_dim1;
float x00 = getInputFloat(input_index);
float x10, x01, x11;
bool end_of_dim0 = false;
if (index_of_input_dim0 == (${e[0].dims[0]} - 1)) {
// It's the end in dimension 0
x01 = x00;
end_of_dim0 = true;
} else {
x01 = getInputFloat(input_index + input_pitches[0]);
}
if (index_of_input_dim1 == (input_pitches[0] - 1)) {
// It's the end in dimension 1
x10 = x00;
x11 = x01;
}
else {
x10 = getInputFloat(input_index + 1);
x11 = end_of_dim0 ? x10 : getInputFloat(input_index + input_pitches[0] + 1);
}
float y0 = x00 + float(y_offset) * (x01 - x00) / float(scales[0]);
float y1 = x10 + float(y_offset) * (x11 - x10) / float(scales[0]);
return y0 + float(x_offset) * (y1 - y0) / float(scales[1]);
}`;return{...Al,output:{dims:s,type:e[0].type,textureType:0},shaderSource:y,variables:[{name:"scales",type:"int",arrayLength:o.scales.length,data:o.scales.map(g=>Math.ceil(g))}]}},Do=(i,e)=>{if(!i||e.opset<9&&i.length!==1||e.opset>=9&&e.opset<11&&i.length!==2||e.opset>=11&&i.length<2)throw new Error("invalid inputs.");if(e.scales.length>0&&i[0].dims.length!==e.scales.length)throw new Error("Invalid input shape.");if(i[0].type==="string")throw new Error("Invalid input tensor types.")},Zr=(i,e,o)=>{if(o){for(let t of i)if(t<=0)throw new Error("Scale value should be greater than 0.")}else for(let t of i)if(t<1)throw new Error("Scale value should be greater than or equal to 1.");if((e==="linear"||e==="cubic")&&i.length!==2&&(i.length!==4||i[0]!==1||i[1]!==1))throw new Error(`'Linear' mode and 'Cubic' mode only support 2-D inputs ('Bilinear', 'Bicubic') or 4-D inputs with the corresponding outermost 2 scale values being 1 in the ${o?"Resize":"Upsample"} opeartor.`)}});var $o,ko,Dl,Ll,Th,wh,vh,Ih,$l=x(()=>{"use strict";X();R();Vt();ge();Lo();$o={name:"Resize",inputNames:["A"],inputTypes:[2]},ko=(i,e,o)=>(Do(e,o),[i.run({...$o,cacheHint:o.cacheKey,get:()=>Th(i,e,o)},e)]),Dl=i=>or(i,10),Ll=i=>or(i,11),Th=(i,e,o)=>{let t=D(i.session.backend.glContext.version),[r,n]=wh(e,o);if(r.every(v=>v===1)&&o.coordinateTransformMode!=="tf_crop_and_resize")return{...$o,output:{dims:n,type:e[0].type,textureType:2},hasMain:!0,shaderSource:`void main() {
vec4 v = ${t.texture2D}(X, TexCoords);
${t.output} = v;
}`};let a=n.length;if(a<2)throw new Error(`output dimension should be at least 2, but got ${a}`);let u=n[a-2],l=n[a-1],f=e[0].dims;if(a!==f.length)throw new Error(`output dimension should match input ${f.length}, but got ${a}`);let c=f[a-2],d=f[a-1],m=r[a-2],y=r[a-1],g="";if(o.mode!=="linear")throw new Error(`resize (packed) does not support mode: '${o.mode}'`);switch(o.coordinateTransformMode){case"asymmetric":g=`
vec4 getSourceFracIndex(ivec4 coords) {
return vec4(coords) / scaleWHWH;
}
`;break;case"half_pixel":g=`
vec4 getSourceFracIndex(ivec4 coords) {
return (vec4(coords) + 0.5) / scaleWHWH - 0.5;
}
`;break;case"pytorch_half_pixel":g=`
vec4 getSourceFracIndex(ivec4 coords) {
vec4 fcoords = vec4(coords);
return vec4(
${l}.0 > 1.0 ? (fcoords.x + 0.5) / scaleWHWH.x - 0.5 : 0.0,
${u}.0 > 1.0 ? (fcoords.y + 0.5) / scaleWHWH.y - 0.5 : 0.0,
${l}.0 > 1.0 ? (fcoords.z + 0.5) / scaleWHWH.z - 0.5 : 0.0,
${u}.0 > 1.0 ? (fcoords.w + 0.5) / scaleWHWH.w - 0.5 : 0.0
);
}
`;break;case"align_corners":g=`
vec4 getSourceFracIndex(ivec4 coords) {
vec4 resized = vec4(${l}.0 - 1.0, ${u}.0 - 1.0, ${l}.0 - 1.0,
${u}.0 - 1.0);
vec4 original = vec4(${d}.0 - 1.0, ${c}.0 - 1.0, ${d}.0 - 1.0,
${c}.0 - 1.0);
vec4 new_scale = original / resized;
return vec4(coords) * new_scale;
}
`;break;default:throw new Error(`resize (packed) does not support coordinateTransformMode: '${o.coordinateTransformMode}'`)}let T=ht(a),_=Ut(),w=`
const vec2 inputWH = vec2(${c}.0, ${d}.0);
const vec4 scaleWHWH = vec4(float(${m}), float(${y}), float(${m}), float(${y}));
${_}
${g}
float getAValue(int x10, int r, int c, int d) {
return getChannel(getA(x10, r, c, d), vec2(c, d));
}
void main() {
${T} rc = getOutputCoords();
int batch = rc[0];
int depth = rc[1];
// retrieve the 4 coordinates that is used in the 4 packed output values.
ivec4 coords = ivec4(rc.wz, rc.w + 1, rc.z + 1);
// calculate the source index in fraction
vec4 sourceFrac = getSourceFracIndex(coords);
// get the lower and upper bound of the 4 values that will be packed into one texel.
ivec4 x00 = ivec4(max(sourceFrac.xy, vec2(0.0)), min(inputWH - 1.0, ceil(sourceFrac.xy)));
ivec4 x01 = ivec4(max(sourceFrac.xw, vec2(0.0)), min(inputWH - 1.0, ceil(sourceFrac.xw)));
ivec4 x10 = ivec4(max(sourceFrac.zy, vec2(0.0)), min(inputWH - 1.0, ceil(sourceFrac.zy)));
ivec4 x11 = ivec4(max(sourceFrac.zw, vec2(0.0)), min(inputWH - 1.0, ceil(sourceFrac.zw)));
bool hasNextRow = rc.w < ${u-1};
bool hasNextCol = rc.z < ${l-1};
// pack x00, x01, x10, x11's top-left corner into one vec4 structure
vec4 topLeft = vec4(
getAValue(batch, depth, x00.x, x00.y),
hasNextCol ? getAValue(batch, depth, x01.x, x01.y) : 0.0,
hasNextRow ? getAValue(batch, depth, x10.x, x10.y) : 0.0,
(hasNextRow && hasNextCol) ? getAValue(batch, depth, x11.x, x11.y) : 0.0);
// pack x00, x01, x10, x11's top-right corner into one vec4 structure
vec4 topRight = vec4(
getAValue(batch, depth, x00.x, x00.w),
hasNextCol ? getAValue(batch, depth, x01.x, x01.w) : 0.0,
hasNextRow ? getAValue(batch, depth, x10.x, x10.w) : 0.0,
(hasNextRow && hasNextCol) ? getAValue(batch, depth, x11.x, x11.w) : 0.0);
// pack x00, x01, x10, x11's bottom-left corner into one vec4 structure
vec4 bottomLeft = vec4(
getAValue(batch, depth, x00.z, x00.y),
hasNextCol ? getAValue(batch, depth, x01.z, x01.y) : 0.0,
hasNextRow ? getAValue(batch, depth, x10.z, x10.y) : 0.0,
(hasNextRow && hasNextCol) ? getAValue(batch, depth, x11.z, x11.y) : 0.0);
// pack x00, x01, x10, x11's bottom-right corner into one vec4 structure
vec4 bottomRight = vec4(
getAValue(batch, depth, x00.z, x00.w),
hasNextCol ? getAValue(batch, depth, x01.z, x01.w) : 0.0,
hasNextRow ? getAValue(batch, depth, x10.z, x10.w) : 0.0,
(hasNextRow && hasNextCol) ? getAValue(batch, depth, x11.z, x11.w) : 0.0);
// calculate the interpolation fraction on u and v direction
vec4 frac = vec4(sourceFrac) - floor(sourceFrac);
vec4 clampFrac = clamp(frac, vec4(0.0), vec4(1.0));
vec4 top = mix(topLeft, topRight, clampFrac.ywyw);
vec4 bottom = mix(bottomLeft, bottomRight, clampFrac.ywyw);
vec4 newValue = mix(top, bottom, clampFrac.xxzz);
${t.output} = vec4(newValue);
}
`;return{...$o,output:{dims:n,type:e[0].type,textureType:2},hasMain:!0,shaderSource:w}},wh=(i,e)=>{let t=i[0].dims,r=e.scales,n;if(r.length===0){let a=i[e.scalesInputIdx];if(a&&a.size!==0){if(i[e.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");r=vh(a,e.mode,e.isResize)}else{let u=i[e.sizesInputIdx];if(!u||u.size===0)throw new Error("Either scales or sizes MUST be provided as input.");n=Array.from(u.integerData),r=Ih(n,t,e.mode,e.isResize)}}else if(i[e.sizesInputIdx])throw new Error("Only one of scales or sizes must be provided as input.");let s=n||t.map((a,u)=>Math.floor(a*r[u]));return[r,s]},vh=(i,e,o)=>{let t=Array.from(i.floatData);return Zr(t,e,o),t},Ih=(i,e,o,t)=>{let r=e.length,n=new Array(r);for(let s=0,a=r;s<a;s++)if(e[s]===0){if(i[s]!==0)throw new Error("Input dim is zero but required output dim is non-zero.");n[s]=1}else n[s]=i[s]/e[s];return Zr(n,o,t),n}});var kl,_h,Bl=x(()=>{"use strict";ye();kl=(i,e)=>(_h(e),[new rt([e[0].dims.length],"int32",void 0,void 0,new Int32Array(e[0].dims))]),_h=i=>{if(!i||i.length!==1)throw new Error("Shape requires 1 input.")}});var Bo,Fl,Nl,Cl,Sh,Rl,Oh,Ah,Gl=x(()=>{"use strict";at();nr();U();R();Bo={name:"Slice",inputNames:["A"],inputTypes:[0]},Fl=(i,e,o)=>(Sh(e),[i.run({...Bo,cacheHint:o.cacheKey,get:()=>Cl(i,e[0],o)},e)]),Nl=i=>{let e=i.attributes.getInts("starts"),o=i.attributes.getInts("ends"),t=i.attributes.getInts("axes",[]);return F({starts:e,ends:o,axes:t})},Cl=(i,e,o)=>{let t=o.axes.length===0?e.dims.slice(0).map((c,d)=>d):o.axes,r=A.normalizeAxes(t,e.dims.length),n=o.starts.map((c,d)=>c>e.dims[r[d]]-1?e.dims[r[d]]:A.normalizeAxis(c,e.dims[r[d]])),s=o.ends.map((c,d)=>c>e.dims[r[d]]-1?e.dims[r[d]]:A.normalizeAxis(c,e.dims[r[d]])),a=e.dims.slice(),u=[];for(let c=0;c<r.length;c++)a[r[c]]=s[c]-n[c],n[c]>0&&u.push(`outputIdx[${r[c]}] += ${n[c]};`);let f=`
float process(int outputIdx[${a.length}]) {
${u.join(`
`)}
return _A(outputIdx);
}`;return{...Bo,output:{dims:a,type:e.type,textureType:0},shaderSource:f}},Sh=i=>{if(!i||i.length!==1)throw new Error("Slice requires 1 input.");if(re.indexOf(i[0].type)===-1)throw new Error("Invalid input type.")},Rl=(i,e)=>{Ah(e);let o=Oh(i,e);return[i.run({...Bo,cacheHint:o.cacheKey,get:()=>Cl(i,e[0],o)},[e[0]])]},Oh=(i,e)=>{if(!i.session.isInitializer(e[1].dataId)||!i.session.isInitializer(e[2].dataId)||e.length>=4&&!i.session.isInitializer(e[3].dataId)||e.length>=5&&!i.session.isInitializer(e[4].dataId))throw new Error("dynamic slice attributes are not allowed");if(e.length>=5&&e[4].integerData.some(s=>s!==1))throw new Error("currently non-1 steps is not supported for Slice");let o=Array.from(e[1].integerData),t=Array.from(e[2].integerData),r=e.length>=4?Array.from(e[3].integerData):[],n=`${r};${o};${t}`;return{starts:o,ends:t,axes:r,cacheKey:n}},Ah=i=>{if(!i||i.length<3||i.length>5)throw new Error("Invalid input number.");if(i[1].type!=="int32"||i[1].dims.length!==1)throw new Error("Invalid input type.");if(i[2].type!=="int32"||i[2].dims.length!==1)throw new Error("Invalid input type.");if(i.length>=4&&(i[3].type!=="int32"||i[3].dims.length!==1))throw new Error("Invalid input type.");if(i.length>=5&&(i[4].type!=="int32"||i[4].dims.length!==1))throw new Error("Invalid input type.")}});var Ml,Vl,Ul,zl,Wl,Hl,ql,jl,Ph,Eh,Dh,Xl,Kl=x(()=>{"use strict";at();U();X();R();Xr();Ml={name:"SoftmaxComputeMax",inputNames:["A"],inputTypes:[0]},Vl={name:"SoftmaxComputeScale",inputNames:["A","Max"],inputTypes:[0,0]},Ul={name:"SoftMax",inputNames:["A","Max","Norm"],inputTypes:[0,0,0]},zl=(i,e,o)=>{Xl(e);let t=e[0].dims.slice(),r=A.normalizeAxis(o.axis,t.length),n=A.sizeToDimension(t,r),s=A.sizeFromDimension(t,r);return jl(i,e,o,n,s)},Wl=i=>F({axis:i.attributes.getInt("axis",1)}),Hl=i=>F({axis:i.attributes.getInt("axis",-1)}),ql=(i,e,o)=>{Xl(e);let t=e[0].dims.slice(),r=A.normalizeAxis(o.axis,t.length),n=t.length,s=r!==n-1,a=[],u=[],l=[],f;s&&(u=Array.from({length:n}).map((y,g)=>g),u[r]=n-1,u[n-1]=r,u.map(y=>a.push(t[y])),f=F({perm:u}),l=Te(i,e,f));let c=s?A.sizeToDimension(a,n-1):A.sizeToDimension(t,n-1),d=s?A.sizeFromDimension(a,n-1):A.sizeFromDimension(t,n-1),m=jl(i,s?l:e,o,c,d);return s?Te(i,m,f):m},jl=(i,e,o,t,r)=>{let n=Ph(i,e[0],t,r,[t]),s=i.run({...Ml,cacheHint:o.cacheKey,get:()=>n},e),a=Eh(i,e[0],t,r,n.output.dims,[t]),u=i.run({...Vl,cacheHint:o.cacheKey,get:()=>a},[e[0],s]),l=Dh(i,e[0],t,r,n.output.dims,a.output.dims);return[i.run({...Ul,cacheHint:o.cacheKey,get:()=>l},[e[0],s,u])]},Ph=(i,e,o,t,r)=>{let[n,s]=i.calculateTextureWidthAndHeight(e.dims,0),a=r.length;if(o<1||t<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(r.length!==1)throw new Error("Dimensionality of the output should be 1");if(r[0]!==o)throw new Error("Shape of the output should be equal to logical row count");let u=D(i.session.backend.glContext.version),l=`
float process(int[${a}] indices) {
int logical_row_start_offset = indices[0] * ${t};
float max = getColorAsFloat(${u.texture2D}(A, offsetToCoords(logical_row_start_offset, ${n},
${s} )));
for(int i=1; i<${t}; ++i)
{
float current = getColorAsFloat(${u.texture2D}(A, offsetToCoords(logical_row_start_offset + i,
${n}, ${s})));
if(current > max)
max = current;
}
return max;
}`;return{...Ml,output:{dims:r,type:e.type,textureType:0},shaderSource:l}},Eh=(i,e,o,t,r,n)=>{let[s,a]=i.calculateTextureWidthAndHeight(e.dims,0),u=n.length;if(o<1||t<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(n.length!==1)throw new Error("Dimensionality of the output should be 1");if(n[0]!==o)throw new Error("Shape of the output should be equal to logical row count");if(r.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(r[0]!==o)throw new Error("Shape of the intermediate results should be equal to logical row count");let l=D(i.session.backend.glContext.version),f=`
float process(int[${u}] indices) {
int logical_row_start_offset = indices[0] * ${t};
float norm_factor = 0.0;
float max = _Max(indices);
for(int i=0; i<${t}; ++i)
{
norm_factor += exp(getColorAsFloat(${l.texture2D}(A, offsetToCoords(logical_row_start_offset + i,
${s}, ${a}))) - max);
}
return norm_factor;
}`;return{...Vl,output:{dims:n,type:e.type,textureType:0},shaderSource:f}},Dh=(i,e,o,t,r,n)=>{let[s,a]=i.calculateTextureWidthAndHeight(e.dims,0),u=e.dims.length;if(o<1||t<1)throw new Error("Logical row count N and feature count D must be greater than or equal to 1");if(r.length!==1||n.length!==1)throw new Error("Dimensionality of the intermediate results should be 1");if(r[0]!==o||n[0]!==o)throw new Error("Shape of the intermediate results should be equal to logical row count");let l=`
float process(int[${u}] indices) {
// get offset of current logical tensor index from the 2-D texture coordinates (TexCoords)
int offset = coordsToOffset(TexCoords, ${s}, ${a});
//determine the logical row for this index
int logical_row_index[1];
logical_row_index[0] = offset / ${t};
float norm_factor = _Norm(logical_row_index);
// avoid possible division by 0
// if norm_facor is 0, all elements are zero
// if so, return 0
if(norm_factor == 0.0)
return 0.0;
return exp(_A(indices) - _Max(logical_row_index)) / norm_factor;
}`;return{...Ul,output:{dims:e.dims,type:e.type,textureType:0},shaderSource:l}},Xl=i=>{if(!i||i.length!==1)throw new Error("Softmax requires 1 input.");if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("Invalid input type")}});var Jl,Zl,Yl,Lh,$h,kh,Ql=x(()=>{"use strict";at();U();R();Jl={name:"Split",inputNames:["A"],inputTypes:[0]},Zl=(i,e,o)=>{kh(e);let t=A.normalizeAxis(o.axis,e[0].dims.length),r=Lh(i,e,t,o),n=[];for(let s=0;s<r;++s)n.push(i.run({...Jl,cacheHint:`${o.cacheKey};${s}`,get:()=>$h(i,e[0],o,t,s)},e));return n},Yl=i=>{let e=i.attributes.getInt("axis",0),o=i.attributes.getInts("split",[]),t=i.outputs.length;return F({axis:e,split:o,numOutputs:t})},Lh=(i,e,o,t)=>{let[,r]=Ze.splitShape(e[0].dims,o,t.split,t.numOutputs);return r.length},$h=(i,e,o,t,r)=>{let[n,s]=Ze.splitShape(e.dims,t,o.split,o.numOutputs),a=s[r],u=n[r],f=`
float process(int indices[${u.length}]) {
indices[${t}] += ${a};
return _A(indices);
}
`;return{...Jl,cacheHint:`${o.cacheKey}:${r}`,output:{dims:u,type:e.type,textureType:0},shaderSource:f}},kh=i=>{if(!i||i.length!==1)throw new Error("Split requires one input.");if(i[0].type!=="int8"&&i[0].type!=="uint8"&&i[0].type!=="int16"&&i[0].type!=="uint16"&&i[0].type!=="int32"&&i[0].type!=="uint32"&&i[0].type!=="float32"&&i[0].type!=="float64"&&i[0].type!=="bool")throw new Error("Invalid input type.")}});var Fo,tf,ef,Bh,Fh,rf=x(()=>{"use strict";U();Fo=(i,e,o)=>{Bh(e);let t=A.squeezeShape(e[0].dims,o);return[i.reshapeUnpacked(e[0],t)]},tf=(i,e)=>(Fh(e),Fo(i,[e[0]],Array.from(e[1].integerData))),ef=i=>i.attributes.getInts("axes"),Bh=i=>{if(!i||i.length!==1)throw new Error("Squeeze requires 1 input.");if(i[0].type==="string")throw new Error("invalid input tensor types.")},Fh=i=>{if(!i||i.length!==2)throw new Error("Squeeze requires 2 inputs.");if(i[1].type!=="int32")throw new Error("Invalid input type.")}});var nf,Nh,Ch,of=x(()=>{"use strict";X();R();nf=(i,e)=>{Ch(e);let o={name:"Sum",inputNames:e.map((r,n)=>`X${n}`),inputTypes:new Array(e.length).fill(0)};return[i.run({...o,get:()=>Nh(i,e,o)},e)]},Nh=(i,e,o)=>{let t=D(i.session.backend.glContext.version),r=e[0].dims.slice(),s=`
void main() {
vec4 result = ${e.map((a,u)=>`${t.texture2D}(X${u},TexCoords)`).join(" + ")};
${t.output} = result;
}
`;return{...o,output:{dims:r,type:e[0].type,textureType:0},hasMain:!0,shaderSource:s}},Ch=i=>{if(!i||i.length===0)throw new Error("Sum requires inputs.");let e=i[0].dims.length;for(let o=1;o<i.length;o++){if(e!==i[o].dims.length)throw new Error("Input shapes are mismatched.");for(let t=0;t<e;t++)if(i[0].dims[t]!==i[o].dims[t])throw new Error("Input shapes are not matched.")}if(i[0].type!=="float32"&&i[0].type!=="float64")throw new Error("Invalid input type.");for(let o=1;o<i.length;o++)if(i[0].type!==i[o].type)throw new Error("Input types are not matched.")}});var af,Rh,Gh,sf=x(()=>{"use strict";nr();R();af=(i,e)=>{Gh(e);let o={name:"Tile",inputNames:["A"],inputTypes:[0]};return[i.run({...o,get:()=>Rh(i,e,o)},e)]},Rh=(i,e,o)=>{let t=e[0].dims.slice(),r=new Array(t.length),n=[];for(let u=0;u<t.length;u++)r[u]=t[u]*e[1].numberData[u],n.push(`inputIdx[${u}] = int(mod(float(outputIdx[${u}]), ${t[u]}.));`);let s=r.length,a=`
float process(int outputIdx[${s}]) {
int inputIdx[${s}];
${n.join(`
`)}
return _A(inputIdx);
}
`;return{...o,output:{dims:r,type:e[0].type,textureType:0},shaderSource:a}},Gh=i=>{if(!i||i.length!==2)throw new Error("Tile requires 2 input.");if(i[1].dims.length!==1)throw new Error("The second input shape must 1 dimension.");if(i[1].dims[0]!==i[0].dims.length)throw new Error("Invalid input shape.");if(re.indexOf(i[0].type)===-1)throw new Error("Invalid input type.");if(i[1].type!=="int32"&&i[1].type!=="int16")throw new Error("Invalid repeat type.")}});var No,uf,lf,Mh,Vh,ff=x(()=>{"use strict";U();No=(i,e,o)=>{Mh(e);let t=A.unsqueezeShape(e[0].dims,o);return[i.reshapeUnpacked(e[0],t)]},uf=(i,e)=>(Vh(e),No(i,[e[0]],Array.from(e[1].integerData))),lf=i=>i.attributes.getInts("axes"),Mh=i=>{if(!i||i.length!==1)throw new Error("Unsqueeze requires 1 input.");if(i[0].type==="string")throw new Error("invalid input tensor types.")},Vh=i=>{if(!i||i.length!==2)throw new Error("Unsqueeze requires 2 inputs.");if(i[1].type!=="int32")throw new Error("Invalid input type.")}});var cf,pf=x(()=>{"use strict";ws();Bs();Cs();zs();Hr();Ou();$u();Fu();Ru();Uu();Hu();Ku();Qu();qr();nl();bl();_l();Ol();$l();Bl();Gl();Kl();Ql();rf();of();sf();Xr();xo();ff();Lo();cf=[["Abs","","6+",Ws],["Acos","","7+",Hs],["Add","","7+",vs],["And","","7+",Is],["Asin","","7+",qs],["Atan","","7+",js],["AveragePool","","7+",il,al],["BatchNormalization","","7+",xs,Ts],["Cast","","6+",Fs,Ns],["Ceil","","6+",Js],["Clip","","6-10",yo,Xs],["Clip","","11+",Ks],["Concat","","4+",Ms,Us],["Conv","","1+",So,Oo],["ConvTranspose","","1+",_u,Su],["Cos","","7+",Zs],["Div","","7+",_s],["Dropout","","7+",go],["DepthToSpace","","1+",Du,Lu],["Equal","","7+",Ss],["Elu","","6+",Ys,Qs],["Exp","","6+",tu],["Flatten","","1+",ku,Bu],["Floor","","6+",eu],["FusedConv","com.microsoft","1+",So,Oo],["Gather","","1+",Nu,Cu],["Gemm","","7-10",Ao,Mu],["Gemm","","11+",Ao,Vu],["GlobalAveragePool","","1+",ul,ll],["GlobalMaxPool","","1+",hl],["Greater","","7+",Os],["Identity","","1+",go],["ImageScaler","","1+",zu,Wu],["InstanceNormalization","","6+",ju,Xu],["LeakyRelu","","6+",ru,nu],["Less","","7+",As],["LRN","","1+",Ju,Zu],["Log","","6+",ou],["MatMul","","1+",yu,gu],["MaxPool","","1+",fl,cl],["Mul","","7+",Ps],["Neg","","6+",iu],["Not","","1+",au],["Or","","7+",Es],["Pad","","2-10",Po,tl],["Pad","","11+",el,rl],["Pow","","7+",Ds],["PRelu","","7+",Ls],["ReduceLogSum","","1+",vl,ne],["ReduceMax","","1+",xl,ne],["ReduceMean","","1+",gl,ne],["ReduceMin","","1+",Tl,ne],["ReduceProd","","1+",wl,ne],["ReduceSum","","1-12",yl,ne],["ReduceSumSquare","","1+",Il,ne],["Relu","","6+",su],["Reshape","","5+",Sl],["Resize","","10",ko,Dl],["Resize","","11+",ko,Ll],["Shape","","1+",kl],["Sigmoid","","6+",uu],["Sin","","7+",lu],["Slice","","10+",Rl],["Slice","","1-9",Fl,Nl],["Softmax","","1-12",zl,Wl],["Softmax","","13+",ql,Hl],["Split","","2-12",Zl,Yl],["Sqrt","","6+",fu],["Squeeze","","1-12",Fo,ef],["Squeeze","","13+",tf],["Sub","","7+",$s],["Sum","","6+",nf],["Tan","","7+",cu],["Tanh","","6+",pu],["Tile","","6+",af],["Transpose","","1+",Te,Pu],["Upsample","","7-8",Eo,Pl],["Upsample","","9",Eo,El],["Unsqueeze","","1-12",No,lf],["Unsqueeze","","13+",uf],["Xor","","7+",ks]]});function hf(i){let e={},o;for(;(o=df.exec(i))!==null;){let t=o[3].split(",").map(r=>{let n=r.trim().split(" ");return n&&n.length===2?{type:n[0],name:n[1]}:null}).filter(r=>r!==null);e[o[2]]={params:t,body:o[4]}}for(let t in e){let r=Uh.replace("__FUNC__",t),n=new RegExp(r,"gm");for(;(o=n.exec(i))!==null;){let s=o[1],a=o[2],u=o[3].split(","),l=s?`${s} ${a};`:"",f=e[t].body,c="";e[t].params.forEach((m,y)=>{m&&(c+=`${m.type} ${m.name} = ${u[y]};
`)}),f=`${c}
${f}`,f=f.replace("return",`${a} = `);let d=`
${l}
{
${f}
}
`;i=i.replace(o[0],d)}}return i=i.replace(df,""),i}var df,Uh,mf=x(()=>{"use strict";df=/@inline[\s\n\r]+(\w+)[\s\n\r]+([0-9a-zA-Z_]+)\s*\(([^)]*)\)\s*{(([^}]|[\n\r])*)}/gm,Uh="(\\w+)?\\s+([_0-9a-zA-Z]+)\\s+=\\s+__FUNC__\\((.*)\\)\\s*;"});function Re(i,e){let o=[],t=[],r=e!=null&&Array.isArray(e)&&e.length===0,n=e==null||r?null:zh(e,i).sort(),s=0;for(let a=0;a<i.length;++a){if(n!=null){if(n[s]===a&&i[a]!==1)throw new Error(`Can't squeeze axis ${a} since its dim '${i[a]}' is not 1`);(n[s]==null||n[s]>a)&&i[a]===1&&(o.push(i[a]),t.push(a)),n[s]<=a&&s++}i[a]!==1&&(o.push(i[a]),t.push(a))}return{newShape:o,keptDims:t}}function zh(i,e){let o=e.length;return i=i==null?e.map((t,r)=>r):[].concat(i),$e(i.every(t=>t>=-o&&t<o),()=>`All values in axis param must be in range [-${o}, ${o}) but got axis ${i}`),$e(i.every(Wh),()=>`All values in axis param must be integers but got axis ${i}`),i.map(t=>t<0?o+t:t)}function Wh(i){return i%1===0}function Hh(i){if(i.length===0)return 1;let e=i[0];for(let o=1;o<i.length;o++)e*=i[o];return e}function bf(i){let e=Math.ceil(Math.sqrt(i));return[e,Math.ceil(i/e)]}var Yr,Co=x(()=>{"use strict";It();U();Yr=class{constructor(e){this.maxTextureSize=e}computeTextureWH(e,o){let t=this.computeTexture(e,o);return o&&o.isPacked&&(t[0]/=2,t[1]/=2),o&&o.reverseWH?[t[1],t[0]]:t}computeTexture(e,o){let t=o&&o.isPacked;if(e.length===0)return t?[2,2]:[1,1];let r=this.maxTextureSize;if(o&&o.breakAxis!==void 0){let a=o.breakAxis>=e.length?1:e.slice(o.breakAxis).reduce((l,f)=>l*f),u=o.breakAxis<=0?1:e.slice(0,o.breakAxis).reduce((l,f)=>l*f);if(a>r||u>r)z.verbose("TextureLayout",`Given width/height preferences were unattainable: shape:${e}, breakAxis:${o.breakAxis}`);else return[a,u]}let n=e.slice(0);t&&(r=r*2,n=n.map((a,u)=>u>=n.length-2?n[u]%2===0?n[u]:n[u]+1:n[u]),n.length===1&&(n=[2,n[0]])),n.length!==2&&(n=Re(n).newShape);let s=Hh(n);return n.length<=1&&s<=r?[1,s]:n.length===2&&n[0]<=r&&n[1]<=r?n:n.length===3&&n[0]*n[1]<=r&&n[2]<=r?[n[0]*n[1],n[2]]:n.length===3&&n[0]<=r&&n[1]*n[2]<=r?[n[0],n[1]*n[2]]:n.length===4&&n[0]*n[1]*n[2]<=r&&n[3]<=r?[n[0]*n[1]*n[2],n[3]]:n.length===4&&n[0]<=r&&n[1]*n[2]*n[3]<=r?[n[0],n[1]*n[2]*n[3]]:t?bf(s/4).map(a=>a*2):bf(s)}}});var Qr,yf=x(()=>{"use strict";U();Xt();X();Co();Vt();Qr=class extends At{constructor(o){super(o)}getFunctions(){return{...this.offsetToCoords(),...this.coordsToOffset(),...this.toVec(),...this.valueFrom(),...this.getCommonUtilFuncs(),...this.getInputsSamplingSnippets(),...this.getOutputSamplingSnippet()}}getCustomTypes(){return{}}offsetToCoords(){let o="offsetToCoords";return{offsetToCoords:new O(`
vec2 ${o}(int offset, int width, int height) {
int t = offset / width;
int s = offset - t*width;
vec2 coords = (vec2(s,t) + vec2(0.5,0.5)) / vec2(width, height);
return coords;
}
`)}}coordsToOffset(){let o="coordsToOffset";return{coordsToOffset:new O(`
int ${o}(vec2 coords, int width, int height) {
float s = coords.s * float(width);
float t = coords.t * float(height);
int offset = int(t) * width + int(s);
return offset;
}
`)}}getOutputSamplingSnippet(){let o=this.context.outputTextureLayout;return o.isPacked?this.getPackedOutputSamplingSnippet(o):this.getUnpackedOutputSamplingSnippet(o)}getPackedOutputSamplingSnippet(o){let t=o.unpackedShape,r=[o.width,o.height],n={},s="getOutputCoords";switch(t.length){case 0:n[s]=this.getOutputScalarCoords();break;case 1:n[s]=this.getOutputPacked1DCoords(t,r);break;case 2:n[s]=this.getOutputPacked2DCoords(t,r);break;case 3:n[s]=this.getOutputPacked3DCoords(t,r);break;default:n[s]=this.getOutputPackedNDCoords(t,r)}let u=`
void setOutput(vec4 val) {
${D(this.context.glContext.version).output} = val;
}
`,l="floatTextureSetRGBA";return n[l]=new O(u),n}getUnpackedOutputSamplingSnippet(o){let t=o.unpackedShape,r=[o.width,o.height],n={},s="getOutputCoords";switch(t.length){case 0:n[s]=this.getOutputScalarCoords();break;case 1:n[s]=this.getOutputUnpacked1DCoords(t,r);break;case 2:n[s]=this.getOutputUnpacked2DCoords(t,r);break;case 3:n[s]=this.getOutputUnpacked3DCoords(t,r);break;case 4:n[s]=this.getOutputUnpacked4DCoords(t,r);break;case 5:n[s]=this.getOutputUnpacked5DCoords(t,r);break;case 6:n[s]=this.getOutputUnpacked6DCoords(t,r);break;default:throw new Error(`Unsupported output dimensionality: ${t.length}`)}let u=`
void setOutput(float val) {
${D(this.context.glContext.version).output} = vec4(val, 0, 0, 0);
}
`,l="floatTextureSetR";return n[l]=new O(u),n}getOutputScalarCoords(){return new O(`
int getOutputCoords() {
return 0;
}
`)}getOutputPacked1DCoords(o,t){let r=t,n="";return r[0]===1?(n=`
int getOutputCoords() {
return 2 * int(TexCoords.y * ${r[1]}.0);
}
`,new O(n)):r[1]===1?(n=`
int getOutputCoords() {
return 2 * int(TexCoords.x * ${r[0]}.0);
}
`,new O(n)):(n=`
int getOutputCoords() {
ivec2 resTexRC = ivec2(TexCoords.xy *
vec2(${r[0]}, ${r[1]}));
return 2 * (resTexRC.y * ${r[0]} + resTexRC.x);
}
`,new O(n))}getOutputPacked2DCoords(o,t){let r="";if(de.arraysEqual(o,t))return r=`
ivec2 getOutputCoords() {
return 2 * ivec2(TexCoords.xy * vec2(${t[0]}, ${t[1]}));
}
`,new O(r);let n=t,s=Math.ceil(o[1]/2);return r=`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(TexCoords.xy *
vec2(${n[0]}, ${n[1]}));
int index = resTexRC.y * ${n[0]} + resTexRC.x;
// reverse r and c order for packed texture
int r = imod(index, ${s}) * 2;
int c = 2 * (index / ${s});
return ivec2(r, c);
}
`,new O(r)}getOutputPacked3DCoords(o,t){let r=[t[0],t[1]],n=Math.ceil(o[2]/2),s=n*Math.ceil(o[1]/2),a=`
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(TexCoords.xy *
vec2(${r[0]}, ${r[1]}));
int index = resTexRC.y * ${r[0]} + resTexRC.x;
int b = index / ${s};
index -= b * ${s};
// reverse r and c order for packed texture
int r = imod(index, ${n}) * 2;
int c = 2 * (index / ${n});
return ivec3(b, r, c);
}
`;return new O(a)}getOutputPackedNDCoords(o,t){let r=[t[0],t[1]],n=Math.ceil(o[o.length-1]/2),s=n*Math.ceil(o[o.length-2]/2),a=s,u="",l="b, r, c";for(let c=2;c<o.length-1;c++)a*=o[o.length-c-1],u=`
int b${c} = index / ${a};
index -= b${c} * ${a};
`+u,l=`b${c}, `+l;let f=`
ivec${o.length} getOutputCoords() {
ivec2 resTexRC = ivec2(TexCoords.xy *
vec2(${r[0]}, ${r[1]}));
int index = resTexRC.y * ${r[0]} + resTexRC.x;
${u}
int b = index / ${s};
index -= b * ${s};
// reverse r and c order for packed texture
int r = imod(index, ${n}) * 2;
int c = 2 * (index / ${n});
return ivec${o.length}(${l});
}
`;return new O(f)}getOutputUnpacked1DCoords(o,t){let r=`
int getOutputCoords() {
ivec2 resTexRC = ivec2(TexCoords.xy *
vec2(${t[0]}, ${t[1]}));
return resTexRC.y * ${t[0]} + resTexRC.x;
}
`;return new O(r)}getOutputUnpacked2DCoords(o,t){let r=`
ivec2 getOutputCoords() {
ivec2 resTexRC = ivec2(TexCoords.xy *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.y * ${t[0]} + resTexRC.x;
int r = index / ${o[1]};
int c = index - r * ${o[1]};
return ivec2(r, c);
}
`;return new O(r)}getOutputUnpacked3DCoords(o,t){let r="",n=o.length,s=null;n<2&&(s=[]),s=new Array(n-1),s[n-2]=o[n-1];for(let l=n-3;l>=0;--l)s[l]=s[l+1]*o[l+1];let a=["r","c","d"],u=s.map((l,f)=>{let c=`int ${a[f]} = index / ${l}`,d=f===s.length-1?`int ${a[f+1]} = index - ${a[f]} * ${l}`:`index -= ${a[f]} * ${l}`;return`${c}; ${d};`}).join("");return r=`
ivec3 getOutputCoords() {
ivec2 resTexRC = ivec2(TexCoords.xy *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.y * ${t[0]} + resTexRC.x;
${u}
return ivec3(r, c, d);
}
`,new O(r)}getOutputUnpacked4DCoords(o,t){let r="",n=o.length,s=null;n<2&&(s=[]),s=new Array(n-1),s[n-2]=o[n-1];for(let l=n-3;l>=0;--l)s[l]=s[l+1]*o[l+1];let a=["r","c","d","d2"],u=s.map((l,f)=>{let c=`int ${a[f]} = index / ${l}`,d=f===s.length-1?`int ${a[f+1]} = index - ${a[f]} * ${l}`:`index -= ${a[f]} * ${l}`;return`${c}; ${d};`}).join("");return r=`
ivec4 getOutputCoords() {
ivec2 resTexRC = ivec2(TexCoords.xy *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.y * ${t[0]} + resTexRC.x;
${u}
return ivec4(r, c, d, d2);
}
`,new O(r)}getOutputUnpacked5DCoords(o,t){let r="",n=o.length,s=null;n<2&&(s=[]),s=new Array(n-1),s[n-2]=o[n-1];for(let l=n-3;l>=0;--l)s[l]=s[l+1]*o[l+1];let a=["r","c","d","d2","d3"],u=s.map((l,f)=>{let c=`int ${a[f]} = index / ${l}`,d=f===s.length-1?`int ${a[f+1]} = index - ${a[f]} * ${l}`:`index -= ${a[f]} * ${l}`;return`${c}; ${d};`}).join("");return r=`
ivec5 getOutputCoords() {
ivec2 resTexRC = ivec2(TexCoords.xy *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.y * ${t[0]} + resTexRC.x;
${u}
return ivec5(r, c, d, d2, d3);
}
`,new O(r)}getOutputUnpacked6DCoords(o,t){let r="",n=o.length,s=null;n<2&&(s=[]),s=new Array(n-1),s[n-2]=o[n-1];for(let l=n-3;l>=0;--l)s[l]=s[l+1]*o[l+1];let a=["r","c","d","d2","d3","d4"],u=s.map((l,f)=>{let c=`int ${a[f]} = index / ${l}`,d=f===s.length-1?`int ${a[f+1]} = index - ${a[f]} * ${l}`:`index -= ${a[f]} * ${l}`;return`${c}; ${d};`}).join("");return r=`
ivec6 getOutputCoords() {
ivec2 resTexRC = ivec2(TexCoords.xy *
vec2(${t[0]}, ${t[1]}));
int index = resTexRC.y * ${t[0]} + resTexRC.x;
${u}
return ivec6(r, c, d, d2, d3, d4);
}
`,new O(r)}getCommonUtilFuncs(){let o={},t="uvFromFlat";o[t]=new O(`
vec2 uvFromFlat(int texNumR, int texNumC, int index) {
int texC = index / texNumR;
int texR = index - texC * texNumR;
// TODO: swap texR, texC order in following function so row is corresponding to u and column is corresponding to
// v.
return (vec2(texR, texC) + halfCR) / vec2(texNumR, texNumC);
}
`),t="packedUVfrom1D",o[t]=new O(`
vec2 packedUVfrom1D(int texNumR, int texNumC, int index) {
int texelIndex = index / 2;
int texR = texelIndex / texNumC;
int texC = texelIndex - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
`),t="packedUVfrom2D",o[t]=new O(`
vec2 packedUVfrom2D(int texNumR, int texNumC, int texelsInLogicalRow, int row, int col) {
int texelIndex = (row / 2) * texelsInLogicalRow + (col / 2);
int texR = texelIndex / texNumC;
int texC = texelIndex - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
`),t="packedUVfrom3D",o[t]=new O(`
vec2 packedUVfrom3D(int texNumR, int texNumC,
int texelsInBatch, int texelsInLogicalRow, int b,
int row, int col) {
int index = b * texelsInBatch + (row / 2) * texelsInLogicalRow + (col / 2);
int texR = index / texNumC;
int texC = index - texR * texNumC;
return (vec2(texC, texR) + halfCR) / vec2(texNumC, texNumR);
}
`),t="sampleTexture";let r=D(this.context.glContext.version);return o[t]=new O(`
float sampleTexture(sampler2D textureSampler, vec2 uv) {
return ${r.texture2D}(textureSampler, uv).r;
}`),o}getInputsSamplingSnippets(){let o={},t=this.context.outputTextureLayout;return this.context.programInfo.inputNames.forEach((r,n)=>{let s=this.context.inputTextureLayouts[n],a=Rr(r);s.isPacked?o[a]=this.getPackedSamplerFromInput(a,r,s):o[a]=this.getUnpackedSamplerFromInput(a,r,s);let u=ns(r);s.unpackedShape.length<=t.unpackedShape.length&&(s.isPacked?o[u]=this.getPackedSamplerAtOutputCoords(u,s,t,r):o[u]=this.getUnpackedSamplerAtOutputCoords(u,s,t,r))}),o}getPackedSamplerAtOutputCoords(o,t,r,n){let s=t.unpackedShape,a=r.unpackedShape,l=Rr(n),f=s.length,c=a.length,d=dt.getBroadcastDims(s,a),m=ht(c),y=c-f,g,T=Gt();f===0?g="":c<2&&d.length>=1?g="coords = 0;":g=d.map(wt=>`coords.${T[wt+y]} = 0;`).join(`
`);let _="";c<2&&f>0?_="coords":_=s.map((wt,mt)=>`coords.${T[mt+y]}`).join(", ");let w="return outputValue;",L=A.size(s)===1,Q=A.size(a)===1;if(f===1&&!L&&!Q)w=`
return vec4(outputValue.xy, outputValue.xy);
`;else if(L&&!Q)c===1?w=`
return vec4(outputValue.x, outputValue.x, 0., 0.);
`:w=`
return vec4(outputValue.x);
`;else if(d.length){let wt=f-2,mt=f-1;d.indexOf(wt)>-1&&d.indexOf(mt)>-1?w="return vec4(outputValue.x);":d.indexOf(wt)>-1?w="return vec4(outputValue.x, outputValue.y, outputValue.x, outputValue.y);":d.indexOf(mt)>-1&&(w="return vec4(outputValue.xx, outputValue.zz);")}let pt=`
int lastDim = coords.${T[c-1]};
coords.${T[c-1]} = coords.${T[c-2]};
coords.${T[c-2]} = lastDim;
`,Ht=`
vec4 ${o}() {
${m} coords = getOutputCoords();
${pt}
${g}
vec4 outputValue = ${l}(${_});
${w}
}
`;return new O(Ht,["coordinates.getOutputCoords"])}getUnpackedSamplerAtOutputCoords(o,t,r,n){let s=[r.width,r.height],a=[t.width,t.height],u=t.unpackedShape.length,l=r.unpackedShape.length,f=t.unpackedShape,c=r.unpackedShape,d=Rr(n);if(u===l&&de.arraysEqual(a,s)){let L=`
float ${o}() {
return sampleTexture(${n}, TexCoords);
}
`;return new O(L,["coordinates.sampleTexture"])}let m=ht(l),y=dt.getBroadcastDims(f,c),g=l-u,T,_=Gt();u===0?T="":l<2&&y.length>=1?T="coords = 0;":T=y.map(L=>`coords.${_[L+g]} = 0;`).join(`
`);let w="";l<2&&u>0?w="coords":w=t.unpackedShape.map((L,B)=>`coords.${_[B+g]}`).join(", ");let v=`
float ${o}() {
${m} coords = getOutputCoords();
${T}
return ${d}(${w});
}
`;return new O(v,["coordinates.getOutputCoords"])}getPackedSamplerFromInput(o,t,r){switch(r.unpackedShape.length){case 0:return this.getPackedSamplerScalar(o,t);case 1:return this.getPackedSampler1D(o,t,r);case 2:return this.getPackedSampler2D(o,t,r);case 3:return this.getPackedSampler3D(o,t,r);default:return this.getPackedSamplerND(o,t,r)}}getUnpackedSamplerFromInput(o,t,r){let n=r.unpackedShape;switch(n.length){case 0:return this.getUnpackedSamplerScalar(o,t,r);case 1:return this.getUnpackedSampler1D(o,t,r);case 2:return this.getUnpackedSampler2D(o,t,r);case 3:return this.getUnpackedSampler3D(o,t,r);case 4:return this.getUnpackedSampler4D(o,t,r);case 5:return this.getUnpackedSampler5D(o,t,r);case 6:return this.getUnpackedSampler6D(o,t,r);default:throw new Error(`Unsupported dimension ${n.length}-D`)}}getPackedSamplerScalar(o,t){let r=D(this.context.glContext.version),n=`
vec4 ${o}() {
return ${r.texture2D}(${t}, halfCR);
}
`;return new O(n)}getPackedSampler1D(o,t,r){let n=[r.width,r.height],s=[n[1],n[0]],a=D(this.context.glContext.version),l=`vec4 ${o}(int index) {
vec2 uv = packedUVfrom1D(
${s[0]}, ${s[1]}, index);
return ${a.texture2D}(${t}, uv);
}`;return new O(l,["coordinates.packedUVfrom1D"])}getPackedSampler2D(o,t,r){let n=r.unpackedShape,s=[r.width,r.height],a=D(this.context.glContext.version),u=s[0],l=s[1];if(s!=null&&de.arraysEqual(n,s)){let y=`vec4 ${o}(int row, int col) {
vec2 uv = (vec2(col, row) + halfCR) / vec2(${l}.0, ${u}.0);
return ${a.texture2D}(${t}, uv);
}`;return new O(y)}let f=s,c=Math.ceil(n[1]/2),m=`vec4 ${o}(int row, int col) {
vec2 uv = packedUVfrom2D(${f[1]}, ${f[0]}, ${c}, row, col);
return ${a.texture2D}(${t}, uv);
}`;return new O(m,["coordinates.packedUVfrom2D"])}getPackedSampler3D(o,t,r){let n=r.unpackedShape,s=[r.width,r.height],a=[s[0],s[1]],u=D(this.context.glContext.version);if(n[0]===1){let g=n.slice(1),T=[1,2],_=ke(n,g),w=["b","row","col"],v=JSON.parse(JSON.stringify(r));v.unpackedShape=_;let L=this.getPackedSamplerFromInput(o,t,v),Q=`${L.routineBody}
vec4 ${o}(int b, int row, int col) {
return ${o}(${Be(w,T)});
} `;return new O(Q,L.dependencies)}let l=a[0],f=a[1],c=Math.ceil(n[2]/2),d=c*Math.ceil(n[1]/2),y=`vec4 ${o}(int b, int row, int col) {
vec2 uv = packedUVfrom3D(
${f}, ${l}, ${d}, ${c}, b, row, col);
return ${u.texture2D}(${t}, uv);}`;return new O(y,["coordinates.packedUVfrom3D"])}getPackedSamplerND(o,t,r){let n=r.unpackedShape,s=n.length,a=[r.width,r.height],u=D(this.context.glContext.version),l=[a[0],a[1]],f=l[1],c=l[0],d=Math.ceil(n[s-1]/2),m=d*Math.ceil(n[s-2]/2),y="int b, int row, int col",g=`b * ${m} + (row / 2) * ${d} + (col / 2)`;for(let w=2;w<s-1;w++)y=`int b${w}, `+y,m*=n[s-w-1],g=`b${w} * ${m} + `+g;let _=`vec4 ${o}(${y}) {
int index = ${g};
int texR = index / ${c};
int texC = index - texR * ${c};
vec2 uv = (vec2(texC, texR) + halfCR) / vec2(${c}, ${f});
return ${u.texture2D}(${t}, uv);
}`;return new O(_)}getUnpackedSamplerScalar(o,t,r){let[n,s]=[r.width,r.height];if(n===1&&s===1){let u=`
float ${o}() {
return sampleTexture(${t}, halfCR);
}
`;return new O(u,["coordinates.sampleTexture"])}let a=`
float ${o}() {
int offset_${t} = coordsToOffset(TexCoords, ${n}, ${s});
vec2 uv = uvFromFlat(${n}, ${s}, offset_${t});
return sampleTexture(${t}, uv);
}
`;return new O(a,["coordinates.uvFromFlat","coordinates.sampleTexture","coordinates.coordsToOffset"])}getUnpackedSampler1D(o,t,r){let n=r.width,s=r.height;if(s===1&&n===1){let u=`
float ${o}(int index) {
return sampleTexture(${t}, halfCR);
}
`;return new O(u,["coordinates.sampleTexture"])}if(s===1){let u=`
float ${o}(int index) {
vec2 uv = vec2((float(index) + 0.5) / ${n}.0, 0.5);
return sampleTexture(${t}, uv);
}
`;return new O(u,["coordinates.sampleTexture"])}if(n===1){let u=`
float ${o}(int index) {
vec2 uv = vec2(0.5, (float(index) + 0.5) / ${s}.0);
return sampleTexture(${t}, uv);
}
`;return new O(u,["coordinates.sampleTexture"])}let a=`
float ${o}(int index) {
vec2 uv = uvFromFlat(${n}, ${s}, index);
return sampleTexture(${t}, uv);
}
`;return new O(a,["coordinates.uvFromFlat","coordinates.sampleTexture"])}getUnpackedSampler2D(o,t,r){let n=r.unpackedShape,s=[r.height,r.width];if(s!=null&&de.arraysEqual(n,s)){let m=s[1],y=s[0],g=`
float ${o}(int row, int col) {
vec2 uv = (vec2(row, col) + halfCR) / vec2(${m}.0, ${y}.0);
return sampleTexture(${t}, uv);
}
`;return new O(g,["coordinates.sampleTexture"])}let{newShape:a,keptDims:u}=Re(n),l=a;if(l.length<n.length){let m=ke(n,l),y=JSON.parse(JSON.stringify(r));y.unpackedShape=m;let g=["col","row"],T=`
${this.getUnpackedSamplerFromInput(o,t,y).routineBody}
float ${o}(int row, int col) {
return ${o}(${Be(g,u)});
}
`;return new O(T,["coordinates.sampleTexture"])}let f=s[1],c=s[0];if(c===1){let m=`
float ${o}(int row, int col) {
int offset_${t} = coordsToOffset(TexCoords, ${f}, ${c});
float index = dot(vec3(row, col, offset_${t}), vec3(${n[1]}, 1, 1));
vec2 uv = vec2(0.5, (index + 0.5) / ${f}.0);
return sampleTexture(${t}, uv);
}
`;return new O(m,["coordinates.sampleTexture","coordinates.coordsToOffset"])}if(f===1){let m=`
float ${o}(int row, int col) {
int offset_${t} = coordsToOffset(TexCoords, ${f}, ${c});
float index = dot(vec3(row, col, offset_${t}), vec3(${n[1]}, 1, 1));
vec2 uv = vec2((index + 0.5) / ${c}.0, 0.5);
return sampleTexture(${t}, uv);
}
`;return new O(m,["coordinates.sampleTexture","coordinates.coordsToOffset"])}let d=`
float ${o}(int row, int col) {
int index = col * ${n[1]} + row;
vec2 uv = uvFromFlat(${f}, ${c}, index);
return sampleTexture(${t}, uv);
}
`;return new O(d,["coordinates.uvFromFlat","coordinates.sampleTexture","coordinates.coordsToOffset"])}getUnpackedSampler3D(o,t,r){let n=r.unpackedShape,s=n[1]*n[2],a=n[2],{newShape:u,keptDims:l}=Re(n),f=u;if(f.length<n.length){let y=ke(n,f),g=["batch","col","row"],T=JSON.parse(JSON.stringify(r));T.unpackedShape=y;let _=this.getUnpackedSamplerFromInput(o,t,T),w=l.reverse(),v=`
${_.routineBody}
float ${o}(int batch, int row, int col) {
return ${o}(${Be(g,w)});
}
`;return new O(v,_.dependencies)}let c=r.width,d=r.height,m=`
float ${o}(int depth, int row, int col) {
// Explicitly use integer operations as dot() only works on floats.
int index = depth * ${s} + col * ${a} + row;
vec2 uv = uvFromFlat(${c}, ${d}, index);
return sampleTexture(${t}, uv);
}
`;return new O(m,["coordinates.uvFromFlat","coordinates.sampleTexture","coordinates.coordsToOffset"])}getUnpackedSampler4D(o,t,r){let n=r.unpackedShape,s=n[3],a=n[2]*s,u=n[1]*a,l=r.width,f=r.height,c=`
float ${o}(int row, int col, int depth, int depth2) {
int index = row * ${u} + col * ${a} +
depth2 * ${s} + depth;
vec2 uv = uvFromFlat(${l}, ${f}, index);
return sampleTexture(${t}, uv);
}
`;return new O(c,["coordinates.uvFromFlat","coordinates.sampleTexture"])}getUnpackedSampler5D(o,t,r){let n=r.unpackedShape,s=n[4],a=n[3]*s,u=n[2]*a,l=n[1]*u,{newShape:f,keptDims:c}=Re(n);if(f.length<n.length){let g=ke(n,f),T=["row","col","depth","depth2","depth3"],_=JSON.parse(JSON.stringify(r));_.unpackedShape=g;let w=`
${this.getUnpackedSamplerFromInput(o,t,_).routineBody}
float ${o}(int row, int col, int depth, int depth2, int depth3) {
return ${o}(${Be(T,c)});
}
`;return new O(w,["coordinates.sampleTexture","coordinates.uvFromFlat"])}let d=r.width,m=r.height,y=`
float ${o}(int row, int col, int depth, int depth2, int depth3) {
int index = row * ${l} + col * ${u} + depth * ${a} +
depth3 * ${s} + depth2;
vec2 uv = uvFromFlat(${d}, ${m}, index);
return sampleTexture(${t}, uv);
}
`;return new O(y,["coordinates.sampleTexture","coordinates.uvFromFlat"])}getUnpackedSampler6D(o,t,r){let n=r.unpackedShape,s=n[5],a=n[4]*s,u=n[3]*a,l=n[2]*u,f=n[1]*l,{newShape:c,keptDims:d}=Re(n);if(c.length<n.length){let T=ke(n,c),_=["row","col","depth","depth2","depth3","depth4"],w=JSON.parse(JSON.stringify(r));w.unpackedShape=T;let v=`
${this.getUnpackedSamplerFromInput(o,t,w).routineBody}
float ${o}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
return ${o}(${Be(_,d)});
}
`;return new O(v,["coordinates.sampleTexture","coordinates.uvFromFlat"])}let m=r.width,y=r.height,g=`
float ${o}(int row, int col, int depth,
int depth2, int depth3, int depth4) {
int index = row * ${f} + col * ${l} + depth * ${u} +
depth2 * ${a} + depth3 * ${s} + depth4;
vec2 uv = uvFromFlat(${m}, ${y}, index);
return sampleTexture(${t}, uv);
}
`;return new O(g,["coordinates.uvFromFlat","coordinates.sampleTexture","coordinates.coordsToOffset"])}toVec(){let o=this.context.outputTextureLayout,t=o.shape.length,r=o.strides,n=o.width,s=o.height,a=[];for(let l=0;l<t-1;++l)a.push(`
c[${l}] = offset / ${r[l]};`),a.push(`
offset -= c[${l}] * ${r[l]};`);a.push(`
c[${t-1}] = offset;`);let u=`
void toVec(vec2 texCoords, out int c[${t}]) {
int offset = coordsToOffset(texCoords, ${n}, ${s});
${a.join("")}
}
void toVec(int offset, out int c[${t}]) {
${a.join("")}
}
`;return{toVec:new O(u,["coordinates.coordsToOffset"])}}valueFrom(){let o={};return this.context.programInfo.inputNames.forEach((t,r)=>{let n=this.context.inputTextureLayouts[r],a=(n.unpackedShape.length>0?n.unpackedShape:n.shape).length,u=`_${t}`;o[u]=new O(this.getValueFromSingle(t,a,n.width,n.height,!1),[`shapeUtils.indicesToOffset${u}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"]),u=u+"_T",o[u]=new O(this.getValueFromSingle(t,a,n.width,n.height,!0),[`shapeUtils.indicesToOffset${u}`,"coordinates.offsetToCoords","fragcolor.getColorAsFloat"])}),o}getValueFromSingle(o,t,r,n,s){let a=`_${o}`;s&&(a=a+"_T");let u=D(this.context.glContext.version);return`
float ${a}(int m[${t}]) {
int offset = indicesToOffset${a}(m);
vec2 coords = offsetToCoords(offset, ${r}, ${n});
float value = getColorAsFloat(${u.texture2D}(${o}, coords));
return value;
}
`}getPackedValueFrom(o,t,r,n,s){let a=`_${o}_Pack`;s&&(a=a+"_T");let u=D(this.context.glContext.version);return`
vec4 ${a}(int m[${t}]) {
int offset = indicesToOffset_${o}(m);
vec2 coords = offsetToCoords(offset, ${r}, ${n});
return ${u.texture2D}(${o}, coords);
}
`}}});var tn,gf=x(()=>{"use strict";Xt();tn=class i extends At{constructor(e){super(e)}getFunctions(){return{...this.encodeFloat32(),...this.decodeFloat32()}}getCustomTypes(){return{}}encodeFloat32(){return{encode:new O(`highp vec4 encode(highp float f) {
return vec4(f, 0.0, 0.0, 0.0);
}
`)}}decodeFloat32(){return{decode:new O(`highp float decode(highp vec4 rgba) {
return rgba.r;
}
`)}}encodeUint8(){let e=i.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{encode:new O(`
highp vec4 encode(highp float f) {
highp float F = abs(f);
highp float Sign = step(0.0,-f);
highp float Exponent = floor(log2(F));
highp float Mantissa = (exp2(- Exponent) * F);
Exponent = floor(log2(F) + 127.0) + floor(log2(Mantissa));
highp vec4 rgba;
rgba[0] = 128.0 * Sign + floor(Exponent*exp2(-1.0));
rgba[1] = 128.0 * mod(Exponent,2.0) + mod(floor(Mantissa*128.0),128.0);
rgba[2] = floor(mod(floor(Mantissa*exp2(23.0 -8.0)),exp2(8.0)));
rgba[3] = floor(exp2(23.0)*mod(Mantissa,exp2(-15.0)));
${e}
rgba = rgba / 255.0; // values need to be normalized to [0,1]
return rgba;
}
`)}}decodeUint8(){let e=i.isLittleEndian()?"rgba.rgba=rgba.abgr;":"";return{decode:new O(`
highp float decode(highp vec4 rgba) {
rgba = rgba * 255.0; // values need to be de-normalized from [0,1] to [0,255]
${e}
highp float Sign = 1.0 - step(128.0,rgba[0])*2.0;
highp float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;
highp float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);
highp float Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 ));
return Result;
}
`)}}static isLittleEndian(){let e=new ArrayBuffer(4),o=new Uint32Array(e),t=new Uint8Array(e);if(o[0]=3735928559,t[0]===239)return!0;if(t[0]===222)return!1;throw new Error("unknown endianness")}}});var en,xf=x(()=>{"use strict";Xt();X();en=class extends At{constructor(e){super(e)}getFunctions(){return{...this.setFragColor(),...this.getColorAsFloat()}}getCustomTypes(){return{}}setFragColor(){let e=D(this.context.glContext.version);return{setFragColor:new O(`
void setFragColor(float value) {
${e.output} = encode(value);
}
`,["encoding.encode"])}}getColorAsFloat(){return{getColorAsFloat:new O(`
float getColorAsFloat(vec4 color) {
return decode(color);
}
`,["encoding.decode"])}}}});var rn,Tf=x(()=>{"use strict";Xt();rn=class i extends At{constructor(e){super(e)}getFunctions(){return{...this.bcastIndex(),...this.bcastMatmulIndex(),...this.offsetToIndices(),...this.indicesToOffset(),...this.incrementIndices()}}getCustomTypes(){return{}}bcastIndex(){let e=this.context.outputTextureLayout.shape.length,o={};return this.context.programInfo.inputNames.forEach((t,r)=>{let n=this.context.inputTextureLayouts[r].unpackedShape;if(n.length<=e){let s=n.length,a=e-s,u=`bcastIndices_${t}`,l="";for(let c=0;c<s;++c)l+=`
realIndices[${c}] = int( mod(float(bcastedIndices[${a+c}]), ${n[c]}.0) );
`;let f=`
void ${u} (int bcastedIndices[${e}], out int realIndices[${s}]) {
${l}
}
`;o[u]=new O(f)}}),o}bcastMatmulIndex(){let e=this.context.outputTextureLayout.shape.length,o={};return this.context.programInfo.inputNames.forEach((t,r)=>{let n=this.context.inputTextureLayouts[r].shape;if(!(n.length<2||n.length>e)){let s=n.length,a=e-s,u=`bcastMatmulIndices_${t}`,l="";for(let c=0;c<s-2;++c)l+=`
realIndices[${c}] = int( mod(float(bcastedIndices[${a+c}]), ${n[c]}.0) );
`;let f=`
void ${u}(int bcastedIndices[${e}], out int realIndices[${s}]) {
${l}
realIndices[${s-1}] = bcastedIndices[${e-1}];
realIndices[${s-2}] = bcastedIndices[${e-2}];
}
`;o[u]=new O(f)}}),o}indicesToOffset(){let e={};return this.context.programInfo.inputNames.forEach((o,t)=>{let r=this.context.inputTextureLayouts[t].shape,n=this.context.inputTextureLayouts[t].strides,s=r.length,a=`indicesToOffset_${o}`;e[a]=new O(i.indexToOffsetSingle(a,s,n)),a=`indicesToOffset_${o}_T`,e[a]=new O(i.indexToOffsetSingle(a,s,n.slice().reverse()))}),e}static indexToOffsetSingle(e,o,t){let r="";for(let n=o-1;n>=0;--n)r+=`
offset += indices[${n}] * ${t[n]};
`;return`
int ${e}(int indices[${o}]) {
int offset = 0;
${r}
return offset;
}
`}offsetToIndices(){let e={};return this.context.programInfo.inputNames.forEach((o,t)=>{let r=this.context.inputTextureLayouts[t].shape,n=this.context.inputTextureLayouts[t].strides,s=r.length,a=`offsetToIndices_${o}`;e[a]=new O(i.offsetToIndicesSingle(a,s,n)),a=`offsetToIndices_${o}_T`,e[a]=new O(i.offsetToIndicesSingle(a,s,n.slice().reverse()))}),e}static offsetToIndicesSingle(e,o,t){let r=[];for(let n=0;n<o-1;++n)r.push(`
indices[${n}] = offset / ${t[n]};`),r.push(`
offset -= indices[${n}] * ${t[n]};`);return r.push(`
indices[${o-1}] = offset;`),`
void ${e}(int offset, out int indices[${o}]) {
${r.join("")}
}
`}incrementIndices(){let e={};return this.context.programInfo.inputNames.forEach((o,t)=>{let r=this.context.inputTextureLayouts[t].shape,n=r.length,s=`incrementIndices_${o}`,a="";for(let l=0;l<n;++l)a+=`
shape[${l}] = ${r[l]};`;let u=`
void ${s}(int axis, out int indices[${n}]) {
int shape[${n}];
${a};
for(int i = ${n} -1 ; i >= 0; --i) {
if(i > axis) continue;
indices[i] += 1;
if(indices[i] < shape[i]) {
break;
}
indices[i] = 0;
}
}
`;e[s]=new O(u)}),e}}});var nn,wf=x(()=>{"use strict";Xt();nn=class extends At{constructor(e){super(e)}getCustomTypes(){return{}}getFunctions(){return{...this.binaryVecFunctions(),...this.copyVec(),...this.setVecItem(),...this.getVecItem()}}binaryVecFunctions(){let o=this.context.outputTextureLayout.shape.length,t={add:"+=",sub:"-=",mul:"*=",div:"/="},r={};for(let n in t){let s=`${n}Vec`,a="";for(let l=0;l<o;++l)a+=`
dest[${l}] ${t[n]} src[${l}];
`;let u=`
void ${s}(int src[${o}], out int dest[${o}]) {
${a}
}
`;r[s]=new O(u)}return r}copyVec(){let o=this.context.outputTextureLayout.shape.length,t="";for(let n=0;n<o;++n)t+=`
dest[${n}] = src[${n}];
`;let r=`
void copyVec(int src[${o}], out int dest[${o}]) {
${t}
}
`;return{copyVec:new O(r)}}setVecItem(){let o=this.context.outputTextureLayout.shape.length,t=`
if(index < 0)
index =${o} + index;
if (index == 0)
m[0] = value;
`;for(let n=1;n<o-1;++n)t+=`
else if (index == ${n})
m[${n}] = value;
`;t+=`
else
m[${o-1}] = value;
`;let r=`
void setVecItem(out int m[${o}], int index, int value) {
${t}
}
`;return{setVecItem:new O(r)}}getVecItem(){let o=this.context.outputTextureLayout.shape.length,t=`
if(index < 0)
index = ${o} + index;
if (index == 0)
return m[0];
`;for(let n=1;n<o-1;++n)t+=`
else if (index == ${n})
return m[${n}];
`;t+=`
else
return m[${o-1}];
`;let r=`
int getVecItem(int m[${o}], int index) {
${t}
}
`;return{getVecItem:new O(r)}}}});var Ro,vf=x(()=>{"use strict";yf();gf();xf();Tf();wf();Ro={encoding:tn,fragcolor:en,vec:nn,shapeUtils:rn,coordinates:Qr}});var on,If=x(()=>{"use strict";Xt();mf();vf();X();on=class{constructor(e,o,t,r){this.libs={};this.glslLibRoutineDependencyGraph={};this.context=new Ur(e,o,t,r),Object.keys(Ro).forEach(s=>{let a=new Ro[s](this.context);this.libs[s]=a});let n=this.glslLibRoutineDependencyGraph;for(let s in this.libs){let u=this.libs[s].getFunctions();for(let l in u){let f=s+"."+l,c;n[f]?(c=n[f],c.routineBody=u[l].routineBody):(c=new rr(f,u[l].routineBody),n[f]=c);let d=u[l].dependencies;if(d)for(let m=0;m<d.length;++m)if(n[d[m]])c.addDependency(n[d[m]]);else{let y=new rr(d[m]);n[d[m]]=y,c.addDependency(y)}}}}preprocess(){let e=this.context.programInfo,o=e.shaderSource;return this.context.programInfo.hasMain||(o=`${o}
${rs(this.context.glContext.version,this.context.outputTextureLayout.shape.length)}`),o=hf(o),`${es(this.context.glContext.version)}
${this.getUniforms(e.inputNames,e.variables)}
${this.getImports(o)}
${o}`}getImports(e){let o=this.selectGlslLibRoutinesToBeIncluded(e);if(o.length===0)return"";let t="";for(let r=0;r<o.length;++r)if(o[r].routineBody)t+=o[r].routineBody+`
`;else throw new Error(`Missing body for the Glsl Library routine: ${o[r].name}`);return t}selectGlslLibRoutinesToBeIncluded(e){let o=[];return Object.keys(this.glslLibRoutineDependencyGraph).forEach(t=>{let r=t.split(".")[1];e.indexOf(r)!==-1&&o.push(this.glslLibRoutineDependencyGraph[t])}),zr.returnOrderedNodes(o)}getUniforms(e,o){let t=[];if(e)for(let r of e)t.push(`uniform sampler2D ${r};`);if(o)for(let r of o)t.push(`uniform ${r.type} ${r.name}${r.arrayLength?`[${r.arrayLength}]`:""};`);return t.join(`
`)}}});var an,_f=x(()=>{"use strict";kt();It();If();X();an=class{constructor(e,o,t){this.profiler=e;this.glContext=o;this.textureLayoutStrategy=t;this.repo=new Map,this.attributesBound=!1}getArtifact(e){return this.repo.get(e)}setArtifact(e,o){this.repo.set(e,o)}run(e,o,t){this.profiler.event("op",`ProgramManager.run ${e.programInfo.name??"unknown kernel"}`,()=>{let r=this.glContext.gl,n=e.program;r.useProgram(n);try{this.bindOutput(t),this.attributesBound||this.bindAttributes(e.attribLocations),this.bindUniforms(e.uniformLocations,e.programInfo.variables??[],o)}catch(s){throw z.error("ProgramManager",e.programInfo.shaderSource),s}this.profiler.event("backend","GlContext.draw()",()=>{this.glContext.draw()})},this.glContext)}dispose(){this.vertexShader&&this.glContext.deleteShader(this.vertexShader),this.repo.forEach(e=>this.glContext.deleteProgram(e.program))}build(e,o,t){return this.profiler.event("backend","ProgramManager.build",()=>{let r=new on(this.glContext,e,o,t),n=r.preprocess(),s=this.compile(n);return{programInfo:e,program:s,uniformLocations:this.getUniformLocations(s,r.context.programInfo.inputNames,r.context.programInfo.variables),attribLocations:this.getAttribLocations(s)}})}compile(e){if(!this.vertexShader){z.verbose("ProrgramManager","Compiling and caching Vertex shader for the first time");let r=ts(this.glContext.version);this.vertexShader=this.glContext.compileShader(r,this.glContext.gl.VERTEX_SHADER)}$.debug&&z.verbose("ProrgramManager",`FragShader:
${e}
`);let o=this.glContext.compileShader(e,this.glContext.gl.FRAGMENT_SHADER),t=this.glContext.createProgram(this.vertexShader,o);return this.glContext.deleteShader(o),t}bindOutput(e){let o=e.width,t=e.height;z.verbose("ProrgramManager",`Binding output texture to Framebuffer: w/h=${o}/${t}, shape=${e.shape}, type=${e.tensor.type}`),this.glContext.attachFramebuffer(e.texture,o,t)}bindAttributes(e){let o=e.position,t=e.textureCoord;this.glContext.setVertexAttributes(o,t),this.attributesBound=!0}bindUniforms(e,o,t){let r=this.glContext.gl,n=0;for(let{name:s,type:a,location:u,arrayLength:l}of e){let f=o.find(c=>c.name===s)?.data;if(a!=="sampler2D"&&!f)throw new Error(`variable '${s}' does not have data defined in program info`);switch(a){case"sampler2D":this.bindTexture(t[n],u,n),n++;break;case"float":l?r.uniform1fv(u,f):r.uniform1f(u,f);break;case"int":l?r.uniform1iv(u,f):r.uniform1i(u,f);break;default:throw new Error(`Uniform not implemented: ${a}`)}}}bindTexture(e,o,t){this.glContext.bindTextureToUniform(e.texture,t,o)}getAttribLocations(e){return{position:this.getAttribLocation(e,"position"),textureCoord:this.getAttribLocation(e,"textureCoord")}}getUniformLocations(e,o,t){let r=[];if(o)for(let n of o)r.push({name:n,type:"sampler2D",location:this.getUniformLocation(e,n)});if(t)for(let n of t)r.push({...n,location:this.getUniformLocation(e,n.name)});return r}getUniformLocation(e,o){let r=this.glContext.gl.getUniformLocation(e,o);if(r===null)throw new Error(`Uniform ${o} not found.`);return r}getAttribLocation(e,o){return this.glContext.gl.getAttribLocation(e,o)}}});var sn,Sf=x(()=>{"use strict";It();tr();sn=class{constructor(e,o,t,r){this.glContext=e;this.layoutStrategy=o;this.profiler=t;this.config=r;this.pendingRead=new Map;r.reuseTextures&&(this.inUseTextures=new Map,this.idleTextures=new Map,this.textureLookup=new Map)}createTextureFromLayout(e,o,t,r){let n=this.toEncoderType(e),s=this.glContext.getEncoder(n,o.channels||1,r);if(o.isPacked&&r===1)throw new Error("not implemented");let a=o.width,u=o.height,l,f;if(this.config.reuseTextures){l=`${a}x${u}_${s.format}_${s.internalFormat}_${s.textureType}`,f=this.inUseTextures.get(l),f||(f=[],this.inUseTextures.set(l,f));let d=this.idleTextures.get(l);if(d&&d.length>0){let m=d.pop();return f.push(m),r===1&&this.glContext.updateTexture(m,a,u,s,this.toTextureData(e,t)),m}}z.verbose("TextureManager",`Creating new texture of size ${o.width}x${o.height}`);let c=this.glContext.allocateTexture(a,u,s,this.toTextureData(e,t));return this.config.reuseTextures&&(f.push(c),this.textureLookup.set(c,l)),c}readTexture(e,o,t){return t||(t=1),this.profiler.event("backend","TextureManager.readTexture",()=>{let r=e.shape.reduce((s,a)=>s*a)*t,n=this.glContext.readTexture(e.texture,e.width,e.height,r,this.toEncoderType(o),t);return this.toTensorData(o,n)})}async readTextureAsync(e,o,t){let r=e.tensor.dataId;if(t||(t=1),this.pendingRead.has(r)){let n=this.pendingRead.get(r);return new Promise(s=>n?.push(s))}return this.profiler.event("backend","TextureManager.readTextureAsync",async()=>{this.pendingRead.set(r,[]);let n=e.shape.reduce((l,f)=>l*f)*t;await this.glContext.createAndWaitForFence();let s=this.glContext.readTexture(e.texture,e.width,e.height,n,this.toEncoderType(o),t),a=this.toTensorData(o,s),u=this.pendingRead.get(r);return this.pendingRead.delete(r),u?.forEach(l=>l(a)),a})}readUint8TextureAsFloat(e){return this.profiler.event("backend","TextureManager.readUint8TextureAsFloat",()=>{let o=e.shape.reduce((r,n)=>r*n),t=this.glContext.readTexture(e.texture,e.width,e.height,o*4,"byte",4);return new Float32Array(t.buffer,t.byteOffset,o)})}releaseTexture(e,o){let t;if(this.config.reuseTextures&&(t=this.textureLookup.get(e.texture),t)){o&&this.textureLookup.delete(t);let r=this.inUseTextures.get(t);if(r){let n=r.indexOf(e.texture);if(n!==-1){r.splice(n,1);let s=this.idleTextures.get(t);s||(s=[],this.idleTextures.set(t,s)),s.push(e.texture)}}}(!t||o)&&(z.verbose("TextureManager",`Deleting texture of size ${e.width}x${e.height}`),this.glContext.deleteTexture(e.texture))}toTensorData(e,o){switch(e){c
Shader source:
${e}`);return r}deleteShader(e){this.gl.deleteShader(e)}bindTextureToUniform(e,o,t){let r=this.gl;r.activeTexture(r.TEXTURE0+o),this.checkError(),r.bindTexture(r.TEXTURE_2D,e),this.checkError(),r.uniform1i(t,o),this.checkError()}draw(){this.gl.drawArrays(this.gl.TRIANGLE_STRIP,0,4),this.checkError()}checkError(){if($.debug){let e=this.gl,o=e.getError(),t="";switch(o){case e.NO_ERROR:return;case e.INVALID_ENUM:t="INVALID_ENUM";break;case e.INVALID_VALUE:t="INVALID_VALUE";break;case e.INVALID_OPERATION:t="INVALID_OPERATION";break;case e.INVALID_FRAMEBUFFER_OPERATION:t="INVALID_FRAMEBUFFER_OPERATION";break;case e.OUT_OF_MEMORY:t="OUT_OF_MEMORY";break;case e.CONTEXT_LOST_WEBGL:t="CONTEXT_LOST_WEBGL";break;default:t=`Unknown WebGL Error: ${o.toString(16)}`}throw new Error(t)}}deleteTexture(e){this.gl.deleteTexture(e)}deleteProgram(e){this.gl.deleteProgram(e)}getEncoder(e,o,t=0){if(this.version===2)return new Gr(this.gl,o);switch(e){case"float":return t===1||this.isRenderFloat32Supported?new Qe(this.gl,o):new Qe(this.gl,o,this.textureHalfFloatExtension.HALF_FLOAT_OES);case"int":throw new Error("not implemented");case"byte":return new Mr(this.gl,o);default:throw new Error(`Invalid dataType: ${e}`)}}clearActiveTextures(){let e=this.gl;for(let o=0;o<this.maxTextureImageUnits;++o)e.activeTexture(e.TEXTURE0+o),e.bindTexture(e.TEXTURE_2D,null)}dispose(){if(this.disposed)return;let e=this.gl;e.bindFramebuffer(e.FRAMEBUFFER,null),e.deleteFramebuffer(this.framebuffer),e.bindBuffer(e.ARRAY_BUFFER,null),e.deleteBuffer(this.vertexbuffer),e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,null),e.finish(),this.disposed=!0}createDefaultGeometry(){return new Float32Array([-1,1,0,0,1,-1,-1,0,0,0,1,1,0,1,1,1,-1,0,1,0])}createVertexbuffer(){let e=this.gl,o=e.createBuffer();if(!o)throw new Error("createBuffer() returned null");let t=this.createDefaultGeometry();return e.bindBuffer(e.ARRAY_BUFFER,o),e.bufferData(e.ARRAY_BUFFER,t,e.STATIC_DRAW),this.checkError(),o}createFramebuffer(){let e=this.gl.createFramebuffer();if(!e)throw new Error("createFramebuffer returned null");return e}queryVitalParameters(){let e=this.gl;if(this.isFloatTextureAttachableToFrameBuffer=this.checkFloatTextureAttachableToFrameBuffer(),this.isRenderFloat32Supported=this.checkRenderFloat32(),this.isFloat32DownloadSupported=this.checkFloat32Download(),this.version===1&&!this.textureHalfFloatExtension&&!this.isRenderFloat32Supported)throw new Error("both float32 and float16 TextureType are not supported");this.isBlendSupported=!this.isRenderFloat32Supported||this.checkFloat32Blend(),this.maxTextureSize=e.getParameter(e.MAX_TEXTURE_SIZE),this.maxTextureImageUnits=e.getParameter(e.MAX_TEXTURE_IMAGE_UNITS),this.version}getExtensions(){this.version===2?(this.colorBufferFloatExtension=this.gl.getExtension("EXT_color_buffer_float"),this.disjointTimerQueryWebgl2Extension=this.gl.getExtension("EXT_disjoint_timer_query_webgl2")):(this.textureFloatExtension=this.gl.getExtension("OES_texture_float"),this.textureHalfFloatExtension=this.gl.getExtension("OES_texture_half_float"))}checkFloatTextureAttachableToFrameBuffer(){let e=this.gl,o=e.createTexture();e.bindTexture(e.TEXTURE_2D,o);let t=this.version===2?e.RGBA32F:e.RGBA;e.texImage2D(e.TEXTURE_2D,0,t,1,1,0,e.RGBA,e.FLOAT,null);let r=e.createFramebuffer();e.bindFramebuffer(e.FRAMEBUFFER,r),e.framebufferTexture2D(e.FRAMEBUFFER,e.COLOR_ATTACHMENT0,e.TEXTURE_2D,o,0);let n=e.checkFramebufferStatus(e.FRAMEBUFFER)===e.FRAMEBUFFER_COMPLETE;return e.bindTexture(e.TEXTURE_2D,null),e.bindFramebuffer(e.FRAMEBUFFER,null),e.deleteTexture(o),e.deleteFramebuffer(r),n}checkRenderFloat32(){if(this.version===2){if(!this.colorBufferFloatExtension)return!1}else if(!this.textureFloatExtension)return!1;return this.isFloatTextureAttachableToFrameBuffer}checkFloat32Download(){if(this.version===2){if(!this.colorBufferFloatExtension)return!1}else if(!this.textureFloatExtension||!this.gl.getExtension("WEBGL_color_buffer_float"))return!1;return this.isFloatTextureAttachableToFrameBuffer}checkFloat32Blend(){let e=this.gl,o,t,r,n,s;try{o=e.createTexture(),t=e.createFramebuffer(),e
as ORT format: ${n}`)}}loadFromOnnxFormat(e,o){let t=Nf.onnx.ModelProto.decode(e);if(xt.longToNumber(t.irVersion)<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=t.opsetImport.map(n=>({domain:n.domain,version:xt.longToNumber(n.version)})),this._graph=Wo.from(t.graph,o)}loadFromOrtFormat(e,o){let t=new b.ByteBuffer(e),r=Yh.InferenceSession.getRootAsInferenceSession(t).model();if(xt.longToNumber(r.irVersion())<3)throw new Error("only support ONNX model with IR_VERSION>=3");this._opsets=[];for(let s=0;s<r.opsetImportLength();s++){let a=r.opsetImport(s);this._opsets.push({domain:a?.domain(),version:xt.longToNumber(a.version())})}this._graph=Wo.from(r.graph(),o)}get graph(){return this._graph}get opsets(){return this._opsets}}});var hn,Rf=x(()=>{"use strict";$f();kf();It();Cf();hn=class{constructor(e={}){this._initialized=!1,this.backendHint=e.backendHint,this.profiler=Dr.create(e.profiler),this.context={profiler:this.profiler,graphInputTypes:[],graphInputDims:[]}}get inputNames(){return this._model.graph.getInputNames()}get outputNames(){return this._model.graph.getOutputNames()}startProfiling(){this.profiler.start()}endProfiling(){this.profiler.stop()}async loadModel(e,o,t){await this.profiler.event("session","Session.loadModel",async()=>{let r=await Mo(this.backendHint);if(this.sessionHandler=r.createSessionHandler(this.context),this._model=new dn,typeof e=="string"){let n=e.endsWith(".ort");{let a=await(await fetch(e)).arrayBuffer();this.initialize(new Uint8Array(a),n)}}else if(ArrayBuffer.isView(e))this.initialize(e);else{let n=new Uint8Array(e,o||0,t||e.byteLength);this.initialize(n)}})}initialize(e,o){if(this._initialized)throw new Error("already initialized");this.profiler.event("session","Session.initialize",()=>{let t=this.sessionHandler.transformGraph?this.sessionHandler:void 0;this._model.load(e,t,o),this.sessionHandler.onGraphInitialized&&this.sessionHandler.onGraphInitialized(this._model.graph),this.initializeOps(this._model.graph),this._executionPlan=new fn(this._model.graph,this._ops,this.profiler)}),this._initialized=!0}async run(e){if(!this._initialized)throw new Error("session not initialized yet");return this.profiler.event("session","Session.run",async()=>{let o=this.normalizeAndValidateInputs(e),t=await this._executionPlan.execute(this.sessionHandler,o);return this.createOutput(t)})}normalizeAndValidateInputs(e){let o=this._model.graph.getInputNames();if(Array.isArray(e)){if(e.length!==o.length)throw new Error(`incorrect input array length: expected ${o.length} but got ${e.length}`)}else{if(e.size!==o.length)throw new Error(`incorrect input map size: expected ${o.length} but got ${e.size}`);let t=new Array(e.size),r=0;for(let n=0;n<o.length;++n){let s=e.get(o[n]);if(!s)throw new Error(`missing input tensor for: '${name}'`);t[r++]=s}e=t}if(!this.context.graphInputTypes||this.context.graphInputTypes.length===0||!this.context.graphInputDims||this.context.graphInputDims.length===0){let t=this._model.graph.getInputIndices(),r=this._model.graph.getValues(),n=new Array(t.length);for(let s=0;s<t.length;++s){let a=r[t[s]];n[s]=a.type.shape.dims,this.context.graphInputTypes.push(a.type.tensorType),this.context.graphInputDims.push(e[s].dims)}this.validateInputTensorDims(n,e,!0)}else this.validateInputTensorDims(this.context.graphInputDims,e,!1);return this.validateInputTensorTypes(this.context.graphInputTypes,e),e}validateInputTensorTypes(e,o){for(let t=0;t<o.length;t++){let r=e[t],n=o[t].type;if(r!==n)throw new Error(`input tensor[${t}] check failed: expected type '${r}' but got ${n}`)}}validateInputTensorDims(e,o,t){for(let r=0;r<o.length;r++){let n=e[r],s=o[r].dims;if(!this.compareTensorDims(n,s,t))throw new Error(`input tensor[${r}] check failed: expected shape '[${n.join(",")}]' but got [${s.join(",")}]`)}}compareTensorDims(e,o,t){if(e.length!==o.length)return!1;for(let r=0;r<e.length;++r)if(e[r]!==o[r]&&(!t||e[r]!==0))return!1;return!0}createOutput(e){let o=this._model.graph.getOutputNames();if(e.length!==o.length)throw new Error("expected number of outputs do not match number of
/*! Bundled license information:
long/index.js:
(**
* @license
* Copyright 2009 The Closure Library Authors
* Copyright 2020 Daniel Wirtz / The long.js Authors.
*
* 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.
*
* SPDX-License-Identifier: Apache-2.0
*)
*/
typeof exports=="object"&&typeof module=="object"&&(module.exports=ort);
//# sourceMappingURL=ort.min.js.map