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

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

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

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

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

            val = (threadIdx.x < blockDim.x / 32) ? shared[lane] : -FLT_MAX;
            if (wid == 0) val = warp_reduce_max(val);

            return val;
        }

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

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

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

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

            return val;
        }

        __global__ void logweightedsumexp_kernel(
            const float* __restrict__ x,
            const float* __restrict__ w,
            float* __restrict__ output,
            int feature_dim,
            int batch_size) 
        {
            int bid = blockIdx.x;
            int tid = threadIdx.x;

            if (bid >= batch_size) return;

            const float* row_x = x + bid * feature_dim;
            const float* row_w = w + bid * feature_dim;

            // 1. Compute Max(x) for stability
            float local_max = -FLT_MAX;

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

            const float4* x_vec = reinterpret_cast<const float4*>(row_x);
            const float4* w_vec = reinterpret_cast<const float4*>(row_w);

            for (int i = tid; i < vec_loops; i += blockDim.x) {
                float4 v = x_vec[i];
                local_max = fmaxf(local_max, fmaxf(v.x, fmaxf(v.y, fmaxf(v.z, v.w))));
            }

            if (tid == 0 && vec_remainder > 0) {
                int start = vec_loops * 4;
                for (int i = 0; i < vec_remainder; ++i) {
                    local_max = fmaxf(local_max, row_x[start + i]);
                }
            }

            float row_max = block_reduce_max(local_max);

            __shared__ float s_max;
            if (tid == 0) s_max = row_max;
            __syncthreads();
            row_max = s_max;

            // 2. Compute Weighted Sum of Exponentials
            float local_sum = 0.0f;
            for (int i = tid; i < vec_loops; i += blockDim.x) {
                float4 vx = x_vec[i];
                float4 vw = w_vec[i];

                local_sum += vw.x * expf(vx.x - row_max);
                local_sum += vw.y * expf(vx.y - row_max);
                local_sum += vw.z * expf(vx.z - row_max);
                local_sum += vw.w * expf(vx.w - row_max);
            }

            if (tid == 0 && vec_remainder > 0) {
                int start = vec_loops * 4;
                for (int i = 0; i < vec_remainder; ++i) {
                    local_sum += row_w[start + i] * expf(row_x[start + i] - row_max);
                }
            }

            float row_sum = block_reduce_sum(local_sum);

            if (tid == 0) {
                // Result = log(Sum) + Max
                output[bid] = logf(row_sum) + row_max;
            }
        }

        torch::Tensor logweightedsumexp_cuda(torch::Tensor x, torch::Tensor w) {
            auto x_c = x.contiguous();
            auto w_c = w.contiguous();

            int batch_size = x_c.size(0);
            int feature_dim = x_c.size(1);

            auto output = torch::empty({batch_size}, x_c.options());

            int threads = 256;
            int blocks = batch_size;

            logweightedsumexp_kernel<<<blocks, threads>>>(
                x_c.data_ptr<float>(),
                w_c.data_ptr<float>(),
                output.data_ptr<float>(),
                feature_dim,
                batch_size
            );

            return output;
        }
        """

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

    def forward(self, x, w):
        return self.op.logweightedsumexp_cuda(x, w)