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 logmeanexp_cuda(torch::Tensor input);
        """

        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 logmeanexp_kernel(
            const float* __restrict__ input,
            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_in = input + bid * feature_dim;

            float local_max = -FLT_MAX;

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

            const float4* in_vec = reinterpret_cast<const float4*>(row_in);

            for (int i = tid; i < vec_loops; i += blockDim.x) {
                float4 v = in_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_in[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;

            float local_sum = 0.0f;
            for (int i = tid; i < vec_loops; i += blockDim.x) {
                float4 v = in_vec[i];
                local_sum += expf(v.x - row_max) + expf(v.y - row_max) + 
                             expf(v.z - row_max) + expf(v.w - row_max);
            }

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

            float row_sum = block_reduce_sum(local_sum);

            if (tid == 0) {
                // LogMeanExp = Max + log(Sum) - log(N)
                output[bid] = row_max + logf(row_sum) - logf((float)feature_dim);
            }
        }

        torch::Tensor logmeanexp_cuda(torch::Tensor input) {
            auto x_c = input.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;

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

            return output;
        }
        """

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

    def forward(self, x):
        return self.op.logmeanexp_cuda(x)