= "+l[u-1]+`) {
+ return getChannel(
+ getT`+u+"("+sl(o,c,f)+`),
+ vec2(`+sl(h,c,f)+`));
+ }`}var m=l.length,g=l[l.length-1];p+=`
+ return getChannel(
+ getT`+m+"("+sl(o,c,g)+`),
+ vec2(`+sl(h,c,g)+"));",this.userCode=`
+ float getValue(`+o.map(function(v){return"int "+v})+`) {
+ `+p+`
+ }
+
+ void main() {
+ `+a+` coords = getOutputCoords();
+ vec4 result = vec4(getValue(`+s+`), 0., 0., 0.);
+
+ `+s[r-1]+" = "+s[r-1]+` + 1;
+ if (`+s[r-1]+" < "+i[r-1]+`) {
+ result.g = getValue(`+s+`);
+ }
+
+ `+s[r-2]+" = "+s[r-2]+` + 1;
+ if (`+s[r-2]+" < "+i[r-2]+`) {
+ result.a = getValue(`+s+`);
+ }
+
+ `+s[r-1]+" = "+s[r-1]+` - 1;
+ if (`+s[r-2]+" < "+i[r-2]+` &&
+ `+s[r-1]+" < "+i[r-1]+`) {
+ result.b = getValue(`+s+`);
+ }
+ setOutput(result);
+ }
+ `}return n}();function sl(n,t,e){var i=n.indexOf(t),r=n.map(function(a,s){return s===i?a+" - "+e:a});return r.join()}var X5=function(){function n(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideHeight,i=t.strideWidth,r=t.padInfo.top,a=t.padInfo.left,s=t.dataFormat==="channelsLast";this.userCode=`
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int wR = coords.x;
+ int wC = coords.y;
+ int d1 = coords.z;
+ int d2 = coords.w;
+
+ // Convolve x(?, ?, d1) with dy(:, :, d2) to get dw(wR, wC, d1, d2).
+ // ? = to be determined. : = across all values in that axis.
+ float dotProd = 0.0;
+
+ for (int b = 0; b < `+t.batchSize+`; b++) {
+ for (int yR = 0; yR < `+t.outHeight+`; yR++) {
+ int xR = wR + yR * `+e+" - "+r+`;
+
+ if (xR < 0 || xR >= `+t.inHeight+`) {
+ continue;
+ }
+
+ for (int yC = 0; yC < `+t.outWidth+`; yC++) {
+ int xC = wC + yC * `+i+" - "+a+`;
+
+ if (xC < 0 || xC >= `+t.inWidth+`) {
+ continue;
+ }
+
+ if (`+s+`) {
+ float dyValue = getDy(b, yR, yC, d2);
+ float xValue = getX(b, xR, xC, d1);
+ dotProd += (xValue * dyValue);
+ } else {
+ float dyValue = getDy(b, d2, yR, yC);
+ float xValue = getX(b, d1, xR, xC);
+ dotProd += (xValue * dyValue);
+ }
+
+ }
+ }
+ }
+ setOutput(dotProd);
+ }
+ `}return n}(),J5=function(){function n(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterHeight,i=t.filterWidth,r=t.strideHeight,a=t.strideWidth,s=t.dataFormat==="channelsLast",o=e-1-t.padInfo.top,l=i-1-t.padInfo.left,u=s?1:2,c=s?2:3,h=s?3:1;this.userCode=`
+ const ivec2 pads = ivec2(`+o+", "+l+`);
+
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int batch = coords[0];
+ int d1 = coords[`+h+`];
+
+ ivec2 dyCorner = ivec2(coords[`+u+"], coords["+c+`]) - pads;
+ int dyRCorner = dyCorner.x;
+ int dyCCorner = dyCorner.y;
+
+ // Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
+ // ? = to be determined. : = across all values in that axis.
+ float dotProd = 0.0;
+ for (int wR = 0; wR < `+e+`; wR++) {
+ float dyR = float(dyRCorner + wR) / `+r+`.0;
+
+ if (dyR < 0.0 || dyR >= `+t.outHeight+`.0 || fract(dyR) > 0.0) {
+ continue;
+ }
+ int idyR = int(dyR);
+
+ int wRPerm = `+e+` - 1 - wR;
+
+ for (int wC = 0; wC < `+i+`; wC++) {
+ float dyC = float(dyCCorner + wC) / `+a+`.0;
+
+ if (dyC < 0.0 || dyC >= `+t.outWidth+`.0 ||
+ fract(dyC) > 0.0) {
+ continue;
+ }
+ int idyC = int(dyC);
+
+ int wCPerm = `+i+` - 1 - wC;
+
+ for (int d2 = 0; d2 < `+t.outChannels+`; d2++) {
+
+ if (`+s+`) {
+ float xValue = getDy(batch, idyR, idyC, d2);
+ float wValue = getW(wRPerm, wCPerm, d1, d2);
+ dotProd += xValue * wValue;
+ } else {
+ float xValue = getDy(batch, d2, idyR, idyC);
+ float wValue = getW(wRPerm, wCPerm, d1, d2);
+ dotProd += xValue * wValue;
+ }
+
+ }
+ }
+ }
+ setOutput(dotProd);
+ }
+ `}return n}(),Z5=function(){function n(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideDepth,i=t.strideHeight,r=t.strideWidth,a=t.padInfo.front,s=t.padInfo.top,o=t.padInfo.left;this.userCode=`
+ void main() {
+ ivec5 coords = getOutputCoords();
+ int wF = coords.x;
+ int wR = coords.y;
+ int wC = coords.z;
+ int d1 = coords.w;
+ int d2 = coords.u;
+
+ float dotProd = 0.0;
+
+ for (int b = 0; b < `+t.batchSize+`; b++) {
+ for (int yF = 0; yF < `+t.outDepth+`; yF++) {
+ int xF = wF + yF * `+e+" - "+a+`;
+
+ if (xF < 0 || xF >= `+t.inDepth+`) {
+ continue;
+ }
+
+ for (int yR = 0; yR < `+t.outHeight+`; yR++) {
+ int xR = wR + yR * `+i+" - "+s+`;
+
+ if (xR < 0 || xR >= `+t.inHeight+`) {
+ continue;
+ }
+
+ for (int yC = 0; yC < `+t.outWidth+`; yC++) {
+ int xC = wC + yC * `+r+" - "+o+`;
+
+ if (xC < 0 || xC >= `+t.inWidth+`) {
+ continue;
+ }
+
+ float dyValue = getDy(b, yF, yR, yC, d2);
+ float xValue = getX(b, xF, xR, xC, d1);
+ dotProd += (xValue * dyValue);
+ }
+ }
+ }
+ }
+ setOutput(dotProd);
+ }
+ `}return n}(),Q5=function(){function n(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterDepth,i=t.filterHeight,r=t.filterWidth,a=t.strideDepth,s=t.strideHeight,o=t.strideWidth,l=e-1-t.padInfo.front,u=i-1-t.padInfo.top,c=r-1-t.padInfo.left;this.userCode=`
+ const ivec3 pads = ivec3(`+l+", "+u+", "+c+`);
+
+ void main() {
+ ivec5 coords = getOutputCoords();
+ int batch = coords.x;
+ int d1 = coords.u;
+
+
+ ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
+ int dyFCorner = dyCorner.x;
+ int dyRCorner = dyCorner.y;
+ int dyCCorner = dyCorner.z;
+
+ float dotProd = 0.0;
+ for (int wF = 0; wF < `+e+`; wF++) {
+ float dyF = float(dyFCorner + wF) / `+a+`.0;
+
+ if (dyF < 0.0 || dyF >= `+t.outDepth+`.0 || fract(dyF) > 0.0) {
+ continue;
+ }
+ int idyF = int(dyF);
+
+ int wFPerm = `+e+` - 1 - wF;
+
+ for (int wR = 0; wR < `+i+`; wR++) {
+ float dyR = float(dyRCorner + wR) / `+s+`.0;
+
+ if (dyR < 0.0 || dyR >= `+t.outHeight+`.0 ||
+ fract(dyR) > 0.0) {
+ continue;
+ }
+ int idyR = int(dyR);
+
+ int wRPerm = `+i+` - 1 - wR;
+
+ for (int wC = 0; wC < `+r+`; wC++) {
+ float dyC = float(dyCCorner + wC) / `+o+`.0;
+
+ if (dyC < 0.0 || dyC >= `+t.outWidth+`.0 ||
+ fract(dyC) > 0.0) {
+ continue;
+ }
+ int idyC = int(dyC);
+
+ int wCPerm = `+r+` - 1 - wC;
+
+ for (int d2 = 0; d2 < `+t.outChannels+`; d2++) {
+ float xValue = getDy(batch, idyF, idyR, idyC, d2);
+ float wValue = getW(wFPerm, wRPerm, wCPerm, d1, d2);
+ dotProd += xValue * wValue;
+ }
+ }
+ }
+ }
+ setOutput(dotProd);
+ }
+ `}return n}();var e9=function(){function n(t){this.variableNames=["x","dy"],this.outputShape=t.filterShape;var e=t.strideHeight,i=t.strideWidth,r=t.padInfo.top,a=t.padInfo.left,s=t.outChannels/t.inChannels;this.userCode=`
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int wR = coords.x;
+ int wC = coords.y;
+ int d1 = coords.z;
+ int dm = coords.w;
+ int d2 = d1 * `+s+` + dm;
+
+ float dotProd = 0.0;
+
+ // TO DO: Vec4 over the batch size
+ for (int b = 0; b < `+t.batchSize+`; b++) {
+ for (int yR = 0; yR < `+t.outHeight+`; yR++) {
+ int xR = wR + yR * `+e+" - "+r+`;
+
+ if (xR < 0 || xR >= `+t.inHeight+`) {
+ continue;
+ }
+
+ for (int yC = 0; yC < `+t.outWidth+`; yC++) {
+ int xC = wC + yC * `+i+" - "+a+`;
+
+ if (xC < 0 || xC >= `+t.inWidth+`) {
+ continue;
+ }
+
+ float dyValue = getDy(b, yR, yC, d2);
+ float xValue = getX(b, xR, xC, d1);
+ dotProd += (xValue * dyValue);
+ }
+ }
+ }
+ setOutput(dotProd);
+ }
+ `}return n}(),t9=function(){function n(t){this.variableNames=["dy","W"],this.outputShape=t.inShape;var e=t.filterHeight,i=t.filterWidth,r=t.strideHeight,a=t.strideWidth,s=e-1-t.padInfo.top,o=i-1-t.padInfo.left,l=t.outChannels/t.inChannels;this.userCode=`
+ const ivec2 pads = ivec2(`+s+", "+o+`);
+
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int batch = coords[0];
+ int d1 = coords[3];
+ ivec2 dyCorner = coords.yz - pads;
+ int dyRCorner = dyCorner.x;
+ int dyCCorner = dyCorner.y;
+
+ float dotProd = 0.0;
+
+ for (int wR = 0; wR < `+e+`; wR++) {
+ float dyR = float(dyRCorner + wR) / `+r+`.0;
+
+ if (dyR < 0.0 || dyR >= `+t.outHeight+`.0 || fract(dyR) > 0.0) {
+ continue;
+ }
+ int idyR = int(dyR);
+
+ int wRPerm = `+e+` - 1 - wR;
+
+ for (int wC = 0; wC < `+i+`; wC++) {
+ float dyC = float(dyCCorner + wC) / `+a+`.0;
+
+ if (dyC < 0.0 || dyC >= `+t.outWidth+`.0 ||
+ fract(dyC) > 0.0) {
+ continue;
+ }
+ int idyC = int(dyC);
+
+ int wCPerm = `+i+` - 1 - wC;
+
+ // TO DO: Vec4 over the channelMul
+ for (int dm = 0; dm < `+l+`; dm++) {
+ int d2 = d1 * `+l+` + dm;
+ float xValue = getDy(batch, idyR, idyC, d2);
+ float wValue = getW(wRPerm, wCPerm, d1, dm);
+ dotProd += xValue * wValue;
+ }
+ }
+ }
+ setOutput(dotProd);
+ }
+ `}return n}();var kS=function(){function n(t,e,i,r){e===void 0&&(e=!1),i===void 0&&(i=null),r===void 0&&(r=!1),this.variableNames=["x","W"],this.outputShape=t.outShape;var a=t.padInfo.top,s=t.padInfo.left,o=t.strideHeight,l=t.strideWidth,u=t.dilationHeight,c=t.dilationWidth,h=t.filterHeight,d=t.filterWidth,p=Math.floor(t.inChannels/4)*4,f=t.inChannels%4,m=t.dataFormat==="channelsLast",g=m?1:2,v=m?2:3,b=m?3:1,w="",S="";i&&(r?w=`float activation(float a) {
+ float b = getPreluActivationWeightsAtOutCoords();
+ `+i+`
+ }`:w=`
+ float activation(float x) {
+ `+i+`
+ }
+ `,S="result = activation(result);");var L=e?"result += getBiasAtOutCoords();":"";e&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),this.userCode=`
+ `+w+`
+
+ const ivec2 strides = ivec2(`+o+", "+l+`);
+ const ivec2 pads = ivec2(`+a+", "+s+`);
+
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int batch = coords[0];
+ int d2 = coords[`+b+`];
+
+ ivec2 xRCCorner =
+ ivec2(coords[`+g+"], coords["+v+`]) * strides - pads;
+ int xRCorner = xRCCorner.x;
+ int xCCorner = xRCCorner.y;
+
+ // Convolve x(?, ?, d1) with w(:, :, d1, d2) to get y(yR, yC, d2).
+ // ? = to be determined. : = across all values in that axis.
+ float dotProd = 0.0;
+ for (int wR = 0; wR < `+h+`; wR++) {
+ int xR = xRCorner + wR * `+u+`;
+
+ if (xR < 0 || xR >= `+t.inHeight+`) {
+ continue;
+ }
+
+ for (int wC = 0; wC < `+d+`; wC++) {
+ int xC = xCCorner + wC * `+c+`;
+
+ if (xC < 0 || xC >= `+t.inWidth+`) {
+ continue;
+ }
+
+ for (int d1 = 0; d1 < `+p+`; d1 += 4) {
+ vec4 wValues = vec4(
+ getW(wR, wC, d1, d2),
+ getW(wR, wC, d1 + 1, d2),
+ getW(wR, wC, d1 + 2, d2),
+ getW(wR, wC, d1 + 3, d2)
+ );
+
+ if (`+m+`) {
+ vec4 xValues = vec4(
+ getX(batch, xR, xC, d1),
+ getX(batch, xR, xC, d1 + 1),
+ getX(batch, xR, xC, d1 + 2),
+ getX(batch, xR, xC, d1 + 3)
+ );
+ dotProd += dot(xValues, wValues);
+ } else {
+ vec4 xValues = vec4(
+ getX(batch, d1, xR, xC),
+ getX(batch, d1 + 1, xR, xC),
+ getX(batch, d1 + 2, xR, xC),
+ getX(batch, d1 + 3, xR, xC)
+ );
+ dotProd += dot(xValues, wValues);
+ }
+ }
+
+ if (`+(f===1)+`) {
+
+ if (`+m+`) {
+ dotProd +=
+ getX(batch, xR, xC, `+p+`) *
+ getW(wR, wC, `+p+`, d2);
+ } else {
+ dotProd +=
+ getX(batch, `+p+`, xR, xC) *
+ getW(wR, wC, `+p+`, d2);
+ }
+
+ } else if (`+(f===2)+`) {
+ vec2 wValues = vec2(
+ getW(wR, wC, `+p+`, d2),
+ getW(wR, wC, `+p+` + 1, d2)
+ );
+
+ if (`+m+`) {
+ vec2 xValues = vec2(
+ getX(batch, xR, xC, `+p+`),
+ getX(batch, xR, xC, `+p+` + 1)
+ );
+ dotProd += dot(xValues, wValues);
+ } else {
+ vec2 xValues = vec2(
+ getX(batch, `+p+`, xR, xC),
+ getX(batch, `+p+` + 1, xR, xC)
+ );
+ dotProd += dot(xValues, wValues);
+ }
+
+ } else if (`+(f===3)+`) {
+ vec3 wValues = vec3(
+ getW(wR, wC, `+p+`, d2),
+ getW(wR, wC, `+p+` + 1, d2),
+ getW(wR, wC, `+p+` + 2, d2)
+ );
+
+ if (`+m+`) {
+ vec3 xValues = vec3(
+ getX(batch, xR, xC, `+p+`),
+ getX(batch, xR, xC, `+p+` + 1),
+ getX(batch, xR, xC, `+p+` + 2)
+ );
+ dotProd += dot(xValues, wValues);
+ } else {
+ vec3 xValues = vec3(
+ getX(batch, `+p+`, xR, xC),
+ getX(batch, `+p+` + 1, xR, xC),
+ getX(batch, `+p+` + 2, xR, xC)
+ );
+ dotProd += dot(xValues, wValues);
+ }
+
+ }
+ }
+ }
+
+ float result = dotProd;
+ `+L+`
+ `+S+`
+ setOutput(result);
+ }
+ `}return n}(),n9=function(){function n(t){this.variableNames=["x","W"],this.outputShape=t.outShape;var e=t.padInfo.front,i=t.padInfo.top,r=t.padInfo.left,a=t.strideDepth,s=t.strideHeight,o=t.strideWidth,l=t.dilationDepth,u=t.dilationHeight,c=t.dilationWidth,h=t.filterDepth,d=t.filterHeight,p=t.filterWidth,f=Math.floor(t.inChannels/4)*4,m=t.inChannels%4;this.userCode=`
+ const ivec3 strides = ivec3(`+a+", "+s+", "+o+`);
+ const ivec3 pads = ivec3(`+e+", "+i+", "+r+`);
+
+ void main() {
+ ivec5 coords = getOutputCoords();
+ int batch = coords.x;
+ int d2 = coords.u;
+
+ ivec3 xFRCCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
+ int xFCorner = xFRCCorner.x;
+ int xRCorner = xFRCCorner.y;
+ int xCCorner = xFRCCorner.z;
+
+ // Convolve x(?, ?, ?, d1) with w(:, :, :, d1, d2) to get
+ // y(yF, yR, yC, d2). ? = to be determined. : = across all
+ // values in that axis.
+ float dotProd = 0.0;
+ for (int wF = 0; wF < `+h+`; wF++) {
+ int xF = xFCorner + wF * `+l+`;
+
+ if (xF < 0 || xF >= `+t.inDepth+`) {
+ continue;
+ }
+
+ for (int wR = 0; wR < `+d+`; wR++) {
+ int xR = xRCorner + wR * `+u+`;
+
+ if (xR < 0 || xR >= `+t.inHeight+`) {
+ continue;
+ }
+
+ for (int wC = 0; wC < `+p+`; wC++) {
+ int xC = xCCorner + wC * `+c+`;
+
+ if (xC < 0 || xC >= `+t.inWidth+`) {
+ continue;
+ }
+
+ for (int d1 = 0; d1 < `+f+`; d1 += 4) {
+ vec4 xValues = vec4(
+ getX(batch, xF, xR, xC, d1),
+ getX(batch, xF, xR, xC, d1 + 1),
+ getX(batch, xF, xR, xC, d1 + 2),
+ getX(batch, xF, xR, xC, d1 + 3)
+ );
+ vec4 wValues = vec4(
+ getW(wF, wR, wC, d1, d2),
+ getW(wF, wR, wC, d1 + 1, d2),
+ getW(wF, wR, wC, d1 + 2, d2),
+ getW(wF, wR, wC, d1 + 3, d2)
+ );
+
+ dotProd += dot(xValues, wValues);
+ }
+
+ if (`+(m===1)+`) {
+ dotProd +=
+ getX(batch, xF, xR, xC, `+f+`) *
+ getW(wF, wR, wC, `+f+`, d2);
+ } else if (`+(m===2)+`) {
+ vec2 xValues = vec2(
+ getX(batch, xF, xR, xC, `+f+`),
+ getX(batch, xF, xR, xC, `+f+` + 1)
+ );
+ vec2 wValues = vec2(
+ getW(wF, wR, wC, `+f+`, d2),
+ getW(wF, wR, wC, `+f+` + 1, d2)
+ );
+ dotProd += dot(xValues, wValues);
+ } else if (`+(m===3)+`) {
+ vec3 xValues = vec3(
+ getX(batch, xF, xR, xC, `+f+`),
+ getX(batch, xF, xR, xC, `+f+` + 1),
+ getX(batch, xF, xR, xC, `+f+` + 2)
+ );
+ vec3 wValues = vec3(
+ getW(wF, wR, wC, `+f+`, d2),
+ getW(wF, wR, wC, `+f+` + 1, d2),
+ getW(wF, wR, wC, `+f+` + 2, d2)
+ );
+ dotProd += dot(xValues, wValues);
+ }
+ }
+ }
+ }
+ setOutput(dotProd);
+ }
+ `}return n}();var FS=function(){function n(t,e,i,r){e===void 0&&(e=!1),i===void 0&&(i=null),r===void 0&&(r=!1),this.variableNames=["x","W"],this.outputShape=t.outShape;var a=t.inHeight,s=t.inWidth,o=t.padInfo.top,l=t.padInfo.left,u=t.strideHeight,c=t.strideWidth,h=t.dilationHeight,d=t.dilationWidth,p=t.filterHeight,f=t.filterWidth,m=t.outChannels/t.inChannels,g="",v="";i&&(r?g=`float activation(float a) {
+ float b = getPreluActivationWeightsAtOutCoords();
+ `+i+`
+ }`:g=`
+ float activation(float x) {
+ `+i+`
+ }
+ `,v="result = activation(result);");var b=e?"result += getBiasAtOutCoords();":"";e&&this.variableNames.push("bias"),r&&this.variableNames.push("preluActivationWeights"),this.userCode=`
+ `+g+`
+
+ const ivec2 strides = ivec2(`+u+", "+c+`);
+ const ivec2 pads = ivec2(`+o+", "+l+`);
+
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int batch = coords.x;
+ ivec2 xRCCorner = coords.yz * strides - pads;
+ int d2 = coords.w;
+ int d1 = d2 / `+m+`;
+ int q = d2 - d1 * `+m+`;
+
+ int xRCorner = xRCCorner.x;
+ int xCCorner = xRCCorner.y;
+
+ // Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2).
+ // ? = to be determined. : = across all values in that axis.
+ float dotProd = 0.0;
+ // TO DO(dsmilkov): Flatten the two for loops and vec4 the operations.
+ for (int wR = 0; wR < `+p+`; wR++) {
+ int xR = xRCorner + wR * `+h+`;
+
+ if (xR < 0 || xR >= `+a+`) {
+ continue;
+ }
+
+ for (int wC = 0; wC < `+f+`; wC++) {
+ int xC = xCCorner + wC * `+d+`;
+
+ if (xC < 0 || xC >= `+s+`) {
+ continue;
+ }
+
+ float xVal = getX(batch, xR, xC, d1);
+ float wVal = getW(wR, wC, d1, q);
+ dotProd += xVal * wVal;
+ }
+ }
+
+ float result = dotProd;
+ `+b+`
+ `+v+`
+ setOutput(result);
+ }
+ `}return n}();var WS=function(){function n(t,e,i,r){e===void 0&&(e=!1),i===void 0&&(i=null),r===void 0&&(r=!1),this.variableNames=["x","W"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t.outShape;for(var a=t.inHeight,s=t.inWidth,o=t.padInfo.top,l=t.padInfo.left,u=t.strideHeight,c=t.strideWidth,h=t.dilationHeight,d=t.dilationWidth,p=t.filterHeight,f=t.filterWidth,m=f,g="int xR; int xC; int xCOffset;",v=0;v= 0 && xR < `+a+" && xCOffset >= 0 && xCOffset < "+s+`) {
+ xTexelR`+v+"C"+b+` = getX(batch, xR, xCOffset, d1);
+
+ // Need to manually clear unused channels in case
+ // we're reading from recycled texture.
+ if(xCOffset + 1 >= `+s+`) {
+ xTexelR`+v+"C"+b+`.zw = vec2(0.);
+ }
+ } else {
+ xTexelR`+v+"C"+b+` = vec4(0.);
+ }
+
+ xCOffset = xC + 1 - 2;
+ if(xR >= 0 && xR < `+a+" && xCOffset >= 0 && xCOffset < "+s+`) {
+ vec4 previous = getX(batch, xR, xCOffset, d1);
+
+ // Need to manually clear unused channels in case
+ // we're reading from recycled texture.
+ if(xCOffset + 1 >= `+s+`) {
+ previous.zw = vec2(0.);
+ }
+
+ xR`+v+"C"+b+" = vec4(previous.zw, xTexelR"+v+"C"+b+`.xy);
+ } else {
+ xR`+v+"C"+b+" = vec4(0, 0, xTexelR"+v+"C"+b+`.xy);
+ }
+ `:g+=`
+ if(xR >= 0 && xR < `+a+" && xC >= 0 && xC < "+s+`) {
+ xTexelR`+v+"C"+b+` = getX(batch, xR, xC, d1);
+ } else {
+ xTexelR`+v+"C"+b+` = vec4(0.);
+ }
+
+ xR`+v+"C"+b+" = xTexelR"+v+"C"+b+`;
+ `,b+1= 0 && xR < `+a+` &&
+ xCOffset >= 0 && xCOffset < `+s+`) {
+ xTexelR`+v+"C"+(b+2)+` = getX(batch, xR, xCOffset, d1);
+ }
+ `,d>1&&(g+=`
+ xCOffset -= 2;
+ if(xR >= 0 && xR < `+a+` &&
+ xCOffset >= 0 && xCOffset < `+s+`) {
+ xTexelR`+v+"C"+b+` = getX(batch, xR, xCOffset, d1);
+ } else {
+ xTexelR`+v+"C"+b+` = vec4(0.);
+ }
+ `),g+=`
+ xR`+v+"C"+(b+1)+` = vec4(
+ xTexelR`+v+"C"+b+".zw, xTexelR"+v+"C"+(b+2)+`.xy);
+ `):g+=`
+ xCOffset = xC + `+S+`;
+
+ if(xR >= 0 && xR < `+a+` &&
+ xCOffset >= 0 && xCOffset < `+s+`) {
+ xTexelR`+v+"C"+(b+2)+` = getX(batch, xR, xCOffset, d1);
+ }
+
+ xR`+v+"C"+(b+1)+" = xTexelR"+v+"C"+(b+2)+`;
+ `}}else b= 0 && xR < `+a+`) {
+ `,l%2===1?(g+=`
+ xCOffset = xC + 1 - `+c+`;
+ if(xCOffset >= 0 && xCOffset < `+s+`) {
+ xTexelR`+v+"C"+b+` = getX(batch, xR, xCOffset, d1);
+ } else {
+ xTexelR`+v+"C"+b+` = vec4(0.);
+ }
+
+ if(xC + 1 >= 0 && xC + 1 < `+s+`) {
+ xTexelR`+v+"C"+(b+2)+` = getX(batch, xR, xC + 1, d1);
+ } else {
+ xTexelR`+v+"C"+(b+2)+` = vec4(0.);
+ }
+
+ xR`+v+"C"+b+` = vec4(
+ xTexelR`+v+"C"+b+".zw, xTexelR"+v+"C"+(b+2)+`.zw);
+ `,b+1= 0 && xCOffset < `+s+`) {
+ final = getX(batch, xR, xCOffset, d1);
+ }
+ xR`+v+"C"+(b+1)+" = vec4(xTexelR"+v+"C"+(b+2)+`.xy, final.xy);
+ `)):(g+=`
+ if(xC >= 0 && xC < `+s+`) {
+ xTexelR`+v+"C"+b+` = getX(batch, xR, xC, d1);
+ } else {
+ xTexelR`+v+"C"+b+` = vec4(0.);
+ }
+
+ xCOffset = xC + `+c+`;
+ if(xCOffset >= 0 && xCOffset < `+s+`) {
+ xTexelR`+v+"C"+(b+2)+` = getX(batch, xR, xCOffset, d1);
+ } else {
+ xTexelR`+v+"C"+(b+2)+` = vec4(0.);
+ }
+
+ xR`+v+"C"+b+` = vec4(
+ xTexelR`+v+"C"+b+".xy, xTexelR"+v+"C"+(b+2)+`.xy);
+ `,b+11?[""+(o-1)/(h-1),"(y2-y1) * height_ratio","y1*"+m+" + float(y)*(height_scale)"]:["0.0","0.0","0.5 * (y1+y2) * "+m],b=v[0],w=v[1],S=v[2],L=d>1?[""+(l-1)/(d-1),"(x2-x1) * width_ratio","x1*"+g+" + float(x)*(width_scale)"]:["0.0","0.0","0.5 * (x1+x2) * "+g],N=L[0],C=L[1],R=L[2];this.userCode=`
+ const float height_ratio = float(`+b+`);
+ const float width_ratio = float(`+N+`);
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords[0];
+ int y = coords[1];
+ int x = coords[2];
+ int d = coords[3];
+
+ // get box vals
+ float y1 = getBoxes(b,0);
+ float x1 = getBoxes(b,1);
+ float y2 = getBoxes(b,2);
+ float x2 = getBoxes(b,3);
+
+ // get image in batch index
+ int bInd = round(getBoxInd(b));
+ if(bInd < 0 || bInd >= `+s+`) {
+ return;
+ }
+
+ float height_scale = `+w+`;
+ float width_scale = `+C+`;
+
+ float in_y = `+S+`;
+ if( in_y < 0.0 || in_y > `+m+` ) {
+ setOutput(float(`+a+`));
+ return;
+ }
+ float in_x = `+R+`;
+ if( in_x < 0.0 || in_x > `+g+` ) {
+ setOutput(float(`+a+`));
+ return;
+ }
+
+ vec2 sourceFracIndexCR = vec2(in_x,in_y);
+ if(`+p+` == 1) {
+ // Compute the four integer indices.
+ ivec2 sourceFloorCR = ivec2(sourceFracIndexCR);
+ ivec2 sourceCeilCR = ivec2(ceil(sourceFracIndexCR));
+
+ float topLeft = getImage(b, sourceFloorCR.y, sourceFloorCR.x, d);
+ float bottomLeft = getImage(b, sourceCeilCR.y, sourceFloorCR.x, d);
+ float topRight = getImage(b, sourceFloorCR.y, sourceCeilCR.x, d);
+ float bottomRight = getImage(b, sourceCeilCR.y, sourceCeilCR.x, d);
+
+ vec2 fracCR = sourceFracIndexCR - vec2(sourceFloorCR);
+
+ float top = topLeft + (topRight - topLeft) * fracCR.x;
+ float bottom = bottomLeft + (bottomRight - bottomLeft) * fracCR.x;
+ float newValue = top + (bottom - top) * fracCR.y;
+ setOutput(newValue);
+ } else {
+ // Compute the coordinators of nearest neighbor point.
+ ivec2 sourceNearestCR = ivec2(floor(
+ sourceFracIndexCR + vec2(0.5,0.5)));
+ float newValue = getImage(b, sourceNearestCR.y, sourceNearestCR.x, d);
+ setOutput(newValue);
+ }
+ }
+ `}return n}(),zS=function(){function n(t,e,i){this.variableNames=["x"],this.outputShape=t;var r=t.length,a=e?"0.0":"getX("+US(r,"coords")+")",s=t[t.length-1],o="",l="";e?(o=i?"end != "+(s-1):"end != 0",l=i?"end + 1":"end - 1"):(o=i?"end + pow2 < "+s:"end >= pow2",l=i?"end + pow2":"end - pow2"),this.userCode=`
+ uniform float index;
+ void main() {
+ `+Je(r)+` coords = getOutputCoords();
+ int end = `+BS(r,"coords")+`;
+ float val = `+a+`;
+ int pow2 = int(pow(2.0, index));
+ if (`+o+`) {
+ int idx = `+l+`;
+ `+BS(r,"coords")+` = idx;
+ val += getX(`+US(r,"coords")+`);
+ }
+ setOutput(val);
+ }
+ `}return n.prototype.getCustomSetupFunc=function(t){var e=this;return function(i,r){e.index==null&&(e.index=i.getUniformLocation(r,"index")),i.gl.uniform1f(e.index,t)}},n}();function US(n,t){if(n===1)return""+t;if(n===2)return t+".x, "+t+".y";if(n===3)return t+".x, "+t+".y, "+t+".z";if(n===4)return t+".x, "+t+".y, "+t+".z, "+t+".w";throw Error("Cumulative sum for rank "+n+" is not yet supported")}function BS(n,t){if(n===1)return""+t;if(n===2)return t+".y";if(n===3)return t+".z";if(n===4)return t+".w";throw Error("Cumulative sum for rank "+n+" is not yet supported")}var r9=function(){function n(t){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.outPackingScheme=ms.DENSE;var e=vs(t),i=Wt();this.outputShape=t,this.userCode=`
+ ivec3 outCoordsFromFlatIndex(int index) {
+ `+wr(["r","c","d"],t)+`
+ return ivec3(r, c, d);
+ }
+
+ void main() {
+ ivec2 resTexRC = ivec2(resultUV.yx *
+ vec2(`+e[0]+", "+e[1]+`));
+ int index = 4 * (resTexRC.x * `+e[1]+` + resTexRC.y);
+
+ vec4 result = vec4(0.);
+
+ for (int i=0; i<4; i++) {
+ int flatIndex = index + i;
+ ivec3 rc = outCoordsFromFlatIndex(flatIndex);
+ result[i] = getA(rc.x, rc.y, rc.z);
+ }
+
+ `+i.output+` = result;
+ }
+ `}return n}();var a9=function(){function n(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outPackingScheme=ms.DENSE;var e=vs(t),i=Wt();this.outputShape=t,this.userCode=`
+ ivec3 outCoordsFromFlatIndex(int index) {
+ `+wr(["r","c","d"],t)+`
+ return ivec3(r, c, d);
+ }
+
+ void main() {
+ ivec2 resTexRC = ivec2(resultUV.yx *
+ vec2(`+e[0]+", "+e[1]+`));
+ int index = 4 * (resTexRC.x * `+e[1]+` + resTexRC.y);
+
+ vec4 result = vec4(0.);
+
+ for (int i=0; i<4; i++) {
+ int flatIndex = index + i;
+ ivec3 rc = outCoordsFromFlatIndex(flatIndex);
+ result[i] = getChannel(getA(rc.x, rc.y, rc.z), vec2(rc.y, rc.z));
+ }
+
+ `+i.output+` = result;
+ }
+ `}return n}();var s9=function(){function n(t,e,i){this.variableNames=["x"],this.outputShape=[],this.outputShape=t,this.blockSize=e,this.dataFormat=i,this.userCode=`
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords[0];
+ int h = `+this.getHeightCoordString()+`;
+ int w = `+this.getWidthCoordString()+`;
+ int d = `+this.getDepthCoordString()+`;
+
+ int in_h = h / `+e+`;
+ int offset_h = imod(h, `+e+`);
+ int in_w = w / `+e+`;
+ int offset_w = imod(w, `+e+`);
+ int offset_d = (offset_h * `+e+` + offset_w) *
+ `+this.getOutputDepthSize()+`;
+ int in_d = d + offset_d;
+
+ float result = `+this.getInputSamplingString()+`;
+ setOutput(result);
+ }
+ `}return n.prototype.getHeightCoordString=function(){return this.dataFormat==="NHWC"?"coords[1]":"coords[2]"},n.prototype.getWidthCoordString=function(){return this.dataFormat==="NHWC"?"coords[2]":"coords[3]"},n.prototype.getDepthCoordString=function(){return this.dataFormat==="NHWC"?"coords[3]":"coords[1]"},n.prototype.getOutputDepthSize=function(){return this.dataFormat==="NHWC"?this.outputShape[3]:this.outputShape[1]},n.prototype.getInputSamplingString=function(){return this.dataFormat==="NHWC"?"getX(b, in_h, in_w, in_d)":"getX(b, in_d, in_h, in_w)"},n}();var o9=function(){function n(t){this.variableNames=["X"],this.outputShape=[t,t],this.userCode=`
+ void main() {
+ ivec2 coords = getOutputCoords();
+ float val = coords[0] == coords[1] ? getX(coords[0]) : 0.0;
+ setOutput(val);
+ }
+ `}return n}();var l9=function(){function n(t){this.variableNames=["A"],this.outTexUsage=an.DOWNLOAD;var e=Wt();this.outputShape=t,this.userCode=`
+ `+IS+`
+
+ void main() {
+ float x = getAAtOutCoords();
+ `+e.output+` = encode_float(x);
+ }
+ `}return n}();var u9=function(){function n(t){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!1,this.outTexUsage=an.DOWNLOAD;var e=Wt();this.outputShape=t,this.userCode=`
+ `+IS+`
+
+ void main() {
+ ivec3 coords = getOutputCoords();
+ float x = getChannel(getAAtOutCoords(), vec2(coords.y, coords.z));
+ `+e.output+` = encode_float(x);
+ }
+ `}return n}();var c9=function(){function n(t,e,i){i===void 0&&(i=!1),this.variableNames=["A"];var r=Wt(),a=e[0],s=e[1];this.outputShape=t;var o="result";i&&(o="floor(result * 255. + 0.5)"),this.userCode=`
+ `+up(t)+`
+
+ void main() {
+ ivec3 coords = getOutputCoords();
+
+ int flatIndex = getFlatIndex(coords);
+ int offset = imod(flatIndex, 4);
+
+ flatIndex = idiv(flatIndex, 4, 1.);
+
+ int r = flatIndex / `+s+`;
+ int c = imod(flatIndex, `+s+`);
+ vec2 uv = (vec2(c, r) + halfCR) / vec2(`+s+".0, "+a+`.0);
+ vec4 values = `+r.texture2D+`(A, uv);
+
+ float result;
+
+ if(offset == 0) {
+ result = values[0];
+ } else if(offset == 1) {
+ result = values[1];
+ } else if(offset == 2) {
+ result = values[2];
+ } else {
+ result = values[3];
+ }
+
+ `+r.output+" = vec4("+o+`, 0., 0., 0.);
+ }
+ `}return n}();var h9=function(){function n(t,e,i){i===void 0&&(i=!1),this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0;var r=Wt(),a=e[0],s=e[1];this.outputShape=t;var o="",l="result";i&&(l="floor(result * 255. + 0.5)");for(var u=0;u<=1;u++)for(var c=0;c<=1;c++){var h=u*2+c;o+=`
+ localCoords = coords;
+ if(localCoords[2] + `+c+" < "+t[2]+`) {
+ localCoords[2] += `+c+`;
+ if(localCoords[1] + `+u+" < "+t[1]+`) {
+ localCoords[1] += `+u+`;
+
+ flatIndex = getFlatIndex(localCoords);
+ offset = imod(flatIndex, 4);
+
+ flatIndex = idiv(flatIndex, 4, 1.);
+
+ r = flatIndex / `+s+`;
+ c = imod(flatIndex, `+s+`);
+ uv = (vec2(c, r) + halfCR) / vec2(`+s+".0, "+a+`.0);
+ values = `+r.texture2D+`(A, uv);
+
+ if(offset == 0) {
+ result[`+h+`] = values[0];
+ } else if(offset == 1) {
+ result[`+h+`] = values[1];
+ } else if(offset == 2) {
+ result[`+h+`] = values[2];
+ } else {
+ result[`+h+`] = values[3];
+ }
+ }
+ }
+ `}this.userCode=`
+ `+up(t)+`
+
+ void main() {
+ ivec3 coords = getOutputCoords();
+
+ vec4 result = vec4(0.);
+ int flatIndex, r, c, offset;
+ ivec3 localCoords;
+ vec2 uv;
+ vec4 values;
+
+ `+o+`
+
+ `+r.output+" = "+l+`;
+ }
+ `}return n}();var PS={REAL:"return real * expR - imag * expI;",IMAG:"return real * expI + imag * expR;"},_S=function(){function n(t,e,i){this.variableNames=["real","imag"];var r=e[1];this.outputShape=e;var a=i?"2.0 * "+Math.PI:"-2.0 * "+Math.PI,s=i?r+".0":"1.0";this.userCode=`
+ const float exponentMultiplier = `+a+`;
+
+ float unaryOpComplex(float real, float expR, float imag, float expI) {
+ `+t+`
+ }
+
+ float mulMatDFT(int batch, int index) {
+ float indexRatio = float(index) / float(`+r+`);
+ float exponentMultiplierTimesIndexRatio =
+ exponentMultiplier * indexRatio;
+
+ float result = 0.0;
+
+ for (int i = 0; i < `+r+`; i++) {
+ // x = (-2|2 * PI / N) * index * i;
+ float x = exponentMultiplierTimesIndexRatio * float(i);
+ float expR = cos(x);
+ float expI = sin(x);
+ float real = getReal(batch, i);
+ float imag = getImag(batch, i);
+
+ result +=
+ unaryOpComplex(real, expR, imag, expI) / `+s+`;
+ }
+
+ return result;
+ }
+
+ void main() {
+ ivec2 coords = getOutputCoords();
+ setOutput(mulMatDFT(coords[0], coords[1]));
+ }
+ `}return n}();var d9=function(){function n(t,e){this.outputShape=[],this.variableNames=["x"],this.outputShape=t,this.userCode=`
+ uniform float value;
+ void main() {
+ // Input can be obtained from uniform value.
+ setOutput(value);
+ }
+ `}return n.prototype.getCustomSetupFunc=function(t){var e=this;return function(i,r){e.valueLoc==null&&(e.valueLoc=i.getUniformLocationNoThrow(r,"value")),i.gl.uniform1f(e.valueLoc,t)}},n}();var f9=function(){function n(t,e,i){this.variableNames=["A","indices"];var r=t.slice();r[i]=e,this.outputShape=r,this.rank=r.length;var a=Je(this.rank),s=p9(t,i);this.userCode=`
+ void main() {
+ `+a+` resRC = getOutputCoords();
+ setOutput(getA(`+s+`));
+ }
+ `}return n}();function p9(n,t){var e=n.length;if(e>4)throw Error("Gather for rank "+e+" is not yet supported");if(e===1)return"int(getIndices(resRC))";for(var i=["resRC.x","resRC.y","resRC.z","resRC.w"],r=[],a=0;a1?"strides[j]":"strides";this.userCode=`
+ `+r+" strides = "+r+"("+this.strides+`);
+ void main() {
+ `+a+` coords = getOutputCoords();
+ int flattenIndex = 0;
+ for (int j = 0; j < `+this.sliceDim+`; j++) {
+ int index = round(getIndices(coords[0], j));
+ flattenIndex += index * `+s+`;
+ }
+ setOutput(getX(flattenIndex, coords[1]));
+ }
+ `}return n}();function MS(n){var t=Wt(),e=t.version+`
+ precision highp float;
+ `+t.attribute+` vec3 clipSpacePos;
+ `+t.attribute+` vec2 uv;
+ `+t.varyingVs+` vec2 resultUV;
+
+ void main() {
+ gl_Position = vec4(clipSpacePos, 1);
+ resultUV = uv;
+ }`;return Q0(n,e)}function HS(n){var t=new Float32Array([-1,1,0,0,1,-1,-1,0,0,0,1,1,0,1,1,1,-1,0,1,0]);return iS(n,t)}function VS(n){var t=new Uint16Array([0,1,2,2,1,3]);return rS(n,t)}function Ss(n,t,e,i,r,a){sS(t,e);var s=aS(n),o=n.TEXTURE_2D;return ce(n,function(){return n.bindTexture(o,s)}),ce(n,function(){return n.texParameteri(o,n.TEXTURE_WRAP_S,n.CLAMP_TO_EDGE)}),ce(n,function(){return n.texParameteri(o,n.TEXTURE_WRAP_T,n.CLAMP_TO_EDGE)}),ce(n,function(){return n.texParameteri(o,n.TEXTURE_MIN_FILTER,n.NEAREST)}),ce(n,function(){return n.texParameteri(o,n.TEXTURE_MAG_FILTER,n.NEAREST)}),ce(n,function(){return n.texImage2D(o,0,i,t,e,0,r,a,null)}),ce(n,function(){return n.bindTexture(n.TEXTURE_2D,null)}),s}function dp(n){return n.internalFormatFloat}function qS(n,t,e,i){var r=gs(t,e),a=r[0],s=r[1];return Ss(n,a,s,dp(i),i.textureFormatFloat,n.FLOAT)}function pp(n){return n.internalFormatHalfFloat}function GS(n,t,e,i){var r=gs(t,e),a=r[0],s=r[1];return Ss(n,a,s,pp(i),i.textureFormatFloat,i.textureTypeHalfFloat)}function fp(n){return n.downloadTextureFormat}function YS(n,t,e,i){var r=gs(t,e),a=r[0],s=r[1];return Ss(n,a,s,fp(i),n.RGBA,n.UNSIGNED_BYTE)}function mp(n){return n.internalFormatPackedFloat}function KS(n,t,e,i){var r=ha(t,e),a=r[0],s=r[1];return Ss(n,a,s,mp(i),n.RGBA,n.FLOAT)}function gp(n){return n.internalFormatPackedHalfFloat}function jS(n,t,e,i){var r=ha(t,e),a=r[0],s=r[1];return Ss(n,a,s,gp(i),n.RGBA,i.textureTypeHalfFloat)}function $S(n,t,e){var i=0,r=3*4,a=3*4+2*4;ce(n,function(){return n.bindBuffer(n.ARRAY_BUFFER,e)});var s=rp(n,t,"clipSpacePos",e,3,a,i);return s&&rp(n,t,"uv",e,2,a,r)}function XS(n,t,e,i,r,a){ce(n,function(){return n.bindTexture(n.TEXTURE_2D,t)});var s,o,l;r instanceof Uint8Array?(s=new Uint8Array(e*i*4),o=n.UNSIGNED_BYTE,l=n.RGBA):(s=new Float32Array(e*i*4),o=n.FLOAT,l=a.internalFormatPackedFloat),s.set(r),ce(n,function(){return n.texImage2D(n.TEXTURE_2D,0,l,e,i,0,n.RGBA,o,s)}),ce(n,function(){return n.bindTexture(n.TEXTURE_2D,null)})}function JS(n,t,e){ce(n,function(){return n.bindTexture(n.TEXTURE_2D,t)}),e.data instanceof Uint8Array?ce(n,function(){return n.texImage2D(n.TEXTURE_2D,0,n.RGBA,e.width,e.height,0,n.RGBA,n.UNSIGNED_BYTE,e.data)}):ce(n,function(){return n.texImage2D(n.TEXTURE_2D,0,n.RGBA,n.RGBA,n.UNSIGNED_BYTE,e)}),ce(n,function(){return n.bindTexture(n.TEXTURE_2D,null)})}function ZS(n,t,e,i){var r=n.createBuffer();ce(n,function(){return n.bindBuffer(n.PIXEL_PACK_BUFFER,r)});var a=4,s=4,o=a*s*t*e;return ce(n,function(){return n.bufferData(n.PIXEL_PACK_BUFFER,o,n.STREAM_READ)}),ce(n,function(){return n.readPixels(0,0,e,t,n.RGBA,n.FLOAT,0)}),ce(n,function(){return n.bindBuffer(n.PIXEL_PACK_BUFFER,null)}),r}function QS(n,t,e){var i=n,r=new Float32Array(e);return i.bindBuffer(i.PIXEL_PACK_BUFFER,t),i.getBufferSubData(i.PIXEL_PACK_BUFFER,0,r),i.bindBuffer(i.PIXEL_PACK_BUFFER,null),r}function eL(n,t,e,i){var r=gs(t,e),a=r[0],s=r[1],o=4,l=new Uint8Array(M_(t*e,o));return ce(n,function(){return n.readPixels(0,0,a,s,i.downloadTextureFormat,n.UNSIGNED_BYTE,l)}),new Float32Array(l.buffer)}function tL(n,t,e,i,r,a,s,o){var l=n,u=new Float32Array(H_(a,s));return l.bindBuffer(l.PIXEL_PACK_BUFFER,t),l.getBufferSubData(l.PIXEL_PACK_BUFFER,0,u),l.bindBuffer(l.PIXEL_PACK_BUFFER,null),u}function nL(n,t,e){var i=new Float32Array(t*e*4);return ce(n,function(){return n.readPixels(0,0,e,t,n.RGBA,n.FLOAT,i)}),i}var g9={__proto__:null,createVertexShader:MS,createVertexBuffer:HS,createIndexBuffer:VS,getInternalFormatForFloat32MatrixTexture:dp,createFloat32MatrixTexture:qS,getInternalFormatForFloat16MatrixTexture:pp,createFloat16MatrixTexture:GS,getInternalFormatForUnsignedBytesMatrixTexture:fp,createUnsignedBytesMatrixTexture:YS,getInternalFormatForPackedMatrixTexture:mp,createPackedMatrixTexture:KS,getInternalFormatForFloat16PackedMatrixTexture:gp,createFloat16PackedMatrixTexture:jS,bindVertexProgramAttributeStreams:$S,uploadDenseMatrixToTexture:XS,uploadPixelDataToTexture:JS,createBufferFromOutputTexture:ZS,downloadFloat32MatrixFromBuffer:QS,downloadByteEncodedFloatMatrixFromOutputTexture:eL,downloadPackedMatrixFromBuffer:tL,downloadMatrixFromPackedOutputTexture:nL};var iL=function(){function n(t){this.outputTexture=null,this.program=null,this.disposed=!1,this.vertexAttrsAreBound=!1,this.itemsToPoll=[];var e=x.env().getNumber("WEBGL_VERSION");t!=null?(this.gl=t,X0(e,t)):this.gl=Hn(e);var i="WEBGL_color_buffer_float",r="EXT_color_buffer_half_float";if(x.env().getNumber("WEBGL_VERSION")===1){var a="OES_texture_float",s="OES_texture_half_float";if(this.textureFloatExtension=ys(this.gl,a),sn(this.gl,s))this.textureHalfFloatExtension=ys(this.gl,s);else if(x.env().get("WEBGL_FORCE_F16_TEXTURES"))throw new Error("GL context does not support half float textures, yet the environment flag WEBGL_FORCE_F16_TEXTURES is set to true.");if(this.colorBufferFloatExtension=this.gl.getExtension(i),sn(this.gl,r))this.colorBufferHalfFloatExtension=ys(this.gl,r);else if(x.env().get("WEBGL_FORCE_F16_TEXTURES"))throw new Error("GL context does not support color renderable half floats, yet the environment flag WEBGL_FORCE_F16_TEXTURES is set to true.")}else if(i="EXT_color_buffer_float",sn(this.gl,i))this.colorBufferFloatExtension=this.gl.getExtension(i);else if(sn(this.gl,r))this.colorBufferHalfFloatExtension=this.gl.getExtension(r);else throw new Error("GL context does not support color renderable floats");this.vertexBuffer=HS(this.gl),this.indexBuffer=VS(this.gl),this.framebuffer=oS(this.gl),this.textureConfig=ip(this.gl,this.textureHalfFloatExtension)}return Object.defineProperty(n.prototype,"debug",{get:function(){return x.env().getBool("DEBUG")},enumerable:!0,configurable:!0}),n.prototype.dispose=function(){var t=this;if(this.disposed)return;this.program!=null&&console.warn("Disposing a GPGPUContext that still has a bound WebGLProgram. This is probably a resource leak, delete the program with GPGPUContext.deleteProgram before disposing."),this.outputTexture!=null&&console.warn("Disposing a GPGPUContext that still has a bound output matrix texture. This is probably a resource leak, delete the output matrix texture with GPGPUContext.deleteMatrixTexture before disposing.");var e=this.gl;ce(e,function(){return e.finish()}),ce(e,function(){return e.bindFramebuffer(e.FRAMEBUFFER,null)}),ce(e,function(){return e.deleteFramebuffer(t.framebuffer)}),ce(e,function(){return e.bindBuffer(e.ARRAY_BUFFER,null)}),ce(e,function(){return e.bindBuffer(e.ELEMENT_ARRAY_BUFFER,null)}),ce(e,function(){return e.deleteBuffer(t.indexBuffer)}),this.disposed=!0},n.prototype.createFloat32MatrixTexture=function(t,e){return this.throwIfDisposed(),qS(this.gl,t,e,this.textureConfig)},n.prototype.createFloat16MatrixTexture=function(t,e){return this.throwIfDisposed(),GS(this.gl,t,e,this.textureConfig)},n.prototype.createUnsignedBytesMatrixTexture=function(t,e){return this.throwIfDisposed(),YS(this.gl,t,e,this.textureConfig)},n.prototype.uploadPixelDataToTexture=function(t,e){this.throwIfDisposed(),JS(this.gl,t,e)},n.prototype.uploadDenseMatrixToTexture=function(t,e,i,r){this.throwIfDisposed(),XS(this.gl,t,e,i,r,this.textureConfig)},n.prototype.createFloat16PackedMatrixTexture=function(t,e){return this.throwIfDisposed(),jS(this.gl,t,e,this.textureConfig)},n.prototype.createPackedMatrixTexture=function(t,e){return this.throwIfDisposed(),KS(this.gl,t,e,this.textureConfig)},n.prototype.deleteMatrixTexture=function(t){var e=this;this.throwIfDisposed(),this.outputTexture===t&&(ap(this.gl,this.framebuffer),this.outputTexture=null),ce(this.gl,function(){return e.gl.deleteTexture(t)})},n.prototype.downloadByteEncodedFloatMatrixFromOutputTexture=function(t,e,i){var r=this;return this.downloadMatrixDriver(t,function(){return eL(r.gl,e,i,r.textureConfig)})},n.prototype.downloadPackedMatrixFromBuffer=function(t,e,i,r,a,s){return tL(this.gl,t,e,i,r,a,s,this.textureConfig)},n.prototype.downloadFloat32MatrixFromBuffer=function(t,e){return QS(this.gl,t,e)},n.prototype.createBufferFromTexture=function(t,e,i){this.bindTextureToFrameBuffer(t);var r=ZS(this.gl,e,i,this.textureConfig);return this.unbindTextureToFrameBuffer(),r},n.prototype.createAndWaitForFence=function(){var t=this.createFence(this.gl);return this.pollFence(t)},n.prototype.createFence=function(t){var e=this,i,r;if(x.env().getBool("WEBGL_FENCE_API_ENABLED")){var a=t,s=a.fenceSync(a.SYNC_GPU_COMMANDS_COMPLETE,0);t.flush(),r=function(){var o=a.clientWaitSync(s,0,0);return o===a.ALREADY_SIGNALED||o===a.CONDITION_SATISFIED},i=s}else x.env().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")>0?(i=this.beginQuery(),this.endQuery(),r=function(){return e.isQueryAvailable(i,x.env().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))}):r=function(){return!0};return{query:i,isFencePassed:r}},n.prototype.downloadMatrixFromPackedTexture=function(t,e,i){var r=this;return this.downloadMatrixDriver(t,function(){return nL(r.gl,e,i)})},n.prototype.createProgram=function(t){this.throwIfDisposed();var e=this.gl,i=eS(e,t),r=MS(e),a=tS(e);return ce(e,function(){return e.attachShader(a,r)}),ce(e,function(){return e.attachShader(a,i)}),nS(e,a),this.debug&&Qo(e,a),this.vertexAttrsAreBound||(this.setProgram(a),this.vertexAttrsAreBound=$S(e,this.program,this.vertexBuffer)),a},n.prototype.deleteProgram=function(t){var e=this;this.throwIfDisposed(),t===this.program&&(this.program=null),t!=null&&ce(this.gl,function(){return e.gl.deleteProgram(t)})},n.prototype.setProgram=function(t){var e=this;this.throwIfDisposed(),this.program=t,this.program!=null&&this.debug&&Qo(this.gl,this.program),ce(this.gl,function(){return e.gl.useProgram(t)})},n.prototype.getUniformLocation=function(t,e,i){return i===void 0&&(i=!0),this.throwIfDisposed(),i?cS(this.gl,t,e):hS(this.gl,t,e)},n.prototype.getAttributeLocation=function(t,e){var i=this;return this.throwIfDisposed(),ce(this.gl,function(){return i.gl.getAttribLocation(t,e)})},n.prototype.getUniformLocationNoThrow=function(t,e){return this.throwIfDisposed(),this.gl.getUniformLocation(t,e)},n.prototype.setInputMatrixTexture=function(t,e,i){this.throwIfDisposed(),this.throwIfNoProgram(),dS(this.gl,t,e,i)},n.prototype.setOutputMatrixTexture=function(t,e,i){this.setOutputMatrixTextureDriver(t,i,e)},n.prototype.setOutputPackedMatrixTexture=function(t,e,i){this.throwIfDisposed();var r=ha(e,i),a=r[0],s=r[1];this.setOutputMatrixTextureDriver(t,a,s)},n.prototype.setOutputMatrixWriteRegion=function(t,e,i,r){this.setOutputMatrixWriteRegionDriver(i,t,r,e)},n.prototype.setOutputPackedMatrixWriteRegion=function(t,e,i,r){throw new Error("setOutputPackedMatrixWriteRegion not implemented.")},n.prototype.debugValidate=function(){this.program!=null&&Qo(this.gl,this.program),bs(this.gl)},n.prototype.executeProgram=function(){this.throwIfDisposed(),this.throwIfNoProgram();var t=this.gl;this.debug&&this.debugValidate(),ce(t,function(){return t.drawElements(t.TRIANGLES,6,t.UNSIGNED_SHORT,0)})},n.prototype.blockUntilAllProgramsCompleted=function(){var t=this;this.throwIfDisposed(),ce(this.gl,function(){return t.gl.finish()})},n.prototype.getQueryTimerExtension=function(){return this.disjointQueryTimerExtension==null&&(this.disjointQueryTimerExtension=ys(this.gl,x.env().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")===2?"EXT_disjoint_timer_query_webgl2":"EXT_disjoint_timer_query")),this.disjointQueryTimerExtension},n.prototype.getQueryTimerExtensionWebGL2=function(){return this.getQueryTimerExtension()},n.prototype.getQueryTimerExtensionWebGL1=function(){return this.getQueryTimerExtension()},n.prototype.beginQuery=function(){if(x.env().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")===2){var t=this.gl,e=this.getQueryTimerExtensionWebGL2(),i=t.createQuery();return t.beginQuery(e.TIME_ELAPSED_EXT,i),i}var r=this.getQueryTimerExtensionWebGL1(),a=r.createQueryEXT();return r.beginQueryEXT(r.TIME_ELAPSED_EXT,a),a},n.prototype.endQuery=function(){if(x.env().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION")===2){var t=this.gl,e=this.getQueryTimerExtensionWebGL2();t.endQuery(e.TIME_ELAPSED_EXT);return}var i=this.getQueryTimerExtensionWebGL1();i.endQueryEXT(i.TIME_ELAPSED_EXT)},n.prototype.waitForQueryAndGetTime=function(t){return Jo(this,void 0,void 0,function(){var e=this;return Zo(this,function(i){switch(i.label){case 0:return[4,x.util.repeatedTry(function(){return e.disposed||e.isQueryAvailable(t,x.env().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))})];case 1:return i.sent(),[2,this.getQueryTime(t,x.env().getNumber("WEBGL_DISJOINT_QUERY_TIMER_EXTENSION_VERSION"))]}})})},n.prototype.getQueryTime=function(t,e){if(e===0)return null;if(e===2){var i=this.gl,r=i.getQueryParameter(t,i.QUERY_RESULT);return r/1e6}else{var a=this.getQueryTimerExtensionWebGL1(),r=a.getQueryObjectEXT(t,a.QUERY_RESULT_EXT);return r/1e6}},n.prototype.isQueryAvailable=function(t,e){if(e===0)return!0;if(e===2){var i=this.gl,r=this.getQueryTimerExtensionWebGL2(),a=i.getQueryParameter(t,i.QUERY_RESULT_AVAILABLE);return this.disjoint==null&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),a&&!this.disjoint}else{var r=this.getQueryTimerExtensionWebGL1(),a=r.getQueryObjectEXT(t,r.QUERY_RESULT_AVAILABLE_EXT);return this.disjoint==null&&(this.disjoint=this.gl.getParameter(r.GPU_DISJOINT_EXT)),a&&!this.disjoint}},n.prototype.pollFence=function(t){var e=this;return new Promise(function(i){e.addItemToPoll(function(){return t.isFencePassed()},function(){return i()})})},n.prototype.pollItems=function(){for(var t=v9(this.itemsToPoll.map(function(r){return r.isDoneFn})),e=0;e<=t;++e){var i=this.itemsToPoll[e].resolveFn;i()}this.itemsToPoll=this.itemsToPoll.slice(t+1)},n.prototype.addItemToPoll=function(t,e){var i=this;if(this.itemsToPoll.push({isDoneFn:t,resolveFn:e}),this.itemsToPoll.length>1)return;x.util.repeatedTry(function(){return i.pollItems(),i.itemsToPoll.length===0})},n.prototype.bindTextureToFrameBuffer=function(t){this.throwIfDisposed(),el(this.gl,t,this.framebuffer),this.debug&&bs(this.gl)},n.prototype.unbindTextureToFrameBuffer=function(){this.outputTexture!=null?(el(this.gl,this.outputTexture,this.framebuffer),this.debug&&bs(this.gl)):ap(this.gl,this.framebuffer)},n.prototype.downloadMatrixDriver=function(t,e){this.bindTextureToFrameBuffer(t);var i=e();return this.unbindTextureToFrameBuffer(),i},n.prototype.setOutputMatrixTextureDriver=function(t,e,i){this.throwIfDisposed();var r=this.gl;el(r,t,this.framebuffer),this.debug&&bs(r),this.outputTexture=t,ce(r,function(){return r.viewport(0,0,e,i)}),ce(r,function(){return r.scissor(0,0,e,i)})},n.prototype.setOutputMatrixWriteRegionDriver=function(t,e,i,r){var a=this;this.throwIfDisposed(),ce(this.gl,function(){return a.gl.scissor(t,e,i,r)})},n.prototype.throwIfDisposed=function(){if(this.disposed)throw new Error("Attempted to use disposed GPGPUContext.")},n.prototype.throwIfNoProgram=function(){if(this.program==null)throw new Error("No GPU program is currently set.")},n}();function v9(n){for(var t=0;t0&&(b.flatOffset=g.texData.slice.flatOffset),{name:t.variableNames[v],shapeInfo:b}}),s=a.map(function(g){return g.shapeInfo}),o={logicalShape:i.shape,texShape:i.texData.texShape,isUniform:!1,isPacked:i.texData.isPacked,flatOffset:null},l=_M(a,o,r,t.packedInputs),u=n.createProgram(l),c=null,h=n.getUniformLocation(u,"NAN",!1);x.env().getNumber("WEBGL_VERSION")===1&&(c=n.getUniformLocation(u,"INFINITY",!1));for(var d={},p=0;p0,l=s.isUniform?"uniform":s.texData.texShape;i+=s.shape+"_"+l+"_"+o});var r=n.userCode,a=n.constructor.name;return a+="_"+i+"_"+r,a}var S9=function(){function n(t,e,i){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t;for(var r=i.filterWidth,a=i.inChannels,s=i.strideWidth,o=i.strideHeight,l=i.padInfo,u=i.outWidth,c=i.dilationWidth,h=i.dilationHeight,d=i.dataFormat,p=l.left,f=l.top,m=a*r,g=Wt(),v=d==="channelsLast",b=v?0:1,w=v?1:2,S="",L=0;L<=1;L++)for(var N=0;N<=1;N++)S+=`
+ blockIndex = rc.y + `+N+`;
+ pos = rc.x + `+L+`;
+
+ if(blockIndex < `+t[1]+" && pos < "+t[0]+`) {
+ offsetY = int(blockIndex / (`+u+")) * "+o+" - "+f+`;
+ d0 = offsetY + `+h+" * (pos / "+m+`);
+
+ if(d0 < `+e[b]+` && d0 >= 0) {
+
+ offsetX = int(mod(float(blockIndex), `+u+".) * "+s+". - "+p+`.);
+ d1 = offsetX + `+c+" * (int(mod(float(pos), "+m+".) / "+a+`.));
+
+ if(d1 < `+e[w]+` && d1 >= 0) {
+
+ ch = int(mod(float(pos), `+a+`.));
+
+ if (`+v+`) {
+ innerDims = vec2(d1, ch);
+ result[`+(L*2+N)+`] = getChannel(
+ getA(d0, int(innerDims.x),
+ int(innerDims.y)), innerDims);
+ } else {
+ innerDims = vec2(d0, d1);
+ result[`+(L*2+N)+`] = getChannel(
+ getA(ch, int(innerDims.x),
+ int(innerDims.y)), innerDims);
+ }
+ }
+ }
+ }
+ `;this.userCode=`
+ void main() {
+ ivec2 rc = getOutputCoords();
+
+ vec4 result = vec4(0);
+
+ int blockIndex, pos, offsetY, d0, offsetX, d1, ch;
+ vec2 innerDims;
+
+ `+S+`
+
+ `+g.output+` = result;
+ }
+ `}return n}();var L9=function(){function n(t,e,i,r,a){this.variableNames=["x"],this.outputShape=[];var s=e,o=t[3]-1;this.outputShape=t;var l,u="float("+i+") + float("+r+") * sum";a===.5?l="inversesqrt("+u+")":a===1?l="1.0/("+u+")":l="exp(log("+u+") * float(-"+a+"));",this.userCode=`
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords[0];
+ int r = coords[1];
+ int c = coords[2];
+ int d = coords[3];
+ float x = getX(b, r, c, d);
+ float sum = 0.0;
+ for (int j = -`+s+"; j <= "+s+`; j++) {
+ int idx = d + j;
+ if (idx >= 0 && idx <= `+o+`) {
+ float z = getX(b, r, c, idx);
+ sum += z * z;
+ }
+ }
+ float val = x * `+l+`;
+ setOutput(val);
+ }
+ `}return n}();var I9=function(){function n(t,e,i,r,a){this.variableNames=["inputImage","outputImage","dy"],this.outputShape=[],this.outputShape=t,this.depth=t[3],this.depthRadius=e,this.bias=i,this.alpha=r,this.beta=a,this.userCode=`
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords[0];
+ int r = coords[1];
+ int c = coords[2];
+
+ float result = 0.0;
+ for (int d = 0; d < `+this.depth+`; ++d) {
+ int depthBegin = int(max(0.0, float(d - `+e+`)));
+ int depthEnd = int(min(float(`+this.depth+`),
+ float(d + `+e+` + 1)));
+
+ const int MIN_DEPTH_BEGIN = 0;
+ const int MAX_DEPTH_END = `+this.depth+`;
+
+ float norm = 0.0;
+ for (int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k) {
+ if (k < depthBegin){
+ continue;
+ }
+ else if (k >= depthBegin && k < depthEnd) {
+ norm += getInputImage(b, r, c, k) * getInputImage(b, r, c, k);
+ }
+ else {
+ break;
+ }
+ }
+
+ norm = float(`+r+") * norm + float("+i+`);
+
+ for(int k = MIN_DEPTH_BEGIN; k < MAX_DEPTH_END; ++k){
+ if (k < depthBegin){
+ continue;
+ }
+ else if (k >= depthBegin && k < depthEnd){
+ float dyi = -2.0 * float(`+r+`)
+ * float(`+a+`)
+ * getInputImage(b ,r ,c, k) * getOutputImage(b, r, c, d)
+ / norm;
+ if (k == d) {
+ dyi += pow(norm, -1.0 * `+a+`);
+ }
+ if (k == coords[3]) {
+ dyi *= getDy(b, r, c, d);
+ result += dyi;
+ }
+ }
+ else {
+ break;
+ }
+ }
+ }
+ setOutput(result);
+ }
+ `}return n}();var A9=function(){function n(t,e,i,r,a){this.variableNames=["x"],this.outputShape=[],this.packedInputs=!0,this.packedOutput=!0;var s=e,o=t[3]-1;this.outputShape=t;var l,u="float("+i+") + float("+r+") * sum";a===.5?l="inversesqrt("+u+")":a===1?l="1.0/("+u+")":l="exp(log("+u+") * float(-"+a+"));",this.userCode=`
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords.x;
+ int r = coords.y;
+ int c = coords.z;
+ int d = coords.w;
+
+ bool hasNextCol = d < `+this.outputShape[3]+`;
+ bool hasNextRow = c < `+this.outputShape[2]+`;
+
+ vec4 sum = vec4(0.);
+ vec4 xFragAtOutputCoords = getX(b, r, c, d);
+
+ vec4 xAtOutputCoords = vec4(
+ getChannel(xFragAtOutputCoords, vec2(c, d)),
+ hasNextCol ?
+ getChannel(xFragAtOutputCoords, vec2(c, d + 1)) : 0.0,
+ hasNextRow ?
+ getChannel(xFragAtOutputCoords , vec2(c + 1, d)) : 0.0,
+ (hasNextRow && hasNextCol) ?
+ getChannel(xFragAtOutputCoords, vec2(c + 1, d + 1)) : 0.0
+ );
+
+ int firstChannel = d - `+s+`;
+ vec2 cache = vec2(0.);
+ if(firstChannel >= 0){
+ vec4 firstChannelFrag = getX(b, r, c, firstChannel);
+ cache.x = getChannel(firstChannelFrag, vec2(c, firstChannel));
+ if(hasNextRow){
+ cache.y = getChannel(firstChannelFrag, vec2(c + 1, firstChannel));
+ }
+ }
+
+ ivec2 depth = ivec2(d, d + 1);
+ for (int j = - `+s+"; j <= "+s+`; j++) {
+ ivec2 idx = depth + j;
+ bvec2 aboveLowerBound = greaterThanEqual(idx, ivec2(0));
+ bvec2 belowUpperBound = lessThanEqual(idx, ivec2(`+o+`));
+
+ bool depthInRange = aboveLowerBound.x && belowUpperBound.x;
+ bool depthPlusOneInRange = aboveLowerBound.y && belowUpperBound.y;
+
+ if(depthInRange || depthPlusOneInRange){
+ vec4 z = vec4(0.);
+ vec4 xFragAtCurrentDepth;
+ z.xz = cache.xy;
+ if(depthPlusOneInRange && hasNextCol){
+ xFragAtCurrentDepth = idx.y != d ?
+ getX(b, r, c, idx.y) : xFragAtOutputCoords;
+ z.y = getChannel(xFragAtCurrentDepth, vec2(c, idx.y));
+ if(hasNextRow){
+ z.w = getChannel(xFragAtCurrentDepth, vec2(c + 1, idx.y));
+ }
+ }
+ cache.xy = z.yw;
+ sum += z * z;
+ }
+ }
+ vec4 result = xAtOutputCoords * `+l+`;
+ setOutput(result);
+ }
+ `}return n}();var T9=function(){function n(t){this.variableNames=["dy","maxPos"],this.outputShape=t.inShape;var e=t.strideHeight,i=t.strideWidth,r=t.dilationHeight,a=t.effectiveFilterHeight,s=t.effectiveFilterWidth,o=a-1-t.padInfo.top,l=s-1-t.padInfo.left,u=a*s-1;this.userCode=`
+ const ivec2 pads = ivec2(`+o+", "+l+`);
+
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords[0];
+ int d = coords[3];
+
+ ivec2 dyRCCorner = coords.yz - pads;
+ int dyRCorner = dyRCCorner.x;
+ int dyCCorner = dyRCCorner.y;
+
+ // Convolve dy(?, ?, d) with pos mask(:, :, d) to get dx(xR, xC, d).
+ // ? = to be determined. : = across all values in that axis.
+ float dotProd = 0.0;
+ for (int wR = 0; wR < `+a+`;
+ wR += `+r+`) {
+ float dyR = float(dyRCorner + wR) / `+e+`.0;
+
+ if (dyR < 0.0 || dyR >= `+t.outHeight+`.0 || fract(dyR) > 0.0) {
+ continue;
+ }
+ int idyR = int(dyR);
+
+ for (int wC = 0; wC < `+s+`; wC++) {
+ float dyC = float(dyCCorner + wC) / `+i+`.0;
+
+ if (dyC < 0.0 || dyC >= `+t.outWidth+`.0 ||
+ fract(dyC) > 0.0) {
+ continue;
+ }
+ int idyC = int(dyC);
+
+ float dyValue = getDy(b, idyR, idyC, d);
+ int maxPosValue = `+u+` - int(getMaxPos(b, idyR, idyC, d));
+
+ // Get the current value, check it against the value from the
+ // position matrix.
+ int curPosValue = wR * `+s+` + wC;
+ float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);
+
+ dotProd += dyValue * mask;
+ }
+ }
+ setOutput(dotProd);
+ }
+ `}return n}(),N9=function(){function n(t){this.variableNames=["dy","maxPos"],this.outputShape=t.inShape;var e=t.strideDepth,i=t.strideHeight,r=t.strideWidth,a=t.dilationDepth,s=t.dilationHeight,o=t.dilationWidth,l=t.effectiveFilterDepth,u=t.effectiveFilterHeight,c=t.effectiveFilterWidth,h=l-1-t.padInfo.front,d=u-1-t.padInfo.top,p=c-1-t.padInfo.left,f=l*u*c-1;this.userCode=`
+ const ivec3 pads = ivec3(`+h+", "+d+", "+p+`);
+
+ void main() {
+ ivec5 coords = getOutputCoords();
+ int batch = coords.x;
+ int ch = coords.u;
+
+ ivec3 dyCorner = ivec3(coords.y, coords.z, coords.w) - pads;
+ int dyDCorner = dyCorner.x;
+ int dyRCorner = dyCorner.y;
+ int dyCCorner = dyCorner.z;
+
+ // Convolve dy(?, ?, ?, ch) with pos mask(:, :, :, d) to get
+ // dx(xD, xR, xC, ch).
+ // ? = to be determined. : = across all values in that axis.
+ float dotProd = 0.0;
+
+ for (int wD = 0; wD < `+l+`;
+ wD += `+a+`) {
+ float dyD = float(dyDCorner + wD) / `+e+`.0;
+
+ if (dyD < 0.0 || dyD >= `+t.outDepth+`.0 || fract(dyD) > 0.0) {
+ continue;
+ }
+ int idyD = int(dyD);
+
+ for (int wR = 0; wR < `+u+`;
+ wR += `+s+`) {
+ float dyR = float(dyRCorner + wR) / `+i+`.0;
+
+ if (dyR < 0.0 || dyR >= `+t.outHeight+`.0 ||
+ fract(dyR) > 0.0) {
+ continue;
+ }
+ int idyR = int(dyR);
+
+ for (int wC = 0; wC < `+c+`;
+ wC += `+o+`) {
+ float dyC = float(dyCCorner + wC) / `+r+`.0;
+
+ if (dyC < 0.0 || dyC >= `+t.outWidth+`.0 ||
+ fract(dyC) > 0.0) {
+ continue;
+ }
+ int idyC = int(dyC);
+
+ float dyValue = getDy(batch, idyD, idyR, idyC, ch);
+ int maxPosValue = `+f+` -
+ int(getMaxPos(batch, idyD, idyR, idyC, ch));
+
+ // Get the current value, check it against the value from the
+ // position matrix.
+ int curPosValue =
+ wD * `+u+" * "+c+` +
+ wR * `+c+` + wC;
+ float mask = float(maxPosValue == curPosValue ? 1.0 : 0.0);
+
+ dotProd += dyValue * mask;
+ }
+ }
+ }
+ setOutput(dotProd);
+ }
+ `}return n}();var vp=function(){function n(t,e,i,r,a,s,o){i===void 0&&(i=!1),r===void 0&&(r=!1),a===void 0&&(a=!1),s===void 0&&(s=null),o===void 0&&(o=!1),this.variableNames=["matrixA","matrixB"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e;var l=i?t[1]:t[2],u=Math.ceil(l/2),c=i?"i * 2, rc.y":"rc.y, i * 2",h=r?"rc.z, i * 2":"i * 2, rc.z",d=i?["a.xxyy","a.zzww"]:["a.xxzz","a.yyww"],p=r?["b.xzxz","b.ywyw"]:["b.xyxy","b.zwzw"],f="",m="";s&&(o?f=`vec4 activation(vec4 a) {
+ vec4 b = getPreluActivationWeightsAtOutCoords();
+ `+s+`
+ }`:f=`vec4 activation(vec4 x) {
+ `+s+`
+ }`,m="result = activation(result);");var g=a?"result += getBiasAtOutCoords();":"";a&&this.variableNames.push("bias"),o&&this.variableNames.push("preluActivationWeights"),this.userCode=`
+ `+f+`
+
+ const float sharedDimension = `+u+`.0;
+
+ vec4 dot2x2ARowBCol(ivec3 rc) {
+ vec4 result = vec4(0);
+ for (int i = 0; i < `+u+`; i++) {
+ vec4 a = getMatrixA(rc.x, `+c+`);
+ vec4 b = getMatrixB(rc.x, `+h+`);
+
+ // These swizzled products need to be separately added.
+ // See: https://github.com/tensorflow/tfjs/issues/1735
+ result += (`+d[0]+" * "+p[0]+`);
+ result += (`+d[1]+" * "+p[1]+`);
+ }
+ return result;
+ }
+
+ void main() {
+ ivec3 rc = getOutputCoords();
+ vec4 result = dot2x2ARowBCol(rc);
+
+ `+g+`
+
+ `+m+`
+
+ setOutput(result);
+ }
+ `}return n}();var x9=function(){function n(t,e,i){this.variableNames=["probs"],this.outputShape=[t,i],this.userCode=`
+ uniform float seed;
+
+ void main() {
+ ivec2 coords = getOutputCoords();
+ int batch = coords[0];
+
+ float r = random(seed);
+ float cdf = 0.0;
+
+ for (int i = 0; i < `+(e-1)+`; i++) {
+ cdf += getProbs(batch, i);
+
+ if (r < cdf) {
+ setOutput(float(i));
+ return;
+ }
+ }
+
+ // If no other event happened, last event happened.
+ setOutput(float(`+(e-1)+`));
+ }
+ `}return n.prototype.getCustomSetupFunc=function(t){var e=this;return function(i,r){e.seedLoc==null&&(e.seedLoc=i.getUniformLocation(r,"seed")),i.gl.uniform1f(e.seedLoc,t)}},n}();var C9=function(){function n(t,e,i,r){this.variableNames=["indices"],this.outputShape=[t,e],this.userCode=`
+ void main() {
+ ivec2 coords = getOutputCoords();
+ int index = round(getIndices(coords.x));
+ setOutput(mix(float(`+r+"), float("+i+`),
+ float(index == coords.y)));
+ }
+ `}return n}();var D9=function(){function n(t){this.variableNames=["A"],this.packedInputs=!1,this.packedOutput=!0,this.outputShape=t;var e=t.length;if(e===0)this.userCode=`
+ void main() {
+ setOutput(vec4(getA(), 0., 0., 0.));
+ }
+ `;else{var i=Jt("rc",e),r=Je(e),a=R9(e,t,i),s=O9(e,t[t.length-1],t[t.length-2],i),o=E9(t,i);this.userCode=`
+ void main() {
+ `+r+` rc = getOutputCoords();
+
+ if(`+a+`) {
+ setOutput(vec4(0));
+ } else {
+ `+s+`
+
+ setOutput(vec4(`+o+`));
+ }
+ }
+ `}}return n}();function k9(n,t){for(var e=[],i=0;i<=1;i++)for(var r=0;r<=1;r++){for(var a=(i===0?"r":"rp1")+", "+(r===0?"c":"cp1"),s=2;s "+t[0];for(var i="",r=n-2;r= "+t[r],r= `+t+`;
+ bool rEdge = rp1 >= `+e+`;
+ `}function E9(n,t){var e=n.length,i=k9(e,t);return e===1?`getA(rc),
+ rc + 1 >= `+n[0]+` ? 0. : getA(rc + 1),
+ 0, 0`:"getA("+i[0]+`),
+ cEdge ? 0. : getA(`+i[1]+`),
+ rEdge ? 0. : getA(`+i[2]+`),
+ rEdge || cEdge ? 0. : getA(`+i[3]+")"}var F9=function(){function n(t,e,i){this.variableNames=["x"],this.outputShape=e.map(function(u,c){return u[0]+t[c]+u[1]});var r=t.length,a=Je(r),s=e.map(function(u){return u[0]}).join(","),o=e.map(function(u,c){return u[0]+t[c]}).join(","),l=["coords[0]","coords[1]","coords[2]","coords[3]"].slice(0,r);if(r===1){this.userCode=`
+ int start = `+s+`;
+ int end = `+o+`;
+
+ void main() {
+ int outC = getOutputCoords();
+ if (outC < start || outC >= end) {
+ setOutput(float(`+i+`));
+ } else {
+ setOutput(getX(outC - start));
+ }
+ }
+ `;return}this.userCode=`
+ `+a+" start = "+a+"("+s+`);
+ `+a+" end = "+a+"("+o+`);
+
+ void main() {
+ `+a+` outC = getOutputCoords();
+ if (any(lessThan(outC, start)) || any(greaterThanEqual(outC, end))) {
+ setOutput(float(`+i+`));
+ } else {
+ `+a+` coords = outC - start;
+ setOutput(getX(`+l+`));
+ }
+ }
+ `}return n}();var W9=function(){function n(t,e,i){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=e.map(function(v,b){return v[0]+t[b]+v[1]});for(var r=t.length,a=Je(r),s=e.map(function(v){return v[0]}).join(","),o=e.map(function(v,b){return v[0]+t[b]}).join(","),l=Jt("rc",r),u=Jt("source",r),c=l[r-1]+" < "+this.outputShape[r-1],h=r===1?"source":"vec2("+u.slice(-2).join()+")",d=[a+" rc = outputLoc;",l[r-1]+` += 1;
+ if(`+c+`) {
+ `,r===1?"":`}
+ rc = outputLoc;
+ `+l[r-2]+` += 1;
+ if(`+l[r-2]+" < "+this.outputShape[r-2]+") {",r===1?"":" "+l[r-1]+` += 1;
+ if(`+c+") {"],p=r===1?"rc < start || rc >= end":"any(lessThan(rc, start)) || any(greaterThanEqual(rc, end))",f="",m=0,g=r===1?2:4;m= `+t.inHeight+`) {
+ continue;
+ }
+
+ for (int wC = 0; wC < `+d+`;
+ wC += `+c+`) {
+ int xC = xCCorner + wC;
+
+ if (xC < 0 || xC >= `+t.inWidth+`) {
+ continue;
+ }
+
+ float value = getX(batch, xR, xC, d);
+
+ // If a min / max value has already been found, use it. If not,
+ // use the current value.
+ float currMinMaxValue = mix(
+ value, minMaxValue, minMaxValueFound);
+ if (value `+w+` currMinMaxValue) {
+ minMaxValue = value;
+ minMaxValueFound = 1.0;
+ minMaxPosition = `+(r?a?g:v:"wR * "+d+" + wC")+`;
+ }
+ }
+ }
+ setOutput(float(minMaxPosition));
+ }
+ `;return}var S="max",L=e+"("+e+"("+e+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";e==="avg"&&(L="avgValue / count");var N=Math.floor(s/4)*4,C=s%4,R=`
+ if (`+m+`) {
+ avgValue += dot(values, ones);
+ } else {
+ minMaxValue = `+S+`(values, minMaxValue);
+ }
+ `;this.userCode=`
+ const ivec2 strides = ivec2(`+o+", "+l+`);
+ const ivec2 pads = ivec2(`+p+", "+f+`);
+ const float initializationValue = `+b+`;
+ const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
+
+ float count = 0.0;
+
+ float getValue(int batch, int xR, int xC, int d) {
+ if (xC < 0 || xC >= `+t.inWidth+`) {
+ return initializationValue;
+ }
+ count += 1.0;
+ return getX(batch, xR, xC, d);
+ }
+
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int batch = coords[0];
+ int d = coords[3];
+
+ ivec2 xRCCorner = coords.yz * strides - pads;
+ int xRCorner = xRCCorner.x;
+ int xCCorner = xRCCorner.y;
+
+ // max/min x(?, ?, d) to get y(yR, yC, d).
+ // ? = to be determined
+ vec4 minMaxValue = vec4(`+b+`);
+ float avgValue = 0.0;
+ count = 0.0;
+
+ for (int wR = 0; wR < `+h+`;
+ wR += `+u+`) {
+ int xR = xRCorner + wR;
+
+ if (xR < 0 || xR >= `+t.inHeight+`) {
+ continue;
+ }
+
+ for (int wC = 0; wC < `+N+`; wC += 4) {
+ int xC = xCCorner + wC * `+c+`;
+
+ vec4 values = vec4(
+ getValue(batch, xR, xC, d),
+ getValue(batch, xR, xC + `+c+`, d),
+ getValue(batch, xR, xC + 2 * `+c+`, d),
+ getValue(batch, xR, xC + 3 * `+c+`, d)
+ );
+
+ `+R+`
+ }
+
+ int xC = xCCorner + `+N+`;
+ if (`+(C===1)+`) {
+ vec4 values = vec4(
+ getValue(batch, xR, xC, d),
+ initializationValue,
+ initializationValue,
+ initializationValue
+ );
+
+ `+R+`
+ } else if (`+(C===2)+`) {
+ vec4 values = vec4(
+ getValue(batch, xR, xC, d),
+ getValue(batch, xR, xC + `+c+`, d),
+ initializationValue,
+ initializationValue
+ );
+
+ `+R+`
+ } else if (`+(C===3)+`) {
+ vec4 values = vec4(
+ getValue(batch, xR, xC, d),
+ getValue(batch, xR, xC + `+c+`, d),
+ getValue(batch, xR, xC + 2 * `+c+`, d),
+ initializationValue
+ );
+
+ `+R+`
+ }
+ }
+ setOutput(`+L+`);
+ }
+ `}return n}(),yp=function(){function n(t,e,i,r,a){if(r===void 0&&(r=!1),a===void 0&&(a=!1),this.variableNames=["x"],e==="avg"&&i)throw new Error("Cannot compute positions for average pool.");var s=t.filterWidth,o=t.strideDepth,l=t.strideHeight,u=t.strideWidth,c=t.dilationDepth,h=t.dilationHeight,d=t.dilationWidth,p=t.effectiveFilterDepth,f=t.effectiveFilterHeight,m=t.effectiveFilterWidth,g=t.padInfo.front,v=t.padInfo.top,b=t.padInfo.left;this.outputShape=t.outShape;var w=e==="avg",S="0.0";if(w||(S="-1.0 / 1e-20"),i){var L=">=";this.userCode=`
+ const ivec3 strides =
+ ivec3(`+o+", "+l+", "+u+`);
+ const ivec3 pads = ivec3(`+g+", "+v+", "+b+`);
+
+ void main() {
+ ivec5 coords = getOutputCoords();
+ int batch = coords.x;
+ int ch = coords.u;
+
+ ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
+ int xDCorner = xCorner.x;
+ int xRCorner = xCorner.y;
+ int xCCorner = xCorner.z;
+
+ // max/min x(?, ?, ?, ch) to get y(yD, yR, yC, ch).
+ // ? = to be determined
+ float minMaxValue = 0.0;
+ float minMaxValueFound = 0.0;
+ int minMaxPosition = 0;
+
+ for (int wD = 0; wD < `+p+`;
+ wD += `+c+`) {
+ int xD = xDCorner + wD;
+
+ if (xD < 0 || xD >= `+t.inDepth+`) {
+ continue;
+ }
+
+ for (int wR = 0; wR < `+f+`;
+ wR += `+h+`) {
+ int xR = xRCorner + wR;
+
+ if (xR < 0 || xR >= `+t.inHeight+`) {
+ continue;
+ }
+
+ for (int wC = 0; wC < `+m+`;
+ wC += `+d+`) {
+ int xC = xCCorner + wC;
+
+ if (xC < 0 || xC >= `+t.inWidth+`) {
+ continue;
+ }
+
+ float value = getX(batch, xD, xR, xC, ch);
+
+ // If a min / max value has already been found, use it. If not,
+ // use the current value.
+ float currMinMaxValue = mix(
+ value, minMaxValue, minMaxValueFound);
+ if (value `+L+` currMinMaxValue) {
+ minMaxValue = value;
+ minMaxValueFound = 1.0;
+ minMaxPosition = `+(r?a?"(((batch * "+t.inDepth+" + xD) * "+t.inHeight+" + xR) * "+t.inWidth+" + xC) * "+t.inChannels+" + ch":"((xD * "+t.inHeight+" + xR) * "+t.inWidth+" + xC) * "+t.inChannels+" + ch":"wD * "+f+" * "+m+` +
+ wR * `+m+" + wC")+`;
+ }
+ }
+ }
+ }
+ setOutput(float(minMaxPosition));
+ }
+ `;return}var N="max",C=e+"("+e+"("+e+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";e==="avg"&&(C="avgValue / count");var R=Math.floor(s/4)*4,D=s%4,k=`
+ if (`+w+`) {
+ avgValue += dot(values, ones);
+ } else {
+ minMaxValue = `+N+`(values, minMaxValue);
+ }
+ `;this.userCode=`
+ const ivec3 strides =
+ ivec3(`+o+", "+l+", "+u+`);
+ const ivec3 pads = ivec3(`+g+", "+v+", "+b+`);
+ const float initializationValue = `+S+`;
+ const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
+
+ float count = 0.0;
+
+ float getValue(int batch, int xD, int xR, int xC, int ch) {
+ if (xC < 0 || xC >= `+t.inWidth+`) {
+ return initializationValue;
+ }
+ count += 1.0;
+ return getX(batch, xD, xR, xC, ch);
+ }
+
+ void main() {
+ ivec5 coords = getOutputCoords();
+ int batch = coords.x;
+ int ch = coords.u;
+
+ ivec3 xCorner = ivec3(coords.y, coords.z, coords.w) * strides - pads;
+ int xDCorner = xCorner.x;
+ int xRCorner = xCorner.y;
+ int xCCorner = xCorner.z;
+
+ // max/min x(?, ?, ?, d) to get y(yD, yR, yC, ch).
+ // ? = to be determined
+ vec4 minMaxValue = vec4(`+S+`);
+ float avgValue = 0.0;
+ count = 0.0;
+
+ for (int wD = 0; wD < `+p+`;
+ wD += `+c+`) {
+ int xD = xDCorner + wD;
+
+ if (xD < 0 || xD >= `+t.inDepth+`) {
+ continue;
+ }
+
+ for (int wR = 0; wR < `+f+`;
+ wR += `+h+`) {
+ int xR = xRCorner + wR;
+
+ if (xR < 0 || xR >= `+t.inHeight+`) {
+ continue;
+ }
+
+ for (int wC = 0; wC < `+R+`; wC += 4) {
+ int xC = xCCorner + wC * `+d+`;
+
+ vec4 values = vec4(
+ getValue(batch, xD, xR, xC, ch),
+ getValue(batch, xD, xR, xC + `+d+`, ch),
+ getValue(batch, xD, xR, xC + 2 * `+d+`, ch),
+ getValue(batch, xD, xR, xC + 3 * `+d+`, ch)
+ );
+
+ `+k+`
+ }
+
+ int xC = xCCorner + `+R+`;
+ if (`+(D===1)+`) {
+ vec4 values = vec4(
+ getValue(batch, xD, xR, xC, ch),
+ initializationValue,
+ initializationValue,
+ initializationValue
+ );
+
+ `+k+`
+ } else if (`+(D===2)+`) {
+ vec4 values = vec4(
+ getValue(batch, xD, xR, xC, ch),
+ getValue(batch, xD, xR, xC + `+d+`, ch),
+ initializationValue,
+ initializationValue
+ );
+
+ `+k+`
+ } else if (`+(D===3)+`) {
+ vec4 values = vec4(
+ getValue(batch, xD, xR, xC, ch),
+ getValue(batch, xD, xR, xC + `+d+`, ch),
+ getValue(batch, xD, xR, xC + 2 * `+d+`, ch),
+ initializationValue
+ );
+
+ `+k+`
+ }
+ }
+ setOutput(`+C+`);
+ }
+ }
+ `}return n}();var aL=function(){function n(t,e){this.variableNames=["x"];var i=t.windowSize,r=t.batchSize,a=t.inSize,s=t.outSize;this.outputShape=[r,s];var o="0.0",l="";e==="prod"?o="1.0":e==="min"?(o="1.0 / 1e-20",l="min"):e==="max"&&(o="-1.0 / 1e-20",l="max");var u=e+"("+e+"("+e+"(minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])";e==="sum"?u="sumValue":e==="prod"?u="prodValue":e==="all"?u="allValue":e==="any"&&(u="anyValue");var c=Math.floor(i/4)*4,h=i%4,d=`
+ if (`+(e==="sum")+`) {
+ sumValue += dot(values, ones);
+ } else if (`+(e==="prod")+`) {
+ vec2 tmp = vec2(values[0], values[1]) * vec2(values[2], values[3]);
+ prodValue *= tmp[0] * tmp[1];
+ } else {
+ minMaxValue = `+l+`(values, minMaxValue);
+ }
+ `,p="vec4";e==="all"?(o="1.0",d=`
+ bool reducedAllValue = all(values);
+ float floatedReducedAllValue = float(reducedAllValue);
+ allValue = float(allValue >= 1.0 && floatedReducedAllValue >= 1.0);
+ `,p="bvec4"):e==="any"&&(o="0.0",d=`
+ bool reducedAnyValue = any(values);
+ float floatedReducedAnyValue = float(reducedAnyValue);
+ anyValue = float(anyValue >= 1.0 || floatedReducedAnyValue >= 1.0);
+ `,p="bvec4");var f="";a%i>0&&(f=`
+ if (inIdx < 0 || inIdx >= `+a+`) {
+ return initializationValue;
+ }
+ `),this.userCode=`
+ const float initializationValue = `+o+`;
+ const vec4 ones = vec4(1.0, 1.0, 1.0, 1.0);
+
+ float getValue(int batch, int inIdx) {
+ `+f+`
+ return getX(batch, inIdx);
+ }
+
+ void main() {
+ ivec2 coords = getOutputCoords();
+ int batch = coords[0];
+ int outIdx = coords[1];
+ int inOffset = outIdx * `+i+`;
+
+ vec4 minMaxValue = vec4(`+o+`);
+ float prodValue = 1.0;
+ float sumValue = 0.0;
+ float allValue = 1.0;
+ float anyValue = 0.0;
+
+ for (int i = 0; i < `+c+`; i += 4) {
+ int inIdx = inOffset + i;
+ `+p+" values = "+p+`(
+ getValue(batch, inIdx),
+ getValue(batch, inIdx + 1),
+ getValue(batch, inIdx + 2),
+ getValue(batch, inIdx + 3)
+ );
+
+ `+d+`
+ }
+
+ int inIdx = inOffset + `+c+`;
+ if (`+(h===1)+`) {
+ `+p+" values = "+p+`(
+ getValue(batch, inIdx),
+ initializationValue,
+ initializationValue,
+ initializationValue
+ );
+
+ `+d+`
+ } else if (`+(h===2)+`) {
+ `+p+" values = "+p+`(
+ getValue(batch, inIdx),
+ getValue(batch, inIdx + 1),
+ initializationValue,
+ initializationValue
+ );
+
+ `+d+`
+ } else if (`+(h===3)+`) {
+ `+p+" values = "+p+`(
+ getValue(batch, inIdx),
+ getValue(batch, inIdx + 1),
+ getValue(batch, inIdx + 2),
+ initializationValue
+ );
+
+ `+d+`
+ }
+ setOutput(`+u+`);
+ }
+ `}return n}();var sL=function(){function n(t,e){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t;for(var i="",r=0;r<4;r++){var a="thisRC = rc;";r%2===1&&(a+="thisRC.z += 1;"),r>1&&(a+="thisRC.y += 1;"),i+=`
+ `+a+`
+ `+(r>0?"if(thisRC.y < rows && thisRC.z < cols){":"")+`
+ int flatIndex = getFlatIndex(thisRC);
+
+ ivec3 inputRC = inputCoordsFromReshapedOutCoords(flatIndex);
+ vec2 inputRCInnerDims = vec2(float(inputRC.y),float(inputRC.z));
+
+ result[`+r+`] =
+ getChannel(getA(inputRC.x, inputRC.y, inputRC.z), inputRCInnerDims);
+ `+(r>0?"}":"")+`
+ `}this.userCode=`
+ `+U9(e)+`
+ `+up(t)+`
+
+ void main() {
+ ivec3 rc = getOutputCoords();
+
+ vec4 result = vec4(0.);
+
+ ivec3 thisRC;
+ int rows = `+t[1]+`;
+ int cols = `+t[2]+`;
+
+ `+i+`
+
+ setOutput(result);
+ }
+ `}return n}();function U9(n){var t=wr(["r","c","d"],n);return`
+ ivec3 inputCoordsFromReshapedOutCoords(int index) {
+ `+t+`
+ return ivec3(r, c, d);
+ }
+ `}var B9=function(){function n(t,e,i){this.variableNames=["dy"],this.outputShape=[],this.outputShape=e.shape;var r=e.shape,a=r[1],s=r[2],o=t.shape,l=o[1],u=o[2],c=[i&&l>1?a-1:a,i&&u>1?s-1:s],h=[i&&l>1?l-1:l,i&&u>1?u-1:u],d=c[0]/h[0],p=c[1]/h[1],f=1/d,m=1/p,g=Math.ceil(f)*2+2,v=Math.ceil(m)*2+2;this.userCode=`
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords[0];
+ int d = coords[3];
+ int r = coords[1];
+ int c = coords[2];
+
+ float accumulator = 0.0;
+
+ const float heightScale = float(`+d+`);
+ const float widthScale = float(`+p+`);
+
+ const float invHeightScale = float(`+f+`);
+ const float invWidthScale = float(`+m+`);
+
+ const int winHeight = int(`+g+`);
+ const int winWidth = int(`+v+`);
+
+ // Compute bounds for where in dy we will look
+ float startRLerp = floor(float(r) * invHeightScale);
+ int startDyR = int(startRLerp - float(winHeight / 2));
+
+ float startCLerp = floor(float(c) * invWidthScale);
+ int startDyC = int(startCLerp - float(winWidth / 2));
+
+ // Loop over dy
+ for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {
+ int dyR = dyROffset + startDyR;
+
+ // Guard against the window exceeding the bounds of dy
+ if (dyR < 0 || dyR >= `+l+`) {
+ continue;
+ }
+
+ for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {
+ int dyC = dyCOffset + startDyC;
+
+ // Guard against the window exceeding the bounds of dy
+ if (dyC < 0 || dyC >= `+u+`) {
+ continue;
+ }
+
+ float dxR = float(dyR) * heightScale;
+ int topDxRIndex = int(floor(dxR));
+ int bottomDxRIndex = int(min(ceil(dxR), `+(a-1)+`.0));
+ float dxRLerp = dxR - float(topDxRIndex);
+ float inverseDxRLerp = 1.0 - dxRLerp;
+
+ float dxC = float(dyC) * widthScale;
+ int leftDxCIndex = int(floor(dxC));
+ int rightDxCIndex = int(min(ceil(dxC), `+(s-1)+`.0));
+ float dxCLerp = dxC - float(leftDxCIndex);
+ float inverseDxCLerp = 1.0 - dxCLerp;
+
+ if (r == topDxRIndex && c == leftDxCIndex) {
+ // topLeft
+ accumulator +=
+ getDy(b, dyR, dyC, d) * inverseDxRLerp * inverseDxCLerp;
+ }
+
+ if (r == topDxRIndex && c == rightDxCIndex) {
+ // topRight
+ accumulator += getDy(b, dyR, dyC, d) * inverseDxRLerp * dxCLerp;
+ }
+
+ if (r == bottomDxRIndex && c == leftDxCIndex) {
+ // bottomLeft
+ accumulator += getDy(b, dyR, dyC, d) * dxRLerp * inverseDxCLerp;
+ }
+
+ if (r == bottomDxRIndex && c == rightDxCIndex) {
+ // bottomRight
+ accumulator += getDy(b, dyR, dyC, d) * dxRLerp * dxCLerp;
+ }
+ }
+ }
+ // End loop over dy
+
+ setOutput(accumulator);
+ }
+ `}return n}();var z9=function(){function n(t,e,i,r){this.variableNames=["A"],this.outputShape=[];var a=t[0],s=t[1],o=t[2],l=t[3];this.outputShape=[a,e,i,l];var u=[r&&e>1?s-1:s,r&&i>1?o-1:o],c=[r&&e>1?e-1:e,r&&i>1?i-1:i];this.userCode=`
+ const vec2 effectiveInputOverOutputRatioRC = vec2(
+ `+u[0]/c[0]+`,
+ `+u[1]/c[1]+`);
+ const vec2 inputShapeRC = vec2(`+s+".0, "+o+`.0);
+
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords[0];
+ int d = coords[3];
+ ivec2 yRC = coords.yz;
+
+ // Fractional source index.
+ vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;
+
+ // Compute the four integer indices.
+ ivec2 sourceFloorRC = ivec2(sourceFracIndexRC);
+ ivec2 sourceCeilRC = ivec2(
+ min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));
+
+ float topLeft = getA(b, sourceFloorRC.x, sourceFloorRC.y, d);
+ float bottomLeft = getA(b, sourceCeilRC.x, sourceFloorRC.y, d);
+ float topRight = getA(b, sourceFloorRC.x, sourceCeilRC.y, d);
+ float bottomRight = getA(b, sourceCeilRC.x, sourceCeilRC.y, d);
+
+ vec2 fracRC = sourceFracIndexRC - vec2(sourceFloorRC);
+
+ float top = topLeft + (topRight - topLeft) * fracRC.y;
+ float bottom = bottomLeft + (bottomRight - bottomLeft) * fracRC.y;
+ float newValue = top + (bottom - top) * fracRC.x;
+
+ setOutput(newValue);
+ }
+ `}return n}();var P9=function(){function n(t,e,i,r){this.variableNames=["A"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=[];var a=t[0],s=t[1],o=t[2],l=t[3];this.outputShape=[a,e,i,l];var u=[r&&e>1?s-1:s,r&&i>1?o-1:o],c=[r&&e>1?e-1:e,r&&i>1?i-1:i];this.userCode=`
+ const vec3 effectiveInputOverOutputRatioRC = vec3(
+ `+u[0]/c[0]+`,
+ `+u[1]/c[1]+`,
+ `+u[1]/c[1]+`);
+ const vec3 inputShapeRC = vec3(`+s+".0, "+o+`.0,
+ `+o+`.0);
+
+ float getAValue(int b, int r, int c, int d) {
+ return getChannel(getA(b, r, c, d), vec2(c, d));
+ }
+
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords[0];
+ int d = coords[3];
+ // Calculate values for next column in yRC.z.
+ ivec3 yRC = coords.yzz + ivec3(0, 0, 1);
+
+ // Fractional source index.
+ vec3 sourceFracIndexRC = vec3(yRC) * effectiveInputOverOutputRatioRC;
+
+ // Compute the four integer indices.
+ ivec3 sourceFloorRC = ivec3(sourceFracIndexRC);
+ ivec3 sourceCeilRC = ivec3(
+ min(inputShapeRC - 1.0, ceil(sourceFracIndexRC)));
+
+ // Should we calculate next column and row elements in 2x2 packed cell.
+ bool hasNextCol = d < `+(l-1)+`;
+ bool hasNextRow = coords.z < `+(i-1)+`;
+
+ // In parallel, construct four corners for all four components in
+ // packed 2x2 cell.
+ vec4 topLeft = vec4(
+ getAValue(b, sourceFloorRC.x, sourceFloorRC.y, d),
+ hasNextCol ? getAValue(b, sourceFloorRC.x, sourceFloorRC.y, d + 1)
+ : 0.0,
+ hasNextRow ? getAValue(b, sourceFloorRC.x, sourceFloorRC.z, d)
+ : 0.0,
+ (hasNextRow && hasNextCol) ?
+ getAValue(b, sourceFloorRC.x, sourceFloorRC.z, d + 1) : 0.0);
+
+ vec4 bottomLeft = vec4(
+ getAValue(b, sourceCeilRC.x, sourceFloorRC.y, d),
+ hasNextCol ? getAValue(b, sourceCeilRC.x, sourceFloorRC.y, d + 1)
+ : 0.0,
+ hasNextRow ? getAValue(b, sourceCeilRC.x, sourceFloorRC.z, d)
+ : 0.0,
+ (hasNextRow && hasNextCol) ?
+ getAValue(b, sourceCeilRC.x, sourceFloorRC.z, d + 1) : 0.0);
+
+ vec4 topRight = vec4(
+ getAValue(b, sourceFloorRC.x, sourceCeilRC.y, d),
+ hasNextCol ? getAValue(b, sourceFloorRC.x, sourceCeilRC.y, d + 1)
+ : 0.0,
+ hasNextRow ? getAValue(b, sourceFloorRC.x, sourceCeilRC.z, d)
+ : 0.0,
+ (hasNextRow && hasNextCol) ?
+ getAValue(b, sourceFloorRC.x, sourceCeilRC.z, d + 1) : 0.0);
+
+ vec4 bottomRight = vec4(
+ getAValue(b, sourceCeilRC.x, sourceCeilRC.y, d),
+ hasNextCol ? getAValue(b, sourceCeilRC.x, sourceCeilRC.y, d + 1)
+ : 0.0,
+ hasNextRow ? getAValue(b, sourceCeilRC.x, sourceCeilRC.z, d)
+ : 0.0,
+ (hasNextRow && hasNextCol) ?
+ getAValue(b, sourceCeilRC.x, sourceCeilRC.z, d + 1) : 0.0);
+
+ vec3 fracRC = sourceFracIndexRC - vec3(sourceFloorRC);
+
+ vec4 top = mix(topLeft, topRight, fracRC.yyzz);
+ vec4 bottom = mix(bottomLeft, bottomRight, fracRC.yyzz);
+ vec4 newValue = mix(top, bottom, fracRC.x);
+
+ setOutput(newValue);
+ }
+ `}return n}();var _9=function(){function n(t,e,i){this.variableNames=["dy"],this.outputShape=[],this.outputShape=e.shape;var r=e.shape,a=r[1],s=r[2],o=t.shape,l=o[1],u=o[2],c=[i&&l>1?a-1:a,i&&u>1?s-1:s],h=[i&&l>1?l-1:l,i&&u>1?u-1:u],d=c[0]/h[0],p=c[1]/h[1],f=1/d,m=1/p,g=Math.ceil(f)*2+2,v=Math.ceil(m)*2+2;this.userCode=`
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords[0];
+ int d = coords[3];
+ int r = coords[1];
+ int c = coords[2];
+
+ float accumulator = 0.0;
+
+ const float heightScale = float(`+d+`);
+ const float widthScale = float(`+p+`);
+
+ const float invHeightScale = float(`+f+`);
+ const float invWidthScale = float(`+m+`);
+
+ const int winHeight = int(`+g+`);
+ const int winWidth = int(`+v+`);
+
+ // Compute bounds for where in dy we will look
+ float startRLerp = floor(float(r) * invHeightScale);
+ int startDyR = int(floor(startRLerp - float(winHeight / 2)));
+
+ float startCLerp = floor(float(c) * invWidthScale);
+ int startDyC = int(floor(startCLerp - float(winWidth / 2)));
+
+ // Loop over dy
+ for (int dyROffset = 0; dyROffset < winHeight; dyROffset++) {
+ int dyR = dyROffset + startDyR;
+
+ // Guard against the window exceeding the bounds of dy
+ if (dyR < 0 || dyR >= `+l+`) {
+ continue;
+ }
+
+ for (int dyCOffset = 0; dyCOffset < winWidth; dyCOffset++) {
+ int dyC = dyCOffset + startDyC;
+
+ // Guard against the window exceeding the bounds of dy
+ if (dyC < 0 || dyC >= `+u+`) {
+ continue;
+ }
+
+ float sourceFracRow =
+ float(`+c[0]+`) *
+ (float(dyR) / float(`+h[0]+`));
+
+ float sourceFracCol =
+ float(`+c[1]+`) *
+ (float(dyC) / float(`+h[1]+`));
+
+ int sourceNearestRow = int(min(
+ float(int(`+a+`) - 1),
+ `+i+` ? float(round(sourceFracRow)) :
+ float(floor(sourceFracRow))));
+
+ int sourceNearestCol = int(min(
+ float(int(`+s+`) - 1),
+ `+i+` ? float(round(sourceFracCol)) :
+ float(floor(sourceFracCol))));
+
+ if (r == sourceNearestRow && c == sourceNearestCol) {
+ accumulator += getDy(b, dyR, dyC, d);
+ }
+ }
+ }
+ // End loop over dy
+
+ setOutput(accumulator);
+ }
+ `}return n}();var M9=function(){function n(t,e,i,r){this.variableNames=["A"],this.outputShape=[];var a=t[0],s=t[1],o=t[2],l=t[3];this.outputShape=[a,e,i,l];var u=[r&&e>1?s-1:s,r&&i>1?o-1:o],c=[r&&e>1?e-1:e,r&&i>1?i-1:i],h=r?"0.5":"0.0";this.userCode=`
+ const vec2 effectiveInputOverOutputRatioRC = vec2(
+ `+u[0]/c[0]+`,
+ `+u[1]/c[1]+`);
+ const vec2 inputShapeRC = vec2(`+s+".0, "+o+`.0);
+
+ void main() {
+ ivec4 coords = getOutputCoords();
+ int b = coords[0];
+ int d = coords[3];
+ ivec2 yRC = coords.yz;
+
+ // Fractional source index.
+ vec2 sourceFracIndexRC = vec2(yRC) * effectiveInputOverOutputRatioRC;
+
+ // Compute the coordinators of nearest neighbor point.
+ ivec2 sourceNearestRC = ivec2(
+ min(inputShapeRC - 1.0, floor(sourceFracIndexRC + `+h+`)));
+
+ float newValue = getA(b, sourceNearestRC.x, sourceNearestRC.y, d);
+
+ setOutput(newValue);
+ }
+ `}return n}();var H9=function(){function n(t,e){this.variableNames=["x"];var i=t.length;if(i>4)throw new Error("WebGL backend: Reverse of rank-"+i+" tensor is not yet supported");if(this.outputShape=t,i===1){this.userCode=`
+ void main() {
+ int coord = getOutputCoords();
+ setOutput(getX(`+t[0]+` - coord - 1));
+ }
+ `;return}var r=function(o){return e.indexOf(o)!==-1&&t[o]!==1?t[o]+" - coords["+o+"] - 1":"coords["+o+"]"},a=t.map(function(o,l){return r(l)}).join(","),s=Je(i);this.userCode=`
+ void main() {
+ `+s+` coords = getOutputCoords();
+ setOutput(getX(`+a+`));
+ }
+ `}return n}();var V9=function(){function n(t,e){this.variableNames=["x"],this.packedInputs=!0,this.packedOutput=!0;var i=t.length;if(i>4)throw new Error("WebGL backend: Reverse of rank-"+i+" tensor is not yet supported");this.outputShape=t;var r=Jt("rc",i),a=r[i-1]+" + 1 < "+this.outputShape[i-1],s=r[i-2]+" + 1 < "+this.outputShape[i-2],o=Je(i);i===1?this.userCode=`
+ void main(){
+ int rc = getOutputCoords();
+ vec4 result = vec4(0.);
+ result.r = getChannel(getX(`+t[0]+` - rc - 1),
+ `+t[0]+` - rc - 1);
+ if(`+a+`){
+ result.g = getChannel(getX(`+t[0]+` - (rc + 1) - 1),
+ `+t[0]+` - (rc + 1) - 1);
+ }
+ setOutput(result);
+ }
+ `:this.userCode=`
+ void main() {
+ `+o+` rc = getOutputCoords();
+ vec4 result = vec4(0.);
+ result.r = `+l(r.slice())+`;
+ if(`+a+`){
+ result.g = `+u(r.slice())+`;
+ }
+ if(`+s+`) {
+ result.b = `+c(r.slice())+`;
+ if(`+a+`) {
+ result.a = `+h(r.slice())+`;
+ }
+ }
+ setOutput(result);
+ }
+ `;function l(f){return d(f)}function u(f){return f[i-1]="("+f[i-1]+" + 1)",d(f)}function c(f){return f[i-2]="("+f[i-2]+" + 1)",d(f)}function h(f){return f[i-1]="("+f[i-1]+" + 1)",f[i-2]="("+f[i-2]+" + 1)",d(f)}function d(f){var m=t.map(function(b,w){return p(w,f)}),g=m.join(","),v=m.slice(-2).join(",");return"getChannel(getX("+g+"), vec2("+v+"))"}function p(f,m){return e.indexOf(f)!==-1&&t[f]!==1?t[f]+" - "+m[f]+" - 1":""+m[f]}}return n}();var oL=function(){function n(t,e,i,r,a,s,o){this.variableNames=["updates","indices","defaultValue"],this.outputShape=s;var l=Je(a.length),u=Je(s.length),c="";i===1?c="i":i===2&&(c="i, j");var h="getIndices("+c+")",d="";r===1?d="i":r===2&&(d="i, coords[1]");var p="getUpdates("+d+")",f=e>1?"strides[j]":"strides";this.userCode=`
+ `+l+" strides = "+l+"("+a+`);
+
+ void main() {
+ `+u+` coords = getOutputCoords();
+ float sum = 0.0;
+ bool found = false;
+ for (int i = 0; i < `+t+`; i++) {
+ int flattenedIndex = 0;
+ for (int j = 0; j < `+e+`; j++) {
+ int index = round(`+h+`);
+ flattenedIndex += index * `+f+`;
+ }
+ if (flattenedIndex == coords[0]) {
+ sum += `+p+`;
+ found = true;
+ }
+ }
+ setOutput(mix(getDefaultValue(), sum, float(found)));
+ }
+ `}return n}();var q9=function(){function n(t,e){this.variableNames=["x","segmentIds"];var i=t.windowSize,r=t.batchSize,a=t.inSize,s=t.numSegments,o=s*Math.ceil(a/i);this.outputShape=[r,o];var l="0.0",u="sumValue",c=Math.floor(i/4)*4,h=i%4,d=`
+ sumValue += dot(values, segFilter);
+ `,p="";a%i>0&&(p=`
+ if (inIdx < 0 || inIdx >= `+a+`) {
+ return initializationValue;
+ }
+ `);var f="";a%i>0&&(f=`
+ if (inIdx < 0 || inIdx >= `+a+`) {
+ return -1.0;
+ }
+ `),this.userCode=`
+ const float initializationValue = `+l+`;
+
+ float getValue(int batch, int inIdx) {
+ `+p+`
+ return getX(batch, inIdx);
+ }
+
+ float getSegmentIdAtIndex(int inIdx) {
+ `+f+`
+ return getSegmentIds(inIdx);
+ }
+
+ void main() {
+ ivec2 coords = getOutputCoords();
+ int batch = coords[0];
+ int outIdx = coords[1];
+ int inOffset = int(floor(float(outIdx) / float(
+ `+s+")) * float("+i+`));
+ int currentSeg = int(mod(float(outIdx), float(`+s+`)));
+
+ float sumValue = 0.0;
+
+ for (int i = 0; i < `+c+`; i += 4) {
+ int inIdx = inOffset + i;
+ vec4 values = vec4(
+ getValue(batch, inIdx),
+ getValue(batch, inIdx + 1),
+ getValue(batch, inIdx + 2),
+ getValue(batch, inIdx + 3)
+ );
+
+ vec4 segFilter = vec4(
+ int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
+ int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
+ int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,
+ int(getSegmentIdAtIndex(inIdx + 3)) == currentSeg ? 1 : 0
+ );
+
+ `+d+`
+ }
+
+ int inIdx = inOffset + `+c+`;
+ if (`+(h===1)+`) {
+ vec4 values = vec4(
+ getValue(batch, inIdx),
+ initializationValue,
+ initializationValue,
+ initializationValue
+ );
+
+ int inIdxSeg = int(getSegmentIdAtIndex(inIdx));
+
+ vec4 segFilter = vec4(
+ int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
+ 0,
+ 0,
+ 0
+ );
+
+ `+d+`
+ } else if (`+(h===2)+`) {
+ vec4 values = vec4(
+ getValue(batch, inIdx),
+ getValue(batch, inIdx + 1),
+ initializationValue,
+ initializationValue
+ );
+
+ vec4 segFilter = vec4(
+ int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
+ int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
+ 0,
+ 0
+ );
+
+ `+d+`
+ } else if (`+(h===3)+`) {
+ vec4 values = vec4(
+ getValue(batch, inIdx),
+ getValue(batch, inIdx + 1),
+ getValue(batch, inIdx + 2),
+ initializationValue
+ );
+
+ vec4 segFilter = vec4(
+ int(getSegmentIdAtIndex(inIdx)) == currentSeg ? 1 : 0,
+ int(getSegmentIdAtIndex(inIdx + 1)) == currentSeg ? 1 : 0,
+ int(getSegmentIdAtIndex(inIdx + 2)) == currentSeg ? 1 : 0,
+ 0
+ );
+
+ `+d+`
+ }
+ setOutput(`+u+`);
+ }
+ `}return n}();var G9=function(){function n(t,e,i){this.variableNames=["c","a","b"],this.outputShape=e;var r,a;if(i>4)throw Error("Where for rank "+i+" is not yet supported");if(i===1)a="resRC",r="resRC";else{for(var s=["resRC.x","resRC.y","resRC.z","resRC.w"],o=[],l=[],u=0;u= 1.0) {
+ setOutput(getA(`+a+`));
+ } else {
+ setOutput(getB(`+a+`));
+ }
+ }
+ `}return n}();var K9=function(){function n(t){this.variableNames=["source"],this.outputShape=t,this.rank=t.length;var e=Je(this.rank),i="uniform int start["+this.rank+"];",r=Y9(this.rank),a,s=t.map(function(o,l){return"sourceLoc."+bp[l]+" = start["+l+"] + coords."+bp[l]+";"});a=`
+ `+e+` sourceLoc;
+ `+e+` coords = getOutputCoords();
+ `+s.join(`
+`)+`
+ `,this.userCode=`
+ `+i+`
+ void main() {
+ `+a+`
+ setOutput(getSource(`+r+`));
+ }
+ `}return n.prototype.getCustomSetupFunc=function(t){var e=this;if(t.length!==this.rank)throw Error("The rank ("+this.rank+") of the program must match the "+("length of start ("+t.length+")"));return function(i,r){if(e.startLoc==null&&(e.startLoc=i.getUniformLocationNoThrow(r,"start"),e.startLoc==null))return;i.gl.uniform1iv(e.startLoc,t)}},n}(),bp=["x","y","z","w","u","v"];function Y9(n){if(n===1)return"sourceLoc";if(n<=6)return bp.slice(0,n).map(function(t){return"sourceLoc."+t}).join(",");throw Error("Slicing for rank "+n+" is not yet supported")}var j9=function(){function n(t){this.variableNames=["source"],this.packedInputs=!0,this.packedOutput=!0,this.outputShape=t,this.rank=t.length;var e=Je(this.rank),i=Jt("coords",this.rank),r=Jt("sourceLoc",this.rank),a=this.rank===1?"sourceLoc":"vec2("+r.slice(-2).join()+")",s="getChannel(getSource("+r.join()+"), "+a+")",o=`
+ result.x = `+s+`;
+ if (++`+i[this.rank-1]+" < "+t[this.rank-1]+`) {
+ ++`+r[this.rank-1]+`;
+ result.y = `+s+`;
+ --`+r[this.rank-1]+`;
+ }
+ `,l=this.rank===1?"":`
+ --`+i[this.rank-1]+`;
+ if (++`+i[this.rank-2]+" < "+t[this.rank-2]+`) {
+ ++`+r[this.rank-2]+`;
+ result.z = `+s+`;
+ if (++`+i[this.rank-1]+" < "+t[this.rank-1]+`) {
+ ++`+r[this.rank-1]+`;
+ result.w = `+s+`;
+ }
+ }
+ `,u=this.rank<=4?`sourceLoc = coords +
+ `+e+"("+t.map(function(c,h){return"start["+h+"]"}).join()+");":t.map(function(c,h){return r[h]+" = "+i[h]+" + start["+h+"];"}).join(`
+`);this.userCode=`
+ uniform int start[`+this.rank+`];
+ void main() {
+ `+e+` coords = getOutputCoords();
+ `+e+` sourceLoc;
+ `+u+`
+ vec4 result = vec4(0.);
+ `+o+`
+ `+l+`
+ setOutput(result);
+ }
+ `}return n.prototype.getCustomSetupFunc=function(t){var e=this;if(t.length!==this.rank)throw Error("The rank ("+this.rank+") of the program must match the "+("length of start ("+t.length+")"));return function(i,r){if(e.startLoc==null&&(e.startLoc=i.getUniformLocationNoThrow(r,"start"),e.startLoc==null))return;i.gl.uniform1iv(e.startLoc,t)}},n}();var $9=function(){function n(t,e,i){this.variableNames=["x"],this.outputShape=i;var r=i.length,a=Je(i.length),s=Je(i.length),o="";if(r===1)o="coords * strides + begin";else{var l=0;o=i.map(function(u,c){return l++,i.length===1?"coords * strides["+c+"] + begin["+c+"]":"coords["+(l-1)+"] * strides["+c+"] + begin["+c+"]"}).join(",")}this.userCode=`
+ `+a+" begin = "+a+"("+t+`);
+ `+a+" strides = "+a+"("+e+`);
+
+ void main() {
+ `+s+` coords = getOutputCoords();
+ setOutput(getX(`+o+`));
+ }
+ `}return n}();var X9=function(){function n(t){this.gpgpu=t,this.numUsedTextures=0,this.numFreeTextures=0,this._numBytesAllocated=0,this._numBytesFree=0,this.freeTextures={},this.logEnabled=!1,this.usedTextures={}}return n.prototype.acquireTexture=function(t,e,i){var r=uL(e,i),a=cL(t,r,i);a in this.freeTextures||(this.freeTextures[a]=[]),a in this.usedTextures||(this.usedTextures[a]=[]);var s=lL(t,r,this.gpgpu.gl,this.gpgpu.textureConfig,i);if(this.freeTextures[a].length>0){this.numFreeTextures--,this.numUsedTextures++,this._numBytesFree-=s,this.log();var o=this.freeTextures[a].shift();return this.usedTextures[a].push(o),o}var l;return r===Rt.PACKED_2X2_FLOAT32?l=this.gpgpu.createPackedMatrixTexture(t[0],t[1]):r===Rt.PACKED_2X2_FLOAT16?l=this.gpgpu.createFloat16PackedMatrixTexture(t[0],t[1]):r===Rt.UNPACKED_FLOAT32?l=this.gpgpu.createFloat32MatrixTexture(t[0],t[1]):r===Rt.UNPACKED_FLOAT16?l=this.gpgpu.createFloat16MatrixTexture(t[0],t[1]):r===Rt.PACKED_4X1_UNSIGNED_BYTE&&(l=this.gpgpu.createUnsignedBytesMatrixTexture(t[0],t[1])),this.usedTextures[a].push(l),this.numUsedTextures++,this._numBytesAllocated+=s,this.log(),l},n.prototype.releaseTexture=function(t,e,i,r){if(this.freeTextures==null)return;var a=uL(i,r),s=cL(e,a,r);s in this.freeTextures||(this.freeTextures[s]=[]);var o=lL(e,a,this.gpgpu.gl,this.gpgpu.textureConfig,r),l=x.env().get("WEBGL_DELETE_TEXTURE_THRESHOLD");l!==-1&&this._numBytesAllocated>l?(this.gpgpu.deleteMatrixTexture(t),this._numBytesAllocated-=o):(this.freeTextures[s].push(t),this.numFreeTextures++,this._numBytesFree+=o),this.numUsedTextures--;var u=this.usedTextures[s],c=u.indexOf(t);if(c<0)throw new Error("Cannot release a texture that was never provided by this texture manager");u.splice(c,1),this.log()},n.prototype.log=function(){if(!this.logEnabled)return;var t=this.numFreeTextures+this.numUsedTextures;console.log("Free/Used",this.numFreeTextures+" / "+this.numUsedTextures,"("+t+")");var e=this._numBytesFree/this._numBytesAllocated;console.log("Bytes allocated: "+this._numBytesAllocated),console.log("Bytes unused: "+this._numBytesFree+" ("+Math.round(100*e)+"%)")},Object.defineProperty(n.prototype,"numBytesAllocated",{get:function(){return this._numBytesAllocated},enumerable:!0,configurable:!0}),Object.defineProperty(n.prototype,"numBytesFree",{get:function(){return this._numBytesFree},enumerable:!0,configurable:!0}),n.prototype.getNumUsedTextures=function(){return this.numUsedTextures},n.prototype.getNumFreeTextures=function(){return this.numFreeTextures},n.prototype.dispose=function(){var t=this;if(this.freeTextures==null)return;for(var e in this.freeTextures)this.freeTextures[e].forEach(function(i){t.gpgpu.deleteMatrixTexture(i)});for(var e in this.usedTextures)this.usedTextures[e].forEach(function(r){t.gpgpu.deleteMatrixTexture(r)});this.freeTextures=null,this.usedTextures=null,this.numUsedTextures=0,this.numFreeTextures=0,this._numBytesAllocated=0,this._numBytesFree=0},n}();function J9(n,t){var e=n;if(t===e.R32F)return 4;if(t===e.R16F)return 2;if(t===e.RGBA32F)return 16;if(t===n.RGBA)return 16;if(t===e.RGBA16F)return 8;throw new Error("Unknown internal format "+t)}function lL(n,t,e,i,r){var a=Z9(t,i),s;if(r){var o=ha(n[0],n[1]),l=o[0],u=o[1];s=l*u}else{var c=gs(n[0],n[1]),h=c[0],d=c[1];s=h*d}var p=J9(e,a);return s*p}function Z9(n,t){switch(n){case Rt.PACKED_2X2_FLOAT32:return mp(t);case Rt.PACKED_2X2_FLOAT16:return gp(t);case Rt.UNPACKED_FLOAT32:return dp(t);case Rt.UNPACKED_FLOAT16:return pp(t);case Rt.PACKED_4X1_UNSIGNED_BYTE:return fp(t);default:throw new Error("Unknown physical texture type "+n)}}function Q9(n){return x.env().getBool("WEBGL_RENDER_FLOAT32_ENABLED")?n?Rt.PACKED_2X2_FLOAT32:Rt.UNPACKED_FLOAT32:n?Rt.PACKED_2X2_FLOAT16:Rt.UNPACKED_FLOAT16}function uL(n,t){if(n===an.UPLOAD)return Rt.PACKED_2X2_FLOAT32;if(n===an.RENDER||n==null)return Q9(t);if(n===an.DOWNLOAD||n===an.PIXELS)return Rt.PACKED_4X1_UNSIGNED_BYTE;throw new Error("Unknown logical texture type "+n)}function cL(n,t,e){return n[0]+"_"+n[1]+"_"+t+"_"+e}var t6=function(){function n(t,e){this.variableNames=["A"];for(var i=new Array(t.length),r=0;r