tvm
structural_equal.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
23 #ifndef TVM_NODE_STRUCTURAL_EQUAL_H_
24 #define TVM_NODE_STRUCTURAL_EQUAL_H_
25 
26 #include <tvm/ffi/container/array.h>
27 #include <tvm/ffi/reflection/access_path.h>
28 #include <tvm/node/functor.h>
29 #include <tvm/runtime/data_type.h>
30 
31 #include <cmath>
32 #include <string>
33 
34 namespace tvm {
35 
40  public:
41  bool operator()(const double& lhs, const double& rhs) const {
42  if (std::isnan(lhs) && std::isnan(rhs)) {
43  // IEEE floats do not compare as equivalent to each other.
44  // However, for the purpose of comparing IR representation, two
45  // NaN values are equivalent.
46  return true;
47  } else if (std::isnan(lhs) || std::isnan(rhs)) {
48  return false;
49  } else if (lhs == rhs) {
50  return true;
51  } else {
52  // fuzzy float pt comparison
53  constexpr double atol = 1e-9;
54  double diff = lhs - rhs;
55  return diff > -atol && diff < atol;
56  }
57  }
58 
59  bool operator()(const int64_t& lhs, const int64_t& rhs) const { return lhs == rhs; }
60  bool operator()(const uint64_t& lhs, const uint64_t& rhs) const { return lhs == rhs; }
61  bool operator()(const Optional<int64_t>& lhs, const Optional<int64_t>& rhs) const {
62  return lhs == rhs;
63  }
64  bool operator()(const Optional<double>& lhs, const Optional<double>& rhs) const {
65  return lhs == rhs;
66  }
67  bool operator()(const int& lhs, const int& rhs) const { return lhs == rhs; }
68  bool operator()(const bool& lhs, const bool& rhs) const { return lhs == rhs; }
69  bool operator()(const std::string& lhs, const std::string& rhs) const { return lhs == rhs; }
70  bool operator()(const DataType& lhs, const DataType& rhs) const { return lhs == rhs; }
71  template <typename ENum, typename = typename std::enable_if<std::is_enum<ENum>::value>::type>
72  bool operator()(const ENum& lhs, const ENum& rhs) const {
73  return lhs == rhs;
74  }
75 };
76 
98  public:
99  // inheritate operator()
100  using BaseValueEqual::operator();
108  TVM_DLL bool operator()(const ffi::Any& lhs, const ffi::Any& rhs,
109  const bool map_free_params = false) const;
110 };
111 
112 } // namespace tvm
113 #endif // TVM_NODE_STRUCTURAL_EQUAL_H_
Equality definition of base value class.
Definition: structural_equal.h:39
bool operator()(const Optional< double > &lhs, const Optional< double > &rhs) const
Definition: structural_equal.h:64
bool operator()(const ENum &lhs, const ENum &rhs) const
Definition: structural_equal.h:72
bool operator()(const DataType &lhs, const DataType &rhs) const
Definition: structural_equal.h:70
bool operator()(const uint64_t &lhs, const uint64_t &rhs) const
Definition: structural_equal.h:60
bool operator()(const int64_t &lhs, const int64_t &rhs) const
Definition: structural_equal.h:59
bool operator()(const int &lhs, const int &rhs) const
Definition: structural_equal.h:67
bool operator()(const Optional< int64_t > &lhs, const Optional< int64_t > &rhs) const
Definition: structural_equal.h:61
bool operator()(const std::string &lhs, const std::string &rhs) const
Definition: structural_equal.h:69
bool operator()(const double &lhs, const double &rhs) const
Definition: structural_equal.h:41
bool operator()(const bool &lhs, const bool &rhs) const
Definition: structural_equal.h:68
Content-aware structural equality comparator for objects.
Definition: structural_equal.h:97
bool operator()(const ffi::Any &lhs, const ffi::Any &rhs, const bool map_free_params=false) const
Compare objects via strutural equal.
Runtime primitive data type.
Definition: data_type.h:47
Defines the Functor data structures.
const Op & isnan()
Check if value is nan.
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37