import torch
import torch.nn as nn
import math

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

T_VALUE = 1.0

class MMReLU(nn.Module):
    '''
    MMReLU: A Simple and Smooth Activation Function with High Convergence Speed
    https://ieeexplore.ieee.org/document/9674529
    Formula:
      f(x) = x                                     if x >= 0
           = (2*sqrt(3t)/(9t))*x^2 + x             if -sqrt(3t) < x < 0
           = t / x                                 if x <= -sqrt(3t)
    '''
    def __init__(self, t=1.0):
        super(MMReLU, self).__init__()
        self.t = t
        self.sqrt_3t = math.sqrt(3 * t)
        self.coeff = (2 * self.sqrt_3t) / (9 * t) if t > 0 else 0.0

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        # Part 1: x >= 0 -> x
        # Part 2: -sqrt(3t) < x < 0 -> quadratic
        # Part 3: x <= -sqrt(3t) -> reciprocal
        
        part1 = x
        part2 = self.coeff * x.pow(2) + x
        part3 = self.t / (x + 1e-9) # Add epsilon for stability
        
        neg_res = torch.where(x > -self.sqrt_3t, part2, part3)
        
        res = torch.where(x >= 0, part1, neg_res)
        
        return res


class Model(nn.Module):
    def __init__(self, t=1.0):
        super(Model, self).__init__()
        self.act = MMReLU(t=t)
    
    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 [T_VALUE]