Repository navigation
fix: make MPS video generation 1.6x faster with GIL-free block reads - #190
Sabbagh Fares (Sabmit) wants to merge 3 commits into
Conversation
DiskBlockReader.read_into reads each block tensor with safetensors get_tensor on the BlockFetcher I/O thread. get_tensor holds the GIL while it reads. The main thread must have the GIL to queue each MPS operation, so it stops while each block loads. On Darwin, read each tensor directly into its CPU slot with os.preadv. os.preadv releases the GIL. A tensor uses the original path when an SDKeyValueOperation matches its key, or when the slot does not have the dtype and shape from the file.
Each test finds one bug: incorrect file offsets, raw bytes for a tensor that sd_ops must change, a slot with a different dtype, a block key map that is not restored, file descriptors that stay open, and the patch installed on a platform that is not macOS.
The direct-read patch handed the original read_into the fallback keys by swapping them into the reader's block key map and restoring the map in a finally block. A second DiskBlockReader that holds only the fallback keys does the same job without touching the shared map, so the restore test and its thread-safety note go away. Claude-Session: https://claude.ai/code/session_01M8Y4f9pRwDZfXY2VAqZNvz
|
Hi Sabbagh Fares (@Sabmit), thank you for this PR and for the detailed work. The GIL analysis, the benchmarks, and the byte-identical output check were very helpful. We also apologize that we did not review it sooner. We are not going to merge this PR. The reason is that LTX-2 v1.6.0 changed the loader in Desktop v1.3.2 uses Thank you again. If you still see the stall on Apple Silicon with |
Summary
This PR makes stage 1 of video generation on Apple Silicon approximately 2.7 times faster. A 540p 5 s generation becomes 1.4 times faster: 4:56 instead of 6:50 on an M3 Max. The output file is byte-identical to stock. The PR adds no dependencies. The PR has no effect on other platforms.
This PR and #189 are independent. You can merge them in any order. With both PRs, a 540p 5 s video takes 3:35 instead of 6:50.
Problem
On macOS, the transformer weights (39 GiB) stream from the disk in each denoising step.
BlockFetcherreads the next block on its I/O thread while the main thread computes the current block.DiskBlockReader.read_intoreads each tensor with safetensorsget_tensor.get_tensorholds the GIL during the full read. We checked this in the safetensors Rust bindings at 0.8.0 and on main: the CPU path never releases the GIL. The main thread needs the GIL to queue each MPS operation. Thus the main thread stops while each block loads. Stage 1 queues many small operations per block (2304 video tokens and 126 audio tokens). Stage 1 loses the most time.On an M3 Max, with a block read on the I/O thread:
get_tensorreados.preadvreadThe disk is not the bottleneck. The SSD gives 6.4 GB/s. Stage 1 needs less than 2 GB/s.
Solution
On Darwin, the patch reads each tensor directly from the file into its CPU slot with
os.preadv.os.preadvreleases the GIL during the read. It does not make a temporary tensor. The bytes are the same bytes thatget_tensorcopies.A tensor uses the direct read only when these conditions are true:
SDKeyValueOperationin the reader'ssd_opsmatches the key. With no match,apply_to_key_valuereturns the tensor unchanged.All other tensors use the original
read_into. The patch calls it on a secondDiskBlockReaderthat holds only these tensors. For the LTX-2.5 bf16 transformer, 3744 of the 4032 block tensors (93%) use the direct read. The other 288 tensors are the f32scale_shift_tabletensors. Their slots are bf16, so they keep theget_tensorpath and its dtype conversion.Results
Benchmark: 540p text-to-video
Conditions:
/api/generaterequest. The total includes the model load.Stock and this PR gave the same file in all 6 runs (SHA-256
6d9811b9b0f7...). "#189 only" and "#189 and this PR" gave one other file in all 6 runs (SHA-2560ffdec439447...). Stage 2 gains less time than stage 1. Its operations are 4 times larger, so the main thread waits for the GIL during a smaller part of its time.Memory
We sampled
/api/gpu-info/mpsandfootprint -peach second during the runs above. Stock and this PR gave these values:Malloc Large: 5751 MBMalloc Large: 5238 MBThe
Malloc Largecategory is 513 MB smaller during streaming. This is the temporary tensor for each weight thatget_tensorallocates and this PR does not. The peaks and the idle footprint do not change.All generation types: smoke suite
We ran
performance_runner/sanity.py --tags smoketwo times: once with this PR and once without it. Both runs had #189. Both runs gavepass=25 fail=0 skip=3. The 3 skipped tests need IC-LoRA models. We installed these models later and ran all IC-LoRA tests separately (see the next section).The speed increase is the largest for short videos at low resolution, because stage 1 is a large part of their time. Text-to-image does not use block streaming, so it does not change.
IC-LoRA
We installed the union-control and day-to-night IC-LoRA models. Then we ran
performance_runner/sanity.py --tags icloratwo times: once with this PR and once without it. The conditions were the same as the benchmark. Both runs gavepass=8 fail=0 skip=0.The 7 output videos are the same files with and without this PR (same SHA-256). The cancel test has no output.
Same output
/api/extendand/api/retakerequests have no seed. Each run gives a different output.Previous work
We did not find a previous report of this problem.
preadis ~2x faster and far more consistent on MPS LTX-2#304 changes the safetensors backend from mmap topread(already indisk_streaming_pread). That change makes the reads faster, butget_tensorstill holds the GIL.get_tensordoes not release the GIL, up to 0.9.0-rc.1. Its GIL-free read paths are for CUDA pinned memory and for bulk MPS reads.DiskTensorReaderdoes not use them.How to check this PR
pnpm backend:test -- tests/test_disk_streaming_direct_read.py.Disk streaming reads blocks with os.preadv (GIL released).Checks
pnpm typecheck: 0 errorspnpm backend:test: all tests pass. The one skipped test is for Windows only.backend/tests/test_disk_streaming_direct_read.py: 5 tests. Each test finds one error in the patch. We put each error into the patch, one at a time. Each time, the related test failed. These are the errors: incorrect file offsets, raw bytes for a tensor that anSDKeyValueOperationmust change, a slot with a different dtype, file descriptors that stay open, and the patch installed on a platform that is not macOS.Removal
Remove this patch when ltx-core
DiskBlockReaderreads without the GIL, or when safetensorsget_tensorreleases the GIL during CPU reads.