[ENH] tft_v2: enable efficient attention by setting need_weights=False - #2429
Open
ramanbansal1 wants to merge 2 commits into
Open
ramanbansal1 wants to merge 2 commits into
ramanbansal1 wants to merge 2 commits into
Conversation
ramanbansal1
requested review from
benHeid,
fkiraly,
jdb78 and
phoeenniixx
as code owners
September 15, 2026 12:24
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2429 +/- ##
=======================================
Coverage ? 88.54%
=======================================
Files ? 208
Lines ? 11381
Branches ? 0
=======================================
Hits ? 10077
Misses ? 1304
Partials ? 0
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
phoeenniixx
requested changes
Sep 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Reference Issues/PRs
N/A
What does this implement/fix? Explain your changes.
This PR optimizes the self-attention computation in
TFT_v2by explicitly passingneed_weights=Falsetonn.MultiheadAttention.Previously,$(L+H) \times (L+H)$ attention probability matrix.
TFT_v2computed the attention output but immediately discarded the attention weights (attended_output, _ = self.self_attention(...)). However, becauseneed_weightsdefaults toTruein PyTorch, this silently forced PyTorch to use its slow, memory-intensive math backend to explicitly construct theBy explicitly setting
need_weights=False, we allow PyTorch 2.0+ to automatically route the computation throughscaled_dot_product_attention(SDPA), unlocking FlashAttention and Memory-Efficient Attention kernels. This is a zero-behavior-change optimization that yields massive memory savings, especially for long sequence lengths (e.g. largemax_encoder_length), which is typically the memory bottleneck for TFT.What should a reviewer concentrate their feedback on?
Did you add any tests for the change?
Benchmark Results (Tesla T4, Sequence Length = 4000, Batch Size = 32):
need_weights=True):1106.06 MB10.44 msneed_weights=False):103.72 MB(~10x reduction)5.62 ms(~2x faster)Any other comments?
This brings the
TFT_v2attention logic in line with PyTorch best practices for memory-efficient attention.PR checklist
pre-commit install.To run hooks independent of commit, execute
pre-commit run --all-files