Skip to content

[Trn2/NKI] Is an all-reduce supported inside nl.fori_loop at LNC=2? 1 trip runs, 2 trips fault #1504

Done

Description

@sanchitmonga22

Describe the bug

On trn2, an NKI kernel at grid 2 that issues ncc.all_reduce inside an nl.fori_loop with a register trip count runs cleanly with 1 trip. With 2 trips in one execution, we see a device fault. We reproduced this as a minimal 1-trip vs 2-trip pair on two runtime versions. Each pair runs in its own fresh 4-rank spawn, with the 1-trip arm as the control.

We also tried several other forms of the loop body:

  • an ncc.collective_permute ring instead of the all-reduce
  • one buffer set per trip, indexed by the loop variable (rejected at trace: the tracer refuses arithmetic on the fori_loop variable)
  • the all-reduce on SBUF tiles instead of shared HBM
  • send buffers written once and fenced before the loop, with no read and no barrier inside the loop

None of them gave us a working multi-trip loop with a collective in it.

This blocks our whole-step decode design for GLM-5.3-Flash on trn2 (open source: https://github.com/RunanywhereAI/trnrt). A batch-24 decode step is ~42 ms, of which ~12 ms is fixed cost: 100+ cross-chip collectives and ~145 kernel launches across 45 layers. We'd like to roll the layers into one device-side loop, and every layer needs an all-reduce.

Model Name

GLM-5.3-Flash. The repro doesn't depend on the model: it's one all-reduce in a loop.

Describe the workload type

Inference (decode). The probe uses 4 ranks on one trn2 device, with the kernel launched at grid 2 (NEURON_LOGICAL_NC_CONFIG=2).

Instance Type

trn2.48xlarge

Release version

Baseline: Neuron SDK 2.32 (neuronx-cc 2.27.5334, NKI 0.6.0, runtime 2.34.10). We saw the same behavior on a second runtime version. We'll post the exact package list from the failing runs (apt list --installed | grep -i neuron, pip list) in a comment.

Reproduction Steps

Pseudocode of the minimal arm (all-reduce only in the loop). The real probe also checks both outputs on every rank bit-exact against a host emulation.

import nki, nki.isa as nisa, nki.language as nl, nki.collectives as ncc

@nki.jit
def ar_in_loop(Pp, L: int):                       # launched as ar_in_loop[2], 4 ranks on one device
    pid = nl.program_id(axis=0)
    ranks = [0, 1, 2, 3]
    R, HW = 8, 4096
    Y  = nl.ndarray((R, HW), dtype=nl.bfloat16, buffer=nl.shared_hbm)   # program 0's sum
    S0 = nl.ndarray((R, HW), dtype=nl.bfloat16, buffer=nl.shared_hbm)   # program 0 sends its rank's partial
    S1 = nl.ndarray((R, HW), dtype=nl.bfloat16, buffer=nl.shared_hbm)   # program 1 sends zeros
    Z1 = nl.ndarray((R, HW), dtype=nl.bfloat16, buffer=nl.shared_hbm)   # program 1's receive, never read
    # program 0 copies Pp into S0, program 1 zero-fills S1,
    # then core_barrier on S0 and on S1 (fenced once, before the loop)
    c0 = nl.ndarray((1, 1), dtype=nl.int32, buffer=nl.sbuf)
    nisa.iota(dst=c0, pattern=[[0, 1]], offset=L, channel_multiplier=0)
    rt = nisa.register_alloc()
    nisa.register_load(rt, c0)

    def body(t):
        # each trip also increments a small vector-engine counter (so the body is never empty)
        ncc.all_reduce(srcs=[S0 if pid == 0 else S1], dsts=[Y if pid == 0 else Z1],
                       replica_group=ncc.ReplicaGroup([ranks]), op=nl.add)

    nl.fori_loop(0, rt, body)
    nisa.core_barrier(data=Y, cores=(0, 1))
    # program 0 reads the first 512 columns of Y and multiplies them by L;
    # both programs write their outputs and fence out_y
  • L = 1: correct on every rank.
  • L = 2: device fault.

Possible Solution

AWS's own TorchNeuron kernels issue ncc.all_gather / ncc.all_reduce inside nl.fori_loop with a runtime trip count, and include trn2 tiling tables (torch_neuronx/python_ops/torch_mlir/ops/dynamic_kernels/dynamic_collectives.py at tag v2.33.0, b1b6c66). Those kernels also stage their tiles in each trip. So it looks supported in some form, and we'd like to know what that path needs that our kernel doesn't: a buffer layout, compiler flags, a minimum runtime or something else.

sglang-neuron's design notes report a related symptom on trn3: "A device loop (nl.fori_loop) whose body holds a collective fails at run time on trn3" (https://github.com/sgl-project/sglang-neuron/blob/4e2432622bfab2c6663d12286a6813ef1a40bfe6/docs/design/megakernel.md#L130).

Questions

  1. Is a collective inside nl.fori_loop supported on trn2 at LNC=2 today? If so, what form is required?
  2. If not, is support planned?
  3. Is there a workaround that keeps one execution per decode step?

Activity

  1. added
    bug
    on Nov 26, 2026
  2. github-actions commented on Nov 26, 2026

    @github-actions

    Hi @sanchitmonga22, Thank you for filing the issue! We will take a look and get back to you.

  3. romagros-aws commented on Nov 30, 2026

    @romagros-aws
    Contributor

    Hi, @sanchitmonga22, thank you for the detailed report.

    Q1: Is a collective inside nl.fori_loop supported on trn2 at LNC=2?

    Yes, it is supported in the latest SDK. This was a limitation in NKI 0.6.0. You can use Neuron SDK 2.33 (NKI 0.7.0), or the latest Neuron SDK 2.34 (NKI 0.8.0).

    Please upgrade and feel free to reopen the ticket / cut another ticket if you are still facing issues.

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

    Trn2buginference

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions