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 matrix multiplication, max pooling, sum, and scaling.
    """
    def __init__(self, in_features, out_features, kernel_size, scale_factor):
        super(Model, self).__init__()
        self.matmul = nn.Linear(in_features, out_features)
        self.max_pool = nn.MaxPool1d(kernel_size)
        self.scale_factor = scale_factor

    def forward(self, x):
        """
        Args:
            x (torch.Tensor): Input tensor of shape (batch_size, in_features).

        Returns:
            torch.Tensor: Output tensor of shape (batch_size, out_features).
        """
        x = self.matmul(x)
        x = self.max_pool(x.unsqueeze(1)).squeeze(1)
        x = torch.sum(x, dim=1)
        x = x * self.scale_factor
        return x

batch_size = 128
in_features = 32768
out_features = 32768
kernel_size = 2
scale_factor = 0.5

def get_inputs():
    return [torch.rand(batch_size, in_features)]

def get_init_inputs():
    return [in_features, out_features, kernel_size, scale_factor]
    ```
    
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 6 of 6 for this task. Your previous solution (turn 5) was:
```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

# Allow tensor-core math for any fp32 fallback matmuls.
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
# Keep fp16 GEMM reductions in fp32 for accuracy.
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False

# ---------------------------------------------------------------------------
# Fused epilogue kernel used by the cuBLAS paths:
#   out[r] = scale * sum_j max(bias[2j]   + sum_c P[c,r,2j],
#                              bias[2j+1] + sum_c P[c,r,2j+1])
# Reads the fp16 partials once, accumulates in fp32, writes `rows` floats.
# ---------------------------------------------------------------------------
_fused_cuda_source = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAException.h>
#include <cuda_runtime.h>
#include <cuda_fp16.h>

#define THREADS 512

__device__ __forceinline__ float blockReduceSumF(float val) {
    __shared__ float shared[32];
    const int lane = threadIdx.x & 31;
    const int wid  = threadIdx.x >> 5;
    #pragma unroll
    for (int offset = 16; offset > 0; offset >>= 1)
        val += __shfl_down_sync(0xffffffffu, val, offset);
    if (lane == 0) shared[wid] = val;
    __syncthreads();
    const int nwarp = (blockDim.x + 31) >> 5;
    if (wid == 0) {
        val = (lane < nwarp) ? shared[lane] : 0.0f;
        #pragma unroll
        for (int offset = 16; offset > 0; offset >>= 1)
            val += __shfl_down_sync(0xffffffffu, val, offset);
    }
    return val;  // only thread 0 holds the block total
}

// P: (CHUNKS, rows, N) fp16 partial sums of the K-split GEMM.
// One float4 load of P covers 8 halfs = 4 pool pairs. Requires N % 8 == 0.
template<int CHUNKS>
__global__ void chunked_partial_maxpool2_sum_scale_kernel(
    const __half* __restrict__ P, const float* __restrict__ bias,
    float* __restrict__ out, const int rows, const long long N,
    const float scale)
{
    const int r = blockIdx.x;
    const long long row_off = (long long)r * N;
    const long long cstride = (long long)rows * N;
    const long long nv = N >> 3;  // groups of 8 halfs
    const float4* __restrict__ b4 = reinterpret_cast<const float4*>(bias);

    float acc = 0.0f;
    for (long long i = threadIdx.x; i < nv; i += blockDim.x) {
        const float4 b0 = b4[2 * i];
        const float4 b1 = b4[2 * i + 1];
        float v0 = b0.x, v1 = b0.y, v2 = b0.z, v3 = b0.w;
        float v4 = b1.x, v5 = b1.y, v6 = b1.z, v7 = b1.w;
        #pragma unroll
        for (int c = 0; c < CHUNKS; ++c) {
            const float4 raw = *reinterpret_cast<const float4*>(
                P + (long long)c * cstride + row_off + (i << 3));
            const __half2* h = reinterpret_cast<const __half2*>(&raw);
            const float2 a = __half22float2(h[0]);
            const float2 bb = __half22float2(h[1]);
            const float2 d = __half22float2(h[2]);
            const float2 e = __half22float2(h[3]);
            v0 += a.x; v1 += a.y; v2 += bb.x; v3 += bb.y;
            v4 += d.x; v5 += d.y; v6 += e.x; v7 += e.y;
        }
        acc += fmaxf(v0, v1) + fmaxf(v2, v3)
             + fmaxf(v4, v5) + fmaxf(v6, v7);
    }
    acc = blockReduceSumF(acc);
    if (threadIdx.x == 0) out[r] = acc * scale;
}

torch::Tensor chunked_partial_maxpool2_sum_scale(torch::Tensor P,
                                                 torch::Tensor bias,
                                                 double scale) {
    TORCH_CHECK(P.is_cuda() && P.dim() == 3, "P must be a 3D CUDA tensor");
    TORCH_CHECK(P.scalar_type() == torch::kFloat16, "P must be fp16");
    TORCH_CHECK(bias.is_cuda() && bias.dim() == 1, "bias must be 1D CUDA");
    TORCH_CHECK(bias.scalar_type() == torch::kFloat32, "bias must be fp32");
    auto Pc = P.contiguous();
    auto bc = bias.contiguous();
    const int chunks = Pc.size(0);
    const int rows   = Pc.size(1);
    const long long N = Pc.size(2);
    TORCH_CHECK(bc.size(0) == N, "bias size mismatch");
    TORCH_CHECK(N % 8 == 0, "N must be a multiple of 8");

    auto out = torch::empty({rows}, bc.options());
    if (rows == 0) return out;
    if (N == 0) { out.zero_(); return out; }

    const dim3 grid(rows);
    const dim3 block(THREADS);
    const float s = (float)scale;
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    const __half* p = reinterpret_cast<const __half*>(Pc.data_ptr<at::Half>());
    const float* bi = bc.data_ptr<float>();
    float* o = out.data_ptr<float>();

    switch (chunks) {
        case 1:
            chunked_partial_maxpool2_sum_scale_kernel<1>
                <<<grid, block, 0, stream>>>(p, bi, o, rows, N, s); break;
        case 2:
            chunked_partial_maxpool2_sum_scale_kernel<2>
                <<<grid, block, 0, stream>>>(p, bi, o, rows, N, s); break;
        case 4:
            chunked_partial_maxpool2_sum_scale_kernel<4>
                <<<grid, block, 0, stream>>>(p, bi, o, rows, N, s); break;
        case 8:
            chunked_partial_maxpool2_sum_scale_kernel<8>
                <<<grid, block, 0, stream>>>(p, bi, o, rows, N, s); break;
        default:
            TORCH_CHECK(false, "unsupported chunk count");
    }
    C10_CUDA_KERNEL_LAUNCH_CHECK();
    return out;
}
"""

_fused_cpp_source = (
    "torch::Tensor chunked_partial_maxpool2_sum_scale(torch::Tensor P, torch::Tensor bias, double scale);"
)

_fused_op = load_inline(
    name="chunked_partial_maxpool_sum_scale_v2",
    cpp_sources=_fused_cpp_source,
    cuda_sources=_fused_cuda_source,
    functions=["chunked_partial_maxpool2_sum_scale"],
    verbose=False,
    extra_cuda_cflags=["-O3"],
)

# ---------------------------------------------------------------------------
# Fully fused Triton GEMM + bias + maxpool(k=2) + sum + scale.
# The GEMM is weight-bandwidth-bound (the fp16 weight is ~2.1 GB); fusing the
# epilogue avoids materializing the (128 x 32768) output entirely -- each
# program reduces its tile to one atomic add per row.
# ---------------------------------------------------------------------------
try:
    import triton
    import triton.language as tl
    _HAS_TRITON = True
except Exception:
    _HAS_TRITON = False

if _HAS_TRITON:
    @triton.jit
    def _gemm_mp2_sum_kernel(
        x_ptr, w_ptr, b_ptr, out_ptr,
        M, N, K,
        sxm, sxk, swk, swn,
        scale,
        BM: tl.constexpr, BN: tl.constexpr, BK: tl.constexpr,
        EVEN_M: tl.constexpr, EVEN_N: tl.constexpr,
    ):
        pid = tl.program_id(0)
        offs_n = pid * BN + tl.arange(0, BN)
        offs_m = tl.arange(0, BM)
        offs_k = tl.arange(0, BK)
        a_ptrs = x_ptr + offs_m[:, None] * sxm + offs_k[None, :] * sxk
        b_ptrs = w_ptr + offs_k[:, None] * swk + offs_n[None, :] * swn
        acc = tl.zeros((BM, BN), dtype=tl.float32)
        m_mask = offs_m < M
        n_mask = offs_n < N
        for _k in range(0, K, BK):
            if EVEN_M:
                a = tl.load(a_ptrs)
            else:
                a = tl.load(a_ptrs, mask=m_mask[:, None], other=0.0)
            if EVEN_N:
                b = tl.load(b_ptrs)
            else:
                b = tl.load(b_ptrs, mask=n_mask[None, :], other=0.0)
            acc = tl.dot(a, b, acc)  # fp16 x fp16 -> fp32
            a_ptrs += BK * sxk
            b_ptrs += BK * swk
        if EVEN_N:
            bias = tl.load(b_ptr + offs_n)
        else:
            bias = tl.load(b_ptr + offs_n, mask=n_mask, other=0.0)
        acc = acc + bias[None, :]
        acc3 = tl.reshape(acc, (BM, BN // 2, 2))
        mx = tl.max(acc3, axis=2)          # max-pool over adjacent pairs
        part = tl.sum(mx, axis=1) * scale  # sum over features, then scale
        if EVEN_M:
            tl.atomic_add(out_ptr + offs_m, part)
        else:
            tl.atomic_add(out_ptr + offs_m, part, mask=m_mask)

# (BLOCK_N, BLOCK_K, num_warps, num_stages); BLOCK_M covers the whole batch.
_TRITON_CFGS = [
    (64, 64, 4, 4),
    (64, 64, 8, 4),
    (64, 32, 4, 5),
    (64, 128, 8, 3),
    (128, 64, 8, 4),
    (128, 32, 8, 4),
]


def _run_cublas(xh, wt, bias, scale, chunks):
    bs = xh.size(0)
    K, N = wt.shape
    if chunks == 1:
        part = torch.mm(xh, wt).unsqueeze(0)
    else:
        k = K // chunks
        # (C, bs, k) strided view; bmm passes the leading dim straight to
        # cuBLAS strided-batched GEMM -- no copies, 4x+ more thread blocks.
        xb = xh.view(bs, chunks, k).transpose(0, 1)
        wb = wt.view(chunks, k, N)
        part = torch.bmm(xb, wb)
    return _fused_op.chunked_partial_maxpool2_sum_scale(part, bias, float(scale))


def _run_linear(xh, w_h, bias, scale):
    part = F.linear(xh, w_h).unsqueeze(0)
    return _fused_op.chunked_partial_maxpool2_sum_scale(part, bias, float(scale))


def _run_triton(xh, wt, bias, scale, cfg):
    M = xh.size(0)
    K, N = wt.shape
    BN, BK, warps, stages = cfg
    BM = max(16, triton.next_power_of_2(M))
    out = torch.zeros(M, device=xh.device, dtype=torch.float32)
    grid = ((N + BN - 1) // BN,)
    _gemm_mp2_sum_kernel[grid](
        xh, wt, bias, out, M, N, K,
        xh.stride(0), xh.stride(1), wt.stride(0), wt.stride(1),
        float(scale),
        BM=BM, BN=BN, BK=BK,
        EVEN_M=(BM == M), EVEN_N=(N % BN == 0),
        num_warps=warps, num_stages=stages,
    )
    return out


def _bench(fn, warmup=2, iters=5):
    for _ in range(warmup):
        fn()
    torch.cuda.synchronize()
    s = torch.cuda.Event(enable_timing=True)
    e = torch.cuda.Event(enable_timing=True)
    s.record()
    for _ in range(iters):
        fn()
    e.record()
    torch.cuda.synchronize()
    return s.elapsed_time(e) / iters


# Global plan cache so repeated ModelNew instances with the same shapes skip
# re-tuning. Key: (device, batch, out_features, in_features).
_PLAN_CACHE = {}


def _select_plan(owner, xh, bias):
    M = xh.size(0)
    K, N = owner._wT_half.shape
    key = (xh.device.index, M, N, K)
    plan = _PLAN_CACHE.get(key)
    if plan is not None:
        return plan

    wt = owner._wT_half
    scale = float(owner.scale_factor)
    cands = []
    for c in (1, 2, 4, 8):
        if K % c == 0:
            cands.append(("cublas", c))
    cands.append(("linear", None))
    if _HAS_TRITON and M <= 128 and N % 128 == 0:
        for cfg in _TRITON_CFGS:
            if K % cfg[1] == 0:
                cands.append(("triton", cfg))

    ref = None
    best_plan = None
    best_t = float("inf")
    for kind, cfg in cands:
        try:
            if kind == "cublas":
                fn = lambda c=cfg: _run_cublas(xh, wt, bias, scale, c)
            elif kind == "linear":
                fn = lambda: _run_linear(xh, owner._get_wh(), bias, scale)
            else:
                fn = lambda c=cfg: _run_triton(xh, wt, bias, scale, c)
            out = fn()
            torch.cuda.synchronize()
            if ref is None:
                ref = out
            elif kind == "triton":
                # Sanity-check the fused kernel against the cuBLAS path.
                if not torch.allclose(out, ref, rtol=1e-3, atol=0.5):
                    continue
            t = _bench(fn)
            if t < best_t:
                best_t = t
                best_plan = (kind, cfg)
        except Exception:
            continue
    if best_plan is None:
        best_plan = ("cublas", 1 if K % 1 == 0 else None)
    _PLAN_CACHE[key] = best_plan
    return best_plan


class ModelNew(nn.Module):
    """
    Optimized model:
      - The linear layer runs in fp16 on tensor cores. The workload is bound by
        streaming the ~2.1 GB fp16 weight from HBM, so several execution plans
        are benchmarked once (cached globally per shape) and the fastest wins:
          * K-split strided-batched cuBLAS GEMM (chunk counts 1/2/4/8) whose
            partials feed a fused custom CUDA kernel doing partial-reduction +
            bias-add + max-pool(k=2) + feature-sum + scale with fp32 accum.
          * single TN cuBLAS GEMM via F.linear + the same fused epilogue.
          * a fully fused Triton GEMM (fp32 accumulation over the full K) with
            the bias/maxpool/sum/scale epilogue in-kernel -- the GEMM output
            is never materialized, only one atomic add per row per tile.
      - fp16 weight copies (both layouts) are built once and cached, keyed on
        the parameter version.
      - Non-CUDA / unsupported configurations fall back to the original ops.
    """
    def __init__(self, in_features, out_features, kernel_size, scale_factor):
        super(ModelNew, self).__init__()
        self.matmul = nn.Linear(in_features, out_features)
        self.kernel_size = kernel_size
        self.scale_factor = scale_factor
        self._wT_half = None
        self._w_half = None
        self._w_ver = -1

    def _refresh_cache(self, w):
        ver = w._version
        if (self._wT_half is None or self._w_ver != ver
                or self._wT_half.device != w.device):
            # (in_f, out_f) fp16 contiguous (NN layout) and (out_f, in_f) fp16
            # (TN layout, just a cast of the original weight).
            self._wT_half = w.detach().half().t().contiguous()
            self._w_half = None
            self._w_ver = ver

    def _get_wh(self):
        w = self.matmul.weight
        if self._w_half is None:
            self._w_half = w.detach().half().contiguous()
        return self._w_half

    def forward(self, x):
        w = self.matmul.weight
        b = self.matmul.bias
        n = w.size(0)
        if (x.is_cuda and x.dtype == torch.float32 and b is not None
                and self.kernel_size == 2 and n % 8 == 0):
            try:
                self._refresh_cache(w)
                xh = x.half()
                if not xh.is_contiguous():
                    xh = xh.contiguous()
                bias = b.detach()
                kind, cfg = _select_plan(self, xh, bias)
                scale = float(self.scale_factor)
                if kind == "cublas":
                    return _run_cublas(xh, self._wT_half, bias, scale, cfg)
                if kind == "linear":
                    return _run_linear(xh, self._get_wh(), bias, scale)
                if kind == "triton":
                    return _run_triton(xh, self._wT_half, bias.contiguous(), scale, cfg)
            except Exception:
                pass
        # Fallback (CPU / other dtypes / unsupported shapes): original ops.
        y = F.linear(x, w, b)
        y = F.max_pool1d(y.unsqueeze(1), self.kernel_size).squeeze(1)
        y = torch.sum(y, dim=1)
        return y * self.scale_factor
```
Result of evaluating it:
The solution failed to compile. Error output:
```
ValueError: @jit functions should be defined in a Python file

```

The fastest correct solution so far (1.988x speedup) is:
```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

# Allow tensor-core math for any fp32 fallback matmuls.
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
# Keep fp16 GEMM reductions in fp32 for accuracy.
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False

# The workload is dominated by a (128 x 32768) @ (32768 x 32768) GEMM whose fp16
# weight matrix is ~2.1 GB, i.e. it is HBM-bandwidth-bound. With batch=128 a
# single GEMM launch creates only ~256 thread blocks (128x128 tiles), barely two
# waves on a 108-SM A100, so the tail leaves the memory system idle. We instead
# split the K dimension into 4 chunks issued as ONE strided-batched GEMM
# (torch.bmm -> cuBLAS), giving 4x the thread blocks and much better SM/HBM
# utilization. cuBLAS accumulates each chunk in fp32 on tensor cores. A fused
# custom kernel then reduces the chunk partials, adds the bias, applies
# max-pool(k=2), sums over features and scales -- reading the fp16 partials once
# and writing only `batch` floats, with fp32 accumulation.
_fused_cuda_source = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAException.h>
#include <cuda_runtime.h>
#include <cuda_fp16.h>

#define THREADS 512

__device__ __forceinline__ float blockReduceSumF(float val) {
    __shared__ float shared[32];
    const int lane = threadIdx.x & 31;
    const int wid  = threadIdx.x >> 5;
    #pragma unroll
    for (int offset = 16; offset > 0; offset >>= 1)
        val += __shfl_down_sync(0xffffffffu, val, offset);
    if (lane == 0) shared[wid] = val;
    __syncthreads();
    const int nwarp = (blockDim.x + 31) >> 5;
    if (wid == 0) {
        val = (lane < nwarp) ? shared[lane] : 0.0f;
        #pragma unroll
        for (int offset = 16; offset > 0; offset >>= 1)
            val += __shfl_down_sync(0xffffffffu, val, offset);
    }
    return val;  // only thread 0 holds the block total
}

// P: (CHUNKS, rows, N) fp16 partial sums of the K-split GEMM.
// bias: (N) fp32.
// out[r] = scale * sum_j max(bias[2j]   + sum_c P[c,r,2j],
//                            bias[2j+1] + sum_c P[c,r,2j+1])
// One float4 load of P covers 8 halfs = 4 pool pairs. Requires N % 8 == 0.
template<int CHUNKS>
__global__ void chunked_partial_maxpool2_sum_scale_kernel(
    const __half* __restrict__ P, const float* __restrict__ bias,
    float* __restrict__ out, const int rows, const long long N,
    const float scale)
{
    const int r = blockIdx.x;
    const long long row_off = (long long)r * N;
    const long long cstride = (long long)rows * N;
    const long long nv = N >> 3;  // groups of 8 halfs
    const float4* __restrict__ b4 = reinterpret_cast<const float4*>(bias);

    float acc = 0.0f;
    for (long long i = threadIdx.x; i < nv; i += blockDim.x) {
        const float4 b0 = b4[2 * i];
        const float4 b1 = b4[2 * i + 1];
        float v0 = b0.x, v1 = b0.y, v2 = b0.z, v3 = b0.w;
        float v4 = b1.x, v5 = b1.y, v6 = b1.z, v7 = b1.w;
        #pragma unroll
        for (int c = 0; c < CHUNKS; ++c) {
            const float4 raw = *reinterpret_cast<const float4*>(
                P + (long long)c * cstride + row_off + (i << 3));
            const __half2* h = reinterpret_cast<const __half2*>(&raw);
            const float2 a = __half22float2(h[0]);
            const float2 b = __half22float2(h[1]);
            const float2 d = __half22float2(h[2]);
            const float2 e = __half22float2(h[3]);
            v0 += a.x; v1 += a.y; v2 += b.x; v3 += b.y;
            v4 += d.x; v5 += d.y; v6 += e.x; v7 += e.y;
        }
        acc += fmaxf(v0, v1) + fmaxf(v2, v3)
             + fmaxf(v4, v5) + fmaxf(v6, v7);
    }
    acc = blockReduceSumF(acc);
    if (threadIdx.x == 0) out[r] = acc * scale;
}

torch::Tensor chunked_partial_maxpool2_sum_scale(torch::Tensor P,
                                                 torch::Tensor bias,
                                                 double scale) {
    TORCH_CHECK(P.is_cuda() && P.dim() == 3, "P must be a 3D CUDA tensor");
    TORCH_CHECK(P.scalar_type() == torch::kFloat16, "P must be fp16");
    TORCH_CHECK(bias.is_cuda() && bias.dim() == 1, "bias must be 1D CUDA");
    TORCH_CHECK(bias.scalar_type() == torch::kFloat32, "bias must be fp32");
    auto Pc = P.contiguous();
    auto bc = bias.contiguous();
    const int chunks = Pc.size(0);
    const int rows   = Pc.size(1);
    const long long N = Pc.size(2);
    TORCH_CHECK(bc.size(0) == N, "bias size mismatch");
    TORCH_CHECK(N % 8 == 0, "N must be a multiple of 8");

    auto out = torch::empty({rows}, bc.options());
    if (rows == 0) return out;
    if (N == 0) { out.zero_(); return out; }

    const dim3 grid(rows);
    const dim3 block(THREADS);
    const float s = (float)scale;
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();
    const __half* p = reinterpret_cast<const __half*>(Pc.data_ptr<at::Half>());
    const float* bi = bc.data_ptr<float>();
    float* o = out.data_ptr<float>();

    switch (chunks) {
        case 1:
            chunked_partial_maxpool2_sum_scale_kernel<1>
                <<<grid, block, 0, stream>>>(p, bi, o, rows, N, s); break;
        case 2:
            chunked_partial_maxpool2_sum_scale_kernel<2>
                <<<grid, block, 0, stream>>>(p, bi, o, rows, N, s); break;
        case 4:
            chunked_partial_maxpool2_sum_scale_kernel<4>
                <<<grid, block, 0, stream>>>(p, bi, o, rows, N, s); break;
        case 8:
            chunked_partial_maxpool2_sum_scale_kernel<8>
                <<<grid, block, 0, stream>>>(p, bi, o, rows, N, s); break;
        default:
            TORCH_CHECK(false, "unsupported chunk count");
    }
    C10_CUDA_KERNEL_LAUNCH_CHECK();
    return out;
}
"""

_fused_cpp_source = (
    "torch::Tensor chunked_partial_maxpool2_sum_scale(torch::Tensor P, torch::Tensor bias, double scale);"
)

_fused_op = load_inline(
    name="chunked_partial_maxpool_sum_scale_v1",
    cpp_sources=_fused_cpp_source,
    cuda_sources=_fused_cuda_source,
    functions=["chunked_partial_maxpool2_sum_scale"],
    verbose=False,
    extra_cuda_cflags=["-O3"],
)

_CHUNKS = 4


class ModelNew(nn.Module):
    """
    Optimized model:
      - The linear layer runs in fp16 on tensor cores. The K (in_features) dim is
        split into 4 chunks issued as a single strided-batched GEMM (torch.bmm),
        which exposes ~4x more thread blocks than one K=32768 GEMM at batch=128
        and keeps the HBM saturated (the kernel is weight-bandwidth-bound).
        The transposed fp16 weight (chunk-major layout) is built once and cached.
      - Chunk-partial reduction + bias add + max-pool(k=2) + feature sum + scale
        are fused into one custom CUDA kernel with fp32 accumulation that reads
        the fp16 partials once and writes only `batch` floats.
    """
    def __init__(self, in_features, out_features, kernel_size, scale_factor):
        super(ModelNew, self).__init__()
        self.matmul = nn.Linear(in_features, out_features)
        self.kernel_size = kernel_size
        self.scale_factor = scale_factor
        self._wT_half = None
        self._wT_ver = -1

    def forward(self, x):
        w = self.matmul.weight
        b = self.matmul.bias
        n = w.size(0)
        in_f = w.size(1)
        if (x.is_cuda and x.dtype == torch.float32 and b is not None
                and self.kernel_size == 2
                and in_f % _CHUNKS == 0 and n % 8 == 0):
            ver = w._version
            if (self._wT_half is None or self._wT_ver != ver
                    or self._wT_half.device != w.device):
                # (in_f, n) fp16, contiguous; viewing as (C, in_f/C, n) yields
                # per-chunk weight^T slices with zero runtime cost.
                self._wT_half = w.detach().half().t().contiguous()
                self._wT_ver = ver
            bs = x.size(0)
            xh = x.half()
            if not xh.is_contiguous():
                xh = xh.contiguous()
            k = in_f // _CHUNKS
            # (C, bs, k) strided view (batch stride k, row stride in_f) -- bmm
            # passes the leading dimension straight to cuBLAS, no copy needed.
            xb = xh.view(bs, _CHUNKS, k).transpose(0, 1)
            wb = self._wT_half.view(_CHUNKS, k, n)
            partials = torch.bmm(xb, wb)  # (C, bs, n) fp16, fp32 accumulation
            return _fused_op.chunked_partial_maxpool2_sum_scale(
                partials, b.detach(), float(self.scale_factor)
            )
        # Fallback (CPU / other dtypes / unsupported shapes): original ops.
        y = F.linear(x, w, b)
        y = F.max_pool1d(y.unsqueeze(1), self.kernel_size).squeeze(1)
        y = torch.sum(y, dim=1)
        return y * self.scale_factor
```

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.
