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
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_clone_transpose_0(in_out_ptr0, in_ptr0, out_ptr0,
    ynumel, xnumel, YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 128
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, YBLOCK], True, tl.int1)
    x2 = xindex
    y3 = yindex
    y0 = yindex % 32
    y1 = yindex // 32
    tmp0 = tl.load(in_out_ptr0 + (x2 + 4096 * y3), ymask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr0 + y0, ymask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (x2 + 4096 * y3), tmp2, ymask)
    tl.store(out_ptr0 + (y0 + 32 * x2 + 131072 * y1), tmp2, ymask)


@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 // 4096 % 32
    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_red_fused__softmax_2(in_ptr0, out_ptr2, xnumel, rnumel, XBLOCK:
    tl.constexpr, RBLOCK: tl.constexpr):
    rnumel = 4096
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rbase = tl.arange(0, RBLOCK)[None, :]
    x0 = xindex
    _tmp2 = tl.full([XBLOCK, RBLOCK], float('-inf'), tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp0 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
        tmp3 = triton_helpers.maximum(_tmp2, tmp1)
        _tmp2 = tl.where(rmask, tmp3, _tmp2)
    tmp2 = triton_helpers.max2(_tmp2, 1)[:, None]
    _tmp8 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp4 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp5 = tmp4 - tmp2
        tmp6 = tl_math.exp(tmp5)
        tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
        tmp9 = _tmp8 + tmp7
        _tmp8 = tl.where(rmask, tmp9, _tmp8)
    tmp8 = tl.sum(_tmp8, 1)[:, None]
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp10 = tl.load(in_ptr0 + (r1 + 4096 * x0), rmask, eviction_policy=
            'evict_first', other=0.0)
        tmp11 = tmp10 - tmp2
        tmp12 = tl_math.exp(tmp11)
        tmp13 = tmp12 / tmp8
        tl.store(out_ptr2 + (r1 + 4096 * x0), tmp13, rmask)


@triton.jit
def triton_poi_fused_clone_3(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 128
    yoffset = tl.program_id(1) * YBLOCK
    yindex = yoffset + tl.arange(0, YBLOCK)[None, :]
    ymask = yindex < ynumel
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, YBLOCK], True, tl.int1)
    x2 = xindex
    y0 = yindex % 32
    y1 = yindex // 32
    y3 = yindex
    tmp0 = tl.load(in_ptr0 + (y0 + 32 * x2 + 131072 * y1), ymask,
        eviction_policy='evict_last')
    tl.store(out_ptr0 + (x2 + 4096 * y3), tmp0, ymask)


@triton.jit
def triton_poi_fused_add_convolution_4(in_out_ptr0, in_ptr0, in_ptr1,
    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 // 4096 % 64
    tmp0 = tl.load(in_out_ptr0 + x3, None)
    tmp1 = tl.load(in_ptr0 + x1, None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr1 + x3, None)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tl.store(in_out_ptr0 + x3, tmp4, None)


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, 64, 64, 64), (262144, 4096, 64, 1))
    assert_size_stride(primals_2, (32, 64, 1, 1), (64, 1, 1, 1))
    assert_size_stride(primals_3, (32,), (1,))
    assert_size_stride(primals_4, (32, 64, 1, 1), (64, 1, 1, 1))
    assert_size_stride(primals_5, (32,), (1,))
    assert_size_stride(primals_6, (32, 64, 1, 1), (64, 1, 1, 1))
    assert_size_stride(primals_7, (32,), (1,))
    assert_size_stride(primals_8, (64, 32, 1, 1), (32, 1, 1, 1))
    assert_size_stride(primals_9, (64,), (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, 32, 64, 64), (131072, 4096, 64, 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, 32, 64, 64), (131072, 4096, 64, 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, 32, 64, 64), (131072, 4096, 64, 1))
        buf3 = reinterpret_tensor(buf0, (4, 4096, 32), (131072, 1, 4096), 0)
        del buf0
        buf15 = empty_strided_cuda((4, 32, 4096), (131072, 1, 32), torch.
            float32)
        get_raw_stream(0)
        triton_poi_fused_clone_transpose_0[grid(128, 4096)](buf3, primals_3,
            buf15, 128, 4096, XBLOCK=32, YBLOCK=128, num_warps=8, num_stages=1)
        del primals_3
        buf4 = buf1
        del buf1
        triton_poi_fused_convolution_1[grid(524288)](buf4, primals_5, 
            524288, XBLOCK=1024, num_warps=4, num_stages=1)
        del primals_5
        buf5 = empty_strided_cuda((4, 4096, 4096), (16777216, 4096, 1),
            torch.float32)
        extern_kernels.bmm(buf3, reinterpret_tensor(buf4, (4, 32, 4096), (
            131072, 4096, 1), 0), out=buf5)
        buf8 = empty_strided_cuda((4, 4096, 4096), (16777216, 4096, 1),
            torch.float32)
        triton_red_fused__softmax_2[grid(16384)](buf5, buf8, 16384, 4096,
            XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1)
        del buf5
        buf9 = reinterpret_tensor(buf2, (4, 4096, 32), (131072, 1, 4096), 0)
        del buf2
        buf14 = reinterpret_tensor(buf3, (4, 32, 4096), (131072, 1, 32), 0)
        del buf3
        triton_poi_fused_clone_transpose_0[grid(128, 4096)](buf9, primals_7,
            buf14, 128, 4096, XBLOCK=32, YBLOCK=128, num_warps=8, num_stages=1)
        del primals_7
        buf10 = empty_strided_cuda((4, 4096, 32), (131072, 32, 1), torch.
            float32)
        extern_kernels.bmm(buf8, buf9, out=buf10)
        buf11 = reinterpret_tensor(buf9, (4, 32, 4096), (131072, 4096, 1), 0)
        del buf9
        triton_poi_fused_clone_3[grid(128, 4096)](buf10, buf11, 128, 4096,
            XBLOCK=32, YBLOCK=32, num_warps=4, num_stages=1)
        del buf10
        buf12 = extern_kernels.convolution(reinterpret_tensor(buf11, (4, 32,
            64, 64), (131072, 4096, 64, 1), 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(buf12, (4, 64, 64, 64), (262144, 4096, 64, 1))
        buf13 = buf12
        del buf12
        triton_poi_fused_add_convolution_4[grid(1048576)](buf13, primals_9,
            primals_1, 1048576, XBLOCK=1024, num_warps=4, num_stages=1)
        del primals_9
    return (buf13, primals_1, primals_2, primals_4, primals_6, primals_8,
        buf8, reinterpret_tensor(buf11, (4, 32, 64, 64), (131072, 4096, 64,
        1), 0), buf14, buf15, reinterpret_tensor(buf4, (4, 4096, 32), (
        131072, 1, 4096), 0))


class GlobalNonLocalNew(nn.Module):

    def __init__(self, in_channel=64):
        super(GlobalNonLocalNew, self).__init__()
        assert in_channel % 2 == 0
        self.hidden_channel = in_channel // 2
        self.theta = nn.Conv2d(in_channel, self.hidden_channel, kernel_size
            =1, stride=1)
        self.phi = nn.Conv2d(in_channel, self.hidden_channel, kernel_size=1,
            stride=1)
        self.g = nn.Conv2d(in_channel, self.hidden_channel, kernel_size=1,
            stride=1)
        self.final = nn.Conv2d(self.hidden_channel, in_channel, kernel_size
            =1, stride=1)

    def forward(self, input_0):
        primals_2 = self.theta.weight
        primals_3 = self.theta.bias
        primals_4 = self.phi.weight
        primals_5 = self.phi.bias
        primals_6 = self.g.weight
        primals_7 = self.g.bias
        primals_8 = self.final.weight
        primals_9 = self.final.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]


Measured latency_ms: 2.230519