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
24 if (op_mode == InvalidReduceOp) {
25 throw std::runtime_error("TN+MVA::SOFIE - Reduce op mode not supported.");
26 }
27
28 auto input_name = nodeproto.input(0);
29 if (parser.IsRegisteredTensorType(input_name)) {
30 input_type = parser.GetTensorType(input_name);
31 } else {
32 throw std::runtime_error("TMVA::SOFIE ONNX Parser Reduce op has input tensor" + input_name +
33 " but its type is not yet registered");
34 }
35
36 std::unique_ptr<ROperator> op;
37 std::string output_name = nodeproto.output(0);
38 int attr_keepdims = 1;
39 int attr_axis = 1;
40 for (int_t i = 0; i < nodeproto.attribute_size(); i++) {
41 std::string attribute_name = nodeproto.attribute(i).name();
42 if (attribute_name == "keepdims")
43 attr_keepdims = nodeproto.attribute(i).i();
44 if (attribute_name == "axis")
45 attr_axis = nodeproto.attribute(i).i();
46 }
47 switch (input_type) {
49 op.reset(new ROperator_Reduce<float, Op>(attr_keepdims, attr_axis, input_name, output_name));
50 break;
51 default:
52 throw std::runtime_error("TMVA::SOFIE - Unsupported - Reduce Operator does not yet support input type " +
53 std::to_string(static_cast<int>(input_type)));
54 }
55
56 if (!parser.IsRegisteredTensorType(output_name)) {
57 parser.RegisterTensorType(output_name, input_type);
58 }
59 return op;
60}
61
62// Parse ReduceMean
63ParserFuncSignature ParseReduceMean = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
64 return ParseReduce<EReduceOpMode::ReduceMean>(parser, nodeproto);
65};
66
67// Parse ReduceSumsquare
68ParserFuncSignature ParseReduceSumsquare = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
69 return ParseReduce<EReduceOpMode::ReduceSumsquare>(parser, nodeproto);
70};
71
72// Parse ReduceProd
73ParserFuncSignature ParseReduceProd = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
74 return ParseReduce<EReduceOpMode::ReduceProd>(parser, nodeproto);
75};
76
77} // namespace SOFIE
78} // namespace Experimental
79} // 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
ParserFuncSignature ParseReduceSumsquare
std::function< std::unique_ptr< ROperator >(RModelParser_ONNX &, const onnx::NodeProto &)> ParserFuncSignature
create variable transformations