Skip to content

Commit 1758c2f

Browse files
committed
Use GPUExternalBuffer with STORAGE usage flag
1 parent 5621975 commit 1758c2f

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

mltensor-explainer.md

+6-5
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ const applyEffectToFrame = async () => {
223223
gpuDevice.queue.submit([tensorizationCommandEncoder.finish()]);
224224

225225
// Return the buffer to WebNN.
226-
tensorizedGpuBuffer.destroy();
226+
tensorizedGpuBuffer.release();
227227

228228
// Perform some inference described by `graph` on the frame
229229
// (e.g. selfie segmentation)
@@ -245,7 +245,7 @@ const applyEffectToFrame = async () => {
245245
gpuDevice.queue.submit([texturizeAndRenderCommandEncoder.finish()]);
246246

247247
// Return the buffer to WebNN for the next frame.
248-
tensorizedGpuBufferAfterInference.destroy();
248+
tensorizedGpuBufferAfterInference.release();
249249

250250
// Call this method for each frame.
251251
video.requestVideoFrameCallback(applyEffectToFrame);
@@ -272,7 +272,7 @@ The `MLTensorUsage.READ` and `MLTensorUsage.WRITE` flags likewise are hints to t
272272

273273
Any `MLTensor` created with the `MLTensorUsage.WEBGPU_INTEROP` flag may be imported into any `GPUDevice`. In the best case, this requires no data copies. If the underlying buffer backing the `MLTensor` is not accessible to the `GPUDevice`, this will require copying the contents of the `MLTensor` to a new buffer, then copying the contents of this buffer back to the `MLTensor` once WebGPU releases its handle to the buffer.
274274

275-
While an `MLTensor` is rented to a `GPUDevice`, the `GPUDevice` has exclusive, read/write access to the imported buffer. All WebNN work depending - directly or indirectly - on the imported `MLTensor` is blocked until the `GPUDevice` returns the tensor.
275+
While an `MLTensor` is rented to a `GPUDevice`, the `GPUDevice` has exclusive, read/write access to the imported buffer, which is created as a `GPUExternalBuffer` with `GPUBufferUsageFlags.STORAGE`. All WebNN work depending - directly or indirectly - on the imported `MLTensor` is blocked until the `GPUDevice` returns the tensor.
276276

277277
Importing and returning the `MLTensor` are each points of synchronization between the respective WebNN and WebGPU [timelines](https://www.w3.org/TR/webgpu/#programming-model-timelines). The `importExternalBuffer()` method is asynchronous to allow the user agent to await completion of WebNN operations before posting WebGPU commands with the imported buffer. This is to avoid making WebGPU workloads explicitly dependent on WebNN operations, which is may not be possible on platforms which [don't support enqueuing GPU work that waits on a fence to be later signaled by the CPU](https://github.com/webmachinelearning/webnn/pull/754#discussion_r1740841364) and/or don't express ML compute in terms of GPU commands.
278278

@@ -288,7 +288,6 @@ It's possible `compute()` may have a performance advantage on some platforms for
288288
- Does the user agent have enough information to appropriately allocate an `MLTensor` if an `MLDeviceType` is not used for creating an `MLContext`? See [#350](https://github.com/webmachinelearning/webnn/issues/350) and [#749](https://github.com/webmachinelearning/webnn/issues/749)
289289
- Should the `dispatch()` method be a part of the `MLGraph` interface rather than `MLContext`? Should `readTensor()` and `writeTensor()` exist on an `MLTensor`? See [#697](https://github.com/webmachinelearning/webnn/issues/697).
290290
- If an `MLContext` is not created from a `GPUDevice`, does there need to be some mechanism - above and beyond the `MLTensorUsage.WEBGPU_INTEROP` flag - for identifying the specific `GPUDevice` with which interop is desired?
291-
- What are the usage flags of a `GPUBuffer` created from an `MLTensor`?
292291
- Is a sync variant of the `importExternalBuffer()` method feasible on platforms where the WebNN timeline _is_ the WebGPU timeline? (i.e. ML compute is expressed in terms of GPU commands on the same `GPUDevice`)
293292

294293
## Considered Alternatives
@@ -408,7 +407,9 @@ partial interface MLContext {
408407

409408
// For WebGPU Interop
410409

411-
interface GPUExternalBuffer {};
410+
interface GPUExternalBuffer {
411+
undefined release();
412+
};
412413
GPUExternalBuffer includes GPUObjectBase;
413414

414415
dictionary GPUExternalBufferDescriptor

0 commit comments

Comments
 (0)