```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 operation is purely memory-bandwidth bound, so the kernel is optimized
# for maximum throughput using vectorized 128-bit (float4) loads/stores,
# a grid-stride loop, and a scalar fallback path for unaligned/leftover data.
scalar_mul_cuda_source = """
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>
#include <cstdint>
#include <algorithm>

// Vectorized kernel: each thread processes float4 (4 floats) per iteration.
__global__ void scalar_mul_vec4_kernel(const float4* __restrict__ A,
                                       float4* __restrict__ C,
                                       float s,
                                       int64_t n_vec) {
    int64_t i = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
    int64_t stride = (int64_t)gridDim.x * blockDim.x;
    for (; i < n_vec; i += stride) {
        float4 a = A[i];
        a.x *= s;
        a.y *= s;
        a.z *= s;
        a.w *= s;
        C[i] = a;
    }
}

// Scalar fallback kernel (handles unaligned data and tail elements).
__global__ void scalar_mul_scalar_kernel(const float* __restrict__ A,
                                         float* __restrict__ C,
                                         float s,
                                         int64_t n) {
    int64_t i = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
    int64_t stride = (int64_t)gridDim.x * blockDim.x;
    for (; i < n; i += stride) {
        C[i] = A[i] * s;
    }
}

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

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

    const float* a_ptr = Ac.data_ptr<float>();
    float* c_ptr = C.data_ptr<float>();
    const float sf = static_cast<float>(s);

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

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

    if (aligned) {
        int64_t n_vec = n / 4;
        if (n_vec > 0) {
            int64_t blocks_needed = (n_vec + block_size - 1) / block_size;
            int blocks = (int)std::min<int64_t>(blocks_needed, 2147483647LL);
            scalar_mul_vec4_kernel<<<blocks, block_size, 0, stream>>>(
                reinterpret_cast<const float4*>(a_ptr),
                reinterpret_cast<float4*>(c_ptr),
                sf,
                n_vec);
        }
        // Handle the remaining (n % 4) tail elements, if any.
        int64_t tail_start = n_vec * 4;
        int64_t tail_n = n - tail_start;
        if (tail_n > 0) {
            scalar_mul_scalar_kernel<<<1, 32, 0, stream>>>(
                a_ptr + tail_start, c_ptr + tail_start, sf, tail_n);
        }
    } else {
        int64_t blocks_needed = (n + block_size - 1) / block_size;
        int blocks = (int)std::min<int64_t>(blocks_needed, 2147483647LL);
        scalar_mul_scalar_kernel<<<blocks, block_size, 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=True,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3", "--use_fast_math"],
    extra_ldflags=[""],
)


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:
        if A.is_cuda and A.dtype == torch.float32:
            return self.scalar_mul.scalar_mul_cuda(A, float(s))
        # Fallback to the native PyTorch op for non-CUDA / non-fp32 inputs.
        return A * s
```