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/public/session_options.h" 20 #include "tensorflow/lite/delegates/flex/buffer_map.h" 21 22 namespace tflite { 23 namespace flex { 24 25 // Data kept by the Flex delegate for the lifetime of an Interpreter. 26 // 27 // Note: This class is *not* thread-safe; any dependent delegates should not be 28 // used concurrently. 29 class DelegateData { 30 public: 31 DelegateData(); 32 ~DelegateData(); 33 34 // Prepare the necessary EagerContext and data for execution. 35 // This must be called at least once before execution. After preparation 36 // succeeds, redundant calls will be ignored (even if the session_options 37 // differ). 38 tensorflow::Status Prepare(const tensorflow::SessionOptions& session_options); 39 40 // The EagerContext that is required for execution of Flex Ops. 41 // Note: The context is lazily created after the first call to |Prepare()|. GetEagerContext()42 tensorflow::EagerContext* GetEagerContext() { return eager_context_.get(); } 43 44 // Map from TF Lite tensor index to TensorFlow tensor for a given context. GetBufferMap(const TfLiteContext * context)45 BufferMap* GetBufferMap(const TfLiteContext* context) { 46 return &buffer_map_[context]; 47 } 48 49 private: 50 // Will be null until Prepare() is called and completes successfully. 51 std::unique_ptr<tensorflow::EagerContext> eager_context_; 52 // TODO(b/112439500): Clean up stale BufferMap instances after adding the 53 // necessary cleanup hook from a TfLiteContext to a TfLiteDelegate. 54 std::unordered_map<const TfLiteContext*, BufferMap> buffer_map_; 55 }; 56 57 } // namespace flex 58 } // namespace tflite 59 60 #endif // TENSORFLOW_LITE_DELEGATES_FLEX_DELEGATE_DATA_H_ 61