This is an automated email from the ASF dual-hosted git repository.

tqchen pushed a commit to branch sequal-upgrade
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/sequal-upgrade by this push:
     new 64724453ab Add Nan handling
64724453ab is described below

commit 64724453abc88c5e18534882c5bb6a551293e187
Author: tqchen <[email protected]>
AuthorDate: Sat Jul 26 09:30:54 2025 -0400

    Add Nan handling
---
 ffi/src/ffi/reflection/structural_equal.cc | 6 ++++++
 ffi/src/ffi/reflection/structural_hash.cc  | 9 +++++++++
 2 files changed, 15 insertions(+)

diff --git a/ffi/src/ffi/reflection/structural_equal.cc 
b/ffi/src/ffi/reflection/structural_equal.cc
index 8ce6dba135..fff6fd0c1d 100644
--- a/ffi/src/ffi/reflection/structural_equal.cc
+++ b/ffi/src/ffi/reflection/structural_equal.cc
@@ -29,6 +29,7 @@
 #include <tvm/ffi/reflection/structural_equal.h>
 #include <tvm/ffi/string.h>
 
+#include <cmath>
 #include <unordered_map>
 
 namespace tvm {
@@ -49,7 +50,12 @@ class StructEqualHandler {
     if (lhs_data->type_index != rhs_data->type_index) {
       return false;
     }
+
     if (lhs_data->type_index < TypeIndex::kTVMFFIStaticObjectBegin) {
+      // specially handle nan for float, as there can be multiple 
representations of nan
+      if (lhs_data->type_index == TypeIndex::kTVMFFIFloat && 
std::isnan(lhs_data->v_float64)) {
+        return std::isnan(rhs_data->v_float64);
+      }
       // this is POD data, we can just compare the value
       return lhs_data->v_int64 == rhs_data->v_int64;
     }
diff --git a/ffi/src/ffi/reflection/structural_hash.cc 
b/ffi/src/ffi/reflection/structural_hash.cc
index 80e3afe55c..c19212223d 100644
--- a/ffi/src/ffi/reflection/structural_hash.cc
+++ b/ffi/src/ffi/reflection/structural_hash.cc
@@ -32,6 +32,8 @@
 
 #include <unordered_map>
 #include <utility>
+#include <limits>
+#include <cmath>
 
 namespace tvm {
 namespace ffi {
@@ -48,6 +50,13 @@ class StructuralHashHandler {
     const TVMFFIAny* src_data = AnyUnsafe::TVMFFIAnyPtrFromAny(src);
 
     if (src_data->type_index < TypeIndex::kTVMFFIStaticObjectBegin) {
+      // specially handle nan for float, as there can be multiple 
representations of nan
+      // make sure they map to the same hash value
+      if (src_data->type_index == TypeIndex::kTVMFFIFloat && 
std::isnan(src_data->v_float64)) {
+        TVMFFIAny temp = *src_data;
+        temp.v_float64 = std::numeric_limits<double>::quiet_NaN();
+        return details::StableHashCombine(temp.type_index, temp.v_uint64);
+      }
       // this is POD data, we can just hash the value
       return details::StableHashCombine(src_data->type_index, 
src_data->v_uint64);
     }

Reply via email to