已合并
【PR】: codegen infershape remove unused symbol #1688
chengyutao3创建于 27 天前
【PR】: codegen infershape remove unused symbol #1688
已合并
共 2 个文件变更+58-2
| @@ -10,6 +10,8 @@ | |||
| 10 | 10 | ||
| 11 | 11 | ||
| 12 | 12 | ||
| 13 | + | ||
| 14 | + | ||
| 13 | namespace codegen { | 15 | namespace codegen { |
| 14 | namespace { | 16 | namespace { |
| 15 | std::string GetFileHeaderDefine() { | 17 | std::string GetFileHeaderDefine() { |
| @@ -62,6 +64,23 @@ static_assert(std::is_standard_layout<InferShapeSymbolEvalContext>::value, | |||
| 62 | )"; | 64 | )"; |
| 63 | return file_header_str; | 65 | return file_header_str; |
| 64 | } | 66 | } |
| 67 | + | ||
| 68 | +std::set<std::string> CollectUsedSymbols(const std::vector<std::vector<std::string>> &symbol_shape_str, | ||
| 69 | + const std::map<std::string, std::string> &shape_info) { | ||
| 70 | + std::set<std::string> used_symbols; | ||
| 71 | + for (const auto &shape_dims : symbol_shape_str) { | ||
| 72 | + for (const auto &expr_str : shape_dims) { | ||
| 73 | + const auto expr = af::Expression::Parse(expr_str.c_str()); | ||
| 74 | + for (const auto &symbol : expr.FreeSymbols()) { | ||
| 75 | + const auto symbol_name = symbol.Str(); | ||
| 76 | + if (symbol_name != nullptr && shape_info.find(symbol_name.get()) != shape_info.end()) { | ||
| 77 | + used_symbols.insert(symbol_name.get()); | ||
| 78 | + } | ||
| 79 | + } | ||
| 80 | + } | ||
| 81 | + } | ||
| 82 | + return used_symbols; | ||
| 83 | +} | ||
| 65 | } // namespace | 84 | } // namespace |
| 66 | 85 | ||
| 67 | std::string InfershapeGen::GenInferShapeFunc(const std::vector<std::vector<std::string>> &symbol_shape_str, | 86 | std::string InfershapeGen::GenInferShapeFunc(const std::vector<std::vector<std::string>> &symbol_shape_str, |
| @@ -71,8 +90,9 @@ std::string InfershapeGen::GenInferShapeFunc(const std::vector<std::vector<std:: | |||
| 71 | printer.AddLine(GetFileHeaderDefine()); | 90 | printer.AddLine(GetFileHeaderDefine()); |
| 72 | 91 | ||
| 73 | std::string common_get_input_str; | 92 | std::string common_get_input_str; |
| 74 | - for (const auto &it : shape_info) { | 93 | + std::set<std::string> used_symbols = CollectUsedSymbols(symbol_shape_str, shape_info); |
| 75 | - common_get_input_str += (blank_space + "auto " + it.first + " = " + it.second + ";\n"); | 94 | + for (const auto &sym_name : used_symbols) { |
| 95 | + common_get_input_str += (blank_space + "auto " + sym_name + " = " + shape_info.at(sym_name) + ";\n"); | ||
| 76 | } | 96 | } |
| 77 | 97 | ||
| 78 | printer.DefineFuncBegin("extern \"C\" ge::graphStatus", "InferShape", "InferShapeSymbolEvalContext *context"); | 98 | printer.DefineFuncBegin("extern \"C\" ge::graphStatus", "InferShape", "InferShapeSymbolEvalContext *context"); |
| @@ -219,6 +219,42 @@ TEST(CodegenInfershapeTest, TestInfershapeFunc_Compile_OK_WithLambda) { | |||
| 219 | ASSERT_TRUE(CompileCodegenCode(code)); | 219 | ASSERT_TRUE(CompileCodegenCode(code)); |
| 220 | } | 220 | } |
| 221 | 221 | ||
| 222 | +TEST(CodegenInfershapeTest, TestInfershapeFunc_CollectUsedSymbols) { | ||
| 223 | + codegen::CodegenOptions opt; | ||
| 224 | + codegen::Codegen codegen(opt); | ||
| 225 | + vector<vector<std::string>> symbol_shape_str{{"s1 + s10", "s1 * s1"}}; | ||
| 226 | + std::map<std::string, std::string> shape_info = { | ||
| 227 | + {"s0", "invalid_unused_symbol"}, | ||
| 228 | + {"s1", "1"}, | ||
| 229 | + {"s10", "10"}, | ||
| 230 | + }; | ||
| 231 | + | ||
| 232 | + std::string code = codegen.GenerateInferShape(symbol_shape_str, shape_info); | ||
| 233 | + | ||
| 234 | + EXPECT_NE(code.find("auto s1 = 1;"), std::string::npos); | ||
| 235 | + EXPECT_NE(code.find("auto s10 = 10;"), std::string::npos); | ||
| 236 | + EXPECT_EQ(code.find("auto s0 = invalid_unused_symbol;"), std::string::npos); | ||
| 237 | + ASSERT_TRUE(CompileCodegenCode(code)); | ||
| 238 | +} | ||
| 239 | + | ||
| 240 | +TEST(CodegenInfershapeTest, TestInfershapeFunc_CollectUsedSymbolsWithBuiltinFunctions) { | ||
| 241 | + codegen::CodegenOptions opt; | ||
| 242 | + codegen::Codegen codegen(opt); | ||
| 243 | + vector<vector<std::string>> symbol_shape_str{{"Max(input_s1, Min(s1, 32))", "Pow(s2, 2)", "Mod(s3, 8)"}}; | ||
| 244 | + std::map<std::string, std::string> shape_info = { | ||
| 245 | + {"unused", "invalid_unused_symbol"}, {"input_s1", "1"}, {"s1", "2"}, {"s2", "3"}, {"s3", "4"}, | ||
| 246 | + }; | ||
| 247 | + | ||
| 248 | + std::string code = codegen.GenerateInferShape(symbol_shape_str, shape_info); | ||
| 249 | + | ||
| 250 | + EXPECT_NE(code.find("auto input_s1 = 1;"), std::string::npos); | ||
| 251 | + EXPECT_NE(code.find("auto s1 = 2;"), std::string::npos); | ||
| 252 | + EXPECT_NE(code.find("auto s2 = 3;"), std::string::npos); | ||
| 253 | + EXPECT_NE(code.find("auto s3 = 4;"), std::string::npos); | ||
| 254 | + EXPECT_EQ(code.find("auto unused = invalid_unused_symbol;"), std::string::npos); | ||
| 255 | + ASSERT_TRUE(CompileCodegenCode(code)); | ||
| 256 | +} | ||
| 257 | + | ||
| 222 | TEST(CodegenInfershapeTest, TestInfershapeFunc_Compile_NOK) { | 258 | TEST(CodegenInfershapeTest, TestInfershapeFunc_Compile_NOK) { |
| 223 | codegen::CodegenOptions opt; | 259 | codegen::CodegenOptions opt; |
| 224 | codegen::Codegen codegen(opt); | 260 | codegen::Codegen codegen(opt); |