NVIDIA NvNeural SDK  2021.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  m_evaluation.r = m_evaluation.s;
121  }
122  else
123  {
124  m_evaluation.r = m_evaluation.u;
125  }
126  m_signed = true;
127  m_depth = sizeof(T) * 8;
128  return;
129  }
130  else if (m_type == REAL)
131  {
132  m_depth = sizeof(T) * 8;
133  return;
134  }
135  }
136  else if (std::is_integral<T>::value)
137  {
138  if (m_type == INTEGER)
139  {
140  m_depth = sizeof(T) * 8;
141  m_signed = std::is_signed<T>::value;
142  return;
143  }
144  else if (m_type == REAL)
145  {
146  m_type = INTEGER;
147  m_evaluation.s = m_evaluation.r;
148  m_signed = std::is_signed<T>::value;
149  m_depth = sizeof(T) * 8;
150  return;
151  }
152  }
153  m_type = WRONG;
154  m_signed = false;
155  m_depth = 0;
156  }
157 
158  void setPointer(void* pVal, int memsize = 0);
159 
160  enum TypeCode
161  {
162  POINTER,
163  INTEGER,
164  REAL,
165  WRONG
166  };
167 
168  static std::vector<AnyOperand> divUp(const AnyOperand& op1, const AnyOperand& op2, bool typing);
169  static std::vector<AnyOperand> findDivisor(const AnyOperand& x, bool typing);
170 
171  void sub(const AnyOperand& op, bool fp16, bool typing);
172  void add(const AnyOperand& op, bool fp16, bool typing);
173 
174  void mul(const AnyOperand& op, bool typing);
175  void div(const AnyOperand& op, bool typing);
176 
177  void neg(bool typing);
178 
179  TypeCode type() const;
180  int depth() const;
181  int memsize() const;
182  int sizeInBytes() const;
183  void* representation();
184 
185  template<class T>
186  T real() const
187  {
188  T rv;
189  switch (m_type)
190  {
191  case REAL:
192  rv = m_evaluation.r;
193  break;
194  case INTEGER:
195  if (m_signed)
196  {
197  rv = double(m_evaluation.s);
198  }
199  else
200  {
201  rv = double(m_evaluation.u);
202  }
203  break;
204  default:
205  rv = 0.0;
206  break;
207  };
208  return rv;
209  }
210 
211  template<class T>
212  T integer() const
213  {
214  T rv = T(m_evaluation.s);
215  if (m_type == REAL)
216  {
217  rv = T(m_evaluation.r);
218  }
219  return rv;
220  }
221 
222  void* pointer() const;
223 
224  private:
225 
226  uint64_t masked_unsigned() const;
227 
228  void typeUpdate(const AnyOperand& op, bool blockToPointer);
229 
230  int m_depth = 0;
231  bool m_signed = false;
232  TypeCode m_type = WRONG;
233  int m_memsize = 0;
234 
235  union
236  {
237  uint64_t u;
238  int64_t s;
239  double r;
240  void* p;
241  } m_evaluation, m_representation;
242  };
243 
244 } // namespace detail
245 } // namespace nvneural
246 
247 #endif // !NVNEURAL_SCRIPTENGINEANYOP_H
Fundamental NvNeural data types are declared here.