import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline

cpp_source = """
#include <torch/extension.h>

torch::Tensor tanhsoft2_cuda_forward(
    const torch::Tensor& input, 
    const torch::Tensor& beta, 
    const torch::Tensor& gamma);
"""

cuda_source = """
#include <torch/extension.h>
#include <cuda_runtime.h>
#include <math.h>

#define BLOCK_SIZE 256

struct __align__(16) Float4 {
    float x, y, z, w;
};

// TanhSoft-2 Logic
__device__ __forceinline__ float compute_tanhsoft2(float x, float beta, float gamma) {
    float gx = fminf(gamma * x, 80.0f); // Clamp
    return x * tanhf(beta * __expf(gx));
}

__global__ void tanhsoft2_kernel(
    float* __restrict__ output,
    const float* __restrict__ input,
    const int n,
    const float beta,
    const float gamma) 
{
    const int idx = blockIdx.x * blockDim.x + threadIdx.x;
    const int vec_n = n / 4;
    
    int i = idx;
    const int stride = blockDim.x * gridDim.x;

    for (; i < vec_n; i += stride) {
        Float4 in_vec = reinterpret_cast<const Float4*>(input)[i];
        Float4 out_vec;

        out_vec.x = compute_tanhsoft2(in_vec.x, beta, gamma);
        out_vec.y = compute_tanhsoft2(in_vec.y, beta, gamma);
        out_vec.z = compute_tanhsoft2(in_vec.z, beta, gamma);
        out_vec.w = compute_tanhsoft2(in_vec.w, beta, gamma);

        reinterpret_cast<Float4*>(output)[i] = out_vec;
    }

    int start_scalar = vec_n * 4;
    int global_tid = blockIdx.x * blockDim.x + threadIdx.x;
    int total_threads = gridDim.x * gridDim.x;
    
    int current_idx = start_scalar + global_tid;
    while (current_idx < n) {
        output[current_idx] = compute_tanhsoft2(input[current_idx], beta, gamma);
        current_idx += total_threads;
    }
}

torch::Tensor tanhsoft2_cuda_forward(
    const torch::Tensor& input, 
    const torch::Tensor& beta_t, 
    const torch::Tensor& gamma_t) 
{
    TORCH_CHECK(input.is_cuda(), "Input must be a CUDA tensor");
    TORCH_CHECK(input.is_contiguous(), "Input must be contiguous");
    
    const int n = input.numel();
    auto output = torch::empty_like(input);

    const float beta = beta_t.item<float>();
    const float gamma = gamma_t.item<float>();

    const int vec_n = n / 4;
    const int grid_size = (vec_n + BLOCK_SIZE - 1) / BLOCK_SIZE;
    
    int final_grid = (grid_size < 1) ? 1 : grid_size;
    if (final_grid > 65535) final_grid = 65535;

    tanhsoft2_kernel<<<final_grid, BLOCK_SIZE>>>(
        output.data_ptr<float>(),
        input.data_ptr<float>(),
        n,
        beta,
        gamma
    );

    return output;
}
"""

tanhsoft2_op_module = load_inline(
    name='tanhsoft2_op',
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=['tanhsoft2_cuda_forward'],
    verbose=False,
    extra_cuda_cflags=['-O3', '--use_fast_math']
)

class ModelNew(nn.Module):
    def __init__(self, beta_init=1.0, gamma_init=1.0):
        super(ModelNew, self).__init__()
        self.beta = nn.Parameter(torch.tensor(beta_init))
        self.gamma = nn.Parameter(torch.tensor(gamma_init))
        self.op = tanhsoft2_op_module

    def forward(self, input_tensor: torch.Tensor) -> torch.Tensor:
        return self.op.tanhsoft2_cuda_forward(input_tensor.contiguous(), self.beta, self.gamma)