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


class ModelNew(nn.Module):
    def __init__(self, negative_slope=0.01):
        super().__init__()
        self.negative_slope = negative_slope
        self._compile_cuda_kernel()

    def _compile_cuda_kernel(self):
        cpp_source = """
        #include <torch/extension.h>
        torch::Tensor leaky_reglu_cuda(torch::Tensor input, float negative_slope);
        """

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

        __device__ __forceinline__ float leaky_relu_f(float x, float slope) {
            return (x > 0.0f) ? x : x * slope;
        }

        __global__ void leaky_reglu_vec4_kernel(
            const float4* __restrict__ x,
            float4* __restrict__ y,
            int vec_dim_out,
            int n_vec_out,
            float slope) 
        {
            int idx = blockIdx.x * blockDim.x + threadIdx.x;
            int stride = gridDim.x * blockDim.x;

            for (int i = idx; i < n_vec_out; i += stride) {
                int row = i / vec_dim_out;
                int col = i % vec_dim_out;

                int gate_idx = row * (2 * vec_dim_out) + col;
                int act_idx  = gate_idx + vec_dim_out;

                float4 g = x[gate_idx];
                float4 a = x[act_idx];
                float4 out;

                out.x = leaky_relu_f(g.x, slope) * a.x;
                out.y = leaky_relu_f(g.y, slope) * a.y;
                out.z = leaky_relu_f(g.z, slope) * a.z;
                out.w = leaky_relu_f(g.w, slope) * a.w;

                y[i] = out;
            }
        }

        torch::Tensor leaky_reglu_cuda(torch::Tensor input, float negative_slope) {
            auto x_c = input.contiguous();

            int last_dim = x_c.size(-1);
            TORCH_CHECK(last_dim % 8 == 0, "Feature dim must be divisible by 8 for float4 optimization");

            auto out_sizes = x_c.sizes().vec();
            out_sizes.back() /= 2;
            auto output = torch::empty(out_sizes, x_c.options());

            int numel_out = output.numel();
            int n_vec_out = numel_out / 4;
            int vec_dim_out = out_sizes.back() / 4;

            int threads = 256;
            int blocks = (n_vec_out + threads - 1) / threads;
            if (blocks > 65535) blocks = 65535;
            if (blocks == 0) blocks = 1;

            leaky_reglu_vec4_kernel<<<blocks, threads>>>(
                reinterpret_cast<const float4*>(x_c.data_ptr<float>()),
                reinterpret_cast<float4*>(output.data_ptr<float>()),
                vec_dim_out,
                n_vec_out,
                negative_slope
            );

            return output;
        }
        """

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

    def forward(self, x):
        return self.op.leaky_reglu_cuda(x, self.negative_slope)