Skip to content

vllm.lora.slot_copy

Batched strided copies into resident LoRA buffers.

Functions:

copy_lora_slot_weights(destinations, sources)

Copy same-shaped GPU tensor pairs with one strided Triton launch.

Supports one to four dimensions and float16, bfloat16 and float32, including mixed dtypes. Only selected destination views are written; padding is never cleared. CPU tensors and overlapping reads/writes are rejected before any writes. Call outside CUDA graph capture.

Source code in vllm/lora/slot_copy.py
def copy_lora_slot_weights(
    destinations: Sequence[torch.Tensor | _TensorMetadata],
    sources: Sequence[torch.Tensor],
) -> None:
    """Copy same-shaped GPU tensor pairs with one strided Triton launch.

    Supports one to four dimensions and float16, bfloat16 and float32, including
    mixed dtypes. Only selected destination views are written; padding is never
    cleared. CPU tensors and overlapping reads/writes are rejected before any
    writes. Call outside CUDA graph capture.
    """
    device, rows, blocks, copies = _prepare_copy(destinations, sources)
    if not blocks:
        return
    assert device is not None and device.index is not None
    with torch.accelerator.device_index(device.index):
        stream = torch.cuda.current_stream(device)
        metadata = _upload_metadata(rows, device)
        stream_id = stream.cuda_stream
        for destination in destinations:
            if isinstance(destination, _TensorMetadata):
                # The allocator remembers a recorded stream until the allocation
                # is freed. This target view lives for the active slot lifetime.
                if destination.last_write_stream != stream_id:
                    destination.tensor.record_stream(stream)
                    destination.last_write_stream = stream_id
            else:
                destination.record_stream(stream)
        for source in sources:
            source.record_stream(stream)
        _copy_slots[(blocks,)](
            metadata, copies, BLOCK_SIZE=_BLOCK_SIZE, MAX_DIMS=_MAX_DIMS
        )