β‘ Viking Engine β BoneMemory
Rewritten Memory Manager for ostris/ai-toolkit
Double-buffered async CUDA Β· bf16 precision Β· Sub-linear rank scaling
Chernihiv, Ukraine πΊπ¦
β οΈ REQUIRES TWO FILES
Results below are only achievable when both files are replaced:
File Purpose toolkit/manager_modules.pyd β‘ Async double-buffer engine jobs/process/BaseSDTrainProcess.py π― bf16 precision patch One without the other will not produce these results.
What This Solves
Standard ai-toolkit layer offloading is sequential:
GPU computes layer N ββββββββββββββββ
Transfer weights N+1 ββββββββ
GPU computes layer N+1 ββββββββ
β GPU idle β
At rank 1024: weight matrices = hundreds of MB per layer. Sequential transfer = 179 sec/iter. Full training = 100+ hours.
Viking Engine makes transfer invisible:
GPU computes layer N ββββββββ
Transfer weights N+1 ββββββββ β parallel CUDA stream
GPU computes layer N+1 ββββββββ
Transfer disappears from the profiler. Completely.
β‘ Benchmark & Performance Verification (FLUX.2-Dev / RTX 4090)
| LoRA Configuration | Speed (s/it) | VRAM Memory Status | |
|---|---|---|---|
| Rank 128 (Optimized) | 6.70s / 6.50s | 24 GB (Zero OOM / Stable) | |
| Rank 512 (Deep Gesture) | 8.97s | 24 GB (Double Buffered) | |
| Rank 1024 (Extreme) | 22.45s | 24 GB (Full 8-bit Stack Forced) | |
| Rank 1280 (Extreme) | 65.80s | 24 GB (Full 8-bit Stack Forced) |
Benchmark - Flux2-dev, RTX 4090, Rank 128
First Benchmark β Flux2-dev (32B params) Β· RTX 4090
| Rank | Trainable Params | Before | After | Speedup |
|---|---|---|---|---|
| 16 | 97,517,568 | baseline | 5.92 s/it | β |
| 32 | ~200,000,000 | ~12 s/it | 6.57 s/it | 1.8Γ |
| 512 | 3,120,562,176 | ~179 s/it | ~14 s/it | 12Γ |
| 1024 | 6,241,124,352 | OOM / 179 s/it | ~31 s/it | β / 5.8Γ |
6.24 billion trainable parameters on a single RTX 4090.
Zero OOM. Zero crashes. 19.5% of entire Flux2 model trained simultaneously.
Speed ββdemonstration: (Rank 512)
Sub-linear scaling
Parameters Γ32 β Speed Γ2.4 only (rank 16 β 512)
Parameters Γ64 β Speed Γ5.2 only (rank 16 β 1024)
The engine becomes more efficient as rank increases.
Higher rank = longer GPU compute = more time to hide transfer latency.
Real log β rank 1024 stabilization:
step 1 β 136.94 s/it (cold start)
step 10 β 40.51 s/it (pipeline filling)
step 30 β 33.39 s/it
step 50 β 31.96 s/it
step 100 β 31.21 s/it β stable β
Iteration profile at rank 32:
backward: 3.85s β
predict_unet: 2.01s β
optimizer_step: 0.08s β
transfer: 0.00s β hidden inside compute β
Architecture
Two engines in one file
Engine A β Direct path (rank β€ 32):
# Overhead from Events > transfer time at small ranks
# Direct non-blocking wins
w = self.m.weight.to(device, non_blocking=True)
return F.linear(x, _dequant(w, dtype), b)
Engine B β Double-buffered async (rank β₯ 512):
The code is closed, but it continues to work and fly at supersonic speeds
bf16 precision patch β one line, 2.7Γ speedup at rank 512:
# Viking method β before network.apply_to()
# todo switch everything to proper mixed precision like this
self.network.force_to(self.device_torch, dtype=torch.bfloat16)
Forces LoRA matrices float32 β bfloat16. Weight size halved.
DMA transfer halved. Overlap efficiency increased.
Additional optimizations:
- CPU pinned memory β GPU DMA reads directly from DRAM, no cache copy
- Smart text encoder orchestration β Mistral-24B loads, encodes, unloads before training starts
- sm_89 support β correct FMA values for RTX 4090 (Ada Lovelace)
- High-priority CUDA streams β compute stream never waits in queue
Works With All ai-toolkit Models
Flux2 (32B) was the test model β heaviest available, worst-case benchmark.
- β FLUX.1 / FLUX.2
- β Stable Diffusion 1.x / 2.x / 3 / 3.5
- β SDXL
- β Video models (Wan, HunyuanVideo, etc.)
- β Any future model in ai-toolkit
Installation
git clone https://github.com/ostris/ai-toolkit
cd ai-toolkit
pip install -r requirements.txt
# Backup originals
cp toolkit/manager_modules.py toolkit/manager_modules_BACKUP.py
cp jobs/process/BaseSDTrainProcess.py jobs/process/BaseSDTrainProcess_BACKUP.py
# Place Viking Engine files from this repo
In BaseSDTrainProcess.py find line ~1778, add before network.apply_to():
self.network.force_to(self.device_torch, dtype=torch.bfloat16)
Recommended config (rank 32 β balanced):
network:
type: lora
linear: 32
linear_alpha: 64
conv: 32
conv_alpha: 64
lokr_full_rank: true
lokr_factor: -1
Expected on RTX 4090 + Flux2: 5.92 β 6.57 sec/iter
Server Scalability
# Consumer (this repo):
w = weight_cpu.to(device, non_blocking=True)
# NVLink cluster β same pattern:
w = weight_gpu0.to(device_1, non_blocking=True)
Same double-buffering. Same CUDA Streams. Same Events.
Maps directly to tensor parallelism on H100/A100 clusters.
Proof of Quality
Models trained with Viking Engine at rank 1024 are on CivitAI.
π Orakul Studio β CivitAI
Context
ostris β author of ai-toolkit used by thousands β requested this code for integration. The ticket is open.
The # todo switch everything to proper mixed precision like this comment
existed in the original source. We read it and went deeper.
Built in Chernihiv, Ukraine. Basement. Artillery fire. RTX 4090.
Architecture matters more than hardware.
Model tree for OrakulStorm/ai-toolkit-bonememory
Base model
black-forest-labs/FLUX.2-dev