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_KERNEL_H_ 16 #define TENSORFLOW_LITE_DELEGATES_FLEX_KERNEL_H_ 17 18 #include <memory> 19 20 #include "tensorflow/core/platform/status.h" 21 #include "tensorflow/core/tfrt/fallback/op_kernel_runner.h" 22 #include "tensorflow/lite/c/common.h" 23 #include "tensorflow/lite/delegates/utils/simple_delegate.h" 24 25 namespace tflite { 26 namespace flex { 27 28 namespace testing { 29 class KernelTest; // friend class declaration. 30 } // namespace testing 31 32 struct OpData; 33 struct OpNode; 34 35 class DelegateKernel : public SimpleDelegateKernelInterface { 36 public: 37 DelegateKernel(); 38 ~DelegateKernel() override; 39 40 TfLiteStatus Init(TfLiteContext* context, 41 const TfLiteDelegateParams* params) override; 42 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) override; 43 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) override; 44 45 private: 46 friend class tflite::flex::testing::KernelTest; 47 48 // Validates that the computed output tensor shape for the Flex node matches 49 // the existing output shape assigned to the output tensor. 50 TfLiteStatus ValidateOutputTensorShapeConsistency( 51 TfLiteContext* context) const; 52 53 // Executes the Tensorflow op based on the inputs/outputs/attributes 54 // information represented in the `node_data`. 55 tensorflow::Status ExecuteOpKernelRunner( 56 tensorflow::tfrt_stub::OpKernelRunState* run_state, 57 TfLiteContext* context, OpNode* node_data); 58 59 // Returns the tensor release map held in `op_data_`; 60 const std::map<int, int>& GetTensorReleaseMap() const; 61 62 std::unique_ptr<OpData> op_data_; 63 }; 64 65 } // namespace flex 66 } // namespace tflite 67 68 #endif // TENSORFLOW_LITE_DELEGATES_FLEX_KERNEL_H_ 69