• 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 "tensorflow/lite/delegates/utils/dummy_delegate/dummy_delegate.h"
16 
17 #include <utility>
18 
19 #include "tensorflow/lite/delegates/utils/simple_delegate.h"
20 
21 namespace tflite {
22 namespace dummy_test {
23 
24 // Dummy delegate kernel.
25 class DummyDelegateKernel : public SimpleDelegateKernelInterface {
26  public:
DummyDelegateKernel(const DummyDelegateOptions & options)27   explicit DummyDelegateKernel(const DummyDelegateOptions& options)
28       : options_(options) {}
29 
Init(TfLiteContext * context,const TfLiteDelegateParams * params)30   TfLiteStatus Init(TfLiteContext* context,
31                     const TfLiteDelegateParams* params) override {
32     return !options_.error_during_init ? kTfLiteOk : kTfLiteError;
33   }
34 
Prepare(TfLiteContext * context,TfLiteNode * node)35   TfLiteStatus Prepare(TfLiteContext* context, TfLiteNode* node) override {
36     return !options_.error_during_prepare ? kTfLiteOk : kTfLiteError;
37   }
38 
Eval(TfLiteContext * context,TfLiteNode * node)39   TfLiteStatus Eval(TfLiteContext* context, TfLiteNode* node) override {
40     return !options_.error_during_invoke ? kTfLiteOk : kTfLiteError;
41   }
42 
43  private:
44   const DummyDelegateOptions options_;
45 };
46 
47 // DummyDelegate implements the interface of SimpleDelegateInterface.
48 // This holds the Delegate capabilities.
49 class DummyDelegate : public SimpleDelegateInterface {
50  public:
DummyDelegate(const DummyDelegateOptions & options)51   explicit DummyDelegate(const DummyDelegateOptions& options)
52       : options_(options) {}
IsNodeSupportedByDelegate(const TfLiteRegistration * registration,const TfLiteNode * node,TfLiteContext * context) const53   bool IsNodeSupportedByDelegate(const TfLiteRegistration* registration,
54                                  const TfLiteNode* node,
55                                  TfLiteContext* context) const override {
56     return options_.allowed_builtin_code == registration->builtin_code;
57   }
58 
Initialize(TfLiteContext * context)59   TfLiteStatus Initialize(TfLiteContext* context) override { return kTfLiteOk; }
60 
Name() const61   const char* Name() const override {
62     static constexpr char kName[] = "DummyDelegate";
63     return kName;
64   }
65 
CreateDelegateKernelInterface()66   std::unique_ptr<SimpleDelegateKernelInterface> CreateDelegateKernelInterface()
67       override {
68     return std::make_unique<DummyDelegateKernel>(options_);
69   }
70 
DelegateOptions() const71   SimpleDelegateInterface::Options DelegateOptions() const override {
72     // Use default options.
73     return SimpleDelegateInterface::Options();
74   }
75 
76  private:
77   const DummyDelegateOptions options_;
78 };
79 
80 }  // namespace dummy_test
81 }  // namespace tflite
82 
TfLiteDummyDelegateOptionsDefault()83 DummyDelegateOptions TfLiteDummyDelegateOptionsDefault() {
84   DummyDelegateOptions options = {0};
85   // Just assign an invalid builtin code so that this dummy test delegate will
86   // not support any node by default.
87   options.allowed_builtin_code = -1;
88   return options;
89 }
90 
91 // Creates a new delegate instance that need to be destroyed with
92 // `TfLiteDummyDelegateDelete` when delegate is no longer used by TFLite.
93 // When `options` is set to `nullptr`, the above default values are used:
TfLiteDummyDelegateCreate(const DummyDelegateOptions * options)94 TfLiteDelegate* TfLiteDummyDelegateCreate(const DummyDelegateOptions* options) {
95   std::unique_ptr<tflite::dummy_test::DummyDelegate> dummy(
96       new tflite::dummy_test::DummyDelegate(
97           options ? *options : TfLiteDummyDelegateOptionsDefault()));
98   return tflite::TfLiteDelegateFactory::CreateSimpleDelegate(std::move(dummy));
99 }
100 
101 // Destroys a delegate created with `TfLiteDummyDelegateCreate` call.
TfLiteDummyDelegateDelete(TfLiteDelegate * delegate)102 void TfLiteDummyDelegateDelete(TfLiteDelegate* delegate) {
103   tflite::TfLiteDelegateFactory::DeleteSimpleDelegate(delegate);
104 }
105