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 #include <utility>
17
18 #include "tensorflow/lite/delegates/utils/dummy_delegate/dummy_delegate.h"
19 #include "tensorflow/lite/tools/delegates/delegate_provider.h"
20
21 namespace tflite {
22 namespace tools {
23
24 class DummyDelegateProvider : public DelegateProvider {
25 public:
DummyDelegateProvider()26 DummyDelegateProvider() {
27 default_params_.AddParam("use_dummy_delegate",
28 ToolParam::Create<bool>(false));
29 }
30
31 std::vector<Flag> CreateFlags(ToolParams* params) const final;
32
33 void LogParams(const ToolParams& params, bool verbose) const final;
34
35 TfLiteDelegatePtr CreateTfLiteDelegate(const ToolParams& params) const final;
36 std::pair<TfLiteDelegatePtr, int> CreateRankedTfLiteDelegate(
37 const ToolParams& params) const final;
38
GetName() const39 std::string GetName() const final { return "DummyDelegate"; }
40 };
41 REGISTER_DELEGATE_PROVIDER(DummyDelegateProvider);
42
CreateFlags(ToolParams * params) const43 std::vector<Flag> DummyDelegateProvider::CreateFlags(ToolParams* params) const {
44 std::vector<Flag> flags = {CreateFlag<bool>("use_dummy_delegate", params,
45 "use the dummy delegate.")};
46 return flags;
47 }
48
LogParams(const ToolParams & params,bool verbose) const49 void DummyDelegateProvider::LogParams(const ToolParams& params,
50 bool verbose) const {
51 LOG_TOOL_PARAM(params, bool, "use_dummy_delegate", "Use dummy test delegate",
52 verbose);
53 }
54
CreateTfLiteDelegate(const ToolParams & params) const55 TfLiteDelegatePtr DummyDelegateProvider::CreateTfLiteDelegate(
56 const ToolParams& params) const {
57 if (params.Get<bool>("use_dummy_delegate")) {
58 auto default_options = TfLiteDummyDelegateOptionsDefault();
59 return TfLiteDummyDelegateCreateUnique(&default_options);
60 }
61 return TfLiteDelegatePtr(nullptr, [](TfLiteDelegate*) {});
62 }
63
64 std::pair<TfLiteDelegatePtr, int>
CreateRankedTfLiteDelegate(const ToolParams & params) const65 DummyDelegateProvider::CreateRankedTfLiteDelegate(
66 const ToolParams& params) const {
67 auto ptr = CreateTfLiteDelegate(params);
68 return std::make_pair(std::move(ptr),
69 params.GetPosition<bool>("use_dummy_delegate"));
70 }
71 } // namespace tools
72 } // namespace tflite
73