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 import 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_ptr0, out_ptr1, out_ptr2, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + 65 * r0, None, eviction_policy='evict_last')
    tmp1 = -1.0
    tmp2 = tmp0 + tmp1
    tmp3 = tmp2 * tmp2
    tmp4 = tl.broadcast_to(tmp3, [XBLOCK, RBLOCK])
    tmp6 = tl.sum(tmp4, 1)[:, None]
    tl.store(out_ptr1 + tl.broadcast_to(65 * r0, [XBLOCK, RBLOCK]), tmp2, None)
    tl.store(out_ptr2 + tl.full([XBLOCK, 1], 0, tl.int32), tmp6, None)


@triton.jit
def triton_red_fused_add_div_mul_pow_sum_1(in_out_ptr0, in_ptr0, xnumel,
    rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    rnumel = 4032
    xoffset = tl.program_id(0) * XBLOCK
    xoffset + tl.arange(0, XBLOCK)[:, None]
    tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rbase = tl.arange(0, RBLOCK)[None, :]
    _tmp3 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r0 = rindex
        tmp0 = tl.load(in_ptr0 + (1 + 65 * (r0 // 64) + r0 % 64), rmask,
            eviction_policy='evict_first', other=0.0)
        tmp1 = tmp0 * tmp0
        tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
        tmp4 = _tmp3 + tmp2
        _tmp3 = tl.where(rmask, tmp4, _tmp3)
    tmp3 = tl.sum(_tmp3, 1)[:, None]
    tmp5 = tl.load(in_out_ptr0 + 0)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, 1])
    tmp7 = 1.0
    tmp8 = tmp3 * tmp7
    tmp9 = tmp6 + tmp8
    tmp10 = 0.000244140625
    tmp11 = tmp9 * tmp10
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp11, None)


def call(args):
    arg0_1, = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((64, 64), (64, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(arg0_1, (64, 2), (1, 128), 0),
            reinterpret_tensor(arg0_1, (2, 64), (128, 1), 64), out=buf0)
        del arg0_1
        buf3 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_add_pow_sum_0[grid(1)](buf0, buf0, buf3, 1, 64,
            XBLOCK=1, num_warps=2, num_stages=1)
        buf5 = buf3
        del buf3
        triton_red_fused_add_div_mul_pow_sum_1[grid(1)](buf5, buf0, 1, 4032,
            XBLOCK=1, RBLOCK=2048, num_warps=16, num_stages=1)
        del buf0
    return buf5,


class PairwiseCrossCorrelationNew(nn.Module):

    def __init__(self, lambd=1):
        super().__init__()
        self.lambd = lambd

    def off_diagonal(self, x):
        n, m = x.shape
        assert n == m
        return x.flatten()[:-1].view(n - 1, n + 1)[:, 1:].flatten()

    def forward(self, input_0):
        arg0_1 = input_0
        output = call([arg0_1])
        return output[0]


Measured latency_ms: 0.034486