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):
    """
    A model that computes Triplet Margin Loss for metric learning tasks.

    Parameters:
        margin (float): The margin between the positive and negative samples.
    """
    def __init__(self, margin=1.0):
        super(Model, self).__init__()
        self.loss_fn = torch.nn.TripletMarginLoss(margin=margin)

    def forward(self, anchor, positive, negative):
        return self.loss_fn(anchor, positive, negative)

batch_size = 32768
input_shape = (8192,)
dim = 1

def get_inputs():
    scale = torch.rand(())
    return [torch.rand(batch_size, *input_shape)*scale, torch.rand(batch_size, *input_shape), torch.rand(batch_size, *input_shape)]
    
def get_init_inputs():
    return [1.0]  # Default margin

    ```
    
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 4 of 6 for this task. Your previous solution (turn 3) 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

# Fused Triplet Margin Loss in a single kernel:
#   loss = mean( max( ||a - p + eps||_2 - ||a - n + eps||_2 + margin, 0 ) )
#
# Improvements over v2:
#  * Streaming loads via __ldcs (evict-first cache hint): the three input
#    tensors are read exactly once, so keeping them out of the L2 LRU improves
#    effective DRAM throughput for this purely bandwidth-bound kernel.
#  * The 16-byte reduction workspace is persistent and self-resetting: the
#    "last block done" that computes the final mean also re-zeroes the
#    accumulator/counter, so the per-call cudaMemsetAsync launch is gone.
#  * Everything else (single launch, x4 manual unroll with 12 independent
#    16-byte loads in flight per thread, double-precision global accumulation)
#    is kept from the already-validated v2.
triplet_margin_loss_cuda_src = r"""
#include <torch/extension.h>
#include <ATen/cuda/CUDAContext.h>
#include <c10/cuda/CUDAGuard.h>
#include <cuda_runtime.h>
#include <cstdint>
#include <mutex>

#define BLOCK_SIZE 256
#define NWARPS (BLOCK_SIZE / 32)

__device__ __forceinline__ float warp_reduce_sum(float v) {
#pragma unroll
    for (int off = 16; off > 0; off >>= 1) {
        v += __shfl_down_sync(0xffffffffu, v, off);
    }
    return v;
}

__device__ __forceinline__ void acc4(const float4 a, const float4 b,
                                     const float eps, float& s) {
    const float d0 = a.x - b.x + eps;
    const float d1 = a.y - b.y + eps;
    const float d2 = a.z - b.z + eps;
    const float d3 = a.w - b.w + eps;
    s += (d0 * d0 + d1 * d1) + (d2 * d2 + d3 * d3);
}

// Block-wide reduction of the two squared-distance sums, hinge application,
// global accumulation, "last block" mean finalize, and workspace reset.
__device__ __forceinline__ void block_hinge_finalize(
        float sp, float sn, const float margin,
        double* __restrict__ accum, unsigned int* __restrict__ counter,
        float* __restrict__ out, const double inv_n) {
    __shared__ float sh_sp[NWARPS];
    __shared__ float sh_sn[NWARPS];
    const int lane = threadIdx.x & 31;
    const int warp = threadIdx.x >> 5;

    sp = warp_reduce_sum(sp);
    sn = warp_reduce_sum(sn);
    if (lane == 0) {
        sh_sp[warp] = sp;
        sh_sn[warp] = sn;
    }
    __syncthreads();

    if (warp == 0) {
        sp = (lane < NWARPS) ? sh_sp[lane] : 0.0f;
        sn = (lane < NWARPS) ? sh_sn[lane] : 0.0f;
        sp = warp_reduce_sum(sp);
        sn = warp_reduce_sum(sn);
        if (lane == 0) {
            const float loss = fmaxf(sqrtf(sp) - sqrtf(sn) + margin, 0.0f);
            if (loss != 0.0f) {
                atomicAdd(accum, (double)loss);
            }
            __threadfence();
            if (atomicAdd(counter, 1u) == gridDim.x - 1u) {
                // Last block: every other block's contribution is visible.
                const double total = atomicAdd(accum, 0.0);
                out[0] = (float)(total * inv_n);
                // Re-zero the persistent workspace so the next launch needs
                // no explicit memset. We are the only remaining writer.
                *accum = 0.0;
                *counter = 0u;
                __threadfence();
            }
        }
    }
}

__global__ void __launch_bounds__(BLOCK_SIZE)
triplet_margin_loss_vec4_kernel(
        const float4* __restrict__ anchor,
        const float4* __restrict__ positive,
        const float4* __restrict__ negative,
        double* __restrict__ accum,
        unsigned int* __restrict__ counter,
        float* __restrict__ out,
        const double inv_n,
        const int n_vec,
        const float margin,
        const float eps) {
    const float4* a = anchor + (size_t)blockIdx.x * n_vec;
    const float4* p = positive + (size_t)blockIdx.x * n_vec;
    const float4* n = negative + (size_t)blockIdx.x * n_vec;

    float sp0 = 0.f, sp1 = 0.f, sp2 = 0.f, sp3 = 0.f;
    float sn0 = 0.f, sn1 = 0.f, sn2 = 0.f, sn3 = 0.f;

    int i = threadIdx.x;
    // Unrolled x4 main loop: 12 independent 16-byte streaming loads in flight.
    for (; i + 3 * BLOCK_SIZE < n_vec; i += 4 * BLOCK_SIZE) {
        const float4 av0 = __ldcs(a + i);
        const float4 av1 = __ldcs(a + i + BLOCK_SIZE);
        const float4 av2 = __ldcs(a + i + 2 * BLOCK_SIZE);
        const float4 av3 = __ldcs(a + i + 3 * BLOCK_SIZE);
        const float4 pv0 = __ldcs(p + i);
        const float4 pv1 = __ldcs(p + i + BLOCK_SIZE);
        const float4 pv2 = __ldcs(p + i + 2 * BLOCK_SIZE);
        const float4 pv3 = __ldcs(p + i + 3 * BLOCK_SIZE);
        const float4 nv0 = __ldcs(n + i);
        const float4 nv1 = __ldcs(n + i + BLOCK_SIZE);
        const float4 nv2 = __ldcs(n + i + 2 * BLOCK_SIZE);
        const float4 nv3 = __ldcs(n + i + 3 * BLOCK_SIZE);
        acc4(av0, pv0, eps, sp0); acc4(av0, nv0, eps, sn0);
        acc4(av1, pv1, eps, sp1); acc4(av1, nv1, eps, sn1);
        acc4(av2, pv2, eps, sp2); acc4(av2, nv2, eps, sn2);
        acc4(av3, pv3, eps, sp3); acc4(av3, nv3, eps, sn3);
    }
    for (; i < n_vec; i += BLOCK_SIZE) {
        const float4 av = __ldcs(a + i);
        const float4 pv = __ldcs(p + i);
        const float4 nv = __ldcs(n + i);
        acc4(av, pv, eps, sp0); acc4(av, nv, eps, sn0);
    }

    const float sp = (sp0 + sp1) + (sp2 + sp3);
    const float sn = (sn0 + sn1) + (sn2 + sn3);
    block_hinge_finalize(sp, sn, margin, accum, counter, out, inv_n);
}

__global__ void __launch_bounds__(BLOCK_SIZE)
triplet_margin_loss_scalar_kernel(
        const float* __restrict__ anchor,
        const float* __restrict__ positive,
        const float* __restrict__ negative,
        double* __restrict__ accum,
        unsigned int* __restrict__ counter,
        float* __restrict__ out,
        const double inv_n,
        const int dim_size,
        const float margin,
        const float eps) {
    const size_t base = (size_t)blockIdx.x * dim_size;
    const float* a = anchor + base;
    const float* p = positive + base;
    const float* n = negative + base;

    float sp0 = 0.f, sp1 = 0.f, sp2 = 0.f, sp3 = 0.f;
    float sn0 = 0.f, sn1 = 0.f, sn2 = 0.f, sn3 = 0.f;

    int i = threadIdx.x;
    for (; i + 3 * BLOCK_SIZE < dim_size; i += 4 * BLOCK_SIZE) {
        const float a0 = __ldcs(a + i), a1 = __ldcs(a + i + BLOCK_SIZE),
                    a2 = __ldcs(a + i + 2 * BLOCK_SIZE), a3 = __ldcs(a + i + 3 * BLOCK_SIZE);
        const float p0 = __ldcs(p + i), p1 = __ldcs(p + i + BLOCK_SIZE),
                    p2 = __ldcs(p + i + 2 * BLOCK_SIZE), p3 = __ldcs(p + i + 3 * BLOCK_SIZE);
        const float n0 = __ldcs(n + i), n1 = __ldcs(n + i + BLOCK_SIZE),
                    n2 = __ldcs(n + i + 2 * BLOCK_SIZE), n3 = __ldcs(n + i + 3 * BLOCK_SIZE);
        float d;
        d = a0 - p0 + eps; sp0 += d * d; d = a0 - n0 + eps; sn0 += d * d;
        d = a1 - p1 + eps; sp1 += d * d; d = a1 - n1 + eps; sn1 += d * d;
        d = a2 - p2 + eps; sp2 += d * d; d = a2 - n2 + eps; sn2 += d * d;
        d = a3 - p3 + eps; sp3 += d * d; d = a3 - n3 + eps; sn3 += d * d;
    }
    for (; i < dim_size; i += BLOCK_SIZE) {
        const float dp = __ldcs(a + i) - __ldcs(p + i) + eps;
        const float dn = __ldcs(a + i) - __ldcs(n + i) + eps;
        sp0 += dp * dp;
        sn0 += dn * dn;
    }

    const float sp = (sp0 + sp1) + (sp2 + sp3);
    const float sn = (sn0 + sn1) + (sn2 + sn3);
    block_hinge_finalize(sp, sn, margin, accum, counter, out, inv_n);
}

torch::Tensor triplet_margin_loss_cuda(
        torch::Tensor anchor,
        torch::Tensor positive,
        torch::Tensor negative,
        double margin) {
    TORCH_CHECK(anchor.is_cuda() && positive.is_cuda() && negative.is_cuda(),
                "All inputs must be CUDA tensors.");
    TORCH_CHECK(anchor.scalar_type() == at::kFloat &&
                positive.scalar_type() == at::kFloat &&
                negative.scalar_type() == at::kFloat,
                "All inputs must be float32.");
    TORCH_CHECK(anchor.dim() == 2, "Inputs must be 2-D.");

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

    auto a = anchor.contiguous();
    auto p = positive.contiguous();
    auto n = negative.contiguous();

    const int64_t batch = a.size(0);
    const int64_t dim = a.size(1);
    TORCH_CHECK(p.size(0) == batch && p.size(1) == dim, "positive shape mismatch.");
    TORCH_CHECK(n.size(0) == batch && n.size(1) == dim, "negative shape mismatch.");
    TORCH_CHECK(batch > 0 && dim > 0, "Empty inputs are not supported by the fused kernel.");

    auto out = torch::empty({}, a.options());

    // Persistent 16-byte scratch per device: bytes [0,8) hold the double sum
    // accumulator, bytes [8,12) hold the block-completion counter. The kernel
    // leaves both zeroed when it finishes, so zero-init happens only once.
    // (Deliberately leaked to avoid teardown-order issues at process exit.)
    static torch::Tensor* g_ws = new torch::Tensor[64];
    static std::mutex g_ws_mutex;
    const int dev = static_cast<int>(a.get_device());
    TORCH_CHECK(dev >= 0 && dev < 64, "Unsupported device index.");
    {
        std::lock_guard<std::mutex> lock(g_ws_mutex);
        if (!g_ws[dev].defined()) {
            g_ws[dev] = torch::zeros({16}, a.options().dtype(at::kByte));
        }
    }
    void* ws_ptr = g_ws[dev].data_ptr();
    double* accum = reinterpret_cast<double*>(ws_ptr);
    unsigned int* counter = reinterpret_cast<unsigned int*>(
        static_cast<unsigned char*>(ws_ptr) + 8);

    cudaStream_t stream = at::cuda::getCurrentCUDAStream();

    const float eps = 1e-6f;
    const float m = static_cast<float>(margin);
    const double inv_n = 1.0 / static_cast<double>(batch);
    const dim3 grid(static_cast<unsigned int>(batch));

    const bool aligned =
        ((reinterpret_cast<uintptr_t>(a.data_ptr<float>()) & 0xF) == 0) &&
        ((reinterpret_cast<uintptr_t>(p.data_ptr<float>()) & 0xF) == 0) &&
        ((reinterpret_cast<uintptr_t>(n.data_ptr<float>()) & 0xF) == 0);

    if (((dim & 3) == 0) && aligned) {
        triplet_margin_loss_vec4_kernel<<<grid, BLOCK_SIZE, 0, stream>>>(
            reinterpret_cast<const float4*>(a.data_ptr<float>()),
            reinterpret_cast<const float4*>(p.data_ptr<float>()),
            reinterpret_cast<const float4*>(n.data_ptr<float>()),
            accum, counter, out.data_ptr<float>(), inv_n,
            static_cast<int>(dim >> 2), m, eps);
    } else {
        triplet_margin_loss_scalar_kernel<<<grid, BLOCK_SIZE, 0, stream>>>(
            a.data_ptr<float>(), p.data_ptr<float>(), n.data_ptr<float>(),
            accum, counter, out.data_ptr<float>(), inv_n,
            static_cast<int>(dim), m, eps);
    }
    return out;
}
"""

triplet_margin_loss_cpp_src = (
    "torch::Tensor triplet_margin_loss_cuda(torch::Tensor anchor, torch::Tensor positive, "
    "torch::Tensor negative, double margin);"
)

# Compile the inline CUDA extension
triplet_margin_loss_op = load_inline(
    name="triplet_margin_loss_fused_v3",
    cpp_sources=triplet_margin_loss_cpp_src,
    cuda_sources=triplet_margin_loss_cuda_src,
    functions=["triplet_margin_loss_cuda"],
    verbose=False,
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
)


class ModelNew(nn.Module):
    """
    Optimized Triplet Margin Loss using a single fused CUDA kernel.

    Parameters:
        margin (float): The margin between the positive and negative samples.
    """
    def __init__(self, margin=1.0):
        super(ModelNew, self).__init__()
        self.margin = float(margin)
        self._fn = triplet_margin_loss_op.triplet_margin_loss_cuda

    def forward(self, anchor, positive, negative):
        if (anchor.is_cuda and anchor.dtype == torch.float32 and anchor.dim() == 2
                and anchor.shape[0] > 0 and anchor.shape[1] > 0):
            return self._fn(anchor, positive, negative, self.margin)
        # Fallback for CPU tensors / exotic inputs
        return F.triplet_margin_loss(
            anchor, positive, negative, margin=self.margin
        )
```
Result of evaluating it:
The solution is correct on the test inputs. Mean runtime 2.15 ms; the PyTorch reference (with TF32 enabled) takes 9.34 ms; speedup 4.344x.
Profiler, top device kernels of one forward pass of your solution:
| device kernel | total device time (us) | calls |
|---|---|---|
| triplet_margin_loss_vec4_kernel(float4 const*, float4 const*, float4 const*, double*, unsi | 2142.0 | 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.
