Skip to content
fn

upload_cpu_to_gpu

→GpuStorage
GpuStorage upload_cpu_to_gpu(const CpuStorage & cpu, const int & shape)

Upload a CPU buffer to GPU-private memory via mlx::core::copy.

MLX arrays own GPU-private pages that cannot alias a CPU pointer (touching the raw data() from the CPU side raises SIGBUS on M-series hardware). This routine allocates a new MLX array of the requested shape and dispatches a lazy mlx::core::copy from the CPU buffer. The cpu storage is kept alive — via a custom deleter on the source MLX view — until the asynchronous copy completes.

See Also

download_gpu_to_cpu — inverse direction. shared_storage_to_gpu — zero-copy variant for shared buffers.

Parameters

cpuconst CpuStorage&
Source buffer in Accelerate-allocated CPU memory.
shapeconst Shape&
Logical shape of the array. Must match cpu element count.

Returns

GpuStorage

A GPU-resident array participating in MemoryTracker accounting.

Notes

Copy bandwidth on M-series unified memory is ~100 GB/s, so the transfer is rarely the bottleneck — but the call still pays for a fresh GPU allocation. Prefer batching uploads.