trainstream 0.8.0__tar.gz → 0.8.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (92) hide show
  1. {trainstream-0.8.0/trainstream.egg-info → trainstream-0.8.2}/PKG-INFO +94 -59
  2. {trainstream-0.8.0 → trainstream-0.8.2}/README.md +93 -58
  3. {trainstream-0.8.0 → trainstream-0.8.2}/setup.py +1 -1
  4. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_attention.py +24 -0
  5. trainstream-0.8.2/tests/test_embed.py +115 -0
  6. trainstream-0.8.2/tests/test_graphs.py +354 -0
  7. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/__init__.py +1 -1
  8. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/attention.py +28 -0
  9. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/bindings.cpp +1 -0
  10. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/fused.cpp +16 -0
  11. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/fused_kernel.cu +33 -0
  12. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/kernels.h +6 -0
  13. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/trainstream.h +4 -0
  14. trainstream-0.8.2/trainstream/embed.py +107 -0
  15. trainstream-0.8.2/trainstream/graphs.py +723 -0
  16. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/optimizer.py +7 -0
  17. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/paging.py +51 -5
  18. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/wrapper.py +130 -3
  19. {trainstream-0.8.0 → trainstream-0.8.2/trainstream.egg-info}/PKG-INFO +94 -59
  20. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream.egg-info/SOURCES.txt +4 -0
  21. {trainstream-0.8.0 → trainstream-0.8.2}/LICENSE +0 -0
  22. {trainstream-0.8.0 → trainstream-0.8.2}/MANIFEST.in +0 -0
  23. {trainstream-0.8.0 → trainstream-0.8.2}/NOTICE +0 -0
  24. {trainstream-0.8.0 → trainstream-0.8.2}/build.bat +0 -0
  25. {trainstream-0.8.0 → trainstream-0.8.2}/pyproject.toml +0 -0
  26. {trainstream-0.8.0 → trainstream-0.8.2}/setup.cfg +0 -0
  27. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_aio.py +0 -0
  28. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_arena.py +0 -0
  29. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_checkpoint.py +0 -0
  30. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_distributed.py +0 -0
  31. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_dpo.py +0 -0
  32. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_experts.py +0 -0
  33. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_features.py +0 -0
  34. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_fp8_grads.py +0 -0
  35. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_fused.py +0 -0
  36. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_gemm.py +0 -0
  37. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_guard.py +0 -0
  38. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_huggingface.py +0 -0
  39. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_kvcache.py +0 -0
  40. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_lora_fused.py +0 -0
  41. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_lowp.py +0 -0
  42. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_multigpu.py +0 -0
  43. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_offload.py +0 -0
  44. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_optimizer.py +0 -0
  45. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_outliers.py +0 -0
  46. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_packing.py +0 -0
  47. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_paging.py +0 -0
  48. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_peer.py +0 -0
  49. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_pissa.py +0 -0
  50. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_planner.py +0 -0
  51. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_quality_options.py +0 -0
  52. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_quant.py +0 -0
  53. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_recompute.py +0 -0
  54. {trainstream-0.8.0 → trainstream-0.8.2}/tests/test_ring.py +0 -0
  55. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/_build.py +0 -0
  56. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/checkpoint.py +0 -0
  57. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/aio.cpp +0 -0
  58. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/aio.h +0 -0
  59. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/ckpt.cpp +0 -0
  60. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/disk.cpp +0 -0
  61. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/gemm_fp4_kernel.cu +0 -0
  62. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/guard.cpp +0 -0
  63. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/memory_pinner.cpp +0 -0
  64. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/offload_adamw.cpp +0 -0
  65. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/offload_adamw.h +0 -0
  66. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/offload_adamw_kernel.cu +0 -0
  67. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/peer.cpp +0 -0
  68. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/quant.cpp +0 -0
  69. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/quant_kernel.cu +0 -0
  70. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/ring.cpp +0 -0
  71. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/csrc/shm.cpp +0 -0
  72. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/disk.py +0 -0
  73. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/experts.py +0 -0
  74. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/fused.py +0 -0
  75. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/hooks.py +0 -0
  76. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/integrations/__init__.py +0 -0
  77. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/integrations/huggingface.py +0 -0
  78. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/kvcache.py +0 -0
  79. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/lora.py +0 -0
  80. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/losses.py +0 -0
  81. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/lowp.py +0 -0
  82. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/packing.py +0 -0
  83. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/peer.py +0 -0
  84. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/planner.py +0 -0
  85. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/profiler.py +0 -0
  86. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/quant.py +0 -0
  87. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/ring.py +0 -0
  88. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream/shared.py +0 -0
  89. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream.egg-info/dependency_links.txt +0 -0
  90. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream.egg-info/not-zip-safe +0 -0
  91. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream.egg-info/requires.txt +0 -0
  92. {trainstream-0.8.0 → trainstream-0.8.2}/trainstream.egg-info/top_level.txt +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: trainstream
3
- Version: 0.8.0
3
+ Version: 0.8.2
4
4
  Summary: Zero-dependency PyTorch training offload: optimizer, gradients, activations and weights to host RAM, with a memory planner and built-in int8/NF4/FP8/NVFP4 quantization.
5
5
  Home-page: https://github.com/johnanishg/TrainStream
6
6
  Author: John Anish G
@@ -49,6 +49,39 @@ model, optimizer = trainstream.wrap(model, optimizer)
49
49
 
50
50
  TrainStream is a C++/CUDA extension for PyTorch. It uses only the C++ standard library, the CUDA runtime and PyTorch's ATen: no DeepSpeed, no bitsandbytes. Updates match `torch.optim.AdamW` (max parameter difference ≈3·10⁻⁷ after 20 steps), and activation and weight paging are bit-exact.
51
51
 
52
+ ## What's new in 0.8.2
53
+
54
+ - **Transformer blocks run as CUDA graphs** (`cuda_graphs="auto"`). At small batches TrainStream's Python launched kernels more slowly than the GPU ran them: one training step of Qwen2.5-14B LoRA on an NVFP4 base at 512 tokens is about 10,600 kernels, and in a traced step the GPU stood idle between them for about 105 ms and, never fully busy, ran at a lower clock. Now, once a run has settled with nothing moving during a step (the optimizer in VRAM, every block's weights in VRAM as bf16 or as fp8 / nvfp4 on tensor cores, activations kept), each block's forward and backward are recorded once as two CUDA graphs and replayed from then on: the same kernels on the same memory, so gradients are bit-identical (checked on Qwen2.5-14B: the loss and all 672 LoRA gradients of a step). Measured on Qwen2.5-14B, LoRA r=16, NVFP4 base, batch 1 × 512, RTX 5090 Laptop GPU:
55
+
56
+ | | without graphs | with graphs |
57
+ |---|---|---|
58
+ | Windows, alternating runs (30 November, 3 each) | 0.509 / 0.515 / 0.521 s | 0.361 / 0.378 / 0.377 s (27–29 % faster) |
59
+ | Windows, full checks: 0.8.1's (30 November) and 0.8.2's (1 December) | 0.502 s, 1.24× bitsandbytes | 0.356 s, 1.61× bitsandbytes |
60
+ | WSL, the same two full checks | 0.451 s, 1.26× bitsandbytes | 0.360 s, 1.66× bitsandbytes |
61
+ | Qwen2.5-1.5B LoRA, batch 2 × 1024 (`benchmarks/quality.py`, Windows, 0.8.1's and 0.8.2's quality runs of 30 November): NVFP4 / FP8 / bf16 | 0.259 / 0.235 / 0.294 s | 0.206 / 0.210 / 0.283 s |
62
+
63
+ In 0.8.2's full check every rival ran 5–15 % slower than in 0.8.1's (bitsandbytes 0.570 → 0.599 s in WSL), so the step times understate the change; the ratios against bitsandbytes don't. Peak VRAM did not change (16.79 against 16.78 GB): the graphs keep one step's activations (1.26 GiB here) in a memory pool of their own, as the eager step held them. Where graphs do not engage, nothing changes: NF4 and int8 bases (their weights are expanded block by block on a copy stream beside the compute; inside a graph that expansion would run in line and cost more than graphs save), blocks streamed from RAM, activations paged or recomputed (in the benchmarks: 2,048 tokens, packed documents, the 12 GB cap), the host optimizer, data parallel. A forward whose inputs differ from the recorded ones, without gradients, in eval mode, under activation checkpointing, or before the previous forward's backward has run, runs eagerly as before. `memory_report()["graphs"]` says what happened and why; `cuda_graphs=False` turns it off.
64
+ - **Fix: Hugging Face gradient checkpointing with `attention="auto"`** (since 0.5). A layer that `gradient_checkpointing_enable()` runs again in backward ran outside the attention-backend order its forward used, so it could pick PyTorch's default kernel instead (on an RTX 50-series GPU, where cuDNN's attention is chosen, it did), and `torch.utils.checkpoint` stopped training with a `CheckpointError` about mismatched saved tensors. Blocks recomputed inside backward now re-enter their forward's order.
65
+
66
+ ## What's new in 0.8.1
67
+
68
+ Measured in the full check of 30 November 2026 on an RTX 5090 Laptop GPU (24 GB), in WSL2 (49 GB VM) and on Windows 11 (64 GB), against the 0.8.0 check of 23 November. All 465 tests pass on Windows and in WSL (from a wheel installed with pip and with uv), and perplexity before fine-tuning is the same as 0.8.0's in every format.
69
+
70
+ - **The input embedding moves to host RAM before any layer does** (`embed_offload="auto"`). A frozen input embedding is a lookup table: a step reads only its batch's rows (5 MB of Qwen2.5-14B's 1.45 GiB at 512 tokens). When the memory plan would stream transformer blocks from RAM, the table now goes to pinned host RAM instead, and a kernel reads the rows it needs straight from there (zero-copy, on the GPU's own stream, so nothing waits for the host). The output is the same bits. The VRAM it frees holds more blocks:
71
+
72
+ | Qwen2.5-14B LoRA | WSL, 0.8.0 → 0.8.1 | Windows, 0.8.0 → 0.8.1 | blocks in VRAM |
73
+ |---|---|---|---|
74
+ | NF4 base, 1 × 512, 12 GB of VRAM | 0.779 → 0.743 s | 0.730 → 0.710 s | 36–37 → 46–47 of 48 |
75
+ | NVFP4 base, 1 × 512, 12 GB of VRAM | 0.657 → 0.630 s | 0.569 → 0.670 s (see below) | 37 → 48 |
76
+ | `"auto"` (bf16 base), 1 × 512 | 0.968 → 0.838 s | 0.982 → 0.895 s | 28 → 31 |
77
+ | `"auto"`, 1 × 2,048 | 3.04 → 2.84 s | 3.04 → 2.82 s | 23–24 → 25–26 |
78
+ | `"auto"`, 1 × 512, 12 GB of VRAM | 2.13 → 1.89 s | 1.60 → 1.50 s | 9–10 → 12–13 |
79
+ | `"auto"`, 2 × 2,048 packed | 6.76 → 6.43 s | 6.75 → 6.61 s | 22 → 25 |
80
+
81
+ On Windows the GPU ran at a lower clock in this check than in 0.8.0's, for every engine: bitsandbytes was 9 % slower too, and in the uncapped NVFP4 run, which this change doesn't touch, the same FP4 matmuls took 391 ms per step against 258 ms (1,417 MHz against 2,400 MHz). Alternating runs with the option on and off in one session are the cleaner comparison there: NVFP4 base under the 12 GB cap 0.700 / 0.699 / 0.703 s → 0.669 / 0.671 / 0.670 s (4 % faster, 30 November); NF4 base 0.759 / 0.758 s → 0.681 / 0.686 s (10 % faster, 23 November).
82
+
83
+ Against Unsloth held to 12 GB of VRAM (WSL, 3 runs each): TrainStream's NF4 base takes 0.743 s against Unsloth's 0.685 s, so Unsloth is still 1.08× faster (1.10× in 0.8.0); the NVFP4 base stays ahead, 0.630 s (1.09×). Where everything already fits in VRAM (the 4-bit bases without a cap) the option doesn't engage and nothing changes. `embed_offload=False` keeps 0.8.0's behaviour.
84
+
52
85
  ## What's new in 0.8
53
86
 
54
87
  Measured in the full check of 23 November 2026 on an RTX 5090 Laptop GPU (24 GB), in WSL2 (49 GB VM) and on Windows 11 (64 GB), with the non-finite guard on (the new default). Every number is in the tables below.
@@ -162,40 +195,40 @@ Measured in the full check of 11 November 2026 on an RTX 5090 Laptop GPU (24 GB)
162
195
 
163
196
  ## Against other libraries (same laptop: WSL2 with a 49 GB VM, then Windows 11 with 64 GB)
164
197
 
165
- > TrainStream 0.8.0, full check of 23 November 2026. Each engine runs in a fresh process, 10 steps after its warm-up (TrainStream: after it has finished tuning itself), and its step time is the median of those. Where two compared engines were within 10 %, both ran 3 times and the median run counts; a win counts only when the gap is wider than the runs' spread. Every engine listed finished all its steps. Between full checks the laptop's speed moves by 5–10 % for every engine alike, so read smaller differences as noise.
198
+ > TrainStream 0.8.2: performance from the full check of 1 December 2026, tests and quality from those of 30 November and 1 December. Each engine runs in a fresh process, 10 steps after its warm-up (TrainStream: after it has finished tuning itself, and recording its CUDA graphs where it does), and its step time is the median of those. Where two compared engines were within 10 %, both ran 3 times and the median run counts; a win counts only when the gap is wider than the runs' spread. Every engine listed finished all its steps. Between full checks the laptop's speed moves by 5–15 % for every engine alike (in this one every rival ran 5–15 % slower than in 0.8.1's), so compare engines within a table, and read smaller differences as noise.
166
199
 
167
200
  **LoRA r=16, Qwen2.5-14B, batch 1 × 512 (WSL2)**
168
201
 
169
202
  | Engine | Step time | Peak VRAM | Against the fastest |
170
203
  |---|---|---|---|
171
- | TrainStream, NVFP4 base (FP4 tensor cores) | 0.462 s | 16.8 GB | fastest |
172
- | TrainStream, NF4 base | 0.560 s (3 runs: 0.554–0.572) | 16.8 GB | 1.21× |
173
- | bitsandbytes QLoRA (NF4) + PEFT | 0.589 s (3 runs: 0.577–0.605) | 17.4 GB | 1.27× |
174
- | Unsloth (4-bit) | 0.714 s | 11.5 GB | 1.55× |
175
- | bitsandbytes QLoRA + Liger kernels | 0.804 s | 12.0 GB | 1.74× |
176
- | bitsandbytes QLoRA + gradient checkpointing | 0.856 s | 12.7 GB | 1.85× |
177
- | TrainStream (auto: exact bf16 base, 20 of 48 layers streamed) | 0.968 s | 21.6 GB | 2.09× |
204
+ | TrainStream, NVFP4 base (FP4 tensor cores, CUDA graphs) | 0.360 s | 16.8 GB | fastest |
205
+ | TrainStream, NF4 base | 0.567 s (3 runs: 0.553–0.574) | 16.8 GB | 1.58× |
206
+ | bitsandbytes QLoRA (NF4) + PEFT | 0.599 s (3 runs: 0.596–0.608) | 17.4 GB | 1.66× |
207
+ | Unsloth (4-bit) | 0.767 s | 11.5 GB | 2.13× |
208
+ | bitsandbytes QLoRA + gradient checkpointing | 0.864 s | 12.7 GB | 2.40× |
209
+ | bitsandbytes QLoRA + Liger kernels | 0.873 s | 12.0 GB | 2.43× |
210
+ | TrainStream (auto: exact bf16 base, 17 of 48 layers streamed) | 0.948 s | 21.5 GB | 2.63× |
178
211
 
179
212
  **LoRA r=16, Qwen2.5-14B, batch 1 × 2048 (WSL2)**
180
213
 
181
214
  | Engine | Step time | Peak VRAM | Against the fastest |
182
215
  |---|---|---|---|
183
- | TrainStream, NVFP4 base | 1.39 s | 21.2 GB | fastest |
184
- | TrainStream, NF4 base | 1.99 s | 19.3 GB | 1.43× |
185
- | Unsloth (4-bit) | 2.48 s | 11.7 GB | 1.79× |
186
- | bitsandbytes QLoRA + Liger kernels | 2.62 s | 13.0 GB | 1.89× |
187
- | bitsandbytes QLoRA + gradient checkpointing | 2.80 s | 16.3 GB | 2.02× |
188
- | TrainStream (auto) | 3.04 s | 22.5 GB | 2.19× |
216
+ | TrainStream, NVFP4 base | 1.49 s | 21.2 GB | fastest |
217
+ | TrainStream, NF4 base | 2.40 s | 19.3 GB | 1.61× |
218
+ | Unsloth (4-bit) | 2.73 s | 11.7 GB | 1.83× |
219
+ | bitsandbytes QLoRA + Liger kernels | 2.90 s | 13.0 GB | 1.95× |
220
+ | bitsandbytes QLoRA + gradient checkpointing | 3.02 s | 16.3 GB | 2.02× |
221
+ | TrainStream (auto) | 3.12 s | 23.1 GB | 2.10× |
189
222
 
190
223
  **LoRA r=16, Qwen2.5-14B, batch 1 × 512, every engine held to 12 GB of VRAM (WSL2)**
191
224
 
192
225
  | Engine | Step time | Peak VRAM | Against the fastest |
193
226
  |---|---|---|---|
194
- | TrainStream, NVFP4 base | 0.657 s (3 runs: 0.655–0.705) | 11.8 GB | fastest |
195
- | Unsloth (4-bit) | 0.711 s (3 runs: 0.711–0.716) | 11.4 GB | 1.08× |
196
- | TrainStream, NF4 base | 0.779 s (3 runs: 0.774–0.795) | 11.6 GB | 1.18× |
197
- | bitsandbytes QLoRA + Liger kernels | 0.800 s | 12.0 GB | 1.22× |
198
- | TrainStream (auto) | 2.13 s | 11.7 GB | 3.23× |
227
+ | TrainStream, NVFP4 base | 0.575 s | 11.8 GB | fastest |
228
+ | TrainStream, NF4 base | 0.702 s (3 runs: 0.682–0.769) | 11.5 GB | 1.22× |
229
+ | Unsloth (4-bit) | 0.767 s (3 runs: 0.765–0.771) | 11.4 GB | 1.33× |
230
+ | bitsandbytes QLoRA + Liger kernels | 0.862 s | 12.0 GB | 1.50× |
231
+ | TrainStream (auto) | 2.08 s | 11.8 GB | 3.61× |
199
232
 
200
233
  bitsandbytes QLoRA, with or without gradient checkpointing, ran out of memory under the 12 GB cap in earlier runs, so the full check leaves it out.
201
234
 
@@ -203,11 +236,11 @@ bitsandbytes QLoRA, with or without gradient checkpointing, ran out of memory un
203
236
 
204
237
  | Engine | Step time | Peak VRAM | Against the fastest |
205
238
  |---|---|---|---|
206
- | TrainStream, NVFP4 base | 3.70 s | 19.9 GB | fastest |
207
- | TrainStream, NF4 base | 5.50 s (3 runs: 5.47–5.51) | 19.8 GB | 1.49× |
208
- | bitsandbytes QLoRA + Liger kernels | 5.99 s (3 runs: 5.98–5.99) | 14.4 GB | 1.62× |
209
- | bitsandbytes QLoRA + gradient checkpointing | 6.47 s | 21.0 GB | 1.75× |
210
- | TrainStream (auto) | 6.76 s | 21.2 GB | 1.83× |
239
+ | TrainStream, NVFP4 base | 3.98 s | 19.9 GB | fastest |
240
+ | TrainStream, NF4 base | 5.76 s | 19.8 GB | 1.45× |
241
+ | bitsandbytes QLoRA + Liger kernels | 6.40 s | 14.4 GB | 1.61× |
242
+ | bitsandbytes QLoRA + gradient checkpointing | 7.06 s | 21.0 GB | 1.77× |
243
+ | TrainStream (auto) | 7.26 s | 21.3 GB | 1.82× |
211
244
 
212
245
  Unsloth isn't compared here: it patches attention itself, so the harness can't check that attention stays inside each packed document.
213
246
 
@@ -215,61 +248,61 @@ Unsloth isn't compared here: it patches attention itself, so the harness can't c
215
248
 
216
249
  | Engine | Step time | Peak VRAM | Against the fastest |
217
250
  |---|---|---|---|
218
- | TrainStream (auto) | 1.09 s | 18.8 GB | fastest |
219
- | Stock PyTorch | 1.62 s | 32.4 GB | 1.48× (spills past VRAM) |
220
- | DeepSpeed ZeRO-2, optimizer offload | 1.76 s | 13.3 GB | 1.61× |
251
+ | TrainStream (auto) | 1.13 s | 18.8 GB | fastest |
252
+ | Stock PyTorch | 1.63 s | 32.4 GB | 1.45× (spills past VRAM) |
253
+ | DeepSpeed ZeRO-2, optimizer offload | 1.81 s | 13.3 GB | 1.61× |
221
254
 
222
255
  **The same on Windows 11 (64 GB RAM; Unsloth, Liger and DeepSpeed aren't installed there)**
223
256
 
224
257
  | Engine | LoRA 14B, 1 × 512 | LoRA 14B, 1 × 2048 | 14B, 12 GB VRAM | 14B, packed | Full fine-tune 1.5B |
225
258
  |---|---|---|---|---|---|
226
- | TrainStream, NVFP4 base | **0.394 s** | **1.40 s** | **0.569 s** | **3.73 s** | — |
227
- | TrainStream, NF4 base | 0.521 s | 2.00 s | 0.730 s | 5.40 s | — |
228
- | TrainStream (auto) | 0.982 s | 3.04 s | 1.60 s | 6.75 s | **1.09 s** |
229
- | bitsandbytes QLoRA (NF4) + PEFT | 0.571 s | spills past VRAM | out of memory | left out (hit the time limit in an earlier run) | — |
230
- | bitsandbytes QLoRA + gradient checkpointing | 0.822 s | 2.99 s | out of memory | 6.62 s | — |
231
- | Stock PyTorch | spills past VRAM | spills past VRAM | — | — | 1.72 s (spills) |
232
-
233
- - With an NVFP4 base TrainStream had the fastest step in every 14B scenario, in WSL and on Windows, including under the 12 GB cap. NVFP4 costs quality (see the perplexity table below).
234
- - With an NF4 base, the format bitsandbytes and Unsloth use, TrainStream was ahead of every rival in every scenario except under the 12 GB cap, where Unsloth was 1.10× faster: there TrainStream streams 12 of the 48 4-bit layers from RAM and Unsloth keeps them all in VRAM. At 512 tokens its lead over bitsandbytes is small (1.05× in WSL, 1.09× on Windows).
259
+ | TrainStream, NVFP4 base | **0.356 s** | **1.48 s** | **0.560 s** | **3.80 s** | — |
260
+ | TrainStream, NF4 base | 0.542 s | 2.18 s | 0.642 s | 5.87 s | — |
261
+ | TrainStream (auto) | 1.02 s | 3.08 s | 1.50 s | 6.98 s | **1.08 s** |
262
+ | bitsandbytes QLoRA (NF4) + PEFT | 0.572 s | spills past VRAM | out of memory | left out (hit the time limit in an earlier run) | — |
263
+ | bitsandbytes QLoRA + gradient checkpointing | 0.877 s | 3.13 s | out of memory | 6.99 s | — |
264
+ | Stock PyTorch | spills past VRAM | spills past VRAM | — | — | 2.40 s (spills) |
265
+
266
+ - With an NVFP4 base TrainStream had the fastest step in every 14B scenario, in WSL and on Windows. At 512 tokens its blocks now run as CUDA graphs: 1.66× bitsandbytes in WSL and 1.61× on Windows (1.26× and 1.24× in 0.8.1's check). NVFP4 costs quality (see the perplexity table below).
267
+ - With an NF4 base, the format bitsandbytes and Unsloth use, TrainStream was ahead of every rival in every scenario. Held to 12 GB of VRAM its median run was 1.09× Unsloth's (0.702 s against 0.767 s), though the slowest of its three runs (0.769 s) was level with Unsloth's (in 0.8.1's check Unsloth was 1.08× faster). At 512 tokens its lead over bitsandbytes is small (1.06× in WSL and on Windows): NF4 blocks don't run as graphs.
235
268
  - When it has room, TrainStream holds more in VRAM than Unsloth (16.8 GB against 11.5 GB at 512 tokens) because it plans up to a VRAM target; held to 12 GB it fits like Unsloth does.
236
- - `"auto"` with 49–64 GB of RAM keeps the 14B base exact in bf16 and streams 20–26 of its 48 layers from RAM. That is slower than the 4-bit engines and changes no weight.
237
- - The full fine-tune: TrainStream keeps its activations in VRAM and stays at 18.8 GB, 1.48× faster than stock PyTorch in WSL and 1.58× on Windows. Stock PyTorch peaks at 32.4 GB, more than the GPU's 24 GB, so its step time depends on how the driver spills into system RAM.
269
+ - `"auto"` with 49–64 GB of RAM keeps the 14B base exact in bf16 and streams 17–23 of its 48 layers from RAM (35–36 under the 12 GB cap). That is slower than the 4-bit engines and changes no weight.
270
+ - The full fine-tune: TrainStream keeps its activations in VRAM and stays at 18.8 GB, 1.45× faster than stock PyTorch in WSL. Stock PyTorch peaks at 32.4 GB, more than the GPU's 24 GB, so its step time depends on how the driver spills into system RAM (2.40 s on Windows in this run, 1.45 s in 0.8.1's).
238
271
  - Stock PyTorch and DeepSpeed in the 14B scenarios, and bitsandbytes without checkpointing at 2,048 tokens, don't fit in 24 GB of VRAM: in earlier runs the driver spilled into system RAM at 46–371 s per step, so the full check leaves them out.
239
- - In three places TrainStream was clearly slower in WSL than on Windows: the NVFP4 base at 512 tokens by 17 % (0.462 s against 0.394 s; the GPU drew 111 W against 170 W), and under the 12 GB cap the NVFP4 base by 16 % (WSL paged the activations, Windows recomputed them) and `"auto"` by 33 %. Everywhere else every engine was within 7 % of its time in the other environment.
272
+ - Between the two environments every engine was within 10 % of its time in the other, except `"auto"` under the 12 GB cap, 1.39× slower in WSL (2.08 s against 1.50 s), and stock PyTorch's full fine-tune, which spills.
240
273
 
241
274
  **Perplexity against bf16** (`benchmarks/quality.py`, held-out Python standard-library source; lower is better)
242
275
 
243
276
  | Format | Qwen2.5-14B | after 20 LoRA steps | Qwen2.5-1.5B | after 40 LoRA steps |
244
277
  |---|---|---|---|---|
245
- | int8 | −0.00 % | −0.03 % | +0.02 % | −0.00 % |
246
- | FP8 (tensor cores) | +0.89 % | +0.79 % | +1.70 % | +1.43 % |
247
- | NF4 | +3.10 % | +2.78 % | +5.94 % | +4.95 % |
248
- | NF4, `keep_precise="auto"` | +2.89 % | +2.59 % | +5.27 % | +4.27 % |
249
- | NF4, `error_rank=16` (LoftQ) | +3.06 % | +2.68 % | +5.76 % | +4.04 % |
250
- | NF4, `double_quant=True` | +2.79 % | +2.59 % | +5.96 % | +4.95 % |
251
- | NVFP4, weights only (bf16 matmuls) | +2.63 % | +2.27 % | +5.42 % | +5.03 % |
252
- | NVFP4 (tensor cores) | +5.19 % | +4.42 % | +10.86 % | +10.28 % |
253
- | NVFP4 with `outliers=0.01` | +4.78 % | +4.43 % | +8.99 % | +8.43 % |
254
- | NVFP4, `error_rank=16` (LoftQ) | +5.14 % | +4.67 % | +10.85 % | +8.76 % |
255
- | NVFP4, `nvfp4_backward=True` | +5.19 % | +4.65 % | +10.86 % | +10.02 % |
278
+ | int8 | −0.00 % | +0.07 % | +0.02 % | +0.02 % |
279
+ | FP8 (tensor cores) | +0.89 % | +0.80 % | +1.70 % | +1.40 % |
280
+ | NF4 | +3.10 % | +2.86 % | +5.94 % | +5.00 % |
281
+ | NF4, `keep_precise="auto"` | +2.89 % | +2.69 % | +5.27 % | +4.29 % |
282
+ | NF4, `error_rank=16` (LoftQ) | +3.06 % | +2.81 % | +5.76 % | +4.09 % |
283
+ | NF4, `double_quant=True` | +2.79 % | +2.65 % | +5.96 % | +4.95 % |
284
+ | NVFP4, weights only (bf16 matmuls) | +2.63 % | +2.30 % | +5.42 % | +5.02 % |
285
+ | NVFP4 (tensor cores) | +5.19 % | +4.54 % | +10.86 % | +10.17 % |
286
+ | NVFP4 with `outliers=0.01` | +4.78 % | +4.28 % | +8.99 % | +8.88 % |
287
+ | NVFP4, `error_rank=16` (LoftQ) | +5.14 % | +4.78 % | +10.85 % | +8.68 % |
288
+ | NVFP4, `nvfp4_backward=True` | +5.19 % | +5.02 % | +10.86 % | +10.08 % |
256
289
 
257
290
  The LoRA steps above use learning rate 2e-4. PiSSA needs a lower one, so it was measured at 2e-5, together with the formats it is compared with:
258
291
 
259
292
  | Format, LoRA at 2e-5 | Qwen2.5-14B | after 20 LoRA steps | Qwen2.5-1.5B | after 40 LoRA steps |
260
293
  |---|---|---|---|---|
261
- | NF4 | +3.10 % | +3.04 % | +5.94 % | +5.64 % |
262
- | NF4, `lora_init="pissa"` | +2.96 % | +1.94 % | +5.47 % | +4.57 % |
263
- | NVFP4 (tensor cores) | +5.19 % | +5.23 % | +10.86 % | +10.59 % |
264
- | NVFP4, `lora_init="pissa"` | +5.27 % | +4.03 % | +10.20 % | +8.81 % |
294
+ | NF4 | +3.10 % | +3.10 % | +5.94 % | +5.69 % |
295
+ | NF4, `lora_init="pissa"` | +2.96 % | +1.95 % | +5.47 % | +4.57 % |
296
+ | NVFP4 (tensor cores) | +5.19 % | +5.02 % | +10.86 % | +10.62 % |
297
+ | NVFP4, `lora_init="pissa"` | +5.27 % | +4.46 % | +10.20 % | +8.72 % |
265
298
 
266
- bf16 itself: 2.300 (14B) and 3.251 (1.5B) on 16 and 64 windows of 1,024 tokens. NVFP4's 4-bit weights alone cost less than NF4; its extra loss comes from quantizing the activations. On 1.5B the short fine-tune at 2e-4 raised bf16's own held-out perplexity (3.251 → 3.306), so compare the formats with each other there. The columns before fine-tuning repeat exactly from run to run. The ones after it don't, since training on the GPU isn't bit-exact: across the saved runs the NF4 formats moved by up to 0.2 points and the NVFP4 formats by up to about 1 point, so differences that small there are noise.
299
+ bf16 itself: 2.300 (14B) and 3.251 (1.5B) on 16 and 64 windows of 1,024 tokens. NVFP4's 4-bit weights alone cost less than NF4; its extra loss comes from quantizing the activations. On 1.5B the short fine-tune at 2e-4 raised bf16's own held-out perplexity (3.251 → 3.306), so compare the formats with each other there. The columns before fine-tuning repeat exactly from run to run. The ones after it don't, since training on the GPU isn't bit-exact: across the saved runs the NF4 formats moved by up to 0.2 points and the NVFP4 formats by up to about 1 point, so differences that small there are noise. In this check PiSSA's gain at 2e-5 was larger than that variation for NF4 on both models and for NVFP4 on 1.5B; for NVFP4 on 14B (+4.46 % against +5.02 %) it was within it (it was 1.20 points in 0.8.0's).
267
300
 
268
301
  Each engine runs in a fresh process with the same model, random-token data (the packed scenario: random documents of 64–2,048 tokens), LoRA settings (r=16, all projections), learning rate and loop. Each computes its loss its own recommended way: TrainStream in chunks from the hidden states, Liger with its fused linear cross-entropy, Unsloth with its own, the others through the model. Re-run with `benchmarks/compare.py` and `benchmarks/quality.py`.
269
302
 
270
303
  ## Results (RTX 5090 Laptop GPU 24 GB, 64 GB RAM)
271
304
 
272
- Measured with TrainStream 0.1–0.4, when each feature arrived: these rows show what fits and how, not today's speed (the comparison above is 0.8.0).
305
+ Measured with TrainStream 0.1–0.4, when each feature arrived: these rows show what fits and how, not today's speed (the comparison above is 0.8.2).
273
306
 
274
307
  | Workload | Stock PyTorch | TrainStream |
275
308
  |---|---|---|
@@ -291,7 +324,7 @@ Offloading costs throughput when a model already fits, which is why `offload="au
291
324
  - The CUDA toolkit matching PyTorch's CUDA major version, and a C++ compiler (Visual Studio 2022+ Build Tools with "Desktop development with C++" on Windows, GCC 11+ on Linux)
292
325
  - Optional, for the Hugging Face integration: `transformers`, `accelerate`, and `peft` for LoRA (all three come with `trainstream[hf]`)
293
326
 
294
- Tested on an RTX 5090 Laptop GPU with Windows 11 (PyTorch 2.14.1, CUDA 13.4, MSVC, Python 3.14), where all 457 tests pass with no warnings, and Ubuntu 24.04 under WSL2 (PyTorch 2.14.1, CUDA 13.3, GCC 13.3, Python 3.12), where all 457 pass as well (with bitsandbytes 0.50.2 installed for the NF4 comparison), both from a wheel installed with pip and with uv. Installs were checked with pip and with uv 0.13 (`uv pip install`, `uv sync` in a project, and building from the sdist) on both. Native Linux and other GPUs are untested.
327
+ Tested on an RTX 5090 Laptop GPU with Windows 11 (PyTorch 2.14.1, CUDA 13.4, MSVC, Python 3.14), where all 487 tests pass with no warnings, and Ubuntu 24.04 under WSL2 (PyTorch 2.14.1, CUDA 13.3, GCC 13.3, Python 3.12), where all 487 pass as well (with bitsandbytes 0.50.2 installed for the NF4 comparison), both from a wheel installed with pip and with uv. Installs were checked with pip and with uv 0.13 (`uv pip install`, `uv sync` in a project, and building from the sdist) on both. Native Linux and other GPUs are untested.
295
328
 
296
329
  ## Install
297
330
 
@@ -468,6 +501,7 @@ model._trainstream_session.adapting # True while it is still tuning itself
468
501
  | `offload` | `"auto"`, `True` | `"auto"` | `"auto"` switches to fused torch AdamW after 2 steps if the model fits |
469
502
  | `activation_offload` | `"auto"`, `True`, `False`, fraction 0.0–1.0 | `"auto"` | how many blocks' activations are paged to RAM; `"auto"`: measured in the first step, kept in VRAM if they fit under the VRAM target, otherwise paged as far as host RAM holds them (see `recompute` for the rest): at most 60 % of the RAM available, after what the pinned staging arena may still grow by, always leaving 10 % of physical RAM (at least 4 GiB) free; if available RAM later falls under that floor, half of the paged blocks are recomputed instead; `True`: all but the last block |
470
503
  | `param_offload` | `"auto"`, `True`, `False`, fraction 0.0–1.0, list of block indices | `"auto"` | `"auto"`: the memory planner decides which layers stay in VRAM |
504
+ | `embed_offload` | `"auto"`, `True`, `False` | `"auto"` | keep a frozen input embedding (a plain `nn.Embedding` not tied to the output layer) in pinned host RAM. Each forward gathers the batch's rows straight from there on the GPU (zero-copy reads over PCIe, about 10 KB per token for a 5,120-wide model), and the VRAM the table held (1.45 GiB for Qwen2.5-14B) goes to blocks and activations. The output is the same bits. `"auto"`: only when the memory plan would otherwise stream transformer blocks from RAM; `True`: whenever the embedding qualifies; `False`: never |
471
505
  | `quantize` | `"auto"`, `"none"`, `None`, `False`, `"int8"`, `"nf4"`, `"fp8"`, `"nvfp4"` | `"auto"` | frozen weights only; `"auto"` quantizes only when the run can't fit otherwise (int8 before NF4); fp8 / nvfp4 run on Blackwell tensor cores |
472
506
  | `quant_block_size` | even integer ≥ 2 | `64` | values per fp32 scale (int8 / nf4; fp8 and nvfp4 use fixed 32 / 16) |
473
507
  | `prefer` | `"exact"`, `"speed"` | `"exact"` | `"speed"` lets `quantize="auto"` choose NF4 when it keeps every layer in VRAM |
@@ -494,6 +528,7 @@ model._trainstream_session.adapting # True while it is still tuning itself
494
528
  | `moe_capacity` | `None`, integer ≥ 1 | `None` | with `moe_paging`: at most this many experts are copied in per MoE layer and pass. Beyond it the most-used ones are copied, and tokens routed to any other expert that is not in VRAM are re-routed: the router's top-k runs again over the allowed experts and its weights are renormalized as the router does. **This changes what the model computes**, in exchange for a bound on PCIe traffic; needs Hugging Face's top-k router next to the experts. `None`: no limit, the model's own routing always |
495
529
  | `host_arena` | `"auto"`, `False`, `None`, bytes | `"auto"` | page-locked host memory reserved up front, from which paged activations, staging and other pinned buffers are carved; `"auto"`: when the run streams anything, reserve one slab now and grow on demand within the page-lock budget; bytes: reserve that much now and cap growth there |
496
530
  | `peer` | `"auto"`, `False`, `None`, bytes | `"auto"` | data parallel on one machine: pool the frozen blocks every rank streams in the GPUs' VRAM and copy them GPU to GPU (needs peer access between the GPUs and CUDA IPC; set up collectively at step 10); `"auto"`: when peer copies reach 0.9× the host link, each GPU gives half the headroom under the memory plan's target; bytes: that much per GPU. While active, `unwrap()` is collective |
531
+ | `cuda_graphs` | `"auto"`, `True`, `False`, `None` | `"auto"` | run each block's forward and backward as two recorded CUDA graphs instead of launching their kernels one by one from Python. At small batches the host launches kernels more slowly than the GPU runs them; replayed graphs keep the GPU busy (and at a higher clock). The same kernels on the same memory: gradients are bit-identical. `"auto"`: recorded between two steps once TrainStream runs with nothing moving during a step (optimizer in VRAM, every block's weights in VRAM as bf16 or as fp8 / nvfp4 on tensor cores, activations kept, one process) and the blocks' inputs have had the same shapes for two steps. Anything else runs eagerly as before: no gradients, eval mode, other input shapes (recorded again once they hold, up to 4 recordings), two forwards before a backward, activation checkpointing, switched adapters (the full list is in `trainstream.graphs`). The graphs hold one step's activations in a memory pool of their own. A training forward through graphed blocks leaves a Hugging Face KV cache empty (training never reads it). `True`: the same, plus a warning when the run doesn't qualify; `False` / `None`: never |
497
532
  | `moment_dtype` | `"auto"`, `"float32"`, `"bfloat16"`, `"bf16"`, `torch.bfloat16`, `"int8"` | `"auto"` | fp32 unless that wouldn't fit in RAM, then bf16 (stochastic rounding), then 8-bit with the host optimizer; `"int8"`: one byte per moment value (see `OffloadAdamW`), a quarter of fp32's RAM |
498
533
  | `device` | `None`, `"cuda"`, `"cuda:N"`, `torch.device` | `None` | target GPU when the model is on the CPU |
499
534
  | `blocks` | `None`, list of layer modules | `None` | the repeated layer stack; auto-detected |
@@ -679,7 +714,7 @@ Methods and attributes:
679
714
  | `trainstream.read_checkpoint(path, keys=None)` | `keys`: which tensors to read (`None`: all; `[]`: none) | `{"manifest": dict, "tensors": {key: flat CPU tensor}}`; keys are `"slot/<n>/<part>"` (`master`, `exp_avg`, `exp_avg_sq`, `m_scale`, `v_scale`, `param_stage`, `param`, `step`) and `"steps"` |
680
715
  | `trainstream.pissa_to_lora(model, save_directory=None, adapter_name=None)` | see `quantize_model` above | the trained adapters as a LoRA for the original base weights |
681
716
 
682
- `memory_report()` keys added in 0.8: `"guard"` (`enabled`, `skipped_steps`, `kernel_flags`: which fused kernels saw a NaN / Inf since the last skipped step); `"disk"` when weights are on disk (`backend`: `"iocp"`, `"io_uring"` or `"threads"`, `lookahead`, `frozen_bytes`, `bytes_read`, `staged_bytes`, `stage_hits`, `copied_bytes`, `stream_wait_ms`, `slots`, `slot_bytes`, `ops`, `busy_ms`); in `"params"`, `disk_bytes` (bytes copied from disk staging); in `"activations"`, `"chunked"` (`tensors` fetched in pieces, `pieces`, `overlapped`: LoRA weight gradients that started while their input was still arriving); in `"experts"`, `experts_used`, `experts_copied`, `bytes_copied`, `cache_budget`, `cache_bytes`, `cache_hits`, `cache_misses`, `cache_evictions`, `prefetch_issued`, `prefetch_hits`, `prefetch_stopped_stacks`, `capacity`, `rerouted_layers`, `rerouted_tokens`; in `"optimizer"`, `nan_guard`, `skipped_steps`, `fp8_gradients`, `distributed_backend`, `ring`; `"mlp_front"` when gated MLPs can drop their gate / up outputs (`mode`, `engaged`: whether `"auto"` has switched on, `recomputed`: fronts run without keeping them, `kernel`: of those, computed by the fused FP4 kernel, `bytes_not_kept`, `fused_gemm`, `kernel_available`).
717
+ `memory_report()` keys added in 0.8: `"guard"` (`enabled`, `skipped_steps`, `kernel_flags`: which fused kernels saw a NaN / Inf since the last skipped step); `"disk"` when weights are on disk (`backend`: `"iocp"`, `"io_uring"` or `"threads"`, `lookahead`, `frozen_bytes`, `bytes_read`, `staged_bytes`, `stage_hits`, `copied_bytes`, `stream_wait_ms`, `slots`, `slot_bytes`, `ops`, `busy_ms`); in `"params"`, `disk_bytes` (bytes copied from disk staging); in `"activations"`, `"chunked"` (`tensors` fetched in pieces, `pieces`, `overlapped`: LoRA weight gradients that started while their input was still arriving); in `"experts"`, `experts_used`, `experts_copied`, `bytes_copied`, `cache_budget`, `cache_bytes`, `cache_hits`, `cache_misses`, `cache_evictions`, `prefetch_issued`, `prefetch_hits`, `prefetch_stopped_stacks`, `capacity`, `rerouted_layers`, `rerouted_tokens`; in `"optimizer"`, `nan_guard`, `skipped_steps`, `fp8_gradients`, `distributed_backend`, `ring`; `"mlp_front"` when gated MLPs can drop their gate / up outputs (`mode`, `engaged`: whether `"auto"` has switched on, `recomputed`: fronts run without keeping them, `kernel`: of those, computed by the fused FP4 kernel, `bytes_not_kept`, `fused_gemm`, `kernel_available`). Added in 0.8.1: `"embedding"` when the input embedding is kept in host RAM (`host_bytes`, `rows`). Added in 0.8.2: `"graphs"` unless `cuda_graphs=False` (`mode`; `state`: `"not started"`, `"watching"` (about to record), `"captured"` or `"off"`; `reason` it is off; `blocks`; `captures`: recordings so far; `capture_s`: seconds they took; `pool_bytes`: VRAM the recorded graphs hold for one step's activations; `replays`: block forwards run as graphs; `eager`: block calls that ran eagerly, counted by reason).
683
718
 
684
719
  The session behind a wrapped model is `model._trainstream_session` (`trainstream.TrainStreamSession`):
685
720
  - `.adapting`: `True` while it is still tuning itself. Meanwhile it may reset PyTorch's peak-memory counter (`torch.cuda.reset_peak_memory_stats`) to measure single steps; once it is `False`, the counter is left alone
@@ -9,6 +9,39 @@ model, optimizer = trainstream.wrap(model, optimizer)
9
9
 
10
10
  TrainStream is a C++/CUDA extension for PyTorch. It uses only the C++ standard library, the CUDA runtime and PyTorch's ATen: no DeepSpeed, no bitsandbytes. Updates match `torch.optim.AdamW` (max parameter difference ≈3·10⁻⁷ after 20 steps), and activation and weight paging are bit-exact.
11
11
 
12
+ ## What's new in 0.8.2
13
+
14
+ - **Transformer blocks run as CUDA graphs** (`cuda_graphs="auto"`). At small batches TrainStream's Python launched kernels more slowly than the GPU ran them: one training step of Qwen2.5-14B LoRA on an NVFP4 base at 512 tokens is about 10,600 kernels, and in a traced step the GPU stood idle between them for about 105 ms and, never fully busy, ran at a lower clock. Now, once a run has settled with nothing moving during a step (the optimizer in VRAM, every block's weights in VRAM as bf16 or as fp8 / nvfp4 on tensor cores, activations kept), each block's forward and backward are recorded once as two CUDA graphs and replayed from then on: the same kernels on the same memory, so gradients are bit-identical (checked on Qwen2.5-14B: the loss and all 672 LoRA gradients of a step). Measured on Qwen2.5-14B, LoRA r=16, NVFP4 base, batch 1 × 512, RTX 5090 Laptop GPU:
15
+
16
+ | | without graphs | with graphs |
17
+ |---|---|---|
18
+ | Windows, alternating runs (30 November, 3 each) | 0.509 / 0.515 / 0.521 s | 0.361 / 0.378 / 0.377 s (27–29 % faster) |
19
+ | Windows, full checks: 0.8.1's (30 November) and 0.8.2's (1 December) | 0.502 s, 1.24× bitsandbytes | 0.356 s, 1.61× bitsandbytes |
20
+ | WSL, the same two full checks | 0.451 s, 1.26× bitsandbytes | 0.360 s, 1.66× bitsandbytes |
21
+ | Qwen2.5-1.5B LoRA, batch 2 × 1024 (`benchmarks/quality.py`, Windows, 0.8.1's and 0.8.2's quality runs of 30 November): NVFP4 / FP8 / bf16 | 0.259 / 0.235 / 0.294 s | 0.206 / 0.210 / 0.283 s |
22
+
23
+ In 0.8.2's full check every rival ran 5–15 % slower than in 0.8.1's (bitsandbytes 0.570 → 0.599 s in WSL), so the step times understate the change; the ratios against bitsandbytes don't. Peak VRAM did not change (16.79 against 16.78 GB): the graphs keep one step's activations (1.26 GiB here) in a memory pool of their own, as the eager step held them. Where graphs do not engage, nothing changes: NF4 and int8 bases (their weights are expanded block by block on a copy stream beside the compute; inside a graph that expansion would run in line and cost more than graphs save), blocks streamed from RAM, activations paged or recomputed (in the benchmarks: 2,048 tokens, packed documents, the 12 GB cap), the host optimizer, data parallel. A forward whose inputs differ from the recorded ones, without gradients, in eval mode, under activation checkpointing, or before the previous forward's backward has run, runs eagerly as before. `memory_report()["graphs"]` says what happened and why; `cuda_graphs=False` turns it off.
24
+ - **Fix: Hugging Face gradient checkpointing with `attention="auto"`** (since 0.5). A layer that `gradient_checkpointing_enable()` runs again in backward ran outside the attention-backend order its forward used, so it could pick PyTorch's default kernel instead (on an RTX 50-series GPU, where cuDNN's attention is chosen, it did), and `torch.utils.checkpoint` stopped training with a `CheckpointError` about mismatched saved tensors. Blocks recomputed inside backward now re-enter their forward's order.
25
+
26
+ ## What's new in 0.8.1
27
+
28
+ Measured in the full check of 30 November 2026 on an RTX 5090 Laptop GPU (24 GB), in WSL2 (49 GB VM) and on Windows 11 (64 GB), against the 0.8.0 check of 23 November. All 465 tests pass on Windows and in WSL (from a wheel installed with pip and with uv), and perplexity before fine-tuning is the same as 0.8.0's in every format.
29
+
30
+ - **The input embedding moves to host RAM before any layer does** (`embed_offload="auto"`). A frozen input embedding is a lookup table: a step reads only its batch's rows (5 MB of Qwen2.5-14B's 1.45 GiB at 512 tokens). When the memory plan would stream transformer blocks from RAM, the table now goes to pinned host RAM instead, and a kernel reads the rows it needs straight from there (zero-copy, on the GPU's own stream, so nothing waits for the host). The output is the same bits. The VRAM it frees holds more blocks:
31
+
32
+ | Qwen2.5-14B LoRA | WSL, 0.8.0 → 0.8.1 | Windows, 0.8.0 → 0.8.1 | blocks in VRAM |
33
+ |---|---|---|---|
34
+ | NF4 base, 1 × 512, 12 GB of VRAM | 0.779 → 0.743 s | 0.730 → 0.710 s | 36–37 → 46–47 of 48 |
35
+ | NVFP4 base, 1 × 512, 12 GB of VRAM | 0.657 → 0.630 s | 0.569 → 0.670 s (see below) | 37 → 48 |
36
+ | `"auto"` (bf16 base), 1 × 512 | 0.968 → 0.838 s | 0.982 → 0.895 s | 28 → 31 |
37
+ | `"auto"`, 1 × 2,048 | 3.04 → 2.84 s | 3.04 → 2.82 s | 23–24 → 25–26 |
38
+ | `"auto"`, 1 × 512, 12 GB of VRAM | 2.13 → 1.89 s | 1.60 → 1.50 s | 9–10 → 12–13 |
39
+ | `"auto"`, 2 × 2,048 packed | 6.76 → 6.43 s | 6.75 → 6.61 s | 22 → 25 |
40
+
41
+ On Windows the GPU ran at a lower clock in this check than in 0.8.0's, for every engine: bitsandbytes was 9 % slower too, and in the uncapped NVFP4 run, which this change doesn't touch, the same FP4 matmuls took 391 ms per step against 258 ms (1,417 MHz against 2,400 MHz). Alternating runs with the option on and off in one session are the cleaner comparison there: NVFP4 base under the 12 GB cap 0.700 / 0.699 / 0.703 s → 0.669 / 0.671 / 0.670 s (4 % faster, 30 November); NF4 base 0.759 / 0.758 s → 0.681 / 0.686 s (10 % faster, 23 November).
42
+
43
+ Against Unsloth held to 12 GB of VRAM (WSL, 3 runs each): TrainStream's NF4 base takes 0.743 s against Unsloth's 0.685 s, so Unsloth is still 1.08× faster (1.10× in 0.8.0); the NVFP4 base stays ahead, 0.630 s (1.09×). Where everything already fits in VRAM (the 4-bit bases without a cap) the option doesn't engage and nothing changes. `embed_offload=False` keeps 0.8.0's behaviour.
44
+
12
45
  ## What's new in 0.8
13
46
 
14
47
  Measured in the full check of 23 November 2026 on an RTX 5090 Laptop GPU (24 GB), in WSL2 (49 GB VM) and on Windows 11 (64 GB), with the non-finite guard on (the new default). Every number is in the tables below.
@@ -122,40 +155,40 @@ Measured in the full check of 11 November 2026 on an RTX 5090 Laptop GPU (24 GB)
122
155
 
123
156
  ## Against other libraries (same laptop: WSL2 with a 49 GB VM, then Windows 11 with 64 GB)
124
157
 
125
- > TrainStream 0.8.0, full check of 23 November 2026. Each engine runs in a fresh process, 10 steps after its warm-up (TrainStream: after it has finished tuning itself), and its step time is the median of those. Where two compared engines were within 10 %, both ran 3 times and the median run counts; a win counts only when the gap is wider than the runs' spread. Every engine listed finished all its steps. Between full checks the laptop's speed moves by 5–10 % for every engine alike, so read smaller differences as noise.
158
+ > TrainStream 0.8.2: performance from the full check of 1 December 2026, tests and quality from those of 30 November and 1 December. Each engine runs in a fresh process, 10 steps after its warm-up (TrainStream: after it has finished tuning itself, and recording its CUDA graphs where it does), and its step time is the median of those. Where two compared engines were within 10 %, both ran 3 times and the median run counts; a win counts only when the gap is wider than the runs' spread. Every engine listed finished all its steps. Between full checks the laptop's speed moves by 5–15 % for every engine alike (in this one every rival ran 5–15 % slower than in 0.8.1's), so compare engines within a table, and read smaller differences as noise.
126
159
 
127
160
  **LoRA r=16, Qwen2.5-14B, batch 1 × 512 (WSL2)**
128
161
 
129
162
  | Engine | Step time | Peak VRAM | Against the fastest |
130
163
  |---|---|---|---|
131
- | TrainStream, NVFP4 base (FP4 tensor cores) | 0.462 s | 16.8 GB | fastest |
132
- | TrainStream, NF4 base | 0.560 s (3 runs: 0.554–0.572) | 16.8 GB | 1.21× |
133
- | bitsandbytes QLoRA (NF4) + PEFT | 0.589 s (3 runs: 0.577–0.605) | 17.4 GB | 1.27× |
134
- | Unsloth (4-bit) | 0.714 s | 11.5 GB | 1.55× |
135
- | bitsandbytes QLoRA + Liger kernels | 0.804 s | 12.0 GB | 1.74× |
136
- | bitsandbytes QLoRA + gradient checkpointing | 0.856 s | 12.7 GB | 1.85× |
137
- | TrainStream (auto: exact bf16 base, 20 of 48 layers streamed) | 0.968 s | 21.6 GB | 2.09× |
164
+ | TrainStream, NVFP4 base (FP4 tensor cores, CUDA graphs) | 0.360 s | 16.8 GB | fastest |
165
+ | TrainStream, NF4 base | 0.567 s (3 runs: 0.553–0.574) | 16.8 GB | 1.58× |
166
+ | bitsandbytes QLoRA (NF4) + PEFT | 0.599 s (3 runs: 0.596–0.608) | 17.4 GB | 1.66× |
167
+ | Unsloth (4-bit) | 0.767 s | 11.5 GB | 2.13× |
168
+ | bitsandbytes QLoRA + gradient checkpointing | 0.864 s | 12.7 GB | 2.40× |
169
+ | bitsandbytes QLoRA + Liger kernels | 0.873 s | 12.0 GB | 2.43× |
170
+ | TrainStream (auto: exact bf16 base, 17 of 48 layers streamed) | 0.948 s | 21.5 GB | 2.63× |
138
171
 
139
172
  **LoRA r=16, Qwen2.5-14B, batch 1 × 2048 (WSL2)**
140
173
 
141
174
  | Engine | Step time | Peak VRAM | Against the fastest |
142
175
  |---|---|---|---|
143
- | TrainStream, NVFP4 base | 1.39 s | 21.2 GB | fastest |
144
- | TrainStream, NF4 base | 1.99 s | 19.3 GB | 1.43× |
145
- | Unsloth (4-bit) | 2.48 s | 11.7 GB | 1.79× |
146
- | bitsandbytes QLoRA + Liger kernels | 2.62 s | 13.0 GB | 1.89× |
147
- | bitsandbytes QLoRA + gradient checkpointing | 2.80 s | 16.3 GB | 2.02× |
148
- | TrainStream (auto) | 3.04 s | 22.5 GB | 2.19× |
176
+ | TrainStream, NVFP4 base | 1.49 s | 21.2 GB | fastest |
177
+ | TrainStream, NF4 base | 2.40 s | 19.3 GB | 1.61× |
178
+ | Unsloth (4-bit) | 2.73 s | 11.7 GB | 1.83× |
179
+ | bitsandbytes QLoRA + Liger kernels | 2.90 s | 13.0 GB | 1.95× |
180
+ | bitsandbytes QLoRA + gradient checkpointing | 3.02 s | 16.3 GB | 2.02× |
181
+ | TrainStream (auto) | 3.12 s | 23.1 GB | 2.10× |
149
182
 
150
183
  **LoRA r=16, Qwen2.5-14B, batch 1 × 512, every engine held to 12 GB of VRAM (WSL2)**
151
184
 
152
185
  | Engine | Step time | Peak VRAM | Against the fastest |
153
186
  |---|---|---|---|
154
- | TrainStream, NVFP4 base | 0.657 s (3 runs: 0.655–0.705) | 11.8 GB | fastest |
155
- | Unsloth (4-bit) | 0.711 s (3 runs: 0.711–0.716) | 11.4 GB | 1.08× |
156
- | TrainStream, NF4 base | 0.779 s (3 runs: 0.774–0.795) | 11.6 GB | 1.18× |
157
- | bitsandbytes QLoRA + Liger kernels | 0.800 s | 12.0 GB | 1.22× |
158
- | TrainStream (auto) | 2.13 s | 11.7 GB | 3.23× |
187
+ | TrainStream, NVFP4 base | 0.575 s | 11.8 GB | fastest |
188
+ | TrainStream, NF4 base | 0.702 s (3 runs: 0.682–0.769) | 11.5 GB | 1.22× |
189
+ | Unsloth (4-bit) | 0.767 s (3 runs: 0.765–0.771) | 11.4 GB | 1.33× |
190
+ | bitsandbytes QLoRA + Liger kernels | 0.862 s | 12.0 GB | 1.50× |
191
+ | TrainStream (auto) | 2.08 s | 11.8 GB | 3.61× |
159
192
 
160
193
  bitsandbytes QLoRA, with or without gradient checkpointing, ran out of memory under the 12 GB cap in earlier runs, so the full check leaves it out.
161
194
 
@@ -163,11 +196,11 @@ bitsandbytes QLoRA, with or without gradient checkpointing, ran out of memory un
163
196
 
164
197
  | Engine | Step time | Peak VRAM | Against the fastest |
165
198
  |---|---|---|---|
166
- | TrainStream, NVFP4 base | 3.70 s | 19.9 GB | fastest |
167
- | TrainStream, NF4 base | 5.50 s (3 runs: 5.47–5.51) | 19.8 GB | 1.49× |
168
- | bitsandbytes QLoRA + Liger kernels | 5.99 s (3 runs: 5.98–5.99) | 14.4 GB | 1.62× |
169
- | bitsandbytes QLoRA + gradient checkpointing | 6.47 s | 21.0 GB | 1.75× |
170
- | TrainStream (auto) | 6.76 s | 21.2 GB | 1.83× |
199
+ | TrainStream, NVFP4 base | 3.98 s | 19.9 GB | fastest |
200
+ | TrainStream, NF4 base | 5.76 s | 19.8 GB | 1.45× |
201
+ | bitsandbytes QLoRA + Liger kernels | 6.40 s | 14.4 GB | 1.61× |
202
+ | bitsandbytes QLoRA + gradient checkpointing | 7.06 s | 21.0 GB | 1.77× |
203
+ | TrainStream (auto) | 7.26 s | 21.3 GB | 1.82× |
171
204
 
172
205
  Unsloth isn't compared here: it patches attention itself, so the harness can't check that attention stays inside each packed document.
173
206
 
@@ -175,61 +208,61 @@ Unsloth isn't compared here: it patches attention itself, so the harness can't c
175
208
 
176
209
  | Engine | Step time | Peak VRAM | Against the fastest |
177
210
  |---|---|---|---|
178
- | TrainStream (auto) | 1.09 s | 18.8 GB | fastest |
179
- | Stock PyTorch | 1.62 s | 32.4 GB | 1.48× (spills past VRAM) |
180
- | DeepSpeed ZeRO-2, optimizer offload | 1.76 s | 13.3 GB | 1.61× |
211
+ | TrainStream (auto) | 1.13 s | 18.8 GB | fastest |
212
+ | Stock PyTorch | 1.63 s | 32.4 GB | 1.45× (spills past VRAM) |
213
+ | DeepSpeed ZeRO-2, optimizer offload | 1.81 s | 13.3 GB | 1.61× |
181
214
 
182
215
  **The same on Windows 11 (64 GB RAM; Unsloth, Liger and DeepSpeed aren't installed there)**
183
216
 
184
217
  | Engine | LoRA 14B, 1 × 512 | LoRA 14B, 1 × 2048 | 14B, 12 GB VRAM | 14B, packed | Full fine-tune 1.5B |
185
218
  |---|---|---|---|---|---|
186
- | TrainStream, NVFP4 base | **0.394 s** | **1.40 s** | **0.569 s** | **3.73 s** | — |
187
- | TrainStream, NF4 base | 0.521 s | 2.00 s | 0.730 s | 5.40 s | — |
188
- | TrainStream (auto) | 0.982 s | 3.04 s | 1.60 s | 6.75 s | **1.09 s** |
189
- | bitsandbytes QLoRA (NF4) + PEFT | 0.571 s | spills past VRAM | out of memory | left out (hit the time limit in an earlier run) | — |
190
- | bitsandbytes QLoRA + gradient checkpointing | 0.822 s | 2.99 s | out of memory | 6.62 s | — |
191
- | Stock PyTorch | spills past VRAM | spills past VRAM | — | — | 1.72 s (spills) |
192
-
193
- - With an NVFP4 base TrainStream had the fastest step in every 14B scenario, in WSL and on Windows, including under the 12 GB cap. NVFP4 costs quality (see the perplexity table below).
194
- - With an NF4 base, the format bitsandbytes and Unsloth use, TrainStream was ahead of every rival in every scenario except under the 12 GB cap, where Unsloth was 1.10× faster: there TrainStream streams 12 of the 48 4-bit layers from RAM and Unsloth keeps them all in VRAM. At 512 tokens its lead over bitsandbytes is small (1.05× in WSL, 1.09× on Windows).
219
+ | TrainStream, NVFP4 base | **0.356 s** | **1.48 s** | **0.560 s** | **3.80 s** | — |
220
+ | TrainStream, NF4 base | 0.542 s | 2.18 s | 0.642 s | 5.87 s | — |
221
+ | TrainStream (auto) | 1.02 s | 3.08 s | 1.50 s | 6.98 s | **1.08 s** |
222
+ | bitsandbytes QLoRA (NF4) + PEFT | 0.572 s | spills past VRAM | out of memory | left out (hit the time limit in an earlier run) | — |
223
+ | bitsandbytes QLoRA + gradient checkpointing | 0.877 s | 3.13 s | out of memory | 6.99 s | — |
224
+ | Stock PyTorch | spills past VRAM | spills past VRAM | — | — | 2.40 s (spills) |
225
+
226
+ - With an NVFP4 base TrainStream had the fastest step in every 14B scenario, in WSL and on Windows. At 512 tokens its blocks now run as CUDA graphs: 1.66× bitsandbytes in WSL and 1.61× on Windows (1.26× and 1.24× in 0.8.1's check). NVFP4 costs quality (see the perplexity table below).
227
+ - With an NF4 base, the format bitsandbytes and Unsloth use, TrainStream was ahead of every rival in every scenario. Held to 12 GB of VRAM its median run was 1.09× Unsloth's (0.702 s against 0.767 s), though the slowest of its three runs (0.769 s) was level with Unsloth's (in 0.8.1's check Unsloth was 1.08× faster). At 512 tokens its lead over bitsandbytes is small (1.06× in WSL and on Windows): NF4 blocks don't run as graphs.
195
228
  - When it has room, TrainStream holds more in VRAM than Unsloth (16.8 GB against 11.5 GB at 512 tokens) because it plans up to a VRAM target; held to 12 GB it fits like Unsloth does.
196
- - `"auto"` with 49–64 GB of RAM keeps the 14B base exact in bf16 and streams 20–26 of its 48 layers from RAM. That is slower than the 4-bit engines and changes no weight.
197
- - The full fine-tune: TrainStream keeps its activations in VRAM and stays at 18.8 GB, 1.48× faster than stock PyTorch in WSL and 1.58× on Windows. Stock PyTorch peaks at 32.4 GB, more than the GPU's 24 GB, so its step time depends on how the driver spills into system RAM.
229
+ - `"auto"` with 49–64 GB of RAM keeps the 14B base exact in bf16 and streams 17–23 of its 48 layers from RAM (35–36 under the 12 GB cap). That is slower than the 4-bit engines and changes no weight.
230
+ - The full fine-tune: TrainStream keeps its activations in VRAM and stays at 18.8 GB, 1.45× faster than stock PyTorch in WSL. Stock PyTorch peaks at 32.4 GB, more than the GPU's 24 GB, so its step time depends on how the driver spills into system RAM (2.40 s on Windows in this run, 1.45 s in 0.8.1's).
198
231
  - Stock PyTorch and DeepSpeed in the 14B scenarios, and bitsandbytes without checkpointing at 2,048 tokens, don't fit in 24 GB of VRAM: in earlier runs the driver spilled into system RAM at 46–371 s per step, so the full check leaves them out.
199
- - In three places TrainStream was clearly slower in WSL than on Windows: the NVFP4 base at 512 tokens by 17 % (0.462 s against 0.394 s; the GPU drew 111 W against 170 W), and under the 12 GB cap the NVFP4 base by 16 % (WSL paged the activations, Windows recomputed them) and `"auto"` by 33 %. Everywhere else every engine was within 7 % of its time in the other environment.
232
+ - Between the two environments every engine was within 10 % of its time in the other, except `"auto"` under the 12 GB cap, 1.39× slower in WSL (2.08 s against 1.50 s), and stock PyTorch's full fine-tune, which spills.
200
233
 
201
234
  **Perplexity against bf16** (`benchmarks/quality.py`, held-out Python standard-library source; lower is better)
202
235
 
203
236
  | Format | Qwen2.5-14B | after 20 LoRA steps | Qwen2.5-1.5B | after 40 LoRA steps |
204
237
  |---|---|---|---|---|
205
- | int8 | −0.00 % | −0.03 % | +0.02 % | −0.00 % |
206
- | FP8 (tensor cores) | +0.89 % | +0.79 % | +1.70 % | +1.43 % |
207
- | NF4 | +3.10 % | +2.78 % | +5.94 % | +4.95 % |
208
- | NF4, `keep_precise="auto"` | +2.89 % | +2.59 % | +5.27 % | +4.27 % |
209
- | NF4, `error_rank=16` (LoftQ) | +3.06 % | +2.68 % | +5.76 % | +4.04 % |
210
- | NF4, `double_quant=True` | +2.79 % | +2.59 % | +5.96 % | +4.95 % |
211
- | NVFP4, weights only (bf16 matmuls) | +2.63 % | +2.27 % | +5.42 % | +5.03 % |
212
- | NVFP4 (tensor cores) | +5.19 % | +4.42 % | +10.86 % | +10.28 % |
213
- | NVFP4 with `outliers=0.01` | +4.78 % | +4.43 % | +8.99 % | +8.43 % |
214
- | NVFP4, `error_rank=16` (LoftQ) | +5.14 % | +4.67 % | +10.85 % | +8.76 % |
215
- | NVFP4, `nvfp4_backward=True` | +5.19 % | +4.65 % | +10.86 % | +10.02 % |
238
+ | int8 | −0.00 % | +0.07 % | +0.02 % | +0.02 % |
239
+ | FP8 (tensor cores) | +0.89 % | +0.80 % | +1.70 % | +1.40 % |
240
+ | NF4 | +3.10 % | +2.86 % | +5.94 % | +5.00 % |
241
+ | NF4, `keep_precise="auto"` | +2.89 % | +2.69 % | +5.27 % | +4.29 % |
242
+ | NF4, `error_rank=16` (LoftQ) | +3.06 % | +2.81 % | +5.76 % | +4.09 % |
243
+ | NF4, `double_quant=True` | +2.79 % | +2.65 % | +5.96 % | +4.95 % |
244
+ | NVFP4, weights only (bf16 matmuls) | +2.63 % | +2.30 % | +5.42 % | +5.02 % |
245
+ | NVFP4 (tensor cores) | +5.19 % | +4.54 % | +10.86 % | +10.17 % |
246
+ | NVFP4 with `outliers=0.01` | +4.78 % | +4.28 % | +8.99 % | +8.88 % |
247
+ | NVFP4, `error_rank=16` (LoftQ) | +5.14 % | +4.78 % | +10.85 % | +8.68 % |
248
+ | NVFP4, `nvfp4_backward=True` | +5.19 % | +5.02 % | +10.86 % | +10.08 % |
216
249
 
217
250
  The LoRA steps above use learning rate 2e-4. PiSSA needs a lower one, so it was measured at 2e-5, together with the formats it is compared with:
218
251
 
219
252
  | Format, LoRA at 2e-5 | Qwen2.5-14B | after 20 LoRA steps | Qwen2.5-1.5B | after 40 LoRA steps |
220
253
  |---|---|---|---|---|
221
- | NF4 | +3.10 % | +3.04 % | +5.94 % | +5.64 % |
222
- | NF4, `lora_init="pissa"` | +2.96 % | +1.94 % | +5.47 % | +4.57 % |
223
- | NVFP4 (tensor cores) | +5.19 % | +5.23 % | +10.86 % | +10.59 % |
224
- | NVFP4, `lora_init="pissa"` | +5.27 % | +4.03 % | +10.20 % | +8.81 % |
254
+ | NF4 | +3.10 % | +3.10 % | +5.94 % | +5.69 % |
255
+ | NF4, `lora_init="pissa"` | +2.96 % | +1.95 % | +5.47 % | +4.57 % |
256
+ | NVFP4 (tensor cores) | +5.19 % | +5.02 % | +10.86 % | +10.62 % |
257
+ | NVFP4, `lora_init="pissa"` | +5.27 % | +4.46 % | +10.20 % | +8.72 % |
225
258
 
226
- bf16 itself: 2.300 (14B) and 3.251 (1.5B) on 16 and 64 windows of 1,024 tokens. NVFP4's 4-bit weights alone cost less than NF4; its extra loss comes from quantizing the activations. On 1.5B the short fine-tune at 2e-4 raised bf16's own held-out perplexity (3.251 → 3.306), so compare the formats with each other there. The columns before fine-tuning repeat exactly from run to run. The ones after it don't, since training on the GPU isn't bit-exact: across the saved runs the NF4 formats moved by up to 0.2 points and the NVFP4 formats by up to about 1 point, so differences that small there are noise.
259
+ bf16 itself: 2.300 (14B) and 3.251 (1.5B) on 16 and 64 windows of 1,024 tokens. NVFP4's 4-bit weights alone cost less than NF4; its extra loss comes from quantizing the activations. On 1.5B the short fine-tune at 2e-4 raised bf16's own held-out perplexity (3.251 → 3.306), so compare the formats with each other there. The columns before fine-tuning repeat exactly from run to run. The ones after it don't, since training on the GPU isn't bit-exact: across the saved runs the NF4 formats moved by up to 0.2 points and the NVFP4 formats by up to about 1 point, so differences that small there are noise. In this check PiSSA's gain at 2e-5 was larger than that variation for NF4 on both models and for NVFP4 on 1.5B; for NVFP4 on 14B (+4.46 % against +5.02 %) it was within it (it was 1.20 points in 0.8.0's).
227
260
 
228
261
  Each engine runs in a fresh process with the same model, random-token data (the packed scenario: random documents of 64–2,048 tokens), LoRA settings (r=16, all projections), learning rate and loop. Each computes its loss its own recommended way: TrainStream in chunks from the hidden states, Liger with its fused linear cross-entropy, Unsloth with its own, the others through the model. Re-run with `benchmarks/compare.py` and `benchmarks/quality.py`.
229
262
 
230
263
  ## Results (RTX 5090 Laptop GPU 24 GB, 64 GB RAM)
231
264
 
232
- Measured with TrainStream 0.1–0.4, when each feature arrived: these rows show what fits and how, not today's speed (the comparison above is 0.8.0).
265
+ Measured with TrainStream 0.1–0.4, when each feature arrived: these rows show what fits and how, not today's speed (the comparison above is 0.8.2).
233
266
 
234
267
  | Workload | Stock PyTorch | TrainStream |
235
268
  |---|---|---|
@@ -251,7 +284,7 @@ Offloading costs throughput when a model already fits, which is why `offload="au
251
284
  - The CUDA toolkit matching PyTorch's CUDA major version, and a C++ compiler (Visual Studio 2022+ Build Tools with "Desktop development with C++" on Windows, GCC 11+ on Linux)
252
285
  - Optional, for the Hugging Face integration: `transformers`, `accelerate`, and `peft` for LoRA (all three come with `trainstream[hf]`)
253
286
 
254
- Tested on an RTX 5090 Laptop GPU with Windows 11 (PyTorch 2.14.1, CUDA 13.4, MSVC, Python 3.14), where all 457 tests pass with no warnings, and Ubuntu 24.04 under WSL2 (PyTorch 2.14.1, CUDA 13.3, GCC 13.3, Python 3.12), where all 457 pass as well (with bitsandbytes 0.50.2 installed for the NF4 comparison), both from a wheel installed with pip and with uv. Installs were checked with pip and with uv 0.13 (`uv pip install`, `uv sync` in a project, and building from the sdist) on both. Native Linux and other GPUs are untested.
287
+ Tested on an RTX 5090 Laptop GPU with Windows 11 (PyTorch 2.14.1, CUDA 13.4, MSVC, Python 3.14), where all 487 tests pass with no warnings, and Ubuntu 24.04 under WSL2 (PyTorch 2.14.1, CUDA 13.3, GCC 13.3, Python 3.12), where all 487 pass as well (with bitsandbytes 0.50.2 installed for the NF4 comparison), both from a wheel installed with pip and with uv. Installs were checked with pip and with uv 0.13 (`uv pip install`, `uv sync` in a project, and building from the sdist) on both. Native Linux and other GPUs are untested.
255
288
 
256
289
  ## Install
257
290
 
@@ -428,6 +461,7 @@ model._trainstream_session.adapting # True while it is still tuning itself
428
461
  | `offload` | `"auto"`, `True` | `"auto"` | `"auto"` switches to fused torch AdamW after 2 steps if the model fits |
429
462
  | `activation_offload` | `"auto"`, `True`, `False`, fraction 0.0–1.0 | `"auto"` | how many blocks' activations are paged to RAM; `"auto"`: measured in the first step, kept in VRAM if they fit under the VRAM target, otherwise paged as far as host RAM holds them (see `recompute` for the rest): at most 60 % of the RAM available, after what the pinned staging arena may still grow by, always leaving 10 % of physical RAM (at least 4 GiB) free; if available RAM later falls under that floor, half of the paged blocks are recomputed instead; `True`: all but the last block |
430
463
  | `param_offload` | `"auto"`, `True`, `False`, fraction 0.0–1.0, list of block indices | `"auto"` | `"auto"`: the memory planner decides which layers stay in VRAM |
464
+ | `embed_offload` | `"auto"`, `True`, `False` | `"auto"` | keep a frozen input embedding (a plain `nn.Embedding` not tied to the output layer) in pinned host RAM. Each forward gathers the batch's rows straight from there on the GPU (zero-copy reads over PCIe, about 10 KB per token for a 5,120-wide model), and the VRAM the table held (1.45 GiB for Qwen2.5-14B) goes to blocks and activations. The output is the same bits. `"auto"`: only when the memory plan would otherwise stream transformer blocks from RAM; `True`: whenever the embedding qualifies; `False`: never |
431
465
  | `quantize` | `"auto"`, `"none"`, `None`, `False`, `"int8"`, `"nf4"`, `"fp8"`, `"nvfp4"` | `"auto"` | frozen weights only; `"auto"` quantizes only when the run can't fit otherwise (int8 before NF4); fp8 / nvfp4 run on Blackwell tensor cores |
432
466
  | `quant_block_size` | even integer ≥ 2 | `64` | values per fp32 scale (int8 / nf4; fp8 and nvfp4 use fixed 32 / 16) |
433
467
  | `prefer` | `"exact"`, `"speed"` | `"exact"` | `"speed"` lets `quantize="auto"` choose NF4 when it keeps every layer in VRAM |
@@ -454,6 +488,7 @@ model._trainstream_session.adapting # True while it is still tuning itself
454
488
  | `moe_capacity` | `None`, integer ≥ 1 | `None` | with `moe_paging`: at most this many experts are copied in per MoE layer and pass. Beyond it the most-used ones are copied, and tokens routed to any other expert that is not in VRAM are re-routed: the router's top-k runs again over the allowed experts and its weights are renormalized as the router does. **This changes what the model computes**, in exchange for a bound on PCIe traffic; needs Hugging Face's top-k router next to the experts. `None`: no limit, the model's own routing always |
455
489
  | `host_arena` | `"auto"`, `False`, `None`, bytes | `"auto"` | page-locked host memory reserved up front, from which paged activations, staging and other pinned buffers are carved; `"auto"`: when the run streams anything, reserve one slab now and grow on demand within the page-lock budget; bytes: reserve that much now and cap growth there |
456
490
  | `peer` | `"auto"`, `False`, `None`, bytes | `"auto"` | data parallel on one machine: pool the frozen blocks every rank streams in the GPUs' VRAM and copy them GPU to GPU (needs peer access between the GPUs and CUDA IPC; set up collectively at step 10); `"auto"`: when peer copies reach 0.9× the host link, each GPU gives half the headroom under the memory plan's target; bytes: that much per GPU. While active, `unwrap()` is collective |
491
+ | `cuda_graphs` | `"auto"`, `True`, `False`, `None` | `"auto"` | run each block's forward and backward as two recorded CUDA graphs instead of launching their kernels one by one from Python. At small batches the host launches kernels more slowly than the GPU runs them; replayed graphs keep the GPU busy (and at a higher clock). The same kernels on the same memory: gradients are bit-identical. `"auto"`: recorded between two steps once TrainStream runs with nothing moving during a step (optimizer in VRAM, every block's weights in VRAM as bf16 or as fp8 / nvfp4 on tensor cores, activations kept, one process) and the blocks' inputs have had the same shapes for two steps. Anything else runs eagerly as before: no gradients, eval mode, other input shapes (recorded again once they hold, up to 4 recordings), two forwards before a backward, activation checkpointing, switched adapters (the full list is in `trainstream.graphs`). The graphs hold one step's activations in a memory pool of their own. A training forward through graphed blocks leaves a Hugging Face KV cache empty (training never reads it). `True`: the same, plus a warning when the run doesn't qualify; `False` / `None`: never |
457
492
  | `moment_dtype` | `"auto"`, `"float32"`, `"bfloat16"`, `"bf16"`, `torch.bfloat16`, `"int8"` | `"auto"` | fp32 unless that wouldn't fit in RAM, then bf16 (stochastic rounding), then 8-bit with the host optimizer; `"int8"`: one byte per moment value (see `OffloadAdamW`), a quarter of fp32's RAM |
458
493
  | `device` | `None`, `"cuda"`, `"cuda:N"`, `torch.device` | `None` | target GPU when the model is on the CPU |
459
494
  | `blocks` | `None`, list of layer modules | `None` | the repeated layer stack; auto-detected |
@@ -639,7 +674,7 @@ Methods and attributes:
639
674
  | `trainstream.read_checkpoint(path, keys=None)` | `keys`: which tensors to read (`None`: all; `[]`: none) | `{"manifest": dict, "tensors": {key: flat CPU tensor}}`; keys are `"slot/<n>/<part>"` (`master`, `exp_avg`, `exp_avg_sq`, `m_scale`, `v_scale`, `param_stage`, `param`, `step`) and `"steps"` |
640
675
  | `trainstream.pissa_to_lora(model, save_directory=None, adapter_name=None)` | see `quantize_model` above | the trained adapters as a LoRA for the original base weights |
641
676
 
642
- `memory_report()` keys added in 0.8: `"guard"` (`enabled`, `skipped_steps`, `kernel_flags`: which fused kernels saw a NaN / Inf since the last skipped step); `"disk"` when weights are on disk (`backend`: `"iocp"`, `"io_uring"` or `"threads"`, `lookahead`, `frozen_bytes`, `bytes_read`, `staged_bytes`, `stage_hits`, `copied_bytes`, `stream_wait_ms`, `slots`, `slot_bytes`, `ops`, `busy_ms`); in `"params"`, `disk_bytes` (bytes copied from disk staging); in `"activations"`, `"chunked"` (`tensors` fetched in pieces, `pieces`, `overlapped`: LoRA weight gradients that started while their input was still arriving); in `"experts"`, `experts_used`, `experts_copied`, `bytes_copied`, `cache_budget`, `cache_bytes`, `cache_hits`, `cache_misses`, `cache_evictions`, `prefetch_issued`, `prefetch_hits`, `prefetch_stopped_stacks`, `capacity`, `rerouted_layers`, `rerouted_tokens`; in `"optimizer"`, `nan_guard`, `skipped_steps`, `fp8_gradients`, `distributed_backend`, `ring`; `"mlp_front"` when gated MLPs can drop their gate / up outputs (`mode`, `engaged`: whether `"auto"` has switched on, `recomputed`: fronts run without keeping them, `kernel`: of those, computed by the fused FP4 kernel, `bytes_not_kept`, `fused_gemm`, `kernel_available`).
677
+ `memory_report()` keys added in 0.8: `"guard"` (`enabled`, `skipped_steps`, `kernel_flags`: which fused kernels saw a NaN / Inf since the last skipped step); `"disk"` when weights are on disk (`backend`: `"iocp"`, `"io_uring"` or `"threads"`, `lookahead`, `frozen_bytes`, `bytes_read`, `staged_bytes`, `stage_hits`, `copied_bytes`, `stream_wait_ms`, `slots`, `slot_bytes`, `ops`, `busy_ms`); in `"params"`, `disk_bytes` (bytes copied from disk staging); in `"activations"`, `"chunked"` (`tensors` fetched in pieces, `pieces`, `overlapped`: LoRA weight gradients that started while their input was still arriving); in `"experts"`, `experts_used`, `experts_copied`, `bytes_copied`, `cache_budget`, `cache_bytes`, `cache_hits`, `cache_misses`, `cache_evictions`, `prefetch_issued`, `prefetch_hits`, `prefetch_stopped_stacks`, `capacity`, `rerouted_layers`, `rerouted_tokens`; in `"optimizer"`, `nan_guard`, `skipped_steps`, `fp8_gradients`, `distributed_backend`, `ring`; `"mlp_front"` when gated MLPs can drop their gate / up outputs (`mode`, `engaged`: whether `"auto"` has switched on, `recomputed`: fronts run without keeping them, `kernel`: of those, computed by the fused FP4 kernel, `bytes_not_kept`, `fused_gemm`, `kernel_available`). Added in 0.8.1: `"embedding"` when the input embedding is kept in host RAM (`host_bytes`, `rows`). Added in 0.8.2: `"graphs"` unless `cuda_graphs=False` (`mode`; `state`: `"not started"`, `"watching"` (about to record), `"captured"` or `"off"`; `reason` it is off; `blocks`; `captures`: recordings so far; `capture_s`: seconds they took; `pool_bytes`: VRAM the recorded graphs hold for one step's activations; `replays`: block forwards run as graphs; `eager`: block calls that ran eagerly, counted by reason).
643
678
 
644
679
  The session behind a wrapped model is `model._trainstream_session` (`trainstream.TrainStreamSession`):
645
680
  - `.adapting`: `True` while it is still tuning itself. Meanwhile it may reset PyTorch's peak-memory counter (`torch.cuda.reset_peak_memory_stats`) to measure single steps; once it is `False`, the counter is left alone
@@ -87,7 +87,7 @@ with open(os.path.join(HERE, "README.md"), encoding="utf-8") as fh:
87
87
 
88
88
  setup(
89
89
  name="trainstream",
90
- version="0.8.0",
90
+ version="0.8.2",
91
91
  author="John Anish G",
92
92
  license="Apache-2.0",
93
93
  description="Zero-dependency PyTorch training offload: optimizer, gradients, activations and weights to host RAM, with a memory planner and built-in int8/NF4/FP8/NVFP4 quantization.",
@@ -156,3 +156,27 @@ def test_masked_inputs_keep_pytorchs_order():
156
156
  assert all(b._ts_sdpa_order is not None for b in session.attention.blocks)
157
157
  finally:
158
158
  trainstream.unwrap(model)
159
+
160
+
161
+ @pytest.mark.parametrize("reentrant", [False, True])
162
+ def test_hugging_face_gradient_checkpointing_recomputes_with_the_same_backend(reentrant):
163
+ # Hugging Face runs a checkpointed layer again inside backward, outside the model's forward
164
+ # and the backend order it set: the layer must re-enter that order, or the recomputation picks
165
+ # PyTorch's default kernel and torch.utils.checkpoint refuses its differently shaped saved
166
+ # tensors (cuDNN's RNG state against the memory-efficient kernel's)
167
+ if "cudnn" not in time_backends(1, 4, 256, 64, torch.bfloat16, DEV):
168
+ pytest.skip("no cuDNN attention here")
169
+ base = tiny_llama()
170
+ losses = {}
171
+ for gc in (False, True):
172
+ m = copy.deepcopy(base)
173
+ if gc:
174
+ m.gradient_checkpointing_enable(gradient_checkpointing_kwargs={"use_reentrant": reentrant})
175
+ m.train()
176
+ m, opt = trainstream.wrap(m, torch.optim.AdamW(m.parameters(), lr=1e-3), offload=True, attention="cudnn",
177
+ profile=False)
178
+ try:
179
+ losses[gc] = [step(m, opt, x) for x in ids(3)]
180
+ finally:
181
+ trainstream.unwrap(m)
182
+ assert losses[True] == pytest.approx(losses[False], rel=1e-3)