import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import *


class AttentionLayer(nn.Module):

    def __init__(self, hidden_dim_en, hidden_dim_de, projected_size):
        super(AttentionLayer, self).__init__()
        self.linear1 = nn.Linear(hidden_dim_en, projected_size)
        self.linear2 = nn.Linear(hidden_dim_de, projected_size)
        self.linear3 = nn.Linear(projected_size, 1, False)

    def forward(self, out_e, h):
        """
        out_e: batch_size * num_frames * en_hidden_dim
        h : batch_size * de_hidden_dim
        """
        assert out_e.size(0) == h.size(0)
        batch_size, num_frames, _ = out_e.size()
        hidden_dim = h.size(1)
        h_att = h.unsqueeze(1).expand(batch_size, num_frames, hidden_dim)
        x = F.tanh(F.dropout(self.linear1(out_e)) + F.dropout(self.linear2(
            h_att)))
        x = F.dropout(self.linear3(x))
        a = F.softmax(x.squeeze(2))
        return a


def get_inputs():
    return [torch.rand([4, 4, 4]), torch.rand([4, 4])]


def get_init_inputs():
    return [[], {'hidden_dim_en': 4, 'hidden_dim_de': 4, 'projected_size': 4}]

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.nn as nn
from torch.autograd import *
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, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x2 = xindex // 16
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tl.store(out_ptr0 + x3, tmp0, xmask)


@triton.jit
def triton_poi_fused_add_1(in_out_ptr0, in_ptr0, 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_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x2, tmp2, xmask)


@triton.jit
def triton_poi_fused_add_tanh_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 64
    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 = tl.load(in_ptr0 + x0, xmask)
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.tanh(tmp2)
    tl.store(in_out_ptr0 + x0, tmp3, 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
    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_4(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
    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)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (1, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_4, reinterpret_tensor(primals_1, (16, 
            4), (4, 1), 0), reinterpret_tensor(primals_3, (4, 4), (1, 4), 0
            ), alpha=1, beta=1, out=buf0)
        del primals_3
        del primals_4
        buf1 = torch.ops.aten.native_dropout.default(reinterpret_tensor(
            buf0, (4, 4, 4), (16, 4, 1), 0), 0.5, True)
        buf2 = buf1[0]
        buf3 = buf1[1]
        del buf1
        buf4 = reinterpret_tensor(buf0, (4, 4, 4), (16, 4, 1), 0)
        del buf0
        get_raw_stream(0)
        triton_poi_fused_clone_0[grid(64)](primals_2, buf4, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del primals_2
        buf5 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf4, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_5, (4, 4), (1, 4), 0), out=buf5)
        del primals_5
        buf6 = reinterpret_tensor(buf5, (4, 4, 4), (16, 4, 1), 0)
        del buf5
        triton_poi_fused_add_1[grid(64)](buf6, primals_6, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del primals_6
        buf7 = torch.ops.aten.native_dropout.default(buf6, 0.5, True)
        del buf6
        buf8 = buf7[0]
        buf9 = buf7[1]
        del buf7
        buf10 = buf2
        del buf2
        triton_poi_fused_add_tanh_2[grid(64)](buf10, buf8, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf8
        buf11 = empty_strided_cuda((16, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf10, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_7, (4, 1), (1, 4), 0), out=buf11)
        buf12 = torch.ops.aten.native_dropout.default(reinterpret_tensor(
            buf11, (4, 4, 1), (4, 1, 1), 0), 0.5, True)
        buf13 = buf12[0]
        buf14 = buf12[1]
        del buf12
        buf15 = reinterpret_tensor(buf11, (4, 4), (4, 1), 0)
        del buf11
        triton_poi_fused__softmax_3[grid(16)](buf13, buf15, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf16 = reinterpret_tensor(buf13, (4, 4), (4, 1), 0)
        del buf13
        triton_poi_fused__softmax_4[grid(16)](buf15, buf16, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf15
    return buf16, reinterpret_tensor(primals_1, (16, 4), (4, 1), 0
        ), buf3, reinterpret_tensor(buf4, (16, 4), (4, 1), 0
        ), buf9, buf10, buf14, buf16, primals_7


class AttentionLayerNew(nn.Module):

    def __init__(self, hidden_dim_en, hidden_dim_de, projected_size):
        super(AttentionLayerNew, self).__init__()
        self.linear1 = nn.Linear(hidden_dim_en, projected_size)
        self.linear2 = nn.Linear(hidden_dim_de, projected_size)
        self.linear3 = nn.Linear(projected_size, 1, False)

    def forward(self, input_0, input_1):
        primals_2 = self.linear1.weight
        primals_4 = self.linear1.bias
        primals_3 = self.linear2.weight
        primals_6 = self.linear2.bias
        primals_7 = self.linear3.weight
        primals_1 = input_0
        primals_5 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0]
