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

# Let cuDNN autotune the fastest convolution algorithm for the (fixed) input shape.
torch.backends.cudnn.benchmark = True
torch.backends.cudnn.allow_tf32 = True

# Fused CUDA kernel: applies (x - sub1) - sub2 followed by Mish, in-place,
# in a single memory pass (replacing 3 separate PyTorch kernels).
fused_epilogue_source = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#include <cuda_runtime.h>

// Mish activation with an algebraically exact, cheap formulation:
//   mish(x) = x * tanh(softplus(x))
//   with t = exp(x):
//   tanh(softplus(x)) = ((1+t)^2 - 1) / ((1+t)^2 + 1)
//                     = t*(t+2) / (t*(t+2) + 2)
// This needs only one transcendental (expf) instead of exp+log1p+tanh.
__device__ __forceinline__ float mish_op(float x) {
    // For x > ~9, tanh(softplus(x)) rounds to exactly 1.0f (float32),
    // so mish(x) == x, matching PyTorch exactly. This guard also avoids
    // overflow of expf for large x.
    if (x > 20.0f) {
        return x;
    }
    float t = expf(x);
    float num = t * (t + 2.0f);
    return x * (num / (num + 2.0f));
}

// Fused epilogue: data = mish((data - sub1) - sub2), elementwise, in-place.
// Vectorized with float4 (16 bytes per thread) for full memory coalescing.
__global__ void fused_sub_mish_kernel(float* __restrict__ data,
                                      const float sub1,
                                      const float sub2,
                                      const int n) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    int base = idx << 2;
    if (base + 3 < n) {
        float4 v = *reinterpret_cast<float4*>(data + base);
        v.x = mish_op((v.x - sub1) - sub2);
        v.y = mish_op((v.y - sub1) - sub2);
        v.z = mish_op((v.z - sub1) - sub2);
        v.w = mish_op((v.w - sub1) - sub2);
        *reinterpret_cast<float4*>(data + base) = v;
    } else {
        // Tail (n not divisible by 4)
        for (int i = base; i < n; ++i) {
            data[i] = mish_op((data[i] - sub1) - sub2);
        }
    }
}

torch::Tensor fused_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() == torch::kFloat32, "x must be float32");

    const c10::cuda::CUDAGuard device_guard(x.device());
    auto xc = x.contiguous();

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

    const int block_size = 256;
    const int64_t threads = (n + 3) / 4;
    const int64_t blocks = (threads + block_size - 1) / block_size;

    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    fused_sub_mish_kernel<<<static_cast<unsigned int>(blocks), block_size, 0, stream>>>(
        xc.data_ptr<float>(),
        static_cast<float>(sub1),
        static_cast<float>(sub2),
        static_cast<int>(n));

    return xc;
}
"""

fused_epilogue_cpp_source = (
    "torch::Tensor fused_sub_mish_cuda(torch::Tensor x, double sub1, double sub2);"
)

# Compile the inline fused CUDA epilogue
fused_epilogue = load_inline(
    name="fused_sub_mish",
    cpp_sources=fused_epilogue_cpp_source,
    cuda_sources=fused_epilogue_source,
    functions=["fused_sub_mish_cuda"],
    verbose=False,
)


class ModelNew(nn.Module):
    """
    Optimized model: convolution runs through cuDNN (autotuned), and the two
    subtractions plus the Mish activation are fused into a single custom CUDA
    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)
        self.fused_epilogue = fused_epilogue

    def forward(self, x):
        x = self.conv(x)
        x = self.fused_epilogue.fused_sub_mish_cuda(
            x, self.subtract_value_1, self.subtract_value_2
        )
        return x
```