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 typing import Optional
from typing import Tuple
import torch.nn as nn
from typing import Union
from torch.nn.modules.utils import _pair
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_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x0 = xindex % 16
    x1 = xindex // 16 % 16
    x2 = xindex // 256 % 4
    x3 = xindex // 1024
    x4 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = x1
    tmp7 = tmp5 < tmp3
    tmp8 = tmp7 & tmp4
    tmp9 = tl.load(in_ptr0 + (4 * x1 + 16 * x2 + 1024 * x3 + x0), tmp8,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tmp5 >= tmp3
    tmp11 = tl.full([1], 8, tl.int64)
    tmp12 = tmp5 < tmp11
    tmp13 = tmp10 & tmp12
    tmp14 = tmp13 & tmp4
    tmp15 = tl.load(in_ptr0 + (256 + 4 * (-4 + x1) + 16 * x2 + 1024 * x3 +
        x0), tmp14, eviction_policy='evict_last', other=0.0)
    tmp16 = tmp5 >= tmp11
    tmp17 = tl.full([1], 12, tl.int64)
    tmp18 = tmp5 < tmp17
    tmp19 = tmp16 & tmp18
    tmp20 = tmp19 & tmp4
    tmp21 = tl.load(in_ptr0 + (512 + 4 * (-8 + x1) + 16 * x2 + 1024 * x3 +
        x0), tmp20, eviction_policy='evict_last', other=0.0)
    tmp22 = tmp5 >= tmp17
    tl.full([1], 16, tl.int64)
    tmp25 = tmp22 & tmp4
    tmp26 = tl.load(in_ptr0 + (768 + 4 * (-12 + x1) + 16 * x2 + 1024 * x3 +
        x0), tmp25, eviction_policy='evict_last', other=0.0)
    tmp27 = tl.where(tmp19, tmp21, tmp26)
    tmp28 = tl.where(tmp13, tmp15, tmp27)
    tmp29 = tl.where(tmp7, tmp9, tmp28)
    tmp30 = tl.full(tmp29.shape, 0.0, tmp29.dtype)
    tmp31 = tl.where(tmp4, tmp29, tmp30)
    tmp32 = tmp0 >= tmp3
    tmp33 = tmp0 < tmp11
    tmp34 = tmp32 & tmp33
    tmp35 = tmp7 & tmp34
    tmp36 = tl.load(in_ptr0 + (64 + 4 * x1 + 16 * x2 + 1024 * x3 + (-4 + x0
        )), tmp35, eviction_policy='evict_last', other=0.0)
    tmp37 = tmp13 & tmp34
    tmp38 = tl.load(in_ptr0 + (320 + 4 * (-4 + x1) + 16 * x2 + 1024 * x3 +
        (-4 + x0)), tmp37, eviction_policy='evict_last', other=0.0)
    tmp39 = tmp19 & tmp34
    tmp40 = tl.load(in_ptr0 + (576 + 4 * (-8 + x1) + 16 * x2 + 1024 * x3 +
        (-4 + x0)), tmp39, eviction_policy='evict_last', other=0.0)
    tmp41 = tmp22 & tmp34
    tmp42 = tl.load(in_ptr0 + (832 + 4 * (-12 + x1) + 16 * x2 + 1024 * x3 +
        (-4 + x0)), tmp41, eviction_policy='evict_last', other=0.0)
    tmp43 = tl.where(tmp19, tmp40, tmp42)
    tmp44 = tl.where(tmp13, tmp38, tmp43)
    tmp45 = tl.where(tmp7, tmp36, tmp44)
    tmp46 = tl.full(tmp45.shape, 0.0, tmp45.dtype)
    tmp47 = tl.where(tmp34, tmp45, tmp46)
    tmp48 = tmp0 >= tmp11
    tmp49 = tmp0 < tmp17
    tmp50 = tmp48 & tmp49
    tmp51 = tmp7 & tmp50
    tmp52 = tl.load(in_ptr0 + (128 + 4 * x1 + 16 * x2 + 1024 * x3 + (-8 +
        x0)), tmp51, eviction_policy='evict_last', other=0.0)
    tmp53 = tmp13 & tmp50
    tmp54 = tl.load(in_ptr0 + (384 + 4 * (-4 + x1) + 16 * x2 + 1024 * x3 +
        (-8 + x0)), tmp53, eviction_policy='evict_last', other=0.0)
    tmp55 = tmp19 & tmp50
    tmp56 = tl.load(in_ptr0 + (640 + 4 * (-8 + x1) + 16 * x2 + 1024 * x3 +
        (-8 + x0)), tmp55, eviction_policy='evict_last', other=0.0)
    tmp57 = tmp22 & tmp50
    tmp58 = tl.load(in_ptr0 + (896 + 4 * (-12 + x1) + 16 * x2 + 1024 * x3 +
        (-8 + x0)), tmp57, eviction_policy='evict_last', other=0.0)
    tmp59 = tl.where(tmp19, tmp56, tmp58)
    tmp60 = tl.where(tmp13, tmp54, tmp59)
    tmp61 = tl.where(tmp7, tmp52, tmp60)
    tmp62 = tl.full(tmp61.shape, 0.0, tmp61.dtype)
    tmp63 = tl.where(tmp50, tmp61, tmp62)
    tmp64 = tmp0 >= tmp17
    tmp66 = tmp7 & tmp64
    tmp67 = tl.load(in_ptr0 + (192 + 4 * x1 + 16 * x2 + 1024 * x3 + (-12 +
        x0)), tmp66, eviction_policy='evict_last', other=0.0)
    tmp68 = tmp13 & tmp64
    tmp69 = tl.load(in_ptr0 + (448 + 4 * (-4 + x1) + 16 * x2 + 1024 * x3 +
        (-12 + x0)), tmp68, eviction_policy='evict_last', other=0.0)
    tmp70 = tmp19 & tmp64
    tmp71 = tl.load(in_ptr0 + (704 + 4 * (-8 + x1) + 16 * x2 + 1024 * x3 +
        (-12 + x0)), tmp70, eviction_policy='evict_last', other=0.0)
    tmp72 = tmp22 & tmp64
    tmp73 = tl.load(in_ptr0 + (960 + 4 * (-12 + x1) + 16 * x2 + 1024 * x3 +
        (-12 + x0)), tmp72, eviction_policy='evict_last', other=0.0)
    tmp74 = tl.where(tmp19, tmp71, tmp73)
    tmp75 = tl.where(tmp13, tmp69, tmp74)
    tmp76 = tl.where(tmp7, tmp67, tmp75)
    tmp77 = tl.full(tmp76.shape, 0.0, tmp76.dtype)
    tmp78 = tl.where(tmp64, tmp76, tmp77)
    tmp79 = tl.where(tmp50, tmp63, tmp78)
    tmp80 = tl.where(tmp34, tmp47, tmp79)
    tmp81 = tl.where(tmp4, tmp31, tmp80)
    tl.store(out_ptr0 + x4, tmp81, None)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4, 4, 4), (1024, 256, 64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 1, 4, 16, 16), (1024, 1024, 256, 16, 
            1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(4096)](arg0_1, buf0, 4096, XBLOCK=128,
            num_warps=4, num_stages=1)
        del arg0_1
    return reinterpret_tensor(buf0, (4, 4, 16, 16), (1024, 256, 16, 1), 0),


def combine_tensor_patches(patches: 'torch.Tensor', window_size:
    'Tuple[int, int]'=(4, 4), stride: 'Tuple[int, int]'=(4, 4), unpadding:
    'Optional[Tuple[int, int, int, int]]'=None) ->torch.Tensor:
    """Restore input from patches.

    Args:
        patches: patched tensor.
        window_size: the size of the sliding window and the output patch size.
        stride: stride of the sliding window.
        unpadding: remove the padding added to both side of the input.

    Shape:
        - Input: :math:`(B, N, C, H_{out}, W_{out})`
        - Output: :math:`(B, C, H, W)`

    Example:
        >>> out = extract_tensor_patches(torch.arange(16).view(1, 1, 4, 4), window_size=(2, 2), stride=(2, 2))
        >>> combine_tensor_patches(out, window_size=(2, 2), stride=(2, 2))
        tensor([[[[ 0,  1,  2,  3],
                  [ 4,  5,  6,  7],
                  [ 8,  9, 10, 11],
                  [12, 13, 14, 15]]]])
    """
    if stride[0] != window_size[0] or stride[1] != window_size[1]:
        raise NotImplementedError(
            f'Only stride == window_size is supported. Got {stride} and {window_size}.Please feel free to drop a PR to Kornia Github.'
            )
    if unpadding is not None:
        window_size = window_size[0] + (unpadding[0] + unpadding[1]
            ) // window_size[0], window_size[1] + (unpadding[2] + unpadding[3]
            ) // window_size[1]
    patches_tensor = patches.view(-1, window_size[0], window_size[1], *
        patches.shape[-3:])
    restored_tensor = torch.cat(torch.chunk(patches_tensor, window_size[0],
        dim=1), -2).squeeze(1)
    restored_tensor = torch.cat(torch.chunk(restored_tensor, window_size[1],
        dim=1), -1).squeeze(1)
    if unpadding is not None:
        restored_tensor = torch.nn.functional.pad(restored_tensor, [(-i) for
            i in unpadding])
    return restored_tensor


class CombineTensorPatchesNew(nn.Module):
    """Module that combine patches from tensors.

    In the simplest case, the output value of the operator with input size
    :math:`(B, N, C, H_{out}, W_{out})` is :math:`(B, C, H, W)`.

    where
      - :math:`B` is the batch size.
      - :math:`N` denotes the total number of extracted patches stacked in
      - :math:`C` denotes the number of input channels.
      - :math:`H`, :math:`W` the input height and width of the input in pixels.
      - :math:`H_{out}`, :math:`W_{out}` denote to denote to the patch size
        defined in the function signature.
        left-right and top-bottom order.

    * :attr:`window_size` is the size of the sliding window and controls the
      shape of the output tensor and defines the shape of the output patch.
    * :attr:`stride` controls the stride to apply to the sliding window and
      regulates the overlapping between the extracted patches.
    * :attr:`padding` controls the amount of implicit zeros-paddings on both
      sizes at each dimension.

    The parameters :attr:`window_size`, :attr:`stride` and :attr:`padding` can
    be either:

        - a single ``int`` -- in which case the same value is used for the
          height and width dimension.
        - a ``tuple`` of two ints -- in which case, the first `int` is used for
          the height dimension, and the second `int` for the width dimension.

    Args:
        patches: patched tensor.
        window_size: the size of the sliding window and the output patch size.
        unpadding: remove the padding added to both side of the input.

    Shape:
        - Input: :math:`(B, N, C, H_{out}, W_{out})`
        - Output: :math:`(B, C, H, W)`

    Example:
        >>> out = extract_tensor_patches(torch.arange(16).view(1, 1, 4, 4), window_size=(2, 2), stride=(2, 2))
        >>> combine_tensor_patches(out, window_size=(2, 2), stride=(2, 2))
        tensor([[[[ 0,  1,  2,  3],
                  [ 4,  5,  6,  7],
                  [ 8,  9, 10, 11],
                  [12, 13, 14, 15]]]])
    """

    def __init__(self, window_size: 'Union[int, Tuple[int, int]]',
        unpadding: 'Union[int, Tuple[int, int]]'=0) ->None:
        super().__init__()
        self.window_size: 'Tuple[int, int]' = _pair(window_size)
        pad: 'Tuple[int, int]' = _pair(unpadding)
        self.unpadding: 'Tuple[int, int, int, int]' = (pad[0], pad[0], pad[
            1], pad[1])

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


Measured latency_ms: 0.015178