| 24 | userCode: string; |
| 25 | |
| 26 | constructor( |
| 27 | reduceInfo: backend_util.ReduceInfo, |
| 28 | reduceType: 'all'|'any'|'max'|'min'|'sum'|'prod') { |
| 29 | const {windowSize, batchSize, inSize, outSize} = reduceInfo; |
| 30 | this.outputShape = [batchSize, outSize]; |
| 31 | |
| 32 | let initializationValue = '0.0'; |
| 33 | let compareOp = ``; |
| 34 | |
| 35 | if (reduceType === 'prod') { |
| 36 | initializationValue = '1.0'; |
| 37 | } else if (reduceType === 'min') { |
| 38 | // WebGL on Firefox Linux can't compile 1/0 so we do 1/eps. |
| 39 | initializationValue = '1.0 / 1e-20'; |
| 40 | compareOp = `min`; |
| 41 | } else if (reduceType === 'max') { |
| 42 | // WebGL on Firefox Linux can't compile 1/0 so we do 1/eps. |
| 43 | initializationValue = '-1.0 / 1e-20'; |
| 44 | compareOp = `max`; |
| 45 | } |
| 46 | |
| 47 | let returnValue = `${reduceType}(${reduceType}(${reduceType}(` + |
| 48 | 'minMaxValue[0], minMaxValue[1]), minMaxValue[2]), minMaxValue[3])'; |
| 49 | |
| 50 | if (reduceType === 'sum') { |
| 51 | returnValue = `sumValue`; |
| 52 | } else if (reduceType === 'prod') { |
| 53 | returnValue = `prodValue`; |
| 54 | } else if (reduceType === 'all') { |
| 55 | returnValue = `allValue`; |
| 56 | } else if (reduceType === 'any') { |
| 57 | returnValue = `anyValue`; |
| 58 | } |
| 59 | |
| 60 | const windowSizeNearestVec4 = Math.floor(windowSize / 4) * 4; |
| 61 | const windowSizeVec4Remainder = windowSize % 4; |
| 62 | |
| 63 | let updateSnippet = ` |
| 64 | if (${reduceType === 'sum'}) { |
| 65 | sumValue += dot(values, ones); |
| 66 | } else if (${reduceType === 'prod'}) { |
| 67 | vec2 tmp = vec2(values[0], values[1]) * vec2(values[2], values[3]); |
| 68 | prodValue *= tmp[0] * tmp[1]; |
| 69 | } else { |
| 70 | minMaxValue = ${compareOp}(values, minMaxValue); |
| 71 | if (${reduceType === 'min'} || ${reduceType === 'max'}) { |
| 72 | minMaxValue = ${compareOp}(values, minMaxValue); |
| 73 | bvec4 isNaN = isnan(values); |
| 74 | if (isNaN.r || isNaN.g || isNaN.b || isNaN.a) { |
| 75 | minMaxValue = vec4(NAN); |
| 76 | } |
| 77 | } |
| 78 | } |
| 79 | `; |
| 80 | |
| 81 | let vecType = `vec4`; |
| 82 | |
| 83 | if (reduceType === 'all') { |