Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ParseReduce.cxx
Go to the documentation of this file.
3#include "onnx_proto3.pb.h"
4#include <stdexcept>
5
6namespace TMVA {
7namespace Experimental {
8namespace SOFIE {
9
10template <EReduceOpMode Op>
11std::unique_ptr<ROperator> ParseReduce(RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto)
12{
13 ETensorType input_type;
14
16
17 if (nodeproto.op_type() == "ReduceMean")
18 op_mode = ReduceMean;
19 else if (nodeproto.op_type() == "ReduceSumSquare")
20 op_mode = ReduceSumSquare;
21 else if (nodeproto.op_type() == "ReduceProd")
22 op_mode = ReduceProd;
23 else if (nodeproto.op_type() == "ReduceSum")
24 op_mode = ReduceSum;
25
26 if (op_mode == InvalidReduceOp) {
27 throw std::runtime_error("TMVA::SOFIE - Reduce op mode not supported.");
28 }
29
30 auto input_name = nodeproto.input(0);
31 if (parser.IsRegisteredTensorType(input_name)) {
32 input_type = parser.GetTensorType(input_name);
33 } else {
34 throw std::runtime_error("TMVA::SOFIE ONNX Parser Reduce op has input tensor" + input_name +
35 " but its type is not yet registered");
36 }
37 //in the latest version of ONNX axis is not an attribute but an input
38 std::string axes_name;
39 if (nodeproto.input_size() > 1) {
40 axes_name = nodeproto.input(1);
41 if (!parser.IsRegisteredTensorType(axes_name)) {
42 throw std::runtime_error("TMVA::SOFIE ONNX Parser Reduce op has input tensor" + axes_name +
43 " but its type is not yet registered");
44 }
45 }
46
47 std::unique_ptr<ROperator> op;
48 std::string output_name = nodeproto.output(0);
49 int attr_keepdims = 1;
50 std::vector<int64_t> attr_axes;
51 for (int_t i = 0; i < nodeproto.attribute_size(); i++) {
52 std::string attribute_name = nodeproto.attribute(i).name();
53 if (attribute_name == "keepdims")
54 attr_keepdims = nodeproto.attribute(i).i();
55 if (attribute_name == "axes") {
56 attr_axes =
57 std::vector<int64_t>({nodeproto.attribute(i).ints().begin(), nodeproto.attribute(i).ints().end()});
58 }
59 }
60 switch (input_type) {
62 op.reset(new ROperator_Reduce<float, Op>(attr_keepdims, attr_axes, input_name, axes_name, output_name));
63 break;
64 default:
65 throw std::runtime_error("TMVA::SOFIE - Unsupported - Reduce Operator does not yet support input type " +
66 std::to_string(static_cast<int>(input_type)));
67 }
68
69 if (!parser.IsRegisteredTensorType(output_name)) {
70 parser.RegisterTensorType(output_name, input_type);
71 }
72 return op;
73}
74
75// Parse ReduceMean
76ParserFuncSignature ParseReduceMean = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
77 return ParseReduce<EReduceOpMode::ReduceMean>(parser, nodeproto);
78};
79
80// Parse ReduceSumSquare
81ParserFuncSignature ParseReduceSumSquare = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
82 return ParseReduce<EReduceOpMode::ReduceSumSquare>(parser, nodeproto);
83};
84
85// Parse ReduceProd
86ParserFuncSignature ParseReduceProd = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
87 return ParseReduce<EReduceOpMode::ReduceProd>(parser, nodeproto);
88};
89
90// Parse ReduceSum
91ParserFuncSignature ParseReduceSum = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
92 return ParseReduce<EReduceOpMode::ReduceSum>(parser, nodeproto);
93};
94
95} // namespace SOFIE
96} // namespace Experimental
97} // namespace TMVA
void RegisterTensorType(const std::string &, ETensorType)
ETensorType GetTensorType(const std::string &name)
std::unique_ptr< ROperator > ParseReduce(RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto)
ParserFuncSignature ParseReduceMean
ParserFuncSignature ParseReduceProd
std::function< std::unique_ptr< ROperator >(RModelParser_ONNX &, const onnx::NodeProto &)> ParserFuncSignature
ParserFuncSignature ParseReduceSum
ParserFuncSignature ParseReduceSumSquare
create variable transformations