Merged
fix(ir): Dump attr support map #4577
赵俊涵Add on Jul 2
fix(ir): Dump attr support map #4577
Merged
Merged fromzhaojunhan/pypto_openfix_ir_dumpintocann/pyptomaster
Total: 2 files changed+72-16
| @@ -35,6 +35,42 @@ class AttrHolder { | |||
| 35 | protected: | 35 | protected: |
| 36 | std::map<std::string, std::any> attributes; | 36 | std::map<std::string, std::any> attributes; |
| 37 | 37 | ||
| 38 | +private: | ||
| 39 | + static std::string DumpElementAttr(const Element& tensorElement) | ||
| 40 | + { | ||
| 41 | + if (tensorElement.IsSigned()) { | ||
| 42 | + return std::to_string(tensorElement.GetSignedData()); | ||
| 43 | + } | ||
| 44 | + if (tensorElement.IsUnsigned()) { | ||
| 45 | + return std::to_string(tensorElement.GetUnsignedData()); | ||
| 46 | + } | ||
| 47 | + if (tensorElement.IsFloat()) { | ||
| 48 | + return std::to_string(tensorElement.GetFloatData()); | ||
| 49 | + } | ||
| 50 | + return ""; | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + static std::string DumpSymbolicScalarListAttr(const std::vector<SymbolicScalar>& scalarList) | ||
| 54 | + { | ||
| 55 | + std::ostringstream oss; | ||
| 56 | + oss << "["; | ||
| 57 | + for (size_t k = 0; k < scalarList.size(); k++) { | ||
| 58 | + oss << ((k != 0) ? "," : "") << scalarList[k].Dump(); | ||
| 59 | + } | ||
| 60 | + oss << "]"; | ||
| 61 | + return oss.str(); | ||
| 62 | + } | ||
| 63 | + | ||
| 64 | + static std::string DumpMapIntIntAttr(const std::map<int, int>& dict) | ||
| 65 | + { | ||
| 66 | + std::ostringstream oss; | ||
| 67 | + size_t index = 0; | ||
| 68 | + for (const auto& [src, dst] : dict) { | ||
| 69 | + oss << ((index++ == 0) ? "" : ",") << src << ":" << dst; | ||
| 70 | + } | ||
| 71 | + return oss.str(); | ||
| 72 | + } | ||
| 73 | + | ||
| 38 | public: | 74 | public: |
| 39 | const std::map<std::string, std::any>& GetAllAttr() const { return attributes; } | 75 | const std::map<std::string, std::any>& GetAllAttr() const { return attributes; } |
| 40 | std::map<std::string, std::any>& GetAllAttr() { return attributes; } | 76 | std::map<std::string, std::any>& GetAllAttr() { return attributes; } |
| @@ -147,29 +183,17 @@ public: | |||
| 147 | } else if (it->second.type() == typeid(std::vector<int64_t>)) { | 183 | } else if (it->second.type() == typeid(std::vector<int64_t>)) { |
| 148 | result = IntVecToStr(pypto::AnyCastRef<std::vector<int64_t>>(it->second)); | 184 | result = IntVecToStr(pypto::AnyCastRef<std::vector<int64_t>>(it->second)); |
| 149 | } else if (it->second.type() == typeid(Element)) { | 185 | } else if (it->second.type() == typeid(Element)) { |
| 150 | - auto tensorElement = pypto::AnyCastRef<Element>(it->second); | 186 | + result = DumpElementAttr(pypto::AnyCastRef<Element>(it->second)); |
| 151 | - if (tensorElement.IsSigned()) { | ||
| 152 | - result = std::to_string(tensorElement.GetSignedData()); | ||
| 153 | - } else if (tensorElement.IsUnsigned()) { | ||
| 154 | - result = std::to_string(tensorElement.GetUnsignedData()); | ||
| 155 | - } else if (tensorElement.IsFloat()) { | ||
| 156 | - result = std::to_string(tensorElement.GetFloatData()); | ||
| 157 | - } | ||
| 158 | } else if (it->second.type() == typeid(SymbolicScalar)) { | 187 | } else if (it->second.type() == typeid(SymbolicScalar)) { |
| 159 | auto scalar = pypto::AnyCastRef<SymbolicScalar>(it->second); | 188 | auto scalar = pypto::AnyCastRef<SymbolicScalar>(it->second); |
| 160 | result = scalar.Dump(); | 189 | result = scalar.Dump(); |
| 161 | } else if (it->second.type() == typeid(std::vector<SymbolicScalar>)) { | 190 | } else if (it->second.type() == typeid(std::vector<SymbolicScalar>)) { |
| 162 | - auto scalarList = pypto::AnyCastRef<std::vector<SymbolicScalar>>(it->second); | 191 | + result = DumpSymbolicScalarListAttr(pypto::AnyCastRef<std::vector<SymbolicScalar>>(it->second)); |
| 163 | - std::ostringstream oss; | ||
| 164 | - oss << "["; | ||
| 165 | - for (size_t k = 0; k < scalarList.size(); k++) { | ||
| 166 | - oss << ((k != 0) ? "," : "") << scalarList[k].Dump(); | ||
| 167 | - } | ||
| 168 | - oss << "]"; | ||
| 169 | - result = oss.str(); | ||
| 170 | } else if (it->second.type() == typeid(std::vector<bool>)) { | 192 | } else if (it->second.type() == typeid(std::vector<bool>)) { |
| 171 | auto scalarList = pypto::AnyCastRef<std::vector<bool>>(it->second); | 193 | auto scalarList = pypto::AnyCastRef<std::vector<bool>>(it->second); |
| 172 | result = IntVecToStr<bool>(scalarList); | 194 | result = IntVecToStr<bool>(scalarList); |
| 195 | + } else if (it->second.type() == typeid(std::map<int, int>)) { | ||
| 196 | + result = DumpMapIntIntAttr(pypto::AnyCastRef<std::map<int, int>>(it->second)); | ||
| 173 | } else { | 197 | } else { |
| 174 | result += "unsupported type "; | 198 | result += "unsupported type "; |
| 175 | result += it->second.type().name(); | 199 | result += it->second.type().name(); |
| @@ -210,6 +234,13 @@ public: | |||
| 210 | return nlohmann::json(pypto::AnyCastRef<bool>(second)); | 234 | return nlohmann::json(pypto::AnyCastRef<bool>(second)); |
| 211 | } else if (second.type() == typeid(Element)) { | 235 | } else if (second.type() == typeid(Element)) { |
| 212 | return ToJson(pypto::AnyCastRef<Element>(second)); | 236 | return ToJson(pypto::AnyCastRef<Element>(second)); |
| 237 | + } else if (second.type() == typeid(std::map<int, int>)) { | ||
| 238 | + nlohmann::json dict = nlohmann::json::object(); | ||
| 239 | + auto inplaceInfo = pypto::AnyCast<std::map<int, int>>(second); | ||
| 240 | + for (const auto& [src, dst] : inplaceInfo) { | ||
| 241 | + dict[std::to_string(src)] = dst; | ||
| 242 | + } | ||
| 243 | + return dict; | ||
| 213 | } else { | 244 | } else { |
| 214 | return nlohmann::json("Unsupported type"); | 245 | return nlohmann::json("Unsupported type"); |
| 215 | } | 246 | } |
| @@ -133,3 +133,28 @@ TEST_F(OperationOpsTest, QuantMX_Fp32ToFp4Unsupported) | |||
| 133 | } | 133 | } |
| 134 | Platform::Instance().GetSoc().SetNPUArch(NPUArch::DAV_UNKNOWN); | 134 | Platform::Instance().GetSoc().SetNPUArch(NPUArch::DAV_UNKNOWN); |
| 135 | } | 135 | } |
| 136 | + | ||
| 137 | +TEST_F(OperationOpsTest, DumpAttr_PrintsInplaceInfoMap) | ||
| 138 | +{ | ||
| 139 | + Program::GetInstance().Reset(); | ||
| 140 | + Tensor self(DT_FP32, {2, 5}); | ||
| 141 | + Tensor indices(DT_INT64, {1, 4}); | ||
| 142 | + | ||
| 143 | + FUNCTION("DumpAttrScatterInplace", {self, indices}) | ||
| 144 | + { | ||
| 145 | + TileShape::Current().SetVecTile(8, 8); | ||
| 146 | + auto out = Scatter(self, indices, Element(DT_FP32, 2.0f), 0, ScatterMode::ADD); | ||
| 147 | + (void)out; | ||
| 148 | + auto* func = Program::GetInstance().GetCurrentFunction(); | ||
| 149 | + ASSERT_NE(func, nullptr); | ||
| 150 | + ASSERT_EQ(func->GetOperationSize(), 1); | ||
| 151 | + auto opDump = func->Operations(false).back().DumpAttr(); | ||
| 152 | + EXPECT_NE(opDump.find("#INPLACE_INFO{0:0}"), std::string::npos); | ||
| 153 | + EXPECT_EQ(opDump.find("unsupported type"), std::string::npos); | ||
| 154 | + | ||
| 155 | + auto opAttrJson = func->Operations(false).back().DumpAttrJson(OpAttributeKey::inplaceInfo); | ||
| 156 | + ASSERT_TRUE(opAttrJson.is_object()); | ||
| 157 | + ASSERT_TRUE(opAttrJson.contains("0")); | ||
| 158 | + EXPECT_EQ(opAttrJson["0"], 0); | ||
| 159 | + } | ||
| 160 | +} | ||