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.T)

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

def get_inputs():
    A = torch.rand(M, K)
    B = torch.rand(N, K)
    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 6 of 6 for this task. Your previous solution (turn 5) was:
```python
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline


_cpp_source = r"""
#include <torch/extension.h>
torch::Tensor matmul_nt_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 <c10/cuda/CUDAException.h>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cublasLt.h>
#include <algorithm>
#include <climits>
#include <cstdint>
#include <map>
#include <memory>
#include <tuple>

static void check_blas(cublasStatus_t status) {
    TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS,
                "cuBLASLt failed: ", static_cast<int>(status));
}

__device__ __forceinline__
unsigned int pack_pair(float x, float y) {
    const unsigned int lo = __half_as_ushort(__float2half_rn(x));
    const unsigned int hi = __half_as_ushort(__float2half_rn(y));
    return lo | (hi << 16);
}

__global__ __launch_bounds__(256)
void pack_half8(
    const float* __restrict__ a,
    const float* __restrict__ b,
    uint4* __restrict__ packed,
    int64_t a_vectors,
    int64_t total_vectors)
{
    const int64_t stride = static_cast<int64_t>(blockDim.x) * gridDim.x;
    for (int64_t i = static_cast<int64_t>(blockIdx.x) * blockDim.x
                     + threadIdx.x;
         i < total_vectors; i += stride) {
        const float4* src;
        int64_t offset;
        if (i < a_vectors) {
            src = reinterpret_cast<const float4*>(a);
            offset = i * 2;
        } else {
            src = reinterpret_cast<const float4*>(b);
            offset = (i - a_vectors) * 2;
        }
        const float4 x = src[offset];
        const float4 y = src[offset + 1];
        packed[i] = make_uint4(
            pack_pair(x.x, x.y),
            pack_pair(x.z, x.w),
            pack_pair(y.x, y.y),
            pack_pair(y.z, y.w));
    }
}

__global__ void pack_half_scalar(
    const float* __restrict__ a,
    const float* __restrict__ b,
    __half* __restrict__ packed,
    int64_t a_elements,
    int64_t total_elements)
{
    const int64_t stride = static_cast<int64_t>(blockDim.x) * gridDim.x;
    for (int64_t i = static_cast<int64_t>(blockIdx.x) * blockDim.x
                     + threadIdx.x;
         i < total_elements; i += stride) {
        packed[i] = __float2half_rn(
            i < a_elements ? a[i] : b[i - a_elements]);
    }
}

struct MatmulPlan {
    cublasLtHandle_t handle = nullptr;
    cublasLtMatmulDesc_t operation = nullptr;
    cublasLtMatrixLayout_t left = nullptr;
    cublasLtMatrixLayout_t right = nullptr;
    cublasLtMatrixLayout_t output = nullptr;
    cublasLtMatmulAlgo_t algorithm{};
    size_t workspace_size = 0;

    MatmulPlan(int m, int n, int k) {
        check_blas(cublasLtCreate(&handle));
        check_blas(cublasLtMatmulDescCreate(
            &operation, CUBLAS_COMPUTE_32F, CUDA_R_32F));

        const cublasOperation_t transpose = CUBLAS_OP_T;
        const cublasOperation_t identity = CUBLAS_OP_N;
        check_blas(cublasLtMatmulDescSetAttribute(
            operation, CUBLASLT_MATMUL_DESC_TRANSA,
            &transpose, sizeof(transpose)));
        check_blas(cublasLtMatmulDescSetAttribute(
            operation, CUBLASLT_MATMUL_DESC_TRANSB,
            &identity, sizeof(identity)));

        // Column-major C^T = B A^T.
        check_blas(cublasLtMatrixLayoutCreate(
            &left, CUDA_R_16F, k, n, k));
        check_blas(cublasLtMatrixLayoutCreate(
            &right, CUDA_R_16F, k, m, k));
        check_blas(cublasLtMatrixLayoutCreate(
            &output, CUDA_R_32F, n, m, n));

        cublasLtMatmulPreference_t preference;
        check_blas(cublasLtMatmulPreferenceCreate(&preference));

        const size_t workspace_limit = 64ULL * 1024 * 1024;
        check_blas(cublasLtMatmulPreferenceSetAttribute(
            preference, CUBLASLT_MATMUL_PREF_MAX_WORKSPACE_BYTES,
            &workspace_limit, sizeof(workspace_limit)));

        const uint64_t offset_bytes =
            static_cast<uint64_t>(m) * k * sizeof(__half);
        uint32_t left_alignment = 256;
        while (offset_bytes % left_alignment != 0)
            left_alignment >>= 1;
        const uint32_t alignment = 256;

        check_blas(cublasLtMatmulPreferenceSetAttribute(
            preference, CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_A_BYTES,
            &left_alignment, sizeof(left_alignment)));
        check_blas(cublasLtMatmulPreferenceSetAttribute(
            preference, CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_B_BYTES,
            &alignment, sizeof(alignment)));
        check_blas(cublasLtMatmulPreferenceSetAttribute(
            preference, CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_C_BYTES,
            &alignment, sizeof(alignment)));
        check_blas(cublasLtMatmulPreferenceSetAttribute(
            preference, CUBLASLT_MATMUL_PREF_MIN_ALIGNMENT_D_BYTES,
            &alignment, sizeof(alignment)));

        cublasLtMatmulHeuristicResult_t result{};
        int returned = 0;
        const cublasStatus_t status = cublasLtMatmulAlgoGetHeuristic(
            handle, operation, left, right, output, output,
            preference, 1, &result, &returned);
        cublasLtMatmulPreferenceDestroy(preference);
        check_blas(status);
        TORCH_CHECK(returned > 0, "No compatible cuBLASLt algorithm");
        check_blas(result.state);
        algorithm = result.algo;
        workspace_size = result.workspaceSize;
    }

    ~MatmulPlan() {
        if (output) cublasLtMatrixLayoutDestroy(output);
        if (right) cublasLtMatrixLayoutDestroy(right);
        if (left) cublasLtMatrixLayoutDestroy(left);
        if (operation) cublasLtMatmulDescDestroy(operation);
        if (handle) cublasLtDestroy(handle);
    }
};

torch::Tensor matmul_nt_cuda(torch::Tensor a, torch::Tensor b) {
    TORCH_CHECK(a.is_cuda() && b.is_cuda(), "Expected CUDA tensors");
    TORCH_CHECK(a.device() == b.device(), "Devices must match");
    TORCH_CHECK(a.scalar_type() == at::kFloat &&
                b.scalar_type() == at::kFloat, "Expected float32");
    TORCH_CHECK(a.dim() == 2 && b.dim() == 2, "Expected matrices");
    TORCH_CHECK(a.size(1) == b.size(1), "Inner dimensions must match");
    TORCH_CHECK(a.is_contiguous() && b.is_contiguous(),
                "Expected contiguous inputs");
    TORCH_CHECK(a.size(0) <= INT_MAX && b.size(0) <= INT_MAX &&
                a.size(1) <= INT_MAX, "Dimensions exceed cuBLAS limits");

    const c10::cuda::CUDAGuard guard(a.device());
    const int m = static_cast<int>(a.size(0));
    const int n = static_cast<int>(b.size(0));
    const int k = static_cast<int>(a.size(1));

    auto out = torch::empty({m, n}, a.options());
    if (m == 0 || n == 0)
        return out;
    if (k == 0) {
        out.zero_();
        return out;
    }

    using Key = std::tuple<int, int, int, int>;
    static thread_local std::map<Key, std::unique_ptr<MatmulPlan>> plans;
    const Key key(a.get_device(), m, n, k);
    auto it = plans.find(key);
    if (it == plans.end()) {
        it = plans.emplace(
            key, std::make_unique<MatmulPlan>(m, n, k)).first;
    }
    MatmulPlan& plan = *it->second;

    const int64_t a_elements = a.numel();
    const int64_t total = a_elements + b.numel();
    auto packed = torch::empty({total}, a.options().dtype(at::kHalf));
    auto workspace = torch::empty(
        {static_cast<int64_t>(plan.workspace_size)},
        a.options().dtype(at::kByte));

    const float* ap = a.data_ptr<float>();
    const float* bp = b.data_ptr<float>();
    __half* hp = reinterpret_cast<__half*>(packed.data_ptr<at::Half>());
    cudaStream_t stream = at::cuda::getCurrentCUDAStream(a.get_device());

    const bool vectorized =
        a_elements % 8 == 0 && b.numel() % 8 == 0 &&
        reinterpret_cast<uintptr_t>(ap) % 16 == 0 &&
        reinterpret_cast<uintptr_t>(bp) % 16 == 0;
    const int64_t work = vectorized ? total / 8 : total;
    const int blocks = static_cast<int>(
        std::min<int64_t>((work + 255) / 256, 4096));

    if (vectorized) {
        pack_half8<<<blocks, 256, 0, stream>>>(
            ap, bp, reinterpret_cast<uint4*>(hp),
            a_elements / 8, total / 8);
    } else {
        pack_half_scalar<<<blocks, 256, 0, stream>>>(
            ap, bp, hp, a_elements, total);
    }
    C10_CUDA_KERNEL_LAUNCH_CHECK();

    const float alpha = 1.0f;
    const float beta = 0.0f;
    check_blas(cublasLtMatmul(
        plan.handle, plan.operation,
        &alpha,
        hp + a_elements, plan.left,
        hp, plan.right,
        &beta,
        out.data_ptr<float>(), plan.output,
        out.data_ptr<float>(), plan.output,
        &plan.algorithm,
        plan.workspace_size ? workspace.data_ptr() : nullptr,
        plan.workspace_size, stream));

    return out;
}
"""

_matmul_extension = load_inline(
    name="custom_matmul_nt_half_lt_v5",
    cpp_sources=_cpp_source,
    cuda_sources=_cuda_source,
    functions=["matmul_nt_cuda"],
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
    extra_ldflags=["-lcublasLt"],
    verbose=False,
)


class ModelNew(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
        if (
            not A.is_cuda
            or not B.is_cuda
            or A.dtype != torch.float32
            or B.dtype != torch.float32
            or A.ndim != 2
            or B.ndim != 2
            or (
                torch.is_grad_enabled()
                and (A.requires_grad or B.requires_grad)
            )
        ):
            return torch.matmul(A, B.T)

        return _matmul_extension.matmul_nt_cuda(
            A.contiguous(), B.contiguous()
        )
```
Result of evaluating it:
The solution is correct on the test inputs. Mean runtime 0.761 ms; the PyTorch reference (with TF32 enabled) takes 1.15 ms; speedup 1.511x.
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 | 515.3 | 1 |
| pack_half8(float const*, float const*, uint4*, long, long) | 221.6 | 1 |

The fastest correct solution so far (1.513x speedup) is:
```python
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline


_cpp_source = r"""
#include <torch/extension.h>
torch::Tensor matmul_nt_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 <c10/cuda/CUDAException.h>
#include <cuda_runtime.h>
#include <cuda_fp16.h>
#include <cublas_v2.h>
#include <climits>
#include <cstdint>

__global__ void pack_half_vectorized(
    const float* __restrict__ a,
    const float* __restrict__ b,
    __half* __restrict__ packed,
    int64_t a_vectors,
    int64_t total_vectors)
{
    const int64_t stride = static_cast<int64_t>(blockDim.x) * gridDim.x;
    for (int64_t i = static_cast<int64_t>(blockIdx.x) * blockDim.x
                     + threadIdx.x;
         i < total_vectors; i += stride) {
        const float4 v = i < a_vectors
            ? reinterpret_cast<const float4*>(a)[i]
            : reinterpret_cast<const float4*>(b)[i - a_vectors];

        __half2* dst = reinterpret_cast<__half2*>(packed) + 2 * i;
        dst[0] = __floats2half2_rn(v.x, v.y);
        dst[1] = __floats2half2_rn(v.z, v.w);
    }
}

__global__ void pack_half_scalar(
    const float* __restrict__ a,
    const float* __restrict__ b,
    __half* __restrict__ packed,
    int64_t a_elements,
    int64_t total_elements)
{
    const int64_t stride = static_cast<int64_t>(blockDim.x) * gridDim.x;
    for (int64_t i = static_cast<int64_t>(blockIdx.x) * blockDim.x
                     + threadIdx.x;
         i < total_elements; i += stride) {
        packed[i] = __float2half_rn(
            i < a_elements ? a[i] : b[i - a_elements]);
    }
}

torch::Tensor matmul_nt_cuda(torch::Tensor a, torch::Tensor b) {
    TORCH_CHECK(a.is_cuda() && b.is_cuda(), "Expected CUDA tensors");
    TORCH_CHECK(a.device() == b.device(), "Devices must match");
    TORCH_CHECK(a.scalar_type() == at::kFloat &&
                b.scalar_type() == at::kFloat, "Expected float32 tensors");
    TORCH_CHECK(a.dim() == 2 && b.dim() == 2, "Expected matrices");
    TORCH_CHECK(a.size(1) == b.size(1), "Inner dimensions must match");
    TORCH_CHECK(a.is_contiguous() && b.is_contiguous(),
                "Expected contiguous tensors");
    TORCH_CHECK(a.size(0) <= INT_MAX && b.size(0) <= INT_MAX &&
                a.size(1) <= INT_MAX, "Dimensions exceed cuBLAS limits");

    const c10::cuda::CUDAGuard guard(a.device());
    const int M = static_cast<int>(a.size(0));
    const int N = static_cast<int>(b.size(0));
    const int K = static_cast<int>(a.size(1));

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

    const int64_t a_elements = a.numel();
    const int64_t total_elements = a_elements + b.numel();
    auto packed = torch::empty(
        {total_elements}, a.options().dtype(at::kHalf));

    const float* ap = a.data_ptr<float>();
    const float* bp = b.data_ptr<float>();
    __half* hp = reinterpret_cast<__half*>(packed.data_ptr<at::Half>());
    cudaStream_t stream = at::cuda::getCurrentCUDAStream(a.get_device());

    const bool vectorized =
        a_elements % 4 == 0 && b.numel() % 4 == 0 &&
        reinterpret_cast<uintptr_t>(ap) % 16 == 0 &&
        reinterpret_cast<uintptr_t>(bp) % 16 == 0;

    const int64_t work = vectorized ? total_elements / 4 : total_elements;
    const int blocks = static_cast<int>(
        std::min<int64_t>((work + 255) / 256, 4096));

    if (vectorized) {
        pack_half_vectorized<<<blocks, 256, 0, stream>>>(
            ap, bp, hp, a_elements / 4, total_elements / 4);
    } else {
        pack_half_scalar<<<blocks, 256, 0, stream>>>(
            ap, bp, hp, a_elements, total_elements);
    }
    C10_CUDA_KERNEL_LAUNCH_CHECK();

    cublasHandle_t handle = at::cuda::getCurrentCUDABlasHandle();
    cublasMath_t old_math;
    cublasStatus_t status = cublasGetMathMode(handle, &old_math);
    TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS,
                "cublasGetMathMode failed: ", static_cast<int>(status));

    const cublasMath_t math = static_cast<cublasMath_t>(
        CUBLAS_TENSOR_OP_MATH |
        CUBLAS_MATH_DISALLOW_REDUCED_PRECISION_REDUCTION);
    status = cublasSetMathMode(handle, math);
    TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS,
                "cublasSetMathMode failed: ", static_cast<int>(status));

    const float alpha = 1.0f;
    const float beta = 0.0f;

    // Column-major C^T = B A^T gives the requested row-major output.
    // Tensor cores multiply FP16 inputs with FP32 accumulation.
    status = cublasGemmEx(
        handle,
        CUBLAS_OP_T, CUBLAS_OP_N,
        N, M, K,
        &alpha,
        hp + a_elements, CUDA_R_16F, K,
        hp, CUDA_R_16F, K,
        &beta,
        out.data_ptr<float>(), CUDA_R_32F, N,
        CUBLAS_COMPUTE_32F,
        CUBLAS_GEMM_DEFAULT_TENSOR_OP);

    const cublasStatus_t restore_status =
        cublasSetMathMode(handle, old_math);

    TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS,
                "cublasGemmEx failed: ", static_cast<int>(status));
    TORCH_CHECK(restore_status == CUBLAS_STATUS_SUCCESS,
                "Restoring cuBLAS math mode failed: ",
                static_cast<int>(restore_status));
    return out;
}
"""

_matmul_extension = load_inline(
    name="custom_matmul_nt_half_tensorcores_v3",
    cpp_sources=_cpp_source,
    cuda_sources=_cuda_source,
    functions=["matmul_nt_cuda"],
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
    extra_ldflags=["-lcublas"],
    verbose=False,
)


class ModelNew(nn.Module):
    def __init__(self):
        super().__init__()

    def forward(self, A: torch.Tensor, B: torch.Tensor) -> torch.Tensor:
        if (
            not A.is_cuda
            or not B.is_cuda
            or A.dtype != torch.float32
            or B.dtype != torch.float32
            or A.ndim != 2
            or B.ndim != 2
            or (torch.is_grad_enabled() and (A.requires_grad or B.requires_grad))
        ):
            return torch.matmul(A, B.T)

        return _matmul_extension.matmul_nt_cuda(
            A.contiguous(), B.contiguous()
        )
```

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.
