```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 triplet_margin_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& sp, float& sn) {
    const float dp = (a - p) + 1.0e-6f;
    const float dn = (a - n) + 1.0e-6f;
    sp = fmaf(dp, dp, sp);
    sn = fmaf(dn, dn, sn);
}

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

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

    float sp = 0.0f;
    float sn = 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 i = tid; i < width / 4; i += blockDim.x) {
            const float4 av = a[i];
            const float4 pv = p[i];
            const float4 nv = n[i];
            accumulate(av.x, pv.x, nv.x, sp, sn);
            accumulate(av.y, pv.y, nv.y, sp, sn);
            accumulate(av.z, pv.z, nv.z, sp, sn);
            accumulate(av.w, pv.w, nv.w, sp, sn);
        }
    } else {
        for (int64_t i = tid; i < width; i += blockDim.x)
            accumulate(anchor[base + i], positive[base + i],
                       negative[base + i], sp, sn);
    }

    sp = warp_sum(sp);
    sn = warp_sum(sn);

    __shared__ float positive_sums[8];
    __shared__ float negative_sums[8];

    if (lane == 0) {
        positive_sums[warp] = sp;
        negative_sums[warp] = sn;
    }
    __syncthreads();

    if (warp == 0) {
        sp = lane < 8 ? positive_sums[lane] : 0.0f;
        sn = lane < 8 ? negative_sums[lane] : 0.0f;
        sp = warp_sum(sp);
        sn = warp_sum(sn);

        if (lane == 0) {
            const float loss = (margin + sqrtf(sp)) - sqrtf(sn);
            losses[blockIdx.x] = isnan(loss) ? loss : fmaxf(loss, 0.0f);
        }
    }
}

__global__ void mean_loss(
    const float* __restrict__ losses,
    float* __restrict__ output,
    int64_t count) {

    const int tid = threadIdx.x;
    const int lane = tid & 31;
    const int warp = tid >> 5;

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

    sum = warp_sum(sum);
    __shared__ float sums[8];

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

    if (warp == 0) {
        sum = lane < 8 ? sums[lane] : 0.0f;
        sum = warp_sum(sum);
        if (lane == 0)
            output[0] = count ? sum / float(count) : nanf("");
    }
}

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

    TORCH_CHECK(anchor.is_cuda() && positive.is_cuda() &&
                negative.is_cuda(), "Inputs must be CUDA tensors");
    TORCH_CHECK(anchor.scalar_type() == at::kFloat &&
                positive.scalar_type() == at::kFloat &&
                negative.scalar_type() == at::kFloat,
                "Inputs must have float32 dtype");
    TORCH_CHECK(anchor.dim() == 2 &&
                anchor.sizes() == positive.sizes() &&
                anchor.sizes() == negative.sizes(),
                "Inputs must have matching two-dimensional shapes");
    TORCH_CHECK(anchor.device() == positive.device() &&
                anchor.device() == negative.device(),
                "Inputs must be on the same device");
    TORCH_CHECK(anchor.is_contiguous() && positive.is_contiguous() &&
                negative.is_contiguous(), "Inputs must be contiguous");

    const c10::cuda::CUDAGuard guard(anchor.device());
    const auto stream = at::cuda::getCurrentCUDAStream();

    const int64_t rows = anchor.size(0);
    const int64_t width = anchor.size(1);
    auto losses = torch::empty({rows}, anchor.options());
    auto output = torch::empty({}, anchor.options());

    if (rows > 0) {
        const bool aligned =
            (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 (width % 4 == 0 && aligned) {
            triplet_rows<true><<<rows, 256, 0, stream.stream()>>>(
                anchor.data_ptr<float>(), positive.data_ptr<float>(),
                negative.data_ptr<float>(), losses.data_ptr<float>(),
                width, float(margin));
        } else {
            triplet_rows<false><<<rows, 256, 0, stream.stream()>>>(
                anchor.data_ptr<float>(), positive.data_ptr<float>(),
                negative.data_ptr<float>(), losses.data_ptr<float>(),
                width, float(margin));
        }
        C10_CUDA_KERNEL_LAUNCH_CHECK();
    }

    mean_loss<<<1, 256, 0, stream.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_loss_cuda",
    cpp_sources=_cpp_source,
    cuda_sources=_cuda_source,
    functions=["triplet_margin_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 zero")
        self.margin = float(margin)

    def forward(self, anchor, positive, negative):
        inputs = (anchor, positive, negative)
        use_custom = (
            all(x.is_cuda and x.dtype == torch.float32 for x in inputs)
            and anchor.ndim == 2
            and anchor.shape == positive.shape == negative.shape
            and anchor.device == positive.device == negative.device
            and not (
                torch.is_grad_enabled()
                and any(x.requires_grad for x in inputs)
            )
        )
        if not use_custom:
            return F.triplet_margin_loss(
                anchor, positive, negative, margin=self.margin
            )

        return _triplet_extension.triplet_margin_cuda(
            anchor.contiguous(),
            positive.contiguous(),
            negative.contiguous(),
            self.margin,
        )
```