You write custom CUDA kernels to replace the PyTorch operators in the given EvoNorm architecture to get speedups.
You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining normalization+affine_transform+nonlinear_gating), or algorithmic changes (such as optimized memory access patterns). You are only limited by your imagination.

CUDA inline extension in PyTorch

Two-pass kernel design (max reduction + softmin computation)

Grid-stride loops for memory coalescing

Shared memory parallel reduction for max/sum operations

Numerical stability using max subtraction trick

Block-level parallelism (one block per data row)

Custom kernel compilation with nvcc -O3 optimization



Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is:
import torch
import torch.nn as nn
import torch.nn.functional as F

# --- Hyperparameters ---
N, D = 16, 512  # Batch Size, Vector Length
DIM = 1  # 默认在 D 维度上进行 Softmin


class Softmin(nn.Module):

    def __init__(self, dim=-1):
        super().__init__()
        self.dim = dim

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        # 确保输入是 float 类型
        if input.dtype != torch.float32:
            input = input.float()

        # 核心：对负输入进行 Softmax
        return torch.softmax(-input, dim=self.dim)


class Model(nn.Module):
    def __init__(self, dim=DIM):
        super().__init__()
        self.op = Softmin(dim=dim)

    def forward(self, input: torch.Tensor) -> torch.Tensor:
        return self.op(input)


# --- 辅助函数 ---

def get_inputs():
    """返回用于前向传播的随机输入张量。"""
    torch.manual_seed(42)
    # 2D 张量 (N, D)
    x = torch.randn(N, D, dtype=torch.float32) * 5.0
    return [x]


def get_init_inputs():
    """返回用于初始化模型的参数。"""
    return [DIM]