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]


Measured latency_ms: 0.050628