• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_DELEGATES_FLEX_DELEGATE_DATA_H_
16 #define TENSORFLOW_LITE_DELEGATES_FLEX_DELEGATE_DATA_H_
17 
18 #include "tensorflow/core/common_runtime/eager/context.h"
19 #include "tensorflow/core/framework/cancellation.h"
20 #include "tensorflow/core/public/session_options.h"
21 #include "tensorflow/lite/core/subgraph.h"
22 #include "tensorflow/lite/delegates/flex/buffer_map.h"
23 #include "tensorflow/lite/delegates/flex/subgraph_resource.h"
24 
25 namespace tflite {
26 namespace flex {
27 
28 // Data kept by the Flex delegate for the lifetime of an Interpreter.
29 //
30 // Note: This class is *not* thread-safe; any dependent delegates should not be
31 // used concurrently.
32 class DelegateData {
33  public:
34   DelegateData();
35   ~DelegateData();
36 
37   // Prepare the necessary EagerContext and data for execution.
38   // This must be called at least once before execution. After preparation
39   // succeeds, redundant calls will be ignored (even if the session_options
40   // differ).
41   // When `main_subgraph` parameter is provided, this function will register
42   // FunctionDefs associated with each of the subgraphs attached to the
43   // `main_subgraph`.
44   tensorflow::Status Prepare(const tensorflow::SessionOptions& session_options,
45                              Subgraph* main_subgraph = nullptr);
46 
47   // The EagerContext that is required for execution of Flex Ops.
48   // Note: The context is lazily created after the first call to |Prepare()|.
GetEagerContext()49   tensorflow::EagerContext* GetEagerContext() { return eager_context_; }
50 
GetCancellationManager()51   tensorflow::CancellationManager* GetCancellationManager() {
52     return cancellation_manager_;
53   }
54 
SetCancellationManager(tensorflow::CancellationManager * cancellation_manager)55   void SetCancellationManager(
56       tensorflow::CancellationManager* cancellation_manager) {
57     cancellation_manager_ = cancellation_manager;
58   }
59 
60   // Map from TF Lite tensor index to TensorFlow tensor for a given context.
GetBufferMap(const TfLiteContext * context)61   BufferMap* GetBufferMap(const TfLiteContext* context) {
62     return &buffer_map_[context];
63   }
64 
65  private:
66   // Will be null until Prepare() is called and completes successfully.
67   tensorflow::EagerContext* eager_context_ = nullptr;
68   // Not owned by DelegateData.
69   tensorflow::CancellationManager* cancellation_manager_ = nullptr;
70   // TODO(b/112439500): Clean up stale BufferMap instances after adding the
71   // necessary cleanup hook from a TfLiteContext to a TfLiteDelegate.
72   std::unordered_map<const TfLiteContext*, BufferMap> buffer_map_;
73 };
74 
75 // Creates a `TFLiteSubgraphResource` for each subgraph (execpt
76 // for main subgraph) in the model and adds it in the eager context's resource
77 // manager. It also registers FunctionDefs in the function library runtime for
78 // subgraphs which are used by a list of flex ops.
79 // TODO(b/181352924): For now, the `GetSubgraphs` API will return all the
80 // subgraphs in the tflite model, so that it ensures we won't miss any subgraphs
81 // during function registration. We need to revisit here when we introduce
82 // multi-signature support.
83 tensorflow::Status RegisterFunctionDefForSubgraphs(
84     Subgraph& main_subgraph,
85     const std::function<tensorflow::Status(
86         const std::vector<std::unique_ptr<Subgraph>>&,
87         std::set<std::string>* result)>& select_subgraphs_to_register,
88     tensorflow::ResourceMgr* resource_mgr,
89     tensorflow::EagerContext* eager_context);
90 
91 }  // namespace flex
92 }  // namespace tflite
93 
94 #endif  // TENSORFLOW_LITE_DELEGATES_FLEX_DELEGATE_DATA_H_
95