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 irl_kernel(const float* pred_rewards, const float* expert_log_probs, const float* policy_log_probs, float* expert_out, float* policy_out, int size) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < size) {
        expert_out[idx] = -pred_rewards[idx] * expert_log_probs[idx];
        policy_out[idx] = pred_rewards[idx] * policy_log_probs[idx];
    }
}

torch::Tensor irl_cuda(torch::Tensor pred_rewards, torch::Tensor expert_log_probs, torch::Tensor policy_log_probs) {
    auto size = pred_rewards.numel();
    auto expert_out = torch::empty_like(pred_rewards);
    auto policy_out = torch::empty_like(pred_rewards);
    const int block_size = 256;
    int num_blocks = (size + block_size - 1) / block_size;
    irl_kernel<<<num_blocks, block_size>>>(pred_rewards.data_ptr<float>(), expert_log_probs.data_ptr<float>(), policy_log_probs.data_ptr<float>(), expert_out.data_ptr<float>(), policy_out.data_ptr<float>(), size);
    return expert_out.mean() + policy_out.mean();
}
"""

cpp_source = """
torch::Tensor irl_cuda(torch::Tensor pred_rewards, torch::Tensor expert_log_probs, torch::Tensor policy_log_probs);
"""

irl_module = load_inline(
    name="inverse_rl",
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=["irl_cuda"],
    verbose=True
)

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

    def forward(self, pred_rewards, expert_log_probs, policy_log_probs):
        return self.irl_module.irl_cuda(pred_rewards, expert_log_probs, policy_log_probs)