Elffuss BraidHub

kikocis/ncnn public

pnnx convert onnx resize with roi, torch.max torch.min with dim returns tuple (#5627)

* pnnx convert onnx resize with roi, torch.max torch.min with dim returns tuple

* torch max min only support single dim
Changesetdf9a7627276debc478c6ea32f1b1d04c37ff320a9ded3aeddf8f5a4840f97d2c
Authornihui <nihuini@tencent.com> · 2024-08-14 11:51 +08:00
CommitterGitHub <noreply@github.com> · 2024-08-14 11:51 +08:00
Parents7d0d9e601a1a

Browse the files at this changeset

7 files changed

modified tools/pnnx/src/ir.cpp +9 −0
@@ -2111,6 +2111,15 @@
21112111 fprintf(pyfp, ", ");
21122112 }
21132113
2114+ if (op->type == "torch.max" || op->type == "torch.max")
2115+ {
2116+ if (op->has_param("dim") && op->outputs.size() == 1)
2117+ {
2118+ // torch.max and torch.min with dim returns tuple
2119+ fprintf(pyfp, ", _");
2120+ }
2121+ }
2122+
21142123 if (op->type.substr(0, 7) == "Tensor.")
21152124 {
21162125 if (op->type == "Tensor.fill")
modified tools/pnnx/src/pass_level2/F_interpolate.cpp +89 −88
@@ -1005,7 +1005,7 @@
10051005 return R"PNNXIR(7767517
10061006 3 2
10071007 pnnx.Input input 0 1 input
1008-Resize op_0 1 1 input out sizes=%sizes coordinate_transformation_mode=%coordinate_transformation_mode mode=%mode nearest_mode=floor cubic_coeff_a=*
1008+Resize op_0 1 1 input out %*=%*
10091009 pnnx.Output output 1 0 out
10101010 )PNNXIR";
10111011 }
@@ -1017,104 +1017,69 @@
10171017
10181018 bool match(const std::map<std::string, const Operator*>& matched_operators, const std::map<std::string, Parameter>& captured_params, const std::map<std::string, Attribute>& /*captured_attrs*/) const
10191019 {
1020- if (captured_params.at("sizes").type != 5)
1020+ if (captured_params.find("op_0.coordinate_transformation_mode") == captured_params.end())
10211021 return false;
10221022
1023- const std::vector<int>& sizes = captured_params.at("sizes").ai;
1024-
1025- if (sizes.size() < 3 || sizes.size() > 5)
1023+ if (captured_params.at("op_0.coordinate_transformation_mode").type != 4)
10261024 return false;
10271025
1028- const std::vector<int>& input_shape = matched_operators.at("op_0")->inputs[0]->shape;
1029- if (input_shape.size() < 3 || input_shape.size() > 5)
1026+ if (captured_params.find("op_0.mode") == captured_params.end())
10301027 return false;
10311028
1032- if (input_shape[0] != sizes[0] || input_shape[1] != sizes[1])
1029+ if (captured_params.at("op_0.mode").type != 4)
10331030 return false;
10341031
1035- return true;
1036- }
1037-
1038- void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
1039- {
1040- const std::string& coordinate_transformation_mode = captured_params.at("coordinate_transformation_mode").s;
1041- std::string mode = captured_params.at("mode").s;
1042- const std::vector<int>& sizes = captured_params.at("sizes").ai;
1043-
1044- if (mode == "linear")
1032+ if (captured_params.find("op_0.nearest_mode") != captured_params.end())
10451033 {
1046- if (coordinate_transformation_mode == "half_pixel")
1047- op->params["align_corners"] = false;
1048- if (coordinate_transformation_mode == "align_corners")
1049- op->params["align_corners"] = true;
1050-
1051- if (sizes.size() == 4)
1052- mode = "bilinear";
1053- if (sizes.size() == 5)
1054- mode = "trilinear";
1034+ if (captured_params.at("op_0.nearest_mode").type != 4 || captured_params.at("op_0.nearest_mode").s != "floor")
1035+ return false;
10551036 }
10561037
1057- if (mode == "cubic")
1038+ if (captured_params.find("op_0.roi") != captured_params.end())
10581039 {
1059- if (coordinate_transformation_mode == "half_pixel")
1060- op->params["align_corners"] = false;
1061- if (coordinate_transformation_mode == "align_corners")
1062- op->params["align_corners"] = true;
1063-
1064- mode = "bicubic";
1040+ if (captured_params.at("op_0.roi").type != 6 || !captured_params.at("op_0.roi").ai.empty())
1041+ return false;
10651042 }
10661043
1067- op->params["mode"] = mode;
1068- if (sizes.size() == 3)
1069- op->params["size"] = {sizes[2]};
1070- if (sizes.size() == 4)
1071- op->params["size"] = {sizes[2], sizes[3]};
1072- if (sizes.size() == 5)
1073- op->params["size"] = {sizes[2], sizes[3], sizes[4]};
1074- }
1075-};
1076-
1077-REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_interpolate_onnx, 10)
1044+ if (captured_params.find("op_0.sizes") == captured_params.end() && captured_params.find("op_0.scales") == captured_params.end())
1045+ return false;
10781046
1079-class F_interpolate_onnx_1 : public GraphRewriterPass
1080-{
1081-public:
1082- const char* match_pattern_graph() const
1083- {
1084- return R"PNNXIR(7767517
1085-3 2
1086-pnnx.Input input 0 1 input
1087-Resize op_0 1 1 input out scales=%scales coordinate_transformation_mode=%coordinate_transformation_mode mode=%mode nearest_mode=floor cubic_coeff_a=*
1088-pnnx.Output output 1 0 out
1089-)PNNXIR";
1090- }
1047+ if (captured_params.find("op_0.sizes") != captured_params.end() && captured_params.at("op_0.sizes").type == 5 && !captured_params.at("op_0.sizes").ai.empty())
1048+ {
1049+ const std::vector<int>& sizes = captured_params.at("op_0.sizes").ai;
10911050
1092- const char* type_str() const
1093- {
1094- return "F.interpolate";
1095- }
1051+ if (sizes.size() < 3 || sizes.size() > 5)
1052+ return false;
10961053
1097- bool match(const std::map<std::string, Parameter>& captured_params) const
1098- {
1099- if (captured_params.at("scales").type != 6)
1100- return false;
1054+ const std::vector<int>& input_shape = matched_operators.at("op_0")->inputs[0]->shape;
1055+ if (input_shape.size() < 3 || input_shape.size() > 5)
1056+ return false;
11011057
1102- const std::vector<float>& scales = captured_params.at("scales").af;
1058+ if (input_shape[0] != sizes[0] || input_shape[1] != sizes[1])
1059+ return false;
1060+ }
1061+ else if (captured_params.find("op_0.scales") != captured_params.end() && captured_params.at("op_0.scales").type == 6 && !captured_params.at("op_0.scales").af.empty())
1062+ {
1063+ const std::vector<float>& scales = captured_params.at("op_0.scales").af;
11031064
1104- if (scales.size() < 3 || scales.size() > 5)
1105- return false;
1065+ if (scales.size() < 3 || scales.size() > 5)
1066+ return false;
11061067
1107- if (scales[0] != 1.f || scales[1] != 1.f)
1068+ if (scales[0] != 1.f || scales[1] != 1.f)
1069+ return false;
1070+ }
1071+ else
1072+ {
11081073 return false;
1074+ }
11091075
11101076 return true;
11111077 }
11121078
11131079 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
11141080 {
1115- const std::string& coordinate_transformation_mode = captured_params.at("coordinate_transformation_mode").s;
1116- std::string mode = captured_params.at("mode").s;
1117- const std::vector<float>& scales = captured_params.at("scales").af;
1081+ const std::string& coordinate_transformation_mode = captured_params.at("op_0.coordinate_transformation_mode").s;
1082+ std::string mode = captured_params.at("op_0.mode").s;
11181083
11191084 if (mode == "linear")
11201085 {
@@ -1122,11 +1087,6 @@
11221087 op->params["align_corners"] = false;
11231088 if (coordinate_transformation_mode == "align_corners")
11241089 op->params["align_corners"] = true;
1125-
1126- if (scales.size() == 4)
1127- mode = "bilinear";
1128- if (scales.size() == 5)
1129- mode = "trilinear";
11301090 }
11311091
11321092 if (mode == "cubic")
@@ -1135,22 +1095,63 @@
11351095 op->params["align_corners"] = false;
11361096 if (coordinate_transformation_mode == "align_corners")
11371097 op->params["align_corners"] = true;
1138-
1139- mode = "bicubic";
11401098 }
11411099
1142- op->params["mode"] = mode;
1143- op->params["recompute_scale_factor"] = false;
1144- if (scales.size() == 3)
1145- op->params["scale_factor"] = {scales[2]};
1146- if (scales.size() == 4)
1147- op->params["scale_factor"] = {scales[2], scales[3]};
1148- if (scales.size() == 5)
1149- op->params["scale_factor"] = {scales[2], scales[3], scales[4]};
1100+ if (captured_params.find("op_0.sizes") != captured_params.end() && captured_params.at("op_0.sizes").type == 5 && !captured_params.at("op_0.sizes").ai.empty())
1101+ {
1102+ const std::vector<int>& sizes = captured_params.at("op_0.sizes").ai;
1103+
1104+ if (mode == "linear")
1105+ {
1106+ if (sizes.size() == 4)
1107+ mode = "bilinear";
1108+ if (sizes.size() == 5)
1109+ mode = "trilinear";
1110+ }
1111+
1112+ if (mode == "cubic")
1113+ {
1114+ mode = "bicubic";
1115+ }
1116+
1117+ op->params["mode"] = mode;
1118+ if (sizes.size() == 3)
1119+ op->params["size"] = {sizes[2]};
1120+ if (sizes.size() == 4)
1121+ op->params["size"] = {sizes[2], sizes[3]};
1122+ if (sizes.size() == 5)
1123+ op->params["size"] = {sizes[2], sizes[3], sizes[4]};
1124+ }
1125+ else if (captured_params.find("op_0.scales") != captured_params.end() && captured_params.at("op_0.scales").type == 6 && !captured_params.at("op_0.scales").af.empty())
1126+ {
1127+ const std::vector<float>& scales = captured_params.at("op_0.scales").af;
1128+
1129+ if (mode == "linear")
1130+ {
1131+ if (scales.size() == 4)
1132+ mode = "bilinear";
1133+ if (scales.size() == 5)
1134+ mode = "trilinear";
1135+ }
1136+
1137+ if (mode == "cubic")
1138+ {
1139+ mode = "bicubic";
1140+ }
1141+
1142+ op->params["mode"] = mode;
1143+ op->params["recompute_scale_factor"] = false;
1144+ if (scales.size() == 3)
1145+ op->params["scale_factor"] = {scales[2]};
1146+ if (scales.size() == 4)
1147+ op->params["scale_factor"] = {scales[2], scales[3]};
1148+ if (scales.size() == 5)
1149+ op->params["scale_factor"] = {scales[2], scales[3], scales[4]};
1150+ }
11501151 }
11511152 };
11521153
1153-REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_interpolate_onnx_1, 10)
1154+REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_interpolate_onnx, 10)
11541155
11551156 class F_interpolate_onnx_2 : public GraphRewriterPass
11561157 {
modified tools/pnnx/src/pass_level2/torch_max.cpp +12 −1
@@ -78,11 +78,22 @@
7878 return "torch.max";
7979 }
8080
81+ bool match(const std::map<std::string, Parameter>& captured_params) const
82+ {
83+ if (captured_params.find("op_0.axes") != captured_params.end())
84+ {
85+ if (captured_params.at("op_0.axes").type != 5 || captured_params.at("op_0.axes").ai.size() != 1)
86+ return false;
87+ }
88+
89+ return true;
90+ }
91+
8192 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
8293 {
8394 if (captured_params.find("op_0.axes") != captured_params.end())
8495 {
85- op->params["dim"] = captured_params.at("op_0.axes");
96+ op->params["dim"] = captured_params.at("op_0.axes").ai[0];
8697
8798 if (captured_params.find("op_0.keepdims") != captured_params.end())
8899 {
modified tools/pnnx/src/pass_level2/torch_min.cpp +12 −1
@@ -78,11 +78,22 @@
7878 return "torch.min";
7979 }
8080
81+ bool match(const std::map<std::string, Parameter>& captured_params) const
82+ {
83+ if (captured_params.find("op_0.axes") != captured_params.end())
84+ {
85+ if (captured_params.at("op_0.axes").type != 5 || captured_params.at("op_0.axes").ai.size() != 1)
86+ return false;
87+ }
88+
89+ return true;
90+ }
91+
8192 void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const
8293 {
8394 if (captured_params.find("op_0.axes") != captured_params.end())
8495 {
85- op->params["dim"] = captured_params.at("op_0.axes");
96+ op->params["dim"] = captured_params.at("op_0.axes").ai[0];
8697
8798 if (captured_params.find("op_0.keepdims") != captured_params.end())
8899 {
modified tools/pnnx/src/pass_ncnn/torch_max.cpp +16 −0
@@ -65,6 +65,22 @@
6565
6666 REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torch_max, 20)
6767
68+class torch_max_0 : public torch_max
69+{
70+public:
71+ const char* match_pattern_graph() const
72+ {
73+ return R"PNNXIR(7767517
74+3 2
75+pnnx.Input input 0 1 input
76+torch.max op_0 1 1 input out dim=%dim keepdim=%keepdim
77+pnnx.Output output 1 0 out
78+)PNNXIR";
79+ }
80+};
81+
82+REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torch_max_0, 20)
83+
6884 class torch_max_1 : public GraphRewriterPass
6985 {
7086 public:
modified tools/pnnx/src/pass_ncnn/torch_min.cpp +16 −0
@@ -65,6 +65,22 @@
6565
6666 REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torch_min, 20)
6767
68+class torch_min_0 : public torch_min
69+{
70+public:
71+ const char* match_pattern_graph() const
72+ {
73+ return R"PNNXIR(7767517
74+3 2
75+pnnx.Input input 0 1 input
76+torch.min op_0 1 1 input out dim=%dim keepdim=%keepdim
77+pnnx.Output output 1 0 out
78+)PNNXIR";
79+ }
80+};
81+
82+REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torch_min_0, 20)
83+
6884 class torch_min_1 : public GraphRewriterPass
6985 {
7086 public:
modified tools/pnnx/src/pass_onnx/fuse_constant_as_attribute.cpp +1 −0
@@ -43,6 +43,7 @@
4343 {"ReduceProd", 1, "axes"},
4444 {"ReduceSum", 1, "axes"},
4545 {"Reshape", 1, "shape"},
46+ {"Resize", 1, "roi"},
4647 {"Resize", 2, "scales"},
4748 {"Resize", 3, "sizes"},
4849 {"Slice", 1, "starts"},