import torch
from abc import abstractmethod
import torch.utils.data.dataloader
import torch.nn.functional as F
import torch.nn as nn
import torch.nn


class SimilarityLoss(nn.Module):

    def __init__(self):
        super(SimilarityLoss, self).__init__()

    @abstractmethod
    def forward(self, inputs, targets):
        pass


class RankingLoss(SimilarityLoss):
    """
    Triplet ranking loss between pair similarities and pair labels.
    """

    def __init__(self, margin=0.1, direction_weights=[0.5, 0.5]):
        super(RankingLoss, self).__init__()
        self.margin = margin
        self.direction_weights = direction_weights

    def forward(self, inputs, targets):
        n = inputs.shape[0]
        neg_targets = torch.ones_like(targets) - targets
        ranking_loss_matrix_01 = neg_targets * F.relu(self.margin + inputs -
            torch.diag(inputs).view(n, 1))
        ranking_loss_matrix_10 = neg_targets * F.relu(self.margin + inputs -
            torch.diag(inputs).view(1, n))
        neg_targets_01_sum = torch.sum(neg_targets, dim=1)
        neg_targets_10_sum = torch.sum(neg_targets, dim=0)
        loss = self.direction_weights[0] * torch.mean(torch.sum(
            ranking_loss_matrix_01 / neg_targets_01_sum, dim=1)
            ) + self.direction_weights[1] * torch.mean(torch.sum(
            ranking_loss_matrix_10 / neg_targets_10_sum, dim=0))
        return loss


def get_inputs():
    return [torch.rand([4, 4]), torch.rand([4, 4])]


def get_init_inputs():
    return [[], {}]

import torch
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid
from torch._C import _cuda_getCurrentRawStream as get_raw_stream
from torch._inductor.runtime import triton_helpers
from abc import abstractmethod
import torch.utils.data.dataloader
import torch.nn as nn
import torch.nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused_add_div_mean_mul_ones_like_relu_sub_sum_0(in_out_ptr0,
    in_ptr0, in_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + 4 * r0, None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr1 + 5 * r0, None, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + 0)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK])
    tmp14 = tl.load(in_ptr0 + 1)
    tmp15 = tl.broadcast_to(tmp14, [XBLOCK, RBLOCK])
    tmp18 = tl.load(in_ptr0 + 2)
    tmp19 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp22 = tl.load(in_ptr0 + 3)
    tmp23 = tl.broadcast_to(tmp22, [XBLOCK, RBLOCK])
    tmp27 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp29 = tl.load(in_ptr1 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp34 = tl.load(in_ptr0 + 4)
    tmp35 = tl.broadcast_to(tmp34, [XBLOCK, RBLOCK])
    tmp37 = tl.load(in_ptr0 + 5)
    tmp38 = tl.broadcast_to(tmp37, [XBLOCK, RBLOCK])
    tmp41 = tl.load(in_ptr0 + 6)
    tmp42 = tl.broadcast_to(tmp41, [XBLOCK, RBLOCK])
    tmp45 = tl.load(in_ptr0 + 7)
    tmp46 = tl.broadcast_to(tmp45, [XBLOCK, RBLOCK])
    tmp51 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp53 = tl.load(in_ptr1 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp58 = tl.load(in_ptr0 + 8)
    tmp59 = tl.broadcast_to(tmp58, [XBLOCK, RBLOCK])
    tmp61 = tl.load(in_ptr0 + 9)
    tmp62 = tl.broadcast_to(tmp61, [XBLOCK, RBLOCK])
    tmp65 = tl.load(in_ptr0 + 10)
    tmp66 = tl.broadcast_to(tmp65, [XBLOCK, RBLOCK])
    tmp69 = tl.load(in_ptr0 + 11)
    tmp70 = tl.broadcast_to(tmp69, [XBLOCK, RBLOCK])
    tmp75 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp77 = tl.load(in_ptr1 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp82 = tl.load(in_ptr0 + 12)
    tmp83 = tl.broadcast_to(tmp82, [XBLOCK, RBLOCK])
    tmp85 = tl.load(in_ptr0 + 13)
    tmp86 = tl.broadcast_to(tmp85, [XBLOCK, RBLOCK])
    tmp89 = tl.load(in_ptr0 + 14)
    tmp90 = tl.broadcast_to(tmp89, [XBLOCK, RBLOCK])
    tmp93 = tl.load(in_ptr0 + 15)
    tmp94 = tl.broadcast_to(tmp93, [XBLOCK, RBLOCK])
    tmp99 = tl.load(in_ptr0 + r0, None)
    tmp101 = tl.load(in_ptr1 + r0, None)
    tmp106 = tl.load(in_ptr0 + (4 + r0), None)
    tmp109 = tl.load(in_ptr0 + (8 + r0), None)
    tmp112 = tl.load(in_ptr0 + (12 + r0), None)
    tmp116 = tl.load(in_ptr1 + (4 + r0), None)
    tmp123 = tl.load(in_ptr1 + (8 + r0), None)
    tmp130 = tl.load(in_ptr1 + (12 + r0), None)
    tmp1 = 1.0
    tmp2 = tmp1 - tmp0
    tmp4 = 0.1
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 - tmp6
    tmp8 = tl.full([1, 1], 0, tl.int32)
    tmp9 = triton_helpers.maximum(tmp8, tmp7)
    tmp10 = tmp2 * tmp9
    tmp13 = tmp1 - tmp12
    tmp16 = tmp1 - tmp15
    tmp17 = tmp13 + tmp16
    tmp20 = tmp1 - tmp19
    tmp21 = tmp17 + tmp20
    tmp24 = tmp1 - tmp23
    tmp25 = tmp21 + tmp24
    tmp26 = tmp10 / tmp25
    tmp28 = tmp1 - tmp27
    tmp30 = tmp29 + tmp4
    tmp31 = tmp30 - tmp6
    tmp32 = triton_helpers.maximum(tmp8, tmp31)
    tmp33 = tmp28 * tmp32
    tmp36 = tmp1 - tmp35
    tmp39 = tmp1 - tmp38
    tmp40 = tmp36 + tmp39
    tmp43 = tmp1 - tmp42
    tmp44 = tmp40 + tmp43
    tmp47 = tmp1 - tmp46
    tmp48 = tmp44 + tmp47
    tmp49 = tmp33 / tmp48
    tmp50 = tmp26 + tmp49
    tmp52 = tmp1 - tmp51
    tmp54 = tmp53 + tmp4
    tmp55 = tmp54 - tmp6
    tmp56 = triton_helpers.maximum(tmp8, tmp55)
    tmp57 = tmp52 * tmp56
    tmp60 = tmp1 - tmp59
    tmp63 = tmp1 - tmp62
    tmp64 = tmp60 + tmp63
    tmp67 = tmp1 - tmp66
    tmp68 = tmp64 + tmp67
    tmp71 = tmp1 - tmp70
    tmp72 = tmp68 + tmp71
    tmp73 = tmp57 / tmp72
    tmp74 = tmp50 + tmp73
    tmp76 = tmp1 - tmp75
    tmp78 = tmp77 + tmp4
    tmp79 = tmp78 - tmp6
    tmp80 = triton_helpers.maximum(tmp8, tmp79)
    tmp81 = tmp76 * tmp80
    tmp84 = tmp1 - tmp83
    tmp87 = tmp1 - tmp86
    tmp88 = tmp84 + tmp87
    tmp91 = tmp1 - tmp90
    tmp92 = tmp88 + tmp91
    tmp95 = tmp1 - tmp94
    tmp96 = tmp92 + tmp95
    tmp97 = tmp81 / tmp96
    tmp98 = tmp74 + tmp97
    tmp100 = tmp1 - tmp99
    tmp102 = tmp101 + tmp4
    tmp103 = tmp102 - tmp6
    tmp104 = triton_helpers.maximum(tmp8, tmp103)
    tmp105 = tmp100 * tmp104
    tmp107 = tmp1 - tmp106
    tmp108 = tmp100 + tmp107
    tmp110 = tmp1 - tmp109
    tmp111 = tmp108 + tmp110
    tmp113 = tmp1 - tmp112
    tmp114 = tmp111 + tmp113
    tmp115 = tmp105 / tmp114
    tmp117 = tmp116 + tmp4
    tmp118 = tmp117 - tmp6
    tmp119 = triton_helpers.maximum(tmp8, tmp118)
    tmp120 = tmp107 * tmp119
    tmp121 = tmp120 / tmp114
    tmp122 = tmp115 + tmp121
    tmp124 = tmp123 + tmp4
    tmp125 = tmp124 - tmp6
    tmp126 = triton_helpers.maximum(tmp8, tmp125)
    tmp127 = tmp110 * tmp126
    tmp128 = tmp127 / tmp114
    tmp129 = tmp122 + tmp128
    tmp131 = tmp130 + tmp4
    tmp132 = tmp131 - tmp6
    tmp133 = triton_helpers.maximum(tmp8, tmp132)
    tmp134 = tmp113 * tmp133
    tmp135 = tmp134 / tmp114
    tmp136 = tmp129 + tmp135
    tmp137 = tl.broadcast_to(tmp98, [XBLOCK, RBLOCK])
    tmp139 = tl.sum(tmp137, 1)[:, None]
    tmp140 = tl.broadcast_to(tmp136, [XBLOCK, RBLOCK])
    tmp142 = tl.sum(tmp140, 1)[:, None]
    tmp143 = 4.0
    tmp144 = tmp139 / tmp143
    tmp145 = 0.5
    tmp146 = tmp144 * tmp145
    tmp147 = tmp142 / tmp143
    tmp148 = tmp147 * tmp145
    tmp149 = tmp146 + tmp148
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp149, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf4 = buf1
        del buf1
        get_raw_stream(0)
        triton_per_fused_add_div_mean_mul_ones_like_relu_sub_sum_0[grid(1)](
            buf4, arg1_1, arg0_1, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf4,


class SimilarityLoss(nn.Module):

    def __init__(self):
        super(SimilarityLoss, self).__init__()

    @abstractmethod
    def forward(self, inputs, targets):
        pass


class RankingLossNew(SimilarityLoss):
    """
    Triplet ranking loss between pair similarities and pair labels.
    """

    def __init__(self, margin=0.1, direction_weights=[0.5, 0.5]):
        super(RankingLossNew, self).__init__()
        self.margin = margin
        self.direction_weights = direction_weights

    def forward(self, input_0, input_1):
        arg0_1 = input_0
        arg1_1 = input_1
        output = call([arg0_1, arg1_1])
        return output[0]
