|
| 1 | +import ctypes |
| 2 | +from typing import Any, Dict, List |
| 3 | + |
| 4 | +import torch |
| 5 | +from core.challenge_base import ChallengeBase |
| 6 | + |
| 7 | + |
| 8 | +class Challenge(ChallengeBase): |
| 9 | + def __init__(self): |
| 10 | + super().__init__( |
| 11 | + name="Layer Normalization", |
| 12 | + atol=1e-04, |
| 13 | + rtol=1e-04, |
| 14 | + num_gpus=1, |
| 15 | + access_tier="free", |
| 16 | + ) |
| 17 | + |
| 18 | + def reference_impl( |
| 19 | + self, |
| 20 | + input: torch.Tensor, |
| 21 | + gamma: torch.Tensor, |
| 22 | + beta: torch.Tensor, |
| 23 | + output: torch.Tensor, |
| 24 | + N: int, |
| 25 | + D: int, |
| 26 | + eps: float, |
| 27 | + ): |
| 28 | + assert input.shape == (N, D), f"Expected input.shape=({N}, {D}), got {input.shape}" |
| 29 | + assert output.shape == (N, D), f"Expected output.shape=({N}, {D}), got {output.shape}" |
| 30 | + assert gamma.shape == (D,), f"Expected gamma.shape=({D},), got {gamma.shape}" |
| 31 | + assert beta.shape == (D,), f"Expected beta.shape=({D},), got {beta.shape}" |
| 32 | + assert input.dtype == gamma.dtype == beta.dtype == output.dtype == torch.float32 |
| 33 | + assert input.device.type == "cuda" |
| 34 | + assert gamma.device.type == "cuda" |
| 35 | + assert beta.device.type == "cuda" |
| 36 | + assert output.device.type == "cuda" |
| 37 | + |
| 38 | + mean = input.mean(dim=1, keepdim=True) |
| 39 | + var = input.var(dim=1, keepdim=True, unbiased=False) |
| 40 | + normalized = (input - mean) / torch.sqrt(var + eps) |
| 41 | + output.copy_(gamma * normalized + beta) |
| 42 | + |
| 43 | + def get_solve_signature(self) -> Dict[str, tuple]: |
| 44 | + return { |
| 45 | + "input": (ctypes.POINTER(ctypes.c_float), "in"), |
| 46 | + "gamma": (ctypes.POINTER(ctypes.c_float), "in"), |
| 47 | + "beta": (ctypes.POINTER(ctypes.c_float), "in"), |
| 48 | + "output": (ctypes.POINTER(ctypes.c_float), "out"), |
| 49 | + "N": (ctypes.c_int, "in"), |
| 50 | + "D": (ctypes.c_int, "in"), |
| 51 | + "eps": (ctypes.c_float, "in"), |
| 52 | + } |
| 53 | + |
| 54 | + def generate_example_test(self) -> Dict[str, Any]: |
| 55 | + dtype = torch.float32 |
| 56 | + N, D = 2, 4 |
| 57 | + input = torch.tensor( |
| 58 | + [[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]], device="cuda", dtype=dtype |
| 59 | + ) |
| 60 | + gamma = torch.tensor([1.0, 1.0, 1.0, 1.0], device="cuda", dtype=dtype) |
| 61 | + beta = torch.tensor([0.0, 0.0, 0.0, 0.0], device="cuda", dtype=dtype) |
| 62 | + output = torch.empty((N, D), device="cuda", dtype=dtype) |
| 63 | + return { |
| 64 | + "input": input, |
| 65 | + "gamma": gamma, |
| 66 | + "beta": beta, |
| 67 | + "output": output, |
| 68 | + "N": N, |
| 69 | + "D": D, |
| 70 | + "eps": 1e-5, |
| 71 | + } |
| 72 | + |
| 73 | + def generate_functional_test(self) -> List[Dict[str, Any]]: |
| 74 | + dtype = torch.float32 |
| 75 | + tests = [] |
| 76 | + |
| 77 | + # single_sample_small_d |
| 78 | + N, D = 1, 4 |
| 79 | + tests.append( |
| 80 | + { |
| 81 | + "input": torch.tensor([[1.0, 2.0, 3.0, 4.0]], device="cuda", dtype=dtype), |
| 82 | + "gamma": torch.ones(D, device="cuda", dtype=dtype), |
| 83 | + "beta": torch.zeros(D, device="cuda", dtype=dtype), |
| 84 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 85 | + "N": N, |
| 86 | + "D": D, |
| 87 | + "eps": 1e-5, |
| 88 | + } |
| 89 | + ) |
| 90 | + |
| 91 | + # single_sample_single_feature — all same value; var=0, norm output = beta |
| 92 | + N, D = 1, 1 |
| 93 | + tests.append( |
| 94 | + { |
| 95 | + "input": torch.tensor([[3.0]], device="cuda", dtype=dtype), |
| 96 | + "gamma": torch.tensor([2.0], device="cuda", dtype=dtype), |
| 97 | + "beta": torch.tensor([1.0], device="cuda", dtype=dtype), |
| 98 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 99 | + "N": N, |
| 100 | + "D": D, |
| 101 | + "eps": 1e-5, |
| 102 | + } |
| 103 | + ) |
| 104 | + |
| 105 | + # all_zeros_input |
| 106 | + N, D = 4, 8 |
| 107 | + tests.append( |
| 108 | + { |
| 109 | + "input": torch.zeros((N, D), device="cuda", dtype=dtype), |
| 110 | + "gamma": torch.ones(D, device="cuda", dtype=dtype), |
| 111 | + "beta": torch.zeros(D, device="cuda", dtype=dtype), |
| 112 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 113 | + "N": N, |
| 114 | + "D": D, |
| 115 | + "eps": 1e-5, |
| 116 | + } |
| 117 | + ) |
| 118 | + |
| 119 | + # negative_numbers |
| 120 | + N, D = 2, 4 |
| 121 | + tests.append( |
| 122 | + { |
| 123 | + "input": torch.tensor( |
| 124 | + [[-1.0, -2.0, -3.0, -4.0], [-5.0, -6.0, -7.0, -8.0]], |
| 125 | + device="cuda", |
| 126 | + dtype=dtype, |
| 127 | + ), |
| 128 | + "gamma": torch.ones(D, device="cuda", dtype=dtype), |
| 129 | + "beta": torch.zeros(D, device="cuda", dtype=dtype), |
| 130 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 131 | + "N": N, |
| 132 | + "D": D, |
| 133 | + "eps": 1e-5, |
| 134 | + } |
| 135 | + ) |
| 136 | + |
| 137 | + # different_gamma_beta |
| 138 | + N, D = 2, 4 |
| 139 | + tests.append( |
| 140 | + { |
| 141 | + "input": torch.tensor( |
| 142 | + [[0.0, 1.0, 2.0, 3.0], [-3.0, -1.0, 1.0, 3.0]], |
| 143 | + device="cuda", |
| 144 | + dtype=dtype, |
| 145 | + ), |
| 146 | + "gamma": torch.tensor([2.0, 0.5, 1.0, 3.0], device="cuda", dtype=dtype), |
| 147 | + "beta": torch.tensor([1.0, -1.0, 0.0, 0.5], device="cuda", dtype=dtype), |
| 148 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 149 | + "N": N, |
| 150 | + "D": D, |
| 151 | + "eps": 1e-5, |
| 152 | + } |
| 153 | + ) |
| 154 | + |
| 155 | + # power_of_2_medium |
| 156 | + N, D = 16, 64 |
| 157 | + tests.append( |
| 158 | + { |
| 159 | + "input": torch.empty((N, D), device="cuda", dtype=dtype).uniform_(-5.0, 5.0), |
| 160 | + "gamma": torch.empty(D, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 161 | + "beta": torch.empty(D, device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 162 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 163 | + "N": N, |
| 164 | + "D": D, |
| 165 | + "eps": 1e-5, |
| 166 | + } |
| 167 | + ) |
| 168 | + |
| 169 | + # power_of_2_large_d |
| 170 | + N, D = 32, 512 |
| 171 | + tests.append( |
| 172 | + { |
| 173 | + "input": torch.empty((N, D), device="cuda", dtype=dtype).uniform_(-10.0, 10.0), |
| 174 | + "gamma": torch.empty(D, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 175 | + "beta": torch.empty(D, device="cuda", dtype=dtype).uniform_(-2.0, 2.0), |
| 176 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 177 | + "N": N, |
| 178 | + "D": D, |
| 179 | + "eps": 1e-5, |
| 180 | + } |
| 181 | + ) |
| 182 | + |
| 183 | + # non_power_of_2 |
| 184 | + N, D = 30, 100 |
| 185 | + tests.append( |
| 186 | + { |
| 187 | + "input": torch.empty((N, D), device="cuda", dtype=dtype).uniform_(-3.0, 3.0), |
| 188 | + "gamma": torch.empty(D, device="cuda", dtype=dtype).uniform_(0.1, 3.0), |
| 189 | + "beta": torch.empty(D, device="cuda", dtype=dtype).uniform_(-5.0, 5.0), |
| 190 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 191 | + "N": N, |
| 192 | + "D": D, |
| 193 | + "eps": 1e-5, |
| 194 | + } |
| 195 | + ) |
| 196 | + |
| 197 | + # non_power_of_2_large |
| 198 | + N, D = 255, 300 |
| 199 | + tests.append( |
| 200 | + { |
| 201 | + "input": torch.empty((N, D), device="cuda", dtype=dtype).uniform_(-50.0, 50.0), |
| 202 | + "gamma": torch.empty(D, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 203 | + "beta": torch.empty(D, device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 204 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 205 | + "N": N, |
| 206 | + "D": D, |
| 207 | + "eps": 1e-5, |
| 208 | + } |
| 209 | + ) |
| 210 | + |
| 211 | + # realistic_transformer_size |
| 212 | + N, D = 1024, 768 |
| 213 | + tests.append( |
| 214 | + { |
| 215 | + "input": torch.empty((N, D), device="cuda", dtype=dtype).uniform_(-10.0, 10.0), |
| 216 | + "gamma": torch.empty(D, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 217 | + "beta": torch.empty(D, device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 218 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 219 | + "N": N, |
| 220 | + "D": D, |
| 221 | + "eps": 1e-5, |
| 222 | + } |
| 223 | + ) |
| 224 | + |
| 225 | + return tests |
| 226 | + |
| 227 | + def generate_performance_test(self) -> Dict[str, Any]: |
| 228 | + dtype = torch.float32 |
| 229 | + N, D = 8192, 4096 |
| 230 | + return { |
| 231 | + "input": torch.empty((N, D), device="cuda", dtype=dtype).uniform_(-10.0, 10.0), |
| 232 | + "gamma": torch.empty(D, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 233 | + "beta": torch.empty(D, device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 234 | + "output": torch.empty((N, D), device="cuda", dtype=dtype), |
| 235 | + "N": N, |
| 236 | + "D": D, |
| 237 | + "eps": 1e-5, |
| 238 | + } |
0 commit comments