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