mstar.utils.h2d#
Copy-only host-to-device staging, for work that runs beside a live CUDA graph.
Pre-planning builds step N+1’s small tensors on the plan stream while step N’s
graph replays. Any SM work it issues (a kernel, a fill_, a D2D copy) slows
the graph; H2D copies and event waits do not. So host values go to the device
in ONE copy from pinned memory (PinnedStager), padding written on the host,
never via a device-side staging tensor. An H2DMirror skips re-copying
values a destination already holds, as in a steady decode batch.
Functions
|
|
Classes
What the stager last copied into one device destination. |
|
|
A persistent pinned arena for one dtype, used as a ring of segments. |
- class mstar.utils.h2d.H2DMirror[source]#
Bases:
objectWhat the stager last copied into one device destination.
Lets
PinnedStager.copy_skip a copy whose values the destination already holds. Only sound for a destination nothing but that stager writes, and that lives as long as its mirror – hence an object the owner keeps with it, not a cache keyed by address (the allocator reuses addresses). A skip is recorded when the copy is issued, possibly on another stream; the engine orders each step after every earlier pre-plan’s copies (Engine._order_after_pre_plans), which makes it sound across streams.
- class mstar.utils.h2d.PinnedStager(dtype, numel=256, depth=8)[source]#
Bases:
objectA persistent pinned arena for one dtype, used as a ring of segments.
copy_writes values at the arena’s cursor and issues one non-blocking H2D copy from there on the current stream. Events are per segment, not per copy: when the cursor leaves a segment it records one on each stream that copied out of it, and a segment is not rewritten until those have completed, so a copy still in flight never sees its source change. Most copies therefore make no event call at all. Thread-safe: the plan thread and the GPU thread may share one.On CUDA the copy is a direct
cudaMemcpyAsync: the torchcopy_of two slices costs several times the copy itself in dispatch, and records a host-allocator event of its own. Elsewhere it is the torch copy.- Parameters:
dtype (torch.dtype)
numel (int)
depth (int)
- MIN_SEGMENT_BYTES = 32768#
- copy_(dst, values, pad_value=None, mirror=None)[source]#
dst[:len(values)] = valuesin one non-blocking H2D copy.With
pad_value, the rest ofdstis set to it by the same copy (the padding is written on the host), sodstis fully overwritten without a device-side fill.dstmust be contiguous.With
mirror(dst’s, seeH2DMirror), nothing is issued whendstalready holds exactly this: the copy that put it there is ahead of anything that readsdstafter this call.
- mstar.utils.h2d.to_device_nonblocking(data, device, dtype=None)[source]#
dataondevicewithout a host stall.A copy from pageable memory blocks the calling thread until it retires, so on the GPU thread it waits out the step still running there. Host values go through a fresh pinned buffer instead, and one non-blocking copy from it is queued on the current stream. torch’s caching host allocator keeps that buffer until the copy has retired, so the caller may reuse its source at once.
A sequence is built straight into pinned memory as
torch.tensor(data, dtype=dtype)would build it. A pageable CPU tensor is written into the pinned buffer with one memcpy on the calling thread (copy_would split it over the intra-op pool, where a descheduled thread holds up the GPU thread), so the device tensor is bit-identical to a pageable copy. An already-pinned contiguous tensor is copied from in place, so don’t write to it until the copy lands; stage a reused buffer throughPinnedStagerinstead. The result is valid on the stream the copy was issued on; another stream has to wait on it, as for any async copy. Where the device takes no pinned memory (a CPU device), the result is a plain.to(device).An ndarray is wrapped (
torch.from_numpy, so on a CPU device the result may share its memory), cast todtypeand staged like a tensor.