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_mul_0(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, out_ptr1, out_ptr2, out_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp4 = tmp0 * tmp3
    tmp6 = tmp0 * tmp5
    tmp8 = tmp0 * tmp7
    tl.store(out_ptr0 + x2, tmp2, xmask)
    tl.store(out_ptr1 + x2, tmp4, xmask)
    tl.store(out_ptr2 + x2, tmp6, xmask)
    tl.store(out_ptr3 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_add_mul_rsub_1(in_out_ptr0, in_out_ptr1, in_out_ptr2,
    in_out_ptr3, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5,
    in_ptr6, in_ptr7, in_ptr8, in_ptr9, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex // 4
    x0 = xindex % 4
    x2 = xindex // 16
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x3, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp4 = tl.load(in_out_ptr0 + x4, xmask)
    tmp6 = tl.load(in_ptr3 + 0)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK])
    tmp15 = tl.load(in_ptr4 + x3, xmask, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr5 + (x0 + 4 * x2), xmask, eviction_policy=
        'evict_last')
    tmp18 = tl.load(in_out_ptr1 + x4, xmask)
    tmp23 = tl.load(in_ptr6 + x3, xmask, eviction_policy='evict_last')
    tmp24 = tl.load(in_ptr7 + (x0 + 4 * x2), xmask, eviction_policy=
        'evict_last')
    tmp26 = tl.load(in_out_ptr2 + x4, xmask)
    tmp31 = tl.load(in_ptr8 + x3, xmask, eviction_policy='evict_last')
    tmp32 = tl.load(in_ptr9 + (x0 + 4 * x2), xmask, eviction_policy=
        'evict_last')
    tmp34 = tl.load(in_out_ptr3 + x4, xmask)
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp8 = tmp5 + tmp7
    tmp9 = tmp0 * tmp8
    tmp10 = 1.0
    tmp11 = tmp10 - tmp0
    tmp12 = -1e+30
    tmp13 = tmp11 * tmp12
    tmp14 = tmp9 + tmp13
    tmp17 = tmp15 + tmp16
    tmp19 = tmp17 + tmp18
    tmp20 = tmp19 + tmp7
    tmp21 = tmp0 * tmp20
    tmp22 = tmp21 + tmp13
    tmp25 = tmp23 + tmp24
    tmp27 = tmp25 + tmp26
    tmp28 = tmp27 + tmp7
    tmp29 = tmp0 * tmp28
    tmp30 = tmp29 + tmp13
    tmp33 = tmp31 + tmp32
    tmp35 = tmp33 + tmp34
    tmp36 = tmp35 + tmp7
    tmp37 = tmp0 * tmp36
    tmp38 = tmp37 + tmp13
    tl.store(in_out_ptr0 + x4, tmp14, xmask)
    tl.store(in_out_ptr1 + x4, tmp22, xmask)
    tl.store(in_out_ptr2 + x4, tmp30, xmask)
    tl.store(in_out_ptr3 + x4, tmp38, xmask)


@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x2, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x2, tmp8, xmask)


@triton.jit
def triton_poi_fused_cat_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 320
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 20
    x1 = xindex // 20
    x2 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (4 * x1 + x0), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tmp6 & tmp8
    tmp10 = tl.load(in_ptr1 + (4 * x1 + (-4 + x0)), tmp9 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp11 = tmp0 >= tmp7
    tmp12 = tl.full([1], 12, tl.int64)
    tmp13 = tmp0 < tmp12
    tmp14 = tmp11 & tmp13
    tmp15 = tl.load(in_ptr2 + (4 * x1 + (-8 + x0)), tmp14 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp16 = tmp0 >= tmp12
    tmp17 = tl.full([1], 16, tl.int64)
    tmp18 = tmp0 < tmp17
    tmp19 = tmp16 & tmp18
    tmp20 = tl.load(in_ptr3 + (4 * x1 + (-12 + x0)), tmp19 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp21 = tmp0 >= tmp17
    tl.full([1], 20, tl.int64)
    tmp24 = tl.load(in_ptr4 + (4 * x1 + (-16 + x0)), tmp21 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp25 = tl.where(tmp19, tmp20, tmp24)
    tmp26 = tl.where(tmp14, tmp15, tmp25)
    tmp27 = tl.where(tmp9, tmp10, tmp26)
    tmp28 = tl.where(tmp4, tmp5, tmp27)
    tl.store(out_ptr0 + x2, tmp28, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
        primals_13, primals_14, primals_15) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (4, 1), (1, 1))
    assert_size_stride(primals_3, (4, 1), (1, 1))
    assert_size_stride(primals_4, (1, 1, 4), (4, 4, 1))
    assert_size_stride(primals_5, (1,), (1,))
    assert_size_stride(primals_6, (4, 4, 1), (4, 1, 1))
    assert_size_stride(primals_7, (4, 1), (1, 1))
    assert_size_stride(primals_8, (4, 1), (1, 1))
    assert_size_stride(primals_9, (1, 1, 4), (4, 4, 1))
    assert_size_stride(primals_10, (4, 1), (1, 1))
    assert_size_stride(primals_11, (4, 1), (1, 1))
    assert_size_stride(primals_12, (1, 1, 4), (4, 4, 1))
    assert_size_stride(primals_13, (4, 1), (1, 1))
    assert_size_stride(primals_14, (4, 1), (1, 1))
    assert_size_stride(primals_15, (1, 1, 4), (4, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            primals_2, out=buf0)
        del primals_2
        buf1 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            primals_3, out=buf1)
        del primals_3
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        buf18 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        buf26 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mul_0[grid(64)](primals_1, primals_4, primals_9,
            primals_12, primals_15, buf2, buf10, buf18, buf26, 64, XBLOCK=
            64, num_warps=1, num_stages=1)
        del primals_12
        del primals_15
        del primals_4
        del primals_9
        buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(buf2, reinterpret_tensor(primals_1, (4, 4, 4), (
            16, 1, 4), 0), out=buf3)
        buf11 = buf2
        del buf2
        extern_kernels.bmm(buf10, reinterpret_tensor(primals_1, (4, 4, 4),
            (16, 1, 4), 0), out=buf11)
        buf16 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            primals_10, out=buf16)
        del primals_10
        buf17 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            primals_11, out=buf17)
        del primals_11
        buf19 = buf10
        del buf10
        extern_kernels.bmm(buf18, reinterpret_tensor(primals_1, (4, 4, 4),
            (16, 1, 4), 0), out=buf19)
        buf24 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            primals_13, out=buf24)
        del primals_13
        buf25 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            primals_14, out=buf25)
        del primals_14
        buf27 = buf18
        del buf18
        extern_kernels.bmm(buf26, reinterpret_tensor(primals_1, (4, 4, 4),
            (16, 1, 4), 0), out=buf27)
        buf8 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            primals_7, out=buf8)
        del primals_7
        buf9 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_1, (16, 4), (4, 1), 0),
            primals_8, out=buf9)
        del primals_8
        buf4 = buf3
        del buf3
        buf12 = buf11
        del buf11
        buf20 = buf19
        del buf19
        buf28 = buf27
        del buf27
        triton_poi_fused_add_mul_rsub_1[grid(64)](buf4, buf12, buf20, buf28,
            primals_6, buf0, buf1, primals_5, buf8, buf9, buf16, buf17,
            buf24, buf25, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf0
        del buf1
        del buf16
        del buf17
        del buf24
        del buf25
        del buf8
        del buf9
        del primals_5
        buf5 = buf26
        del buf26
        triton_poi_fused__softmax_2[grid(64)](buf4, buf5, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf6 = buf4
        del buf4
        triton_poi_fused__softmax_3[grid(64)](buf5, buf6, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf7 = buf5
        del buf5
        extern_kernels.bmm(buf6, primals_1, out=buf7)
        buf13 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_2[grid(64)](buf12, buf13, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf14 = buf12
        del buf12
        triton_poi_fused__softmax_3[grid(64)](buf13, buf14, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf15 = buf13
        del buf13
        extern_kernels.bmm(buf14, primals_1, out=buf15)
        buf21 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_2[grid(64)](buf20, buf21, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf22 = buf20
        del buf20
        triton_poi_fused__softmax_3[grid(64)](buf21, buf22, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf23 = buf21
        del buf21
        extern_kernels.bmm(buf22, primals_1, out=buf23)
        buf29 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_2[grid(64)](buf28, buf29, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf30 = buf28
        del buf28
        triton_poi_fused__softmax_3[grid(64)](buf29, buf30, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf31 = buf29
        del buf29
        extern_kernels.bmm(buf30, primals_1, out=buf31)
        buf32 = empty_strided_cuda((4, 4, 20), (80, 20, 1), torch.float32)
        triton_poi_fused_cat_4[grid(320)](primals_1, buf7, buf15, buf23,
            buf31, buf32, 320, XBLOCK=256, num_warps=4, num_stages=1)
        del buf15
        del buf23
        del buf31
        del buf7
    return buf32, primals_1, primals_6, buf6, buf14, buf22, buf30


def masked_softmax(logits, mask, dim=-1, log_softmax=False):
    """Take the softmax of `logits` over given dimension, and set
    entries to 0 wherever `mask` is 0.

    Args:
        logits (torch.Tensor): Inputs to the softmax function.
        mask (torch.Tensor): Same shape as `logits`, with 0 indicating
            positions that should be assigned 0 probability in the output.
        dim (int): Dimension over which to take softmax.
        log_softmax (bool): Take log-softmax rather than regular softmax.
            E.g., some PyTorch functions such as `F.nll_loss` expect log-softmax.

    Returns:
        probs (torch.Tensor): Result of taking masked softmax over the logits.
    """
    mask = mask.type(torch.float32)
    masked_logits = mask * logits + (1 - mask) * -1e+30
    softmax_fn = F.log_softmax if log_softmax else F.softmax
    probs = softmax_fn(masked_logits, dim)
    return probs


class BiDAFSelfAttentionNew(nn.Module):

    def __init__(self, hidden_size, drop_prob=0.1):
        super(BiDAFSelfAttentionNew, self).__init__()
        self.drop_prob = drop_prob
        self.c_weight = nn.Parameter(torch.zeros(hidden_size, 1))
        self.q_weight = nn.Parameter(torch.zeros(hidden_size, 1))
        self.cq_weight = nn.Parameter(torch.zeros(1, 1, hidden_size))
        self.c_weight_2 = nn.Parameter(torch.zeros(hidden_size, 1))
        self.q_weight_2 = nn.Parameter(torch.zeros(hidden_size, 1))
        self.cq_weight_2 = nn.Parameter(torch.zeros(1, 1, hidden_size))
        self.c_weight_3 = nn.Parameter(torch.zeros(hidden_size, 1))
        self.q_weight_3 = nn.Parameter(torch.zeros(hidden_size, 1))
        self.cq_weight_3 = nn.Parameter(torch.zeros(1, 1, hidden_size))
        self.c_weight_4 = nn.Parameter(torch.zeros(hidden_size, 1))
        self.q_weight_4 = nn.Parameter(torch.zeros(hidden_size, 1))
        self.cq_weight_4 = nn.Parameter(torch.zeros(1, 1, hidden_size))
        for weight in (self.c_weight, self.q_weight, self.cq_weight, self.
            c_weight_2, self.q_weight_2, self.cq_weight_2, self.c_weight_3,
            self.q_weight_3, self.cq_weight_3, self.c_weight_4, self.
            q_weight_4, self.cq_weight_4):
            nn.init.xavier_uniform_(weight)
        self.bias = nn.Parameter(torch.zeros(1))

    def get_similarity_matrix(self, c, q, c_weight, q_weight, cq_weight):
        """ Just performing w_sim^T[c_i; q_j; c_i * q_j] except c == q
        (Copied over from BidafAttention)
        """
        c_len, q_len = c.size(1), q.size(1)
        c = F.dropout(c, self.drop_prob, self.training)
        q = F.dropout(q, self.drop_prob, self.training)
        s0 = torch.matmul(c, c_weight).expand([-1, -1, q_len])
        s1 = torch.matmul(q, q_weight).transpose(1, 2).expand([-1, c_len, -1])
        s2 = torch.matmul(c * cq_weight, q.transpose(1, 2))
        s = s0 + s1 + s2 + self.bias
        return s

    def forward(self, input_0, input_1):
        primals_2 = self.c_weight
        primals_3 = self.q_weight
        primals_4 = self.cq_weight
        primals_7 = self.c_weight_2
        primals_8 = self.q_weight_2
        primals_9 = self.cq_weight_2
        primals_10 = self.c_weight_3
        primals_11 = self.q_weight_3
        primals_12 = self.cq_weight_3
        primals_13 = self.c_weight_4
        primals_14 = self.q_weight_4
        primals_15 = self.cq_weight_4
        primals_5 = self.bias
        primals_1 = input_0
        primals_6 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11, primals_12, primals_13, primals_14,
            primals_15])
        return output[0]


Measured latency_ms: 0.028568