Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ParseReduce.cxx
Go to the documentation of this file.
3#include "onnx.hxx"
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 else if (nodeproto.op_type() == "ReduceMax")
26 op_mode = ReduceMax;
27 else if (nodeproto.op_type() == "ReduceMin")
28 op_mode = ReduceMin;
29
30 if (op_mode == InvalidReduceOp) {
31 throw std::runtime_error("TMVA::SOFIE - Reduce op mode not supported.");
32 }
33
34 auto input_name = nodeproto.input(0);
35 if (parser.IsRegisteredTensorType(input_name)) {
36 input_type = parser.GetTensorType(input_name);
37 } else {
38 throw std::runtime_error("TMVA::SOFIE ONNX Parser Reduce op has input tensor" + input_name +
39 " but its type is not yet registered");
40 }
41 //in the latest version of ONNX axis is not an attribute but an input
42 std::string axes_name;
43 if (nodeproto.input_size() > 1) {
44 axes_name = nodeproto.input(1);
45 if (!parser.IsRegisteredTensorType(axes_name)) {
46 throw std::runtime_error("TMVA::SOFIE ONNX Parser Reduce op has input tensor" + axes_name +
47 " but its type is not yet registered");
48 }
49 }
50
51 std::unique_ptr<ROperator> op;
52 std::string output_name = nodeproto.output(0);
53 int attr_keepdims = 1;
54 std::vector<int64_t> attr_axes;
55 for (int_t i = 0; i < nodeproto.attribute_size(); i++) {
56 std::string attribute_name = nodeproto.attribute(i).name();
57 if (attribute_name == "keepdims")
58 attr_keepdims = nodeproto.attribute(i).i();
59 if (attribute_name == "axes") {
60 attr_axes =
61 std::vector<int64_t>({nodeproto.attribute(i).ints().begin(), nodeproto.attribute(i).ints().end()});
62 }
63 }
64 op.reset(new ROperator_Reduce<Op>(attr_keepdims, attr_axes, input_name, axes_name, output_name));
65 // switch (input_type) {
66 // case ETensorType::FLOAT:
67 // op.reset(new ROperator_Reduce<float, Op>(attr_keepdims, attr_axes, input_name, axes_name, output_name));
68 // break;
69 // default:
70 // throw std::runtime_error("TMVA::SOFIE - Unsupported - Reduce Operator does not yet support input type " +
71 // std::to_string(static_cast<int>(input_type)));
72 // }
73
74 if (!parser.IsRegisteredTensorType(output_name)) {
75 parser.RegisterTensorType(output_name, input_type);
76 }
77 return op;
78}
79
81{
82 parser.RegisterOperator("ReduceMean", ParseReduce<EReduceOpMode::ReduceMean>);
83 parser.RegisterOperator("ReduceSumSquare", ParseReduce<EReduceOpMode::ReduceSumSquare>);
84 parser.RegisterOperator("ReduceProd", ParseReduce<EReduceOpMode::ReduceProd>);
85 parser.RegisterOperator("ReduceSum", ParseReduce<EReduceOpMode::ReduceSum>);
86 parser.RegisterOperator("ReduceMax", ParseReduce<EReduceOpMode::ReduceMax>);
87 parser.RegisterOperator("ReduceMin", ParseReduce<EReduceOpMode::ReduceMin>);
88}
89
90} // namespace SOFIE
91} // namespace Experimental
92} // namespace TMVA
void RegisterOperator(const std::string &name, ParserFuncSignature func)
void RegisterTensorType(const std::string &, ETensorType)
ETensorType GetTensorType(const std::string &name)
const std::vector< int64_t > & ints() const
Definition onnx.hxx:483
const std::string & op_type() const
Definition onnx.hxx:506
const AttributeProto & attribute(int i) const
Definition onnx.hxx:515
const std::string & input(int i) const
Definition onnx.hxx:509
const std::string & output(int i) const
Definition onnx.hxx:512
std::unique_ptr< ROperator > ParseReduce(RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto)
void RegisterReduceParsers(RModelParser_ONNX &parser)
create variable transformations