import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.parallel
import torch.utils.data.distributed


class AdaptiveConv(nn.Module):

    def __init__(self, in_channels, out_channels, stride=1, padding=1,
        dilation=1, groups=1, bias=False, size=(256, 256)):
        super(AdaptiveConv, self).__init__()
        self.conv3x3 = nn.Conv2d(in_channels, out_channels, 3, stride,
            padding=1, dilation=dilation, groups=groups, bias=bias)
        self.conv1x1 = nn.Conv2d(in_channels, out_channels, 1, stride,
            padding=0, dilation=dilation, groups=groups, bias=bias)
        self.gap = nn.AdaptiveAvgPool2d(1)
        self.fc1 = nn.Conv2d(in_channels, out_channels, kernel_size=1)
        self.fc2 = nn.Conv2d(out_channels, out_channels, kernel_size=1)
        self.size = size
        self.w = nn.Parameter(torch.ones(3, 1, self.size[0], self.size[1]))
        self.softmax = nn.Softmax()
        self.relu = nn.ReLU(inplace=True)

    def forward(self, x):
        _, _, _h, _w = x.size()
        weight = self.softmax(self.w)
        w1 = weight[0, :, :, :]
        w2 = weight[1, :, :, :]
        w3 = weight[2, :, :, :]
        x1 = self.conv3x3(x)
        x2 = self.conv1x1(x)
        size = x1.size()[2:]
        gap = self.gap(x)
        gap = self.relu(self.fc1(gap))
        gap = self.fc2(gap)
        gap = F.upsample(gap, size=size, mode='nearest')
        x = w1 * x1 + w2 * x2 + w3 * gap
        return x


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


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 import triton_helpers
from torch._inductor.runtime.triton_helpers import math as tl_math
import torch.nn as nn
import torch.nn.parallel
import torch.utils.data.distributed
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_red_fused_mean_0(in_ptr0, out_ptr0, xnumel, rnumel, XBLOCK: tl.
    constexpr, RBLOCK: tl.constexpr):
    xnumel = 128
    rnumel = 8192
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    _tmp2 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + (r1 + 8192 * x0), rmask & xmask,
            eviction_policy='evict_last', other=0.0)
        tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
        tmp3 = _tmp2 + tmp1
        _tmp2 = tl.where(rmask & xmask, tmp3, _tmp2)
    tmp2 = tl.sum(_tmp2, 1)[:, None]
    tl.store(out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_per_fused_mean_1(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK:
    tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 8
    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 + 8 * x0), xmask, other=0.0)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.where(xmask, tmp1, 0)
    tmp4 = tl.sum(tmp3, 1)[:, None]
    tmp5 = 65536.0
    tmp6 = tmp4 / tmp5
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp6, xmask)


@triton.jit
def triton_poi_fused_convolution_relu_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
    tmp3 = tl.full([1], 0, tl.int32)
    tmp4 = triton_helpers.maximum(tmp3, tmp2)
    tl.store(in_out_ptr0 + x2, tmp4, xmask)


@triton.jit
def triton_poi_fused__to_copy_add_arange_mul_3(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 = x0
    tmp1 = tmp0.to(tl.float32)
    tmp2 = 0.00390625
    tmp3 = tmp1 * tmp2
    tmp4 = tmp3.to(tl.int32)
    tl.store(out_ptr0 + x0, tmp4, xmask)


@triton.jit
def triton_poi_fused__unsafe_index_add_convolution_mul_4(in_ptr0, in_ptr1,
    in_ptr2, in_ptr3, in_ptr4, in_ptr5, out_ptr0, out_ptr1, xnumel, XBLOCK:
    tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x1 = xindex // 256 % 256
    x0 = xindex % 256
    x5 = xindex // 65536
    x2 = xindex // 65536 % 4
    x6 = xindex
    x4 = xindex % 65536
    tmp0 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp5 = tl.load(in_ptr0 + x0, None, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + x5, None, eviction_policy='evict_last')
    tmp10 = tl.load(in_ptr2 + x2, None, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr3 + x4, None, eviction_policy='evict_last')
    tmp16 = tl.load(in_ptr4 + x6, None)
    tmp18 = tl.load(in_ptr3 + (65536 + x4), None, eviction_policy='evict_last')
    tmp22 = tl.load(in_ptr5 + x6, None)
    tmp25 = tl.load(in_ptr3 + (131072 + x4), None, eviction_policy='evict_last'
        )
    tmp1 = tl.full([XBLOCK], 1, tl.int32)
    tmp2 = tmp0 + tmp1
    tmp3 = tmp0 < 0
    tl.where(tmp3, tmp2, tmp0)
    tmp6 = tmp5 + tmp1
    tmp7 = tmp5 < 0
    tl.where(tmp7, tmp6, tmp5)
    tmp11 = tmp9 + tmp10
    tmp13 = tmp12 - tmp12
    tmp14 = tl_math.exp(tmp13)
    tmp15 = tmp14 / tmp14
    tmp17 = tmp15 * tmp16
    tmp19 = tmp18 - tmp18
    tmp20 = tl_math.exp(tmp19)
    tmp21 = tmp20 / tmp20
    tmp23 = tmp21 * tmp22
    tmp24 = tmp17 + tmp23
    tmp26 = tmp25 - tmp25
    tmp27 = tl_math.exp(tmp26)
    tmp28 = tmp27 / tmp27
    tmp29 = tmp28 * tmp11
    tmp30 = tmp24 + tmp29
    tl.store(out_ptr0 + x6, tmp11, None)
    tl.store(out_ptr1 + x6, tmp30, None)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 256, 256), (262144, 65536, 256, 1))
    assert_size_stride(primals_2, (3, 1, 256, 256), (65536, 65536, 256, 1))
    assert_size_stride(primals_3, (4, 4, 3, 3), (36, 9, 3, 1))
    assert_size_stride(primals_4, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_5, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_6, (4,), (1,))
    assert_size_stride(primals_7, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_8, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_1, primals_3, stride=(1, 
            1), padding=(1, 1), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 256, 256), (262144, 65536, 256, 1))
        buf1 = extern_kernels.convolution(primals_1, primals_4, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf1, (4, 4, 256, 256), (262144, 65536, 256, 1))
        buf2 = empty_strided_cuda((4, 4, 1, 1, 8), (32, 8, 128, 128, 1),
            torch.float32)
        get_raw_stream(0)
        triton_red_fused_mean_0[grid(128)](primals_1, buf2, 128, 8192,
            XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1)
        buf3 = empty_strided_cuda((4, 4, 1, 1), (4, 1, 16, 16), torch.float32)
        buf4 = reinterpret_tensor(buf3, (4, 4, 1, 1), (4, 1, 1, 1), 0)
        del buf3
        triton_per_fused_mean_1[grid(16)](buf4, buf2, 16, 8, XBLOCK=8,
            num_warps=2, num_stages=1)
        del buf2
        buf5 = extern_kernels.convolution(buf4, primals_5, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf5, (4, 4, 1, 1), (4, 1, 1, 1))
        buf6 = buf5
        del buf5
        triton_poi_fused_convolution_relu_2[grid(16)](buf6, primals_6, 16,
            XBLOCK=16, num_warps=1, num_stages=1)
        del primals_6
        buf7 = extern_kernels.convolution(buf6, primals_7, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf7, (4, 4, 1, 1), (4, 1, 1, 1))
        buf8 = empty_strided_cuda((256,), (1,), torch.int64)
        triton_poi_fused__to_copy_add_arange_mul_3[grid(256)](buf8, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        buf9 = empty_strided_cuda((4, 4, 256, 256), (262144, 65536, 256, 1),
            torch.float32)
        buf10 = empty_strided_cuda((4, 4, 256, 256), (262144, 65536, 256, 1
            ), torch.float32)
        triton_poi_fused__unsafe_index_add_convolution_mul_4[grid(1048576)](
            buf8, buf7, primals_8, primals_2, buf0, buf1, buf9, buf10, 
            1048576, XBLOCK=1024, num_warps=4, num_stages=1)
        del buf7
        del primals_8
    return (buf10, primals_1, primals_2, primals_3, primals_4, primals_5,
        primals_7, buf0, buf1, buf4, buf6, buf8, buf9)


class AdaptiveConvNew(nn.Module):

    def __init__(self, in_channels, out_channels, stride=1, padding=1,
        dilation=1, groups=1, bias=False, size=(256, 256)):
        super(AdaptiveConvNew, self).__init__()
        self.conv3x3 = nn.Conv2d(in_channels, out_channels, 3, stride,
            padding=1, dilation=dilation, groups=groups, bias=bias)
        self.conv1x1 = nn.Conv2d(in_channels, out_channels, 1, stride,
            padding=0, dilation=dilation, groups=groups, bias=bias)
        self.gap = nn.AdaptiveAvgPool2d(1)
        self.fc1 = nn.Conv2d(in_channels, out_channels, kernel_size=1)
        self.fc2 = nn.Conv2d(out_channels, out_channels, kernel_size=1)
        self.size = size
        self.w = nn.Parameter(torch.ones(3, 1, self.size[0], self.size[1]))
        self.softmax = nn.Softmax()
        self.relu = nn.ReLU(inplace=True)

    def forward(self, input_0):
        primals_2 = self.w
        primals_3 = self.conv3x3.weight
        primals_4 = self.conv1x1.weight
        primals_5 = self.fc1.weight
        primals_6 = self.fc1.bias
        primals_7 = self.fc2.weight
        primals_8 = self.fc2.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8])
        return output[0]
