| 24 | userCode: string; |
| 25 | |
| 26 | constructor( |
| 27 | xShape: number[], meanShape: number[], varianceShape: number[], |
| 28 | offsetShape: number[]|null, scaleShape: number[]|null, |
| 29 | varianceEpsilon: number) { |
| 30 | this.variableNames = ['x', 'mean', 'variance']; |
| 31 | backend_util.assertAndGetBroadcastShape(xShape, meanShape); |
| 32 | backend_util.assertAndGetBroadcastShape(xShape, varianceShape); |
| 33 | |
| 34 | let offsetSnippet = '0.0'; |
| 35 | if (offsetShape != null) { |
| 36 | backend_util.assertAndGetBroadcastShape(xShape, offsetShape); |
| 37 | this.variableNames.push('offset'); |
| 38 | offsetSnippet = 'getOffsetAtOutCoords()'; |
| 39 | } |
| 40 | |
| 41 | let scaleSnippet = '1.0'; |
| 42 | if (scaleShape != null) { |
| 43 | backend_util.assertAndGetBroadcastShape(xShape, scaleShape); |
| 44 | this.variableNames.push('scale'); |
| 45 | scaleSnippet = 'getScaleAtOutCoords()'; |
| 46 | } |
| 47 | |
| 48 | this.outputShape = xShape; |
| 49 | this.userCode = ` |
| 50 | void main() { |
| 51 | float x = getXAtOutCoords(); |
| 52 | float mean = getMeanAtOutCoords(); |
| 53 | float variance = getVarianceAtOutCoords(); |
| 54 | float offset = ${offsetSnippet}; |
| 55 | float scale = ${scaleSnippet}; |
| 56 | float inv = scale * inversesqrt(variance + float(${varianceEpsilon})); |
| 57 | setOutput(dot(vec3(x, -mean, offset), vec3(inv, inv, 1))); |
| 58 | } |
| 59 | `; |
| 60 | } |
| 61 | } |