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 5 of 6 for this task. Your previous solution (turn 4) was:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.cpp_extension import load_inline

_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>
#include <cstdint>

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

__global__ __launch_bounds__(256, 4)
void triplet_rows_8192(
    const float4* __restrict__ anchor,
    const float4* __restrict__ positive,
    const float4* __restrict__ negative,
    float* __restrict__ partials,
    int64_t rows,
    float margin
) {
    constexpr int RowsPerBlock = 8;
    constexpr int VectorsPerRow = 2048;

    const int lane = threadIdx.x & 31;
    const int warp = threadIdx.x >> 5;
    const int64_t row = int64_t(blockIdx.x) * RowsPerBlock + warp;

    float p0 = 0.f, p1 = 0.f, p2 = 0.f, p3 = 0.f;
    float n0 = 0.f, n1 = 0.f, n2 = 0.f, n3 = 0.f;

    if (row < rows) {
        const int64_t base = row * VectorsPerRow;

        #pragma unroll 4
        for (int j = lane; j < VectorsPerRow; j += 32) {
            const float4 av = anchor[base + j];
            const float4 pv = positive[base + j];
            const float4 nv = negative[base + j];

            const float dp0 = (av.x - pv.x) + 1.e-6f;
            const float dp1 = (av.y - pv.y) + 1.e-6f;
            const float dp2 = (av.z - pv.z) + 1.e-6f;
            const float dp3 = (av.w - pv.w) + 1.e-6f;

            const float dn0 = (av.x - nv.x) + 1.e-6f;
            const float dn1 = (av.y - nv.y) + 1.e-6f;
            const float dn2 = (av.z - nv.z) + 1.e-6f;
            const float dn3 = (av.w - nv.w) + 1.e-6f;

            p0 = fmaf(dp0, dp0, p0);
            p1 = fmaf(dp1, dp1, p1);
            p2 = fmaf(dp2, dp2, p2);
            p3 = fmaf(dp3, dp3, p3);

            n0 = fmaf(dn0, dn0, n0);
            n1 = fmaf(dn1, dn1, n1);
            n2 = fmaf(dn2, dn2, n2);
            n3 = fmaf(dn3, dn3, n3);
        }
    }

    const float ps = warp_sum((p0 + p1) + (p2 + p3));
    const float ns = warp_sum((n0 + n1) + (n2 + n3));

    __shared__ float losses[RowsPerBlock];
    if (lane == 0) {
        float loss = 0.f;
        if (row < rows) {
            loss = (margin + sqrtf(ps)) - sqrtf(ns);
            loss = loss < 0.f ? 0.f : loss;
        }
        losses[warp] = loss;
    }
    __syncthreads();

    if (warp == 0) {
        float sum = lane < RowsPerBlock ? losses[lane] : 0.f;
        sum = warp_sum(sum);
        if (lane == 0)
            partials[blockIdx.x] = sum;
    }
}

__global__ __launch_bounds__(256)
void triplet_mean(
    const float* __restrict__ partials,
    float* __restrict__ output,
    int64_t count,
    int64_t rows
) {
    float s0 = 0.f, s1 = 0.f, s2 = 0.f, s3 = 0.f;
    const float4* vectors = reinterpret_cast<const float4*>(partials);

    for (int64_t i = threadIdx.x; i < count / 4; i += 256) {
        const float4 value = vectors[i];
        s0 += value.x;
        s1 += value.y;
        s2 += value.z;
        s3 += value.w;
    }

    for (int64_t i = (count / 4) * 4 + threadIdx.x;
         i < count; i += 256)
        s0 += partials[i];

    float sum = warp_sum((s0 + s1) + (s2 + s3));

    __shared__ float warp_sums[8];
    const int lane = threadIdx.x & 31;
    const int warp = threadIdx.x >> 5;

    if (lane == 0)
        warp_sums[warp] = sum;
    __syncthreads();

    if (warp == 0) {
        sum = lane < 8 ? warp_sums[lane] : 0.f;
        sum = warp_sum(sum);
        if (lane == 0)
            output[0] = sum / static_cast<float>(rows);
    }
}

torch::Tensor triplet_forward(
    torch::Tensor anchor,
    torch::Tensor positive,
    torch::Tensor negative,
    double margin
) {
    const c10::cuda::CUDAGuard guard(anchor.device());
    const int64_t rows = anchor.size(0);
    const int64_t groups = (rows + 7) / 8;

    auto partials = torch::empty({groups}, anchor.options());
    auto output = torch::empty({}, anchor.options());
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();

    triplet_rows_8192<<<static_cast<unsigned int>(groups), 256, 0, stream>>>(
        reinterpret_cast<const float4*>(anchor.data_ptr<float>()),
        reinterpret_cast<const float4*>(positive.data_ptr<float>()),
        reinterpret_cast<const float4*>(negative.data_ptr<float>()),
        partials.data_ptr<float>(),
        rows,
        static_cast<float>(margin)
    );
    C10_CUDA_KERNEL_LAUNCH_CHECK();

    triplet_mean<<<1, 256, 0, stream>>>(
        partials.data_ptr<float>(),
        output.data_ptr<float>(),
        groups,
        rows
    );
    C10_CUDA_KERNEL_LAUNCH_CHECK();

    return output;
}
"""

_triplet_cuda = load_inline(
    name="fused_triplet_margin_warp_rows_v4",
    cpp_sources=r"""
        torch::Tensor triplet_forward(
            torch::Tensor anchor,
            torch::Tensor positive,
            torch::Tensor negative,
            double margin);
    """,
    cuda_sources=_cuda_source,
    functions=["triplet_forward"],
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
    verbose=False,
)


class ModelNew(nn.Module):
    def __init__(self, margin=1.0):
        super().__init__()
        if margin <= 0:
            raise ValueError("margin must be greater than zero")
        self.margin = float(margin)

    def forward(self, anchor, positive, negative):
        tensors = (anchor, positive, negative)
        use_custom = (
            anchor.ndim == 2
            and anchor.shape[1] == 8192
            and 0 < anchor.shape[0] <= 17179869176
            and anchor.shape == positive.shape == negative.shape
            and anchor.device == positive.device == negative.device
            and all(
                t.is_cuda
                and t.dtype == torch.float32
                and t.is_contiguous()
                and t.data_ptr() % 16 == 0
                for t in tensors
            )
            and not (
                torch.is_grad_enabled()
                and any(t.requires_grad for t in tensors)
            )
        )

        if use_custom:
            return _triplet_cuda.triplet_forward(
                anchor, positive, negative, self.margin
            )

        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.17 ms; the PyTorch reference (with TF32 enabled) takes 9.34 ms; speedup 4.304x.
Profiler, top device kernels of one forward pass of your solution:
| device kernel | total device time (us) | calls |
|---|---|---|
| triplet_rows_8192(float4 const*, float4 const*, float4 const*, float*, long, float) | 2150.5 | 1 |
| triplet_mean(float const*, float*, long, long) | 3.3 | 1 |

The fastest correct solution so far (4.324x speedup) is:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.cpp_extension import load_inline

_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>
#include <cstdint>

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

template<int FixedDimensions, bool Vectorized>
__global__ __launch_bounds__(512, 2)
void triplet_groups(
    const float* __restrict__ anchor,
    const float* __restrict__ positive,
    const float* __restrict__ negative,
    float* __restrict__ partials,
    int64_t rows,
    int dimensions,
    float margin
) {
    constexpr int ThreadsPerRow = 128;
    constexpr int RowsPerBlock = 4;

    const int tid = threadIdx.x;
    const int row_thread = tid & (ThreadsPerRow - 1);
    const int64_t row = int64_t(blockIdx.x) * RowsPerBlock + tid / ThreadsPerRow;
    const int d = FixedDimensions ? FixedDimensions : dimensions;
    const int64_t base = row * int64_t(d);

    float p0 = 0.f, p1 = 0.f, p2 = 0.f, p3 = 0.f;
    float n0 = 0.f, n1 = 0.f, n2 = 0.f, n3 = 0.f;

    if (row < rows) {
        if (Vectorized) {
            const float4* a = reinterpret_cast<const float4*>(anchor + base);
            const float4* p = reinterpret_cast<const float4*>(positive + base);
            const float4* n = reinterpret_cast<const float4*>(negative + base);

            #pragma unroll 4
            for (int j = row_thread; j < d / 4; j += ThreadsPerRow) {
                const float4 av = a[j];
                const float4 pv = p[j];
                const float4 nv = n[j];

                const float dp0 = (av.x - pv.x) + 1.e-6f;
                const float dp1 = (av.y - pv.y) + 1.e-6f;
                const float dp2 = (av.z - pv.z) + 1.e-6f;
                const float dp3 = (av.w - pv.w) + 1.e-6f;
                const float dn0 = (av.x - nv.x) + 1.e-6f;
                const float dn1 = (av.y - nv.y) + 1.e-6f;
                const float dn2 = (av.z - nv.z) + 1.e-6f;
                const float dn3 = (av.w - nv.w) + 1.e-6f;

                p0 = fmaf(dp0, dp0, p0);
                p1 = fmaf(dp1, dp1, p1);
                p2 = fmaf(dp2, dp2, p2);
                p3 = fmaf(dp3, dp3, p3);
                n0 = fmaf(dn0, dn0, n0);
                n1 = fmaf(dn1, dn1, n1);
                n2 = fmaf(dn2, dn2, n2);
                n3 = fmaf(dn3, dn3, n3);
            }
        } else {
            for (int j = row_thread; j < d; j += ThreadsPerRow) {
                const float av = anchor[base + j];
                const float dp = (av - positive[base + j]) + 1.e-6f;
                const float dn = (av - negative[base + j]) + 1.e-6f;
                p0 = fmaf(dp, dp, p0);
                n0 = fmaf(dn, dn, n0);
            }
        }
    }

    float ps = warp_sum((p0 + p1) + (p2 + p3));
    float ns = warp_sum((n0 + n1) + (n2 + n3));

    __shared__ float positive_warps[16];
    __shared__ float negative_warps[16];

    const int lane = tid & 31;
    const int warp = tid >> 5;
    if (lane == 0) {
        positive_warps[warp] = ps;
        negative_warps[warp] = ns;
    }
    __syncthreads();

    if (warp == 0) {
        ps = lane < 16 ? positive_warps[lane] : 0.f;
        ns = lane < 16 ? negative_warps[lane] : 0.f;

        ps += __shfl_down_sync(0xffffffffu, ps, 2, 4);
        ns += __shfl_down_sync(0xffffffffu, ns, 2, 4);
        ps += __shfl_down_sync(0xffffffffu, ps, 1, 4);
        ns += __shfl_down_sync(0xffffffffu, ns, 1, 4);

        float loss = 0.f;
        if (lane < 16 && (lane & 3) == 0 &&
            int64_t(blockIdx.x) * RowsPerBlock + lane / 4 < rows) {
            loss = (margin + sqrtf(ps)) - sqrtf(ns);
            loss = loss < 0.f ? 0.f : loss;
        }

        loss = warp_sum(loss);
        if (lane == 0)
            partials[blockIdx.x] = loss;
    }
}

__global__ void triplet_mean(
    const float* __restrict__ partials,
    float* __restrict__ output,
    int64_t count,
    int64_t rows
) {
    const int tid = threadIdx.x;
    float s0 = 0.f, s1 = 0.f, s2 = 0.f, s3 = 0.f;
    const float4* vectors = reinterpret_cast<const float4*>(partials);

    for (int64_t i = tid; i < count / 4; i += blockDim.x) {
        const float4 v = vectors[i];
        s0 += v.x;
        s1 += v.y;
        s2 += v.z;
        s3 += v.w;
    }
    for (int64_t i = (count / 4) * 4 + tid; i < count; i += blockDim.x)
        s0 += partials[i];

    float sum = warp_sum((s0 + s1) + (s2 + s3));
    __shared__ float warp_sums[16];

    const int lane = tid & 31;
    const int warp = tid >> 5;
    if (lane == 0)
        warp_sums[warp] = sum;
    __syncthreads();

    if (warp == 0) {
        sum = lane < 16 ? warp_sums[lane] : 0.f;
        sum = warp_sum(sum);
        if (lane == 0)
            output[0] = sum / static_cast<float>(rows);
    }
}

torch::Tensor triplet_forward(
    torch::Tensor anchor,
    torch::Tensor positive,
    torch::Tensor negative,
    double margin
) {
    const c10::cuda::CUDAGuard guard(anchor.device());
    const int64_t rows = anchor.size(0);
    const int dimensions = static_cast<int>(anchor.size(1));
    const int64_t groups = (rows + 3) / 4;

    auto partials = torch::empty({groups}, anchor.options());
    auto output = torch::empty({}, anchor.options());
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();

    const float* a = anchor.data_ptr<float>();
    const float* p = positive.data_ptr<float>();
    const float* n = negative.data_ptr<float>();

    if (groups) {
        const bool vectorized =
            dimensions % 4 == 0 &&
            ((reinterpret_cast<uintptr_t>(a) |
              reinterpret_cast<uintptr_t>(p) |
              reinterpret_cast<uintptr_t>(n)) & 15) == 0;

        if (vectorized && dimensions == 8192) {
            triplet_groups<8192, true><<<groups, 512, 0, stream>>>(
                a, p, n, partials.data_ptr<float>(), rows,
                dimensions, static_cast<float>(margin));
        } else if (vectorized) {
            triplet_groups<0, true><<<groups, 512, 0, stream>>>(
                a, p, n, partials.data_ptr<float>(), rows,
                dimensions, static_cast<float>(margin));
        } else {
            triplet_groups<0, false><<<groups, 512, 0, stream>>>(
                a, p, n, partials.data_ptr<float>(), rows,
                dimensions, static_cast<float>(margin));
        }
        C10_CUDA_KERNEL_LAUNCH_CHECK();
    }

    triplet_mean<<<1, 512, 0, stream>>>(
        partials.data_ptr<float>(), output.data_ptr<float>(), groups, rows);
    C10_CUDA_KERNEL_LAUNCH_CHECK();
    return output;
}
"""

_triplet_cuda = load_inline(
    name="fused_triplet_margin_grouped_v2",
    cpp_sources="""
        torch::Tensor triplet_forward(
            torch::Tensor anchor,
            torch::Tensor positive,
            torch::Tensor negative,
            double margin);
    """,
    cuda_sources=_cuda_source,
    functions=["triplet_forward"],
    extra_cflags=["-O3"],
    extra_cuda_cflags=["-O3"],
    verbose=False,
)


class ModelNew(nn.Module):
    def __init__(self, margin=1.0):
        super().__init__()
        if margin <= 0:
            raise ValueError("margin must be greater than zero")
        self.margin = float(margin)

    def forward(self, anchor, positive, negative):
        tensors = (anchor, positive, negative)
        use_custom = (
            all(t.is_cuda and t.dtype == torch.float32 for t in tensors)
            and anchor.ndim == 2
            and anchor.shape == positive.shape == negative.shape
            and anchor.device == positive.device == negative.device
            and anchor.shape[1] <= 2147483519
            and anchor.shape[0] <= 8589934588
            and not (
                torch.is_grad_enabled()
                and any(t.requires_grad for t in tensors)
            )
        )
        if not use_custom:
            return F.triplet_margin_loss(
                anchor, positive, negative, margin=self.margin
            )

        return _triplet_cuda.triplet_forward(
            anchor.contiguous(),
            positive.contiguous(),
            negative.contiguous(),
            self.margin,
        )
```

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.
