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