```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 that fuses the two element-wise ops:
#   x = torch.min(x, constant)
#   x = x - constant
# into a single pass over the linear layer's output (with float4 vectorization).
fused_min_sub_cuda_source = """
#include <torch/extension.h>
#include <cuda_runtime.h>

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

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

torch::Tensor fused_min_sub_cuda(torch::Tensor x, double c) {
    TORCH_CHECK(x.is_cuda(), "x must be a CUDA tensor");
    TORCH_CHECK(x.scalar_type() == torch::kFloat32, "x must be float32");

    auto x_c = x.contiguous();
    auto out = torch::empty_like(x_c);

    const int n = (int)x_c.numel();
    const float cf = static_cast<float>(c);
    const int block_size = 256;

    if (n % 4 == 0) {
        const int n4 = n / 4;
        const int num_blocks = (n4 + block_size - 1) / block_size;
        fused_min_sub_vec4_kernel<<<num_blocks, block_size>>>(
            reinterpret_cast<const float4*>(x_c.data_ptr<float>()),
            reinterpret_cast<float4*>(out.data_ptr<float>()),
            cf, n4);
    } else {
        const int num_blocks = (n + block_size - 1) / block_size;
        fused_min_sub_scalar_kernel<<<num_blocks, block_size>>>(
            x_c.data_ptr<float>(), out.data_ptr<float>(), cf, n);
    }

    return out;
}
"""

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

# Compile the inline CUDA code for the fused min + subtract epilogue
fused_min_sub = load_inline(
    name="fused_min_sub",
    cpp_sources=fused_min_sub_cpp_source,
    cuda_sources=fused_min_sub_cuda_source,
    functions=["fused_min_sub_cuda"],
    verbose=False,
)


class ModelNew(nn.Module):
    """
    Optimized model:
      - matrix multiplication (matmul + bias) is handled by the highly tuned
        cuBLAS-backed nn.Linear,
      - the subsequent `torch.min(x, constant)` and `x - constant` are fused
        into a single custom CUDA kernel, eliminating one full read/write pass
        over the activation tensor and one kernel launch.
    """
    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 = self.linear(x)
        x = self.fused_min_sub.fused_min_sub_cuda(x, self.constant.item())
        return x
```