|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2018 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 {Conv2DInfo} from '../../ops/conv_util'; |
| 19 | +import {GPGPUProgram} from './gpgpu_math'; |
| 20 | + |
| 21 | +export class DepthwiseConvPacked2DProgram implements GPGPUProgram { |
| 22 | + variableNames = ['x', 'W']; |
| 23 | + usesPackedTextures = true; |
| 24 | + outputShape: number[]; |
| 25 | + userCode: string; |
| 26 | + |
| 27 | + constructor(convInfo: Conv2DInfo) { |
| 28 | + this.outputShape = convInfo.outShape; |
| 29 | + |
| 30 | + const xNumRows = convInfo.inHeight; |
| 31 | + const xNumCols = convInfo.inWidth; |
| 32 | + const padTop = convInfo.padInfo.top; |
| 33 | + const padLeft = convInfo.padInfo.left; |
| 34 | + const strideHeight = convInfo.strideHeight; |
| 35 | + const strideWidth = convInfo.strideWidth; |
| 36 | + const filterHeight = convInfo.filterHeight; |
| 37 | + const filterWidth = convInfo.filterWidth; |
| 38 | + const texelsAcross = Math.ceil((filterWidth + 1) / 2); |
| 39 | + |
| 40 | + let mainLoop = `int xR; int xC;`; |
| 41 | + |
| 42 | + for (let r = 0; r < filterHeight; r++) { |
| 43 | + for (let c = -padLeft; c < texelsAcross * 2; c++) { |
| 44 | + mainLoop += `vec4 ${xTexelName(r, c)} = vec4(0.);`; |
| 45 | + } |
| 46 | + |
| 47 | + for (let c = 0; c < filterWidth; c++) { |
| 48 | + mainLoop += ` |
| 49 | + vec4 wR${r}C${c} = vec4(0.); |
| 50 | + vec4 xR${r}C${c} = vec4(0.);`; |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * This vectorized implementation of depthwiseConv works by gathering the |
| 56 | + * values needed for each output channel's dot product into vec4's and then |
| 57 | + * multiplying them all together (this happens in the final double for-loop |
| 58 | + * below). Most of the main loop consists of constructing these vec4's with |
| 59 | + * the minimum number of texture2D calls as possible, which entails logic |
| 60 | + * for making use of all four returned values from a texture2D call at once. |
| 61 | + */ |
| 62 | + for (let r = 0; r < filterHeight; r++) { |
| 63 | + for (let c = 0; c < texelsAcross; c++) { |
| 64 | + const col = c * 2; |
| 65 | + const left = c * 2 + padLeft; |
| 66 | + |
| 67 | + mainLoop += ` |
| 68 | + xR = xRCorner + ${r}; |
| 69 | + xC = xCCorner + ${left}; |
| 70 | +
|
| 71 | + if(xR >= 0 && xR < ${xNumRows} && xC >= 0 && xC < ${xNumCols}) { |
| 72 | + ${xTexelName(r, left)} = getX(batch, xR, xC, d1); |
| 73 | + }`; |
| 74 | + |
| 75 | + if (padLeft === 0) { |
| 76 | + if (col < filterWidth && c === texelsAcross - 1) { |
| 77 | + if (strideWidth > 1) { |
| 78 | + mainLoop += ` |
| 79 | + vec4 ${xTexelName(r, left + 2)} = vec4(0.); |
| 80 | +
|
| 81 | + if(xR >= 0 && xR < ${xNumRows} && xC + 2 < ${xNumCols}) { |
| 82 | + ${xTexelName(r, left + 2)} = getX(batch, xR, xC + 2, d1); |
| 83 | + }`; |
| 84 | + } |
| 85 | + |
| 86 | + mainLoop += ` |
| 87 | + xR${r}C${left} = ${constructTexel(r, left, strideWidth, padLeft)}; |
| 88 | + `; |
| 89 | + } |
| 90 | + } else if (c === 0) { |
| 91 | + mainLoop += ` |
| 92 | + if(xR >= 0 && xR < ${xNumRows} && xC - 2 >= 0) { |
| 93 | + ${xTexelName(r, left - 2)} = getX(batch, xR, xC - 2, d1); |
| 94 | + }`; |
| 95 | + } |
| 96 | + |
| 97 | + if (col > 0) { |
| 98 | + mainLoop += `xR${r}C${left - 2} = |
| 99 | + ${constructTexel(r, left - 2, strideWidth, padLeft)};`; |
| 100 | + } |
| 101 | + |
| 102 | + if (left - 1 >= 0 && left - 1 < filterWidth) { |
| 103 | + mainLoop += `xR${r}C${left - 1} = |
| 104 | + ${constructTexel(r, left - 1, strideWidth, padLeft)};`; |
| 105 | + } |
| 106 | + |
| 107 | + if (col < filterWidth) { |
| 108 | + mainLoop += ` |
| 109 | + vec4 wTexel${r}C${col} = getW(${r}, ${col}, d1, q); |
| 110 | + wR${r}C${col} = vec4(wTexel${r}C${col}.xz, wTexel${r}C${col}.xz); |
| 111 | + `; |
| 112 | + |
| 113 | + if (col + 1 < filterWidth) { |
| 114 | + mainLoop += ` |
| 115 | + vec4 wTexelR${r}C${col + 1} = getW(${r}, ${col + 1}, d1, q); |
| 116 | + wR${r}C${col + 1} = |
| 117 | + vec4(wTexelR${r}C${col + 1}.xz, wTexelR${r}C${col + 1}.xz);`; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + for (let r = 0; r < filterHeight; r++) { |
| 124 | + for (let c = 0; c < filterWidth; c++) { |
| 125 | + mainLoop += `result += xR${r}C${c} * wR${r}C${c};`; |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + this.userCode = ` |
| 130 | + const ivec2 strides = ivec2(${strideHeight}, ${strideWidth}); |
| 131 | + const ivec2 pads = ivec2(${padTop}, ${padLeft}); |
| 132 | +
|
| 133 | + void main() { |
| 134 | + ivec4 coords = getOutputCoords(); |
| 135 | + int batch = coords.x; |
| 136 | + ivec2 xRCCorner = coords.yz * strides - pads; |
| 137 | + int d2 = coords.w; |
| 138 | + int d1 = d2; |
| 139 | + int q = 0; |
| 140 | + int xRCorner = xRCCorner.x; |
| 141 | + int xCCorner = xRCCorner.y; |
| 142 | +
|
| 143 | + vec4 result = vec4(0.); |
| 144 | +
|
| 145 | + ${mainLoop} |
| 146 | +
|
| 147 | + setOutput(result); |
| 148 | + } |
| 149 | + `; |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +function xTexelName(r: number, c: number): string { |
| 154 | + return `xTexelR${r}C${c < 0 ? 'minus' + Math.abs(c).toString() : c}`; |
| 155 | +} |
| 156 | + |
| 157 | +/** |
| 158 | + * Given a 2x2 filter, we want to multiply xR0C0, wR0C0, xR0C1, wR0C1, xR1C0, |
| 159 | + * wR1C0, xR1C1, xR1C1. The xRC's are constructed out of xTexelRC's, which are |
| 160 | + * the vec4's returned from sampling calls. Sometimes this means mixing channels |
| 161 | + * from adjacent samples, which constructTexel handles. |
| 162 | + */ |
| 163 | +function constructTexel( |
| 164 | + r: number, c: number, stride: number, padLeft: number): string { |
| 165 | + if (stride === 1) { |
| 166 | + if (padLeft % 2 === c % 2) { |
| 167 | + return xTexelName(r, c); |
| 168 | + } |
| 169 | + return `vec4(${xTexelName(r, c - 1)}.zw, ${xTexelName(r, c + 1)}.xy)`; |
| 170 | + } |
| 171 | + |
| 172 | + if (padLeft % 2 === c % 2) { |
| 173 | + return `vec4(${xTexelName(r, c)}.xy, ${xTexelName(r, c + 2)}.xy)`; |
| 174 | + } |
| 175 | + return `vec4(${xTexelName(r, c - 1)}.zw, ${xTexelName(r, c + 1)}.zw)`; |
| 176 | +} |
0 commit comments