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
import torch.nn as nn
import torch.nn.init
import torch.utils.data
import torch.utils.data.distributed
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_mul_sum_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 48
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x2 = xindex // 12
    x1 = xindex // 4 % 3
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr0 + (x0 + 16 * x1 + 64 * x2), xmask)
    tmp3 = tl.load(in_ptr0 + (52 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (4 + x0 + 16 * x1 + 64 * x2), xmask)
    tmp7 = tl.load(in_ptr0 + (56 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp8 = tl.load(in_ptr0 + (8 + x0 + 16 * x1 + 64 * x2), xmask)
    tmp11 = tl.load(in_ptr0 + (60 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp12 = tl.load(in_ptr0 + (12 + x0 + 16 * x1 + 64 * x2), xmask)
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tl.store(out_ptr0 + x3, tmp14, xmask)


@triton.jit
def triton_per_fused_add_clamp_mean_sub_1(in_out_ptr0, in_ptr0, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 32
    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)
    r2 = rindex // 8
    r3 = rindex % 8
    r0 = rindex % 4
    tmp0 = tl.load(in_ptr0 + (r3 + 12 * r2), None)
    tmp3 = tl.load(in_ptr0 + (8 + r0 + 12 * r2), None, eviction_policy=
        'evict_last')
    tmp1 = 0.0
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 - tmp3
    tmp5 = triton_helpers.maximum(tmp4, tmp1)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tmp9 = 32.0
    tmp10 = tmp8 / tmp9
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp10, None)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 3, 4), (12, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_sum_0[grid(48)](arg0_1, buf0, 48, XBLOCK=64,
            num_warps=1, num_stages=1)
        del arg0_1
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = buf1
        del buf1
        triton_per_fused_add_clamp_mean_sub_1[grid(1)](buf2, buf0, 1, 32,
            XBLOCK=1, num_warps=2, num_stages=1)
        del buf0
    return buf2,


def cosine_sim(im, s):
    return im.mm(s.t())


class ContrastiveLossNew(nn.Module):

    def __init__(self, margin=0):
        super(ContrastiveLossNew, self).__init__()
        self.margin = margin
        self.sim = cosine_sim

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


Measured latency_ms: 0.026218