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 torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import numpy as np
import torch.nn as nn
import torch.utils.model_zoo
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_0(in_ptr0,
    in_ptr1, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x2 = xindex // 16
    x3 = xindex % 16
    x1 = xindex // 4 % 4
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp1 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr1 + x3, xmask, eviction_policy='evict_last')
    tmp17 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp25 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp1 * tmp1
    tmp4 = tmp3 * tmp3
    tmp5 = tmp2 + tmp4
    tmp7 = tmp6 * tmp6
    tmp8 = tmp5 + tmp7
    tmp10 = tmp9 * tmp9
    tmp11 = tmp8 + tmp10
    tmp12 = libdevice.sqrt(tmp11)
    tmp13 = 1e-08
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp0 / tmp14
    tmp18 = tmp17 * tmp17
    tmp20 = tmp19 * tmp19
    tmp21 = tmp18 + tmp20
    tmp23 = tmp22 * tmp22
    tmp24 = tmp21 + tmp23
    tmp26 = tmp25 * tmp25
    tmp27 = tmp24 + tmp26
    tmp28 = libdevice.sqrt(tmp27)
    tmp29 = triton_helpers.maximum(tmp28, tmp13)
    tmp30 = tmp16 / tmp29
    tmp31 = tmp15 * tmp30
    tl.store(out_ptr0 + x4, tmp31, xmask)


@triton.jit
def triton_poi_fused_sum_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 16 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp3 = tl.load(in_ptr0 + (2 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp5 = tl.load(in_ptr0 + (3 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp9 = tl.load(in_ptr0 + (4 + 16 * x0), xmask, eviction_policy='evict_last'
        )
    tmp10 = tl.load(in_ptr0 + (5 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr0 + (6 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr0 + (7 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_ptr0 + (8 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp19 = tl.load(in_ptr0 + (9 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp21 = tl.load(in_ptr0 + (10 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp23 = tl.load(in_ptr0 + (11 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp27 = tl.load(in_ptr0 + (12 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp28 = tl.load(in_ptr0 + (13 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp30 = tl.load(in_ptr0 + (14 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp32 = tl.load(in_ptr0 + (15 + 16 * x0), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 1.0
    tmp8 = tmp6 * tmp7
    tmp11 = tmp9 + tmp10
    tmp13 = tmp11 + tmp12
    tmp15 = tmp13 + tmp14
    tmp16 = tmp15 * tmp7
    tmp17 = triton_helpers.maximum(tmp8, tmp16)
    tmp20 = tmp18 + tmp19
    tmp22 = tmp20 + tmp21
    tmp24 = tmp22 + tmp23
    tmp25 = tmp24 * tmp7
    tmp26 = triton_helpers.maximum(tmp17, tmp25)
    tmp29 = tmp27 + tmp28
    tmp31 = tmp29 + tmp30
    tmp33 = tmp31 + tmp32
    tmp34 = tmp33 * tmp7
    tmp35 = triton_helpers.maximum(tmp26, tmp34)
    tl.store(out_ptr0 + x0, tmp35, xmask)


@triton.jit
def triton_poi_fused__log_softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (16 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp10 = tl.load(in_ptr0 + (17 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr0 + (18 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp14 = tl.load(in_ptr0 + (19 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_ptr0 + (32 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp19 = tl.load(in_ptr0 + (33 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp21 = tl.load(in_ptr0 + (34 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp23 = tl.load(in_ptr0 + (35 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp27 = tl.load(in_ptr0 + (48 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp28 = tl.load(in_ptr0 + (49 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp30 = tl.load(in_ptr0 + (50 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp32 = tl.load(in_ptr0 + (51 + 4 * x0), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 0.25
    tmp8 = tmp6 * tmp7
    tmp11 = tmp9 + tmp10
    tmp13 = tmp11 + tmp12
    tmp15 = tmp13 + tmp14
    tmp16 = tmp15 * tmp7
    tmp17 = triton_helpers.maximum(tmp8, tmp16)
    tmp20 = tmp18 + tmp19
    tmp22 = tmp20 + tmp21
    tmp24 = tmp22 + tmp23
    tmp25 = tmp24 * tmp7
    tmp26 = triton_helpers.maximum(tmp17, tmp25)
    tmp29 = tmp27 + tmp28
    tmp31 = tmp29 + tmp30
    tmp33 = tmp31 + tmp32
    tmp34 = tmp33 * tmp7
    tmp35 = triton_helpers.maximum(tmp26, tmp34)
    tl.store(out_ptr0 + x0, tmp35, xmask)


@triton.jit
def triton_poi_fused__log_softmax_sum_3(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 1.0
    tmp8 = tmp6 * tmp7
    tmp10 = tmp8 - tmp9
    tmp11 = 0.25
    tmp12 = tmp10 * tmp11
    tmp13 = tmp6 * tmp11
    tmp15 = tmp13 - tmp14
    tl.store(out_ptr0 + x2, tmp12, xmask)
    tl.store(out_ptr1 + x2, tmp15, xmask)


@triton.jit
def triton_per_fused_add_arange_mul_nll_loss_forward_4(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
    tmp6 = tl.load(in_ptr0 + 4 * r0, None, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp26 = tl.load(in_ptr1 + r0, None)
    tmp28 = tl.load(in_ptr1 + (4 + r0), None)
    tmp31 = tl.load(in_ptr1 + (8 + r0), None)
    tmp34 = tl.load(in_ptr1 + (12 + r0), None)
    tmp0 = r0
    tmp1 = tl.full([1, 1], -100, tl.int64)
    tmp2 = tmp0 != tmp1
    tmp3 = tl.full([1, 1], 0, tl.int64)
    tmp4 = tl.where(tmp2, tmp0, tmp3)
    tmp5 = tl.load(in_ptr0 + (tmp4 + 4 * r0), None, eviction_policy=
        'evict_last')
    tmp7 = tl_math.exp(tmp6)
    tmp9 = tl_math.exp(tmp8)
    tmp10 = tmp7 + tmp9
    tmp12 = tl_math.exp(tmp11)
    tmp13 = tmp10 + tmp12
    tmp15 = tl_math.exp(tmp14)
    tmp16 = tmp13 + tmp15
    tmp17 = tl_math.log(tmp16)
    tmp18 = tmp5 - tmp17
    tmp19 = -tmp18
    tmp20 = 0.0
    tmp21 = tl.where(tmp2, tmp19, tmp20)
    tmp22 = tl.broadcast_to(tmp21, [XBLOCK, RBLOCK])
    tmp24 = tl.sum(tmp22, 1)[:, None]
    tmp25 = tl.load(in_ptr1 + (r0 + 4 * tmp4), None)
    tmp27 = tl_math.exp(tmp26)
    tmp29 = tl_math.exp(tmp28)
    tmp30 = tmp27 + tmp29
    tmp32 = tl_math.exp(tmp31)
    tmp33 = tmp30 + tmp32
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp33 + tmp35
    tmp37 = tl_math.log(tmp36)
    tmp38 = tmp25 - tmp37
    tmp39 = -tmp38
    tmp40 = tl.where(tmp2, tmp39, tmp20)
    tmp41 = tl.broadcast_to(tmp40, [XBLOCK, RBLOCK])
    tmp43 = tl.sum(tmp41, 1)[:, None]
    tmp44 = tmp2.to(tl.int64)
    tmp45 = tl.broadcast_to(tmp44, [XBLOCK, RBLOCK])
    tmp47 = tl.sum(tmp45, 1)[:, None]
    tmp48 = tmp47.to(tl.float32)
    tmp49 = tmp24 / tmp48
    tmp50 = tmp43 / tmp48
    tmp51 = tmp49 + tmp50
    tmp52 = 0.5
    tmp53 = tmp51 * tmp52
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp53, 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)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_0[grid(64)](
            arg0_1, arg1_1, buf0, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del arg0_1
        del arg1_1
        buf1 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        triton_poi_fused_sum_1[grid(4)](buf0, buf1, 4, XBLOCK=4, num_warps=
            1, num_stages=1)
        buf5 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        triton_poi_fused__log_softmax_2[grid(4)](buf0, buf5, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf6 = empty_strided_cuda((4, 4), (1, 4), torch.float32)
        triton_poi_fused__log_softmax_sum_3[grid(16)](buf0, buf1, buf5,
            buf2, buf6, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del buf0
        del buf1
        del buf5
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf9 = buf3
        del buf3
        triton_per_fused_add_arange_mul_nll_loss_forward_4[grid(1)](buf9,
            buf2, buf6, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del buf2
        del buf6
    return buf9,


class SimCLRLossNew(nn.Module):

    def __init__(self, temperature):
        super(SimCLRLossNew, self).__init__()
        self.T = temperature
        self.ce = nn.CrossEntropyLoss()
        self.norm = nn.functional.normalize
        self.softmax = nn.functional.softmax
        self.cosine = nn.CosineSimilarity(dim=-1)
        self.kl = nn.KLDivLoss(reduction='batchmean')
        self.mse = nn.MSELoss()

    def _get_correlated_mask(self, batch_size):
        diag = np.eye(2 * batch_size)
        l1 = np.eye(2 * batch_size, 2 * batch_size, k=-batch_size)
        l2 = np.eye(2 * batch_size, 2 * batch_size, k=batch_size)
        mask = diag + l1 + l2
        return mask

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


Measured latency_ms: 0.036076