```python
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.

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

torch.backends.cudnn.benchmark = True

# Fused CUDA kernel: out = tanh(x - bias[c]) in a single memory pass.
# The ConvTranspose2d itself is kept on cuDNN (it is compute bound and
# already near-optimal), while the bias-subtraction and tanh (two full
# read+write passes over the large 32x64x512x512 activation) are fused
# into one vectorized pass.
fused_sub_bias_tanh_source = """
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>

// Vectorized path (requires HW % 4 == 0 so each float4 stays within one channel)
__global__ void sub_bias_tanh_vec4_kernel(const float4* __restrict__ x,
                                          const float* __restrict__ bias,
                                          float4* __restrict__ out,
                                          const int C,
                                          const int HW4,
                                          const long long total4) {
    long long i = (long long)blockIdx.x * blockDim.x + threadIdx.x;
    if (i < total4) {
        int c = (int)((i / HW4) % C);
        float b = bias[c];
        float4 v = x[i];
        v.x = tanhf(v.x - b);
        v.y = tanhf(v.y - b);
        v.z = tanhf(v.z - b);
        v.w = tanhf(v.w - b);
        out[i] = v;
    }
}

// Generic scalar fallback
__global__ void sub_bias_tanh_kernel(const float* __restrict__ x,
                                     const float* __restrict__ bias,
                                     float* __restrict__ out,
                                     const int C,
                                     const long long HW,
                                     const long long total) {
    long long i = (long long)blockIdx.x * blockDim.x + threadIdx.x;
    if (i < total) {
        int c = (int)((i / HW) % C);
        out[i] = tanhf(x[i] - bias[c]);
    }
}

torch::Tensor sub_bias_tanh_cuda(torch::Tensor x, torch::Tensor bias) {
    TORCH_CHECK(x.is_cuda() && bias.is_cuda(), "Inputs must be CUDA tensors");
    TORCH_CHECK(x.scalar_type() == at::kFloat && bias.scalar_type() == at::kFloat,
                "Inputs must be float32");

    auto x_c = x.contiguous();
    auto bias_c = bias.contiguous();
    auto out = torch::empty_like(x_c);

    const long long total = x_c.numel();
    const long long C = x_c.size(1);
    const long long HW = (long long)x_c.size(2) * x_c.size(3);
    const int block = 256;
    auto stream = at::cuda::getCurrentCUDAStream();

    if (total > 0 && (HW % 4 == 0) && (total % 4 == 0)) {
        const long long total4 = total / 4;
        const long long HW4 = HW / 4;
        const long long blocks = (total4 + block - 1) / block;
        sub_bias_tanh_vec4_kernel<<<blocks, block, 0, stream>>>(
            reinterpret_cast<const float4*>(x_c.data_ptr<float>()),
            bias_c.data_ptr<float>(),
            reinterpret_cast<float4*>(out.data_ptr<float>()),
            (int)C, (int)HW4, total4);
    } else if (total > 0) {
        const long long blocks = (total + block - 1) / block;
        sub_bias_tanh_kernel<<<blocks, block, 0, stream>>>(
            x_c.data_ptr<float>(), bias_c.data_ptr<float>(), out.data_ptr<float>(),
            (int)C, HW, total);
    }
    return out;
}
"""

fused_sub_bias_tanh_cpp_source = (
    "torch::Tensor sub_bias_tanh_cuda(torch::Tensor x, torch::Tensor bias);"
)

fused_sub_bias_tanh = load_inline(
    name="fused_sub_bias_tanh",
    cpp_sources=fused_sub_bias_tanh_cpp_source,
    cuda_sources=fused_sub_bias_tanh_source,
    functions=["sub_bias_tanh_cuda"],
    verbose=False,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
)


class ModelNew(nn.Module):
    """
    Optimized model: ConvTranspose2d (cuDNN) followed by a single fused
    CUDA kernel that performs the bias subtraction and tanh activation in
    one memory-bound pass (float4 vectorized).
    """
    def __init__(self, in_channels, out_channels, kernel_size, bias_shape, stride=2, padding=1, output_padding=1):
        super(ModelNew, self).__init__()
        self.conv_transpose = nn.ConvTranspose2d(in_channels, out_channels, kernel_size,
                                                 stride=stride, padding=padding,
                                                 output_padding=output_padding)
        self.bias = nn.Parameter(torch.randn(bias_shape))

    def forward(self, x):
        x = self.conv_transpose(x)
        return fused_sub_bias_tanh.sub_bias_tanh_cuda(x, self.bias.view(-1))
```