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
import math
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_per_fused_max_stack_0(in_ptr0, out_ptr1, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 16
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (4 * (r1 // 2 % 2) + 16 * (r1 // 4) + 64 * x0 +
        r1 % 2), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, float('-inf'))
    tmp4 = triton_helpers.max2(tmp3, 1)[:, None]
    tl.store(out_ptr1 + 4 * x0, tmp4, xmask)


@triton.jit
def triton_per_fused_max_stack_1(in_ptr0, out_ptr1, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 16
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (8 + 4 * (r1 // 2 % 2) + 16 * (r1 // 4) + 64 *
        x0 + r1 % 2), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, float('-inf'))
    tmp4 = triton_helpers.max2(tmp3, 1)[:, None]
    tl.store(out_ptr1 + 4 * x0, tmp4, xmask)


@triton.jit
def triton_per_fused_max_stack_2(in_ptr0, out_ptr1, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 16
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (2 + 4 * (r1 // 2 % 2) + 16 * (r1 // 4) + 64 *
        x0 + r1 % 2), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, float('-inf'))
    tmp4 = triton_helpers.max2(tmp3, 1)[:, None]
    tl.store(out_ptr1 + 4 * x0, tmp4, xmask)


@triton.jit
def triton_per_fused_max_stack_3(in_ptr0, out_ptr1, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 16
    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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (10 + 4 * (r1 // 2 % 2) + 16 * (r1 // 4) + 64 *
        x0 + r1 % 2), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, float('-inf'))
    tmp4 = triton_helpers.max2(tmp3, 1)[:, None]
    tl.store(out_ptr1 + 4 * x0, tmp4, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf8 = reinterpret_tensor(buf12, (4, 1), (4, 1), 0)
        get_raw_stream(0)
        triton_per_fused_max_stack_0[grid(4)](arg0_1, buf8, 4, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf9 = reinterpret_tensor(buf12, (4, 1), (4, 1), 1)
        triton_per_fused_max_stack_1[grid(4)](arg0_1, buf9, 4, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf10 = reinterpret_tensor(buf12, (4, 1), (4, 1), 2)
        triton_per_fused_max_stack_2[grid(4)](arg0_1, buf10, 4, 16, XBLOCK=
            1, num_warps=2, num_stages=1)
        buf11 = reinterpret_tensor(buf12, (4, 1), (4, 1), 3)
        triton_per_fused_max_stack_3[grid(4)](arg0_1, buf11, 4, 16, XBLOCK=
            1, num_warps=2, num_stages=1)
        del arg0_1
    return buf12,


class BinaryChunkNew(torch.nn.Module):

    def __init__(self, nCls, isLogit=False, pooling='max', chunk_dim=-1):
        super(BinaryChunkNew, self).__init__()
        self.nClass = nCls
        self.nChunk = int(math.ceil(math.log2(self.nClass)))
        self.pooling = pooling
        self.isLogit = isLogit

    def __repr__(self):
        main_str = super(BinaryChunkNew, self).__repr__()
        if self.isLogit:
            main_str += '_logit'
        main_str += (
            f'_nChunk{self.nChunk}_cls[{self.nClass}]_pool[{self.pooling}]')
        return main_str

    def chunk_poll(self, ck, nSamp):
        x2 = ck.contiguous().view(nSamp, -1)
        if self.pooling == 'max':
            x3 = torch.max(x2, 1)
            return x3.values
        else:
            x3 = torch.mean(x2, 1)
            return x3

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


Measured latency_ms: 0.051197