Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ParseReshape.cxx
Go to the documentation of this file.
3#include "onnx_proto3.pb.h"
4
5namespace TMVA {
6namespace Experimental {
7namespace SOFIE {
8
9ParserFuncSignature ParseReshape = [](RModelParser_ONNX &parser, const onnx::NodeProto &nodeproto) {
10 // make Reshape operator
12
13 ReshapeOpMode opMode = Reshape;
14 if (nodeproto.op_type() == "Flatten")
15 opMode = Flatten;
16 else if (nodeproto.op_type() == "Squeeze")
17 opMode = Squeeze;
18 else if (nodeproto.op_type() == "Unsqueeze")
19 opMode = Unsqueeze;
20
21 // reshape has as extra input shape tensor (int64) but
22 // it is not present for Flatten, Squeeze and Unsquueze
23 auto input_name = nodeproto.input(0);
24 // for squeeze is optional ?
25 auto shape_name = ((nodeproto.input_size() > 1) && ( opMode == Reshape || opMode == Unsqueeze || opMode == Squeeze) )
26 ? nodeproto.input(1) : "";
27 if (parser.IsRegisteredTensorType(input_name)) {
28 input_type = parser.GetTensorType(input_name);
29 } else {
30 throw std::runtime_error("TMVA::SOFIE ONNX Parser Reshape op has input tensor" + input_name +
31 " but its type is not yet registered");
32 }
33
34 // Reshape is having one attribute: allowzero (int) (default = 0)
35 // Flatten is having one attribute: axis (int) (default=1)
36 // old version of reshape and squeeze have axes as attributes
37 std::unique_ptr<ROperator> op;
38 int attr_value = (opMode == Reshape) ? 0 : 1;
39 if (opMode == Reshape && nodeproto.attribute_size() > 0)
40 attr_value = nodeproto.attribute(0).i();
41
42 std::vector<int64_t> attr_axes = {};
43 if (nodeproto.input_size() == 1 && (opMode == Squeeze || opMode == Unsqueeze)) {
44 std::string attribute_name = nodeproto.attribute(0).name();
45 if (attribute_name == "axes")
46 attr_axes = {nodeproto.attribute(0).ints().begin(), nodeproto.attribute(0).ints().end()};
47 }
48
49 std::string output_name = nodeproto.output(0);
50
51 if (attr_axes.empty())
52 op.reset(new ROperator_Reshape(opMode, attr_value, input_name, shape_name, output_name));
53 else // for old Squeeze and Unsqueeze
54 op.reset(new ROperator_Reshape(opMode, attr_axes, input_name, output_name));
55
56 if (!parser.IsRegisteredTensorType(output_name)) {
57 parser.RegisterTensorType(output_name, input_type);
58 }
59
60 return op;
61};
62
63} // namespace SOFIE
64} // namespace Experimental
65} // namespace TMVA
void RegisterTensorType(const std::string &, ETensorType)
ETensorType GetTensorType(const std::string &name)
ParserFuncSignature ParseReshape
std::function< std::unique_ptr< ROperator >(RModelParser_ONNX &, const onnx::NodeProto &)> ParserFuncSignature
create variable transformations