|
| 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", atol=1e-04, rtol=1e-04, num_gpus=1, access_tier="free" |
| 12 | + ) |
| 13 | + |
| 14 | + def reference_impl( |
| 15 | + self, |
| 16 | + input: torch.Tensor, |
| 17 | + weight: torch.Tensor, |
| 18 | + bias: torch.Tensor, |
| 19 | + output: torch.Tensor, |
| 20 | + N: int, |
| 21 | + C: int, |
| 22 | + eps: float, |
| 23 | + ): |
| 24 | + assert input.shape == output.shape == (N, C) |
| 25 | + assert weight.shape == bias.shape == (C,) |
| 26 | + assert input.dtype == weight.dtype == bias.dtype == output.dtype |
| 27 | + assert input.device == weight.device == bias.device == output.device |
| 28 | + assert str(input.device).startswith("cuda") |
| 29 | + |
| 30 | + mean = input.mean(dim=1, keepdim=True) |
| 31 | + var = input.var(dim=1, keepdim=True, unbiased=False) |
| 32 | + normalized = (input - mean) / torch.sqrt(var + eps) |
| 33 | + output.copy_(weight * normalized + bias) |
| 34 | + |
| 35 | + def get_solve_signature(self) -> Dict[str, tuple]: |
| 36 | + return { |
| 37 | + "input": (ctypes.POINTER(ctypes.c_float), "in"), |
| 38 | + "weight": (ctypes.POINTER(ctypes.c_float), "in"), |
| 39 | + "bias": (ctypes.POINTER(ctypes.c_float), "in"), |
| 40 | + "output": (ctypes.POINTER(ctypes.c_float), "out"), |
| 41 | + "N": (ctypes.c_int, "in"), |
| 42 | + "C": (ctypes.c_int, "in"), |
| 43 | + "eps": (ctypes.c_float, "in"), |
| 44 | + } |
| 45 | + |
| 46 | + def generate_example_test(self) -> Dict[str, Any]: |
| 47 | + dtype = torch.float32 |
| 48 | + N, C = 2, 4 |
| 49 | + input = torch.tensor( |
| 50 | + [[1.0, 2.0, 3.0, 4.0], [-1.0, 0.0, 0.0, 1.0]], device="cuda", dtype=dtype |
| 51 | + ) |
| 52 | + weight = torch.ones(C, device="cuda", dtype=dtype) |
| 53 | + bias = torch.zeros(C, device="cuda", dtype=dtype) |
| 54 | + output = torch.empty((N, C), device="cuda", dtype=dtype) |
| 55 | + eps = 1e-5 |
| 56 | + return { |
| 57 | + "input": input, |
| 58 | + "weight": weight, |
| 59 | + "bias": bias, |
| 60 | + "output": output, |
| 61 | + "N": N, |
| 62 | + "C": C, |
| 63 | + "eps": eps, |
| 64 | + } |
| 65 | + |
| 66 | + def generate_functional_test(self) -> List[Dict[str, Any]]: |
| 67 | + dtype = torch.float32 |
| 68 | + tests = [] |
| 69 | + |
| 70 | + # edge: single element per row |
| 71 | + N, C = 1, 1 |
| 72 | + tests.append( |
| 73 | + { |
| 74 | + "input": torch.tensor([[3.0]], device="cuda", dtype=dtype), |
| 75 | + "weight": torch.tensor([1.0], device="cuda", dtype=dtype), |
| 76 | + "bias": torch.tensor([0.5], device="cuda", dtype=dtype), |
| 77 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 78 | + "N": N, |
| 79 | + "C": C, |
| 80 | + "eps": 1e-5, |
| 81 | + } |
| 82 | + ) |
| 83 | + |
| 84 | + # edge: 2x2, all zeros |
| 85 | + N, C = 2, 2 |
| 86 | + tests.append( |
| 87 | + { |
| 88 | + "input": torch.zeros((N, C), device="cuda", dtype=dtype), |
| 89 | + "weight": torch.ones(C, device="cuda", dtype=dtype), |
| 90 | + "bias": torch.zeros(C, device="cuda", dtype=dtype), |
| 91 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 92 | + "N": N, |
| 93 | + "C": C, |
| 94 | + "eps": 1e-5, |
| 95 | + } |
| 96 | + ) |
| 97 | + |
| 98 | + # edge: 4x4, negative values |
| 99 | + N, C = 4, 4 |
| 100 | + tests.append( |
| 101 | + { |
| 102 | + "input": torch.tensor( |
| 103 | + [ |
| 104 | + [-1.0, -2.0, -3.0, -4.0], |
| 105 | + [1.0, 2.0, 3.0, 4.0], |
| 106 | + [0.0, 0.0, 0.0, 0.0], |
| 107 | + [-2.0, 0.0, 2.0, 4.0], |
| 108 | + ], |
| 109 | + device="cuda", |
| 110 | + dtype=dtype, |
| 111 | + ), |
| 112 | + "weight": torch.tensor([1.0, 2.0, 1.0, 0.5], device="cuda", dtype=dtype), |
| 113 | + "bias": torch.tensor([0.0, 0.0, 1.0, -1.0], device="cuda", dtype=dtype), |
| 114 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 115 | + "N": N, |
| 116 | + "C": C, |
| 117 | + "eps": 1e-5, |
| 118 | + } |
| 119 | + ) |
| 120 | + |
| 121 | + # power-of-2: 8x16 |
| 122 | + N, C = 8, 16 |
| 123 | + tests.append( |
| 124 | + { |
| 125 | + "input": torch.empty((N, C), device="cuda", dtype=dtype).uniform_(-5.0, 5.0), |
| 126 | + "weight": torch.empty(C, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 127 | + "bias": torch.empty(C, device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 128 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 129 | + "N": N, |
| 130 | + "C": C, |
| 131 | + "eps": 1e-5, |
| 132 | + } |
| 133 | + ) |
| 134 | + |
| 135 | + # power-of-2: 32x64 |
| 136 | + N, C = 32, 64 |
| 137 | + tests.append( |
| 138 | + { |
| 139 | + "input": torch.empty((N, C), device="cuda", dtype=dtype).uniform_(-10.0, 10.0), |
| 140 | + "weight": torch.empty(C, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 141 | + "bias": torch.empty(C, device="cuda", dtype=dtype).uniform_(-2.0, 2.0), |
| 142 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 143 | + "N": N, |
| 144 | + "C": C, |
| 145 | + "eps": 1e-5, |
| 146 | + } |
| 147 | + ) |
| 148 | + |
| 149 | + # power-of-2: 128x256 |
| 150 | + N, C = 128, 256 |
| 151 | + tests.append( |
| 152 | + { |
| 153 | + "input": torch.empty((N, C), device="cuda", dtype=dtype).uniform_(-10.0, 10.0), |
| 154 | + "weight": torch.empty(C, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 155 | + "bias": torch.empty(C, device="cuda", dtype=dtype).uniform_(-2.0, 2.0), |
| 156 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 157 | + "N": N, |
| 158 | + "C": C, |
| 159 | + "eps": 1e-5, |
| 160 | + } |
| 161 | + ) |
| 162 | + |
| 163 | + # non-power-of-2: 7x30 |
| 164 | + N, C = 7, 30 |
| 165 | + tests.append( |
| 166 | + { |
| 167 | + "input": torch.empty((N, C), device="cuda", dtype=dtype).uniform_(-5.0, 5.0), |
| 168 | + "weight": torch.ones(C, device="cuda", dtype=dtype), |
| 169 | + "bias": torch.zeros(C, device="cuda", dtype=dtype), |
| 170 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 171 | + "N": N, |
| 172 | + "C": C, |
| 173 | + "eps": 1e-5, |
| 174 | + } |
| 175 | + ) |
| 176 | + |
| 177 | + # non-power-of-2: 15x100 |
| 178 | + N, C = 15, 100 |
| 179 | + tests.append( |
| 180 | + { |
| 181 | + "input": torch.empty((N, C), device="cuda", dtype=dtype).uniform_(-100.0, 100.0), |
| 182 | + "weight": torch.empty(C, device="cuda", dtype=dtype).uniform_(0.1, 3.0), |
| 183 | + "bias": torch.empty(C, device="cuda", dtype=dtype).uniform_(-5.0, 5.0), |
| 184 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 185 | + "N": N, |
| 186 | + "C": C, |
| 187 | + "eps": 1e-5, |
| 188 | + } |
| 189 | + ) |
| 190 | + |
| 191 | + # non-power-of-2: 25x255 |
| 192 | + N, C = 25, 255 |
| 193 | + tests.append( |
| 194 | + { |
| 195 | + "input": torch.empty((N, C), device="cuda", dtype=dtype).uniform_(-10.0, 10.0), |
| 196 | + "weight": torch.empty(C, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 197 | + "bias": torch.empty(C, device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 198 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 199 | + "N": N, |
| 200 | + "C": C, |
| 201 | + "eps": 1e-5, |
| 202 | + } |
| 203 | + ) |
| 204 | + |
| 205 | + # realistic: 512x768 (BERT hidden size) |
| 206 | + N, C = 512, 768 |
| 207 | + tests.append( |
| 208 | + { |
| 209 | + "input": torch.empty((N, C), device="cuda", dtype=dtype).uniform_(-5.0, 5.0), |
| 210 | + "weight": torch.empty(C, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 211 | + "bias": torch.empty(C, device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 212 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 213 | + "N": N, |
| 214 | + "C": C, |
| 215 | + "eps": 1e-5, |
| 216 | + } |
| 217 | + ) |
| 218 | + |
| 219 | + return tests |
| 220 | + |
| 221 | + def generate_performance_test(self) -> Dict[str, Any]: |
| 222 | + dtype = torch.float32 |
| 223 | + N, C = 65536, 512 |
| 224 | + return { |
| 225 | + "input": torch.empty((N, C), device="cuda", dtype=dtype).uniform_(-5.0, 10.0), |
| 226 | + "weight": torch.empty(C, device="cuda", dtype=dtype).uniform_(0.5, 2.0), |
| 227 | + "bias": torch.empty(C, device="cuda", dtype=dtype).uniform_(-1.0, 1.0), |
| 228 | + "output": torch.empty((N, C), device="cuda", dtype=dtype), |
| 229 | + "N": N, |
| 230 | + "C": C, |
| 231 | + "eps": 1e-5, |
| 232 | + } |
0 commit comments