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
import torch.onnx.operators
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_mean_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tl.load(in_ptr0 + (16 + x0), xmask)
    tmp3 = tl.load(in_ptr0 + (32 + x0), xmask)
    tmp5 = tl.load(in_ptr0 + (48 + x0), xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp7 = 4.0
    tmp8 = tmp6 / tmp7
    tl.store(out_ptr0 + x0, tmp8, xmask)


@triton.jit
def triton_poi_fused_cat_1(in_ptr0, in_ptr1, in_ptr2, 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
    x3 = xindex // 8
    x4 = 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 = tl.load(in_ptr1 + x0, tmp4 & xmask, eviction_policy='evict_last',
        other=0.0)
    tmp7 = tmp5 + tmp6
    tmp8 = tl.full([1], 0, tl.int32)
    tmp9 = triton_helpers.maximum(tmp8, tmp7)
    tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype)
    tmp11 = tl.where(tmp4, tmp9, tmp10)
    tmp12 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp15 = tl.load(in_ptr2 + (4 * x3 + (-4 + x0)), tmp12 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp16 = tl.where(tmp4, tmp11, tmp15)
    tl.store(out_ptr0 + x4, tmp16, xmask)


@triton.jit
def triton_poi_fused__softmax_relu_2(in_ptr0, out_ptr0, 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 + x2, xmask)
    tmp3 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp4 = triton_helpers.maximum(tmp1, tmp3)
    tmp6 = triton_helpers.maximum(tmp1, tmp5)
    tmp7 = triton_helpers.maximum(tmp4, tmp6)
    tmp9 = triton_helpers.maximum(tmp1, tmp8)
    tmp10 = triton_helpers.maximum(tmp7, tmp9)
    tmp12 = triton_helpers.maximum(tmp1, tmp11)
    tmp13 = triton_helpers.maximum(tmp10, tmp12)
    tmp14 = tmp2 - tmp13
    tmp15 = tl_math.exp(tmp14)
    tl.store(out_ptr0 + x2, tmp15, xmask)


@triton.jit
def triton_poi_fused__softmax_3(in_ptr0, out_ptr0, 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 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tl.load(in_ptr0 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr0 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (12 + x0), 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, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 8
    x1 = xindex // 8
    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 = tl.load(in_ptr1 + x0, tmp4 & xmask, eviction_policy='evict_last',
        other=0.0)
    tmp7 = tmp5 + tmp6
    tmp8 = tl.full([1], 0, tl.int32)
    tmp9 = triton_helpers.maximum(tmp8, tmp7)
    tmp10 = tl.full(tmp9.shape, 0.0, tmp9.dtype)
    tmp11 = tl.where(tmp4, tmp9, tmp10)
    tmp12 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp15 = tl.load(in_ptr2 + (4 * x1 + (-4 + x0)), tmp12 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp16 = tl.load(in_ptr3 + x1, tmp12 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp17 = tmp15 * tmp16
    tmp18 = tl.load(in_ptr2 + (16 + 4 * x1 + (-4 + x0)), tmp12 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp19 = tl.load(in_ptr3 + (4 + x1), tmp12 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp20 = tmp18 * tmp19
    tmp21 = tmp17 + tmp20
    tmp22 = tl.load(in_ptr2 + (32 + 4 * x1 + (-4 + x0)), tmp12 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp23 = tl.load(in_ptr3 + (8 + x1), tmp12 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp24 = tmp22 * tmp23
    tmp25 = tmp21 + tmp24
    tmp26 = tl.load(in_ptr2 + (48 + 4 * x1 + (-4 + x0)), tmp12 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp27 = tl.load(in_ptr3 + (12 + x1), tmp12 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp28 = tmp26 * tmp27
    tmp29 = tmp25 + tmp28
    tmp30 = tl.full(tmp29.shape, 0.0, tmp29.dtype)
    tmp31 = tl.where(tmp12, tmp29, tmp30)
    tmp32 = tl.where(tmp4, tmp11, tmp31)
    tl.store(out_ptr0 + x2, tmp32, xmask)


@triton.jit
def triton_poi_fused_relu_5(in_out_ptr0, in_ptr0, 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_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(in_out_ptr0 + x2, tmp4, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_6(in_ptr0, in_ptr1, out_ptr0,
    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 + 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) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (1, 8), (8, 1))
    assert_size_stride(primals_5, (1,), (1,))
    assert_size_stride(primals_6, (4, 8), (8, 1))
    assert_size_stride(primals_7, (4,), (1,))
    assert_size_stride(primals_8, (4, 4), (4, 1))
    assert_size_stride(primals_9, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mean_0[grid(16)](primals_1, buf0, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf0, reinterpret_tensor(primals_2, (4, 4), (1, 4
            ), 0), out=buf1)
        del primals_2
        buf2 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32)
        triton_poi_fused_cat_1[grid(128)](buf1, primals_3, primals_1, buf2,
            128, XBLOCK=128, num_warps=4, num_stages=1)
        buf4 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_5, reinterpret_tensor(buf2, (16, 8), (
            8, 1), 0), reinterpret_tensor(primals_4, (8, 1), (1, 8), 0),
            alpha=1, beta=1, out=buf4)
        del primals_5
        buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_relu_2[grid(16)](buf4, buf5, 16, XBLOCK=
            16, num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_3[grid(16)](buf5, buf6, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf7 = empty_strided_cuda((4, 8), (8, 1), torch.float32)
        triton_poi_fused_cat_4[grid(32)](buf1, primals_3, primals_1, buf6,
            buf7, 32, XBLOCK=32, num_warps=1, num_stages=1)
        buf8 = reinterpret_tensor(buf6, (4, 4), (4, 1), 0)
        del buf6
        extern_kernels.mm(buf7, reinterpret_tensor(primals_6, (8, 4), (1, 8
            ), 0), out=buf8)
        buf9 = buf8
        del buf8
        triton_poi_fused_relu_5[grid(16)](buf9, primals_7, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del primals_7
        buf10 = reinterpret_tensor(buf5, (4, 4), (4, 1), 0)
        del buf5
        extern_kernels.addmm(primals_9, buf9, reinterpret_tensor(primals_8,
            (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf10)
        del primals_9
        buf11 = empty_strided_cuda((4, 4), (4, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_6[grid(16)](buf1,
            primals_3, buf11, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del buf1
        del primals_3
    return buf10, reinterpret_tensor(primals_1, (4, 4, 4), (16, 4, 1), 0
        ), buf0, reinterpret_tensor(buf2, (16, 8), (8, 1), 0
        ), buf4, buf7, buf9, primals_8, primals_6, primals_4, buf11


class WordPredictorNew(nn.Module):

    def __init__(self, encoder_output_dim, hidden_dim, output_dim):
        super().__init__()
        self.encoder_output_dim = encoder_output_dim
        self.hidden_dim = hidden_dim
        self.output_dim = output_dim
        self.init_layer = nn.Linear(encoder_output_dim, encoder_output_dim)
        self.attn_layer = nn.Linear(2 * encoder_output_dim, 1)
        self.hidden_layer = nn.Linear(2 * encoder_output_dim, hidden_dim)
        self.output_layer = nn.Linear(hidden_dim, output_dim)

    def _get_init_state(self, encoder_hiddens):
        x = torch.mean(encoder_hiddens, 0)
        x = F.relu(self.init_layer(x))
        return x

    def _attention(self, encoder_hiddens, init_state):
        init_state = init_state.unsqueeze(0).expand_as(encoder_hiddens)
        attn_input = torch.cat([init_state, encoder_hiddens], 2)
        attn_scores = F.relu(self.attn_layer(attn_input))
        attn_scores = F.softmax(attn_scores, 0)
        return attn_scores

    def get_normalized_probs(self, net_output, log_probs):
        """Get normalized probabilities (or log probs) from a net's output."""
        logits = net_output
        if log_probs:
            return F.log_softmax(logits, dim=1)
        else:
            return F.softmax(logits, dim=1)

    def forward(self, input_0):
        primals_2 = self.init_layer.weight
        primals_3 = self.init_layer.bias
        primals_4 = self.attn_layer.weight
        primals_5 = self.attn_layer.bias
        primals_6 = self.hidden_layer.weight
        primals_7 = self.hidden_layer.bias
        primals_8 = self.output_layer.weight
        primals_9 = self.output_layer.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])
        return output[0]


Measured latency_ms: 0.132047