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
from torch._inductor.runtime import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
import torch.nn.functional as F
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_view_0(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 64
    xnumel = 16
    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
    y3 = yindex
    y0 = yindex % 4
    y1 = yindex // 4
    tmp0 = tl.load(in_ptr0 + (x2 + 16 * y3), xmask & ymask, eviction_policy
        ='evict_last')
    tl.store(out_ptr0 + (y0 + 4 * x2 + 64 * y1), tmp0, xmask & ymask)


@triton.jit
def triton_per_fused_convolution_sum_1(in_ptr0, out_ptr0, out_ptr1, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    xnumel = 16000
    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)
    r2 = rindex
    x0 = xindex % 1000
    x1 = xindex // 1000
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 1000 * r2 + 16000 * x1), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, 0)
    tmp4 = tl.sum(tmp3, 1)[:, None]
    tl.store(out_ptr0 + (r2 + 16 * x3), tmp0, xmask)
    tl.store(out_ptr1 + x3, tmp4, xmask)


@triton.jit
def triton_per_fused__softmax_2(in_ptr0, out_ptr2, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    rnumel = 1000
    RBLOCK: tl.constexpr = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    rmask = rindex < rnumel
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 1000 * x0), rmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [RBLOCK])
    tmp3 = tl.where(rmask, tmp1, float('-inf'))
    tmp4 = triton_helpers.promote_to_tensor(triton_helpers.max2(tmp3, 0))
    tmp5 = tmp0 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp7 = tl.broadcast_to(tmp6, [RBLOCK])
    tmp9 = tl.where(rmask, tmp7, 0)
    tmp10 = triton_helpers.promote_to_tensor(tl.sum(tmp9, 0))
    tmp11 = tmp6 / tmp10
    tl.store(out_ptr2 + (r1 + 1000 * x0), tmp11, rmask)


@triton.jit
def triton_per_fused__softmax_3(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 16
    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 + (r1 + 16 * x0), 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]
    tmp5 = tmp0 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.where(xmask, tmp7, 0)
    tmp10 = tl.sum(tmp9, 1)[:, None]
    tmp11 = tmp6 / tmp10
    tl.store(out_ptr2 + (r1 + 16 * x0), tmp11, xmask)


@triton.jit
def triton_per_fused__softmax_add_4(in_ptr0, in_ptr1, in_ptr2, out_ptr2,
    xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    rnumel = 1000
    RBLOCK: tl.constexpr = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    rmask = rindex < rnumel
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 1000 * x0), rmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + (r1 + 1000 * x0), rmask, other=0.0)
    tmp2 = tl.load(in_ptr2 + r1, rmask, eviction_policy='evict_last', other=0.0
        )
    tmp3 = tmp1 + tmp2
    tmp4 = tmp0 + tmp3
    tmp5 = tl.broadcast_to(tmp4, [RBLOCK])
    tmp7 = tl.where(rmask, tmp5, float('-inf'))
    tmp8 = triton_helpers.promote_to_tensor(triton_helpers.max2(tmp7, 0))
    tmp9 = tmp4 - tmp8
    tmp10 = tl_math.exp(tmp9)
    tmp11 = tl.broadcast_to(tmp10, [RBLOCK])
    tmp13 = tl.where(rmask, tmp11, 0)
    tmp14 = triton_helpers.promote_to_tensor(tl.sum(tmp13, 0))
    tmp15 = tmp10 / tmp14
    tl.store(out_ptr2 + (r1 + 1000 * x0), tmp15, rmask)


@triton.jit
def triton_red_fused_mul_sum_5(in_ptr0, in_ptr1, out_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    rnumel = 125
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rbase = tl.arange(0, RBLOCK)[None, :]
    x1 = xindex // 16
    x0 = xindex % 16
    _tmp4 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    x3 = xindex
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r2 = rindex
        tmp0 = tl.load(in_ptr0 + (r2 + 125 * x1), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp1 = tl.load(in_ptr1 + (x0 + 16 * r2 + 2000 * x1), rmask,
            eviction_policy='evict_last', other=0.0)
        tmp2 = tmp0 * tmp1
        tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
        tmp5 = _tmp4 + tmp3
        _tmp4 = tl.where(rmask, tmp5, _tmp4)
    tmp4 = tl.sum(_tmp4, 1)[:, None]
    tl.store(out_ptr0 + x3, tmp4, None)


@triton.jit
def triton_per_fused_mul_sum_6(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 256
    RBLOCK: tl.constexpr = 8
    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
    x0 = xindex % 16
    x1 = xindex // 16
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 16 * r2 + 128 * x1), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, 0)
    tmp4 = tl.sum(tmp3, 1)[:, None]
    tl.store(out_ptr0 + x3, tmp4, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4, 4), (256, 64, 16, 4, 1))
    assert_size_stride(primals_2, (1000, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_3, (333, 1000), (1000, 1))
    assert_size_stride(primals_4, (333,), (1,))
    assert_size_stride(primals_5, (333, 1000), (1000, 1))
    assert_size_stride(primals_6, (333,), (1,))
    assert_size_stride(primals_7, (333, 1000), (1000, 1))
    assert_size_stride(primals_8, (333,), (1,))
    assert_size_stride(primals_9, (1000, 333), (333, 1))
    assert_size_stride(primals_10, (1000,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 4, 4, 4), (64, 1, 16, 4), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_view_0[grid(64, 16)](primals_1, buf0, 64, 16,
            XBLOCK=16, YBLOCK=64, num_warps=4, num_stages=1)
        del primals_1
        buf1 = extern_kernels.convolution(buf0, primals_2, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (16, 1000, 4, 4), (16000, 1, 4000, 1000))
        buf2 = empty_strided_cuda((16, 1000, 4, 4), (16000, 16, 4, 1),
            torch.float32)
        buf3 = empty_strided_cuda((16, 1000), (1000, 1), torch.float32)
        triton_per_fused_convolution_sum_1[grid(16000)](buf1, buf2, buf3, 
            16000, 16, XBLOCK=32, num_warps=4, num_stages=1)
        del buf1
        buf6 = empty_strided_cuda((16, 1000), (1000, 1), torch.float32)
        triton_per_fused__softmax_2[grid(16)](buf3, buf6, 16, 1000,
            num_warps=8, num_stages=1)
        buf7 = empty_strided_cuda((16, 333), (333, 1), torch.float32)
        extern_kernels.addmm(primals_4, buf6, reinterpret_tensor(primals_3,
            (1000, 333), (1, 1000), 0), alpha=1, beta=1, out=buf7)
        del primals_4
        buf8 = empty_strided_cuda((16, 333), (333, 1), torch.float32)
        extern_kernels.addmm(primals_6, buf6, reinterpret_tensor(primals_5,
            (1000, 333), (1, 1000), 0), alpha=1, beta=1, out=buf8)
        del primals_6
        buf9 = empty_strided_cuda((16, 333), (333, 1), torch.float32)
        extern_kernels.addmm(primals_8, buf6, reinterpret_tensor(primals_7,
            (1000, 333), (1, 1000), 0), alpha=1, beta=1, out=buf9)
        del primals_8
        buf10 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        extern_kernels.mm(buf8, reinterpret_tensor(buf7, (333, 16), (1, 333
            ), 0), out=buf10)
        buf13 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        triton_per_fused__softmax_3[grid(16)](buf10, buf13, 16, 16, XBLOCK=
            8, num_warps=2, num_stages=1)
        buf14 = empty_strided_cuda((16, 333), (333, 1), torch.float32)
        extern_kernels.mm(buf13, buf9, out=buf14)
        buf15 = buf3
        del buf3
        extern_kernels.mm(buf14, reinterpret_tensor(primals_9, (333, 1000),
            (1, 333), 0), out=buf15)
        buf18 = empty_strided_cuda((16, 1000), (1000, 1), torch.float32)
        triton_per_fused__softmax_add_4[grid(16)](buf6, buf15, primals_10,
            buf18, 16, 1000, num_warps=8, num_stages=1)
        del buf15
        del primals_10
        buf19 = empty_strided_cuda((16, 1, 4, 4, 8), (128, 2048, 4, 1, 16),
            torch.float32)
        triton_red_fused_mul_sum_5[grid(2048)](buf18, buf2, buf19, 2048, 
            125, XBLOCK=64, RBLOCK=8, num_warps=4, num_stages=1)
        buf20 = reinterpret_tensor(buf10, (16, 1, 4, 4), (16, 16, 4, 1), 0)
        del buf10
        triton_per_fused_mul_sum_6[grid(256)](buf19, buf20, 256, 8, XBLOCK=
            64, num_warps=4, num_stages=1)
        del buf19
        buf23 = empty_strided_cuda((16, 1, 16), (16, 16, 1), torch.float32)
        triton_per_fused__softmax_3[grid(16)](buf20, buf23, 16, 16, XBLOCK=
            8, num_warps=2, num_stages=1)
        del buf20
    return (reinterpret_tensor(buf23, (16, 1, 4, 4), (16, 16, 4, 1), 0),
        buf18, buf6, buf2, primals_2, buf0, buf2, buf6, buf8, buf13, buf14,
        buf18, buf23, primals_9, reinterpret_tensor(buf9, (333, 16), (1, 
        333), 0), buf7, primals_7, primals_5, primals_3)


class VectorAttention(nn.Module):
    """vector attention"""

    def __init__(self, input_dim, hidden_dim):
        super(VectorAttention, self).__init__()
        self.theta = nn.Linear(input_dim, hidden_dim)
        self.phi = nn.Linear(input_dim, hidden_dim)
        self.psi = nn.Linear(input_dim, hidden_dim)
        self.recover = nn.Linear(hidden_dim, input_dim)

    def forward(self, x):
        x_t = self.theta(x)
        x_ph = self.phi(x)
        x_psi = self.psi(x)
        attention_map = torch.matmul(x_ph, torch.transpose(x_t, 0, 1))
        attention_map = attention_map
        attention_map = F.softmax(attention_map, dim=1)
        x_add = torch.matmul(attention_map, x_psi)
        x_add = self.recover(x_add)
        return x + x_add


class GenerateMaskNew(nn.Module):
    """
    sparsify by 1x1 conv and apply gumbel softmax
    """

    def __init__(self, ch, resolution, or_cadidate=1000, no_attention=False):
        super(GenerateMaskNew, self).__init__()
        self.conv = nn.Conv2d(ch, or_cadidate, kernel_size=1, padding=0,
            bias=False)
        self.relu = nn.ReLU()
        self.no_attention = no_attention
        if not self.no_attention:
            self.prob_attention = VectorAttention(input_dim=or_cadidate,
                hidden_dim=or_cadidate // 3)

    def forward(self, input_0):
        primals_2 = self.conv.weight
        primals_3 = self.prob_attention.theta.weight
        primals_4 = self.prob_attention.theta.bias
        primals_5 = self.prob_attention.phi.weight
        primals_6 = self.prob_attention.phi.bias
        primals_7 = self.prob_attention.psi.weight
        primals_8 = self.prob_attention.psi.bias
        primals_9 = self.prob_attention.recover.weight
        primals_10 = self.prob_attention.recover.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9, primals_10])
        return output[0], output[1], output[2], output[3]


Measured latency_ms: 0.187348