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


class ModelNew(nn.Module):
    def __init__(self, beta=2.0):
        super().__init__()
        self.beta = beta
        self._compile_cuda_kernel()

    def _compile_cuda_kernel(self):
        cpp_source = """
        #include <torch/extension.h>
        torch::Tensor smelu_cuda(torch::Tensor x, float beta);
        """

        cuda_source = """
        #include <cuda_runtime.h>

        __device__ __forceinline__ float smelu_op(float x, float beta, float inv_4beta) {
            float abs_x = fabsf(x);
            if (abs_x < beta) {
                float tmp = x + beta;
                return tmp * tmp * inv_4beta;
            } else {
                return (x > 0.0f) ? x : 0.0f;
            }
        }

        __global__ void smelu_kernel_vec4(
            const float* __restrict__ x,
            float* __restrict__ y,
            int n,
            float beta,
            float inv_4beta) 
        {
            int idx = blockIdx.x * blockDim.x + threadIdx.x;
            int stride = blockDim.x * gridDim.x;

            int vec_n = n / 4;
            const float4* x_vec = reinterpret_cast<const float4*>(x);
            float4* y_vec = reinterpret_cast<float4*>(y);

            for (int i = idx; i < vec_n; i += stride) {
                float4 v = x_vec[i];
                float4 out;

                out.x = smelu_op(v.x, beta, inv_4beta);
                out.y = smelu_op(v.y, beta, inv_4beta);
                out.z = smelu_op(v.z, beta, inv_4beta);
                out.w = smelu_op(v.w, beta, inv_4beta);

                y_vec[i] = out;
            }

            int tail = vec_n * 4;
            for (int i = tail + idx; i < n; i += stride) {
                y[i] = smelu_op(x[i], beta, inv_4beta);
            }
        }

        torch::Tensor smelu_cuda(torch::Tensor x, float beta) {
            auto x_c = x.contiguous();
            auto output = torch::empty_like(x_c);

            int n = x_c.numel();
            int threads = 256;
            int blocks = (n / 4 + threads - 1) / threads;
            if (blocks > 65535) blocks = 65535;
            if (blocks == 0) blocks = 1;

            float inv_4beta = 1.0f / (4.0f * beta);

            smelu_kernel_vec4<<<blocks, threads>>>(
                x_c.data_ptr<float>(),
                output.data_ptr<float>(),
                n,
                beta,
                inv_4beta
            );

            return output;
        }
        """

        self.op = load_inline(
            name="smelu_opt_vec4",
            cpp_sources=cpp_source,
            cuda_sources=cuda_source,
            functions=["smelu_cuda"],
            extra_cuda_cflags=["-O3", "--use_fast_math"],
            verbose=False
        )

    def forward(self, x):
        return self.op.smelu_cuda(x, self.beta)