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


cpp_src = "torch::Tensor causal_mask_cuda(torch::Tensor input, float val);"


cuda_src = """
#include <cuda_runtime.h>

__global__ void causal_mask_kernel(
    const float* __restrict__ input,
    float* __restrict__ output,
    int seq_len,
    int total_elements,
    float val
) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx >= total_elements) return;

    int plane_size = seq_len * seq_len;
    int in_plane_idx = idx % plane_size;
    
    int row = in_plane_idx / seq_len;
    int col = in_plane_idx % seq_len;

  
    if (col > row) {
        output[idx] = val;
    } else {
        output[idx] = input[idx];
    }
}



torch::Tensor causal_mask_cuda(torch::Tensor input, float val) {
    int numel = input.numel();
    input = input.contiguous();
    auto output = torch::empty_like(input);


    int seq_len = input.size(-1);

    const int block_size = 256;
    int grid_size = (numel + block_size - 1) / block_size;
    
    causal_mask_kernel<<<grid_size, block_size>>>(
        input.data_ptr<float>(),
        output.data_ptr<float>(),
        seq_len,
        numel,
        val
    );

    return output;
}
"""

class ModelNew(nn.Module):
    def __init__(self, val=-1e9):
        super().__init__()
        self.val = val
        self.module = load_inline(
            name="causal_mask_opt_v1",
            cpp_sources=cpp_src,
            cuda_sources=cuda_src,
            functions=["causal_mask_cuda"],
            verbose=False,
            extra_cuda_cflags=["-O3"]
        )

    def forward(self, x):
        return self.module.causal_mask_cuda(x, self.val)