Skip to content

Decode LoRA cast cache: refresh after fused optimizer steps - #15052

Merged
danielhanchen merged 2 commits into
mainfrom
lora-cache-optimizer-steps
Oct 24, 2026
Merged

danielhanchen merged 2 commits into
mainfrom
lora-cache-optimizer-steps

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

#14754 keyed the decode LoRA cast cache (_fast_lora_weights in unsloth/kernels/utils.py) on the adapters' _version, so an adapter that keeps training between generations gets a fresh cast. Fused optimizers do not bump _version, so after a torch.optim.AdamW(fused = True) step the cache keeps serving the old adapter weights.

Cause

aten::_fused_adamw_, aten::_fused_adam_ and aten::_fused_sgd_ have no ADInplaceOrView kernel, so they update the parameter values without bumping _version. add_ does have one. Measured with torch 2.14.1:

_version after one step values changed
AdamW foreach, CPU 0 -> 2 yes
AdamW fused, CPU 0 -> 0 yes
AdamW foreach, CUDA 0 -> 2 yes
AdamW fused, CUDA 0 -> 0 yes

adamw_torch_fused is the transformers TrainingArguments default on torch >= 2.8. for_training already drops the cache, so this is only reached when generation and training interleave without that switch (for example model.eval() before generate, then more training).

Change

  • New key element: a global optimizer step post hook (torch.optim.optimizer.register_optimizer_step_post_hook) counts steps, and the count is part of the cache key next to _version.
  • Which optimizers it covers: every torch.optim.Optimizer subclass, including fused AdamW and the bitsandbytes 8-bit optimizers.
  • What stays the same:
    • inference tensors still key on storage alone;
    • the compiled path still skips the cache;
    • the same-dtype path still returns the adapters directly.
  • Cost: one integer increment per optimizer step and one extra integer in the key per decode projection.

Tests

tests/test_fast_lora_cast_cache.py (CPU):

  • After a step: the cache returns the new weights after a fused AdamW, foreach AdamW, for-loop AdamW and SGD step.
  • Without updates: it still hits when nothing changed.
  • Same dtype: it is skipped when the dtypes match.
torch main this PR
2.7.1 1 failed (adamw_fused), 5 passed 6 passed
2.10.0 1 failed (adamw_fused), 5 passed 6 passed
2.14.1 1 failed (adamw_fused), 5 passed 6 passed

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 24, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-24T00:47:50.223016Z a5b5cce Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Can't wait for the next one!

Reviewed commit: a5b5ccebfd

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danielhanchen
danielhanchen merged commit 5c38c96 into main Oct 24, 2026
3 of 29 checks passed
@danielhanchen
danielhanchen deleted the lora-cache-optimizer-steps branch October 24, 2026 00:54
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.

1 participant