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, scales the result, and applies batch normalization.
    """
    def __init__(self, in_features, out_features, scale_shape, eps=1e-5, momentum=0.1):
        super(Model, self).__init__()
        self.gemm = nn.Linear(in_features, out_features)
        self.scale = nn.Parameter(torch.randn(scale_shape))
        self.bn = nn.BatchNorm1d(out_features, eps=eps, momentum=momentum)

    def forward(self, x):
        x = self.gemm(x)
        x = x * self.scale
        x = self.bn(x)
        return x

batch_size = 16384
in_features = 4096
out_features = 4096
scale_shape = (out_features,)

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

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


_cpp = r"""
#include <torch/extension.h>
#include <vector>

torch::Tensor linear_tf32_cuda(
    torch::Tensor x, torch::Tensor weight, torch::Tensor bias);

std::vector<torch::Tensor> scaled_bn_cuda(
    torch::Tensor input, torch::Tensor scale,
    torch::Tensor weight, torch::Tensor bias,
    torch::Tensor running_mean, torch::Tensor running_var,
    bool training, double momentum, double eps);
"""

_cuda = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#include <c10/cuda/CUDAException.h>
#include <cublas_v2.h>
#include <cuda_runtime.h>
#include <algorithm>
#include <vector>

constexpr int ROW_TILE = 256;

__global__ void add_bias_kernel(
    float* __restrict__ output,
    const float* __restrict__ bias,
    int64_t elements,
    int features)
{
    for (int64_t i = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
         i < elements;
         i += (int64_t)blockDim.x * gridDim.x) {
        output[i] += bias[i % features];
    }
}

torch::Tensor linear_tf32_cuda(
    torch::Tensor x, torch::Tensor weight, torch::Tensor bias)
{
    const c10::cuda::CUDAGuard guard(x.device());
    const auto stream = at::cuda::getCurrentCUDAStream();

    const int m = static_cast<int>(x.size(0));
    const int k = static_cast<int>(x.size(1));
    const int n = static_cast<int>(weight.size(0));
    auto output = torch::empty({m, n}, x.options());

    auto handle = at::cuda::getCurrentCUDABlasHandle();
    cublasMath_t previous_math;
    TORCH_CHECK(
        cublasGetMathMode(handle, &previous_math) == CUBLAS_STATUS_SUCCESS,
        "cublasGetMathMode failed");
    TORCH_CHECK(
        cublasSetMathMode(handle, CUBLAS_TF32_TENSOR_OP_MATH)
            == CUBLAS_STATUS_SUCCESS,
        "cublasSetMathMode failed");

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

    // Row-major X @ W.T is column-major W @ X.T.
    const auto status = cublasGemmEx(
        handle,
        CUBLAS_OP_T, CUBLAS_OP_N,
        n, m, k,
        &alpha,
        weight.data_ptr<float>(), CUDA_R_32F, k,
        x.data_ptr<float>(), CUDA_R_32F, k,
        &beta,
        output.data_ptr<float>(), CUDA_R_32F, n,
        CUBLAS_COMPUTE_32F_FAST_TF32,
        CUBLAS_GEMM_DEFAULT_TENSOR_OP);

    const auto restore_status = cublasSetMathMode(handle, previous_math);
    TORCH_CHECK(status == CUBLAS_STATUS_SUCCESS,
                "TF32 GEMM failed: ", static_cast<int>(status));
    TORCH_CHECK(restore_status == CUBLAS_STATUS_SUCCESS,
                "Restoring cuBLAS math mode failed");

    const int blocks = static_cast<int>(
        std::min<int64_t>((output.numel() + 255) / 256, 65535));
    add_bias_kernel<<<blocks, 256, 0, stream>>>(
        output.data_ptr<float>(), bias.data_ptr<float>(),
        output.numel(), n);
    C10_CUDA_KERNEL_LAUNCH_CHECK();
    return output;
}

__global__ void partial_statistics_kernel(
    const float* __restrict__ input,
    const float* __restrict__ scale,
    float* __restrict__ partial_mean,
    float* __restrict__ partial_m2,
    int64_t rows,
    int features)
{
    __shared__ float means[8][32];
    __shared__ float m2s[8][32];
    __shared__ int counts[8];

    const int lane = threadIdx.x;
    const int group = threadIdx.y;
    const int feature = blockIdx.x * 32 + lane;
    const int64_t begin = (int64_t)blockIdx.y * ROW_TILE;
    const int64_t end = begin + ROW_TILE < rows
        ? begin + ROW_TILE : rows;

    float mean = 0.0f;
    float m2 = 0.0f;
    int count = 0;
    const float s = feature < features ? scale[feature] : 0.0f;

    for (int64_t row = begin + group; row < end; row += 8) {
        ++count;
        if (feature < features) {
            const float value = input[row * features + feature] * s;
            const float delta = value - mean;
            mean += delta / static_cast<float>(count);
            m2 += delta * (value - mean);
        }
    }

    means[group][lane] = mean;
    m2s[group][lane] = m2;
    if (lane == 0)
        counts[group] = count;
    __syncthreads();

    if (group == 0 && feature < features) {
        float merged_mean = 0.0f;
        float merged_m2 = 0.0f;
        int merged_count = 0;

        #pragma unroll
        for (int g = 0; g < 8; ++g) {
            const int n = counts[g];
            if (n > 0) {
                const int total = merged_count + n;
                const float delta = means[g][lane] - merged_mean;
                merged_mean += delta * ((float)n / (float)total);
                merged_m2 += m2s[g][lane] +
                    delta * delta *
                    ((float)merged_count * (float)n / (float)total);
                merged_count = total;
            }
        }

        const int64_t index = (int64_t)blockIdx.y * features + feature;
        partial_mean[index] = merged_mean;
        partial_m2[index] = merged_m2;
    }
}

__global__ void finish_statistics_kernel(
    const float* __restrict__ partial_mean,
    const float* __restrict__ partial_m2,
    float* __restrict__ running_mean,
    float* __restrict__ running_var,
    float* __restrict__ mean_out,
    float* __restrict__ invstd_out,
    int64_t rows,
    int features,
    int tiles,
    float momentum,
    float eps)
{
    const int feature = blockIdx.x * blockDim.x + threadIdx.x;
    if (feature >= features)
        return;

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

    for (int tile = 0; tile < tiles; ++tile) {
        const int64_t remaining = rows - (int64_t)tile * ROW_TILE;
        const int n = remaining < ROW_TILE ? (int)remaining : ROW_TILE;
        const int64_t index = (int64_t)tile * features + feature;
        const int64_t total = count + n;
        const float delta = partial_mean[index] - mean;

        mean += delta * ((float)n / (float)total);
        m2 += partial_m2[index] +
            delta * delta * ((float)count * (float)n / (float)total);
        count = total;
    }

    mean_out[feature] = mean;
    invstd_out[feature] = rsqrtf(m2 / (float)rows + eps);
    running_mean[feature] =
        (1.0f - momentum) * running_mean[feature] + momentum * mean;
    running_var[feature] =
        (1.0f - momentum) * running_var[feature] +
        momentum * (m2 / (float)(rows - 1));
}

__global__ void inference_statistics_kernel(
    const float* __restrict__ running_mean,
    const float* __restrict__ running_var,
    float* __restrict__ mean,
    float* __restrict__ invstd,
    int features,
    float eps)
{
    const int feature = blockIdx.x * blockDim.x + threadIdx.x;
    if (feature < features) {
        mean[feature] = running_mean[feature];
        invstd[feature] = rsqrtf(running_var[feature] + eps);
    }
}

__global__ void scale_normalize_kernel(
    const float* __restrict__ input,
    const float* __restrict__ scale,
    const float* __restrict__ weight,
    const float* __restrict__ bias,
    const float* __restrict__ mean,
    const float* __restrict__ invstd,
    float* __restrict__ output,
    int64_t elements,
    int features)
{
    for (int64_t i = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
         i < elements;
         i += (int64_t)blockDim.x * gridDim.x) {
        const int feature = i % features;
        const float value = input[i] * scale[feature];
        const float normalized = (value - mean[feature]) * invstd[feature];
        output[i] = normalized * weight[feature] + bias[feature];
    }
}

std::vector<torch::Tensor> scaled_bn_cuda(
    torch::Tensor input, torch::Tensor scale,
    torch::Tensor weight, torch::Tensor bias,
    torch::Tensor running_mean, torch::Tensor running_var,
    bool training, double momentum, double eps)
{
    const c10::cuda::CUDAGuard guard(input.device());
    const auto stream = at::cuda::getCurrentCUDAStream();

    const int64_t rows = input.size(0);
    const int features = static_cast<int>(input.size(1));
    const int tiles = static_cast<int>((rows + ROW_TILE - 1) / ROW_TILE);

    auto output = torch::empty_like(input);
    auto mean = torch::empty({features}, input.options());
    auto invstd = torch::empty({features}, input.options());

    if (training) {
        auto partial_mean = torch::empty({tiles, features}, input.options());
        auto partial_m2 = torch::empty({tiles, features}, input.options());

        partial_statistics_kernel<<<
            dim3((features + 31) / 32, tiles), dim3(32, 8), 0, stream>>>(
                input.data_ptr<float>(), scale.data_ptr<float>(),
                partial_mean.data_ptr<float>(), partial_m2.data_ptr<float>(),
                rows, features);
        C10_CUDA_KERNEL_LAUNCH_CHECK();

        finish_statistics_kernel<<<
            (features + 255) / 256, 256, 0, stream>>>(
                partial_mean.data_ptr<float>(), partial_m2.data_ptr<float>(),
                running_mean.data_ptr<float>(), running_var.data_ptr<float>(),
                mean.data_ptr<float>(), invstd.data_ptr<float>(),
                rows, features, tiles, (float)momentum, (float)eps);
        C10_CUDA_KERNEL_LAUNCH_CHECK();
    } else {
        inference_statistics_kernel<<<
            (features + 255) / 256, 256, 0, stream>>>(
                running_mean.data_ptr<float>(), running_var.data_ptr<float>(),
                mean.data_ptr<float>(), invstd.data_ptr<float>(),
                features, (float)eps);
        C10_CUDA_KERNEL_LAUNCH_CHECK();
    }

    const int blocks = static_cast<int>(
        std::min<int64_t>((input.numel() + 255) / 256, 65535));
    scale_normalize_kernel<<<blocks, 256, 0, stream>>>(
        input.data_ptr<float>(), scale.data_ptr<float>(),
        weight.data_ptr<float>(), bias.data_ptr<float>(),
        mean.data_ptr<float>(), invstd.data_ptr<float>(),
        output.data_ptr<float>(), input.numel(), features);
    C10_CUDA_KERNEL_LAUNCH_CHECK();

    return {output, mean, invstd};
}
"""

_ops = load_inline(
    name="tf32_linear_fused_scaled_bn_v2",
    cpp_sources=_cpp,
    cuda_sources=_cuda,
    functions=["linear_tf32_cuda", "scaled_bn_cuda"],
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3", "--fmad=false"],
    extra_ldflags=["-lcublas"],
    verbose=False,
)


class _LinearTF32(torch.autograd.Function):
    @staticmethod
    def forward(ctx, x, weight, bias):
        ctx.save_for_backward(x, weight)
        return _ops.linear_tf32_cuda(x, weight, bias)

    @staticmethod
    def backward(ctx, grad_output):
        x, weight = ctx.saved_tensors
        grad_x = grad_output @ weight if ctx.needs_input_grad[0] else None
        grad_weight = (
            grad_output.transpose(0, 1) @ x
            if ctx.needs_input_grad[1] else None
        )
        grad_bias = grad_output.sum(0) if ctx.needs_input_grad[2] else None
        return grad_x, grad_weight, grad_bias


class _ScaledBatchNorm(torch.autograd.Function):
    @staticmethod
    def forward(
        ctx, z, scale, weight, bias, running_mean, running_var,
        training, momentum, eps
    ):
        output, mean, invstd = _ops.scaled_bn_cuda(
            z, scale, weight, bias, running_mean, running_var,
            training, momentum, eps
        )
        ctx.training = training
        ctx.eps = eps
        ctx.save_for_backward(z, scale, weight, mean, invstd)
        return output

    @staticmethod
    def backward(ctx, grad_output):
        z, scale, weight, mean, invstd = ctx.saved_tensors
        scaled = z * scale

        if ctx.training and torch.is_grad_enabled():
            variance, mean = torch.var_mean(scaled, dim=0, correction=0)
            invstd = torch.rsqrt(variance + ctx.eps)

        normalized = (scaled - mean) * invstd
        sum_grad = grad_output.sum(dim=0)
        sum_grad_normalized = (grad_output * normalized).sum(dim=0)

        if ctx.training:
            count = z.shape[0]
            grad_scaled = (
                grad_output - sum_grad / count
                - normalized * (sum_grad_normalized / count)
            ) * (weight * invstd)
        else:
            grad_scaled = grad_output * (weight * invstd)

        grad_z = grad_scaled * scale if ctx.needs_input_grad[0] else None
        grad_scale = (
            (grad_scaled * z).sum(dim=0)
            if ctx.needs_input_grad[1] else None
        )
        grad_weight = (
            sum_grad_normalized if ctx.needs_input_grad[2] else None
        )
        grad_bias = sum_grad if ctx.needs_input_grad[3] else None

        return (
            grad_z, grad_scale, grad_weight, grad_bias,
            None, None, None, None, None
        )


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

    def forward(self, x):
        linear = self.gemm
        use_tf32 = (
            x.is_cuda
            and x.dtype == torch.float32
            and x.ndim == 2
            and x.is_contiguous()
            and x.shape[0] > 0
            and x.shape[1] == linear.in_features
            and linear.in_features > 0
            and linear.out_features > 0
            and not torch.is_autocast_enabled()
            and all(
                t is not None
                and t.device == x.device
                and t.dtype == torch.float32
                and t.is_contiguous()
                for t in (linear.weight, linear.bias)
            )
        )

        if use_tf32:
            z = _LinearTF32.apply(x, linear.weight, linear.bias)
        else:
            z = linear(x)

        bn = self.bn
        use_custom_bn = (
            z.is_cuda
            and z.dtype == torch.float32
            and z.ndim == 2
            and z.is_contiguous()
            and z.shape[0] > 1
            and z.shape[1] > 0
            and (z.shape[0] + 255) // 256 <= 65535
            and self.scale.shape == (z.shape[1],)
            and bn.affine
            and bn.track_running_stats
            and all(
                t is not None
                and t.device == z.device
                and t.dtype == torch.float32
                and t.is_contiguous()
                for t in (
                    self.scale, bn.weight, bn.bias,
                    bn.running_mean, bn.running_var
                )
            )
        )

        if not use_custom_bn:
            return bn(z * self.scale)

        factor = 0.0 if bn.momentum is None else bn.momentum
        if bn.training and bn.num_batches_tracked is not None:
            bn.num_batches_tracked.add_(1)
            if bn.momentum is None:
                factor = 1.0 / bn.num_batches_tracked.item()

        return _ScaledBatchNorm.apply(
            z, self.scale, bn.weight, bn.bias,
            bn.running_mean, bn.running_var,
            bn.training, factor, bn.eps
        )
```
Result of evaluating it:
The solution compiled and ran but its output did not match the reference. Details:
```
{"avg_difference": ["0.000325", "0.000325", "0.000325", "0.000325", "0.000325"], "correctness_issue": "Output mismatch", "correctness_trials": "(0 / 5)", "max_difference": ["0.002259", "0.002316", "0.002357", "0.002388", "0.002413"]}
```

The fastest correct solution so far (0.179x 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>
#include <vector>

std::vector<torch::Tensor> scaled_bn_forward_cuda(
    torch::Tensor input,
    torch::Tensor scale,
    torch::Tensor weight,
    torch::Tensor bias,
    torch::Tensor running_mean,
    torch::Tensor running_var,
    bool training,
    double momentum,
    double eps);
"""

_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 <vector>
#include <algorithm>

constexpr int ROW_TILE = 256;

__global__ void partial_statistics_kernel(
    const float* __restrict__ input,
    const float* __restrict__ scale,
    float* __restrict__ partial_mean,
    float* __restrict__ partial_m2,
    int64_t rows,
    int features)
{
    __shared__ float means[8][32];
    __shared__ float m2s[8][32];
    __shared__ int counts[8];

    const int lane = threadIdx.x;
    const int group = threadIdx.y;
    const int feature = blockIdx.x * 32 + lane;
    const int64_t begin = static_cast<int64_t>(blockIdx.y) * ROW_TILE;
    const int64_t end =
        begin + ROW_TILE < rows ? begin + ROW_TILE : rows;

    float mean = 0.0f;
    float m2 = 0.0f;
    int count = 0;
    const float s = feature < features ? scale[feature] : 0.0f;

    for (int64_t row = begin + group; row < end; row += 8) {
        ++count;
        if (feature < features) {
            const float value = input[row * features + feature] * s;
            const float delta = value - mean;
            mean += delta / static_cast<float>(count);
            m2 += delta * (value - mean);
        }
    }

    means[group][lane] = mean;
    m2s[group][lane] = m2;
    if (lane == 0)
        counts[group] = count;
    __syncthreads();

    if (group == 0 && feature < features) {
        float merged_mean = 0.0f;
        float merged_m2 = 0.0f;
        int merged_count = 0;

        #pragma unroll
        for (int g = 0; g < 8; ++g) {
            const int n = counts[g];
            if (n > 0) {
                const int total = merged_count + n;
                const float delta = means[g][lane] - merged_mean;
                merged_mean += delta *
                    (static_cast<float>(n) / static_cast<float>(total));
                merged_m2 += m2s[g][lane] + delta * delta *
                    (static_cast<float>(merged_count) *
                     static_cast<float>(n) / static_cast<float>(total));
                merged_count = total;
            }
        }

        const int64_t index =
            static_cast<int64_t>(blockIdx.y) * features + feature;
        partial_mean[index] = merged_mean;
        partial_m2[index] = merged_m2;
    }
}

__global__ void finish_statistics_kernel(
    const float* __restrict__ partial_mean,
    const float* __restrict__ partial_m2,
    float* __restrict__ running_mean,
    float* __restrict__ running_var,
    float* __restrict__ mean_out,
    float* __restrict__ invstd_out,
    int64_t rows,
    int features,
    int tiles,
    float momentum,
    float eps)
{
    const int feature = blockIdx.x * blockDim.x + threadIdx.x;
    if (feature >= features)
        return;

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

    for (int tile = 0; tile < tiles; ++tile) {
        const int64_t remaining =
            rows - static_cast<int64_t>(tile) * ROW_TILE;
        const int n = remaining < ROW_TILE
            ? static_cast<int>(remaining) : ROW_TILE;
        const int64_t index =
            static_cast<int64_t>(tile) * features + feature;

        const int64_t total = count + n;
        const float delta = partial_mean[index] - mean;
        mean += delta *
            (static_cast<float>(n) / static_cast<float>(total));
        m2 += partial_m2[index] + delta * delta *
            (static_cast<float>(count) * static_cast<float>(n) /
             static_cast<float>(total));
        count = total;
    }

    mean_out[feature] = mean;
    invstd_out[feature] =
        rsqrtf(m2 / static_cast<float>(rows) + eps);

    running_mean[feature] =
        (1.0f - momentum) * running_mean[feature] + momentum * mean;
    running_var[feature] =
        (1.0f - momentum) * running_var[feature] +
        momentum * (m2 / static_cast<float>(rows - 1));
}

__global__ void inference_statistics_kernel(
    const float* __restrict__ running_mean,
    const float* __restrict__ running_var,
    float* __restrict__ mean_out,
    float* __restrict__ invstd_out,
    int features,
    float eps)
{
    const int feature = blockIdx.x * blockDim.x + threadIdx.x;
    if (feature < features) {
        mean_out[feature] = running_mean[feature];
        invstd_out[feature] = rsqrtf(running_var[feature] + eps);
    }
}

__global__ void scale_normalize_kernel(
    const float* __restrict__ input,
    const float* __restrict__ scale,
    const float* __restrict__ weight,
    const float* __restrict__ bias,
    const float* __restrict__ mean,
    const float* __restrict__ invstd,
    float* __restrict__ output,
    int64_t elements,
    int features)
{
    for (int64_t index =
             static_cast<int64_t>(blockIdx.x) * blockDim.x + threadIdx.x;
         index < elements;
         index += static_cast<int64_t>(blockDim.x) * gridDim.x) {
        const int feature = index % features;
        const float value = input[index] * scale[feature];
        const float normalized =
            (value - mean[feature]) * invstd[feature];
        output[index] =
            normalized * weight[feature] + bias[feature];
    }
}

std::vector<torch::Tensor> scaled_bn_forward_cuda(
    torch::Tensor input,
    torch::Tensor scale,
    torch::Tensor weight,
    torch::Tensor bias,
    torch::Tensor running_mean,
    torch::Tensor running_var,
    bool training,
    double momentum,
    double eps)
{
    const c10::cuda::CUDAGuard guard(input.device());
    const auto stream = at::cuda::getCurrentCUDAStream();

    const int64_t rows = input.size(0);
    const int features = static_cast<int>(input.size(1));
    const int tiles = static_cast<int>((rows + ROW_TILE - 1) / ROW_TILE);

    auto output = torch::empty_like(input);
    auto mean = torch::empty({features}, input.options());
    auto invstd = torch::empty({features}, input.options());

    if (training) {
        auto partial_mean = torch::empty({tiles, features}, input.options());
        auto partial_m2 = torch::empty({tiles, features}, input.options());

        partial_statistics_kernel<<<
            dim3((features + 31) / 32, tiles),
            dim3(32, 8), 0, stream>>>(
                input.data_ptr<float>(),
                scale.data_ptr<float>(),
                partial_mean.data_ptr<float>(),
                partial_m2.data_ptr<float>(),
                rows, features);
        C10_CUDA_KERNEL_LAUNCH_CHECK();

        finish_statistics_kernel<<<
            (features + 255) / 256, 256, 0, stream>>>(
                partial_mean.data_ptr<float>(),
                partial_m2.data_ptr<float>(),
                running_mean.data_ptr<float>(),
                running_var.data_ptr<float>(),
                mean.data_ptr<float>(),
                invstd.data_ptr<float>(),
                rows, features, tiles,
                static_cast<float>(momentum),
                static_cast<float>(eps));
        C10_CUDA_KERNEL_LAUNCH_CHECK();
    } else {
        inference_statistics_kernel<<<
            (features + 255) / 256, 256, 0, stream>>>(
                running_mean.data_ptr<float>(),
                running_var.data_ptr<float>(),
                mean.data_ptr<float>(),
                invstd.data_ptr<float>(),
                features, static_cast<float>(eps));
        C10_CUDA_KERNEL_LAUNCH_CHECK();
    }

    const int blocks = static_cast<int>(
        std::min<int64_t>((input.numel() + 255) / 256, 65535));

    scale_normalize_kernel<<<blocks, 256, 0, stream>>>(
        input.data_ptr<float>(),
        scale.data_ptr<float>(),
        weight.data_ptr<float>(),
        bias.data_ptr<float>(),
        mean.data_ptr<float>(),
        invstd.data_ptr<float>(),
        output.data_ptr<float>(),
        input.numel(), features);
    C10_CUDA_KERNEL_LAUNCH_CHECK();

    return {output, mean, invstd};
}
"""

_scaled_bn = load_inline(
    name="fused_scaled_batchnorm_welford_v1",
    cpp_sources=_cpp_source,
    cuda_sources=_cuda_source,
    functions=["scaled_bn_forward_cuda"],
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3", "--fmad=false"],
    verbose=False,
)


class _ScaledBatchNorm(torch.autograd.Function):
    @staticmethod
    def forward(
        ctx, z, scale, weight, bias, running_mean, running_var,
        training, momentum, eps
    ):
        output, mean, invstd = _scaled_bn.scaled_bn_forward_cuda(
            z, scale, weight, bias, running_mean, running_var,
            training, momentum, eps
        )
        ctx.training = training
        ctx.eps = eps
        ctx.save_for_backward(z, scale, weight, mean, invstd)
        return output

    @staticmethod
    def backward(ctx, grad_output):
        z, scale, weight, mean, invstd = ctx.saved_tensors
        scaled = z * scale

        # Recompute differentiable statistics when constructing higher gradients.
        if ctx.training and torch.is_grad_enabled():
            variance, mean = torch.var_mean(scaled, dim=0, correction=0)
            invstd = torch.rsqrt(variance + ctx.eps)

        normalized = (scaled - mean) * invstd
        sum_grad = grad_output.sum(dim=0)
        sum_grad_normalized = (grad_output * normalized).sum(dim=0)

        if ctx.training:
            count = z.shape[0]
            grad_scaled = (
                grad_output
                - sum_grad / count
                - normalized * (sum_grad_normalized / count)
            ) * (weight * invstd)
        else:
            grad_scaled = grad_output * (weight * invstd)

        grad_z = grad_scaled * scale if ctx.needs_input_grad[0] else None
        grad_scale = (
            (grad_scaled * z).sum(dim=0)
            if ctx.needs_input_grad[1] else None
        )
        grad_weight = (
            sum_grad_normalized if ctx.needs_input_grad[2] else None
        )
        grad_bias = sum_grad if ctx.needs_input_grad[3] else None

        return (
            grad_z, grad_scale, grad_weight, grad_bias,
            None, None, None, None, None
        )


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

    def forward(self, x):
        z = self.gemm(x)
        bn = self.bn

        use_custom = (
            z.is_cuda
            and z.dtype == torch.float32
            and z.ndim == 2
            and z.is_contiguous()
            and z.shape[0] > 1
            and z.shape[1] > 0
            and (z.shape[0] + 255) // 256 <= 65535
            and self.scale.shape == (z.shape[1],)
            and bn.affine
            and bn.track_running_stats
            and all(
                t is not None
                and t.device == z.device
                and t.dtype == torch.float32
                and t.is_contiguous()
                for t in (
                    self.scale, bn.weight, bn.bias,
                    bn.running_mean, bn.running_var
                )
            )
        )

        if not use_custom:
            return bn(z * self.scale)

        factor = 0.0 if bn.momentum is None else bn.momentum
        if bn.training and bn.num_batches_tracked is not None:
            bn.num_batches_tracked.add_(1)
            if bn.momentum is None:
                factor = 1.0 / float(bn.num_batches_tracked.item())

        return _ScaledBatchNorm.apply(
            z, self.scale, bn.weight, bn.bias,
            bn.running_mean, bn.running_var,
            bn.training, factor, bn.eps
        )
```

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.
