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


@triton.jit
def triton_poi_fused_add_mul_sub_sum_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x2 = xindex // 64
    x1 = xindex // 16 % 4
    x3 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp3 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp5 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp7 = tl.load(in_ptr1 + 4 * x1, xmask, eviction_policy='evict_last')
    tmp9 = tl.load(in_ptr1 + (1 + 4 * x1), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr1 + (2 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp15 = tl.load(in_ptr1 + (3 + 4 * x1), xmask, eviction_policy='evict_last'
        )
    tmp19 = tl.load(in_ptr2 + x3, xmask)
    tmp2 = tmp0 + tmp1
    tmp4 = tmp2 + tmp3
    tmp6 = tmp4 + tmp5
    tmp8 = tmp7 * tmp7
    tmp10 = tmp9 * tmp9
    tmp11 = tmp8 + tmp10
    tmp13 = tmp12 * tmp12
    tmp14 = tmp11 + tmp13
    tmp16 = tmp15 * tmp15
    tmp17 = tmp14 + tmp16
    tmp18 = tmp6 + tmp17
    tmp20 = 2.0
    tmp21 = tmp19 * tmp20
    tmp22 = tmp18 - tmp21
    tl.store(out_ptr0 + x3, tmp22, xmask)


@triton.jit
def triton_poi_fused__softmax_div_neg_pow_relu_1(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, xnumel, XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 16
    x1 = xindex // 16
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x1), xmask)
    tmp4 = tl.load(in_ptr1 + 0)
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK])
    tmp8 = tl.load(in_ptr0 + (16 + x0 + 64 * x1), xmask)
    tmp11 = tl.load(in_ptr1 + 1)
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK])
    tmp16 = tl.load(in_ptr0 + (32 + x0 + 64 * x1), xmask)
    tmp19 = tl.load(in_ptr1 + 2)
    tmp20 = tl.broadcast_to(tmp19, [XBLOCK])
    tmp24 = tl.load(in_ptr0 + (48 + x0 + 64 * x1), xmask)
    tmp27 = tl.load(in_ptr1 + 3)
    tmp28 = tl.broadcast_to(tmp27, [XBLOCK])
    tmp1 = tl.full([1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp3 = -tmp2
    tmp6 = tmp5 * tmp5
    tmp7 = tmp3 / tmp6
    tmp9 = triton_helpers.maximum(tmp1, tmp8)
    tmp10 = -tmp9
    tmp13 = tmp12 * tmp12
    tmp14 = tmp10 / tmp13
    tmp15 = triton_helpers.maximum(tmp7, tmp14)
    tmp17 = triton_helpers.maximum(tmp1, tmp16)
    tmp18 = -tmp17
    tmp21 = tmp20 * tmp20
    tmp22 = tmp18 / tmp21
    tmp23 = triton_helpers.maximum(tmp15, tmp22)
    tmp25 = triton_helpers.maximum(tmp1, tmp24)
    tmp26 = -tmp25
    tmp29 = tmp28 * tmp28
    tmp30 = tmp26 / tmp29
    tmp31 = triton_helpers.maximum(tmp23, tmp30)
    tmp32 = tmp7 - tmp31
    tmp33 = tl_math.exp(tmp32)
    tmp34 = tmp14 - tmp31
    tmp35 = tl_math.exp(tmp34)
    tmp36 = tmp33 + tmp35
    tmp37 = tmp22 - tmp31
    tmp38 = tl_math.exp(tmp37)
    tmp39 = tmp36 + tmp38
    tmp40 = tmp30 - tmp31
    tmp41 = tl_math.exp(tmp40)
    tmp42 = tmp39 + tmp41
    tl.store(out_ptr0 + x2, tmp31, xmask)
    tl.store(out_ptr1 + x2, tmp42, xmask)


@triton.jit
def triton_per_fused__softmax_div_mean_mul_neg_pow_relu_2(in_out_ptr0,
    in_ptr0, in_ptr1, in_ptr2, in_ptr3, 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)
    tmp4 = tl.load(in_ptr1 + x0, xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr2 + (r2 + 16 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp10 = tl.load(in_ptr3 + (r2 + 16 * x1), xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp1 = tl.full([1, 1], 0, tl.int32)
    tmp2 = triton_helpers.maximum(tmp1, tmp0)
    tmp3 = -tmp2
    tmp5 = tmp4 * tmp4
    tmp6 = tmp3 / tmp5
    tmp8 = tmp6 - tmp7
    tmp9 = tl_math.exp(tmp8)
    tmp11 = tmp9 / tmp10
    tmp12 = tl.broadcast_to(tmp11, [XBLOCK, RBLOCK])
    tmp14 = tl.where(xmask, tmp12, 0)
    tmp15 = tl.sum(tmp14, 1)[:, None]
    tmp16 = 16.0
    tmp17 = tmp15 / tmp16
    tmp18 = 4.0
    tmp19 = tmp17 * tmp18
    tl.debug_barrier()
    tl.store(in_out_ptr0 + x3, tmp19, xmask)


def call(args):
    primals_1, primals_2, primals_3 = 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, (1, 4, 1, 1), (4, 1, 1, 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, 4, 4), (64, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_add_mul_sub_sum_0[grid(256)](primals_1, primals_2,
            buf0, buf1, 256, XBLOCK=256, num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 1, 4, 4), (16, 64, 4, 1), torch.float32)
        triton_poi_fused__softmax_div_neg_pow_relu_1[grid(64)](buf1,
            primals_3, buf2, buf3, 64, XBLOCK=64, num_warps=1, num_stages=1)
        buf4 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        buf5 = buf4
        del buf4
        triton_per_fused__softmax_div_mean_mul_neg_pow_relu_2[grid(16)](buf5,
            buf1, primals_3, buf2, buf3, 16, 16, XBLOCK=1, num_warps=2,
            num_stages=1)
        del buf1
        del buf2
        del buf3
    return buf5, primals_1, primals_2, primals_3, buf0


class BoF_PoolingNew(nn.Module):

    def __init__(self, n_codewords, features, spatial_level=0, **kwargs):
        super(BoF_PoolingNew, self).__init__()
        """
        Initializes a BoF Pooling layer
        :param n_codewords: the number of the codewords to be used
        :param spatial_level: 0 -> no spatial pooling, 1 -> spatial pooling at level 1 (4 regions). Note that the
         codebook is shared between the different spatial regions
        :param kwargs:
        """
        self.N_k = n_codewords
        self.spatial_level = spatial_level
        self.V, self.sigmas = None, None
        self.relu = nn.ReLU()
        self.init(features)
        self.softmax = nn.Softmax(dim=1)

    def init(self, features):
        self.V = nn.Parameter(nn.init.uniform_(torch.empty((self.N_k,
            features, 1, 1), requires_grad=True)))
        self.sigmas = nn.Parameter(nn.init.constant_(torch.empty((1, self.
            N_k, 1, 1), requires_grad=True), 0.1))

    def compute_output_shape(self, input_shape):
        if self.spatial_level == 0:
            return input_shape[0], self.N_k
        elif self.spatial_level == 1:
            return input_shape[0], 4 * self.N_k

    def forward(self, input_0):
        primals_2 = self.V
        primals_3 = self.sigmas
        primals_1 = input_0
        output = call([primals_1, primals_2, primals_3])
        return output[0]


Measured latency_ms: 0.055411