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 libdevice, math as tl_math
import torch.utils.data
import torch.nn as nn
from torch.nn.utils import weight_norm
import torch.nn.modules.module
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_div_mul_norm_0(in_out_ptr0, in_ptr0, in_ptr1, out_ptr0,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    rnumel = 48
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    rmask = rindex < rnumel
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, rmask, other=0.0)
    tmp7 = tl.load(in_ptr1 + 0)
    tmp8 = tl.broadcast_to(tmp7, [XBLOCK, RBLOCK])
    tmp1 = tmp0 * tmp0
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp4 = tl.where(rmask, tmp2, 0)
    tmp5 = tl.sum(tmp4, 1)[:, None]
    tmp6 = libdevice.sqrt(tmp5)
    tmp9 = tmp8 / tmp6
    tmp10 = tmp0 * tmp9
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp6, None)
    tl.store(out_ptr0 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp10, rmask)


@triton.jit
def triton_poi_fused_clone_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 3072
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x4 = xindex // 768
    x5 = xindex % 192
    x0 = xindex % 12
    x2 = xindex // 48 % 4
    x7 = xindex
    tmp0 = tl.load(in_ptr0 + (x5 + 192 * x4), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr1 + (x0 + 12 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp4 = tmp2 * tmp3
    tl.store(out_ptr0 + x7, tmp4, xmask)


@triton.jit
def triton_poi_fused_clone_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 3072
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4 % 12
    x3 = xindex // 192 % 4
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + (x1 + 12 * x0 + 48 * x3), xmask,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(out_ptr0 + x5, tmp4, xmask)


@triton.jit
def triton_per_fused__softmax_3(in_ptr0, in_ptr1, out_ptr2, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    xnumel = 64
    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
    x3 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = tl.where(xmask, tmp3, float('-inf'))
    tmp6 = triton_helpers.max2(tmp5, 1)[:, None]
    tmp7 = tmp2 - tmp6
    tmp8 = tl_math.exp(tmp7)
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.where(xmask, tmp9, 0)
    tmp12 = tl.sum(tmp11, 1)[:, None]
    tmp13 = tmp8 / tmp12
    tl.store(out_ptr2 + (r2 + 16 * x3), tmp13, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_4(in_ptr0, in_ptr1, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 192
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 12
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tmp5 = 0.0
    tmp6 = tmp4 <= tmp5
    tl.store(out_ptr0 + x2, tmp6, 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), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_3, (), ())
    assert_size_stride(primals_4, (12, 4), (4, 1))
    assert_size_stride(primals_5, (12,), (1,))
    assert_size_stride(primals_6, (), ())
    assert_size_stride(primals_7, (12, 4), (4, 1))
    assert_size_stride(primals_8, (12,), (1,))
    assert_size_stride(primals_9, (1, 4, 1, 12), (48, 12, 12, 1))
    assert_size_stride(primals_10, (1, 4, 1, 1), (4, 1, 1, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf1 = buf0
        del buf0
        buf2 = empty_strided_cuda((12, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_div_mul_norm_0[grid(1)](buf1, primals_4, primals_3,
            buf2, 1, 48, XBLOCK=1, num_warps=2, num_stages=1)
        buf3 = empty_strided_cuda((64, 12), (12, 1), torch.float32)
        extern_kernels.addmm(primals_5, reinterpret_tensor(primals_1, (64, 
            4), (4, 1), 0), reinterpret_tensor(buf2, (4, 12), (1, 4), 0),
            alpha=1, beta=1, out=buf3)
        del primals_5
        buf4 = empty_strided_cuda((), (), torch.float32)
        buf5 = buf4
        del buf4
        buf6 = empty_strided_cuda((12, 4), (4, 1), torch.float32)
        triton_per_fused_div_mul_norm_0[grid(1)](buf5, primals_7, primals_6,
            buf6, 1, 48, XBLOCK=1, num_warps=2, num_stages=1)
        buf7 = empty_strided_cuda((16, 12), (12, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_2, (16, 4), (4, 1), 0),
            reinterpret_tensor(buf6, (4, 12), (1, 4), 0), out=buf7)
        buf8 = empty_strided_cuda((4, 4, 4, 4, 12), (768, 192, 48, 12, 1),
            torch.float32)
        triton_poi_fused_clone_1[grid(3072)](buf3, primals_9, buf8, 3072,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf9 = empty_strided_cuda((4, 4, 4, 12, 4), (768, 192, 48, 4, 1),
            torch.float32)
        triton_poi_fused_clone_2[grid(3072)](buf7, primals_8, buf9, 3072,
            XBLOCK=256, num_warps=4, num_stages=1)
        buf10 = empty_strided_cuda((64, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf8, (64, 4, 12), (48, 12, 1
            ), 0), reinterpret_tensor(buf9, (64, 12, 4), (48, 4, 1), 0),
            out=buf10)
        buf13 = empty_strided_cuda((16, 4, 16), (64, 16, 1), torch.float32)
        triton_per_fused__softmax_3[grid(64)](buf10, primals_10, buf13, 64,
            16, XBLOCK=32, num_warps=4, num_stages=1)
        del buf10
        del primals_10
        buf14 = empty_strided_cuda((4, 4, 12), (48, 12, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_4[grid(192)](buf7,
            primals_8, buf14, 192, XBLOCK=128, num_warps=4, num_stages=1)
        del buf7
        del primals_8
    return (reinterpret_tensor(buf13, (16, 4, 4, 4), (64, 16, 4, 1), 0),
        buf2, buf6, primals_3, primals_4, primals_6, primals_7, primals_9,
        buf1, reinterpret_tensor(primals_1, (64, 4), (4, 1), 0), buf3, buf5,
        reinterpret_tensor(primals_2, (16, 4), (4, 1), 0), buf13,
        reinterpret_tensor(buf8, (64, 12, 4), (48, 1, 12), 0),
        reinterpret_tensor(buf9, (64, 4, 12), (48, 1, 4), 0), buf14)


class FCNet(nn.Module):

    def __init__(self, in_size, out_size, activate=None, drop=0.0):
        super(FCNet, self).__init__()
        self.lin = weight_norm(nn.Linear(in_size, out_size), dim=None)
        self.drop_value = drop
        self.drop = nn.Dropout(drop)
        self.activate = activate.lower() if activate is not None else None
        if activate == 'relu':
            self.ac_fn = nn.ReLU()
        elif activate == 'sigmoid':
            self.ac_fn = nn.Sigmoid()
        elif activate == 'tanh':
            self.ac_fn = nn.Tanh()

    def forward(self, x):
        if self.drop_value > 0:
            x = self.drop(x)
        x = self.lin(x)
        if self.activate is not None:
            x = self.ac_fn(x)
        return x


class BiAttentionNew(nn.Module):

    def __init__(self, v_features, q_features, mid_features, glimpses, drop=0.0
        ):
        super(BiAttentionNew, self).__init__()
        self.hidden_aug = 3
        self.glimpses = glimpses
        self.lin_v = FCNet(v_features, int(mid_features * self.hidden_aug),
            activate='relu', drop=drop / 2.5)
        self.lin_q = FCNet(q_features, int(mid_features * self.hidden_aug),
            activate='relu', drop=drop / 2.5)
        self.h_weight = nn.Parameter(torch.Tensor(1, glimpses, 1, int(
            mid_features * self.hidden_aug)).normal_())
        self.h_bias = nn.Parameter(torch.Tensor(1, glimpses, 1, 1).normal_())
        self.drop = nn.Dropout(drop)

    def forward(self, input_0, input_1):
        primals_9 = self.h_weight
        primals_10 = self.h_bias
        primals_5 = self.lin_v.lin.bias
        primals_3 = self.lin_v.lin.weight_g
        primals_4 = self.lin_v.lin.weight_v
        primals_8 = self.lin_q.lin.bias
        primals_6 = self.lin_q.lin.weight_g
        primals_7 = self.lin_q.lin.weight_v
        primals_1 = input_0
        primals_2 = input_1
        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]


Measured latency_ms: 0.09223