|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2019 Google LLC. All Rights Reserved. |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * ============================================================================= |
| 16 | + */ |
| 17 | + |
| 18 | +import {backend_util, util} from '@tensorflow/tfjs-core'; |
| 19 | +import {computeDispatch} from '../webgpu_util'; |
| 20 | +import {WebGPUProgram} from './webgpu_program'; |
| 21 | + |
| 22 | +export class DepthwiseConv2DProgram implements WebGPUProgram { |
| 23 | + outputShape: number[]; |
| 24 | + userCode: string; |
| 25 | + dispatchLayout: {x: number[], y: number[], z: number[]}; |
| 26 | + dispatch: [number, number, number]; |
| 27 | + variableNames = ['x', 'W']; |
| 28 | + uniforms = 'ivec2 filterDims, pad, stride;'; |
| 29 | + workGroupSize: [number, number, number] = [4, 8, 1]; |
| 30 | + |
| 31 | + constructor(convInfo: backend_util.Conv2DInfo) { |
| 32 | + this.outputShape = convInfo.outShape; |
| 33 | + this.dispatchLayout = {x: [2], y: [1], z: [0, 3]}; |
| 34 | + this.dispatch = computeDispatch( |
| 35 | + this.dispatchLayout, this.outputShape, this.workGroupSize); |
| 36 | + const xNumRows = convInfo.inHeight; |
| 37 | + const xNumCols = convInfo.inWidth; |
| 38 | + const padTop = convInfo.padInfo.top; |
| 39 | + const padLeft = convInfo.padInfo.left; |
| 40 | + const strideHeight = convInfo.strideHeight; |
| 41 | + const strideWidth = convInfo.strideWidth; |
| 42 | + const dilationHeight = convInfo.dilationHeight; |
| 43 | + const dilationWidth = convInfo.dilationWidth; |
| 44 | + const filterHeight = convInfo.filterHeight; |
| 45 | + const filterWidth = convInfo.filterWidth; |
| 46 | + const channelMul = convInfo.outChannels / convInfo.inChannels; |
| 47 | + |
| 48 | + util.assert( |
| 49 | + convInfo.dataFormat === 'channelsLast', |
| 50 | + () => 'TODO: NCHW is unimplemented'); |
| 51 | + util.assert( |
| 52 | + convInfo.dilationHeight === 1 && convInfo.dilationWidth === 1, |
| 53 | + () => 'TODO: Dilation is unimplemented'); |
| 54 | + |
| 55 | + this.userCode = ` |
| 56 | + const ivec2 strides = ivec2(${strideHeight}, ${strideWidth}); |
| 57 | + const ivec2 pads = ivec2(${padTop}, ${padLeft}); |
| 58 | +
|
| 59 | + void writeResult(int batch, int row, int col, int chan, float value) { |
| 60 | + ivec4 coord = ivec4(batch, row, col, chan); |
| 61 | + if (coordIsValid(coord, outShape)) { |
| 62 | + setOutput(batch, row, col, chan, value); |
| 63 | + } |
| 64 | + } |
| 65 | +
|
| 66 | + void main() { |
| 67 | + ivec4 coords = getOutputCoords(); |
| 68 | + int batch = coords[0]; |
| 69 | + ivec2 xRCCorner = coords.yz * strides - pads; |
| 70 | + int d2 = coords[3]; |
| 71 | + int d1 = d2 / ${channelMul}; |
| 72 | + int q = d2 - d1 * ${channelMul}; |
| 73 | +
|
| 74 | + int xRCorner = xRCCorner.x; |
| 75 | + int xCCorner = xRCCorner.y; |
| 76 | +
|
| 77 | + // Convolve x(?, ?, d1) with w(:, :, d1, q) to get y(yR, yC, d2). |
| 78 | + // ? = to be determined. : = across all values in that axis. |
| 79 | + float dotProd = 0.0; |
| 80 | + // TODO(xing.xu): Flatten the two for loops and vec4 the operations. |
| 81 | + for (int wR = 0; wR < ${filterHeight}; wR++) { |
| 82 | + int xR = xRCorner + wR * ${dilationHeight}; |
| 83 | +
|
| 84 | + if (xR < 0 || xR >= ${xNumRows}) { |
| 85 | + continue; |
| 86 | + } |
| 87 | +
|
| 88 | + for (int wC = 0; wC < ${filterWidth}; wC++) { |
| 89 | + int xC = xCCorner + wC * ${dilationWidth}; |
| 90 | +
|
| 91 | + if (xC < 0 || xC >= ${xNumCols}) { |
| 92 | + continue; |
| 93 | + } |
| 94 | +
|
| 95 | + float xVal = getX(batch, xR, xC, d1); |
| 96 | + float wVal = getW(wR, wC, d1, q); |
| 97 | + dotProd += xVal * wVal; |
| 98 | + } |
| 99 | + } |
| 100 | + writeResult(batch, coords[1], coords[2], d2, dotProd); |
| 101 | + } |
| 102 | + `; |
| 103 | + } |
| 104 | +} |
0 commit comments