```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

# Fused CUDA kernel: subtract constant + Mish activation in a single pass.
# Replaces the 3 separate elementwise kernels (sub, sub, mish) that each
# read+write the full (large) convolution output tensor.
fused_sub_mish_cuda_source = r"""
#include <torch/extension.h>
#include <cuda_runtime.h>

__device__ __forceinline__ float mish_stable(float x) {
    // mish(x) = x * tanh(softplus(x)), softplus(x) = log1p(exp(x))
    // Threshold matches PyTorch's softplus for numerical stability.
    float sp = (x > 20.0f) ? x : log1pf(expf(x));
    return x * tanhf(sp);
}

__global__ void fused_sub_mish_vec4_kernel(const float4* __restrict__ in,
                                           float4* __restrict__ out,
                                           const float sub,
                                           const long n4) {
    long idx = (long)blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n4) {
        float4 v = in[idx];
        v.x = mish_stable(v.x - sub);
        v.y = mish_stable(v.y - sub);
        v.z = mish_stable(v.z - sub);
        v.w = mish_stable(v.w - sub);
        out[idx] = v;
    }
}

__global__ void fused_sub_mish_tail_kernel(const float* __restrict__ in,
                                           float* __restrict__ out,
                                           const float sub,
                                           const long start,
                                           const long n) {
    long idx = start + (long)blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n) {
        out[idx] = mish_stable(in[idx] - sub);
    }
}

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

    auto xc = x.contiguous();
    auto out = torch::empty_like(xc);
    const long n = xc.numel();
    if (n == 0) {
        return out;
    }

    const float* in_ptr = xc.data_ptr<float>();
    float* out_ptr = out.data_ptr<float>();
    const float sub_f = static_cast<float>(sub);
    const int block = 256;

    // Vectorized main body (float4 = 16 bytes per thread).
    const long n4 = n / 4;
    if (n4 > 0) {
        const long blocks = (n4 + block - 1) / block;
        fused_sub_mish_vec4_kernel<<<(unsigned int)blocks, block>>>(
            reinterpret_cast<const float4*>(in_ptr),
            reinterpret_cast<float4*>(out_ptr),
            sub_f,
            n4);
    }

    // Handle the (<= 3) remaining scalar elements.
    const long rem = n - n4 * 4;
    if (rem > 0) {
        fused_sub_mish_tail_kernel<<<1, (unsigned int)rem>>>(
            in_ptr, out_ptr, sub_f, n4 * 4, n);
    }

    return out;
}
"""

fused_sub_mish_cpp_source = (
    "torch::Tensor fused_sub_mish_cuda(torch::Tensor x, double sub);"
)

fused_sub_mish = load_inline(
    name="fused_sub_mish_ext",
    cpp_sources=fused_sub_mish_cpp_source,
    cuda_sources=fused_sub_mish_cuda_source,
    functions=["fused_sub_mish_cuda"],
    verbose=False,
    extra_cuda_cflags=["-O3"],
)


class ModelNew(nn.Module):
    """
    Optimized model: keeps the convolution on cuDNN and fuses
    (x - subtract_value_1 - subtract_value_2) + Mish into a single
    custom CUDA kernel. The two subtraction constants are folded into
    one scalar at init time.
    """
    def __init__(self, in_channels, out_channels, kernel_size, subtract_value_1, subtract_value_2):
        super(ModelNew, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)
        # Fold the two constants into one (plain attribute, stays out of
        # the state_dict so weight loading from the original model works).
        self.sub_total = float(subtract_value_1) + float(subtract_value_2)
        # Let cuDNN autotune the fastest convolution algorithm.
        torch.backends.cudnn.benchmark = True

    def forward(self, x):
        x = self.conv(x)
        return fused_sub_mish.fused_sub_mish_cuda(x, self.sub_total)
```