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_H_ 16 #define TENSORFLOW_LITE_DELEGATES_FLEX_DELEGATE_H_ 17 18 #include "tensorflow/lite/c/common.h" 19 #include "tensorflow/lite/delegates/flex/delegate_data.h" 20 #include "tensorflow/lite/delegates/utils/simple_delegate.h" 21 22 namespace tflite { 23 24 namespace flex { 25 namespace testing { 26 class KernelTest; 27 } // namespace testing 28 } // namespace flex 29 30 // WARNING: This is an experimental interface that is subject to change. 31 // Delegate that can be used to extract parts of a graph that are designed to be 32 // executed by TensorFlow's runtime via Eager. 33 // 34 // The interpreter must be constructed after the FlexDelegate and destructed 35 // before the FlexDelegate. This delegate may be used with multiple 36 // interpreters, but it is *not* thread-safe. 37 // 38 // Usage: 39 // auto delegate = FlexDelegate::Create(); 40 // ... build interpreter ... 41 // 42 // if (delegate) { 43 // interpreter->ModifyGraphWithDelegate(std::move(delegate)); 44 // } 45 // ... run inference ... 46 // ... destroy interpreter ... 47 class FlexDelegate : public SimpleDelegateInterface { 48 public: 49 friend class flex::testing::KernelTest; 50 51 // Creates a delegate that supports TF ops. Create()52 static TfLiteDelegateUniquePtr Create() { 53 return Create(/*base_delegate*/ nullptr); 54 } 55 ~FlexDelegate()56 ~FlexDelegate() override {} 57 mutable_data()58 flex::DelegateData* mutable_data() { return &delegate_data_; } 59 60 protected: 61 // We sometimes have to create certain stub data to test FlexDelegate. To 62 // achieve this, we will make a testing flex delegate class that inherits from 63 // FlexDelegate to override certain things for stub data creation. Therefore, 64 // this function accepts a FlexDelegate instance to initiliaze it properly for 65 // create a testing flex delegate in some cases, and it is only used in 66 // testing. 67 static TfLiteDelegateUniquePtr Create( 68 std::unique_ptr<FlexDelegate> base_delegate); 69 FlexDelegate()70 FlexDelegate() {} 71 72 const char* Name() const override; 73 74 bool IsNodeSupportedByDelegate(const TfLiteRegistration* registration, 75 const TfLiteNode* node, 76 TfLiteContext* context) const override; 77 78 TfLiteStatus Initialize(TfLiteContext* context) override; 79 DelegateOptions()80 SimpleDelegateInterface::Options DelegateOptions() const override { 81 // Use default options. 82 return SimpleDelegateInterface::Options(); 83 } 84 85 std::unique_ptr<SimpleDelegateKernelInterface> CreateDelegateKernelInterface() 86 override; 87 88 TfLiteStatus CopyFromBufferHandle(TfLiteContext* context, 89 TfLiteBufferHandle buffer_handle, 90 TfLiteTensor* output); 91 92 flex::DelegateData delegate_data_; 93 }; 94 95 } // namespace tflite 96 97 #endif // TENSORFLOW_LITE_DELEGATES_FLEX_DELEGATE_H_ 98