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
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
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_per_fused_add_pow_sum_0(in_out_ptr0, in_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (r1 + 16 * x0), xmask, other=0.0)
    tmp1 = tmp0 * tmp0
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp4 = tl.where(xmask, tmp2, 0)
    tmp5 = tl.sum(tmp4, 1)[:, None]
    tmp6 = 1e-05
    tmp7 = tmp5 + tmp6
    tmp8 = libdevice.sqrt(tmp7)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp8, xmask)


@triton.jit
def triton_poi_fused_add_mean_mul_pow_1(in_ptr0, in_ptr1, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + 0)
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK])
    tmp5 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp6 = tl.load(in_ptr1 + 1)
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK])
    tmp11 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp12 = tl.load(in_ptr1 + 2)
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK])
    tmp17 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp18 = tl.load(in_ptr1 + 3)
    tmp19 = tl.broadcast_to(tmp18, [XBLOCK])
    tmp3 = tmp0 * tmp2
    tmp4 = tmp3 * tmp3
    tmp8 = tmp5 * tmp7
    tmp9 = tmp8 * tmp8
    tmp10 = tmp4 + tmp9
    tmp14 = tmp11 * tmp13
    tmp15 = tmp14 * tmp14
    tmp16 = tmp10 + tmp15
    tmp20 = tmp17 * tmp19
    tmp21 = tmp20 * tmp20
    tmp22 = tmp16 + tmp21
    tmp23 = 4.0
    tmp24 = tmp22 / tmp23
    tmp25 = 1e-05
    tmp26 = tmp24 + tmp25
    tmp27 = libdevice.sqrt(tmp26)
    tl.store(out_ptr0 + x0, tmp27, xmask)


@triton.jit
def triton_poi_fused_add_div_mean_mul_pow_tanh_2(in_ptr0, in_ptr1, in_ptr2,
    in_ptr3, in_ptr4, out_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
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x0, xmask, eviction_policy='evict_last')
    tmp4 = tl.load(in_ptr3 + x1, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr4 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 / tmp4
    tmp6 = tmp2 * tmp5
    tmp8 = tmp6 + tmp7
    tmp9 = libdevice.tanh(tmp8)
    tmp10 = 1.0
    tmp11 = tmp9 + tmp10
    tl.store(out_ptr0 + x2, tmp11, xmask)


@triton.jit
def triton_poi_fused_add_div_mean_mul_pow_tanh_3(in_ptr0, in_ptr1, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x1 = xindex // 16
    tmp0 = tl.load(in_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tl.store(out_ptr0 + x2, tmp2, xmask)


@triton.jit
def triton_per_fused_native_group_norm_relu_threshold_backward_4(in_ptr0,
    in_ptr1, in_ptr2, out_ptr0, out_ptr2, out_ptr3, out_ptr4, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    rnumel = 324
    RBLOCK: tl.constexpr = 512
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    rmask = rindex < rnumel
    r1 = rindex
    x0 = xindex
    r3 = rindex // 81
    tmp0 = tl.load(in_ptr0 + (r1 + 324 * x0), rmask, other=0.0)
    tmp24 = tl.load(in_ptr1 + r3, rmask, eviction_policy='evict_last',
        other=0.0)
    tmp26 = tl.load(in_ptr2 + r3, rmask, eviction_policy='evict_last',
        other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [RBLOCK])
    tl.where(rmask, tmp1, 0)
    tmp4 = tl.broadcast_to(tmp1, [RBLOCK])
    tmp6 = tl.where(rmask, tmp4, 0)
    tmp7 = triton_helpers.promote_to_tensor(tl.sum(tmp6, 0))
    tmp8 = tl.full([1], 324, tl.int32)
    tmp9 = tmp8.to(tl.float32)
    tmp10 = tmp7 / tmp9
    tmp11 = tmp1 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tl.broadcast_to(tmp12, [RBLOCK])
    tmp15 = tl.where(rmask, tmp13, 0)
    tmp16 = triton_helpers.promote_to_tensor(tl.sum(tmp15, 0))
    tmp17 = tmp0 - tmp10
    tmp18 = 324.0
    tmp19 = tmp16 / tmp18
    tmp20 = 1e-05
    tmp21 = tmp19 + tmp20
    tmp22 = libdevice.rsqrt(tmp21)
    tmp23 = tmp17 * tmp22
    tmp25 = tmp23 * tmp24
    tmp27 = tmp25 + tmp26
    tmp28 = tl.full([1], 0, tl.int32)
    tmp29 = triton_helpers.maximum(tmp28, tmp27)
    tmp30 = 0.0
    tmp31 = tmp29 <= tmp30
    tl.store(out_ptr2 + (r1 + 324 * x0), tmp29, rmask)
    tl.store(out_ptr3 + (r1 + 324 * x0), tmp31, rmask)
    tl.store(out_ptr4 + x0, tmp22, None)
    tl.store(out_ptr0 + x0, tmp10, None)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (1, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_3, (1, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_4, (1, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_5, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
        buf1 = reinterpret_tensor(buf0, (4, 4, 1, 1), (4, 1, 1, 1), 0)
        del buf0
        get_raw_stream(0)
        triton_per_fused_add_pow_sum_0[grid(16)](buf1, primals_1, 16, 16,
            XBLOCK=8, num_warps=2, num_stages=1)
        buf2 = empty_strided_cuda((4, 1, 1, 1), (1, 4, 4, 4), torch.float32)
        triton_poi_fused_add_mean_mul_pow_1[grid(4)](buf1, primals_2, buf2,
            4, XBLOCK=4, num_warps=1, num_stages=1)
        buf3 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
        triton_poi_fused_add_div_mean_mul_pow_tanh_2[grid(16)](buf1,
            primals_2, primals_3, buf2, primals_4, buf3, 16, XBLOCK=16,
            num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_add_div_mean_mul_pow_tanh_3[grid(256)](primals_1,
            buf3, buf4, 256, XBLOCK=256, num_warps=4, num_stages=1)
        del buf3
        buf5 = extern_kernels.convolution(buf4, primals_5, stride=(1, 1),
            padding=(4, 4), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf5, (4, 4, 9, 9), (324, 81, 9, 1))
        buf6 = buf2
        del buf2
        buf10 = empty_strided_cuda((4, 4, 9, 9), (324, 81, 9, 1), torch.float32
            )
        buf11 = empty_strided_cuda((4, 4, 9, 9), (324, 81, 9, 1), torch.bool)
        buf9 = empty_strided_cuda((4, 1, 1, 1), (1, 4, 4, 4), torch.float32)
        triton_per_fused_native_group_norm_relu_threshold_backward_4[grid(4)](
            buf5, primals_6, primals_7, buf6, buf10, buf11, buf9, 4, 324,
            num_warps=4, num_stages=1)
        del primals_7
    return (buf10, primals_1, primals_2, primals_3, primals_4, primals_5,
        primals_6, buf1, buf4, buf5, reinterpret_tensor(buf6, (4, 1), (1, 1
        ), 0), reinterpret_tensor(buf9, (4, 1), (1, 1), 0), buf11)


class GCT(nn.Module):

    def __init__(self, num_channels, epsilon=1e-05, mode='l2', after_relu=False
        ):
        super(GCT, self).__init__()
        self.alpha = nn.Parameter(torch.ones(1, num_channels, 1, 1))
        self.gamma = nn.Parameter(torch.zeros(1, num_channels, 1, 1))
        self.beta = nn.Parameter(torch.zeros(1, num_channels, 1, 1))
        self.epsilon = epsilon
        self.mode = mode
        self.after_relu = after_relu

    def forward(self, x):
        if self.mode == 'l2':
            embedding = (x.pow(2).sum((2, 3), keepdim=True) + self.epsilon
                ).pow(0.5) * self.alpha
            norm = self.gamma / (embedding.pow(2).mean(dim=1, keepdim=True) +
                self.epsilon).pow(0.5)
        elif self.mode == 'l1':
            if not self.after_relu:
                _x = torch.abs(x)
            else:
                _x = x
            embedding = _x.sum((2, 3), keepdim=True) * self.alpha
            norm = self.gamma / (torch.abs(embedding).mean(dim=1, keepdim=
                True) + self.epsilon)
        else:
            None
            exit()
        gate = 1.0 + torch.tanh(embedding * norm + self.beta)
        return x * gate


class _ASPPModuleNew(nn.Module):

    def __init__(self, inplanes, planes, kernel_size, padding, dilation):
        super(_ASPPModuleNew, self).__init__()
        self.GCT = GCT(inplanes)
        self.atrous_conv = nn.Conv2d(inplanes, planes, kernel_size=
            kernel_size, stride=1, padding=padding, dilation=dilation, bias
            =False)
        self.bn = nn.GroupNorm(int(planes / 4), planes)
        self.relu = nn.ReLU(inplace=True)
        self._init_weight()

    def _init_weight(self):
        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                torch.nn.init.kaiming_normal_(m.weight)
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def forward(self, input_0):
        primals_2 = self.GCT.alpha
        primals_3 = self.GCT.gamma
        primals_4 = self.GCT.beta
        primals_1 = self.atrous_conv.weight
        primals_6 = self.bn.weight
        primals_7 = self.bn.bias
        primals_5 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7])
        return output[0]


Measured latency_ms: 0.02493