TECHNOLOGY

WebGPU buffer mapping: CPU access ends before GPU submission

In this article
THE QUICK TAKE

A mapped buffer is unavailable for GPU use; unmapping ends access through its mapped JavaScript ranges, so keep an independent copy only when the consumer needs one.

A staging view is not permanently shared memory

A browser model loader maps an upload buffer, fills a JavaScript view and retains that view as its convenient input handle. Another part of the runtime submits GPU work while the buffer remains mapped. The problem is an access-phase violation, not necessarily incorrect tensor values. WebGPU mapping does not promise unrestricted simultaneous CPU and GPU ownership. Make the handoff explicit before tuning transfers or describing the implementation as a zero-copy inference path.

Mapping establishes a temporary CPU-access phase

The WebGPU specification describes asynchronous mapping and requires a mapped buffer to be unmapped before GPU work using it is submitted. Await the supported mapping result before reading its range. Distinguish recording commands from submitting them: a runtime can prepare future work without making current CPU access overlap its execution. Track buffer state at the component that performs submission, not only at the helper that originally created the mapped view.

Unmapping ends the mapped view’s validity

Unmapping detaches the ArrayBuffer ranges supplied by getMappedRange. A strong JavaScript reference does not preserve their previous access. If a downstream parser or asynchronous UI consumer needs readback bytes after that handoff, provide an application-owned copy before unmapping, or keep the mapped phase until the consumer finishes under the chosen design. Do not retain a detached view and diagnose its later unusability as a changed model output. Buffer existence and mapped-range validity are different states.

Use staging and readback roles deliberately

The Chrome GPU compute example demonstrates upload handoff and a separate readback buffer. Review actual usage flags and supported combinations rather than assigning every buffer every possible role. A readback range being available to JavaScript does not make the model’s device storage generally shared. Preserve the direction of the transfer, completion observation and ownership transition. Then measure costs if needed; the API example does not establish transfer latency or memory bandwidth for a particular browser model.

Check phase errors with a small known result

A proposed fixture uploads harmless known values, submits only after the supported handoff and maps readback after the copy path. Include an attempted mapped-buffer submission and a consumer retaining a range past unmap. Verify that diagnostics separate validation, mapping failure and stale-view use. Acceptance requires correct values within valid phases and defined cleanup on rejection. No browser commands or transfer benchmark ran here; this fixture describes contract evidence rather than a measured performance improvement.

Keep pointer lifetime and mapping state distinct

Device I/O binding explains another interoperability contract. WebGPU allocation-cache behavior explains reuse costs. Neither removes mapping-state requirements. Record the owner of each buffer, the phase authorizing access and the event that ends view validity. This exposes a concrete class of browser inference defects without claiming that every extra copy is avoidable or every mapped range represents direct physical access to the model’s GPU memory.

Earn rental income from GPUs

Have idle GPUs or looking to fund AI infrastructure? Retrivium finds a rental platform, sets up the server and helps you assess income and costs with a pilot.

Explore GPU hosting
← All articles