# Async Weight Streaming

import BenchmarkChart from '@components/memory/BenchmarkChart.astro';
import BenchmarkRow from '@components/memory/BenchmarkRow.astro';

When a model does not fit in VRAM, [Low-VRAM mode](/configuration/optimization/low-vram-mode/) keeps part of its layers
in RAM and copies each of them to the GPU every time a step needs it. By default each of those copies blocks: the GPU
finishes its queued work, the layer is copied, then the computation continues. The GPU waits for every copy, and the
copy waits for the GPU.

`async_weight_streaming` is a global setting that overlaps the two. The layers in RAM are page-locked, so the GPU can
read them directly, and each copy runs on a separate stream while the GPU is still computing the layers before it.
A step then takes closer to the time of a fully loaded model.

It only changes anything while a model is **partially loaded**. The log line for a model load shows how much of it is in
VRAM, for example `VRAM: 12000.00MB (50.0%)`; at 100% nothing is copied per step, and the setting has no effect.

Images are **bit-identical** to the default path: the same weights reach the same computations, only earlier.

Measured end to end with the VRAM cache capped at 8 GB (`max_cache_vram_gb: 8`), so the transformer loads only partly,
and a RAM cache large enough to keep the text encoder and the transformer between images (`max_cache_ram_gb: 40`, see
[below](#requirements)): 1024², 8 steps, the median of warm runs from interleaved pairs, pixel-identical images. The
gain depends on how much of the model streams, on the GPU's PCIe link and on how fast the system copies from ordinary
RAM; a model fully in VRAM sees no change. The process's peak VRAM with the setting stayed within 50 MB of the peak
without it in these runs.

<BenchmarkChart title="Time per image on an RTX 4090 (PCIe 4.0 x16, Windows), BF16, transformer partly in VRAM" setting="async_weight_streaming">
  <BenchmarkRow label="ERNIE-Image Turbo, about 18% in VRAM" before={20.3} after={8.7} />
  <BenchmarkRow label="Z-Image Turbo, about 24% in VRAM" before={15.1} after={6.7} />
</BenchmarkChart>

<BenchmarkChart title="Time per image on an RTX PRO 6000 Blackwell (Linux), BF16, transformer partly in VRAM" setting="async_weight_streaming">
  <BenchmarkRow label="ERNIE-Image Turbo, about 31% in VRAM" before={6.05} after={3.71} />
  <BenchmarkRow label="Z-Image Turbo, about 40% in VRAM" before={4.56} after={3.09} />
</BenchmarkChart>

<BenchmarkChart title="Time per image on an RX 9060 XT (PCIe 4.0 x4, Windows, ROCm 10), transformer partly in VRAM" setting="async_weight_streaming">
  <BenchmarkRow label="Krea-2 Turbo, scaled FP8, about 38% in VRAM" before={61.9} after={33.7} />
</BenchmarkChart>

## Requirements

- **An NVIDIA GPU, or an AMD GPU with ROCm.** On Apple silicon and the CPU, models load whole, so there is nothing to
  stream. Intel GPUs keep the blocking copy, and the log says so once: `async_weight_streaming is enabled, but not used
  on <device>: it needs an NVIDIA or AMD (ROCm) GPU.`
- **`keep_ram_copy_of_weights: true`**, the default. Without the RAM copy, the layers in RAM are new allocations after
  every unload and are not page-locked, so they keep the blocking copy. The log says so once.
- **`enable_partial_loading: true`**, the default.
- **Room in the RAM cache for the models of one generation.** The first time a model's layers are locked costs time
  (see [Memory](#memory)); they then stay locked while the model is in the RAM cache. When the RAM cache cannot hold,
  say, the text encoder and the transformer at once, Invoke loads them again for every image, and every image pays for
  the first lock again. On Linux with an 8 GB RAM cache, ERNIE-Image Turbo took 9.4 seconds per image with the setting
  instead of 8.5 without it. The startup log shows the size (`Calculated model RAM cache size`); without
  `max_cache_ram_gb`, it is at most your VRAM, or `max_cache_vram_gb` when that is set. Raise
  [`max_cache_ram_gb`](/configuration/optimization/memory-options/) so the models you use together fit. When a model
  with locked layers leaves the RAM cache, the log says so once.

Layers whose weights use a special tensor format, such as GGUF and SDNQ, keep the blocking copy, so the setting does
not speed up those models; it keeps VRAM free only for their few plain layers. Full-precision, FP8 and int8 layers
stream.

## Turning it on

Set it in your [`invokeai.yaml`](/configuration/invokeai-yaml/) and restart InvokeAI:

```yaml
async_weight_streaming: true
```

The first layer that streams logs, once per GPU:

```
async_weight_streaming: weights in RAM stream to cuda:0 asynchronously.
```

## Memory

- **VRAM.** Copies that the GPU has not reached yet take up to about 384MB of VRAM. Invoke keeps that much free for a
  model it loads partially with this setting on (less for a model with fewer layers to stream), so slightly less of
  the model stays in VRAM. A model that fits in VRAM is loaded whole as before. When the model is released, Invoke
  hands that memory back to the driver; on a system with several GPUs this waits while another GPU is mid-generation,
  so the memory stays reserved a little longer there.
  Nodes that hold a second model within a VRAM limit (Wan 2.2's experts, Ideogram 4's second branch) count that
  memory inside the limit: what they move to RAM streams from then on, so the resident weights stay that much under
  it, also when the model was loaded partially to begin with. Nothing is kept free when the limit on locked RAM (see
  below) leaves nothing to stream: such a model then keeps the blocking copy until its next load.
- **RAM.** Page-locking makes no second copy: the existing RAM copy of the layers is locked in place. It does change
  what that memory is. Weights that are still mapped from their file (see Loading below) are page cache, which the
  system can reclaim; once locked, the driver has made a private copy of the pages. That copy stays private after
  the layer is unlocked (it can then be swapped out, but it is no longer reclaimable page cache) until its model
  leaves the RAM cache. A layer stays locked
  while its model is in the RAM cache, also while a later load keeps it in VRAM and between generations, so models
  that take turns in VRAM lock their layers only once. Locked memory cannot be moved to the page file or swap, so at
  most 40% of your physical RAM is locked. When a model needs more than that, Invoke unlocks layers that no running
  model streams right now, the longest unused first, and only as many as it needs; the layers that still do not fit
  keep the blocking copy. A model removed from the RAM cache is unlocked.
- **Loading.** Most models are read from their files lazily. Locking a layer the first time reads it, and the driver
  makes a private copy of the file's pages as it locks them: about 0.3 seconds per GiB on Linux and 0.5 seconds per
  GiB on Windows when the file is already in the system's file cache, up to about 2.5 seconds per GiB when it is read
  from an NVMe SSD. Only the reading is a cost the first step would pay anyway. Other models keep loading meanwhile.
  Locking a layer again after another model reclaimed it is much cheaper: about 0.03 seconds per GiB was measured for
  memory already in RAM.