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 kldiv_cuda(torch::Tensor x, torch::Tensor y);
        """

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

        __device__ __forceinline__ double warp_sum(double val) {
            #pragma unroll
            for (int offset = 16; offset > 0; offset /= 2) {
                val += __shfl_down_sync(0xffffffff, val, offset);
            }
            return val;
        }

        __device__ __forceinline__ double block_sum(double val) {
            static __shared__ double shared[32];
            int lane = threadIdx.x % 32;
            int wid = threadIdx.x / 32;

            val = warp_sum(val);
            if (lane == 0) shared[wid] = val;
            __syncthreads();

            val = (threadIdx.x < blockDim.x / 32) ? shared[lane] : 0.0;
            if (wid == 0) val = warp_sum(val);
            return val;
        }

        __global__ void kldiv_kernel_opt(
            const float* __restrict__ x,
            const float* __restrict__ y,
            float* __restrict__ output,
            int total_elements) 
        {
            int idx = blockIdx.x * blockDim.x + threadIdx.x;
            int stride = gridDim.x * blockDim.x;

            double sum = 0.0;

            int vec_loops = total_elements / 4;
            int vec_remainder = total_elements % 4;

            const float4* x_vec = reinterpret_cast<const float4*>(x);
            const float4* y_vec = reinterpret_cast<const float4*>(y);

            for (int i = idx; i < vec_loops; i += stride) {
                float4 vx = x_vec[i];
                float4 vy = y_vec[i];

                // KLDiv Loss: y * (log(y) - x)
                // If y <= 0, term is 0.

                float l1 = (vy.x > 0.0f) ? (vy.x * (logf(vy.x) - vx.x)) : 0.0f;
                float l2 = (vy.y > 0.0f) ? (vy.y * (logf(vy.y) - vx.y)) : 0.0f;
                float l3 = (vy.z > 0.0f) ? (vy.z * (logf(vy.z) - vx.z)) : 0.0f;
                float l4 = (vy.w > 0.0f) ? (vy.w * (logf(vy.w) - vx.w)) : 0.0f;

                sum += (double)(l1 + l2 + l3 + l4);
            }

            // Tail handling
            if (idx == 0 && vec_remainder > 0) {
                int tail_start = vec_loops * 4;
                for (int i = 0; i < vec_remainder; ++i) {
                    int curr_idx = tail_start + i;
                    float val_x = x[curr_idx];
                    float val_y = y[curr_idx];
                    float loss = (val_y > 0.0f) ? (val_y * (logf(val_y) - val_x)) : 0.0f;
                    sum += (double)loss;
                }
            }

            sum = block_sum(sum);

            if (threadIdx.x == 0) {
                output[blockIdx.x] = (float)sum;
            }
        }

        torch::Tensor kldiv_cuda(torch::Tensor x, torch::Tensor y) {
            auto x_c = x.contiguous();
            auto y_c = y.contiguous();

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

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

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

            auto output = torch::empty({blocks}, x.options());

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

            return output.sum() / total_elements;
        }
        """

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

    def forward(self, x, y):
        return self.op.kldiv_cuda(x, y)