```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 that performs:
#     v = (x - subtract_value_1) - subtract_value_2
#     out = mish(v) = v * tanh(log1p(exp(v)))
# in a single memory pass (instead of three separate elementwise passes).
# Vectorized with float4 loads/stores when possible.
fused_sub_mish_cuda_source = r"""
#include <torch/extension.h>
#include <cuda_runtime.h>
#include <cstdint>

__device__ __forceinline__ float mish(float v) {
    return v * tanhf(log1pf(expf(v)));
}

__global__ void fused_sub_mish_kernel_vec4(const float4* __restrict__ x,
                                           float4* __restrict__ out,
                                           const long n4,
                                           const float s1,
                                           const float s2) {
    const long idx = (long)blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n4) {
        float4 v = x[idx];
        float4 r;
        r.x = mish((v.x - s1) - s2);
        r.y = mish((v.y - s1) - s2);
        r.z = mish((v.z - s1) - s2);
        r.w = mish((v.w - s1) - s2);
        out[idx] = r;
    }
}

__global__ void fused_sub_mish_kernel_scalar(const float* __restrict__ x,
                                             float* __restrict__ out,
                                             const long n,
                                             const float s1,
                                             const float s2) {
    const long idx = (long)blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < n) {
        out[idx] = mish((x[idx] - s1) - s2);
    }
}

torch::Tensor fused_sub_mish_cuda(torch::Tensor x, double s1, double s2) {
    auto out = torch::empty_like(x);
    const long n = (long)x.numel();
    if (n == 0) {
        return out;
    }

    const int block_size = 256;
    const float fs1 = (float)s1;
    const float fs2 = (float)s2;

    const bool aligned =
        (reinterpret_cast<uintptr_t>(x.data_ptr<float>()) % 16 == 0) &&
        (reinterpret_cast<uintptr_t>(out.data_ptr<float>()) % 16 == 0);

    if (((n & 3) == 0) && aligned) {
        const long n4 = n >> 2;
        const long grid = (n4 + block_size - 1) / block_size;
        fused_sub_mish_kernel_vec4<<<grid, block_size>>>(
            reinterpret_cast<const float4*>(x.data_ptr<float>()),
            reinterpret_cast<float4*>(out.data_ptr<float>()),
            n4, fs1, fs2);
    } else {
        const long grid = (n + block_size - 1) / block_size;
        fused_sub_mish_kernel_scalar<<<grid, block_size>>>(
            x.data_ptr<float>(), out.data_ptr<float>(), n, fs1, fs2);
    }
    return out;
}
"""

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

# Compile the inline CUDA code for the fused subtract + Mish kernel
fused_sub_mish = load_inline(
    name="fused_sub_mish",
    cpp_sources=fused_sub_mish_cpp_source,
    cuda_sources=fused_sub_mish_cuda_source,
    functions=["fused_sub_mish_cuda"],
    verbose=True,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
    extra_ldflags=[""],
)


class ModelNew(nn.Module):
    """
    Optimized version of Model.

    Optimizations:
      * The convolution is kept on the highly-optimized cuDNN path, with
        cudnn.benchmark enabled so the fastest algorithm is autotuned for
        this (fixed) input shape.
      * The two subtractions and the Mish activation are fused into a single
        custom CUDA kernel, reducing three full read+write passes over the
        (large) activation tensor to one, using vectorized float4 accesses.
    """
    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)
        # Enable cuDNN autotuning to pick the fastest conv algorithm.
        torch.backends.cudnn.benchmark = True

    def forward(self, x):
        x = self.conv(x)
        if x.is_cuda and x.dtype == torch.float32:
            if not x.is_contiguous():
                x = x.contiguous()
            return fused_sub_mish.fused_sub_mish_cuda(
                x, self.subtract_value_1, self.subtract_value_2)
        # Fallback for non-CUDA / non-fp32 tensors: original sequence of ops.
        x = x - self.subtract_value_1
        x = x - self.subtract_value_2
        return F.mish(x)
```