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


_cpp_source = """
#include <torch/extension.h>
torch::Tensor triplet_loss_cuda(
    torch::Tensor anchor,
    torch::Tensor positive,
    torch::Tensor negative,
    double margin);
"""

_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>

__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;
}

__device__ __forceinline__ void accumulate(
    float a, float p, float n, float& positive_sum, float& negative_sum) {
    float dp = (a - p) + 1.0e-6f;
    float dn = (a - n) + 1.0e-6f;
    positive_sum = fmaf(dp, dp, positive_sum);
    negative_sum = fmaf(dn, dn, negative_sum);
}

template<bool VECTORIZED>
__global__ void triplet_rows(
    const float* __restrict__ anchor,
    const float* __restrict__ positive,
    const float* __restrict__ negative,
    float* __restrict__ losses,
    int64_t features,
    float margin) {

    const int tid = threadIdx.x;
    const int lane = tid & 31;
    const int warp = tid >> 5;
    const int64_t base = static_cast<int64_t>(blockIdx.x) * features;

    float ps = 0.0f;
    float ns = 0.0f;

    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);

        for (int64_t j = tid; j < features / 4; j += blockDim.x) {
            float4 av = a[j];
            float4 pv = p[j];
            float4 nv = n[j];
            accumulate(av.x, pv.x, nv.x, ps, ns);
            accumulate(av.y, pv.y, nv.y, ps, ns);
            accumulate(av.z, pv.z, nv.z, ps, ns);
            accumulate(av.w, pv.w, nv.w, ps, ns);
        }
    } else {
        for (int64_t j = tid; j < features; j += blockDim.x)
            accumulate(anchor[base + j], positive[base + j],
                       negative[base + j], ps, ns);
    }

    ps = warp_sum(ps);
    ns = warp_sum(ns);

    __shared__ float positive_partial[8];
    __shared__ float negative_partial[8];

    if (lane == 0) {
        positive_partial[warp] = ps;
        negative_partial[warp] = ns;
    }
    __syncthreads();

    if (warp == 0) {
        ps = warp_sum(lane < 8 ? positive_partial[lane] : 0.0f);
        ns = warp_sum(lane < 8 ? negative_partial[lane] : 0.0f);

        if (lane == 0) {
            float loss = (margin + sqrtf(ps)) - sqrtf(ns);
            if (loss < 0.0f)
                loss = 0.0f;
            losses[blockIdx.x] = loss;
        }
    }
}

__global__ void triplet_mean(
    const float* __restrict__ losses,
    float* __restrict__ output,
    int64_t rows) {

    float sum = 0.0f;
    for (int64_t i = threadIdx.x; i < rows; i += blockDim.x)
        sum += losses[i];

    sum = warp_sum(sum);

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

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

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

torch::Tensor triplet_loss_cuda(
    torch::Tensor anchor,
    torch::Tensor positive,
    torch::Tensor negative,
    double margin) {

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

    const int64_t rows = anchor.size(0);
    const int64_t features = anchor.size(1);
    auto losses = torch::empty({rows}, anchor.options());
    auto output = torch::empty({}, anchor.options());
    cudaStream_t stream = at::cuda::getCurrentCUDAStream();

    if (rows > 0) {
        const bool vectorized =
            features % 4 == 0 &&
            reinterpret_cast<uintptr_t>(anchor.data_ptr<float>()) % 16 == 0 &&
            reinterpret_cast<uintptr_t>(positive.data_ptr<float>()) % 16 == 0 &&
            reinterpret_cast<uintptr_t>(negative.data_ptr<float>()) % 16 == 0;

        if (vectorized) {
            triplet_rows<true><<<rows, 256, 0, stream>>>(
                anchor.data_ptr<float>(), positive.data_ptr<float>(),
                negative.data_ptr<float>(), losses.data_ptr<float>(),
                features, static_cast<float>(margin));
        } else {
            triplet_rows<false><<<rows, 256, 0, stream>>>(
                anchor.data_ptr<float>(), positive.data_ptr<float>(),
                negative.data_ptr<float>(), losses.data_ptr<float>(),
                features, static_cast<float>(margin));
        }
        C10_CUDA_KERNEL_LAUNCH_CHECK();
    }

    triplet_mean<<<1, 256, 0, stream>>>(
        losses.data_ptr<float>(), output.data_ptr<float>(), rows);
    C10_CUDA_KERNEL_LAUNCH_CHECK();
    return output;
}
"""

_triplet_extension = load_inline(
    name="fused_triplet_margin_cuda",
    cpp_sources=_cpp_source,
    cuda_sources=_cuda_source,
    functions=["triplet_loss_cuda"],
    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 0")
        self.margin = float(margin)

    def forward(self, anchor, positive, negative):
        tensors = (anchor, positive, negative)
        if (
            all(t.is_cuda and t.dtype == torch.float32 and t.ndim == 2
                for t in tensors)
            and anchor.shape == positive.shape == negative.shape
            and anchor.device == positive.device == negative.device
            and not (
                torch.is_grad_enabled()
                and any(t.requires_grad for t in tensors)
            )
        ):
            return _triplet_extension.triplet_loss_cuda(
                anchor.contiguous(),
                positive.contiguous(),
                negative.contiguous(),
                self.margin,
            )

        return F.triplet_margin_loss(
            anchor, positive, negative,
            margin=self.margin, p=2, eps=1e-6,
            swap=False, reduction="mean",
        )
```