Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
RModelParser_ONNX.cxx
Go to the documentation of this file.
1#include "Byteswap.h"
3#include "onnx_proto3.pb.h"
4
5#include <stdexcept>
6#include <string>
7#include <memory>
8#include <cassert>
9#include <iostream>
10#include <unordered_map>
11#include <functional>
12#include "TMVA/SOFIE_common.hxx"
13
14namespace TMVA {
15namespace Experimental {
16namespace SOFIE {
17
18// Declaration of operators
19// Unary operators
29// Binary operators
36// Nary operators
41//Comparision Operators
47// Reduce operators
52// Others
94// Declaration of fused operators
100
101// Definition of RModelParser_ONNX::OperatorsMap
103 // Registered operators
104 std::unordered_map<std::string, ParserFuncSignature> fOperatorsMap;
105};
106
107// helper function to get initialized tensor data
108template<typename T>
110};
111// trait function to extract data from TensorProto
112template<>
113struct ExtractDataFromTP<float> {
114 static void Copy(onnx::TensorProto * tensor, void * data) {
115 tensor->mutable_float_data()->ExtractSubrange(0, tensor->float_data_size(),
116 static_cast<float *>(data));
117 }
118};
119template<>
121 static void Copy(onnx::TensorProto * tensor, void * data) {
122 tensor->mutable_double_data()->ExtractSubrange(0, tensor->double_data_size(),
123 static_cast<double *>(data));
124 }
125};
126template<>
127struct ExtractDataFromTP<int32_t> {
128 static void Copy(onnx::TensorProto * tensor, void * data) {
129 tensor->mutable_int32_data()->ExtractSubrange(0, tensor->int32_data_size(),
130 static_cast<int32_t *>(data));
131 }
132};
133template<>
134struct ExtractDataFromTP<int64_t> {
135 static void Copy(onnx::TensorProto * tensor, void * data) {
136 tensor->mutable_int64_data()->ExtractSubrange(0, tensor->int64_data_size(),
137 static_cast<int64_t *>(data));
138 }
139};
140template<typename T>
141std::shared_ptr<void> GetInitializedTensorData(onnx::TensorProto * tensorproto, size_t length) {
142 std::shared_ptr<void> data(malloc(length * sizeof(T)), free);
143
144 if (!tensorproto->raw_data().empty()) {
145#ifdef R__BYTESWAP
146 std::memcpy(data.get(), tensorproto->raw_data().c_str(), length * sizeof(T));
147#else
148 for (std::size_t k = 0; k < length; ++k)
149 (reinterpret_cast<typename RByteSwap<sizeof(T)>::value_type *>(data.get()))[k] =
150 RByteSwap<sizeof(T)>::bswap((reinterpret_cast<const typename RByteSwap<sizeof(T)>::value_type *>(tensorproto->raw_data().c_str()))[k]);
151#endif
152 } else {
154 }
155 return data;
156}
157
158// Constructor of the parser
159RModelParser_ONNX::RModelParser_ONNX() noexcept : fOperatorsMapImpl(std::make_unique<OperatorsMapImpl>()) {
160 // Register operators
161 // Unary operators
163 RegisterOperator("Reciprocal", ParseReciprocal);
170 RegisterOperator("Softplus", ParseSoftplus);
171 // Binary operators
178 // Nary operators
183 //Comparision Operators
184 RegisterOperator("Equal", ParseEq);
186 RegisterOperator("LessOrEqual", ParseLessEq);
187 RegisterOperator("Greater", ParseGreater);
188 RegisterOperator("GreaterOrEqual", ParseGreaterEq);
189 // Reduce operators
190 RegisterOperator("ReduceMean", ParseReduceMean);
191 RegisterOperator("ReduceSum", ParseReduceSum);
192 RegisterOperator("ReduceSumSquare", ParseReduceSumSquare);
193 RegisterOperator("ReduceProd", ParseReduceProd);
194 // Others
195 RegisterOperator("BatchNormalization", ParseBatchNormalization);
196 RegisterOperator("Constant", ParseConstant);
197 RegisterOperator("ConstantOfShape", ParseConstant);
199 RegisterOperator("Concat", ParseConcat);
201 RegisterOperator("ConvTranspose", ParseConvTranspose);
204 RegisterOperator("Identity", ParseIdentity);
205 RegisterOperator("LeakyRelu", ParseLeakyRelu);
207 RegisterOperator("AveragePool", ParsePool);
208 RegisterOperator("GlobalAveragePool", ParsePool);
209 RegisterOperator("MaxPool", ParsePool);
211 RegisterOperator("Reshape", ParseReshape);
212 RegisterOperator("Flatten", ParseReshape);
213 RegisterOperator("Squeeze", ParseReshape);
214 RegisterOperator("Unsqueeze", ParseReshape);
218 RegisterOperator("Sigmoid", ParseSigmoid);
220 RegisterOperator("Softmax", ParseSoftmax);
221 RegisterOperator("LogSoftmax", ParseSoftmax);
223 RegisterOperator("Transpose", ParseTranspose);
224 RegisterOperator("MatMul", ParseMatMul);
225 RegisterOperator("LayerNormalization", ParseLayerNormalization);
226 RegisterOperator("Expand", ParseExpand);
227 RegisterOperator("Gather", ParseGather);
228 RegisterOperator("GatherND", ParseGatherND);
231 RegisterOperator("EyeLike", ParseEyeLike);
239 RegisterOperator("Einsum", ParseEinsum);
240 RegisterOperator("RandomNormal", ParseRandom);
241 RegisterOperator("RandomNormalLike", ParseRandom);
242 RegisterOperator("RandomUniform", ParseRandom);
243 RegisterOperator("RandomUniformLike", ParseRandom);
244 RegisterOperator("ScatterElements", ParseScatterElements);
245 RegisterOperator("NonZero", ParseNonZero);
246}
247
248// Destructor of the parser
250
252{
253 fOperatorsMapImpl->fOperatorsMap[name] = func;
254}
255
257{
258 return fOperatorsMapImpl->fOperatorsMap.find(name) != fOperatorsMapImpl->fOperatorsMap.end();
259}
260
262{
263 std::vector<std::string> ops;
264 ops.reserve(fOperatorsMapImpl->fOperatorsMap.size());
265 for (auto &it : fOperatorsMapImpl->fOperatorsMap) {
266 ops.emplace_back(it.first);
267 }
268 // return sorted list in alphabetical order
269 std::sort(ops.begin(), ops.end());
270 return ops;
271}
272
277
279{
281}
282
287
288// Parse an operator
289std::unique_ptr<ROperator>
290RModelParser_ONNX::ParseOperator(const size_t i, const onnx::GraphProto &graphproto, const std::vector<size_t> &nodes, const std::vector<int> & children)
291{
292 if (i >= nodes.size())
293 throw std::runtime_error("TMVA::SOFIE - Error in parsing ordered operators " + std::to_string(i) + " is >= " + std::to_string(nodes.size()));
294 int idx = nodes[i];
295 const auto &nodeproto = graphproto.node(idx);
296 const std::string op_type = nodeproto.op_type();
297 if (fVerbose)
298 std::cout << "Parsing operator " << op_type << std::endl;
299
300 // skip already fused operators
301 if (fFusedOperators[idx]) return nullptr;
302
303 // try to fuse with following operator in case it is not last one
304 if (children.size() == 1) {
305 int idx2 = children.front();
306 if (op_type == "MatMul") {
307 // Fuse MatMul and Add
308 if (idx2 < graphproto.node_size() && graphproto.node(idx2).op_type() == "Add") {
309 fFusedOperators[idx2] = true;
310 return ParseFuseMatMulAdd(*this, graphproto.node(idx), graphproto.node(idx2));
311 }
312 else {
313 return ParseMatMul(*this, graphproto.node(idx));
314 }
315 } else if (nodeproto.op_type() == "Conv" || nodeproto.op_type() == "ConvTranspose") {
316 // Fuse Conv or ConvTranspose without bias and Add
317 if (idx2 < graphproto.node_size() && graphproto.node(idx2).op_type() == "Add") {
318 if (nodeproto.op_type() == "Conv") {
319 fFusedOperators[idx2] = true;
320 return ParseFuseConvAdd(*this, graphproto.node(idx), graphproto.node(idx2));
321 } else {
322 fFusedOperators[idx2] = true;
323 return ParseFuseConvTransposeAdd(*this, graphproto.node(idx), graphproto.node(idx2));
324 }
325 }
326 } else if (nodeproto.op_type() == "Gemm") {
327 // Fuse Gemm with activation operators
328 if (idx2 < graphproto.node_size() && graphproto.node(idx2).op_type() == "Relu") {
329 fFusedOperators[idx2] = true;
330 return ParseFuseGemmRelu(*this, graphproto.node(idx), graphproto.node(idx2));
331 }
332 } else if (nodeproto.op_type() == "BatchNormalization") {
333 if (idx2 < graphproto.node_size() && graphproto.node(idx2).op_type() == "Relu") {
334 fFusedOperators[idx2] = true;
335 return ParseFuseBatchnormRelu(*this, graphproto.node(idx), graphproto.node(idx2));
336 }
337 }
338 }
339
340
341
342 auto it = fOperatorsMapImpl->fOperatorsMap.find(op_type);
343 if (it == fOperatorsMapImpl->fOperatorsMap.end()) {
344 std::cout << "operator " << op_type << " is not supported" << std::endl;
345 throw std::runtime_error("TMVA::SOFIE Operator type " + op_type + " is not yet supported");
346 }
347 if (fVerbose) {
348 std::cout << "\tCreating operator " << op_type << std::endl;
349 }
350 return it->second(*this, nodeproto);
351}
352
353// Parse a model
354RModel RModelParser_ONNX::Parse(std::string filename, bool verbose)
355{
356 fVerbose = verbose;
357
358 fTensorTypeMap.clear();
359
360 auto model = LoadModel(filename);
361 if (!model)
362 throw std::runtime_error("TMVA::SOFIE - Failed to load onnx file " + filename);
363
364 const onnx::GraphProto &graph = model->graph(); // not a memory leak. model freed automatically at the end.
365
366
367 std::time_t ttime = std::time(0);
368 std::tm *gmt_time = std::gmtime(&ttime);
369 std::string parsetime(std::asctime(gmt_time));
370
371 // get name of model (filename without directory name)
372 char sep = '/';
373#ifdef _WIN32
374 sep = '\\';
375#endif
376 size_t isep = filename.rfind(sep, filename.length());
377 std::string filename_nodir = filename;
378 if (isep != std::string::npos) {
379 filename_nodir = (filename.substr(isep + 1, filename.length() - isep));
380 }
381
384 return rmodel;
385}
386
387std::unique_ptr<onnx::ModelProto> RModelParser_ONNX::LoadModel(std::string filename) {
388
390 auto model = std::make_unique<onnx::ModelProto>();
391
392 std::fstream input(filename, std::ios::in | std::ios::binary);
393 if (!model->ParseFromIstream(&input)) {
394 std::cerr << "TMVA::SOFIE - Failed to open onnx file " << filename << std::endl;
395 return std::unique_ptr<onnx::ModelProto>();
396 }
397
398 // ONNX version is ir_version() - model_version() returns 0
399 if (fVerbose) {
400 std::cout << "ONNX Version " << model->ir_version() << std::endl;
401 }
402 google::protobuf::ShutdownProtobufLibrary();
403 return model;
404
405}
406
407void RModelParser_ONNX::CheckGraph(const onnx::GraphProto & graph, int & level, std::map<std::string, int> & missingOperators) {
408 if (fVerbose)
409 std::cout << "\n" << graph.name() << " Graph operator list\n";
410 for (int i = 0; i < graph.node_size(); i++) {
411 const auto & node = graph.node(i);
412 const std::string opType = node.op_type();
413 if (fVerbose) {
414 std::cout << "\tOperator " << i << " : " << opType << " (" << node.name() << "), " << graph.node(i).input_size()
415 << " inputs : {";
416 for (int j = 0; j < graph.node(i).input_size(); j++) {
417 std::cout << graph.node(i).input(j);
418 if (j < graph.node(i).input_size() - 1)
419 std::cout << ", ";
420 }
421 std::cout << " }" << std::endl;
422 }
423 // check if operator exists
425 missingOperators[opType] = level;
426 // see if sub-graph exists as node attributes
427 for (int j = 0; j < node.attribute_size(); j++) {
428 const auto & attribute = node.attribute(j);
429 if (attribute.has_g()) {
430 const auto & subGraph = attribute.g();
431 level += 1;
433 }
434 }
435 }
436}
437
438bool RModelParser_ONNX::CheckModel(std::string filename, bool verbose) {
439
440 fVerbose = verbose;
441 auto model = LoadModel(filename);
442 if (!model) return false;
443
444 const onnx::GraphProto &graph = model->graph();
445 // Initial operator order
446 if (fVerbose)
447 std::cout << "\nModel operator list " << model->producer_name() << "\n";
448
449 std::map<std::string, int> missingOperators;
450 int level = 1;
451 CheckGraph(graph, level, missingOperators);
452
453 if (!missingOperators.empty()) {
454 std::cout << "List of missing operators for model loaded from file " << filename << std::endl;
455 for (auto & op : missingOperators) {
456 std::cout << op.first << " " << op.second << std::endl;
457 }
458 return false;
459 }
460 std::cout << "All operators in the loaded model are supported!\n";
461 return true;
462}
463
464void RModelParser_ONNX::ParseONNXGraph(RModel & rmodel, const onnx::GraphProto & graph, std::string graphName)
465{
466 bool verbose = fVerbose;
467
468 if (graphName.empty())
469 graphName = graph.name();
470
471 if (verbose)
472 std::cout << "\nParsing Graph - " << graphName << std::endl;
473
474 std::unordered_set<std::string> initializer_names;
475 for (int i = 0; i < graph.initializer_size(); i++) {
476 initializer_names.insert(graph.initializer(i).name());
477 }
478
479 if (verbose)
480 std::cout << "Parsing model inputs...." << std::endl;
481 /// Loop on model inputs
482 for (int i = 0; i < graph.input_size(); i++) {
483 RegisterTensorType(graph.input(i).name(),
484 static_cast<ETensorType>(graph.input(i).type().tensor_type().elem_type()));
485
486 if (verbose)
487 std::cout << "\tgraph input " << i << " name " << graph.input(i).name() << " type "
488 << graph.input(i).type().tensor_type().elem_type() << std::endl;
489
490 if (initializer_names.find(graph.input(i).name()) != initializer_names.end())
491 continue;
492
493 // input data node is not a weight node (has no initializer)
494 const onnx::ValueInfoProto &valueinfoproto = graph.input(i);
495 std::string input_name = valueinfoproto.name();
496
497 ETensorType type = static_cast<ETensorType>(valueinfoproto.type().tensor_type().elem_type());
498
499 std::vector<Dim> fShape;
500 bool existParam = false;
501 if (!valueinfoproto.type().tensor_type().has_shape())
502 throw std::runtime_error("TMVA::SOFIE data node with no shape restrictions is not supported yet");
503 for (int j = 0; j < valueinfoproto.type().tensor_type().shape().dim_size(); j++) {
504 Dim dim;
505 if (valueinfoproto.type().tensor_type().shape().dim(j).value_case() ==
506 onnx::TensorShapeProto_Dimension::ValueCase::kDimValue) {
507 int dim_value = valueinfoproto.type().tensor_type().shape().dim(j).dim_value();
508 dim.dim = dim_value;
509 // case input dim is -1 - set a parametric shape
510 if (dim_value < 0) {
511 dim.isParam = true;
512 existParam = true;
513 dim.param = UTILITY::Clean_name(input_name) + "_size";
514 }
515 } else if (valueinfoproto.type().tensor_type().shape().dim(j).value_case() ==
516 onnx::TensorShapeProto_Dimension::ValueCase::kDimParam) {
517 dim.isParam = true;
518 existParam = true;
519 dim.param = valueinfoproto.type().tensor_type().shape().dim(j).dim_param();
520 } else {
521 throw std::runtime_error("TMVA::SOFIE ONNX file error: Valueinfoproto " + input_name +
522 " has neither dim_value nor dim_param! \n");
523 }
524 fShape.push_back(dim);
525 }
526 if (valueinfoproto.type().tensor_type().shape().dim_size() == 0) {
527 Dim dim;
528 dim.dim = 1;
529 fShape.push_back(dim);
530 } // in case this TensorShapeProto has no dimension message: ONNX IR defines this to be a scalar
531
532 if (!existParam) {
533 std::vector<size_t> fShape_sizet;
534 for (auto &j : fShape) {
535 fShape_sizet.push_back(j.dim);
536 }
537
538 rmodel.AddInputTensorInfo(input_name, type, fShape_sizet);
539 } else {
540 rmodel.AddInputTensorInfo(input_name, type, fShape);
541 }
542 rmodel.AddInputTensorName(input_name); // store also names in given order
543 }
544
545 std::map<std::string, int> allInitializedTensors;
546
547 if (verbose)
548 std::cout << "\nParsing graph initializer list and fill model initialized tensors" << std::endl;
549
550 for (int i = 0; i < graph.initializer_size(); i++) {
551 onnx::TensorProto *tensorproto = const_cast<onnx::TensorProto *>(&graph.initializer(i));
552 std::vector<std::size_t> shape;
553 std::size_t fLength = 1;
554 for (int j = 0; j < tensorproto->dims_size(); j++) {
555 shape.push_back(tensorproto->dims(j));
556 fLength *= tensorproto->dims(j);
557 }
558 // in case of scalars keep an empty shape but with length =1
559
560 std::string input_name = graph.initializer(i).name();
561
562 if (verbose)
563 std::cout << "\t initializer " << i << " name " << input_name << " type " << graph.initializer(i).data_type()
564 << std::endl;
565
566 // register also the initialized tensors
567 auto tensor_type = static_cast<ETensorType>(graph.initializer(i).data_type());
569
570 switch (tensor_type) {
571 case ETensorType::FLOAT: {
572 std::shared_ptr<void> data = GetInitializedTensorData<float>(tensorproto, fLength);
573 if (verbose) std::cout << "add FLOAT initialized tensor " << input_name << " shape " << ConvertShapeToString(shape) << std::endl;
574 rmodel.AddInitializedTensor(input_name, ETensorType::FLOAT, shape, data);
576 break;
577 }
578 case ETensorType::DOUBLE: {
579 std::shared_ptr<void> data = GetInitializedTensorData<double>(tensorproto, fLength);
580 if (verbose) std::cout << "add DOUBLE initialized tensor " << input_name << " shape " << ConvertShapeToString(shape) << std::endl;
581 rmodel.AddInitializedTensor(input_name, ETensorType::DOUBLE, shape, data);
583 break;
584 }
585 case ETensorType::INT32: {
586 std::shared_ptr<void> data = GetInitializedTensorData<int32_t>(tensorproto, fLength);
587 if (verbose) std::cout << "add INT32 initialized tensor " << input_name << " shape " << ConvertShapeToString(shape) << std::endl;
588 rmodel.AddInitializedTensor(input_name, ETensorType::INT32, shape, data);
590 break;
591 }
592 case ETensorType::INT64: {
593 std::shared_ptr<void> data = GetInitializedTensorData<int64_t>(tensorproto, fLength);
594 if (verbose) std::cout << "add INT64 initialized tensor " << input_name << " shape " << ConvertShapeToString(shape) << std::endl;
595 rmodel.AddInitializedTensor(input_name, ETensorType::INT64, shape, data);
597 break;
598 }
599 default:
600 throw std::runtime_error("Data type in weight tensor " + graph.initializer(i).name() + " not supported!\n");
601 }
602 }
603
604 // Initial operator order
605 if (verbose) {
606 std::cout << "\nGraph operator list (ONNX order)\n";
607 for (int i = 0; i < graph.node_size(); i++) {
608 std::cout << "\tOperator " << i << " : " << graph.node(i).op_type() << " , " << graph.node(i).input_size()
609 << " inputs : {";
610 for (int j = 0; j < graph.node(i).input_size(); j++) {
611 std::cout << graph.node(i).input(j);
612 if (j < graph.node(i).input_size() - 1)
613 std::cout << ", ";
614 }
615 std::cout << " }" << std::endl;
616 }
617 }
618
619 // make order of nodes:
620 if (verbose)
621 std::cout << "\n***********************\nRe-Order graph operator list\n*************************\n";
622 std::vector<size_t> nodesOrder;
623 nodesOrder.reserve(graph.node_size());
624 std::vector<bool> foundNodes(graph.node_size());
625
626 // loop at graph inputs
627 std::map<std::string, int> allInputs;
628 for (int i = 0; i < graph.input_size(); i++) {
629 allInputs[graph.input(i).name()] = -1;
630 }
631 do {
632 auto psize = nodesOrder.size();
633 for (int i = 0; i < graph.node_size(); i++) {
634 if (foundNodes[i])
635 continue;
636 // check if all input exists add to list
637 bool existInputs = true;
638 int input_size = graph.node(i).input_size();
639 // special case for Reshape where shape is input and not a weight tensor
640 if (fVerbose)
641 std::cout << "Checking input of Node " << i << " : " << graph.node(i).name() << std::endl;
642 for (int j = 0; j < input_size; j++) {
643 std::string name = graph.node(i).input(j);
644 // skip empty names
645 if (!name.empty()) {
646 existInputs &= (allInputs.find(name) != allInputs.end() ||
648 if (fVerbose) {
649 std::cout << "\t\t input " << name << " "
650 << bool(allInputs.find(name) != allInputs.end()) << " " <<
652 existInputs << std::endl;
653 }
654 }
655 }
656 if (!existInputs) {
657 if (fVerbose) {
658 std::cout << "skip node " << graph.node(i).op_type() << " " << graph.node(i).name() << " inputs are not existing ";
659 for (int j = 0; j < input_size; j++) {
660 std::cout << graph.node(i).input(j) << " ";
661 }
662 std::cout << std::endl;
663 }
664 continue;
665 }
666
667 // adding node to the currectly ordered list
668 if (verbose)
669 std::cout << "===> New node " << graph.node(i).op_type() << " " << graph.node(i).name() << " order " << i << std::endl;
670
671 nodesOrder.push_back(i);
672 foundNodes[i] = true;
673 // register the outputs
674 for (int j = 0; j < graph.node(i).output_size(); j++) {
675 if (fVerbose) std::cout << "\toutput : " << graph.node(i).output(j) << std::endl;
676 allInputs[graph.node(i).output(j)] = i;
677 }
678 }
679 // no increment in nodes - something wrong
680 if (nodesOrder.size() == psize) {
681 int ilast = nodesOrder.back();
682 std::cout << "cannot find a new node after " << graph.node(ilast).op_type() << " " << graph.node(ilast).name() << std::endl;
683 throw std::runtime_error("TMVA::SOFIE - cannot find a new node ");
684 }
685 } while ((int)nodesOrder.size() < graph.node_size());
686
687
688 // find list of children for each operator (used for fusing oiperators)
689 std::vector<std::vector<int>> nodesChildren(graph.node_size());
690
691 for (int k = 0; k < graph.node_size(); k++) {
692 int i = nodesOrder[k];
693 // compute the number of output for the operators
694 if (graph.node(i).output_size() > 0) nodesChildren[i].reserve(graph.node(i).output_size());
695 for (const auto& output_name : graph.node(i).output()) {
696 // loop on all nodes
697 for (int l = k; l < graph.node_size(); l++) {
698 int j = nodesOrder[l];
699 for (const auto& input_name : graph.node(j).input()) {
700 if (input_name == output_name)
701 nodesChildren[i].push_back(j);
702 }
703 }
704 }
705 }
706
707 // print lit of order operators with list of inputs and list of children nodes
708 if (verbose) {
709 std::cout << "\nGraph operator list (re-ordered)\n";
710 for (int k = 0; k < graph.node_size(); k++) {
711 int i = nodesOrder[k];
712 std::cout << "\tOperator " << i << " : " << graph.node(i).op_type() << " , " << graph.node(i).name() << " input tensors : {";
713 for (int j = 0; j < graph.node(i).input_size(); j++) {
714 std::cout << graph.node(i).input(j);
715 if (j < graph.node(i).input_size() - 1)
716 std::cout << ", ";
717 }
718 std::cout << " } ";
719 std::cout << " children : {";
720 for ( const auto & ichild : nodesChildren[i]) {
721 std::cout << " [ " << ichild << " " << graph.node(ichild).op_type() << " , " << graph.node(ichild).name() << "]";
722 }
723 std::cout << "}" << std::endl;
724 }
725 }
726
727 // fill model with operators
728 if (verbose) {
729 std::cout << "Fill RModel with operators...\n";
730 }
731
732 // we have to record order of node execution separately to
733 // account for fused operators
734 size_t node_order_exec = 0;
735 fFusedOperators = std::vector<bool>(graph.node_size(), false);
736 for (int i = 0; i < graph.node_size(); i++) {
737 std::string op_type = graph.node(nodesOrder[i]).op_type();
738
739 if (verbose) {
740 std::cout << "\t" << i << " " << nodesOrder[i] << " parsing operator " << op_type << std::endl;
741 }
742
743 std::unique_ptr<ROperator> op = ParseOperator(i, graph, nodesOrder, nodesChildren[i]);
744 if (!op) {
745 if (verbose) {
746 std::cout << "\t\tskipping operator since it is fused with previous one" << std::endl;
747 }
748 // for skipping the fused nodes like Add after MatMul
749 continue;
750 }
751 rmodel.AddOperator(std::move(op), node_order_exec++);
752 }
753
754 std::vector<std::string> outputnames;
755 if (verbose)
756 std::cout << "\nParsing Graph output list\n";
757 for (int i = 0; i < graph.output_size(); i++) {
758 if (verbose)
759 std::cout << "\toutput " << i << " name " << graph.output(i).name() << std::endl;
760 outputnames.push_back(graph.output(i).name());
761 }
762 rmodel.AddOutputTensorNameList(outputnames);
763
764 return;
765}
766
767} // namespace SOFIE
768} // namespace Experimental
769} // namespace TMVA
dims_t fShape
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 data
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 filename
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
char name[80]
Definition TGX11.cxx:110
#define malloc
Definition civetweb.c:1575
const_iterator begin() const
const_iterator end() const
void RegisterOperator(const std::string &name, ParserFuncSignature func)
std::unique_ptr< ROperator > ParseOperator(const size_t, const onnx::GraphProto &, const std::vector< size_t > &, const std::vector< int > &)
bool IsRegisteredOperator(const std::string &name)
void CheckGraph(const onnx::GraphProto &g, int &level, std::map< std::string, int > &missingOperators)
void ParseONNXGraph(RModel &model, const onnx::GraphProto &g, std::string name="")
std::unordered_map< std::string, ETensorType > fTensorTypeMap
RModel Parse(std::string filename, bool verbose=false)
void RegisterTensorType(const std::string &, ETensorType)
std::unique_ptr< onnx::ModelProto > LoadModel(std::string filename)
ETensorType GetTensorType(const std::string &name)
std::vector< std::string > GetRegisteredOperators()
std::unique_ptr< OperatorsMapImpl > fOperatorsMapImpl
bool CheckModel(std::string filename, bool verbose=false)
std::string Clean_name(std::string input_tensor_name)
ParserFuncSignature ParseSqrt
ParserFuncSignature ParseBatchNormalization
ParserFuncSignature ParseGreater
std::function< std::unique_ptr< ROperator >(RModelParser_ONNX &, const onnx::NodeProto &, const onnx::NodeProto &)> ParserFuseFuncSignature
ParserFuncSignature ParseReshape
ParserFuseFuncSignature ParseFuseConvTransposeAdd
ParserFuncSignature ParseReduceMean
ParserFuseFuncSignature ParseFuseMatMulAdd
ParserFuncSignature ParseGather
ParserFuncSignature ParseNeg
ParserFuncSignature ParseWhere
Definition ParseWhere.cxx:9
ParserFuncSignature ParseCos
ParserFuncSignature ParseLog
ParserFuncSignature ParseLeakyRelu
ParserFuncSignature ParseExp
std::function< std::unique_ptr< ROperator >(RModelParser_ONNX &, const onnx::NodeProto &)> ParserFuncSignature
ParserFuncSignature ParseEinsum
ParserFuncSignature ParsePool
Definition ParsePool.cxx:9
ParserFuncSignature ParseDiv
ParserFuncSignature ParseLayerNormalization
ParserFuncSignature ParseConcat
ParserFuncSignature ParseTopK
Definition ParseTopK.cxx:9
ParserFuncSignature ParseMax
ParserFuncSignature ParseEq
ParserFuncSignature ParseIdentity
ParserFuncSignature ParseConvTranspose
ParserFuncSignature ParseReduceProd
ParserFuncSignature ParseSlice
Definition ParseSlice.cxx:9
ParserFuncSignature ParseRandom
ParserFuncSignature ParseTranspose
ParserFuncSignature ParseLess
ParserFuncSignature ParseShape
Definition ParseShape.cxx:9
ParserFuncSignature ParseGRU
Definition ParseGRU.cxx:9
ParserFuncSignature ParseMatMul
ParserFuncSignature ParseErf
Definition ParseErf.cxx:9
ParserFuncSignature ParseSub
ParserFuncSignature ParseAdd
ParserFuncSignature ParseNonZero
std::shared_ptr< void > GetInitializedTensorData(onnx::TensorProto *tensorproto, size_t length)
ParserFuncSignature ParseIf
Definition ParseIf.cxx:9
ParserFuncSignature ParseRange
Definition ParseRange.cxx:9
ParserFuncSignature ParseSoftplus
ParserFuncSignature ParseExpand
ParserFuncSignature ParseRNN
Definition ParseRNN.cxx:9
ParserFuncSignature ParseLSTM
Definition ParseLSTM.cxx:9
ParserFuncSignature ParseCast
Definition ParseCast.cxx:9
ParserFuncSignature ParseReciprocal
ParserFuncSignature ParseSigmoid
ParserFuseFuncSignature ParseFuseConvAdd
ParserFuseFuncSignature ParseFuseBatchnormRelu
ParserFuncSignature ParseSoftmax
ParserFuncSignature ParseGreaterEq
ParserFuncSignature ParseMod
ParserFuncSignature ParseMean
ParserFuncSignature ParseSplit
Definition ParseSplit.cxx:9
ParserFuncSignature ParseConstant
ParserFuncSignature ParseSelu
Definition ParseSelu.cxx:9
ParserFuncSignature ParseLessEq
ParserFuncSignature ParseGatherND
ParserFuncSignature ParseSum
ParserFuncSignature ParseEyeLike
ParserFuncSignature ParsePad
Definition ParsePad.cxx:9
ParserFuncSignature ParseElu
Definition ParseElu.cxx:9
std::string ConvertShapeToString(const std::vector< size_t > &shape)
ParserFuncSignature ParseMin
ParserFuncSignature ParseRelu
Definition ParseRelu.cxx:9
ParserFuncSignature ParseReduceSum
ParserFuncSignature ParseConv
Definition ParseConv.cxx:9
ParserFuncSignature ParseScatterElements
ParserFuncSignature ParseGemm
Definition ParseGemm.cxx:9
ParserFuncSignature ParseTile
Definition ParseTile.cxx:9
ParserFuncSignature ParseMul
ParserFuseFuncSignature ParseFuseGemmRelu
ParserFuncSignature ParsePow
ParserFuncSignature ParseAbs
ParserFuncSignature ParseSin
ParserFuncSignature ParseReduceSumSquare
ParserFuncSignature ParseTanh
Definition ParseTanh.cxx:9
create variable transformations
Helper templated class for swapping bytes; specializations for N={2,4,8} are provided below.
Definition Byteswap.h:124
static void Copy(onnx::TensorProto *tensor, void *data)
static void Copy(onnx::TensorProto *tensor, void *data)
static void Copy(onnx::TensorProto *tensor, void *data)
static void Copy(onnx::TensorProto *tensor, void *data)
std::unordered_map< std::string, ParserFuncSignature > fOperatorsMap
TLine l
Definition textangle.C:4