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
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_mm_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_mm_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last')
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_mm_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_mm_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tl.store(out_ptr0 + x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_add_tanh_4(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, out_ptr1, out_ptr2, out_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp4 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = libdevice.tanh(tmp2)
    tmp5 = tmp4 + tmp1
    tmp6 = libdevice.tanh(tmp5)
    tmp8 = tmp7 + tmp1
    tmp9 = libdevice.tanh(tmp8)
    tmp11 = tmp10 + tmp1
    tmp12 = libdevice.tanh(tmp11)
    tl.store(out_ptr0 + x2, tmp3, xmask)
    tl.store(out_ptr1 + x2, tmp6, xmask)
    tl.store(out_ptr2 + x2, tmp9, xmask)
    tl.store(out_ptr3 + x2, tmp12, xmask)


@triton.jit
def triton_poi_fused__softmax_5(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_6(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)


@triton.jit
def triton_poi_fused_mul_sum_7(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
    x1 = xindex // 4
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp9 = tmp7 * tmp8
    tmp10 = tmp6 + tmp9
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tl.store(out_ptr0 + x2, tmp14, 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, 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, 4), (4, 1))
    assert_size_stride(primals_5, (1, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_mm_0[grid(4)](primals_1, buf0, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf0, reinterpret_tensor(primals_2, (4, 4), (1, 4
            ), 0), out=buf1)
        buf2 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(primals_4, reinterpret_tensor(primals_3, (4, 4),
            (1, 4), 0), out=buf2)
        del primals_3
        buf10 = buf0
        del buf0
        triton_poi_fused_mm_1[grid(4)](primals_1, buf10, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        buf11 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf10, reinterpret_tensor(primals_2, (4, 4), (1, 
            4), 0), out=buf11)
        buf4 = buf10
        del buf10
        triton_poi_fused_mm_2[grid(4)](primals_1, buf4, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        buf5 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf4, reinterpret_tensor(primals_2, (4, 4), (1, 4
            ), 0), out=buf5)
        buf7 = buf4
        del buf4
        triton_poi_fused_mm_3[grid(4)](primals_1, buf7, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        buf8 = empty_strided_cuda((1, 4), (4, 1), torch.float32)
        extern_kernels.mm(buf7, reinterpret_tensor(primals_2, (4, 4), (1, 4
            ), 0), out=buf8)
        del buf7
        del primals_2
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf12 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_tanh_4[grid(16)](buf1, buf2, buf5, buf8, buf11,
            buf3, buf6, buf9, buf12, 16, XBLOCK=16, num_warps=1, num_stages=1)
        del buf1
        del buf11
        del buf5
        del buf8
        buf17 = buf2
        del buf2
        buf13 = reinterpret_tensor(buf17, (4, 1), (4, 1), 0)
        extern_kernels.mm(buf3, reinterpret_tensor(primals_5, (4, 1), (1, 4
            ), 0), out=buf13)
        buf14 = reinterpret_tensor(buf17, (4, 1), (4, 1), 1)
        extern_kernels.mm(buf6, reinterpret_tensor(primals_5, (4, 1), (1, 4
            ), 0), out=buf14)
        buf15 = reinterpret_tensor(buf17, (4, 1), (4, 1), 2)
        extern_kernels.mm(buf9, reinterpret_tensor(primals_5, (4, 1), (1, 4
            ), 0), out=buf15)
        buf16 = reinterpret_tensor(buf17, (4, 1), (4, 1), 3)
        extern_kernels.mm(buf12, reinterpret_tensor(primals_5, (4, 1), (1, 
            4), 0), out=buf16)
        buf18 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused__softmax_5[grid(16)](buf17, buf18, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf13
        del buf14
        del buf15
        del buf16
        buf19 = buf17
        del buf17
        triton_poi_fused__softmax_6[grid(16)](buf18, buf19, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf20 = buf18
        del buf18
        triton_poi_fused_mul_sum_7[grid(16)](buf19, primals_1, buf20, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
    return buf20, reinterpret_tensor(buf19, (4, 4, 1), (4, 1, 1), 0
        ), primals_1, primals_4, reinterpret_tensor(primals_1, (1, 4), (16,
        4), 0), buf3, reinterpret_tensor(primals_1, (1, 4), (16, 4), 1
        ), buf6, reinterpret_tensor(primals_1, (1, 4), (16, 4), 2
        ), buf9, reinterpret_tensor(primals_1, (1, 4), (16, 4), 3
        ), buf12, buf19, primals_5


class AdditiveAttentionNew(nn.Module):

    def __init__(self, encoder_hidden_state_dim, decoder_hidden_state_dim,
        internal_dim=None):
        super(AdditiveAttentionNew, self).__init__()
        if internal_dim is None:
            internal_dim = int((encoder_hidden_state_dim +
                decoder_hidden_state_dim) / 2)
        self.w1 = nn.Linear(encoder_hidden_state_dim, internal_dim, bias=False)
        self.w2 = nn.Linear(decoder_hidden_state_dim, internal_dim, bias=False)
        self.v = nn.Linear(internal_dim, 1, bias=False)

    def score(self, encoder_state, decoder_state):
        return self.v(torch.tanh(self.w1(encoder_state) + self.w2(
            decoder_state)))

    def forward(self, input_0, input_1):
        primals_1 = self.w1.weight
        primals_2 = self.w2.weight
        primals_5 = self.v.weight
        primals_3 = input_0
        primals_4 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0], output[1]


Measured latency_ms: 0.076634