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


@triton.jit
def triton_poi_fused_clone_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    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 % 4
    x3 = xindex // 64
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 4 * x2 + 16 * x1 + 64 * x3), xmask)
    tl.store(out_ptr0 + x4, tmp0, xmask)


@triton.jit
def triton_poi_fused__softmax_2(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    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_3(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    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_cat_4(in_ptr0, in_ptr1, 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
    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.full([1], 0, tl.int32)
    tmp7 = triton_helpers.maximum(tmp6, tmp5)
    tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype)
    tmp9 = tl.where(tmp4, tmp7, tmp8)
    tmp10 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp13 = tl.load(in_ptr1 + (4 * x1 + (-4 + x0)), tmp10 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp14 = tl.where(tmp4, tmp9, tmp13)
    tl.store(out_ptr0 + x2, tmp14, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_5(in_out_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
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp3 = 0.0
    tmp4 = tmp2 <= tmp3
    tl.store(in_out_ptr0 + x0, tmp2, xmask)
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused_relu_threshold_backward_6(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
    tmp0 = tl.load(in_ptr0 + x0, xmask)
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp3 = 0.0
    tmp4 = tmp2 <= tmp3
    tl.store(out_ptr0 + x0, tmp4, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (16, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4, 4), (16, 4, 1))
    assert_size_stride(primals_3, (4, 16), (16, 1))
    assert_size_stride(primals_4, (4, 8), (8, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((16, 16), (16, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(primals_2, (16, 4), (4, 1), 0),
            reinterpret_tensor(primals_1, (4, 16), (1, 4), 0), out=buf0)
        del primals_1
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_repeat_0[grid(256)](primals_2, buf1, 256, XBLOCK=
            256, num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_clone_1[grid(256)](buf0, buf2, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf3 = reinterpret_tensor(buf0, (16, 4, 4), (16, 4, 1), 0)
        del buf0
        extern_kernels.bmm(reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(buf1, (16, 4, 4), (16, 1, 4), 0), out=buf3)
        buf4 = buf2
        del buf2
        triton_poi_fused__softmax_2[grid(256)](buf3, buf4, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf5 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused__softmax_3[grid(256)](buf4, buf5, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf6 = reinterpret_tensor(buf4, (16, 4, 4), (16, 4, 1), 0)
        del buf4
        extern_kernels.bmm(reinterpret_tensor(buf5, (16, 4, 4), (16, 4, 1),
            0), reinterpret_tensor(buf1, (16, 4, 4), (16, 4, 1), 0), out=buf6)
        buf7 = buf5
        del buf5
        triton_poi_fused_clone_1[grid(256)](buf6, buf7, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        del buf6
        buf8 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf7, (16, 16), (16, 1), 0),
            reinterpret_tensor(primals_3, (16, 4), (1, 16), 0), out=buf8)
        buf9 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32)
        triton_poi_fused_cat_4[grid(128)](buf8, primals_2, buf9, 128,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf10 = empty_strided_cuda((16, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf9, (16, 8), (8, 1), 0),
            reinterpret_tensor(primals_4, (8, 4), (1, 8), 0), out=buf10)
        buf11 = reinterpret_tensor(buf10, (4, 4, 4), (16, 4, 1), 0)
        del buf10
        buf12 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_5[grid(64)](buf11, buf12, 
            64, XBLOCK=64, num_warps=1, num_stages=1)
        buf13 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.bool)
        triton_poi_fused_relu_threshold_backward_6[grid(64)](buf8, buf13, 
            64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf8
    return buf11, reinterpret_tensor(primals_2, (16, 4), (4, 1), 0
        ), buf1, buf3, reinterpret_tensor(buf7, (16, 16), (16, 1), 0
        ), reinterpret_tensor(buf9, (16, 8), (8, 1), 0
        ), buf12, primals_4, buf13, primals_3


class SelfAttentionNew(torch.nn.Module):

    def __init__(self, num_heads, model_dim, dropout_keep_prob):
        super(SelfAttentionNew, self).__init__()
        self.num_heads = num_heads
        self.model_dim = model_dim
        self.dropout_keep_prob = dropout_keep_prob
        self.q_layer = torch.nn.Linear(model_dim, model_dim * self.
            num_heads, bias=False)
        self.out_layer = torch.nn.Linear(model_dim * self.num_heads,
            model_dim, bias=False)
        self.out_layer2 = torch.nn.Linear(model_dim * 2, model_dim, bias=False)
        self.relu = torch.nn.ReLU()
        self.softmax = torch.nn.Softmax(dim=-1)
        self.dropout = torch.nn.Dropout(1 - dropout_keep_prob)

    def _linear_projection(self, batched_inputs):
        q = self.q_layer(batched_inputs)
        return q

    def _split_heads(self, q):

        def split_last_dimension_then_transpose(tensor, num_heads, dim):
            tensor = tensor.view([-1, tensor.size()[1], num_heads, dim])
            return tensor.transpose(1, 2)
        qs = split_last_dimension_then_transpose(q, self.num_heads, self.
            model_dim)
        return qs

    def _scaled_dot_product(self, qs, ks, tiled_inputs, valid_mask):
        queries_dot_keys = torch.matmul(qs, ks.transpose(2, 3))
        scaled_scores = queries_dot_keys
        if valid_mask is not None:
            mask = torch.log(valid_mask.view(valid_mask.size()[0], 1, 1,
                valid_mask.size()[1]))
            scaled_scores += mask
        attention_weights = self.softmax(scaled_scores)
        return torch.matmul(attention_weights, tiled_inputs)

    def _concat_heads(self, outputs):
        max_contexts = outputs.size()[2]
        tensor = outputs.transpose(1, 2)
        return tensor.contiguous().view([-1, max_contexts, self.model_dim *
            self.num_heads])

    def forward(self, input_0):
        primals_1 = self.q_layer.weight
        primals_3 = self.out_layer.weight
        primals_4 = self.out_layer2.weight
        primals_2 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.139952