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 bregman_divergence_gelu_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_val = 0.0f;

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

        float fx = val_x * val_x;
        float ft = val_t * val_t;
        float grad_ft = 2.0f * val_t;

        float term = fx - ft - grad_ft * (val_x - val_t);
        sum_val += term;
    }

    sum_val = warp_reduce(sum_val);

    static __shared__ float shared_mem[32];
    int lane = tid % 32;
    int wid = tid / 32;

    if (lane == 0) shared_mem[wid] = sum_val;
    __syncthreads();

    sum_val = (tid < blockDim.x / 32) ? shared_mem[lane] : 0.0f;
    if (wid == 0) sum_val = warp_reduce(sum_val);

    if (tid == 0) {
        float val = sum_val;
        float gelu = val * 0.5f * (1.0f + erff(val * 0.70710678f));
        y[row] = gelu;
    }
}

torch::Tensor launch_bregman_divergence_gelu(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;

    bregman_divergence_gelu_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_bregman_divergence_gelu(torch::Tensor x, torch::Tensor target);
"""

bregman_divergence_gelu_module = load_inline(
    name='bregman_divergence_gelu_op',
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=['launch_bregman_divergence_gelu'],
    verbose=False
)


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

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