```python
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.cpp_extension import load_inline


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

torch::Tensor conv3d_forward_cuda(
    torch::Tensor input,
    torch::Tensor weight,
    c10::optional<torch::Tensor> bias);
"""

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

constexpr int TILE_M = 64;
constexpr int TILE_N = 128;
constexpr int TILE_K = 16;

__global__ __launch_bounds__(256)
void conv3d_3x3x3_kernel(
    const float* __restrict__ input,
    const float* __restrict__ weight,
    const float* __restrict__ bias,
    float* __restrict__ output,
    int channels,
    int out_channels,
    int depth,
    int height,
    int width,
    int out_depth,
    int out_height,
    int out_width,
    int spatial_size)
{
    __shared__ float shared_weight[TILE_K][TILE_M];
    __shared__ float shared_input[TILE_K][TILE_N];

    const int tid = threadIdx.x;
    const int lane = tid & 31;
    const int warp = tid >> 5;
    const int channel_start = blockIdx.y * TILE_M;
    const int spatial_start = blockIdx.x * TILE_N;
    const int batch = blockIdx.z;
    const int reduction_size = channels * 27;

    float accum[8][4];

    #pragma unroll
    for (int i = 0; i < 8; ++i) {
        #pragma unroll
        for (int j = 0; j < 4; ++j) {
            accum[i][j] = 0.0f;
        }
    }

    const int input_column = tid & 127;
    const int spatial_index = spatial_start + input_column;
    const bool valid_spatial = spatial_index < spatial_size;

    const int output_plane = out_height * out_width;
    const int oz = spatial_index / output_plane;
    const int remainder = spatial_index - oz * output_plane;
    const int oy = remainder / out_width;
    const int ox = remainder - oy * out_width;

    const int64_t input_volume =
        static_cast<int64_t>(depth) * height * width;
    const int64_t batch_input_offset =
        static_cast<int64_t>(batch) * channels * input_volume;

    for (int reduction_start = 0;
         reduction_start < reduction_size;
         reduction_start += TILE_K)
    {
        #pragma unroll
        for (int q = 0; q < 4; ++q) {
            const int linear = tid + q * 256;
            const int local_channel = linear / TILE_K;
            const int local_k = linear % TILE_K;
            const int output_channel = channel_start + local_channel;
            const int k = reduction_start + local_k;

            float value = 0.0f;
            if (output_channel < out_channels && k < reduction_size) {
                value = weight[
                    static_cast<int64_t>(output_channel) *
                    reduction_size + k];
            }
            shared_weight[local_k][local_channel] = value;
        }

        #pragma unroll
        for (int q = 0; q < 8; ++q) {
            const int local_k = (tid >> 7) + 2 * q;
            const int k = reduction_start + local_k;

            float value = 0.0f;
            if (valid_spatial && k < reduction_size) {
                const int input_channel = k / 27;
                const int kernel_offset = k - input_channel * 27;
                const int kz = kernel_offset / 9;
                const int ky = (kernel_offset % 9) / 3;
                const int kx = kernel_offset % 3;

                const int64_t input_index =
                    batch_input_offset +
                    static_cast<int64_t>(input_channel) * input_volume +
                    static_cast<int64_t>(oz + kz) * height * width +
                    static_cast<int64_t>(oy + ky) * width + ox + kx;

                value = input[input_index];
            }
            shared_input[local_k][input_column] = value;
        }

        __syncthreads();

        #pragma unroll
        for (int k = 0; k < TILE_K; ++k) {
            float input_values[4];

            #pragma unroll
            for (int j = 0; j < 4; ++j) {
                input_values[j] = shared_input[k][lane + 32 * j];
            }

            #pragma unroll
            for (int i = 0; i < 8; ++i) {
                const float w = shared_weight[k][warp + 8 * i];

                #pragma unroll
                for (int j = 0; j < 4; ++j) {
                    accum[i][j] =
                        fmaf(w, input_values[j], accum[i][j]);
                }
            }
        }

        __syncthreads();
    }

    #pragma unroll
    for (int i = 0; i < 8; ++i) {
        const int output_channel = channel_start + warp + 8 * i;

        if (output_channel < out_channels) {
            const float bias_value =
                bias != nullptr ? bias[output_channel] : 0.0f;

            const int64_t output_base =
                (static_cast<int64_t>(batch) * out_channels +
                 output_channel) * spatial_size;

            #pragma unroll
            for (int j = 0; j < 4; ++j) {
                const int position = spatial_start + lane + 32 * j;
                if (position < spatial_size) {
                    output[output_base + position] =
                        accum[i][j] + bias_value;
                }
            }
        }
    }
}

torch::Tensor conv3d_forward_cuda(
    torch::Tensor input,
    torch::Tensor weight,
    c10::optional<torch::Tensor> bias)
{
    TORCH_CHECK(input.is_cuda() && weight.is_cuda(),
                "Input and weight must be CUDA tensors");
    TORCH_CHECK(input.scalar_type() == at::kFloat &&
                weight.scalar_type() == at::kFloat,
                "Input and weight must be float32");
    TORCH_CHECK(input.is_contiguous() && weight.is_contiguous(),
                "Input and weight must be contiguous");
    TORCH_CHECK(input.dim() == 5 && weight.dim() == 5,
                "Expected five-dimensional tensors");
    TORCH_CHECK(input.device() == weight.device(),
                "Input and weight must be on the same device");
    TORCH_CHECK(weight.size(1) == input.size(1) &&
                weight.size(2) == 3 &&
                weight.size(3) == 3 &&
                weight.size(4) == 3,
                "Expected a dense 3x3x3 convolution");

    const c10::cuda::CUDAGuard device_guard(input.device());

    const int batch = input.size(0);
    const int channels = input.size(1);
    const int depth = input.size(2);
    const int height = input.size(3);
    const int width = input.size(4);
    const int out_channels = weight.size(0);

    TORCH_CHECK(depth >= 3 && height >= 3 && width >= 3,
                "Input dimensions must be at least three");

    const int out_depth = depth - 2;
    const int out_height = height - 2;
    const int out_width = width - 2;
    const int spatial_size = out_depth * out_height * out_width;

    auto output = torch::empty(
        {batch, out_channels, out_depth, out_height, out_width},
        input.options());

    const float* bias_ptr = nullptr;
    if (bias.has_value()) {
        const auto& b = bias.value();
        TORCH_CHECK(b.device() == input.device() &&
                    b.scalar_type() == at::kFloat &&
                    b.is_contiguous() &&
                    b.numel() == out_channels,
                    "Invalid bias tensor");
        bias_ptr = b.data_ptr<float>();
    }

    if (output.numel() == 0) {
        return output;
    }

    const dim3 grid(
        (spatial_size + TILE_N - 1) / TILE_N,
        (out_channels + TILE_M - 1) / TILE_M,
        batch);

    conv3d_3x3x3_kernel<<<
        grid, 256, 0, at::cuda::getCurrentCUDAStream()>>>(
            input.data_ptr<float>(),
            weight.data_ptr<float>(),
            bias_ptr,
            output.data_ptr<float>(),
            channels,
            out_channels,
            depth,
            height,
            width,
            out_depth,
            out_height,
            out_width,
            spatial_size);

    C10_CUDA_KERNEL_LAUNCH_CHECK();
    return output;
}
"""

_extension = None


def _get_extension():
    global _extension
    if _extension is None:
        _extension = load_inline(
            name="custom_conv3d_implicit_gemm_v1",
            cpp_sources=_cpp_source,
            cuda_sources=_cuda_source,
            functions=["conv3d_forward_cuda"],
            extra_cflags=["-O3"],
            extra_cuda_cflags=["-O3"],
            verbose=False,
        )
    return _extension


class ModelNew(nn.Module):
    def __init__(
        self,
        in_channels: int,
        out_channels: int,
        kernel_size: int,
        stride: int = 1,
        padding: int = 0,
        dilation: int = 1,
        groups: int = 1,
        bias: bool = False,
    ):
        super().__init__()
        self.conv3d = nn.Conv3d(
            in_channels,
            out_channels,
            (kernel_size, kernel_size, kernel_size),
            stride=stride,
            padding=padding,
            dilation=dilation,
            groups=groups,
            bias=bias,
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        conv = self.conv3d

        use_custom = (
            x.is_cuda
            and x.dtype == torch.float32
            and x.ndim == 5
            and x.is_contiguous()
            and conv.weight.dtype == torch.float32
            and conv.weight.is_contiguous()
            and conv.kernel_size == (3, 3, 3)
            and conv.stride == (1, 1, 1)
            and conv.padding == (0, 0, 0)
            and conv.dilation == (1, 1, 1)
            and conv.groups == 1
            and not torch.is_autocast_enabled()
        )

        if use_custom:
            return _get_extension().conv3d_forward_cuda(
                x, conv.weight, conv.bias
            )

        return F.conv3d(
            x,
            conv.weight,
            conv.bias,
            conv.stride,
            conv.padding,
            conv.dilation,
            conv.groups,
        )
```