Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ParseBasicBinary.cxx
Go to the documentation of this file.
3#include "onnx_proto3.pb.h"
4
5namespace TMVA {
6namespace Experimental {
7namespace SOFIE {
8
9template <EBasicBinaryOperator Op>
10std::unique_ptr<ROperator> ParseBasicBinary(RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto)
11{
13
14 for (int i = 0; i < 2; ++i) {
15 auto input_name = nodeproto.input(i);
16 if (parser.IsRegisteredTensorType(input_name)) {
17 // according to ONNX both inputs have same type
18 if (i == 0)
19 input_type = parser.GetTensorType(input_name);
20 else {
21 ETensorType input_type2 = parser.GetTensorType(input_name);
22 if (input_type2 != input_type) {
23 throw
24 std::runtime_error("TMVA::SOFIE ONNX parser Binary op has input tensors of different types: " +
25 input_name + " : " + ConvertTypeToString(input_type2) +
26 " and " + nodeproto.input(0) + " : " + ConvertTypeToString(input_type));
27 }
28 }
29 } else {
30 throw std::runtime_error("TMVA::SOFIE ONNX Parser Binary op has input tensor " + input_name +
31 " but its type is not yet registered");
32 }
33 }
34
35 std::unique_ptr<ROperator> op;
36 std::string output_name = nodeproto.output(0);
37
38 switch (input_type) {
40 op.reset(new ROperator_BasicBinary<float, Op>(nodeproto.input(0), nodeproto.input(1), output_name));
41 break;
43 op.reset(new ROperator_BasicBinary<int64_t, Op>(nodeproto.input(0), nodeproto.input(1), output_name));
44 break;
45 default:
46 throw std::runtime_error("TMVA::SOFIE - Unsupported - Binary Operator does not yet support input type " +
47 std::to_string(static_cast<int>(input_type)));
48 }
49
50 // Infer the output type
51 if (!parser.IsRegisteredTensorType(output_name)) {
52 parser.RegisterTensorType(output_name, input_type);
53 }
54
55 return op;
56};
57
58// Parse Add
59ParserFuncSignature ParseAdd = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
60 return ParseBasicBinary<EBasicBinaryOperator::Add>(parser, nodeproto);
61};
62
63// Parse Sub
64ParserFuncSignature ParseSub = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
65 return ParseBasicBinary<EBasicBinaryOperator::Sub>(parser, nodeproto);
66};
67
68// Parse Mul
69ParserFuncSignature ParseMul = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
70 return ParseBasicBinary<EBasicBinaryOperator::Mul>(parser, nodeproto);
71};
72
73// Parse Div
74ParserFuncSignature ParseDiv = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
75 return ParseBasicBinary<EBasicBinaryOperator::Div>(parser, nodeproto);
76};
77
78// Parse Pow
79ParserFuncSignature ParsePow = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
80 return ParseBasicBinary<EBasicBinaryOperator::Pow>(parser, nodeproto);
81};
82
83} // namespace SOFIE
84} // namespace Experimental
85} // namespace TMVA
void RegisterTensorType(const std::string &, ETensorType)
ETensorType GetTensorType(const std::string &name)
ParserFuncSignature ParseDiv
ParserFuncSignature ParseSub
std::unique_ptr< ROperator > ParseBasicBinary(RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto)
ParserFuncSignature ParseAdd
std::function< std::unique_ptr< ROperator >(RModelParser_ONNX &, const onnx::NodeProto &)> ParserFuncSignature
std::string ConvertTypeToString(ETensorType type)
ParserFuncSignature ParseMul
ParserFuncSignature ParsePow
create variable transformations