```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).
# The op is purely memory-bandwidth bound, so the kernel is optimized for
# maximal throughput using vectorized 128-bit (float4) loads/stores and a
# grid-stride loop that works for arbitrary tensor sizes.
scalar_mul_cuda_source = """
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>
#include <cstdint>

__global__ void scalar_mul_vec4_kernel(const float4* __restrict__ A,
                                       float4* __restrict__ C,
                                       float s,
                                       long n4) {
    long idx = blockIdx.x * (long)blockDim.x + threadIdx.x;
    long stride = (long)gridDim.x * blockDim.x;
    for (long i = idx; i < n4; i += stride) {
        float4 v = A[i];
        v.x *= s;
        v.y *= s;
        v.z *= s;
        v.w *= s;
        C[i] = v;
    }
}

__global__ void scalar_mul_kernel(const float* __restrict__ A,
                                  float* __restrict__ C,
                                  float s,
                                  long n) {
    long idx = blockIdx.x * (long)blockDim.x + threadIdx.x;
    long stride = (long)gridDim.x * blockDim.x;
    for (long i = idx; i < n; i += stride) {
        C[i] = A[i] * s;
    }
}

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() == at::kFloat, "A must be float32");

    auto Ac = A.contiguous();
    auto C = torch::empty_like(Ac);
    long n = Ac.numel();
    if (n == 0) {
        return C;
    }

    const float sf = static_cast<float>(s);
    const int block = 256;
    const float* a_ptr = Ac.data_ptr<float>();
    float* c_ptr = C.data_ptr<float>();
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();

    bool aligned = ((reinterpret_cast<uintptr_t>(a_ptr) & 0xF) == 0) &&
                   ((reinterpret_cast<uintptr_t>(c_ptr) & 0xF) == 0);

    if ((n % 4 == 0) && aligned) {
        long n4 = n / 4;
        long blocks = (n4 + block - 1) / block;
        scalar_mul_vec4_kernel<<<blocks, block, 0, stream>>>(
            reinterpret_cast<const float4*>(a_ptr),
            reinterpret_cast<float4*>(c_ptr),
            sf,
            n4);
    } else {
        long blocks = (n + block - 1) / block;
        scalar_mul_kernel<<<blocks, block, 0, stream>>>(a_ptr, c_ptr, sf, n);
    }

    return C;
}
"""

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_cuda_source,
    functions=["scalar_mul_cuda"],
    verbose=False,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
)


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

    def forward(self, A: torch.Tensor, s: float) -> torch.Tensor:
        if A.is_cuda and A.dtype == torch.float32:
            return scalar_mul_ext.scalar_mul_cuda(A, float(s))
        # Fallback for CPU tensors or non-float32 dtypes
        return A * s
```