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

to_device_nonblocking(data, device[, dtype])

data on device without a host stall.

Classes

H2DMirror()

What the stager last copied into one device destination.

PinnedStager(dtype[, numel, depth])

A persistent pinned arena for one dtype, used as a ring of segments.

class mstar.utils.h2d.H2DMirror[source]#

Bases: object

What 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.

invalidate()[source]#

The destination was written some other way.

Return type:

None

matches(key, values)[source]#
Parameters:
  • key (tuple)

  • values (ndarray)

Return type:

bool

record(key, values)[source]#
Parameters:
  • key (tuple)

  • values (ndarray)

Return type:

None

class mstar.utils.h2d.PinnedStager(dtype, numel=256, depth=8)[source]#

Bases: object

A 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 torch copy_ 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:
MIN_SEGMENT_BYTES = 32768#
copy_(dst, values, pad_value=None, mirror=None)[source]#

dst[:len(values)] = values in one non-blocking H2D copy.

With pad_value, the rest of dst is set to it by the same copy (the padding is written on the host), so dst is fully overwritten without a device-side fill. dst must be contiguous.

With mirror (dst’s, see H2DMirror), nothing is issued when dst already holds exactly this: the copy that put it there is ahead of anything that reads dst after this call.

Parameters:
Return type:

None

copy_runs_(dsts, values)[source]#

Fill each of dsts from consecutive values, in order: one non-blocking copy per destination, all out of one claim on the arena, so a flush of many runs takes the lock and checks the ring once.

Parameters:
Return type:

None

mstar.utils.h2d.to_device_nonblocking(data, device, dtype=None)[source]#

data on device without 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 through PinnedStager instead. 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 to dtype and staged like a tensor.

Parameters:
Return type:

Tensor