# AOT ID: ['0_forward']
from ctypes import c_void_p, c_long, c_int
import torch
import math
import random
import os
import tempfile
from math import inf, nan
from torch._inductor.hooks import run_intermediate_hooks
from torch._inductor.utils import maybe_profile
from torch._inductor.codegen.memory_planning import _align as align
from torch import device, empty_strided
from torch._inductor.async_compile import AsyncCompile
from torch._inductor.select_algorithm import extern_kernels
from torch._inductor.codegen.multi_kernel import MultiKernelCall
import triton
import triton.language as tl
from torch._inductor.runtime.triton_heuristics import grid, split_scan_grid, grid_combo_kernels, start_graph, end_graph
from torch._C import _cuda_getCurrentRawStream as get_raw_stream

aten = torch.ops.aten
inductor_ops = torch.ops.inductor
_quantized = torch.ops._quantized
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cpu = torch._C._dynamo.guards._empty_strided_cpu
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
empty_strided_xpu = torch._C._dynamo.guards._empty_strided_xpu
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor
alloc_from_pool = torch.ops.inductor._alloc_from_pool
async_compile = AsyncCompile()


# kernel path: runs/run_shard_7/inductor_cache/ut/cut6cqulw73tholzcyksggio6hke2lni4wxkvbauepmazihm5hwn.py
# Topologically Sorted Source Nodes: [min_1, q_1, nbs, mul, rec, cumsum, cumsum_1, add, prec, sum_3, rec_1, mul_1, ap], Original ATen: [aten.minimum, aten.clamp, aten.sum, aten.mul, aten.cumsum, aten.add, aten.div]
# Source node to ATen node mapping:
#   add => add
#   ap => sum_4
#   cumsum => cumsum
#   cumsum_1 => cumsum_1
#   min_1 => minimum
#   mul => mul
#   mul_1 => mul_1
#   nbs => sum_1
#   prec => div
#   q_1 => clamp_min
#   rec => sum_2
#   rec_1 => div_1
#   sum_3 => sum_3
# Graph fragment:
#   %minimum : [num_users=2] = call_function[target=torch.ops.aten.minimum.default](args = (%slice_2, %slice_4), kwargs = {})
#   %clamp_min : [num_users=2] = call_function[target=torch.ops.aten.clamp_min.default](args = (%minimum, 0), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%clamp_min, [-1]), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clamp_min, %view), kwargs = {})
#   %sum_2 : [num_users=3] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul, [-1]), kwargs = {})
#   %cumsum : [num_users=2] = call_function[target=torch.ops.aten.cumsum.default](args = (%sum_2, -1), kwargs = {})
#   %cumsum_1 : [num_users=2] = call_function[target=torch.ops.aten.cumsum.default](args = (%sum_1, -1), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%cumsum_1, 1e-16), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%cumsum, %add), kwargs = {})
#   %sum_3 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%sum_2, [-1]), kwargs = {})
#   %div_1 : [num_users=2] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_2, %unsqueeze_1), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div, %div_1), kwargs = {})
#   %sum_4 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_1, [-1]), kwargs = {})
triton_per_fused_add_clamp_cumsum_div_minimum_mul_sum_0 = async_compile.triton('triton_per_fused_add_clamp_cumsum_div_minimum_mul_sum_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton.jit
def _triton_helper_fn_add0(arg0_0, arg1_0):
    tmp0 = arg0_0 + arg1_0
    return tmp0

@triton_heuristics.persistent_reduction(
    size_hints=[4, 32],
    reduction_hint=ReductionHint.DEFAULT,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: '*fp32', 6: '*fp32', 7: '*fp32', 8: 'i32', 9: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5, 6, 7), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_add_clamp_cumsum_div_minimum_mul_sum_0', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 14, 'num_reduction': 2, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False}
)
@triton.jit
def triton_per_fused_add_clamp_cumsum_div_minimum_mul_sum_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, out_ptr1, out_ptr2, out_ptr3, out_ptr4, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 4
    rnumel = 25
    RBLOCK: tl.constexpr = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    roffset = 0
    rmask = rindex < rnumel
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + ((4*r1) + (200*x0)), rmask & xmask, eviction_policy='evict_last', other=0.0)
    tmp1 = tl.load(in_ptr1 + (r1), rmask, eviction_policy='evict_last', other=0.0)
    tmp3 = tl.load(in_ptr0 + (100 + (4*r1) + (200*x0)), rmask & xmask, eviction_policy='evict_last', other=0.0)
    tmp4 = tl.load(in_ptr1 + (25 + r1), rmask, eviction_policy='evict_last', other=0.0)
    tmp9 = tl.load(in_ptr0 + (1 + (4*r1) + (200*x0)), rmask & xmask, eviction_policy='evict_last', other=0.0)
    tmp11 = tl.load(in_ptr0 + (101 + (4*r1) + (200*x0)), rmask & xmask, eviction_policy='evict_last', other=0.0)
    tmp16 = tl.load(in_ptr0 + (2 + (4*r1) + (200*x0)), rmask & xmask, eviction_policy='evict_last', other=0.0)
    tmp18 = tl.load(in_ptr0 + (102 + (4*r1) + (200*x0)), rmask & xmask, eviction_policy='evict_last', other=0.0)
    tmp23 = tl.load(in_ptr0 + (3 + (4*r1) + (200*x0)), rmask & xmask, eviction_policy='evict_last', other=0.0)
    tmp25 = tl.load(in_ptr0 + (103 + (4*r1) + (200*x0)), rmask & xmask, eviction_policy='evict_last', other=0.0)
    tmp30 = tl.load(in_ptr2 + (4*x0), xmask, eviction_policy='evict_last')
    tmp32 = tl.load(in_ptr2 + (1 + (4*x0)), xmask, eviction_policy='evict_last')
    tmp35 = tl.load(in_ptr2 + (2 + (4*x0)), xmask, eviction_policy='evict_last')
    tmp38 = tl.load(in_ptr2 + (3 + (4*x0)), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = triton_helpers.minimum(tmp2, tmp5)
    tmp7 = 0.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp10 = tmp9 + tmp1
    tmp12 = tmp11 + tmp4
    tmp13 = triton_helpers.minimum(tmp10, tmp12)
    tmp14 = triton_helpers.maximum(tmp13, tmp7)
    tmp15 = tmp8 + tmp14
    tmp17 = tmp16 + tmp1
    tmp19 = tmp18 + tmp4
    tmp20 = triton_helpers.minimum(tmp17, tmp19)
    tmp21 = triton_helpers.maximum(tmp20, tmp7)
    tmp22 = tmp15 + tmp21
    tmp24 = tmp23 + tmp1
    tmp26 = tmp25 + tmp4
    tmp27 = triton_helpers.minimum(tmp24, tmp26)
    tmp28 = triton_helpers.maximum(tmp27, tmp7)
    tmp29 = tmp22 + tmp28
    tmp31 = tmp8 * tmp30
    tmp33 = tmp14 * tmp32
    tmp34 = tmp31 + tmp33
    tmp36 = tmp21 * tmp35
    tmp37 = tmp34 + tmp36
    tmp39 = tmp28 * tmp38
    tmp40 = tmp37 + tmp39
    tmp41 = tmp40.to(tl.float32)
    tmp42 = tl.broadcast_to(tmp41, [XBLOCK, RBLOCK])
    tmp43, = tl.associative_scan((tmp42,), 1, _triton_helper_fn_add0)
    tmp44 = tl.broadcast_to(tmp40, [XBLOCK, RBLOCK])
    tmp46 = tl.where(rmask & xmask, tmp44, 0)
    tmp47 = tl.sum(tmp46, 1)[:, None]
    tmp48 = tmp40 / tmp47
    tmp49 = tmp29.to(tl.float32)
    tmp50 = tl.broadcast_to(tmp49, [XBLOCK, RBLOCK])
    tmp51, = tl.associative_scan((tmp50,), 1, _triton_helper_fn_add0)
    tmp52 = 1e-16
    tmp53 = tmp51 + tmp52
    tmp54 = tmp43 / tmp53
    tmp55 = tmp54 * tmp48
    tmp56 = tl.broadcast_to(tmp55, [XBLOCK, RBLOCK])
    tmp58 = tl.where(rmask & xmask, tmp56, 0)
    tmp59 = tl.sum(tmp58, 1)[:, None]
    tl.store(out_ptr1 + (r1 + (25*x0)), tmp43, rmask & xmask)
    tl.store(out_ptr3 + (r1 + (25*x0)), tmp48, rmask & xmask)
    tl.store(in_out_ptr0 + (r1 + (25*x0)), tmp51, rmask & xmask)
    tl.store(out_ptr2 + (x0), tmp47, xmask)
    tl.store(out_ptr4 + (x0), tmp59, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/qj/cqjr4eiwboedemi6srskvhjfoufzmlpe3ceaikjhrkhpojkgwjcv.py
# Topologically Sorted Source Nodes: [min_1], Original ATen: [aten.minimum, aten.ge, aten.eq, aten.lt, aten.gt]
# Source node to ATen node mapping:
#   min_1 => minimum
# Graph fragment:
#   %minimum : [num_users=2] = call_function[target=torch.ops.aten.minimum.default](args = (%slice_2, %slice_4), kwargs = {})
#   %ge : [num_users=1] = call_function[target=torch.ops.aten.ge.Scalar](args = (%minimum, 0), kwargs = {})
#   %eq : [num_users=1] = call_function[target=torch.ops.aten.eq.Tensor](args = (%slice_2, %slice_4), kwargs = {})
#   %lt : [num_users=1] = call_function[target=torch.ops.aten.lt.Tensor](args = (%slice_2, %slice_4), kwargs = {})
#   %gt : [num_users=1] = call_function[target=torch.ops.aten.gt.Tensor](args = (%slice_2, %slice_4), kwargs = {})
triton_poi_fused_eq_ge_gt_lt_minimum_1 = async_compile.triton('triton_poi_fused_eq_ge_gt_lt_minimum_1', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

from torch._inductor.runtime import triton_helpers, triton_heuristics
from torch._inductor.runtime.triton_helpers import libdevice, math as tl_math
from torch._inductor.runtime.hints import AutotuneHint, ReductionHint, TileHint, instance_descriptor, DeviceProperties

@triton_heuristics.pointwise(
    size_hints=[512], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*i1', 3: '*i1', 4: '*i1', 5: '*i1', 6: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5, 6), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_eq_ge_gt_lt_minimum_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 0, 'backend_hash': 'A9C866B4A14FD3277824029365D703C2427B2E685E54EC9B3EF4ADC8D1EEAC1D', 'are_deterministic_algorithms_enabled': False, 'assert_indirect_indexing': True, 'autotune_local_cache': True, 'autotune_pointwise': True, 'autotune_remote_cache': None, 'force_disable_caches': False, 'dynamic_scale_rblock': True, 'max_autotune': False, 'max_autotune_pointwise': False, 'min_split_scan_rblock': 256, 'spill_threshold': 16, 'store_cubin': False},
    min_elem_per_thread=0
)
@triton.jit
def triton_poi_fused_eq_ge_gt_lt_minimum_1(in_ptr0, in_ptr1, out_ptr0, out_ptr1, out_ptr2, out_ptr3, xnumel, XBLOCK : tl.constexpr):
    xnumel = 400
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = (xindex // 100)
    x3 = xindex % 100
    x1 = (xindex // 4) % 25
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x3 + (200*x2)), xmask)
    tmp1 = tl.load(in_ptr1 + (x1), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (100 + x3 + (200*x2)), xmask)
    tmp4 = tl.load(in_ptr1 + (25 + x1), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = triton_helpers.minimum(tmp2, tmp5)
    tmp7 = 0.0
    tmp8 = tmp6 >= tmp7
    tmp9 = tmp2 == tmp5
    tmp10 = tmp2 < tmp5
    tmp11 = tmp2 > tmp5
    tl.store(out_ptr0 + (x4), tmp8, xmask)
    tl.store(out_ptr1 + (x4), tmp9, xmask)
    tl.store(out_ptr2 + (x4), tmp10, xmask)
    tl.store(out_ptr3 + (x4), tmp11, xmask)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (50, 1, 1), (1, 1, 1))
    assert_size_stride(primals_4, (50, ), (1, ))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        # Topologically Sorted Source Nodes: [q], Original ATen: [aten.convolution]
        buf0 = extern_kernels.convolution(reinterpret_tensor(primals_1, (4, 1, 4), (4, 4, 1), 0), primals_3, stride=(1,), padding=(0,), dilation=(1,), transposed=False, output_padding=(0,), groups=1, bias=None)
        assert_size_stride(buf0, (4, 50, 4), (200, 4, 1))
        buf1 = empty_strided_cuda((4, 25), (25, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 25), (25, 1), torch.float32)
        buf5 = empty_strided_cuda((4, ), (1, ), torch.float32)
        buf6 = empty_strided_cuda((4, 25), (25, 1), torch.float32)
        buf4 = buf1; del buf1  # reuse
        buf7 = empty_strided_cuda((4, ), (1, ), torch.float32)
        # Topologically Sorted Source Nodes: [min_1, q_1, nbs, mul, rec, cumsum, cumsum_1, add, prec, sum_3, rec_1, mul_1, ap], Original ATen: [aten.minimum, aten.clamp, aten.sum, aten.mul, aten.cumsum, aten.add, aten.div]
        stream0 = get_raw_stream(0)
        triton_per_fused_add_clamp_cumsum_div_minimum_mul_sum_0.run(buf4, buf0, primals_4, primals_2, buf3, buf5, buf6, buf7, 4, 25, grid=grid(4), stream=stream0)
        buf8 = empty_strided_cuda((4, 25, 4), (100, 4, 1), torch.bool)
        buf9 = empty_strided_cuda((4, 25, 4), (100, 4, 1), torch.bool)
        buf10 = empty_strided_cuda((4, 25, 4), (100, 4, 1), torch.bool)
        buf11 = empty_strided_cuda((4, 25, 4), (100, 4, 1), torch.bool)
        # Topologically Sorted Source Nodes: [min_1], Original ATen: [aten.minimum, aten.ge, aten.eq, aten.lt, aten.gt]
        triton_poi_fused_eq_ge_gt_lt_minimum_1.run(buf0, primals_4, buf8, buf9, buf10, buf11, 400, grid=grid(400), stream=stream0)
        del buf0
        del primals_4
    return (buf7, primals_2, primals_3, reinterpret_tensor(primals_1, (4, 1, 4), (4, 4, 1), 0), buf3, buf4, reinterpret_tensor(buf5, (4, 1), (1, 1), 0), buf6, buf8, buf9, buf10, buf11, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    primals_1 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_2 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    primals_3 = rand_strided((50, 1, 1), (1, 1, 1), device='cuda:0', dtype=torch.float32)
    primals_4 = rand_strided((50, ), (1, ), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([primals_1, primals_2, primals_3, primals_4])
    return print_performance(fn, times=times, repeat=repeat)


if __name__ == "__main__":
    from torch._inductor.wrapper_benchmark import compiled_module_main
    compiled_module_main('None', benchmark_compiled_module)

import torch
import numpy as np
import torch.optim
import torch.nn as nn
import torch.utils.data


class APLoss(nn.Module):
    """ differentiable AP loss, through quantization.

        Input: (N, M)   values in [min, max]
        label: (N, M)   values in {0, 1}

        Returns: list of query AP (for each n in {1..N})
                 Note: typically, you want to minimize 1 - mean(AP)
    """

    def __init__(self, nq=25, min=0, max=1, euc=False):
        nn.Module.__init__(self)
        assert isinstance(nq, int) and 2 <= nq <= 100
        self.nq = nq
        self.min = min
        self.max = max
        self.euc = euc
        gap = max - min
        assert gap > 0
        self.quantizer = q = nn.Conv1d(1, 2 * nq, kernel_size=1, bias=True)
        a = (nq - 1) / gap
        q.weight.data[:nq] = -a
        q.bias.data[:nq] = torch.from_numpy(a * min + np.arange(nq, 0, -1))
        q.weight.data[nq:] = a
        q.bias.data[nq:] = torch.from_numpy(np.arange(2 - nq, 2, 1) - a * min)
        q.weight.data[0] = q.weight.data[-1] = 0
        q.bias.data[0] = q.bias.data[-1] = 1

    def compute_AP(self, x, label):
        N, M = x.shape
        if self.euc:
            x = 1 - torch.sqrt(2.001 - 2 * x)
        q = self.quantizer(x.unsqueeze(1))
        q = torch.min(q[:, :self.nq], q[:, self.nq:]).clamp(min=0)
        nbs = q.sum(dim=-1)
        rec = (q * label.view(N, 1, M).float()).sum(dim=-1)
        prec = rec.cumsum(dim=-1) / (1e-16 + nbs.cumsum(dim=-1))
        rec /= rec.sum(dim=-1).unsqueeze(1)
        ap = (prec * rec).sum(dim=-1)
        return ap

    def forward(self, x, label):
        assert x.shape == label.shape
        return self.compute_AP(x, label)


def get_inputs():
    return [torch.rand([4, 4]), torch.rand([4, 4])]


def get_init_inputs():
    return [[], {}]

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
import numpy as np
import torch.optim
import torch.nn as nn
import torch.utils.data
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_helper_fn_add0(arg0_0, arg1_0):
    tmp0 = arg0_0 + arg1_0
    return tmp0


@triton.jit
def triton_per_fused_add_clamp_cumsum_div_minimum_mul_sum_0(in_out_ptr0,
    in_ptr0, in_ptr1, in_ptr2, out_ptr1, out_ptr2, out_ptr3, out_ptr4,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    rnumel = 25
    RBLOCK: tl.constexpr = 32
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = xindex < xnumel
    rindex = tl.arange(0, RBLOCK)[None, :]
    rmask = rindex < rnumel
    r1 = rindex
    x0 = xindex
    tmp0 = tl.load(in_ptr0 + (4 * r1 + 200 * x0), rmask & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp1 = tl.load(in_ptr1 + r1, rmask, eviction_policy='evict_last', other=0.0
        )
    tmp3 = tl.load(in_ptr0 + (100 + 4 * r1 + 200 * x0), rmask & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp4 = tl.load(in_ptr1 + (25 + r1), rmask, eviction_policy='evict_last',
        other=0.0)
    tmp9 = tl.load(in_ptr0 + (1 + 4 * r1 + 200 * x0), rmask & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp11 = tl.load(in_ptr0 + (101 + 4 * r1 + 200 * x0), rmask & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp16 = tl.load(in_ptr0 + (2 + 4 * r1 + 200 * x0), rmask & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp18 = tl.load(in_ptr0 + (102 + 4 * r1 + 200 * x0), rmask & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp23 = tl.load(in_ptr0 + (3 + 4 * r1 + 200 * x0), rmask & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp25 = tl.load(in_ptr0 + (103 + 4 * r1 + 200 * x0), rmask & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp30 = tl.load(in_ptr2 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp32 = tl.load(in_ptr2 + (1 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp35 = tl.load(in_ptr2 + (2 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp38 = tl.load(in_ptr2 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = triton_helpers.minimum(tmp2, tmp5)
    tmp7 = 0.0
    tmp8 = triton_helpers.maximum(tmp6, tmp7)
    tmp10 = tmp9 + tmp1
    tmp12 = tmp11 + tmp4
    tmp13 = triton_helpers.minimum(tmp10, tmp12)
    tmp14 = triton_helpers.maximum(tmp13, tmp7)
    tmp15 = tmp8 + tmp14
    tmp17 = tmp16 + tmp1
    tmp19 = tmp18 + tmp4
    tmp20 = triton_helpers.minimum(tmp17, tmp19)
    tmp21 = triton_helpers.maximum(tmp20, tmp7)
    tmp22 = tmp15 + tmp21
    tmp24 = tmp23 + tmp1
    tmp26 = tmp25 + tmp4
    tmp27 = triton_helpers.minimum(tmp24, tmp26)
    tmp28 = triton_helpers.maximum(tmp27, tmp7)
    tmp29 = tmp22 + tmp28
    tmp31 = tmp8 * tmp30
    tmp33 = tmp14 * tmp32
    tmp34 = tmp31 + tmp33
    tmp36 = tmp21 * tmp35
    tmp37 = tmp34 + tmp36
    tmp39 = tmp28 * tmp38
    tmp40 = tmp37 + tmp39
    tmp41 = tmp40.to(tl.float32)
    tmp42 = tl.broadcast_to(tmp41, [XBLOCK, RBLOCK])
    tmp43, = tl.associative_scan((tmp42,), 1, _triton_helper_fn_add0)
    tmp44 = tl.broadcast_to(tmp40, [XBLOCK, RBLOCK])
    tmp46 = tl.where(rmask & xmask, tmp44, 0)
    tmp47 = tl.sum(tmp46, 1)[:, None]
    tmp48 = tmp40 / tmp47
    tmp49 = tmp29.to(tl.float32)
    tmp50 = tl.broadcast_to(tmp49, [XBLOCK, RBLOCK])
    tmp51, = tl.associative_scan((tmp50,), 1, _triton_helper_fn_add0)
    tmp52 = 1e-16
    tmp53 = tmp51 + tmp52
    tmp54 = tmp43 / tmp53
    tmp55 = tmp54 * tmp48
    tmp56 = tl.broadcast_to(tmp55, [XBLOCK, RBLOCK])
    tmp58 = tl.where(rmask & xmask, tmp56, 0)
    tmp59 = tl.sum(tmp58, 1)[:, None]
    tl.store(out_ptr1 + (r1 + 25 * x0), tmp43, rmask & xmask)
    tl.store(out_ptr3 + (r1 + 25 * x0), tmp48, rmask & xmask)
    tl.store(in_out_ptr0 + (r1 + 25 * x0), tmp51, rmask & xmask)
    tl.store(out_ptr2 + x0, tmp47, xmask)
    tl.store(out_ptr4 + x0, tmp59, xmask)


@triton.jit
def triton_poi_fused_eq_ge_gt_lt_minimum_1(in_ptr0, in_ptr1, out_ptr0,
    out_ptr1, out_ptr2, out_ptr3, xnumel, XBLOCK: tl.constexpr):
    xnumel = 400
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x2 = xindex // 100
    x3 = xindex % 100
    x1 = xindex // 4 % 25
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x3 + 200 * x2), xmask)
    tmp1 = tl.load(in_ptr1 + x1, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (100 + x3 + 200 * x2), xmask)
    tmp4 = tl.load(in_ptr1 + (25 + x1), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 + tmp1
    tmp5 = tmp3 + tmp4
    tmp6 = triton_helpers.minimum(tmp2, tmp5)
    tmp7 = 0.0
    tmp8 = tmp6 >= tmp7
    tmp9 = tmp2 == tmp5
    tmp10 = tmp2 < tmp5
    tmp11 = tmp2 > tmp5
    tl.store(out_ptr0 + x4, tmp8, xmask)
    tl.store(out_ptr1 + x4, tmp9, xmask)
    tl.store(out_ptr2 + x4, tmp10, xmask)
    tl.store(out_ptr3 + x4, tmp11, xmask)


def call(args):
    primals_1, primals_2, primals_3, primals_4 = args
    args.clear()
    assert_size_stride(primals_1, (4, 4), (4, 1))
    assert_size_stride(primals_2, (4, 4), (4, 1))
    assert_size_stride(primals_3, (50, 1, 1), (1, 1, 1))
    assert_size_stride(primals_4, (50,), (1,))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = extern_kernels.convolution(reinterpret_tensor(primals_1, (4,
            1, 4), (4, 4, 1), 0), primals_3, stride=(1,), padding=(0,),
            dilation=(1,), transposed=False, output_padding=(0,), groups=1,
            bias=None)
        assert_size_stride(buf0, (4, 50, 4), (200, 4, 1))
        buf1 = empty_strided_cuda((4, 25), (25, 1), torch.float32)
        buf3 = empty_strided_cuda((4, 25), (25, 1), torch.float32)
        buf5 = empty_strided_cuda((4,), (1,), torch.float32)
        buf6 = empty_strided_cuda((4, 25), (25, 1), torch.float32)
        buf4 = buf1
        del buf1
        buf7 = empty_strided_cuda((4,), (1,), torch.float32)
        get_raw_stream(0)
        triton_per_fused_add_clamp_cumsum_div_minimum_mul_sum_0[grid(4)](buf4,
            buf0, primals_4, primals_2, buf3, buf5, buf6, buf7, 4, 25,
            XBLOCK=1, num_warps=2, num_stages=1)
        buf8 = empty_strided_cuda((4, 25, 4), (100, 4, 1), torch.bool)
        buf9 = empty_strided_cuda((4, 25, 4), (100, 4, 1), torch.bool)
        buf10 = empty_strided_cuda((4, 25, 4), (100, 4, 1), torch.bool)
        buf11 = empty_strided_cuda((4, 25, 4), (100, 4, 1), torch.bool)
        triton_poi_fused_eq_ge_gt_lt_minimum_1[grid(400)](buf0, primals_4,
            buf8, buf9, buf10, buf11, 400, XBLOCK=128, num_warps=4,
            num_stages=1)
        del buf0
        del primals_4
    return buf7, primals_2, primals_3, reinterpret_tensor(primals_1, (4, 1,
        4), (4, 4, 1), 0), buf3, buf4, reinterpret_tensor(buf5, (4, 1), (1,
        1), 0), buf6, buf8, buf9, buf10, buf11


class APLossNew(nn.Module):
    """ differentiable AP loss, through quantization.

        Input: (N, M)   values in [min, max]
        label: (N, M)   values in {0, 1}

        Returns: list of query AP (for each n in {1..N})
                 Note: typically, you want to minimize 1 - mean(AP)
    """

    def __init__(self, nq=25, min=0, max=1, euc=False):
        nn.Module.__init__(self)
        assert isinstance(nq, int) and 2 <= nq <= 100
        self.nq = nq
        self.min = min
        self.max = max
        self.euc = euc
        gap = max - min
        assert gap > 0
        self.quantizer = q = nn.Conv1d(1, 2 * nq, kernel_size=1, bias=True)
        a = (nq - 1) / gap
        q.weight.data[:nq] = -a
        q.bias.data[:nq] = torch.from_numpy(a * min + np.arange(nq, 0, -1))
        q.weight.data[nq:] = a
        q.bias.data[nq:] = torch.from_numpy(np.arange(2 - nq, 2, 1) - a * min)
        q.weight.data[0] = q.weight.data[-1] = 0
        q.bias.data[0] = q.bias.data[-1] = 1

    def compute_AP(self, x, label):
        N, M = x.shape
        if self.euc:
            x = 1 - torch.sqrt(2.001 - 2 * x)
        q = self.quantizer(x.unsqueeze(1))
        q = torch.min(q[:, :self.nq], q[:, self.nq:]).clamp(min=0)
        nbs = q.sum(dim=-1)
        rec = (q * label.view(N, 1, M).float()).sum(dim=-1)
        prec = rec.cumsum(dim=-1) / (1e-16 + nbs.cumsum(dim=-1))
        rec /= rec.sum(dim=-1).unsqueeze(1)
        ap = (prec * rec).sum(dim=-1)
        return ap

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