← Back to Pull requests

[ENH] tft_v2: enable efficient attention by setting need_weights=False #2429

Open
unknown opened this pull request with 299 lines diff across 3 files
View on GitHub
unknown unknown commented on Sep 15, 2026 12:24 UTC
Author

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_v2 by explicitly passing need_weights=False to nn.MultiheadAttention.

Previously, TFT_v2 computed the attention output but immediately discarded the attention weights (attended_output, _ = self.self_attention(...)). However, because need_weights defaults to True in 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 through scaled_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. large max_encoder_length), which is typically the memory bottleneck for TFT.

What should a reviewer concentrate their feedback on?


  • This is a zero-behavior-change optimization. The only change is how PyTorch allocates memory under the hood since the original attention weights were discarded anyway.
  • Did you add any tests for the change?


  • No, but I benchmarked the layer to confirm the memory and speed improvements.
  • Benchmark Results (Tesla T4, Sequence Length = 4000, Batch Size = 32):
    Before (need_weights=True):
    Peak Memory: 1106.06 MB
    Avg Forward Time: 10.44 ms
    After (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_v2 attention logic in line with PyTorch best practices for memory-efficient attention.

    PR checklist

  • The PR title starts with either [ENH], [MNT], [DOC], or [BUG]. [ENH]

  • Added/modified tests (Not applicable for this minor performance flag)

  • Used pre-commit hooks when committing to ensure that code is compliant with hooks. Install hooks with pre-commit install.

  • To run hooks independent of commit, execute pre-commit run --all-files

    Diff: 299 lines (+224 / -75) across 3 files Reviewer: phoeenniixx Review duration: 66610m
    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
    0.47
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    44%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 44.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 47% (MEDIUM) · Higher = larger blast radius
    47%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 44.7% · Lower = rushed or shallow review
    44.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 26% · Safe to merge
    26%