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.utils.data
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, in_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    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 = libdevice.tanh(tmp2)
    tl.store(in_out_ptr0 + x2, tmp3, xmask)


@triton.jit
def triton_poi_fused_exp_max_mul_sub_sum_1(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex // 4
    x2 = xindex // 16
    x4 = xindex % 16
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x4 + 64 * x2), xmask)
    tmp3 = tl.load(in_ptr0 + (16 + x3), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (16 + x4 + 64 * x2), xmask)
    tmp7 = tl.load(in_ptr0 + (32 + x3), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (32 + x4 + 64 * x2), xmask)
    tmp11 = tl.load(in_ptr0 + (48 + x3), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (48 + x4 + 64 * x2), xmask)
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = triton_helpers.maximum(tmp2, tmp5)
    tmp9 = tmp7 * tmp8
    tmp10 = triton_helpers.maximum(tmp6, tmp9)
    tmp13 = tmp11 * tmp12
    tmp14 = triton_helpers.maximum(tmp10, tmp13)
    tmp15 = tmp2 - tmp14
    tmp16 = tl_math.exp(tmp15)
    tmp17 = tmp16 * tmp1
    tmp18 = tmp5 - tmp14
    tmp19 = tl_math.exp(tmp18)
    tmp20 = tmp19 * tmp4
    tmp21 = tmp17 + tmp20
    tmp22 = tmp9 - tmp14
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tmp23 * tmp8
    tmp25 = tmp21 + tmp24
    tmp26 = tmp13 - tmp14
    tmp27 = tl_math.exp(tmp26)
    tmp28 = tmp27 * tmp12
    tmp29 = tmp25 + tmp28
    tl.store(out_ptr0 + x5, tmp14, xmask)
    tl.store(out_ptr1 + x5, tmp29, xmask)


@triton.jit
def triton_poi_fused_mul_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x5 = xindex % 256
    x1 = xindex // 4 % 4
    x6 = xindex // 64
    x3 = xindex // 64 % 4
    x4 = xindex // 256
    x7 = xindex % 16
    x8 = xindex
    tmp0 = tl.load(in_ptr0 + x5, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x1 + 4 * x6), xmask, eviction_policy='evict_last'
        )
    tmp2 = tl.load(in_ptr2 + (x7 + 16 * x4 + 64 * x3), xmask,
        eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr3 + (x7 + 16 * x3), xmask, eviction_policy=
        'evict_last')
    tmp8 = tl.load(in_ptr4 + (x7 + 16 * x3), xmask, eviction_policy=
        'evict_last')
    tmp3 = tmp1 * tmp2
    tmp5 = tmp3 - tmp4
    tmp6 = tl_math.exp(tmp5)
    tmp7 = tmp6 * tmp2
    tmp9 = 1e-06
    tmp10 = tmp8 + tmp9
    tmp11 = tmp7 / tmp10
    tmp12 = tmp0 * tmp11
    tl.store(out_ptr0 + x8, tmp12, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4,), (1,))
    assert_size_stride(primals_3, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_4, (1, 4), (4, 1))
    assert_size_stride(primals_5, (1,), (1,))
    assert_size_stride(primals_6, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_3, (64, 4), (4, 1), 0),
            reinterpret_tensor(primals_1, (4, 4), (1, 4), 0), out=buf0)
        del primals_1
        buf1 = reinterpret_tensor(buf0, (4, 4, 4, 4), (64, 16, 4, 1), 0)
        del buf0
        get_raw_stream(0)
        triton_poi_fused_tanh_0[grid(256)](buf1, primals_2, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        del primals_2
        buf3 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_5, reinterpret_tensor(buf1, (64, 4), (
            4, 1), 0), reinterpret_tensor(primals_4, (4, 1), (1, 4), 0),
            alpha=1, beta=1, out=buf3)
        del primals_5
        buf4 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        buf5 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        triton_poi_fused_exp_max_mul_sub_sum_1[grid(64)](buf3, primals_6,
            buf4, buf5, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.float32)
        triton_poi_fused_mul_2[grid(1024)](primals_3, buf3, primals_6, buf4,
            buf5, buf6, 1024, XBLOCK=128, num_warps=4, num_stages=1)
        del buf4
        del buf5
    return buf6, primals_3, primals_6, buf1, buf3, primals_4


def masked_softmax(x, m=None, dim=-1):
    """
    Softmax with mask
    :param x:
    :param m:
    :param dim:
    :return:
    """
    if m is not None:
        m = m.float()
        x = x * m
    e_x = torch.exp(x - torch.max(x, dim=dim, keepdim=True)[0])
    if m is not None:
        e_x = e_x * m
    softmax = e_x / (torch.sum(e_x, dim=dim, keepdim=True) + 1e-06)
    return softmax


class SelfAttentionGatedNew(torch.nn.Module):
    """
    Self-Attention Gated layer, it`s not weighted sum in the last, but just weighted
    math: \\softmax(W*	anh(W*x)) * x

    Args:
        input_size: The number of expected features in the input x

    Inputs: input, mask
        - **input** (seq_len, batch, input_size): tensor containing the features
          of the input sequence.
        - **mask** (batch, seq_len): tensor show whether a padding index for each element in the batch.

    Outputs: output
        - **output** (seq_len, batch, input_size): gated output tensor
    """

    def __init__(self, input_size):
        super(SelfAttentionGatedNew, self).__init__()
        self.linear_g = torch.nn.Linear(input_size, input_size)
        self.linear_t = torch.nn.Linear(input_size, 1)

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


Measured latency_ms: 0.043978