vllm.distributed.weight_transfer.packed_tensor ¶
Packed tensor utilities for efficient weight transfer.
PackedChunk dataclass ¶
Result of packing tensors into a single contiguous uint8 buffer.
Source code in vllm/distributed/weight_transfer/packed_tensor.py
PackedIpcChunk dataclass ¶
Metadata and IPC handle for a single packed chunk.
Source code in vllm/distributed/weight_transfer/packed_tensor.py
pack_tensors ¶
pack_tensors(
iterator: Iterator[tuple[str, Tensor]],
post_iter_func: Callable[[tuple[str, Tensor]], Tensor],
buffer_size_bytes: int,
tensor_list: list[Tensor] | None = None,
current_size: int = 0,
) -> PackedChunk | None
Pack tensors from an iterator into a single contiguous uint8 buffer.
Consumes from the iterator until the accumulated size exceeds buffer_size_bytes or the iterator is exhausted, then returns a PackedChunk. Returns None if no tensors were consumed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterator | Iterator[tuple[str, Tensor]] | Iterator of (name, tensor) pairs | required |
post_iter_func | Callable[[tuple[str, Tensor]], Tensor] | Applied to each item before linearizing to uint8 | required |
buffer_size_bytes | int | Max bytes before flushing | required |
tensor_list | list[Tensor] | None | Pre-existing tensor list to append to (for NCCL multi-buffer reuse). If None, a fresh list is created. | None |
current_size | int | Byte count already accumulated in tensor_list | 0 |
Source code in vllm/distributed/weight_transfer/packed_tensor.py
packed_ipc_consumer ¶
packed_ipc_consumer(
ipc_handle: dict[str, tuple],
names: list[str],
shapes: list[list[int]],
dtype_names: list[str],
tensor_sizes: list[int],
device_index: int,
) -> list[tuple[str, Tensor]]
Unpack a single packed IPC chunk into named tensors.
Reconstructs the packed buffer via rebuild_cuda_tensor, unpacks into individual tensors, and clones each into independent storage before returning.
The clone is intentional: the producer reuses one IPC buffer across chunks, so any tensor view that aliases the buffer would observe the next chunk's bytes as soon as the producer's generator is resumed. Callers that retain references past their own update_weights call (notably vLLM's layerwise reload, which buffers bound_args for replay in _layerwise_process) would otherwise replay against stale data and silently corrupt multi-chunk weight transfers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ipc_handle | dict[str, tuple] | Mapping of GPU UUID to rebuild_cuda_tensor args tuple | required |
names | list[str] | Parameter names in the packed buffer | required |
shapes | list[list[int]] | Parameter shapes | required |
dtype_names | list[str] | Parameter dtype name strings (e.g. "float16") | required |
tensor_sizes | list[int] | Size in bytes of each parameter in the packed buffer | required |
device_index | int | Local CUDA device index | required |
Source code in vllm/distributed/weight_transfer/packed_tensor.py
packed_ipc_producer ¶
packed_ipc_producer(
iterator: Iterator[tuple[str, Tensor]],
gpu_uuid: str,
post_iter_func: Callable[[tuple[str, Tensor]], Tensor],
buffer_size_bytes: int = DEFAULT_PACKED_BUFFER_SIZE_BYTES,
) -> Iterator[PackedIpcChunk]
Pack tensors into a reusable IPC buffer and yield handles.
Allocates a single GPU buffer of buffer_size_bytes and registers it for IPC once via reduce_tensor. Each chunk's packed data is copied into this buffer before yielding, so only one IPC-shared allocation exists for the lifetime of the transfer.
Callers must ensure the consumer has finished reading the buffer (e.g. ray.get returned) before resuming the generator for the next chunk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterator | Iterator[tuple[str, Tensor]] | Iterator of (name, tensor) pairs. | required |
gpu_uuid | str | Physical GPU UUID string for this rank. | required |
post_iter_func | Callable[[tuple[str, Tensor]], Tensor] | Applied to each (name, tensor) before packing. | required |
buffer_size_bytes | int | Exact capacity of the reusable IPC buffer. Every chunk is guaranteed to fit within this size. A | DEFAULT_PACKED_BUFFER_SIZE_BYTES |
Source code in vllm/distributed/weight_transfer/packed_tensor.py
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |
packed_nccl_broadcast_consumer ¶
packed_nccl_broadcast_consumer(
iterator: Iterator[tuple[str, tuple[list[int], dtype]]],
group: Any,
src: int,
post_unpack_func: Callable[
[list[tuple[str, Tensor]]], None
],
buffer_size_bytes: int = DEFAULT_PACKED_BUFFER_SIZE_BYTES,
num_buffers: int = DEFAULT_PACKED_NUM_BUFFERS,
) -> None
Consume packed tensors and unpack them into a list of tensors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterator | Iterator[tuple[str, tuple[list[int], dtype]]] | Iterator of parameter metadata. Returns (name, (shape, dtype)) | required |
group | Any | Process group (PyNcclCommunicator) | required |
src | int | Source rank (0 in current implementation) | required |
post_unpack_func | Callable[[list[tuple[str, Tensor]]], None] | Function to apply to each list of (name, tensor) after unpacking | required |
buffer_size_bytes | int | Size in bytes for each packed tensor buffer. Both producer and consumer must use the same value. | DEFAULT_PACKED_BUFFER_SIZE_BYTES |
num_buffers | int | Number of buffers for double/triple buffering. Both producer and consumer must use the same value. | DEFAULT_PACKED_NUM_BUFFERS |
Source code in vllm/distributed/weight_transfer/packed_tensor.py
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 | |
packed_nccl_broadcast_producer ¶
packed_nccl_broadcast_producer(
iterator: Iterator[tuple[str, Tensor]],
group: Any,
src: int,
post_iter_func: Callable[[tuple[str, Tensor]], Tensor],
buffer_size_bytes: int = DEFAULT_PACKED_BUFFER_SIZE_BYTES,
num_buffers: int = DEFAULT_PACKED_NUM_BUFFERS,
) -> None
Broadcast tensors in a packed manner from trainer to workers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
iterator | Iterator[tuple[str, Tensor]] | Iterator of model parameters. Returns a tuple of (name, tensor) | required |
group | Any | Process group (PyNcclCommunicator) | required |
src | int | Source rank (0 in current implementation) | required |
post_iter_func | Callable[[tuple[str, Tensor]], Tensor] | Function to apply to each (name, tensor) pair before packing, should return a tensor | required |
buffer_size_bytes | int | Size in bytes for each packed tensor buffer. Both producer and consumer must use the same value. | DEFAULT_PACKED_BUFFER_SIZE_BYTES |
num_buffers | int | Number of buffers for double/triple buffering. Both producer and consumer must use the same value. | DEFAULT_PACKED_NUM_BUFFERS |
Source code in vllm/distributed/weight_transfer/packed_tensor.py
unpack_tensor ¶
unpack_tensor(
packed_tensor: Tensor,
names: list[str],
shapes: list[list[int]],
dtypes: list[dtype],
tensor_sizes: list[int],
) -> list[tuple[str, Tensor]]
Unpack a packed uint8 tensor into a list of named tensors.
The returned tensors are views of packed_tensor (the .contiguous() call is a no-op on already-contiguous row-slices). If packed_tensor lives in storage that may be reused — e.g. a reused CUDA IPC buffer — callers must clone the results before the underlying storage is overwritten.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
packed_tensor | Tensor | The packed torch.uint8 tensor to unpack | required |
names | list[str] | List of tensor names | required |
shapes | list[list[int]] | List of tensor shapes | required |
dtypes | list[dtype] | List of tensor dtypes | required |
tensor_sizes | list[int] | List of tensor sizes in bytes | required |