|
| 1 | +# RUN: %PYTHON %S/kernel_bench.py --input %s --dump-kernel xegpu-wg | FileCheck %s --implicit-check-not=memref.alloc --implicit-check-not=memref.copy --implicit-check-not=gpu.alloc |
| 2 | +# REQUIRES: torch, torch_mlir |
| 3 | + |
| 4 | +"""Llama 3.2 1B post-attention RMSNorm, SwiGLU FFN and residual connection. |
| 5 | +
|
| 6 | +One normalization result feeds gate and up projections. Four kernels materialize |
| 7 | +normalized input, activated gate and gated activations. The residual add is |
| 8 | +fused into down projection's epilogue, using the original unnormalized input. |
| 9 | +""" |
| 10 | + |
| 11 | +import torch |
| 12 | +import torch.nn as nn |
| 13 | +import torch.nn.functional as F |
| 14 | + |
| 15 | + |
| 16 | +batch_size = 4 |
| 17 | +n_ctx = 512 |
| 18 | +dim = 2048 |
| 19 | +hidden_dim = 8192 |
| 20 | + |
| 21 | + |
| 22 | +class Model(nn.Module): |
| 23 | + def __init__(self): |
| 24 | + super().__init__() |
| 25 | + self.norm_weight = nn.Parameter(torch.ones(dim)) |
| 26 | + self.gate_proj = nn.Linear(dim, hidden_dim, bias=False) |
| 27 | + self.up_proj = nn.Linear(dim, hidden_dim, bias=False) |
| 28 | + self.down_proj = nn.Linear(hidden_dim, dim, bias=False) |
| 29 | + |
| 30 | + def forward(self, x: torch.Tensor) -> torch.Tensor: |
| 31 | + h = x.float() |
| 32 | + variance = h.pow(2).mean(dim=-1, keepdim=True) |
| 33 | + h = self.norm_weight * (h * torch.rsqrt(variance + 1e-5)).to(x.dtype) |
| 34 | + return x + self.down_proj(F.silu(self.gate_proj(h)) * self.up_proj(h)) |
| 35 | + |
| 36 | + |
| 37 | +def get_inputs(): |
| 38 | + return [torch.randn(batch_size, n_ctx, dim)] |
| 39 | + |
| 40 | + |
| 41 | +def get_init_inputs(): |
| 42 | + return [] |
| 43 | + |
| 44 | + |
| 45 | +# CHECK-COUNT-3: gpu.alloc |
| 46 | +# CHECK: gpu.func @main_kernel |
| 47 | +# CHECK: vector.multi_reduction |
| 48 | +# CHECK: math.rsqrt |
| 49 | +# CHECK-NOT: xegpu.dpas |
| 50 | +# CHECK: gpu.func @main_kernel |
| 51 | +# CHECK: xegpu.dpas |
| 52 | +# CHECK: math.exp |
| 53 | +# CHECK: xegpu.store_nd |
| 54 | +# CHECK: gpu.func @main_kernel |
| 55 | +# CHECK: xegpu.dpas |
| 56 | +# CHECK: arith.mulf |
| 57 | +# CHECK: xegpu.store_nd |
| 58 | +# CHECK: gpu.func @main_kernel |
| 59 | +# CHECK: xegpu.dpas |
| 60 | +# CHECK: arith.addf |
| 61 | +# CHECK: xegpu.store_nd |
| 62 | +# CHECK-NOT: gpu.func |
0 commit comments