```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
        )
```