import torch
from torch.utils.cpp_extension import load_inline

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

__global__ void tanhshrink_affine_kernel(const float* x, const float* scale, 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 = __fmul_rn(x[i], scale[j]);
        z = __fadd_rn(z, bias[j]);
        y[i] = z - tanhf(z);
    }
}

torch::Tensor tanhshrink_affine_cuda(torch::Tensor x, torch::Tensor scale, torch::Tensor bias) {
    auto x_contig = x.contiguous();
    auto s_contig = scale.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;
    tanhshrink_affine_kernel<<<(int)grid, block>>>(x_contig.data_ptr<float>(), s_contig.data_ptr<float>(), b_contig.data_ptr<float>(), y.data_ptr<float>(), dim, total);
    return y;
}
"""

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

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

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

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