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


class ModelNew(nn.Module):
    def __init__(self, feature_dim):
        super().__init__()
        self.register_buffer("center", torch.zeros(feature_dim))
        self._compile_cuda_kernel()

    def _compile_cuda_kernel(self):
        cpp_source = """
        #include <torch/extension.h>
        torch::Tensor chebyshev_cuda(torch::Tensor x, torch::Tensor center);
        """

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

        __device__ __forceinline__ float warp_reduce_max(float val) {
            #pragma unroll
            for (int offset = 16; offset > 0; offset /= 2) {
                float other = __shfl_down_sync(0xffffffff, val, offset);
                val = fmaxf(val, other);
            }
            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] : 0.0f;

            if (wid == 0) val = warp_reduce_max(val);

            return val;
        }

        __global__ void chebyshev_kernel_vec4(
            const float* __restrict__ x,
            const float* __restrict__ center,
            float* __restrict__ output,
            int batch_size,
            int feature_dim) 
        {
            int bid = blockIdx.x;
            if (bid >= batch_size) return;

            const float* row_x = x + bid * feature_dim;

            float max_val = 0.0f;
            int vec_loops = feature_dim / 4;
            int vec_remainder = feature_dim % 4;

            for (int i = threadIdx.x; i < vec_loops; i += blockDim.x) {
                float4 vx = reinterpret_cast<const float4*>(row_x)[i];
                float4 vc = reinterpret_cast<const float4*>(center)[i];

                max_val = fmaxf(max_val, fabsf(vx.x - vc.x));
                max_val = fmaxf(max_val, fabsf(vx.y - vc.y));
                max_val = fmaxf(max_val, fabsf(vx.z - vc.z));
                max_val = fmaxf(max_val, fabsf(vx.w - vc.w));
            }

            int tail_start = vec_loops * 4;
            if (threadIdx.x < vec_remainder) {
                int idx = tail_start + threadIdx.x;
                max_val = fmaxf(max_val, fabsf(row_x[idx] - center[idx]));
            }

            max_val = block_reduce_max(max_val);

            if (threadIdx.x == 0) {
                output[bid] = max_val;
            }
        }

        torch::Tensor chebyshev_cuda(torch::Tensor x, torch::Tensor center) {
            auto x_c = x.contiguous();
            auto c_c = center.contiguous();

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

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

            int threads = 256;
            int blocks = batch_size;

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

            return output;
        }
        """

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

    def forward(self, x):
        return self.op.chebyshev_cuda(x, self.center)