• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_COMPILER_MLIR_LITE_METRICS_UTIL_H_
17 #define TENSORFLOW_COMPILER_MLIR_LITE_METRICS_UTIL_H_
18 
19 #include <functional>
20 #include <string>
21 
22 #include "mlir/IR/Location.h"  // from @llvm-project
23 #include "tensorflow/lite/python/metrics/converter_error_data.pb.h"
24 
25 namespace mlir {
26 namespace TFL {
27 
28 // The hash function for mlir::Location.
29 struct LocationHash {
operatorLocationHash30   std::size_t operator()(const Location& v) const noexcept {
31     return hash_value(v);
32   }
33 };
34 
35 // The hash function for ConverterErrorData.
36 struct ConverterErrorDataHash {
operatorConverterErrorDataHash37   std::size_t operator()(
38       const tflite::metrics::ConverterErrorData& v) const noexcept {
39     std::size_t hash_result = std::hash<std::string>{}(v.error_message());
40     if (v.has_subcomponent()) {
41       hash_result ^= std::hash<std::string>{}(v.subcomponent()) << 1;
42     }
43     if (v.has_error_code()) {
44       hash_result ^= std::hash<int>{}(v.error_code()) << 2;
45     }
46     if (v.has_operator_() && v.operator_().has_name()) {
47       hash_result ^= std::hash<std::string>{}(v.operator_().name()) << 3;
48     }
49     return hash_result;
50   }
51 };
52 
53 // The comparison function for ConverterErrorData.
54 struct ConverterErrorDataComparison {
operatorConverterErrorDataComparison55   std::size_t operator()(
56       const tflite::metrics::ConverterErrorData& a,
57       const tflite::metrics::ConverterErrorData& b) const noexcept {
58     return ConverterErrorDataHash()(a) == ConverterErrorDataHash()(b);
59   }
60 };
61 
62 // Helper function to create a new ConverterErrorData.
63 tflite::metrics::ConverterErrorData NewConverterErrorData(
64     const std ::string& pass_name, const std::string& error_message,
65     tflite::metrics::ConverterErrorData::ErrorCode error_code,
66     const std::string& op_name, const Location& location);
67 
68 }  // namespace TFL
69 }  // namespace mlir
70 #endif  // TENSORFLOW_COMPILER_MLIR_LITE_METRICS_UTIL_H_
71