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 math as tl_math
import 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_pow_sort_sub_sum_0(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 4
    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)
    r2 = rindex
    x1 = xindex // 4
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (4 * r2 + 16 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp1 = tl.load(in_ptr1 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (1 + 4 * r2 + 16 * x1), xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp5 = tl.load(in_ptr1 + (1 + 4 * x3), xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr0 + (2 + 4 * r2 + 16 * x1), xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp10 = tl.load(in_ptr1 + (2 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp14 = tl.load(in_ptr0 + (3 + 4 * r2 + 16 * x1), xmask,
        eviction_policy='evict_last', other=0.0)
    tmp15 = tl.load(in_ptr1 + (3 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 - tmp1
    tmp3 = tmp2 * tmp2
    tmp6 = tmp4 - tmp5
    tmp7 = tmp6 * tmp6
    tmp8 = tmp3 + tmp7
    tmp11 = tmp9 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tmp8 + tmp12
    tmp16 = tmp14 - tmp15
    tmp17 = tmp16 * tmp16
    tmp18 = tmp13 + tmp17
    tmp19 = r2
    tmp20 = tmp19.to(tl.int16)
    tmp21 = tl.broadcast_to(tmp18, [XBLOCK, RBLOCK])
    tmp22 = tl.broadcast_to(tmp20, [XBLOCK, RBLOCK])
    _tmp23, tmp24 = triton_helpers.sort_with_index(tmp21, tmp22, None, 1,
        stable=False, descending=False)
    tl.store(out_ptr0 + (r2 + 4 * x3), tmp18, xmask)
    tl.store(out_ptr1 + (r2 + 4 * x3), tmp24, xmask)


@triton.jit
def triton_poi_fused_gather_mul_sub_sum_1(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex // 4
    x4 = xindex
    x2 = xindex // 16
    tmp0 = tl.load(in_ptr0 + 4 * x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x4, xmask)
    tmp12 = tl.load(in_ptr0 + (1 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp18 = tl.load(in_ptr0 + (2 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp24 = tl.load(in_ptr0 + (3 + 4 * x3), xmask, eviction_policy='evict_last'
        )
    tmp30 = tl.load(in_ptr1 + 4 * x3, xmask, eviction_policy='evict_last')
    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_ptr2 + (4 * tmp6 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp9 = tmp0 - tmp8
    tmp10 = tl.load(in_ptr3 + (4 * tmp6 + 16 * x2), xmask, eviction_policy=
        'evict_last')
    tmp11 = tmp9 * tmp10
    tmp13 = tl.load(in_ptr2 + (1 + 4 * tmp6 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp14 = tmp12 - tmp13
    tmp15 = tl.load(in_ptr3 + (1 + 4 * tmp6 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp16 = tmp14 * tmp15
    tmp17 = tmp11 + tmp16
    tmp19 = tl.load(in_ptr2 + (2 + 4 * tmp6 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp20 = tmp18 - tmp19
    tmp21 = tl.load(in_ptr3 + (2 + 4 * tmp6 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp22 = tmp20 * tmp21
    tmp23 = tmp17 + tmp22
    tmp25 = tl.load(in_ptr2 + (3 + 4 * tmp6 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp26 = tmp24 - tmp25
    tmp27 = tl.load(in_ptr3 + (3 + 4 * tmp6 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp28 = tmp26 * tmp27
    tmp29 = tmp23 + tmp28
    tmp31 = tmp30.to(tl.int64)
    tmp32 = tmp31 + tmp3
    tmp33 = tmp31 < 0
    tmp34 = tl.where(tmp33, tmp32, tmp31)
    tl.device_assert((0 <= tmp34) & (tmp34 < 4) | ~xmask,
        'index out of bounds: 0 <= tmp34 < 4')
    tmp36 = tl.load(in_ptr3 + (4 * tmp34 + 16 * x2), xmask, eviction_policy
        ='evict_last')
    tmp37 = tmp10 * tmp36
    tmp38 = tl.load(in_ptr3 + (1 + 4 * tmp34 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp39 = tmp15 * tmp38
    tmp40 = tmp37 + tmp39
    tmp41 = tl.load(in_ptr3 + (2 + 4 * tmp34 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp42 = tmp21 * tmp41
    tmp43 = tmp40 + tmp42
    tmp44 = tl.load(in_ptr3 + (3 + 4 * tmp34 + 16 * x2), xmask,
        eviction_policy='evict_last')
    tmp45 = tmp27 * tmp44
    tmp46 = tmp43 + tmp45
    tl.store(out_ptr0 + x4, tmp29, xmask)
    tl.store(out_ptr1 + x4, tmp46, xmask)


@triton.jit
def triton_per_fused_abs_div_exp_gather_mul_neg_rsub_sum_2(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, out_ptr2, 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
    tmp0 = tl.load(in_ptr0 + 4 * r0, None, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr1 + 4 * r0, None, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr3 + 4 * r0, None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr1 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp35 = tl.load(in_ptr3 + (1 + 4 * r0), None, eviction_policy='evict_last')
    tmp43 = tl.load(in_ptr0 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp45 = tl.load(in_ptr1 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp55 = tl.load(in_ptr3 + (2 + 4 * r0), None, eviction_policy='evict_last')
    tmp63 = tl.load(in_ptr0 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp65 = tl.load(in_ptr1 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp75 = tl.load(in_ptr3 + (3 + 4 * r0), None, eviction_policy='evict_last')
    tmp1 = tl_math.abs(tmp0)
    tmp3 = tmp2.to(tl.int64)
    tmp4 = tl.full([XBLOCK, RBLOCK], 4, tl.int32)
    tmp5 = tmp3 + tmp4
    tmp6 = tmp3 < 0
    tmp7 = tl.where(tmp6, tmp5, tmp3)
    tl.device_assert((0 <= tmp7) & (tmp7 < 4),
        'index out of bounds: 0 <= tmp7 < 4')
    tmp9 = tl.load(in_ptr2 + (tmp7 + 4 * r0), None, eviction_policy=
        'evict_last')
    tmp10 = -tmp9
    tmp11 = 1111.111111111111
    tmp12 = tmp10 * tmp11
    tmp13 = tl_math.exp(tmp12)
    tmp15 = 1.0
    tmp16 = tmp15 - tmp14
    tmp17 = -tmp16
    tmp18 = 29.34774027049054
    tmp19 = tmp17 * tmp18
    tmp20 = tl_math.exp(tmp19)
    tmp21 = tmp13 * tmp20
    tmp22 = tmp1 * tmp21
    tmp24 = tl_math.abs(tmp23)
    tmp26 = tmp25.to(tl.int64)
    tmp27 = tmp26 + tmp4
    tmp28 = tmp26 < 0
    tmp29 = tl.where(tmp28, tmp27, tmp26)
    tl.device_assert((0 <= tmp29) & (tmp29 < 4),
        'index out of bounds: 0 <= tmp29 < 4')
    tmp31 = tl.load(in_ptr2 + (tmp29 + 4 * r0), None, eviction_policy=
        'evict_last')
    tmp32 = -tmp31
    tmp33 = tmp32 * tmp11
    tmp34 = tl_math.exp(tmp33)
    tmp36 = tmp15 - tmp35
    tmp37 = -tmp36
    tmp38 = tmp37 * tmp18
    tmp39 = tl_math.exp(tmp38)
    tmp40 = tmp34 * tmp39
    tmp41 = tmp24 * tmp40
    tmp42 = tmp22 + tmp41
    tmp44 = tl_math.abs(tmp43)
    tmp46 = tmp45.to(tl.int64)
    tmp47 = tmp46 + tmp4
    tmp48 = tmp46 < 0
    tmp49 = tl.where(tmp48, tmp47, tmp46)
    tl.device_assert((0 <= tmp49) & (tmp49 < 4),
        'index out of bounds: 0 <= tmp49 < 4')
    tmp51 = tl.load(in_ptr2 + (tmp49 + 4 * r0), None, eviction_policy=
        'evict_last')
    tmp52 = -tmp51
    tmp53 = tmp52 * tmp11
    tmp54 = tl_math.exp(tmp53)
    tmp56 = tmp15 - tmp55
    tmp57 = -tmp56
    tmp58 = tmp57 * tmp18
    tmp59 = tl_math.exp(tmp58)
    tmp60 = tmp54 * tmp59
    tmp61 = tmp44 * tmp60
    tmp62 = tmp42 + tmp61
    tmp64 = tl_math.abs(tmp63)
    tmp66 = tmp65.to(tl.int64)
    tmp67 = tmp66 + tmp4
    tmp68 = tmp66 < 0
    tmp69 = tl.where(tmp68, tmp67, tmp66)
    tl.device_assert((0 <= tmp69) & (tmp69 < 4),
        'index out of bounds: 0 <= tmp69 < 4')
    tmp71 = tl.load(in_ptr2 + (tmp69 + 4 * r0), None, eviction_policy=
        'evict_last')
    tmp72 = -tmp71
    tmp73 = tmp72 * tmp11
    tmp74 = tl_math.exp(tmp73)
    tmp76 = tmp15 - tmp75
    tmp77 = -tmp76
    tmp78 = tmp77 * tmp18
    tmp79 = tl_math.exp(tmp78)
    tmp80 = tmp74 * tmp79
    tmp81 = tmp64 * tmp80
    tmp82 = tmp62 + tmp81
    tmp83 = tmp21 + tmp40
    tmp84 = tmp83 + tmp60
    tmp85 = tmp84 + tmp80
    tmp86 = tmp82 / tmp85
    tmp87 = tl.broadcast_to(tmp86, [XBLOCK, RBLOCK])
    tmp89 = tl.sum(tmp87, 1)[:, None]
    tl.store(out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp89, None)


def call(args):
    arg0_1, arg1_1, arg2_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(arg2_1, (4, 4, 4), (16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.int16)
        get_raw_stream(0)
        triton_per_fused_pow_sort_sub_sum_0[grid(16)](arg0_1, arg1_1, buf0,
            buf2, 16, 4, XBLOCK=8, num_warps=2, num_stages=1)
        buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_gather_mul_sub_sum_1[grid(64)](arg1_1, buf2,
            arg0_1, arg2_1, buf3, buf4, 64, XBLOCK=64, num_warps=1,
            num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
        buf7 = empty_strided_cuda((), (), torch.float32)
        triton_per_fused_abs_div_exp_gather_mul_neg_rsub_sum_2[grid(1)](buf3,
            buf2, buf0, buf4, buf7, 1, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del buf0
        del buf2
        del buf3
        del buf4
    return buf7,


def get_knn_idx_dist(pos: 'torch.FloatTensor', query: 'torch.FloatTensor',
    k, offset=0):
    """
    :param  pos:     (B, N, F)
    :param  query:   (B, M, F)
    :return knn_idx: (B, M, k)
    """
    B, N, F = tuple(pos.size())
    M = query.size(1)
    pos = pos.unsqueeze(1).expand(B, M, N, F)
    query = query.unsqueeze(2).expand(B, M, N, F)
    dist = torch.sum((pos - query) ** 2, dim=3, keepdim=False)
    knn_idx = torch.argsort(dist, dim=2)[:, :, offset:k + offset]
    knn_dist = torch.gather(dist, dim=2, index=knn_idx)
    return knn_idx, knn_dist


def group(x: 'torch.FloatTensor', idx: 'torch.LongTensor'):
    """
    :param  x:      (B, N, F)
    :param  idx:    (B, M, k)
    :return (B, M, k, F)
    """
    B, N, F = tuple(x.size())
    _, M, k = tuple(idx.size())
    x = x.unsqueeze(1).expand(B, M, N, F)
    idx = idx.unsqueeze(3).expand(B, M, k, F)
    return torch.gather(x, dim=2, index=idx)


class ProjectionLossNew(nn.Module):

    def __init__(self, knn=8, sigma_p=0.03, sigma_n=math.radians(15)):
        super().__init__()
        self.sigma_p = sigma_p
        self.sigma_n = sigma_n
        self.knn = knn

    def distance_weight(self, dist):
        """
        :param  dist: (B, N, k), Squared L2 distance
        :return (B, N, k)
        """
        return torch.exp(-dist / self.sigma_p ** 2)

    def angle_weight(self, nb_normals):
        """
        :param  nb_normals: (B, N, k, 3), Normals of neighboring points 
        :return (B, N, k)
        """
        estm_normal = nb_normals[:, :, 0:1, :]
        inner_prod = (nb_normals * estm_normal.expand_as(nb_normals)).sum(dim
            =-1)
        return torch.exp(-(1 - inner_prod) / (1 - math.cos(self.sigma_n)))

    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]


Measured latency_ms: 0.029721