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 libdevice, 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_red_fused_linalg_vector_norm_0(in_ptr0, out_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    rnumel = 128
    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 % 4096
    x1 = xindex // 4096
    _tmp3 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    x3 = xindex
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r2 = rindex
        tmp0 = tl.load(in_ptr0 + (x0 + 4096 * r2 + 524288 * x1), rmask,
            eviction_policy='evict_last', 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]
    tl.store(out_ptr0 + x3, tmp3, None)


@triton.jit
def triton_poi_fused_div_1(in_ptr0, in_ptr1, out_ptr0, ynumel, xnumel,
    YBLOCK: tl.constexpr, XBLOCK: tl.constexpr):
    ynumel = 512
    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
    y1 = yindex // 128
    y0 = yindex % 128
    tmp0 = tl.load(in_ptr0 + (x2 + 4096 * y3), ymask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr1 + (x2 + 4096 * y1), ymask, eviction_policy=
        'evict_last')
    tmp2 = libdevice.sqrt(tmp1)
    tmp3 = 1e-12
    tmp4 = triton_helpers.maximum(tmp2, tmp3)
    tmp5 = tmp0 / tmp4
    tl.store(out_ptr0 + (y0 + 128 * x2 + 524288 * y1), tmp5, ymask)


@triton.jit
def triton_per_fused__softmax_convolution_2(in_out_ptr0, in_ptr0, out_ptr0,
    out_ptr1, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = 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)
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_out_ptr0 + (r1 + 64 * x0), None)
    tmp1 = tl.load(in_ptr0 + r1, None, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp3 = tl.broadcast_to(tmp2, [XBLOCK, RBLOCK])
    tmp5 = triton_helpers.max2(tmp3, 1)[:, None]
    tmp6 = tmp2 - tmp5
    tmp7 = tl_math.exp(tmp6)
    tmp8 = tl.broadcast_to(tmp7, [XBLOCK, RBLOCK])
    tmp10 = tl.sum(tmp8, 1)[:, None]
    tl.store(in_out_ptr0 + (r1 + 64 * x0), tmp2, None)
    tl.store(out_ptr0 + x0, tmp5, None)
    tl.store(out_ptr1 + x0, tmp10, None)


@triton.jit
def triton_red_fused_mul_sub_sum_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    in_ptr4, out_ptr0, 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 % 128
    x2 = xindex // 8192
    x4 = xindex % 8192
    tmp1 = tl.load(in_ptr1 + x4, None, eviction_policy='evict_last')
    x1 = xindex // 128 % 64
    _tmp11 = tl.full([XBLOCK, RBLOCK], 0, tl.float32)
    x5 = xindex
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r3 = rindex
        tmp0 = tl.load(in_ptr0 + (x0 + 128 * r3 + 524288 * x2), rmask,
            eviction_policy='evict_last', other=0.0)
        tmp3 = tl.load(in_ptr2 + (x1 + 64 * r3 + 262144 * x2), rmask,
            eviction_policy='evict_last', other=0.0)
        tmp4 = tl.load(in_ptr3 + (r3 + 4096 * x2), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp7 = tl.load(in_ptr4 + (r3 + 4096 * x2), rmask, eviction_policy=
            'evict_last', other=0.0)
        tmp2 = tmp0 - tmp1
        tmp5 = tmp3 - tmp4
        tmp6 = tl_math.exp(tmp5)
        tmp8 = tmp6 / tmp7
        tmp9 = tmp2 * tmp8
        tmp10 = tl.broadcast_to(tmp9, [XBLOCK, RBLOCK])
        tmp12 = _tmp11 + tmp10
        _tmp11 = tl.where(rmask, tmp12, _tmp11)
    tmp11 = tl.sum(_tmp11, 1)[:, None]
    tl.store(out_ptr0 + x5, tmp11, None)


@triton.jit
def triton_per_fused_linalg_vector_norm_4(in_out_ptr0, in_ptr0, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    RBLOCK: tl.constexpr = 128
    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 + 128 * x0), xmask, other=0.0)
    tmp1 = tmp0 * tmp0
    tmp2 = tl.broadcast_to(tmp1, [XBLOCK, RBLOCK])
    tmp4 = tl.where(xmask, tmp2, 0)
    tmp5 = tl.sum(tmp4, 1)[:, None]
    tmp6 = libdevice.sqrt(tmp5)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp6, xmask)


@triton.jit
def triton_red_fused_div_linalg_vector_norm_5(in_out_ptr0, in_ptr0, in_ptr1,
    out_ptr0, xnumel, rnumel, XBLOCK: tl.constexpr, RBLOCK: tl.constexpr):
    xnumel = 4
    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
    _tmp7 = 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.load(in_ptr1 + (64 * x0 + r1 // 128), rmask & xmask,
            eviction_policy='evict_last', other=0.0)
        tmp2 = 1e-12
        tmp3 = triton_helpers.maximum(tmp1, tmp2)
        tmp4 = tmp0 / tmp3
        tmp5 = tmp4 * tmp4
        tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
        tmp8 = _tmp7 + tmp6
        _tmp7 = tl.where(rmask & xmask, tmp8, _tmp7)
    tmp7 = tl.sum(_tmp7, 1)[:, None]
    tmp9 = libdevice.sqrt(tmp7)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp9, xmask)
    for roffset in range(0, rnumel, RBLOCK):
        rindex = roffset + rbase
        rmask = rindex < rnumel
        r1 = rindex
        tmp10 = tl.load(in_ptr0 + (r1 + 8192 * x0), rmask & xmask,
            eviction_policy='evict_first', other=0.0)
        tmp11 = tl.load(in_ptr1 + (64 * x0 + r1 // 128), rmask & xmask,
            eviction_policy='evict_last', other=0.0)
        tmp12 = 1e-12
        tmp13 = triton_helpers.maximum(tmp11, tmp12)
        tmp14 = tmp10 / tmp13
        tmp15 = triton_helpers.maximum(tmp9, tmp12)
        tmp16 = tmp14 / tmp15
        tl.store(out_ptr0 + (r1 + 8192 * x0), tmp16, rmask & xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 128, 64, 64), (524288, 4096, 64, 1))
    assert_size_stride(primals_2, (64, 128, 1, 1), (128, 1, 1, 1))
    assert_size_stride(primals_3, (64,), (1,))
    assert_size_stride(primals_4, (64, 128), (128, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 1, 64, 64), (4096, 16384, 64, 1),
            torch.float32)
        get_raw_stream(0)
        triton_red_fused_linalg_vector_norm_0[grid(16384)](primals_1, buf0,
            16384, 128, XBLOCK=64, RBLOCK=8, num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((4, 128, 64, 64), (524288, 1, 8192, 128),
            torch.float32)
        triton_poi_fused_div_1[grid(512, 4096)](primals_1, buf0, buf1, 512,
            4096, XBLOCK=16, YBLOCK=256, num_warps=8, num_stages=1)
        del primals_1
        buf2 = extern_kernels.convolution(buf1, primals_2, stride=(1, 1),
            padding=(0, 0), dilation=(1, 1), transposed=False,
            output_padding=(0, 0), groups=1, bias=None)
        assert_size_stride(buf2, (4, 64, 64, 64), (262144, 1, 4096, 64))
        buf3 = buf2
        del buf2
        buf4 = reinterpret_tensor(buf0, (4, 1, 4096), (4096, 4096, 1), 0)
        del buf0
        buf5 = empty_strided_cuda((4, 1, 4096), (4096, 4096, 1), torch.float32)
        triton_per_fused__softmax_convolution_2[grid(16384)](buf3,
            primals_3, buf4, buf5, 16384, 64, XBLOCK=32, num_warps=8,
            num_stages=1)
        del primals_3
        buf6 = empty_strided_cuda((4, 64, 128), (8192, 128, 1), torch.float32)
        triton_red_fused_mul_sub_sum_3[grid(32768)](buf1, primals_4, buf3,
            buf4, buf5, buf6, 32768, 4096, XBLOCK=8, RBLOCK=256, num_warps=
            16, num_stages=1)
        buf7 = empty_strided_cuda((4, 64, 1), (64, 1, 256), torch.float32)
        buf8 = reinterpret_tensor(buf7, (4, 64, 1), (64, 1, 1), 0)
        del buf7
        triton_per_fused_linalg_vector_norm_4[grid(256)](buf8, buf6, 256, 
            128, XBLOCK=8, num_warps=8, num_stages=1)
        buf9 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        buf10 = reinterpret_tensor(buf9, (4, 1), (1, 1), 0)
        del buf9
        buf11 = empty_strided_cuda((4, 8192), (8192, 1), torch.float32)
        triton_red_fused_div_linalg_vector_norm_5[grid(4)](buf10, buf6,
            buf8, buf11, 4, 8192, XBLOCK=1, RBLOCK=2048, num_warps=16,
            num_stages=1)
    return (buf11, primals_2, primals_4, buf1, buf3, buf4, buf5, buf6, buf8,
        buf10)


class NetVLADNew(nn.Module):
    """NetVLAD layer implementation"""

    def __init__(self, num_clusters=64, dim=128, alpha=100.0,
        normalize_input=True):
        """
        Args:
            num_clusters : int
                The number of clusters
            dim : int
                Dimension of descriptors
            alpha : float
                Parameter of initialization. Larger value is harder assignment.
            normalize_input : bool
                If true, descriptor-wise L2 normalization is applied to input.
        """
        super(NetVLADNew, self).__init__()
        self.num_clusters = num_clusters
        self.dim = dim
        self.alpha = alpha
        self.normalize_input = normalize_input
        self.conv = nn.Conv2d(dim, num_clusters, kernel_size=(1, 1), bias=True)
        self.centroids = nn.Parameter(torch.rand(num_clusters, dim))
        self._init_params()

    def _init_params(self):
        self.conv.weight = nn.Parameter((2.0 * self.alpha * self.centroids)
            .unsqueeze(-1).unsqueeze(-1))
        self.conv.bias = nn.Parameter(-self.alpha * self.centroids.norm(dim=1))

    def forward(self, input_0):
        primals_4 = self.centroids
        primals_2 = self.conv.weight
        primals_3 = self.conv.bias
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3, primals_4])
        return output[0]


Measured latency_ms: 0.034232