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 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__softmax_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x0 = xindex % 16
    x2 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = triton_helpers.maximum(tmp1, tmp2)
    tmp5 = triton_helpers.maximum(tmp3, tmp4)
    tmp7 = triton_helpers.maximum(tmp5, tmp6)
    tmp8 = tmp0 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tl.store(out_ptr0 + x3, tmp9, xmask)


@triton.jit
def triton_poi_fused__softmax_1(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr
    ):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex
    x0 = xindex % 16
    x2 = xindex // 64
    tmp0 = tl.load(in_ptr0 + x3, xmask)
    tmp1 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp2 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp4 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp6 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = tmp1 + tmp2
    tmp5 = tmp3 + tmp4
    tmp7 = tmp5 + tmp6
    tmp8 = tmp0 / tmp7
    tl.store(out_ptr0 + x3, tmp8, xmask)


@triton.jit
def triton_per_fused_sub_sum_2(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    out_ptr1, out_ptr2, out_ptr3, out_ptr4, out_ptr5, out_ptr6, xnumel,
    rnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    RBLOCK: tl.constexpr = 16
    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)
    r2 = rindex
    x3 = xindex
    x0 = xindex % 4
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + (r2 + 16 * x3), xmask, other=0.0)
    tmp1 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp9 = tl.load(in_ptr1 + (4 + x0), xmask, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr1 + (8 + x0), xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr1 + (12 + x0), xmask, eviction_policy='evict_last')
    tmp15 = tl.load(in_ptr2 + (16 + r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp21 = tl.load(in_ptr2 + (32 + r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp27 = tl.load(in_ptr2 + (48 + r2 + 64 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp2 = tmp0 - tmp1
    tmp4 = tmp2 * tmp3
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp7 = tl.where(xmask, tmp5, 0)
    tmp8 = tl.sum(tmp7, 1)[:, None]
    tmp10 = tmp0 - tmp9
    tmp12 = tmp0 - tmp11
    tmp14 = tmp0 - tmp13
    tmp16 = tmp10 * tmp15
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, 0)
    tmp20 = tl.sum(tmp19, 1)[:, None]
    tmp22 = tmp12 * tmp21
    tmp23 = tl.broadcast_to(tmp22, [XBLOCK, RBLOCK])
    tmp25 = tl.where(xmask, tmp23, 0)
    tmp26 = tl.sum(tmp25, 1)[:, None]
    tmp28 = tmp14 * tmp27
    tmp29 = tl.broadcast_to(tmp28, [XBLOCK, RBLOCK])
    tmp31 = tl.where(xmask, tmp29, 0)
    tmp32 = tl.sum(tmp31, 1)[:, None]
    tl.store(out_ptr1 + (r2 + 16 * x3), tmp10, xmask)
    tl.store(out_ptr2 + (r2 + 16 * x3), tmp12, xmask)
    tl.store(out_ptr3 + (r2 + 16 * x3), tmp14, xmask)
    tl.store(out_ptr0 + x3, tmp8, xmask)
    tl.store(out_ptr4 + x3, tmp20, xmask)
    tl.store(out_ptr5 + x3, tmp26, xmask)
    tl.store(out_ptr6 + x3, tmp32, xmask)


@triton.jit
def triton_poi_fused_copy_zeros_3(in_ptr0, in_ptr1, in_ptr2, in_ptr3,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 4 % 4
    x0 = xindex % 4
    x2 = xindex // 16
    x3 = xindex
    tmp0 = x1
    tmp1 = tl.full([1], 3, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.load(in_ptr0 + (x0 + 4 * x2), tmp2 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp4 = tl.full([1], 2, tl.int64)
    tmp5 = tmp0 >= tmp4
    tmp6 = tmp0 < tmp1
    tmp7 = tmp5 & tmp6
    tmp8 = tl.load(in_ptr1 + (x0 + 4 * x2), tmp7 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp9 = tl.full([1], 1, tl.int64)
    tmp10 = tmp0 >= tmp9
    tmp11 = tmp0 < tmp4
    tmp12 = tmp10 & tmp11
    tmp13 = tl.load(in_ptr2 + (x0 + 4 * x2), tmp12 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp14 = tmp0 < tmp9
    tmp15 = tl.load(in_ptr3 + (x0 + 4 * x2), tmp14 & xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp16 = 0.0
    tmp17 = tl.where(tmp14, tmp15, tmp16)
    tmp18 = tl.where(tmp12, tmp13, tmp17)
    tmp19 = tl.where(tmp7, tmp8, tmp18)
    tmp20 = tl.where(tmp2, tmp3, tmp19)
    tl.store(out_ptr0 + x3, tmp20, xmask)


@triton.jit
def triton_per_fused_div_linalg_vector_norm_native_layer_norm_4(in_out_ptr0,
    in_out_ptr1, in_ptr0, in_ptr1, in_ptr2, out_ptr0, out_ptr1, out_ptr2,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    RBLOCK: tl.constexpr = 16
    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)
    r3 = rindex
    x0 = xindex
    r2 = rindex // 4
    tmp0 = tl.load(in_ptr0 + (r3 + 16 * x0), xmask, other=0.0)
    tmp1 = tl.load(in_ptr0 + (4 * r2 + 16 * x0), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp3 = tl.load(in_ptr0 + (1 + 4 * r2 + 16 * x0), xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp6 = tl.load(in_ptr0 + (2 + 4 * r2 + 16 * x0), xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp9 = tl.load(in_ptr0 + (3 + 4 * r2 + 16 * x0), xmask, eviction_policy
        ='evict_last', other=0.0)
    tmp47 = tl.load(in_ptr1 + r3, None, eviction_policy='evict_last')
    tmp49 = tl.load(in_ptr2 + r3, None, eviction_policy='evict_last')
    tmp2 = tmp1 * tmp1
    tmp4 = tmp3 * tmp3
    tmp5 = tmp2 + tmp4
    tmp7 = tmp6 * tmp6
    tmp8 = tmp5 + tmp7
    tmp10 = tmp9 * tmp9
    tmp11 = tmp8 + tmp10
    tmp12 = libdevice.sqrt(tmp11)
    tmp13 = 1e-12
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp0 / tmp14
    tmp16 = tmp15 * tmp15
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, 0)
    tmp20 = tl.sum(tmp19, 1)[:, None]
    tmp21 = libdevice.sqrt(tmp20)
    tmp22 = triton_helpers.maximum(tmp21, tmp13)
    tmp23 = tmp15 / tmp22
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tl.where(xmask, tmp24, 0)
    tmp27 = tl.broadcast_to(tmp24, [XBLOCK, RBLOCK])
    tmp29 = tl.where(xmask, tmp27, 0)
    tmp30 = tl.sum(tmp29, 1)[:, None]
    tmp31 = tl.full([XBLOCK, 1], 16, tl.int32)
    tmp32 = tmp31.to(tl.float32)
    tmp33 = tmp30 / tmp32
    tmp34 = tmp24 - tmp33
    tmp35 = tmp34 * tmp34
    tmp36 = tl.broadcast_to(tmp35, [XBLOCK, RBLOCK])
    tmp38 = tl.where(xmask, tmp36, 0)
    tmp39 = tl.sum(tmp38, 1)[:, None]
    tmp40 = 16.0
    tmp41 = tmp39 / tmp40
    tmp42 = 1e-05
    tmp43 = tmp41 + tmp42
    tmp44 = libdevice.rsqrt(tmp43)
    tmp45 = tmp23 - tmp33
    tmp46 = tmp45 * tmp44
    tmp48 = tmp46 * tmp47
    tmp50 = tmp48 + tmp49
    tl.store(out_ptr0 + (r3 + 16 * x0), tmp15, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x0, tmp21, xmask)
    tl.debug_barrier()
    tl.store(in_out_ptr1 + x0, tmp44, xmask)
    tl.store(out_ptr2 + (r3 + 16 * x0), tmp50, xmask)
    tl.store(out_ptr1 + x0, tmp33, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4, primals_5 = 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, 4), (4, 1))
    assert_size_stride(primals_4, (16,), (1,))
    assert_size_stride(primals_5, (16,), (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 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused__softmax_0[grid(256)](buf0, buf1, 256, XBLOCK=256,
            num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 16), (64, 16, 1), torch.float32)
        triton_poi_fused__softmax_1[grid(256)](buf1, buf2, 256, XBLOCK=128,
            num_warps=4, num_stages=1)
        buf3 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf4 = reinterpret_tensor(buf1, (4, 1, 4, 16), (64, 1, 16, 1), 0)
        del buf1
        buf6 = empty_strided_cuda((4, 1, 4, 16), (64, 1, 16, 1), torch.float32)
        buf8 = empty_strided_cuda((4, 1, 4, 16), (64, 1, 16, 1), torch.float32)
        buf5 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf7 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf9 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        triton_per_fused_sub_sum_2[grid(16)](primals_1, primals_3, buf2,
            buf3, buf4, buf6, buf8, buf5, buf7, buf9, 16, 16, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf2
        buf10 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_copy_zeros_3[grid(64)](buf9, buf7, buf5, buf3,
            buf10, 64, XBLOCK=64, num_warps=1, num_stages=1)
        del buf3
        del buf5
        del buf7
        del buf9
        buf11 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        buf12 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        buf13 = reinterpret_tensor(buf12, (4, 1), (1, 1), 0)
        del buf12
        buf14 = empty_strided_cuda((4, 1), (1, 1), torch.float32)
        buf15 = empty_strided_cuda((4, 1), (1, 4), torch.float32)
        buf17 = reinterpret_tensor(buf15, (4, 1), (1, 1), 0)
        del buf15
        buf18 = empty_strided_cuda((4, 16), (16, 1), torch.float32)
        triton_per_fused_div_linalg_vector_norm_native_layer_norm_4[grid(4)](
            buf13, buf17, buf10, primals_4, primals_5, buf11, buf14, buf18,
            4, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del buf11
        del primals_5
    return buf18, primals_1, primals_2, primals_4, buf0, reinterpret_tensor(
        primals_3, (1, 4), (4, 1), 0), reinterpret_tensor(buf4, (4, 4, 4, 4
        ), (64, 16, 4, 1), 0), reinterpret_tensor(buf6, (4, 4, 4, 4), (64, 
        16, 4, 1), 0), reinterpret_tensor(buf8, (4, 4, 4, 4), (64, 16, 4, 1), 0
        ), buf10, buf13, buf14, buf17


class NetVLADNew(nn.Module):

    def __init__(self, dims, num_clusters, outdims=None):
        super(NetVLADNew, self).__init__()
        self.num_clusters = num_clusters
        self.dims = dims
        self.centroids = nn.Parameter(torch.randn(num_clusters, dims) /
            math.sqrt(self.dims))
        self.conv = nn.Conv2d(dims, num_clusters, kernel_size=1, bias=False)
        if outdims is not None:
            self.outdims = outdims
            self.reduction_layer = nn.Linear(self.num_clusters * self.dims,
                self.outdims, bias=False)
        else:
            self.outdims = self.num_clusters * self.dims
        self.norm = nn.LayerNorm(self.outdims)
        self.reset_parameters()

    def reset_parameters(self):
        self.conv.weight = nn.Parameter(self.centroids.detach().clone().
            unsqueeze(-1).unsqueeze(-1))
        if hasattr(self, 'reduction_layer'):
            nn.init.normal_(self.reduction_layer.weight, std=1 / math.sqrt(
                self.num_clusters * self.dims))

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


Measured latency_ms: 0.017242