← Back to Pull requests

add support for int64 pointer in tiled_matmul kernel #1346

Open
Maxon081102 opened this pull request with 246 lines diff across 1 files
View on GitHub
Maxon081102 Maxon081102 commented on Oct 15, 2025 08:43 UTC
Author

What does this PR do?
Fixes runtime errors caused by pointer size mismatches in Triton kernels during very large matrix computations.
This PR introduces a new Triton kernel that uses int64 pointers and adds automatic kernel selection between the existing int32 kernel and the new int64 version.
The change ensures stable execution on long‑context LLM training tasks, preventing RuntimeError: Triton Error [CUDA]: an illegal memory access was encountered when matrix dimensions exceed int32 limits.

Diff: 246 lines (+245 / -1) across 1 files Reviewer: lw Review duration: 599923m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.48) × (1 − Review Confidence (0.88)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 246 lines diff across 1 files
0.48
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.91 · 4 substantive comment(s)
0.91
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
87%
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.330
Ai Authored ai_authored
0.050
Real PR Conversation Timeline
6 items
meta-cla[bot] meta-cla[bot] commented on Oct 15, 2025 08:43 UTC
Bot

Hi @Maxon081102!

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%231346). Thanks!

meta-cla[bot] meta-cla[bot] commented on Oct 15, 2025 11:11 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 Oct 27, 2025 16:29 UTC
Code Review
xformers/ops/_triton/tiled_matmul_kernels.py:1
@@ -346,6 +346,215 @@ def _xformers_tiled_matmul_kernel(
         tl.atomic_add(C, acc, mask=mask)
 
 
+@triton.autotune(
+    configs=TRITON_CONFIGS,
+    key=["M1", "M2", "M3", "N1", "N2", "N3", "K1", "K2", "K3"],
+    prune_configs_by={
+        "early_config_prune": our_early_config_prune,
+        "perf_model": our_estimate_matmul_time,
+        "top_k": 10,
+    },
+)
+@triton.heuristics(
+    {
+        "EVEN_K": lambda args: all(
+            k % (args["BLOCK_K"] * args["SPLIT_K"]) == 0
+            for k in [args["K1"], args["K2"], args["K3"]]
+        ),
+    }
+)
+@triton.jit()
+def _xformers_tiled_matmul_kernel_int64(
+    A11,
+    A12,
+    A13,
+    A21,
+    A22,
+    A23,
+    A31,
+    A32,
+    A33,
+    B11,
+    B12,
+    B13,
+    B21,
+    B22,
+    B23,
+    B31,
+    B32,
+    B33,
+    C11,
+    C12,
+    C13,
+    C21,
+    C22,
+    C23,
+    C31,
+    C32,
+    C33,
+    M1,
+    M2,
+    M3,
+    N1,
+    N2,
+    N3,
+    K1,
+    K2,
+    K3,
+    stride_am1,
+    stride_am2,
+    stride_am3,
+    stride_ak1,
+    stride_ak2,
+    stride_ak3,
+    stride_bk1,
+    stride_bk2,
+    stride_bk3,
+    stride_bn1,
+    stride_bn2,
+    stride_bn3,
+    stride_cm1,
+    stride_cm2,
+    stride_cm3,
+    stride_cn1,
+    stride_cn2,
+    stride_cn3,
+    BLOCK_M: tl.constexpr,  # DO NOT CHANGE NAME: MUST MATCH PERF MODEL
+    BLOCK_N: tl.constexpr,  # DO NOT CHANGE NAME: MUST MATCH PERF MODEL
+    BLOCK_K: tl.constexpr,  # DO NOT CHANGE NAME: MUST MATCH PERF MODEL
+    GROUP_M: tl.constexpr,
+    SPLIT_K: tl.constexpr,  # DO NOT CHANGE NAME: MUST MATCH PERF MODEL
+    EVEN_K: tl.constexpr,
+    ACC_TYPE: tl.constexpr,
+):
+    # matrix multiplication
+    pid = tl.program_id(0).to(tl.int64)
+    pid_k = tl.program_id(1).to(tl.int64)

I suspect these are the only two lines that differ wrt the original version, is that so?

What is the downside of always casting to int64 in the original kernel? Did you observe some performance regression?

lw lw commented on code on Oct 27, 2025 16:32 UTC
Code Review
xformers/ops/_triton/tiled_matmul_kernels.py:1
@@ -407,7 +616,42 @@ def grid(META):
             META["SPLIT_K"],
         )
 
-    _xformers_tiled_matmul_kernel[grid](
+    # Decide whether 32-bit address arithmetic can overflow; if so, use int64-safe kernel
+    INT32_MAX = (1 << 31) - 1
+    def _dim_or_zero(xs, i):
+        return xs[i] if len(xs) > i else 0
+
+    use_int64 = False
+    for i in range(3):
+        Mi = max(0, _dim_or_zero(ms, i))
+        Ni = max(0, _dim_or_zero(ns, i))
+        Ki = max(0, _dim_or_zero(ks, i))
+
+        # A offsets
+        a_row_term = max(0, Mi - 1) * int(strides_am[i])
+        a_col_term = max(0, Ki - 1) * int(strides_ak[i])
+        # B offsets
+        b_row_term = max(0, Ki - 1) * int(strides_bk[i])
+        b_col_term = max(0, Ni - 1) * int(strides_bn[i])
+        # C offsets
+        c_row_term = max(0, Mi - 1) * int(strides_cm[i])
+        c_col_term = max(0, Ni - 1) * int(strides_cn[i])
+
+        # Check per-term and per-address sums
+        if (
+            a_row_term > INT32_MAX or a_col_term > INT32_MAX or
+            b_row_term > INT32_MAX or b_col_term > INT32_MAX or
+            c_row_term > INT32_MAX or c_col_term > INT32_MAX or
+            (a_row_term + a_col_term) > INT32_MAX or
+            (b_row_term + b_col_term) > INT32_MAX or
+            (c_row_term + c_col_term) > INT32_MAX
+        ):
+            use_int64 = True
+            break

This code can be simplified

lw lw requested changes on Oct 27, 2025 16:33 UTC
Changes Requested Reviewer

Thanks for the contribution. The change is very large, and this code is in best-effort maintenance, hence we can only accept changes if they don't add too much surface.

Reviewers
Assignees
Maxon081102 Maxon081102
Labels
risk: low confidence: 87.8% 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.9% (MEDIUM) · Higher = larger blast radius
47.9%
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: 87.8% · Lower = rushed or shallow review
87.8%
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.9% · Safe to merge
5.9%