tileops 0.0.1.dev0__tar.gz → 0.0.1.dev2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. tileops-0.0.1.dev2/PKG-INFO +147 -0
  2. tileops-0.0.1.dev2/README.md +119 -0
  3. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/pyproject.toml +28 -14
  4. tileops-0.0.1.dev2/setup.py +44 -0
  5. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_blocksparse_attention.py +15 -8
  6. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_gqa.py +18 -10
  7. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_linear_attn_fused_chunk.py +0 -3
  8. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_linear_attn_fused_recurrent.py +0 -3
  9. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_mamba_chunk_scan.py +0 -3
  10. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_mamba_chunk_state.py +0 -3
  11. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_mha.py +5 -7
  12. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_mha_decode.py +5 -7
  13. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_mla.py +0 -3
  14. tileops-0.0.1.dev2/tests/test_sparse_mla.py +57 -0
  15. tileops-0.0.1.dev2/tileops.egg-info/PKG-INFO +147 -0
  16. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tileops.egg-info/SOURCES.txt +15 -1
  17. tileops-0.0.1.dev2/tileops.egg-info/requires.txt +18 -0
  18. tileops-0.0.1.dev2/tileops.egg-info/top_level.txt +4 -0
  19. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/top/__init__.py +4 -6
  20. tileops-0.0.1.dev2/top/kernel/__init__.py +15 -0
  21. tileops-0.0.1.dev2/top/kernel/bitnet.py +490 -0
  22. tileops-0.0.1.dev2/top/kernel/blocksparse_attention.py +605 -0
  23. tileops-0.0.1.dev2/top/kernel/gqa.py +1079 -0
  24. tileops-0.0.1.dev2/top/kernel/linear_attention/__init__.py +0 -0
  25. tileops-0.0.1.dev2/top/kernel/linear_attention/linear_attn.py +490 -0
  26. tileops-0.0.1.dev2/top/kernel/mamba_chunk_scan.py +335 -0
  27. tileops-0.0.1.dev2/top/kernel/mamba_chunk_state.py +269 -0
  28. tileops-0.0.1.dev2/top/kernel/mha.py +920 -0
  29. tileops-0.0.1.dev2/top/kernel/mla.py +311 -0
  30. tileops-0.0.1.dev2/top/kernel/sparse_mla.py +502 -0
  31. tileops-0.0.1.dev2/top/utils/__init__.py +1 -0
  32. tileops-0.0.1.dev2/top/utils/utils.py +55 -0
  33. tileops-0.0.1.dev0/PKG-INFO +0 -82
  34. tileops-0.0.1.dev0/README.md +0 -65
  35. tileops-0.0.1.dev0/setup.py +0 -33
  36. tileops-0.0.1.dev0/tileops.egg-info/PKG-INFO +0 -82
  37. tileops-0.0.1.dev0/tileops.egg-info/requires.txt +0 -2
  38. tileops-0.0.1.dev0/tileops.egg-info/top_level.txt +0 -1
  39. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/LICENSE +0 -0
  40. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/setup.cfg +0 -0
  41. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_bitnet.py +0 -0
  42. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tests/test_gqa_decode.py +0 -0
  43. {tileops-0.0.1.dev0 → tileops-0.0.1.dev2}/tileops.egg-info/dependency_links.txt +0 -0
@@ -0,0 +1,147 @@
1
+ Metadata-Version: 2.4
2
+ Name: tileops
3
+ Version: 0.0.1.dev2
4
+ Summary: TileOPs kernels for efficient inference
5
+ Author: Tile-AI
6
+ License-Expression: MIT
7
+ Requires-Python: >=3.8
8
+ Description-Content-Type: text/markdown
9
+ License-File: LICENSE
10
+ Requires-Dist: torch>=2.1.0
11
+ Requires-Dist: numpy>=1.23.5
12
+ Requires-Dist: einops>=0.6.0
13
+ Requires-Dist: Cython
14
+ Requires-Dist: tqdm>=4.62.3
15
+ Requires-Dist: typing_extensions>=4.10.0
16
+ Requires-Dist: cloudpickle
17
+ Requires-Dist: ml_dtypes
18
+ Requires-Dist: psutil
19
+ Requires-Dist: tilelang>=0.1.6.post1
20
+ Provides-Extra: dev
21
+ Requires-Dist: yapf==0.40.2; extra == "dev"
22
+ Requires-Dist: toml==0.10.2; extra == "dev"
23
+ Requires-Dist: tomli==2.0.1; extra == "dev"
24
+ Requires-Dist: ruff==0.6.5; extra == "dev"
25
+ Requires-Dist: codespell==2.3.0; extra == "dev"
26
+ Requires-Dist: clang-format==15.0.7; extra == "dev"
27
+ Dynamic: license-file
28
+
29
+ # TileOPs (TOP)
30
+
31
+ **TileOPs (TOP)** is a high-performance machine learning operator collections built on top of [TileLang](https://github.com/tile-ai/tilelang). It offers efficient, modular, and composable implementations optimized for AI workloads.
32
+
33
+ Note: TileOPs is still under rapid development.
34
+
35
+ ---
36
+
37
+ ![DeepSeek-V3.2-Exp DeepSeek Sparse Attention (DSA) performance on H800 SXM](https://raw.githubusercontent.com/tile-ai/TileOPs/main/docs/figures/sparse_mla_perf.png)
38
+ *DeepSeek-V3.2-Exp DeepSeek Sparse Attention (DSA) performance on H800 SXM*
39
+
40
+ ## 📦 Installation
41
+
42
+ ### Requirements
43
+
44
+ - Python 3.8+
45
+ - PyTorch >= 2.1
46
+ - GLIBCXX_3.4.32
47
+ - [TileLang](https://github.com/tile-ai/tilelang)
48
+
49
+ ### Method 1: Install with Pip
50
+
51
+ ```bash
52
+ pip install tileops
53
+ ```
54
+
55
+ ### Method 2: Install from source (editable mode for development)
56
+
57
+ ```bash
58
+ git clone https://github.com/tile-ai/TileOPs
59
+ cd TileOPs
60
+ pip install -e '.[dev]' -v # remove -e option if you don't want to install in editable mode, -v for verbose output
61
+ ```
62
+
63
+ ## 🚀 Quick Usage
64
+
65
+ ### Sparse MLA
66
+
67
+ ```python
68
+ import torch
69
+ from top import SparseMLAKernel
70
+
71
+ batch_size = 1
72
+ seq_len = 1024
73
+ seq_len_kv = 2048
74
+ q_start_index_s = 1024
75
+ n_heads = 128
76
+ head_dim = 512
77
+ tail_dim = 64
78
+ topk = 2048
79
+ kv_stride = 1
80
+ kv_group = 1
81
+ sm_scale = None
82
+
83
+ sparse_mla = SparseMLAKernel(
84
+ batch=batch_size,
85
+ seq_len=seq_len,
86
+ seq_len_kv=seq_len_kv,
87
+ q_start_index_s=q_start_index_s,
88
+ heads=n_heads,
89
+ dim=head_dim,
90
+ tail_dim=tail_dim,
91
+ topk=topk,
92
+ kv_stride=kv_stride,
93
+ kv_group=kv_group,
94
+ sm_scale=sm_scale,
95
+ is_casual=True,
96
+ dtype=torch.bfloat16,
97
+ device='cuda',
98
+ )
99
+
100
+ # Evaluate the Sparse MLA kernel performance
101
+ sparse_mla.check()
102
+ latency = sparse_mla.profile()
103
+ print(f"Latency: {latency:.4f} ms")
104
+ print(f'fwd tflops = ',
105
+ (batch_size * seq_len * (head_dim + tail_dim + head_dim) * topk * 2 * n_heads) / (latency * 1e-3) / 1e12)
106
+ ```
107
+
108
+ ### MLA
109
+
110
+ ```python
111
+ import torch
112
+ import top
113
+ from top import MLAKernel
114
+
115
+ device = "cuda"
116
+ dtype = torch.float16
117
+
118
+ batch = 128
119
+ heads = 64
120
+ kv_heads = 1
121
+ kv_ctx = 8192
122
+ dim = 512
123
+ pe_dim = 64
124
+
125
+ # Query input: [batch, heads, dim]
126
+ q = torch.randn(batch, heads, dim, device=device, dtype=dtype)
127
+
128
+ # Query positional encoding: [batch, heads, pe_dim]
129
+ q_pe = torch.randn(batch, heads, pe_dim, device=device, dtype=dtype)
130
+
131
+ # KV cache input: [batch, kv_ctx, kv_heads, dim]
132
+ kv = torch.randn(batch, kv_ctx, kv_heads, dim, device=device, dtype=dtype)
133
+
134
+ # KV positional encoding: [batch, kv_ctx, kv_heads, pe_dim]
135
+ k_pe = torch.randn(batch, kv_ctx, kv_heads, pe_dim, device=device, dtype=dtype)
136
+
137
+ # Use MLA kernel
138
+ block_N = 64
139
+ block_H = 64
140
+ num_split = 1
141
+
142
+ mla = MLAKernel(batch, heads, kv_heads, kv_ctx, dim, pe_dim, block_N, block_H, num_split)
143
+
144
+ out = mla(q, q_pe, kv, k_pe)
145
+ ```
146
+
147
+ ## Acknowledgments
@@ -0,0 +1,119 @@
1
+ # TileOPs (TOP)
2
+
3
+ **TileOPs (TOP)** is a high-performance machine learning operator collections built on top of [TileLang](https://github.com/tile-ai/tilelang). It offers efficient, modular, and composable implementations optimized for AI workloads.
4
+
5
+ Note: TileOPs is still under rapid development.
6
+
7
+ ---
8
+
9
+ ![DeepSeek-V3.2-Exp DeepSeek Sparse Attention (DSA) performance on H800 SXM](https://raw.githubusercontent.com/tile-ai/TileOPs/main/docs/figures/sparse_mla_perf.png)
10
+ *DeepSeek-V3.2-Exp DeepSeek Sparse Attention (DSA) performance on H800 SXM*
11
+
12
+ ## 📦 Installation
13
+
14
+ ### Requirements
15
+
16
+ - Python 3.8+
17
+ - PyTorch >= 2.1
18
+ - GLIBCXX_3.4.32
19
+ - [TileLang](https://github.com/tile-ai/tilelang)
20
+
21
+ ### Method 1: Install with Pip
22
+
23
+ ```bash
24
+ pip install tileops
25
+ ```
26
+
27
+ ### Method 2: Install from source (editable mode for development)
28
+
29
+ ```bash
30
+ git clone https://github.com/tile-ai/TileOPs
31
+ cd TileOPs
32
+ pip install -e '.[dev]' -v # remove -e option if you don't want to install in editable mode, -v for verbose output
33
+ ```
34
+
35
+ ## 🚀 Quick Usage
36
+
37
+ ### Sparse MLA
38
+
39
+ ```python
40
+ import torch
41
+ from top import SparseMLAKernel
42
+
43
+ batch_size = 1
44
+ seq_len = 1024
45
+ seq_len_kv = 2048
46
+ q_start_index_s = 1024
47
+ n_heads = 128
48
+ head_dim = 512
49
+ tail_dim = 64
50
+ topk = 2048
51
+ kv_stride = 1
52
+ kv_group = 1
53
+ sm_scale = None
54
+
55
+ sparse_mla = SparseMLAKernel(
56
+ batch=batch_size,
57
+ seq_len=seq_len,
58
+ seq_len_kv=seq_len_kv,
59
+ q_start_index_s=q_start_index_s,
60
+ heads=n_heads,
61
+ dim=head_dim,
62
+ tail_dim=tail_dim,
63
+ topk=topk,
64
+ kv_stride=kv_stride,
65
+ kv_group=kv_group,
66
+ sm_scale=sm_scale,
67
+ is_casual=True,
68
+ dtype=torch.bfloat16,
69
+ device='cuda',
70
+ )
71
+
72
+ # Evaluate the Sparse MLA kernel performance
73
+ sparse_mla.check()
74
+ latency = sparse_mla.profile()
75
+ print(f"Latency: {latency:.4f} ms")
76
+ print(f'fwd tflops = ',
77
+ (batch_size * seq_len * (head_dim + tail_dim + head_dim) * topk * 2 * n_heads) / (latency * 1e-3) / 1e12)
78
+ ```
79
+
80
+ ### MLA
81
+
82
+ ```python
83
+ import torch
84
+ import top
85
+ from top import MLAKernel
86
+
87
+ device = "cuda"
88
+ dtype = torch.float16
89
+
90
+ batch = 128
91
+ heads = 64
92
+ kv_heads = 1
93
+ kv_ctx = 8192
94
+ dim = 512
95
+ pe_dim = 64
96
+
97
+ # Query input: [batch, heads, dim]
98
+ q = torch.randn(batch, heads, dim, device=device, dtype=dtype)
99
+
100
+ # Query positional encoding: [batch, heads, pe_dim]
101
+ q_pe = torch.randn(batch, heads, pe_dim, device=device, dtype=dtype)
102
+
103
+ # KV cache input: [batch, kv_ctx, kv_heads, dim]
104
+ kv = torch.randn(batch, kv_ctx, kv_heads, dim, device=device, dtype=dtype)
105
+
106
+ # KV positional encoding: [batch, kv_ctx, kv_heads, pe_dim]
107
+ k_pe = torch.randn(batch, kv_ctx, kv_heads, pe_dim, device=device, dtype=dtype)
108
+
109
+ # Use MLA kernel
110
+ block_N = 64
111
+ block_H = 64
112
+ num_split = 1
113
+
114
+ mla = MLAKernel(batch, heads, kv_heads, kv_ctx, dim, pe_dim, block_N, block_H, num_split)
115
+
116
+ out = mla(q, q_pe, kv, k_pe)
117
+ ```
118
+
119
+ ## Acknowledgments
@@ -1,21 +1,42 @@
1
1
  [project]
2
2
  name = "tileops"
3
- version = "0.0.1.dev0"
3
+ version = "0.0.1.dev2"
4
4
  description = "TileOPs kernels for efficient inference"
5
- authors = [{ name="Tile-AI" }]
6
5
  readme = "README.md"
6
+ authors = [{ name="Tile-AI"}]
7
+ dependencies = [
8
+ "torch>=2.1.0",
9
+ "numpy>=1.23.5",
10
+ "einops>=0.6.0",
11
+ "Cython",
12
+ "tqdm>=4.62.3",
13
+ "typing_extensions>=4.10.0",
14
+ "cloudpickle",
15
+ "ml_dtypes",
16
+ "psutil",
17
+ "tilelang>=0.1.6.post1"
18
+ ]
7
19
  requires-python = ">=3.8"
8
20
  license = "MIT"
9
- dependencies = ["torch", "tilelang"]
10
- classifiers = [
11
- "Development Status :: 3 - Alpha",
12
- "Programming Language :: Python :: 3"
21
+
22
+ [project.optional-dependencies]
23
+ dev = [
24
+ "yapf==0.40.2",
25
+ "toml==0.10.2",
26
+ "tomli==2.0.1",
27
+ "ruff==0.6.5",
28
+ "codespell==2.3.0",
29
+ "clang-format==15.0.7",
13
30
  ]
14
31
 
15
32
  [build-system]
16
- requires = ["setuptools", "wheel"]
33
+ requires = ["setuptools>=68", "wheel", "build"]
17
34
  build-backend = "setuptools.build_meta"
18
35
 
36
+ [tool.setuptools.packages.find]
37
+ where = ["."]
38
+ exclude = ["3rdparty", "3rdparty.*", "tests", "tests.*"]
39
+
19
40
  [tool.yapf]
20
41
  based_on_style = "yapf"
21
42
  column_limit = 100
@@ -30,13 +51,6 @@ skip = [
30
51
  ".venv"
31
52
  ]
32
53
 
33
- [tool.setuptools]
34
- packages = ["top"]
35
-
36
- [project.urls]
37
- "Homepage" = "https://github.com/tile-ai/tileops"
38
- "Bug Tracker" = "https://github.com/tile-ai/tileops/issues"
39
-
40
54
  [tool.ruff.lint]
41
55
  select = [
42
56
  # pycodestyle
@@ -0,0 +1,44 @@
1
+ import os
2
+ import sys
3
+ from setuptools import setup
4
+
5
+
6
+ # Control whether to use custom installation logic
7
+ # Custom installation is enabled only when:
8
+ # 1. TILEOPS_DEV_INSTALL environment variable is set to "1"
9
+ # 2. The current command is not for building distribution packages (bdist_wheel, sdist)
10
+ # This ensures that custom installation logic is skipped during package building
11
+ USE_CUSTOM_INSTALL = (
12
+ os.environ.get("TILEOPS_DEV_INSTALL") == "1"
13
+ and not any(cmd in sys.argv for cmd in ["bdist_wheel", "sdist"])
14
+ )
15
+
16
+ cmdclass = {}
17
+
18
+ if USE_CUSTOM_INSTALL:
19
+ from setuptools.command.install import install
20
+ import subprocess
21
+ class CustomInstall(install):
22
+
23
+ def run(self):
24
+ # Step 1: Install system dependencies (optional, warning: requires sudo)
25
+ print("Installing system dependencies for TileLang...")
26
+ subprocess.run(["sudo", "apt-get", "update"])
27
+ subprocess.run([
28
+ "sudo", "apt-get", "install", "-y", "python3-setuptools", "gcc",
29
+ "libtinfo-dev", "zlib1g-dev", "build-essential", "cmake",
30
+ "libedit-dev", "libxml2-dev"
31
+ ])
32
+
33
+ # Step 2: Install TileLang via pip from local path
34
+ print("Installing TileLang from submodule...")
35
+ subprocess.run(["pip", "install", "-e", "3rdparty/tilelang"])
36
+
37
+ # Continue normal installation
38
+ install.run(self)
39
+
40
+ cmdclass = {"install": CustomInstall}
41
+
42
+ setup(name = "tileops",
43
+ version = "0.0.1.dev1",
44
+ cmdclass=cmdclass)
@@ -1,6 +1,3 @@
1
- # Copyright (c) Tile-AI.
2
- # Licensed under the MIT License.
3
-
4
1
  import argparse
5
2
  import torch
6
3
  from top import BlockSparseAttentionKernel
@@ -42,13 +39,23 @@ def main():
42
39
  low=0, high=2, size=(BATCH, H, M_BLOCKS, N_BLOCKS), dtype=torch.bool, device="cuda")
43
40
 
44
41
  attention = BlockSparseAttentionKernel(
45
- BATCH, H, N_CTX, D_HEAD_QK, D_HEAD_V, BLOCK_M, BLOCK_N, causal=causal, groups=groups)
46
-
47
- o = attention.backward(Q, K, V, dO, block_mask)
42
+ BATCH,
43
+ H,
44
+ N_CTX,
45
+ D_HEAD_QK,
46
+ D_HEAD_V,
47
+ BLOCK_M,
48
+ BLOCK_N,
49
+ causal=causal,
50
+ block_mask=block_mask,
51
+ groups=groups,
52
+ fwd_tune=True,
53
+ bwd_tune=True)
54
+
55
+ o = attention.backward(Q, K, V, dO)
48
56
  print(o)
49
57
 
50
- attention.check(Q, K, V, dO, block_mask)
51
- attention.profile(Q, K, V, dO, block_mask)
58
+ attention.profile()
52
59
 
53
60
 
54
61
  if __name__ == "__main__":
@@ -1,6 +1,3 @@
1
- # Copyright (c) Tile-AI.
2
- # Licensed under the MIT License.
3
-
4
1
  import argparse
5
2
  import torch
6
3
  from top import GQAKernel
@@ -18,8 +15,10 @@ def main():
18
15
  args = parser.parse_args()
19
16
  BATCH, H, N_CTX, D_HEAD_QK, D_HEAD_V, groups, causal = args.batch, args.h, args.n_ctx, args.d_head_qk, args.d_head_v, args.groups, args.causal
20
17
 
21
- BLOCK_M = 128
22
- BLOCK_N = 64
18
+ fwd_BLOCK_M = 128
19
+ fwd_BLOCK_N = 32
20
+ bwd_BLOCK_M = 64
21
+ bwd_BLOCK_N = 32
23
22
  # q = torch.randn(batch, heads, dim, device='cuda', dtype=torch.float16)
24
23
  Q = (
25
24
  torch.empty(BATCH, N_CTX, H, D_HEAD_QK, dtype=torch.half,
@@ -37,11 +36,20 @@ def main():
37
36
  device="cuda").normal_().requires_grad_())
38
37
 
39
38
  gqa = GQAKernel(
40
- BATCH, H, N_CTX, D_HEAD_QK, D_HEAD_V, BLOCK_M, BLOCK_N, causal=causal, groups=groups)
41
- o = gqa.backward(Q, K, V, dO)
42
- print(o)
43
- latency = gqa.profile()
44
- print(f"Latency: {latency:.4f} ms")
39
+ BATCH,
40
+ H,
41
+ N_CTX,
42
+ D_HEAD_QK,
43
+ D_HEAD_V,
44
+ fwd_BLOCK_M,
45
+ fwd_BLOCK_N,
46
+ bwd_BLOCK_M,
47
+ bwd_BLOCK_N,
48
+ fwd_tune=True,
49
+ bwd_tune=True,
50
+ causal=causal,
51
+ groups=groups)
52
+ gqa.profile(Q, K, V, dO)
45
53
  gqa.check(Q, K, V, dO)
46
54
 
47
55
 
@@ -1,6 +1,3 @@
1
- # Copyright (c) Tile-AI.
2
- # Licensed under the MIT License.
3
-
4
1
  import argparse
5
2
  from top import LinearAttentionFusedChunkKernel
6
3
 
@@ -1,6 +1,3 @@
1
- # Copyright (c) Tile-AI.
2
- # Licensed under the MIT License.
3
-
4
1
  import argparse
5
2
  from top import LinearAttentionFusedRecurrentKernel
6
3
 
@@ -1,6 +1,3 @@
1
- # Copyright (c) Tile-AI.
2
- # Licensed under the MIT License.
3
-
4
1
  import argparse
5
2
  import torch
6
3
  from top import MambaChunkScanKernel
@@ -1,6 +1,3 @@
1
- # Copyright (c) Tile-AI.
2
- # Licensed under the MIT License.
3
-
4
1
  import argparse
5
2
  import torch
6
3
  from top import MambaChunkStateKernel
@@ -1,12 +1,9 @@
1
- # Copyright (c) Tile-AI.
2
- # Licensed under the MIT License.
3
-
4
1
  import argparse
5
2
  from top import MHAKernel
6
3
 
7
4
 
8
- def test_mha_kernel(B, S, H, D, causal):
9
- kernel = MHAKernel(B, H, S, D, causal)
5
+ def test_mha_kernel(B, S, H, D, causal, tune):
6
+ kernel = MHAKernel(B, H, S, D, causal, fwd_tune=tune, bwd_tune=tune)
10
7
  kernel.check()
11
8
  kernel.profile()
12
9
 
@@ -18,7 +15,8 @@ if __name__ == "__main__":
18
15
  parser.add_argument('--heads', type=int, default=32, help='num heads')
19
16
  parser.add_argument('--dim', type=int, default=64, help='head dim')
20
17
  parser.add_argument('--causal', action='store_true', default=False, help='causal attention')
18
+ parser.add_argument('--tune', action='store_true', default=True, help='tune the kernel')
21
19
  args = parser.parse_args()
22
- B, S, H, D, causal = args.batch, args.seq_len, args.heads, args.dim, args.causal
20
+ B, S, H, D, causal, tune = args.batch, args.seq_len, args.heads, args.dim, args.causal, args.tune
23
21
 
24
- test_mha_kernel(B, S, H, D, causal)
22
+ test_mha_kernel(B, S, H, D, causal, tune)
@@ -1,12 +1,9 @@
1
- # Copyright (c) Tile-AI.
2
- # Licensed under the MIT License.
3
-
4
1
  import argparse
5
2
  from top import MHADecodeKernel
6
3
 
7
4
 
8
- def test_mha_decode_kernel(B, S, H, D):
9
- kernel = MHADecodeKernel(B, H, S, D)
5
+ def test_mha_decode_kernel(B, S, H, D, tune):
6
+ kernel = MHADecodeKernel(B, H, S, D, tune=tune)
10
7
  kernel.check()
11
8
  kernel.profile()
12
9
 
@@ -17,7 +14,8 @@ if __name__ == "__main__":
17
14
  parser.add_argument('--seqlen_kv', type=int, default=8192, help='sequence length')
18
15
  parser.add_argument('--heads', type=int, default=32, help='num heads')
19
16
  parser.add_argument('--dim', type=int, default=128, help='head dim')
17
+ parser.add_argument('--tune', action='store_true', default=True, help='tune the kernel')
20
18
  args = parser.parse_args()
21
- B, S, H, D = args.batch, args.seqlen_kv, args.heads, args.dim
19
+ B, S, H, D, tune = args.batch, args.seqlen_kv, args.heads, args.dim, args.tune
22
20
 
23
- test_mha_decode_kernel(B, S, H, D)
21
+ test_mha_decode_kernel(B, S, H, D, tune)
@@ -1,6 +1,3 @@
1
- # Copyright (c) Tile-AI.
2
- # Licensed under the MIT License.
3
-
4
1
  import argparse
5
2
  import torch
6
3
  from top import MLAKernel
@@ -0,0 +1,57 @@
1
+ import argparse
2
+ import torch
3
+ from top import SparseMLAKernel
4
+
5
+
6
+ def test_sparsemla_kernel(batch, seq_len, seq_len_kv, q_start_index_s, heads, dim, tail_dim, topk,
7
+ kv_stride, kv_group, sm_scale, dtype):
8
+ sparse_mla = SparseMLAKernel(
9
+ batch=batch,
10
+ seq_len=seq_len,
11
+ seq_len_kv=seq_len_kv,
12
+ q_start_index_s=q_start_index_s,
13
+ heads=heads,
14
+ dim=dim,
15
+ tail_dim=tail_dim,
16
+ topk=topk,
17
+ kv_stride=kv_stride,
18
+ kv_group=kv_group,
19
+ sm_scale=sm_scale,
20
+ is_casual=True,
21
+ dtype=dtype,
22
+ device='cuda',
23
+ )
24
+ sparse_mla.check()
25
+ latency = sparse_mla.profile()
26
+ print(f"Latency: {latency:.4f} ms")
27
+ print(f'fwd tflops = ',
28
+ (batch * seq_len * (dim + tail_dim + dim) * topk * 2 * heads) / (latency * 1e-3) / 1e12)
29
+
30
+
31
+ if __name__ == "__main__":
32
+ parser = argparse.ArgumentParser()
33
+ parser.add_argument('--batch', type=int, default=1, help='batch size')
34
+ parser.add_argument('--seq_len', type=int, default=1024, help='sequence length')
35
+ parser.add_argument('--seq_len_kv', type=int, default=2048, help='key/value sequence length')
36
+ parser.add_argument('--heads', type=int, default=128, help='num heads')
37
+ parser.add_argument('--dim', type=int, default=512, help='head dim')
38
+ parser.add_argument('--tail_dim', type=int, default=64, help='tail dim')
39
+ parser.add_argument('--topk', type=int, default=2048, help='topk')
40
+ parser.add_argument('--kv_stride', type=int, default=1, help='kv_stride')
41
+ parser.add_argument('--kv_group', type=int, default=1, help='kv_group')
42
+ parser.add_argument('--sm_scale', type=float, default=None, help='softmax scaling factor')
43
+ args = parser.parse_args()
44
+ batch = args.batch
45
+ seq_len = args.seq_len
46
+ seq_len_kv = args.seq_len_kv
47
+ heads = args.heads
48
+ dim = args.dim
49
+ tail_dim = args.tail_dim
50
+ topk = args.topk
51
+ kv_stride = args.kv_stride
52
+ kv_group = args.kv_group
53
+ sm_scale = args.sm_scale
54
+ dtype = torch.bfloat16
55
+
56
+ test_sparsemla_kernel(batch, seq_len, seq_len_kv, 1024, heads, dim, tail_dim, topk, kv_stride,
57
+ kv_group, sm_scale, dtype)