import torch
import torch.nn as nn


class RelationNonLocal(nn.Module):

    def __init__(self, C):
        super(RelationNonLocal, self).__init__()
        self.conv_fv = nn.Conv2d(C, C, kernel_size=1, stride=1)
        self.conv_fk = nn.Conv2d(C, C, kernel_size=1, stride=1)
        self.conv_fq = nn.Conv2d(C, C, kernel_size=1, stride=1)
        self.conv_fr = nn.Conv2d(C, C, kernel_size=1, stride=1)

    def forward(self, input_):
        N, C, H, W = input_.shape
        f_v = self.conv_fv(input_)
        f_k = self.conv_fk(input_)
        f_q = self.conv_fq(input_)
        f_k = f_k.reshape([N, C, H * W]).permute(0, 2, 1)
        f_q = f_q.reshape([N, C, H * W])
        w = torch.matmul(f_k, f_q) / (H * W)
        f_r = torch.matmul(w.permute(0, 2, 1), f_v.reshape([N, C, H * W]).
            permute(0, 2, 1)).permute(0, 2, 1)
        f_r = f_r.reshape(N, C, H, W)
        f_r = self.conv_fr(f_r)
        return f_r


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


def get_init_inputs():
    return [[], {'C': 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
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_poi_fused_convolution_0(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x1 = xindex // 16 % 4
    tmp0 = tl.load(in_out_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + x1, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.store(in_out_ptr0 + x3, tmp2, xmask)


@triton.jit
def triton_poi_fused_div_1(in_out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + x0, xmask)
    tmp1 = 0.0625
    tmp2 = tmp0 * tmp1
    tl.store(in_out_ptr0 + x0, tmp2, xmask)


@triton.jit
def triton_poi_fused_convolution_2(in_out_ptr0, in_ptr0, xnumel, XBLOCK: tl
    .constexpr):
    xnumel = 256
    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
    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) = args
    args.clear()
    assert_size_stride(primals_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(primals_2, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_3, (4,), (1,))
    assert_size_stride(primals_4, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_5, (4,), (1,))
    assert_size_stride(primals_6, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_7, (4,), (1,))
    assert_size_stride(primals_8, (4, 4, 1, 1), (4, 1, 1, 1))
    assert_size_stride(primals_9, (4,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(primals_1, primals_2, stride=(1, 
            1), padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf0, (4, 4, 4, 4), (64, 16, 4, 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, 4, 4), (64, 16, 4, 1))
        buf2 = extern_kernels.convolution(primals_1, 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, 4, 4, 4), (64, 16, 4, 1))
        buf3 = buf1
        del buf1
        get_raw_stream(0)
        triton_poi_fused_convolution_0[grid(256)](buf3, primals_5, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        del primals_5
        buf4 = buf2
        del buf2
        triton_poi_fused_convolution_0[grid(256)](buf4, primals_7, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        del primals_7
        buf5 = empty_strided_cuda((4, 16, 16), (256, 16, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf3, (4, 16, 4), (64, 1, 16),
            0), reinterpret_tensor(buf4, (4, 4, 16), (64, 16, 1), 0), out=buf5)
        buf6 = buf0
        del buf0
        triton_poi_fused_convolution_0[grid(256)](buf6, primals_3, 256,
            XBLOCK=256, num_warps=4, num_stages=1)
        del primals_3
        buf7 = buf5
        del buf5
        triton_poi_fused_div_1[grid(1024)](buf7, 1024, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf8 = empty_strided_cuda((4, 16, 4), (64, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(buf7, (4, 16, 16), (256, 1, 
            16), 0), reinterpret_tensor(buf6, (4, 16, 4), (64, 1, 16), 0),
            out=buf8)
        buf9 = extern_kernels.convolution(reinterpret_tensor(buf8, (4, 4, 4,
            4), (64, 1, 16, 4), 0), primals_8, stride=(1, 1), padding=(0, 0
            ), dilation=(1, 1), transposed=False, output_padding=(0, 0),
            groups=1, bias=None)
        assert_size_stride(buf9, (4, 4, 4, 4), (64, 1, 16, 4))
        buf10 = buf9
        del buf9
        triton_poi_fused_convolution_2[grid(256)](buf10, primals_9, 256,
            XBLOCK=128, num_warps=4, num_stages=1)
        del primals_9
    return (buf10, primals_1, primals_2, primals_4, primals_6, primals_8,
        reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 1, 16, 4), 0), buf7,
        reinterpret_tensor(buf6, (4, 4, 16), (64, 16, 1), 0),
        reinterpret_tensor(buf3, (4, 4, 16), (64, 16, 1), 0),
        reinterpret_tensor(buf4, (4, 16, 4), (64, 1, 16), 0))


class RelationNonLocalNew(nn.Module):

    def __init__(self, C):
        super(RelationNonLocalNew, self).__init__()
        self.conv_fv = nn.Conv2d(C, C, kernel_size=1, stride=1)
        self.conv_fk = nn.Conv2d(C, C, kernel_size=1, stride=1)
        self.conv_fq = nn.Conv2d(C, C, kernel_size=1, stride=1)
        self.conv_fr = nn.Conv2d(C, C, kernel_size=1, stride=1)

    def forward(self, input_0):
        primals_2 = self.conv_fv.weight
        primals_3 = self.conv_fv.bias
        primals_4 = self.conv_fk.weight
        primals_5 = self.conv_fk.bias
        primals_6 = self.conv_fq.weight
        primals_7 = self.conv_fq.bias
        primals_8 = self.conv_fr.weight
        primals_9 = self.conv_fr.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4,
            primals_5, primals_6, primals_7, primals_8, primals_9])
        return output[0]
