NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
ClassRegistry.h
Go to the documentation of this file.
1 /*
2 * SPDX-FileCopyrightText: Copyright (c) 2020-2021 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3 * SPDX-License-Identifier: MIT
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23 
25 
26 #ifndef NVNEURAL_CLASSREGISTRY_H
27 #define NVNEURAL_CLASSREGISTRY_H
28 
29 #include <nvneural/CoreTypes.h>
30 #include <nvneural/CoreHelpers.h>
31 #include <nvneural/RefObject.h>
32 #include <functional>
33 #include <map>
34 #include <string>
35 #include <vector>
36 
37 #include "LayerDescHostContainer.h"
40 
41 namespace nvneural {
42  class INetwork;
43  class IPlugin;
44  class IPluginLoader;
45 
46  struct OpaqueClassRegistration;
47 
52  using OpaqueClassRegistrationHandle = OpaqueClassRegistration*;
53 
71  class ClassRegistry : public refobj::RefObjectBase<refobj::Implements<IClassRegistry>>
72  {
73  public:
76 
78  ClassRegistry();
81 
83  NeuralResult importPlugin(IPlugin* pPlugin) override final;
85  NeuralResult importPluginLoader(IPluginLoader* pPluginLoader) override final;
86 
88  NeuralResult createObject(IRefObject** ppOut, const char* pObjectClass) const noexcept override final;
89 
91  std::size_t exportedActivationsCount() const noexcept override final;
93  const ActivationDesc* exportedActivationByIndex(std::size_t index) const noexcept override final;
95  std::size_t exportedLayersCount() const noexcept override final;
97  const LayerDesc* exportedLayerByIndex(std::size_t index) const noexcept override final;
99  std::size_t exportedFusingRulesCount() const noexcept override final;
101  const FusingRule* exportedFusingRuleByIndex(std::size_t index) const noexcept override final;
103  std::size_t exportedPrototypesCount() const noexcept override final;
105  const PrototypeDesc* exportedPrototypeByIndex(std::size_t index) const noexcept override final;
107  PrototypeRuntimeValidatorFunction exportedPrototypeRuntimeValidatorByIndex(std::size_t index) const noexcept override final;
108 
110  bool isPrototypeRuntimeCompatible(const char* pPrototypeObjectClass, const INetwork* pNetwork) const override final;
111 
116  template<typename TObject>
117  static OpaqueClassRegistrationHandle registerObjectClass(const char* pObjectClass)
118  {
119  return registerObjectClassInternal(pObjectClass, []() -> IRefObject* { return static_cast<IRefObject*>(static_cast<void*>(new TObject)); });
120  }
121 
129  template<typename TObject>
131  {
132  // Keep note that this layerDesc is in terms of the plugin's LayerDesc.
133  registerLayerDesc(&layerDesc);
134  return registerObjectClass<TObject>(layerDesc.pObjectClass);
135  }
136 
144  template<typename TObject>
146  {
147  // Keep note that this activationDesc is in terms of the plugin's activationDesc.
148  registerActivationDesc(&activationDesc);
149  return registerObjectClass<TObject>(activationDesc.pObjectClass);
150  }
151 
160  template<typename TObject>
161  static OpaqueClassRegistrationHandle registerPrototype(const PrototypeDesc& prototypeDesc, PrototypeRuntimeValidatorFunction runtimeValidator)
162  {
163  registerPrototypeDesc(&prototypeDesc, runtimeValidator);
164  return registerObjectClass<TObject>(prototypeDesc.pObjectClass);
165  }
166 
173  static OpaqueClassRegistrationHandle registerFusingRule(std::uint32_t syntaxVersion, const char* pFusingRule);
174 
180 
186 
192  static OpaqueClassRegistrationHandle registerPrototypeDesc(const PrototypeDesc* pPrototypeDesc, PrototypeRuntimeValidatorFunction runtimeValidator);
193 
201  static OpaqueClassRegistrationHandle registerObjectClassInternal(const char* pObjectClass, CreationFunction classFactory);
202 
203  private:
204  void loadStaticRegistrations();
205 
206  bool registerImportedActivationDesc(const ActivationDesc* pImportedActivationDesc);
207  bool registerImportedLayerDesc(const LayerDesc* pImportedLayerDesc);
208  bool registerImportedPrototypeDesc(const PrototypeDesc* pImportPrototypeDesc);
209 
210  std::vector<RefPtr<IPlugin>> m_loadedPlugins;
211 
212  std::vector<ActivationDescHostContainer> m_exportedActivationHostContainers;
213  std::vector<LayerDescHostContainer> m_exportedLayerHostContainers;
214  std::vector<PrototypeDescHostContainer> m_exportedPrototypeHostContainers;
215 
216  std::vector<const ActivationDesc*> m_exportedActivations;
217  std::vector<const LayerDesc*> m_exportedLayers;
218  std::vector<const FusingRule*> m_exportedFusingRules;
219  std::vector<const PrototypeDesc*> m_exportedPrototypes;
220 
221  std::map<std::string, CreationFunction> m_objectTypes;
222  std::map<std::string, PrototypeRuntimeValidatorFunction> m_exportedPrototypeRuntimeValidators;
223  };
224 }
225 
226 #endif // NVNEURAL_CLASSREGISTRY_H
describes a container that has the imported ActivationDesc in terms of the host's version.
OpaqueClassRegistration * OpaqueClassRegistrationHandle
Opaque return type from registration functions.
Definition: ClassRegistry.h:52
Common helper classes and template function implementations.
Fundamental NvNeural data types are declared here.
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:275
describes a container that has the imported LayerDesc in terms of the host's version.
describes a container that has the imported PrototypeDesc in terms of the host's version.
Standard implementation for IRefObject-derived objects.
ClassRegistry is the default implementation for IClassRegistry.
Definition: ClassRegistry.h:72
const ActivationDesc * exportedActivationByIndex(std::size_t index) const noexcept final
Returns a registered activation function descriptor for a given index.
Definition: ClassRegistry.cpp:210
static OpaqueClassRegistrationHandle registerObjectClass(const char *pObjectClass)
Registers a C++ object as a factory-creatable object class.
Definition: ClassRegistry.h:117
const PrototypeDesc * exportedPrototypeByIndex(std::size_t index) const noexcept final
Returns a registered prototype layer descriptor for a given index.
Definition: ClassRegistry.cpp:273
static OpaqueClassRegistrationHandle registerLayerDesc(const LayerDesc *pLayerDesc)
Registers a LayerDesc structure for discovery.
Definition: ClassRegistry.cpp:358
static OpaqueClassRegistrationHandle registerActivation(const ActivationDesc &activationDesc)
Registers a C++ object and ActivationDesc structure for discovery and creation.
Definition: ClassRegistry.h:145
NeuralResult createObject(IRefObject **ppOut, const char *pObjectClass) const noexcept final
Creates the designated object using its manifest-specified class name.
Definition: ClassRegistry.cpp:304
static OpaqueClassRegistrationHandle registerPrototypeDesc(const PrototypeDesc *pPrototypeDesc, PrototypeRuntimeValidatorFunction runtimeValidator)
Registers a PrototypeDesc structure and validator for discovery.
Definition: ClassRegistry.cpp:374
std::size_t exportedFusingRulesCount() const noexcept final
Returns the count of registered fusing rules.
Definition: ClassRegistry.cpp:246
static OpaqueClassRegistrationHandle registerPrototype(const PrototypeDesc &prototypeDesc, PrototypeRuntimeValidatorFunction runtimeValidator)
Registers a C++ object as a prototype layer for discovery and creation.
Definition: ClassRegistry.h:161
static OpaqueClassRegistrationHandle registerLayer(const LayerDesc &layerDesc)
Registers a C++ object and LayerDesc structure for discovery and creation.
Definition: ClassRegistry.h:130
std::size_t exportedPrototypesCount() const noexcept final
Returns the count of registered prototype layers.
Definition: ClassRegistry.cpp:267
ClassRegistry()
Creates a new ClassRegistry containing statically-registered descriptors and object classes.
Definition: ClassRegistry.cpp:90
PrototypeRuntimeValidatorFunction exportedPrototypeRuntimeValidatorByIndex(std::size_t index) const noexcept final
Returns the runtime validator of a registered prototype layer for a given index.
Definition: ClassRegistry.cpp:288
NeuralResult importPluginLoader(IPluginLoader *pPluginLoader) final
Imports all plugins loaded by a plugin loader.
Definition: ClassRegistry.cpp:182
static OpaqueClassRegistrationHandle registerObjectClassInternal(const char *pObjectClass, CreationFunction classFactory)
Registers an object class and associated creation function.
Definition: ClassRegistry.cpp:330
NeuralResult importPlugin(IPlugin *pPlugin) final
Imports everything (layer, activation, fusing rules, and prototypes) registered with a single plugin.
Definition: ClassRegistry.cpp:99
const LayerDesc * exportedLayerByIndex(std::size_t index) const noexcept final
Returns a registered layer descriptor for a given index.
Definition: ClassRegistry.cpp:231
IRefObject *(*)() CreationFunction
Typedef for factory functions that take no arguments and return a new IRefObject.
Definition: ClassRegistry.h:75
~ClassRegistry()
Destroys the ClassRegistry.
Definition: ClassRegistry.cpp:95
const FusingRule * exportedFusingRuleByIndex(std::size_t index) const noexcept final
Returns a registered fusing rule for a given index.
Definition: ClassRegistry.cpp:252
std::size_t exportedLayersCount() const noexcept final
Returns the count of registered layers.
Definition: ClassRegistry.cpp:225
std::size_t exportedActivationsCount() const noexcept final
Returns the count of registered activation functions.
Definition: ClassRegistry.cpp:204
bool isPrototypeRuntimeCompatible(const char *pPrototypeObjectClass, const INetwork *pNetwork) const final
Checks if the given prototype object class is compatible with any of the registered prototypes.
Definition: ClassRegistry.cpp:482
static OpaqueClassRegistrationHandle registerActivationDesc(const ActivationDesc *pActivationDesc)
Registers an ActivationDesc structure for discovery.
Definition: ClassRegistry.cpp:342
static OpaqueClassRegistrationHandle registerFusingRule(std::uint32_t syntaxVersion, const char *pFusingRule)
Register a fusing rule for export.
Definition: ClassRegistry.cpp:336
INetwork is the public interface to Network.
Definition: NetworkTypes.h:119
IPlugin is the general factory interface used by NvNeural's plugin system.
Definition: PluginTypes.h:41
Platform-agnostic plugin loader interface.
Definition: PluginTypes.h:200
Base class for all objects, similar to COM's IUnknown.
Definition: CoreTypes.h:343
Structure describing an activation function (IActivationFunction) for tool interfaces and network bui...
Definition: CoreTypes.h:1634
const char * pObjectClass
This is the programmatic class name used to instantiate the activation object through the IPlugin::cr...
Definition: CoreTypes.h:1644
Defines a fusing rule that can be applied during model loading.
Definition: CoreTypes.h:1781
This structure describes an ILayer for tools and network builders.
Definition: CoreTypes.h:1554
const char * pObjectClass
This is the programmatic class name used to instantiate the layer object through the IPlugin::createO...
Definition: CoreTypes.h:1566
This structure represent a prototype layer version (backend, tensor format and kernel type),...
Definition: CoreTypes.h:1697
const char * pObjectClass
This is the programmatic class name used to instantiate the prototype object through the IPlugin::cre...
Definition: CoreTypes.h:1707
Parameterized base class implementing common IRefObject operations.
Definition: RefObject.h:336