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 "absl/memory/memory.h"
18 
19 namespace tflite {
20 namespace optimize {
21 namespace calibration {
22 
LoggingOpResolver(const BuiltinOpsSet & ops_to_replace,const OpResolver & base_resolver,KernelEvalFuncPtr logging_eval_fn)23 LoggingOpResolver::LoggingOpResolver(const BuiltinOpsSet& ops_to_replace,
24                                      const OpResolver& base_resolver,
25                                      KernelEvalFuncPtr logging_eval_fn) {
26   for (const auto& op_and_version : ops_to_replace) {
27     const TfLiteRegistration* base_registration =
28         base_resolver.FindOp(op_and_version.first, op_and_version.second);
29     BuiltinOperatorKey key = op_and_version;
30     builtin_op_evalfn_map_[key] = base_registration->invoke;
31     std::unique_ptr<TfLiteRegistration> logging_registation =
32         absl::make_unique<TfLiteRegistration>(*base_registration);
33     logging_registation->invoke = logging_eval_fn;
34     builtin_op_registration_map_[key] = std::move(logging_registation);
35   }
36 }
37 
FindOp(BuiltinOperator op,int version) const38 const TfLiteRegistration* LoggingOpResolver::FindOp(BuiltinOperator op,
39                                                     int version) const {
40   BuiltinOperatorKey key = {op, version};
41   if (builtin_op_registration_map_.find(key) !=
42       builtin_op_registration_map_.end()) {
43     return builtin_op_registration_map_.at(key).get();
44   }
45 
46   return nullptr;
47 }
48 
GetWrappedKernelInvoke(BuiltinOperator op,int version) const49 KernelEvalFuncPtr LoggingOpResolver::GetWrappedKernelInvoke(BuiltinOperator op,
50                                                             int version) const {
51   return builtin_op_evalfn_map_.at({op, version});
52 }
53 
FindOp(const char * op,int version) const54 const TfLiteRegistration* LoggingOpResolver::FindOp(const char* op,
55                                                     int version) const {
56   // TODO(b/121374947): Support custom ops as well.
57   return nullptr;
58 }
59 }  // namespace calibration
60 }  // namespace optimize
61 }  // namespace tflite
62