import torch
import torch.nn as nn

class Model(nn.Module):
    """
    原始模型：InstanceNorm + ReLU
    """
    def __init__(self, num_features=64, eps=1e-5, affine=True, track_running_stats=False):
        super(Model, self).__init__()
        self.num_features = num_features
        self.eps = eps
        self.affine = affine
        self.track_running_stats = track_running_stats

        # 创建InstanceNorm层
        self.instance_norm = nn.InstanceNorm2d(
            num_features=num_features,
            eps=eps,
            affine=affine,
            track_running_stats=track_running_stats
        )
        
        # 创建ReLU层
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        原始实现：InstanceNorm -> ReLU
        
        Args:
            x (torch.Tensor): 输入张量 shape [B, C, H, W]
        
        Returns:
            torch.Tensor: ReLU(InstanceNorm(x))
        """
        x_normalized = self.instance_norm(x)
        return self.relu(x_normalized)

class ModelNew(torch.nn.Module):
    """
    融合模型：直接实现InstanceNorm + ReLU
    """
    def __init__(self, num_features=64, eps=1e-5, affine=True, track_running_stats=False):
        super(ModelNew, self).__init__()
        self.num_features = num_features
        self.eps = eps
        self.affine = affine
        self.track_running_stats = False  # 强制为False以支持CUDA实现

        if affine:
            self.weight = torch.nn.Parameter(torch.ones(num_features))
            self.bias = torch.nn.Parameter(torch.zeros(num_features))
        else:
            self.register_parameter('weight', None)
            self.register_parameter('bias', None)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """
        融合实现：在CUDA kernel中直接完成InstanceNorm + ReLU
        
        Args:
            x (torch.Tensor): 输入张量 shape [B, C, H, W]
        
        Returns:
            torch.Tensor: ReLU(InstanceNorm(x))
        """
        # 这个将在CUDA中实现
        pass

# 测试参数
batch_size = 256
num_features = 64
height = 128
width = 128

def get_inputs():
    """生成测试输入"""
    x = torch.randn(batch_size, num_features, height, width)
    return [x]

def get_init_inputs():
    """获取初始化参数"""
    return [num_features]
