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):
    """
    Simple model that performs a matrix multiplication, adds a value, applies Swish, Tanh, GELU, and Hardtanh activation functions.
    """
    def __init__(self, in_features, out_features, add_value_shape):
        super(Model, self).__init__()
        self.matmul = nn.Linear(in_features, out_features)
        self.add_value = nn.Parameter(torch.randn(add_value_shape)) 

    def forward(self, x):
        x = self.matmul(x)
        x = x + self.add_value
        x = torch.sigmoid(x) * x # Swish
        x = torch.tanh(x)
        x = torch.nn.functional.gelu(x) # GELU
        x = torch.nn.functional.hardtanh(x, min_val=-1, max_val=1) # Hardtanh
        return x

batch_size = 1024
in_features = 8192
out_features = 8192
add_value_shape = (out_features,)

def get_inputs():
    return [torch.rand(batch_size, in_features)]

def get_init_inputs():
    return [in_features, out_features, add_value_shape]
    ```
    
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 4 of 6 for this task. Your previous solution (turn 3) 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
import torch.nn.functional as F
from torch.utils.cpp_extension import load_inline

# Strategy (incremental refinement of the previous 2.08x solution):
#   * Keep the FP16 tensor-core cuBLAS GEMM with FP32 accumulate/output
#     (it runs at ~76% of A100 FP16 peak; the dominant cost).
#   * Move the FP32->FP16 input cast out of PyTorch/aten and into the
#     extension as a custom vectorized kernel (float4 -> 2x half2). This
#     removes one Python->aten dispatch from the hot path and uses a
#     higher-bandwidth cast.
#   * Keep bias+add_value pre-added and cached, weight fp16 copy cached.
#   * Epilogue kernel now processes 2 float4 per thread (more memory-level
#     parallelism) and keeps the whole activation chain
#     (Swish -> Tanh -> GELU(erf) -> Hardtanh) fused in one pass.
#   * Fewer Python ops per forward: a single extension call performs
#     cast + GEMM + epilogue.

cuda_source = r"""
#include <torch/extension.h>
#include <ATen/ATen.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cublas_v2.h>

namespace {

__device__ __forceinline__ float fused_act(float v) {
    // Swish: v * sigmoid(v) = v / (1 + exp(-v))
    float sv = v / (1.0f + expf(-v));
    // Tanh
    float tv = tanhf(sv);
    // GELU (exact erf form, matches F.gelu default)
    float gv = 0.5f * tv * (1.0f + erff(tv * 0.70710678118654752440f));
    // Hardtanh clamp to [-1, 1]
    return fminf(fmaxf(gv, -1.0f), 1.0f);
}

__global__ void cast_f32_f16_vec4_kernel(const float4* __restrict__ in,
                                         __half2* __restrict__ out,
                                         int n4) {
    int i = blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n4) {
        float4 v = in[i];
        out[2 * i]     = __floats2half2_rn(v.x, v.y);
        out[2 * i + 1] = __floats2half2_rn(v.z, v.w);
    }
}

__global__ void cast_f32_f16_scalar_kernel(const float* __restrict__ in,
                                           __half* __restrict__ out,
                                           long long n) {
    long long i = (long long)blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        out[i] = __float2half_rn(in[i]);
    }
}

// Each thread handles two adjacent float4 (8 columns). Requires cols4 even.
__global__ void epilogue_vec4x2_kernel(float4* __restrict__ data,
                                       const float4* __restrict__ ba,
                                       int cols4) {
    int p = blockIdx.x * blockDim.x + threadIdx.x;
    int c4 = 2 * p;
    if (c4 >= cols4) return;
    long long base = (long long)blockIdx.y * cols4 + c4;
    float4 v0 = data[base];
    float4 v1 = data[base + 1];
    float4 b0 = ba[c4];
    float4 b1 = ba[c4 + 1];
    v0.x = fused_act(v0.x + b0.x);
    v0.y = fused_act(v0.y + b0.y);
    v0.z = fused_act(v0.z + b0.z);
    v0.w = fused_act(v0.w + b0.w);
    v1.x = fused_act(v1.x + b1.x);
    v1.y = fused_act(v1.y + b1.y);
    v1.z = fused_act(v1.z + b1.z);
    v1.w = fused_act(v1.w + b1.w);
    data[base]     = v0;
    data[base + 1] = v1;
}

__global__ void epilogue_scalar_kernel(float* __restrict__ data,
                                       const float* __restrict__ ba,
                                       int cols) {
    int col = blockIdx.x * blockDim.x + threadIdx.x;
    if (col < cols) {
        long long idx = (long long)blockIdx.y * cols + col;
        data[idx] = fused_act(data[idx] + ba[col]);
    }
}

}  // namespace

torch::Tensor fused_forward_cuda(torch::Tensor x,
                                 torch::Tensor w_half,
                                 torch::Tensor bias_add) {
    TORCH_CHECK(x.is_cuda() && w_half.is_cuda() && bias_add.is_cuda(),
                "all inputs must be CUDA tensors");
    TORCH_CHECK(x.scalar_type() == at::kFloat, "x must be float32");
    TORCH_CHECK(w_half.scalar_type() == at::kHalf, "w_half must be half");
    TORCH_CHECK(bias_add.scalar_type() == at::kFloat,
                "bias_add must be float32");

    auto xc = x.is_contiguous() ? x : x.contiguous();
    auto wc = w_half.is_contiguous() ? w_half : w_half.contiguous();
    auto bc = bias_add.is_contiguous() ? bias_add : bias_add.contiguous();

    const int64_t M = xc.size(0);
    const int64_t K = xc.size(1);
    const int64_t N = wc.size(0);
    TORCH_CHECK(wc.size(1) == K, "weight shape mismatch");
    TORCH_CHECK(bc.numel() == N, "bias_add shape mismatch");

    auto out = torch::empty({M, N}, xc.options());
    if (M == 0 || N == 0) {
        return out;
    }

    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    const int threads = 256;

    // 1) FP32 -> FP16 cast of the input (vectorized when possible).
    auto x_half = torch::empty({M, K}, xc.options().dtype(at::kHalf));
    const long long numel = (long long)M * K;
    if ((numel & 3LL) == 0 && (numel >> 2) <= (long long)INT_MAX) {
        int n4 = (int)(numel >> 2);
        int blocks = (n4 + threads - 1) / threads;
        cast_f32_f16_vec4_kernel<<<blocks, threads, 0, stream>>>(
            reinterpret_cast<const float4*>(xc.data_ptr<float>()),
            reinterpret_cast<__half2*>(x_half.data_ptr<at::Half>()), n4);
    } else {
        long long blocks = (numel + threads - 1) / threads;
        TORCH_CHECK(blocks <= (long long)INT_MAX, "input too large");
        cast_f32_f16_scalar_kernel<<<(unsigned int)blocks, threads, 0, stream>>>(
            xc.data_ptr<float>(),
            reinterpret_cast<__half*>(x_half.data_ptr<at::Half>()), numel);
    }

    // 2) GEMM: out(M,N) = x(M,K) @ w(N,K)^T via column-major cuBLAS view:
    //      out_cm(N,M) = op_T(w_cm(K,N)) @ x_cm(K,M)
    cublasHandle_t handle = at::cuda::getCurrentCUDABlasHandle();
    cublasSetStream(handle, stream);
    const float alpha = 1.0f;
    const float beta = 0.0f;
    cublasStatus_t status = cublasGemmEx(
        handle,
        CUBLAS_OP_T, CUBLAS_OP_N,
        (int)N, (int)M, (int)K,
        &alpha,
        wc.data_ptr<at::Half>(), CUDA_R_16F, (int)K,
        x_half.data_ptr<at::Half>(), CUDA_R_16F, (int)K,
        &beta,
        out.data_ptr<float>(), CUDA_R_32F, (int)N,
        CUBLAS_COMPUTE_32F,
        CUBLAS_GEMM_DEFAULT);
    TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS,
                "cublasGemmEx failed with status ", (int)status);

    // 3) Fused epilogue: out = hardtanh(gelu(tanh(swish(out + bias)))).
    float* out_ptr = out.data_ptr<float>();
    const float* ba_ptr = bc.data_ptr<float>();
    const int64_t maxRows = 65535;
    if ((N & 7) == 0) {
        int cols4 = (int)(N >> 2);
        int pairs = cols4 >> 1;
        int xblocks = (pairs + threads - 1) / threads;
        for (int64_t r0 = 0; r0 < M; r0 += maxRows) {
            int64_t rem = M - r0;
            int rows = (int)(rem < maxRows ? rem : maxRows);
            dim3 grid(xblocks, rows);
            epilogue_vec4x2_kernel<<<grid, threads, 0, stream>>>(
                reinterpret_cast<float4*>(out_ptr + (long long)r0 * N),
                reinterpret_cast<const float4*>(ba_ptr), cols4);
        }
    } else {
        int xblocks = (int)((N + threads - 1) / threads);
        for (int64_t r0 = 0; r0 < M; r0 += maxRows) {
            int64_t rem = M - r0;
            int rows = (int)(rem < maxRows ? rem : maxRows);
            dim3 grid(xblocks, rows);
            epilogue_scalar_kernel<<<grid, threads, 0, stream>>>(
                out_ptr + (long long)r0 * N, ba_ptr, (int)N);
        }
    }
    return out;
}
"""

cpp_source = (
    "torch::Tensor fused_forward_cuda(torch::Tensor x, "
    "torch::Tensor w_half, torch::Tensor bias_add);"
)

fused_module = load_inline(
    name="fused_linear_fp16_swish_tanh_gelu_hardtanh_v3",
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=["fused_forward_cuda"],
    verbose=False,
    extra_cuda_cflags=["-O3"],
    extra_ldflags=["-lcublas"],
)


class ModelNew(nn.Module):
    def __init__(self, in_features, out_features, add_value_shape):
        super(ModelNew, self).__init__()
        self.matmul = nn.Linear(in_features, out_features)
        self.add_value = nn.Parameter(torch.randn(add_value_shape))
        self._fn = fused_module.fused_forward_cuda
        # Caches (plain attributes, not part of state_dict).
        self._cached_w_half = None
        self._cached_w_key = None
        self._cached_ba = None
        self._cached_ba_key = None

    def _fallback(self, x):
        x = F.linear(x, self.matmul.weight, self.matmul.bias)
        x = x + self.add_value
        x = torch.sigmoid(x) * x  # Swish
        x = torch.tanh(x)
        x = F.gelu(x)  # GELU
        x = F.hardtanh(x, min_val=-1, max_val=1)  # Hardtanh
        return x

    def forward(self, x):
        w = self.matmul.weight
        if (not x.is_cuda) or x.dtype != torch.float32 or x.dim() != 2:
            return self._fallback(x)
        N, K = w.shape[0], w.shape[1]
        if (K % 8) != 0 or (N % 8) != 0 or w.device != x.device:
            return self._fallback(x)

        w_key = (w._version, w.data_ptr())
        if self._cached_w_key != w_key:
            self._cached_w_half = w.detach().to(torch.float16).contiguous()
            self._cached_w_key = w_key

        b = self.matmul.bias
        av = self.add_value
        if b is None:
            ba_key = (None, av._version, av.data_ptr())
            if self._cached_ba_key != ba_key:
                self._cached_ba = av.detach().clone().contiguous()
                self._cached_ba_key = ba_key
        else:
            ba_key = (b._version, av._version, b.data_ptr())
            if self._cached_ba_key != ba_key:
                self._cached_ba = (b.detach() + av.detach()).contiguous()
                self._cached_ba_key = ba_key

        return self._fn(x, self._cached_w_half, self._cached_ba)
```
Result of evaluating it:
The solution is correct on the test inputs. Mean runtime 0.697 ms; the PyTorch reference (with TF32 enabled) takes 1.45 ms; speedup 2.080x.
Profiler, top device kernels of one forward pass of your solution:
| device kernel | total device time (us) | calls |
|---|---|---|
| void cutlass::Kernel2<cutlass_80_tensorop_s16816gemm_f16_128x128_32x4_tn_align8>(cutlass_8 | 584.1 | 1 |
| (anonymous namespace)::epilogue_vec4x2_kernel(float4*, float4 const*, int) | 52.2 | 1 |
| (anonymous namespace)::cast_f32_f16_vec4_kernel(float4 const*, __half2*, int) | 42.6 | 1 |
| Memset (Device) | 1.2 | 1 |

The fastest correct solution so far (2.083x 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
import torch.nn.functional as F
from torch.utils.cpp_extension import load_inline

# Strategy:
#   * The previous version was bottlenecked by a full FP32 SGEMM (~7.8 ms)
#     because at::addmm did not pick up TF32 tensor-core math inside the
#     extension. We now drive cuBLAS directly via cublasGemmEx with
#     half-precision inputs and FP32 accumulate/output (tensor cores,
#     ~2x the throughput of TF32, same 10-bit input mantissa class of
#     accuracy that the TF32 reference baseline uses).
#   * The weight is converted to fp16 once and cached in the module
#     (it is constant across benchmark iterations); the input cast is a
#     cheap ~30 us bandwidth-bound kernel.
#   * bias + add_value are pre-added and cached, and the whole activation
#     chain (Swish -> Tanh -> GELU(erf) -> Hardtanh) is fused into a single
#     in-place vectorized epilogue kernel on the GEMM output, eliminating
#     ~5 extra global-memory round trips and kernel launches.

cuda_source = r"""
#include <torch/extension.h>
#include <ATen/ATen.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cublas_v2.h>

__device__ __forceinline__ float fused_act(float v) {
    // Swish: v * sigmoid(v) = v / (1 + exp(-v))
    float sv = v / (1.0f + expf(-v));
    // Tanh
    float tv = tanhf(sv);
    // GELU (exact erf form, matches F.gelu default)
    float gv = 0.5f * tv * (1.0f + erff(tv * 0.70710678118654752440f));
    // Hardtanh clamp to [-1, 1]
    return fminf(fmaxf(gv, -1.0f), 1.0f);
}

__global__ void fused_epilogue_vec4_kernel(float4* __restrict__ data,
                                           const float4* __restrict__ ba,
                                           int cols4) {
    int c4 = blockIdx.x * blockDim.x + threadIdx.x;
    int row = blockIdx.y;
    if (c4 < cols4) {
        long long idx = (long long)row * cols4 + c4;
        float4 v = data[idx];
        float4 b = ba[c4];
        v.x = fused_act(v.x + b.x);
        v.y = fused_act(v.y + b.y);
        v.z = fused_act(v.z + b.z);
        v.w = fused_act(v.w + b.w);
        data[idx] = v;
    }
}

__global__ void fused_epilogue_scalar_kernel(float* __restrict__ data,
                                             const float* __restrict__ ba,
                                             int cols) {
    int col = blockIdx.x * blockDim.x + threadIdx.x;
    int row = blockIdx.y;
    if (col < cols) {
        long long idx = (long long)row * cols + col;
        data[idx] = fused_act(data[idx] + ba[col]);
    }
}

torch::Tensor fused_forward_cuda(torch::Tensor x_half,
                                 torch::Tensor w_half,
                                 torch::Tensor bias_add) {
    TORCH_CHECK(x_half.is_cuda() && w_half.is_cuda() && bias_add.is_cuda(),
                "all inputs must be CUDA tensors");
    TORCH_CHECK(x_half.scalar_type() == at::kHalf &&
                w_half.scalar_type() == at::kHalf, "x/w must be half");
    TORCH_CHECK(bias_add.scalar_type() == at::kFloat, "bias_add must be float");
    TORCH_CHECK(x_half.is_contiguous() && w_half.is_contiguous() &&
                bias_add.is_contiguous(), "inputs must be contiguous");

    const int M = (int)x_half.size(0);
    const int K = (int)x_half.size(1);
    const int N = (int)w_half.size(0);
    TORCH_CHECK((int)w_half.size(1) == K, "weight shape mismatch");
    TORCH_CHECK(bias_add.numel() == N, "bias_add shape mismatch");

    auto out = torch::empty({M, N}, bias_add.options());
    if (M == 0 || N == 0) {
        return out;
    }

    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    cublasHandle_t handle = at::cuda::getCurrentCUDABlasHandle();
    cublasSetStream(handle, stream);

    // Row-major out(M,N) = x(M,K) @ w(N,K)^T computed through the
    // column-major cuBLAS view:
    //   out_cm(N,M) = op_T(w_cm(K,N)) @ x_cm(K,M)
    const float alpha = 1.0f;
    const float beta = 0.0f;
    cublasStatus_t status = cublasGemmEx(
        handle,
        CUBLAS_OP_T, CUBLAS_OP_N,
        N, M, K,
        &alpha,
        w_half.data_ptr<at::Half>(), CUDA_R_16F, K,
        x_half.data_ptr<at::Half>(), CUDA_R_16F, K,
        &beta,
        out.data_ptr<float>(), CUDA_R_32F, N,
        CUBLAS_COMPUTE_32F,
        CUBLAS_GEMM_DEFAULT);
    TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS,
                "cublasGemmEx failed with status ", (int)status);

    const int threads = 256;
    if ((N & 3) == 0) {
        int cols4 = N >> 2;
        dim3 grid((cols4 + threads - 1) / threads, M);
        fused_epilogue_vec4_kernel<<<grid, threads, 0, stream>>>(
            reinterpret_cast<float4*>(out.data_ptr<float>()),
            reinterpret_cast<const float4*>(bias_add.data_ptr<float>()),
            cols4);
    } else {
        dim3 grid((N + threads - 1) / threads, M);
        fused_epilogue_scalar_kernel<<<grid, threads, 0, stream>>>(
            out.data_ptr<float>(),
            bias_add.data_ptr<float>(),
            N);
    }
    return out;
}
"""

cpp_source = (
    "torch::Tensor fused_forward_cuda(torch::Tensor x_half, "
    "torch::Tensor w_half, torch::Tensor bias_add);"
)

fused_module = load_inline(
    name="fused_linear_fp16_swish_tanh_gelu_hardtanh",
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=["fused_forward_cuda"],
    verbose=False,
    extra_cuda_cflags=["-O3"],
    extra_ldflags=["-lcublas"],
)


class ModelNew(nn.Module):
    def __init__(self, in_features, out_features, add_value_shape):
        super(ModelNew, self).__init__()
        self.matmul = nn.Linear(in_features, out_features)
        self.add_value = nn.Parameter(torch.randn(add_value_shape))
        # Caches (plain attributes, not part of state_dict).
        self._cached_w_half = None
        self._cached_w_key = None
        self._cached_ba = None
        self._cached_ba_key = None

    def _fallback(self, x):
        x = F.linear(x, self.matmul.weight, self.matmul.bias)
        x = x + self.add_value
        x = torch.sigmoid(x) * x  # Swish
        x = torch.tanh(x)
        x = F.gelu(x)  # GELU
        x = F.hardtanh(x, min_val=-1, max_val=1)  # Hardtanh
        return x

    def forward(self, x):
        w = self.matmul.weight
        b = self.matmul.bias
        av = self.add_value

        if (not x.is_cuda) or x.dtype != torch.float32 or w.device != x.device \
                or (w.shape[1] % 8) != 0 or (w.shape[0] % 8) != 0:
            return self._fallback(x)

        w_key = (w._version, w.device, w.data_ptr())
        if self._cached_w_key != w_key:
            self._cached_w_half = w.detach().to(torch.float16).contiguous()
            self._cached_w_key = w_key

        ba_key = (b._version, av._version, b.device)
        if self._cached_ba_key != ba_key:
            self._cached_ba = (b.detach() + av.detach()).contiguous()
            self._cached_ba_key = ba_key

        x_half = x.contiguous().to(torch.float16)
        return fused_module.fused_forward_cuda(
            x_half, self._cached_w_half, self._cached_ba
        )
```

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.
