Skip to content

[Bug][Relax] x[:, i] = v writes garbage values when v has to be broadcast #20925

Open

Description

@raindrops-0199

Expected behavior

v is broadcast to the indexed shape, here (2, 2), so both selected columns get [1, 2]. It works in eager and torch.export, so the converted program should give the same result.

Actual behavior

The output has wrong values, which change from run to run. The same happens for a v of shape (2,), for x[i, j] = v with index tensors that broadcast (i of shape (2, 1), j of shape (2,)), and for index_put_(..., accumulate=True). A v that already has the indexed shape, or a 0-d v, works.

eager: [[1.0, 0.0, 1.0], [2.0, 0.0, 2.0]]
tvm:   [[1.0, 0.0, 2.0], [1.1350517561031018e-43, 0.0, 0.0]]

Possibly neither _index_put nor relax.op.index_put broadcasts values to the broadcast index shape, while topi.index_put reads values[k] for every index position.

Environment

  • TVM: main @ 2bf0532 (0.28.dev0), built from source, target llvm
  • PyTorch: 2.12.0+cu130
  • Python: 3.12, Ubuntu x86_64

Steps to reproduce

import torch
import tvm
import tvm_tirx
from tvm import relax
from tvm.relax.frontend.torch import from_exported_program


class M(torch.nn.Module):
    def forward(self, x, v):
        x = x.clone()
        x[:, [0, 2]] = v
        return x


x = torch.zeros(2, 3)
v = torch.tensor([[1.0], [2.0]])
args = (x, v)
print("eager:", M()(*args).tolist())

ep = torch.export.export(M(), args)
mod = from_exported_program(ep)
ex = tvm.compile(mod, target="llvm")
vm = relax.VirtualMachine(ex, tvm_tirx.cpu())
out = vm["main"](*[tvm_tirx.runtime.tensor(a.numpy()) for a in args])
print("tvm:  ", out[0].numpy().tolist())

Activity

  1. added
    needs-triagePRs or issues that need to be investigated by maintainers to find the right assignees to address it
    on Nov 6, 2026
  2. GoyalIshaan commented on Nov 6, 2026

    @GoyalIshaan

    Could I work on the index_put values-broadcasting bug, including CPU regression tests for broadcasted values and accumulate=True? Please let me know if this is available.

    AI-generated comment posted with Ishaan's authorization.

  3. YidaWeng commented on Nov 15, 2026

    @YidaWeng

    I had a working patch for this locally and just opened #20984. It broadcasts values to the indexed-region shape in topi.index_put, where the flat values[k] access goes out of bounds, and adds topi plus Relax end-to-end regression tests. The new Relax test fails on current main and passes with the patch.

    I saw your earlier note asking to work on this. If you are actively working on it, let me know and we can consolidate; I am happy to review your patch or hand this over instead of keeping a competing PR.

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

    needs-triagePRs or issues that need to be investigated by maintainers to find the right assignees to address ittype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions