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):
    """
    Model that performs a matrix multiplication (Gemm), Batch Normalization, scaling, and Softmax.
    """
    def __init__(self, in_features, out_features, bn_eps=1e-5, bn_momentum=0.1, scale_shape=(1,)):
        super(Model, self).__init__()
        self.gemm = nn.Linear(in_features, out_features)
        self.bn = nn.BatchNorm1d(out_features, eps=bn_eps, momentum=bn_momentum)
        self.scale = nn.Parameter(torch.ones(scale_shape))
        self.softmax = nn.Softmax(dim=1)

    def forward(self, x):
        """
        Args:
            x (torch.Tensor): Input tensor of shape (batch_size, in_features).
        Returns:
            torch.Tensor: Output tensor of shape (batch_size, out_features).
        """
        x = self.gemm(x)
        x = self.bn(x)
        x = self.scale * x
        x = self.softmax(x)
        return x

batch_size = 1024
in_features = 8192
out_features = 8192
bn_eps = 1e-5
bn_momentum = 0.1
scale_shape = (1,)

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

def get_init_inputs():
    return [in_features, out_features, bn_eps, bn_momentum, scale_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 2 of 6 for this task. Your previous solution (turn 1) 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 fused_bn_scale_softmax(
    torch::Tensor x,
    torch::Tensor weight,
    torch::Tensor bias,
    torch::Tensor running_mean,
    torch::Tensor running_var,
    torch::Tensor scale,
    bool training,
    double eps,
    double momentum);
"""

_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 <math.h>

constexpr int ROW_TILE = 128;
constexpr int SOFTMAX_THREADS = 256;

__global__ void partial_stats_kernel(
    const float* __restrict__ x,
    float* __restrict__ partial,
    int rows,
    int cols,
    int parts)
{
    __shared__ float means[8][32];
    __shared__ float m2s[8][32];

    const int tx = threadIdx.x;
    const int ty = threadIdx.y;
    const int col = blockIdx.x * 32 + tx;
    const int start = blockIdx.y * ROW_TILE;
    const int end = min(start + ROW_TILE, rows);

    float mean = 0.0f;
    float m2 = 0.0f;
    int count = 0;

    if (col < cols) {
        for (int row = start + ty; row < end; row += 8) {
            const float value = x[(int64_t)row * cols + col];
            ++count;
            const float delta = value - mean;
            mean += delta / count;
            m2 += delta * (value - mean);
        }
    }

    means[ty][tx] = mean;
    m2s[ty][tx] = m2;
    __syncthreads();

    if (ty == 0 && col < cols) {
        float total_mean = 0.0f;
        float total_m2 = 0.0f;
        int total_count = 0;

        #pragma unroll
        for (int lane = 0; lane < 8; ++lane) {
            const int n = max(0, (end - start - lane + 7) / 8);
            if (n > 0) {
                const int combined = total_count + n;
                const float delta = means[lane][tx] - total_mean;
                total_m2 += m2s[lane][tx]
                    + delta * delta * ((float)total_count * n / combined);
                total_mean += delta * ((float)n / combined);
                total_count = combined;
            }
        }

        const int64_t index = (int64_t)blockIdx.y * cols + col;
        partial[index] = total_mean;
        partial[(int64_t)parts * cols + index] = total_m2;
    }
}

__global__ void training_coefficients_kernel(
    const float* __restrict__ partial,
    const float* __restrict__ weight,
    const float* __restrict__ bias,
    float* __restrict__ running_mean,
    float* __restrict__ running_var,
    float* __restrict__ coefficients,
    int rows,
    int cols,
    int parts,
    float eps,
    float momentum)
{
    const int col = blockIdx.x * blockDim.x + threadIdx.x;
    if (col >= cols) return;

    float mean = 0.0f;
    float m2 = 0.0f;
    int count = 0;

    for (int part = 0; part < parts; ++part) {
        const int n = min(ROW_TILE, rows - part * ROW_TILE);
        const int combined = count + n;
        const int64_t index = (int64_t)part * cols + col;
        const float part_mean = partial[index];
        const float part_m2 = partial[(int64_t)parts * cols + index];
        const float delta = part_mean - mean;

        m2 += part_m2 + delta * delta * ((float)count * n / combined);
        mean += delta * ((float)n / combined);
        count = combined;
    }

    const float variance = fmaxf(m2 / rows, 0.0f);
    const float alpha = weight[col] * rsqrtf(variance + eps);

    coefficients[col] = alpha;
    coefficients[cols + col] = bias[col] - mean * alpha;

    running_mean[col] =
        (1.0f - momentum) * running_mean[col] + momentum * mean;
    running_var[col] =
        (1.0f - momentum) * running_var[col]
        + momentum * (m2 / (rows - 1));
}

__global__ void inference_coefficients_kernel(
    const float* __restrict__ weight,
    const float* __restrict__ bias,
    const float* __restrict__ running_mean,
    const float* __restrict__ running_var,
    float* __restrict__ coefficients,
    int cols,
    float eps)
{
    const int col = blockIdx.x * blockDim.x + threadIdx.x;
    if (col >= cols) return;

    const float alpha = weight[col] * rsqrtf(running_var[col] + eps);
    coefficients[col] = alpha;
    coefficients[cols + col] = bias[col] - running_mean[col] * alpha;
}

template<bool MAXIMUM>
__device__ __forceinline__ float block_reduce(float value)
{
    __shared__ float warp_results[8];
    const int lane = threadIdx.x & 31;
    const int warp = threadIdx.x >> 5;

    #pragma unroll
    for (int offset = 16; offset > 0; offset >>= 1) {
        const float other = __shfl_down_sync(0xffffffff, value, offset);
        if (MAXIMUM) value = fmaxf(value, other);
        else value += other;
    }

    if (lane == 0) warp_results[warp] = value;
    __syncthreads();

    if (warp == 0) {
        value = lane < 8
            ? warp_results[lane]
            : (MAXIMUM ? -INFINITY : 0.0f);

        #pragma unroll
        for (int offset = 16; offset > 0; offset >>= 1) {
            const float other = __shfl_down_sync(0xffffffff, value, offset);
            if (MAXIMUM) value = fmaxf(value, other);
            else value += other;
        }

        if (lane == 0) warp_results[0] = value;
    }

    __syncthreads();
    const float result = warp_results[0];
    __syncthreads();
    return result;
}

template<int ITEMS>
__global__ void normalized_softmax_kernel(
    const float* __restrict__ x,
    const float* __restrict__ coefficients,
    const float* __restrict__ scale,
    float* __restrict__ out,
    int cols,
    bool scalar_scale)
{
    const int tid = threadIdx.x;
    const int64_t row_offset = (int64_t)blockIdx.x * cols;

    float values[ITEMS];
    float local_max = -INFINITY;

    #pragma unroll
    for (int i = 0; i < ITEMS; ++i) {
        const int col = tid + i * SOFTMAX_THREADS;
        float value = -INFINITY;
        if (col < cols) {
            value = (
                x[row_offset + col] * coefficients[col]
                + coefficients[cols + col]
            ) * scale[scalar_scale ? 0 : col];
        }
        values[i] = value;
        local_max = fmaxf(local_max, value);
    }

    const float maximum = block_reduce<true>(local_max);
    float local_sum = 0.0f;

    #pragma unroll
    for (int i = 0; i < ITEMS; ++i) {
        values[i] = expf(values[i] - maximum);
        local_sum += values[i];
    }

    const float inverse_sum = 1.0f / block_reduce<false>(local_sum);

    #pragma unroll
    for (int i = 0; i < ITEMS; ++i) {
        const int col = tid + i * SOFTMAX_THREADS;
        if (col < cols) {
            out[row_offset + col] = values[i] * inverse_sum;
        }
    }
}

torch::Tensor fused_bn_scale_softmax(
    torch::Tensor x,
    torch::Tensor weight,
    torch::Tensor bias,
    torch::Tensor running_mean,
    torch::Tensor running_var,
    torch::Tensor scale,
    bool training,
    double eps,
    double momentum)
{
    const c10::cuda::CUDAGuard device_guard(x.device());
    const auto stream = at::cuda::getCurrentCUDAStream();

    const int rows = x.size(0);
    const int cols = x.size(1);

    auto out = torch::empty_like(x);
    if (rows == 0) return out;

    auto coefficients = torch::empty({2, cols}, x.options());

    if (training) {
        const int parts = (rows + ROW_TILE - 1) / ROW_TILE;
        auto partial = torch::empty({2, parts, cols}, x.options());

        partial_stats_kernel<<<
            dim3((cols + 31) / 32, parts), dim3(32, 8), 0, stream
        >>>(
            x.data_ptr<float>(), partial.data_ptr<float>(),
            rows, cols, parts
        );

        training_coefficients_kernel<<<
            (cols + 255) / 256, 256, 0, stream
        >>>(
            partial.data_ptr<float>(),
            weight.data_ptr<float>(),
            bias.data_ptr<float>(),
            running_mean.data_ptr<float>(),
            running_var.data_ptr<float>(),
            coefficients.data_ptr<float>(),
            rows, cols, parts, (float)eps, (float)momentum
        );
    } else {
        inference_coefficients_kernel<<<
            (cols + 255) / 256, 256, 0, stream
        >>>(
            weight.data_ptr<float>(),
            bias.data_ptr<float>(),
            running_mean.data_ptr<float>(),
            running_var.data_ptr<float>(),
            coefficients.data_ptr<float>(),
            cols, (float)eps
        );
    }

    const bool scalar_scale = scale.numel() == 1;

    #define LAUNCH_SOFTMAX(N) \
        normalized_softmax_kernel<N><<<rows, SOFTMAX_THREADS, 0, stream>>>( \
            x.data_ptr<float>(), coefficients.data_ptr<float>(), \
            scale.data_ptr<float>(), out.data_ptr<float>(), \
            cols, scalar_scale)

    if      (cols <= 256)   { LAUNCH_SOFTMAX(1); }
    else if (cols <= 512)   { LAUNCH_SOFTMAX(2); }
    else if (cols <= 1024)  { LAUNCH_SOFTMAX(4); }
    else if (cols <= 2048)  { LAUNCH_SOFTMAX(8); }
    else if (cols <= 4096)  { LAUNCH_SOFTMAX(16); }
    else if (cols <= 8192)  { LAUNCH_SOFTMAX(32); }
    else                   { LAUNCH_SOFTMAX(64); }

    #undef LAUNCH_SOFTMAX

    C10_CUDA_KERNEL_LAUNCH_CHECK();
    return out;
}
"""

_fused_ops = load_inline(
    name="gemm_bn_scale_softmax_cuda_v1",
    cpp_sources=_cpp_source,
    cuda_sources=_cuda_source,
    functions=["fused_bn_scale_softmax"],
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
    verbose=False,
)


class ModelNew(nn.Module):
    def __init__(
        self,
        in_features,
        out_features,
        bn_eps=1e-5,
        bn_momentum=0.1,
        scale_shape=(1,),
    ):
        super().__init__()
        self.gemm = nn.Linear(in_features, out_features)
        self.bn = nn.BatchNorm1d(
            out_features, eps=bn_eps, momentum=bn_momentum
        )
        self.scale = nn.Parameter(torch.ones(scale_shape))
        self.softmax = nn.Softmax(dim=1)

    def forward(self, x):
        x = self.gemm(x)
        cols = self.gemm.out_features

        supported_scale = tuple(self.scale.shape) in {
            (), (1,), (cols,), (1, 1), (1, cols)
        }

        tensors = (
            self.bn.weight,
            self.bn.bias,
            self.bn.running_mean,
            self.bn.running_var,
            self.scale,
        )

        supported = (
            x.is_cuda
            and x.dtype == torch.float32
            and x.ndim == 2
            and x.is_contiguous()
            and 0 < cols <= 16384
            and supported_scale
            and self.bn.affine
            and self.bn.track_running_stats
            and (not self.bn.training or x.size(0) > 1)
            and all(
                t is not None
                and t.device == x.device
                and t.dtype == torch.float32
                and t.is_contiguous()
                for t in tensors
            )
        )

        if not supported:
            return self.softmax(self.scale * self.bn(x))

        momentum = self.bn.momentum
        if self.bn.training:
            self.bn.num_batches_tracked.add_(1)
            if momentum is None:
                momentum = 1.0 / self.bn.num_batches_tracked.item()
        elif momentum is None:
            momentum = 0.0

        return _fused_ops.fused_bn_scale_softmax(
            x,
            self.bn.weight,
            self.bn.bias,
            self.bn.running_mean,
            self.bn.running_var,
            self.scale,
            self.bn.training,
            self.bn.eps,
            momentum,
        )
```
Result of evaluating it:
The solution is correct on the test inputs. Mean runtime 8 ms; the PyTorch reference (with TF32 enabled) takes 1.34 ms; speedup 0.168x.
Profiler, top device kernels of one forward pass of your solution:
| device kernel | total device time (us) | calls |
|---|---|---|
| ampere_sgemm_128x32_tn | 7894.6 | 1 |
| void normalized_softmax_kernel<32>(float const*, float const*, float const*, float*, int,  | 51.4 | 1 |
| partial_stats_kernel(float const*, float*, int, int, int) | 48.7 | 1 |
| training_coefficients_kernel(float const*, float const*, float const*, float*, float*, flo | 9.1 | 1 |
| void at::native::vectorized_elementwise_kernel<2, at::native::CUDAFunctorOnSelf_add<long>, | 2.5 | 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.
