12 std::string input_name;
13 auto ninputs = nodeproto.input_size();
14 bool isConstantOfShape =
false;
17 input_name = nodeproto.input(0);
18 isConstantOfShape =
true;
20 throw std::runtime_error(
"TMVA::SOFIE ONNX Parser ConstantOfShape op has input tensor" + input_name +
21 " but its type is not yet registered");
26 std::cout <<
"\t.... ";
27 if (isConstantOfShape)
28 std::cout <<
"ConstantOfShape " << nodeproto.input(0) <<
" -> ";
30 std::cout <<
"Constant --> ";
31 std::cout << nodeproto.output(0) << std::endl;
34 std::unique_ptr<ROperator> op;
35 std::string attr_type;
37 std::string output_name = nodeproto.output(0);
39 std::vector<std::size_t> shape;
41 if (nodeproto.attribute_size() > 1)
42 throw std::runtime_error(
"TMVA::SOFIE ONNX Parser Constant or ConstantOfShape and attribute size is larger than 1");
43 if (nodeproto.attribute_size() > 0) {
44 std::string attribute_name = nodeproto.attribute(0).name();
46 if (attribute_name ==
"value") {
47 const onnx::TensorProto & t = nodeproto.attribute(0).t();
48 output_type =
static_cast<ETensorType>(t.data_type());
50 std::size_t length = 1;
51 for (
int j = 0; j < t.dims_size(); j++) {
52 shape.push_back(t.dims(j));
55 if (isConstantOfShape) {
58 throw std::runtime_error(
"TMVA::SOFIE ONNX Parser ConstantOfShape has invalid tensor size " + std::to_string(length));
64 std::vector<int32_t> values(length);
65 if (t.int32_data_size() ==
int(length)) {
66 for (
size_t i = 0; i < length; i++)
67 values[i] = t.int32_data(i);
69 auto raw_data_ptr =
reinterpret_cast<int32_t *
>(
const_cast<char *
>(t.raw_data().c_str()));
70 std::memcpy(values.data(), raw_data_ptr, length *
sizeof(int32_t));
76 std::vector<int64_t> values(length);
77 if (t.int64_data_size() ==
int(length)) {
78 for (
size_t i = 0; i < length; i++)
79 values[i] = t.int64_data(i);
81 auto raw_data_ptr =
reinterpret_cast<int64_t *
>(
const_cast<char *
>(t.raw_data().c_str()));
82 std::memcpy(values.data(), raw_data_ptr, length *
sizeof(int64_t));
88 std::vector<float> values(length);
89 if (t.float_data_size() ==
int(length)) {
90 for (
size_t i = 0; i < length; i++)
91 values[i] = t.float_data(i);
93 auto raw_data_ptr =
reinterpret_cast<float *
>(
const_cast<char *
>(t.raw_data().c_str()));
94 std::memcpy(values.data(), raw_data_ptr, length *
sizeof(
float));
100 std::vector<double> values(length);
101 if (t.double_data_size() ==
int(length)) {
102 for (
size_t i = 0; i < length; i++)
103 values[i] = t.double_data(i);
105 auto raw_data_ptr =
reinterpret_cast<double *
>(
const_cast<char *
>(t.raw_data().c_str()));
106 std::memcpy(values.data(), raw_data_ptr, length *
sizeof(
double));
113 std::vector<int8_t> values(length);
114 if (t.int32_data_size() ==
int(length)) {
115 for (
size_t i = 0; i < length; i++) {
116 auto val = t.int32_data(i);
117 if (val < 0 || val > 1)
118 throw std::runtime_error(
"TMVA::SOFIE ONNX Parser Constant has invalid boolean value " + std::to_string(val));
119 values[i] =
static_cast<int8_t
>(val);
122 throw std::runtime_error(
"TMVA::SOFIE ONNX Parser COnstant : invalid tensor data values");
128 throw std::runtime_error(
"Data type in Constant op attribute " +
ConvertTypeToString(output_type) +
129 " is not supported!\n");
134 if (!isConstantOfShape) {
136 if (attribute_name ==
"value_float") {
137 std::vector<float> values(1);
138 values[0] = nodeproto.attribute(0).f();
142 else if (attribute_name ==
"value_floats") {
143 auto values = std::vector<float>({nodeproto.attribute(0).floats().begin(), nodeproto.attribute(0).floats().end()});
144 shape.push_back(values.size());
147 else if (attribute_name ==
"value_int") {
148 std::vector<int64_t> values(1);
149 values[0] = nodeproto.attribute(0).i();
153 else if (attribute_name ==
"value_ints") {
154 auto values = std::vector<int64_t>({nodeproto.attribute(0).ints().begin(), nodeproto.attribute(0).ints().end()});
155 shape.push_back(values.size());
158 throw std::runtime_error(
"TMVA::SOFIE ONNX Parser Constant op: not yet supporting attribute " + attribute_name);
161 throw std::runtime_error(
"TMVA::SOFIE ONNX Parser ConstantOfShape op: parsed invalid attribute " + attribute_name);
168 if (isConstantOfShape) {
169 std::vector<float> values(1);
170 std::vector<size_t> constantShape(1,1);
173 throw std::runtime_error(
"TMVA::SOFIE ONNX Parser Constant has no attribute");
182 std::cout <<
"\t ParseConstant: operator created\n";