You write custom CUDA kernels to replace the pytorch operators in the given architecture to get speedups. 

    You have complete freedom to choose the set of operators you want to replace. You may make the decision to replace some operators with custom CUDA kernels and leave others unchanged. You may replace multiple operators with custom implementations, consider operator fusion opportunities (combining multiple operators into a single kernel, for example, combining matmul+relu), or algorithmic changes (such as online softmax). You are only limited by your imagination.


        Here's an example to show you the syntax of inline embedding custom CUDA operators in torch: The example given architecture is: 

        ``` 

        # 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


class Model(nn.Module):
    def __init__(self) -> None:
        super().__init__()

    def forward(self, a, b):
        return a + b


def get_inputs():
    # randomly generate input tensors based on the model architecture
    a = torch.randn(1, 128).cuda()
    b = torch.randn(1, 128).cuda()
    return [a, b]


def get_init_inputs():
    # randomly generate tensors required for initialization based on the model architecture
    return []

        ``` 

        The example new arch with custom CUDA kernels looks like this:
        ```
        # 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

# Define the custom CUDA kernel for element-wise addition
elementwise_add_source = """
#include <torch/extension.h>
#include <cuda_runtime.h>

__global__ void elementwise_add_kernel(const float* a, const float* b, float* out, int size) {
    int idx = blockIdx.x * blockDim.x + threadIdx.x;
    if (idx < size) {
        out[idx] = a[idx] + b[idx];
    }
}

torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b) {
    auto size = a.numel();
    auto out = torch::zeros_like(a);

    const int block_size = 256;
    const int num_blocks = (size + block_size - 1) / block_size;

    elementwise_add_kernel<<<num_blocks, block_size>>>(a.data_ptr<float>(), b.data_ptr<float>(), out.data_ptr<float>(), size);

    return out;
}
"""

elementwise_add_cpp_source = (
    "torch::Tensor elementwise_add_cuda(torch::Tensor a, torch::Tensor b);"
)

# Compile the inline CUDA code for element-wise addition
elementwise_add = load_inline(
    name="elementwise_add",
    cpp_sources=elementwise_add_cpp_source,
    cuda_sources=elementwise_add_source,
    functions=["elementwise_add_cuda"],
    verbose=True,
    extra_cflags=[""],
    extra_ldflags=[""],
)


class ModelNew(nn.Module):
    def __init__(self) -> None:
        super().__init__()
        self.elementwise_add = elementwise_add

    def forward(self, a, b):
        return self.elementwise_add.elementwise_add_cuda(a, b)

        ``` 

        
    You are given the following architecture: 

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

class Model(nn.Module):
    """
    Model that performs a convolution, applies Group Normalization, Tanh, HardSwish, 
    Residual Addition, and LogSumExp.
    """
    def __init__(self, in_channels, out_channels, kernel_size, groups, eps=1e-5):
        super(Model, self).__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)
        self.group_norm = nn.GroupNorm(groups, out_channels, eps=eps)
        self.tanh = nn.Tanh()
        self.hard_swish = nn.Hardswish()

    def forward(self, x):
        # Convolution
        x_conv = self.conv(x)
        # Group Normalization
        x_norm = self.group_norm(x_conv)
        # Tanh
        x_tanh = self.tanh(x_norm)
        # HardSwish
        x_hard_swish = self.hard_swish(x_tanh)
        # Residual Addition
        x_res = x_conv + x_hard_swish
        # LogSumExp
        x_logsumexp = torch.logsumexp(x_res, dim=1, keepdim=True)
        return x_logsumexp

batch_size = 128
in_channels = 8
out_channels = 64
height, width = 128, 128
kernel_size = 3
groups = 16

def get_inputs():
    return [torch.rand(batch_size, in_channels, height, width)]

def get_init_inputs():
    return [in_channels, out_channels, kernel_size, groups]
    ```
    
Optimize the architecture named Model with custom CUDA operators! Name your optimized output architecture ModelNew. Output the new code in codeblocks. Please generate real code, NOT pseudocode, make sure the code compiles and is fully functional. Just output the new model code, no other text, and NO testing code! 



This is turn 3 of 6 for this task. Your previous solution (turn 2) was:
```python
import torch
import torch.nn as nn
from torch.utils.cpp_extension import load_inline


_cuda_source = r"""
#include <torch/extension.h>
#include <c10/cuda/CUDAGuard.h>
#include <c10/cuda/CUDAStream.h>
#include <c10/cuda/CUDAException.h>
#include <cuda.h>
#include <cuda_runtime.h>
#include <math_constants.h>

constexpr int STATS_THREADS = 256;
constexpr int CHUNK_SIZE = 4096;

__device__ __forceinline__ float warp_sum(float value) {
    #pragma unroll
    for (int offset = 16; offset > 0; offset >>= 1)
        value += __shfl_down_sync(0xffffffff, value, offset);
    return value;
}

__global__ void partial_moments(
    const float* __restrict__ x,
    float2* __restrict__ partial,
    int group_size,
    int chunks
) {
    const int chunk = blockIdx.x;
    const int row = blockIdx.y;
    const int tid = threadIdx.x;
    const int start = chunk * CHUNK_SIZE;
    const float* base = x + static_cast<int64_t>(row) * group_size;

    float sum = 0.0f;
    float square_sum = 0.0f;

    #pragma unroll
    for (int j = 0; j < CHUNK_SIZE / STATS_THREADS; ++j) {
        const int index = start + tid + j * STATS_THREADS;
        if (index < group_size) {
            const float value = base[index];
            sum += value;
            square_sum = fmaf(value, value, square_sum);
        }
    }

    sum = warp_sum(sum);
    square_sum = warp_sum(square_sum);

    __shared__ float shared_sum[8];
    __shared__ float shared_square_sum[8];

    const int lane = tid & 31;
    const int warp = tid >> 5;
    if (lane == 0) {
        shared_sum[warp] = sum;
        shared_square_sum[warp] = square_sum;
    }
    __syncthreads();

    if (warp == 0) {
        sum = lane < 8 ? shared_sum[lane] : 0.0f;
        square_sum = lane < 8 ? shared_square_sum[lane] : 0.0f;
        sum = warp_sum(sum);
        square_sum = warp_sum(square_sum);

        if (lane == 0)
            partial[static_cast<int64_t>(row) * chunks + chunk] =
                make_float2(sum, square_sum);
    }
}

__global__ void finish_moments(
    const float2* __restrict__ partial,
    float2* __restrict__ moments,
    int group_size,
    int chunks,
    float eps
) {
    const int row = blockIdx.x;
    const int lane = threadIdx.x;
    float sum = 0.0f;
    float square_sum = 0.0f;

    for (int i = lane; i < chunks; i += 32) {
        const float2 value =
            partial[static_cast<int64_t>(row) * chunks + i];
        sum += value.x;
        square_sum += value.y;
    }

    sum = warp_sum(sum);
    square_sum = warp_sum(square_sum);

    if (lane == 0) {
        const float mean = sum / static_cast<float>(group_size);
        const float variance = fmaxf(
            fmaf(-mean, mean, square_sum / static_cast<float>(group_size)),
            0.0f
        );
        moments[row] = make_float2(mean, rsqrtf(variance + eps));
    }
}

template<int CHANNELS>
__global__ void fused_normalize_activate_logsumexp(
    const float* __restrict__ x,
    const float* __restrict__ weight,
    const float* __restrict__ bias,
    const float2* __restrict__ moments,
    float* __restrict__ output,
    int spatial,
    int groups
) {
    const int position = blockIdx.x * blockDim.x + threadIdx.x;
    const int batch = blockIdx.y;
    if (position >= spatial)
        return;

    const int channels_per_group = CHANNELS / groups;
    const float* input =
        x + static_cast<int64_t>(batch) * CHANNELS * spatial + position;

    float values[CHANNELS];
    float maximum = -CUDART_INF_F;

    #pragma unroll
    for (int channel = 0; channel < CHANNELS; ++channel) {
        const float original =
            input[static_cast<int64_t>(channel) * spatial];
        const float2 stats =
            moments[batch * groups + channel / channels_per_group];

        const float normalized = fmaf(
            (original - stats.x) * stats.y,
            weight[channel],
            bias[channel]
        );
        const float activated = tanhf(normalized);
        const float hard_swish =
            (activated * (activated + 3.0f)) * (1.0f / 6.0f);
        const float residual = original + hard_swish;

        values[channel] = residual;
        maximum = fmaxf(maximum, residual);
    }

    float exponential_sum = 0.0f;
    #pragma unroll
    for (int channel = 0; channel < CHANNELS; ++channel)
        exponential_sum += __expf(values[channel] - maximum);

    output[static_cast<int64_t>(batch) * spatial + position] =
        maximum + __logf(exponential_sum);
}

torch::Tensor fused_post_conv_cuda(
    torch::Tensor x,
    torch::Tensor weight,
    torch::Tensor bias,
    int64_t groups,
    double eps
) {
    TORCH_CHECK(x.is_cuda(), "Expected a CUDA tensor");
    TORCH_CHECK(x.scalar_type() == torch::kFloat32, "Expected float32");
    TORCH_CHECK(
        x.dim() == 4 && x.size(1) == 64 && x.is_contiguous(),
        "Expected contiguous NCHW input with 64 channels"
    );
    TORCH_CHECK(groups > 0 && 64 % groups == 0, "Invalid group count");
    TORCH_CHECK(
        weight.is_cuda() && bias.is_cuda() &&
        weight.device() == x.device() && bias.device() == x.device() &&
        weight.scalar_type() == torch::kFloat32 &&
        bias.scalar_type() == torch::kFloat32 &&
        weight.is_contiguous() && bias.is_contiguous() &&
        weight.numel() == 64 && bias.numel() == 64,
        "Expected contiguous float32 affine parameters on the input device"
    );

    const c10::cuda::CUDAGuard guard(x.device());
    const auto stream =
        c10::cuda::getCurrentCUDAStream(x.get_device()).stream();

    const int batch = static_cast<int>(x.size(0));
    const int spatial = static_cast<int>(x.size(2) * x.size(3));
    const int group_count = static_cast<int>(groups);

    auto output = torch::empty(
        {x.size(0), 1, x.size(2), x.size(3)}, x.options()
    );
    if (batch == 0 || spatial == 0)
        return output;

    const int group_size = (64 / group_count) * spatial;
    const int chunks = (group_size + CHUNK_SIZE - 1) / CHUNK_SIZE;

    auto partial = torch::empty(
        {static_cast<int64_t>(batch) * group_count, chunks, 2},
        x.options()
    );
    auto moments = torch::empty(
        {static_cast<int64_t>(batch) * group_count, 2},
        x.options()
    );

    partial_moments<<<
        dim3(chunks, batch * group_count), STATS_THREADS, 0, stream
    >>>(
        x.data_ptr<float>(),
        reinterpret_cast<float2*>(partial.data_ptr<float>()),
        group_size,
        chunks
    );
    C10_CUDA_KERNEL_LAUNCH_CHECK();

    finish_moments<<<batch * group_count, 32, 0, stream>>>(
        reinterpret_cast<const float2*>(partial.data_ptr<float>()),
        reinterpret_cast<float2*>(moments.data_ptr<float>()),
        group_size,
        chunks,
        static_cast<float>(eps)
    );
    C10_CUDA_KERNEL_LAUNCH_CHECK();

    constexpr int threads = 128;
    fused_normalize_activate_logsumexp<64><<<
        dim3((spatial + threads - 1) / threads, batch),
        threads, 0, stream
    >>>(
        x.data_ptr<float>(),
        weight.data_ptr<float>(),
        bias.data_ptr<float>(),
        reinterpret_cast<const float2*>(moments.data_ptr<float>()),
        output.data_ptr<float>(),
        spatial,
        group_count
    );
    C10_CUDA_KERNEL_LAUNCH_CHECK();

    return output;
}
"""

_cpp_source = r"""
#include <torch/extension.h>

torch::Tensor fused_post_conv_cuda(
    torch::Tensor x,
    torch::Tensor weight,
    torch::Tensor bias,
    int64_t groups,
    double eps
);
"""

_fused_ops = load_inline(
    name="fused_gn_tanh_hswish_residual_lse_v2",
    cpp_sources=_cpp_source,
    cuda_sources=_cuda_source,
    functions=["fused_post_conv_cuda"],
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
    verbose=False,
)


class ModelNew(nn.Module):
    def __init__(
        self, in_channels, out_channels, kernel_size, groups, eps=1e-5
    ):
        super().__init__()
        self.conv = nn.Conv2d(in_channels, out_channels, kernel_size)
        self.group_norm = nn.GroupNorm(groups, out_channels, eps=eps)
        self.tanh = nn.Tanh()
        self.hard_swish = nn.Hardswish()

    def forward(self, x):
        x_conv = self.conv(x)

        if (
            x_conv.is_cuda
            and x_conv.dtype == torch.float32
            and x_conv.shape[1] == 64
            and x_conv.is_contiguous()
            and self.group_norm.weight.dtype == torch.float32
            and self.group_norm.bias.dtype == torch.float32
        ):
            return _fused_ops.fused_post_conv_cuda(
                x_conv,
                self.group_norm.weight,
                self.group_norm.bias,
                self.group_norm.num_groups,
                self.group_norm.eps,
            )

        x_norm = self.group_norm(x_conv)
        x_activated = self.hard_swish(self.tanh(x_norm))
        return torch.logsumexp(x_conv + x_activated, dim=1, keepdim=True)
```
Result of evaluating it:
The solution is correct on the test inputs. Mean runtime 4.48 ms; the PyTorch reference (with TF32 enabled) takes 9.69 ms; speedup 2.163x.
Profiler, top device kernels of one forward pass of your solution:
| device kernel | total device time (us) | calls |
|---|---|---|
| _5x_cudnn_ampere_scudnn_winograd_128x128_ldg1_ldg4_relu_tile148t_nt_v1 | 2492.4 | 1 |
| void fused_normalize_activate_logsumexp<64>(float const*, float const*, float const*, floa | 787.3 | 1 |
| void at::native::elementwise_kernel<128, 2, at::native::gpu_kernel_impl_nocast<at::native: | 782.0 | 1 |
| partial_moments(float const*, float2*, int, int) | 378.3 | 1 |
| finish_moments(float2 const*, float2*, int, int, float) | 4.5 | 1 |
| void cudnn::winograd::generateWinogradTilesKernel<0, float, float>(cudnn::winograd::Genera | 3.2 | 1 |

Write an improved complete ModelNew: fix any error, and if the solution is already correct, make it faster. Output the new code in one ```python codeblock. Just the model code, no other text, and NO testing code.
