apache / apache/tvm

[Bug] [Relax] `InternalError: Divide by zero` when compiling a `reshape` that yields a 0-sized dimension

Open
#19,922 5 comments 0 reactions 0 assignees View on GitHub
needs-triage type: bug
Dominant language
Python
Stars
13.7k
Forks
4k
Avg merge
2d 1h
Merged PRs (30d)
112

Description

### Expected behavior

`tvm.compile` should compile a Relax module containing a `reshape` whose result has a
0-sized dimension (e.g. reshaping an empty intermediate tensor to `(-1, 10)`, which yields
shape `(0, 10)`). A 0-sized dimension is a legal empty-tensor shape: eager PyTorch,
`torch.export`, and the `from_exported_program` frontend all accept this model, and eager
returns a valid `(0, 10)` tensor.

### Actual behavior

`tvm.compile` aborts at compile time with:
```
tvm.error.InternalError: Check failed: pb->value != 0 (0 vs. 0) : Divide by zero
File ".../src/tirx/op/op.cc", line 623, in tvm::floormod(tvm::PrimExpr, tvm::PrimExpr, tvm::Span)
if (auto ret = arith::TryConstFold(a, b)) return ret.value();
File ".../src/arith/const_fold.h", line 322, in tvm::arith::TryConstFold(...) [Op = tvm::tirx::FloorMod]
TVM_FFI_ICHECK_NE(pb->value, 0) << "Divide by zero";
```

### Environment

- TVM: 0.25.dev0 (commit `6b4b866d6`)
- PyTorch: 2.12.0+cu130
- Python: 3.12
- OS: Ubuntu x86_64
- Target: `llvm` (CPU)
- Frontend: `tvm.relax.frontend.torch.from_exported_program`

### Steps to reproduce

```python
import torch
import tvm
from tvm import relax
from tvm.relax.frontend.torch import from_exported_program

class M(torch.nn.Module):
def forward(self, x):
return x.reshape(-1)[: x.numel() // 10 * 10].reshape(-1, 10)

m = M()
x = torch.randn(3)

# eager mode
out = m(x)
print("shape:", tuple(out.shape))

# compile mode
ep = torch.export.export(m, (x,))
mod = from_exported_program(ep, keep_params_as_input=True, unwrap_unit_return_tuple=True)
mod, _ = relax.frontend.detach_params(mod)
tvm.compile(mod, target=tvm.target.Target("llvm"))
```

### Triage
* needs-triage

cc @junrushao

Contributor guide

No contributing guide indexed for this repository

Research direction

Reproduce the failure with the provided PyTorch and Relax export example, then inspect src/tirx/op/op.cc around the floormod implementation and src/arith/const_fold.h around the divide-by-zero check. Trace how the reshape producing (0, 10) reaches constant folding. Done means tvm.compile succeeds for the reproducer and preserves the expected empty-tensor shape.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
compilers, machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.