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

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

__global__ void bregman_divergence_kernel(
    const float* __restrict__ p,
    const float* __restrict__ q,
    float* __restrict__ output,
    int n,
    float eps
) {
    extern __shared__ float sdata[];
    int tid = threadIdx.x;
    int idx = blockIdx.x * blockDim.x + threadIdx.x;

    float local_sum = 0.0f;
    for (int i = idx; i < n; i += blockDim.x * gridDim.x) {
        float p_val = p[i];
        float q_val = q[i];

        // F(x) = x * log(x)
        float f_p = p_val * logf(p_val + eps);
        float f_q = q_val * logf(q_val + eps);

        // dF(x) = 1 + log(x)
        float df_q = 1.0f + logf(q_val + eps);

        // Bregman formulation: F(p) - F(q) - dF(q) * (p - q)
        float diff = p_val - q_val;
        float term = f_p - f_q - df_q * diff;

        local_sum += term;
    }

    sdata[tid] = local_sum;
    __syncthreads();

    for (int s = blockDim.x / 2; s > 0; s >>= 1) {
        if (tid < s) {
            sdata[tid] += sdata[tid + s];
        }
        __syncthreads();
    }

    if (tid == 0) {
        atomicAdd(output, sdata[0]);
    }
}

torch::Tensor bregman_divergence_cuda(torch::Tensor p, torch::Tensor q, float eps) {
    int batch_size = p.size(0);
    int num_classes = p.size(1);
    int n = batch_size * num_classes;

    auto output = at::zeros({1}, p.options());

    int threads = 256;
    int blocks = 128;
    int shared_mem = threads * sizeof(float);

    bregman_divergence_kernel<<<blocks, threads, shared_mem>>>(
        p.data_ptr<float>(),
        q.data_ptr<float>(),
        output.data_ptr<float>(),
        n,
        eps
    );

    return output / batch_size;
}
"""

cpp_source = """
torch::Tensor bregman_divergence_cuda(torch::Tensor p, torch::Tensor q, float eps);
"""

bregman_divergence_loss = load_inline(
    name="bregman_divergence_loss",
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=["bregman_divergence_cuda"],
    verbose=False
)


class ModelNew(nn.Module):
    def __init__(self, eps=1e-8):
        super(ModelNew, self).__init__()
        self.eps = eps

    def forward(self, p, target):
        p_prob = F.softmax(p, dim=1)
        q_prob = F.softmax(target, dim=1)
        return bregman_divergence_loss.bregman_divergence_cuda(p_prob, q_prob, self.eps)