# AOT ID: ['0_inference']
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_6/inductor_cache/yi/cyiskpoo6jil2no3yp6e4cjwicinrfr5gafwmueah6or26aovbuw.py
# Topologically Sorted Source Nodes: [label_matrix], Original ATen: [aten.eq]
# Source node to ATen node mapping:
#   label_matrix => eq
# Graph fragment:
#   %eq : [num_users=2] = call_function[target=torch.ops.aten.eq.Tensor](args = (%unsqueeze, %unsqueeze_1), kwargs = {})
#   %clone_1 : [num_users=3] = call_function[target=torch.ops.aten.clone](args = (%eq,), kwargs = {})
triton_poi_fused_eq_0 = async_compile.triton('triton_poi_fused_eq_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_heuristics.pointwise(
    size_hints=[1024], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*i1', 2: '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), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_eq_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 2, '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_0(in_ptr0, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 64
    x2 = (xindex // 256)
    x3 = xindex % 256
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + (64*x2)), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr0 + (x3), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 == tmp1
    tl.store(out_ptr0 + (x4), tmp2, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/i4/ci457pnjg4fsfqqsyoibupqa7vgipgqjf6zs5wlpi5n37vmwyohl.py
# Topologically Sorted Source Nodes: [positive_matrix], Original ATen: [aten.fill]
# Source node to ATen node mapping:
#   positive_matrix => full_default
# Graph fragment:
#   %full_default : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([4], False), kwargs = {dtype: torch.bool, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %copy__default : [num_users=0] = call_function[target=torch.ops.aten.copy_.default](args = (%as_strided_default, %full_default), kwargs = {})
triton_poi_fused_fill_1 = async_compile.triton('triton_poi_fused_fill_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=[4], 
    filename=__file__,
    triton_meta={'signature': {0: '*i1', 1: '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,), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_fill_1', 'mutated_arg_names': ['out_ptr0'], 'no_x_dim': False, 'num_load': 0, '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_fill_1(out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.full([1], False, tl.int1)
    tl.store(out_ptr0 + (341*x0), tmp0, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/ap/cap7om7td64y73m6tyohnbyrznztgqfiwzeh2apiixcoopoajv36.py
# Topologically Sorted Source Nodes: [similarity_matrix], Original ATen: [aten.clone]
# Source node to ATen node mapping:
#   similarity_matrix => clone
# Graph fragment:
#   %clone : [num_users=1] = call_function[target=torch.ops.aten.clone.default](args = (%expand_1,), kwargs = {memory_format: torch.contiguous_format})
triton_poi_fused_clone_2 = async_compile.triton('triton_poi_fused_clone_2', '''
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=[4, 64], tile_hint=TileHint.SQUARE,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: 'i32', 3: '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, 3), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_clone_2', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 1, '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_clone_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK : tl.constexpr, XBLOCK : tl.constexpr):
    ynumel = 4
    xnumel = 64
    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]
    xmask = xindex < xnumel
    x1 = xindex % 4
    x2 = (xindex // 4) % 4
    x3 = (xindex // 16)
    y0 = yindex
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (y0 + (4*x3) + (16*x2) + (64*x1)), xmask & ymask, eviction_policy='evict_last')
    tl.store(out_ptr0 + (x4 + (64*y0)), tmp0, xmask & ymask)
''', device_str='cuda')


# kernel path: runs/run_shard_6/inductor_cache/ef/cefelxoht5rwfpxl5fyrxh2de67rktuusfkdiwwoj332uk4b3oqb.py
# Topologically Sorted Source Nodes: [label_matrix, zeros_like, sp, sub, ap, mul, sub_1, logit_p, zeros_like_2, logit_p_1, logsumexp, negative_matrix, zeros_like_1, sn, add, an, mul_2, sub_2, logit_n, zeros_like_3, logit_n_1, logsumexp_1, add_1, softplus, loss], Original ATen: [aten.eq, aten.zeros_like, aten.where, aten.rsub, aten.clamp_min, aten.mul, aten.sub, aten.logsumexp, aten.logical_not, aten.add, aten.softplus, aten.mean]
# Source node to ATen node mapping:
#   add => add
#   add_1 => add_3
#   an => clamp_min_1
#   ap => clamp_min
#   label_matrix => eq
#   logit_n => mul_3
#   logit_n_1 => where_3
#   logit_p => mul_1
#   logit_p_1 => where_2
#   logsumexp => abs_1, add_1, amax, eq_1, exp, full_default_5, log, sub_3, sum_1, where_4
#   logsumexp_1 => abs_2, add_2, amax_1, eq_2, exp_1, full_default_6, log_1, sub_4, sum_2, where_5
#   loss => mean
#   mul => mul
#   mul_2 => mul_2
#   negative_matrix => logical_not
#   sn => where_1
#   softplus => exp_2, gt, log1p, where_6
#   sp => where
#   sub => sub
#   sub_1 => sub_1
#   sub_2 => sub_2
#   zeros_like => full_default_1
#   zeros_like_1 => full_default_2
#   zeros_like_2 => full_default_3
#   zeros_like_3 => full_default_4
# Graph fragment:
#   %eq : [num_users=2] = call_function[target=torch.ops.aten.eq.Tensor](args = (%unsqueeze, %unsqueeze_1), kwargs = {})
#   %full_default_1 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([4, 4, 4, 4], 0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %where : [num_users=2] = call_function[target=torch.ops.aten.where.self](args = (%clone_1, %view_2, %full_default_1), kwargs = {})
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (5, %where), kwargs = {})
#   %clamp_min : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%sub, 0.0), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clamp_min, -4), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%where, -3), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul, %sub_1), kwargs = {})
#   %full_default_3 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([4, 4, 4, 4, 4], 0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %where_2 : [num_users=2] = call_function[target=torch.ops.aten.where.self](args = (%clone_1, %mul_1, %full_default_3), kwargs = {})
#   %amax : [num_users=2] = call_function[target=torch.ops.aten.amax.default](args = (%where_2, [1], True), kwargs = {})
#   %abs_1 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%amax,), kwargs = {})
#   %eq_1 : [num_users=1] = call_function[target=torch.ops.aten.eq.Scalar](args = (%abs_1, inf), kwargs = {})
#   %full_default_5 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([], 0.0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %where_4 : [num_users=2] = call_function[target=torch.ops.aten.where.self](args = (%eq_1, %full_default_5, %amax), kwargs = {})
#   %sub_3 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%where_2, %where_4), kwargs = {})
#   %exp : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%sub_3,), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp, [1]), kwargs = {})
#   %log : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%sum_1,), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%log, %squeeze), kwargs = {})
#   %logical_not : [num_users=2] = call_function[target=torch.ops.aten.logical_not.default](args = (%eq,), kwargs = {})
#   %full_default_2 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([4, 4, 4, 4], 0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %where_1 : [num_users=2] = call_function[target=torch.ops.aten.where.self](args = (%logical_not, %view_2, %full_default_2), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%where_1, 4), kwargs = {})
#   %clamp_min_1 : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%add, 0.0), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%clamp_min_1, 4), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%where_1, 4), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%mul_2, %sub_2), kwargs = {})
#   %full_default_4 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([4, 4, 4, 4, 4], 0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %where_3 : [num_users=2] = call_function[target=torch.ops.aten.where.self](args = (%logical_not, %mul_3, %full_default_4), kwargs = {})
#   %amax_1 : [num_users=2] = call_function[target=torch.ops.aten.amax.default](args = (%where_3, [1], True), kwargs = {})
#   %abs_2 : [num_users=1] = call_function[target=torch.ops.aten.abs.default](args = (%amax_1,), kwargs = {})
#   %eq_2 : [num_users=1] = call_function[target=torch.ops.aten.eq.Scalar](args = (%abs_2, inf), kwargs = {})
#   %full_default_6 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([], 0.0), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %where_5 : [num_users=2] = call_function[target=torch.ops.aten.where.self](args = (%eq_2, %full_default_6, %amax_1), kwargs = {})
#   %sub_4 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%where_3, %where_5), kwargs = {})
#   %exp_1 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%sub_4,), kwargs = {})
#   %sum_2 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp_1, [1]), kwargs = {})
#   %log_1 : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%sum_2,), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%log_1, %squeeze_1), kwargs = {})
#   %add_3 : [num_users=3] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_1, %add_2), kwargs = {})
#   %gt : [num_users=1] = call_function[target=torch.ops.aten.gt.Scalar](args = (%add_3, 20), kwargs = {})
#   %exp_2 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%add_3,), kwargs = {})
#   %log1p : [num_users=1] = call_function[target=torch.ops.aten.log1p.default](args = (%exp_2,), kwargs = {})
#   %where_6 : [num_users=1] = call_function[target=torch.ops.aten.where.self](args = (%gt, %add_3, %log1p), kwargs = {})
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%where_6,), kwargs = {})
triton_per_fused_add_clamp_min_eq_logical_not_logsumexp_mean_mul_rsub_softplus_sub_where_zeros_like_3 = async_compile.triton('triton_per_fused_add_clamp_min_eq_logical_not_logsumexp_mean_mul_rsub_softplus_sub_where_zeros_like_3', '''
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.persistent_reduction(
    size_hints=[1, 256],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*i1', 2: '*fp32', 3: '*fp32', 4: 'i32', 5: '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': {4: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 5), equal_to_1=(4,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_add_clamp_min_eq_logical_not_logsumexp_mean_mul_rsub_softplus_sub_where_zeros_like_3', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': True, 'num_load': 13, 'num_reduction': 1, '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_min_eq_logical_not_logsumexp_mean_mul_rsub_softplus_sub_where_zeros_like_3(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, rnumel):
    xnumel = 1
    XBLOCK: tl.constexpr = 1
    rnumel = 256
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    xmask = tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    roffset = 0
    rmask = tl.full([RBLOCK], True, tl.int1)
    r0 = rindex % 64
    r1 = (rindex // 64)
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + (r0 + (256*r1)), None).to(tl.int1)
    tmp1 = tl.load(in_ptr1 + (r0), None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (64 + r0 + (256*r1)), None).to(tl.int1)
    tmp14 = tl.load(in_ptr1 + (64 + r0), None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (128 + r0 + (256*r1)), None).to(tl.int1)
    tmp24 = tl.load(in_ptr1 + (128 + r0), None, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr0 + (192 + r0 + (256*r1)), None).to(tl.int1)
    tmp34 = tl.load(in_ptr1 + (192 + r0), None, eviction_policy='evict_last')
    tmp58 = tl.load(in_ptr2 + (r2), None)
    tmp59 = tl.load(in_ptr2 + (r0), None, eviction_policy='evict_last')
    tmp70 = tl.load(in_ptr2 + (64 + r0), None, eviction_policy='evict_last')
    tmp81 = tl.load(in_ptr2 + (128 + r0), None, eviction_policy='evict_last')
    tmp92 = tl.load(in_ptr2 + (192 + r0), None, eviction_policy='evict_last')
    tmp2 = 0.0
    tmp3 = tl.where(tmp0, tmp1, tmp2)
    tmp4 = 5.0
    tmp5 = tmp4 - tmp3
    tmp6 = triton_helpers.maximum(tmp5, tmp2)
    tmp7 = -4.0
    tmp8 = tmp6 * tmp7
    tmp9 = -3.0
    tmp10 = tmp3 - tmp9
    tmp11 = tmp8 * tmp10
    tmp12 = tl.where(tmp0, tmp11, tmp2)
    tmp15 = tl.where(tmp13, tmp14, tmp2)
    tmp16 = tmp4 - tmp15
    tmp17 = triton_helpers.maximum(tmp16, tmp2)
    tmp18 = tmp17 * tmp7
    tmp19 = tmp15 - tmp9
    tmp20 = tmp18 * tmp19
    tmp21 = tl.where(tmp13, tmp20, tmp2)
    tmp22 = triton_helpers.maximum(tmp12, tmp21)
    tmp25 = tl.where(tmp23, tmp24, tmp2)
    tmp26 = tmp4 - tmp25
    tmp27 = triton_helpers.maximum(tmp26, tmp2)
    tmp28 = tmp27 * tmp7
    tmp29 = tmp25 - tmp9
    tmp30 = tmp28 * tmp29
    tmp31 = tl.where(tmp23, tmp30, tmp2)
    tmp32 = triton_helpers.maximum(tmp22, tmp31)
    tmp35 = tl.where(tmp33, tmp34, tmp2)
    tmp36 = tmp4 - tmp35
    tmp37 = triton_helpers.maximum(tmp36, tmp2)
    tmp38 = tmp37 * tmp7
    tmp39 = tmp35 - tmp9
    tmp40 = tmp38 * tmp39
    tmp41 = tl.where(tmp33, tmp40, tmp2)
    tmp42 = triton_helpers.maximum(tmp32, tmp41)
    tmp43 = tl_math.abs(tmp42)
    tmp44 = float("inf")
    tmp45 = tmp43 == tmp44
    tmp46 = tl.where(tmp45, tmp2, tmp42)
    tmp47 = tmp12 - tmp46
    tmp48 = tl_math.exp(tmp47)
    tmp49 = tmp21 - tmp46
    tmp50 = tl_math.exp(tmp49)
    tmp51 = tmp48 + tmp50
    tmp52 = tmp31 - tmp46
    tmp53 = tl_math.exp(tmp52)
    tmp54 = tmp51 + tmp53
    tmp55 = tmp41 - tmp46
    tmp56 = tl_math.exp(tmp55)
    tmp57 = tmp54 + tmp56
    tmp60 = tmp58 == tmp59
    tmp61 = tmp60 == 0
    tmp62 = tl.where(tmp61, tmp1, tmp2)
    tmp63 = 4.0
    tmp64 = tmp62 + tmp63
    tmp65 = triton_helpers.maximum(tmp64, tmp2)
    tmp66 = tmp65 * tmp63
    tmp67 = tmp62 - tmp63
    tmp68 = tmp66 * tmp67
    tmp69 = tl.where(tmp61, tmp68, tmp2)
    tmp71 = tmp58 == tmp70
    tmp72 = tmp71 == 0
    tmp73 = tl.where(tmp72, tmp14, tmp2)
    tmp74 = tmp73 + tmp63
    tmp75 = triton_helpers.maximum(tmp74, tmp2)
    tmp76 = tmp75 * tmp63
    tmp77 = tmp73 - tmp63
    tmp78 = tmp76 * tmp77
    tmp79 = tl.where(tmp72, tmp78, tmp2)
    tmp80 = triton_helpers.maximum(tmp69, tmp79)
    tmp82 = tmp58 == tmp81
    tmp83 = tmp82 == 0
    tmp84 = tl.where(tmp83, tmp24, tmp2)
    tmp85 = tmp84 + tmp63
    tmp86 = triton_helpers.maximum(tmp85, tmp2)
    tmp87 = tmp86 * tmp63
    tmp88 = tmp84 - tmp63
    tmp89 = tmp87 * tmp88
    tmp90 = tl.where(tmp83, tmp89, tmp2)
    tmp91 = triton_helpers.maximum(tmp80, tmp90)
    tmp93 = tmp58 == tmp92
    tmp94 = tmp93 == 0
    tmp95 = tl.where(tmp94, tmp34, tmp2)
    tmp96 = tmp95 + tmp63
    tmp97 = triton_helpers.maximum(tmp96, tmp2)
    tmp98 = tmp97 * tmp63
    tmp99 = tmp95 - tmp63
    tmp100 = tmp98 * tmp99
    tmp101 = tl.where(tmp94, tmp100, tmp2)
    tmp102 = triton_helpers.maximum(tmp91, tmp101)
    tmp103 = tl_math.abs(tmp102)
    tmp104 = tmp103 == tmp44
    tmp105 = tl.where(tmp104, tmp2, tmp102)
    tmp106 = tmp69 - tmp105
    tmp107 = tl_math.exp(tmp106)
    tmp108 = tmp79 - tmp105
    tmp109 = tl_math.exp(tmp108)
    tmp110 = tmp107 + tmp109
    tmp111 = tmp90 - tmp105
    tmp112 = tl_math.exp(tmp111)
    tmp113 = tmp110 + tmp112
    tmp114 = tmp101 - tmp105
    tmp115 = tl_math.exp(tmp114)
    tmp116 = tmp113 + tmp115
    tmp117 = tl_math.log(tmp57)
    tmp118 = tmp117 + tmp46
    tmp119 = tl_math.log(tmp116)
    tmp120 = tmp119 + tmp105
    tmp121 = tmp118 + tmp120
    tmp122 = 20.0
    tmp123 = tmp121 > tmp122
    tmp124 = tl_math.exp(tmp121)
    tmp125 = libdevice.log1p(tmp124)
    tmp126 = tl.where(tmp123, tmp121, tmp125)
    tmp127 = tl.broadcast_to(tmp126, [RBLOCK])
    tmp129 = triton_helpers.promote_to_tensor(tl.sum(tmp127, 0))
    tmp130 = 256.0
    tmp131 = tmp129 / tmp130
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([1], 0, tl.int32)), tmp131, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1), torch.bool)
        # Topologically Sorted Source Nodes: [label_matrix], Original ATen: [aten.eq]
        stream0 = get_raw_stream(0)
        triton_poi_fused_eq_0.run(arg1_1, buf0, 1024, grid=grid(1024), stream=stream0)
        # Topologically Sorted Source Nodes: [positive_matrix], Original ATen: [aten.fill]
        triton_poi_fused_fill_1.run(buf0, 4, grid=grid(4), stream=stream0)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [similarity_matrix], Original ATen: [aten.clone]
        triton_poi_fused_clone_2.run(arg0_1, buf2, 4, 64, grid=grid(4, 64), stream=stream0)
        buf3 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [similarity_matrix], Original ATen: [aten.bmm]
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (16, 4, 4), (16, 4, 1), 0), reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0), out=buf3)
        del arg0_1
        del buf2
        buf8 = empty_strided_cuda((), (), torch.float32)
        buf9 = buf8; del buf8  # reuse
        # Topologically Sorted Source Nodes: [label_matrix, zeros_like, sp, sub, ap, mul, sub_1, logit_p, zeros_like_2, logit_p_1, logsumexp, negative_matrix, zeros_like_1, sn, add, an, mul_2, sub_2, logit_n, zeros_like_3, logit_n_1, logsumexp_1, add_1, softplus, loss], Original ATen: [aten.eq, aten.zeros_like, aten.where, aten.rsub, aten.clamp_min, aten.mul, aten.sub, aten.logsumexp, aten.logical_not, aten.add, aten.softplus, aten.mean]
        triton_per_fused_add_clamp_min_eq_logical_not_logsumexp_mean_mul_rsub_softplus_sub_where_zeros_like_3.run(buf9, buf0, buf3, arg1_1, 1, 256, grid=grid(1), stream=stream0)
        del arg1_1
        del buf0
        del buf3
    return (buf9, )


def benchmark_compiled_module(times=10, repeat=10):
    from torch._dynamo.testing import rand_strided
    from torch._inductor.utils import print_performance
    arg0_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg1_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1])
    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
from typing import *
from torch import nn
import torch.nn.functional as F
from torch import functional as F
from torch.nn import functional as F


class CircleLoss(nn.Module):
    """CircleLoss from
	`"Circle Loss: A Unified Perspective of Pair Similarity Optimization"
	<https://arxiv.org/pdf/2002.10857>`_ paper.

	Parameters
	----------
	m: float.
		Margin parameter for loss.
	gamma: int.
		Scale parameter for loss.

	Outputs:
		- **loss**: scalar.
	"""

    def __init__(self, m, gamma):
        super(CircleLoss, self).__init__()
        self.m = m
        self.gamma = gamma
        self.dp = 1 - m
        self.dn = m

    def forward(self, x, target):
        similarity_matrix = x @ x.T
        label_matrix = target.unsqueeze(1) == target.unsqueeze(0)
        negative_matrix = label_matrix.logical_not()
        positive_matrix = label_matrix.fill_diagonal_(False)
        sp = torch.where(positive_matrix, similarity_matrix, torch.
            zeros_like(similarity_matrix))
        sn = torch.where(negative_matrix, similarity_matrix, torch.
            zeros_like(similarity_matrix))
        ap = torch.clamp_min(1 + self.m - sp.detach(), min=0.0)
        an = torch.clamp_min(sn.detach() + self.m, min=0.0)
        logit_p = -self.gamma * ap * (sp - self.dp)
        logit_n = self.gamma * an * (sn - self.dn)
        logit_p = torch.where(positive_matrix, logit_p, torch.zeros_like(
            logit_p))
        logit_n = torch.where(negative_matrix, logit_n, torch.zeros_like(
            logit_n))
        loss = F.softplus(torch.logsumexp(logit_p, dim=1) + torch.logsumexp
            (logit_n, dim=1)).mean()
        return loss


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


def get_init_inputs():
    return [[], {'m': 4, 'gamma': 4}]

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
from typing import *
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_poi_fused_eq_0(in_ptr0, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 64
    x2 = xindex // 256
    x3 = xindex % 256
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 64 * x2), xmask, eviction_policy=
        'evict_last')
    tmp1 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 == tmp1
    tl.store(out_ptr0 + x4, tmp2, xmask)


@triton.jit
def triton_poi_fused_fill_1(out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 4
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex
    tmp0 = tl.full([1], False, tl.int1)
    tl.store(out_ptr0 + 341 * x0, tmp0, xmask)


@triton.jit
def triton_poi_fused_clone_2(in_ptr0, out_ptr0, ynumel, xnumel, YBLOCK: tl.
    constexpr, XBLOCK: tl.constexpr):
    ynumel = 4
    xnumel = 64
    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]
    xmask = xindex < xnumel
    x1 = xindex % 4
    x2 = xindex // 4 % 4
    x3 = xindex // 16
    y0 = yindex
    x4 = xindex
    tmp0 = tl.load(in_ptr0 + (y0 + 4 * x3 + 16 * x2 + 64 * x1), xmask &
        ymask, eviction_policy='evict_last')
    tl.store(out_ptr0 + (x4 + 64 * y0), tmp0, xmask & ymask)


@triton.jit
def triton_per_fused_add_clamp_min_eq_logical_not_logsumexp_mean_mul_rsub_softplus_sub_where_zeros_like_3(
    in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 256
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex % 64
    r1 = rindex // 64
    r2 = rindex
    tmp0 = tl.load(in_ptr0 + (r0 + 256 * r1), None).to(tl.int1)
    tmp1 = tl.load(in_ptr1 + r0, None, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (64 + r0 + 256 * r1), None).to(tl.int1)
    tmp14 = tl.load(in_ptr1 + (64 + r0), None, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (128 + r0 + 256 * r1), None).to(tl.int1)
    tmp24 = tl.load(in_ptr1 + (128 + r0), None, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr0 + (192 + r0 + 256 * r1), None).to(tl.int1)
    tmp34 = tl.load(in_ptr1 + (192 + r0), None, eviction_policy='evict_last')
    tmp58 = tl.load(in_ptr2 + r2, None)
    tmp59 = tl.load(in_ptr2 + r0, None, eviction_policy='evict_last')
    tmp70 = tl.load(in_ptr2 + (64 + r0), None, eviction_policy='evict_last')
    tmp81 = tl.load(in_ptr2 + (128 + r0), None, eviction_policy='evict_last')
    tmp92 = tl.load(in_ptr2 + (192 + r0), None, eviction_policy='evict_last')
    tmp2 = 0.0
    tmp3 = tl.where(tmp0, tmp1, tmp2)
    tmp4 = 5.0
    tmp5 = tmp4 - tmp3
    tmp6 = triton_helpers.maximum(tmp5, tmp2)
    tmp7 = -4.0
    tmp8 = tmp6 * tmp7
    tmp9 = -3.0
    tmp10 = tmp3 - tmp9
    tmp11 = tmp8 * tmp10
    tmp12 = tl.where(tmp0, tmp11, tmp2)
    tmp15 = tl.where(tmp13, tmp14, tmp2)
    tmp16 = tmp4 - tmp15
    tmp17 = triton_helpers.maximum(tmp16, tmp2)
    tmp18 = tmp17 * tmp7
    tmp19 = tmp15 - tmp9
    tmp20 = tmp18 * tmp19
    tmp21 = tl.where(tmp13, tmp20, tmp2)
    tmp22 = triton_helpers.maximum(tmp12, tmp21)
    tmp25 = tl.where(tmp23, tmp24, tmp2)
    tmp26 = tmp4 - tmp25
    tmp27 = triton_helpers.maximum(tmp26, tmp2)
    tmp28 = tmp27 * tmp7
    tmp29 = tmp25 - tmp9
    tmp30 = tmp28 * tmp29
    tmp31 = tl.where(tmp23, tmp30, tmp2)
    tmp32 = triton_helpers.maximum(tmp22, tmp31)
    tmp35 = tl.where(tmp33, tmp34, tmp2)
    tmp36 = tmp4 - tmp35
    tmp37 = triton_helpers.maximum(tmp36, tmp2)
    tmp38 = tmp37 * tmp7
    tmp39 = tmp35 - tmp9
    tmp40 = tmp38 * tmp39
    tmp41 = tl.where(tmp33, tmp40, tmp2)
    tmp42 = triton_helpers.maximum(tmp32, tmp41)
    tmp43 = tl_math.abs(tmp42)
    tmp44 = float('inf')
    tmp45 = tmp43 == tmp44
    tmp46 = tl.where(tmp45, tmp2, tmp42)
    tmp47 = tmp12 - tmp46
    tmp48 = tl_math.exp(tmp47)
    tmp49 = tmp21 - tmp46
    tmp50 = tl_math.exp(tmp49)
    tmp51 = tmp48 + tmp50
    tmp52 = tmp31 - tmp46
    tmp53 = tl_math.exp(tmp52)
    tmp54 = tmp51 + tmp53
    tmp55 = tmp41 - tmp46
    tmp56 = tl_math.exp(tmp55)
    tmp57 = tmp54 + tmp56
    tmp60 = tmp58 == tmp59
    tmp61 = tmp60 == 0
    tmp62 = tl.where(tmp61, tmp1, tmp2)
    tmp63 = 4.0
    tmp64 = tmp62 + tmp63
    tmp65 = triton_helpers.maximum(tmp64, tmp2)
    tmp66 = tmp65 * tmp63
    tmp67 = tmp62 - tmp63
    tmp68 = tmp66 * tmp67
    tmp69 = tl.where(tmp61, tmp68, tmp2)
    tmp71 = tmp58 == tmp70
    tmp72 = tmp71 == 0
    tmp73 = tl.where(tmp72, tmp14, tmp2)
    tmp74 = tmp73 + tmp63
    tmp75 = triton_helpers.maximum(tmp74, tmp2)
    tmp76 = tmp75 * tmp63
    tmp77 = tmp73 - tmp63
    tmp78 = tmp76 * tmp77
    tmp79 = tl.where(tmp72, tmp78, tmp2)
    tmp80 = triton_helpers.maximum(tmp69, tmp79)
    tmp82 = tmp58 == tmp81
    tmp83 = tmp82 == 0
    tmp84 = tl.where(tmp83, tmp24, tmp2)
    tmp85 = tmp84 + tmp63
    tmp86 = triton_helpers.maximum(tmp85, tmp2)
    tmp87 = tmp86 * tmp63
    tmp88 = tmp84 - tmp63
    tmp89 = tmp87 * tmp88
    tmp90 = tl.where(tmp83, tmp89, tmp2)
    tmp91 = triton_helpers.maximum(tmp80, tmp90)
    tmp93 = tmp58 == tmp92
    tmp94 = tmp93 == 0
    tmp95 = tl.where(tmp94, tmp34, tmp2)
    tmp96 = tmp95 + tmp63
    tmp97 = triton_helpers.maximum(tmp96, tmp2)
    tmp98 = tmp97 * tmp63
    tmp99 = tmp95 - tmp63
    tmp100 = tmp98 * tmp99
    tmp101 = tl.where(tmp94, tmp100, tmp2)
    tmp102 = triton_helpers.maximum(tmp91, tmp101)
    tmp103 = tl_math.abs(tmp102)
    tmp104 = tmp103 == tmp44
    tmp105 = tl.where(tmp104, tmp2, tmp102)
    tmp106 = tmp69 - tmp105
    tmp107 = tl_math.exp(tmp106)
    tmp108 = tmp79 - tmp105
    tmp109 = tl_math.exp(tmp108)
    tmp110 = tmp107 + tmp109
    tmp111 = tmp90 - tmp105
    tmp112 = tl_math.exp(tmp111)
    tmp113 = tmp110 + tmp112
    tmp114 = tmp101 - tmp105
    tmp115 = tl_math.exp(tmp114)
    tmp116 = tmp113 + tmp115
    tmp117 = tl_math.log(tmp57)
    tmp118 = tmp117 + tmp46
    tmp119 = tl_math.log(tmp116)
    tmp120 = tmp119 + tmp105
    tmp121 = tmp118 + tmp120
    tmp122 = 20.0
    tmp123 = tmp121 > tmp122
    tmp124 = tl_math.exp(tmp121)
    tmp125 = libdevice.log1p(tmp124)
    tmp126 = tl.where(tmp123, tmp121, tmp125)
    tmp127 = tl.broadcast_to(tmp126, [RBLOCK])
    tmp129 = triton_helpers.promote_to_tensor(tl.sum(tmp127, 0))
    tmp130 = 256.0
    tmp131 = tmp129 / tmp130
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp131, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg1_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 4, 4, 4), (256, 64, 16, 4, 1),
            torch.bool)
        get_raw_stream(0)
        triton_poi_fused_eq_0[grid(1024)](arg1_1, buf0, 1024, XBLOCK=128,
            num_warps=4, num_stages=1)
        triton_poi_fused_fill_1[grid(4)](buf0, 4, XBLOCK=4, num_warps=1,
            num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4, 4), (64, 16, 4, 1), torch.float32)
        triton_poi_fused_clone_2[grid(4, 64)](arg0_1, buf2, 4, 64, XBLOCK=
            32, YBLOCK=4, num_warps=4, num_stages=1)
        buf3 = empty_strided_cuda((16, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (16, 4, 4), (16, 4, 1
            ), 0), reinterpret_tensor(buf2, (16, 4, 4), (16, 4, 1), 0), out
            =buf3)
        del arg0_1
        del buf2
        buf8 = empty_strided_cuda((), (), torch.float32)
        buf9 = buf8
        del buf8
        triton_per_fused_add_clamp_min_eq_logical_not_logsumexp_mean_mul_rsub_softplus_sub_where_zeros_like_3[
            grid(1)](buf9, buf0, buf3, arg1_1, 1, 256, num_warps=2,
            num_stages=1)
        del arg1_1
        del buf0
        del buf3
    return buf9,


class CircleLossNew(nn.Module):
    """CircleLoss from
	`"Circle Loss: A Unified Perspective of Pair Similarity Optimization"
	<https://arxiv.org/pdf/2002.10857>`_ paper.

	Parameters
	----------
	m: float.
		Margin parameter for loss.
	gamma: int.
		Scale parameter for loss.

	Outputs:
		- **loss**: scalar.
	"""

    def __init__(self, m, gamma):
        super(CircleLossNew, self).__init__()
        self.m = m
        self.gamma = gamma
        self.dp = 1 - m
        self.dn = m

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