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
from torch import nn
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_tanh_0(in_out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 300
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = libdevice.tanh(tmp0)
    tl.store(in_out_ptr0 + x0, tmp1, xmask)


@triton.jit
def triton_per_fused__softmax_mul_sum_1(in_ptr0, in_ptr1, out_ptr2, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp9 = tl.load(in_ptr0 + 0)
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK])
    tmp14 = tl.load(in_ptr1 + r0, None)
    tmp16 = tl.load(in_ptr0 + 1)
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp21 = tl.load(in_ptr1 + (4 + r0), None)
    tmp24 = tl.load(in_ptr0 + 2)
    tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp29 = tl.load(in_ptr1 + (8 + r0), None)
    tmp32 = tl.load(in_ptr0 + 3)
    tmp33 = tl.broadcast_to(tmp32, [XBLOCK, RBLOCK])
    tmp37 = tl.load(in_ptr1 + (12 + r0), None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = triton_helpers.max2(tmp1, 1)[:, None]
    tmp4 = tmp0 - tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tmp11 = tmp10 - tmp3
    tmp12 = tl_math.exp(tmp11)
    tmp13 = tmp12 / tmp8
    tmp15 = tmp13 * tmp14
    tmp18 = tmp17 - tmp3
    tmp19 = tl_math.exp(tmp18)
    tmp20 = tmp19 / tmp8
    tmp22 = tmp20 * tmp21
    tmp23 = tmp15 + tmp22
    tmp26 = tmp25 - tmp3
    tmp27 = tl_math.exp(tmp26)
    tmp28 = tmp27 / tmp8
    tmp30 = tmp28 * tmp29
    tmp31 = tmp23 + tmp30
    tmp34 = tmp33 - tmp3
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp35 / tmp8
    tmp38 = tmp36 * tmp37
    tmp39 = tmp31 + tmp38
    tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp39, None)


@triton.jit
def triton_poi_fused_add_tanh_2(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 300
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 75
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.tanh(tmp2)
    tl.store(out_ptr0 + x2, tmp3, xmask)


@triton.jit
def triton_per_fused__log_softmax__log_softmax_backward_data__softmax_mul_sum_3(
    in_ptr0, in_ptr1, out_ptr2, out_ptr3, out_ptr4, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp9 = tl.load(in_ptr0 + 0)
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK])
    tmp14 = tl.load(in_ptr1 + r0, None)
    tmp16 = tl.load(in_ptr0 + 1)
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp21 = tl.load(in_ptr1 + (4 + r0), None)
    tmp24 = tl.load(in_ptr0 + 2)
    tmp25 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp29 = tl.load(in_ptr1 + (8 + r0), None)
    tmp32 = tl.load(in_ptr0 + 3)
    tmp33 = tl.broadcast_to(tmp32, [XBLOCK, RBLOCK])
    tmp37 = tl.load(in_ptr1 + (12 + r0), None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = triton_helpers.max2(tmp1, 1)[:, None]
    tmp4 = tmp0 - tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tmp11 = tmp10 - tmp3
    tmp12 = tl_math.exp(tmp11)
    tmp13 = tmp12 / tmp8
    tmp15 = tmp13 * tmp14
    tmp18 = tmp17 - tmp3
    tmp19 = tl_math.exp(tmp18)
    tmp20 = tmp19 / tmp8
    tmp22 = tmp20 * tmp21
    tmp23 = tmp15 + tmp22
    tmp26 = tmp25 - tmp3
    tmp27 = tl_math.exp(tmp26)
    tmp28 = tmp27 / tmp8
    tmp30 = tmp28 * tmp29
    tmp31 = tmp23 + tmp30
    tmp34 = tmp33 - tmp3
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp35 / tmp8
    tmp38 = tmp36 * tmp37
    tmp39 = tmp31 + tmp38
    tmp40 = tl_math.log(tmp8)
    tmp41 = tmp4 - tmp40
    tmp42 = tl_math.exp(tmp41)
    tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp39, None)
    tl.store(out_ptr3 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp41, None)
    tl.store(out_ptr4 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp42, None)


@triton.jit
def triton_poi_fused_add_tanh_4(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 300
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 75
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.tanh(tmp2)
    tl.store(in_out_ptr0 + x2, tmp3, xmask)


@triton.jit
def triton_per_fused__log_softmax__log_softmax_backward_data_5(in_ptr0,
    out_ptr2, out_ptr3, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = triton_helpers.max2(tmp1, 1)[:, None]
    tmp4 = tmp0 - tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tmp9 = tl_math.log(tmp8)
    tmp10 = tmp4 - tmp9
    tmp11 = tl_math.exp(tmp10)
    tl.store(out_ptr2 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp10, None)
    tl.store(out_ptr3 + tl.broadcast_to(r0, [XBLOCK, RBLOCK]), tmp11, None)


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, (75, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (1, 75), (75, 1))
    assert_size_stride(primals_4, (1,), (1,))
    assert_size_stride(primals_5, (75, 4), (4, 1))
    assert_size_stride(primals_6, (4, 4), (4, 1))
    assert_size_stride(primals_7, (75, 4), (4, 1))
    assert_size_stride(primals_8, (1, 75), (75, 1))
    assert_size_stride(primals_9, (12, 4), (4, 1))
    assert_size_stride(primals_10, (12, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 75), (75, 1), torch.float32)
        extern_kernels.mm(primals_2, reinterpret_tensor(primals_1, (4, 75),
            (1, 4), 0), out=buf0)
        del primals_1
        buf1 = buf0
        del buf0
        get_raw_stream(0)
        triton_poi_fused_tanh_0[grid(300)](buf1, 300, XBLOCK=128, num_warps
            =4, num_stages=1)
        buf3 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_4, buf1, reinterpret_tensor(primals_3,
            (75, 1), (1, 75), 0), alpha=1, beta=1, out=buf3)
        del primals_4
        buf6 = empty_strided_cuda((4,), (1,), torch.float32)
        triton_per_fused__softmax_mul_sum_1[grid(1)](buf3, primals_2, buf6,
            1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        buf7 = empty_strided_cuda((4, 75), (75, 1), torch.float32)
        extern_kernels.mm(primals_6, reinterpret_tensor(primals_5, (4, 75),
            (1, 4), 0), out=buf7)
        del primals_5
        buf8 = empty_strided_cuda((1, 75), (75, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf6, (1, 4), (4, 1), 0),
            reinterpret_tensor(primals_7, (4, 75), (1, 4), 0), out=buf8)
        buf9 = empty_strided_cuda((4, 75), (75, 1), torch.float32)
        triton_poi_fused_add_tanh_2[grid(300)](buf7, buf8, buf9, 300,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf10 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        extern_kernels.mm(buf9, reinterpret_tensor(primals_8, (75, 1), (1, 
            75), 0), out=buf10)
        buf13 = empty_strided_cuda((4,), (1,), torch.float32)
        buf24 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        buf27 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        triton_per_fused__log_softmax__log_softmax_backward_data__softmax_mul_sum_3[
            grid(1)](buf10, primals_6, buf13, buf24, buf27, 1, 4, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf14 = empty_strided_cuda((1, 12), (12, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf13, (1, 4), (4, 1), 0),
            reinterpret_tensor(primals_9, (4, 12), (1, 4), 0), out=buf14)
        buf15 = empty_strided_cuda((1, 12), (12, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf6, (1, 4), (4, 1), 0),
            reinterpret_tensor(primals_10, (4, 12), (1, 4), 0), out=buf15)
        buf16 = torch.ops.aten._thnn_fused_gru_cell.default(buf14, buf15,
            reinterpret_tensor(buf6, (1, 4), (4, 1), 0))
        del buf14
        del buf15
        buf17 = buf16[0]
        buf18 = buf16[1]
        del buf16
        buf19 = buf8
        del buf8
        extern_kernels.mm(buf17, reinterpret_tensor(primals_7, (4, 75), (1,
            4), 0), out=buf19)
        buf20 = buf7
        del buf7
        triton_poi_fused_add_tanh_4[grid(300)](buf20, buf19, 300, XBLOCK=
            128, num_warps=4, num_stages=1)
        del buf19
        buf21 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        extern_kernels.mm(buf20, reinterpret_tensor(primals_8, (75, 1), (1,
            75), 0), out=buf21)
        buf25 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        buf26 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        triton_per_fused__log_softmax__log_softmax_backward_data_5[grid(1)](
            buf21, buf25, buf26, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del buf21
    return (reinterpret_tensor(buf24, (4,), (1,), 0), reinterpret_tensor(
        buf25, (4,), (1,), 0), primals_2, primals_6, buf1, buf3,
        reinterpret_tensor(buf6, (1, 4), (4, 1), 0), buf9, buf10,
        reinterpret_tensor(buf13, (1, 4), (4, 1), 0), buf18, buf17, buf20,
        buf26, primals_8, primals_7, primals_10, primals_9, buf27, primals_3)


class PointerNetworkNew(nn.Module):

    def __init__(self, input_size, model_dim, attn_size=75, dropout=0.2):
        """ Pointer Network
                Args:
                    input_size(int): size of input
                Input:
                    - **H** of shape `(passage_legth, batch, input_size)`: a float tensor in which we determine
                    the importance of information in the passage regarding a question
                    - **question** of shape `(question_length, batch, input_size)`: a float tensor containing question
                    representation
                Output:
                    - start(torch.tensor of shape (batch_size, passage_length, 1)): start position of the answer
                    - end(torch.tensor of shape (batch_size, passage_length, 1)): end position of the answer

        """
        super(PointerNetworkNew, self).__init__()
        self.Whp = nn.Linear(input_size, attn_size, bias=False)
        self.Wha1 = nn.Linear(model_dim, attn_size, bias=False)
        self.v = nn.Linear(attn_size, 1, bias=False)
        self.cell = nn.GRUCell(input_size, model_dim, False)
        self.Wuq = nn.Linear(model_dim, attn_size, bias=False)
        self.v1 = nn.Linear(attn_size, 1)

    def get_initial_state(self, question):
        s = torch.tanh(self.Wuq(question))
        s = self.v1(s)
        a = nn.functional.softmax(s, 0)
        r = a * question
        return r.sum(0)

    def forward(self, input_0, input_1):
        primals_1 = self.Whp.weight
        primals_5 = self.Wha1.weight
        primals_3 = self.v.weight
        primals_9 = self.cell.weight_ih
        primals_10 = self.cell.weight_hh
        primals_7 = self.Wuq.weight
        primals_8 = self.v1.weight
        primals_4 = self.v1.bias
        primals_2 = 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])
        return output[0], output[1]


Measured latency_ms: 0.022358