3#include "onnx_proto3.pb.h"
10#include <unordered_map>
15namespace Experimental {
112 tensor->mutable_float_data()->ExtractSubrange(0,
tensor->float_data_size(),
113 static_cast<float *
>(
data));
119 tensor->mutable_double_data()->ExtractSubrange(0,
tensor->double_data_size(),
120 static_cast<double *
>(
data));
126 tensor->mutable_int32_data()->ExtractSubrange(0,
tensor->int32_data_size(),
127 static_cast<int32_t *
>(
data));
133 tensor->mutable_int64_data()->ExtractSubrange(0,
tensor->int64_data_size(),
134 static_cast<int64_t *
>(
data));
145 for (std::size_t k = 0; k <
length; ++k)
256 std::vector<std::string>
ops;
259 ops.emplace_back(it.first);
282std::unique_ptr<ROperator>
285 if (i >= nodes.size())
286 throw std::runtime_error(
"TMVA::SOFIE - Error in parsing ordered operators " + std::to_string(i) +
" is >= " + std::to_string(nodes.size()));
291 std::cout <<
"Parsing operator " <<
op_type << std::endl;
297 if (children.size() == 1) {
298 int idx2 = children.front();
319 }
else if (
nodeproto.op_type() ==
"Gemm") {
325 }
else if (
nodeproto.op_type() ==
"BatchNormalization") {
337 std::cout <<
"operator " <<
op_type <<
" is not supported" << std::endl;
338 throw std::runtime_error(
"TMVA::SOFIE Operator type " +
op_type +
" is not yet supported");
341 std::cout <<
"\tCreating operator " <<
op_type << std::endl;
355 throw std::runtime_error(
"TMVA::SOFIE - Failed to load onnx file " +
filename);
357 const onnx::GraphProto &graph = model->graph();
360 std::time_t
ttime = std::time(0);
371 if (
isep != std::string::npos) {
383 auto model = std::make_unique<onnx::ModelProto>();
385 std::fstream
input(
filename, std::ios::in | std::ios::binary);
386 if (!model->ParseFromIstream(&
input)) {
387 std::cerr <<
"TMVA::SOFIE - Failed to open onnx file " <<
filename << std::endl;
388 return std::unique_ptr<onnx::ModelProto>();
393 std::cout <<
"ONNX Version " << model->ir_version() << std::endl;
395 google::protobuf::ShutdownProtobufLibrary();
402 std::cout <<
"\n" << graph.name() <<
" Graph operator list\n";
403 for (
int i = 0; i < graph.node_size(); i++) {
404 const auto & node = graph.node(i);
405 const std::string
opType = node.op_type();
407 std::cout <<
"\tOperator " << i <<
" : " <<
opType <<
" (" << node.name() <<
"), " << graph.node(i).input_size()
410 std::cout << graph.node(i).input(
j);
411 if (
j < graph.node(i).input_size() - 1)
414 std::cout <<
" }" << std::endl;
420 for (
int j = 0;
j < node.attribute_size();
j++) {
435 if (!model)
return false;
437 const onnx::GraphProto &graph = model->graph();
440 std::cout <<
"\nModel operator list " << model->producer_name() <<
"\n";
447 std::cout <<
"List of missing operators for model loaded from file " <<
filename << std::endl;
449 std::cout <<
op.first <<
" " <<
op.second << std::endl;
453 std::cout <<
"All operators in the loaded model are supported!\n";
465 std::cout <<
"\nParsing Graph - " <<
graphName << std::endl;
468 for (
int i = 0; i < graph.initializer_size(); i++) {
473 std::cout <<
"Parsing model inputs...." << std::endl;
475 for (
int i = 0; i < graph.input_size(); i++) {
477 static_cast<ETensorType>(graph.input(i).type().tensor_type().elem_type()));
480 std::cout <<
"\tgraph input " << i <<
" name " << graph.input(i).name() <<
" type "
481 << graph.input(i).type().tensor_type().elem_type() << std::endl;
495 throw std::runtime_error(
"TMVA::SOFIE data node with no shape restrictions is not supported yet");
496 for (
int j = 0;
j <
valueinfoproto.type().tensor_type().shape().dim_size();
j++) {
499 onnx::TensorShapeProto_Dimension::ValueCase::kDimValue) {
508 }
else if (
valueinfoproto.type().tensor_type().shape().dim(
j).value_case() ==
509 onnx::TensorShapeProto_Dimension::ValueCase::kDimParam) {
514 throw std::runtime_error(
"TMVA::SOFIE ONNX file error: Valueinfoproto " +
input_name +
515 " has neither dim_value nor dim_param! \n");
519 if (
valueinfoproto.type().tensor_type().shape().dim_size() == 0) {
541 std::cout <<
"\nParsing graph initializer list and fill model initialized tensors" << std::endl;
543 for (
int i = 0; i < graph.initializer_size(); i++) {
544 onnx::TensorProto *
tensorproto =
const_cast<onnx::TensorProto *
>(&graph.initializer(i));
545 std::vector<std::size_t> shape;
546 std::size_t fLength = 1;
553 std::string
input_name = graph.initializer(i).name();
556 std::cout <<
"\t initializer " << i <<
" name " <<
input_name <<
" type " << graph.initializer(i).data_type()
593 throw std::runtime_error(
"Data type in weight tensor " + graph.initializer(i).name() +
" not supported!\n");
599 std::cout <<
"\nGraph operator list (ONNX order)\n";
600 for (
int i = 0; i < graph.node_size(); i++) {
601 std::cout <<
"\tOperator " << i <<
" : " << graph.node(i).op_type() <<
" , " << graph.node(i).input_size()
604 std::cout << graph.node(i).input(
j);
605 if (
j < graph.node(i).input_size() - 1)
608 std::cout <<
" }" << std::endl;
614 std::cout <<
"\n***********************\nRe-Order graph operator list\n*************************\n";
617 std::vector<bool>
foundNodes(graph.node_size());
621 for (
int i = 0; i < graph.input_size(); i++) {
626 for (
int i = 0; i < graph.node_size(); i++) {
634 std::cout <<
"Checking input of Node " << i <<
" : " << graph.node(i).name() << std::endl;
636 std::string
name = graph.node(i).input(
j);
642 std::cout <<
"\t\t input " <<
name <<
" "
651 std::cout <<
"skip node " << graph.node(i).op_type() <<
" " << graph.node(i).name() <<
" inputs are not existing ";
653 std::cout << graph.node(i).input(
j) <<
" ";
655 std::cout << std::endl;
662 std::cout <<
"===> New node " << graph.node(i).op_type() <<
" " << graph.node(i).name() <<
" order " << i << std::endl;
668 if (
fVerbose) std::cout <<
"\toutput : " << graph.node(i).output(
j) << std::endl;
675 std::cout <<
"cannot find a new node after " << graph.node(
ilast).op_type() <<
" " << graph.node(
ilast).name() << std::endl;
676 throw std::runtime_error(
"TMVA::SOFIE - cannot find a new node ");
678 }
while ((
int)
nodesOrder.size() < graph.node_size());
682 std::vector<std::vector<int>>
nodesChildren(graph.node_size());
684 for (
int k = 0; k < graph.node_size(); k++) {
687 if (graph.node(i).output_size() > 0)
nodesChildren[i].reserve(graph.node(i).output_size());
688 for (
const auto&
output_name : graph.node(i).output()) {
690 for (
int l = k;
l < graph.node_size();
l++) {
692 for (
const auto&
input_name : graph.node(
j).input()) {
702 std::cout <<
"\nGraph operator list (re-ordered)\n";
703 for (
int k = 0; k < graph.node_size(); k++) {
705 std::cout <<
"\tOperator " << i <<
" : " << graph.node(i).op_type() <<
" , " << graph.node(i).name() <<
" input tensors : {";
706 for (
int j = 0;
j < graph.node(i).input_size();
j++) {
707 std::cout << graph.node(i).input(
j);
708 if (
j < graph.node(i).input_size() - 1)
712 std::cout <<
" children : {";
714 std::cout <<
" [ " <<
ichild <<
" " << graph.node(
ichild).op_type() <<
" , " << graph.node(
ichild).name() <<
"]";
716 std::cout <<
"}" << std::endl;
722 std::cout <<
"Fill RModel with operators...\n";
729 for (
int i = 0; i < graph.node_size(); i++) {
733 std::cout <<
"\t" << i <<
" " <<
nodesOrder[i] <<
" parsing operator " <<
op_type << std::endl;
739 std::cout <<
"\t\tskipping operator since it is fused with previous one" << std::endl;
749 std::cout <<
"\nParsing Graph output list\n";
750 for (
int i = 0; i < graph.output_size(); i++) {
752 std::cout <<
"\toutput " << i <<
" name " << graph.output(i).name() << std::endl;
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
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="")
RModelParser_ONNX() noexcept
std::unordered_map< std::string, ETensorType > fTensorTypeMap
RModel Parse(std::string filename, bool verbose=false)
bool IsRegisteredTensorType(const std::string &)
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::vector< bool > fFusedOperators
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
ParserFuncSignature ParseCos
ParserFuncSignature ParseLog
ParserFuncSignature ParseLeakyRelu
ParserFuncSignature ParseExp
std::function< std::unique_ptr< ROperator >(RModelParser_ONNX &, const onnx::NodeProto &)> ParserFuncSignature
ParserFuncSignature ParseEinsum
ParserFuncSignature ParsePool
ParserFuncSignature ParseDiv
ParserFuncSignature ParseLayerNormalization
ParserFuncSignature ParseConcat
ParserFuncSignature ParseTopK
ParserFuncSignature ParseMax
ParserFuncSignature ParseEq
ParserFuncSignature ParseIdentity
ParserFuncSignature ParseConvTranspose
ParserFuncSignature ParseReduceProd
ParserFuncSignature ParseSlice
ParserFuncSignature ParseRandom
ParserFuncSignature ParseTranspose
ParserFuncSignature ParseLess
ParserFuncSignature ParseShape
ParserFuncSignature ParseGRU
ParserFuncSignature ParseMatMul
ParserFuncSignature ParseErf
ParserFuncSignature ParseSub
ParserFuncSignature ParseAdd
std::shared_ptr< void > GetInitializedTensorData(onnx::TensorProto *tensorproto, size_t length)
ParserFuncSignature ParseIf
ParserFuncSignature ParseRange
ParserFuncSignature ParseSoftplus
ParserFuncSignature ParseExpand
ParserFuncSignature ParseRNN
ParserFuncSignature ParseLSTM
ParserFuncSignature ParseCast
ParserFuncSignature ParseReciprocal
ParserFuncSignature ParseSigmoid
ParserFuseFuncSignature ParseFuseConvAdd
ParserFuseFuncSignature ParseFuseBatchnormRelu
ParserFuncSignature ParseSoftmax
ParserFuncSignature ParseGreaterEq
ParserFuncSignature ParseMean
ParserFuncSignature ParseSplit
ParserFuncSignature ParseConstant
ParserFuncSignature ParseSelu
ParserFuncSignature ParseLessEq
ParserFuncSignature ParseSum
ParserFuncSignature ParseEyeLike
ParserFuncSignature ParsePad
ParserFuncSignature ParseElu
std::string ConvertShapeToString(const std::vector< size_t > &shape)
ParserFuncSignature ParseMin
ParserFuncSignature ParseRelu
ParserFuncSignature ParseReduceSum
ParserFuncSignature ParseConv
ParserFuncSignature ParseScatterElements
ParserFuncSignature ParseGemm
ParserFuncSignature ParseTile
ParserFuncSignature ParseMul
ParserFuseFuncSignature ParseFuseGemmRelu
ParserFuncSignature ParsePow
ParserFuncSignature ParseAbs
ParserFuncSignature ParseSin
ParserFuncSignature ParseReduceSumSquare
ParserFuncSignature ParseTanh
create variable transformations
Helper templated class for swapping bytes; specializations for N={2,4,8} are provided below.
std::unordered_map< std::string, ParserFuncSignature > fOperatorsMap