Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
SOFIE_common.cxx
Go to the documentation of this file.
2#include<cctype>
3#include <sstream>
4#include <iostream>
5#include <stdexcept>
6
7namespace TMVA{
8namespace Experimental{
9namespace SOFIE{
10
11std::vector<Dim> ConvertShapeToDim(std::vector<size_t> shape){
12 std::vector<Dim> fshape(shape.size());
13 for (size_t i =0; i < shape.size(); i++){
14 fshape[i].dim = shape[i];
15 }
16 return fshape;
17}
18
19std::size_t ConvertShapeToLength(std::vector<size_t> shape){
20 // Empty shape represent scalar values, so we return a length=1
21 std::size_t fLength = 1;
22 for (auto& dim: shape) fLength *= dim;
23 return fLength;
24}
25
27 switch(type){
28 case ETensorType::FLOAT : {
29 return "float";
30 }
31 case ETensorType::INT16 : {
32 return "int16_t";
33 }
34 case ETensorType::INT32 : {
35 return "int32_t";
36 }
37 case ETensorType::INT64 : {
38 return "int64_t";
39 }
40 case ETensorType::UINT16 : {
41 return "uint16_t";
42 }
43 case ETensorType::UINT32 : {
44 return "uint32_t";
45 }
46 case ETensorType::UINT64 : {
47 return "uint64_t";
48 }
49 case ETensorType::DOUBLE : {
50 return "double";
51 }
52 default:{
53 return "other";
54 }
55 }
56}
57
59 if(type == "float32" || type == "float" || type == "Float"){
60 return ETensorType::FLOAT;
61 }
62 else if(type == "int64"){
63 return ETensorType::INT64;
64 }
65 else if (type == "double" || type == "float64"){
67 }
68 else{
70 }
71}
72
73std::string ConvertShapeToString(std::vector<size_t> shape) {
74 std::stringstream out;
75 out << "{ ";
76 for (size_t i = 0; i < shape.size(); i++) {
77 out << shape[i];
78 if (i < shape.size()-1) out << " , ";
79 }
80 out << " }";
81 return out.str();
82}
83
84namespace{
85template<typename T>
86static inline void copy_vector_data(int_t no_of_copies, int_t input_size, T* input, T* target){ //only visible within this translation unit
87 std::memcpy(target, input, input_size * sizeof(T));
88 int_t already_copied = 1;
89
90 while (already_copied * 2 <= no_of_copies){
91 std::memcpy(target + already_copied * input_size, target, already_copied * input_size * sizeof(T));
92 already_copied *= 2;
93 }
94
95 if (already_copied < no_of_copies){
96 std::memcpy(target + already_copied * input_size, target, (no_of_copies - already_copied) * input_size * sizeof(T));
97 }
98}
99}
100
101bool UTILITY::AreSameShape(const std::vector<size_t>& shapeA, const std::vector<size_t>& shapeB) {
102 if (shapeA.size() != shapeB.size()) {
103 return false;
104 }
105 for (size_t dim = 0; dim < shapeA.size(); dim++) {
106 if (shapeA[dim] != shapeB[dim]) {
107 return false;
108 }
109 }
110 return true;
111}
112
113std::vector<size_t> UTILITY::MultidirectionalBroadcastShape(std::vector<std::vector<size_t>> shape)
114{
115 if (shape.size() < 2) {
116 throw
117 std::runtime_error("TMVA::SOFIE - MultidirectionalBroadcastShape requires at least 2 input shapes.");
118 }
119 // Number of input shapes to broadcast
120 size_t n = shape.size();
121 // Size of the output shape
122 size_t targetSize = shape[0].size();
123 for (size_t i = 1; i < n; i++) {
124 targetSize = std::max(targetSize, shape[i].size());
125 }
126 // Check if they have the same size
127 bool sameSize = true;
128 for (size_t i = 0; i < n; i++) {
129 if (shape[i].size() != targetSize) {
130 sameSize = false;
131 break;
132 }
133 }
134 if (sameSize) {
135 // Check if they have the same shape
136 bool sameShape = true;
137 for (size_t i = 1; i < n; i++) {
138 for (size_t dim = 0; dim < shape[0].size(); dim++) {
139 if (shape[i][dim] != shape[0][dim]) {
140 sameShape = false;
141 break;
142 }
143 }
144 if (!sameShape) {
145 break;
146 }
147 }
148 if (sameShape) {
149 return shape[0];
150 } else {
151 // Set the target shape
152 std::vector<size_t> targetShape(targetSize, 1);
153 for (size_t i = 0; i < n; i++) {
154 for (size_t dim = 0; dim < targetSize; dim++) {
155 targetShape[dim] = std::max(targetShape[dim], shape[i][dim]);
156 }
157 }
158 // Check if the input shapes are broadcastable to targetShape
159 bool broadcastable = true;
160 for (size_t i = 0; i < n; i++) {
161 for (size_t dim = 0; dim < targetSize; dim++) {
162 if (shape[i][dim] != 1 && targetShape[dim] != 1 && shape[i][dim] != targetShape[dim]) {
163 broadcastable = false;
164 break;
165 }
166 if (!broadcastable) {
167 break;
168 }
169 }
170 }
171 // They have the same shape and they are broadcastable to targetShape
172 if (broadcastable) {
173 return targetShape;
174 } else {
175 std::stringstream ss;
176 ss << "TMVA::SOFIE - Error multidirectional broadcasting shapes ";
177 for (size_t i = 0; i < n; i++) {
178 ss << ConvertShapeToString(shape[i]);
179 if (n > 2 && i < n - 2) {
180 ss << ", ";
181 } else if ( n >=2 && i == n - 2) {
182 ss << " and ";
183 }
184 }
185 ss << " to the same shape.";
186 throw
187 std::runtime_error(ss.str());
188 }
189 } // end sameShape
190 } // end sameSize
191 // Prepend the ith shape with ones
192 for (size_t i = 0; i < n; i++) {
193 if (shape[i].size() < targetSize) {
194 std::vector<size_t> newShape(targetSize, 1);
195 size_t offset = targetSize - shape[i].size();
196 std::copy(shape[i].begin(), shape[i].end(), newShape.begin() + offset);
197 shape[i] = newShape;
198 }
199 }
200 // Set the target shape
201 std::vector<size_t> targetShape(targetSize, 1);
202 for (size_t i = 0; i < n; i++) {
203 for (size_t dim = 0; dim < targetSize; dim++) {
204 targetShape[dim] = std::max(targetShape[dim], shape[i][dim]);
205 }
206 }
207 // Check if the shapes are broadcastable to targetShape
208 bool broadcastable = true;
209 for (size_t i = 0; i < n; i++) {
210 for (size_t dim = 0; dim < targetSize; dim++) {
211 if (shape[i][dim] != targetShape[dim] && shape[i][dim] != 1 && targetShape[dim] != 1) {
212 broadcastable = false;
213 break;
214 }
215 }
216 if (!broadcastable) {
217 break;
218 }
219 }
220 if (broadcastable) {
221 return targetShape;
222 } else {
223 std::stringstream ss;
224 ss << "TMVA::SOFIE - Error multidirectional broadcasting shapes ";
225 for (size_t i = 0; i < n; i++) {
226 ss << ConvertShapeToString(shape[i]);
227 if (n > 2 && i < n - 2) {
228 ss << ", ";
229 } else if ( n >=2 && i == n - 2) {
230 ss << " and ";
231 }
232 }
233 ss << " to the same shape.";
234 throw
235 std::runtime_error(ss.str());
236 }
237}
238
239std::vector<size_t> UTILITY::UnidirectionalBroadcastShape(std::vector<size_t> shapeA, std::vector<size_t> shapeB)
240{
241 size_t sizeA = shapeA.size();
242 size_t sizeB = shapeB.size();
243 // Check if A and B have the same shape
244 if (UTILITY::AreSameShape(shapeA, shapeB)){
245 return shapeA;
246 }
247 // Find the common shape of A and B
248 size_t size = std::max(sizeA, sizeB);
249 if (sizeA < size) {
250 std::vector<size_t> newShapeA(size, 1);
251 size_t offset = size - sizeA;
252 std::copy(shapeA.begin(), shapeA.end(), newShapeA.begin() + offset);
253 shapeA = std::move(newShapeA);
254 }
255 if (sizeB < size) {
256 std::vector<size_t> newShapeB(size, 1);
257 size_t offset = size - sizeB;
258 std::copy(shapeB.begin(), shapeB.end(), newShapeB.begin() + offset);
259 shapeB = std::move(newShapeB);
260 }
261 bool broadcastable = true;
262 for (size_t i = 0; i < size; i++) {
263 if (shapeA[i] != shapeB[i] && shapeA[i] != 1 && shapeB[i] != 1) {
264 broadcastable = false;
265 break;
266 }
267 }
268 if (broadcastable) {
269 // The output shape is max(outShape, targetShape)
270 std::vector<size_t> targetShape(size, 1);
271 for (size_t i = 0; i < size; i++) {
272 targetShape[i] = std::max(shapeA[i], shapeB[i]);
273 }
274 return targetShape;
275 } else {
276 throw
277 std::runtime_error("TMVA::SOFIE - Error unidirectional broadcasting tensors of shape "
278 + ConvertShapeToString(shapeA) + " and " + ConvertShapeToString(shapeB)
279 + " to a common shape.");
280 }
281}
282
283std::string UTILITY::Clean_name(std::string input_tensor_name){
284 std::string s (input_tensor_name);
285 s.erase(std::remove_if(s.begin(), s.end(), []( char const& c ) -> bool { return !std::isalnum(c); } ), s.end());
286 return s;
287}
288
289std::vector<size_t> UTILITY::ComputeStrideFromShape(const std::vector<size_t> & shape) {
290 // assume row major layout
291 const auto size = shape.size();
292 std::vector<size_t> strides(size,1);
293 for (std::size_t i = 1; i < size; i++) {
294 strides[size - 1 - i] = strides[size - 1 - i + 1] * shape[size - 1 - i + 1];
295 }
296 return strides;
297}
298
299
300}//SOFIE
301}//Experimental
302}//TMVA
#define c(i)
Definition RSha256.hxx:101
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
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 Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t type
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)
std::string ConvertShapeToString(std::vector< size_t > shape)
std::string ConvertTypeToString(ETensorType type)
ETensorType ConvertStringToType(std::string type)
std::size_t ConvertShapeToLength(std::vector< size_t > shape)
create variable transformations