import torch
import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    def __init__(self, margin=2.0):
        super().__init__()
        self.margin = margin

    def forward(self, x1: torch.Tensor, x2: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
        dist = F.pairwise_distance(x1, x2, keepdim=True)

        loss_con = (1 - y) * torch.pow(dist, 2)
        loss_dis = y * torch.pow(torch.clamp(self.margin - dist, min=0.0), 2)

        loss = 0.5 * (loss_con + loss_dis)
        return loss.mean()


batch_size = 128
feature_dim = 512


def get_inputs():
    x1 = torch.randn(batch_size, feature_dim, dtype=torch.float32)
    x2 = torch.randn(batch_size, feature_dim, dtype=torch.float32)
    y = torch.randint(0, 2, (batch_size, 1), dtype=torch.float32)
    return [x1, x2, y]


def get_init_inputs():
    return [2.0]