import torch
from torch.utils.cpp_extension import load_inline

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

// 纯CUDA实现：Chebyshev + LeakyReLU融合
__global__ void chebyshev_leakyrelu_kernel(
    const float* __restrict__ x,
    const float* __restrict__ y,
    float* __restrict__ distances,
    float* __restrict__ leakyrelu_output,
    int batch_size,
    int feature_dim,
    float negative_slope
) {
    // 每个block处理一个样本
    int sample_idx = blockIdx.x;
    if (sample_idx >= batch_size) return;
    
    int tid = threadIdx.x;
    int base = sample_idx * feature_dim;
    
    // 使用共享内存进行归约
    extern __shared__ float shared_max[];
    shared_max[tid] = 0.0f;
    
    // 每个线程处理多个元素
    int stride = blockDim.x;
    for (int dim = tid; dim < feature_dim; dim += stride) {
        // 应用LeakyReLU激活
        float x_activated = x[base + dim] > 0.0f ? x[base + dim] : negative_slope * x[base + dim];
        
        // 存储激活后的输出
        leakyrelu_output[base + dim] = x_activated;
        
        // 计算Chebyshev距离
        float diff = x_activated - y[base + dim];
        float abs_diff = fabsf(diff);
        if (abs_diff > shared_max[tid]) {
            shared_max[tid] = abs_diff;
        }
    }
    
    __syncthreads();
    
    // 块内归约找最大值
    for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
        if (tid < stride) {
            if (shared_max[tid + stride] > shared_max[tid]) {
                shared_max[tid] = shared_max[tid + stride];
            }
        }
        __syncthreads();
    }
    
    // 第一个线程写入结果
    if (tid == 0) {
        distances[sample_idx] = shared_max[0];
    }
}

// Warp级优化版本 - Chebyshev + LeakyReLU融合
__global__ void chebyshev_leakyrelu_kernel_warp(
    const float* __restrict__ x,
    const float* __restrict__ y,
    float* __restrict__ distances,
    float* __restrict__ leakyrelu_output,
    int batch_size,
    int feature_dim,
    float negative_slope
) {
    int sample_idx = blockIdx.x;
    if (sample_idx >= batch_size) return;
    
    int tid = threadIdx.x;
    int warp_id = tid / 32;
    int lane_id = tid % 32;
    
    int base = sample_idx * feature_dim;
    
    // Warp级计算最大值
    float warp_max = 0.0f;
    
    // 每个warp处理一部分特征
    int elements_per_warp = (feature_dim + 8 - 1) / 8;
    int start_dim = warp_id * elements_per_warp;
    int end_dim = min(start_dim + elements_per_warp, feature_dim);
    
    for (int dim = start_dim + lane_id; dim < end_dim; dim += 32) {
        // 应用LeakyReLU激活
        float x_activated = x[base + dim] > 0.0f ? x[base + dim] : negative_slope * x[base + dim];
        
        // 存储激活后的输出
        leakyrelu_output[base + dim] = x_activated;
        
        // 计算Chebyshev距离
        float diff = x_activated - y[base + dim];
        float abs_diff = fabsf(diff);
        if (abs_diff > warp_max) {
            warp_max = abs_diff;
        }
    }
    
    // Warp级归约找最大值
    for (int offset = 16; offset > 0; offset /= 2) {
        float other_max = __shfl_down_sync(0xffffffff, warp_max, offset);
        if (other_max > warp_max) {
            warp_max = other_max;
        }
    }
    
    // 使用共享内存进行跨warp归约
    extern __shared__ float shared_data[];
    if (lane_id == 0) {
        shared_data[warp_id] = warp_max;
    }
    __syncthreads();
    
    // 第一个线程找到全局最大值
    if (tid == 0) {
        float global_max = 0.0f;
        int num_warps = blockDim.x / 32;
        for (int i = 0; i < num_warps; i++) {
            if (shared_data[i] > global_max) {
                global_max = shared_data[i];
            }
        }
        distances[sample_idx] = global_max;
    }
}

// 主函数 - 纯CUDA实现
torch::Tensor chebyshev_leakyrelu_cuda(
    torch::Tensor x,
    torch::Tensor y,
    float negative_slope = 0.01f
) {
    // 输入验证
    TORCH_CHECK(x.scalar_type() == torch::kFloat32, "X must be float32");
    TORCH_CHECK(y.scalar_type() == torch::kFloat32, "Y must be float32");
    TORCH_CHECK(x.sizes() == y.sizes(), "X and Y must have same shape");
    
    auto x_contig = x.contiguous();
    auto y_contig = y.contiguous();
    
    int batch_size = x_contig.size(0);
    int feature_dim = x_contig.size(1);
    
    // 创建输出张量
    auto distances = torch::zeros({batch_size}, x.options());
    auto leakyrelu_output = torch::empty_like(x_contig);
    
    const int block_size = 256; // 8个warps
    size_t shared_mem = 8 * sizeof(float); // 8个warp的结果
    
    // 使用Warp级优化版本
    chebyshev_leakyrelu_kernel_warp<<<batch_size, block_size, shared_mem>>>(
        x_contig.data_ptr<float>(),
        y_contig.data_ptr<float>(),
        distances.data_ptr<float>(),
        leakyrelu_output.data_ptr<float>(),
        batch_size,
        feature_dim,
        negative_slope
    );
    
    return distances;
}
"""

chebyshev_leakyrelu_cpp_source = """
torch::Tensor chebyshev_leakyrelu_cuda(torch::Tensor x, torch::Tensor y, float negative_slope);
"""

# 编译CUDA代码
chebyshev_leakyrelu = load_inline(
    name="chebyshev_leakyrelu",
    cpp_sources=chebyshev_leakyrelu_cpp_source,
    cuda_sources=chebyshev_leakyrelu_source,
    functions=["chebyshev_leakyrelu_cuda"],
    extra_cuda_cflags=[
        "-O3", 
        "--use_fast_math",
        "-gencode=arch=compute_80,code=sm_80"
    ],
    verbose=True
)

class ModelNew(torch.nn.Module):
    def __init__(self, negative_slope=0.01):
        super(ModelNew, self).__init__()
        self.negative_slope = negative_slope
        self.chebyshev_leakyrelu = chebyshev_leakyrelu

    def forward(self, x, y):
        return self.chebyshev_leakyrelu.chebyshev_leakyrelu_cuda(x, y, self.negative_slope)
