# 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_7/inductor_cache/bd/cbdsxlyts4mwuqli5273sdktccdtyxdoznohbs5msxdngmxo6b6p.py
# Topologically Sorted Source Nodes: [similarity], Original ATen: [aten.linalg_vector_norm]
# Source node to ATen node mapping:
#   similarity => pow_1, sum_1
# Graph fragment:
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%expand_1, 2), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%pow_1, [-2], True), kwargs = {})
triton_poi_fused_linalg_vector_norm_0 = async_compile.triton('triton_poi_fused_linalg_vector_norm_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: '*fp32', 2: '*fp32', 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, 2, 3), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_linalg_vector_norm_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 8, '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_linalg_vector_norm_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = (xindex // 8) % 8
    x2 = (xindex // 64)
    x3 = xindex
    tmp0 = x1
    tmp1 = tl.full([1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x2 + (64*x1)), tmp4 & xmask, eviction_policy='evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tl.load(in_ptr1 + (x2 + (64*((-4) + x1))), tmp6 & xmask, eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp11 = tmp10 * tmp10
    tmp12 = tl.load(in_ptr0 + (16 + x2 + (64*x1)), tmp4 & xmask, eviction_policy='evict_last', other=0.0)
    tmp13 = tl.load(in_ptr1 + (16 + x2 + (64*((-4) + x1))), tmp6 & xmask, eviction_policy='evict_last', other=0.0)
    tmp14 = tl.where(tmp4, tmp12, tmp13)
    tmp15 = tmp14 * tmp14
    tmp16 = tmp11 + tmp15
    tmp17 = tl.load(in_ptr0 + (32 + x2 + (64*x1)), tmp4 & xmask, eviction_policy='evict_last', other=0.0)
    tmp18 = tl.load(in_ptr1 + (32 + x2 + (64*((-4) + x1))), tmp6 & xmask, eviction_policy='evict_last', other=0.0)
    tmp19 = tl.where(tmp4, tmp17, tmp18)
    tmp20 = tmp19 * tmp19
    tmp21 = tmp16 + tmp20
    tmp22 = tl.load(in_ptr0 + (48 + x2 + (64*x1)), tmp4 & xmask, eviction_policy='evict_last', other=0.0)
    tmp23 = tl.load(in_ptr1 + (48 + x2 + (64*((-4) + x1))), tmp6 & xmask, eviction_policy='evict_last', other=0.0)
    tmp24 = tl.where(tmp4, tmp22, tmp23)
    tmp25 = tmp24 * tmp24
    tmp26 = tmp21 + tmp25
    tl.store(out_ptr0 + (x3), tmp26, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/y5/cy5rrcntt7relb3pmc2gjnh2h677irtorxmfeyf2x2fi5bz26m2e.py
# Topologically Sorted Source Nodes: [similarity], Original ATen: [aten.linalg_vector_norm]
# Source node to ATen node mapping:
#   similarity => pow_3, sum_2
# Graph fragment:
#   %pow_3 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%expand, 2), kwargs = {})
#   %sum_2 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%pow_3, [-2], True), kwargs = {})
triton_poi_fused_linalg_vector_norm_1 = async_compile.triton('triton_poi_fused_linalg_vector_norm_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=[1024], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 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, 2, 3), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_linalg_vector_norm_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 8, '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_linalg_vector_norm_1(in_ptr0, in_ptr1, 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 % 8
    x2 = (xindex // 64)
    x3 = xindex
    tmp0 = x0
    tmp1 = tl.full([1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x2 + (64*x0)), tmp4 & xmask, eviction_policy='evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tl.load(in_ptr1 + (x2 + (64*((-4) + x0))), tmp6 & xmask, eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp11 = tmp10 * tmp10
    tmp12 = tl.load(in_ptr0 + (16 + x2 + (64*x0)), tmp4 & xmask, eviction_policy='evict_last', other=0.0)
    tmp13 = tl.load(in_ptr1 + (16 + x2 + (64*((-4) + x0))), tmp6 & xmask, eviction_policy='evict_last', other=0.0)
    tmp14 = tl.where(tmp4, tmp12, tmp13)
    tmp15 = tmp14 * tmp14
    tmp16 = tmp11 + tmp15
    tmp17 = tl.load(in_ptr0 + (32 + x2 + (64*x0)), tmp4 & xmask, eviction_policy='evict_last', other=0.0)
    tmp18 = tl.load(in_ptr1 + (32 + x2 + (64*((-4) + x0))), tmp6 & xmask, eviction_policy='evict_last', other=0.0)
    tmp19 = tl.where(tmp4, tmp17, tmp18)
    tmp20 = tmp19 * tmp19
    tmp21 = tmp16 + tmp20
    tmp22 = tl.load(in_ptr0 + (48 + x2 + (64*x0)), tmp4 & xmask, eviction_policy='evict_last', other=0.0)
    tmp23 = tl.load(in_ptr1 + (48 + x2 + (64*((-4) + x0))), tmp6 & xmask, eviction_policy='evict_last', other=0.0)
    tmp24 = tl.where(tmp4, tmp22, tmp23)
    tmp25 = tmp24 * tmp24
    tmp26 = tmp21 + tmp25
    tl.store(out_ptr0 + (x3), tmp26, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/3z/c3zco4vmq5jt6zbk3k66bqdx3yy3xituhdcuecs3ydyr77tw7xrg.py
# Topologically Sorted Source Nodes: [similarity], Original ATen: [aten.linalg_vector_norm, aten.clamp_min, aten.div, aten.mul]
# Source node to ATen node mapping:
#   similarity => clamp_min, clamp_min_1, div, div_1, mul, pow_2, pow_4
# Graph fragment:
#   %pow_2 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_1, 0.5), kwargs = {})
#   %clamp_min : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%pow_2, 1.1920928955078125e-07), kwargs = {})
#   %div_1 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%expand_1, %clamp_min), kwargs = {})
#   %pow_4 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sum_2, 0.5), kwargs = {})
#   %clamp_min_1 : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%pow_4, 1.1920928955078125e-07), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%expand, %clamp_min_1), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_1, %div), kwargs = {})
triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_2 = async_compile.triton('triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_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=[4096], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 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': {}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 5), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_2', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 6, '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_clamp_min_div_linalg_vector_norm_mul_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 4096
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = tl.full([XBLOCK], True, tl.int1)
    x2 = (xindex // 32) % 8
    x1 = (xindex // 8) % 4
    x3 = (xindex // 256)
    x0 = xindex % 8
    x4 = (xindex // 32)
    x5 = xindex
    tmp11 = tl.load(in_ptr2 + (x0 + (8*x4)), None, eviction_policy='evict_last')
    tmp24 = tl.load(in_ptr3 + (x0 + (8*x4)), None, eviction_policy='evict_last')
    tmp0 = x2
    tmp1 = tl.full([1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x3 + (16*x1) + (64*x2)), tmp4, eviction_policy='evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tl.load(in_ptr1 + (x3 + (16*x1) + (64*((-4) + x2))), tmp6, eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp12 = libdevice.sqrt(tmp11)
    tmp13 = 1.1920928955078125e-07
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp10 / tmp14
    tmp16 = x0
    tmp17 = tmp16 >= tmp1
    tmp18 = tmp16 < tmp3
    tmp19 = tl.load(in_ptr0 + (x3 + (16*x1) + (64*x0)), tmp18, eviction_policy='evict_last', other=0.0)
    tmp20 = tmp16 >= tmp3
    tmp21 = tmp16 < tmp7
    tmp22 = tl.load(in_ptr1 + (x3 + (16*x1) + (64*((-4) + x0))), tmp20, eviction_policy='evict_last', other=0.0)
    tmp23 = tl.where(tmp18, tmp19, tmp22)
    tmp25 = libdevice.sqrt(tmp24)
    tmp26 = triton_helpers.maximum(tmp25, tmp13)
    tmp27 = tmp23 / tmp26
    tmp28 = tmp15 * tmp27
    tl.store(out_ptr0 + (x5), tmp28, None)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/bp/cbpoftbr55ldmytwdwnljolr3bm6phi3m3fy2gdpelwrlzc3rjz7.py
# Topologically Sorted Source Nodes: [mask_nd, neg_inf, similarity, similarity_1, log_softmax], Original ATen: [aten.repeat, aten.mul, aten.sum, aten.where, aten._log_softmax]
# Source node to ATen node mapping:
#   log_softmax => exp, sum_4
#   mask_nd => repeat
#   neg_inf => full_default_2
#   similarity => sum_3
#   similarity_1 => where_1
# Graph fragment:
#   %repeat : [num_users=1] = call_function[target=torch.ops.aten.repeat.default](args = (%unsqueeze_3, [4, 4, 1, 1]), kwargs = {})
#   %full_default_2 : [num_users=1] = call_function[target=torch.ops.aten.full.default](args = ([4, 4, 8, 8], -inf), kwargs = {dtype: torch.float32, layout: torch.strided, device: cuda:0, pin_memory: False})
#   %sum_3 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul, [-2]), kwargs = {})
#   %where_1 : [num_users=1] = call_function[target=torch.ops.aten.where.self](args = (%repeat, %full_default_2, %sum_3), kwargs = {})
#   %mul_tensor : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%where_1, 1), kwargs = {})
#   %amax_default : [num_users=1] = call_function[target=torch.ops.aten.amax.default](args = (%mul_tensor, [-1], True), kwargs = {})
#   %sub_tensor : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%mul_tensor, %amax_default), kwargs = {})
#   %div_tensor : [num_users=2] = call_function[target=torch.ops.aten.div.Tensor](args = (%sub_tensor, 1.0), kwargs = {})
#   %exp : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%div_tensor,), kwargs = {})
#   %sum_4 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp, [-1], True), kwargs = {})
triton_per_fused__log_softmax_mul_repeat_sum_where_3 = async_compile.triton('triton_per_fused__log_softmax_mul_repeat_sum_where_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=[128, 8],
    reduction_hint=ReductionHint.DEFAULT,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: 'i32', 4: '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), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused__log_softmax_mul_repeat_sum_where_3', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 4, '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__log_softmax_mul_repeat_sum_where_3(in_ptr0, out_ptr1, out_ptr2, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 128
    rnumel = 8
    RBLOCK: tl.constexpr = 8
    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 = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    x0 = xindex % 8
    r2 = rindex
    x3 = xindex
    tmp7 = tl.load(in_ptr0 + (r2 + (32*x3)), xmask, other=0.0)
    tmp8 = tl.load(in_ptr0 + (8 + r2 + (32*x3)), xmask, other=0.0)
    tmp10 = tl.load(in_ptr0 + (16 + r2 + (32*x3)), xmask, other=0.0)
    tmp12 = tl.load(in_ptr0 + (24 + r2 + (32*x3)), xmask, other=0.0)
    tmp0 = x0
    tmp1 = r2
    tmp2 = tmp0 == tmp1
    tmp3 = 1.0
    tmp4 = 0.0
    tmp5 = tl.where(tmp2, tmp3, tmp4)
    tmp6 = (tmp5 != 0)
    tmp9 = tmp7 + tmp8
    tmp11 = tmp9 + tmp10
    tmp13 = tmp11 + tmp12
    tmp14 = float("-inf")
    tmp15 = tl.where(tmp6, tmp14, tmp13)
    tmp16 = tmp15 * tmp3
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, float("-inf"))
    tmp20 = triton_helpers.max2(tmp19, 1)[:, None]
    tmp21 = tmp16 - tmp20
    tmp22 = tmp21 * tmp3
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp26 = tl.where(xmask, tmp24, 0)
    tmp27 = tl.sum(tmp26, 1)[:, None]
    tl.store(out_ptr1 + (r2 + (8*x3)), tmp22, xmask)
    tl.store(out_ptr2 + (x3), tmp27, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/y3/cy3k5lieetpk5flruwavi2d2gc32r4qn2etbkrjewl4nxsi7syai.py
# Topologically Sorted Source Nodes: [positive_pairs], Original ATen: [aten.cat]
# Source node to ATen node mapping:
#   positive_pairs => cat_1
# Graph fragment:
#   %cat_1 : [num_users=1] = call_function[target=torch.ops.aten.cat.default](args = ([%diagonal, %diagonal_1], -1), kwargs = {})
triton_poi_fused_cat_4 = async_compile.triton('triton_poi_fused_cat_4', '''
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=[64], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 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, 2, 3), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_cat_4', '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_cat_4(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = (xindex // 4)
    tmp0 = tl.load(in_ptr0 + (4 + (9*x0) + (64*x1)), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x0 + (8*x1)), xmask)
    tmp2 = tl_math.log(tmp1)
    tmp3 = tmp0 - tmp2
    tl.store(out_ptr0 + (x0 + (8*x1)), tmp3, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/jh/cjhuzffkbppfabjw57yx4gc736wd25ordnwa2tnw54urj2v72l3i.py
# Topologically Sorted Source Nodes: [positive_pairs], Original ATen: [aten.cat]
# Source node to ATen node mapping:
#   positive_pairs => cat_1
# Graph fragment:
#   %cat_1 : [num_users=1] = call_function[target=torch.ops.aten.cat.default](args = ([%diagonal, %diagonal_1], -1), kwargs = {})
triton_poi_fused_cat_5 = async_compile.triton('triton_poi_fused_cat_5', '''
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=[64], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 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_cat_5', '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_cat_5(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = (xindex // 4)
    tmp0 = tl.load(in_ptr0 + (32 + (9*x0) + (64*x1)), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (4 + x0 + (8*x1)), xmask)
    tmp2 = tl_math.log(tmp1)
    tmp3 = tmp0 - tmp2
    tl.store(out_ptr0 + (x0 + (8*x1)), tmp3, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_7/inductor_cache/c4/cc435nlek6ugsevb7ct35myrjq6775utyhoih5vzjurusq7oobkk.py
# Topologically Sorted Source Nodes: [mean, neg], Original ATen: [aten.mean, aten.neg]
# Source node to ATen node mapping:
#   mean => mean
#   neg => neg
# Graph fragment:
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%cat_1,), kwargs = {})
#   %neg : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%mean,), kwargs = {})
triton_per_fused_mean_neg_6 = async_compile.triton('triton_per_fused_mean_neg_6', '''
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, 128],
    reduction_hint=ReductionHint.INNER,
    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': {2: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 3), equal_to_1=(2,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_mean_neg_6', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 1, '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_mean_neg_6(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 1
    rnumel = 128
    RBLOCK: tl.constexpr = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:, None]
    xmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[None, :]
    roffset = 0
    rmask = tl.full([XBLOCK, RBLOCK], True, tl.int1)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + (r0), None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp4 = 128.0
    tmp5 = tmp3 / tmp4
    tmp6 = -tmp5
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([XBLOCK, 1], 0, tl.int32)), tmp6, 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, 8, 1, 8), (256, 64, 8, 1024, 1), torch.float32)
        # Topologically Sorted Source Nodes: [similarity], Original ATen: [aten.linalg_vector_norm]
        stream0 = get_raw_stream(0)
        triton_poi_fused_linalg_vector_norm_0.run(arg0_1, arg1_1, buf0, 1024, grid=grid(1024), stream=stream0)
        buf1 = empty_strided_cuda((4, 4, 8, 1, 8), (256, 64, 8, 1024, 1), torch.float32)
        # Topologically Sorted Source Nodes: [similarity], Original ATen: [aten.linalg_vector_norm]
        triton_poi_fused_linalg_vector_norm_1.run(arg0_1, arg1_1, buf1, 1024, grid=grid(1024), stream=stream0)
        buf2 = empty_strided_cuda((4, 4, 8, 4, 8), (1024, 256, 32, 8, 1), torch.float32)
        # Topologically Sorted Source Nodes: [similarity], Original ATen: [aten.linalg_vector_norm, aten.clamp_min, aten.div, aten.mul]
        triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_2.run(arg0_1, arg1_1, buf0, buf1, buf2, 4096, grid=grid(4096), stream=stream0)
        del arg0_1
        del arg1_1
        del buf0
        buf4 = reinterpret_tensor(buf1, (4, 4, 8, 8), (256, 64, 8, 1), 0); del buf1  # reuse
        buf5 = empty_strided_cuda((4, 4, 8, 1), (32, 8, 1, 128), torch.float32)
        # Topologically Sorted Source Nodes: [mask_nd, neg_inf, similarity, similarity_1, log_softmax], Original ATen: [aten.repeat, aten.mul, aten.sum, aten.where, aten._log_softmax]
        triton_per_fused__log_softmax_mul_repeat_sum_where_3.run(buf2, buf4, buf5, 128, 8, grid=grid(128), stream=stream0)
        del buf2
        buf8 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32)
        buf6 = reinterpret_tensor(buf8, (4, 4, 4), (32, 8, 1), 0)  # alias
        # Topologically Sorted Source Nodes: [positive_pairs], Original ATen: [aten.cat]
        triton_poi_fused_cat_4.run(buf4, buf5, buf6, 64, grid=grid(64), stream=stream0)
        buf7 = reinterpret_tensor(buf8, (4, 4, 4), (32, 8, 1), 4)  # alias
        # Topologically Sorted Source Nodes: [positive_pairs], Original ATen: [aten.cat]
        triton_poi_fused_cat_5.run(buf4, buf5, buf7, 64, grid=grid(64), stream=stream0)
        del buf4
        del buf5
        buf9 = empty_strided_cuda((), (), torch.float32)
        buf10 = buf9; del buf9  # reuse
        # Topologically Sorted Source Nodes: [mean, neg], Original ATen: [aten.mean, aten.neg]
        triton_per_fused_mean_neg_6.run(buf10, buf8, 1, 128, grid=grid(1), stream=stream0)
        del buf6
        del buf7
        del buf8
    return (buf10, )


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)

from torch.nn import Module
import torch
from torch.nn import LogSoftmax
from torch.nn.functional import cosine_similarity


class ContrastiveLoss(Module):
    """A contrastive loss adapted from SimCLR.

    Link to SimCLR: https://arxiv.org/abs/2002.05709v3.
    """

    def __init__(self, temperature: 'float'=1.0):
        """Save hyper-params."""
        super().__init__()
        self.temperature = temperature
        self._log_softmax_fn = LogSoftmax(dim=-1)

    def forward(self, inputs: 'torch.Tensor', targets: 'torch.Tensor'
        ) ->torch.Tensor:
        """Get the loss."""
        inputs = inputs.permute(2, 3, 0, 1)
        targets = targets.permute(2, 3, 0, 1)
        batch_size = inputs.shape[-2]
        left = torch.cat([inputs, targets], -2).unsqueeze(-1)
        right = left.permute(0, 1, 4, 3, 2)
        similarity = cosine_similarity(left, right, dim=-2, eps=torch.finfo
            (left.dtype).eps)
        mask = torch.eye(2 * batch_size, device=similarity.device).bool()
        mask_nd = mask.unsqueeze(0).unsqueeze(0).tile(*similarity.shape[:2],
            1, 1)
        neg_inf = float('-inf') * torch.ones_like(similarity)
        similarity = torch.where(mask_nd, neg_inf, similarity)
        log_softmax = self._log_softmax_fn(similarity / self.temperature)
        positive_pairs = torch.cat([torch.diagonal(log_softmax, offset=
            batch_size, dim1=-2, dim2=-1), torch.diagonal(log_softmax,
            offset=-batch_size, dim1=-2, dim2=-1)], -1)
        return -positive_pairs.mean()


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


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

import torch
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 torch.nn import Module
from torch.nn import LogSoftmax
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_linalg_vector_norm_0(in_ptr0, in_ptr1, out_ptr0,
    xnumel, XBLOCK: tl.constexpr):
    xnumel = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 8 % 8
    x2 = xindex // 64
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x2 + 64 * x1), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x2 + 64 * (-4 + x1)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp11 = tmp10 * tmp10
    tmp12 = tl.load(in_ptr0 + (16 + x2 + 64 * x1), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp13 = tl.load(in_ptr1 + (16 + x2 + 64 * (-4 + x1)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp14 = tl.where(tmp4, tmp12, tmp13)
    tmp15 = tmp14 * tmp14
    tmp16 = tmp11 + tmp15
    tmp17 = tl.load(in_ptr0 + (32 + x2 + 64 * x1), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp18 = tl.load(in_ptr1 + (32 + x2 + 64 * (-4 + x1)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp19 = tl.where(tmp4, tmp17, tmp18)
    tmp20 = tmp19 * tmp19
    tmp21 = tmp16 + tmp20
    tmp22 = tl.load(in_ptr0 + (48 + x2 + 64 * x1), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp23 = tl.load(in_ptr1 + (48 + x2 + 64 * (-4 + x1)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp24 = tl.where(tmp4, tmp22, tmp23)
    tmp25 = tmp24 * tmp24
    tmp26 = tmp21 + tmp25
    tl.store(out_ptr0 + x3, tmp26, xmask)


@triton.jit
def triton_poi_fused_linalg_vector_norm_1(in_ptr0, in_ptr1, 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 % 8
    x2 = xindex // 64
    x3 = xindex
    tmp0 = x0
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x2 + 64 * x0), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x2 + 64 * (-4 + x0)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp11 = tmp10 * tmp10
    tmp12 = tl.load(in_ptr0 + (16 + x2 + 64 * x0), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp13 = tl.load(in_ptr1 + (16 + x2 + 64 * (-4 + x0)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp14 = tl.where(tmp4, tmp12, tmp13)
    tmp15 = tmp14 * tmp14
    tmp16 = tmp11 + tmp15
    tmp17 = tl.load(in_ptr0 + (32 + x2 + 64 * x0), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp18 = tl.load(in_ptr1 + (32 + x2 + 64 * (-4 + x0)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp19 = tl.where(tmp4, tmp17, tmp18)
    tmp20 = tmp19 * tmp19
    tmp21 = tmp16 + tmp20
    tmp22 = tl.load(in_ptr0 + (48 + x2 + 64 * x0), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp23 = tl.load(in_ptr1 + (48 + x2 + 64 * (-4 + x0)), tmp6 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp24 = tl.where(tmp4, tmp22, tmp23)
    tmp25 = tmp24 * tmp24
    tmp26 = tmp21 + tmp25
    tl.store(out_ptr0 + x3, tmp26, xmask)


@triton.jit
def triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_2(in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    tl.full([XBLOCK], True, tl.int1)
    x2 = xindex // 32 % 8
    x1 = xindex // 8 % 4
    x3 = xindex // 256
    x0 = xindex % 8
    x4 = xindex // 32
    x5 = xindex
    tmp11 = tl.load(in_ptr2 + (x0 + 8 * x4), None, eviction_policy='evict_last'
        )
    tmp24 = tl.load(in_ptr3 + (x0 + 8 * x4), None, eviction_policy='evict_last'
        )
    tmp0 = x2
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x3 + 16 * x1 + 64 * x2), tmp4,
        eviction_policy='evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1], 8, tl.int64)
    tmp9 = tl.load(in_ptr1 + (x3 + 16 * x1 + 64 * (-4 + x2)), tmp6,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp12 = libdevice.sqrt(tmp11)
    tmp13 = 1.1920928955078125e-07
    tmp14 = triton_helpers.maximum(tmp12, tmp13)
    tmp15 = tmp10 / tmp14
    tmp16 = x0
    tmp18 = tmp16 < tmp3
    tmp19 = tl.load(in_ptr0 + (x3 + 16 * x1 + 64 * x0), tmp18,
        eviction_policy='evict_last', other=0.0)
    tmp20 = tmp16 >= tmp3
    tmp22 = tl.load(in_ptr1 + (x3 + 16 * x1 + 64 * (-4 + x0)), tmp20,
        eviction_policy='evict_last', other=0.0)
    tmp23 = tl.where(tmp18, tmp19, tmp22)
    tmp25 = libdevice.sqrt(tmp24)
    tmp26 = triton_helpers.maximum(tmp25, tmp13)
    tmp27 = tmp23 / tmp26
    tmp28 = tmp15 * tmp27
    tl.store(out_ptr0 + x5, tmp28, None)


@triton.jit
def triton_per_fused__log_softmax_mul_repeat_sum_where_3(in_ptr0, out_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    xnumel = 128
    RBLOCK: tl.constexpr = 8
    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)
    x0 = xindex % 8
    r2 = rindex
    x3 = xindex
    tmp7 = tl.load(in_ptr0 + (r2 + 32 * x3), xmask, other=0.0)
    tmp8 = tl.load(in_ptr0 + (8 + r2 + 32 * x3), xmask, other=0.0)
    tmp10 = tl.load(in_ptr0 + (16 + r2 + 32 * x3), xmask, other=0.0)
    tmp12 = tl.load(in_ptr0 + (24 + r2 + 32 * x3), xmask, other=0.0)
    tmp0 = x0
    tmp1 = r2
    tmp2 = tmp0 == tmp1
    tmp3 = 1.0
    tmp4 = 0.0
    tmp5 = tl.where(tmp2, tmp3, tmp4)
    tmp6 = tmp5 != 0
    tmp9 = tmp7 + tmp8
    tmp11 = tmp9 + tmp10
    tmp13 = tmp11 + tmp12
    tmp14 = float('-inf')
    tmp15 = tl.where(tmp6, tmp14, tmp13)
    tmp16 = tmp15 * tmp3
    tmp17 = tl.broadcast_to(tmp16, [XBLOCK, RBLOCK])
    tmp19 = tl.where(xmask, tmp17, float('-inf'))
    tmp20 = triton_helpers.max2(tmp19, 1)[:, None]
    tmp21 = tmp16 - tmp20
    tmp22 = tmp21 * tmp3
    tmp23 = tl_math.exp(tmp22)
    tmp24 = tl.broadcast_to(tmp23, [XBLOCK, RBLOCK])
    tmp26 = tl.where(xmask, tmp24, 0)
    tmp27 = tl.sum(tmp26, 1)[:, None]
    tl.store(out_ptr1 + (r2 + 8 * x3), tmp22, xmask)
    tl.store(out_ptr2 + x3, tmp27, xmask)


@triton.jit
def triton_poi_fused_cat_4(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + (4 + 9 * x0 + 64 * x1), xmask, eviction_policy
        ='evict_last')
    tmp1 = tl.load(in_ptr1 + (x0 + 8 * x1), xmask)
    tmp2 = tl_math.log(tmp1)
    tmp3 = tmp0 - tmp2
    tl.store(out_ptr0 + (x0 + 8 * x1), tmp3, xmask)


@triton.jit
def triton_poi_fused_cat_5(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK: tl.
    constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x1 = xindex // 4
    tmp0 = tl.load(in_ptr0 + (32 + 9 * x0 + 64 * x1), xmask,
        eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (4 + x0 + 8 * x1), xmask)
    tmp2 = tl_math.log(tmp1)
    tmp3 = tmp0 - tmp2
    tl.store(out_ptr0 + (x0 + 8 * x1), tmp3, xmask)


@triton.jit
def triton_per_fused_mean_neg_6(in_out_ptr0, in_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 128
    xoffset = tl.program_id(0) * XBLOCK
    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)
    r0 = rindex
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp4 = 128.0
    tmp5 = tmp3 / tmp4
    tmp6 = -tmp5
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp6, 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, 8, 1, 8), (256, 64, 8, 1024, 1),
            torch.float32)
        get_raw_stream(0)
        triton_poi_fused_linalg_vector_norm_0[grid(1024)](arg0_1, arg1_1,
            buf0, 1024, XBLOCK=256, num_warps=4, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 8, 1, 8), (256, 64, 8, 1024, 1),
            torch.float32)
        triton_poi_fused_linalg_vector_norm_1[grid(1024)](arg0_1, arg1_1,
            buf1, 1024, XBLOCK=128, num_warps=4, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 8, 4, 8), (1024, 256, 32, 8, 1),
            torch.float32)
        triton_poi_fused_clamp_min_div_linalg_vector_norm_mul_2[grid(4096)](
            arg0_1, arg1_1, buf0, buf1, buf2, 4096, XBLOCK=128, num_warps=4,
            num_stages=1)
        del arg0_1
        del arg1_1
        del buf0
        buf4 = reinterpret_tensor(buf1, (4, 4, 8, 8), (256, 64, 8, 1), 0)
        del buf1
        buf5 = empty_strided_cuda((4, 4, 8, 1), (32, 8, 1, 128), torch.float32)
        triton_per_fused__log_softmax_mul_repeat_sum_where_3[grid(128)](buf2,
            buf4, buf5, 128, 8, XBLOCK=8, num_warps=2, num_stages=1)
        del buf2
        buf8 = empty_strided_cuda((4, 4, 8), (32, 8, 1), torch.float32)
        buf6 = reinterpret_tensor(buf8, (4, 4, 4), (32, 8, 1), 0)
        triton_poi_fused_cat_4[grid(64)](buf4, buf5, buf6, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        buf7 = reinterpret_tensor(buf8, (4, 4, 4), (32, 8, 1), 4)
        triton_poi_fused_cat_5[grid(64)](buf4, buf5, buf7, 64, XBLOCK=64,
            num_warps=1, num_stages=1)
        del buf4
        del buf5
        buf9 = empty_strided_cuda((), (), torch.float32)
        buf10 = buf9
        del buf9
        triton_per_fused_mean_neg_6[grid(1)](buf10, buf8, 1, 128, XBLOCK=1,
            num_warps=2, num_stages=1)
        del buf6
        del buf7
        del buf8
    return buf10,


class ContrastiveLossNew(Module):
    """A contrastive loss adapted from SimCLR.

    Link to SimCLR: https://arxiv.org/abs/2002.05709v3.
    """

    def __init__(self, temperature: 'float'=1.0):
        """Save hyper-params."""
        super().__init__()
        self.temperature = temperature
        self._log_softmax_fn = LogSoftmax(dim=-1)

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