|
| 1 | +import torch |
| 2 | + |
| 3 | +from kernels.benchmark import Benchmark |
| 4 | + |
| 5 | + |
| 6 | +class RMSNormBenchmark(Benchmark): |
| 7 | + seed: int = 42 |
| 8 | + eps: float = 1e-5 |
| 9 | + |
| 10 | + # Workload: small (B=2, S=128, D=768) |
| 11 | + def setup_small(self): |
| 12 | + B, S, D = 2, 128, 768 |
| 13 | + self.x = torch.randn(B, S, D, device="cuda", dtype=torch.float16) |
| 14 | + self.weight = torch.ones(D, device="cuda", dtype=torch.float16) |
| 15 | + self.out = torch.empty_like(self.x) |
| 16 | + self.B, self.S, self.D = B, S, D |
| 17 | + |
| 18 | + def benchmark_small(self): |
| 19 | + self.out = self.kernel.dropout_add_ln_fwd( |
| 20 | + input=self.x.view(-1, self.D), |
| 21 | + gamma=self.weight, |
| 22 | + beta=None, |
| 23 | + rowscale=None, |
| 24 | + colscale=None, |
| 25 | + x0_subset=None, |
| 26 | + z_subset=None, |
| 27 | + dropout_p=0.0, |
| 28 | + epsilon=self.eps, |
| 29 | + rowscale_const=1.0, |
| 30 | + z_numrows=self.S, |
| 31 | + gen=None, |
| 32 | + residual_in_fp32=False, |
| 33 | + is_rms_norm=True, |
| 34 | + )[0].view(self.B, self.S, self.D) |
| 35 | + |
| 36 | + def verify_small(self) -> torch.Tensor: |
| 37 | + var = self.x.pow(2).mean(-1, keepdim=True) |
| 38 | + return (self.x * torch.rsqrt(var + self.eps)) * self.weight |
| 39 | + |
| 40 | + # Workload: medium (B=4, S=512, D=2048) |
| 41 | + def setup_medium(self): |
| 42 | + B, S, D = 4, 512, 2048 |
| 43 | + self.x = torch.randn(B, S, D, device="cuda", dtype=torch.float16) |
| 44 | + self.weight = torch.ones(D, device="cuda", dtype=torch.float16) |
| 45 | + self.out = torch.empty_like(self.x) |
| 46 | + self.B, self.S, self.D = B, S, D |
| 47 | + |
| 48 | + def benchmark_medium(self): |
| 49 | + self.out = self.kernel.dropout_add_ln_fwd( |
| 50 | + input=self.x.view(-1, self.D), |
| 51 | + gamma=self.weight, |
| 52 | + beta=None, |
| 53 | + rowscale=None, |
| 54 | + colscale=None, |
| 55 | + x0_subset=None, |
| 56 | + z_subset=None, |
| 57 | + dropout_p=0.0, |
| 58 | + epsilon=self.eps, |
| 59 | + rowscale_const=1.0, |
| 60 | + z_numrows=self.S, |
| 61 | + gen=None, |
| 62 | + residual_in_fp32=False, |
| 63 | + is_rms_norm=True, |
| 64 | + )[0].view(self.B, self.S, self.D) |
| 65 | + |
| 66 | + def verify_medium(self) -> torch.Tensor: |
| 67 | + var = self.x.pow(2).mean(-1, keepdim=True) |
| 68 | + return (self.x * torch.rsqrt(var + self.eps)) * self.weight |
| 69 | + |
| 70 | + # Workload: large (B=8, S=1024, D=4096) |
| 71 | + def setup_large(self): |
| 72 | + B, S, D = 8, 1024, 4096 |
| 73 | + self.x = torch.randn(B, S, D, device="cuda", dtype=torch.float16) |
| 74 | + self.weight = torch.ones(D, device="cuda", dtype=torch.float16) |
| 75 | + self.out = torch.empty_like(self.x) |
| 76 | + self.B, self.S, self.D = B, S, D |
| 77 | + |
| 78 | + def benchmark_large(self): |
| 79 | + self.out = self.kernel.dropout_add_ln_fwd( |
| 80 | + input=self.x.view(-1, self.D), |
| 81 | + gamma=self.weight, |
| 82 | + beta=None, |
| 83 | + rowscale=None, |
| 84 | + colscale=None, |
| 85 | + x0_subset=None, |
| 86 | + z_subset=None, |
| 87 | + dropout_p=0.0, |
| 88 | + epsilon=self.eps, |
| 89 | + rowscale_const=1.0, |
| 90 | + z_numrows=self.S, |
| 91 | + gen=None, |
| 92 | + residual_in_fp32=False, |
| 93 | + is_rms_norm=True, |
| 94 | + )[0].view(self.B, self.S, self.D) |
| 95 | + |
| 96 | + def verify_large(self) -> torch.Tensor: |
| 97 | + var = self.x.pow(2).mean(-1, keepdim=True) |
| 98 | + return (self.x * torch.rsqrt(var + self.eps)) * self.weight |
| 99 | + |
| 100 | + |
| 101 | +class LayerNormBenchmark(Benchmark): |
| 102 | + seed: int = 42 |
| 103 | + eps: float = 1e-5 |
| 104 | + |
| 105 | + # Workload: small (B=2, S=128, D=768) |
| 106 | + def setup_small(self): |
| 107 | + B, S, D = 2, 128, 768 |
| 108 | + self.x = torch.randn(B, S, D, device="cuda", dtype=torch.float16) |
| 109 | + self.weight = torch.ones(D, device="cuda", dtype=torch.float16) |
| 110 | + self.out = torch.empty_like(self.x) |
| 111 | + self.B, self.S, self.D = B, S, D |
| 112 | + |
| 113 | + def benchmark_small(self): |
| 114 | + self.out = self.kernel.dropout_add_ln_fwd( |
| 115 | + input=self.x.view(-1, self.D), |
| 116 | + gamma=self.weight, |
| 117 | + beta=None, |
| 118 | + rowscale=None, |
| 119 | + colscale=None, |
| 120 | + x0_subset=None, |
| 121 | + z_subset=None, |
| 122 | + dropout_p=0.0, |
| 123 | + epsilon=self.eps, |
| 124 | + rowscale_const=1.0, |
| 125 | + z_numrows=self.S, |
| 126 | + gen=None, |
| 127 | + residual_in_fp32=False, |
| 128 | + is_rms_norm=False, |
| 129 | + )[0].view(self.B, self.S, self.D) |
| 130 | + |
| 131 | + def verify_small(self) -> torch.Tensor: |
| 132 | + return torch.nn.functional.layer_norm( |
| 133 | + self.x, [self.D], self.weight, eps=self.eps |
| 134 | + ) |
| 135 | + |
| 136 | + # Workload: medium (B=4, S=512, D=2048) |
| 137 | + def setup_medium(self): |
| 138 | + B, S, D = 4, 512, 2048 |
| 139 | + self.x = torch.randn(B, S, D, device="cuda", dtype=torch.float16) |
| 140 | + self.weight = torch.ones(D, device="cuda", dtype=torch.float16) |
| 141 | + self.out = torch.empty_like(self.x) |
| 142 | + self.B, self.S, self.D = B, S, D |
| 143 | + |
| 144 | + def benchmark_medium(self): |
| 145 | + self.out = self.kernel.dropout_add_ln_fwd( |
| 146 | + input=self.x.view(-1, self.D), |
| 147 | + gamma=self.weight, |
| 148 | + beta=None, |
| 149 | + rowscale=None, |
| 150 | + colscale=None, |
| 151 | + x0_subset=None, |
| 152 | + z_subset=None, |
| 153 | + dropout_p=0.0, |
| 154 | + epsilon=self.eps, |
| 155 | + rowscale_const=1.0, |
| 156 | + z_numrows=self.S, |
| 157 | + gen=None, |
| 158 | + residual_in_fp32=False, |
| 159 | + is_rms_norm=False, |
| 160 | + )[0].view(self.B, self.S, self.D) |
| 161 | + |
| 162 | + def verify_medium(self) -> torch.Tensor: |
| 163 | + return torch.nn.functional.layer_norm( |
| 164 | + self.x, [self.D], self.weight, eps=self.eps |
| 165 | + ) |
| 166 | + |
| 167 | + # Workload: large (B=8, S=1024, D=4096) |
| 168 | + def setup_large(self): |
| 169 | + B, S, D = 8, 1024, 4096 |
| 170 | + self.x = torch.randn(B, S, D, device="cuda", dtype=torch.float16) |
| 171 | + self.weight = torch.ones(D, device="cuda", dtype=torch.float16) |
| 172 | + self.out = torch.empty_like(self.x) |
| 173 | + self.B, self.S, self.D = B, S, D |
| 174 | + |
| 175 | + def benchmark_large(self): |
| 176 | + self.out = self.kernel.dropout_add_ln_fwd( |
| 177 | + input=self.x.view(-1, self.D), |
| 178 | + gamma=self.weight, |
| 179 | + beta=None, |
| 180 | + rowscale=None, |
| 181 | + colscale=None, |
| 182 | + x0_subset=None, |
| 183 | + z_subset=None, |
| 184 | + dropout_p=0.0, |
| 185 | + epsilon=self.eps, |
| 186 | + rowscale_const=1.0, |
| 187 | + z_numrows=self.S, |
| 188 | + gen=None, |
| 189 | + residual_in_fp32=False, |
| 190 | + is_rms_norm=False, |
| 191 | + )[0].view(self.B, self.S, self.D) |
| 192 | + |
| 193 | + def verify_large(self) -> torch.Tensor: |
| 194 | + return torch.nn.functional.layer_norm( |
| 195 | + self.x, [self.D], self.weight, eps=self.eps |
| 196 | + ) |
0 commit comments