Skip to content

Invoke tfjs operations functions with GPU resource #6232

@BruceDai

Description

@BruceDai

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions