1 /* Copyright 2018 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 #include "tensorflow/lite/tools/optimize/calibration/logging_op_resolver.h"
16
17 #include <memory>
18 #include <string>
19 #include <utility>
20
21 #include "absl/memory/memory.h"
22 #include "absl/strings/str_cat.h"
23 #include "absl/strings/str_join.h"
24 #include "tensorflow/lite/minimal_logging.h"
25 #include "tensorflow/lite/util.h"
26
27 namespace tflite {
28 namespace optimize {
29 namespace calibration {
30
LoggingOpResolver(const BuiltinOpsSet & builtin_ops_to_replace,const CustomOpsSet & custom_ops_to_replace,const OpResolver & base_resolver,KernelEvalFuncPtr logging_eval_fn,ErrorReporter * error_reporter)31 LoggingOpResolver::LoggingOpResolver(
32 const BuiltinOpsSet& builtin_ops_to_replace,
33 const CustomOpsSet& custom_ops_to_replace, const OpResolver& base_resolver,
34 KernelEvalFuncPtr logging_eval_fn, ErrorReporter* error_reporter) {
35 std::vector<std::string> unresolved_builtin_ops;
36 std::vector<std::string> unresolved_custom_ops;
37
38 for (const auto& op_and_version : builtin_ops_to_replace) {
39 const TfLiteRegistration* base_registration =
40 base_resolver.FindOp(op_and_version.first, op_and_version.second);
41 if (!base_registration) {
42 unresolved_builtin_ops.push_back(
43 EnumNameBuiltinOperator(op_and_version.first));
44 continue;
45 }
46 BuiltinOperatorKey key = op_and_version;
47 builtin_op_evalfn_map_[key] = base_registration->invoke;
48 auto logging_registration =
49 std::make_unique<TfLiteRegistration>(*base_registration);
50 logging_registration->invoke = logging_eval_fn;
51 builtin_op_registration_map_[key] = std::move(logging_registration);
52 }
53 for (const auto& op_and_version : custom_ops_to_replace) {
54 const TfLiteRegistration* base_registration = base_resolver.FindOp(
55 op_and_version.first.c_str(), op_and_version.second);
56 if (!base_registration) {
57 if (!IsFlexOp(op_and_version.first.c_str()))
58 unresolved_custom_ops.push_back(op_and_version.first.c_str());
59 continue;
60 }
61 CustomOperatorKey key = op_and_version;
62 custom_op_evalfn_map_[key] = base_registration->invoke;
63 auto logging_registration =
64 std::make_unique<TfLiteRegistration>(*base_registration);
65 logging_registration->invoke = logging_eval_fn;
66 custom_op_registration_map_[key] = std::move(logging_registration);
67 }
68
69 if (!unresolved_builtin_ops.empty() || !unresolved_custom_ops.empty()) {
70 if (!error_reporter) return;
71 std::string error_message =
72 "Failed to initialize op resolver for calibration:";
73 if (!unresolved_builtin_ops.empty())
74 absl::StrAppend(&error_message, "\nThere are unresolved builtin ops: [",
75 absl::StrJoin(unresolved_builtin_ops, ", "), "]");
76 if (!unresolved_custom_ops.empty()) {
77 absl::StrAppend(&error_message, "\nThere are unresolved custom ops: [",
78 absl::StrJoin(unresolved_custom_ops, ", "), "]");
79 }
80 TF_LITE_REPORT_ERROR(error_reporter, error_message.c_str());
81 }
82 }
83
FindOp(BuiltinOperator op,int version) const84 const TfLiteRegistration* LoggingOpResolver::FindOp(BuiltinOperator op,
85 int version) const {
86 BuiltinOperatorKey key = {op, version};
87 if (builtin_op_registration_map_.find(key) !=
88 builtin_op_registration_map_.end()) {
89 return builtin_op_registration_map_.at(key).get();
90 }
91
92 return nullptr;
93 }
94
GetWrappedKernelInvoke(BuiltinOperator op,int version) const95 KernelEvalFuncPtr LoggingOpResolver::GetWrappedKernelInvoke(BuiltinOperator op,
96 int version) const {
97 return builtin_op_evalfn_map_.at({op, version});
98 }
99
FindOp(const char * op,int version) const100 const TfLiteRegistration* LoggingOpResolver::FindOp(const char* op,
101 int version) const {
102 CustomOperatorKey key = {op, version};
103 if (custom_op_registration_map_.find(key) !=
104 custom_op_registration_map_.end()) {
105 return custom_op_registration_map_.at(key).get();
106 }
107
108 return nullptr;
109 }
110
GetWrappedKernelInvoke(const char * op,int version) const111 KernelEvalFuncPtr LoggingOpResolver::GetWrappedKernelInvoke(const char* op,
112 int version) const {
113 return custom_op_evalfn_map_.at({op, version});
114 }
115
116 } // namespace calibration
117 } // namespace optimize
118 } // namespace tflite
119