Skip to content

[Bug] Relax ONNX frontend folds integer Div through floating-point division for PrimExpr inputs #19974

Description

@viiccwen

Expected behavior

The Relax ONNX frontend should preserve ONNX integer Div semantics. ONNX defines integer Div as truncating division, rounding toward zero.

This behavior should hold both for constant tensors and for shape-derived scalar values represented as Relax/TIR primitive expressions.

Actual behavior

When an ONNX Div has integer inputs and one input is a shape-derived PrimExpr, the importer can fold it through NumPy floating-point division. This produces a floating-point TIR value such as T.float64(128.67676767) instead of an integer value such as T.int64(128).

If that result is then used as a slice bound or another index value, Relax import can fail because the downstream operator expects an integer primitive expression.

One failing pattern is:

Shape -> Gather -> Div -> Slice

where the Div result is used as the end argument of Slice.

Environment

  • Component: Relax ONNX frontend
  • Affected file: python/tvm/relax/frontend/onnx/onnx_frontend.py
  • Reproduced with a local TVM checkout using the Python ONNX frontend tests

Steps to reproduce

Create a graph where Shape(x)[0] is divided by an int64 constant and used as a
Slice bound:

x: float32[386]
Shape(x) -> Gather(axis=0) -> Div(3) -> Slice(x, starts=[0], ends=[Div result])

The importer may fail with an error similar to:

relax.strided_slice requires the end argument to be a tuple of int64 PrimExpr values
...
the end argument (T.float64(128.66666666666666),) has type T.Tuple(T.float64)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions