• 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 #ifndef TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_CONFIGURATION_DELEGATE_REGISTRY_H_
16 #define TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_CONFIGURATION_DELEGATE_REGISTRY_H_
17 
18 #include <functional>
19 #include <memory>
20 #include <string>
21 #include <unordered_map>
22 
23 #include "absl/synchronization/mutex.h"
24 #include "tensorflow/lite/c/common.h"
25 #include "tensorflow/lite/experimental/acceleration/configuration/configuration_generated.h"
26 
27 // Defines an interface for TFLite delegate plugins.
28 //
29 // The acceleration library aims to support all TFLite delegates based on
30 // configuration expressed as data (flatbuffers). However, consumers tend to
31 // care about size and also use a subset of delegates. Hence we don't want to
32 // statically build against all delegates.
33 //
34 // This interface allows plugins to handle specific delegates.
35 //
36 // Goal of this interface is not to abstract away all the differences between
37 // delegates. The goal is only to avoid static linking.
38 //
39 // Note to implementers: this interface may change if new delegates don't fit
40 // into the same design.
41 namespace tflite {
42 namespace delegates {
43 
44 // Same w/ Interpreter::TfLiteDelegatePtr to avoid pulling
45 // tensorflow/lite/interpreter.h dependency
46 using TfLiteDelegatePtr =
47     std::unique_ptr<TfLiteDelegate, void (*)(TfLiteDelegate*)>;
48 
49 class DelegatePluginInterface {
50  public:
51   virtual TfLiteDelegatePtr Create() = 0;
52   virtual int GetDelegateErrno(TfLiteDelegate* from_delegate) = 0;
53   virtual ~DelegatePluginInterface() = default;
54 };
55 
56 // A stripped-down registry that allows delegate plugins to be created by name.
57 //
58 // Limitations:
59 // - Doesn't allow deregistration.
60 // - Doesn't check for duplication registration.
61 //
62 class DelegatePluginRegistry {
63  public:
64   typedef std::function<std::unique_ptr<DelegatePluginInterface>(
65       const TFLiteSettings&)>
66       CreatorFunction;
67   // Returns a DelegatePluginInterface registered with `name` or nullptr if no
68   // matching plugin found.
69   // TFLiteSettings is per-plugin, so that the corresponding delegate options
70   // data lifetime is maintained.
71   static std::unique_ptr<DelegatePluginInterface> CreateByName(
72       const std::string& name, const TFLiteSettings& settings);
73 
74   // Struct to be statically allocated for registration.
75   struct Register {
76     Register(const std::string& name, CreatorFunction creator_function);
77   };
78 
79  private:
80   void RegisterImpl(const std::string& name, CreatorFunction creator_function);
81   std::unique_ptr<DelegatePluginInterface> CreateImpl(
82       const std::string& name, const TFLiteSettings& settings);
83   static DelegatePluginRegistry* GetSingleton();
84   absl::Mutex mutex_;
85   std::unordered_map<std::string, CreatorFunction> factories_
86       ABSL_GUARDED_BY(mutex_);
87 };
88 
89 }  // namespace delegates
90 }  // namespace tflite
91 
92 #define TFLITE_REGISTER_DELEGATE_FACTORY_FUNCTION_VNAME(name, f) \
93   static auto* g_delegate_plugin_##name##_ =                     \
94       new DelegatePluginRegistry::Register(#name, f);
95 #define TFLITE_REGISTER_DELEGATE_FACTORY_FUNCTION(name, f) \
96   TFLITE_REGISTER_DELEGATE_FACTORY_FUNCTION_VNAME(name, f);
97 
98 #endif  // TENSORFLOW_LITE_EXPERIMENTAL_ACCELERATION_CONFIGURATION_DELEGATE_REGISTRY_H_
99