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
import math
import torch.nn as nn
from torch.nn.parameter import Parameter
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_clamp_0(in_ptr0, 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)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp2 = 1.01
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp4 = 6.0
    tmp5 = triton_helpers.minimum(tmp3, tmp4)
    tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp5, None)


@triton.jit
def triton_poi_fused_clamp_1(in_ptr0, 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)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp2 = 0.1
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp4 = 3.0
    tmp5 = triton_helpers.minimum(tmp3, tmp4)
    tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp5, None)


@triton.jit
def triton_poi_fused_clamp_2(in_ptr0, 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)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK])
    tmp2 = 0.5
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp4 = 2.0
    tmp5 = triton_helpers.minimum(tmp3, tmp4)
    tl.store(out_ptr0 + tl.full([XBLOCK], 0, tl.int32), tmp5, None)


@triton.jit
def triton_poi_fused_add_exp_ge_lt_masked_fill_mul_neg_pow_relu_sub_3(in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, in_ptr4, out_ptr0, out_ptr1, out_ptr2,
    out_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    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)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp5 = tl.load(in_ptr2 + 0)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK])
    tmp7 = tl.load(in_ptr3 + 0)
    tmp8 = tl.broadcast_to(tmp7, [XBLOCK])
    tmp19 = tl.load(in_ptr4 + 0)
    tmp20 = tl.broadcast_to(tmp19, [XBLOCK])
    tmp3 = tmp0 < tmp2
    tmp4 = tmp0 >= tmp2
    tmp9 = -tmp8
    tmp10 = tmp0 - tmp2
    tmp11 = tmp10 * tmp10
    tmp12 = tmp9 * tmp11
    tmp13 = tl_math.exp(tmp12)
    tmp14 = tmp6 * tmp13
    tmp15 = 0.0
    tmp16 = tl.where(tmp4, tmp15, tmp14)
    tmp17 = 1.0
    tmp18 = tmp6 - tmp17
    tmp21 = -tmp20
    tmp22 = tmp21 * tmp11
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tmp18 * tmp23
    tmp25 = tmp24 + tmp17
    tmp26 = tl.where(tmp3, tmp15, tmp25)
    tmp27 = tmp16 + tmp26
    tmp28 = tl.full([1], 0, tl.int32)
    tmp29 = triton_helpers.maximum(tmp28, tmp0)
    tmp30 = 0.8
    tmp31 = libdevice.pow(tmp29, tmp30)
    tl.store(out_ptr0 + x0, tmp3, xmask)
    tl.store(out_ptr1 + x0, tmp4, xmask)
    tl.store(out_ptr2 + x0, tmp27, xmask)
    tl.store(out_ptr3 + x0, tmp31, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5, primals_6 = 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, (), ())
    assert_size_stride(primals_4, (), ())
    assert_size_stride(primals_5, (), ())
    assert_size_stride(primals_6, (), ())
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_2, primals_1, stride=(2, 
            2), padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 2, 2), (16, 4, 2, 1))
        buf1 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clamp_0[grid(1)](primals_3, buf1, 1, XBLOCK=1,
            num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((), (), torch.float32)
        triton_poi_fused_clamp_1[grid(1)](primals_4, buf2, 1, XBLOCK=1,
            num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((), (), torch.float32)
        triton_poi_fused_clamp_2[grid(1)](primals_5, buf3, 1, XBLOCK=1,
            num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((), (), torch.float32)
        triton_poi_fused_clamp_2[grid(1)](primals_6, buf4, 1, XBLOCK=1,
            num_warps=1, num_stages=1)
        buf5 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.bool)
        buf6 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.bool)
        buf7 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
        buf8 = empty_strided_cuda((4, 4, 2, 2), (16, 4, 2, 1), torch.float32)
        triton_poi_fused_add_exp_ge_lt_masked_fill_mul_neg_pow_relu_sub_3[grid
            (64)](buf0, buf2, buf1, buf3, buf4, buf5, buf6, buf7, buf8, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        buf9 = torch.ops.aten.set_.source_Tensor(primals_3, buf1)
        assert_size_stride(buf9, (), ())
        del primals_3
        buf19 = torch.ops.aten.set_.source_Tensor(primals_4, buf2)
        assert_size_stride(buf19, (), ())
        del primals_4
        buf29 = torch.ops.aten.set_.source_Tensor(primals_5, buf3)
        assert_size_stride(buf29, (), ())
        del primals_5
        buf34 = torch.ops.aten.set_.source_Tensor(primals_6, buf4)
        assert_size_stride(buf34, (), ())
        del primals_6
    return (buf7, buf8, primals_1, primals_2, buf0, buf1, buf2, buf3, buf4,
        buf5, buf6)


def weights_init(init_type='gaussian'):

    def init_fun(m):
        classname = m.__class__.__name__
        if (classname.find('Conv') == 0 or classname.find('Linear') == 0
            ) and hasattr(m, 'weight'):
            if init_type == 'gaussian':
                nn.init.normal_(m.weight, 0.0, 0.02)
            elif init_type == 'xavier':
                nn.init.xavier_normal_(m.weight, gain=math.sqrt(2))
            elif init_type == 'kaiming':
                nn.init.kaiming_normal_(m.weight, a=0, mode='fan_in')
            elif init_type == 'orthogonal':
                nn.init.orthogonal_(m.weight, gain=math.sqrt(2))
            elif init_type == 'default':
                pass
            else:
                assert 0, 'Unsupported initialization: {}'.format(init_type)
            if hasattr(m, 'bias') and m.bias is not None:
                nn.init.constant_(m.bias, 0.0)
    return init_fun


class GaussActivation(nn.Module):

    def __init__(self, a, mu, sigma1, sigma2):
        super(GaussActivation, self).__init__()
        self.a = Parameter(torch.tensor(a, dtype=torch.float32))
        self.mu = Parameter(torch.tensor(mu, dtype=torch.float32))
        self.sigma1 = Parameter(torch.tensor(sigma1, dtype=torch.float32))
        self.sigma2 = Parameter(torch.tensor(sigma2, dtype=torch.float32))

    def forward(self, inputFeatures):
        self.a.data = torch.clamp(self.a.data, 1.01, 6.0)
        self.mu.data = torch.clamp(self.mu.data, 0.1, 3.0)
        self.sigma1.data = torch.clamp(self.sigma1.data, 0.5, 2.0)
        self.sigma2.data = torch.clamp(self.sigma2.data, 0.5, 2.0)
        lowerThanMu = inputFeatures < self.mu
        largerThanMu = inputFeatures >= self.mu
        leftValuesActiv = self.a * torch.exp(-self.sigma1 * (inputFeatures -
            self.mu) ** 2)
        leftValuesActiv.masked_fill_(largerThanMu, 0.0)
        rightValueActiv = 1 + (self.a - 1) * torch.exp(-self.sigma2 * (
            inputFeatures - self.mu) ** 2)
        rightValueActiv.masked_fill_(lowerThanMu, 0.0)
        output = leftValuesActiv + rightValueActiv
        return output


class MaskUpdate(nn.Module):

    def __init__(self, alpha):
        super(MaskUpdate, self).__init__()
        self.updateFunc = nn.ReLU(True)
        self.alpha = alpha

    def forward(self, inputMaskMap):
        """ self.alpha.data = torch.clamp(self.alpha.data, 0.6, 0.8)
        print(self.alpha) """
        return torch.pow(self.updateFunc(inputMaskMap), self.alpha)


class ReverseMaskConvNew(nn.Module):

    def __init__(self, inputChannels, outputChannels, kernelSize=4, stride=
        2, padding=1, dilation=1, groups=1, convBias=False):
        super(ReverseMaskConvNew, self).__init__()
        self.reverseMaskConv = nn.Conv2d(inputChannels, outputChannels,
            kernelSize, stride, padding, dilation, groups, bias=convBias)
        self.reverseMaskConv.apply(weights_init())
        self.activationFuncG_A = GaussActivation(1.1, 1.0, 0.5, 0.5)
        self.updateMask = MaskUpdate(0.8)

    def forward(self, input_0):
        primals_1 = self.reverseMaskConv.weight
        primals_3 = self.activationFuncG_A.a
        primals_4 = self.activationFuncG_A.mu
        primals_5 = self.activationFuncG_A.sigma1
        primals_6 = self.activationFuncG_A.sigma2
        primals_2 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6])
        return output[0], output[1]


Measured latency_ms: 0.076545