Changesets · Branches · Tags · Compare
| Changeset | df9a7627276debc478c6ea32f1b1d04c37ff320a9ded3aeddf8f5a4840f97d2c |
|---|---|
| Author | nihui <nihuini@tencent.com> · 2024-08-14 11:51 +08:00 |
| Committer | GitHub <noreply@github.com> · 2024-08-14 11:51 +08:00 |
| Parents | 7d0d9e601a1a |
7 files changed
modified tools/pnnx/src/ir.cpp +9 −0
| @@ -2111,6 +2111,15 @@ | ||
| 2111 | 2111 | fprintf(pyfp, ", "); |
| 2112 | 2112 | } |
| 2113 | 2113 | |
| 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 | + | |
| 2114 | 2123 | if (op->type.substr(0, 7) == "Tensor.") |
| 2115 | 2124 | { |
| 2116 | 2125 | if (op->type == "Tensor.fill") |
modified tools/pnnx/src/pass_level2/F_interpolate.cpp +89 −88
| @@ -1005,7 +1005,7 @@ | ||
| 1005 | 1005 | return R"PNNXIR(7767517 |
| 1006 | 1006 | 3 2 |
| 1007 | 1007 | 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 %*=%* | |
| 1009 | 1009 | pnnx.Output output 1 0 out |
| 1010 | 1010 | )PNNXIR"; |
| 1011 | 1011 | } |
| @@ -1017,104 +1017,69 @@ | ||
| 1017 | 1017 | |
| 1018 | 1018 | 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 |
| 1019 | 1019 | { |
| 1020 | - if (captured_params.at("sizes").type != 5) | |
| 1020 | + if (captured_params.find("op_0.coordinate_transformation_mode") == captured_params.end()) | |
| 1021 | 1021 | return false; |
| 1022 | 1022 | |
| 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) | |
| 1026 | 1024 | return false; |
| 1027 | 1025 | |
| 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()) | |
| 1030 | 1027 | return false; |
| 1031 | 1028 | |
| 1032 | - if (input_shape[0] != sizes[0] || input_shape[1] != sizes[1]) | |
| 1029 | + if (captured_params.at("op_0.mode").type != 4) | |
| 1033 | 1030 | return false; |
| 1034 | 1031 | |
| 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()) | |
| 1045 | 1033 | { |
| 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; | |
| 1055 | 1036 | } |
| 1056 | 1037 | |
| 1057 | - if (mode == "cubic") | |
| 1038 | + if (captured_params.find("op_0.roi") != captured_params.end()) | |
| 1058 | 1039 | { |
| 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; | |
| 1065 | 1042 | } |
| 1066 | 1043 | |
| 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; | |
| 1078 | 1046 | |
| 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; | |
| 1091 | 1050 | |
| 1092 | - const char* type_str() const | |
| 1093 | - { | |
| 1094 | - return "F.interpolate"; | |
| 1095 | - } | |
| 1051 | + if (sizes.size() < 3 || sizes.size() > 5) | |
| 1052 | + return false; | |
| 1096 | 1053 | |
| 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; | |
| 1101 | 1057 | |
| 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; | |
| 1103 | 1064 | |
| 1104 | - if (scales.size() < 3 || scales.size() > 5) | |
| 1105 | - return false; | |
| 1065 | + if (scales.size() < 3 || scales.size() > 5) | |
| 1066 | + return false; | |
| 1106 | 1067 | |
| 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 | + { | |
| 1108 | 1073 | return false; |
| 1074 | + } | |
| 1109 | 1075 | |
| 1110 | 1076 | return true; |
| 1111 | 1077 | } |
| 1112 | 1078 | |
| 1113 | 1079 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 1114 | 1080 | { |
| 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; | |
| 1118 | 1083 | |
| 1119 | 1084 | if (mode == "linear") |
| 1120 | 1085 | { |
| @@ -1122,11 +1087,6 @@ | ||
| 1122 | 1087 | op->params["align_corners"] = false; |
| 1123 | 1088 | if (coordinate_transformation_mode == "align_corners") |
| 1124 | 1089 | op->params["align_corners"] = true; |
| 1125 | - | |
| 1126 | - if (scales.size() == 4) | |
| 1127 | - mode = "bilinear"; | |
| 1128 | - if (scales.size() == 5) | |
| 1129 | - mode = "trilinear"; | |
| 1130 | 1090 | } |
| 1131 | 1091 | |
| 1132 | 1092 | if (mode == "cubic") |
| @@ -1135,22 +1095,63 @@ | ||
| 1135 | 1095 | op->params["align_corners"] = false; |
| 1136 | 1096 | if (coordinate_transformation_mode == "align_corners") |
| 1137 | 1097 | op->params["align_corners"] = true; |
| 1138 | - | |
| 1139 | - mode = "bicubic"; | |
| 1140 | 1098 | } |
| 1141 | 1099 | |
| 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 | + } | |
| 1150 | 1151 | } |
| 1151 | 1152 | }; |
| 1152 | 1153 | |
| 1153 | -REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_interpolate_onnx_1, 10) | |
| 1154 | +REGISTER_GLOBAL_PNNX_GRAPH_REWRITER_PASS(F_interpolate_onnx, 10) | |
| 1154 | 1155 | |
| 1155 | 1156 | class F_interpolate_onnx_2 : public GraphRewriterPass |
| 1156 | 1157 | { |
modified tools/pnnx/src/pass_level2/torch_max.cpp +12 −1
| @@ -78,11 +78,22 @@ | ||
| 78 | 78 | return "torch.max"; |
| 79 | 79 | } |
| 80 | 80 | |
| 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 | + | |
| 81 | 92 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 82 | 93 | { |
| 83 | 94 | if (captured_params.find("op_0.axes") != captured_params.end()) |
| 84 | 95 | { |
| 85 | - op->params["dim"] = captured_params.at("op_0.axes"); | |
| 96 | + op->params["dim"] = captured_params.at("op_0.axes").ai[0]; | |
| 86 | 97 | |
| 87 | 98 | if (captured_params.find("op_0.keepdims") != captured_params.end()) |
| 88 | 99 | { |
modified tools/pnnx/src/pass_level2/torch_min.cpp +12 −1
| @@ -78,11 +78,22 @@ | ||
| 78 | 78 | return "torch.min"; |
| 79 | 79 | } |
| 80 | 80 | |
| 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 | + | |
| 81 | 92 | void write(Operator* op, const std::map<std::string, Parameter>& captured_params) const |
| 82 | 93 | { |
| 83 | 94 | if (captured_params.find("op_0.axes") != captured_params.end()) |
| 84 | 95 | { |
| 85 | - op->params["dim"] = captured_params.at("op_0.axes"); | |
| 96 | + op->params["dim"] = captured_params.at("op_0.axes").ai[0]; | |
| 86 | 97 | |
| 87 | 98 | if (captured_params.find("op_0.keepdims") != captured_params.end()) |
| 88 | 99 | { |
modified tools/pnnx/src/pass_ncnn/torch_max.cpp +16 −0
| @@ -65,6 +65,22 @@ | ||
| 65 | 65 | |
| 66 | 66 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torch_max, 20) |
| 67 | 67 | |
| 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 | + | |
| 68 | 84 | class torch_max_1 : public GraphRewriterPass |
| 69 | 85 | { |
| 70 | 86 | public: |
modified tools/pnnx/src/pass_ncnn/torch_min.cpp +16 −0
| @@ -65,6 +65,22 @@ | ||
| 65 | 65 | |
| 66 | 66 | REGISTER_GLOBAL_PNNX_NCNN_GRAPH_REWRITER_PASS(torch_min, 20) |
| 67 | 67 | |
| 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 | + | |
| 68 | 84 | class torch_min_1 : public GraphRewriterPass |
| 69 | 85 | { |
| 70 | 86 | public: |
modified tools/pnnx/src/pass_onnx/fuse_constant_as_attribute.cpp +1 −0
| @@ -43,6 +43,7 @@ | ||
| 43 | 43 | {"ReduceProd", 1, "axes"}, |
| 44 | 44 | {"ReduceSum", 1, "axes"}, |
| 45 | 45 | {"Reshape", 1, "shape"}, |
| 46 | + {"Resize", 1, "roi"}, | |
| 46 | 47 | {"Resize", 2, "scales"}, |
| 47 | 48 | {"Resize", 3, "sizes"}, |
| 48 | 49 | {"Slice", 1, "starts"}, |