NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
CoreHelpers.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 // Definitions in this file are not part of the version-safe ABI, but are provided as aids.
27 
28 #ifndef NVNEURAL_COREHELPERS_H
29 #define NVNEURAL_COREHELPERS_H
30 
31 #include <atomic>
32 #include <functional>
33 #include <string>
34 #include <typeinfo>
35 #include <type_traits>
36 #include <utility>
37 #include <nvneural/CoreTypes.h>
38 #include <nvneural/LayerTypes.h>
39 #include <nvneural/RefPtr.h>
40 
41 namespace nvneural {
42 
47 ILogger* DefaultLogger();
48 
50 void SetDefaultLogger(ILogger* pDefaultLogger);
51 
53 enum class TensorDimensionParseOrder : std::uint32_t
54 {
61  Tensor = 0,
62 
69  Image = 1,
70 
71  // When porting older code, a trailing ", true" in the tensorDim parse call means Image/whcn mode.
72 };
73 
84 NeuralResult TensorDimensionFromString(TensorDimension& outDim, const std::string& dimStr, TensorDimensionParseOrder parseOrder, size_t fillValue) noexcept;
85 
92 std::string TensorDimensionToString(const TensorDimension& dim) noexcept;
93 
99 inline TensorDimension align(const TensorDimension& dim, const TensorDimension& stepping) noexcept
100 {
101  const auto alignComponent = [](size_t x, size_t y) -> size_t { return ((x + y - 1) / y) * y; };
102  const TensorDimension result
103  {
104  alignComponent(dim.n, stepping.n),
105  alignComponent(dim.c, stepping.c),
106  alignComponent(dim.h, stepping.h),
107  alignComponent(dim.w, stepping.w),
108  };
109  return result;
110 }
111 
122 template<typename TDividend, typename TDivisor>
123 inline auto constexpr DivideRoundingUp(TDividend dividend, TDivisor divisor) noexcept
124 {
125  return ((dividend + divisor - TDivisor{1}) / divisor);
126 }
127 
134 inline constexpr std::size_t dataSize(TensorDataType dataType) noexcept
135 {
136  switch (dataType)
137  {
138  case TensorDataType::Float: return sizeof(float);
139  case TensorDataType::Half: return sizeof(Float16);
140  default:
141  return 0;
142  }
143 }
144 
155 inline std::vector<std::string> split(std::string strToSplit, char delimiter, bool skip_empty = false)
156 {
157  std::vector<std::string> splitStrings;
158  size_t j = 0;
159  for(size_t i = 0; i < strToSplit.size(); i++)
160  {
161  if(strToSplit[i] == delimiter)
162  {
163  if(!skip_empty || (i - j))
164  {
165  splitStrings.push_back(strToSplit.substr(j, i - j));
166  }
167  j = i + 1;
168  }
169 
170  if(i == strToSplit.size() - 1)
171  {
172  if(!skip_empty || (strToSplit.size() - j))
173  {
174  splitStrings.push_back(strToSplit.substr(j, strToSplit.size() - j));
175  }
176  }
177  }
178  return splitStrings;
179 }
180 
193 inline std::vector<std::string> splitEscape(std::string strToSplit, char delimiter, bool skip_empty = false)
194 {
195  std::vector<std::string> splitStrings;
196 
197  std::string buildString;
198  bool lastCharEscape = false;
199  for (size_t newSplitNdx = 0; newSplitNdx < strToSplit.size(); newSplitNdx++)
200  {
201  if ((strToSplit[newSplitNdx] == '\\') && (!lastCharEscape))
202  {
203  lastCharEscape = true;
204  }
205  else
206  {
207  if ((strToSplit[newSplitNdx] == delimiter) && (!lastCharEscape))
208  {
209  if (!skip_empty || !buildString.empty())
210  {
211  splitStrings.push_back(buildString);
212  buildString.clear();
213  }
214  }
215  else
216  {
217  try
218  {
219  buildString += strToSplit[newSplitNdx];
220  }
221  catch (const std::exception& ex)
222  {
223  DefaultLogger()->logWarning(0, "Splitting string problem: %s : %s", buildString.c_str(), ex.what());
224  return splitStrings;
225  }
226  lastCharEscape = false;
227  }
228  }
229  }
230 
231  if (!skip_empty || !buildString.empty())
232  {
233  splitStrings.push_back(buildString);
234  buildString.clear();
235  }
236 
237  return splitStrings;
238 }
239 
246 inline NeuralResult getInputLayerIndex(ILayer* pLayer, ILayer* pInputLayer, size_t* pIndex)
247 {
248  RefPtr<ILayerList> pInputList;
249  const auto status = pLayer->getInputLayers(pInputList.put());
250  if (failed(status))
251  {
252  return status;
253  }
254  for (size_t index = 0; index < pInputList->layerCount(); ++index)
255  {
256  const auto pInput = pInputList->getLayerByIndex(index);
257  if (pInput == pInputLayer)
258  {
259  *pIndex = index;
260  return NeuralResult::Success;
261  }
262  }
263 
264  return NeuralResult::Failure;
265 }
266 
267 } // namespace nvneural
268 
269 namespace std {
270 
274 template<> struct hash<nvneural::NetworkBackendId>
275 {
279  std::size_t operator ()(nvneural::NetworkBackendId backendId) const noexcept
280  {
281  return std::hash<uint32_t>()(static_cast<std::uint32_t>(backendId));
282  }
283 };
284 
285 } // namespace std
286 
287 // compat: RefObjectBase and friends moved to a different header
288 #include <nvneural/RefObject.h>
289 
290 #endif // NVNEURAL_COREHELPERS_H
std::vector< std::string > split(std::string strToSplit, char delimiter, bool skip_empty=false)
Splits a string by a delimiter, returns a vector of the split strings.
Definition: CoreHelpers.h:155
std::vector< std::string > splitEscape(std::string strToSplit, char delimiter, bool skip_empty=false)
Splits a string by a delimiter, returns a vector of the split strings.
Definition: CoreHelpers.h:193
void SetDefaultLogger(ILogger *pDefaultLogger)
Sets a default logger for the module.
Definition: Logging.cpp:43
constexpr auto DivideRoundingUp(TDividend dividend, TDivisor divisor) noexcept
Division operation, rounding up.
Definition: CoreHelpers.h:123
ILogger * DefaultLogger()
Returns a pointer to the default logger for this module.
Definition: Logging.cpp:38
constexpr std::size_t dataSize(TensorDataType dataType) noexcept
Returns the size of an individual tensor element of a particular type, similar to sizeof.
Definition: CoreHelpers.h:134
TensorDimension align(const TensorDimension &dim, const TensorDimension &stepping) noexcept
Rounds a TensorDimension object up to be a multiple of a provided set of strides.
Definition: CoreHelpers.h:99
TensorDimensionParseOrder
Order to read TensorDimension components in for TensorDimensionFromString.
Definition: CoreHelpers.h:54
@ Tensor
This order is used for parameters that typically describe tensor dimensions.
NeuralResult getInputLayerIndex(ILayer *pLayer, ILayer *pInputLayer, size_t *pIndex)
Retrieves the layer index in the input list.
Definition: CoreHelpers.h:246
Fundamental NvNeural data types are declared here.
constexpr bool failed(NeuralResult result) noexcept
Helper function akin to COM's FAILED() macro.
Definition: CoreTypes.h:289
TensorDataType
Enumeration describing common tensor element types.
Definition: CoreTypes.h:60
NetworkBackendId
Enumeration describing common network backends.
Definition: CoreTypes.h:239
NeuralResult
NeuralResult is a generic success/failure result type similar to COM HRESULT.
Definition: CoreTypes.h:275
Interface types needed by layer objects.
Standard implementation for IRefObject-derived objects.
Smart pointer class for tracking IRefObject instances.
Float16 is a CPU-side implementation of half-precision floating point (aka FP16).
Definition: CoreTypes.h:201
ILayer is the base class for neural network layers.
Definition: LayerTypes.h:59
virtual NeuralResult getInputLayers(ILayerList **ppInputLayers) const noexcept=0
Retrieves the inputs for this layer.
virtual NeuralResult logWarning(VerbosityLevel verbosity, const char *format,...) noexcept=0
Logs a warning message.
Standard implementation for IFileImage.
Definition: CpuImage.h:113
Intrusive pointer using IRefObject's reference counting system.
Definition: RefPtr.h:46
OutputWrapper put() &noexcept
Returns an expression proxy that acts like a TObject** for receiving new pointers.
Definition: RefPtr.h:337
TensorDimension describes the dimensions of a four-dimensional image tensor.
Definition: CoreTypes.h:136