| 166 | } |
| 167 | |
| 168 | getUserCode(): string { |
| 169 | let updateSnippet: string; |
| 170 | if (this.poolType === 'avg') { |
| 171 | updateSnippet = `resultValue += value; count += 1.0;`; |
| 172 | } else if (this.computePositions) { |
| 173 | const positionStr = this.flattenPositions ? |
| 174 | (this.includeBatchIndex ? |
| 175 | `(((batch * uniforms.xShape.y + xD) * uniforms.xShape.z + xR) * uniforms.xShape.w + xC) * uniforms.xShape.u + ch` : |
| 176 | `((xD * uniforms.xShape.z + xR) * uniforms.xShape.w + xC) * uniforms.xShape.u + ch`) : |
| 177 | `wD * uniforms.filterDims.y * uniforms.filterDims.y + wR * uniforms.filterDims.z + wC`; |
| 178 | updateSnippet = `let currMaxValue = mix(value, maxValue, maxValueFound); |
| 179 | if (value >= currMaxValue) { |
| 180 | maxValue = value; |
| 181 | maxValueFound = 1.0; |
| 182 | maxPosition = ${positionStr}; |
| 183 | }`; |
| 184 | } else { |
| 185 | updateSnippet = `resultValue = max(value, resultValue);`; |
| 186 | } |
| 187 | |
| 188 | let returnValue = `resultValue`; |
| 189 | if (this.poolType === 'avg') { |
| 190 | returnValue = `resultValue / max(count, 1.0)`; |
| 191 | } |
| 192 | |
| 193 | const userCode = ` |
| 194 | ${main('index')} { |
| 195 | if (index < uniforms.size) { |
| 196 | let coords = getCoordsFromIndex(index); |
| 197 | let batch = coords.x; |
| 198 | let ch = coords.u; |
| 199 | |
| 200 | let xCorner = vec3<i32>(coords.y, coords.z, coords.w) * uniforms.strides - uniforms.pads; |
| 201 | let xDCorner = xCorner.x; |
| 202 | let xRCorner = xCorner.y; |
| 203 | let xCCorner = xCorner.z; |
| 204 | |
| 205 | ${ |
| 206 | this.computePositions ? |
| 207 | `var maxValue = 0.0; |
| 208 | var maxValueFound = 0.0; |
| 209 | var maxPosition = 0;` : |
| 210 | `var resultValue = ${ |
| 211 | this.poolType === 'avg' ? '0.0' : '-1.0 / pow(10.0, -20.0)'};`} |
| 212 | |
| 213 | var count = 0.0; |
| 214 | for (var wD = 0; wD < uniforms.filterDims.x; wD++) { |
| 215 | let xD = xDCorner + wD; |
| 216 | if (xD < 0 || xD >= uniforms.convDims.x) { |
| 217 | continue; |
| 218 | } |
| 219 | |
| 220 | for (var wR = 0; wR < uniforms.filterDims.y; wR++) { |
| 221 | let xR = xRCorner + wR; |
| 222 | if (xR < 0 || xR >= uniforms.convDims.y) { |
| 223 | continue; |
| 224 | } |
| 225 | |