Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
ParseFuseMatMulAdd.cxx
Go to the documentation of this file.
3#include "onnx_proto3.pb.h"
4
5namespace TMVA {
6namespace Experimental {
7namespace SOFIE {
8
9ParserFuseFuncSignature ParseFuseMatMulAdd = [](RModelParser_ONNX &parser, const onnx::NodeProto &matmulnode,
10 const onnx::NodeProto &addnode) {
12
13 // check input tye - only first input from MatMul
14 auto input_name = matmulnode.input(0);
15 if (parser.IsRegisteredTensorType(input_name)) {
16 input_type = parser.GetTensorType(input_name);
17 } else {
18 throw std::runtime_error("TMVA::SOFIE ONNX Parser MatMul op has input tensor " + input_name +
19 " but its type is not yet registered");
20 }
21
22 if (addnode.input_size() != 2)
23 throw std::runtime_error("TMVA::SOFIE ONNX Parser : cannot fuse MatMul if Add does not have 2 inputs");
24 // output of matmul should be one of the input of Add
25 std::string nameBias;
26 if (matmulnode.output(0) == addnode.input(0))
27 nameBias = addnode.input(1);
28 else if (matmulnode.output(0) == addnode.input(1))
29 nameBias = addnode.input(0);
30 else
31 throw std::runtime_error("TMVA::SOFIE ONNX Parser : cannot fuse MatMul and Add since have different inputs");
32
33 // we don't check input type of ADD since it is not be registered
34 std::unique_ptr<ROperator> op;
35
36 float attr_alpha = 1.0;
37 float attr_beta = 1.0;
38 int_t attr_transA = 0;
39 int_t attr_transB = 0;
40
41 switch (input_type) {
43 op.reset(new ROperator_Gemm<float>(attr_alpha, attr_beta, attr_transA, attr_transB, matmulnode.input(0),
44 matmulnode.input(1), nameBias, addnode.output(0)));
45 break;
46 default:
47 throw std::runtime_error(
48 "TMVA::SOFIE - Unsupported - Operator for fusing MatMul and Add to Gemm does not yet support input type " +
49 std::to_string(static_cast<int>(input_type)));
50 }
51
52 std::string output_name = addnode.output(0);
53 if (!parser.IsRegisteredTensorType(output_name)) {
54 parser.RegisterTensorType(output_name, input_type);
55 }
56
57 return op;
58};
59
60} // namespace SOFIE
61} // namespace Experimental
62} // namespace TMVA
void RegisterTensorType(const std::string &, ETensorType)
ETensorType GetTensorType(const std::string &name)
std::function< std::unique_ptr< ROperator >(RModelParser_ONNX &, const onnx::NodeProto &, const onnx::NodeProto &)> ParserFuseFuncSignature
ParserFuseFuncSignature ParseFuseMatMulAdd
create variable transformations