import torch
import torch.nn as nn
import torch.nn.functional as F

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

DELTA_INIT = 1.0

DTYPE = torch.float64

class TanhSoft3(nn.Module):
    '''
    TanhSoft—Dynamic Trainable Activation Functions for Faster Learning and Better Performance
    https://ieeexplore.ieee.org/document/9514829
    Formula: f(x) = log(1 + exp(x) * tanh(delta * x))
    '''
    def __init__(self, delta_init=1.0):
        super(TanhSoft3, self).__init__()
        self.delta = nn.Parameter(torch.tensor(delta_init, dtype=DTYPE))
        self.clamp_val = 700.0 # for double

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        clamped_x = torch.clamp(x, max=self.clamp_val)
        inner = 1.0 + torch.exp(clamped_x) * torch.tanh(self.delta * x)
        return torch.log(inner.clamp(min=1e-12)) # Epsilon for double

class Model(nn.Module):
    def __init__(self, delta_init=1.0):
        super(Model, self).__init__()
        self.act = TanhSoft3(delta_init)
    
    def forward(self, x):
        return self.act(x)

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

def get_init_inputs():
    return [DELTA_INIT]