```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 memory-bandwidth bound, so the kernel is optimized for
# maximum throughput using:
#   1. Vectorized 128-bit float4 loads/stores.
#   2. Four independent float4 transactions per thread (ILP / unrolling).
#   3. Perfectly coalesced global memory access.
scalar_mul_source = """
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>
#include <cstdint>

// Vectorized kernel: each thread processes 4 float4 elements (16 floats).
__global__ void scalar_mul_vec4_kernel(const float4* __restrict__ A,
                                       float4* __restrict__ C,
                                       float s,
                                       long long n4) {
    long long base = (long long)blockIdx.x * ((long long)blockDim.x * 4)
                     + threadIdx.x;
    long long step = blockDim.x;
    #pragma unroll
    for (int k = 0; k < 4; ++k) {
        long long i = base + (long long)k * step;
        if (i < n4) {
            float4 a = A[i];
            a.x *= s;
            a.y *= s;
            a.z *= s;
            a.w *= s;
            C[i] = a;
        }
    }
}

// Scalar kernel used for the tail elements and as a general fallback.
__global__ void scalar_mul_scalar_kernel(const float* __restrict__ A,
                                         float* __restrict__ C,
                                         float s,
                                         long long start,
                                         long long n) {
    long long idx = start + (long long)blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n) {
        C[idx] = A[idx] * 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 of type float32");

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

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

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

    const int block_size = 256;

    if (aligned16) {
        long long n4 = n / 4;
        if (n4 > 0) {
            long long work_per_block = (long long)block_size * 4;
            long long num_blocks =
                (n4 + work_per_block - 1) / work_per_block;
            scalar_mul_vec4_kernel<<<num_blocks, block_size, 0, stream>>>(
                reinterpret_cast<const float4*>(a_ptr),
                reinterpret_cast<float4*>(c_ptr),
                sf, n4);
        }
        long long start = n4 * 4;
        if (start < n) {
            long long rem = n - start;
            long long num_blocks = (rem + block_size - 1) / block_size;
            scalar_mul_scalar_kernel<<<num_blocks, block_size, 0, stream>>>(
                a_ptr, c_ptr, sf, start, n);
        }
    } else {
        long long num_blocks = (n + block_size - 1) / block_size;
        scalar_mul_scalar_kernel<<<num_blocks, block_size, 0, stream>>>(
            a_ptr, c_ptr, sf, 0LL, 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_source,
    functions=["scalar_mul_cuda"],
    verbose=True,
    extra_cuda_cflags=["-O3", "--use_fast_math"],
)


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__()
        self.scalar_mul_ext = 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_ext.scalar_mul_cuda(A, float(s))
        # Fallback for non-CUDA / non-float32 inputs
        return A * s
```