from torch.autograd import Function
import torch
import torch.nn as nn
import torch.nn.functional as F


class SignFunction(Function):

    def __init__(self):
        super(SignFunction, self).__init__()

    @staticmethod
    def forward(ctx, input, is_training=True):
        if is_training:
            prob = input.new(input.size()).uniform_()
            x = input.clone()
            x[(1 - input) / 2 <= prob] = 1
            x[(1 - input) / 2 > prob] = -1
            return x
        else:
            return input.sign()

    @staticmethod
    def backward(ctx, grad_output):
        return grad_output, None


class Sign(nn.Module):

    def __init__(self):
        super(Sign, self).__init__()

    def forward(self, x):
        return SignFunction.apply(x, self.training)


class Binarizer(nn.Module):

    def __init__(self, in_channels, out_channels):
        super(Binarizer, self).__init__()
        self.sign = Sign()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=1,
            bias=False)

    def forward(self, x):
        x = self.conv1(x)
        x = F.tanh(x)
        return self.sign(x)


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


def get_init_inputs():
    return [[], {'in_channels': 4, 'out_channels': 4}]

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.triton_helpers import libdevice
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_sign_tanh_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 = libdevice.tanh(tmp0)
    tmp2 = tl.full([1], 0, tl.int32)
    tmp3 = tmp2 < tmp1
    tmp4 = tmp3.to(tl.int8)
    tmp5 = tmp1 < tmp2
    tmp6 = tmp5.to(tl.int8)
    tmp7 = tmp4 - tmp6
    tmp8 = tmp7.to(tmp1.dtype)
    tl.store(out_ptr0 + x0, tmp8, xmask)


def call(args):
    primals_1, primals_2 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_2, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_2, primals_1, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 1))
        buf1 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_sign_tanh_0[grid(256)](buf0, buf1, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
    return buf1, primals_1, primals_2, buf0


class SignFunction(Function):

    def __init__(self):
        super(SignFunction, self).__init__()

    @staticmethod
    def forward(ctx, input, is_training=True):
        if is_training:
            prob = input.new(input.size()).uniform_()
            x = input.clone()
            x[(1 - input) / 2 <= prob] = 1
            x[(1 - input) / 2 > prob] = -1
            return x
        else:
            return input.sign()

    @staticmethod
    def backward(ctx, grad_output):
        return grad_output, None


class Sign(nn.Module):

    def __init__(self):
        super(Sign, self).__init__()

    def forward(self, x):
        return SignFunction.apply(x, self.training)


class BinarizerNew(nn.Module):

    def __init__(self, in_channels, out_channels):
        super(BinarizerNew, self).__init__()
        self.sign = Sign()
        self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=1,
            bias=False)

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