```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 CUDA kernel for matrix-scalar multiplication (C = A * s).
# This operation is purely memory-bandwidth bound, so the kernel uses
# float4 vectorized loads/stores to maximize memory throughput and a
# simple one-element-per-thread mapping to minimize index overhead.
scalar_mul_source = """
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>

namespace {

// Vectorized kernel: each thread handles one float4 (4 floats).
__global__ void scalar_mul_vec4_kernel(const float4* __restrict__ in,
                                       float4* __restrict__ out,
                                       const float s,
                                       const long n4) {
    const long i = (long)blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n4) {
        float4 v = in[i];
        v.x *= s;
        v.y *= s;
        v.z *= s;
        v.w *= s;
        out[i] = v;
    }
}

// Tail kernel for the remaining elements when numel is not divisible by 4.
__global__ void scalar_mul_tail_kernel(const float* __restrict__ in,
                                       float* __restrict__ out,
                                       const float s,
                                       const long start,
                                       const long n) {
    const long i = start + (long)blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n) {
        out[i] = in[i] * s;
    }
}

} // namespace

torch::Tensor scalar_mul_cuda(torch::Tensor A, double s) {
    TORCH_CHECK(A.is_cuda(), "A must be a CUDA tensor");
    TORCH_CHECK(A.scalar_type() == torch::kFloat32, "A must be a float32 tensor");

    auto A_contig = A.contiguous();
    auto out = torch::empty_like(A_contig);

    const long n = A_contig.numel();
    if (n == 0) {
        return out;
    }

    const float* in_ptr = A_contig.data_ptr<float>();
    float* out_ptr = out.data_ptr<float>();
    const float sf = static_cast<float>(s);

    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    const int block = 256;

    // Main body: 4 floats per thread via float4.
    const long n4 = n >> 2;
    if (n4 > 0) {
        const long blocks_needed = (n4 + block - 1) / block;
        // Grid-x is limited to 2^31 - 1 blocks; cap just in case.
        const int grid = (int)(blocks_needed < 2147483647L ? blocks_needed : 2147483647L);
        scalar_mul_vec4_kernel<<<grid, block, 0, stream>>>(
            reinterpret_cast<const float4*>(in_ptr),
            reinterpret_cast<float4*>(out_ptr),
            sf,
            n4);
    }

    // Tail elements (n % 4), if any.
    const long start = n4 << 2;
    const long rem = n - start;
    if (rem > 0) {
        const int grid = (int)((rem + block - 1) / block);
        scalar_mul_tail_kernel<<<grid, block, 0, stream>>>(
            in_ptr, out_ptr, sf, start, n);
    }

    return out;
}
"""

scalar_mul_cpp_source = "torch::Tensor scalar_mul_cuda(torch::Tensor A, double s);"

# Compile the inline CUDA extension.
scalar_mul_ext = load_inline(
    name="scalar_mul_ext",
    cpp_sources=scalar_mul_cpp_source,
    cuda_sources=scalar_mul_source,
    functions=["scalar_mul_cuda"],
    verbose=False,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
)


class ModelNew(nn.Module):
    """
    Optimized model that performs matrix-scalar multiplication (C = A * s)
    using a custom vectorized CUDA kernel.
    """
    def __init__(self):
        super(ModelNew, self).__init__()
        self.scalar_mul = scalar_mul_ext

    def forward(self, A: torch.Tensor, s: float) -> torch.Tensor:
        # Use the custom CUDA kernel when possible; otherwise fall back
        # to the native PyTorch operator.
        if A.is_cuda and A.dtype == torch.float32:
            return self.scalar_mul.scalar_mul_cuda(A, float(s))
        return A * s
```