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 single matrix multiplication (C = A * B)
    """
    def __init__(self):
        super(Model, self).__init__()
    
    def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
        """
        Performs matrix multiplication.

        Args:
            A: Input tensor of shape (M, K).
            B: Input tensor of shape (K, N).

        Returns:
            Output tensor of shape (M, N).
        """
        return torch.matmul(A, B)

M = 1024 * 2
K = 4096 * 2
N = 2048 * 2

def get_inputs():
    A = torch.rand(M, K)
    B = torch.rand(K, N)
    return [A, B]

def get_init_inputs():
    return []  # No special initialization inputs needed
    ```
    
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 os
import glob

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


def _cublas_ldflags():
    """Locate libcublas shipped with the pip CUDA toolkit (or a system CUDA)."""
    sp = os.path.dirname(os.path.dirname(os.path.abspath(torch.__file__)))
    patterns = [
        os.path.join(sp, "nvidia", "*", "lib", "libcublas.so*"),
        os.path.join(sp, "nvidia", "*", "lib64", "libcublas.so*"),
        "/usr/local/cuda/lib64/libcublas.so*",
        "/usr/local/cuda/lib/libcublas.so*",
        "/usr/lib/x86_64-linux-gnu/libcublas.so*",
        "/usr/lib64/libcublas.so*",
    ]
    found = []
    for pat in patterns:
        found.extend(sorted(glob.glob(pat)))
    found = [f for f in found if "stubs" not in f]
    if not found:
        return ["-lcublas"]
    unversioned = [f for f in found if f.endswith(".so")]
    lib = unversioned[0] if unversioned else found[0]
    libdir = os.path.dirname(lib)
    return ["-L" + libdir, "-Wl,-rpath," + libdir, lib]


# Optimized GEMM: cast FP32 inputs to FP16 and run a cuBLAS tensor-core GEMM
# (cublasGemmEx) with FP32 accumulation and FP32 output. FP16 has the same
# 10-bit mantissa as TF32, so precision is TF32-comparable while tensor-core
# throughput is roughly doubled. A private cached cuBLAS handle (with a large
# workspace) is used so no internal ATen cuBLAS headers are required.
_cpp_source = "torch::Tensor matmul_cuda(torch::Tensor A, torch::Tensor B);"

_cuda_source = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#include <cublas_v2.h>
#include <cuda_runtime.h>
#include <mutex>

namespace {

cublasHandle_t get_blas_handle(int device, cudaStream_t stream) {
    static cublasHandle_t handles[64] = {nullptr};
    static void* workspaces[64] = {nullptr};
    static std::mutex mu;
    if (device < 0 || device >= 64) return nullptr;
    if (handles[device] == nullptr) {
        std::lock_guard<std::mutex> lock(mu);
        if (handles[device] == nullptr) {
            const c10::cuda::CUDAGuard guard(device);
            cublasHandle_t h = nullptr;
            if (cublasCreate(&h) != CUBLAS_STATUS_SUCCESS) return nullptr;
            const size_t ws_bytes = (size_t)32 * 1024 * 1024;
            if (cudaMalloc(&workspaces[device], ws_bytes) == cudaSuccess) {
                cublasSetWorkspace(h, workspaces[device], ws_bytes);
            }
            handles[device] = h;
        }
    }
    cublasSetStream(handles[device], stream);
    return handles[device];
}

}  // namespace

torch::Tensor matmul_cuda(torch::Tensor A, torch::Tensor B) {
    TORCH_CHECK(A.is_cuda() && B.is_cuda(), "A and B must be CUDA tensors");
    TORCH_CHECK(A.dim() == 2 && B.dim() == 2, "A and B must be 2-D");
    TORCH_CHECK(A.scalar_type() == at::kFloat && B.scalar_type() == at::kFloat,
                "A and B must be float32");

    const c10::cuda::OptionalCUDAGuard device_guard(A.device());

    auto Ac = A.contiguous();
    auto Bc = B.contiguous();
    const int64_t M = Ac.size(0);
    const int64_t K = Ac.size(1);
    const int64_t N = Bc.size(1);
    TORCH_CHECK(Bc.size(0) == K, "inner dimension mismatch");

    auto C = at::empty({M, N}, Ac.options());
    if (M == 0 || N == 0) return C;
    if (K == 0) { C.zero_(); return C; }

    // The fast tensor-core path wants 8-element divisibility and one device.
    const bool fast = (((M | N | K) & 7) == 0) &&
                      (Ac.get_device() == Bc.get_device());
    if (fast) {
        auto Ah = Ac.to(at::kHalf);
        auto Bh = Bc.to(at::kHalf);
        cublasHandle_t handle = get_blas_handle(
            (int)Ac.get_device(), at::cuda::getCurrentCUDAStream().stream());
        if (handle != nullptr) {
            const float alpha = 1.0f;
            const float beta = 0.0f;
            // Row-major C(MxN) = A(MxK) * B(KxN) is computed as the
            // column-major gemm C^T = B^T * A^T with m=N, n=M, k=K.
            cublasStatus_t status = cublasGemmEx(
                handle, CUBLAS_OP_N, CUBLAS_OP_N,
                (int)N, (int)M, (int)K,
                &alpha,
                Bh.data_ptr(), CUDA_R_16F, (int)N,
                Ah.data_ptr(), CUDA_R_16F, (int)K,
                &beta,
                C.data_ptr(), CUDA_R_32F, (int)N,
                CUBLAS_COMPUTE_32F, CUBLAS_GEMM_DEFAULT);
            if (status == CUBLAS_STATUS_SUCCESS) {
                return C;
            }
        }
        // Fallback: fp16 matmul (fp32 accumulate) then upcast to fp32.
        C.copy_(at::matmul(Ah, Bh));
        return C;
    }

    C.copy_(at::matmul(Ac, Bc));
    return C;
}
"""

_matmul_ext = load_inline(
    name="matmul_gemmex_fp16tc_v3",
    cpp_sources=_cpp_source,
    cuda_sources=_cuda_source,
    functions=["matmul_cuda"],
    verbose=False,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
    extra_ldflags=_cublas_ldflags(),
)


class ModelNew(nn.Module):
    """
    Optimized model that performs a single matrix multiplication (C = A * B)
    using FP16 tensor cores via cuBLAS GemmEx with FP32 accumulation/output.
    """

    def __init__(self):
        super(ModelNew, self).__init__()
        self._matmul = _matmul_ext

    def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
        if (A.is_cuda and B.is_cuda and A.dtype == torch.float32
                and B.dtype == torch.float32 and A.dim() == 2 and B.dim() == 2):
            return self._matmul.matmul_cuda(A, B)
        return torch.matmul(A, B)
```
Result of evaluating it:
The solution is correct on the test inputs. Mean runtime 0.747 ms; the PyTorch reference (with TF32 enabled) takes 1.15 ms; speedup 1.539x.
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_nn_align8>(cutlass_8 | 498.3 | 1 |
| void at::native::vectorized_elementwise_kernel<4, at::native::float16_copy_kernel_cuda(at: | 227.1 | 2 |

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.
