```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(0xffffffff, value, offset);
    return value;
}

template<bool Vectorized>
__global__ void triplet_rows(
    const float* __restrict__ anchor,
    const float* __restrict__ positive,
    const float* __restrict__ negative,
    float* __restrict__ losses,
    int64_t dimensions,
    float margin
) {
    const int tid = threadIdx.x;
    const int64_t row = blockIdx.x;
    const int64_t base = row * dimensions;
    float positive_sum = 0.0f;
    float negative_sum = 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 < dimensions / 4; j += blockDim.x) {
            const float4 av = a[j];
            const float4 pv = p[j];
            const float4 nv = n[j];

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

            positive_sum = fmaf(dp0, dp0, positive_sum);
            positive_sum = fmaf(dp1, dp1, positive_sum);
            positive_sum = fmaf(dp2, dp2, positive_sum);
            positive_sum = fmaf(dp3, dp3, positive_sum);
            negative_sum = fmaf(dn0, dn0, negative_sum);
            negative_sum = fmaf(dn1, dn1, negative_sum);
            negative_sum = fmaf(dn2, dn2, negative_sum);
            negative_sum = fmaf(dn3, dn3, negative_sum);
        }
    } else {
        for (int64_t j = tid; j < dimensions; j += blockDim.x) {
            const float av = anchor[base + j];
            const float dp = (av - positive[base + j]) + 1.0e-6f;
            const float dn = (av - negative[base + j]) + 1.0e-6f;
            positive_sum = fmaf(dp, dp, positive_sum);
            negative_sum = fmaf(dn, dn, negative_sum);
        }
    }

    positive_sum = warp_sum(positive_sum);
    negative_sum = warp_sum(negative_sum);

    __shared__ float positive_warps[8];
    __shared__ float negative_warps[8];

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

    if (warp == 0) {
        positive_sum = lane < 8 ? positive_warps[lane] : 0.0f;
        negative_sum = lane < 8 ? negative_warps[lane] : 0.0f;
        positive_sum = warp_sum(positive_sum);
        negative_sum = warp_sum(negative_sum);

        if (lane == 0) {
            const float loss =
                (margin + sqrtf(positive_sum)) - sqrtf(negative_sum);
            losses[row] = loss < 0.0f ? 0.0f : 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 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.0f;
        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
) {
    TORCH_CHECK(anchor.is_cuda() && positive.is_cuda() && negative.is_cuda(),
                "Inputs must be CUDA tensors");
    TORCH_CHECK(anchor.device() == positive.device() &&
                anchor.device() == negative.device(),
                "Inputs must be on the same device");
    TORCH_CHECK(anchor.scalar_type() == at::kFloat &&
                positive.scalar_type() == at::kFloat &&
                negative.scalar_type() == at::kFloat,
                "Inputs must have dtype float32");
    TORCH_CHECK(anchor.dim() == 2 &&
                anchor.sizes() == positive.sizes() &&
                anchor.sizes() == negative.sizes(),
                "Inputs must have matching two-dimensional shapes");
    TORCH_CHECK(anchor.is_contiguous() && positive.is_contiguous() &&
                negative.is_contiguous(), "Inputs must be contiguous");

    const c10::cuda::CUDAGuard guard(anchor.device());
    const int64_t rows = anchor.size(0);
    const int64_t dimensions = anchor.size(1);
    auto losses = torch::empty({rows}, 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 (rows > 0) {
        const bool vectorized =
            dimensions % 4 == 0 &&
            reinterpret_cast<uintptr_t>(a) % 16 == 0 &&
            reinterpret_cast<uintptr_t>(p) % 16 == 0 &&
            reinterpret_cast<uintptr_t>(n) % 16 == 0;

        if (vectorized) {
            triplet_rows<true><<<rows, 256, 0, stream>>>(
                a, p, n, losses.data_ptr<float>(), dimensions,
                static_cast<float>(margin));
        } else {
            triplet_rows<false><<<rows, 256, 0, stream>>>(
                a, p, n, losses.data_ptr<float>(), dimensions,
                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_cuda = load_inline(
    name="fused_triplet_margin_cuda",
    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 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,
        )
```