| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import platform |
| from pathlib import Path |
|
|
| import kernels |
| import torch |
| import torch.nn.functional as F |
|
|
|
|
| def main() -> None: |
| |
| |
| |
| repo = Path(__file__).parent |
| build_dir = repo / "result" if (repo / "result").exists() else repo / "build" |
| kernel = kernels.get_local_kernel(build_dir, "cuda") |
|
|
| |
| if platform.system() == "Darwin": |
| device = torch.device("mps") |
| elif hasattr(torch, "xpu") and torch.xpu.is_available(): |
| device = torch.device("xpu") |
| elif torch.version.cuda is not None and torch.cuda.is_available(): |
| device = torch.device("cuda") |
| else: |
| device = torch.device("cpu") |
|
|
| print(f"Using device: {device}") |
|
|
| |
| B, H, S, D = 2, 8, 512, 64 |
| q = torch.randn(B, H, S, D, device=device, dtype=torch.float16) |
| k = torch.randn(B, H, S, D, device=device, dtype=torch.float16) |
| v = torch.randn(B, H, S, D, device=device, dtype=torch.float16) |
|
|
| |
| result = kernel.attention(q, k, v) |
| print(f"Output shape: {tuple(result.shape)}") |
|
|
| |
| expected = F.scaled_dot_product_attention(q, k, v) |
| assert torch.allclose(result, expected, atol=5e-2, rtol=2e-2), ( |
| "Kernel output doesn't match SDPA!" |
| ) |
| print("Success!") |
|
|
|
|
| |
| |
| |
| |
| |
| if __name__ == "__main__": |
| main() |
|
|