已合并
add group_type=2 meta tests and negative cases for npu_grouped_matmul #34653
XianglongZeng创建于 4月28日
add group_type=2 meta tests and negative cases for npu_grouped_matmul #34653
已合并
XianglongZeng创建于 4月28日
1 个文件变更+56-0
Mtest/test_fake_tensor.py+56-0
@@ -1823,6 +1823,62 @@ class TestGroupedMatmul(TestCase):
1823 self.assertTrue(x[0].shape[0] == res[0].shape[0])1823 self.assertTrue(x[0].shape[0] == res[0].shape[0])
1824 self.assertTrue(w[0].shape[2] == res[0].shape[1])1824 self.assertTrue(w[0].shape[2] == res[0].shape[1])
1825 1825 
1826+ def test_npu_grouped_matmul_meta_5(self):
1827+ # K_SPLIT (group_type=2) with multi-weight and split_item=2
1828+ with FakeTensorMode():
1829+ torch.manual_seed(0)
1830+ x1 = torch.randn(256, 1792, dtype=torch.float16).npu()
1831+ x = [x1.t()]
1832+ w1 = torch.randn(256, 256, dtype=torch.float16).npu()
1833+ w2 = torch.randn(256, 256, dtype=torch.float16).npu()
1834+ w3 = torch.randn(512, 256, dtype=torch.float16).npu()
1835+ w4 = torch.randn(768, 256, dtype=torch.float16).npu()
1836+ w = [w1, w2, w3, w4]
1837+ group_list = torch.tensor([256, 512, 1024, 1792]).npu()
1838+ split_item = 2
1839+ 
1840+ res = torch_npu.npu_grouped_matmul(x, w, bias=None, group_list=group_list,
1841+ split_item=split_item, group_type=2,
1842+ group_list_type=0)
1843+ self.assertTrue(len(res[0].shape) == 3)
1844+ self.assertTrue(group_list.shape[0] == res[0].shape[0])
1845+ self.assertTrue(x1.shape[1] == res[0].shape[1])
1846+ self.assertTrue(w1.shape[1] == res[0].shape[2])
1847+ 
1848+ def test_npu_grouped_matmul_meta_6(self):
1849+ # K_SPLIT (group_type=2) with single-weight and split_item=3
1850+ with FakeTensorMode():
1851+ torch.manual_seed(0)
1852+ x1 = torch.randn(256, 1792, dtype=torch.float16).npu()
1853+ x = [x1.t()]
1854+ w1 = torch.randn(1792, 256, dtype=torch.float16).npu()
1855+ w = [w1]
1856+ group_list = torch.tensor([256, 512, 1024, 1792]).npu()
1857+ split_item = 3
1858+ 
1859+ res = torch_npu.npu_grouped_matmul(x, w, bias=None, group_list=group_list,
1860+ split_item=split_item, group_type=2,
1861+ group_list_type=0)
1862+ self.assertTrue(len(res[0].shape) == 3)
1863+ self.assertTrue(group_list.shape[0] == res[0].shape[0])
1864+ self.assertTrue(x1.shape[1] == res[0].shape[1])
1865+ self.assertTrue(w1.shape[1] == res[0].shape[2])
1866+ 
1867+ def test_npu_grouped_matmul_meta_invalid_group_type(self):
1868+ # Negative test: invalid group_type should be rejected by meta layer
1869+ with FakeTensorMode():
1870+ torch.manual_seed(0)
1871+ x1 = torch.randn(256, 256, dtype=torch.float16).npu()
1872+ x = [x1]
1873+ w1 = torch.randn(256, 256, dtype=torch.float16).npu()
1874+ w = [w1]
1875+ group_list = torch.tensor([256]).npu()
1876+ 
1877+ for invalid_type in [1, 3, 100]:
1878+ with self.assertRaisesRegex(RuntimeError, "group_type only supports"):
1879+ torch_npu.npu_grouped_matmul(x, w, group_list=group_list,
1880+ split_item=2, group_type=invalid_type)
1881+ 
1826 1882 
1827class TestQuantMatmul(TestCase):1883class TestQuantMatmul(TestCase):
1828 def test_npu_quant_matmul_meta(self):1884 def test_npu_quant_matmul_meta(self):