vllm.distributed.weight_transfer.packed_tensor
¶
Packed tensor utilities for efficient weight transfer.
Classes:
-
Chunk– -
PackedIpcChunk–One chunk of a packed IPC transfer, naming the tensors that start in it.
-
PackedIpcConsumer–Receives packed IPC transfers, a chunk per
receivecall. -
Piece–Bytes
[start, end)of the tensor atpositionin the sent
Functions:
-
chunk_capacity–The largest chunk, in bytes, of a
buffer_sizepacked buffer. -
chunks–Lazily cut the
ALIGN-aligned byte stream ofspecsinto chunks of -
dtype_name–The wire name of
dtype, e.g. "float16" (getattr(torch, name)). -
grow_buffer–bufif it holdsnbytes, else a new buffer with its firstkeepbytes: -
local_rebuild_args–rebuild_cuda_tensorargs of this GPU's entry, ondevice_index. -
packed_ipc_producer–Pack tensors into one IPC buffer, refilled and yielded per chunk.
-
packed_nccl_broadcast_consumer–Consume packed tensors and unpack them into a list of tensors.
-
packed_nccl_broadcast_producer–Broadcast tensors in a packed manner from trainer to workers. A tensor
Chunk
¶
Bases: NamedTuple
Attributes:
PackedIpcChunk
dataclass
¶
One chunk of a packed IPC transfer, naming the tensors that start in it.
Only the first chunk carries chunk_capacity and the buffer's ipc_handle
(none if the transfer has no bytes); only the last one has last set.
PackedIpcConsumer
¶
Receives packed IPC transfers, a chunk per receive call.
The first chunk of a transfer maps the producer's buffer, if it has bytes;
close() releases it and raises if a transfer did not reach its last
chunk.
Methods:
-
close–Release the buffer; raises if a transfer did not reach its last
-
receive–Unpack the next chunk; returns the tensors it completes, copied out
close()
¶
Release the buffer; raises if a transfer did not reach its last chunk.
receive(ipc_handle, names, shapes, dtype_names, chunk_capacity, last)
¶
Unpack the next chunk; returns the tensors it completes, copied out on the current stream. The caller must synchronize it before the producer refills the buffer.
Parameters:
-
(ipc_handle¶dict[str, tuple] | None) –GPU UUID to
rebuild_cuda_tensorargs; set on the first chunk of a transfer with bytes only. -
(names¶list[str]) –Names of the tensors that start in this chunk
-
(shapes¶list[list[int]]) –Their shapes
-
(dtype_names¶list[str]) –Their dtype names (e.g. "float16")
-
(chunk_capacity¶int | None) –The chunk size in bytes; set on the first chunk.
-
(last¶bool) –Whether this chunk ends the transfer.
Piece
¶
Bases: NamedTuple
Bytes [start, end) of the tensor at position in the sent
sequence, at offset in its chunk.
_Unpacker
¶
Rebuilds the tensors of a packed transfer, one chunk at a time.
Split tensors are copied into their own storage; whole ones too unless
views, which returns them as views of the chunk buffer.
Methods:
-
feed–Append the tensors that start in the next chunks.
-
unpack–Unpack a chunk of
layoutfrombuf; returns the tensors it ends.
Attributes:
-
incomplete(bool) –A split tensor still misses bytes.
_bytes_end(pieces)
¶
Where the bytes of pieces end in their chunk; 0 if they have none.
_pack_copies(buf, pieces, tensors)
¶
Destinations and sources of one _foreach_copy_ packing pieces into
buf, as bytes: CUDA copies lack some dtypes (float8_e8m0fnu). Each tensor
is dropped from tensors at its last piece.
_record_sources(srcs, device, stream)
¶
Keep GPU srcs alive until their copies end: on stream on device, on
the source device's current stream (where copy_ runs) on another GPU.
chunk_capacity(buffer_size)
¶
The largest chunk, in bytes, of a buffer_size packed buffer.
chunks(specs, buffer_size, before_pull=None)
¶
Lazily cut the ALIGN-aligned byte stream of specs into chunks of
chunk_capacity(buffer_size) bytes, splitting tensors that cross a cut.
A full chunk is yielded before the next spec is pulled, and before_pull
gets each piece once, before the next pull and before its chunk is yielded.
dtype_name(dtype)
¶
The wire name of dtype, e.g. "float16" (getattr(torch, name)).
grow_buffer(buf, nbytes, capacity, device, keep=0)
¶
buf if it holds nbytes, else a new buffer with its first keep bytes:
buf's size (at first, MIN_PACKED_BUFFER_BYTES) doubled to hold nbytes,
at most capacity.
local_rebuild_args(ipc_handle, gpu_uuid, device_index)
¶
rebuild_cuda_tensor args of this GPU's entry, on device_index.
packed_ipc_producer(iterator, gpu_uuid, device_index, post_iter_func, buffer_size_bytes=DEFAULT_PACKED_BUFFER_SIZE_BYTES)
¶
Pack tensors into one IPC buffer, refilled and yielded per chunk.
The consumer must have read a chunk before the generator is resumed. A tensor is copied before the next is pulled, so the iterator may reuse one tensor for consecutive parameters.
Parameters:
-
(iterator¶Iterator[tuple[str, Tensor]]) –Iterator of (name, tensor) pairs.
-
(gpu_uuid¶str) –Physical GPU UUID string of
device_index. -
(device_index¶int) –The CUDA device the IPC buffer is allocated on.
-
(post_iter_func¶Callable[[tuple[str, Tensor]], Tensor]) –Applied to each (name, tensor) before packing.
-
(buffer_size_bytes¶int, default:DEFAULT_PACKED_BUFFER_SIZE_BYTES) –Chunk size; the IPC buffer grows up to it while the first chunk is packed.
packed_nccl_broadcast_consumer(iterator, group, src, post_unpack_func, buffer_size_bytes=DEFAULT_PACKED_BUFFER_SIZE_BYTES, num_buffers=DEFAULT_PACKED_NUM_BUFFERS, device='cuda')
¶
Consume packed tensors and unpack them into a list of tensors.
Parameters:
-
(iterator¶Iterator[tuple[str, tuple[list[int], dtype]]]) –Iterator of parameter metadata. Returns (name, (shape, dtype))
-
(group¶Any) –Process group (PyNcclCommunicator)
-
(src¶int) –Source rank (0 in current implementation)
-
(post_unpack_func¶Callable[[list[tuple[str, Tensor]]], None]) –Function to apply to each list of (name, tensor) after unpacking
-
(buffer_size_bytes¶int, default:DEFAULT_PACKED_BUFFER_SIZE_BYTES) –Chunk size in bytes, as on the producer.
-
(num_buffers¶int, default:DEFAULT_PACKED_NUM_BUFFERS) –Number of chunks in flight; may differ from the producer's.
-
(device¶device | str, default:'cuda') –Device of the NCCL communicator (the worker's device).
packed_nccl_broadcast_producer(iterator, group, src, post_iter_func, buffer_size_bytes=DEFAULT_PACKED_BUFFER_SIZE_BYTES, num_buffers=DEFAULT_PACKED_NUM_BUFFERS)
¶
Broadcast tensors in a packed manner from trainer to workers. A tensor is copied before the next is pulled, so the iterator may reuse one tensor for consecutive parameters.
Parameters:
-
(iterator¶Iterator[tuple[str, Tensor]]) –Iterator of model parameters. Returns a tuple of (name, tensor)
-
(group¶Any) –Process group (PyNcclCommunicator)
-
(src¶int) –Source rank (0 in current implementation)
-
(post_iter_func¶Callable[[tuple[str, Tensor]], Tensor]) –Function to apply to each (name, tensor) pair before packing, should return a tensor
-
(buffer_size_bytes¶int, default:DEFAULT_PACKED_BUFFER_SIZE_BYTES) –Chunk size in bytes; the consumer must use the same.
-
(num_buffers¶int, default:DEFAULT_PACKED_NUM_BUFFERS) –Number of chunks in flight; may differ from the consumer's.