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