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_NODE_INFO_DELEGATE_H_
16 #define TENSORFLOW_LITE_TOOLS_OPTIMIZE_NODE_INFO_DELEGATE_H_
17 
18 #include <unordered_map>
19 
20 #include "tensorflow/lite/context.h"
21 #include "tensorflow/lite/tools/optimize/calibration/calibration_common.h"
22 
23 namespace tflite {
24 namespace optimize {
25 namespace calibration {
26 
27 // An interface for delegate observer that can listen to TfLiteDelegate::Prepare
28 // calls.
29 class DelegateObserver {
30  public:
31   virtual TfLiteStatus OnDelegatePrepareCalled(TfLiteContext* context) = 0;
~DelegateObserver()32   virtual ~DelegateObserver() {}
33 };
34 
35 // The parameters for the node info delegate.
36 struct NodeInfoDelegateParams {
37   DelegateObserver* delegate_observer;
38 };
39 
40 // Creates a delegate with the given |params|.
41 TfLiteDelegate CreateNodeInfoDelegate(NodeInfoDelegateParams* params);
42 
43 // A delegate observer that can construct the map from TfLiteNode* ->
44 // OperatorInfo.
45 class NodeInfoDelegateObserver : public DelegateObserver {
46  public:
NodeInfoDelegateObserver(const std::unordered_map<int,OperatorInfo> & node_index_to_op,std::unordered_map<const TfLiteNode *,OperatorInfo> * node_ptr_opinfo_map)47   NodeInfoDelegateObserver(
48       const std::unordered_map<int, OperatorInfo>& node_index_to_op,
49       std::unordered_map<const TfLiteNode*, OperatorInfo>* node_ptr_opinfo_map)
50       : node_index_opinfo_map_(node_index_to_op),
51         node_ptr_opinfo_map_(node_ptr_opinfo_map) {}
52 
53   TfLiteStatus OnDelegatePrepareCalled(TfLiteContext* context) override;
54 
55   // Returns the context that was used to called the prepare method.
GetContext()56   const TfLiteContext* GetContext() const { return context_; }
57 
58  private:
59   const TfLiteContext* context_ = nullptr;
60   const std::unordered_map<int, OperatorInfo>& node_index_opinfo_map_;
61   std::unordered_map<const TfLiteNode*, OperatorInfo>* node_ptr_opinfo_map_;
62 };
63 
64 }  // namespace calibration
65 }  // namespace optimize
66 }  // namespace tflite
67 #endif  // TENSORFLOW_LITE_TOOLS_OPTIMIZE_NODE_INFO_DELEGATE_H_
68