-
Notifications
You must be signed in to change notification settings - Fork 2k
[WIP] Provide an efficient way for toPixels #5914
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
Open
qjia7
wants to merge
13
commits into
tensorflow:master
Choose a base branch
from
qjia7:toPixels
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6910fa4
webgpu: Add ToPixels kernel
qjia7 c0cd326
register kernel
qjia7 9e42330
nit
qjia7 a2f0847
Fix shader compilation error
qjia7 ac04afb
Fix memory leak
qjia7 187ca0f
write result to canvas
qjia7 adc12a7
fix test failures
qjia7 0a43f34
Cleanup
qjia7 84c0444
Fix the incorrect result of 3d tensor with 4 channels.
qjia7 ff0abe5
Fix tslint error
qjia7 fbbbcf2
cache the render pipeline
qjia7 dc4da80
Pass a canvas to backend
qjia7 7fc02d0
Use ToCanvas as the new kernel
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
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 Google LLC. All Rights Reserved. | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use backend 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 {KernelConfig, KernelFunc, TensorInfo} from '@tensorflow/tfjs-core'; | ||
import {ToCanvas, ToCanvasInputs, ToCanvasOutput} from '@tensorflow/tfjs-core'; | ||
|
||
import {WebGPUBackend} from '../backend_webgpu'; | ||
|
||
import {ToCanvasProgram} from './to_canvas_webgpu'; | ||
|
||
export const toCanvasConfig: KernelConfig = { | ||
kernelName: ToCanvas, | ||
backendName: 'webgpu', | ||
kernelFunc: toCanvas as {} as KernelFunc, | ||
}; | ||
|
||
export function toCanvas(args: { | ||
inputs: ToCanvasInputs, | ||
backend: WebGPUBackend, | ||
attrs: ToCanvasOutput | ||
}): TensorInfo { | ||
const {inputs, backend, attrs} = args; | ||
const {$img} = inputs; | ||
const {canvas} = attrs; | ||
const [height, width] = $img.shape.slice(0, 2); | ||
|
||
const outShape = [height, width, 4]; | ||
const program = new ToCanvasProgram(outShape, $img.dtype); | ||
canvas.width = width; | ||
canvas.height = height; | ||
const gpuContext = canvas.getContext('webgpu'); | ||
// 'rgba8unorm' is not supported yet as the context format. Otherwise, we | ||
// can save the second render pass. Ideally, just one comput pass, we can | ||
// transfer the input tensor data to webgpu context canvas and then return | ||
// the canvas to user. https://bugs.chromium.org/p/dawn/issues/detail?id=1219 | ||
gpuContext.configure({ | ||
device: backend.device, | ||
format: 'bgra8unorm', | ||
compositingAlphaMode: 'opaque' | ||
}); | ||
|
||
backend.runToCanvasProgram(program, $img, gpuContext); | ||
|
||
const outTensor = backend.makeTensorInfo(program.outputShape, 'int32'); | ||
return outTensor; | ||
} |
This file contains hidden or 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,88 @@ | ||
/** | ||
* @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 {DataType} from '@tensorflow/tfjs-core'; | ||
|
||
import {getMainHeaderAndGlobalIndexString} from '../shader_preprocessor'; | ||
import {computeDispatch, flatDispatchLayout} from '../webgpu_util'; | ||
|
||
import {WebGPUProgram} from './webgpu_program'; | ||
|
||
export class ToCanvasProgram implements WebGPUProgram { | ||
variableNames = ['Image']; | ||
outputShape: number[]; | ||
shaderKey: string; | ||
dispatchLayout: {x: number[]}; | ||
dispatch: [number, number, number]; | ||
workGroupSize: [number, number, number] = [64, 1, 1]; | ||
workPerThread = 4; | ||
type: DataType; | ||
|
||
constructor(outShape: number[], type: DataType) { | ||
this.outputShape = outShape; | ||
this.dispatchLayout = flatDispatchLayout(this.outputShape); | ||
this.dispatch = computeDispatch( | ||
this.dispatchLayout, this.outputShape, this.workGroupSize, | ||
[this.workPerThread, 1, 1]); | ||
this.shaderKey = `toCanvas_${type}`; | ||
this.type = type; | ||
} | ||
|
||
getUserCode(): string { | ||
let calculateResult; | ||
if (this.type === 'float32') { | ||
calculateResult = ` | ||
if (uniforms.numChannels == 1) { | ||
rgba[0] = value; | ||
rgba[1] = value; | ||
rgba[2] = value; | ||
} else { | ||
rgba[d] = value; | ||
}`; | ||
} else { | ||
calculateResult = ` | ||
if (uniforms.numChannels == 1) { | ||
rgba[0] = value / 255.0; | ||
rgba[1] = value / 255.0; | ||
rgba[2] = value / 255.0; | ||
} else { | ||
rgba[d] = value / 255.0; | ||
}`; | ||
} | ||
|
||
const userCode = ` | ||
[[group(0), binding(0)]] var outImage : texture_storage_2d<rgba8unorm, write>; | ||
[[group(0), binding(1)]] var<storage, read> inBuf : Matrix0; | ||
|
||
${getMainHeaderAndGlobalIndexString()} | ||
let flatIndex = index * 4; | ||
if (flatIndex < uniforms.size) { | ||
var rgba = vec4<f32>(0.0, 0.0, 0.0, 1.0); | ||
|
||
for (var d = 0; d < uniforms.numChannels; d = d + 1) { | ||
let value = f32(inBuf.numbers[index * uniforms.numChannels + d]); | ||
${calculateResult} | ||
} | ||
|
||
let coords = getCoordsFromFlatIndex(flatIndex); | ||
textureStore(outImage, vec2<i32>(coords.yx), rgba); | ||
} | ||
} | ||
`; | ||
return userCode; | ||
} | ||
} |
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 this drawing to a different webgpu context? can similar be done with WebGL?
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.
This webgpu context is configured to the current using
GPUDevice
. So I can draw to any webgpu context canvas as long as they are set to the same gpu device. You can see that this webgpu context needs to be configured as below:For webgl, you can't draw to a different webgl context. So we have to adjust the default webgl context's size and draw to it. I think webgl should support adjust the default framebuffer's size. I will check it.