forked from tile-ai/TileOPs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_grouped_gemm.py
More file actions
128 lines (111 loc) · 5 KB
/
Copy pathtest_grouped_gemm.py
File metadata and controls
128 lines (111 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import pytest
import torch
from tests.test_base import FixtureBase, TestBase
from tileops.ops.grouped_gemm import GroupedGemmOp
from workloads.grouped_gemm import (
GroupedGemmTest as _GroupedGemmTestWorkload,
)
class GroupedGemmTest(_GroupedGemmTestWorkload, TestBase):
def ref_program(self, A: torch.Tensor, B: torch.Tensor, batch_sizes: torch.Tensor,
batch_offsets: torch.Tensor,
batch_padded_offsets: torch.Tensor) -> torch.Tensor:
if not self.transpose_a:
# NT / NN: output is (batch_sum, N)
if self.transpose_b:
# NT: A @ B^T
assert A.shape[0] == sum(batch_sizes)
assert B.shape[0] == len(batch_sizes)
output = torch.empty((sum(batch_sizes), B.shape[1]), device=A.device, dtype=A.dtype)
start = 0
for i, size in enumerate(batch_sizes):
size = int(size.item())
end = start + size
output[start:end] = torch.mm(A[start:end], B[i].transpose(0, 1).contiguous())
start = end
else:
# NN: A @ B
assert A.shape[0] == sum(batch_sizes)
assert B.shape[0] == len(batch_sizes)
output = torch.empty((sum(batch_sizes), B.shape[2]), device=A.device, dtype=A.dtype)
start = 0
for i, size in enumerate(batch_sizes):
size = int(size.item())
end = start + size
output[start:end] = torch.mm(A[start:end], B[i])
start = end
else:
# TN / TT: output is (batch_count, N, K)
total_batch = int(batch_sizes.sum().item())
assert A.shape[0] == total_batch
N = A.shape[1]
batch_count = len(batch_sizes)
if self.transpose_b:
# TT: A^T @ B^T
K = B.shape[0]
assert B.shape[1] == total_batch
output = torch.zeros((batch_count, N, K), device=A.device, dtype=A.dtype)
start = 0
for i, size in enumerate(batch_sizes):
size = int(size.item())
end = start + size
output[i] = torch.mm(A[start:end].transpose(0, 1),
B[:, start:end].transpose(0, 1))
start = end
else:
# TN: A^T @ B
K = B.shape[1]
assert B.shape[0] == total_batch
output = torch.zeros((batch_count, N, K), device=A.device, dtype=A.dtype)
start = 0
for i, size in enumerate(batch_sizes):
size = int(size.item())
end = start + size
output[i] = torch.mm(A[start:end].transpose(0, 1), B[start:end])
start = end
return output
# ---------------------------------------------------------------------------
# Shared helper
# ---------------------------------------------------------------------------
# ---------------------------------------------------------------------------
# Parametrized grouped GEMM test
# ---------------------------------------------------------------------------
class GroupedGemmFixture(FixtureBase):
PARAMS = [
("batch_sum, batch_count, N, K, dtype, transpose_a, transpose_b, tune", [
pytest.param(
16384, 4, 4864, 4096, torch.float16, False, True, False,
marks=pytest.mark.smoke,
),
pytest.param(
16384, 4, 4864, 4096, torch.float16, False, False, False,
marks=pytest.mark.full,
),
pytest.param(
16384, 4, 4864, 4096, torch.float16, True, False, False,
marks=pytest.mark.full,
),
pytest.param(
16384, 4, 4864, 4096, torch.float16, True, True, False,
marks=pytest.mark.full,
),
]),
]
@GroupedGemmFixture
def test_grouped_gemm(batch_sum: int, batch_count: int, N: int, K: int, dtype: torch.dtype,
transpose_a: bool, transpose_b: bool, tune: bool) -> None:
test = GroupedGemmTest(batch_sum, batch_count, N, K, dtype, transpose_a, transpose_b)
op = GroupedGemmOp(
batch_sum, batch_count, N, K, dtype, transpose_a=transpose_a, transpose_b=transpose_b,
tune=tune)
test.check(op, *test.gen_inputs())
# ---------------------------------------------------------------------------
# Complete variant: forward (NT) + backward dA (NN) + backward dB (TN)
# ---------------------------------------------------------------------------
class GroupedGemmCompleteFixture(FixtureBase):
PARAMS = [
("batch_sum, batch_count, N, K, dtype, tune", [
pytest.param(16384, 4, 4864, 4096, torch.float16, False, marks=pytest.mark.smoke),
]),
]
if __name__ == "__main__":
pytest.main([__file__, "-vvs"])