# 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_4/inductor_cache/zs/czsbjuesyn4kz6jr2a3jbsks5mzfe6aebueyp73vnqjatykr5mhf.py
# Topologically Sorted Source Nodes: [lower_bound, upper_bound], Original ATen: [aten.sort, aten.isnan, aten.any]
# Source node to ATen node mapping:
#   lower_bound => any_1, isnan, sort
#   upper_bound => any_2, isnan_1, sort_1
# Graph fragment:
#   %sort : [num_users=1] = call_function[target=torch.ops.aten.sort.default](args = (%view,), kwargs = {})
#   %sort_1 : [num_users=1] = call_function[target=torch.ops.aten.sort.default](args = (%view_2,), kwargs = {})
#   %isnan : [num_users=1] = call_function[target=torch.ops.aten.isnan.default](args = (%getitem,), kwargs = {})
#   %any_1 : [num_users=1] = call_function[target=torch.ops.aten.any.dim](args = (%isnan, -1, True), kwargs = {})
#   %isnan_1 : [num_users=1] = call_function[target=torch.ops.aten.isnan.default](args = (%getitem_2,), kwargs = {})
#   %any_2 : [num_users=1] = call_function[target=torch.ops.aten.any.dim](args = (%isnan_1, -1, True), kwargs = {})
triton_per_fused_any_isnan_sort_0 = async_compile.triton('triton_per_fused_any_isnan_sort_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=[1, 256],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*i1', 4: '*i1', 5: 'i32', 6: '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': {5: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 6), equal_to_1=(5,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_any_isnan_sort_0', 'mutated_arg_names': [], 'no_x_dim': True, 'num_load': 1, 'num_reduction': 2, '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_any_isnan_sort_0(in_ptr0, out_ptr0, out_ptr1, out_ptr2, out_ptr3, xnumel, rnumel):
    xnumel = 1
    XBLOCK: tl.constexpr = 1
    rnumel = 256
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    xmask = tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    roffset = 0
    rmask = tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + (r0), None)
    tmp1 = r0
    tmp2 = tmp1.to(tl.int16)
    tmp3 = tl.broadcast_to(tmp0, [RBLOCK])
    tmp4 = tl.broadcast_to(tmp2, [RBLOCK])
    tmp5, tmp6, = triton_helpers.sort_with_index(tmp3, tmp4, None, 0, stable=False, descending=False)
    tmp7 = libdevice.isnan(tmp5).to(tl.int1)
    tmp8 = tmp7.to(tl.int64)
    tmp9 = (tmp8 != 0)
    tmp10 = tl.broadcast_to(tmp9, [RBLOCK])
    tmp12 = triton_helpers.promote_to_tensor(triton_helpers.any(tmp10, 0))
    tl.store(out_ptr0 + (tl.broadcast_to(r0, [RBLOCK])), tmp5, None)
    tl.store(out_ptr1 + (tl.broadcast_to(r0, [RBLOCK])), tmp5, None)
    tl.store(out_ptr2 + (tl.full([1], 0, tl.int32)), tmp12, None)
    tl.store(out_ptr3 + (tl.full([1], 0, tl.int32)), tmp12, None)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/se/csec4copjxrqfi72woce2hjbanvp4276cvyk3qlrp2uenbd4ar6s.py
# Topologically Sorted Source Nodes: [abs_1, abs_2, scale], Original ATen: [aten.abs, aten.maximum]
# Source node to ATen node mapping:
#   abs_1 => abs_3
#   abs_2 => abs_4
#   scale => maximum
# Graph fragment:
#   %abs_3 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%squeeze,), kwargs = {})
#   %abs_4 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%squeeze_1,), kwargs = {})
#   %maximum : [num_users=1] = call_function[target=torch.ops.aten.maximum.default](args = (%abs_3, %abs_4), kwargs = {})
triton_poi_fused_abs_maximum_1 = async_compile.triton('triton_poi_fused_abs_maximum_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=[1], 
    filename=__file__,
    triton_meta={'signature': {0: '*i1', 1: '*fp32', 2: '*i1', 3: '*fp32', 4: '*fp32', 5: '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': {5: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4), equal_to_1=(5,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_abs_maximum_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 2, '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_abs_maximum_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 1
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = tl.full([XBLOCK], True, tl.int1)
    tmp0 = tl.load(in_ptr0 + (0)).to(tl.int1)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp32 = tl.load(in_ptr2 + (0)).to(tl.int1)
    tmp33 = tl.broadcast_to(tmp32, [XBLOCK])
    tmp2 = 255.0
    tmp3 = 0.0
    tmp4 = tl.where(tmp1, tmp2, tmp3)
    tmp5 = tmp4.to(tl.int64)
    tmp6 = tmp5.to(tl.float32)
    tmp7 = tmp4 - tmp6
    tmp8 = tl_math.abs(tmp7)
    tmp9 = 0.5
    tmp10 = tmp8 >= tmp9
    tmp11 = 1.0
    tmp12 = tmp7 - tmp11
    tmp13 = tl.where(tmp10, tmp12, tmp7)
    tmp14 = libdevice.ceil(tmp4)
    tmp15 = tmp14.to(tl.int64)
    tmp16 = tl.full([XBLOCK], 256, tl.int32)
    tmp17 = tmp15 + tmp16
    tmp18 = tmp15 < 0
    tmp19 = tl.where(tmp18, tmp17, tmp15)
    tl.device_assert((0 <= tmp19) & (tmp19 < 256), "index out of bounds: 0 <= tmp19 < 256")
    tmp21 = tl.load(in_ptr1 + (tmp19), None, eviction_policy='evict_last')
    tmp22 = tmp5 + tmp16
    tmp23 = tmp5 < 0
    tmp24 = tl.where(tmp23, tmp22, tmp5)
    tl.device_assert((0 <= tmp24) & (tmp24 < 256), "index out of bounds: 0 <= tmp24 < 256")
    tmp26 = tl.load(in_ptr1 + (tmp24), None, eviction_policy='evict_last')
    tmp27 = tmp21 - tmp26
    tmp28 = tmp13 * tmp27
    tmp29 = tl.where(tmp10, tmp21, tmp26)
    tmp30 = tmp28 + tmp29
    tmp31 = tl_math.abs(tmp30)
    tmp34 = tl.where(tmp33, tmp2, tmp2)
    tmp35 = tmp34.to(tl.int64)
    tmp36 = tmp35.to(tl.float32)
    tmp37 = tmp34 - tmp36
    tmp38 = tl_math.abs(tmp37)
    tmp39 = tmp38 >= tmp9
    tmp40 = tmp37 - tmp11
    tmp41 = tl.where(tmp39, tmp40, tmp37)
    tmp42 = libdevice.ceil(tmp34)
    tmp43 = tmp42.to(tl.int64)
    tmp44 = tmp43 + tmp16
    tmp45 = tmp43 < 0
    tmp46 = tl.where(tmp45, tmp44, tmp43)
    tl.device_assert((0 <= tmp46) & (tmp46 < 256), "index out of bounds: 0 <= tmp46 < 256")
    tmp48 = tl.load(in_ptr3 + (tmp46), None, eviction_policy='evict_last')
    tmp49 = tmp35 + tmp16
    tmp50 = tmp35 < 0
    tmp51 = tl.where(tmp50, tmp49, tmp35)
    tl.device_assert((0 <= tmp51) & (tmp51 < 256), "index out of bounds: 0 <= tmp51 < 256")
    tmp53 = tl.load(in_ptr3 + (tmp51), None, eviction_policy='evict_last')
    tmp54 = tmp48 - tmp53
    tmp55 = tmp41 * tmp54
    tmp56 = tl.where(tmp39, tmp48, tmp53)
    tmp57 = tmp55 + tmp56
    tmp58 = tl_math.abs(tmp57)
    tmp59 = triton_helpers.maximum(tmp31, tmp58)
    tl.store(out_ptr0 + (tl.full([XBLOCK], 0, tl.int32)), tmp59, None)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/xa/cxafu33fxgn63equeaqf6a3mfx4kere2c2xauqotgx5ft3rzxebe.py
# Topologically Sorted Source Nodes: [clamp, output], Original ATen: [aten.clamp, aten.div]
# Source node to ATen node mapping:
#   clamp => clamp_max, clamp_min
#   output => div
# Graph fragment:
#   %clamp_min : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.Tensor](args = (%arg0_1, %squeeze), kwargs = {})
#   %clamp_max : [num_users=1] = call_function[target=torch.ops.aten.clamp_max.Tensor](args = (%clamp_min, %squeeze_1), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%clamp_max, %maximum), kwargs = {})
triton_poi_fused_clamp_div_2 = async_compile.triton('triton_poi_fused_clamp_div_2', '''
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: '*i1', 3: '*fp32', 4: '*i1', 5: '*fp32', 6: '*fp32', 7: '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, 4, 5, 6, 7), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_clamp_div_2', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 4, '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_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, in_ptr4, in_ptr5, xnumel, XBLOCK : tl.constexpr):
    xnumel = 256
    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_ptr1 + (0)).to(tl.int1)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp33 = tl.load(in_ptr3 + (0)).to(tl.int1)
    tmp34 = tl.broadcast_to(tmp33, [XBLOCK])
    tmp60 = tl.load(in_ptr5 + (0))
    tmp61 = tl.broadcast_to(tmp60, [XBLOCK])
    tmp3 = 255.0
    tmp4 = 0.0
    tmp5 = tl.where(tmp2, tmp3, tmp4)
    tmp6 = tmp5.to(tl.int64)
    tmp7 = tmp6.to(tl.float32)
    tmp8 = tmp5 - tmp7
    tmp9 = tl_math.abs(tmp8)
    tmp10 = 0.5
    tmp11 = tmp9 >= tmp10
    tmp12 = 1.0
    tmp13 = tmp8 - tmp12
    tmp14 = tl.where(tmp11, tmp13, tmp8)
    tmp15 = libdevice.ceil(tmp5)
    tmp16 = tmp15.to(tl.int64)
    tmp17 = tl.full([XBLOCK], 256, tl.int32)
    tmp18 = tmp16 + tmp17
    tmp19 = tmp16 < 0
    tmp20 = tl.where(tmp19, tmp18, tmp16)
    tl.device_assert((0 <= tmp20) & (tmp20 < 256), "index out of bounds: 0 <= tmp20 < 256")
    tmp22 = tl.load(in_ptr2 + (tmp20), None, eviction_policy='evict_last')
    tmp23 = tmp6 + tmp17
    tmp24 = tmp6 < 0
    tmp25 = tl.where(tmp24, tmp23, tmp6)
    tl.device_assert((0 <= tmp25) & (tmp25 < 256), "index out of bounds: 0 <= tmp25 < 256")
    tmp27 = tl.load(in_ptr2 + (tmp25), None, eviction_policy='evict_last')
    tmp28 = tmp22 - tmp27
    tmp29 = tmp14 * tmp28
    tmp30 = tl.where(tmp11, tmp22, tmp27)
    tmp31 = tmp29 + tmp30
    tmp32 = triton_helpers.maximum(tmp0, tmp31)
    tmp35 = tl.where(tmp34, tmp3, tmp3)
    tmp36 = tmp35.to(tl.int64)
    tmp37 = tmp36.to(tl.float32)
    tmp38 = tmp35 - tmp37
    tmp39 = tl_math.abs(tmp38)
    tmp40 = tmp39 >= tmp10
    tmp41 = tmp38 - tmp12
    tmp42 = tl.where(tmp40, tmp41, tmp38)
    tmp43 = libdevice.ceil(tmp35)
    tmp44 = tmp43.to(tl.int64)
    tmp45 = tmp44 + tmp17
    tmp46 = tmp44 < 0
    tmp47 = tl.where(tmp46, tmp45, tmp44)
    tl.device_assert((0 <= tmp47) & (tmp47 < 256), "index out of bounds: 0 <= tmp47 < 256")
    tmp49 = tl.load(in_ptr4 + (tmp47), None, eviction_policy='evict_last')
    tmp50 = tmp36 + tmp17
    tmp51 = tmp36 < 0
    tmp52 = tl.where(tmp51, tmp50, tmp36)
    tl.device_assert((0 <= tmp52) & (tmp52 < 256), "index out of bounds: 0 <= tmp52 < 256")
    tmp54 = tl.load(in_ptr4 + (tmp52), None, eviction_policy='evict_last')
    tmp55 = tmp49 - tmp54
    tmp56 = tmp42 * tmp55
    tmp57 = tl.where(tmp40, tmp49, tmp54)
    tmp58 = tmp56 + tmp57
    tmp59 = triton_helpers.minimum(tmp32, tmp58)
    tmp62 = tmp59 / tmp61
    tl.store(in_out_ptr0 + (x0), tmp62, 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((256, ), (1, ), torch.float32)
        buf2 = empty_strided_cuda((256, ), (1, ), torch.float32)
        buf4 = empty_strided_cuda((1, ), (1, ), torch.bool)
        buf5 = empty_strided_cuda((1, ), (1, ), torch.bool)
        # Topologically Sorted Source Nodes: [lower_bound, upper_bound], Original ATen: [aten.sort, aten.isnan, aten.any]
        stream0 = get_raw_stream(0)
        triton_per_fused_any_isnan_sort_0.run(arg0_1, buf0, buf2, buf4, buf5, 1, 256, grid=grid(1), stream=stream0)
        buf7 = empty_strided_cuda((), (), torch.float32)
        # Topologically Sorted Source Nodes: [abs_1, abs_2, scale], Original ATen: [aten.abs, aten.maximum]
        triton_poi_fused_abs_maximum_1.run(buf4, buf0, buf5, buf2, buf7, 1, grid=grid(1), stream=stream0)
        buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf8 = buf6; del buf6  # reuse
        # Topologically Sorted Source Nodes: [clamp, output], Original ATen: [aten.clamp, aten.div]
        triton_poi_fused_clamp_div_2.run(buf8, arg0_1, buf4, buf0, buf5, buf2, buf7, 256, grid=grid(256), stream=stream0)
        del arg0_1
        del buf0
        del buf2
        del buf4
        del buf5
        del buf7
    return (buf8, )


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 torch
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed


class RawScale(torch.nn.Module):
    """
    Scale raw data to [-1, 1] in a symmetric manner, which meets bipolar/unipolar bitstream requirements.
    The remaining data count for 'quantile' quantile of the total data.
    The input quantile needs to be within (0, 1].
    """

    def __init__(self, hwcfg={'quantile': 1}):
        super(RawScale, self).__init__()
        self.hwcfg = {}
        self.hwcfg['quantile'] = hwcfg['quantile']
        assert hwcfg['quantile'] > 0 and hwcfg['quantile'
            ] <= 1, "Error: the hw config 'quantile' of " + str(self
            ) + ' class needs to be within (0, 1].'
        self.quantile = hwcfg['quantile']
        self.quantile_lower = 0.5 - self.quantile / 2
        self.quantile_upper = 0.5 + self.quantile / 2

    def forward(self, raw):
        lower_bound = torch.quantile(raw, self.quantile_lower)
        upper_bound = torch.quantile(raw, self.quantile_upper)
        scale = torch.max(lower_bound.abs(), upper_bound.abs())
        output = raw.clamp(lower_bound, upper_bound).div(scale)
        return output


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
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
import torch.nn.parallel
import torch.optim
import torch.utils.data
import torch.utils.data.distributed
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_any_isnan_sort_0(in_ptr0, out_ptr0, out_ptr1, out_ptr2,
    out_ptr3, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = r0
    tmp2 = tmp1.to(tl.int16)
    tmp3 = tl.broadcast_to(tmp0, [RBLOCK])
    tmp4 = tl.broadcast_to(tmp2, [RBLOCK])
    tmp5, _tmp6 = triton_helpers.sort_with_index(tmp3, tmp4, None, 0,
        stable=False, descending=False)
    tmp7 = libdevice.isnan(tmp5).to(tl.int1)
    tmp8 = tmp7.to(tl.int64)
    tmp9 = tmp8 != 0
    tmp10 = tl.broadcast_to(tmp9, [RBLOCK])
    tmp12 = triton_helpers.promote_to_tensor(triton_helpers.any(tmp10, 0))
    tl.store(out_ptr0 + tl.broadcast_to(r0, [RBLOCK]), tmp5, None)
    tl.store(out_ptr1 + tl.broadcast_to(r0, [RBLOCK]), tmp5, None)
    tl.store(out_ptr2 + tl.full([1], 0, tl.int32), tmp12, None)
    tl.store(out_ptr3 + tl.full([1], 0, tl.int32), tmp12, None)


@triton.jit
def triton_poi_fused_abs_maximum_1(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    tmp0 = tl.load(in_ptr0 + 0).to(tl.int1)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp32 = tl.load(in_ptr2 + 0).to(tl.int1)
    tmp33 = tl.broadcast_to(tmp32, [XBLOCK])
    tmp2 = 255.0
    tmp3 = 0.0
    tmp4 = tl.where(tmp1, tmp2, tmp3)
    tmp5 = tmp4.to(tl.int64)
    tmp6 = tmp5.to(tl.float32)
    tmp7 = tmp4 - tmp6
    tmp8 = tl_math.abs(tmp7)
    tmp9 = 0.5
    tmp10 = tmp8 >= tmp9
    tmp11 = 1.0
    tmp12 = tmp7 - tmp11
    tmp13 = tl.where(tmp10, tmp12, tmp7)
    tmp14 = libdevice.ceil(tmp4)
    tmp15 = tmp14.to(tl.int64)
    tmp16 = tl.full([XBLOCK], 256, tl.int32)
    tmp17 = tmp15 + tmp16
    tmp18 = tmp15 < 0
    tmp19 = tl.where(tmp18, tmp17, tmp15)
    tl.device_assert((0 <= tmp19) & (tmp19 < 256),
        'index out of bounds: 0 <= tmp19 < 256')
    tmp21 = tl.load(in_ptr1 + tmp19, None, eviction_policy='evict_last')
    tmp22 = tmp5 + tmp16
    tmp23 = tmp5 < 0
    tmp24 = tl.where(tmp23, tmp22, tmp5)
    tl.device_assert((0 <= tmp24) & (tmp24 < 256),
        'index out of bounds: 0 <= tmp24 < 256')
    tmp26 = tl.load(in_ptr1 + tmp24, None, eviction_policy='evict_last')
    tmp27 = tmp21 - tmp26
    tmp28 = tmp13 * tmp27
    tmp29 = tl.where(tmp10, tmp21, tmp26)
    tmp30 = tmp28 + tmp29
    tmp31 = tl_math.abs(tmp30)
    tmp34 = tl.where(tmp33, tmp2, tmp2)
    tmp35 = tmp34.to(tl.int64)
    tmp36 = tmp35.to(tl.float32)
    tmp37 = tmp34 - tmp36
    tmp38 = tl_math.abs(tmp37)
    tmp39 = tmp38 >= tmp9
    tmp40 = tmp37 - tmp11
    tmp41 = tl.where(tmp39, tmp40, tmp37)
    tmp42 = libdevice.ceil(tmp34)
    tmp43 = tmp42.to(tl.int64)
    tmp44 = tmp43 + tmp16
    tmp45 = tmp43 < 0
    tmp46 = tl.where(tmp45, tmp44, tmp43)
    tl.device_assert((0 <= tmp46) & (tmp46 < 256),
        'index out of bounds: 0 <= tmp46 < 256')
    tmp48 = tl.load(in_ptr3 + tmp46, None, eviction_policy='evict_last')
    tmp49 = tmp35 + tmp16
    tmp50 = tmp35 < 0
    tmp51 = tl.where(tmp50, tmp49, tmp35)
    tl.device_assert((0 <= tmp51) & (tmp51 < 256),
        'index out of bounds: 0 <= tmp51 < 256')
    tmp53 = tl.load(in_ptr3 + tmp51, None, eviction_policy='evict_last')
    tmp54 = tmp48 - tmp53
    tmp55 = tmp41 * tmp54
    tmp56 = tl.where(tmp39, tmp48, tmp53)
    tmp57 = tmp55 + tmp56
    tmp58 = tl_math.abs(tmp57)
    tmp59 = triton_helpers.maximum(tmp31, tmp58)
    tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp59, None)


@triton.jit
def triton_poi_fused_clamp_div_2(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, in_ptr4, in_ptr5, xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    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_ptr1 + 0).to(tl.int1)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp33 = tl.load(in_ptr3 + 0).to(tl.int1)
    tmp34 = tl.broadcast_to(tmp33, [XBLOCK])
    tmp60 = tl.load(in_ptr5 + 0)
    tmp61 = tl.broadcast_to(tmp60, [XBLOCK])
    tmp3 = 255.0
    tmp4 = 0.0
    tmp5 = tl.where(tmp2, tmp3, tmp4)
    tmp6 = tmp5.to(tl.int64)
    tmp7 = tmp6.to(tl.float32)
    tmp8 = tmp5 - tmp7
    tmp9 = tl_math.abs(tmp8)
    tmp10 = 0.5
    tmp11 = tmp9 >= tmp10
    tmp12 = 1.0
    tmp13 = tmp8 - tmp12
    tmp14 = tl.where(tmp11, tmp13, tmp8)
    tmp15 = libdevice.ceil(tmp5)
    tmp16 = tmp15.to(tl.int64)
    tmp17 = tl.full([XBLOCK], 256, tl.int32)
    tmp18 = tmp16 + tmp17
    tmp19 = tmp16 < 0
    tmp20 = tl.where(tmp19, tmp18, tmp16)
    tl.device_assert((0 <= tmp20) & (tmp20 < 256),
        'index out of bounds: 0 <= tmp20 < 256')
    tmp22 = tl.load(in_ptr2 + tmp20, None, eviction_policy='evict_last')
    tmp23 = tmp6 + tmp17
    tmp24 = tmp6 < 0
    tmp25 = tl.where(tmp24, tmp23, tmp6)
    tl.device_assert((0 <= tmp25) & (tmp25 < 256),
        'index out of bounds: 0 <= tmp25 < 256')
    tmp27 = tl.load(in_ptr2 + tmp25, None, eviction_policy='evict_last')
    tmp28 = tmp22 - tmp27
    tmp29 = tmp14 * tmp28
    tmp30 = tl.where(tmp11, tmp22, tmp27)
    tmp31 = tmp29 + tmp30
    tmp32 = triton_helpers.maximum(tmp0, tmp31)
    tmp35 = tl.where(tmp34, tmp3, tmp3)
    tmp36 = tmp35.to(tl.int64)
    tmp37 = tmp36.to(tl.float32)
    tmp38 = tmp35 - tmp37
    tmp39 = tl_math.abs(tmp38)
    tmp40 = tmp39 >= tmp10
    tmp41 = tmp38 - tmp12
    tmp42 = tl.where(tmp40, tmp41, tmp38)
    tmp43 = libdevice.ceil(tmp35)
    tmp44 = tmp43.to(tl.int64)
    tmp45 = tmp44 + tmp17
    tmp46 = tmp44 < 0
    tmp47 = tl.where(tmp46, tmp45, tmp44)
    tl.device_assert((0 <= tmp47) & (tmp47 < 256),
        'index out of bounds: 0 <= tmp47 < 256')
    tmp49 = tl.load(in_ptr4 + tmp47, None, eviction_policy='evict_last')
    tmp50 = tmp36 + tmp17
    tmp51 = tmp36 < 0
    tmp52 = tl.where(tmp51, tmp50, tmp36)
    tl.device_assert((0 <= tmp52) & (tmp52 < 256),
        'index out of bounds: 0 <= tmp52 < 256')
    tmp54 = tl.load(in_ptr4 + tmp52, None, eviction_policy='evict_last')
    tmp55 = tmp49 - tmp54
    tmp56 = tmp42 * tmp55
    tmp57 = tl.where(tmp40, tmp49, tmp54)
    tmp58 = tmp56 + tmp57
    tmp59 = triton_helpers.minimum(tmp32, tmp58)
    tmp62 = tmp59 / tmp61
    tl.store(in_out_ptr0 + x0, tmp62, 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((256,), (1,), torch.float32)
        buf2 = empty_strided_cuda((256,), (1,), torch.float32)
        buf4 = empty_strided_cuda((1,), (1,), torch.bool)
        buf5 = empty_strided_cuda((1,), (1,), torch.bool)
        get_raw_stream(0)
        triton_per_fused_any_isnan_sort_0[grid(1)](arg0_1, buf0, buf2, buf4,
            buf5, 1, 256, num_warps=2, num_stages=1)
        buf7 = empty_strided_cuda((), (), torch.float32)
        triton_poi_fused_abs_maximum_1[grid(1)](buf4, buf0, buf5, buf2,
            buf7, 1, XBLOCK=1, num_warps=1, num_stages=1)
        buf6 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        buf8 = buf6
        del buf6
        triton_poi_fused_clamp_div_2[grid(256)](buf8, arg0_1, buf4, buf0,
            buf5, buf2, buf7, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del arg0_1
        del buf0
        del buf2
        del buf4
        del buf5
        del buf7
    return buf8,


class RawScaleNew(torch.nn.Module):
    """
    Scale raw data to [-1, 1] in a symmetric manner, which meets bipolar/unipolar bitstream requirements.
    The remaining data count for 'quantile' quantile of the total data.
    The input quantile needs to be within (0, 1].
    """

    def __init__(self, hwcfg={'quantile': 1}):
        super(RawScaleNew, self).__init__()
        self.hwcfg = {}
        self.hwcfg['quantile'] = hwcfg['quantile']
        assert hwcfg['quantile'] > 0 and hwcfg['quantile'
            ] <= 1, "Error: the hw config 'quantile' of " + str(self
            ) + ' class needs to be within (0, 1].'
        self.quantile = hwcfg['quantile']
        self.quantile_lower = 0.5 - self.quantile / 2
        self.quantile_upper = 0.5 + self.quantile / 2

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