|
| 1 | +import ctypes |
| 2 | +from typing import Any, List, Dict |
| 3 | +import torch |
| 4 | +from core.challenge_base import ChallengeBase |
| 5 | + |
| 6 | +class Challenge(ChallengeBase): |
| 7 | + def __init__(self): |
| 8 | + super().__init__( |
| 9 | + name="Attention with Linear Biases", |
| 10 | + atol=1e-04, |
| 11 | + rtol=1e-04, |
| 12 | + num_gpus=1, |
| 13 | + access_tier="free" |
| 14 | + ) |
| 15 | + |
| 16 | + def reference_impl(self, Q: torch.Tensor, K: torch.Tensor, V: torch.Tensor, output: torch.Tensor, M: int, N: int, d: int, alpha: float): |
| 17 | + assert Q.shape == (M,d) |
| 18 | + assert K.shape == (N,d) |
| 19 | + assert V.shape == (N,d) |
| 20 | + assert output.shape == (M,d) |
| 21 | + |
| 22 | + scale = d ** 0.5 |
| 23 | + attn = torch.matmul(Q, K.t()) / scale |
| 24 | + |
| 25 | + pos_bias = alpha * (torch.arange(M, device=Q.device).unsqueeze(1) - torch.arange(N, device=K.device).unsqueeze(0)) |
| 26 | + attn = attn + pos_bias |
| 27 | + |
| 28 | + attn = torch.softmax(attn, dim=1) # M , N |
| 29 | + torch.matmul(attn, V, out=output) |
| 30 | + |
| 31 | + def get_solve_signature(self) -> Dict[str, Any]: |
| 32 | + return { |
| 33 | + "Q": ctypes.POINTER(ctypes.c_float), |
| 34 | + "K": ctypes.POINTER(ctypes.c_float), |
| 35 | + "V": ctypes.POINTER(ctypes.c_float), |
| 36 | + "output": ctypes.POINTER(ctypes.c_float), |
| 37 | + "M": ctypes.c_int, |
| 38 | + "N": ctypes.c_int, |
| 39 | + "d": ctypes.c_int, |
| 40 | + "alpha": ctypes.c_float, |
| 41 | + } |
| 42 | + |
| 43 | + def generate_example_test(self) -> Dict[str, Any]: |
| 44 | + dtype = torch.float32 |
| 45 | + Q = torch.tensor([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]], device="cuda", dtype=dtype) |
| 46 | + K = torch.tensor([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]], device="cuda", dtype=dtype) |
| 47 | + V = torch.tensor([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0]], device="cuda", dtype=dtype) |
| 48 | + output = torch.empty(2, 4, device="cuda", dtype=dtype) |
| 49 | + return {"Q": Q, "K": K, "V": V, "output": output, "M": 2, "N": 3, "d": 4, "alpha": 0.5} |
| 50 | + |
| 51 | + def generate_functional_test(self) -> List[Dict[str, Any]]: |
| 52 | + dtype = torch.float32 |
| 53 | + tests = [] |
| 54 | + |
| 55 | + # basic_example 1 |
| 56 | + tests.append({ |
| 57 | + "Q": torch.tensor([[1.0, 2.0]], device="cuda", dtype=dtype), |
| 58 | + "K": torch.tensor([[1.0, 0.0],[0.0, 1.0]], device="cuda", dtype=dtype), |
| 59 | + "V": torch.tensor([[3.0, 4.0], [5.0, 6.0]], device="cuda", dtype=dtype), |
| 60 | + "output": torch.empty(1, 2, device="cuda", dtype=dtype), |
| 61 | + "M": 1, "N": 2, "d": 2, "alpha": 0.8 |
| 62 | + }) |
| 63 | + |
| 64 | + # basic_example 2 |
| 65 | + tests.append({ |
| 66 | + "Q": torch.tensor([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0]], device="cuda", dtype=dtype), |
| 67 | + "K": torch.tensor([[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]], device="cuda", dtype=dtype), |
| 68 | + "V": torch.tensor([[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0], [9.0, 10.0, 11.0, 12.0]], device="cuda", dtype=dtype), |
| 69 | + "output": torch.empty(2, 4, device="cuda", dtype=dtype), |
| 70 | + "M": 2, "N": 3, "d": 4, "alpha": 0.5 |
| 71 | + }) |
| 72 | + |
| 73 | + # zero_matrices |
| 74 | + tests.append({ |
| 75 | + "Q": torch.zeros((3, 5), device="cuda", dtype=dtype), |
| 76 | + "K": torch.zeros((3, 5), device="cuda", dtype=dtype), |
| 77 | + "V": torch.zeros((3, 5), device="cuda", dtype=dtype), |
| 78 | + "output": torch.empty(3, 5, device="cuda", dtype=dtype), |
| 79 | + "M": 3, "N": 3, "d": 5, "alpha": 0.5 |
| 80 | + }) |
| 81 | + |
| 82 | + # mixed_values |
| 83 | + tests.append({ |
| 84 | + "Q": torch.tensor([[-1.0, 2.0, -3.0], [4.0, -5.0, 6.0], [-7.0, 8.0, -9.0], [10.0, -11.0, 12.0]], device="cuda", dtype=dtype), |
| 85 | + "K": torch.tensor([[2.0, -1.0, 3.0], [-4.0, 5.0, -6.0], [7.0, -8.0, 9.0], [-10.0, 11.0, -12.0]], device="cuda", dtype=dtype), |
| 86 | + "V": torch.tensor([[1.0, 0.5, -0.5], [-1.0, 2.0, 3.0], [4.0, -2.0, 1.0], [0.0, 1.0, -1.0]], device="cuda", dtype=dtype), |
| 87 | + "output": torch.empty(4, 3, device="cuda", dtype=dtype), |
| 88 | + "M": 4, "N": 4, "d": 3, "alpha": 1.0 |
| 89 | + }) |
| 90 | + |
| 91 | + # large_matrices |
| 92 | + tests.append({ |
| 93 | + "Q": torch.empty((64, 32), device="cuda", dtype=dtype).uniform_(-0.1, 0.1), |
| 94 | + "K": torch.empty((128, 32), device="cuda", dtype=dtype).uniform_(-0.1, 0.1), |
| 95 | + "V": torch.empty((128, 32), device="cuda", dtype=dtype).uniform_(-0.1, 0.1), |
| 96 | + "output": torch.empty(64, 32, device="cuda", dtype=dtype), |
| 97 | + "M": 64, "N": 128, "d": 32, "alpha": -0.76 |
| 98 | + }) |
| 99 | + |
| 100 | + # different alpha |
| 101 | + tests.append({ |
| 102 | + "Q": torch.empty((64, 32), device="cuda", dtype=dtype).uniform_(-1, 1), |
| 103 | + "K": torch.empty((128, 32), device="cuda", dtype=dtype).uniform_(-1, 1), |
| 104 | + "V": torch.empty((128, 32), device="cuda", dtype=dtype).uniform_(-1, 1), |
| 105 | + "output": torch.empty(64, 32, device="cuda", dtype=dtype), |
| 106 | + "M": 64, "N": 128, "d": 32, "alpha": -0.3 |
| 107 | + }) |
| 108 | + |
| 109 | + return tests |
| 110 | + |
| 111 | + def generate_performance_test(self) -> Dict[str, Any]: |
| 112 | + dtype = torch.float32 |
| 113 | + M, N, d = 2048, 2048, 1024 |
| 114 | + Q = torch.empty((M, d), device="cuda", dtype=dtype).uniform_(-0.1, 0.1) |
| 115 | + K = torch.empty((N, d), device="cuda", dtype=dtype).uniform_(-0.1, 0.1) |
| 116 | + V = torch.empty((N,d), device="cuda", dtype=dtype).uniform_(-0.1, 0.1) |
| 117 | + output = torch.empty(M, d, device="cuda", dtype=dtype) |
| 118 | + return {"Q": Q, "K": K, "V": V, "output": output, "M": M, "N": N, "d": d, "alpha": 0.5} |
0 commit comments