import torch
import torch.nn as nn

BATCH_SIZE = 4096
HIDDEN_DIM = 4096
SHAPE = (BATCH_SIZE, HIDDEN_DIM)

ALPHA_VALUE = 0.882267 # 论文中的建议值

class ErfReLU(nn.Module):
    """
    "ErfReLU: adaptive activation function for deep neural network" (Pattern Analysis and Applications, 2024)
    https://link.springer.com/article/10.1007/s10044-024-01277-w
    Formula:
      f(x) = x            if x >= 0
      f(x) = alpha * erf(x) if x < 0
    """
    def __init__(self, alpha=0.882267):
        super(ErfReLU, self).__init__()
        self.alpha = alpha

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        pos_part = x
        neg_part = self.alpha * torch.erf(x)
        return torch.where(x >= 0, pos_part, neg_part)

class Model(nn.Module):
    def __init__(self, alpha=0.882267):
        super(Model, self).__init__()
        self.act = ErfReLU(alpha=alpha)
    
    def forward(self, x):
        return self.act(x)

def get_inputs():
    input_tensor = torch.randn(SHAPE, dtype=torch.float32) * 2.0
    return [input_tensor.contiguous()]

def get_init_inputs():
    return [ALPHA_VALUE]