import torch
from torch._inductor.select_algorithm import extern_kernels
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
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_mul_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 36
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 3
    x1 = xindex // 3 % 3
    x2 = xindex // 9
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x1 + 16 * x2), xmask)
    tmp1 = -1.0
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x3, tmp2, xmask)


@triton.jit
def triton_poi_fused_add_copy_fill_lift_fresh_new_zeros_1(in_ptr0, in_ptr1,
    out_ptr0, ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 16
    xnumel = 4
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    x2 = xindex
    y0 = yindex % 4
    y1 = yindex // 4
    y3 = yindex
    tmp0 = x2
    tmp1 = tl.full([1, 1], 3, tl.int32)
    tmp2 = tmp0 == tmp1
    tmp3 = y0
    tmp4 = tmp3 == tmp1
    tmp5 = tl.full([1, 1], 3, tl.int64)
    tmp6 = tmp5 < tmp5
    tmp7 = tl.broadcast_to(y0, [XBLOCK, YBLOCK])
    tmp8 = tmp7 >= tmp5
    tmp9 = tmp8 & tmp6
    tmp10 = tl.load(in_ptr0 + tl.broadcast_to(3 + 3 * y1, [XBLOCK, YBLOCK]),
        tmp9 & xmask & ymask, eviction_policy='evict_last', other=0.0)
    tmp11 = tmp6 & tmp6
    tmp12 = tmp7 < tmp5
    tmp13 = tmp12 & tmp11
    tmp14 = tl.load(in_ptr1 + tl.broadcast_to(3 + 4 * y3, [XBLOCK, YBLOCK]),
        tmp13 & xmask & ymask, eviction_policy='evict_last', other=0.0)
    tmp15 = 1e-06
    tmp16 = tl.where(tmp12, tmp14, tmp15)
    tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype)
    tmp18 = tl.where(tmp11, tmp16, tmp17)
    tmp19 = tl.where(tmp6, tmp18, tmp15)
    tmp20 = tl.where(tmp8, tmp10, tmp19)
    tmp21 = tl.full(tmp20.shape, 0.0, tmp20.dtype)
    tmp22 = tl.where(tmp6, tmp20, tmp21)
    tmp23 = tmp12 & tmp6
    tmp24 = tl.load(in_ptr1 + tl.broadcast_to(3 + 4 * y3, [XBLOCK, YBLOCK]),
        tmp23 & xmask & ymask, eviction_policy='evict_last', other=0.0)
    tmp25 = tl.where(tmp12, tmp24, tmp15)
    tmp26 = tl.full(tmp25.shape, 0.0, tmp25.dtype)
    tmp27 = tl.where(tmp6, tmp25, tmp26)
    tmp28 = tl.where(tmp6, tmp27, tmp15)
    tmp29 = tl.where(tmp6, tmp22, tmp28)
    tmp30 = 1.0
    tmp31 = tl.where(tmp4, tmp30, tmp29)
    tmp32 = tmp0 < tmp5
    tmp33 = tmp8 & tmp32
    tmp34 = tl.load(in_ptr0 + (x2 + 3 * y1), tmp33 & xmask & ymask,
        eviction_policy='evict_last', other=0.0)
    tmp35 = tl.broadcast_to(x2, [XBLOCK, YBLOCK])
    tmp36 = tmp35 < tmp5
    tmp37 = tmp36 & tmp32
    tmp38 = tmp12 & tmp37
    tmp39 = tl.load(in_ptr1 + (x2 + 4 * y3), tmp38 & xmask & ymask,
        eviction_policy='evict_last', other=0.0)
    tmp40 = tl.where(tmp12, tmp39, tmp15)
    tmp41 = tl.full(tmp40.shape, 0.0, tmp40.dtype)
    tmp42 = tl.where(tmp37, tmp40, tmp41)
    tmp43 = tl.where(tmp36, tmp42, tmp15)
    tmp44 = tl.where(tmp8, tmp34, tmp43)
    tmp45 = tl.full(tmp44.shape, 0.0, tmp44.dtype)
    tmp46 = tl.where(tmp32, tmp44, tmp45)
    tmp47 = tmp12 & tmp32
    tmp48 = tl.load(in_ptr1 + (x2 + 4 * y3), tmp47 & xmask & ymask,
        eviction_policy='evict_last', other=0.0)
    tmp49 = tl.where(tmp12, tmp48, tmp15)
    tmp50 = tl.full(tmp49.shape, 0.0, tmp49.dtype)
    tmp51 = tl.where(tmp32, tmp49, tmp50)
    tmp52 = tl.where(tmp32, tmp51, tmp15)
    tmp53 = tl.where(tmp32, tmp46, tmp52)
    tmp54 = tl.where(tmp2, tmp31, tmp53)
    tl.store(out_ptr0 + (y0 + 4 * x2 + 16 * y1), tmp54, xmask & ymask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4), (16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 3, 3), (9, 1, 3), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_0[grid(36)](arg0_1, buf0, 36, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 3, 1), (3, 1, 1), torch.float32)
        extern_kernels.bmm(buf0, reinterpret_tensor(arg0_1, (4, 3, 1), (16,
            4, 1), 3), out=buf1)
        del buf0
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_add_copy_fill_lift_fresh_new_zeros_1[grid(16, 4)](buf1
            , arg0_1, buf2, 16, 4, XBLOCK=4, YBLOCK=8, num_warps=1,
            num_stages=1)
        del arg0_1
        del buf1
    return buf2,


def inverse_pose(pose, eps=1e-06):
    """Function that inverts a 4x4 pose.

    Args:
        points (Tensor): tensor with poses.

    Returns:
        Tensor: tensor with inverted poses.

    Shape:
        - Input: :math:`(N, 4, 4)`
        - Output: :math:`(N, 4, 4)`

    Example:
        >>> pose = torch.rand(1, 4, 4)         # Nx4x4
        >>> pose_inv = tgm.inverse_pose(pose)  # Nx4x4
    """
    if not torch.is_tensor(pose):
        raise TypeError('Input type is not a torch.Tensor. Got {}'.format(
            type(pose)))
    if not len(pose.shape) == 3 and pose.shape[-2:] == (4, 4):
        raise ValueError('Input size must be a Nx4x4 tensor. Got {}'.format
            (pose.shape))
    r_mat = pose[..., :3, 0:3]
    t_vec = pose[..., :3, 3:4]
    r_mat_trans = torch.transpose(r_mat, 1, 2)
    pose_inv = pose.new_zeros(pose.shape) + eps
    pose_inv[..., :3, 0:3] = r_mat_trans
    pose_inv[..., :3, 3:4] = torch.matmul(-1.0 * r_mat_trans, t_vec)
    pose_inv[..., 3, 3] = 1.0
    return pose_inv


class InversePoseNew(nn.Module):
    """Creates a transformation that inverts a 4x4 pose.

    Args:
        points (Tensor): tensor with poses.

    Returns:
        Tensor: tensor with inverted poses.

    Shape:
        - Input: :math:`(N, 4, 4)`
        - Output: :math:`(N, 4, 4)`

    Example:
        >>> pose = torch.rand(1, 4, 4)  # Nx4x4
        >>> transform = tgm.InversePose()
        >>> pose_inv = transform(pose)  # Nx4x4
    """

    def __init__(self):
        super(InversePoseNew, self).__init__()

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


Measured latency_ms: 0.03494