| 58 | } |
| 59 | |
| 60 | getUserCode(): string { |
| 61 | let updateSnippet: string; |
| 62 | if (this.poolType === 'avg') { |
| 63 | updateSnippet = `resultValue = resultValue + value; count = count + 1.0;`; |
| 64 | } else if (this.computePositions) { |
| 65 | const positionStr = this.flattenPositions ? |
| 66 | (this.includeBatchIndex ? |
| 67 | `((batch * uniforms.xShape[1] + xR) * uniforms.xShape[2] + xC) * uniforms.xShape[3] + d` : |
| 68 | `(xR * uniforms.xShape[2] + xC) * uniforms.xShape[3] + d`) : |
| 69 | `wR * uniforms.filterDims.y + wC`; |
| 70 | updateSnippet = `let currMaxValue = mix(value, maxValue, maxValueFound); |
| 71 | if (value >= currMaxValue) { |
| 72 | maxValue = value; |
| 73 | maxValueFound = 1.0; |
| 74 | maxPosition = ${positionStr}; |
| 75 | }`; |
| 76 | } else { |
| 77 | updateSnippet = `resultValue = max(value, resultValue);`; |
| 78 | } |
| 79 | |
| 80 | let returnValue = `resultValue`; |
| 81 | if (this.poolType === 'avg') { |
| 82 | returnValue = `resultValue / max(count, 1.0)`; |
| 83 | } |
| 84 | |
| 85 | const userCode = ` |
| 86 | ${main('index')} { |
| 87 | if (index < uniforms.size) { |
| 88 | let coords = getCoordsFromIndex(index); |
| 89 | let batch = coords[0]; |
| 90 | let d = coords[3]; |
| 91 | let xRCCorner = vec2<i32>(coords.yz) * uniforms.strides - uniforms.pads; |
| 92 | let xRCorner = xRCCorner.x; |
| 93 | let xCCorner = xRCCorner.y; |
| 94 | |
| 95 | ${ |
| 96 | this.computePositions ? |
| 97 | `var maxValue = 0.0; |
| 98 | var maxValueFound = 0.0; |
| 99 | var maxPosition = 0;` : |
| 100 | `var resultValue = ${ |
| 101 | this.poolType === 'avg' ? '0.0' : '-1.0 / pow(10.0, -20.0)'};`} |
| 102 | |
| 103 | var count = 0.0; |
| 104 | for (var wR = 0; wR < uniforms.filterDims.x; wR = wR + uniforms.dilations.x) { |
| 105 | let xR = xRCorner + wR; |
| 106 | |
| 107 | if (xR < 0 || xR >= uniforms.convDims.x) { |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | for (var wC = 0; wC < uniforms.filterDims.y; wC = wC + uniforms.dilations.y) { |
| 112 | let xC = xCCorner + wC; |
| 113 | if (xC < 0 || xC >= uniforms.convDims.y) { |
| 114 | continue; |
| 115 | } |
| 116 | |
| 117 | let value = getX(batch, xR, xC, d); |