Merged
fix(interface): Reduce UT time and resolve code style. #3697
黄俊杰Add on Jun 2
fix(interface): Reduce UT time and resolve code style. #3697
Merged
黄俊杰Add on Jun 2
Total: 9 files changed+37-47
@@ -106,7 +106,6 @@ def elementwise_kernel(
106 106 
107 107 
108def test_elementwise_ops(device_id=None):108def test_elementwise_ops(device_id=None):
109- """Element-wise add + scalar mul: out = (a + b) * 2."""
110 print("=" * 60)109 print("=" * 60)
111 print("Example 2: Element-wise Operations")110 print("Example 2: Element-wise Operations")
112 print("=" * 60)111 print("=" * 60)
@@ -169,7 +168,6 @@ def matmul_kernel(
169 168 
170 169 
171def test_matmul(device_id=None):170def test_matmul(device_id=None):
172- """Basic matrix multiplication: C = A @ B."""
173 print("=" * 60)171 print("=" * 60)
174 print("Example 3: Matrix Multiplication")172 print("Example 3: Matrix Multiplication")
175 print("=" * 60)173 print("=" * 60)
@@ -179,7 +179,6 @@ def test_add_scalar_loop_dyn_axis_static_cond(device_id=None) -> None:
179 print(f"Input0 shape: {input_data0.shape}")179 print(f"Input0 shape: {input_data0.shape}")
180 print(f"Input1 shape: {input_data1.shape}")180 print(f"Input1 shape: {input_data1.shape}")
181 181 
182- # Test with flag=False: output = input0 + input1
183 output_data = torch.empty(shape, dtype=torch.float, device=device)182 output_data = torch.empty(shape, dtype=torch.float, device=device)
184 add_scalar_loop_dyn_axis_static_cond_kernel_static(input_data0, input_data1, output_data, val, False)183 add_scalar_loop_dyn_axis_static_cond_kernel_static(input_data0, input_data1, output_data, val, False)
185 golden = torch.add(input_data0, input_data1)184 golden = torch.add(input_data0, input_data1)
@@ -189,7 +188,6 @@ def test_add_scalar_loop_dyn_axis_static_cond(device_id=None) -> None:
189 if global_run_mode == pypto.RunMode.NPU:188 if global_run_mode == pypto.RunMode.NPU:
190 assert_allclose(np.array(output_data.cpu()), np.array(golden.cpu()), rtol=3e-3, atol=3e-3)189 assert_allclose(np.array(output_data.cpu()), np.array(golden.cpu()), rtol=3e-3, atol=3e-3)
191 190 
192- # Test with flag=True: output = input0 + input1 + val
193 output_data2 = torch.empty(shape, dtype=torch.float, device=device)191 output_data2 = torch.empty(shape, dtype=torch.float, device=device)
194 add_scalar_loop_dyn_axis_static_cond_kernel_dynamic(input_data0, input_data1, output_data2, val, True)192 add_scalar_loop_dyn_axis_static_cond_kernel_dynamic(input_data0, input_data1, output_data2, val, True)
195 golden2 = torch.add(input_data0, input_data1) + val193 golden2 = torch.add(input_data0, input_data1) + val
@@ -54,7 +54,7 @@ inline std::string DemangleTypeName(const char* mangledName)
54 // Simplify common pypto types for readability54 // Simplify common pypto types for readability
55 size_t pos = result.find("pypto::");55 size_t pos = result.find("pypto::");
56 if (pos != std::string::npos) {56 if (pos != std::string::npos) {
57- result = result.substr(pos + 7); // Remove "pypto::" prefix57+ result = result.substr(pos + 0x7); // Remove "pypto::" prefix
58 }58 }
59 59 
60 return result;60 return result;
@@ -132,32 +132,32 @@ public:
132 {132 {
133 switch (code_) {133 switch (code_) {
134 case kBoolCode:134 case kBoolCode:
135- return 8;135+ return 8; // These dtypes occupy 8 bits.
136 case kHf4Code:136 case kHf4Code:
137 case kFp4Code:137 case kFp4Code:
138 case kUInt4Code:138 case kUInt4Code:
139 case kInt4Code:139 case kInt4Code:
140- return 4;140+ return 4; // These dtypes occupy 4 bits.
141 case kHf8Code:141 case kHf8Code:
142 case kFp8e4m3fnCode:142 case kFp8e4m3fnCode:
143 case kFp8e5m2Code:143 case kFp8e5m2Code:
144 case kUInt8Code:144 case kUInt8Code:
145 case kInt8Code:145 case kInt8Code:
146- return 8;146+ return 8; // These dtypes occupy 8 bits.
147 case kBf16Code:147 case kBf16Code:
148 case kFp16Code:148 case kFp16Code:
149 case kUInt16Code:149 case kUInt16Code:
150 case kInt16Code:150 case kInt16Code:
151- return 16;151+ return 16; // These dtypes occupy 16 bits.
152 case kFp32Code:152 case kFp32Code:
153 case kUInt32Code:153 case kUInt32Code:
154 case kInt32Code:154 case kInt32Code:
155- return 32;155+ return 32; // These dtypes occupy 32 bits.
156 case kUInt64Code:156 case kUInt64Code:
157 case kInt64Code:157 case kInt64Code:
158 case kFp64Code:158 case kFp64Code:
159 case kIndexCode:159 case kIndexCode:
160- return 64;160+ return 64; // These dtypes occupy 64 bits.
161 default:161 default:
162 return 0;162 return 0;
163 }163 }
@@ -550,7 +550,7 @@ public:
550 template <typename T>550 template <typename T>
551 FatalLogger& operator<<(T&& val)551 FatalLogger& operator<<(T&& val)
552 {552 {
553- ss << std::forward<T>(val);553+ ss << (std::forward<T>(val));
554 return *this;554 return *this;
555 }555 }
556 556 
@@ -227,11 +227,11 @@ static std::string CleanupFilePath(const std::string& path)
227 // Replace the prefix up to "/./" with "./"227 // Replace the prefix up to "/./" with "./"
228 size_t markerPos = path.find("/./");228 size_t markerPos = path.find("/./");
229 if (markerPos != std::string::npos) {229 if (markerPos != std::string::npos) {
230- return "./" + path.substr(markerPos + 3);230+ return "./" + path.substr(markerPos + 0x3);
231 }231 }
232 232 
233 // If path already starts with "./", keep it as-is233 // If path already starts with "./", keep it as-is
234- if (path.size() >= 2 && path[0] == '.' && path[1] == '/') {234+ if (path.size() >= 0x2 && path[0] == '.' && path[1] == '/') {
235 return path;235 return path;
236 }236 }
237 237 
@@ -313,7 +313,6 @@ std::vector<StackFrame> Backtrace::CaptureStackTrace(int skip)
313 313 
314 // Try to get file and line information using addr2line314 // Try to get file and line information using addr2line
315 FileLocation loc = GetFileLineFromAddr2line(addr);315 FileLocation loc = GetFileLineFromAddr2line(addr);
316- 
317 if (!loc.filename.empty()) {316 if (!loc.filename.empty()) {
318 loc.filename = CleanupFilePath(loc.filename);317 loc.filename = CleanupFilePath(loc.filename);
319 }318 }
@@ -1321,11 +1321,11 @@ static void Uniform(
1321 const TensorData& out, const Element& key, const Element& counter0, const Element& counter1, const Element& rounds,1321 const TensorData& out, const Element& key, const Element& counter0, const Element& counter1, const Element& rounds,
1322 DataType dtype)1322 DataType dtype)
1323{1323{
1324- std::vector<uint32_t> keyVec(2);1324+ std::vector<uint32_t> keyVec(0x2);
1325 keyVec[0] = static_cast<uint32_t>(key.Cast<uint64_t>() & 0xFFFFFFFF);1325 keyVec[0] = static_cast<uint32_t>(key.Cast<uint64_t>() & 0xFFFFFFFF);
1326 keyVec[1] = static_cast<uint32_t>(key.Cast<uint64_t>() >> 0x20);1326 keyVec[1] = static_cast<uint32_t>(key.Cast<uint64_t>() >> 0x20);
1327 1327 
1328- std::vector<uint32_t> counterVec(4);1328+ std::vector<uint32_t> counterVec(0x4);
1329 counterVec[0] = static_cast<uint32_t>(counter0.Cast<uint64_t>() & 0xFFFFFFFF);1329 counterVec[0] = static_cast<uint32_t>(counter0.Cast<uint64_t>() & 0xFFFFFFFF);
1330 counterVec[1] = static_cast<uint32_t>(counter0.Cast<uint64_t>() >> 0x20);1330 counterVec[1] = static_cast<uint32_t>(counter0.Cast<uint64_t>() >> 0x20);
1331 counterVec[2] = static_cast<uint32_t>(counter1.Cast<uint64_t>() & 0xFFFFFFFF);1331 counterVec[2] = static_cast<uint32_t>(counter1.Cast<uint64_t>() & 0xFFFFFFFF);
@@ -1697,9 +1697,9 @@ static torch::Tensor BuildMXScaleForA(const torch::Tensor& scaleA, bool scaleATr
1697 localScaleA.size(localScaleIndex1) == mSize && localScaleA.size(localScaleIndex2) == 0x2)1697 localScaleA.size(localScaleIndex1) == mSize && localScaleA.size(localScaleIndex2) == 0x2)
1698 << "MX trans scale_a shape mismatch, expected [K/64, M, 2], got [" << localScaleA.size(localScaleIndex0)1698 << "MX trans scale_a shape mismatch, expected [K/64, M, 2], got [" << localScaleA.size(localScaleIndex0)
1699 << ", " << localScaleA.size(localScaleIndex1) << ", " << localScaleA.size(localScaleIndex2) << "]";1699 << ", " << localScaleA.size(localScaleIndex1) << ", " << localScaleA.size(localScaleIndex2) << "]";
1700- merged = localScaleA.transpose(-2, -1).reshape({-1, mSize}).transpose(0, 1);1700+ merged = localScaleA.transpose(-0x2, -1).reshape({-1, mSize}).transpose(0, 1);
1701 }1701 }
1702- auto expanded = merged.repeat_interleave(32, 1);1702+ auto expanded = merged.repeat_interleave(0x20, 1);
1703 ASSERT(CalculatorErrorScene::MATMUL_INPUT_SHAPE_MISMATCH, expanded.size(1) == kSize)1703 ASSERT(CalculatorErrorScene::MATMUL_INPUT_SHAPE_MISMATCH, expanded.size(1) == kSize)
1704 << "MX scale_a expanded K mismatch, got " << expanded.size(1) << ", expect " << kSize;1704 << "MX scale_a expanded K mismatch, got " << expanded.size(1) << ", expect " << kSize;
1705 return expanded;1705 return expanded;
@@ -1713,7 +1713,7 @@ static torch::Tensor BuildMXScaleForB(const torch::Tensor& scaleB, bool scaleBTr
1713 torch::Tensor merged;1713 torch::Tensor merged;
1714 constexpr size_t localScaleIndex0 = 0;1714 constexpr size_t localScaleIndex0 = 0;
1715 constexpr size_t localScaleIndex1 = 1;1715 constexpr size_t localScaleIndex1 = 1;
1716- constexpr size_t localScaleIndex2 = 2;1716+ constexpr size_t localScaleIndex2 = 0x2;
1717 if (!scaleBTrans) {1717 if (!scaleBTrans) {
1718 // test reference: torch.transpose(scale_b, -2, -1).reshape(k / 32, n)1718 // test reference: torch.transpose(scale_b, -2, -1).reshape(k / 32, n)
1719 ASSERT(1719 ASSERT(
@@ -1721,7 +1721,7 @@ static torch::Tensor BuildMXScaleForB(const torch::Tensor& scaleB, bool scaleBTr
1721 localScaleB.size(localScaleIndex1) == nSize && localScaleB.size(localScaleIndex2) == 0x2)1721 localScaleB.size(localScaleIndex1) == nSize && localScaleB.size(localScaleIndex2) == 0x2)
1722 << "MX scale_b shape mismatch, expected [K/64, N, 2], got [" << localScaleB.size(localScaleIndex0) << ", "1722 << "MX scale_b shape mismatch, expected [K/64, N, 2], got [" << localScaleB.size(localScaleIndex0) << ", "
1723 << localScaleB.size(localScaleIndex1) << ", " << localScaleB.size(localScaleIndex2) << "]";1723 << localScaleB.size(localScaleIndex1) << ", " << localScaleB.size(localScaleIndex2) << "]";
1724- merged = localScaleB.transpose(-2, -1).reshape({-1, nSize});1724+ merged = localScaleB.transpose(-0x2, -1).reshape({-1, nSize});
1725 } else {1725 } else {
1726 // test reference: scale_b.view(n, k / 32).T1726 // test reference: scale_b.view(n, k / 32).T
1727 ASSERT(1727 ASSERT(
@@ -1731,7 +1731,7 @@ static torch::Tensor BuildMXScaleForB(const torch::Tensor& scaleB, bool scaleBTr
1731 << ", " << localScaleB.size(localScaleIndex1) << ", " << localScaleB.size(localScaleIndex2) << "]";1731 << ", " << localScaleB.size(localScaleIndex1) << ", " << localScaleB.size(localScaleIndex2) << "]";
1732 merged = localScaleB.reshape({nSize, -1}).transpose(0, 1);1732 merged = localScaleB.reshape({nSize, -1}).transpose(0, 1);
1733 }1733 }
1734- auto expanded = merged.repeat_interleave(32, 0);1734+ auto expanded = merged.repeat_interleave(0x20, 0);
1735 ASSERT(CalculatorErrorScene::MATMUL_INPUT_SHAPE_MISMATCH, expanded.size(0) == kSize)1735 ASSERT(CalculatorErrorScene::MATMUL_INPUT_SHAPE_MISMATCH, expanded.size(0) == kSize)
1736 << "MX scale_b expanded K mismatch, got " << expanded.size(0) << ", expect " << kSize;1736 << "MX scale_b expanded K mismatch, got " << expanded.size(0) << ", expect " << kSize;
1737 return expanded;1737 return expanded;
@@ -2008,7 +2008,7 @@ void GatherMask(const TensorData& out, const TensorData& self, int patternMode)
2008 auto ret = From(out);2008 auto ret = From(out);
2009 auto src = From(self);2009 auto src = From(self);
2010 auto last_dim = src.second.size(src.second.dim() - 1);2010 auto last_dim = src.second.size(src.second.dim() - 1);
2011- if (patternMode == 7) {2011+ if (patternMode == 0x7) {
2012 ret.second = src.second;2012 ret.second = src.second;
2013 } else {2013 } else {
2014 torch::Tensor selected_indices;2014 torch::Tensor selected_indices;
@@ -839,10 +839,12 @@ TEST_F_WITH_COST(DynamicOpsTest, Cube, 98)
839 std::string logOutput = CaptureLogFileAndEcho([]() {839 std::string logOutput = CaptureLogFileAndEcho([]() {
840 config::SetVerifyOption(KEY_ENABLE_PASS_VERIFY, true);840 config::SetVerifyOption(KEY_ENABLE_PASS_VERIFY, true);
841 config::SetVerifyOption(KEY_PASS_VERIFY_SAVE_TENSOR, true);841 config::SetVerifyOption(KEY_PASS_VERIFY_SAVE_TENSOR, true);
842+ std::vector<std::string> passList{"CodegenPreproc"};
843+ config::SetVerifyOption(KEY_PASS_VERIFY_FILTER, passList);
842 844 
843- int n = 4;845+ const int n = 4;
844- int k = 1024;846+ const int k = 1024;
845- int m = 576;847+ const int m = 576;
846 848 
847 using EltType = float;849 using EltType = float;
848 DataType eltType = DT_FP32;850 DataType eltType = DT_FP32;
@@ -851,25 +853,18 @@ TEST_F_WITH_COST(DynamicOpsTest, Cube, 98)
851 Tensor t2(eltType, {n, m}, "t2");853 Tensor t2(eltType, {n, m}, "t2");
852 854 
853 std::vector<EltType> t0Data(n * k);855 std::vector<EltType> t0Data(n * k);
854- for (int i = 0; i < n; i++) {
855- for (int j = 0; j < k; j++) {
856- t0Data[i * k + j] = (i / 32) * 4 + (j / 64);
857- }
858- }
859 std::vector<EltType> t1Data(k * m);856 std::vector<EltType> t1Data(k * m);
860- for (int i = 0; i < k; i++) {857+ std::vector<EltType> t2Data(n * m, 0);
861- for (int j = 0; j < m; j++) {858+ 
862- t1Data[i * m + j] = (i / 64) * 4 + (j / 64);859+ for (int idx = 0; idx < n * k; ++idx) {
863- }860+ t0Data[idx] = ((idx / k) / 32) * 4 + ((idx % k) / 64);
864 }861 }
865- std::vector<EltType> t2Data(n * m);862+ for (int idx = 0; idx < k * m; ++idx) {
866- for (int i = 0; i < n; i++) {863+ t1Data[idx] = ((idx / m) / 64) * 4 + ((idx % m) / 64);
867- for (int j = 0; j < m; j++) {864+ }
868- EltType sum = 0;865+ for (int idx = 0; idx < n * m; ++idx) {
869- for (int x = 0; x < k; x++) {866+ for (int x = 0; x < k; ++x) {
870- sum += t0Data[i * k + x] * t1Data[x * m + j];867+ t2Data[idx] += t0Data[(idx / m) * k + x] * t1Data[x * m + (idx % m)];
871- }
872- t2Data[i * m + j] = sum;
873 }868 }
874 }869 }
875 870 
@@ -889,7 +884,7 @@ TEST_F_WITH_COST(DynamicOpsTest, Cube, 98)
889 LOOP("L0", FunctionType::DYNAMIC_LOOP, i, LoopRange(1))884 LOOP("L0", FunctionType::DYNAMIC_LOOP, i, LoopRange(1))
890 {885 {
891 (void)i;886 (void)i;
892- t2 = Matrix::Matmul(eltType, t0, t1); // int32887+ t2 = Matrix::Matmul(eltType, t0, t1);
893 }888 }
894 }889 }
895 });890 });
@@ -190,11 +190,11 @@ void bind_controller_loop(py::module& m)
190 .def(py::init<std::int64_t>()) // C++ Implicit conversion int64_t -> SymbolicScalar190 .def(py::init<std::int64_t>()) // C++ Implicit conversion int64_t -> SymbolicScalar
191 .def("Dump", (std::string(LoopRange::*)()) & LoopRange::Dump)191 .def("Dump", (std::string(LoopRange::*)()) & LoopRange::Dump)
192 .def(192 .def(
193- "Begin", (SymbolicScalar & (LoopRange::*)()) & LoopRange::Begin,193+ "Begin", (SymbolicScalar& (LoopRange::*)())& LoopRange::Begin,
194 py::return_value_policy::reference_internal)194 py::return_value_policy::reference_internal)
195- .def("End", (SymbolicScalar & (LoopRange::*)()) & LoopRange::End, py::return_value_policy::reference_internal)195+ .def("End", (SymbolicScalar& (LoopRange::*)())& LoopRange::End, py::return_value_policy::reference_internal)
196 .def(196 .def(
197- "Step", (SymbolicScalar & (LoopRange::*)()) & LoopRange::Step, py::return_value_policy::reference_internal);197+ "Step", (SymbolicScalar& (LoopRange::*)())& LoopRange::Step, py::return_value_policy::reference_internal);
198 198 
199 m.def("IsLoopBegin", &IsLoopBegin, py::arg("symbol"), py::arg("begin"));199 m.def("IsLoopBegin", &IsLoopBegin, py::arg("symbol"), py::arg("begin"));
200 m.def("IsLoopEnd", &IsLoopEnd, py::arg("symbol"), py::arg("end"));200 m.def("IsLoopEnd", &IsLoopEnd, py::arg("symbol"), py::arg("end"));