pytorch - 💡(How to fix) Fix [inductor] argmax over comparison mask is accepted and used for indexing while eager raises

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Error Message

import torch import traceback

def fn(scores, values): mask = scores > 0.0 idx = torch.argmax(mask, dim=1) return values[idx].sum()

scores = torch.tensor( [[-1.0, 2.0, -3.0], [4.0, -5.0, 6.0]], dtype=torch.float32, )

values = torch.tensor([10.0, 20.0, 30.0])

try: print("eager:", fn(scores, values)) except Exception as e: print("eager error:", type(e).name, str(e).splitlines()[0])

for backend in ["eager", "aot_eager", "inductor"]: try: cf = torch.compile(fn, backend=backend, fullgraph=True) print(backend, "compiled:", cf(scores, values)) except Exception as e: print(backend, "compiled error:", type(e).name, str(e).splitlines()[0]) traceback.print_exc(limit=4)

Code Example

import torch
import traceback

def fn(scores, values):
    mask = scores > 0.0
    idx = torch.argmax(mask, dim=1)
    return values[idx].sum()

scores = torch.tensor(
    [[-1.0, 2.0, -3.0],
     [4.0, -5.0, 6.0]],
    dtype=torch.float32,
)

values = torch.tensor([10.0, 20.0, 30.0])

try:
    print("eager:", fn(scores, values))
except Exception as e:
    print("eager error:", type(e).__name__, str(e).splitlines()[0])

for backend in ["eager", "aot_eager", "inductor"]:
    try:
        cf = torch.compile(fn, backend=backend, fullgraph=True)
        print(backend, "compiled:", cf(scores, values))
    except Exception as e:
        print(backend, "compiled error:", type(e).__name__, str(e).splitlines()[0])
        traceback.print_exc(limit=4)

---

eager error: RuntimeError argmax(): does not support bool input
eager compiled error: RuntimeError argmax(): does not support bool input
aot_eager compiled error: RuntimeError argmax(): does not support bool input
inductor compiled: tensor(30.)
RAW_BUFFERClick to expand / collapse

🐛 Describe the bug

torch.argmax rejects bool input in eager mode. The same error is also preserved by torch.compile(..., backend="eager") and torch.compile(..., backend="aot_eager").

However, when the bool tensor is produced by a comparison expression such as scores > 0.0, torch.compile(..., backend="inductor", fullgraph=True) silently accepts torch.argmax on the bool mask. The resulting index is then consumed by downstream indexing, and the compiled graph returns a final numeric value.

This is an Inductor-only eager-parity issue. Compiled execution should raise the same RuntimeError as eager, rather than producing an index from an unsupported bool argmax.

Expected behavior: Inductor should raise RuntimeError: argmax(): does not support bool input.

Actual behavior: Inductor returns tensor(30.).

import torch
import traceback

def fn(scores, values):
    mask = scores > 0.0
    idx = torch.argmax(mask, dim=1)
    return values[idx].sum()

scores = torch.tensor(
    [[-1.0, 2.0, -3.0],
     [4.0, -5.0, 6.0]],
    dtype=torch.float32,
)

values = torch.tensor([10.0, 20.0, 30.0])

try:
    print("eager:", fn(scores, values))
except Exception as e:
    print("eager error:", type(e).__name__, str(e).splitlines()[0])

for backend in ["eager", "aot_eager", "inductor"]:
    try:
        cf = torch.compile(fn, backend=backend, fullgraph=True)
        print(backend, "compiled:", cf(scores, values))
    except Exception as e:
        print(backend, "compiled error:", type(e).__name__, str(e).splitlines()[0])
        traceback.print_exc(limit=4)

Output:

eager error: RuntimeError argmax(): does not support bool input
eager compiled error: RuntimeError argmax(): does not support bool input
aot_eager compiled error: RuntimeError argmax(): does not support bool input
inductor compiled: tensor(30.)

This may be related to existing bool argmin/argmax correctness work, but the issue here is specifically that eager rejects torch.argmax on bool input while Inductor silently accepts it and allows the unsupported result to affect downstream indexing.

Versions

PyTorch version: 2.10.0+cpu

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING