NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
FilesystemWeightsLoader.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_FILESYSTEMWEIGHTSLOADER_H
27 #define NVNEURAL_FILESYSTEMWEIGHTSLOADER_H
28 
29 #include <nvneural/CoreTypes.h>
30 #include <nvneural/CoreHelpers.h>
31 #include <string>
32 
33 namespace nvneural {
34 
37  {
38  public:
40  static const IRefObject::TypeId typeID = 0x21f4e2b9c7038e85ul;
41 
52 
54  virtual NeuralResult setAllowRandomWeights(bool allowRandoms) noexcept = 0;
55 
57  virtual bool allowRandomWeights() const noexcept = 0;
58 
60 
66 
68  virtual NeuralResult setAllowNumpyWeights(bool allowNumpy) noexcept = 0;
69 
71  virtual bool allowNumpyWeights() const noexcept = 0;
72 
74 
86 
88  virtual NeuralResult setAllowBinaryWeights(bool allowBinary) noexcept = 0;
89 
91  virtual bool allowBinaryWeights() const noexcept = 0;
92 
94  };
95 
102  class FilesystemWeightsLoader: public refobj::RefObjectBase<
103  refobj::Implements<IFilesystemWeightsLoader>,
104  refobj::Implements<IWeightsLoader>>
105  {
106  public:
110  explicit FilesystemWeightsLoader(const std::string& weightsPrefix);
111 
113  NeuralResult getWeightsForLayer(IWeightsData** ppWeightsDataOut, const INetworkRuntime* pNetwork, const ILayer* pLayer, const char* pName) const noexcept override;
114 
116  NeuralResult setAllowRandomWeights(bool allowRandoms) noexcept override;
118  bool allowRandomWeights() const noexcept override;
119 
121  NeuralResult setAllowNumpyWeights(bool allowNumpy) noexcept override;
123  bool allowNumpyWeights() const noexcept override;
124 
126  NeuralResult setAllowBinaryWeights(bool allowBinary) noexcept override;
128  bool allowBinaryWeights() const noexcept override;
129 
130  protected:
136 
140  void init(const std::string& weightsPrefix);
141 
142  private:
143  std::string m_weightsPrefix;
144  bool m_allowRandomWeights;
145  bool m_allowBinaryWeights;
146  bool m_allowNumpyWeights;
147 
148  NeuralResult loadBinaryWeights(IWeightsData** ppWeightsDataOut, const std::string& networkName, const std::string& layerName, TensorDimension layerDim) const;
149  NeuralResult loadNumpyWeights(IWeightsData** ppWeightsDataOut, const std::string& networkName, const std::string& layerName, TensorDimension layerDim) const;
150  NeuralResult generateRandomWeights(IWeightsData** ppWeightsDataOut, const ILayer* pLayer, TensorDimension weightsDimension) const;
151 
152  NeuralResult tryLoadNumpyFile(IWeightsData** ppWeightsDataOut, const std::string& path, TensorDimension layerDim) const;
153  };
154 
155 } // namespace nvneural
156 
157 #endif // NVNEURAL_FILESYSTEMWEIGHTSLOADER_H
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
Standard IWeightsLoader interface that loads weights from the local filesystem.
Definition: FilesystemWeightsLoader.h:105
Official "public" interface allowing control of FilesystemWeightsLoader from other modules.
Definition: FilesystemWeightsLoader.h:37
virtual NeuralResult setAllowNumpyWeights(bool allowNumpy) noexcept=0
Turn NumPy support on or off.
virtual bool allowNumpyWeights() const noexcept=0
Returns true if NumPy support is enabled.
virtual bool allowRandomWeights() const noexcept=0
Returns true if random weights generation is enabled.
static const IRefObject::TypeId typeID
Interface TypeId for InterfaceOf purposes.
Definition: FilesystemWeightsLoader.h:40
virtual bool allowBinaryWeights() const noexcept=0
Returns true if binary support is enabled.
virtual NeuralResult setAllowRandomWeights(bool allowRandoms) noexcept=0
Turn random weights generation on or off.
virtual NeuralResult setAllowBinaryWeights(bool allowBinary) noexcept=0
Turn binary support on or off.
ILayer is the base class for neural network layers.
Definition: LayerTypes.h:59
INetworkRuntime is a subset of the basic network interface that is accessible from layer classes duri...
Definition: CoreTypes.h:1129
Base class for all objects, similar to COM's IUnknown.
Definition: CoreTypes.h:343
std::uint64_t TypeId
Every interface must define a unique TypeId. This should be randomized.
Definition: CoreTypes.h:349
IWeightsData represents a binary buffer provided by IWeightsLoader.
Definition: CoreTypes.h:494
IWeightsLoader is an interface that provides weights to layers.
Definition: CoreTypes.h:525
TensorDimension describes the dimensions of a four-dimensional image tensor.
Definition: CoreTypes.h:136