import torch
from torch.utils.cpp_extension import load_inline

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

// 极致优化版本 - float4向量化 + 多线程
__global__ void manhattan_kernel_optimized(
    const float* __restrict__ x,
    const float* __restrict__ y,
    float* __restrict__ distances,
    int batch_size,
    int feature_dim
) {
    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_sum[];
    shared_sum[tid] = 0.0f;
    
    // 每个线程处理4个元素（float4向量化）
    int stride = blockDim.x * 4;
    for (int dim = tid * 4; dim < feature_dim; dim += stride) {
        // 确保不越界
        if (dim + 3 < feature_dim) {
            float4 x_val = *reinterpret_cast<const float4*>(&x[base + dim]);
            float4 y_val = *reinterpret_cast<const float4*>(&y[base + dim]);
            
            shared_sum[tid] += fabsf(x_val.x - y_val.x) + fabsf(x_val.y - y_val.y) + 
                              fabsf(x_val.z - y_val.z) + fabsf(x_val.w - y_val.w);
        } else {
            // 处理剩余元素
            for (int i = dim; i < feature_dim; i++) {
                float diff = x[base + i] - y[base + i];
                shared_sum[tid] += fabsf(diff);
            }
        }
    }
    
    __syncthreads();
    
    // 块内归约求和
    for (int stride = blockDim.x / 2; stride > 0; stride >>= 1) {
        if (tid < stride) {
            shared_sum[tid] += shared_sum[tid + stride];
        }
        __syncthreads();
    }
    
    // 第一个线程写入结果
    if (tid == 0) {
        distances[sample_idx] = shared_sum[0];
    }
}

torch::Tensor manhattan_cuda(torch::Tensor x, torch::Tensor y) {
    TORCH_CHECK(x.scalar_type() == torch::kFloat32, "X must be float32");
    TORCH_CHECK(y.scalar_type() == torch::kFloat32, "Y must be float32");
    
    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());
    
    // 极致优化版本
    const int block_size = 64;  // 每个线程处理4个元素，所以用更少的线程
    size_t shared_mem = block_size * sizeof(float);
    
    manhattan_kernel_optimized<<<batch_size, block_size, shared_mem>>>(
        x_contig.data_ptr<float>(),
        y_contig.data_ptr<float>(),
        distances.data_ptr<float>(),
        batch_size,
        feature_dim
    );
    
    return distances;
}
"""

manhattan_cpp_source = """
torch::Tensor manhattan_cuda(torch::Tensor x, torch::Tensor y);
"""

# Compile the inline CUDA code
manhattan = load_inline(
    name="manhattan",
    cpp_sources=manhattan_cpp_source,
    cuda_sources=manhattan_source,
    functions=["manhattan_cuda"],
    verbose=True
)

class ModelNew(torch.nn.Module):
    def __init__(self):
        super(ModelNew, self).__init__()
        self.manhattan = manhattan  # The module containing the kernel

    def forward(self, x, y):
        return self.manhattan.manhattan_cuda(x, y)
