import torch
from torch.utils.cpp_extension import load_inline

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

__global__ void mish_bias_kernel(const float* x, const float* bias, float* y, int dim, long long total) {
    long long idx = blockIdx.x * blockDim.x + threadIdx.x;
    long long stride = blockDim.x * gridDim.x;
    for (long long i = idx; i < total; i += stride) {
        int j = (int)(i % dim);
        float z = x[i] + bias[j];
        float sp = z > 0.0f ? z + log1pf(expf(-z)) : log1pf(expf(z));
        float t = tanhf(sp);
        y[i] = z * t;
    }
}

torch::Tensor mish_bias_cuda(torch::Tensor x, torch::Tensor bias) {
    auto x_contig = x.contiguous();
    auto b_contig = bias.contiguous();
    auto y = torch::empty_like(x_contig);
    long long total = x_contig.numel();
    int dim = (int)x_contig.size(-1);
    int block = 512;
    long long grid = (total + block - 1) / block;
    grid = grid > 65535 ? 65535 : grid;
    mish_bias_kernel<<<(int)grid, block>>>(x_contig.data_ptr<float>(), b_contig.data_ptr<float>(), y.data_ptr<float>(), dim, total);
    return y;
}
"""

cpp_source = """
torch::Tensor mish_bias_cuda(torch::Tensor x, torch::Tensor bias);
"""

ops = load_inline(
    name="mish_bias",
    cpp_sources=cpp_source,
    cuda_sources=source,
    functions=["mish_bias_cuda"],
    verbose=True
)

class ModelNew(torch.nn.Module):
    def __init__(self, bias: torch.Tensor):
        super(ModelNew, self).__init__()
        self.ops = ops
        self.register_buffer("bias", bias)

    def forward(self, x):
        return self.ops.mish_bias_cuda(x, self.bias)
