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
import torch.multiprocessing
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 (buf8, buf7, primals_1, primals_2, buf0, buf1, buf2, buf3, buf4,
        buf5, buf6)


def weights_init(init_type='gaussian'):

    def init_func(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_func


class GaussianActivation(nn.Module):

    def __init__(self, a, mu, gamma_l, gamma_r):
        super(GaussianActivation, self).__init__()
        self.a = Parameter(torch.tensor(a, dtype=torch.float32))
        self.mu = Parameter(torch.tensor(mu, dtype=torch.float32))
        self.gamma_l = Parameter(torch.tensor(gamma_l, dtype=torch.float32))
        self.gamma_r = Parameter(torch.tensor(gamma_r, dtype=torch.float32))

    def forward(self, input_features):
        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.gamma_l.data = torch.clamp(self.gamma_l.data, 0.5, 2.0)
        self.gamma_r.data = torch.clamp(self.gamma_r.data, 0.5, 2.0)
        left = input_features < self.mu
        right = input_features >= self.mu
        g_A_left = self.a * torch.exp(-self.gamma_l * (input_features -
            self.mu) ** 2)
        g_A_left.masked_fill_(right, 0.0)
        g_A_right = 1 + (self.a - 1) * torch.exp(-self.gamma_r * (
            input_features - self.mu) ** 2)
        g_A_right.masked_fill_(left, 0.0)
        g_A = g_A_left + g_A_right
        return g_A


class MaskUpdate(nn.Module):

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

    def forward(self, input_masks):
        return torch.pow(self.func(input_masks), self.alpha)


class ReverseAttentionLayerNew(nn.Module):

    def __init__(self, in_channels, out_channels, kernel_size=4, stride=2,
        padding=1, dilation=1, groups=1, bias=False):
        super(ReverseAttentionLayerNew, self).__init__()
        self.mask_conv = nn.Conv2d(in_channels, out_channels, kernel_size,
            stride, padding, dilation, groups, bias)
        self.mask_conv.apply(weights_init())
        self.gaussian_activation = GaussianActivation(a=1.1, mu=2.0,
            gamma_l=1.0, gamma_r=1.0)
        self.mask_update = MaskUpdate(alpha=0.8)

    def forward(self, input_0):
        primals_1 = self.mask_conv.weight
        primals_3 = self.gaussian_activation.a
        primals_4 = self.gaussian_activation.mu
        primals_5 = self.gaussian_activation.gamma_l
        primals_6 = self.gaussian_activation.gamma_r
        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.014822