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
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
    x1 = xindex // 4 % 4
    x2 = xindex // 16
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1), xmask)
    tl.store(out_ptr0 + x3, tmp0, xmask)


@triton.jit
def triton_poi_fused__softmax_eq_masked_fill_1(in_ptr0, in_ptr1, 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
    x1 = xindex // 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + 4 * x2, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (1 + 4 * x2), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp13 = tl.load(in_ptr1 + (2 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp16 = tl.load(in_ptr0 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp18 = tl.load(in_ptr1 + (3 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp1 = 0.0
    tmp2 = tmp0 == tmp1
    tmp4 = -10000000000.0
    tmp5 = tl.where(tmp2, tmp4, tmp3)
    tmp7 = tmp6 == tmp1
    tmp9 = tl.where(tmp7, tmp4, tmp8)
    tmp10 = triton_helpers.maximum(tmp5, tmp9)
    tmp12 = tmp11 == tmp1
    tmp14 = tl.where(tmp12, tmp4, tmp13)
    tmp15 = triton_helpers.maximum(tmp10, tmp14)
    tmp17 = tmp16 == tmp1
    tmp19 = tl.where(tmp17, tmp4, tmp18)
    tmp20 = triton_helpers.maximum(tmp15, tmp19)
    tmp21 = tmp5 - tmp20
    tmp22 = tl_math.exp(tmp21)
    tmp23 = tmp9 - tmp20
    tmp24 = tl_math.exp(tmp23)
    tmp25 = tmp22 + tmp24
    tmp26 = tmp14 - tmp20
    tmp27 = tl_math.exp(tmp26)
    tmp28 = tmp25 + tmp27
    tmp29 = tmp19 - tmp20
    tmp30 = tl_math.exp(tmp29)
    tmp31 = tmp28 + tmp30
    tl.store(out_ptr0 + x2, tmp20, xmask)
    tl.store(out_ptr1 + x2, tmp31, xmask)


@triton.jit
def triton_poi_fused__softmax_eq_masked_fill_2(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, 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
    x4 = xindex // 4
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2), xmask, eviction_policy='evict_last'
        )
    tmp3 = tl.load(in_out_ptr0 + x3, xmask)
    tmp6 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
    tmp1 = 0.0
    tmp2 = tmp0 == tmp1
    tmp4 = -10000000000.0
    tmp5 = tl.where(tmp2, tmp4, tmp3)
    tmp7 = tmp5 - tmp6
    tmp8 = tl_math.exp(tmp7)
    tmp10 = tmp8 / tmp9
    tl.store(in_out_ptr0 + x3, tmp10, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = 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, (4, 4), (4, 1))
    assert_size_stride(primals_4, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clone_0[grid(64)](primals_1, buf0, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf0, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_3, (4, 4), (1, 4), 0), out=buf1)
        del primals_3
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_clone_0[grid(64)](primals_2, buf2, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del primals_2
        buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf1, (4, 4, 4), (16, 4, 1), 
            0), reinterpret_tensor(buf2, (4, 4, 4), (16, 1, 4), 0), out=buf3)
        del buf1
        buf4 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_eq_masked_fill_1[grid(16)](primals_4,
            buf3, buf4, buf5, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf6 = buf3
        del buf3
        triton_poi_fused__softmax_eq_masked_fill_2[grid(64)](buf6,
            primals_4, buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf4
        del buf5
    return buf6, primals_4, reinterpret_tensor(buf0, (16, 4), (4, 1), 0
        ), buf6, buf2


class AttentionNew(nn.Module):

    def __init__(self, hidden_size):
        super(AttentionNew, self).__init__()
        self.hidden_size = hidden_size
        self.linear_in = nn.Linear(hidden_size, hidden_size, bias=False)

    def score(self, hidden_state, encoder_states):
        """
        Args:
            hidden_state (tgt_len(=1), batch, hidden_size)
            encoder_states (src_len, batch, hidden_size)
        Return:
            score (batch, tgt_len(=1), src_len)
        """
        h_t = hidden_state.transpose(0, 1).contiguous()
        h_s = encoder_states.transpose(0, 1).contiguous()
        _src_batch, _src_len, _src_dim = h_s.size()
        tgt_batch, tgt_len, tgt_dim = h_t.size()
        h_t_ = h_t.view(tgt_batch * tgt_len, tgt_dim)
        h_t_ = self.linear_in(h_t_)
        h_t = h_t_.view(tgt_batch, tgt_len, tgt_dim)
        h_s_ = h_s.transpose(1, 2)
        score = torch.bmm(h_t, h_s_)
        return score

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


Measured latency_ms: 0.0713