## Kernel memory (items verified by the protected evaluator on training problems; may not all apply)

### Lessons about this environment (from official checker verdicts and verified outcomes)
- Pitfall: A reply without a ```python fenced code block was rejected by the checker. Always wrap the full ModelNew source in a ```python block.
- Checker rule: getattr needs a literal, non-private attribute name
- Pitfall: Output mismatch beyond 1e-4 occurred repeatedly. Fix by forcing exact fp32 tl.dot(allow_tf32=False) and exact masking: K bounds at block granularity, tril mask, and zero-initialized output for skipped tiles.
- Checker rule: forward modified its input tensors in place
- Checker rule: do not define a class named Model (the reference); define ModelNew only
- Process: never SUBMIT code that has not passed ACTION: CHECK in this episode. Untested submissions that fail to compile or mismatch the reference score 0; a CHECK costs one reply and shows the compiler output.
- Pitfall: cpp_sources is compiled by plain c++ without CUDA headers: never use cudaStream_t or at::cuda::getCurrentCUDAStream there. Put wrappers needing streams in cuda_sources, keeping only plain-type prototypes in cpp_sources.
- Pitfall: If cpp_sources must reference CUDA types or the current stream, explicitly #include <cuda_runtime.h> and <ATen/cuda/CUDAContext.h>; torch/extension.h does not provide them.
- Checker rule: assignment to attribute 'm.conv1' is not allowed (only self.<x>)
- Checker rule: assignment to attribute 'm.bn1' is not allowed (only self.<x>)
- Checker rule: assignment to attribute 'm.conv2' is not allowed (only self.<x>)
- Checker rule: assignment to attribute 'm.bn2' is not allowed (only self.<x>)
- Checker rule: assignment to attribute 'm.downsample' is not allowed (only self.<x>)
- Checker rule: At least one of your own kernels must actually launch during forward; a pure torch path (even with a compiled but unused kernel) is rejected as no_custom_kernel.

### Pitfalls seen on earlier matmul problems (wrong results or slower than eager)
- Pitfall: For memory-bound broadcast or elementwise ops, eager is already near peak bandwidth. Use 2D row/column tiling instead of per-element integer division on flat indices, and load each broadcast operand once per tile.
- Pitfall: When using split-K, keep the split count small; a very large split writes and re-reads a huge partial-sum buffer plus an extra reduction launch, eating the occupancy gain.
- Pitfall: Strict fp32 Triton tl.dot (allow_tf32=False) rarely beats cuBLAS on matmul; a hand split-K version was correct but still 0.88x. Fuse surrounding ops or exploit structure instead of reimplementing plain GEMM.
- Pitfall: For bandwidth-bound Triton kernels, give each program more work: larger tiles, multiple rows per program, more warps. Tiny per-program work and Python launch overhead lose to cuBLAS/ATen kernels.
- Pitfall: Don't expect a single-pass memory-bound elementwise or broadcast kernel to beat eager, which already runs near bandwidth peak. Win by fusing multiple ops or reducing memory traffic.
- Pitfall: If writing a Triton GEMM anyway, use @triton.autotune over several BM/BN/BK, num_warps and num_stages configs, plus grouped tile ordering. A single fixed 64x64x32 config is far slower than eager.

### A verified kernel for a related matmul problem (L2_012 Gemm_Multiply_LeakyReLU, 1.00x): adapt what applies
```python
import torch
import torch.nn as nn
import triton
import triton.language as tl

@triton.jit
def fused_kernel(x_ptr, out_ptr, mult, neg_slope, n, BLOCK: tl.constexpr):
    pid = tl.program_id(0)
    offs = pid * BLOCK + tl.arange(0, BLOCK)
    mask = offs < n
    v = tl.load(x_ptr + offs, mask=mask, other=0.0)
    v = v * mult
    v = tl.where(v >= 0, v, v * neg_slope)
    tl.store(out_ptr + offs, v, mask=mask)

class ModelNew(nn.Module):
    def __init__(self, in_features, out_features, multiplier, negative_slope):
        super().__init__()
        self.gemm = nn.Linear(in_features, out_features)
        self.multiplier = multiplier
        self.negative_slope = negative_slope

    def forward(self, x):
        z = torch.addmm(self.gemm.bias, x, self.gemm.weight.t())
        n = z.numel()
        out = torch.empty_like(z)
        BLOCK = 1024
        grid = (triton.cdiv(n, BLOCK),)
        fused_kernel[grid](z, out, self.multiplier, self.negative_slope, n, BLOCK=BLOCK)
        return out
```

### A verified kernel for a related matmul problem (L2_075 Gemm_GroupNorm_Min_BiasAdd, 1.06x): adapt what applies
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
import triton
import triton.language as tl


@triton.jit
def gn_min_kernel(x_ptr, gamma_ptr, beta_ptr, out_ptr,
                  C, eps,
                  G: tl.constexpr, CG: tl.constexpr,
                  BLOCK: tl.constexpr):
    row = tl.program_id(0)
    offs = tl.arange(0, BLOCK)
    x = tl.load(x_ptr + row * C + offs)
    g = tl.load(gamma_ptr + offs)
    b = tl.load(beta_ptr + offs)

    xg = tl.reshape(x, (G, CG))
    mean = tl.sum(xg, axis=1) / CG
    xc = xg - mean[:, None]
    var = tl.sum(xc * xc, axis=1) / CG
    inv = 1.0 / tl.sqrt(var + eps)
    norm = xc * inv[:, None]
    normf = tl.reshape(norm, (BLOCK,))
    y = normf * g + b
    m = tl.min(y, axis=0)
    tl.store(out_ptr + row, m)


class ModelNew(nn.Module):
    def __init__(self, in_features, out_features, num_groups, bias_shape):
        super().__init__()
        self.gemm = nn.Linear(in_features, out_features)
        self.group_norm = nn.GroupNorm(num_groups, out_features)
        self.bias = nn.Parameter(torch.randn(bias_shape))
        self.num_groups = num_groups
        self.out_features = out_features

    def forward(self, x):
        z = F.linear(x, self.gemm.weight, self.gemm.bias)
        M, C = z.shape
        G = self.num_groups
        CG = C // G
        min_vec = torch.empty(M, device=z.device, dtype=z.dtype)
        BLOCK = C
        gn_min_kernel[(M,)](
            z, self.group_norm.weight, self.group_norm.bias, min_vec,
            C, float(self.group_norm.eps),
            G=G, CG=CG, BLOCK=BLOCK, num_warps=8,
        )
        out = min_vec.view(M, 1) + self.bias
        return out
```