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
from torch.autograd import Function
import torch.nn as nn
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_abs_add_clamp_div_floor_mul_sign_0(in_ptr0, out_ptr0,
    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 = 0.1
    tmp2 = tmp0 * tmp1
    tmp3 = 0.0
    tmp4 = triton_helpers.maximum(tmp2, tmp3)
    tmp5 = 1.0
    tmp6 = triton_helpers.minimum(tmp4, tmp5)
    tmp7 = 255.0
    tmp8 = tmp6 * tmp7
    tmp9 = tl.full([1], 0, tl.int32)
    tmp10 = tmp9 < tmp8
    tmp11 = tmp10.to(tl.int8)
    tmp12 = tmp8 < tmp9
    tmp13 = tmp12.to(tl.int8)
    tmp14 = tmp11 - tmp13
    tmp15 = tmp14.to(tmp8.dtype)
    tmp16 = tl_math.abs(tmp8)
    tmp17 = 0.5
    tmp18 = tmp16 + tmp17
    tmp19 = libdevice.floor(tmp18)
    tmp20 = tmp15 * tmp19
    tmp21 = 0.00392156862745098
    tmp22 = tmp20 * tmp21
    tl.store(out_ptr0 + x0, tmp22, xmask)


@triton.jit
def triton_per_fused_abs_add_div_floor_max_mul_sign_sub_tanh_1(in_ptr0,
    out_ptr0, out_ptr1, 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 = libdevice.tanh(tmp0)
    tmp2 = tl_math.abs(tmp1)
    tmp3 = tl.broadcast_to(tmp2, [RBLOCK])
    tmp5 = triton_helpers.promote_to_tensor(triton_helpers.max2(tmp3, 0))
    tmp6 = 0.5
    tmp7 = tmp1 * tmp6
    tmp8 = tmp7 / tmp5
    tmp9 = tmp8 + tmp6
    tmp10 = 255.0
    tmp11 = tmp9 * tmp10
    tmp12 = tl.full([1], 0, tl.int32)
    tmp13 = tmp12 < tmp11
    tmp14 = tmp13.to(tl.int8)
    tmp15 = tmp11 < tmp12
    tmp16 = tmp15.to(tl.int8)
    tmp17 = tmp14 - tmp16
    tmp18 = tmp17.to(tmp11.dtype)
    tmp19 = tl_math.abs(tmp11)
    tmp20 = tmp19 + tmp6
    tmp21 = libdevice.floor(tmp20)
    tmp22 = tmp18 * tmp21
    tmp23 = 0.00392156862745098
    tmp24 = tmp22 * tmp23
    tmp25 = 2.0
    tmp26 = tmp24 * tmp25
    tmp27 = 1.0
    tmp28 = tmp26 - tmp27
    tl.store(out_ptr1 + tl.broadcast_to(r0, [RBLOCK]), tmp28, None)
    tl.store(out_ptr0 + tl.full([1], 0, tl.int32), tmp5, None)


@triton.jit
def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 4
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x2, tmp2, xmask)


def call(args):
    primals_1, primals_2, primals_3 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_3, (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.float32)
        get_raw_stream(0)
        triton_poi_fused_abs_add_clamp_div_floor_mul_sign_0[grid(256)](
            primals_1, buf0, 256, XBLOCK=128, num_warps=4, num_stages=1)
        del primals_1
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_per_fused_abs_add_div_floor_max_mul_sign_sub_tanh_1[grid(1)](
            primals_2, buf1, buf2, 1, 256, num_warps=2, num_stages=1)
        buf3 = extern_kernels.convolution(buf0, buf2, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 4, 1, 1), (4, 1, 1, 1))
        buf4 = buf3
        del buf3
        triton_poi_fused_convolution_2[grid(16)](buf4, primals_3, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del primals_3
    return buf4, primals_2, buf0, buf1, buf2


class Round(Function):

    @staticmethod
    def forward(self, input):
        sign = torch.sign(input)
        output = sign * torch.floor(torch.abs(input) + 0.5)
        return output

    @staticmethod
    def backward(self, grad_output):
        grad_input = grad_output.clone()
        return grad_input


class ActivationQuantizer(nn.Module):

    def __init__(self, a_bits):
        super(ActivationQuantizer, self).__init__()
        self.a_bits = a_bits

    def round(self, input):
        output = Round.apply(input)
        return output

    def forward(self, input):
        if self.a_bits == 32:
            output = input
        elif self.a_bits == 1:
            None
            assert self.a_bits != 1
        else:
            output = torch.clamp(input * 0.1, 0, 1)
            scale = 1 / float(2 ** self.a_bits - 1)
            output = self.round(output / scale) * scale
        return output


class WeightQuantizer(nn.Module):

    def __init__(self, w_bits):
        super(WeightQuantizer, self).__init__()
        self.w_bits = w_bits

    def round(self, input):
        output = Round.apply(input)
        return output

    def forward(self, input):
        if self.w_bits == 32:
            output = input
        elif self.w_bits == 1:
            None
            assert self.w_bits != 1
        else:
            output = torch.tanh(input)
            output = output / 2 / torch.max(torch.abs(output)) + 0.5
            scale = 1 / float(2 ** self.w_bits - 1)
            output = self.round(output / scale) * scale
            output = 2 * output - 1
        return output


class QuantConv2dNew(nn.Conv2d):

    def __init__(self, in_channels, out_channels, kernel_size, stride=1,
        padding=0, dilation=1, groups=1, bias=True, padding_mode='zeros',
        a_bits=8, w_bits=8, quant_inference=False):
        super(QuantConv2dNew, self).__init__(in_channels, out_channels,
            kernel_size, stride, padding, dilation, groups, bias, padding_mode)
        self.quant_inference = quant_inference
        self.activation_quantizer = ActivationQuantizer(a_bits=a_bits)
        self.weight_quantizer = WeightQuantizer(w_bits=w_bits)

    def forward(self, input_0):
        primals_1 = self.weight
        primals_3 = self.bias
        primals_2 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.014037