Skip to content

Commit 2cc528b

Browse files
authored
webgpu: update dataToGPU doc (#6810)
1 parent 6391b20 commit 2cc528b

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

docs/OPTIMIZATION_PURE_GPU_PIPELINE.md

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ API that allows data to stay in GPU. Our users are pretty faimilar with
66
`tensor.dataSync()` and `tensor.data()`, however both will download data from GPU to CPU. In the latest release, we have added `tensor.dataToGPU()`,
77
which returns a `GPUData` type.
88

9+
# For webgl backend
910
The example below shows how to get the tensor texture by calling
10-
`datatoGPU`. Also see this [example code](https://github.com/tensorflow/tfjs-examples/tree/master/gpu-pipeline) for details.
11+
`dataToGPU`. Also see this [example code](https://github.com/tensorflow/tfjs-examples/tree/master/gpu-pipeline/webgl) for details.
1112

1213
```javascript
1314
// Getting the texture that holds the data on GPU.
@@ -51,3 +52,47 @@ So if a tensor's shape is `[1, height, width, 4]` or `[height, width, 4]`, the w
5152
downstream processing assumes the texture is an image, it doesn't need to
5253
do any additional coordination conversion. Each texel is mapped to a [CSS pixel](https://developer.mozilla.org/en-US/docs/Glossary/CSS_pixel) in an image. However, if the tensor has other shapes, downstream
5354
processing steps may need to reformat the data onto another texture. The `texShape` field has the `[height, width]` information of the texture, which can be used for transformation.
55+
56+
# For webgpu backend
57+
The example below shows how to get the tensor buffer by calling
58+
`dataToGPU`. Also see this [example code](https://github.com/tensorflow/tfjs-examples/tree/master/gpu-pipeline/webgpu) for details.
59+
60+
```javascript
61+
// Getting the buffer that holds the data on GPU.
62+
// data has below fields:
63+
// {buffer, bufSize, tensorRef}
64+
// buffer: A GPUBuffer.
65+
// bufSize: the size of the buffer.
66+
// tensorRef: the tensor associated with the buffer.
67+
//
68+
// Unlike webgl backend, There is no parameter for dataToGPU API on the webgpu
69+
// backend, and the size of returned buffer is the same as the tensor size.
70+
const data = tensor.dataToGPU();
71+
72+
// Once we have the buffer, we can bind it and pass it to the downstream
73+
// webgpu processing steps to use the buffer.
74+
const uniformBindGroup = device.createBindGroup({
75+
layout: pipeline.getBindGroupLayout(0),
76+
entries: [
77+
{
78+
binding: 1,
79+
resource: {
80+
buffer: data.buffer,
81+
size: data.bufSize,
82+
},
83+
},
84+
...
85+
],
86+
});
87+
88+
// Some webgpu processing steps.
89+
90+
// Defines the mapping between resources of all GPUBindGroup objects
91+
passEncoder.setBindGroup(0, uniformBindGroup);
92+
93+
// Some webgpu processing steps.
94+
95+
// Remember to dispose the buffer after use, otherwise, there will be
96+
// memory leak.
97+
data.tensorRef.dispose();
98+
```

0 commit comments

Comments
 (0)