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_cat_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 8
    x1 = xindex // 8 % 16
    x2 = xindex // 128
    x3 = 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 * x0 + 16 * x2 + x1 // 4), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr0 + (4 * (-4 + x0) + 16 * x2 + x1 % 4), tmp6 &
        xmask, eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tl.store(out_ptr0 + x3, tmp10, xmask)


@triton.jit
def triton_poi_fused_leaky_relu_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 8
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = 0.0
    tmp4 = tmp2 > tmp3
    tmp5 = 4.0
    tmp6 = tmp2 * tmp5
    tmp7 = tl.where(tmp4, tmp2, tmp6)
    tl.store(out_ptr0 + x2, tmp4, xmask)
    tl.store(out_ptr1 + x2, tmp7, xmask)


@triton.jit
def triton_poi_fused__softmax_2(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
    x2 = xindex
    x0 = xindex % 4
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + 4 * x2, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (1 + 4 * x0 + 16 * x1 + 16 * ((1 + 4 * x0) // 
        16)), xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr0 + (2 + 4 * x0 + 16 * x1 + 16 * ((1 + 2 * x0) // 
        8)), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr0 + (3 + 4 * x0 + 16 * x1 + 16 * ((3 + 4 * x0) //
        16)), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    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 = tmp5 - tmp14
    tmp18 = tl_math.exp(tmp17)
    tmp19 = tmp16 + tmp18
    tmp20 = tmp9 - tmp14
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp19 + tmp21
    tmp23 = tmp13 - tmp14
    tmp24 = tl_math.exp(tmp23)
    tmp25 = tmp22 + tmp24
    tl.store(out_ptr0 + x2, tmp14, xmask)
    tl.store(out_ptr1 + x2, tmp25, xmask)


@triton.jit
def triton_poi_fused__softmax_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x4 = xindex
    x3 = xindex % 16
    x5 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x4, xmask)
    tmp1 = tl.load(in_ptr1 + x3, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x5, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + x5, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 - tmp3
    tmp5 = tl_math.exp(tmp4)
    tmp7 = tmp5 / tmp6
    tl.store(out_ptr0 + x4, tmp7, xmask)


@triton.jit
def triton_poi_fused_sigmoid_sigmoid_backward_4(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.sigmoid(tmp0)
    tmp2 = 1.0
    tmp3 = tmp2 - tmp1
    tmp4 = tmp1 * tmp3
    tl.store(in_out_ptr0 + x0, tmp1, xmask)
    tl.store(out_ptr0 + x0, tmp4, 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), (16, 4, 1))
    assert_size_stride(primals_2, (8, 8), (8, 1))
    assert_size_stride(primals_3, (8,), (1,))
    assert_size_stride(primals_4, (8, 1), (1, 1))
    assert_size_stride(primals_5, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 16, 8), (128, 8, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(512)](primals_1, buf0, 512, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((64, 8), (8, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf0, (64, 8), (8, 1), 0),
            reinterpret_tensor(primals_2, (8, 8), (1, 8), 0), out=buf1)
        del primals_2
        buf2 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.bool)
        buf3 = empty_strided_cuda((4, 4, 4, 8), (128, 32, 8, 1), torch.float32)
        triton_poi_fused_leaky_relu_1[grid(512)](buf1, primals_3, buf2,
            buf3, 512, XBLOCK=256, num_warps=4, num_stages=1)
        del buf1
        del primals_3
        buf4 = empty_strided_cuda((64, 1), (1, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf3, (64, 8), (8, 1), 0),
            primals_4, out=buf4)
        buf5 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        buf6 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused__softmax_2[grid(16)](buf4, primals_5, buf5, buf6, 
            16, XBLOCK=16, num_warps=1, num_stages=1)
        buf7 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused__softmax_3[grid(64)](buf4, primals_5, buf5, buf6,
            buf7, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf5
        del buf6
        del primals_5
        buf8 = reinterpret_tensor(buf4, (4, 4, 4), (16, 4, 1), 0)
        del buf4
        extern_kernels.bmm(buf7, reinterpret_tensor(primals_1, (4, 4, 4), (
            16, 1, 4), 0), out=buf8)
        buf9 = buf8
        del buf8
        buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_sigmoid_sigmoid_backward_4[grid(64)](buf9, buf10, 
            64, XBLOCK=64, num_warps=1, num_stages=1)
    return reinterpret_tensor(buf9, (4, 4, 4), (16, 1, 4), 0
        ), reinterpret_tensor(buf0, (64, 8), (8, 1), 0
        ), buf2, buf7, buf10, primals_1, reinterpret_tensor(buf3, (8, 64),
        (1, 8), 0), reinterpret_tensor(primals_4, (1, 8), (1, 1), 0)


class FeatureAttentionLayerNew(nn.Module):
    """Single Graph Feature/Spatial Attention Layer
    :param n_features: Number of input features/nodes
    :param window_size: length of the input sequence
    :param dropout: percentage of nodes to dropout
    :param alpha: negative slope used in the leaky rely activation function
    :param embed_dim: embedding dimension (output dimension of linear transformation)
    :param use_gatv2: whether to use the modified attention mechanism of GATv2 instead of standard GAT
    :param use_bias: whether to include a bias term in the attention layer
    """

    def __init__(self, n_features, window_size, dropout, alpha, embed_dim=
        None, use_gatv2=True, use_bias=True):
        super(FeatureAttentionLayerNew, self).__init__()
        self.n_features = n_features
        self.window_size = window_size
        self.dropout = dropout
        self.embed_dim = embed_dim if embed_dim is not None else window_size
        self.use_gatv2 = use_gatv2
        self.num_nodes = n_features
        self.use_bias = use_bias
        if self.use_gatv2:
            self.embed_dim *= 2
            lin_input_dim = 2 * window_size
            a_input_dim = self.embed_dim
        else:
            lin_input_dim = window_size
            a_input_dim = 2 * self.embed_dim
        self.lin = nn.Linear(lin_input_dim, self.embed_dim)
        self.a = nn.Parameter(torch.empty((a_input_dim, 1)))
        nn.init.xavier_uniform_(self.a.data, gain=1.414)
        if self.use_bias:
            self.bias = nn.Parameter(torch.empty(n_features, n_features))
        self.leakyrelu = nn.LeakyReLU(alpha)
        self.sigmoid = nn.Sigmoid()

    def _make_attention_input(self, v):
        """Preparing the feature attention mechanism.
        Creating matrix with all possible combinations of concatenations of node.
        Each node consists of all values of that node within the window
            v1 || v1,
            ...
            v1 || vK,
            v2 || v1,
            ...
            v2 || vK,
            ...
            ...
            vK || v1,
            ...
            vK || vK,
        """
        K = self.num_nodes
        blocks_repeating = v.repeat_interleave(K, dim=1)
        blocks_alternating = v.repeat(1, K, 1)
        combined = torch.cat((blocks_repeating, blocks_alternating), dim=2)
        if self.use_gatv2:
            return combined.view(v.size(0), K, K, 2 * self.window_size)
        else:
            return combined.view(v.size(0), K, K, 2 * self.embed_dim)

    def forward(self, input_0):
        primals_4 = self.a
        primals_5 = self.bias
        primals_2 = self.lin.weight
        primals_3 = self.lin.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0]


Measured latency_ms: 0.039093