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.nn import Module
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused_mul_sum_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex % 64
    x1 = xindex // 4 % 16
    x2 = xindex // 64
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x1 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (64 + x3), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (16 + x1 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp7 = tl.load(in_ptr0 + (128 + x3), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (32 + x1 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp11 = tl.load(in_ptr0 + (192 + x3), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (48 + x1 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tl.store(out_ptr0 + x4, tmp14, xmask)


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


def aggregate(x, dim, aggr='add', mask=None, keepdim=False):
    """
    Args:
        x:    (..., A, ..., F), Features to be aggregated.
        mask: (..., A, ...)
    Returns:
        (...,  , ..., F), if keepdim == False
        (..., 1, ..., F), if keepdim == True
    """
    assert aggr in ('add', 'mean')
    if mask is not None:
        x = x * mask.unsqueeze(-1)
    y = torch.sum(x, dim=dim, keepdim=keepdim)
    if aggr == 'mean':
        if mask is not None:
            n = torch.sum(mask, dim=dim, keepdim=keepdim)
            n = torch.max(n, other=torch.ones_like(n))
        else:
            n = x.size(dim)
        y = y / n
    return y


def readout(x, mask, aggr='add'):
    """
    Args:
        x:    (B, N_max, F)
        mask: (B, N_max)
    Returns:
        (B, F)
    """
    return aggregate(x=x, dim=1, aggr=aggr, mask=mask, keepdim=False)


class ReadoutNew(Module):

    def __init__(self, aggr='add'):
        super().__init__()
        assert aggr in ('add', 'mean')
        self.aggr = aggr

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