Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions backend/ltx2_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
del _safetensors_loader_fix
import services.patches.disk_streaming_pread as _disk_streaming_pread # Remove once ltx-core DiskTensorReader passes backend=pread on Darwin. No-op off Darwin.
_disk_streaming_pread.install()
import services.patches.disk_streaming_direct_read as _disk_streaming_direct_read # Remove once ltx-core DiskBlockReader reads without holding the GIL. No-op off Darwin.
_disk_streaming_direct_read.install()
import services.patches.linux_disk_streaming_backend as _linux_disk_streaming_backend # Remove once safetensors maps read-only (LTX-Desktop#163). No-op off Linux.
_linux_disk_streaming_backend.install()
import services.patches.safetensors_metadata_fix as _safetensors_metadata_fix # pyright: ignore[reportUnusedImport] # Remove once safetensors supports read-only mmap
Expand Down Expand Up @@ -112,6 +114,8 @@
# install() ran before basicConfig, so its status log would have been dropped.
_disk_streaming_pread.log_status()
del _disk_streaming_pread
_disk_streaming_direct_read.log_status()
del _disk_streaming_direct_read
_linux_disk_streaming_backend.log_status()
del _linux_disk_streaming_backend

Expand Down
193 changes: 193 additions & 0 deletions backend/services/patches/disk_streaming_direct_read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
"""Read streamed transformer blocks with ``os.preadv`` on Darwin.

``DiskBlockReader.read_into`` runs on the ``BlockFetcher`` I/O thread. It calls
``safetensors`` ``get_tensor`` for each tensor of the next block and copies the
result into a CPU slot. ``get_tensor`` holds the GIL while it reads. The main
thread must have the GIL to queue each MPS operation, so it waits between
operations while a block loads. Stage 1 queues many small operations per block
and loses the most time.

``os.preadv`` releases the GIL during the read. This patch reads each tensor
directly from the file into its slot, with no temporary tensor. The bytes are
the same as the ``get_tensor`` bytes, so the output video does not change.

A tensor uses the direct read only when all of these are true:

- No ``SDKeyValueOperation`` in the reader's ``sd_ops`` matches its key. Such an
operation can change the value or the key. With no match,
``apply_to_key_value`` returns the tensor unchanged.
- The slot is a contiguous CPU tensor with the dtype and shape from the file.

All other tensors use the original ``get_tensor`` path.

Darwin only. The I/O thread on CUDA hosts copies into pinned memory and was not
measured.

Remove once ltx-core ``DiskBlockReader`` reads without holding the GIL.

Usage:
import services.patches.disk_streaming_direct_read as disk_streaming_direct_read
disk_streaming_direct_read.install()
"""

from __future__ import annotations

import json
import logging
import os
import struct
import sys
from typing import Any, NamedTuple

import torch

logger = logging.getLogger(__name__)

_DTYPES: dict[str, torch.dtype] = {
"F64": torch.float64,
"F32": torch.float32,
"F16": torch.float16,
"BF16": torch.bfloat16,
}

_installed = False
_checked = False
_original_reader_init: Any = None
_original_reader_close: Any = None
_original_read_into: Any = None


class _FileSpan(NamedTuple):
fd: int
offset: int
nbytes: int
dtype: torch.dtype | None
shape: tuple[int, ...]


def _index_file(path: str) -> tuple[int, dict[str, _FileSpan]]:
"""Open *path* and return its fd and the byte span of each tensor."""
with open(path, "rb") as f:
(header_len,) = struct.unpack("<Q", f.read(8))
header: dict[str, Any] = json.loads(f.read(header_len))
header.pop("__metadata__", None)
data_start = 8 + header_len
fd = os.open(path, os.O_RDONLY)
spans: dict[str, _FileSpan] = {}
for key, meta in header.items():
start, end = meta["data_offsets"]
spans[key] = _FileSpan(fd, data_start + start, end - start, _DTYPES.get(meta["dtype"]), tuple(meta["shape"]))
return fd, spans


def _read_span(span: _FileSpan, target: torch.Tensor) -> None:
view = memoryview(target.view(-1).view(torch.uint8).numpy())
done = 0
while done < span.nbytes:
got = os.preadv(span.fd, [view[done:]], span.offset + done)
if got <= 0:
raise OSError(f"short read: {done} of {span.nbytes} bytes at offset {span.offset}")
done += got


def _can_read_direct(span: _FileSpan | None, target: torch.Tensor | None) -> bool:
return (
span is not None
and target is not None
and target.device.type == "cpu"
and target.is_contiguous()
and target.dtype == span.dtype
and tuple(target.shape) == span.shape
and target.numel() * target.element_size() == span.nbytes
)


def install() -> None:
"""Read streamed blocks with ``os.preadv`` on Darwin. Idempotent.

No-op on every other platform. Does not log. Call ``log_status`` once
logging exists.
"""
global _installed, _checked, _original_reader_init, _original_reader_close, _original_read_into
if _installed:
return
_checked = True
if sys.platform != "darwin":
return

from ltx_core.block_streaming.disk import DiskBlockReader, DiskTensorReader # noqa: PLC0415
from ltx_core.block_streaming.utils import make_block_key # noqa: PLC0415
from ltx_core.loader.sd_ops import SDKeyValueOperation # noqa: PLC0415

_original_reader_init = DiskTensorReader.__init__
_original_reader_close = DiskTensorReader.close
_original_read_into = DiskBlockReader.read_into

def _reader_init(self: DiskTensorReader, paths: list[str]) -> None:
_original_reader_init(self, paths)
fds: list[int] = []
spans: dict[str, _FileSpan] = {}
try:
for path in paths:
fd, file_spans = _index_file(path)
fds.append(fd)
spans.update(file_spans)
except Exception:
for fd in fds:
os.close(fd)
raise
self._direct_fds = fds # type: ignore[attr-defined]
self._direct_spans = spans # type: ignore[attr-defined]

def _reader_close(self: DiskTensorReader) -> None:
for fd in getattr(self, "_direct_fds", []):
os.close(fd)
self._direct_fds = [] # type: ignore[attr-defined]
self._direct_spans = {} # type: ignore[attr-defined]
_original_reader_close(self)

def _value_op_matches(reader: DiskBlockReader, full_key: str) -> bool:
sd_ops = reader._sd_ops # pyright: ignore[reportPrivateUsage]
if sd_ops is None:
return False
return any(
isinstance(op, SDKeyValueOperation)
and full_key.startswith(op.key_matcher.prefix)
and full_key.endswith(op.key_matcher.suffix)
for op in sd_ops.mapping
)

def _read_into(self: DiskBlockReader, target: dict[str, torch.Tensor], block_idx: int) -> None:
spans: dict[str, _FileSpan] = getattr(self._reader, "_direct_spans", {}) # pyright: ignore[reportPrivateUsage]
blocks_prefix: str = self._blocks_prefix # pyright: ignore[reportPrivateUsage]
rest: list[tuple[str, str]] = []
for sft_key, param_name in self._block_key_map[block_idx]: # pyright: ignore[reportPrivateUsage]
span = spans.get(sft_key)
if (
_can_read_direct(span, target.get(param_name))
and not _value_op_matches(self, make_block_key(blocks_prefix, block_idx, param_name))
):
assert span is not None
_read_span(span, target[param_name])
else:
rest.append((sft_key, param_name))
if rest:
# The original read_into takes its keys from the reader's block key map.
# A throwaway reader with only the fallback keys leaves this reader's map untouched.
fallback = DiskBlockReader(self._reader, {block_idx: rest}, self._sd_ops, blocks_prefix) # pyright: ignore[reportPrivateUsage]
_original_read_into(fallback, target, block_idx)

DiskTensorReader.__init__ = _reader_init # type: ignore[method-assign]
DiskTensorReader.close = _reader_close # type: ignore[method-assign]
DiskBlockReader.read_into = _read_into # type: ignore[method-assign]
_installed = True


def log_status() -> None:
"""Report the block read path. Call after logging is configured."""
if not _checked:
return
if _installed:
logger.info("Disk streaming reads blocks with os.preadv (GIL released)")
else:
logger.info("Disk streaming reads blocks with safetensors get_tensor; preadv is Darwin-only")
151 changes: 151 additions & 0 deletions backend/tests/test_disk_streaming_direct_read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
"""Streamed block reads with os.preadv on Darwin give the same slot values as get_tensor."""

from __future__ import annotations

import os
import sys
from collections.abc import Iterator
from pathlib import Path

import pytest
import torch
from ltx_core.block_streaming.disk import DiskBlockReader, DiskTensorReader
from ltx_core.loader.sd_ops import KeyValueOperationResult, SDOps
from safetensors.torch import save_file

import services.patches.disk_streaming_direct_read as direct_read

PREFIX = "blocks"
needs_preadv = pytest.mark.skipif(not hasattr(os, "preadv"), reason="os.preadv and /dev/fd are POSIX-only")


@pytest.fixture(autouse=True)
def _restore_readers() -> Iterator[None]:
init, close, read_into = DiskTensorReader.__init__, DiskTensorReader.close, DiskBlockReader.read_into
yield
DiskTensorReader.__init__ = init # type: ignore[method-assign]
DiskTensorReader.close = close # type: ignore[method-assign]
DiskBlockReader.read_into = read_into # type: ignore[method-assign]
direct_read._installed = False
direct_read._checked = False


@pytest.fixture
def get_tensor_keys(monkeypatch: pytest.MonkeyPatch) -> list[str]:
"""Keys that went through the original get_tensor path."""
keys: list[str] = []
original = DiskTensorReader.get_tensor

def _record(self: DiskTensorReader, key: str) -> torch.Tensor:
keys.append(key)
return original(self, key)

monkeypatch.setattr(DiskTensorReader, "get_tensor", _record)
return keys


def _checkpoint(tmp_path: Path) -> tuple[list[str], dict[str, torch.Tensor]]:
"""Two files; mixed dtypes, so every tensor starts at a different offset."""
torch.manual_seed(0)
tensors = {
f"{PREFIX}.0.attn.weight": torch.randn(8, 4, dtype=torch.bfloat16),
f"{PREFIX}.0.attn.bias": torch.randn(3, dtype=torch.float32),
f"{PREFIX}.0.ff.weight": torch.randn(5, 7, dtype=torch.bfloat16),
f"{PREFIX}.0.ff.scale": torch.randn(6, dtype=torch.float32),
}
names = list(tensors)
first, second = tmp_path / "a.safetensors", tmp_path / "b.safetensors"
save_file({k: tensors[k] for k in names[:2]}, str(first))
save_file({k: tensors[k] for k in names[2:]}, str(second))
return [str(first), str(second)], tensors


def _key_map(tensors: dict[str, torch.Tensor]) -> dict[int, list[tuple[str, str]]]:
return {0: [(key, key.removeprefix(f"{PREFIX}.0.")) for key in tensors]}


def _slots(tensors: dict[str, torch.Tensor]) -> dict[str, torch.Tensor]:
return {key.removeprefix(f"{PREFIX}.0."): torch.zeros_like(t) for key, t in tensors.items()}


def _install_on_darwin(monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(sys, "platform", "darwin")
direct_read.install()


def _read_block(paths: list[str], key_map: dict[int, list[tuple[str, str]]], slots: dict[str, torch.Tensor], sd_ops: SDOps | None = None) -> None:
reader = DiskTensorReader(paths)
try:
DiskBlockReader(reader, key_map, sd_ops=sd_ops, blocks_prefix=PREFIX).read_into(slots, 0)
finally:
reader.close()


@needs_preadv
def test_direct_read_offset_error_corrupts_slots(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, get_tensor_keys: list[str]
) -> None:
paths, tensors = _checkpoint(tmp_path)
expected = _slots(tensors)
_read_block(paths, _key_map(tensors), expected)

_install_on_darwin(monkeypatch)
get_tensor_keys.clear()
actual = _slots(tensors)
_read_block(paths, _key_map(tensors), actual)

assert get_tensor_keys == []
for name, value in expected.items():
assert torch.equal(actual[name], value), name


@needs_preadv
def test_value_operation_key_gets_raw_file_bytes(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, get_tensor_keys: list[str]
) -> None:
paths, tensors = _checkpoint(tmp_path)
doubled = f"{PREFIX}.0.ff.scale"
sd_ops = SDOps("double_scale").with_matching().with_kv_operation(
lambda key, value: [KeyValueOperationResult(key, value * 2)], key_suffix=".ff.scale"
)
_install_on_darwin(monkeypatch)
slots = _slots(tensors)
_read_block(paths, _key_map(tensors), slots, sd_ops=sd_ops)

assert get_tensor_keys == [doubled]
assert torch.equal(slots["ff.scale"], tensors[doubled] * 2)
assert torch.equal(slots["attn.weight"], tensors[f"{PREFIX}.0.attn.weight"])


@needs_preadv
def test_dtype_mismatch_slot_gets_unconverted_bytes(
tmp_path: Path, monkeypatch: pytest.MonkeyPatch, get_tensor_keys: list[str]
) -> None:
paths, tensors = _checkpoint(tmp_path)
_install_on_darwin(monkeypatch)
slots = _slots(tensors)
slots["attn.bias"] = torch.zeros(3, dtype=torch.bfloat16) # file holds f32
_read_block(paths, _key_map(tensors), slots)

assert get_tensor_keys == [f"{PREFIX}.0.attn.bias"]
assert torch.equal(slots["attn.bias"], tensors[f"{PREFIX}.0.attn.bias"].to(torch.bfloat16))


@needs_preadv
def test_close_leaks_file_descriptors(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None:
paths, _ = _checkpoint(tmp_path)
_install_on_darwin(monkeypatch)
before = len(os.listdir("/dev/fd"))
reader = DiskTensorReader(paths)
assert len(os.listdir("/dev/fd")) > before
reader.close()
assert len(os.listdir("/dev/fd")) == before


@pytest.mark.parametrize("platform", ["win32", "linux"])
def test_install_patches_reader_off_darwin(platform: str, monkeypatch: pytest.MonkeyPatch) -> None:
monkeypatch.setattr(sys, "platform", platform)
before = DiskBlockReader.read_into
direct_read.install()
assert DiskBlockReader.read_into is before
assert direct_read._installed is False