| 24 | userCode: string; |
| 25 | |
| 26 | constructor( |
| 27 | convInfo: backend_util.Conv2DInfo, poolType: 'max'|'avg', |
| 28 | computePositions: boolean, flattenPositions = false, |
| 29 | includeBatchInIndex = false) { |
| 30 | if (poolType === 'avg' && computePositions) { |
| 31 | throw new Error('Cannot compute positions for average pool.'); |
| 32 | } |
| 33 | |
| 34 | const filterWidth = convInfo.filterWidth; |
| 35 | const strideHeight = convInfo.strideHeight; |
| 36 | const strideWidth = convInfo.strideWidth; |
| 37 | const dilationHeight = convInfo.dilationHeight; |
| 38 | const dilationWidth = convInfo.dilationWidth; |
| 39 | const effectiveFilterHeight = convInfo.effectiveFilterHeight; |
| 40 | const effectiveFilterWidth = convInfo.effectiveFilterWidth; |
| 41 | |
| 42 | const padTop = convInfo.padInfo.top; |
| 43 | const padLeft = convInfo.padInfo.left; |
| 44 | this.outputShape = convInfo.outShape; |
| 45 | |
| 46 | const isAvgPool = poolType === 'avg'; |
| 47 | const batchFlattenPositionStr = `((batch * ${convInfo.inHeight} + xR) * ${ |
| 48 | convInfo.inWidth} + xC) * ${convInfo.inChannels} + d`; |
| 49 | const flattenPositionStr = |
| 50 | `(xR * ${convInfo.inWidth} + xC) * ${convInfo.inChannels} + d`; |
| 51 | |
| 52 | let initializationValue = '0.0'; |
| 53 | if (!isAvgPool) { |
| 54 | // WebGL on Firefox Linux can't compile 1/0 so we do 1/eps. |
| 55 | initializationValue = '-1.0 / 1e-20'; |
| 56 | } |
| 57 | |
| 58 | if (computePositions) { |
| 59 | const compareOp = '>='; |
| 60 | |
| 61 | this.userCode = ` |
| 62 | const ivec2 strides = ivec2(${strideHeight}, ${strideWidth}); |
| 63 | const ivec2 pads = ivec2(${padTop}, ${padLeft}); |
| 64 | |
| 65 | void main() { |
| 66 | ivec4 coords = getOutputCoords(); |
| 67 | int batch = coords[0]; |
| 68 | int d = coords[3]; |
| 69 | |
| 70 | ivec2 xRCCorner = coords.yz * strides - pads; |
| 71 | int xRCorner = xRCCorner.x; |
| 72 | int xCCorner = xRCCorner.y; |
| 73 | |
| 74 | // max/min x(?, ?, d) to get y(yR, yC, d). |
| 75 | // ? = to be determined |
| 76 | float minMaxValue = 0.0; |
| 77 | float minMaxValueFound = 0.0; |
| 78 | int minMaxPosition = 0; |
| 79 | float avgValue = 0.0; |
| 80 | |
| 81 | for (int wR = 0; wR < ${effectiveFilterHeight}; |
| 82 | wR += ${dilationHeight}) { |
| 83 | int xR = xRCorner + wR; |