```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
import torch.nn.functional as F
from torch.utils.cpp_extension import load_inline

# Fused CUDA kernel: out = min(x, constant) - constant
# This fuses the two elementwise ops (torch.min + subtraction) into a single
# kernel, eliminating one full read/write pass over the activation tensor and
# one kernel launch. The GEMM itself is left to cuBLAS (F.linear), which is
# already near-peak for this size.
fused_min_sub_source = """
#include <torch/extension.h>
#include <cuda_runtime.h>

__global__ void fused_min_sub_kernel_vec4(const float4* __restrict__ x,
                                          float4* __restrict__ out,
                                          const float* __restrict__ c_ptr,
                                          int n4) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n4) {
        float c = c_ptr[0];
        float4 v = x[idx];
        v.x = fminf(v.x, c) - c;
        v.y = fminf(v.y, c) - c;
        v.z = fminf(v.z, c) - c;
        v.w = fminf(v.w, c) - c;
        out[idx] = v;
    }
}

__global__ void fused_min_sub_kernel(const float* __restrict__ x,
                                     float* __restrict__ out,
                                     const float* __restrict__ c_ptr,
                                     int n) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n) {
        float c = c_ptr[0];
        out[idx] = fminf(x[idx], c) - c;
    }
}

torch::Tensor fused_min_sub_cuda(torch::Tensor x, torch::Tensor c) {
    TORCH_CHECK(x.is_cuda() && c.is_cuda(), "Inputs must be CUDA tensors");
    TORCH_CHECK(x.scalar_type() == torch::kFloat32, "x must be float32");
    auto x_contig = x.contiguous();
    auto out = torch::empty_like(x_contig);
    int n = x_contig.numel();
    if (n == 0) return out;

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

    if (n % 4 == 0) {
        int n4 = n / 4;
        int grid = (n4 + block - 1) / block;
        fused_min_sub_kernel_vec4<<<grid, block, 0, stream>>>(
            reinterpret_cast<const float4*>(x_contig.data_ptr<float>()),
            reinterpret_cast<float4*>(out.data_ptr<float>()),
            c.data_ptr<float>(), n4);
    } else {
        int grid = (n + block - 1) / block;
        fused_min_sub_kernel<<<grid, block, 0, stream>>>(
            x_contig.data_ptr<float>(), out.data_ptr<float>(),
            c.data_ptr<float>(), n);
    }
    return out;
}
"""

fused_min_sub_cpp_source = (
    "torch::Tensor fused_min_sub_cuda(torch::Tensor x, torch::Tensor c);"
)

fused_min_sub = load_inline(
    name="fused_min_sub",
    cpp_sources=fused_min_sub_cpp_source,
    cuda_sources=fused_min_sub_source,
    functions=["fused_min_sub_cuda"],
    verbose=False,
    extra_cflags=[""],
    extra_ldflags=[""],
)


class ModelNew(nn.Module):
    """
    Optimized model: cuBLAS GEMM (F.linear) followed by a single fused
    CUDA kernel computing min(x, constant) - constant.
    """
    def __init__(self, in_features, out_features, constant):
        super(ModelNew, self).__init__()
        self.linear = nn.Linear(in_features, out_features)
        self.constant = nn.Parameter(torch.tensor(constant))
        self.fused_min_sub = fused_min_sub

    def forward(self, x):
        x = F.linear(x, self.linear.weight, self.linear.bias)
        x = self.fused_min_sub.fused_min_sub_cuda(x, self.constant)
        return x
```