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_clone_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 8
    x1 = xindex // 8 % 4
    x2 = xindex // 32
    x3 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x1 + 4 * x0 + 16 * x2), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x1 + 4 * (-4 + x0) + 16 * x2), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tl.store(out_ptr0 + x3, tmp10, xmask)


@triton.jit
def triton_poi_fused__log_softmax_add_mul_rsub_1(in_ptr0, in_ptr1, in_ptr2,
    out_ptr0, out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    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 + 4 * x2, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + 0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
    tmp11 = tl.load(in_ptr0 + (1 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp19 = tl.load(in_ptr0 + (2 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp20 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp27 = tl.load(in_ptr0 + (3 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp28 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp4 = tmp1 + tmp3
    tmp5 = tmp0 * tmp4
    tmp6 = 1.0
    tmp7 = tmp6 - tmp0
    tmp8 = -1e+30
    tmp9 = tmp7 * tmp8
    tmp10 = tmp5 + tmp9
    tmp13 = tmp12 + tmp3
    tmp14 = tmp11 * tmp13
    tmp15 = tmp6 - tmp11
    tmp16 = tmp15 * tmp8
    tmp17 = tmp14 + tmp16
    tmp18 = triton_helpers.maximum(tmp10, tmp17)
    tmp21 = tmp20 + tmp3
    tmp22 = tmp19 * tmp21
    tmp23 = tmp6 - tmp19
    tmp24 = tmp23 * tmp8
    tmp25 = tmp22 + tmp24
    tmp26 = triton_helpers.maximum(tmp18, tmp25)
    tmp29 = tmp28 + tmp3
    tmp30 = tmp27 * tmp29
    tmp31 = tmp6 - tmp27
    tmp32 = tmp31 * tmp8
    tmp33 = tmp30 + tmp32
    tmp34 = triton_helpers.maximum(tmp26, tmp33)
    tmp35 = tmp10 - tmp34
    tmp36 = tl_math.exp(tmp35)
    tmp37 = tmp17 - tmp34
    tmp38 = tl_math.exp(tmp37)
    tmp39 = tmp36 + tmp38
    tmp40 = tmp25 - tmp34
    tmp41 = tl_math.exp(tmp40)
    tmp42 = tmp39 + tmp41
    tmp43 = tmp33 - tmp34
    tmp44 = tl_math.exp(tmp43)
    tmp45 = tmp42 + tmp44
    tl.store(out_ptr0 + x2, tmp34, xmask)
    tl.store(out_ptr1 + x2, tmp45, xmask)


@triton.jit
def triton_poi_fused__log_softmax_add_mul_rsub_2(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, in_ptr4, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x4 = xindex % 16
    x5 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr2 + 0)
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK])
    tmp11 = tl.load(in_ptr3 + x5, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr4 + x5, xmask, eviction_policy='evict_last')
    tmp4 = tmp1 + tmp3
    tmp5 = tmp0 * tmp4
    tmp6 = 1.0
    tmp7 = tmp6 - tmp0
    tmp8 = -1e+30
    tmp9 = tmp7 * tmp8
    tmp10 = tmp5 + tmp9
    tmp12 = tmp10 - tmp11
    tmp14 = tl_math.log(tmp13)
    tmp15 = tmp12 - tmp14
    tl.store(out_ptr0 + x3, tmp15, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_3, (1, 8), (8, 1))
    assert_size_stride(primals_4, (1,), (1,))
    assert_size_stride(primals_5, (4, 4, 4), (16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clone_0[grid(128)](primals_1, primals_2, buf0, 128,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_1
        del primals_2
        buf1 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf0, (16, 8), (8, 1), 0),
            reinterpret_tensor(primals_3, (8, 1), (1, 8), 0), out=buf1)
        del primals_3
        buf2 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        buf3 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__log_softmax_add_mul_rsub_1[grid(16)](primals_5,
            buf1, primals_4, buf2, buf3, 16, XBLOCK=16, num_warps=1,
            num_stages=1)
        buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__log_softmax_add_mul_rsub_2[grid(64)](primals_5,
            buf1, primals_4, buf2, buf3, buf4, 64, XBLOCK=64, num_warps=1,
            num_stages=1)
        del buf1
        del buf2
        del buf3
        del primals_4
    return buf4, primals_5, reinterpret_tensor(buf0, (16, 8), (8, 1), 0), buf4


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 OutputLayerNew(nn.Module):
    """Output Layer which outputs the probability distribution for the answer span in the context span.
    Takes inputs from 2 Model Encoder Layers.
    """

    def __init__(self, drop_prob, word_embed):
        """
        @param drop_prob (float): Probability of zero-ing out activations.
        @param word_embed (int): Word vector size. (128)
        """
        super(OutputLayerNew, self).__init__()
        self.ff = nn.Linear(2 * word_embed, 1)

    def forward(self, input_0, input_1, input_2):
        primals_3 = self.ff.weight
        primals_4 = self.ff.bias
        primals_1 = input_0
        primals_2 = input_1
        primals_5 = input_2
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0]


Measured latency_ms: 0.014944