```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 fastest convolution algorithm for the fixed shapes.
torch.backends.cudnn.benchmark = True

# Fused CUDA kernel: (x - s1 - s2) followed by Mish activation in a single pass.
# This eliminates two full elementwise read/write passes over the conv output.
fused_sub_mish_cuda_source = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <cuda_runtime.h>
#include <cstdint>

__device__ __forceinline__ float mish_act(float x) {
    // mish(x) = x * tanh(softplus(x)) = x * tanh(log1p(exp(x)))
    // For large x, expf(x) -> inf, log1pf(inf) -> inf, tanhf(inf) -> 1.0f,
    // so the result gracefully reduces to x (matching PyTorch behavior).
    return x * tanhf(log1pf(expf(x)));
}

__global__ void fused_sub_mish_vec4_kernel(const float4* __restrict__ in,
                                           float4* __restrict__ out,
                                           int64_t n4,
                                           float s1,
                                           float s2) {
    int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
    int64_t stride = (int64_t)gridDim.x * blockDim.x;
    for (int64_t i = idx; i < n4; i += stride) {
        float4 v = in[i];
        v.x = mish_act((v.x - s1) - s2);
        v.y = mish_act((v.y - s1) - s2);
        v.z = mish_act((v.z - s1) - s2);
        v.w = mish_act((v.w - s1) - s2);
        out[i] = v;
    }
}

__global__ void fused_sub_mish_kernel(const float* __restrict__ in,
                                      float* __restrict__ out,
                                      int64_t n,
                                      float s1,
                                      float s2) {
    int64_t idx = (int64_t)blockIdx.x * blockDim.x + threadIdx.x;
    int64_t stride = (int64_t)gridDim.x * blockDim.x;
    for (int64_t i = idx; i < n; i += stride) {
        float v = in[i];
        out[i] = mish_act((v - s1) - s2);
    }
}

torch::Tensor fused_sub_mish_cuda(torch::Tensor x, double s1, double s2) {
    TORCH_CHECK(x.is_cuda(), "Input must be a CUDA tensor");
    TORCH_CHECK(x.scalar_type() == at::kFloat, "Input must be float32");

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

    const float fs1 = static_cast<float>(s1);
    const float fs2 = static_cast<float>(s2);
    const float* in_ptr = xc.data_ptr<float>();
    float* out_ptr = out.data_ptr<float>();

    const int block = 256;
    const int64_t max_grid = 262144;
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();

    const bool aligned =
        ((reinterpret_cast<uintptr_t>(in_ptr) & 15) == 0) &&
        ((reinterpret_cast<uintptr_t>(out_ptr) & 15) == 0);

    if (((n & 3) == 0) && aligned) {
        const int64_t n4 = n >> 2;
        int64_t grid = (n4 + block - 1) / block;
        if (grid > max_grid) grid = max_grid;
        fused_sub_mish_vec4_kernel<<<(int)grid, block, 0, stream>>>(
            reinterpret_cast<const float4*>(in_ptr),
            reinterpret_cast<float4*>(out_ptr),
            n4, fs1, fs2);
    } else {
        int64_t grid = (n + block - 1) / block;
        if (grid > max_grid) grid = max_grid;
        fused_sub_mish_kernel<<<(int)grid, block, 0, stream>>>(
            in_ptr, out_ptr, n, fs1, fs2);
    }
    return out;
}
"""

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

try:
    _fused_sub_mish = load_inline(
        name="fused_sub_mish_ext_v1",
        cpp_sources=fused_sub_mish_cpp_source,
        cuda_sources=fused_sub_mish_cuda_source,
        functions=["fused_sub_mish_cuda"],
        extra_cuda_cflags=["-O3"],
        verbose=False,
    )
except Exception:
    _fused_sub_mish = None


class ModelNew(nn.Module):
    """
    Optimized model: Conv2d (cuDNN) followed by a single fused CUDA kernel that
    performs both scalar subtractions and the Mish activation in one pass,
    eliminating two full memory round-trips over the convolution output.
    """

    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 = self.conv(x)
        if (_fused_sub_mish is not None) and x.is_cuda and x.dtype == torch.float32:
            return _fused_sub_mish.fused_sub_mish_cuda(
                x, self.subtract_value_1, self.subtract_value_2)
        x = x - self.subtract_value_1
        x = x - self.subtract_value_2
        return F.mish(x)
```