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

# Let cuDNN autotune the best convolution algorithm for our fixed shape.
torch.backends.cudnn.benchmark = True

# Fused kernel: (x - sub1 - sub2) followed by Mish activation, in a single pass.
# Mish(x) = x * tanh(softplus(x)) = x * exp(x) / (exp(x) + 2)
sub_mish_cuda_source = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>

__device__ __forceinline__ float sub_mish_scalar(float v, float s1, float s2) {
    // Match the original op order exactly: (x - s1) - s2, then mish.
    float t = (v - s1) - s2;
    // For t > 20, mish(t) == t up to float precision (and expf stays finite).
    if (t > 20.0f) {
        return t;
    }
    float e = expf(t);
    return t * e / (e + 2.0f);
}

__global__ void sub_mish_kernel(const float4* __restrict__ in,
                                float4* __restrict__ out,
                                float s1, float s2,
                                long long n4, long long n) {
    long long i = (long long)blockIdx.x * blockDim.x + threadIdx.x;
    if (i < n4) {
        float4 v = in[i];
        v.x = sub_mish_scalar(v.x, s1, s2);
        v.y = sub_mish_scalar(v.y, s1, s2);
        v.z = sub_mish_scalar(v.z, s1, s2);
        v.w = sub_mish_scalar(v.w, s1, s2);
        out[i] = v;
    }
    // Handle the tail (n % 4 elements) with a single thread.
    if (blockIdx.x == 0 && threadIdx.x == 0) {
        const float* inf = reinterpret_cast<const float*>(in);
        float* outf = reinterpret_cast<float*>(out);
        for (long long j = n4 * 4; j < n; ++j) {
            outf[j] = sub_mish_scalar(inf[j], s1, s2);
        }
    }
}

torch::Tensor sub_mish_cuda(torch::Tensor x, double sub1, double sub2) {
    TORCH_CHECK(x.is_cuda(), "x must be a CUDA tensor");
    TORCH_CHECK(x.scalar_type() == at::kFloat, "x must be float32");
    TORCH_CHECK(x.is_non_overlapping_and_dense(), "x must be dense");

    auto out = torch::empty_like(x);
    long long n = (long long)x.numel();
    if (n == 0) {
        return out;
    }
    long long n4 = n / 4;
    const int threads = 256;
    long long blocks = (n4 + threads - 1) / threads;
    if (blocks < 1) blocks = 1;

    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    sub_mish_kernel<<<blocks, threads, 0, stream>>>(
        reinterpret_cast<const float4*>(x.data_ptr<float>()),
        reinterpret_cast<float4*>(out.data_ptr<float>()),
        (float)sub1, (float)sub2, n4, n);
    return out;
}
"""

sub_mish_cpp_source = (
    "torch::Tensor sub_mish_cuda(torch::Tensor x, double sub1, double sub2);"
)

fused_sub_mish = load_inline(
    name="fused_sub_mish",
    cpp_sources=sub_mish_cpp_source,
    cuda_sources=sub_mish_cuda_source,
    functions=["sub_mish_cuda"],
    verbose=False,
    extra_cuda_cflags=["-O3", "--use_fast_math"],
)


class ModelNew(nn.Module):
    """
    Optimized model:
      - Conv2d runs through cuDNN in channels_last (NHWC) layout so it can use
        the fastest tensor-core kernels.
      - The two scalar subtractions and the Mish activation are fused into a
        single custom CUDA elementwise kernel (one memory pass instead of three).
    """
    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)
        self.subtract_value_1 = float(subtract_value_1)
        self.subtract_value_2 = float(subtract_value_2)

    def forward(self, x):
        x = x.contiguous(memory_format=torch.channels_last)
        w = self.conv.weight.contiguous(memory_format=torch.channels_last)
        x = F.conv2d(x, w, self.conv.bias)
        x = fused_sub_mish.sub_mish_cuda(
            x, self.subtract_value_1, self.subtract_value_2
        )
        return x
```