```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

# Fused CUDA kernel implementing, in a single pass over the conv_transpose output:
#   1) numerically-stable LogSumExp over the channel dim (online max/sum, single read)
#   2) HardSwish:  y * sigmoid(y + 3) / 6
#   3) bias subtraction
#   4) clamp to [-1, 1]
fused_post_source = """
#include <torch/extension.h>
#include <cuda_runtime.h>

__global__ void fused_post_kernel(
    const float* __restrict__ x,
    const float* __restrict__ bias,
    float* __restrict__ out,
    int C,
    long spatial,
    long total)
{
    long idx = (long)blockIdx.x * blockDim.x + threadIdx.x;
    if (idx >= total) return;

    long b = idx / spatial;
    long s = idx - b * spatial;
    const float* base = x + (b * (long)C) * spatial + s;

    // Online (single-pass) numerically-stable logsumexp over channels.
    float m = base[0];
    float sum = 1.0f;
    for (int c = 1; c < C; ++c) {
        float v = base[(long)c * spatial];
        if (v > m) {
            sum = sum * expf(m - v) + 1.0f;
            m = v;
        } else {
            sum += expf(v - m);
        }
    }
    float y = m + logf(sum);

    // HardSwish: y * sigmoid(y + 3) / 6
    float sig = 1.0f / (1.0f + expf(-(y + 3.0f)));
    y = y * sig * (1.0f / 6.0f);

    // Bias subtraction + clamp
    y = y - bias[0];
    y = fminf(fmaxf(y, -1.0f), 1.0f);

    out[idx] = y;
}

torch::Tensor fused_post(torch::Tensor x, torch::Tensor bias) {
    TORCH_CHECK(x.is_cuda(), "x must be a CUDA tensor");
    TORCH_CHECK(x.dtype() == torch::kFloat32, "x must be float32");
    TORCH_CHECK(x.dim() == 5, "x must be 5D (N, C, D, H, W)");
    TORCH_CHECK(bias.numel() == 1, "bias must be a scalar-like tensor");

    auto xc = x.contiguous();
    auto bias_c = bias.contiguous();

    long N = xc.size(0);
    long C = xc.size(1);
    long spatial = xc.numel() / (N * C);

    auto out = torch::empty({N, 1, xc.size(2), xc.size(3), xc.size(4)}, xc.options());

    long total = N * spatial;
    if (total == 0) return out;

    const int block = 256;
    long grid = (total + block - 1) / block;

    fused_post_kernel<<<(unsigned int)grid, block>>>(
        xc.data_ptr<float>(),
        bias_c.data_ptr<float>(),
        out.data_ptr<float>(),
        (int)C,
        spatial,
        total);

    return out;
}
"""

fused_post_cpp_source = "torch::Tensor fused_post(torch::Tensor x, torch::Tensor bias);"

fused_post_ops = load_inline(
    name="fused_post_ops",
    cpp_sources=fused_post_cpp_source,
    cuda_sources=fused_post_source,
    functions=["fused_post"],
    verbose=False,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
    extra_ldflags=[""],
)


class ModelNew(nn.Module):
    """
    Optimized model: keeps the cuDNN ConvTranspose3d, and fuses
    LogSumExp + HardSwish + bias-subtraction + clamp into one custom CUDA kernel.
    """
    def __init__(self, in_channels, out_channels, kernel_size, stride, padding, bias_shape):
        super(ModelNew, self).__init__()
        self.conv_transpose = nn.ConvTranspose3d(
            in_channels, out_channels, kernel_size, stride=stride, padding=padding
        )
        self.bias = nn.Parameter(torch.randn(*bias_shape))
        self.fused_post = fused_post_ops

    def forward(self, x):
        x = self.conv_transpose(x)
        if x.is_cuda:
            return self.fused_post.fused_post(x, self.bias)
        # CPU fallback (reference implementation)
        x = torch.logsumexp(x, dim=1, keepdim=True)
        x = x * torch.sigmoid(x + 3) / 6
        x = x - self.bias
        x = torch.clamp(x, min=-1, max=1)
        return x
```