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
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_poi_fused_add_gather_max_repeat_0(in_ptr0, in_ptr1, out_ptr0,
    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
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp9 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp20 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp1 = libdevice.trunc(tmp0)
    tmp2 = tmp1.to(tl.int64)
    tmp3 = tl.full([XBLOCK], 4, tl.int32)
    tmp4 = tmp2 + tmp3
    tmp5 = tmp2 < 0
    tmp6 = tl.where(tmp5, tmp4, tmp2)
    tl.device_assert((0 <= tmp6) & (tmp6 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp6 < 4')
    tmp8 = tl.load(in_ptr1 + 4 * tmp6, xmask, eviction_policy='evict_last')
    tmp10 = tmp8 + tmp9
    tmp11 = tl.load(in_ptr1 + (1 + 4 * tmp6), xmask, eviction_policy=
        'evict_last')
    tmp13 = tmp11 + tmp12
    tmp14 = triton_helpers.maximum(tmp10, tmp13)
    tmp15 = tl.load(in_ptr1 + (2 + 4 * tmp6), xmask, eviction_policy=
        'evict_last')
    tmp17 = tmp15 + tmp16
    tmp18 = triton_helpers.maximum(tmp14, tmp17)
    tmp19 = tl.load(in_ptr1 + (3 + 4 * tmp6), xmask, eviction_policy=
        'evict_last')
    tmp21 = tmp19 + tmp20
    tmp22 = triton_helpers.maximum(tmp18, tmp21)
    tl.store(out_ptr0 + x2, tmp22, xmask)


@triton.jit
def triton_per_fused_max_min_sum_1(in_ptr0, out_ptr0, out_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 + r0, None)
    tmp1 = tl.load(in_ptr0 + (4 + r0), None)
    tmp3 = tl.load(in_ptr0 + (8 + r0), None)
    tmp5 = tl.load(in_ptr0 + (12 + r0), None)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = triton_helpers.max2(tmp7, 1)[:, None]
    tmp11 = triton_helpers.min2(tmp7, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp9, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp11, None)


@triton.jit
def triton_per_fused_add_div_mean_mul_pow_rsub_sub_2(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, out_ptr0, out_ptr1, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    RBLOCK: tl.constexpr = 16
    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 % 4
    r1 = rindex // 4
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + (r1 + 4 * r0), None, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + r2, None)
    tmp11 = tl.load(in_ptr2 + 0)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK, 1])
    tmp13 = tl.load(in_ptr3 + 0)
    tmp14 = tl.broadcast_to(tmp13, [XBLOCK, 1])
    tmp2 = tmp0 * tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.sum(tmp3, 1)[:, None]
    tmp6 = 16.0
    tmp7 = tmp5 / tmp6
    tmp8 = 2.0
    tmp9 = tmp8 - tmp7
    tmp10 = tmp9 * tmp9
    tmp15 = tmp12 - tmp14
    tmp16 = 1.0
    tmp17 = tmp15 * tmp16
    tmp18 = tmp17 + tmp10
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp10, None)
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp18, None)
    tl.store(out_ptr1 + tl.full([XBLOCK, 1], 0, tl.int32), tmp17, None)


@triton.jit
def triton_poi_fused_clamp_div_mean_rsub_3(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')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 4.0
    tmp8 = tmp6 / tmp7
    tmp9 = 2.0
    tmp10 = tmp9 - tmp8
    tmp11 = 0.5
    tmp12 = tmp10 * tmp11
    tmp13 = triton_helpers.maximum(tmp12, tmp11)
    tl.store(out_ptr0 + x0, tmp13, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    assert_size_stride(arg2_1, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (1, 4), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_gather_max_repeat_0[grid(16)](arg0_1, arg1_1,
            buf0, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del arg0_1
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_max_min_sum_1[grid(1)](arg1_1, buf1, buf2, 1, 4,
            XBLOCK=1, num_warps=2, num_stages=1)
        del arg1_1
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf4 = buf3
        del buf3
        buf6 = empty_strided_cuda((), (), torch.float32)
        buf7 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_add_div_mean_mul_pow_rsub_sub_2[grid(1)](buf4,
            buf0, arg2_1, buf1, buf2, buf6, buf7, 1, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        del arg2_1
        del buf1
        del buf2
        buf5 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_poi_fused_clamp_div_mean_rsub_3[grid(4)](buf0, buf5, 4,
            XBLOCK=4, num_warps=1, num_stages=1)
        del buf0
    return buf6, buf4, buf7, buf5


class MyLossNew(nn.Module):

    def __init__(self):
        super(MyLossNew, self).__init__()
        None
        self.reduce_var = True
    pass
    """
    weights has shape (n), multiply loss of point i with weights[i]
    """
    pass

    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], output[1], output[2], output[3]


Measured latency_ms: 0.06135