• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2020 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 #include <memory>
16 
17 #include "absl/memory/memory.h"
18 #include "tensorflow/lite/experimental/acceleration/configuration/configuration_generated.h"
19 #include "tensorflow/lite/experimental/acceleration/configuration/delegate_registry.h"
20 
21 #if defined(__ARM_ARCH)
22 #include "tensorflow/lite/delegates/hexagon/hexagon_delegate.h"
23 #endif
24 
25 namespace tflite {
26 namespace delegates {
27 class HexagonPlugin : public DelegatePluginInterface {
28  public:
Create()29   TfLiteDelegatePtr Create() override {
30 #if defined(__ARM_ARCH)
31     TfLiteHexagonInit();
32     auto* delegate_ptr = TfLiteHexagonDelegateCreate(&options_);
33     TfLiteDelegatePtr delegate(delegate_ptr, [](TfLiteDelegate* delegate) {
34       TfLiteHexagonDelegateDelete(delegate);
35       TfLiteHexagonTearDown();
36     });
37     return delegate;
38 #else   // !defined(__ARM_ARCH)
39     return TfLiteDelegatePtr(nullptr, [](TfLiteDelegate*) {});
40 #endif  // defined(__ARM_ARCH)
41   }
GetDelegateErrno(TfLiteDelegate *)42   int GetDelegateErrno(TfLiteDelegate* /* from_delegate */) override {
43     return 0;
44   }
New(const TFLiteSettings & tflite_settings)45   static std::unique_ptr<HexagonPlugin> New(
46       const TFLiteSettings& tflite_settings) {
47     return std::make_unique<HexagonPlugin>(tflite_settings);
48   }
HexagonPlugin(const TFLiteSettings & tflite_settings)49   explicit HexagonPlugin(const TFLiteSettings& tflite_settings) {
50     const HexagonSettings* settings = tflite_settings.hexagon_settings();
51 #if defined(__ARM_ARCH)
52     options_ = TfLiteHexagonDelegateOptions({0});
53     if (settings) {
54       options_.debug_level = settings->debug_level();
55       options_.powersave_level = settings->powersave_level();
56       options_.print_graph_profile = settings->print_graph_profile();
57       options_.print_graph_debug = settings->print_graph_debug();
58       if (tflite_settings.max_delegated_partitions() >= 0) {
59         options_.max_delegated_partitions =
60             tflite_settings.max_delegated_partitions();
61       }
62     }
63 #else
64     (void)settings;
65 #endif
66   }
67 
68  private:
69 #if defined(__ARM_ARCH)
70   TfLiteHexagonDelegateOptions options_;
71 #endif
72 };
73 
74 TFLITE_REGISTER_DELEGATE_FACTORY_FUNCTION(HexagonPlugin, HexagonPlugin::New);
75 
76 }  // namespace delegates
77 }  // namespace tflite
78