forked from root-project/root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RModel.cxx
772 lines (696 loc) · 30.4 KB
/
RModel.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
#include <limits>
#include <algorithm>
#include <cctype>
#include <memory>
#include "TFile.h"
#include "TMVA/RModel.hxx"
#include "TMVA/SOFIE_common.hxx"
namespace TMVA {
namespace Experimental {
namespace SOFIE {
std::underlying_type_t<Options> operator|(Options opA, Options opB) {
return static_cast<std::underlying_type_t<Options>>(opA) | static_cast<std::underlying_type_t<Options>>(opB);
}
std::underlying_type_t<Options> operator|(std::underlying_type_t<Options> opA, Options opB) {
return opA | static_cast<std::underlying_type_t<Options>>(opB);
}
RModel::RModel(RModel&& other) {
fInputTensorInfos = std::move(other.fInputTensorInfos);
fReadyInputTensorInfos = std::move(other.fReadyInputTensorInfos);
fOutputTensorNames = other.fOutputTensorNames;
fInputTensorNames = other.fInputTensorNames;
fOperators = std::move(other.fOperators);
fInitializedTensors = std::move(other.fInitializedTensors);
fIntermediateTensorInfos = std::move(other.fIntermediateTensorInfos);
fName = other.fName;
fFileName = other.fFileName;
fParseTime = other.fParseTime;
fGC = other.fGC;
fNeededBlasRoutines = other.fNeededBlasRoutines;
fNeededStdLib = other.fNeededStdLib;
}
RModel& RModel::operator=(RModel&& other) {
fInputTensorInfos = std::move(other.fInputTensorInfos);
fReadyInputTensorInfos = std::move(other.fReadyInputTensorInfos);
fOutputTensorNames = other.fOutputTensorNames;
fInputTensorNames = other.fInputTensorNames;
fOperators = std::move(other.fOperators);
fInitializedTensors = std::move(other.fInitializedTensors);
fIntermediateTensorInfos = std::move(other.fIntermediateTensorInfos);
fName = other.fName;
fFileName = other.fFileName;
fParseTime = other.fParseTime;
fGC = other.fGC;
fNeededBlasRoutines = other.fNeededBlasRoutines;
fNeededStdLib = other.fNeededStdLib;
return *this;
}
const std::vector<size_t>& RModel::GetTensorShape(std::string name) {
auto f = fReadyInputTensorInfos.find(name);
if (f != fReadyInputTensorInfos.end()) {
return f->second.shape;
}
auto f2 = fInitializedTensors.find(name);
if (f2 != fInitializedTensors.end()) {
return f2->second.fShape;
}
auto f3 = fInputTensorInfos.find(name);
if (f3 != fInputTensorInfos.end()) {
throw std::runtime_error("TMVA SOFIE tensor [" + name + "] is an input tensor with unspecified dimension parameter");
}
auto f4 = fIntermediateTensorInfos.find(name);
if (f4 != fIntermediateTensorInfos.end()) {
return f4->second.shape;
}
throw std::runtime_error("TMVA SOFIE tensor [" + name + "] for which the shape is requested is not found");
}
const ETensorType& RModel::GetTensorType(std::string name) {
auto f = fReadyInputTensorInfos.find(name);
if (f != fReadyInputTensorInfos.end()) {
return f->second.type;
}
auto f2 = fInitializedTensors.find(name);
if (f2 != fInitializedTensors.end()) {
return f2->second.fType;
}
auto f3 = fInputTensorInfos.find(name);
if (f3 != fInputTensorInfos.end()) {
return f3->second.type;
}
auto f4 = fIntermediateTensorInfos.find(name);
if (f4 != fIntermediateTensorInfos.end()) {
return f4->second.type;
}
throw std::runtime_error("TMVA SOFIE tensor [" + name + "] for which the type is requested is not found");
}
bool RModel::CheckIfTensorAlreadyExist(std::string tensor_name) {
if (fReadyInputTensorInfos.find(tensor_name) != fReadyInputTensorInfos.end()) return true;
if (fInitializedTensors.find(tensor_name) != fInitializedTensors.end()) return true;
if (fIntermediateTensorInfos.find(tensor_name) != fIntermediateTensorInfos.end()) return true;
return false;
}
void RModel::AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<Dim> shape) {
input_name = UTILITY::Clean_name(input_name);
if (CheckIfTensorAlreadyExist(input_name)) {
throw std::runtime_error("TMVA-SOFIE: input tensor with name " + input_name + " already exists \n");
}
InputTensorInfo inputInfo { type, shape };
fInputTensorInfos[input_name] = inputInfo;
}
void RModel::AddInputTensorInfo(std::string input_name, ETensorType type, std::vector<size_t> shape) {
input_name = UTILITY::Clean_name(input_name);
if (CheckIfTensorAlreadyExist(input_name)) {
throw std::runtime_error("TMVA-SOFIE: input tensor with name " + input_name + " already exists \n");
}
TensorInfo inputInfo { type, shape };
fReadyInputTensorInfos[input_name] = inputInfo;
}
void RModel::AddInputTensorName(std::string input_name) {
fInputTensorNames.push_back(UTILITY::Clean_name(input_name));
}
void RModel::AddOperator(std::unique_ptr<ROperator> op, int order_execution) {
AddBlasRoutines(op->GetBlasRoutines());
auto libs = op->GetStdLibs();
for (auto& stdlib : libs) {
AddNeededStdLib(stdlib);
}
if (order_execution >= 0) {
fOperators.insert(fOperators.begin() + order_execution, std::move(op));
} else {
fOperators.push_back(std::move(op));
}
}
void RModel::AddInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
tensor_name = UTILITY::Clean_name(tensor_name);
//NB: own data
if (CheckIfTensorAlreadyExist(tensor_name)) {
throw std::runtime_error("TMVA-SOFIE: initialized tensor with name " + tensor_name + " already exists \n");
}
InitializedTensor new_tensor {type, shape, data};
fInitializedTensors[tensor_name] = new_tensor;
}
bool RModel::IsInitializedTensor(const std::string& tensorName) const {
std::string name = UTILITY::Clean_name(tensorName);
return fInitializedTensors.find(name) != fInitializedTensors.end();
}
void RModel::AddIntermediateTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape) {
tensor_name = UTILITY::Clean_name(tensor_name);
if (CheckIfTensorAlreadyExist(tensor_name)) {
throw std::runtime_error("TMVA-SOFIE: intermediate tensor with name " + tensor_name + " already exists \n");
}
TensorInfo new_tensor {type, shape};
fIntermediateTensorInfos[tensor_name] = new_tensor;
}
void RModel::AddOutputTensorNameList(std::vector<std::string> outputtensornames) {
fOutputTensorNames.clear();
for(auto& it : outputtensornames) {
fOutputTensorNames.push_back(UTILITY::Clean_name(it));
}
}
void RModel::UpdateOutputTensorList(std::vector<std::string> curr_output_tensors, std::vector<std::string> new_output_tensors) {
for(auto& it:curr_output_tensors) {
fOutputTensorNames.erase(std::remove(fOutputTensorNames.begin(), fOutputTensorNames.end(), it), fOutputTensorNames.end());
}
fOutputTensorNames.insert(fOutputTensorNames.end(), new_output_tensors.begin(), new_output_tensors.end());
}
void RModel::UpdateInitializedTensor(std::string tensor_name, ETensorType type, std::vector<std::size_t> shape, std::shared_ptr<void> data) {
tensor_name = UTILITY::Clean_name(tensor_name);
if (!CheckIfTensorAlreadyExist(tensor_name)) {
throw std::runtime_error("TMVA-SOFIE: tensor " + tensor_name + " not found when trying to update it");
}
InitializedTensor new_tensor {type, shape, data};
fInitializedTensors[tensor_name] = new_tensor;
}
std::shared_ptr<void> RModel::GetInitializedTensorData(std::string tensor_name) {
auto f = fInitializedTensors.find(tensor_name);
if (f == fInitializedTensors.end()) {
throw std::runtime_error("TMVA-SOFIE: tensor " + tensor_name + " not found when trying to get its data");
} else {
return f->second.fData;
}
}
void RModel::Initialize(int batchSize) {
// check if there are only parametrized input tensor and convert in
// ready input tensor according to batch size
// convert parametric shape to a dimensional shape
fIntermediateTensorInfos.clear();
if (fReadyInputTensorInfos.size() != fInputTensorNames.size()) {
if ( fReadyInputTensorInfos.size() + fInputTensorInfos.size() != fInputTensorNames.size())
throw std::runtime_error("TMVA-SOFIE: RModel::Initializes: invalid inputs");
for (auto & input : fInputTensorInfos) {
std::vector<size_t> shape;
shape.reserve(input.second.shape.size());
for (auto & d : input.second.shape) {
if (d.isParam)
shape.push_back(batchSize);
else
shape.push_back(d.dim);
}
AddInputTensorInfo(input.first, input.second.type, shape);
}
}
// check if there are initialized tensors to write in a weight file
// support for the time being only weight of FLOAT type
if (fUseWeightFile) {
bool modelHasWeights = false;
for (auto& i: fInitializedTensors) {
if (i.second.fType == ETensorType::FLOAT) {
modelHasWeights = true;
break;
}
}
if (!modelHasWeights) fUseWeightFile = false;
}
for (auto& i : fOperators) {
i->Initialize(*this);
}
}
void RModel::GenerateInitializedTensorInfo() {
for (auto& i: fInitializedTensors) {
if (i.second.fType == ETensorType::FLOAT) {
size_t length = 1;
for (auto & dim: i.second.fShape) {
length *= dim;
}
if (!fUseWeightFile) {
fGC += "float tensor_" + i.first + "[" + std::to_string(length) + "] = {";
std::shared_ptr<float> data = std::static_pointer_cast<float>(i.second.fData);
std::stringstream floats;
for (size_t idx = 0; idx < length-1; idx++) {
floats << std::setprecision(std::numeric_limits<float>::max_digits10) << data.get()[idx] << ", ";
}
floats << std::setprecision(std::numeric_limits<float>::max_digits10) << data.get()[length-1];
fGC += floats.str();
fGC += "};\n";
}
else {
fGC += "std::vector<float> fTensor_" + i.first + " = std::vector<float>(" + std::to_string(length) + ");\n";
fGC += "float * tensor_" + i.first + " = fTensor_" + i.first + ".data();\n";
}
}
}
}
void RModel::GenerateIntermediateTensorInfo() {
for (auto&i: fIntermediateTensorInfos) {
size_t length = ConvertShapeToLength(i.second.shape);
if (i.second.type == ETensorType::FLOAT) {
fGC += "std::vector<float> fTensor_" + i.first + " = std::vector<float>(" + std::to_string(length) + ");\n";
fGC += "float * tensor_" + i.first + " = fTensor_" + i.first + ".data();\n";
}
if (i.second.type == ETensorType::DOUBLE) {
fGC += "std::vector<double> fTensor_" + i.first + " = std::vector<double>(" + std::to_string(length) + ");\n";
fGC += "double * tensor_" + i.first + " = fTensor_" + i.first + ".data();\n";
}
if (i.second.type == ETensorType::INT64) {
fGC += "std::vector<int64_t> fTensor_" + i.first + " = std::vector<int64_t>(" + std::to_string(length) + ");\n";
fGC += "int64_t * tensor_" + i.first + " = fTensor_" + i.first + ".data();\n";
}
}
}
void RModel::GenerateOutput() {
size_t outputSize = fOutputTensorNames.size();
// assume output types are all the same
std::string outputType;
if (outputSize == 1) {
auto f = fIntermediateTensorInfos.find(fOutputTensorNames[0]);
if (f == fIntermediateTensorInfos.end()) {
throw std::runtime_error("TMVA-SOFIE: output tensor " + fOutputTensorNames[0] + " not found when trying to get its info");
} else {
outputType = ConvertTypeToString(f->second.type);
fGC += "std::vector<" + outputType + "> ";
}
} else {
std::vector<ETensorType> outputTensorsTypes(outputSize);
for (size_t i = 0; i < outputSize; i++) {
auto f = fIntermediateTensorInfos.find(fOutputTensorNames[i]);
if (f == fIntermediateTensorInfos.end()) {
throw std::runtime_error("TMVA-SOFIE: output tensor " + fOutputTensorNames[i]
+ " not found when trying to get its info");
} else {
outputTensorsTypes[i] = f->second.type;
}
}
// assume all output types are the same
outputType = ConvertTypeToString(outputTensorsTypes[0]);
for (size_t i = 0; i < outputSize; i++) {
if (outputTensorsTypes[i] != outputTensorsTypes[0]) {
throw std::runtime_error("TMVA-SOFIE: output tensor " + fOutputTensorNames[i] + " is of different type.");
}
}
fGC += "std::vector<std::vector<" + outputType + ">> ";
}
fGC += "infer(";
for(size_t i = 0; i<fInputTensorNames.size(); ++i) {
switch((fReadyInputTensorInfos[fInputTensorNames[i]]).type) {
case ETensorType::FLOAT : {
fGC += "float* tensor_" + fInputTensorNames[i] + ",";
break;
}
case ETensorType::INT32 : {
fGC += "int32_t* tensor_" + fInputTensorNames[i] + ",";
break;
}
case ETensorType::INT64 : {
fGC += "int64_t* tensor_" + fInputTensorNames[i] + ",";
break;
}
case ETensorType::DOUBLE : {
fGC += "double* tensor_" + fInputTensorNames[i] + ",";
break;
}
default: {
throw std::runtime_error("TMVA-SOFIE: input tensor " + fInputTensorNames[i] + " is of a data type which is not yet supported.");
}
}
}
fGC.pop_back(); //remove last ","
fGC += "){\n";
const std::string SP = " ";
for (size_t id = 0; id < fOperators.size() ; id++) {
fGC+= (fOperators[id]->Generate(std::to_string(id)));
}
if (outputSize == 1) {
size_t outputLength = ConvertShapeToLength(GetTensorShape(fOutputTensorNames[0]));
fGC += SP + "std::vector<" + outputType + "> ret (tensor_" + fOutputTensorNames[0] + ", tensor_" + fOutputTensorNames[0] + " + " +
std::to_string(outputLength) + ");\n";
} else {
for (size_t i = 0; i < outputSize; i++) {
if (!fOutputTensorNames[i].empty()) {
size_t outputLength = ConvertShapeToLength(GetTensorShape(fOutputTensorNames[i]));
fGC += SP + "std::vector<" + outputType + "> ret_";
fGC += std::to_string(i);
fGC += " (tensor_" + fOutputTensorNames[i] + ", tensor_" + fOutputTensorNames[i] + " + " +
std::to_string(outputLength) + ");\n";
}
}
fGC += SP + "std::vector<std::vector<" + outputType + ">> ret({";
for (size_t i = 0; i < outputSize; i++) {
if (fOutputTensorNames[i].empty()) {
fGC += "{}";
} else {
fGC += "ret_";
fGC += std::to_string(i);
}
if (i < outputSize - 1) {
fGC += ",";
}
}
fGC += "});\n";
}
fGC += SP + "return ret;\n";
fGC += "}\n";
}
void RModel::Generate(std::underlying_type_t<Options> options, int batchSize, long pos) {
// session flag is used in operator initialize
if (static_cast<std::underlying_type_t<Options>>(Options::kNoSession) & options) {
fUseSession = false;
fWeightFile = WeightFileType::None;
}
if (static_cast<std::underlying_type_t<Options>>(Options::kNoWeightFile) & options) {
fUseWeightFile = false;
fWeightFile = WeightFileType::None;
}
if (static_cast<std::underlying_type_t<Options>>(Options::kRootBinaryWeightFile) & options) {
fUseWeightFile = true;
fWeightFile = WeightFileType::RootBinary;
}
if (fUseWeightFile && !fUseSession) {
throw
std::runtime_error("TMVA-SOFIE: RModel::Generate: cannot use a separate weight file without generating a Session class");
}
if (static_cast<std::underlying_type_t<Options>>(Options::kGNN) & options)
fIsGNN = true;
if (static_cast<std::underlying_type_t<Options>>(Options::kGNNComponent) & options)
fIsGNNComponent = true;
Initialize(batchSize);
std::string hgname;
if(!fIsGNNComponent) {
fGC.clear();
GenerateHeaderInfo(hgname);
if (fUseSession) {
fGC += "struct Session {\n";
}
}
GenerateInitializedTensorInfo();
GenerateIntermediateTensorInfo();
if (fUseSession) {
// add here specific operator code that needs to define session data members
fGC += "\n";
for (size_t id = 0; id < fOperators.size(); id++) {
std::string opName = std::to_string(id);
fGC += fOperators[id]->GenerateSessionMembersCode(opName);
}
fGC += "\n";
// here add initialization and reading of weight tensors
if (fUseWeightFile) {
fGC += "Session(std::string filename =\"\") {\n";
fGC += " if (filename.empty()) filename = \"" + fName;
if (fWeightFile == WeightFileType::Text) {
fGC += ".dat\";\n";
}
if (fWeightFile == WeightFileType::RootBinary) {
fGC += ".root\";\n";
}
ReadInitializedTensorsFromFile(pos);
//fUseWeightFile = fUseWeightFile;
} else {
// no need to pass weight file since it is not used
// keep passing a string for compatibility
fGC += "Session(std::string = \"\") {\n";
}
// add here initialization code
for (size_t id = 0; id < fOperators.size() ; id++) {
fGC += fOperators[id]->GenerateInitCode();
}
fGC += "}\n\n";
}
GenerateOutput();
if(!fIsGNNComponent) {
if (fUseSession) {
fGC += "};\n";
}
fGC += ("} //TMVA_SOFIE_" + fName + "\n");
fGC += "\n#endif // " + hgname + "\n";
}
}
void RModel::ReadInitializedTensorsFromFile(long pos) {
// generate the code to read initialized tensors from a text data file
if (fWeightFile == WeightFileType::Text) {
if (fInitializedTensors.empty()) return;
fGC += " std::ifstream f;\n";
fGC += " f.open(filename);\n";
fGC += " if (!f.is_open()) {\n";
fGC += " throw std::runtime_error(\"tmva-sofie failed to open file for input weights\");\n";
fGC += " }\n";
if(fIsGNNComponent) {
fGC += " f.seekg(" + std::to_string(pos) + ");\n";
}
fGC += " std::string tensor_name;\n";
fGC += " size_t length;\n";
// loop on tensors and parse the file
for (auto& i: fInitializedTensors) {
if (i.second.fType == ETensorType::FLOAT) {
size_t length = 1;
length = ConvertShapeToLength(i.second.fShape);
std::string tensor_name = "tensor_" + i.first;
std::string slength = std::to_string(length);
fGC += " f >> tensor_name >> length;\n";
fGC += " if (tensor_name != \"" + tensor_name + "\" ) {\n";
fGC += " std::string err_msg = \"TMVA-SOFIE failed to read the correct tensor name; expected name is " +
tensor_name + " , read \" + tensor_name;\n";
fGC += " throw std::runtime_error(err_msg);\n";
fGC += " }\n";
fGC += " if (length != " + slength + ") {\n";
fGC += " std::string err_msg = \"TMVA-SOFIE failed to read the correct tensor size; expected size is " +
slength + " , read \" + std::to_string(length) ;\n";
fGC += " throw std::runtime_error(err_msg);\n";
fGC += " }\n";
fGC += " for (size_t i = 0; i < length; ++i)\n";
fGC += " f >> " + tensor_name + "[i];\n";
}
}
fGC += " f.close();\n";
}
// generate the code to read initialized tensors from a ROOT data file
if(fWeightFile == WeightFileType::RootBinary) {
fGC += " {\n";
fGC += " std::unique_ptr<TFile> rootFile(TFile::Open(filename.c_str(), \"READ\"));\n";
fGC += " if (!rootFile->IsOpen()) {\n";
fGC += " throw std::runtime_error(\"tmva-sofie failed to open ROOT file for input weights\");\n";
fGC += " }\n";
std::string dirName = fName + "_weights";
fGC += " if (!rootFile->GetKey(\"" + dirName + "\")) {\n";
fGC += " throw std::runtime_error(\"tmva-sofie failed to open ROOT directory for input weights\");\n";
fGC += " }\n";
for (auto &i : fInitializedTensors) {
fGC += " {\n";
std::string tensor_name = "tensor_" + i.first;
if (i.second.fType == ETensorType::FLOAT) {
fGC += " fTensor_" + i.first + " = *reinterpret_cast<std::vector<float>*>(rootFile->Get(\"";
fGC += dirName + "/" + tensor_name + "\"));\n";
} else if (i.second.fType == ETensorType::DOUBLE) {
fGC += " fTensor_" + i.first + " = *reinterpret_cast<std::vector<double>*>(rootFile->Get(\"";
fGC += dirName + + "/" + tensor_name + "\"));\n";
} else if (i.second.fType == ETensorType::INT64) {
fGC += " fTensor_" + i.first + " = *reinterpret_cast<std::vector<int64_t>*>(rootFile->Get(\"";
fGC += dirName + "/" + tensor_name + "\"));\n";
}
fGC += " }\n";
}
fGC += " }\n";
}
}
long RModel::WriteInitializedTensorsToFile(std::string filename) {
// Determine the file extension based on the weight file type
std::string fileExtension;
switch (fWeightFile) {
case WeightFileType::None:
fileExtension = ".dat";
break;
case WeightFileType::RootBinary:
fileExtension = ".root";
break;
case WeightFileType::Text:
fileExtension = ".dat";
break;
}
// If filename is empty, use the model name as the base filename
if (filename.empty()) {
filename = fFileName + fileExtension;
}
// Write the initialized tensors to the file
if (fWeightFile == WeightFileType::RootBinary) {
if(fIsGNNComponent || fIsGNN) {
throw std::runtime_error("SOFIE-GNN yet not supports writing to a ROOT file.")
}
std::unique_ptr<TFile> outputFile(TFile::Open(filename.c_str(), "UPDATE"));
std::string dirName = fName + "_weights";
// check if directory exists, in case delete to replace with new one
if (outputFile->GetKey(dirName.c_str()))
outputFile->rmdir(dirName.c_str());
auto outputDir = outputFile->mkdir(dirName.c_str());
for (const auto& item : fInitializedTensors) {
std::string tensorName = "tensor_" + item.first;
size_t length = 1;
length = ConvertShapeToLength(item.second.fShape);
if(item.second.fType == ETensorType::FLOAT) {
const std::shared_ptr<void> ptr = item.second.fData; // shared_ptr<void> instance
const float* data = (std::static_pointer_cast<float>(item.second.fData)).get();
std::vector<float> tensorDataVector(data, data + length);
outputDir->WriteObjectAny(&tensorDataVector, "std::vector<float>", tensorName.c_str());
}
else if(item.second.fType == ETensorType::DOUBLE) {
const std::shared_ptr<void> ptr = item.second.fData; // shared_ptr<void> instance
const double* data = (std::static_pointer_cast<double>(item.second.fData)).get();
std::vector<double> tensorDataVector(data, data + length);
outputDir->WriteObjectAny(&tensorDataVector, "std::vector<double>", tensorName.c_str());
}
else if(item.second.fType == ETensorType::INT64) {
const std::shared_ptr<void> ptr = item.second.fData; // shared_ptr<void> instance
const int64_t* data = (std::static_pointer_cast<int64_t>(item.second.fData)).get();
std::vector<int64_t> tensorDataVector(data, data + length);
outputDir->WriteObjectAny(&tensorDataVector, "std::vector<int64_t>", tensorName.c_str());
}
}
outputFile->Write(filename.c_str());
// this needs to be changed, similar to the text file
return 0;
}
// Write the initialized tensors to a text file
if (fWeightFile == WeightFileType::Text) {
std::ofstream f;
if(fIsGNNComponent) {
// appending all GNN components into the same file
f.open(filename, std::ios::app);
} else {
f.open(filename);
}
if (!f.is_open())
throw
std::runtime_error("tmva-sofie failed to open file for tensor weight data");
for (auto& i: fInitializedTensors) {
if (i.second.fType == ETensorType::FLOAT) {
size_t length = 1;
for (auto &dim : i.second.fShape) {
length *= dim;
}
std::string tensor_name = "tensor_" + i.first;
f << tensor_name << " " << length << "\n";
const float * data = (std::static_pointer_cast<float>(i.second.fData)).get();
for (size_t idx = 0; idx < length - 1; idx++) {
f << std::setprecision(std::numeric_limits<float>::max_digits10) << data[idx] << " ";
}
f << std::setprecision(std::numeric_limits<float>::max_digits10) << data[length - 1];
f << "\n";
}
}
long curr_pos = f.tellp();
f.close();
return curr_pos;
}
}
void RModel::PrintRequiredInputTensors() {
std::cout << "Model requires following inputs:\n";
for (auto& inputInfo: fInputTensorInfos) {
std::cout << "Parameterised Tensor name: " << inputInfo.first << "\t";
std::cout << "type: " << ConvertTypeToString(inputInfo.second.type) << "\t";
std::cout << "shape: [";
for (size_t i = 0; i < inputInfo.second.shape.size(); i++) {
if (inputInfo.second.shape[i].isParam) {
std::cout << inputInfo.second.shape[i].param;
} else {
std::cout << inputInfo.second.shape[i].dim ;
}
if (i < inputInfo.second.shape.size() - 1) std::cout << ",";
}
std::cout << "]" << std::endl;
}
for (auto& inputInfo: fReadyInputTensorInfos) {
std::cout << "Fully Specified Tensor name: " << inputInfo.first << "\t";
std::cout << "type: " << ConvertTypeToString(inputInfo.second.type) << "\t";
std::cout << "shape: [";
for (size_t i = 0; i < inputInfo.second.shape.size(); i++) {
std::cout << inputInfo.second.shape[i];
if (i < inputInfo.second.shape.size() - 1) std::cout << ",";
}
std::cout << "]" << std::endl;
}
}
void RModel::PrintInitializedTensors() {
std::cout << "Model initialized the following tensors:\n";
for (auto& it: fInitializedTensors) {
std::cout << "Tensor name: \"" << it.first << "\"\t";
std::cout << "type: " << ConvertTypeToString(it.second.fType) << "\t";
std::cout << "shape: [";
for (size_t i = 0; i < it.second.fShape.size(); i++) {
std::cout << it.second.fShape[i];
if (i < it.second.fShape.size() - 1) std::cout << ",";
}
std::cout << "]" << std::endl;
}
}
void RModel::PrintIntermediateTensors() {
std::cout << "Model specify the following intermediate tensors:\n";
for (auto& it: fIntermediateTensorInfos) {
std::cout << "Tensor name: \"" << it.first << "\"\t";
std::cout << "type: " << ConvertTypeToString(it.second.type) << "\t";
std::cout << "shape: [";
for (size_t i = 0; i < it.second.shape.size(); i++) {
std::cout << it.second.shape[i];
if (i < it.second.shape.size() - 1) std::cout << ",";
}
std::cout << "]" << std::endl;
}
}
void RModel::HeadInitializedTensors(std::string name, int n_print) {
auto it = fInitializedTensors.find(name);
if (it == fInitializedTensors.end()) {
std::cout << "Tensor " << name << " not found in model's intialized tensor list" << std::endl;
return;
}
std::cout << "Tensor name: " << it->first << "\t";
std::cout << "type: " << ConvertTypeToString(it->second.fType) << "\t";
int length =1;
std::cout << "shape: [";
for (size_t i = 0; i < it->second.fShape.size(); i++) {
std::cout << it->second.fShape[i];
length *= it->second.fShape[i];
if (i < it->second.fShape.size() - 1) std::cout << ",";
}
std::cout << "]" << std::endl;
bool ellipsis = true;
if (n_print > length) {
n_print = length;
ellipsis = false;
}
std::cout << "data: [" << std::endl;
//switch(it->second.type){
// case ETensorType::FLOAT : {
if (it->second.fType == ETensorType::FLOAT) {
auto converted_data = std::static_pointer_cast<float>(it->second.fData).get();
for (int i =0; i < n_print; i++) {
std::cout << converted_data[i];
if (i < n_print - 1) std::cout << " ,";
}
// break;
// }
}
if (ellipsis) std::cout << ", ...";
std::cout << "]" << std::endl;
}
void RModel::OutputGenerated(std::string filename, bool append) {
RModel_Base::OutputGenerated(filename, append);
// write weights in a text file
if (fUseWeightFile) {
if (!filename.empty()) {
size_t pos = filename.find(".hxx");
if (fWeightFile == WeightFileType::Text)
filename.replace(pos, 4, ".dat");
if (fWeightFile == WeightFileType::RootBinary) {
filename = filename.erase(pos, 4);
filename += ".root";
}
} else {
filename = fName;
filename += fWeightFile == WeightFileType::Text ? ".dat" : ".root";
}
WriteInitializedTensorsToFile(filename);
}
}
void RModel::Streamer(TBuffer &R__b) {
if (R__b.IsReading()) {
RModel::Class()->ReadBuffer(R__b, this);
for(auto i=RModel::fInitializedTensors.begin(); i!=RModel::fInitializedTensors.end(); ++i) {
i->second.CastPersistentToShared();
}
}
else {
for(auto i=RModel::fInitializedTensors.begin(); i!=RModel::fInitializedTensors.end(); ++i) {
i->second.CastSharedToPersistent();
}
RModel::Class()->WriteBuffer(R__b, this);
}
}
}//SOFIE
}//Experimental
}//TMVA