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


class InnerProductLoss(nn.Module):
    """This is the inner-product loss used in CFKG for optimization.
    """

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

    def forward(self, anchor, positive, negative):
        pos_score = torch.mul(anchor, positive).sum(dim=1)
        neg_score = torch.mul(anchor, negative).sum(dim=1)
        return (F.softplus(-pos_score) + F.softplus(neg_score)).mean()


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), torch.rand(
        [4, 4, 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.triton_helpers import libdevice, math as tl_math
import torch.nn as 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_mean_mul_neg_softplus_sum_0(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    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 % 16
    r1 = rindex // 16
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r1), None)
    tmp1 = tl.load(in_ptr1 + (r0 + 64 * r1), None)
    tmp3 = tl.load(in_ptr0 + (16 + r0 + 64 * r1), None)
    tmp4 = tl.load(in_ptr1 + (16 + r0 + 64 * r1), None)
    tmp7 = tl.load(in_ptr0 + (32 + r0 + 64 * r1), None)
    tmp8 = tl.load(in_ptr1 + (32 + r0 + 64 * r1), None)
    tmp11 = tl.load(in_ptr0 + (48 + r0 + 64 * r1), None)
    tmp12 = tl.load(in_ptr1 + (48 + r0 + 64 * r1), None)
    tmp16 = tl.load(in_ptr2 + (r0 + 64 * r1), None)
    tmp18 = tl.load(in_ptr2 + (16 + r0 + 64 * r1), None)
    tmp21 = tl.load(in_ptr2 + (32 + r0 + 64 * r1), None)
    tmp24 = tl.load(in_ptr2 + (48 + r0 + 64 * r1), None)
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tmp15 = -tmp14
    tmp17 = tmp0 * tmp16
    tmp19 = tmp3 * tmp18
    tmp20 = tmp17 + tmp19
    tmp22 = tmp7 * tmp21
    tmp23 = tmp20 + tmp22
    tmp25 = tmp11 * tmp24
    tmp26 = tmp23 + tmp25
    tmp27 = 20.0
    tmp28 = tmp15 > tmp27
    tmp29 = tl_math.exp(tmp15)
    tmp30 = libdevice.log1p(tmp29)
    tmp31 = tl.where(tmp28, tmp15, tmp30)
    tmp32 = tmp26 > tmp27
    tmp33 = tl_math.exp(tmp26)
    tmp34 = libdevice.log1p(tmp33)
    tmp35 = tl.where(tmp32, tmp26, tmp34)
    tmp36 = tmp31 + tmp35
    tmp37 = tl.broadcast_to(tmp36, [XBLOCK, RBLOCK])
    tmp39 = tl.sum(tmp37, 1)[:, None]
    tmp40 = 64.0
    tmp41 = tmp39 / tmp40
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp41, None)


def call(args):
    arg0_1, arg1_1, arg2_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf2 = empty_strided_cuda((), (), torch.float32)
        buf3 = buf2
        del buf2
        get_raw_stream(0)
        triton_per_fused_add_mean_mul_neg_softplus_sum_0[grid(1)](buf3,
            arg1_1, arg0_1, arg2_1, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
    return buf3,


class InnerProductLossNew(nn.Module):
    """This is the inner-product loss used in CFKG for optimization.
    """

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

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