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

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

__global__ void advantage_loss_kernel(const float* log_probs, const float* advantages, float* output, int size) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < size) {
        output[idx] = -log_probs[idx] * advantages[idx];
    }
}

torch::Tensor advantage_loss_cuda(torch::Tensor log_probs, torch::Tensor advantages) {
    auto size = log_probs.numel();
    auto output = torch::empty_like(log_probs);
    const int block_size = 256;
    int num_blocks = (size + block_size - 1) / block_size;
    advantage_loss_kernel<<<num_blocks, block_size>>>(log_probs.data_ptr<float>(), advantages.data_ptr<float>(), output.data_ptr<float>(), size);
    return output.mean();
}
"""

cpp_source = """
torch::Tensor advantage_loss_cuda(torch::Tensor log_probs, torch::Tensor advantages);
"""

adv_module = load_inline(
    name="advantage_loss",
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=["advantage_loss_cuda"],
    verbose=True
)

class ModelNew(torch.nn.Module):
    def __init__(self):
        super(ModelNew, self).__init__()
        self.adv_module = adv_module

    def forward(self, log_probs, advantages):
        return self.adv_module.advantage_loss_cuda(log_probs, advantages)