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 2 of 6 for this task. Your previous solution (turn 1) 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

# Custom tiled SGEMM kernel computing C = A @ B^T where
# A is (M, K) row-major and B is (N, K) row-major.
# Strategy:
#   - 128x128 block tile, 16-wide K tile, 256 threads per block
#   - each thread computes an 8x8 micro-tile (strided layout for
#     conflict-free shared memory access)
#   - float4 vectorized global loads (both operands are K-contiguous)
#   - register prefetch (double buffering) of the next K-tile to hide
#     global memory latency
cuda_source = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>

#define BM 128
#define BN 128
#define BK 16
#define SPAD 4
#define LDS (BM + SPAD)

__global__ __launch_bounds__(256) void sgemm_a_bT_kernel(
    const float* __restrict__ A,   // (M, K) row-major
    const float* __restrict__ B,   // (N, K) row-major
    float* __restrict__ C,         // (M, N) row-major
    const int M, const int N, const long long K)
{
    __shared__ float As[BK][LDS];
    __shared__ float Bs[BK][LDS];

    const int tid = threadIdx.x;
    const int tx = tid & 15;        // n-dim lane (0..15)
    const int ty = tid >> 4;        // m-dim lane (0..15)

    const int m0 = blockIdx.y * BM;
    const int n0 = blockIdx.x * BN;

    // Global-tile load coordinates: each thread loads 2 float4 from A
    // and 2 float4 from B (128 rows x 4 float4 per row = 512 loads).
    const int lrow = tid >> 2;      // 0..63
    const int lk4  = tid & 3;       // 0..3

    float acc[8][8];
    #pragma unroll
    for (int i = 0; i < 8; ++i)
        #pragma unroll
        for (int j = 0; j < 8; ++j)
            acc[i][j] = 0.0f;

    float afrag[2][4];
    float bfrag[2][4];

    const int numTiles = (int)((K + BK - 1) / BK);

    auto load_global = [&](int kt) {
        const int kk = kt * BK + lk4 * 4;
        #pragma unroll
        for (int r = 0; r < 2; ++r) {
            const int row = lrow + r * 64;
            const int ga = m0 + row;
            const int gb = n0 + row;
            if (ga < M) {
                const float* p = A + (long long)ga * K + kk;
                if (kk + 3 < K) {
                    float4 v = *reinterpret_cast<const float4*>(p);
                    afrag[r][0] = v.x; afrag[r][1] = v.y;
                    afrag[r][2] = v.z; afrag[r][3] = v.w;
                } else {
                    #pragma unroll
                    for (int i = 0; i < 4; ++i)
                        afrag[r][i] = (kk + i < K) ? p[i] : 0.0f;
                }
            } else {
                #pragma unroll
                for (int i = 0; i < 4; ++i) afrag[r][i] = 0.0f;
            }
            if (gb < N) {
                const float* p = B + (long long)gb * K + kk;
                if (kk + 3 < K) {
                    float4 v = *reinterpret_cast<const float4*>(p);
                    bfrag[r][0] = v.x; bfrag[r][1] = v.y;
                    bfrag[r][2] = v.z; bfrag[r][3] = v.w;
                } else {
                    #pragma unroll
                    for (int i = 0; i < 4; ++i)
                        bfrag[r][i] = (kk + i < K) ? p[i] : 0.0f;
                }
            } else {
                #pragma unroll
                for (int i = 0; i < 4; ++i) bfrag[r][i] = 0.0f;
            }
        }
    };

    auto store_shared = [&]() {
        #pragma unroll
        for (int r = 0; r < 2; ++r) {
            const int row = lrow + r * 64;
            #pragma unroll
            for (int i = 0; i < 4; ++i) {
                As[lk4 * 4 + i][row] = afrag[r][i];
                Bs[lk4 * 4 + i][row] = bfrag[r][i];
            }
        }
    };

    load_global(0);
    store_shared();
    __syncthreads();

    for (int kt = 0; kt < numTiles; ++kt) {
        if (kt + 1 < numTiles) load_global(kt + 1);   // prefetch to registers

        #pragma unroll
        for (int k = 0; k < BK; ++k) {
            float av[8], bv[8];
            #pragma unroll
            for (int i = 0; i < 8; ++i) av[i] = As[k][ty + i * 16];
            #pragma unroll
            for (int j = 0; j < 8; ++j) bv[j] = Bs[k][tx + j * 16];
            #pragma unroll
            for (int i = 0; i < 8; ++i)
                #pragma unroll
                for (int j = 0; j < 8; ++j)
                    acc[i][j] = fmaf(av[i], bv[j], acc[i][j]);
        }

        if (kt + 1 < numTiles) {
            __syncthreads();
            store_shared();
            __syncthreads();
        }
    }

    #pragma unroll
    for (int i = 0; i < 8; ++i) {
        const int grow = m0 + ty + i * 16;
        if (grow < M) {
            #pragma unroll
            for (int j = 0; j < 8; ++j) {
                const int gcol = n0 + tx + j * 16;
                if (gcol < N) C[(long long)grow * N + gcol] = acc[i][j];
            }
        }
    }
}

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

    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(0);
    TORCH_CHECK(Bc.size(1) == K, "Inner dimensions must match.");

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

    dim3 block(256);
    dim3 grid((unsigned)((N + BN - 1) / BN), (unsigned)((M + BM - 1) / BM));
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();

    sgemm_a_bT_kernel<<<grid, block, 0, stream>>>(
        Ac.data_ptr<float>(), Bc.data_ptr<float>(), C.data_ptr<float>(),
        (int)M, (int)N, (long long)K);

    return C;
}
"""

cpp_source = "torch::Tensor matmul_a_bT_cuda(torch::Tensor A, torch::Tensor B);"

# Compile the inline CUDA extension
_matmul_ext = load_inline(
    name="sgemm_a_bT_ext_v1",
    cpp_sources=cpp_source,
    cuda_sources=cuda_source,
    functions=["matmul_a_bT_cuda"],
    verbose=False,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
)


class ModelNew(nn.Module):
    """
    Optimized model that performs C = A @ B^T with a custom tiled CUDA
    SGEMM kernel (shared-memory tiling + register prefetch + float4 loads).
    """
    def __init__(self):
        super(ModelNew, self).__init__()
        self._op = _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
            and A.size(1) == B.size(1)
            and (A.size(1) % 4 == 0)  # 16-byte alignment for float4 loads
        ):
            return self._op.matmul_a_bT_cuda(A, B)
        # Safe fallback for anything the custom kernel doesn't cover
        return torch.matmul(A, B.t())
```
Result of evaluating it:
The solution is correct on the test inputs. Mean runtime 8.76 ms; the PyTorch reference (with TF32 enabled) takes 1.15 ms; speedup 0.131x.
Profiler, top device kernels of one forward pass of your solution:
| device kernel | total device time (us) | calls |
|---|---|---|
| sgemm_a_bT_kernel(float const*, float const*, float*, int, int, long long) | 8738.9 | 1 |

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.
