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


class ModelNew(nn.Module):
    def __init__(self, value_coef):
        super().__init__()
        self.value_coef = value_coef.item() if isinstance(value_coef, torch.Tensor) else value_coef
        self._compile_cuda_kernel()

    def _compile_cuda_kernel(self):
        cpp_source = """
        torch::Tensor ac_loss_cuda(
            torch::Tensor log_probs,
            torch::Tensor values,
            torch::Tensor returns,
            torch::Tensor advantages,
            float value_coef);
        """

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

        __global__ void ac_loss_kernel(
            const float* __restrict__ log_probs,
            const float* __restrict__ values,
            const float* __restrict__ returns,
            const float* __restrict__ advantages,
            float* __restrict__ output,
            float value_coef,
            int n)
        {
            extern __shared__ float sdata[];
            unsigned int tid = threadIdx.x;
            unsigned int i = blockIdx.x * blockDim.x + threadIdx.x;
            unsigned int gridSize = blockDim.x * gridDim.x;

            float local_sum = 0.0f;

            while (i < n) {
                float lp = log_probs[i];
                float adv = advantages[i];
                float val = values[i];
                float ret = returns[i];

                float actor_term = -lp * adv;
                float diff = val - ret;
                float critic_term = diff * diff;

                local_sum += actor_term + value_coef * critic_term;
                i += gridSize;
            }

            sdata[tid] = local_sum;
            __syncthreads();

            for (unsigned int s = blockDim.x / 2; s > 0; s >>= 1) {
                if (tid < s) {
                    sdata[tid] += sdata[tid + s];
                }
                __syncthreads();
            }

            if (tid == 0) {
                atomicAdd(output, sdata[0] / n);
            }
        }

        torch::Tensor ac_loss_cuda(
            torch::Tensor log_probs,
            torch::Tensor values,
            torch::Tensor returns,
            torch::Tensor advantages,
            float value_coef)
        {
            auto log_probs_c = log_probs.contiguous();
            auto values_c = values.contiguous();
            auto returns_c = returns.contiguous();
            auto advantages_c = advantages.contiguous();

            int n = log_probs_c.numel();
            auto output = torch::zeros({1}, log_probs.options());

            const int threads = 256;
            const int blocks = min((n + threads - 1) / threads, 1024);
            const int shared_mem = threads * sizeof(float);

            ac_loss_kernel<<<blocks, threads, shared_mem>>>(
                log_probs_c.data_ptr<float>(),
                values_c.data_ptr<float>(),
                returns_c.data_ptr<float>(),
                advantages_c.data_ptr<float>(),
                output.data_ptr<float>(),
                value_coef,
                n
            );

            return output[0];
        }
        """

        self.op = load_inline(
            name="ac_loss_op",
            cpp_sources=cpp_source,
            cuda_sources=cuda_source,
            functions=["ac_loss_cuda"],
            extra_cuda_cflags=["-O3"],
            verbose=False
        )

    def forward(self, log_probs, values, returns, advantages):
        return self.op.ac_loss_cuda(log_probs, values, returns, advantages, self.value_coef)