forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconv_backprop_webgpu.ts
428 lines (387 loc) · 15.6 KB
/
conv_backprop_webgpu.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
/**
* @license
* Copyright 2021 Google LLC. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* =============================================================================
*/
import {backend_util} from '@tensorflow/tfjs-core';
import {getMainHeaderString as main, WebGPUProgram} from './webgpu_program';
import {computeDispatch, flatDispatchLayout} from './webgpu_util';
export class Conv2DDerInputProgram implements WebGPUProgram {
variableNames = ['dy', 'W'];
uniforms =
'filterDims : vec2<i32>, pads : vec2<i32>, strides : vec2<i32>, outBackprop : vec4<i32>,';
outputShape: number[];
shaderKey: string;
dispatchLayout: {x: number[], y?: number[], z?: number[]};
dispatch: [number, number, number];
workgroupSize: [number, number, number] = [64, 1, 1];
isChannelsLast: boolean;
size = false;
isVec4 = false;
workPerThread = 1;
outputComponent: number;
constructor(convInfo: backend_util.Conv2DInfo) {
this.outputShape = convInfo.inShape;
this.isChannelsLast = convInfo.dataFormat === 'channelsLast';
this.isVec4 = this.isChannelsLast && convInfo.outChannels % 4 === 0 &&
convInfo.inChannels % 4 === 0;
if (this.isVec4) {
// TODO: Expand to any value.
this.workPerThread = 2;
this.outputComponent = 4;
this.workgroupSize = [4, 4, 4];
this.dispatchLayout = {x: [3], y: [2], z: [0, 1]};
this.dispatch = computeDispatch(
this.dispatchLayout, this.outputShape, this.workgroupSize,
[4, this.workPerThread, 1]);
} else {
this.size = true;
this.workPerThread = 1;
this.workgroupSize = [64, 1, 1];
this.dispatchLayout = flatDispatchLayout(this.outputShape);
this.dispatch = computeDispatch(
this.dispatchLayout, this.outputShape, this.workgroupSize);
}
this.shaderKey = `conv2DDerInput_${this.isChannelsLast}_${this.isVec4}_${
this.workPerThread}`;
}
getUserCode(): string {
const rowDim = this.isChannelsLast ? 1 : 2;
const colDim = this.isChannelsLast ? 2 : 3;
const channelDim = this.isChannelsLast ? 3 : 1;
const vec4Snippet = `
${main()} {
let batch = i32(globalId.z) / uniforms.outShape[1];
let r = i32(globalId.z) % uniforms.outShape[1];
let c = i32(globalId.y) * ${this.workPerThread};
let d1 = i32(globalId.x) * 4;
let dyCorner = vec2<i32>(r, c) - uniforms.pads;
// Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
// ? = to be determined. : = across all values in that axis.
var dotProd: array<vec4<f32>, ${this.workPerThread}>;
for (var i = 0; i < ${this.workPerThread}; i++) {
dotProd[i] = vec4<f32>(0.0);
}
for (var wR = 0; wR < uniforms.filterDims.x; wR = wR + 1) {
let dyR = f32(dyCorner.x + wR) / f32(uniforms.strides.x);
let wRPerm = uniforms.filterDims.x - 1 - wR;
if (dyR < 0.0 || dyR >= f32(uniforms.outBackprop[1]) ||
fract(dyR) > 0.0) {
continue;
}
let idyR = i32(dyR);
for (var wC = 0; wC < uniforms.filterDims.y; wC = wC + 1) {
let dyC = f32(dyCorner.y + wC) / f32(uniforms.strides.y);
let dyC2 = f32(dyCorner.y + 1 + wC) / f32(uniforms.strides.y);
let wCPerm = uniforms.filterDims.y - 1 - wC;
var bDyCVal = true;
var bDyCVal2 = true;
if (dyC < 0.0 || dyC >= f32(uniforms.outBackprop[2]) ||
fract(dyC) > 0.0) {
bDyCVal = false;
}
if (dyC2 < 0.0 || dyC2 >= f32(uniforms.outBackprop[2]) ||
fract(dyC2) > 0.0) {
bDyCVal2 = false;
}
let idyC = i32(dyC);
let idyC2 = i32(dyC2);
if (bDyCVal && bDyCVal2) {
let d2Length = uniforms.outBackprop[3];
for (var d2 = 0; d2 < d2Length; d2 = d2 + 4) {
let wValue0 = getW(wRPerm, wCPerm, d1, d2);
let wValue1 = getW(wRPerm, wCPerm, d1 + 1, d2);
let wValue2 = getW(wRPerm, wCPerm, d1 + 2, d2);
let wValue3 = getW(wRPerm, wCPerm, d1 + 3, d2);
var xValue = getDy(batch, idyR, idyC, d2);
let tmpval = vec4<f32>(dot(xValue, wValue0),
dot(xValue, wValue1),
dot(xValue, wValue2),
dot(xValue, wValue3));
dotProd[0] = dotProd[0] + tmpval;
xValue = getDy(batch, idyR, idyC2, d2);
dotProd[1] = dotProd[1] + vec4<f32>(dot(xValue, wValue0),
dot(xValue, wValue1),
dot(xValue, wValue2),
dot(xValue, wValue3));
}
} else if (bDyCVal) {
let d2Length = uniforms.outBackprop[3];
for (var d2 = 0; d2 < d2Length; d2 = d2 + 4) {
let wValue0 = getW(wRPerm, wCPerm, d1, d2);
let wValue1 = getW(wRPerm, wCPerm, d1 + 1, d2);
let wValue2 = getW(wRPerm, wCPerm, d1 + 2, d2);
let wValue3 = getW(wRPerm, wCPerm, d1 + 3, d2);
var xValue = getDy(batch, idyR, idyC, d2);
let tmpval = vec4<f32>(dot(xValue, wValue0),
dot(xValue, wValue1),
dot(xValue, wValue2),
dot(xValue, wValue3));
dotProd[0] = dotProd[0] + tmpval;
}
} else if (bDyCVal2) {
let d2Length = uniforms.outBackprop[3];
for (var d2 = 0; d2 < d2Length; d2 = d2 + 4) {
let wValue0 = getW(wRPerm, wCPerm, d1, d2);
let wValue1 = getW(wRPerm, wCPerm, d1 + 1, d2);
let wValue2 = getW(wRPerm, wCPerm, d1 + 2, d2);
let wValue3 = getW(wRPerm, wCPerm, d1 + 3, d2);
var xValue = getDy(batch, idyR, idyC2, d2);
let tmpval = vec4<f32>(dot(xValue, wValue0),
dot(xValue, wValue1),
dot(xValue, wValue2),
dot(xValue, wValue3));
dotProd[1] = dotProd[1] + tmpval;
}
}
}
}
for (var i = 0; i < ${this.workPerThread}; i = i + 1) {
let coords = vec4<i32>(batch, r, c + i, d1);
if (coordsInBounds4D(coords, uniforms.outShape)) {
setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], dotProd[i]);
}
}
}
`;
return this.isVec4 ?
`
${vec4Snippet}
` :
`
${main('index')} {
if(index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let batch = coords[0];
let d1 = coords[${channelDim}];
let dyCorner = vec2<i32>(coords[${rowDim}], coords[${
colDim}]) - uniforms.pads;
let dyRCorner = dyCorner.x;
let dyCCorner = dyCorner.y;
// Convolve dy(?, ?, d2) with w(:, :, d1, d2) to compute dx(xR, xC, d1).
// ? = to be determined. : = across all values in that axis.
var dotProd = 0.0;
for (var wR = 0; wR < uniforms.filterDims.x; wR = wR + 1) {
let dyR = (f32(dyRCorner) + f32(wR)) / f32(uniforms.strides.x);
let wRPerm = uniforms.filterDims.x - 1 - wR;
if (dyR < 0.0 || dyR >= f32(uniforms.outBackprop[1]) || fract(dyR) > 0.0 ||
wRPerm < 0) {
continue;
}
let idyR = i32(dyR);
for (var wC = 0; wC < uniforms.filterDims.y; wC = wC + 1) {
let dyC = (f32(dyCCorner) + f32(wC)) / f32(uniforms.strides.y);
let wCPerm = uniforms.filterDims.y - 1 - wC;
if (dyC < 0.0 || dyC >= f32(uniforms.outBackprop[2]) ||
fract(dyC) > 0.0 || wCPerm < 0) {
continue;
}
let idyC = i32(dyC);
for (var d2 = 0; d2 < uniforms.outBackprop[3]; d2 = d2 + 1) {
let xValue = ${
this.isChannelsLast ? 'getDy(batch, idyR, idyC, d2)' :
'getDy(batch, d2, idyR, idyC)'};
let wValue = getW(wRPerm, wCPerm, d1, d2);
dotProd = dotProd + xValue * wValue;
}
}
}
setOutputAtIndex(index, dotProd);
}
}
`;
}
}
export class Conv2DDerFilterProgram implements WebGPUProgram {
variableNames = ['x', 'dy'];
uniforms =
'pads : vec2<i32>, strides : vec2<i32>, batchSize : i32, outHeight : i32, outWidth : i32, inHeight : i32, inWidth : i32,';
outputShape: number[];
shaderKey: string;
dispatchLayout: {x: number[]};
dispatch: [number, number, number];
workgroupSize: [number, number, number] = [64, 1, 1];
isChannelsLast: boolean;
size = true;
constructor(convInfo: backend_util.Conv2DInfo) {
this.outputShape = convInfo.filterShape;
this.dispatchLayout = flatDispatchLayout(this.outputShape);
this.dispatch = computeDispatch(
this.dispatchLayout, this.outputShape, this.workgroupSize);
this.isChannelsLast = convInfo.dataFormat === 'channelsLast';
this.shaderKey = `conv2DDerFilter_${this.isChannelsLast}`;
}
getUserCode(): string {
return `
${main('index')} {
if(index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let wR = coords[0];
let wC = coords[1];
let d1 = coords[2];
let d2 = coords[3];
// Convolve x(?, ?, d1) with dy(:, :, d2) to get dw(wR, wC, d1, d2).
// ? = to be determined. : = across all values in that axis.
var dotProd = 0.0;
for (var b = 0; b < uniforms.batchSize; b = b + 1) {
for (var yR = 0; yR < uniforms.outHeight; yR = yR + 1) {
let xR = wR + yR * uniforms.strides[0] - uniforms.pads[0];
if (xR < 0 || xR >= uniforms.inHeight) {
continue;
}
for (var yC = 0; yC < uniforms.outWidth; yC = yC + 1) {
let xC = wC + yC * uniforms.strides[1] - uniforms.pads[1];
if (xC < 0 || xC >= uniforms.inWidth) {
continue;
}
if (${this.isChannelsLast}) {
let dyValue = getDy(b, yR, yC, d2);
let xValue = getX(b, xR, xC, d1);
dotProd = dotProd + xValue * dyValue;
} else {
let dyValue = getDy(b, d2, yR, yC);
let xValue = getX(b, d1, xR, xC);
dotProd = dotProd + xValue * dyValue;
}
}
}
}
setOutputAtIndex(index, dotProd);
}
}
`;
}
}
export class Conv3DDerFilterProgram implements WebGPUProgram {
variableNames = ['x', 'dy'];
uniforms =
`pads : vec3<i32>, strides : vec3<i32>, batchSize : i32, outDepth : i32,
outHeight : i32, outWidth : i32, inDepth : i32, inHeight : i32, inWidth : i32,`;
outputShape: number[];
shaderKey: string;
dispatchLayout: {x: number[]};
dispatch: [number, number, number];
workgroupSize: [number, number, number] = [64, 1, 1];
size = true;
constructor(convInfo: backend_util.Conv3DInfo) {
this.outputShape = convInfo.filterShape;
this.dispatchLayout = flatDispatchLayout(this.outputShape);
this.dispatch = computeDispatch(
this.dispatchLayout, this.outputShape, this.workgroupSize);
this.shaderKey = `conv3DDerFilter`;
}
getUserCode(): string {
return `
${main('index')} {
if(index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let wF = coords.x;
let wR = coords.y;
let wC = coords.z;
let d1 = coords.w;
let d2 = coords.u;
var dotProd = 0.0;
for (var b = 0; b < uniforms.batchSize; b++) {
for (var yF = 0; yF < uniforms.outDepth; yF++) {
let xF = wF + yF * uniforms.strides[0] - uniforms.pads[0];
if (xF < 0 || xF >= uniforms.inDepth) {
continue;
}
for (var yR = 0; yR < uniforms.outHeight; yR++) {
let xR = wR + yR * uniforms.strides[1] - uniforms.pads[1];
if (xR < 0 || xR >= uniforms.inHeight) {
continue;
}
for (var yC = 0; yC < uniforms.outWidth; yC++) {
let xC = wC + yC * uniforms.strides[2] - uniforms.pads[2];
if (xC < 0 || xC >= uniforms.inWidth) {
continue;
}
let dyValue = getDy(b, yF, yR, yC, d2);
let xValue = getX(b, xF, xR, xC, d1);
dotProd += xValue * dyValue;
}
}
}
}
setOutputAtIndex(index, dotProd);
}
}
`;
}
}
export class Conv3DDerInputProgram implements WebGPUProgram {
variableNames = ['dy', 'W'];
uniforms = `filterDims : vec3<i32>, pads : vec3<i32>, strides : vec3<i32>,
outDepth : i32, outHeight : i32, outWidth : i32, outChannels : i32,`;
outputShape: number[];
shaderKey: string;
dispatchLayout: {x: number[]};
dispatch: [number, number, number];
workgroupSize: [number, number, number] = [64, 1, 1];
size = true;
constructor(convInfo: backend_util.Conv3DInfo) {
this.outputShape = convInfo.inShape;
this.dispatchLayout = flatDispatchLayout(this.outputShape);
this.dispatch = computeDispatch(
this.dispatchLayout, this.outputShape, this.workgroupSize);
this.shaderKey = `conv3DDerInput`;
}
getUserCode(): string {
return `
${main('index')} {
if(index < uniforms.size) {
let coords = getCoordsFromIndex(index);
let batch = coords.x;
let d1 = coords.u;
let dyCorner = vec3<i32>(coords.y, coords.z, coords.w) - uniforms.pads;
let dyFCorner = dyCorner.x;
let dyRCorner = dyCorner.y;
let dyCCorner = dyCorner.z;
var dotProd = 0.0;
for (var wF = 0; wF < uniforms.filterDims[0]; wF++) {
let dyF = f32(dyFCorner + wF) / f32(uniforms.strides[0]);
if (dyF < 0.0 || dyF >= f32(uniforms.outDepth) || fract(dyF) > 0.0) {
continue;
}
let idyF = i32(dyF);
let wFPerm = uniforms.filterDims[0] - 1 - wF;
for (var wR = 0; wR < uniforms.filterDims[1]; wR++) {
let dyR = f32(dyRCorner + wR) / f32(uniforms.strides[1]);
if (dyR < 0.0 || dyR >= f32(uniforms.outHeight) || fract(dyR) > 0.0) {
continue;
}
let idyR = i32(dyR);
let wRPerm = uniforms.filterDims[1] - 1 - wR;
for (var wC = 0; wC < uniforms.filterDims[2]; wC++) {
let dyC = f32(dyCCorner + wC) / f32(uniforms.strides[2]);
if (dyC < 0.0 || dyC >= f32(uniforms.outWidth) || fract(dyC) > 0.0) {
continue;
}
let idyC = i32(dyC);
let wCPerm = uniforms.filterDims[2] - 1 - wC;
for (var d2 = 0; d2 < uniforms.outChannels; d2++) {
let xValue = getDy(batch, idyF, idyR, idyC, d2);
let wValue = getW(wFPerm, wRPerm, wCPerm, d1, d2);
dotProd += xValue * wValue;
}
}
}
}
setOutputAtIndex(index, dotProd);
}
}
`;
}
}