```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 op is purely memory-bandwidth bound, so the kernel is tuned to
# saturate DRAM bandwidth:
#   * 128-bit vectorized (float4) loads/stores
#   * 8x unroll for high memory-level parallelism
#   * streaming cache hints (__ldcs / __stcs, evict-first) so the
#     single-use data does not pollute L1/L2
#   * launch on the current CUDA stream
scalar_mul_source = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>
#include <cstdint>
#include <algorithm>

namespace {

constexpr int kBlock = 256;
constexpr int kUnroll = 8;

__global__ void __launch_bounds__(kBlock)
scalar_mul_vec4_kernel(const float4* __restrict__ A,
                       float4* __restrict__ C,
                       const float s,
                       const int64_t n4) {
    const int64_t base = (int64_t)blockIdx.x * (kBlock * kUnroll) + threadIdx.x;
    #pragma unroll
    for (int j = 0; j < kUnroll; ++j) {
        const int64_t i = base + (int64_t)j * kBlock;
        if (i < n4) {
            float4 v = __ldcs(A + i);
            v.x *= s;
            v.y *= s;
            v.z *= s;
            v.w *= s;
            __stcs(C + i, v);
        }
    }
}

__global__ void __launch_bounds__(kBlock)
scalar_mul_kernel(const float* __restrict__ A,
                  float* __restrict__ C,
                  const float s,
                  const int64_t n) {
    const int64_t stride = (int64_t)gridDim.x * kBlock;
    for (int64_t i = (int64_t)blockIdx.x * kBlock + threadIdx.x; i < n; i += stride) {
        __stcs(C + i, __ldcs(A + 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() == at::kFloat, "A must be float32");

    auto Ac = A.is_contiguous() ? A : A.contiguous();
    auto C = torch::empty_like(Ac);

    const int64_t n = Ac.numel();
    if (n == 0) {
        return C;
    }

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

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

    if (aligned) {
        const int64_t n4 = n >> 2;
        if (n4 > 0) {
            const int64_t per_block = (int64_t)kBlock * kUnroll;
            const int64_t blocks = (n4 + per_block - 1) / per_block;
            scalar_mul_vec4_kernel<<<(unsigned int)blocks, kBlock, 0, stream>>>(
                reinterpret_cast<const float4*>(a_ptr),
                reinterpret_cast<float4*>(c_ptr), sf, n4);
        }
        const int64_t rem = n & 3;
        if (rem > 0) {
            const int64_t start = n4 << 2;
            scalar_mul_kernel<<<1, kBlock, 0, stream>>>(
                a_ptr + start, c_ptr + start, sf, rem);
        }
    } else {
        const int64_t full_blocks = (n + kBlock - 1) / kBlock;
        const int64_t blocks = full_blocks < 4096 ? full_blocks : 4096;
        scalar_mul_kernel<<<(unsigned int)blocks, kBlock, 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_stream_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"],
    extra_ldflags=[""],
)


class ModelNew(nn.Module):
    """
    Optimized model that performs matrix-scalar multiplication (C = A * s)
    using a custom vectorized, streaming CUDA kernel.
    """
    def __init__(self):
        super(ModelNew, self).__init__()
        self._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._ext.scalar_mul_cuda(A, float(s))
        # Fallback for non-CUDA / non-FP32 inputs
        return A * s
```