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
import torch.nn.functional as F
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_elu_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 + x0, xmask)
    tmp1 = 0.0
    tmp2 = tmp0 > tmp1
    tmp3 = 1.0
    tmp4 = tmp0 * tmp3
    tmp5 = libdevice.expm1(tmp4)
    tmp6 = tmp5 * tmp3
    tmp7 = tl.where(tmp2, tmp4, tmp6)
    tl.store(out_ptr0 + x0, tmp7, xmask)


@triton.jit
def triton_poi_fused_add_mul_native_layer_norm_sigmoid_1(in_ptr0, in_ptr1,
    in_ptr2, out_ptr0, out_ptr1, 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')
    tmp2 = tl.load(in_ptr1 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr2 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr1 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr2 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp13 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp15 = tl.load(in_ptr1 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp17 = tl.load(in_ptr2 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp20 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp22 = tl.load(in_ptr1 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp24 = tl.load(in_ptr2 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp1 = tl.sigmoid(tmp0)
    tmp3 = tmp1 * tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tl.sigmoid(tmp6)
    tmp9 = tmp7 * tmp8
    tmp11 = tmp9 + tmp10
    tmp12 = tmp5 + tmp11
    tmp14 = tl.sigmoid(tmp13)
    tmp16 = tmp14 * tmp15
    tmp18 = tmp16 + tmp17
    tmp19 = tmp12 + tmp18
    tmp21 = tl.sigmoid(tmp20)
    tmp23 = tmp21 * tmp22
    tmp25 = tmp23 + tmp24
    tmp26 = tmp19 + tmp25
    tmp27 = 4.0
    tmp28 = tmp26 / tmp27
    tmp29 = tmp5 - tmp28
    tmp30 = tmp29 * tmp29
    tmp31 = tmp11 - tmp28
    tmp32 = tmp31 * tmp31
    tmp33 = tmp30 + tmp32
    tmp34 = tmp18 - tmp28
    tmp35 = tmp34 * tmp34
    tmp36 = tmp33 + tmp35
    tmp37 = tmp25 - tmp28
    tmp38 = tmp37 * tmp37
    tmp39 = tmp36 + tmp38
    tmp40 = tmp39 / tmp27
    tl.store(out_ptr0 + x0, tmp28, xmask)
    tl.store(out_ptr1 + x0, tmp40, xmask)


@triton.jit
def triton_poi_fused_add_mul_native_layer_norm_sigmoid_2(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, in_ptr5, in_ptr6, 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
    x0 = xindex % 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp2 = tl.load(in_ptr1 + x2, xmask)
    tmp4 = tl.load(in_ptr2 + x2, xmask)
    tmp6 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr4 + x1, xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr5 + x0, xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr6 + x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.sigmoid(tmp0)
    tmp3 = tmp1 * tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 - tmp6
    tmp9 = 1e-05
    tmp10 = tmp8 + tmp9
    tmp11 = libdevice.rsqrt(tmp10)
    tmp12 = tmp7 * tmp11
    tmp14 = tmp12 * tmp13
    tmp16 = tmp14 + tmp15
    tl.store(out_ptr0 + x2, tmp16, xmask)


@triton.jit
def triton_poi_fused__softmax_3(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_mean_4(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 + x0, xmask)
    tmp1 = tl.load(in_ptr0 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp3 = tl.load(in_ptr0 + 1)
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK])
    tmp6 = tl.load(in_ptr0 + 2)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK])
    tmp9 = tl.load(in_ptr0 + 3)
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK])
    tmp13 = tl.load(in_ptr0 + (4 + x0), xmask)
    tmp14 = tl.load(in_ptr0 + 4)
    tmp15 = tl.broadcast_to(tmp14, [XBLOCK])
    tmp16 = tl.load(in_ptr0 + 5)
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK])
    tmp19 = tl.load(in_ptr0 + 6)
    tmp20 = tl.broadcast_to(tmp19, [XBLOCK])
    tmp22 = tl.load(in_ptr0 + 7)
    tmp23 = tl.broadcast_to(tmp22, [XBLOCK])
    tmp27 = tl.load(in_ptr0 + (8 + x0), xmask)
    tmp28 = tl.load(in_ptr0 + 8)
    tmp29 = tl.broadcast_to(tmp28, [XBLOCK])
    tmp30 = tl.load(in_ptr0 + 9)
    tmp31 = tl.broadcast_to(tmp30, [XBLOCK])
    tmp33 = tl.load(in_ptr0 + 10)
    tmp34 = tl.broadcast_to(tmp33, [XBLOCK])
    tmp36 = tl.load(in_ptr0 + 11)
    tmp37 = tl.broadcast_to(tmp36, [XBLOCK])
    tmp41 = tl.load(in_ptr0 + (12 + x0), xmask)
    tmp42 = tl.load(in_ptr0 + 12)
    tmp43 = tl.broadcast_to(tmp42, [XBLOCK])
    tmp44 = tl.load(in_ptr0 + 13)
    tmp45 = tl.broadcast_to(tmp44, [XBLOCK])
    tmp47 = tl.load(in_ptr0 + 14)
    tmp48 = tl.broadcast_to(tmp47, [XBLOCK])
    tmp50 = tl.load(in_ptr0 + 15)
    tmp51 = tl.broadcast_to(tmp50, [XBLOCK])
    tmp5 = tmp2 + tmp4
    tmp8 = tmp5 + tmp7
    tmp11 = tmp8 + tmp10
    tmp12 = tmp0 / tmp11
    tmp18 = tmp15 + tmp17
    tmp21 = tmp18 + tmp20
    tmp24 = tmp21 + tmp23
    tmp25 = tmp13 / tmp24
    tmp26 = tmp12 + tmp25
    tmp32 = tmp29 + tmp31
    tmp35 = tmp32 + tmp34
    tmp38 = tmp35 + tmp37
    tmp39 = tmp27 / tmp38
    tmp40 = tmp26 + tmp39
    tmp46 = tmp43 + tmp45
    tmp49 = tmp46 + tmp48
    tmp52 = tmp49 + tmp51
    tmp53 = tmp41 / tmp52
    tmp54 = tmp40 + tmp53
    tmp55 = 4.0
    tmp56 = tmp54 / tmp55
    tl.store(out_ptr0 + x0, tmp56, xmask)


@triton.jit
def triton_poi_fused_mul_5(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
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x2, tmp2, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11, primals_12,
        primals_13) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (1, 4), (4, 1))
    assert_size_stride(primals_3, (1,), (1,))
    assert_size_stride(primals_4, (1, 1), (1, 1))
    assert_size_stride(primals_5, (1,), (1,))
    assert_size_stride(primals_6, (4, 4), (4, 1))
    assert_size_stride(primals_7, (4,), (1,))
    assert_size_stride(primals_8, (4, 1), (1, 1))
    assert_size_stride(primals_9, (4,), (1,))
    assert_size_stride(primals_10, (4, 1), (1, 1))
    assert_size_stride(primals_11, (4,), (1,))
    assert_size_stride(primals_12, (4,), (1,))
    assert_size_stride(primals_13, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf1 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_3, primals_1, reinterpret_tensor(
            primals_2, (4, 1), (1, 4), 0), alpha=1, beta=1, out=buf1)
        del primals_2
        del primals_3
        buf2 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_elu_0[grid(4)](buf1, buf2, 4, XBLOCK=4, num_warps=
            1, num_stages=1)
        buf4 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        extern_kernels.addmm(primals_5, buf2, primals_4, alpha=1, beta=1,
            out=buf4)
        del primals_5
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_7, primals_1, reinterpret_tensor(
            primals_6, (4, 4), (1, 4), 0), alpha=1, beta=1, out=buf5)
        del primals_6
        del primals_7
        buf6 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_9, buf4, reinterpret_tensor(primals_8,
            (1, 4), (1, 1), 0), alpha=1, beta=1, out=buf6)
        del primals_9
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.addmm(primals_11, buf4, reinterpret_tensor(
            primals_10, (1, 4), (1, 1), 0), alpha=1, beta=1, out=buf7)
        del primals_11
        buf8 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        buf9 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        triton_poi_fused_add_mul_native_layer_norm_sigmoid_1[grid(4)](buf6,
            buf7, buf5, buf8, buf9, 4, XBLOCK=4, num_warps=1, num_stages=1)
        buf10 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused_add_mul_native_layer_norm_sigmoid_2[grid(16)](buf6,
            buf7, buf5, buf8, buf9, primals_12, primals_13, buf10, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del buf8
        buf11 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_poi_fused__softmax_3[grid(16)](buf10, buf11, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        del buf10
        buf12 = reinterpret_tensor(buf9, (4,), (1,), 0)
        del buf9
        triton_poi_fused__softmax_mean_4[grid(4)](buf11, buf12, 4, XBLOCK=4,
            num_warps=1, num_stages=1)
        buf13 = buf11
        del buf11
        triton_poi_fused_mul_5[grid(16)](buf12, primals_1, buf13, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
    return (buf13, buf12, primals_1, primals_12, primals_13, buf1, buf2,
        buf4, buf5, buf6, buf7, primals_10, primals_8, primals_4)


class GatedLinearUnit(nn.Module):
    """**The unit of gating operation that maps the input to the range of 0-1 and multiple original input through the
    sigmoid function.**

    """

    def __init__(self, input_size, hidden_layer_size, dropout_rate,
        activation=None):
        """

        :param input_size: Number of features
        :param hidden_layer_size: The size of nn.Linear layer, global default is 160
        :param dropout_rate: The rate of linear layer parameters randomly discarded during training
        :param activation: activation function used to activate raw input, default is None
        """
        super(GatedLinearUnit, self).__init__()
        self.input_size = input_size
        self.hidden_layer_size = hidden_layer_size
        self.dropout_rate = dropout_rate
        self.activation_name = activation
        if self.dropout_rate:
            self.dropout = nn.Dropout(p=self.dropout_rate)
        self.W4 = torch.nn.Linear(self.input_size, self.hidden_layer_size)
        self.W5 = torch.nn.Linear(self.input_size, self.hidden_layer_size)
        if self.activation_name:
            self.activation = getattr(nn, self.activation_name)()
        self.sigmoid = nn.Sigmoid()
        self.init_weights()

    def init_weights(self):
        for n, p in self.named_parameters():
            if 'bias' not in n:
                torch.nn.init.xavier_uniform_(p)
            elif 'bias' in n:
                torch.nn.init.zeros_(p)

    def forward(self, x):
        if self.dropout_rate:
            x = self.dropout(x)
        if self.activation_name:
            output = self.sigmoid(self.W4(x)) * self.activation(self.W5(x))
        else:
            output = self.sigmoid(self.W4(x)) * self.W5(x)
        return output


class GateAddNormNetwork(nn.Module):
    """**Units that adding gating output to skip connection improves generalization.**"""

    def __init__(self, input_size, hidden_layer_size, dropout_rate,
        activation=None):
        """

        :param input_size: Number of features
        :param hidden_layer_size: The size of nn.Linear layer, global default is 160
        :param dropout_rate: The rate of linear layer parameters randomly discarded during training
        :param activation: activation function used to activate raw input, default is None
        """
        super(GateAddNormNetwork, self).__init__()
        self.input_size = input_size
        self.hidden_layer_size = hidden_layer_size
        self.dropout_rate = dropout_rate
        self.activation_name = activation
        self.GLU = GatedLinearUnit(self.input_size, self.hidden_layer_size,
            self.dropout_rate, activation=self.activation_name)
        self.LayerNorm = nn.LayerNorm(self.hidden_layer_size)

    def forward(self, x, skip):
        output = self.LayerNorm(self.GLU(x) + skip)
        return output


class GatedResidualNetwork(nn.Module):
    """**GRN main module, which divides all inputs into two ways, calculates the gating one way for linear mapping twice and
    passes the original input to GateAddNormNetwork together. ** """

    def __init__(self, hidden_layer_size, input_size=None, output_size=None,
        dropout_rate=None):
        """

        :param hidden_layer_size: The size of nn.Linear layer, global default is 160
        :param input_size: Number of features
        :param output_size: Number of features
        :param dropout_rate: The rate of linear layer parameters randomly discarded during training
        """
        super(GatedResidualNetwork, self).__init__()
        self.hidden_layer_size = hidden_layer_size
        self.input_size = input_size if input_size else self.hidden_layer_size
        self.output_size = output_size
        self.dropout_rate = dropout_rate
        self.W1 = torch.nn.Linear(self.hidden_layer_size, self.
            hidden_layer_size)
        self.W2 = torch.nn.Linear(self.input_size, self.hidden_layer_size)
        if self.output_size:
            self.skip_linear = torch.nn.Linear(self.input_size, self.
                output_size)
            self.glu_add_norm = GateAddNormNetwork(self.hidden_layer_size,
                self.output_size, self.dropout_rate)
        else:
            self.glu_add_norm = GateAddNormNetwork(self.hidden_layer_size,
                self.hidden_layer_size, self.dropout_rate)
        self.init_weights()

    def init_weights(self):
        for name, p in self.named_parameters():
            if ('W2' in name or 'W3' in name) and 'bias' not in name:
                torch.nn.init.kaiming_normal_(p, a=0, mode='fan_in',
                    nonlinearity='leaky_relu')
            elif ('skip_linear' in name or 'W1' in name
                ) and 'bias' not in name:
                torch.nn.init.xavier_uniform_(p)
            elif 'bias' in name:
                torch.nn.init.zeros_(p)

    def forward(self, x):
        n2 = F.elu(self.W2(x))
        n1 = self.W1(n2)
        if self.output_size:
            output = self.glu_add_norm(n1, self.skip_linear(x))
        else:
            output = self.glu_add_norm(n1, x)
        return output


class VariableSelectionNetworkNew(nn.Module):
    """**Feature selection module, which inputs a vector stitched into all features, takes the weights of each
    feature and multiply with the original input as output. ** """

    def __init__(self, hidden_layer_size, dropout_rate, output_size, input_size
        ):
        """

        :param hidden_layer_size: The size of nn.Linear layer, global default is 160
        :param dropout_rate: The rate of linear layer parameters randomly discarded during training
        :param output_size: Number of features
        :param input_size: Number of features
        """
        super(VariableSelectionNetworkNew, self).__init__()
        self.hidden_layer_size = hidden_layer_size
        self.input_size = input_size
        self.output_size = output_size
        self.dropout_rate = dropout_rate
        self.flattened_grn = GatedResidualNetwork(self.hidden_layer_size,
            input_size=self.input_size, output_size=self.output_size,
            dropout_rate=self.dropout_rate)

    def forward(self, input_0):
        primals_4 = self.flattened_grn.W1.weight
        primals_3 = self.flattened_grn.W1.bias
        primals_2 = self.flattened_grn.W2.weight
        primals_5 = self.flattened_grn.W2.bias
        primals_1 = self.flattened_grn.skip_linear.weight
        primals_7 = self.flattened_grn.skip_linear.bias
        primals_8 = self.flattened_grn.glu_add_norm.GLU.W4.weight
        primals_9 = self.flattened_grn.glu_add_norm.GLU.W4.bias
        primals_10 = self.flattened_grn.glu_add_norm.GLU.W5.weight
        primals_11 = self.flattened_grn.glu_add_norm.GLU.W5.bias
        primals_12 = self.flattened_grn.glu_add_norm.LayerNorm.weight
        primals_13 = self.flattened_grn.glu_add_norm.LayerNorm.bias
        primals_6 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11, primals_12, primals_13])
        return output[0], output[1]


Measured latency_ms: 0.038239