Skip to content
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

Invoke tfjs operations functions with GPU resource #6232

Closed
BruceDai opened this issue Mar 15, 2022 · 2 comments
Closed

Invoke tfjs operations functions with GPU resource #6232

BruceDai opened this issue Mar 15, 2022 · 2 comments
Assignees
Labels
type:feature New feature or request

Comments

@BruceDai
Copy link
Contributor

Hi. I have this scenario of invoking tfjs operations functions for processing GPU resource, for example, invoking tf.add(a, b) with gpuBufferA and gpuBufferB by WebGPU backend.

Current my workaround likes:

// Get TypedArray f32ArrayA
const readbackBufferA = gpuDevice.createBuffer({size: sizeInBytes, usage: GPUBufferUsage.MAP_READ | GPUBufferUsage.COPY_DST});
const readbackEncoder = gpuDevice.createCommandEncoder();
readbackEncoder.copyBufferToBuffer(gpuBufferA, 0, readbackBufferA, 0, sizeInBytes);
device.queue.submit([readbackEncoder.finish()]);
readbackBufferA.mapAsync(GPUMapMode.READ);
const f32ArrayA = new Float32Array(readbackBufferA.getMappedRange());

// Same to get f32ArrayB
...
const f32ArrayB = new Float32Array(readbackBufferB.getMappedRange());

// Invoke tf.add()
const result = tf.add(f32ArrayA, f32ArrayB);

I don't think it's good on memory copy / performance.

Is there any efficient solution for this scenario? Any suggestion? Thanks.

@huningxin
Copy link

This would readback the data from GPU to CPU. If tf.js uses webgpu backend, it would ideal to let the data stay on GPU.

@pyu10055 @qjia7 , is there a method to create a tf.Tensor from an existing WebGPU buffer? tf.tensor only supports CPU buffer (TypedArrayBuffer).

@qjia7
Copy link
Contributor

qjia7 commented Mar 16, 2022

@huningxin Currently, there is no such kind of interface to directly import an external GPU resource to TFJS. I just offline talked with @lina128. We think it's helpful to have a such kind of API, like tf.tensor (GPUData) to take GPU resource as the input to create a tensor. Thanks.

axinging added a commit to axinging/tfjs that referenced this issue Jul 28, 2022
This only works on WebGPU.

TODO: refines API docs; Add more dtype.

BUG: tensorflow#6232
axinging added a commit to axinging/tfjs that referenced this issue Nov 11, 2022
axinging added a commit to axinging/tfjs that referenced this issue Nov 21, 2022
axinging added a commit to axinging/tfjs that referenced this issue Nov 22, 2022
axinging added a commit to axinging/tfjs that referenced this issue Nov 22, 2022
@qjia7 qjia7 closed this as completed in 775619c Nov 23, 2022
Linchenn pushed a commit to Linchenn/tfjs that referenced this issue Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants