You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. 

    You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination.


        Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: 

        ``` 

        # 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
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()

    def forward(self, a, b):
        return a + b


def get_inputs():
    # randomly generate input tensors based on the model architecture
    a = torch.randn(1, 128).cuda()
    b = torch.randn(1, 128).cuda()
    return [a, b]


def get_init_inputs():
    # randomly generate tensors required for initialization based on the model architecture
    return []

        ``` 

        The example new arch with custom CUDA kernels looks like this:
        ```
        # 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
import torch.nn.functional as F
from torch.utils.cpp_extension import load_inline

# Define the custom CUDA kernel for element-wise addition
elementwise_add_source = """
#include <torch/extension.h>
#include <cuda_runtime.h>

__global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < size) {
        out[idx] = a[idx] + b[idx];
    }
}

torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) {
    auto size = a.numel();
    auto out = torch::zeros_like(a);

    const int block_size = 256;
    const int num_blocks = (size + block_size - 1) / block_size;

    elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size);

    return out;
}
"""

elementwise_add_cpp_source = (
    "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);"
)

# Compile the inline CUDA code for element-wise addition
elementwise_add = load_inline(
    name="elementwise_add",
    cpp_sources=elementwise_add_cpp_source,
    cuda_sources=elementwise_add_source,
    functions=["elementwise_add_cuda"],
    verbose=True,
    extra_cflags=[""],
    extra_ldflags=[""],
)


class ModelNew(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.elementwise_add = elementwise_add

    def forward(self, a, b):
        return self.elementwise_add.elementwise_add_cuda(a, b)

        ``` 

        
    You are given the following architecture: 

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

class Model(nn.Module):
    """
    A model that performs a sequence of operations:
        - ConvTranspose3d
        - MaxPool3d
        - Softmax
        - Subtract
        - Swish
        - Max
    """
    def __init__(self, in_channels, out_channels, kernel_size, stride, padding, output_padding, pool_kernel_size, pool_stride, pool_padding):
        super(Model, self).__init__()
        self.conv_transpose = nn.ConvTranspose3d(in_channels, out_channels, kernel_size, stride=stride, padding=padding, output_padding=output_padding)
        self.max_pool = nn.MaxPool3d(kernel_size=pool_kernel_size, stride=pool_stride, padding=pool_padding)
        self.subtract = nn.Parameter(torch.randn(out_channels)) # Assuming subtraction is element-wise across channels

    def forward(self, x):
        x = self.conv_transpose(x)
        x = self.max_pool(x)
        x = torch.softmax(x, dim=1) # Apply softmax across channels (dim=1)
        x = x - self.subtract.view(1, -1, 1, 1, 1) # Subtract across channels
        x = torch.sigmoid(x) * x # Swish activation
        x = torch.max(x, dim=1)[0] # Max pooling across channels
        return x

batch_size = 128
in_channels = 3
out_channels = 16
depth, height, width = 16, 32, 32
kernel_size = 3
stride = 2
padding = 1
output_padding = 1
pool_kernel_size = 2
pool_stride = 2
pool_padding = 0

def get_inputs():
    return [torch.rand(batch_size, in_channels, depth, height, width)]

def get_init_inputs():
    return [in_channels, out_channels, kernel_size, stride, padding, output_padding, pool_kernel_size, pool_stride, pool_padding]
    ```
    
Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code! 



This is turn 5 of 6 for this task. Your previous solution (turn 4) was:
```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

# Fully fused TF32 tensor-core kernel:
#   ConvTranspose3d (k=3, s=2, p=1, op=1) -> MaxPool3d (2,2,0) -> Softmax(dim=1)
#   -> per-channel subtract -> Swish -> Max over channels
#
# Design notes:
#  * The stride-2 conv-transpose is decomposed into 8 stride-1 "parity"
#    sub-convolutions on the input grid. The 2x2x2 max-pool window corresponds
#    exactly to the 8 parity classes at one grid cell, so pooling becomes an
#    elementwise max across the 8 accumulator fragments, and the pooled spatial
#    size equals the input spatial size.
#  * Each warp computes a 16(co) x 8(cells) output tile per parity with
#    mma.sync.aligned.m16n8k8.row.col.f32.tf32.tf32.f32 (TF32 tensor-core math,
#    same precision class as the TF32 reference). All 14 weight fragments are
#    preloaded into registers once per warp and reused across every tile.
#  * B-fragments (gathered inputs) are loaded straight from global memory with
#    per-thread address computation -- no shared-memory staging, no barriers.
#  * Softmax denominator exponentials are reused for the probabilities; the
#    subtract + swish + channel-max epilogue uses __shfl_xor reductions within
#    8-lane groups, so no intermediate tensor is ever materialized.

_fused_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>
#include <cfloat>

#define CI 3
#define CO 16

#define PAR_KD(p) (((p) >> 2) & 1)
#define PAR_KH(p) (((p) >> 1) & 1)
#define PAR_KW(p) ((p) & 1)

__global__ __launch_bounds__(128) void fused_convT_pool_softmax_swish_cmax_mma_kernel(
    const float* __restrict__ x,        // [B, CI, D, H, W]
    const float* __restrict__ weight,   // [CI, CO, 3, 3, 3]
    const float* __restrict__ bias,     // [CO]
    const float* __restrict__ sub,      // [CO]
    float* __restrict__ out,            // [B, Dp=D, Hp=H, Wp=W]
    const int D, const int H, const int W,
    const int nwt,                      // W/8 tiles per (b,d,h)
    const int tiles_per_b,              // D*H*nwt
    const int nblocks, const int T)     // tiles per warp chunk
{
    __shared__ int   s_ci[8][24];       // input channel per (parity, k)
    __shared__ int   s_tap[8][24];      // flattened 3x3x3 tap index per (parity, k)
    __shared__ short s_off[8][24][3];   // input offsets (d,h,w) per (parity, k)

    // Build the parity -> (ci, tap, offsets) tables (single warp, once).
    if (threadIdx.x < 32) {
        const int lane = threadIdx.x;
        for (int idx = lane; idx < 8 * 24; idx += 32) {
            const int p = idx >> 5;                 // 24 rounded to 32 per parity
            const int k = idx & 31;
            int ci = 0, tap = 0, od = 0, oh = 0, ow = 0;
            if (k < 24) {
                const int kd = PAR_KD(p), kh = PAR_KH(p), kw = PAR_KW(p);
                const int TW = kw + 1, TH = kh + 1, TD = kd + 1;
                const int TPS = TD * TH * TW;
                const int Kp  = CI * TPS;
                if (k < Kp) {
                    ci = k / TPS;
                    int r = k - ci * TPS;
                    const int di = r / (TH * TW);
                    r -= di * (TH * TW);
                    const int hi = r / TW;
                    const int wi = r - hi * TW;
                    const int tdk = kd ? (di == 0 ? 0 : 2) : 1;
                    const int thk = kh ? (hi == 0 ? 0 : 2) : 1;
                    const int twk = kw ? (wi == 0 ? 0 : 2) : 1;
                    od = kd ? (1 - di) : 0;
                    oh = kh ? (1 - hi) : 0;
                    ow = kw ? (1 - wi) : 0;
                    tap = tdk * 9 + thk * 3 + twk;
                }
            }
            s_ci[p][k] = ci;
            s_tap[p][k] = tap;
            s_off[p][k][0] = (short)od;
            s_off[p][k][1] = (short)oh;
            s_off[p][k][2] = (short)ow;
        }
    }
    __syncthreads();

    const int lane = threadIdx.x & 31;
    const int wid  = threadIdx.x >> 5;
    const int g  = lane >> 2;    // mma group id (0..7)
    const int tg = lane & 3;     // thread id in group (0..3)

    const int HW  = H * W;
    const int DHW = D * HW;

    // ---- Preload all 14 A (weight) fragments into registers. ----
    float a0[14], a1[14], a2[14], a3[14];
    {
        int f = 0;
        #pragma unroll
        for (int p = 0; p < 8; ++p) {
            const int TW = PAR_KW(p) + 1, TH = PAR_KH(p) + 1, TD = PAR_KD(p) + 1;
            const int Kp = CI * TD * TH * TW;
            const int steps = (Kp + 7) >> 3;
            for (int st = 0; st < steps; ++st, ++f) {
                const int ka = 8 * st + tg, kb = ka + 4;
                const float* wg = weight + g * 27;
                a0[f] = (ka < Kp) ? __ldg(wg + s_ci[p][ka] * 432 + s_tap[p][ka]) : 0.0f;
                a1[f] = (ka < Kp) ? __ldg(wg + 216 + s_ci[p][ka] * 432 + s_tap[p][ka]) : 0.0f;
                a2[f] = (kb < Kp) ? __ldg(wg + s_ci[p][kb] * 432 + s_tap[p][kb]) : 0.0f;
                a3[f] = (kb < Kp) ? __ldg(wg + 216 + s_ci[p][kb] * 432 + s_tap[p][kb]) : 0.0f;
            }
        }
    }

    const float b0c = __ldg(bias + g);
    const float b8c = __ldg(bias + g + 8);
    const float s0c = __ldg(sub + g);
    const float s8c = __ldg(sub + g + 8);

    // ---- Tile loop: each warp handles a contiguous chunk of T tiles. ----
    for (int blk = blockIdx.x; blk < nblocks; blk += gridDim.x) {
        const int base = (blk * 4 + wid) * T;
        if (base >= tiles_per_b * gridDim.z) continue;
        const int b   = blockIdx.z;
        const int rem = tiles_per_b - base;
        const int tcount = rem < T ? rem : T;

        const float* xb = x + (long long)b * CI * DHW;
        float* outb = out + (long long)b * D * HW;

        for (int t = 0; t < tcount; ++t) {
            const int tile = base + t;
            const int wt = tile % nwt;
            const int dh = tile / nwt;
            const int h = dh % H;
            const int d = dh / H;
            const int w0 = wt * 8;

            // Accumulators: [parity][c0..c3]
            float acc[8][4];
            #pragma unroll
            for (int p = 0; p < 8; ++p) {
                acc[p][0] = 0.0f; acc[p][1] = 0.0f;
                acc[p][2] = 0.0f; acc[p][3] = 0.0f;
            }

            int f = 0;
            #pragma unroll
            for (int p = 0; p < 8; ++p) {
                const int TW = PAR_KW(p) + 1, TH = PAR_KH(p) + 1, TD = PAR_KD(p) + 1;
                const int Kp = CI * TD * TH * TW;
                const int steps = (Kp + 7) >> 3;
                #pragma unroll
                for (int st = 0; st < 3; ++st) {
                    if (st < steps) {
                        const int ka = 8 * st + tg, kb = ka + 4;
                        // B fragment: row k, column n=g -> input w index w0 + g + offw
                        const int wa = w0 + g + s_off[p][ka < 24 ? ka : 0][2];
                        const int wb = w0 + g + s_off[p][kb < 24 ? kb : 0][2];
                        const bool va = (ka < Kp) && ((unsigned)wa < (unsigned)W);
                        const bool vb = (kb < Kp) && ((unsigned)wb < (unsigned)W);
                        const int da = d + s_off[p][ka < 24 ? ka : 0][0];
                        const int ha = h + s_off[p][ka < 24 ? ka : 0][1];
                        const int db = d + s_off[p][kb < 24 ? kb : 0][0];
                        const int hb = h + s_off[p][kb < 24 ? kb : 0][1];
                        const bool vda = va && ((unsigned)da < (unsigned)D)
                                       && ((unsigned)ha < (unsigned)H);
                        const bool vdb = vb && ((unsigned)db < (unsigned)D)
                                       && ((unsigned)hb < (unsigned)H);
                        const long long offa = (long long)s_ci[p][ka < 24 ? ka : 0] * DHW
                                             + (long long)da * HW + (long long)ha * W + wa;
                        const long long offb = (long long)s_ci[p][kb < 24 ? kb : 0] * DHW
                                             + (long long)db * HW + (long long)hb * W + wb;
                        const float bv0 = vda ? __ldg(xb + offa) : 0.0f;
                        const float bv1 = vdb ? __ldg(xb + offb) : 0.0f;
                        asm volatile(
                            "mma.sync.aligned.m16n8k8.row.col.f32.tf32.tf32.f32 "
                            "{%0,%1,%2,%3}, {%4,%5,%6,%7}, {%8,%9}, {%0,%1,%2,%3};\n"
                            : "+f"(acc[p][0]), "+f"(acc[p][1]),
                              "+f"(acc[p][2]), "+f"(acc[p][3])
                            : "r"(__float_as_uint(a0[f])), "r"(__float_as_uint(a1[f])),
                              "r"(__float_as_uint(a2[f])), "r"(__float_as_uint(a3[f])),
                              "r"(__float_as_uint(bv0)), "r"(__float_as_uint(bv1)));
                        ++f;
                    }
                }
            }

            // MaxPool3d across the 8 parity classes, then add the conv bias.
            float r0 = acc[0][0], r1 = acc[0][1], r2 = acc[0][2], r3 = acc[0][3];
            #pragma unroll
            for (int p = 1; p < 8; ++p) {
                r0 = fmaxf(r0, acc[p][0]);
                r1 = fmaxf(r1, acc[p][1]);
                r2 = fmaxf(r2, acc[p][2]);
                r3 = fmaxf(r3, acc[p][3]);
            }
            r0 += b0c; r1 += b0c; r2 += b8c; r3 += b8c;

            // Softmax across the 16 channels (rows) of each column.
            float m01 = fmaxf(r0, r1);
            float m23 = fmaxf(r2, r3);
            m01 = fmaxf(m01, __shfl_xor_sync(0xffffffffu, m01, 4));
            m23 = fmaxf(m23, __shfl_xor_sync(0xffffffffu, m23, 4));
            m01 = fmaxf(m01, __shfl_xor_sync(0xffffffffu, m01, 8));
            m23 = fmaxf(m23, __shfl_xor_sync(0xffffffffu, m23, 8));
            m01 = fmaxf(m01, __shfl_xor_sync(0xffffffffu, m01, 16));
            m23 = fmaxf(m23, __shfl_xor_sync(0xffffffffu, m23, 16));

            float e0 = __expf(r0 - m01), e1 = __expf(r1 - m01);
            float e2 = __expf(r2 - m23), e3 = __expf(r3 - m23);
            float sA = e0 + e1, sB = e2 + e3;
            sA += __shfl_xor_sync(0xffffffffu, sA, 4);
            sB += __shfl_xor_sync(0xffffffffu, sB, 4);
            sA += __shfl_xor_sync(0xffffffffu, sA, 8);
            sB += __shfl_xor_sync(0xffffffffu, sB, 8);
            sA += __shfl_xor_sync(0xffffffffu, sA, 16);
            sB += __shfl_xor_sync(0xffffffffu, sB, 16);
            const float invA = __fdividef(1.0f, sA);
            const float invB = __fdividef(1.0f, sB);

            // Subtract per-channel constant, swish (y * sigmoid(y)), max over channels.
            float y0 = fmaf(e0, invA, -s0c);
            float y1 = fmaf(e1, invA, -s0c);
            float y2 = fmaf(e2, invB, -s8c);
            float y3 = fmaf(e3, invB, -s8c);
            float z0 = __fdividef(y0, 1.0f + __expf(-y0));
            float z1 = __fdividef(y1, 1.0f + __expf(-y1));
            float z2 = __fdividef(y2, 1.0f + __expf(-y2));
            float z3 = __fdividef(y3, 1.0f + __expf(-y3));
            float zA = fmaxf(z0, z1);
            float zB = fmaxf(z2, z3);
            zA = fmaxf(zA, __shfl_xor_sync(0xffffffffu, zA, 4));
            zB = fmaxf(zB, __shfl_xor_sync(0xffffffffu, zB, 4));
            zA = fmaxf(zA, __shfl_xor_sync(0xffffffffu, zA, 8));
            zB = fmaxf(zB, __shfl_xor_sync(0xffffffffu, zB, 8));
            zA = fmaxf(zA, __shfl_xor_sync(0xffffffffu, zA, 16));
            zB = fmaxf(zB, __shfl_xor_sync(0xffffffffu, zB, 16));

            if (g == 0) {
                outb[d * HW + h * W + w0 + 2 * tg]     = zA;
                outb[d * HW + h * W + w0 + 2 * tg + 1] = zB;
            }
        }
    }
}

torch::Tensor fused_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,
                            torch::Tensor sub, int64_t Dp, int64_t Hp, int64_t Wp)
{
    TORCH_CHECK(x.is_cuda() && weight.is_cuda() && bias.is_cuda() && sub.is_cuda(),
                "all tensors must be CUDA");
    TORCH_CHECK(x.scalar_type() == at::kFloat, "x must be float32");
    TORCH_CHECK(weight.size(0) == CI && weight.size(1) == CO && weight.size(2) == 3,
                "unexpected weight shape");

    const int64_t B = x.size(0);
    const int64_t D = x.size(2);
    const int64_t H = x.size(3);
    const int64_t W = x.size(4);

    auto out = at::empty({B, Dp, Hp, Wp}, x.options());
    const long long total = (long long)B * Dp * Hp * Wp;
    if (total == 0) return out;

    const int nwt = (int)((W + 7) / 8);
    const int tiles_per_b = (int)(D * H) * nwt;

    // Choose the per-warp chunk so that all SMs are saturated.
    int dev = 0;
    cudaGetDevice(&dev);
    static int nsm = 0;
    if (nsm == 0) cudaDeviceGetAttribute(&nsm, cudaDevAttrMultiProcessorCount, dev);
    const int want_blocks = nsm * 16;
    int T = (tiles_per_b + want_blocks - 1) / want_blocks;
    if (T < 1) T = 1;
    const int nblocks = (tiles_per_b + 4 * T - 1) / (4 * T);
    const int gx = nblocks > 65535 ? 65535 : nblocks;

    const dim3 grid((unsigned)gx, 1u, (unsigned)B);
    const dim3 block(128, 1, 1);
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    fused_convT_pool_softmax_swish_cmax_mma_kernel<<<grid, block, 0, stream>>>(
        x.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(),
        sub.data_ptr<float>(), out.data_ptr<float>(),
        (int)D, (int)H, (int)W, nwt, tiles_per_b, nblocks, T);
    return out;
}
"""

_fused_cpp_src = (
    "torch::Tensor fused_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, "
    "torch::Tensor sub, int64_t Dp, int64_t Hp, int64_t Wp);"
)

_fused_ext = load_inline(
    name="fused_convT3d_pool_softmax_sub_swish_cmax_mma_v4",
    cpp_sources=_fused_cpp_src,
    cuda_sources=_fused_cuda_src,
    functions=["fused_forward"],
    verbose=False,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3", "-arch=sm_80"],
)


class ModelNew(nn.Module):
    """
    Optimized version of Model. Uses a single fully-fused TF32 tensor-core CUDA
    kernel that performs ConvTranspose3d + MaxPool3d + Softmax + Subtract +
    Swish + channel-wise Max without materializing any intermediate tensor in
    global memory. Falls back to the original PyTorch ops for unsupported
    configurations.
    """
    def __init__(self, in_channels, out_channels, kernel_size, stride, padding,
                 output_padding, pool_kernel_size, pool_stride, pool_padding):
        super(ModelNew, self).__init__()
        self.conv_transpose = nn.ConvTranspose3d(in_channels, out_channels, kernel_size,
                                                 stride=stride, padding=padding,
                                                 output_padding=output_padding)
        self.max_pool = nn.MaxPool3d(kernel_size=pool_kernel_size, stride=pool_stride,
                                     padding=pool_padding)
        self.subtract = nn.Parameter(torch.randn(out_channels))

        def _i(v):
            return v[0] if isinstance(v, (tuple, list)) else v

        cfg = (in_channels, out_channels, _i(kernel_size), _i(stride), _i(padding),
               _i(output_padding), _i(pool_kernel_size), _i(pool_stride), _i(pool_padding))
        self._fused_ok = (cfg == (3, 16, 3, 2, 1, 1, 2, 2, 0))
        self._zero_bias = None

    def forward(self, x):
        if self._fused_ok and x.is_cuda and x.dtype == torch.float32:
            B, D, H, W = x.shape[0], x.shape[2], x.shape[3], x.shape[4]
            # conv-transpose output size: (n-1)*2 - 2 + 3 + 1 = 2n
            Dc = (D - 1) * 2 - 2 + 3 + 1
            Hc = (H - 1) * 2 - 2 + 3 + 1
            Wc = (W - 1) * 2 - 2 + 3 + 1
            # max-pool (k=2, s=2, p=0) output size
            Dp = (Dc - 2) // 2 + 1
            Hp = (Hc - 2) // 2 + 1
            Wp = (Wc - 2) // 2 + 1
            if (x.numel() < 2**31 and B <= 65535 and Dp == D and Hp == H and Wp == W
                    and B * D * H * W < 2**31):
                bias = self.conv_transpose.bias
                if bias is None:
                    if self._zero_bias is None or self._zero_bias.device != x.device:
                        self._zero_bias = torch.zeros(16, device=x.device, dtype=torch.float32)
                    bias = self._zero_bias
                return _fused_ext.fused_forward(
                    x.contiguous(),
                    self.conv_transpose.weight.contiguous(),
                    bias.contiguous(),
                    self.subtract.contiguous(),
                    Dp, Hp, Wp)

        # Generic fallback: original PyTorch operator sequence.
        x = self.conv_transpose(x)
        x = self.max_pool(x)
        x = torch.softmax(x, dim=1)
        x = x - self.subtract.view(1, -1, 1, 1, 1)
        x = torch.sigmoid(x) * x
        x = torch.max(x, dim=1)[0]
        return x
```
Result of evaluating it:
The solution compiled but failed at run time. Error output:
```
torch.AcceleratorError: CUDA error: an illegal memory access was encountered
Search for `cudaErrorIllegalAddress' in https://docs.nvidia.com/cuda/cuda-runtime-api/group__CUDART__TYPES.html for more information.
CUDA kernel errors might be asynchronously reported at some other API call, so the stacktrace below might be incorrect.
For debugging consider passing CUDA_LAUNCH_BLOCKING=1
The CUDA driver logged these messages, which may provide useful details:
Sticky error detected
Returning 700 (CUDA_ERROR_ILLEGAL_ADDRESS) from cuMemFree_v2
```

The fastest correct solution so far (46.556x speedup) is:
```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

# Fully fused CUDA kernel v3:
#   ConvTranspose3d (k=3, s=2, p=1, op=1) -> MaxPool3d (2,2,0) -> Softmax(dim=1)
#   -> per-channel subtract -> Swish -> Max over channels
#
# Optimizations over the previous fused version:
#  * Division-free thread mapping: a 3D grid gives (b, pd, ph) directly from
#    block indices and (pw) from the thread x-index, so the kernel contains no
#    integer div/mod at all and uses 32-bit addressing throughout.
#  * The softmax + swish epilogue no longer dominates: exponentials use the
#    __expf intrinsic (MUFU-based, ~1e-6 relative error, far inside the
#    tolerance that already permits TF32), reciprocals/divisions use
#    __fdividef, and the per-channel exponentials computed for the softmax
#    denominator are kept in registers and reused for the probabilities
#    (the reference recomputed them, doubling the transcendental count).
#  * Bias is folded after the pool max (bc + max8 instead of 8 adds + max),
#    keeping the FP32 pipe (the binding resource) as lean as possible.

_fused_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>
#include <cfloat>

#define CI 3
#define CO 16
#define KPAD 28

__global__ __launch_bounds__(256) void fused_convT_pool_softmax_swish_cmax_kernel(
    const float* __restrict__ x,        // [B, CI, D, H, W]
    const float* __restrict__ weight,   // [CI, CO, 3, 3, 3]
    const float* __restrict__ bias,     // [CO]
    const float* __restrict__ sub,      // [CO]
    float* __restrict__ out,            // [B, Dp, Hp, Wp]
    const int D, const int H, const int W,
    const int Dp, const int Hp, const int Wp)
{
    __shared__ __align__(16) float Ws[CI * CO * KPAD];
    __shared__ float bs[CO];
    __shared__ float ss[CO];

    const int tid = threadIdx.y * 32 + threadIdx.x;
    for (int i = tid; i < CI * CO * 27; i += 256) {
        const int g = i / 27;
        const int k = i - g * 27;
        Ws[g * KPAD + k] = weight[i];
    }
    if (tid < CI * CO) Ws[tid * KPAD + 27] = 0.0f;
    if (tid < CO) { bs[tid] = bias[tid]; ss[tid] = sub[tid]; }
    __syncthreads();

    const int pw = blockIdx.x * 32 + threadIdx.x;
    const int ph = blockIdx.y * 8  + threadIdx.y;
    if (pw >= Wp || ph >= Hp) return;
    const int pd = blockIdx.z % Dp;
    const int b  = blockIdx.z / Dp;

    const int HW  = H * W;
    const int DHW = D * HW;
    const float* xb = x + b * CI * DHW;

    // Load the 2x2x2xCI input block this output voxel depends on.
    float xv[CI][2][2][2];
    #pragma unroll
    for (int ci = 0; ci < CI; ++ci) {
        #pragma unroll
        for (int id = 0; id < 2; ++id) {
            #pragma unroll
            for (int ih = 0; ih < 2; ++ih) {
                #pragma unroll
                for (int iw = 0; iw < 2; ++iw) {
                    const int ii = pd + id;
                    const int jj = ph + ih;
                    const int kk = pw + iw;
                    float v = 0.0f;
                    if (ii < D && jj < H && kk < W) {
                        v = __ldg(xb + ci * DHW + ii * HW + jj * W + kk);
                    }
                    xv[ci][id][ih][iw] = v;
                }
            }
        }
    }

    // For each output channel: compute the 2x2x2 conv-transpose block and pool it.
    // Per-dim mapping of kernel offset k -> (block offset delta, input offset):
    //   k=0 -> (delta=1, i_off=1), k=1 -> (delta=0, i_off=0), k=2 -> (delta=1, i_off=0)
    const float4* Ws4 = reinterpret_cast<const float4*>(Ws);
    float e[CO];
    float mx = -FLT_MAX;
    #pragma unroll
    for (int co = 0; co < CO; ++co) {
        float acc[8] = {0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f, 0.f};
        #pragma unroll
        for (int ci = 0; ci < CI; ++ci) {
            const float4* wbase = Ws4 + (ci * CO + co) * 7;
            #pragma unroll
            for (int t = 0; t < 7; ++t) {
                const float4 wv = wbase[t];
                const float wa[4] = {wv.x, wv.y, wv.z, wv.w};
                #pragma unroll
                for (int j = 0; j < 4; ++j) {
                    const int k = (t << 2) + j;
                    if (k < 27) {
                        const int kd = k / 9;
                        const int kh = (k / 3) % 3;
                        const int kw = k % 3;
                        const int dd = (kd == 1) ? 0 : 1;
                        const int id = (kd == 0) ? 1 : 0;
                        const int dh = (kh == 1) ? 0 : 1;
                        const int ih = (kh == 0) ? 1 : 0;
                        const int dw = (kw == 1) ? 0 : 1;
                        const int iw = (kw == 0) ? 1 : 0;
                        acc[(dd << 2) | (dh << 1) | dw] =
                            fmaf(xv[ci][id][ih][iw], wa[j],
                                 acc[(dd << 2) | (dh << 1) | dw]);
                    }
                }
            }
        }
        float m = acc[0];
        #pragma unroll
        for (int a = 1; a < 8; ++a) m = fmaxf(m, acc[a]);
        m += bs[co];
        e[co] = m;              // temporarily holds the pooled logit
        mx = fmaxf(mx, m);
    }

    // Softmax across channels: reuse the exponentials for the probabilities.
    float sum = 0.0f;
    #pragma unroll
    for (int c = 0; c < CO; ++c) {
        e[c] = __expf(e[c] - mx);
        sum += e[c];
    }
    const float inv = __fdividef(1.0f, sum);

    // Subtract, swish (y * sigmoid(y)) and max over channels.
    float res = -FLT_MAX;
    #pragma unroll
    for (int c = 0; c < CO; ++c) {
        const float y = fmaf(e[c], inv, -ss[c]);
        const float z = __fdividef(y, 1.0f + __expf(-y));
        res = fmaxf(res, z);
    }

    out[(b * Dp + pd) * (Hp * Wp) + ph * Wp + pw] = res;
}

torch::Tensor fused_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias,
                            torch::Tensor sub, int64_t Dp, int64_t Hp, int64_t Wp)
{
    TORCH_CHECK(x.is_cuda() && weight.is_cuda() && bias.is_cuda() && sub.is_cuda(),
                "all tensors must be CUDA");
    TORCH_CHECK(x.scalar_type() == at::kFloat, "x must be float32");
    TORCH_CHECK(weight.size(0) == CI && weight.size(1) == CO && weight.size(2) == 3,
                "unexpected weight shape");

    const int64_t B  = x.size(0);
    const int64_t D  = x.size(2);
    const int64_t H  = x.size(3);
    const int64_t W  = x.size(4);

    auto out = at::empty({B, Dp, Hp, Wp}, x.options());
    const long long total = (long long)B * Dp * Hp * Wp;
    if (total == 0) return out;

    const dim3 block(32, 8, 1);
    const dim3 grid((unsigned)((Wp + 31) / 32),
                    (unsigned)((Hp + 7) / 8),
                    (unsigned)(B * Dp));
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    fused_convT_pool_softmax_swish_cmax_kernel<<<grid, block, 0, stream>>>(
        x.data_ptr<float>(), weight.data_ptr<float>(), bias.data_ptr<float>(),
        sub.data_ptr<float>(), out.data_ptr<float>(),
        (int)D, (int)H, (int)W, (int)Dp, (int)Hp, (int)Wp);
    return out;
}
"""

_fused_cpp_src = (
    "torch::Tensor fused_forward(torch::Tensor x, torch::Tensor weight, torch::Tensor bias, "
    "torch::Tensor sub, int64_t Dp, int64_t Hp, int64_t Wp);"
)

_fused_ext = load_inline(
    name="fused_convT3d_pool_softmax_sub_swish_cmax_v3",
    cpp_sources=_fused_cpp_src,
    cuda_sources=_fused_cuda_src,
    functions=["fused_forward"],
    verbose=False,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
)


class ModelNew(nn.Module):
    """
    Optimized version of Model. Uses a single fully-fused CUDA kernel that
    performs ConvTranspose3d + MaxPool3d + Softmax + Subtract + Swish +
    channel-wise Max without materializing any intermediate tensor in global
    memory. Falls back to the original PyTorch ops for unsupported configs.
    """
    def __init__(self, in_channels, out_channels, kernel_size, stride, padding,
                 output_padding, pool_kernel_size, pool_stride, pool_padding):
        super(ModelNew, self).__init__()
        self.conv_transpose = nn.ConvTranspose3d(in_channels, out_channels, kernel_size,
                                                 stride=stride, padding=padding,
                                                 output_padding=output_padding)
        self.max_pool = nn.MaxPool3d(kernel_size=pool_kernel_size, stride=pool_stride,
                                     padding=pool_padding)
        self.subtract = nn.Parameter(torch.randn(out_channels))

        def _i(v):
            return v[0] if isinstance(v, (tuple, list)) else v

        cfg = (in_channels, out_channels, _i(kernel_size), _i(stride), _i(padding),
               _i(output_padding), _i(pool_kernel_size), _i(pool_stride), _i(pool_padding))
        self._fused_ok = (cfg == (3, 16, 3, 2, 1, 1, 2, 2, 0))
        self._zero_bias = None

    def forward(self, x):
        if self._fused_ok and x.is_cuda and x.dtype == torch.float32:
            B, D, H, W = x.shape[0], x.shape[2], x.shape[3], x.shape[4]
            # conv-transpose output size: (n-1)*2 - 2 + 3 + 1 = 2n
            Dc = (D - 1) * 2 - 2 + 3 + 1
            Hc = (H - 1) * 2 - 2 + 3 + 1
            Wc = (W - 1) * 2 - 2 + 3 + 1
            # max-pool (k=2, s=2, p=0) output size
            Dp = (Dc - 2) // 2 + 1
            Hp = (Hc - 2) // 2 + 1
            Wp = (Wc - 2) // 2 + 1
            # 32-bit kernel indexing + 3D grid limits
            if (x.numel() < 2**31 and B * Dp <= 65535
                    and B * D * H * W < 2**31 and Dp * Hp * Wp * B < 2**31):
                bias = self.conv_transpose.bias
                if bias is None:
                    if self._zero_bias is None or self._zero_bias.device != x.device:
                        self._zero_bias = torch.zeros(16, device=x.device, dtype=torch.float32)
                    bias = self._zero_bias
                return _fused_ext.fused_forward(
                    x.contiguous(),
                    self.conv_transpose.weight.contiguous(),
                    bias.contiguous(),
                    self.subtract.contiguous(),
                    Dp, Hp, Wp)

        # Generic fallback: original PyTorch operator sequence.
        x = self.conv_transpose(x)
        x = self.max_pool(x)
        x = torch.softmax(x, dim=1)
        x = x - self.subtract.view(1, -1, 1, 1, 1)
        x = torch.sigmoid(x) * x
        x = torch.max(x, dim=1)[0]
        return x
```

Write an improved complete ModelNew: fix any error, and if the solution is already correct, make it faster. Output the new code in one ```python codeblock. Just the model code, no other text, and NO testing code.


Precision rule: the timed forward must compute in FP32 (TF32 tensor-core math is allowed, as in the PyTorch reference). Do not cast inputs, weights or intermediate results to float16 or bfloat16 and do not call half-precision kernels in forward().
