Skip to content

[Bug][Flex Attention] Flex Attention crashes with LLVM error after triton version bump #178554

Closed

Description

@Lucaskabela

🐛 Describe the bug

On torch nightly, we run into a new CI failure in torchtitan (see pytorch/torchtitan#2722)

The crash looks like:

python: /source/llvm-project/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp:2690: int llvm::slpvectorizer::BoUpSLP::LookAheadHeuristics::getScoreAtLevelRec(Value *, Value *, Instruction *, Instruction *, int, ArrayRef<Value *>) const: Assertion `FromIdx <= ToIdx && "Bad index"' failed.
Aborted (core dumped)

We find a workaround is to disable llvm opt - further, we were able to minimize to just:

import os
# Ensure not set so we repro
os.environ.pop("DISABLE_LLVM_OPT", None)

import torch
from torch.nn.attention.flex_attention import create_block_mask

_compiled_create_block_mask = torch.compile(create_block_mask)


def causal_mask(b, h, q_idx, kv_idx):
    return q_idx >= kv_idx


def main():
    print("Building attention mask...")
    block_mask = _compiled_create_block_mask(
        causal_mask, 8, None, 2048, 2048
    )
    print("Done — no crash.")


if __name__ == "__main__":
    main()

Additional Context

Changing batch size (8) or seq len (2048) smaller makes this repro disappear

Claude has the following analysis:

Workaround for Triton 3.7 LLVM SLP vectorizer crash on flex attention kernels. The Triton pin update (#174896) ships an LLVM build whose SLP vectorizer hits an assertion (FromIdx <= ToIdx, "Bad index") when compiling certain flex attention Triton kernels. The crash occurs in llvm.optimize_module() during make_llir, which runs the LLVM O3 pipeline including SLP vectorization. There is no Python-level API to disable only the SLP vectorizer pass (optimize_module ignores its flags parameter, and DISABLE_LLVM_OPT= only sets flags to true). Setting DISABLE_LLVM_OPT=1 skips the LLVM O3 pipeline entirely; the MLIR-level optimizations still run, so GPU kernel performance impact is minimal.

Versions

Details

Collecting environment information...
PyTorch version: 2.12.0a0+gite7bcaf2
Is debug build: False
CUDA used to build PyTorch: 12.9
ROCM used to build PyTorch: N/A

OS: CentOS Stream 9 (x86_64)
GCC version: (GCC) 11.5.0 20240719 (Red Hat 11.5.0-14)
Clang version: 21.1.7 (CentOS 21.1.7-1.el9)
CMake version: version 4.3.0
Libc version: glibc-2.34

Python version: 3.10.20 (main, Mar 11 2026, 17:46:40) [GCC 14.3.0] (64-bit runtime)
Python platform: Linux-6.13.2-0_fbk12_0_g0b66b3635210-x86_64-with-glibc2.34
Is CUDA available: True
CUDA runtime version: 12.9.86
CUDA_MODULE_LOADING set to:
GPU models and configuration:
GPU 0: NVIDIA H100
GPU 1: NVIDIA H100
GPU 2: NVIDIA H100
GPU 3: NVIDIA H100
GPU 4: NVIDIA H100
GPU 5: NVIDIA H100
GPU 6: NVIDIA H100
GPU 7: NVIDIA H100

Nvidia driver version: 580.82.07
cuDNN version: Could not collect
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
Caching allocator config: N/A

CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 52 bits physical, 57 bits virtual
Byte Order: Little Endian
CPU(s): 384
On-line CPU(s) list: 0-383
Vendor ID: AuthenticAMD
Model name: AMD EPYC 9654 96-Core Processor
CPU family: 25
Model: 17
Thread(s) per core: 2
Core(s) per socket: 96
Socket(s): 2
Stepping: 1
Frequency boost: enabled
CPU(s) scaling MHz: 100%
CPU max MHz: 2400.0000
CPU min MHz: 1500.0000
BogoMIPS: 4792.43
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ht syscall nx mmxext fxsr_opt pdpe1gb rdtscp lm constant_tsc rep_good amd_lbr_v2 nopl xtopology nonstop_tsc cpuid extd_apicid aperfmperf rapl pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand lahf_lm cmp_legacy svm extapic cr8_legacy abm sse4a misalignsse 3dnowprefetch osvw ibs skinit wdt tce topoext perfctr_core perfctr_nb bpext perfctr_llc mwaitx cpb cat_l3 cdp_l3 hw_pstate ssbd mba perfmon_v2 ibrs ibpb stibp ibrs_enhanced vmmcall fsgsbase bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local avx512_bf16 clzero irperf xsaveerptr rdpru wbnoinvd amd_ppin cppc arat npt lbrv svm_lock nrip_save tsc_scale vmcb_clean flushbyasid decodeassists pausefilter pfthreshold avic v_vmsave_vmload vgif x2avic v_spec_ctrl vnmi avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg avx512_vpopcntdq rdpid overflow_recov succor smca fsrm flush_l1d debug_swap
Virtualization: AMD-V
L1d cache: 6 MiB (192 instances)
L1i cache: 6 MiB (192 instances)
L2 cache: 192 MiB (192 instances)
L3 cache: 768 MiB (24 instances)
NUMA node(s): 2
NUMA node0 CPU(s): 0-95,192-287
NUMA node1 CPU(s): 96-191,288-383
Vulnerability Gather data sampling: Not affected
Vulnerability Itlb multihit: Not affected
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Not affected
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Not affected
Vulnerability Spec rstack overflow: Vulnerable
Vulnerability Spec store bypass: Vulnerable
Vulnerability Spectre v1: Vulnerable: __user pointer sanitization and usercopy barriers only; no swapgs barriers
Vulnerability Spectre v2: Vulnerable; IBPB: disabled; STIBP: disabled; PBRSB-eIBRS: Not affected; BHI: Not affected
Vulnerability Srbds: Not affected
Vulnerability Tsx async abort: Not affected

Versions of relevant libraries:
[pip3] intel-cmplr-lib-ur==2025.3.3
[pip3] intel-openmp==2025.3.3
[pip3] mkl-include==2025.3.1
[pip3] mkl-static==2025.3.1
[pip3] mypy_extensions==1.1.0
[pip3] numpy==2.2.6
[pip3] nvidia-cudnn-frontend==1.18.0
[pip3] onemkl-license==2025.3.1
[pip3] optree==0.19.0
[pip3] tbb==2022.3.1
[pip3] tbb-devel==2022.3.1
[pip3] tcmlib==1.4.1
[pip3] torch==2.12.0a0+gite7bcaf2
[pip3] torch_c_dlpack_ext==0.1.5
[pip3] torchdata==0.11.0
[pip3] torchmonarch==0.3.0
[pip3] torchstore==0.0.0.dev0
[pip3] torchtitan==0.2.2
[pip3] torchx-nightly==2026.3.26
[pip3] triton==3.6.0+git7e48d5df
[pip3] umf==1.0.3
[conda] intel-cmplr-lib-ur 2025.3.3 pypi_0 pypi
[conda] intel-openmp 2025.3.3 pypi_0 pypi
[conda] mkl-include 2025.3.1 pypi_0 pypi
[conda] mkl-static 2025.3.1 pypi_0 pypi
[conda] numpy 2.2.6 pypi_0 pypi
[conda] nvidia-cudnn-frontend 1.18.0 pypi_0 pypi
[conda] onemkl-license 2025.3.1 pypi_0 pypi
[conda] optree 0.19.0 pypi_0 pypi
[conda] tbb 2022.3.1 pypi_0 pypi
[conda] tbb-devel 2022.3.1 pypi_0 pypi
[conda] tcmlib 1.4.1 pypi_0 pypi
[conda] torch 2.12.0a0+gite7bcaf2 pypi_0 pypi
[conda] torch-c-dlpack-ext 0.1.5 pypi_0 pypi
[conda] torchdata 0.11.0 pypi_0 pypi
[conda] torchmonarch 0.3.0 pypi_0 pypi
[conda] torchstore 0.0.0.dev0 pypi_0 pypi
[conda] torchtitan 0.2.2 pypi_0 pypi
[conda] torchx-nightly 2026.3.26 pypi_0 pypi
[conda] triton 3.6.0+git7e48d5df pypi_0 pypi
[conda] umf 1.0.3 pypi_0 pypi

cc @chauhang @penguinwu @voznesenskym @EikanWang @jgong5 @Guobing-Chen @XiaobingSuper @zhuhaozhe @blzheng @wenzhe-nrv @jiayisunx @ipiszy @kadeng @muchulee8 @amjames @aakhundov @coconutruben @jataylo @bertmaher @int3 @davidberard98 @nmacchioni @embg @peterbell10 @iupaikov-amd @Chillee @drisspg @yanboliang @BoyuanFeng @liangel-02 @howardzhang-cv

Activity

  1. Lucaskabela commented on Mar 26, 2026

    @Lucaskabela
    ContributorAuthor
  2. added
    upstream triton
    on Mar 26, 2026
  3. Lucaskabela commented on Mar 26, 2026

    @Lucaskabela
    ContributorAuthor

    Suggested fix to upstream triton (again courtesy of claude) - This change to llvm.cc should be submitted as a PR to triton-lang/triton

    Added vectorize-slp=false after the existing slp-copyable-elements hack (line 531). This disables SLP vectorization via the LLVM cl::opt while keeping the SLP pass in the
    pipeline (preserving the scheduling benefits noted in the TODO). Can be overridden with DISABLE_LLVM_OPT=vectorize-slp.

  4. Lucaskabela commented on Mar 26, 2026

    @Lucaskabela
    ContributorAuthor

    cc @eqy

  5. Lucaskabela commented on Mar 26, 2026

    @Lucaskabela
    ContributorAuthor

    I can take point on this if we agree this is a reasonable course of action :) Just want to make sure there isn't something I am missing with the usage that could explain this issue

  6. added this to the 2.12.0 milestone on Mar 26, 2026
  7. drisspg commented on Mar 27, 2026

    @drisspg
    Contributor

    This is pretty vanilla block mask creation usage. This func has one of the most complicated reductions that always seems to tickle the triton demons

  8. desertfire commented on Mar 27, 2026

    @desertfire
    Contributor
  9. htyu commented on Mar 27, 2026

    @htyu
    Contributor

    DISABLE_LLVM_OPT=1 might be a bit overfit, as that disabler the whole llvm optimization pipeline. Can you try DISABLE_LLVM_OPT=disable-slp-vectorization instead?

  10. CRobeck commented on Mar 30, 2026

    @CRobeck
    Contributor

    We ran into a similar issue in triton-lang/triton#8908 which was root caused to inductor requesting huge block sizes and crashing the LLVM backend. The fix ended up being tweaking the inductor auto tuning search space to reduce the requested block size maximums.

    I agree with @htyu. Blanket disabling all LLVM optimizations is not a good solution.

    FWIW, We did end up adding a top level tuning knob internally to disable that pass here:

    facebookexperimental/triton#742

    but decided we ultimately didn't need it after fixing the inductor configs.

  11. drisspg commented on Mar 30, 2026

    @drisspg
    Contributor

    Smaller more isolated Repo:

    cc @CRobeck @htyu seems pretty normal

    """Standalone repro for LLVM SLPVectorizer crash in Triton.
    
    Crashes with:
      llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp:2690:
      Assertion `FromIdx <= ToIdx && "Bad index"' failed.
    
    Kernel: triton_per_fused__to_copy_clone_slice_sort_sum_transpose_3
    Generated by torch.compile(create_block_mask)(causal_mask, 8, None, 2048, 2048)
    """
    import triton
    import triton.language as tl
    
    from torch._inductor.runtime import triton_helpers
    
    
    @triton.jit
    def triton_per_fused__to_copy_clone_slice_sort_sum_transpose_3(
        in_ptr0, out_ptr2, out_ptr3, xnumel, r0_numel, XBLOCK: tl.constexpr
    ):
        xnumel = 128
        r0_numel = 16
        R0_BLOCK: tl.constexpr = 16
        rnumel = r0_numel
        RBLOCK: tl.constexpr = R0_BLOCK
        xoffset = tl.program_id(0) * XBLOCK
        xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
        xmask = xindex < xnumel
        r0_index = tl.arange(0, R0_BLOCK)[None, :]
        r0_offset = 0
        r0_mask = r0_index < r0_numel
        roffset = r0_offset
        rindex = r0_index
        r0_2 = r0_index
        x0 = xindex % 16
        x1 = xindex // 16
        x3 = xindex
        tmp0 = tl.load(in_ptr0 + (x0 + 17 * r0_2 + 272 * x1), r0_mask & xmask, other=0.0)
        tmp1 = r0_2
        tmp2 = tmp1.to(tl.int16)
        tmp3 = tl.broadcast_to(tmp0, [XBLOCK, R0_BLOCK])
        tmp4 = tl.broadcast_to(tmp2, [XBLOCK, R0_BLOCK])
        tmp5, tmp6 = triton_helpers.sort_with_index(
            tmp3, tmp4, rnumel, 1, stable=True, descending=True
        )
        tmp7 = tmp0.to(tl.int64)
        tmp8 = tl.broadcast_to(tmp7, [XBLOCK, R0_BLOCK])
        tmp10 = tl.where(r0_mask & xmask, tmp8, 0)
        tmp11 = tl.sum(tmp10, 1)[:, None].to(tl.int64)
        tmp12 = tmp6.to(tl.int64)
        tmp13 = tmp12.to(tl.int32)
        tmp14 = tmp11.to(tl.int32)
        tl.store(out_ptr2 + (r0_2 + 16 * x3), tmp13, r0_mask & xmask)
        tl.store(out_ptr3 + (x3), tmp14, xmask)
    
    
    if __name__ == "__main__":
        from triton.compiler import ASTSource, compile as triton_compile
    
        src = ASTSource(
            fn=triton_per_fused__to_copy_clone_slice_sort_sum_transpose_3,
            signature={
                "in_ptr0": "*i32",
                "out_ptr2": "*i32",
                "out_ptr3": "*i32",
                "xnumel": "i32",
                "r0_numel": "i32",
            },
            constexprs={"XBLOCK": 128},
        )
        compiled = triton_compile(src)
        print("Compiled OK — no crash.")
  12. FindHao commented on Mar 30, 2026

    @FindHao
    Member

    The culprit triton commit is a llvm bump: triton-lang/triton@731540b
    not surprised. bisecting llvm now.

  13. 2 remaining items

  14. added
    triaged
    and removed on Mar 31, 2026
  15. Lucaskabela commented on Apr 6, 2026

    @Lucaskabela
    ContributorAuthor

    @FindHao any updates on backport here?

  16. FindHao commented on Apr 6, 2026

    @FindHao
    Member

    @FindHao any updates on backport here?

    I think we are in a loop circle. LOL, I asked driss if you guys have a final solution. let me ask triton release people.

  17. removed this from the 2.12.0 milestone on Apr 22, 2026
  18. atalman commented on Apr 22, 2026

    @atalman
    Collaborator

    revert for LLVM bump unblocked torchtitan and helped us remove the workaround . No further action for 2.12.0 release is needed for now

  19. drisspg commented on Apr 22, 2026

    @drisspg
    Contributor

    can we close @atalman

  20. jansel commented on May 15, 2026

    @jansel
    Contributor

    My agent was unable to reproduce this on 642b8f8. Reopen if you see this on the latest PyTorch nightly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    module: flex attentionmodule: inductoroncall: pt2triagedupstream triton

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions