← Back to Pull requests

Autotune index_select_cat and scaled_index_add Triton kernels #1406

Open
rootkiller6788 opened this pull request with 91 lines diff across 4 files
View on GitHub
rootkiller6788 rootkiller6788 commented on Aug 16, 2026 16:26 UTC
Author

The two indexing kernels hard-coded BLOCK_SIZE_COL=512 (and BLOCK_SIZE_INDEX=1), which under-utilizes the GPU for small column dimensions (e.g. attention head dimensions of 64-512). Auto-tune over block sizes and num_warps instead.

This speeds up index_select_cat by ~5-25% over PyTorch native indexing for the common small-column case (it was previously ~5-11% slower), and keeps the large-column case at parity.

It also fixes a latent bug in the scaled_index_add backward kernel: the grad_output offset was missing a [:, None, None] broadcast, which only compiles once BLOCK_SIZE_INDEX > 1. The forward kernel passes restore_value to autotune since it mutates its input in place.

Adds regression tests for small column dimensions.

What does this PR do?


Fixes # (issue).

Before submitting

  • Did you have fun?

  • - Make sure you had fun coding 🙃
  • Did you read the contributor guideline?

  • Was this discussed/approved via a Github issue? (no need for typos, doc improvements)

  • - [ ] N/A
  • Did you make sure to update the docs?

  • - [ ] N/A
  • Did you write any new necessary tests?

  • - [ ] N/A
  • Did you update the changelog? (if needed)

  • - [ ] N/A

    PR review


    Anyone in the community is free to review the PR once the tests have passed.
    If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

    Diff: 91 lines (+79 / -12) across 4 files Reviewer: lw Review duration: 160259m
    All Vouch review confidence checks passed
    Residual risk 0.05 = Change Risk (0.41) × (1 − Review Confidence (0.88)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.41 · 91 lines diff across 4 files
    0.41
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.91 · 3 substantive comment(s)
    0.91
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    88%
    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
    Diff Size diff_size
    0.271
    Real PR Conversation Timeline
    4 items
    meta-cla[bot] meta-cla[bot] commented on Aug 16, 2026 16:26 UTC
    Bot

    Hi @rootkiller6788!

    Thank you for your pull request and welcome to our community.

    Action Required

    In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

    Process

    In order for us to review and merge your suggested changes, please sign at <https://code.facebook.com/cla>. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

    Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

    If you have received this in error or have any questions, please contact us at [cla@meta.com](mailto:cla@meta.com?subject=CLA%20for%20facebookresearch%2Fxformers%20%231406). Thanks!

    meta-cla[bot] meta-cla[bot] commented on Aug 16, 2026 17:33 UTC
    Bot

    Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

    lw lw commented on code on Aug 20, 2026 12:50 UTC
    Code Review
    xformers/ops/_triton/k_index_select_cat.py:1
    @@ -8,6 +8,19 @@
     import triton.language as tl
     
     
    +def _index_select_cat_configs():
    +    return [
    +        triton.Config({"BLOCK_SIZE_INDEX": bi, "BLOCK_SIZE_COL": bc}, num_warps=w)
    +        for bi in (1, 2, 4, 8)
    +        for bc in (64, 128, 256, 512)
    +        for w in (1, 2, 4)

    How were these values chosen? I'm somewhat worried of making everyone compile and benchmark 48 different kernels...

    Reviewers
    Assignees
    rootkiller6788 rootkiller6788
    Labels
    risk: low confidence: 88% 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: 41.4% (MEDIUM) · Higher = larger blast radius
    41.4%
    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: 88% · Lower = rushed or shallow review
    88%
    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: 5.0% · Safe to merge
    5.0%