[Bug][Relax][ONNX] NonZero returns the wrong output shape for scalar inputs

Abierto Apto para principiantes
#21,051 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 3 días

@tandede ya está trabajando en esto.

Desde el 25/11/2026.

  • #21061 de @tandede — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
70/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
python

Línea de trabajo

Empieza en python/tvm/relax/frontend/onnx/onnx_frontend.py, en el conversor NonZero, donde se construye la forma de salida y rank 0 se asigna a una dimensión inicial de 1. Luego revisa el caso escalar existente en tests/python/relax/test_frontend_onnx.py, que actualmente espera esa forma. Ejecuta el script de reproducción del issue. Terminado cuando una entrada escalar dé una salida (0, N) que coincida con ONNX, y la prueba escalar se actualice para esperarla.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

needs-triage type: bug

Thanks for participating in the TVM community! We use https://discuss.tvm.apache.org/ for any general usage questions and discussions. The issue tracker is used for actionable items such as feature proposals discussion, roadmaps, and bug tracking. You are always welcomed to post on the forum first 😸

Issues that are inactive for a period of time may get closed. We adopt this policy so that we won't lose track of actionable issues that may fall at the bottom of the pile. Feel free to reopen a new one if you feel there is an additional problem that needs attention when an old one gets closed.

Expected behavior

For a scalar input, ONNX opset 13 defines the output shape of NonZero as (0, N), where N is the number of nonzero elements. For example, a float32 scalar with value 2 should produce an int64 tensor of shape (0, 1). ONNX NonZero-13 specification

Actual behavior

TVM imports and executes the model, but the output has a leading dimension of 1 for both scalar inputs. A downstream ONNX Shape node also returns [1, 1] for scalar 2.

Input Expected shape TVM output shape
Scalar 0 (0, 0) (1, 0)
Scalar 2 (0, 1) (1, 1)
Vector [0] (1, 0) (1, 0)
Vector [2] (1, 1) (1, 1)

All four models pass ONNX validation. ONNX Runtime and the ONNX Python reference implementation also return the shapes in the TVM column. ONNX Runtime emits an output shape warning for both scalar inputs.

The ONNX converter explicitly maps input rank 0 to 1 when constructing the output shape. The existing scalar frontend test expects this leading dimension of 1. Converter at the tested commit · Scalar test

Environment
  • TVM commit 3d89c4991c5a37492f470800231a5ea8f57a95c6, LLVM 21, CPU
  • Linux x86-64 (WSL2), Python 3.10.22
  • ONNX 1.21.0, ONNX Runtime 1.23.2
Steps to reproduce
import numpy as np
import onnx
import tvm_tirx
from onnx import TensorProto, helper
from tvm import relax
from tvm.relax.frontend.onnx import from_onnx

x = np.asarray(2, dtype=np.float32)
graph = helper.make_graph(
    [helper.make_node("NonZero", ["X"], ["Y"])],
    "scalar_nonzero",
    [helper.make_tensor_value_info("X", TensorProto.FLOAT, [])],
    [helper.make_tensor_value_info("Y", TensorProto.INT64, ["rank", "count"])],
)
model = helper.make_model(
    graph, opset_imports=[helper.make_opsetid("", 13)], ir_version=8
)
onnx.checker.check_model(model, full_check=True)

mod = from_onnx(model, keep_params_in_input=False)
vm = relax.VirtualMachine(relax.build(mod, target="llvm"), tvm_tirx.cpu())
result = vm["main"](tvm_tirx.runtime.tensor(x))
print(result.numpy().shape)  # Actual: (1, 1). Expected: (0, 1).
Triage
  • needs-triage
Lenguaje dominante
Python
Estrellas
13.9k
Forks
4.1k
Merge medio
4 d 22 h
PR fusionados (30 d)
22

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de apache/tvm

Todos los issues de apache/tvm

Issues similares

Más issues de Python

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.