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 <string>
16
17 #include "tensorflow/lite/tools/delegates/delegate_provider.h"
18 #include "tensorflow/lite/tools/evaluation/utils.h"
19
20 namespace tflite {
21 namespace tools {
22
23 class XnnpackDelegateProvider : public DelegateProvider {
24 public:
XnnpackDelegateProvider()25 XnnpackDelegateProvider() {
26 default_params_.AddParam("use_xnnpack", ToolParam::Create<bool>(false));
27 }
28
29 std::vector<Flag> CreateFlags(ToolParams* params) const final;
30
31 void LogParams(const ToolParams& params, bool verbose) const final;
32
33 TfLiteDelegatePtr CreateTfLiteDelegate(const ToolParams& params) const final;
34
GetName() const35 std::string GetName() const final { return "XNNPACK"; }
36 };
37 REGISTER_DELEGATE_PROVIDER(XnnpackDelegateProvider);
38
CreateFlags(ToolParams * params) const39 std::vector<Flag> XnnpackDelegateProvider::CreateFlags(
40 ToolParams* params) const {
41 std::vector<Flag> flags = {
42 CreateFlag<bool>("use_xnnpack", params, "use XNNPack")};
43 return flags;
44 }
45
LogParams(const ToolParams & params,bool verbose) const46 void XnnpackDelegateProvider::LogParams(const ToolParams& params,
47 bool verbose) const {
48 LOG_TOOL_PARAM(params, bool, "use_xnnpack", "Use xnnpack", verbose);
49 }
50
CreateTfLiteDelegate(const ToolParams & params) const51 TfLiteDelegatePtr XnnpackDelegateProvider::CreateTfLiteDelegate(
52 const ToolParams& params) const {
53 if (params.Get<bool>("use_xnnpack")) {
54 return evaluation::CreateXNNPACKDelegate(
55 params.Get<int32_t>("num_threads"));
56 }
57 return TfLiteDelegatePtr(nullptr, [](TfLiteDelegate*) {});
58 }
59
60 } // namespace tools
61 } // namespace tflite
62