Skip to content

vllm.distributed.ec_transfer.ec_connector.tiering.io

Raw-tensor filesystem I/O for encoder-cache entries.

Functions:

  • load_entry –

    Read a complete entry into writable CPU destination buffers.

  • remove_entry –

    Remove an entry file, treating an already absent file as success.

  • store_entry –

    Write a complete entry to a temporary file, then publish atomically.

_consume(buffers, completed_size_bytes)

Advance pending views past bytes completed by the last I/O call.

Source code in vllm/distributed/ec_transfer/ec_connector/tiering/io.py
def _consume(buffers: deque[memoryview], completed_size_bytes: int) -> None:
    """Advance pending views past bytes completed by the last I/O call."""
    remaining = completed_size_bytes
    while remaining:
        buffer = buffers[0]
        if remaining < len(buffer):
            buffers[0] = buffer[remaining:]
            return
        remaining -= len(buffer)
        buffers.popleft()

_get_iov_max()

Return the system limit for buffers in one readv/writev call.

Source code in vllm/distributed/ec_transfer/ec_connector/tiering/io.py
def _get_iov_max() -> int:
    """Return the system limit for buffers in one readv/writev call."""
    try:
        return max(1, int(os.sysconf("SC_IOV_MAX")))
    except (AttributeError, OSError, ValueError):
        return _DEFAULT_IOV_MAX

_prepare_buffers(buffers, file_size_bytes, *, writable)

Validate buffers and return zero-copy views measured in bytes.

Source code in vllm/distributed/ec_transfer/ec_connector/tiering/io.py
def _prepare_buffers(
    buffers: Sequence[memoryview],
    file_size_bytes: int,
    *,
    writable: bool,
) -> deque[memoryview]:
    """Validate buffers and return zero-copy views measured in bytes."""
    if file_size_bytes <= 0:
        raise ValueError("file_size_bytes must be positive")

    prepared: deque[memoryview] = deque()
    total_size = 0
    for buffer in buffers:
        try:
            byte_view = buffer.cast("B")
        except TypeError as exc:
            raise ValueError("buffers must be contiguous") from exc
        if writable and byte_view.readonly:
            raise ValueError("destination buffers must be writable")
        if not byte_view:
            continue

        prepared.append(byte_view)
        total_size += len(byte_view)

    if total_size != file_size_bytes:
        raise ValueError(
            f"buffer size is {total_size} bytes, expected exactly {file_size_bytes}"
        )
    return prepared

_transfer_all(fd, buffers, transfer, operation)

Run readv/writev until all buffers are transferred.

Source code in vllm/distributed/ec_transfer/ec_connector/tiering/io.py
def _transfer_all(
    fd: int,
    buffers: deque[memoryview],
    transfer: Callable[[int, Sequence[memoryview]], int],
    operation: str,
) -> None:
    """Run readv/writev until all buffers are transferred."""
    while buffers:
        batch = list(islice(buffers, 0, _IOV_MAX))
        transferred = transfer(fd, batch)
        if transferred <= 0:
            raise OSError(f"short {operation}: no forward progress")
        _consume(buffers, transferred)

load_entry(entry_path, destinations, file_size_bytes)

Read a complete entry into writable CPU destination buffers.

Source code in vllm/distributed/ec_transfer/ec_connector/tiering/io.py
def load_entry(
    entry_path: Path,
    destinations: Sequence[memoryview],
    file_size_bytes: int,
) -> None:
    """Read a complete entry into writable CPU destination buffers."""
    buffers = _prepare_buffers(destinations, file_size_bytes, writable=True)
    fd = os.open(entry_path, os.O_RDONLY)
    try:
        actual_size = os.fstat(fd).st_size
        if actual_size != file_size_bytes:
            raise OSError(
                f"encoder-cache file size is {actual_size} bytes, "
                f"expected {file_size_bytes}"
            )
        _transfer_all(fd, buffers, os.readv, "read")
    finally:
        os.close(fd)

remove_entry(entry_path)

Remove an entry file, treating an already absent file as success.

Source code in vllm/distributed/ec_transfer/ec_connector/tiering/io.py
def remove_entry(entry_path: Path) -> None:
    """Remove an entry file, treating an already absent file as success."""
    entry_path.unlink(missing_ok=True)

store_entry(entry_path, temporary_path, sources, file_size_bytes, *, fsync=False)

Write a complete entry to a temporary file, then publish atomically.

Source code in vllm/distributed/ec_transfer/ec_connector/tiering/io.py
def store_entry(
    entry_path: Path,
    temporary_path: Path,
    sources: Sequence[memoryview],
    file_size_bytes: int,
    *,
    fsync: bool = False,
) -> None:
    """Write a complete entry to a temporary file, then publish atomically."""
    if temporary_path == entry_path:
        raise ValueError("temporary file must differ from final file")

    buffers = _prepare_buffers(sources, file_size_bytes, writable=False)
    entry_path.parent.mkdir(parents=True, exist_ok=True)
    temporary_path.parent.mkdir(parents=True, exist_ok=True)

    fd: int | None = None
    try:
        fd = os.open(
            temporary_path,
            os.O_CREAT | os.O_EXCL | os.O_WRONLY,
            0o644,
        )
        _transfer_all(fd, buffers, os.writev, "write")
        if fsync:
            os.fsync(fd)
        os.close(fd)
        fd = None
        os.replace(temporary_path, entry_path)
    except BaseException:
        if fd is not None:
            with suppress(OSError):
                os.close(fd)
        with suppress(OSError):
            temporary_path.unlink()
        raise