forked from tensorflow/tfjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdepthwise_conv2d_vec4_webgpu.ts
143 lines (128 loc) · 5.3 KB
/
depthwise_conv2d_vec4_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
/**
* @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, util} from '@tensorflow/tfjs-core';
import {activationFnSnippet, biasActivationSnippet} from './activation_util';
import {getMainHeaderString as main, WebGPUProgram} from './webgpu_program';
import {computeDispatch, flatDispatchLayout} from './webgpu_util';
export class DepthwiseConv2DVec4Program implements WebGPUProgram {
outputShape: number[];
shaderKey: string;
dispatchLayout: {x: number[]};
dispatch: [number, number, number];
variableNames = ['x', 'W'];
uniforms = 'pads : vec2<i32>, inDims : vec2<i32>, virtualWidth : i32,';
workgroupSize: [number, number, number] = [64, 1, 1];
workPerThread = 4;
convInfo: backend_util.Conv2DInfo;
addBias: boolean;
activation: backend_util.Activation;
hasPreluActivation: boolean;
outputComponent = 4;
virtualWidth: number;
constructor(
convInfo: backend_util.Conv2DInfo, addBias = false,
activation: backend_util.Activation = null, hasPreluActivation = false) {
this.outputShape = convInfo.outShape;
this.virtualWidth = Math.ceil(this.outputShape[2] / this.workPerThread) *
this.workPerThread;
const virtualOutputShape = [
this.outputShape[0], this.outputShape[1], this.virtualWidth,
this.outputShape[3]
];
this.dispatchLayout = flatDispatchLayout(virtualOutputShape);
this.dispatch = computeDispatch(
this.dispatchLayout, virtualOutputShape, this.workgroupSize,
[this.outputComponent * this.workPerThread, 1, 1]);
util.assert(
convInfo.dataFormat === 'channelsLast',
() => 'TODO: NCHW is unimplemented');
if (addBias) {
this.variableNames.push('bias');
}
if (hasPreluActivation) {
this.variableNames.push('preluActivationWeights');
}
this.convInfo = convInfo;
this.addBias = addBias;
this.activation = activation;
this.hasPreluActivation = hasPreluActivation;
this.shaderKey =
`depthwiseVec4_${activation}_${this.convInfo.filterHeight}_${
this.convInfo.filterWidth}_${this.convInfo.strideHeight}_${
this.convInfo.strideWidth}_${this.workPerThread}`;
}
getUserCode(): string {
const xNumber = (this.workPerThread - 1) * this.convInfo.strideWidth +
this.convInfo.filterWidth;
const strideHeight = this.convInfo.strideHeight;
const strideWidth = this.convInfo.strideWidth;
const userCode = `
${activationFnSnippet(this.activation, this.hasPreluActivation, true, 4)}
fn readX(batch : i32, row : i32, col : i32, channel : i32) -> vec4<f32> {
var value = vec4<f32>(0.0);
if (col >=0 && col < uniforms.inDims[1]) {
value = getX(batch, row, col, channel);
}
return value;
}
${main('index')} {
let width0 = uniforms.outShape[3] / ${this.outputComponent};
let d1 = (index % width0) * ${this.outputComponent};
var index1 = index / width0;
let width1 = uniforms.virtualWidth / ${this.workPerThread};
let c = (index1 % width1) * ${this.workPerThread};
index1 = index1 / width1;
let r = index1 % uniforms.outShape[1];
let batch = index1 / uniforms.outShape[1];
let xRCCorner = vec2<i32>(r, c) * vec2<i32>(${strideHeight}, ${
strideWidth}) - uniforms.pads;
let xRCorner = xRCCorner.x;
let xCCorner = xRCCorner.y;
var xVals : array<vec4<f32>, ${xNumber}>;
var dotProd : array<vec4<f32>, ${this.workPerThread}>;
for (var i = 0; i < ${this.workPerThread}; i++) {
dotProd[i] = vec4<f32>(0.0);
}
// Use constant instead of uniform can give better performance.
for (var wR = 0; wR < ${this.convInfo.filterHeight}; wR = wR + 1) {
let xR = xRCorner + wR;
if (xR >=0 && xR < uniforms.inDims[0]) {
for (var i = 0; i < ${xNumber}; i++) {
xVals[i] = readX(batch, xR, xCCorner + i, d1);
}
for (var wC = 0; wC < ${this.convInfo.filterWidth}; wC = wC + 1) {
let wValue = getW(wR, wC, d1, 0);
for (var i = 0; i < ${this.workPerThread}; i++) {
dotProd[i] = fma(xVals[i * ${
strideWidth} + wC], wValue, dotProd[i]);
}
}
}
}
for (var i = 0; i < ${this.workPerThread}; i = i + 1) {
let coords = vec4<i32>(batch, r, c + i, d1);
if (coordsInBounds4D(coords, uniforms.outShape)) {
var value = dotProd[i];
${biasActivationSnippet(this.addBias, this.activation)}
setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], value);
}
}
}
`;
return userCode;
}
}