# AOT ID: ['0_inference']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_7/inductor_cache/p4/cp4wvaid5nhy4qhyozmkikygldcherqkx73jdv2fuas7vih2cydw.py
# Topologically Sorted Source Nodes: [mean], Original ATen: [aten.mean]
# Source node to ATen node mapping:
#   mean => mean
# Graph fragment:
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.dim](args = (%view, [1]), kwargs = {})
triton_per_fused_mean_0 = async_compile.triton('triton_per_fused_mean_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.persistent_reduction(
    size_hints=[4, 32],
    reduction_hint=ReductionHint.DEFAULT,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32', 3: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 3), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_mean_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 8, 'num_reduction': 1, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@triton.jit
def triton_per_fused_mean_0(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 4
    rnumel = 32
    RBLOCK: tl.constexpr = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    roffset = 0
    rmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp21 = tl.load(in_ptr0 + (r1 + (64*x0)), xmask, other=0.0)
    tmp44 = tl.load(in_ptr0 + (32 + r1 + (64*x0)), xmask, other=0.0)
    tmp0 = (r1 // 16)
    tmp1 = tl.full([1, 1], 2, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.broadcast_to((r1 // 16), [XBLOCK, RBLOCK])
    tmp4 = tmp3 < tmp1
    tmp5 = tmp4 & tmp2
    tmp6 = tl.load(in_ptr0 + (r1 + (64*x0)), tmp5 & xmask, other=0.0)
    tmp7 = 0.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp9 = tl.full(tmp8.shape, 0.0, tmp8.dtype)
    tmp10 = tl.where(tmp5, tmp8, tmp9)
    tmp11 = tl.load(in_ptr0 + (r1 + (64*x0)), tmp2 & xmask, other=0.0)
    tmp12 = tl.where(tmp4, tmp10, tmp11)
    tmp13 = triton_helpers.minimum(tmp12, tmp7)
    tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype)
    tmp15 = tl.where(tmp2, tmp13, tmp14)
    tmp16 = tmp0 < tmp1
    tmp17 = tl.load(in_ptr0 + (r1 + (64*x0)), tmp16 & xmask, other=0.0)
    tmp18 = triton_helpers.maximum(tmp17, tmp7)
    tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype)
    tmp20 = tl.where(tmp16, tmp18, tmp19)
    tmp22 = tl.where(tmp16, tmp20, tmp21)
    tmp23 = tl.where(tmp2, tmp15, tmp22)
    tmp24 = tmp23 + tmp7
    tmp25 = 2 + (r1 // 16)
    tmp26 = tmp25 >= tmp1
    tmp27 = tl.broadcast_to(2 + (r1 // 16), [XBLOCK, RBLOCK])
    tmp28 = tmp27 < tmp1
    tmp29 = tmp28 & tmp26
    tmp30 = tl.load(in_ptr0 + (32 + r1 + (64*x0)), tmp29 & xmask, other=0.0)
    tmp31 = triton_helpers.maximum(tmp30, tmp7)
    tmp32 = tl.full(tmp31.shape, 0.0, tmp31.dtype)
    tmp33 = tl.where(tmp29, tmp31, tmp32)
    tmp34 = tl.load(in_ptr0 + (32 + r1 + (64*x0)), tmp26 & xmask, other=0.0)
    tmp35 = tl.where(tmp28, tmp33, tmp34)
    tmp36 = triton_helpers.minimum(tmp35, tmp7)
    tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype)
    tmp38 = tl.where(tmp26, tmp36, tmp37)
    tmp39 = tmp25 < tmp1
    tmp40 = tl.load(in_ptr0 + (32 + r1 + (64*x0)), tmp39 & xmask, other=0.0)
    tmp41 = triton_helpers.maximum(tmp40, tmp7)
    tmp42 = tl.full(tmp41.shape, 0.0, tmp41.dtype)
    tmp43 = tl.where(tmp39, tmp41, tmp42)
    tmp45 = tl.where(tmp39, tmp43, tmp44)
    tmp46 = tl.where(tmp26, tmp38, tmp45)
    tmp47 = tmp46 + tmp7
    tmp48 = tmp24 - tmp47
    tmp49 = tl.broadcast_to(tmp48, [XBLOCK, RBLOCK])
    tmp51 = tl.where(xmask, tmp49, 0)
    tmp52 = tl.sum(tmp51, 1)[:, None]
    tl.store(out_ptr0 + (x0), tmp52, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/nx/cnxiu4g3dzrtvxcvolkvqt5kgh7kowusla5ptvodcl3nrg5qeq5h.py
# Topologically Sorted Source Nodes: [clamp_, clamp__1, truediv], Original ATen: [aten.clamp, aten.div]
# Source node to ATen node mapping:
#   clamp_ => clamp_min
#   clamp__1 => clamp_max
#   truediv => div
# Graph fragment:
#   %clamp_min : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%getitem, 0), kwargs = {})
#   %slice_scatter_default : [num_users=2] = call_function[target=torch.ops.aten.slice_scatter.default](args = (%arg0_1, %clamp_min, 1, 0, 2), kwargs = {})
#   %clamp_max : [num_users=1] = call_function[target=torch.ops.aten.clamp_max.default](args = (%getitem_5, 0), kwargs = {})
#   %slice_scatter_default_1 : [num_users=2] = call_function[target=torch.ops.aten.slice_scatter.default](args = (%slice_scatter_default, %clamp_max, 1, 2, 4), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%slice_scatter_default_1, %view_1), kwargs = {})
triton_poi_fused_clamp_div_1 = async_compile.triton('triton_poi_fused_clamp_div_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[256], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_clamp_div_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 5, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@triton.jit
def triton_poi_fused_clamp_div_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = (xindex // 16) % 4
    x3 = xindex
    x2 = (xindex // 64)
    tmp19 = tl.load(in_ptr0 + (x3), xmask)
    tmp22 = tl.load(in_ptr1 + (x2), xmask, eviction_policy='evict_last')
    tmp0 = x1
    tmp1 = tl.full([1], 2, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tmp0 < tmp1
    tmp4 = tmp3 & tmp2
    tmp5 = tl.load(in_ptr0 + (x3), tmp4 & xmask, other=0.0)
    tmp6 = 0.0
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype)
    tmp9 = tl.where(tmp4, tmp7, tmp8)
    tmp10 = tl.load(in_ptr0 + (x3), tmp2 & xmask, other=0.0)
    tmp11 = tl.where(tmp3, tmp9, tmp10)
    tmp12 = triton_helpers.minimum(tmp11, tmp6)
    tmp13 = tl.full(tmp12.shape, 0.0, tmp12.dtype)
    tmp14 = tl.where(tmp2, tmp12, tmp13)
    tmp15 = tl.load(in_ptr0 + (x3), tmp3 & xmask, other=0.0)
    tmp16 = triton_helpers.maximum(tmp15, tmp6)
    tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype)
    tmp18 = tl.where(tmp3, tmp16, tmp17)
    tmp20 = tl.where(tmp3, tmp18, tmp19)
    tmp21 = tl.where(tmp2, tmp14, tmp20)
    tmp23 = 32.0
    tmp24 = tmp22 / tmp23
    tmp25 = 1.2533141373155001
    tmp26 = tmp24 * tmp25
    tmp27 = 1e-08
    tmp28 = tmp26 + tmp27
    tmp29 = tmp21 / tmp28
    tl.store(out_ptr0 + (x3), tmp29, xmask)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, ), (1, ), torch.float32)
        # Topologically Sorted Source Nodes: [mean], Original ATen: [aten.mean]
        stream0 = get_raw_stream(0)
        triton_per_fused_mean_0.run(arg0_1, buf0, 4, 32, grid=grid(4), stream=stream0)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [clamp_, clamp__1, truediv], Original ATen: [aten.clamp, aten.div]
        triton_poi_fused_clamp_div_1.run(arg0_1, buf0, buf1, 256, grid=grid(256), stream=stream0)
        del arg0_1
        del buf0
    return (buf1, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    arg0_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import math
import torch
import torch.nn as nn
from torch.autograd.function import InplaceFunction
import torch.nn.parallel
import torch.utils.data


def birelu(x, inplace=False):
    return BiReLUFunction().apply(x, inplace)


def rnlu(x, inplace=False, shift=0, scale_fix=(math.pi / 2) ** 0.5):
    x = birelu(x, inplace=inplace)
    pos, neg = (x + shift).chunk(2, dim=1)
    scale = (pos - neg).view(pos.size(0), -1).mean(1) * scale_fix + 1e-08
    return x / scale.view(scale.size(0), *([1] * (x.dim() - 1)))


class BiReLUFunction(InplaceFunction):

    @classmethod
    def forward(cls, ctx, input, inplace=False):
        if input.size(1) % 2 != 0:
            raise RuntimeError(
                'dimension 1 of input must be multiple of 2, but got {}'.
                format(input.size(1)))
        ctx.inplace = inplace
        if ctx.inplace:
            ctx.mark_dirty(input)
            output = input
        else:
            output = input.clone()
        pos, neg = output.chunk(2, dim=1)
        pos.clamp_(min=0)
        neg.clamp_(max=0)
        ctx.save_for_backward(output)
        return output

    @staticmethod
    def backward(ctx, grad_output):
        output, = ctx.saved_variables
        grad_input = grad_output.masked_fill(output.eq(0), 0)
        return grad_input, None


class RnLU(nn.Module):
    """docstring for RnLU."""

    def __init__(self, inplace=False):
        super(RnLU, self).__init__()
        self.inplace = inplace

    def forward(self, x):
        return rnlu(x, inplace=self.inplace)


def get_inputs():
    return [torch.rand([4, 4, 4, 4])]


def get_init_inputs():
    return [[], {}]

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._inductor.runtime import triton_helpers
import math
import torch.nn as nn
from torch.autograd.function import InplaceFunction
import torch.nn.parallel
import torch.utils.data
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused_mean_0(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp21 = tl.load(in_ptr0 + (r1 + 64 * x0), xmask, other=0.0)
    tmp44 = tl.load(in_ptr0 + (32 + r1 + 64 * x0), xmask, other=0.0)
    tmp0 = r1 // 16
    tmp1 = tl.full([1, 1], 2, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.broadcast_to(r1 // 16, [XBLOCK, RBLOCK])
    tmp4 = tmp3 < tmp1
    tmp5 = tmp4 & tmp2
    tmp6 = tl.load(in_ptr0 + (r1 + 64 * x0), tmp5 & xmask, other=0.0)
    tmp7 = 0.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp9 = tl.full(tmp8.shape, 0.0, tmp8.dtype)
    tmp10 = tl.where(tmp5, tmp8, tmp9)
    tmp11 = tl.load(in_ptr0 + (r1 + 64 * x0), tmp2 & xmask, other=0.0)
    tmp12 = tl.where(tmp4, tmp10, tmp11)
    tmp13 = triton_helpers.minimum(tmp12, tmp7)
    tmp14 = tl.full(tmp13.shape, 0.0, tmp13.dtype)
    tmp15 = tl.where(tmp2, tmp13, tmp14)
    tmp16 = tmp0 < tmp1
    tmp17 = tl.load(in_ptr0 + (r1 + 64 * x0), tmp16 & xmask, other=0.0)
    tmp18 = triton_helpers.maximum(tmp17, tmp7)
    tmp19 = tl.full(tmp18.shape, 0.0, tmp18.dtype)
    tmp20 = tl.where(tmp16, tmp18, tmp19)
    tmp22 = tl.where(tmp16, tmp20, tmp21)
    tmp23 = tl.where(tmp2, tmp15, tmp22)
    tmp24 = tmp23 + tmp7
    tmp25 = 2 + r1 // 16
    tmp26 = tmp25 >= tmp1
    tmp27 = tl.broadcast_to(2 + r1 // 16, [XBLOCK, RBLOCK])
    tmp28 = tmp27 < tmp1
    tmp29 = tmp28 & tmp26
    tmp30 = tl.load(in_ptr0 + (32 + r1 + 64 * x0), tmp29 & xmask, other=0.0)
    tmp31 = triton_helpers.maximum(tmp30, tmp7)
    tmp32 = tl.full(tmp31.shape, 0.0, tmp31.dtype)
    tmp33 = tl.where(tmp29, tmp31, tmp32)
    tmp34 = tl.load(in_ptr0 + (32 + r1 + 64 * x0), tmp26 & xmask, other=0.0)
    tmp35 = tl.where(tmp28, tmp33, tmp34)
    tmp36 = triton_helpers.minimum(tmp35, tmp7)
    tmp37 = tl.full(tmp36.shape, 0.0, tmp36.dtype)
    tmp38 = tl.where(tmp26, tmp36, tmp37)
    tmp39 = tmp25 < tmp1
    tmp40 = tl.load(in_ptr0 + (32 + r1 + 64 * x0), tmp39 & xmask, other=0.0)
    tmp41 = triton_helpers.maximum(tmp40, tmp7)
    tmp42 = tl.full(tmp41.shape, 0.0, tmp41.dtype)
    tmp43 = tl.where(tmp39, tmp41, tmp42)
    tmp45 = tl.where(tmp39, tmp43, tmp44)
    tmp46 = tl.where(tmp26, tmp38, tmp45)
    tmp47 = tmp46 + tmp7
    tmp48 = tmp24 - tmp47
    tmp49 = tl.broadcast_to(tmp48, [XBLOCK, RBLOCK])
    tmp51 = tl.where(xmask, tmp49, 0)
    tmp52 = tl.sum(tmp51, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp52, xmask)


@triton.jit
def triton_poi_fused_clamp_div_1(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16 % 4
    x3 = xindex
    x2 = xindex // 64
    tmp19 = tl.load(in_ptr0 + x3, xmask)
    tmp22 = tl.load(in_ptr1 + x2, xmask, eviction_policy='evict_last')
    tmp0 = x1
    tmp1 = tl.full([1], 2, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tmp0 < tmp1
    tmp4 = tmp3 & tmp2
    tmp5 = tl.load(in_ptr0 + x3, tmp4 & xmask, other=0.0)
    tmp6 = 0.0
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tl.full(tmp7.shape, 0.0, tmp7.dtype)
    tmp9 = tl.where(tmp4, tmp7, tmp8)
    tmp10 = tl.load(in_ptr0 + x3, tmp2 & xmask, other=0.0)
    tmp11 = tl.where(tmp3, tmp9, tmp10)
    tmp12 = triton_helpers.minimum(tmp11, tmp6)
    tmp13 = tl.full(tmp12.shape, 0.0, tmp12.dtype)
    tmp14 = tl.where(tmp2, tmp12, tmp13)
    tmp15 = tl.load(in_ptr0 + x3, tmp3 & xmask, other=0.0)
    tmp16 = triton_helpers.maximum(tmp15, tmp6)
    tmp17 = tl.full(tmp16.shape, 0.0, tmp16.dtype)
    tmp18 = tl.where(tmp3, tmp16, tmp17)
    tmp20 = tl.where(tmp3, tmp18, tmp19)
    tmp21 = tl.where(tmp2, tmp14, tmp20)
    tmp23 = 32.0
    tmp24 = tmp22 / tmp23
    tmp25 = 1.2533141373155001
    tmp26 = tmp24 * tmp25
    tmp27 = 1e-08
    tmp28 = tmp26 + tmp27
    tmp29 = tmp21 / tmp28
    tl.store(out_ptr0 + x3, tmp29, xmask)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4,), (1,), torch.float32)
        get_raw_stream(0)
        triton_per_fused_mean_0[grid(4)](arg0_1, buf0, 4, 32, XBLOCK=1,
            num_warps=2, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_clamp_div_1[grid(256)](arg0_1, buf0, buf1, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        del arg0_1
        del buf0
    return buf1,


def birelu(x, inplace=False):
    return BiReLUFunction().apply(x, inplace)


def rnlu(x, inplace=False, shift=0, scale_fix=(math.pi / 2) ** 0.5):
    x = birelu(x, inplace=inplace)
    pos, neg = (x + shift).chunk(2, dim=1)
    scale = (pos - neg).view(pos.size(0), -1).mean(1) * scale_fix + 1e-08
    return x / scale.view(scale.size(0), *([1] * (x.dim() - 1)))


class BiReLUFunction(InplaceFunction):

    @classmethod
    def forward(cls, ctx, input, inplace=False):
        if input.size(1) % 2 != 0:
            raise RuntimeError(
                'dimension 1 of input must be multiple of 2, but got {}'.
                format(input.size(1)))
        ctx.inplace = inplace
        if ctx.inplace:
            ctx.mark_dirty(input)
            output = input
        else:
            output = input.clone()
        pos, neg = output.chunk(2, dim=1)
        pos.clamp_(min=0)
        neg.clamp_(max=0)
        ctx.save_for_backward(output)
        return output

    @staticmethod
    def backward(ctx, grad_output):
        output, = ctx.saved_variables
        grad_input = grad_output.masked_fill(output.eq(0), 0)
        return grad_input, None


class RnLUNew(nn.Module):
    """docstring for RnLU."""

    def __init__(self, inplace=False):
        super(RnLUNew, self).__init__()
        self.inplace = inplace

    def forward(self, input_0):
        arg0_1 = input_0
        output = call([arg0_1])
        return output[0]
