|
27 | 27 | import os |
28 | 28 | import sys |
29 | 29 | import time |
30 | | - |
31 | 30 | _REPO_ROOT = os.path.abspath(os.path.join(os.path.dirname(__file__), "../../..")) |
32 | 31 | if _REPO_ROOT not in sys.path: |
33 | 32 | sys.path.insert(0, _REPO_ROOT) |
34 | 33 |
|
35 | 34 | from kernels.preshuffle_gemm import compile_preshuffle_gemm_a8 |
36 | 35 |
|
37 | 36 |
|
38 | | -def _run_kernel( |
| 37 | +def precompile_to_cache(launch_fn, M: int, N: int, K: int, in_dtype: str): |
| 38 | + """Trigger JIT compilation with CPU dummy tensors and COMPILE_ONLY=1.""" |
| 39 | + import torch |
| 40 | + |
| 41 | + is_low_prec = in_dtype not in ("fp16", "bf16") |
| 42 | + a_dtype = torch.int8 if is_low_prec else (torch.float16 if in_dtype == "fp16" else torch.bfloat16) |
| 43 | + b_elems = (N * K) // 2 if in_dtype == "int4" else N * K |
| 44 | + |
| 45 | + prev = os.environ.get("COMPILE_ONLY") |
| 46 | + os.environ["COMPILE_ONLY"] = "1" |
| 47 | + try: |
| 48 | + launch_fn( |
| 49 | + torch.zeros(M * N, dtype=torch.float16), |
| 50 | + torch.zeros(M * K, dtype=a_dtype), |
| 51 | + torch.zeros(b_elems, dtype=torch.int8 if is_low_prec else a_dtype), |
| 52 | + torch.zeros(M, dtype=torch.float32) if is_low_prec else torch.empty(0, dtype=torch.float32), |
| 53 | + torch.zeros(N, dtype=torch.float32) if is_low_prec else torch.empty(0, dtype=torch.float32), |
| 54 | + M, N, 0, |
| 55 | + ) |
| 56 | + finally: |
| 57 | + if prev is None: |
| 58 | + os.environ.pop("COMPILE_ONLY", None) |
| 59 | + else: |
| 60 | + os.environ["COMPILE_ONLY"] = prev |
| 61 | + |
| 62 | + |
| 63 | +def run_and_verify( |
39 | 64 | launch_fn, |
40 | 65 | M: int, |
41 | 66 | N: int, |
@@ -211,12 +236,14 @@ def compile_one_config( |
211 | 236 | in_dtype=in_dtype, |
212 | 237 | lds_stage=lds_stage, |
213 | 238 | ) |
| 239 | + if run_kernel: |
| 240 | + run_and_verify(launch_fn, M, N, K, in_dtype) |
| 241 | + else: |
| 242 | + precompile_to_cache(launch_fn, M, N, K, in_dtype) |
| 243 | + |
214 | 244 | elapsed = time.time() - t0 |
215 | 245 | result["compile_time"] = elapsed |
216 | 246 | print(f" [OK] compile {elapsed:6.1f}s {shape_str}") |
217 | | - |
218 | | - if run_kernel and launch_fn is not None: |
219 | | - _run_kernel(launch_fn, M, N, K, in_dtype) |
220 | 247 | except Exception as e: |
221 | 248 | print(f" [FAIL] compile {shape_str}: {e}") |
222 | 249 |
|
|
0 commit comments