ROOT
master
Reference Guide
Loading...
Searching...
No Matches
ParseReshape.cxx
Go to the documentation of this file.
1
#include "
TMVA/RModelParser_ONNX.hxx
"
2
#include "
TMVA/ROperator_Reshape.hxx
"
3
#include "
onnx.hxx
"
4
5
namespace
TMVA
{
6
namespace
Experimental {
7
namespace
SOFIE {
8
9
ParserFuncSignature
ParseReshape
= [](
RModelParser_ONNX
&parser,
const
onnx::NodeProto
&
nodeproto
) {
10
// make Reshape operator
11
ETensorType
input_type
=
ETensorType::UNDEFINED
;
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
// second input is a tensor indicating shape (ReShape) or (for Squeeze/Unsqueeze) axes (it is optional)
25
auto
input2_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
||
opMode
==
Flatten
) &&
nodeproto
.attribute_size() > 0)
40
attr_value
=
nodeproto
.attribute(0).i();
41
42
std::vector<int64_t>
attr_axes
= {};
43
// this is for older ONNX versions ( <= 11)
44
if
(
nodeproto
.input_size() == 1 &&
nodeproto
.attribute_size() > 0 && (
opMode
==
Squeeze
||
opMode
==
Unsqueeze
)) {
45
std::string
attribute_name
=
nodeproto
.attribute(0).name();
46
if
(
attribute_name
==
"axes"
)
47
attr_axes
= {
nodeproto
.attribute(0).ints().
begin
(),
nodeproto
.attribute(0).ints().
end
()};
48
}
49
50
std::string
output_name
=
nodeproto
.output(0);
51
52
if
(
attr_axes
.empty())
53
// for Reshape and new version of Squeeze and Unsqueeze
54
op
.reset(
new
ROperator_Reshape
(
opMode
,
attr_value
,
input_name
,
input2_name
,
output_name
));
55
else
// for old Squeeze and Unsqueeze
56
op
.reset(
new
ROperator_Reshape
(
opMode
,
attr_axes
,
input_name
,
output_name
));
57
58
if
(!parser.
IsRegisteredTensorType
(
output_name
)) {
59
parser.
RegisterTensorType
(
output_name
,
input_type
);
60
}
61
62
return
op
;
63
};
64
65
}
// namespace SOFIE
66
}
// namespace Experimental
67
}
// namespace TMVA
RModelParser_ONNX.hxx
ROperator_Reshape.hxx
TRangeDynCast
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Definition
TCollection.h:359
ROOT::Detail::TRangeCast
Definition
TCollection.h:312
ROOT::RRangeCast::begin
const_iterator begin() const
Definition
RRangeCast.hxx:103
ROOT::RRangeCast::end
const_iterator end() const
Definition
RRangeCast.hxx:104
TMVA::Experimental::SOFIE::RModelParser_ONNX
Definition
RModelParser_ONNX.hxx:30
TMVA::Experimental::SOFIE::RModelParser_ONNX::IsRegisteredTensorType
bool IsRegisteredTensorType(const std::string &)
Definition
RModelParser_ONNX.cxx:426
TMVA::Experimental::SOFIE::RModelParser_ONNX::RegisterTensorType
void RegisterTensorType(const std::string &, ETensorType)
Definition
RModelParser_ONNX.cxx:421
TMVA::Experimental::SOFIE::RModelParser_ONNX::GetTensorType
ETensorType GetTensorType(const std::string &name)
Definition
RModelParser_ONNX.cxx:431
TMVA::Experimental::SOFIE::ROperator_Reshape
Definition
ROperator_Reshape.hxx:21
onnx::NodeProto
Definition
onnx.hxx:492
TMVA::Experimental::SOFIE::ParseReshape
ParserFuncSignature ParseReshape
Definition
ParseReshape.cxx:9
TMVA::Experimental::SOFIE::ETensorType
ETensorType
Definition
SOFIE_common.hxx:29
TMVA::Experimental::SOFIE::ETensorType::UNDEFINED
@ UNDEFINED
TMVA::Experimental::SOFIE::ParserFuncSignature
std::function< std::unique_ptr< ROperator >(RModelParser_ONNX &, const onnx::NodeProto &)> ParserFuncSignature
Definition
RModelParser_ONNX.hxx:25
TMVA::Experimental::SOFIE::ReshapeOpMode
ReshapeOpMode
Definition
ROperator_Reshape.hxx:17
TMVA::Experimental::SOFIE::Unsqueeze
@ Unsqueeze
Definition
ROperator_Reshape.hxx:17
TMVA::Experimental::SOFIE::Squeeze
@ Squeeze
Definition
ROperator_Reshape.hxx:17
TMVA::Experimental::SOFIE::Reshape
@ Reshape
Definition
ROperator_Reshape.hxx:17
TMVA::Experimental::SOFIE::Flatten
@ Flatten
Definition
ROperator_Reshape.hxx:17
TMVA
create variable transformations
Definition
GeneticMinimizer.h:22
onnx.hxx
tmva
sofie_parsers
src
ParseReshape.cxx
ROOTmaster - Reference Guide Generated on Sun Aug 16 2026 04:54:07 (GVA Time) using Doxygen 1.10.0