1 /* Copyright 2019 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_HEXAGON_HEXAGON_DELEGATE_KERNEL_H_ 16 #define TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_DELEGATE_KERNEL_H_ 17 18 #include <time.h> 19 20 #include <memory> 21 #include <string> 22 #include <utility> 23 #include <vector> 24 25 #include "hexagon/hexagon_nn_ops.h" 26 #include "tensorflow/lite/builtin_ops.h" 27 #include "tensorflow/lite/c/builtin_op_data.h" 28 #include "tensorflow/lite/c/common.h" 29 #include "tensorflow/lite/core/api/profiler.h" 30 #include "tensorflow/lite/delegates/hexagon/builders/op_builder.h" 31 #include "tensorflow/lite/delegates/hexagon/hexagon_delegate.h" 32 #include "tensorflow/lite/delegates/hexagon/hexagon_implementation.h" 33 #include "tensorflow/lite/delegates/hexagon/hexagon_nn/hexagon_nn.h" 34 #include "tensorflow/lite/delegates/utils/simple_delegate.h" 35 #include "tensorflow/lite/schema/schema_generated.h" 36 37 namespace tflite { 38 39 // Represents an abstraction of a Hexagon NNLib graph with functionality to 40 // initialize, prepare and invoke it based on the TFLite subgraph to be 41 // delegated. 42 class HexagonDelegateKernel : public SimpleDelegateKernelInterface { 43 public: HexagonDelegateKernel(const::TfLiteHexagonDelegateOptions & params)44 explicit HexagonDelegateKernel(const ::TfLiteHexagonDelegateOptions& params) 45 : params_(params) {} 46 47 // Initialize the Hexagon graph and add required nodes. 48 TfLiteStatus Init(TfLiteContext* context, 49 const TfLiteDelegateParams* params) override; 50 51 // Prepare the Hexagon graph with hexagon_nn_prepare. 52 TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) override; 53 54 // Allocate Hexagon tensordefs for graph I/O & execute it. 55 TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) override; 56 57 ~HexagonDelegateKernel() override; 58 59 // Sets the environment required for Hexagon execution: DSP attributes, 60 // rpcmem, etc. 61 static void InitState(); 62 63 // Teardown the environment initialized in InitState. 64 static void Teardown(); 65 66 private: 67 // Builds the Hexagon graph based on delegated TFLite subgraph. 68 TfLiteStatus BuildGraph(TfLiteContext* context, 69 const TfLiteIntArray* input_tensors, 70 const TfLiteIntArray* output_tensors); 71 72 void ReportError(TfLiteContext* context, const std::string& msg); 73 74 // Resizes output tensors in case the delegate has dynamic batch enabled. 75 // Returns Error otherwise or if the requested size is invalid. 76 TfLiteStatus ResizeOutputTensors(TfLiteContext* context, TfLiteNode* node); 77 78 void PrintLog(); 79 80 // Prints performance information about the graph including cycles per node. 81 // If 'profiler' is not nullptr data will be added to it. 82 void PrintPerformanceData(Profiler* profiler); 83 84 // Print debugging information about the graph constructed. 85 // Amount of information can be increased with debug level. 86 void PrintDebuggingGraph(); 87 88 const HexagonNN* hexagon_nn_ = nullptr; // Not owned. 89 std::unique_ptr<delegates::hexagon::GraphBuilder> builder_; 90 hexagon_nn_nn_id graph_id_ = -1; 91 // Indices of nodes in the delegated TfLite subgraph. 92 std::vector<int> nodes_; 93 ::TfLiteHexagonDelegateOptions params_; 94 95 // Whether the Hexagon graph is prepared or not. 96 bool graph_prepared_ = false; 97 }; 98 99 } // namespace tflite 100 101 #endif // TENSORFLOW_LITE_DELEGATES_HEXAGON_HEXAGON_DELEGATE_KERNEL_H_ 102