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 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_abs_clone_mul_sub_sum_0(in_ptr0, in_ptr1, out_ptr0,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    rnumel = 120
    RBLOCK: tl.constexpr = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    rmask = rindex < rnumel
    r2 = rindex // 3
    x0 = xindex
    r3 = rindex
    r1 = rindex % 3
    tmp7 = tl.load(in_ptr0 + (3 + 4 * r2 + 160 * x0), rmask & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp9 = tl.load(in_ptr1 + (r3 + 120 * x0), rmask & xmask, other=0.0)
    tmp10 = tl.load(in_ptr1 + (117 + r1 + 120 * x0), rmask & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp19 = tl.load(in_ptr0 + (r1 + 4 * r2 + 160 * x0), rmask & xmask,
        other=0.0)
    tmp0 = tl.full([1, 1], 3, tl.int64)
    tmp1 = tmp0 < tmp0
    tmp2 = tl.load(in_ptr0 + (3 + 4 * r2 + 160 * x0), rmask & tmp1 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp3 = tl.load(in_ptr0 + tl.broadcast_to(159 + 160 * x0, [XBLOCK,
        RBLOCK]), rmask & tmp1 & xmask, eviction_policy='evict_last', other=0.0
        )
    tmp4 = tmp2 - tmp3
    tmp5 = tl.full(tmp4.shape, 0.0, tmp4.dtype)
    tmp6 = tl.where(tmp1, tmp4, tmp5)
    tmp8 = tl.where(tmp1, tmp6, tmp7)
    tmp11 = tmp9 - tmp10
    tmp12 = r1
    tmp13 = tmp12 < tmp0
    tmp14 = tl.load(in_ptr0 + (r1 + 4 * r2 + 160 * x0), rmask & tmp13 &
        xmask, other=0.0)
    tmp15 = tl.load(in_ptr0 + (156 + r1 + 160 * x0), rmask & tmp13 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp16 = tmp14 - tmp15
    tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype)
    tmp18 = tl.where(tmp13, tmp16, tmp17)
    tmp20 = tl.where(tmp13, tmp18, tmp19)
    tmp21 = tmp11 - tmp20
    tmp22 = tl_math.abs(tmp21)
    tmp23 = tmp8 * tmp22
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp26 = tl.where(rmask & xmask, tmp24, 0)
    tmp27 = tl.sum(tmp26, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp27, xmask)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 40, 3), (480, 120, 3, 1))
    assert_size_stride(arg1_1, (4, 4, 40, 4), (640, 160, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_abs_clone_mul_sub_sum_0[grid(16)](arg1_1, arg0_1,
            buf0, 16, 120, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
    return buf0,


class Keypoint3DLossNew(nn.Module):

    def __init__(self, loss_type: 'str'='l1'):
        """
        3D keypoint loss module.
        Args:
            loss_type (str): Choose between l1 and l2 losses.
        """
        super(Keypoint3DLossNew, self).__init__()
        if loss_type == 'l1':
            self.loss_fn = nn.L1Loss(reduction='none')
        elif loss_type == 'l2':
            self.loss_fn = nn.MSELoss(reduction='none')
        else:
            raise NotImplementedError('Unsupported loss function')

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