NVIDIA NvNeural SDK  2022.2
GPU inference framework for NVIDIA Nsight Deep Learning Designer
ScriptEngineAnyOp.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_SCRIPTENGINEANYOP_H
27 #define NVNEURAL_SCRIPTENGINEANYOP_H
28 
29 #include <cassert>
30 #include <nvneural/CoreTypes.h>
31 
32 namespace nvneural {
33 namespace detail {
34 
35  class AnyOperand
36  {
37  public:
38  AnyOperand();
39  AnyOperand(const AnyOperand& val);
40  AnyOperand(void* pVal);
41  AnyOperand(const Float16& val);
42 
43  template<class T>
44  AnyOperand(const T& val)
45  {
46  if (std::is_floating_point<T>::value)
47  {
48  setReal(val);
49  }
50  else if (std::is_integral<T>::value)
51  {
52  setInteger(val);
53  }
54  else if (!std::is_pointer<T>::value)
55  {
56  m_type = WRONG;
57  m_signed = false;
58  m_depth = 0;
59  }
60  else
61  {
62  assert(false);
63  }
64  }
65 
66  AnyOperand& operator=(const AnyOperand& val);
67 
68  template<class T>
69  void setInteger(T val)
70  {
71  m_depth = sizeof(T) * 8;
72  m_type = INTEGER;
73  m_signed = std::is_signed<T>::value;
74  m_evaluation.s = val;
75  }
76 
77  template<class T>
78  void setReal(T val)
79  {
80  m_depth = sizeof(T) * 8;
81  m_type = REAL;
82  m_signed = true;
83  m_evaluation.r = val;
84  }
85 
86  template<class T>
87  bool canTransform()
88  {
89  if (std::is_floating_point<T>::value || std::is_same<T, Float16>::value)
90  {
91  if (m_type == INTEGER || m_type == REAL)
92  {
93  return true;
94  }
95  }
96  else if (std::is_integral<T>::value)
97  {
98  if (m_type == INTEGER || m_type == REAL)
99  {
100  return true;
101  }
102  }
103  else if (std::is_pointer<T>::value)
104  {
105  return m_type == POINTER;
106  }
107  return false;
108  }
109 
110  template<class T>
111  void transform()
112  {
113  if (std::is_floating_point<T>::value || std::is_same<T, Float16>::value)
114  {
115  if (m_type == INTEGER)
116  {
117  m_type = REAL;
118  if (m_signed)
119  {
120  const double rTemp = static_cast<double>(m_evaluation.s);
121  m_evaluation.r = rTemp;
122  }
123  else
124  {
125  const double rTemp = static_cast<double>(m_evaluation.u);
126  m_evaluation.r = rTemp;
127  }
128  m_signed = true;
129  m_depth = sizeof(T) * 8;
130  return;
131  }
132  else if (m_type == REAL)
133  {
134  m_depth = sizeof(T) * 8;
135  return;
136  }
137  }
138  else if (std::is_integral<T>::value)
139  {
140  if (m_type == INTEGER)
141  {
142  m_depth = sizeof(T) * 8;
143  m_signed = std::is_signed<T>::value;
144  return;
145  }
146  else if (m_type == REAL)
147  {
148  m_type = INTEGER;
149  const int64_t sTemp = static_cast<int64_t>(m_evaluation.r);
150  m_evaluation.s = sTemp;
151  m_signed = std::is_signed<T>::value;
152  m_depth = sizeof(T) * 8;
153  return;
154  }
155  }
156  m_type = WRONG;
157  m_signed = false;
158  m_depth = 0;
159  }
160 
161  void setPointer(void* pVal, int memsize = 0);
162 
163  enum TypeCode
164  {
165  POINTER,
166  INTEGER,
167  REAL,
168  WRONG
169  };
170 
171  static std::vector<AnyOperand> divUp(const AnyOperand& op1, const AnyOperand& op2, bool typing);
172  static std::vector<AnyOperand> findDivisor(const AnyOperand& x, bool typing);
173 
174  void sub(const AnyOperand& op, bool fp16, bool typing);
175  void add(const AnyOperand& op, bool fp16, bool typing);
176 
177  void mul(const AnyOperand& op, bool typing);
178  void div(const AnyOperand& op, bool typing);
179 
180  void neg(bool typing);
181 
182  TypeCode type() const;
183  int depth() const;
184  int memsize() const;
185  int sizeInBytes() const;
186  void* representation();
187 
188  template<class T>
189  T real() const
190  {
191  T rv;
192  switch (m_type)
193  {
194  case REAL:
195  rv = m_evaluation.r;
196  break;
197  case INTEGER:
198  if (m_signed)
199  {
200  rv = double(m_evaluation.s);
201  }
202  else
203  {
204  rv = double(m_evaluation.u);
205  }
206  break;
207  default:
208  rv = 0.0;
209  break;
210  };
211  return rv;
212  }
213 
214  template<class T>
215  T integer() const
216  {
217  T rv = T(m_evaluation.s);
218  if (m_type == REAL)
219  {
220  rv = T(m_evaluation.r);
221  }
222  return rv;
223  }
224 
225  void* pointer() const;
226 
227  private:
228 
229  uint64_t masked_unsigned() const;
230 
231  void typeUpdate(const AnyOperand& op, bool blockToPointer);
232 
233  int m_depth = 0;
234  bool m_signed = false;
235  TypeCode m_type = WRONG;
236  int m_memsize = 0;
237 
238  union
239  {
240  uint64_t u;
241  int64_t s;
242  double r;
243  void* p;
244  } m_evaluation = {0}, m_representation = {0};
245  };
246 
247 } // namespace detail
248 } // namespace nvneural
249 
250 #endif // !NVNEURAL_SCRIPTENGINEANYOP_H
Fundamental NvNeural data types are declared here.