Skip to content

fix: detach k, v in SelfAttention to prevent gradient accumulation - #221

Merged
hzhangxyz merged 1 commit into
mainfrom
fix/detach-kv
Mar 27, 2026
Merged

fix: detach k, v in SelfAttention to prevent gradient accumulation#221
hzhangxyz merged 1 commit into
mainfrom
fix/detach-kv

Conversation

@hzhangxyz

Copy link
Copy Markdown
Member

Return detached key and value tensors to avoid unintended gradient tracking during backpropagation.

Return detached key and value tensors to avoid unintended gradient
tracking during backpropagation.
Copilot AI review requested due to automatic review settings March 27, 2026 02:02

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the transformer self-attention KV-cache behavior to avoid unintentionally retaining autograd history when reusing cached keys/values across decoding/generation steps.

Changes:

  • Detach k and v tensors before returning them as the KV cache from SelfAttention.forward().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hzhangxyz
hzhangxyz merged commit 4e6adfa into main Mar 27, 2026
12 checks passed
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.

2 participants