xref: /aosp_15_r20/external/tensorflow/tensorflow/lite/tools/optimize/calibration/logging_op_resolver.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
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 #ifndef TENSORFLOW_LITE_TOOLS_OPTIMIZE_LOGGING_OP_RESOLVER_H_
16 #define TENSORFLOW_LITE_TOOLS_OPTIMIZE_LOGGING_OP_RESOLVER_H_
17 
18 #include <set>
19 #include <unordered_map>
20 
21 #include "tensorflow/lite/core/api/op_resolver.h"
22 #include "tensorflow/lite/mutable_op_resolver.h"
23 #include "tensorflow/lite/op_resolver.h"
24 #include "tensorflow/lite/tools/optimize/calibration/calibration_common.h"
25 
26 namespace tflite {
27 namespace optimize {
28 namespace calibration {
29 
30 // A resolver that replaces the kernel invocations with a wrapper
31 // eval function.
32 class LoggingOpResolver : public OpResolver {
33  public:
34   // Creates an instance of |LoggingOpResolver|.
35   // All |TfLiteRegistration.invoke| functions are replaced by
36   // |logging_eval_fn|.
37   // TODO(shashishekhar): This interface needs to change for
38   // BuiltinOps that need special logging implementations.
39   LoggingOpResolver(const BuiltinOpsSet& builtin_ops_to_replace,
40                     const CustomOpsSet& custom_ops_to_replace,
41                     const OpResolver& base_resolver,
42                     KernelEvalFuncPtr logging_eval_fn,
43                     ErrorReporter* error_reporter);
44 
45   const TfLiteRegistration* FindOp(BuiltinOperator op,
46                                    int version) const override;
47   KernelEvalFuncPtr GetWrappedKernelInvoke(BuiltinOperator op,
48                                            int version) const;
49 
50   const TfLiteRegistration* FindOp(const char* op, int version) const override;
51   KernelEvalFuncPtr GetWrappedKernelInvoke(const char* op, int version) const;
52 
53  private:
54   BuiltinOpsMap<std::unique_ptr<TfLiteRegistration>>
55       builtin_op_registration_map_;
56   BuiltinOpsMap<KernelEvalFuncPtr> builtin_op_evalfn_map_;
57   CustomOpsMap<std::unique_ptr<TfLiteRegistration>> custom_op_registration_map_;
58   CustomOpsMap<KernelEvalFuncPtr> custom_op_evalfn_map_;
59 };
60 
61 }  // namespace calibration
62 }  // namespace optimize
63 }  // namespace tflite
64 
65 #endif  // TENSORFLOW_LITE_TOOLS_OPTIMIZE_LOGGING_OP_RESOLVER_H_
66