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
from torch.nn import Module
from torch.nn import LogSoftmax
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_linalg_vector_norm_0(in_ptr0, in_ptr1, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 8 % 8
    x2 = xindex // 64
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x2 + 64 * x1), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x2 + 64 * (-4 + x1)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp11 = tmp10 * tmp10
    tmp12 = tl.load(in_ptr0 + (16 + x2 + 64 * x1), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp13 = tl.load(in_ptr1 + (16 + x2 + 64 * (-4 + x1)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp14 = tl.where(tmp4, tmp12, tmp13)
    tmp15 = tmp14 * tmp14
    tmp16 = tmp11 + tmp15
    tmp17 = tl.load(in_ptr0 + (32 + x2 + 64 * x1), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp18 = tl.load(in_ptr1 + (32 + x2 + 64 * (-4 + x1)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp19 = tl.where(tmp4, tmp17, tmp18)
    tmp20 = tmp19 * tmp19
    tmp21 = tmp16 + tmp20
    tmp22 = tl.load(in_ptr0 + (48 + x2 + 64 * x1), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp23 = tl.load(in_ptr1 + (48 + x2 + 64 * (-4 + x1)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp24 = tl.where(tmp4, tmp22, tmp23)
    tmp25 = tmp24 * tmp24
    tmp26 = tmp21 + tmp25
    tl.store(out_ptr0 + x3, tmp26, xmask)


@triton.jit
def triton_poi_fused_linalg_vector_norm_1(in_ptr0, in_ptr1, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 8
    x2 = xindex // 64
    x3 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x2 + 64 * x0), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x2 + 64 * (-4 + x0)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp11 = tmp10 * tmp10
    tmp12 = tl.load(in_ptr0 + (16 + x2 + 64 * x0), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp13 = tl.load(in_ptr1 + (16 + x2 + 64 * (-4 + x0)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp14 = tl.where(tmp4, tmp12, tmp13)
    tmp15 = tmp14 * tmp14
    tmp16 = tmp11 + tmp15
    tmp17 = tl.load(in_ptr0 + (32 + x2 + 64 * x0), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp18 = tl.load(in_ptr1 + (32 + x2 + 64 * (-4 + x0)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp19 = tl.where(tmp4, tmp17, tmp18)
    tmp20 = tmp19 * tmp19
    tmp21 = tmp16 + tmp20
    tmp22 = tl.load(in_ptr0 + (48 + x2 + 64 * x0), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp23 = tl.load(in_ptr1 + (48 + x2 + 64 * (-4 + x0)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp24 = tl.where(tmp4, tmp22, tmp23)
    tmp25 = tmp24 * tmp24
    tmp26 = tmp21 + tmp25
    tl.store(out_ptr0 + x3, tmp26, xmask)


@triton.jit
def triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_2(in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x2 = xindex // 32 % 8
    x1 = xindex // 8 % 4
    x3 = xindex // 256
    x0 = xindex % 8
    x4 = xindex // 32
    x5 = xindex
    tmp11 = tl.load(in_ptr2 + (x0 + 8 * x4), None, eviction_policy='evict_last'
        )
    tmp24 = tl.load(in_ptr3 + (x0 + 8 * x4), None, eviction_policy='evict_last'
        )
    tmp0 = x2
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x3 + 16 * x1 + 64 * x2), tmp4,
        eviction_policy='evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x3 + 16 * x1 + 64 * (-4 + x2)), tmp6,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp12 = libdevice.sqrt(tmp11)
    tmp13 = 1.1920928955078125e-07
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp10 / tmp14
    tmp16 = x0
    tmp18 = tmp16 < tmp3
    tmp19 = tl.load(in_ptr0 + (x3 + 16 * x1 + 64 * x0), tmp18,
        eviction_policy='evict_last', other=0.0)
    tmp20 = tmp16 >= tmp3
    tmp22 = tl.load(in_ptr1 + (x3 + 16 * x1 + 64 * (-4 + x0)), tmp20,
        eviction_policy='evict_last', other=0.0)
    tmp23 = tl.where(tmp18, tmp19, tmp22)
    tmp25 = libdevice.sqrt(tmp24)
    tmp26 = triton_helpers.maximum(tmp25, tmp13)
    tmp27 = tmp23 / tmp26
    tmp28 = tmp15 * tmp27
    tl.store(out_ptr0 + x5, tmp28, None)


@triton.jit
def triton_per_fused__log_softmax_mul_repeat_sum_where_3(in_ptr0, out_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 128
    RBLOCK: tl.constexpr = 8
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    x0 = xindex % 8
    r2 = rindex
    x3 = xindex
    tmp7 = tl.load(in_ptr0 + (r2 + 32 * x3), xmask, other=0.0)
    tmp8 = tl.load(in_ptr0 + (8 + r2 + 32 * x3), xmask, other=0.0)
    tmp10 = tl.load(in_ptr0 + (16 + r2 + 32 * x3), xmask, other=0.0)
    tmp12 = tl.load(in_ptr0 + (24 + r2 + 32 * x3), xmask, other=0.0)
    tmp0 = x0
    tmp1 = r2
    tmp2 = tmp0 == tmp1
    tmp3 = 1.0
    tmp4 = 0.0
    tmp5 = tl.where(tmp2, tmp3, tmp4)
    tmp6 = tmp5 != 0
    tmp9 = tmp7 + tmp8
    tmp11 = tmp9 + tmp10
    tmp13 = tmp11 + tmp12
    tmp14 = float('-inf')
    tmp15 = tl.where(tmp6, tmp14, tmp13)
    tmp16 = tmp15 * tmp3
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, float('-inf'))
    tmp20 = triton_helpers.max2(tmp19, 1)[:, None]
    tmp21 = tmp16 - tmp20
    tmp22 = tmp21 * tmp3
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp26 = tl.where(xmask, tmp24, 0)
    tmp27 = tl.sum(tmp26, 1)[:, None]
    tl.store(out_ptr1 + (r2 + 8 * x3), tmp22, xmask)
    tl.store(out_ptr2 + x3, tmp27, xmask)


@triton.jit
def triton_poi_fused_cat_4(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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + (4 + 9 * x0 + 64 * x1), xmask, eviction_policy
        ='evict_last')
    tmp1 = tl.load(in_ptr1 + (x0 + 8 * x1), xmask)
    tmp2 = tl_math.log(tmp1)
    tmp3 = tmp0 - tmp2
    tl.store(out_ptr0 + (x0 + 8 * x1), tmp3, xmask)


@triton.jit
def triton_poi_fused_cat_5(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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + (32 + 9 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (4 + x0 + 8 * x1), xmask)
    tmp2 = tl_math.log(tmp1)
    tmp3 = tmp0 - tmp2
    tl.store(out_ptr0 + (x0 + 8 * x1), tmp3, xmask)


@triton.jit
def triton_per_fused_mean_neg_6(in_out_ptr0, in_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 128
    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 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp4 = 128.0
    tmp5 = tmp3 / tmp4
    tmp6 = -tmp5
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp6, None)


def call(args):
    arg0_1, arg1_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))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 8, 1, 8), (256, 64, 8, 1024, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused_linalg_vector_norm_0[grid(1024)](arg0_1, arg1_1,
            buf0, 1024, XBLOCK=256, num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 8, 1, 8), (256, 64, 8, 1024, 1),
            torch.float32)
        triton_poi_fused_linalg_vector_norm_1[grid(1024)](arg0_1, arg1_1,
            buf1, 1024, XBLOCK=128, num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 8, 4, 8), (1024, 256, 32, 8, 1),
            torch.float32)
        triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_2[grid(4096)](
            arg0_1, arg1_1, buf0, buf1, buf2, 4096, XBLOCK=128, num_warps=4,
            num_stages=1)
        del arg0_1
        del arg1_1
        del buf0
        buf4 = reinterpret_tensor(buf1, (4, 4, 8, 8), (256, 64, 8, 1), 0)
        del buf1
        buf5 = empty_strided_cuda((4, 4, 8, 1), (32, 8, 1, 128), torch.float32)
        triton_per_fused__log_softmax_mul_repeat_sum_where_3[grid(128)](buf2,
            buf4, buf5, 128, 8, XBLOCK=8, num_warps=2, num_stages=1)
        del buf2
        buf8 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32)
        buf6 = reinterpret_tensor(buf8, (4, 4, 4), (32, 8, 1), 0)
        triton_poi_fused_cat_4[grid(64)](buf4, buf5, buf6, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf7 = reinterpret_tensor(buf8, (4, 4, 4), (32, 8, 1), 4)
        triton_poi_fused_cat_5[grid(64)](buf4, buf5, buf7, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf4
        del buf5
        buf9 = empty_strided_cuda((), (), torch.float32)
        buf10 = buf9
        del buf9
        triton_per_fused_mean_neg_6[grid(1)](buf10, buf8, 1, 128, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf6
        del buf7
        del buf8
    return buf10,


class ContrastiveLossNew(Module):
    """A contrastive loss adapted from SimCLR.

    Link to SimCLR: https://arxiv.org/abs/2002.05709v3.
    """

    def __init__(self, temperature: 'float'=1.0):
        """Save hyper-params."""
        super().__init__()
        self.temperature = temperature
        self._log_softmax_fn = LogSoftmax(dim=-1)

    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.02448