Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
SOFIE_common.cxx
Go to the documentation of this file.
2
3#include <cctype>
4#include <sstream>
5#include <stdexcept>
6
7namespace TMVA {
8namespace Experimental {
9namespace SOFIE {
10
11/// @brief Convert shape from integer format to dynamic one (based on Dim)
12/// @param shape
13/// @return shape based on Dim
14std::vector<Dim> ConvertShapeToDim(std::vector<size_t> shape){
15 std::vector<Dim> ret_shape(shape.size());
16 for (size_t i =0; i < shape.size(); i++){
17 ret_shape[i].dim = shape[i];
18 }
19 return ret_shape;
20}
21
22/// @brief Convert shape based on Dim to integer format
23/// @param shape
24/// @return shape based on integer. Return an empty shape in case shape is dynamic (has a parameter)
25std::vector<size_t> ConvertShapeToInt(std::vector<Dim> shape){
26 std::vector<size_t> ret_shape(shape.size());
27 for (size_t i =0; i < shape.size(); i++){
28 if (shape[i].isParam) {
29 // try converting to integer in case string is a number >=0
30 int val = -1;
31 try {
32 val = std::stoi(shape[i].param);
33 if (val >= 0) ret_shape[i] = static_cast<size_t>(val);
34 else {
35 ret_shape.clear();
36 break;
37 }
38 }
39 catch (const std::invalid_argument& ) {
40 ret_shape.clear();
41 break;
42 }
43 } else {
44 ret_shape[i] = shape[i].dim;
45 }
46 }
47 return ret_shape;
48}
49
50
51std::size_t ConvertShapeToLength(std::vector<size_t> shape){
52 // Empty shape represent scalar values, so we return a length=1
53 std::size_t fLength = 1;
54 for (auto& dim: shape) fLength *= dim;
55 return fLength;
56}
57
59 switch(type){
60 case ETensorType::FLOAT : {
61 return "float";
62 }
63 case ETensorType::INT8 : {
64 return "int8_t";
65 }
66 case ETensorType::INT16 : {
67 return "int16_t";
68 }
69 case ETensorType::INT32 : {
70 return "int32_t";
71 }
72 case ETensorType::INT64 : {
73 return "int64_t";
74 }
75 case ETensorType::UINT8 : {
76 return "uint8_t";
77 }
78 case ETensorType::UINT16 : {
79 return "uint16_t";
80 }
81 case ETensorType::UINT32 : {
82 return "uint32_t";
83 }
84 case ETensorType::UINT64 : {
85 return "uint64_t";
86 }
87 case ETensorType::DOUBLE : {
88 return "double";
89 }
90 case ETensorType::BOOL : {
91 return "bool";
92 }
93 default:{
94 return "other_" + std::to_string( (int) type);
95 }
96 }
97}
98
100 if(type == "float32" || type == "float" || type == "Float"){
101 return ETensorType::FLOAT;
102 }
103 else if(type == "int64" || type == "int64_t"){
104 return ETensorType::INT64;
105 }
106 else if (type == "double" || type == "float64"){
107 return ETensorType::DOUBLE;
108 }
109 else if (type == "bool" ){
110 return ETensorType::BOOL;
111 }
112 else{
114 }
115}
116
117std::string ConvertShapeToString(std::vector<size_t> shape) {
118 std::stringstream out;
119 out << "{ ";
120 for (size_t i = 0; i < shape.size(); i++) {
121 out << shape[i];
122 if (i < shape.size()-1) out << " , ";
123 }
124 out << " }";
125 return out.str();
126}
127
128std::string ConvertDynamicShapeToString(std::vector<Dim> shape) {
129 std::stringstream out;
130 out << "{ ";
131 for (size_t i = 0; i < shape.size(); i++) {
132 out << shape[i].GetVal();
133 if (i < shape.size()-1) out << " , ";
134 }
135 out << " }";
136 return out.str();
137}
138
139std::string ConvertDynamicShapeToLength(std::vector<Dim> shape) {
140 // convert generic shape to a string
141 // multiply all the integer specified dimensions of the shape
142 std::string length;
143 size_t int_length = 0;
144 for (size_t i = 0; i < shape.size(); i++) {
145 if (shape[i].isParam) {
146 if (!length.empty()) length += " * ";
147 length += shape[i].param;
148 } else {
149 if (int_length == 0)
150 int_length = shape[i].dim;
151 else
152 int_length *= shape[i].dim;
153 }
154 }
155 // multiply the integer components to the parametric one
156 if (int_length > 0) {
157 if (!length.empty()) length += " * ";
158 length += std::to_string(int_length);
159 }
160 return length;
161}
162
163namespace{
164template<typename T>
165static inline void copy_vector_data(int_t no_of_copies, int_t input_size, T* input, T* target){ //only visible within this translation unit
166 std::memcpy(target, input, input_size * sizeof(T));
168
169 while (already_copied * 2 <= no_of_copies){
170 std::memcpy(target + already_copied * input_size, target, already_copied * input_size * sizeof(T));
171 already_copied *= 2;
172 }
173
175 std::memcpy(target + already_copied * input_size, target, (no_of_copies - already_copied) * input_size * sizeof(T));
176 }
177}
178}
179
180bool UTILITY::AreSameShape(const std::vector<size_t>& shapeA, const std::vector<size_t>& shapeB) {
181 if (shapeA.size() != shapeB.size()) {
182 return false;
183 }
184 for (size_t dim = 0; dim < shapeA.size(); dim++) {
185 if (shapeA[dim] != shapeB[dim]) {
186 return false;
187 }
188 }
189 return true;
190}
191bool UTILITY::AreSameShape(const std::vector<size_t>& shapeA, const std::vector<Dim>& shapeB) {
192 if (shapeA.size() != shapeB.size()) {
193 return false;
194 }
195 for (size_t dim = 0; dim < shapeA.size(); dim++) {
196 if (shapeB[dim].isParam) return false;
197 if (shapeA[dim] != shapeB[dim].dim) {
198 return false;
199 }
200 }
201 return true;
202}
203bool UTILITY::AreSameShape(const std::vector<Dim>& shapeA, const std::vector<Dim>& shapeB) {
204 if (shapeA.size() != shapeB.size()) {
205 return false;
206 }
207 for (size_t dim = 0; dim < shapeA.size(); dim++) {
208 if (shapeA[dim].GetVal() != shapeB[dim].GetVal()) {
209 return false;
210 }
211 }
212 return true;
213}
214
215std::vector<size_t> UTILITY::MultidirectionalBroadcastShape(std::vector<std::vector<size_t>> shape)
216{
217 if (shape.size() < 2) {
218 throw
219 std::runtime_error("TMVA::SOFIE - MultidirectionalBroadcastShape requires at least 2 input shapes.");
220 }
221 // Number of input shapes to broadcast
222 size_t n = shape.size();
223 // Size of the output shape
224 size_t targetSize = shape[0].size();
225 for (size_t i = 1; i < n; i++) {
226 targetSize = std::max(targetSize, shape[i].size());
227 }
228 // Check if they have the same size
229 bool sameSize = true;
230 for (size_t i = 0; i < n; i++) {
231 if (shape[i].size() != targetSize) {
232 sameSize = false;
233 break;
234 }
235 }
236 if (sameSize) {
237 // Check if they have the same shape
238 bool sameShape = true;
239 for (size_t i = 1; i < n; i++) {
240 for (size_t dim = 0; dim < shape[0].size(); dim++) {
241 if (shape[i][dim] != shape[0][dim]) {
242 sameShape = false;
243 break;
244 }
245 }
246 if (!sameShape) {
247 break;
248 }
249 }
250 if (sameShape) {
251 return shape[0];
252 } else {
253 // Set the target shape
254 std::vector<size_t> targetShape(targetSize, 1);
255 for (size_t i = 0; i < n; i++) {
256 for (size_t dim = 0; dim < targetSize; dim++) {
257 targetShape[dim] = std::max(targetShape[dim], shape[i][dim]);
258 }
259 }
260 // Check if the input shapes are broadcastable to targetShape
261 bool broadcastable = true;
262 for (size_t i = 0; i < n; i++) {
263 for (size_t dim = 0; dim < targetSize; dim++) {
264 if (shape[i][dim] != 1 && targetShape[dim] != 1 && shape[i][dim] != targetShape[dim]) {
265 broadcastable = false;
266 break;
267 }
268 if (!broadcastable) {
269 break;
270 }
271 }
272 }
273 // They have the same shape and they are broadcastable to targetShape
274 if (broadcastable) {
275 return targetShape;
276 } else {
277 std::stringstream ss;
278 ss << "TMVA::SOFIE - Error multidirectional broadcasting shapes ";
279 for (size_t i = 0; i < n; i++) {
280 ss << ConvertShapeToString(shape[i]);
281 if (n > 2 && i < n - 2) {
282 ss << ", ";
283 } else if ( n >=2 && i == n - 2) {
284 ss << " and ";
285 }
286 }
287 ss << " to the same shape.";
288 throw
289 std::runtime_error(ss.str());
290 }
291 } // end sameShape
292 } // end sameSize
293 // Prepend the ith shape with ones
294 for (size_t i = 0; i < n; i++) {
295 if (shape[i].size() < targetSize) {
296 std::vector<size_t> newShape(targetSize, 1);
297 size_t offset = targetSize - shape[i].size();
298 std::copy(shape[i].begin(), shape[i].end(), newShape.begin() + offset);
299 shape[i] = newShape;
300 }
301 }
302 // Set the target shape
303 std::vector<size_t> targetShape(targetSize, 1);
304 for (size_t i = 0; i < n; i++) {
305 for (size_t dim = 0; dim < targetSize; dim++) {
306 targetShape[dim] = std::max(targetShape[dim], shape[i][dim]);
307 }
308 }
309 // Check if the shapes are broadcastable to targetShape
310 bool broadcastable = true;
311 for (size_t i = 0; i < n; i++) {
312 for (size_t dim = 0; dim < targetSize; dim++) {
313 if (shape[i][dim] != targetShape[dim] && shape[i][dim] != 1 && targetShape[dim] != 1) {
314 broadcastable = false;
315 break;
316 }
317 }
318 if (!broadcastable) {
319 break;
320 }
321 }
322 if (broadcastable) {
323 return targetShape;
324 } else {
325 std::stringstream ss;
326 ss << "TMVA::SOFIE - Error multidirectional broadcasting shapes ";
327 for (size_t i = 0; i < n; i++) {
328 ss << ConvertShapeToString(shape[i]);
329 if (n > 2 && i < n - 2) {
330 ss << ", ";
331 } else if ( n >=2 && i == n - 2) {
332 ss << " and ";
333 }
334 }
335 ss << " to the same shape.";
336 throw
337 std::runtime_error(ss.str());
338 }
339}
340
341std::vector<size_t> UTILITY::UnidirectionalBroadcastShape(std::vector<size_t> shapeA, std::vector<size_t> shapeB)
342{
343 size_t sizeA = shapeA.size();
344 size_t sizeB = shapeB.size();
345 // Check if A and B have the same shape
347 return shapeA;
348 }
349 // Find the common shape of A and B
350 size_t size = std::max(sizeA, sizeB);
351 if (sizeA < size) {
352 std::vector<size_t> newShapeA(size, 1);
353 size_t offset = size - sizeA;
354 std::copy(shapeA.begin(), shapeA.end(), newShapeA.begin() + offset);
355 shapeA = std::move(newShapeA);
356 }
357 if (sizeB < size) {
358 std::vector<size_t> newShapeB(size, 1);
359 size_t offset = size - sizeB;
360 std::copy(shapeB.begin(), shapeB.end(), newShapeB.begin() + offset);
361 shapeB = std::move(newShapeB);
362 }
363 bool broadcastable = true;
364 for (size_t i = 0; i < size; i++) {
365 if (shapeA[i] != shapeB[i] && shapeA[i] != 1 && shapeB[i] != 1) {
366 broadcastable = false;
367 break;
368 }
369 }
370 if (broadcastable) {
371 // The output shape is max(outShape, targetShape)
372 std::vector<size_t> targetShape(size, 1);
373 for (size_t i = 0; i < size; i++) {
374 targetShape[i] = std::max(shapeA[i], shapeB[i]);
375 }
376 return targetShape;
377 } else {
378 throw
379 std::runtime_error("TMVA::SOFIE - Error unidirectional broadcasting tensors of shape "
381 + " to a common shape.");
382 }
383}
384
385std::string UTILITY::Clean_name(std::string input_tensor_name){
386 std::string s (input_tensor_name);
387 std::replace( s.begin(), s.end(), '-', '_');
388 // replace all non-alpohanumeric character except for "_"
389 s.erase(std::remove_if(s.begin(), s.end(), []( char const& c ) -> bool { return !std::isalnum(c) && c != '_'; } ), s.end());
390 return s;
391}
392
393std::vector<size_t> UTILITY::ComputeStrideFromShape(const std::vector<size_t> & shape) {
394 // assume row major layout
395 const auto size = shape.size();
396 std::vector<size_t> strides(size,1);
397 for (std::size_t i = 1; i < size; i++) {
398 strides[size - 1 - i] = strides[size - i ] * shape[size - i];
399 }
400 return strides;
401}
402
403std::vector<Dim> UTILITY::ComputeStrideFromShape(const std::vector<Dim> & shape) {
404 // assume row major layout
405 const auto size = shape.size();
406 std::vector<Dim> strides(size);
407 strides[size-1] = Dim{1};
408 for (std::size_t i = 1; i < size; i++) {
409 if (!shape[size-i].isParam && !strides[size-i].isParam)
410 strides[size - 1 - i] = Dim{strides[size-i].dim * shape[size-i].dim};
411 else
412 strides[size - 1 - i] = Dim{std::string(strides[size-i].GetVal() + "*" + shape[size-i].GetVal())};
413 }
414 return strides;
415}
416
417} // namespace SOFIE
418} // namespace Experimental
419} // namespace TMVA
#define c(i)
Definition RSha256.hxx:101
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void input
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h length
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
const_iterator begin() const
const_iterator end() const
const Int_t n
Definition legend1.C:16
bool AreSameShape(const std::vector< size_t > &, const std::vector< size_t > &)
std::vector< size_t > UnidirectionalBroadcastShape(std::vector< size_t >, std::vector< size_t >)
std::string Clean_name(std::string input_tensor_name)
std::vector< size_t > MultidirectionalBroadcastShape(std::vector< std::vector< size_t > >)
std::vector< size_t > ComputeStrideFromShape(const std::vector< size_t > &shape)
compute stride of a tensor given its shape (assume layout is row-major)
std::vector< Dim > ConvertShapeToDim(std::vector< size_t > shape)
Convert shape from integer format to dynamic one (based on Dim)
std::string ConvertDynamicShapeToLength(std::vector< Dim > shape)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertTypeToString(ETensorType type)
std::string ConvertDynamicShapeToString(std::vector< Dim > shape)
ETensorType ConvertStringToType(std::string type)
std::vector< size_t > ConvertShapeToInt(std::vector< Dim > shape)
Convert shape based on Dim to integer format.
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations