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

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

__inline__ __device__ float warp_reduce(float val) {
    for (int offset = 16; offset > 0; offset /= 2)
        val += __shfl_down_sync(0xffffffff, val, offset);
    return val;
}

__global__ void kldiv_jsdiv_swish_kernel(
    const float* __restrict__ x, 
    const float* __restrict__ target, 
    float* __restrict__ y, 
    int batch_size, 
    int width) 
{
    int row = blockIdx.x;
    int tid = threadIdx.x;

    if (row >= batch_size) return;

    const float* row_x = x + row * width;

    float sum_kl_x = 0.0f;
    float sum_kl_t = 0.0f;
    const float eps = 1e-8f;

    for (int i = tid; i < width; i += blockDim.x) {
        float val_x = row_x[i];
        float val_t = target[i];

        float m = 0.5f * (val_x + val_t);
        float log_m = logf(m + eps);


        sum_kl_x += val_x * (logf(val_x + eps) - log_m);

        sum_kl_t += val_t * (logf(val_t + eps) - log_m);
    }

    sum_kl_x = warp_reduce(sum_kl_x);
    sum_kl_t = warp_reduce(sum_kl_t);

    static __shared__ float shared_kl_x[32];
    static __shared__ float shared_kl_t[32];

    int lane = tid % 32;
    int wid = tid / 32;

    if (lane == 0) {
        shared_kl_x[wid] = sum_kl_x;
        shared_kl_t[wid] = sum_kl_t;
    }
    __syncthreads();

    sum_kl_x = (tid < blockDim.x / 32) ? shared_kl_x[lane] : 0.0f;
    sum_kl_t = (tid < blockDim.x / 32) ? shared_kl_t[lane] : 0.0f;

    if (wid == 0) {
        sum_kl_x = warp_reduce(sum_kl_x);
        sum_kl_t = warp_reduce(sum_kl_t);
    }

    if (tid == 0) {
        float js_div = 0.5f * (sum_kl_x + sum_kl_t);
        float swish = js_div / (1.0f + expf(-js_div));
        y[row] = swish;
    }
}

torch::Tensor launch_kldiv_jsdiv_swish(torch::Tensor x, torch::Tensor target) {
    auto batch_size = x.size(0);
    auto width = x.size(1);
    auto y = torch::empty({batch_size}, x.options());

    const int threads = 256;
    const int blocks = batch_size;

    kldiv_jsdiv_swish_kernel<<<blocks, threads>>>(
        x.data_ptr<float>(), 
        target.data_ptr<float>(), 
        y.data_ptr<float>(), 
        batch_size, 
        width
    );
    return y;
}
"""

cpp_source = """
torch::Tensor launch_kldiv_jsdiv_swish(torch::Tensor x, torch::Tensor target);
"""

kldiv_jsdiv_swish_module = load_inline(
    name='kldiv_jsdiv_swish_op',
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=['launch_kldiv_jsdiv_swish'],
    verbose=False
)


class ModelNew(nn.Module):
    def __init__(self, target):
        super(ModelNew, self).__init__()
        self.target = nn.Parameter(target)
        self.op = kldiv_jsdiv_swish_module

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        return self.op.launch_kldiv_jsdiv_swish(x.contiguous(), self.target.contiguous())