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


class ModelNew(nn.Module):
    def __init__(self):
        super().__init__()
        self._compile_cuda_kernel()

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

        cuda_source = """
        #include <cuda_runtime.h>
        __device__ __forceinline__ float mish_op_fast(float x) {
            // 阈值保护：x > 20 时，Softplus(x) ≈ x, Tanh(x) ≈ 1, Mish ≈ x
            // 同时也防止 e^(2x) 在 float32 下溢出 (e^88 溢出)
            if (x > 20.0f) return x;

            float e = expf(x);
            float n = e * (2.0f + e);
            float d = 2.0f + 2.0f * e + e * e;

            return x * (n / d);
        }

        __global__ void mish_kernel_alg(
            const float* __restrict__ x,
            float* __restrict__ y,
            int n) 
        {
            int tid = 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);

            int i = tid;

            for (; i < vec_n - 1; i += stride) {
                float4 v1 = x_vec[i];
                float4 v2 = x_vec[i + 1]; 

                float4 o1, o2;

                o1.x = mish_op_fast(v1.x);
                o1.y = mish_op_fast(v1.y);
                o1.z = mish_op_fast(v1.z);
                o1.w = mish_op_fast(v1.w);

                o2.x = mish_op_fast(v2.x);
                o2.y = mish_op_fast(v2.y);
                o2.z = mish_op_fast(v2.z);
                o2.w = mish_op_fast(v2.w);

                y_vec[i] = o1;
                y_vec[i + 1] = o2;

                i++; // Skip next
            }

            for (; i < vec_n; i += stride) {
                float4 v = x_vec[i];
                float4 o;
                o.x = mish_op_fast(v.x);
                o.y = mish_op_fast(v.y);
                o.z = mish_op_fast(v.z);
                o.w = mish_op_fast(v.w);
                y_vec[i] = o;
            }

            int tail_start = vec_n * 4;
            for (int j = tail_start + tid; j < n; j += stride) {
                y[j] = mish_op_fast(x[j]);
            }
        }

        torch::Tensor mish_cuda(torch::Tensor x) {
            auto x_c = x.contiguous();
            auto output = torch::empty_like(x_c);

            int total_elements = x_c.numel();
            int threads = 256;

            int vec_elements = total_elements / 4;
            int blocks = (vec_elements + threads - 1) / threads;

            if (blocks > 65535) blocks = 65535;
            if (blocks == 0) blocks = 1;

            mish_kernel_alg<<<blocks, threads>>>(
                x_c.data_ptr<float>(),
                output.data_ptr<float>(),
                total_elements
            );

            return output;
        }
        """

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

    def forward(self, x):
        return self.op.mish_cuda(x)