| 24 | userCode: string; |
| 25 | |
| 26 | constructor( |
| 27 | reduceInfo: backend_util.ReduceInfo, op: 'max'|'min', |
| 28 | firstPass: boolean) { |
| 29 | const {windowSize, batchSize, outSize} = reduceInfo; |
| 30 | if (!firstPass) { |
| 31 | this.variableNames.push('bestIndicesA'); |
| 32 | } |
| 33 | this.outputShape = [batchSize, outSize]; |
| 34 | const compOp = (op === 'max') ? '>' : '<'; |
| 35 | const indexSnippet = firstPass ? |
| 36 | 'inOffset + i;' : |
| 37 | 'round(getBestIndicesA(batch, inOffset + i));'; |
| 38 | |
| 39 | this.userCode = ` |
| 40 | void main() { |
| 41 | ivec2 coords = getOutputCoords(); |
| 42 | int batch = coords[0]; |
| 43 | int outIdx = coords[1]; |
| 44 | int inOffset = outIdx * ${windowSize}; |
| 45 | |
| 46 | int bestIndex = inOffset; |
| 47 | float bestValue = getA(batch, bestIndex); |
| 48 | |
| 49 | for (int i = 0; i < ${windowSize}; i++) { |
| 50 | int inIdx = ${indexSnippet}; |
| 51 | float candidate = getA(batch, inIdx); |
| 52 | if (candidate ${compOp} bestValue) { |
| 53 | bestValue = candidate; |
| 54 | bestIndex = inIdx; |
| 55 | } |
| 56 | } |
| 57 | setOutput(float(bestIndex)); |
| 58 | } |
| 59 | `; |
| 60 | } |
| 61 | } |