import torch
import torch.nn as nn
import torch.nn.functional as F


def xcorr_fast(x, kernel):
    """group conv2d to calculate cross correlation, fast version
    """
    batch = kernel.size()[0]
    pk = kernel.view(-1, x.size()[1], kernel.size()[2], kernel.size()[3])
    px = x.view(1, -1, x.size()[2], x.size()[3])
    po = F.conv2d(px, pk, groups=batch)
    po = po.view(batch, -1, po.size()[2], po.size()[3])
    return po


class RPN(nn.Module):

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

    def forward(self, z_f, x_f):
        raise NotImplementedError


class UPChannelRPN(RPN):

    def __init__(self, anchor_num=5, feature_in=256):
        super(UPChannelRPN, self).__init__()
        cls_output = 2 * anchor_num
        loc_output = 4 * anchor_num
        self.template_cls_conv = nn.Conv2d(feature_in, feature_in *
            cls_output, kernel_size=3)
        self.template_loc_conv = nn.Conv2d(feature_in, feature_in *
            loc_output, kernel_size=3)
        self.search_cls_conv = nn.Conv2d(feature_in, feature_in, kernel_size=3)
        self.search_loc_conv = nn.Conv2d(feature_in, feature_in, kernel_size=3)
        self.loc_adjust = nn.Conv2d(loc_output, loc_output, kernel_size=1)

    def forward(self, z_f, x_f):
        cls_kernel = self.template_cls_conv(z_f)
        loc_kernel = self.template_loc_conv(z_f)
        cls_feature = self.search_cls_conv(x_f)
        loc_feature = self.search_loc_conv(x_f)
        cls = xcorr_fast(cls_feature, cls_kernel)
        loc = self.loc_adjust(xcorr_fast(loc_feature, loc_kernel))
        return cls, loc


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


def get_init_inputs():
    return [[], {}]

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
import torch.nn as nn
import torch.nn.functional as F
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_convolution_view_0(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x4 = xindex
    x1 = xindex // 3844 % 2560
    tmp0 = tl.load(in_out_ptr0 + x4, None)
    tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x4, tmp2, None)


@triton.jit
def triton_poi_fused_convolution_1(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x3 = xindex
    x1 = xindex // 3844 % 256
    tmp0 = tl.load(in_out_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x3, tmp2, None)


@triton.jit
def triton_poi_fused_convolution_view_2(in_out_ptr0, in_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x4 = xindex
    x1 = xindex // 3844 % 5120
    tmp0 = tl.load(in_out_ptr0 + x4, None)
    tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x4, tmp2, None)


@triton.jit
def triton_poi_fused_convolution_3(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 80
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex
    x0 = xindex % 20
    tmp0 = tl.load(in_out_ptr0 + x2, xmask)
    tmp1 = tl.load(in_ptr0 + x0, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x2, tmp2, xmask)


def call(args):
    (primals_1, primals_2, primals_3, primals_4, primals_5, primals_6,
        primals_7, primals_8, primals_9, primals_10, primals_11, primals_12
        ) = args
    args.clear()
    assert_size_stride(primals_1, (2560, 256, 3, 3), (2304, 9, 3, 1))
    assert_size_stride(primals_2, (2560,), (1,))
    assert_size_stride(primals_3, (4, 256, 64, 64), (1048576, 4096, 64, 1))
    assert_size_stride(primals_4, (5120, 256, 3, 3), (2304, 9, 3, 1))
    assert_size_stride(primals_5, (5120,), (1,))
    assert_size_stride(primals_6, (256, 256, 3, 3), (2304, 9, 3, 1))
    assert_size_stride(primals_7, (256,), (1,))
    assert_size_stride(primals_8, (4, 256, 64, 64), (1048576, 4096, 64, 1))
    assert_size_stride(primals_9, (256, 256, 3, 3), (2304, 9, 3, 1))
    assert_size_stride(primals_10, (256,), (1,))
    assert_size_stride(primals_11, (20, 20, 1, 1), (20, 1, 1, 1))
    assert_size_stride(primals_12, (20,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_3, 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, 2560, 62, 62), (9840640, 3844, 62, 1))
        buf1 = extern_kernels.convolution(primals_3, 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, 5120, 62, 62), (19681280, 3844, 62, 1))
        buf2 = extern_kernels.convolution(primals_8, primals_6, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 256, 62, 62), (984064, 3844, 62, 1))
        buf3 = extern_kernels.convolution(primals_8, primals_9, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf3, (4, 256, 62, 62), (984064, 3844, 62, 1))
        buf4 = buf0
        del buf0
        buf5 = reinterpret_tensor(buf4, (40, 256, 62, 62), (984064, 3844, 
            62, 1), 0)
        del buf4
        get_raw_stream(0)
        triton_poi_fused_convolution_view_0[grid(39362560)](buf5, primals_2,
            39362560, XBLOCK=512, num_warps=8, num_stages=1)
        del primals_2
        buf6 = buf2
        del buf2
        triton_poi_fused_convolution_1[grid(3936256)](buf6, primals_7, 
            3936256, XBLOCK=512, num_warps=8, num_stages=1)
        del primals_7
        buf7 = extern_kernels.convolution(reinterpret_tensor(buf6, (1, 1024,
            62, 62), (0, 3844, 62, 1), 0), buf5, stride=(1, 1), padding=(0,
            0), dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=4, bias=None)
        assert_size_stride(buf7, (1, 40, 1, 1), (40, 1, 1, 1))
        buf8 = buf1
        del buf1
        buf9 = reinterpret_tensor(buf8, (80, 256, 62, 62), (984064, 3844, 
            62, 1), 0)
        del buf8
        triton_poi_fused_convolution_view_2[grid(78725120)](buf9, primals_5,
            78725120, XBLOCK=512, num_warps=8, num_stages=1)
        del primals_5
        buf10 = buf3
        del buf3
        triton_poi_fused_convolution_1[grid(3936256)](buf10, primals_10, 
            3936256, XBLOCK=512, num_warps=8, num_stages=1)
        del primals_10
        buf11 = extern_kernels.convolution(reinterpret_tensor(buf10, (1, 
            1024, 62, 62), (0, 3844, 62, 1), 0), buf9, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=4, bias=None)
        assert_size_stride(buf11, (1, 80, 1, 1), (80, 1, 1, 1))
        buf12 = extern_kernels.convolution(reinterpret_tensor(buf11, (4, 20,
            1, 1), (20, 1, 1, 1), 0), primals_11, stride=(1, 1), padding=(0,
            0), dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf12, (4, 20, 1, 1), (20, 1, 1, 1))
        buf13 = buf12
        del buf12
        triton_poi_fused_convolution_3[grid(80)](buf13, primals_12, 80,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_12
    return (reinterpret_tensor(buf7, (4, 10, 1, 1), (10, 1, 1, 1), 0),
        buf13, primals_1, primals_3, primals_4, primals_6, primals_8,
        primals_9, primals_11, buf5, reinterpret_tensor(buf6, (1, 1024, 62,
        62), (3936256, 3844, 62, 1), 0), buf9, reinterpret_tensor(buf10, (1,
        1024, 62, 62), (3936256, 3844, 62, 1), 0), reinterpret_tensor(buf11,
        (4, 20, 1, 1), (20, 1, 1, 1), 0))


def xcorr_fast(x, kernel):
    """group conv2d to calculate cross correlation, fast version
    """
    batch = kernel.size()[0]
    pk = kernel.view(-1, x.size()[1], kernel.size()[2], kernel.size()[3])
    px = x.view(1, -1, x.size()[2], x.size()[3])
    po = F.conv2d(px, pk, groups=batch)
    po = po.view(batch, -1, po.size()[2], po.size()[3])
    return po


class RPN(nn.Module):

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

    def forward(self, z_f, x_f):
        raise NotImplementedError


class UPChannelRPNNew(RPN):

    def __init__(self, anchor_num=5, feature_in=256):
        super(UPChannelRPNNew, self).__init__()
        cls_output = 2 * anchor_num
        loc_output = 4 * anchor_num
        self.template_cls_conv = nn.Conv2d(feature_in, feature_in *
            cls_output, kernel_size=3)
        self.template_loc_conv = nn.Conv2d(feature_in, feature_in *
            loc_output, kernel_size=3)
        self.search_cls_conv = nn.Conv2d(feature_in, feature_in, kernel_size=3)
        self.search_loc_conv = nn.Conv2d(feature_in, feature_in, kernel_size=3)
        self.loc_adjust = nn.Conv2d(loc_output, loc_output, kernel_size=1)

    def forward(self, input_0, input_1):
        primals_1 = self.template_cls_conv.weight
        primals_2 = self.template_cls_conv.bias
        primals_4 = self.template_loc_conv.weight
        primals_5 = self.template_loc_conv.bias
        primals_6 = self.search_cls_conv.weight
        primals_7 = self.search_cls_conv.bias
        primals_9 = self.search_loc_conv.weight
        primals_10 = self.search_loc_conv.bias
        primals_11 = self.loc_adjust.weight
        primals_12 = self.loc_adjust.bias
        primals_3 = input_0
        primals_8 = input_1
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9,
            primals_10, primals_11, primals_12])
        return output[0], output[1]
