[ENH] tft_v2: enable efficient attention by setting need_weights=False #2429
All Vouch review confidence checks passed
Residual risk 0.26 = Change Risk (0.47) × (1 − Review Confidence (0.45)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 299 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Shallow Review Depth
shallow_review_depth
0.344
Diff Size
diff_size
0.341
Real PR Conversation Timeline
1 comments
"Review active on #2429"
general
weight: 0.35
phoeenniixx
Reference Issues/PRs
<!-- If there's an existing issue related to memory or TFT performance, you can link it here. E.g., Fixes #XYZ. Otherwise, you can leave it blank or write N/A -->
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,
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 the $(L+H) \times (L+H)$ attention probability matrix.By 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):
Before (
need_weights=True):Peak Memory:
1106.06 MBAvg Forward Time:
10.44 msAfter (
need_weights=False):Peak Memory:
103.72 MB(~10x reduction)Avg Forward Time:
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