Skip to content

CPU offloading of (quantized) KV cache buffers (#47) - #47

Merged
mseeger merged 1 commit into
mainfrom
cache_offload2
Mar 3, 2026
Merged

mseeger merged 1 commit into
mainfrom
cache_offload2

Conversation

@mseeger

@mseeger mseeger commented Mar 3, 2026

Copy link
Copy Markdown
Contributor

Enables CPU offloading of quantized KV cache buffers, which allows to run inference for bigger models.

This is done by sharing quantizers across all quantized KV cache buffers, but storing the quantizer states per layer to CPU.

Closes #18.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@mseeger
mseeger merged commit a5c3fe2 into main Mar 3, 2026
6 checks passed
@mseeger
mseeger deleted the cache_offload2 branch March 3, 2026 22:23
@mseeger

mseeger commented Mar 4, 2026 •

Copy link
Copy Markdown
Contributor Author
  • Forward options:

[1] cell, then layer, then chunk/cell (we do this right now)
[2] layer, then cell, then chunk/cell

Assume: Mem for KV cache = Mem for cell embeddings

How many copies CPU/GPU?
[1]

  • embeddings: -
  • caches: n_cells * n_layers

[2]

  • embeddings: n_cells * n_layers
  • caches: n_layers

==> Pretty much the same. Stick with [1]

Another way to look at it (if quantization is used):

[1] Embeddings don't need quant, but caches n_cells * n_layers
[2] Caches don't need quant along cells, but embeddings do

==> Easiest to stick with [1]

  • What we have:
  • KV cache buffers (quantized):
    • Each has its own quantizers with buffers on GPU
    • Dequantization buffers shared
    • Quantizer.create_quantizer_state -> what we need!
  • KV cache buffers (not quantized):
    • Each has its own buffers on GPU
  • KV cache checkpointing
    • Stores KV cache buffers for certain chunk_ids; only in single layer
    • I/O via DefaultKVCacheBuffers -> not so useful

OK: Do not use code in checkpoints.py!

  • What is missing:
  • Quantizers can be shared by several KVCacheBuffer's
  • QuantizerState owns the copying via
    copy_ (buffer -> state), restore (state -> buffer)
  • Container to store the quantizer states on CPU
  • Design

Current:

  • QuantizedKVCacheBuffers:
    • quantizer_k -> Quantizer
    • quantizer_v -> Quantizer
  • Quantizer:
    • No reference back to QuantizedKVCacheBuffers. No idea when
      buffer is changing!
    • create_quantizer_state -> QuantizerState
  • QuantizerState:
    • quantizer -> Quantizer (reference back)
    • copy_ and restore
  • N layers:
    N times independent (QuantizedKVCacheBuffers, Quantizer)

What we need:

  • N (QuantizedKVCacheBuffers, QuantizerState) pairs, but only 1
    Quantizer
  • buffer -> quantizer: Quantizer has the right state

Two possibilities:

  • Push: Buffer initializes the state update for quantizer
    (+) Simple; quantizer code can stay the same
    (+) Could also include push to write state back
    (-) Need to remember to do it
  • Pull: Quantizer notices buffer changing, initializes state update
    (+) Safer; no need to remember
    (-) More complex; quantizer has to store back reference; dependence?
    (-) State for previous buffer written back only upon change; safe?

Like to have "pull", but without complex code change in quantizer!

Idea: Callback

  • Quantizer gets optional callback(new_buffers, current_buffers); it
    also stores current_buffers (need not be typed, to avoid bidirectional
    dep.)
  • Quantizer methods called from QuantizedKVCacheBuffers gets optional
    buffers arg. This is required if callback is given. Before anything
    is done in the method: Call callback, and then store new buffers
  • All complexity in class maintaining the quantization states:
    • Maintains quantization states
    • Creation of QuantizedKVCacheBuffers's and Quantizer's
    • Definition of callbacks
    • Method flush: Quantizer writes back its current state. This would
      be called at the end of an inference pass, to be safe

@mseeger

mseeger commented Mar 4, 2026

Copy link
Copy Markdown
Contributor Author
  • Larger comparison: We run the eval script and compare loss values

[1] Eval without offloading

CUDA_VISIBLE_DEVICES="0,1,2,3" PYTORCH_ALLOC_CONF=expandable_segments:True KEYSVALS_LOG_DIR="/home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/variant0_copy1/eval_logs"; python3 keys_values/__main__.py eval_long /home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/variant0_copy1 --model_type lora --devices 4 --batch_size 4 --kv_cache.name h2o-torch-quantized8 --kv_cache.cache_length 32768 --kv_cache.chunk_size 1024 --verbose some --attention_forward_temp_size_gb 8 --lora_dropout 0 --kv_cache.cpu_offload False

variant0_copy1/eval_logs/gpu0.log:[rank 0 | 2026-03-03 10:22:20]: Batch final, [8, 9, 10, 11]: loss = 1.312, eval_time = 193066.46 ms
variant0_copy1/eval_logs/gpu0.log:[rank 0 | 2026-03-03 10:26:03]: Batch final, [24, 25, 26, 27]: loss = 2.188, eval_time = 219998.20 ms
variant0_copy1/eval_logs/gpu0.log:[rank 0 | 2026-03-03 10:30:18]: Batch final, [40, 41, 42, 43]: loss = 3.141, eval_time = 251227.05 ms
variant0_copy1/eval_logs/gpu1.log:[rank 1 | 2026-03-03 10:22:17]: Batch final, [4, 5, 6, 7]: loss = 2.062, eval_time = 190352.98 ms
variant0_copy1/eval_logs/gpu1.log:[rank 1 | 2026-03-03 10:25:51]: Batch final, [20, 21, 22, 23]: loss = 1.875, eval_time = 211357.64 ms
variant0_copy1/eval_logs/gpu1.log:[rank 1 | 2026-03-03 10:29:54]: Batch final, [36, 37, 38, 39]: loss = 1.516, eval_time = 239489.14 ms
variant0_copy1/eval_logs/gpu2.log:[rank 2 | 2026-03-03 10:22:13]: Batch final, [0, 1, 2, 3]: loss = 2.094, eval_time = 186245.56 ms
variant0_copy1/eval_logs/gpu2.log:[rank 2 | 2026-03-03 10:25:38]: Batch final, [16, 17, 18, 19]: loss = 2.031, eval_time = 203140.78 ms
variant0_copy1/eval_logs/gpu2.log:[rank 2 | 2026-03-03 10:29:32]: Batch final, [32, 33, 34, 35]: loss = 2.188, eval_time = 230206.17 ms
variant0_copy1/eval_logs/gpu3.log:[rank 3 | 2026-03-03 10:22:23]: Batch final, [12, 13, 14, 15]: loss = 3.266, eval_time = 196409.54 ms
variant0_copy1/eval_logs/gpu3.log:[rank 3 | 2026-03-03 10:26:13]: Batch final, [28, 29, 30, 31]: loss = 1.977, eval_time = 226969.37 ms
variant0_copy1/eval_logs/gpu3.log:[rank 3 | 2026-03-03 10:30:41]: Batch final, [44, 45, 46, 47]: loss = 1.914, eval_time = 263371.05 ms
[STOPPED]

[2] Eval with offloading

CUDA_VISIBLE_DEVICES="4,5,6,7" PYTORCH_ALLOC_CONF=expandable_segments:True KEYSVALS_LOG_DIR="/home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/variant0_copy2/eval_logs"; python3 keys_values/__main__.py eval_long /home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/variant0_copy2 --model_type lora --devices 4 --batch_size 4 --kv_cache.name h2o-torch-quantized8 --kv_cache.cache_length 32768 --kv_cache.chunk_size 1024 --verbose some --attention_forward_temp_size_gb 8 --lora_dropout 0 --kv_cache.cpu_offload True

variant0_copy2/eval_logs/gpu0.log:[rank 0 | 2026-03-03 10:23:34]: Batch final, [8, 9, 10, 11]: loss = 1.312, eval_time = 200362.90 ms
variant0_copy2/eval_logs/gpu0.log:[rank 0 | 2026-03-03 10:27:24]: Batch final, [24, 25, 26, 27]: loss = 2.188, eval_time = 227792.65 ms
variant0_copy2/eval_logs/gpu1.log:[rank 1 | 2026-03-03 10:23:25]: Batch final, [0, 1, 2, 3]: loss = 2.094, eval_time = 192124.85 ms
variant0_copy2/eval_logs/gpu1.log:[rank 1 | 2026-03-03 10:26:57]: Batch final, [16, 17, 18, 19]: loss = 2.031, eval_time = 210134.41 ms
variant0_copy2/eval_logs/gpu1.log:[rank 1 | 2026-03-03 10:30:58]: Batch final, [32, 33, 34, 35]: loss = 2.188, eval_time = 237537.48 ms
variant0_copy2/eval_logs/gpu2.log:[rank 2 | 2026-03-03 10:23:30]: Batch final, [4, 5, 6, 7]: loss = 2.062, eval_time = 197221.11 ms
variant0_copy2/eval_logs/gpu2.log:[rank 2 | 2026-03-03 10:27:11]: Batch final, [20, 21, 22, 23]: loss = 1.875, eval_time = 217741.58 ms
variant0_copy2/eval_logs/gpu2.log:[rank 2 | 2026-03-03 10:31:21]: Batch final, [36, 37, 38, 39]: loss = 1.516, eval_time = 247520.29 ms
variant0_copy2/eval_logs/gpu3.log:[rank 3 | 2026-03-03 10:23:36]: Batch final, [12, 13, 14, 15]: loss = 3.266, eval_time = 202567.43 ms
variant0_copy2/eval_logs/gpu3.log:[rank 3 | 2026-03-03 10:27:33]: Batch final, [28, 29, 30, 31]: loss = 1.977, eval_time = 234370.63 ms
[STOPPED]

Cool: Produce the same results. Offloading slightly slower, as expected

  • Comparison of fine-tuning

Also to check whether not wrapping cpu_offloader creates issues.

[3] finetune_offload_full without offloading

CUDA_VISIBLE_DEVICES="0" PYTORCH_ALLOC_CONF=expandable_segments:True KEYSVALS_LOG_DIR="/home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/debug_no_off/logs"; python3 keys_values/__main__.py finetune_offload_full Qwen/Qwen2.5-0.5B --out_dir /home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/debug_no_off --data LongBenchV2 --data.max_seq_length 150000 --data.metadata_dir /home/ubuntu/out/finetune/data --precision bf16-true --kv_cache.name h2o-torch-quantized8 --kv_cache.cache_length 16384 --kv_cache.chunk_size 1024 --verbose some --grad.layers_per_cell 1 --train.save_interval 10 --train.micro_batch_size 4 --train.global_batch_size 4 --eval.interval 10 --eval.micro_batch_size 4 --head_model seq_classification_on_logits --eval.initial_validation False --data.trainloader_longest_first True --kv_cache.cpu_offload False

DONE: Fails with NaN after 20 steps, as above.

[4] finetune_offload_full with offloading

CUDA_VISIBLE_DEVICES="1" PYTORCH_ALLOC_CONF=expandable_segments:True KEYSVALS_LOG_DIR="/home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/debug_off/logs";    python3 keys_values/__main__.py finetune_offload_full Qwen/Qwen2.5-0.5B --out_dir /home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/debug_off    --data LongBenchV2 --data.max_seq_length 150000 --data.metadata_dir /home/ubuntu/out/finetune/data --precision bf16-true --kv_cache.name h2o-torch-quantized8 --kv_cache.cache_length 16384 --kv_cache.chunk_size 1024 --verbose some --grad.layers_per_cell 1 --train.save_interval 10 --train.micro_batch_size 4 --train.global_batch_size 4 --eval.interval 10 --eval.micro_batch_size 4 --head_model seq_classification_on_logits --eval.initial_validation False --data.trainloader_longest_first True --kv_cache.cpu_offload True

DONE: Fails with NaN after 20 steps, as above.
==> Results are similar to [3], but not the same. Certainly not worse

[5] finetune_long_full without offloading

CUDA_VISIBLE_DEVICES="2" PYTORCH_ALLOC_CONF=expandable_segments:True KEYSVALS_LOG_DIR="/home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/debug2_no_off/logs"; python3 keys_values/__main__.py finetune_long_full Qwen/Qwen2.5-0.5B --out_dir /home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/debug2_no_off --data LongBenchV2 --data.max_seq_length 150000 --data.metadata_dir /home/ubuntu/out/finetune/data --precision bf16-true --kv_cache.name h2o-torch-quantized8 --kv_cache.cache_length 16384 --kv_cache.chunk_size 1024 --verbose some --grad.layers_per_cell 1 --train.save_interval 10 --train.micro_batch_size 4 --train.global_batch_size 4 --eval.interval 10 --eval.micro_batch_size 4 --head_model seq_classification_on_logits --eval.initial_validation False --data.trainloader_longest_first True --kv_cache.cpu_offload False

DONE: Fails with NaN after 20 steps, as above.

[6] finetune_long_full with offloading

CUDA_VISIBLE_DEVICES="0" PYTORCH_ALLOC_CONF=expandable_segments:True KEYSVALS_LOG_DIR="/home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/debug2_off/logs";    python3 keys_values/__main__.py finetune_long_full Qwen/Qwen2.5-0.5B --out_dir /home/ubuntu/out/finetune/ml_ws/lora/qwen2_5_0_5b/debug2_off    --data LongBenchV2 --data.max_seq_length 150000 --data.metadata_dir /home/ubuntu/out/finetune/data --precision bf16-true --kv_cache.name h2o-torch-quantized8 --kv_cache.cache_length 16384 --kv_cache.chunk_size 1024 --verbose some --grad.layers_per_cell 1 --train.save_interval 10 --train.micro_batch_size 4 --train.global_batch_size 4 --eval.interval 10 --eval.micro_batch_size 4 --head_model seq_classification_on_logits --eval.initial_validation False --data.trainloader_longest_first True --kv_cache.cpu_offload True

DONE: Fails with NaN after 20 steps, as above.
==> Results are similar to [5], but not the same. Certainly not worse

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement CPU offloading for KV cache buffers

1 participant