raindrops-0199 opened a new issue, #19676:
URL: https://github.com/apache/tvm/issues/19676
### Expected behavior
Compiling a model that reduces a 0-dimensional (scalar) tensor — e.g.
`torch.max(x.sum())` — should succeed. PyTorch and NumPy both define a
reduction over a scalar as the identity (it returns the scalar unchanged):
```python
>>> torch.max(torch.randn(4).sum()) # input is 0-D
tensor(-1.5686)
```
TVM should either lower a reduction over a 0-D tensor as the identity, or
raise a clear frontend error — not hit an internal assertion at compile time.
### Actual behavior
The model is accepted by `torch.export` and by
`relax.frontend.torch.from_exported_program`, but `tvm.compile` aborts with an
internal check failure during legalization:
```
InternalError: Check failed: ndim != 0 (0 vs. 0) : Cannot reduce a 0 dim
Tensor
File ".../tvm/include/tvm/topi/reduction.h", line 187, in
tvm::topi::CommReduce(
const tvm::te::Tensor&, const Optional<Array<Integer>>&, FReduce,
bool, bool)
TVM_FFI_ICHECK_NE(ndim, 0) << "Cannot reduce a 0 dim Tensor";
```
`torch.max(x)` (no `dim`) is converted to a reduction over all axes; with a
0-D input, `topi::CommReduce` asserts `ndim != 0` instead of treating it as
identity.
### Environment
- TVM: 0.25.dev0
- OS: Ubuntu 22.04 (Linux 6.8, x86_64)
- Python: 3.12
- PyTorch: 2.12.0
### Steps to reproduce
```python
import torch
import torch.nn as nn
from torch.export import export
import tvm
from tvm import relax
from tvm.relax.frontend.torch import from_exported_program
class M(nn.Module):
def forward(self, x):
return torch.max(x.sum()) # x.sum() -> 0-D scalar; torch.max
reduces it again
m = M().eval()
args = (torch.randn(4),)
ep = export(m, args)
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
* relax
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]