You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "viiccwen (via GitHub)" <gi...@apache.org> on 2026/07/10 06:27:41 UTC

[PR] [Fix][Relax][ONNX] Preserve integer Div truncation during import [tvm]

viiccwen opened a new pull request, #19975:
URL: https://github.com/apache/tvm/pull/19975

   ONNX integer Div uses truncating division, rounding toward zero. The Relax ONNX frontend already special-cased integer Div to detect zero divisors, but its PrimExpr folding path could still use NumPy floating-point division when one of the inputs was a shape-derived PrimExpr.
   
   That behavior can produce floating-point TIR values for integer shape/index computations. For example, a `Shape -> Gather -> Div -> Slice` subgraph can produce `T.float64(128.666...)` as a Slice bound, which Relax rejects because strided_slice expects integer PrimExpr bounds.
   
   This patch handles scalar integer Div inputs that contain a PrimExpr using TIR `truncdiv`, preserving ONNX semantics while keeping shape computations in TIR instead of routing them through NumPy. Constant tensor Div continues to use the existing generic binary constant-folding path.
   
   The regression tests cover:
   
   - integer constant folding with negative values to distinguish truncation from floor division
   - a shape-derived PrimExpr Div used as a Slice bound
   - integer zero-divisor error handling
   
   Verification:
   
   - `python -m pytest tests/python/relax/test_frontend_onnx.py::test_div_integer_constant_zero_divisor_raises_valueerror tests/python/relax/test_frontend_onnx.py::test_div_integer_constant_folding_truncates_toward_zero tests/python/relax/test_frontend_onnx.py::test_div_integer_primexpr_folding_truncates_toward_zero -q`
   
   Fixes #19974


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Fix][Relax][ONNX] Preserve integer Div truncation during import [tvm]

Posted by "viiccwen (via GitHub)" <gi...@apache.org>.
viiccwen commented on PR #19975:
URL: https://github.com/apache/tvm/pull/19975#issuecomment-4937182457

   cc @guan404ming, @tlopex. 🙌


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Fix][Relax][ONNX] Preserve integer Div truncation during import [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19975:
URL: https://github.com/apache/tvm/pull/19975#discussion_r3556841485


##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -527,11 +527,37 @@ class Div(BinaryBase):
     numpy_op = _np.divide
     relax_op = relax.op.divide
 
+    @staticmethod
+    def _as_scalar_prim_expr(expr, dtype):
+        if tvm.ir.is_prim_expr(expr):
+            return expr
+        if isinstance(expr, relax.Constant) and expr.data.shape == ():
+            return tirx.const(expr.data.numpy().item(), dtype)
+        return None

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   The static method `_as_scalar_prim_expr` checks if `expr` is a `PrimExpr` using `tvm.ir.is_prim_expr(expr)`. However, shape-derived values in Relax are often wrapped in `relax.PrimValue` (for example, the output of `Gather` on a `ShapeExpr` returns `relax.prim_value(shape_val)`). Since `relax.PrimValue` is a subclass of `relax.Expr` and not `tvm.ir.PrimExpr`, `tvm.ir.is_prim_expr` will return `False` for it. We should also check for and unwrap `relax.PrimValue` to extract its underlying `PrimExpr` via `.value`.
   
   ```suggestion
       @staticmethod
       def _as_scalar_prim_expr(expr, dtype):
           if isinstance(expr, relax.PrimValue):
               return expr.value
           if tvm.ir.is_prim_expr(expr):
               return expr
           if isinstance(expr, relax.Constant) and expr.data.shape == ():
               return tirx.const(expr.data.numpy().item(), dtype)
           return None
   ```



##########
python/tvm/relax/frontend/onnx/onnx_frontend.py:
##########
@@ -540,9 +566,15 @@ def _impl_v7(cls, bb, inputs, attr, params):
         if not (lhs_is_integer and rhs_is_integer):
             return cls.base_impl(bb, inputs, attr, params)
 
-        if isinstance(inputs[1], relax.Constant) and bool(_np.any(inputs[1].data.numpy() == 0)):
+        if cls._is_zero(inputs[1]):
             raise ValueError("ONNX Div with integer inputs encountered divisor value 0.")
 
+        has_prim_expr = any(tvm.ir.is_prim_expr(inp) for inp in inputs)

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   Similar to `_as_scalar_prim_expr`, `has_prim_expr` is computed using `any(tvm.ir.is_prim_expr(inp) for inp in inputs)`. If the inputs contain a `relax.PrimValue` (which is a Relax expression, not a TIR `PrimExpr`), `tvm.ir.is_prim_expr` will return `False`. This will cause `has_prim_expr` to be `False`, bypassing the `tirx.truncdiv` path and falling back to `base_impl`, which can fail with an `AttributeError` because `relax.PrimValue` does not have a `data` attribute. We should also check for `relax.PrimValue` when determining `has_prim_expr`.
   
   ```suggestion
           has_prim_expr = any(tvm.ir.is_prim_expr(inp) or isinstance(inp, relax.PrimValue) for inp in inputs)
   ```



-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Fix][Relax][ONNX] Preserve integer Div truncation during import [tvm]

Posted by "tlopex (via GitHub)" <gi...@apache.org>.
tlopex merged PR #19975:
URL: https://github.com/apache/tvm/pull/19975


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Fix][Relax][ONNX] Preserve integer Div truncation during import [tvm]

Posted by "yongwww (via GitHub)" <gi...@apache.org>.
yongwww closed pull request #19975: [Fix][Relax][ONNX] Preserve integer Div truncation during import
URL: https://github.com/apache/tvm/pull/19975


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [Fix][Relax][ONNX] Preserve integer Div truncation during import [tvm]

Posted by "viiccwen (via GitHub)" <gi...@apache.org>.
viiccwen commented on PR #19975:
URL: https://github.com/apache/tvm/pull/19975#issuecomment-4932978140

   In the current Relax Python API, `relax.prim_value` does not produce a separate `relax.PrimValue` wrapper. It returns a TIR PrimExpr directly for both PrimExpr inputs and Python scalar inputs, so `tvm.ir.is_prim_expr(...)` already covers these values. I verified this locally, and there does not appear to be a `relax.PrimValue` Python class to unwrap.
   
   Thx Gemini.
   
   <img width="232" height="186" alt="image" src="https://github.com/user-attachments/assets/4a358a68-2a9c-4950-8529-d6df679c5ad3" />
   
   


-- 
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: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org