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 "swjng (via GitHub)" <gi...@apache.org> on 2026/04/08 05:53:11 UTC

[PR] [BugFix] Align tir.round to ties-to-even across all backends [tvm]

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

   ## Problem
   
   `tir.round` constant-folds using `std::nearbyint` (IEEE 754 ties-to-even), but all backends lower it to platform `round()` which uses ties-away-from-zero. This means compiled code can produce different results from constant-folded code for midpoint values:
   
   | Input | Constant-fold (ties-to-even) | Compiled (ties-away) |
   |-------|-----|------|
   | 0.5   | 0.0 | 1.0  |
   | 2.5   | 2.0 | 3.0  |
   | -0.5  | 0.0 | -1.0 |
   
   This was identified as a follow-up to #19367 — see [this comment](https://github.com/apache/tvm/pull/19367#issuecomment-4201800320).
   
   ## Fix
   
   Align all backends to use ties-to-even intrinsics, matching the constant-folding behavior:
   
   | Backend | Before | After |
   |---------|--------|-------|
   | LLVM/ROCm/Hexagon | `llvm::Intrinsic::round` | `llvm::Intrinsic::nearbyint` |
   | NVPTX | `__nv_round[f]` | `__nv_nearbyint[f]` |
   | CUDA | `round`/`roundf` | `nearbyint`/`nearbyintf` (f16/bf16 already used `hrint`) |
   | Metal/OpenCL | `round` | `rint` |
   | Vulkan/SPIR-V | `GLSLstd450Round` | `GLSLstd450RoundEven` |
   
   Also fixes OpenCL codegen where `tir.nearbyint` was incorrectly mapped to OpenCL `round()` instead of `rint()`.
   
   Updates `op.h` documentation to explicitly state ties-to-even semantics for both `round()` and `nearbyint()`.
   
   ## Testing
   
   ```
   python -m pytest tests/python/tirx-base/test_tir_intrin.py -xvs
   ```
   
   New `test_round_ties_to_even` verifies midpoint inputs `[0.5, 1.5, 2.5, 3.5, -0.5, -1.5, -2.5, -3.5]` produce ties-to-even results on the LLVM backend. All 12 tests pass (10 passed, 2 skipped for CUDA).


-- 
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] [BugFix] Align `tir.round` to ties-to-even across all backends [tvm]

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


##########
src/target/source/intrin_rule_opencl.cc:
##########
@@ -47,7 +47,16 @@ TVM_REGISTER_OP("tirx.fabs")
     .set_attr<FLowerIntrinsic>("opencl.FLowerIntrinsic", DispatchPureExtern<Direct>);
 
 TVM_REGISTER_OP("tirx.round")
-    .set_attr<FLowerIntrinsic>("opencl.FLowerIntrinsic", DispatchPureExtern<Direct>);
+    .set_attr<FLowerIntrinsic>("opencl.FLowerIntrinsic", [](const PrimExpr& e) -> PrimExpr {
+      // OpenCL's rint() uses ties-to-even, matching constant-folding semantics.
+      const tirx::CallNode* call = e.as<tirx::CallNode>();
+      TVM_FFI_ICHECK(call != nullptr);
+      ffi::Array<PrimExpr> new_args = {StringImm("rint")};

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   `StringImm` is missing the `tirx::` namespace prefix. This will likely cause a compilation error as the `tirx` namespace is not imported in this scope.
   
   ```suggestion
         ffi::Array<PrimExpr> new_args = {tirx::StringImm("rint")};
   ```



##########
src/target/source/intrin_rule_metal.cc:
##########
@@ -68,7 +68,16 @@ TVM_REGISTER_OP("tirx.fabs")
     .set_attr<FLowerIntrinsic>("metal.FLowerIntrinsic", DispatchPureExtern<Direct>);
 
 TVM_REGISTER_OP("tirx.round")
-    .set_attr<FLowerIntrinsic>("metal.FLowerIntrinsic", DispatchPureExtern<Direct>);
+    .set_attr<FLowerIntrinsic>("metal.FLowerIntrinsic", [](const PrimExpr& e) -> PrimExpr {
+      // Metal's rint() uses ties-to-even, matching constant-folding semantics.
+      const tirx::CallNode* call = e.as<tirx::CallNode>();
+      TVM_FFI_ICHECK(call != nullptr);
+      ffi::Array<PrimExpr> new_args = {StringImm("rint")};

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   `StringImm` is missing the `tirx::` namespace prefix. Since there is no `using namespace tirx;` in this scope, this will likely cause a compilation error. Note that `tirx::Call` and `tirx::builtin` are correctly prefixed later in the same lambda.
   
   ```suggestion
         ffi::Array<PrimExpr> new_args = {tirx::StringImm("rint")};
   ```



##########
src/target/llvm/intrin_rule_nvptx.cc:
##########
@@ -66,7 +66,20 @@ TVM_REGISTER_OP("tirx.ceil")
     .set_attr<FLowerIntrinsic>("nvptx.FLowerIntrinsic", DispatchPureExternLibDevice);
 
 TVM_REGISTER_OP("tirx.round")
-    .set_attr<FLowerIntrinsic>("nvptx.FLowerIntrinsic", DispatchPureExternLibDevice);
+    .set_attr<FLowerIntrinsic>("nvptx.FLowerIntrinsic", [](const PrimExpr& e) -> PrimExpr {
+      // Use nearbyint (ties-to-even) instead of round (ties-away-from-zero)
+      // to match constant-folding semantics.
+      using namespace tirx;
+      const CallNode* call = e.as<CallNode>();
+      TVM_FFI_ICHECK(call != nullptr);
+      TVM_FFI_ICHECK(call->dtype.bits() == 32 || call->dtype.bits() == 64);

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The `TVM_FFI_ICHECK` is missing an error message. For better diagnostics and consistency with `DispatchPureExternLibDevice` in the same file, it should include a descriptive message.
   
   ```suggestion
         TVM_FFI_ICHECK(call->dtype.bits() == 32 || call->dtype.bits() == 64) << "Only support float32 or float64.";
   ```



-- 
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] [BugFix] Align `tir.round` to ties-to-even across all backends [tvm]

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

   @LeiWang1999 What issue did you meet?
   also cc @swjng 


-- 
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] [BugFix] Align `tir.round` to ties-to-even across all backends [tvm]

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


-- 
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] [BugFix] Align `tir.round` to ties-to-even across all backends [tvm]

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

   just curious and I came aross an issue for this change, if user want to codegen `roundf` in cuda, is there currently no suitable method for that?"


-- 
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] [BugFix] Align `tir.round` to ties-to-even across all backends [tvm]

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

   @LeiWang1999 @tlopex Implementation shouldn't be hard. The lowerings this PR replaced map almost 1:1 onto a new intrinsic. Adding e.g. `tir.round_ties_away` (→ `llvm.round`, `__nv_round[f]`, CUDA `roundf`, OpenCL/Metal `round`, SPIR-V `GLSLstd450Round`, `std::round` for constant folding) should cover it.


-- 
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] [BugFix] Align `tir.round` to ties-to-even across all backends [tvm]

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

   After thinking about it a bit more deeply, I agree with aligning the default rounding behavior with Python instead of cuda/metal. Thanks for your response and no further issues from my side!


-- 
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