import torch
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 import nn
from torch.nn.parameter import Parameter
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused__to_copy_add_ge_gt_le_lt_mul_0(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, out_ptr2, 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_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr3 + x0, xmask, eviction_policy='evict_last')
    tmp19 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 >= tmp1
    tmp4 = tmp0 <= tmp3
    tmp5 = tmp2.to(tl.float32)
    tmp7 = tmp0 + tmp1
    tmp8 = tmp6 * tmp7
    tmp9 = tmp1 + tmp8
    tmp10 = tmp5 * tmp9
    tmp11 = tmp0 < tmp1
    tmp12 = tmp11.to(tl.float32)
    tmp13 = tmp0 > tmp3
    tmp14 = tmp13.to(tl.float32)
    tmp15 = tmp12 * tmp14
    tmp16 = tmp15 * tmp0
    tmp17 = tmp10 + tmp16
    tmp18 = tmp4.to(tl.float32)
    tmp20 = tmp0 + tmp3
    tmp21 = tmp19 * tmp20
    tmp22 = tmp3 + tmp21
    tmp23 = tmp18 * tmp22
    tmp24 = tmp17 + tmp23
    tl.store(out_ptr0 + x2, tmp2, xmask)
    tl.store(out_ptr1 + x2, tmp4, xmask)
    tl.store(out_ptr2 + x2, tmp24, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = args
    args.clear()
    assert_size_stride(primals_1, (4,), (1,))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4,), (1,))
    assert_size_stride(primals_5, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.bool)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__to_copy_add_ge_gt_le_lt_mul_0[grid(256)](primals_2,
            primals_1, primals_4, primals_3, primals_5, buf0, buf1, buf2, 
            256, XBLOCK=128, num_warps=4, num_stages=1)
    return (buf2, primals_1, primals_2, primals_3, primals_4, primals_5,
        buf0, buf1)


class SReLUNew(nn.Module):
    """
    SReLU (S-shaped Rectified Linear Activation Unit): a combination of three linear functions, which perform mapping R → R with the following formulation:

    .. math::

        h(x_i) = \\left\\{\\begin{matrix} t_i^r + a_i^r(x_i - t_i^r), x_i \\geq t_i^r \\\\  x_i, t_i^r > x_i > t_i^l\\\\  t_i^l + a_i^l(x_i - t_i^l), x_i \\leq  t_i^l \\\\ \\end{matrix}\\right.

    with 4 trainable parameters.

    Shape:
        - Input: (N, *) where * means, any number of additional
          dimensions
        - Output: (N, *), same shape as the input

    Parameters:

        .. math:: \\{t_i^r, a_i^r, t_i^l, a_i^l\\}

    4 trainable parameters, which model an individual SReLU activation unit. The subscript i indicates that we allow SReLU to vary in different channels. Parameters can be initialized manually or randomly.

    References:
        - See SReLU paper:
        https://arxiv.org/pdf/1512.07030.pdf

    Examples:
        >>> srelu_activation = srelu((2,2))
        >>> t = torch.randn((2,2), dtype=torch.float, requires_grad = True)
        >>> output = srelu_activation(t)
    """

    def __init__(self, in_features, parameters=None):
        """
        Initialization.
        INPUT:
            - in_features: shape of the input
            - parameters: (tr, tl, ar, al) parameters for manual initialization, default value is None. If None is passed, parameters are initialized randomly.
        """
        super(SReLUNew, self).__init__()
        self.in_features = in_features
        if parameters is None:
            self.tr = Parameter(torch.randn(in_features, dtype=torch.float,
                requires_grad=True))
            self.tl = Parameter(torch.randn(in_features, dtype=torch.float,
                requires_grad=True))
            self.ar = Parameter(torch.randn(in_features, dtype=torch.float,
                requires_grad=True))
            self.al = Parameter(torch.randn(in_features, dtype=torch.float,
                requires_grad=True))
        else:
            self.tr, self.tl, self.ar, self.al = parameters

    def forward(self, input_0):
        primals_1 = self.tr
        primals_3 = self.tl
        primals_4 = self.ar
        primals_5 = self.al
        primals_2 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4, primals_5])
        return output[0]


Measured latency_ms: 0.015824