-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
webgpu: Support depthwise conv2d with nchw format #6084
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
f11fd95
webgpu: Optimize depthwise conv2d
qjia7 2bf705e
Fix bug
qjia7 4283e36
Rename depthwise_conv2d_nchw_shared_webgpu
qjia7 4eb876a
Update year
qjia7 c3846c1
Use nchw conditionally
qjia7 d5ad65d
Fix tslint errors
qjia7 e660a40
fix rebase error and use more tight shared size
qjia7 e20611d
support common nchw depthwise conv2d
qjia7 89cb8cd
Add test cases
qjia7 7b2de64
Fix non-uniform control warning
qjia7 1b6828b
Update year
qjia7 73b8ace
make shader more flexible
qjia7 52a444b
nits
qjia7 ace2c45
Merge branch 'master' into depthwise_conv2d_nchw
Linchenn 840056c
Use DepthwiseConv2DNCHWSharedProgram only for NCHW
qjia7 0da3a2c
Merge branch 'master' into depthwise_conv2d_nchw
qjia7 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
177 changes: 177 additions & 0 deletions
177
tfjs-backend-webgpu/src/depthwise_conv2d_nchw_shared_webgpu.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,177 @@ | ||
/** | ||
* @license | ||
* Copyright 2022 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 {mapActivationToShaderProgram} from './activation_util'; | ||
import {getWorkGroupSizeString, WebGPUProgram} from './webgpu_program'; | ||
import {computeDispatch} from './webgpu_util'; | ||
|
||
export class DepthwiseConv2DNCHWSharedProgram implements WebGPUProgram { | ||
outputShape: number[]; | ||
shaderKey: string; | ||
dispatchLayout: {x: number[], y: number[], z: number[]}; | ||
dispatch: [number, number, number]; | ||
variableNames = ['x', 'W']; | ||
uniforms = `pad : vec2<i32>, stride : vec2<i32>, dilation : vec2<i32>, | ||
inDims : vec2<i32>, filterHeight : i32, filterWidth : i32, | ||
channelMul : i32,`; | ||
workGroupSize: [number, number, number] = [16, 16, 1]; | ||
addBias: boolean; | ||
activation: backend_util.Activation; | ||
hasPreluActivation: boolean; | ||
filterHeight: number; | ||
filterWidth: number; | ||
|
||
constructor( | ||
outputShape: number[], filterHeight: number, filterWidth: number, | ||
addBias = false, activation: backend_util.Activation = null, | ||
hasPreluActivation = false) { | ||
this.outputShape = outputShape; | ||
this.dispatchLayout = {x: [3], y: [2], z: [0, 1]}; | ||
this.dispatch = computeDispatch( | ||
this.dispatchLayout, this.outputShape, this.workGroupSize); | ||
|
||
if (addBias) { | ||
this.variableNames.push('bias'); | ||
} | ||
if (hasPreluActivation) { | ||
this.variableNames.push('preluActivationWeights'); | ||
} | ||
|
||
this.addBias = addBias; | ||
this.activation = activation; | ||
this.hasPreluActivation = hasPreluActivation; | ||
this.filterHeight = filterHeight; | ||
this.filterWidth = filterWidth; | ||
this.shaderKey = `depthwiseNCHW_${this.activation}_${this.filterHeight}_${ | ||
this.filterWidth}`; | ||
} | ||
|
||
getUserCode(): string { | ||
let activationSnippet = '', applyActivationSnippet = ''; | ||
if (this.activation) { | ||
const activationOp = mapActivationToShaderProgram(this.activation, false); | ||
if (this.hasPreluActivation) { | ||
activationSnippet = | ||
`fn activation(a : f32, outCoord : vec4<i32>) -> f32 { | ||
let b = getPreluActivationWeightsByOutputCoords(outCoord); | ||
${activationOp} | ||
}`; | ||
} else { | ||
activationSnippet = ` | ||
fn activation(a : f32, outCoord : vec4<i32>) -> f32 { | ||
${activationOp} | ||
} | ||
`; | ||
} | ||
|
||
applyActivationSnippet = `dotProd = activation(dotProd, coords);`; | ||
} | ||
|
||
const addBiasSnippet = this.addBias ? | ||
'dotProd = dotProd + getBiasByOutputCoords(coords);' : | ||
''; | ||
|
||
const filterSize = this.filterWidth * this.filterHeight; | ||
const workGroupSize = | ||
this.workGroupSize[0] * this.workGroupSize[1] * this.workGroupSize[2]; | ||
const tileAHeight = this.workGroupSize[1] + this.filterHeight - 1; | ||
const tileAWidth = this.workGroupSize[0] + this.filterWidth - 1; | ||
|
||
const userCode = ` | ||
${activationSnippet} | ||
|
||
var<workgroup> mm_Asub : array<array<f32, ${tileAWidth}>, ${tileAHeight}>; | ||
var<workgroup> mm_Bsub : array<array<f32, ${this.filterWidth}>, ${ | ||
this.filterHeight}>; | ||
fn readX(batch : i32, channel : i32, row : i32, col : i32) -> f32 { | ||
var value = 0.0; | ||
if (row >=0 && row < uniforms.inDims[0] && col >=0 && col < uniforms.inDims[1]) | ||
{ | ||
value = getX(batch, channel, row, col); | ||
} | ||
return value; | ||
} | ||
|
||
${getWorkGroupSizeString()} | ||
fn main(@builtin(local_invocation_id) LocalId : vec3<u32>, | ||
@builtin(global_invocation_id) GlobalId : vec3<u32>, | ||
@builtin(local_invocation_index) LocalIndex: u32, | ||
@builtin(num_workgroups) NumWorkgroups: vec3<u32>) { | ||
localId = LocalId; | ||
globalId = GlobalId; | ||
let localIndex = i32(LocalIndex); | ||
numWorkgroups = NumWorkgroups; | ||
let coords = getOutputCoords(); | ||
let batch = coords[0]; | ||
let xRCCorner = vec2<i32>(coords.zw) * uniforms.stride - uniforms.pad; | ||
let d2 = coords[1]; | ||
let d1 = d2 / uniforms.channelMul; | ||
let q = d2 - d1 * uniforms.channelMul; | ||
|
||
let inputRowStart = xRCCorner.x; | ||
let inputColStart = xRCCorner.y; | ||
|
||
let localRow = i32(localId.y); | ||
let localCol = i32(localId.x); | ||
|
||
// Load one tile of X into local memory. | ||
for (var inputRow = localRow; inputRow < ${ | ||
tileAHeight}; inputRow = inputRow + ${this.workGroupSize[1]}) { | ||
for (var inputCol = localCol; inputCol < ${ | ||
tileAWidth}; inputCol = inputCol + ${this.workGroupSize[0]}) { | ||
let rowOffset = inputRow - localRow; | ||
let colOffset = inputCol - localCol; | ||
mm_Asub[inputRow][inputCol] = readX(batch, d1, inputRowStart + rowOffset, inputColStart + colOffset); | ||
} | ||
} | ||
|
||
// Load one tile of W into local memory. | ||
var wIndex = localIndex; | ||
${ | ||
filterSize < workGroupSize ? | ||
`if (wIndex < ${filterSize})` : | ||
`for(; wIndex < ${filterSize}; wIndex = wIndex + ${workGroupSize})`} | ||
|
||
{ | ||
let wRow = wIndex / ${this.filterWidth}; | ||
let wCol = wIndex % ${this.filterWidth}; | ||
mm_Bsub[wRow][wCol] = getW(wRow, wCol, d1, q); | ||
} | ||
|
||
workgroupBarrier(); | ||
|
||
var dotProd = 0.0; | ||
for (var wR = 0; wR < uniforms.filterHeight; wR = wR + 1) { | ||
for (var wC = 0; wC < uniforms.filterWidth; wC = wC + 1) { | ||
let xVal = mm_Asub[localRow + wR][localCol + wC]; | ||
let wVal = mm_Bsub[wR][wC]; | ||
dotProd = fma(xVal, wVal, dotProd); | ||
} | ||
} | ||
|
||
${addBiasSnippet} | ||
${applyActivationSnippet} | ||
if (coordsInBounds4D(coords, uniforms.outShape)) { | ||
setOutputAtCoords(coords[0], coords[1], coords[2], coords[3], dotProd); | ||
} | ||
} | ||
`; | ||
return userCode; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is
if
statement needed here? Is it already a subset offor
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may be slightly good for perf to use
if
instead offor
since it doesn't need to callwIndex = wIndex + ${workGroupSize})
and anotherwIndex < ${filterSize}
checking if filterSize is smaller than workGroupSize.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, thank you.