Add DG_PRINT_CONFIGS

This commit is contained in:
Chenggang Zhao
2025-05-15 16:36:40 +08:00
parent 816b39053a
commit 4373af2e82
6 changed files with 26 additions and 14 deletions

View File

@@ -123,7 +123,7 @@ The library also provides some environment variables, which may be useful:
- Post optimization - Post optimization
- `DG_JIT_DISABLE_FFMA_INTERLEAVE`: `0` or `1`, disable FFMA-interleaving optimization, `0` by default - `DG_JIT_DISABLE_FFMA_INTERLEAVE`: `0` or `1`, disable FFMA-interleaving optimization, `0` by default
- Heuristic selection - Heuristic selection
- `DG_PRINT_HEURISTIC`: `0` or `1`, print selected configs for each shape, `0` by default - `DG_PRINT_CONFIGS`: `0` or `1`, print selected configs for each shape, `0` by default
- Testing - Testing
- `DG_NSYS_PROFILING`: `0` or `1`, Nsight-system compatible testing, `0` by default - `DG_NSYS_PROFILING`: `0` or `1`, Nsight-system compatible testing, `0` by default

View File

@@ -5,7 +5,7 @@ import re
import subprocess import subprocess
import time import time
import uuid import uuid
from typing import List, Tuple, Type from typing import Any, Dict, List, Tuple, Type
import cuda.bindings import cuda.bindings
import cuda.bindings.nvrtc as nvrtc import cuda.bindings.nvrtc as nvrtc
@@ -128,7 +128,7 @@ class Compiler:
return [get_jit_include_dir()] return [get_jit_include_dir()]
@classmethod @classmethod
def build(cls, name: str, code: str, runtime_cls: Type[Runtime]) -> Runtime: def build(cls, name: str, code: str, runtime_cls: Type[Runtime], kwargs: Dict[str, Any] = None) -> Runtime:
# Compiler flags # Compiler flags
flags = cls.flags() flags = cls.flags()
@@ -140,7 +140,7 @@ class Compiler:
# Check runtime cache or file system hit # Check runtime cache or file system hit
global runtime_cache global runtime_cache
cached_runtime = runtime_cache.get(path, runtime_cls) cached_runtime = runtime_cache.get(path, runtime_cls, name, kwargs)
if cached_runtime is not None: if cached_runtime is not None:
if int(os.getenv('DG_JIT_DEBUG', 0)): if int(os.getenv('DG_JIT_DEBUG', 0)):
print(f'Using cached JIT runtime {name} during build') print(f'Using cached JIT runtime {name} during build')
@@ -166,8 +166,8 @@ class Compiler:
os.replace(tmp_cubin_path, cubin_path) os.replace(tmp_cubin_path, cubin_path)
# Put cache and return # Put cache and return
runtime = runtime_cls(path) runtime = runtime_cache.get(path, runtime_cls, name, kwargs)
runtime_cache[path] = runtime assert runtime is not None
return runtime return runtime
@@ -279,6 +279,6 @@ class NVRTCCompiler(Compiler):
assert nvrtc.nvrtcDestroyProgram(program)[0] == nvrtc.nvrtcResult.NVRTC_SUCCESS, f'Failed to destroy program: {result}' assert nvrtc.nvrtcDestroyProgram(program)[0] == nvrtc.nvrtcResult.NVRTC_SUCCESS, f'Failed to destroy program: {result}'
def build(name: str, code: str, runtime_cls: Type[Runtime]) -> Runtime: def build(name: str, code: str, runtime_cls: Type[Runtime], kwargs: Dict[str, Any] = None) -> Runtime:
compiler_cls = NVRTCCompiler if int(os.getenv('DG_JIT_USE_NVRTC', 0)) else NVCCCompiler compiler_cls = NVRTCCompiler if int(os.getenv('DG_JIT_USE_NVRTC', 0)) else NVCCCompiler
return compiler_cls.build(name, code, runtime_cls=runtime_cls) return compiler_cls.build(name, code, runtime_cls, kwargs)

View File

@@ -1,9 +1,11 @@
import copy
import os import os
import subprocess import subprocess
import time import time
import torch
import cuda.bindings.driver as cbd import cuda.bindings.driver as cbd
from typing import List, Optional, Type from typing import Any, Dict, Optional, Type
from torch.utils.cpp_extension import CUDA_HOME from torch.utils.cpp_extension import CUDA_HOME
@@ -79,13 +81,23 @@ class RuntimeCache:
def __setitem__(self, path: str, runtime: Runtime) -> None: def __setitem__(self, path: str, runtime: Runtime) -> None:
self.cache[path] = runtime self.cache[path] = runtime
def get(self, path: str, runtime_cls: Type[Runtime]) -> Optional[Runtime]: def get(self, path: str, runtime_cls: Type[Runtime],
name: str = '', kwargs: Dict[str, Any] = None) -> Optional[Runtime]:
# In Python runtime # In Python runtime
if path in self.cache: if path in self.cache:
return self.cache[path] return self.cache[path]
# Already compiled # Already compiled
if not int(os.getenv('DG_JIT_DISABLE_CACHE', 0)) and os.path.exists(path) and Runtime.is_path_valid(path): if not int(os.getenv('DG_JIT_DISABLE_CACHE', 0)) and os.path.exists(path) and Runtime.is_path_valid(path):
# Print heuristic for the first time
if name and (int(os.getenv('DG_JIT_DEBUG', 0)) or int(os.getenv('DG_PRINT_CONFIGS', 0))):
simplified_kwargs = dict()
for key, value in kwargs.items():
value = f'torch.Tensor<{value.dtype}>' if isinstance(value, torch.Tensor) else value
value = f'cuda.bindings.driver.CUtensorMap' if isinstance(value, cbd.CUtensorMap) else value
simplified_kwargs[key] = value
print(f'Put kernel {name} with {simplified_kwargs} into runtime cache')
runtime = runtime_cls(path) runtime = runtime_cls(path)
self.cache[path] = runtime self.cache[path] = runtime
return runtime return runtime

View File

@@ -238,5 +238,5 @@ def gemm_fp8_fp8_bf16_nt(lhs: Tuple[torch.Tensor, torch.Tensor],
# Generate, build and run the kernel # Generate, build and run the kernel
code = FP8GemmRuntime.generate(**kwargs) code = FP8GemmRuntime.generate(**kwargs)
runtime = build('gemm_fp8_fp8_bf16_nt', code, FP8GemmRuntime) runtime = build('gemm_fp8_fp8_bf16_nt', code, FP8GemmRuntime, kwargs)
runtime(**kwargs) runtime(**kwargs)

View File

@@ -102,7 +102,7 @@ def m_grouped_gemm_fp8_fp8_bf16_nt_contiguous(lhs: Tuple[torch.Tensor, torch.Ten
# Generate, build and run the kernel # Generate, build and run the kernel
code = FP8GemmRuntime.generate(**kwargs) code = FP8GemmRuntime.generate(**kwargs)
runtime = build('m_grouped_gemm_fp8_fp8_bf16_nt', code, FP8GemmRuntime) runtime = build('m_grouped_gemm_fp8_fp8_bf16_nt', code, FP8GemmRuntime, kwargs)
runtime(**kwargs) runtime(**kwargs)
@@ -201,5 +201,5 @@ def m_grouped_gemm_fp8_fp8_bf16_nt_masked(lhs: Tuple[torch.Tensor, torch.Tensor]
# Generate, build and run the kernel # Generate, build and run the kernel
code = FP8GemmRuntime.generate(**kwargs) code = FP8GemmRuntime.generate(**kwargs)
runtime = build('m_grouped_gemm_fp8_fp8_bf16_nt', code, FP8GemmRuntime) runtime = build('m_grouped_gemm_fp8_fp8_bf16_nt', code, FP8GemmRuntime, kwargs)
runtime(**kwargs) runtime(**kwargs)

View File

@@ -111,7 +111,7 @@ def wgrad_gemm_fp8_fp8_fp32_nt(lhs: Tuple[torch.Tensor, torch.Tensor],
# Generate, build and run the kernel # Generate, build and run the kernel
code = FP8WGradGemmRuntime.generate(**kwargs) code = FP8WGradGemmRuntime.generate(**kwargs)
runtime = build('wgrad_gemm_fp8_fp8_fp32_nt', code, FP8WGradGemmRuntime) runtime = build('wgrad_gemm_fp8_fp8_fp32_nt', code, FP8WGradGemmRuntime, kwargs)
runtime(**kwargs) runtime(**kwargs)