# 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_4/inductor_cache/ps/cps5r3mimvzmxfkludsnsxwqfrprrusee5gwryeh4w7edcd7walj.py
# Topologically Sorted Source Nodes: [k_embed, k_norm], Original ATen: [aten.mul, aten.linalg_vector_norm, aten.clamp_min]
# Source node to ATen node mapping:
#   k_embed => mul
#   k_norm => clamp_min, pow_1, pow_2, sum_1
# Graph fragment:
#   %mul : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%arg1_1, %unsqueeze), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%mul, 2), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%pow_1, [2], True), kwargs = {})
#   %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, 1e-10), kwargs = {})
triton_poi_fused_clamp_min_linalg_vector_norm_mul_0 = async_compile.triton('triton_poi_fused_clamp_min_linalg_vector_norm_mul_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=[16], 
    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_clamp_min_linalg_vector_norm_mul_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 5, '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_linalg_vector_norm_mul_0(in_ptr0, in_ptr1, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (4*x0), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x2), xmask)
    tmp4 = tl.load(in_ptr0 + (1 + (4*x0)), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + (4*x0)), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (3 + (4*x0)), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp3 = tmp2 * tmp2
    tmp5 = tmp4 * tmp1
    tmp6 = tmp5 * tmp5
    tmp7 = tmp3 + tmp6
    tmp9 = tmp8 * tmp1
    tmp10 = tmp9 * tmp9
    tmp11 = tmp7 + tmp10
    tmp13 = tmp12 * tmp1
    tmp14 = tmp13 * tmp13
    tmp15 = tmp11 + tmp14
    tmp16 = libdevice.sqrt(tmp15)
    tmp17 = 1e-10
    tmp18 = triton_helpers.maximum(tmp16, tmp17)
    tl.store(out_ptr0 + (x2), tmp18, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/ny/cnyuymkbpiqhtjgqpwlinpbmbfxj6fw64hfk3le4jmznef66mptc.py
# Topologically Sorted Source Nodes: [k_embed, k_norm], Original ATen: [aten.mul, aten.div]
# Source node to ATen node mapping:
#   k_embed => mul
#   k_norm => div
# Graph fragment:
#   %mul : [num_users=2] = call_function[target=torch.ops.aten.mul.Tensor](args = (%arg1_1, %unsqueeze), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%mul, %expand), kwargs = {})
triton_poi_fused_div_mul_1 = async_compile.triton('triton_poi_fused_div_mul_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=[64], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 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, 4), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_div_mul_1', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 3, '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_div_mul_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex % 16
    x4 = (xindex // 4)
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + (x3), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (x4), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (x4), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 / tmp3
    tl.store(out_ptr0 + (x5), tmp4, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/fk/cfkpb4ktlmv2quxk6raxdfre2a2g2vszmw6fgblhx44wxl6p3vbc.py
# Topologically Sorted Source Nodes: [sub, pow_1, neg, pow_2, truediv, truediv_1, kernel_outputs, sum_1], Original ATen: [aten.sub, aten.pow, aten.neg, aten.div, aten.exp, aten.sum]
# Source node to ATen node mapping:
#   kernel_outputs => exp
#   neg => neg
#   pow_1 => pow_5
#   pow_2 => pow_6
#   sub => sub
#   sum_1 => sum_3
#   truediv => div_2
#   truediv_1 => div_3
# Graph fragment:
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%unsqueeze_4, %arg4_1), kwargs = {})
#   %pow_5 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sub, 2), kwargs = {})
#   %neg : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%pow_5,), kwargs = {})
#   %pow_6 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%arg5_1, 2), kwargs = {})
#   %div_2 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%neg, %pow_6), kwargs = {})
#   %div_3 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%div_2, 2), kwargs = {})
#   %exp : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%div_3,), kwargs = {})
#   %sum_3 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%exp, [2]), kwargs = {})
triton_poi_fused_div_exp_neg_pow_sub_sum_2 = async_compile.triton('triton_poi_fused_div_exp_neg_pow_sub_sum_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=[512], 
    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_div_exp_neg_pow_sub_sum_2', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 10, '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_div_exp_neg_pow_sub_sum_2(in_ptr0, in_ptr1, in_ptr2, in_ptr3, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 336
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = (xindex // 21)
    x0 = xindex % 21
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (4*x1), xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + (4*x1), xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + (x0), xmask, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr3 + (x0), xmask, eviction_policy='evict_last')
    tmp13 = tl.load(in_ptr0 + (1 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp14 = tl.load(in_ptr1 + (1 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp23 = tl.load(in_ptr0 + (2 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp24 = tl.load(in_ptr1 + (2 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp33 = tl.load(in_ptr0 + (3 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp34 = tl.load(in_ptr1 + (3 + (4*x1)), xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 - tmp3
    tmp5 = tmp4 * tmp4
    tmp6 = -tmp5
    tmp8 = tmp7 * tmp7
    tmp9 = tmp6 / tmp8
    tmp10 = 0.5
    tmp11 = tmp9 * tmp10
    tmp12 = tl_math.exp(tmp11)
    tmp15 = tmp13 * tmp14
    tmp16 = tmp15 - tmp3
    tmp17 = tmp16 * tmp16
    tmp18 = -tmp17
    tmp19 = tmp18 / tmp8
    tmp20 = tmp19 * tmp10
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tmp12 + tmp21
    tmp25 = tmp23 * tmp24
    tmp26 = tmp25 - tmp3
    tmp27 = tmp26 * tmp26
    tmp28 = -tmp27
    tmp29 = tmp28 / tmp8
    tmp30 = tmp29 * tmp10
    tmp31 = tl_math.exp(tmp30)
    tmp32 = tmp22 + tmp31
    tmp35 = tmp33 * tmp34
    tmp36 = tmp35 - tmp3
    tmp37 = tmp36 * tmp36
    tmp38 = -tmp37
    tmp39 = tmp38 / tmp8
    tmp40 = tmp39 * tmp10
    tmp41 = tl_math.exp(tmp40)
    tmp42 = tmp32 + tmp41
    tl.store(out_ptr0 + (x2), tmp42, xmask)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/dm/cdmmcpjmg6vkigutcpwgkxhqfgxp7syymhmshwvkinhujhvx7rra.py
# Topologically Sorted Source Nodes: [clamp, log, kernel_outputs_1, logits], Original ATen: [aten.clamp, aten.log, aten.mul, aten.sum]
# Source node to ATen node mapping:
#   clamp => clamp_min_2
#   kernel_outputs_1 => mul_3
#   log => log
#   logits => sum_4
# Graph fragment:
#   %clamp_min_2 : [num_users=1] = call_function[target=torch.ops.aten.clamp_min.default](args = (%sum_3, 1e-10), kwargs = {})
#   %log : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%clamp_min_2,), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%log, 0.01), kwargs = {})
#   %sum_4 : [num_users=1] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%mul_3, [1]), kwargs = {})
triton_poi_fused_clamp_log_mul_sum_3 = async_compile.triton('triton_poi_fused_clamp_log_mul_sum_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.pointwise(
    size_hints=[128], 
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 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), equal_to_1=())]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_poi_fused_clamp_log_mul_sum_3', '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_clamp_log_mul_sum_3(in_ptr0, out_ptr0, xnumel, XBLOCK : tl.constexpr):
    xnumel = 84
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 21
    x1 = (xindex // 21)
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + (84*x1)), xmask)
    tmp6 = tl.load(in_ptr0 + (21 + x0 + (84*x1)), xmask)
    tmp11 = tl.load(in_ptr0 + (42 + x0 + (84*x1)), xmask)
    tmp16 = tl.load(in_ptr0 + (63 + x0 + (84*x1)), xmask)
    tmp1 = 1e-10
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp3 = tl_math.log(tmp2)
    tmp4 = 0.01
    tmp5 = tmp3 * tmp4
    tmp7 = triton_helpers.maximum(tmp6, tmp1)
    tmp8 = tl_math.log(tmp7)
    tmp9 = tmp8 * tmp4
    tmp10 = tmp5 + tmp9
    tmp12 = triton_helpers.maximum(tmp11, tmp1)
    tmp13 = tl_math.log(tmp12)
    tmp14 = tmp13 * tmp4
    tmp15 = tmp10 + tmp14
    tmp17 = triton_helpers.maximum(tmp16, tmp1)
    tmp18 = tl_math.log(tmp17)
    tmp19 = tmp18 * tmp4
    tmp20 = tmp15 + tmp19
    tl.store(out_ptr0 + (x2), tmp20, xmask)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    assert_size_stride(arg2_1, (4, 4), (4, 1))
    assert_size_stride(arg3_1, (4, 4), (4, 1))
    assert_size_stride(arg4_1, (1, 1, 1, 21), (21, 21, 21, 1))
    assert_size_stride(arg5_1, (1, 1, 1, 21), (21, 21, 21, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        # Topologically Sorted Source Nodes: [k_embed, k_norm], Original ATen: [aten.mul, aten.linalg_vector_norm, aten.clamp_min]
        stream0 = get_raw_stream(0)
        triton_poi_fused_clamp_min_linalg_vector_norm_mul_0.run(arg1_1, arg0_1, buf0, 16, grid=grid(16), stream=stream0)
        buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        # Topologically Sorted Source Nodes: [v_embed, v_norm], Original ATen: [aten.mul, aten.linalg_vector_norm, aten.clamp_min]
        triton_poi_fused_clamp_min_linalg_vector_norm_mul_0.run(arg3_1, arg2_1, buf1, 16, grid=grid(16), stream=stream0)
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [k_embed, k_norm], Original ATen: [aten.mul, aten.div]
        triton_poi_fused_div_mul_1.run(arg1_1, arg0_1, buf0, buf2, 64, grid=grid(64), stream=stream0)
        del arg1_1
        del buf0
        buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [v_embed, v_norm], Original ATen: [aten.mul, aten.div]
        triton_poi_fused_div_mul_1.run(arg3_1, arg2_1, buf1, buf3, 64, grid=grid(64), stream=stream0)
        del arg3_1
        del buf1
        buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        # Topologically Sorted Source Nodes: [k_embed, k_norm, bmm_1], Original ATen: [aten.mul, aten.div, aten.bmm]
        extern_kernels.bmm(buf2, reinterpret_tensor(buf3, (4, 4, 4), (16, 1, 4), 0), out=buf4)
        del buf2
        buf5 = buf3; del buf3  # reuse
        # Topologically Sorted Source Nodes: [k_by_v_mask], Original ATen: [aten.bmm]
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 4, 1), (4, 1, 1), 0), reinterpret_tensor(arg2_1, (4, 1, 4), (4, 1, 1), 0), out=buf5)
        del arg0_1
        del arg2_1
        buf6 = empty_strided_cuda((4, 4, 21), (84, 21, 1), torch.float32)
        # Topologically Sorted Source Nodes: [sub, pow_1, neg, pow_2, truediv, truediv_1, kernel_outputs, sum_1], Original ATen: [aten.sub, aten.pow, aten.neg, aten.div, aten.exp, aten.sum]
        triton_poi_fused_div_exp_neg_pow_sub_sum_2.run(buf4, buf5, arg4_1, arg5_1, buf6, 336, grid=grid(336), stream=stream0)
        del arg4_1
        del arg5_1
        del buf4
        del buf5
        buf7 = empty_strided_cuda((4, 21), (21, 1), torch.float32)
        # Topologically Sorted Source Nodes: [clamp, log, kernel_outputs_1, logits], Original ATen: [aten.clamp, aten.log, aten.mul, aten.sum]
        triton_poi_fused_clamp_log_mul_sum_3.run(buf6, buf7, 84, grid=grid(84), stream=stream0)
        del buf6
    return (buf7, )


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, 1), device='cuda:0', dtype=torch.float32)
    arg1_1 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    arg2_1 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    arg3_1 = rand_strided((4, 4), (4, 1), device='cuda:0', dtype=torch.float32)
    arg4_1 = rand_strided((1, 1, 1, 21), (21, 21, 21, 1), device='cuda:0', dtype=torch.float32)
    arg5_1 = rand_strided((1, 1, 1, 21), (21, 21, 21, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_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 Dict
import torch.nn as nn
import torch.nn.functional as F


class KernelMatcher(nn.Module):

    def __init__(self, embed_dim: 'int', kernel_num: 'int'=21) ->None:
        super(KernelMatcher, self).__init__()
        self._embed_dim = embed_dim
        self._kernel_num = kernel_num
        mus, sigmas = self.kernel_init(self._kernel_num)
        self._mus = nn.Parameter(mus, requires_grad=False)
        self._sigmas = nn.Parameter(sigmas, requires_grad=False)

    def kernel_init(self, kernel_num: 'int') ->Dict[str, torch.Tensor]:
        mus = [1]
        bin_size = 2.0 / (kernel_num - 1)
        mus.append(1 - bin_size / 2)
        for i in range(1, kernel_num - 1):
            mus.append(mus[i] - bin_size)
        mus = torch.tensor(mus).view(1, 1, 1, kernel_num)
        sigmas = [0.001]
        sigmas += [0.1] * (kernel_num - 1)
        sigmas = torch.tensor(sigmas).view(1, 1, 1, kernel_num)
        return mus, sigmas

    def forward(self, k_embed: 'torch.Tensor', k_mask: 'torch.Tensor',
        v_embed: 'torch.Tensor', v_mask: 'torch.Tensor') ->torch.Tensor:
        k_embed = k_embed * k_mask.unsqueeze(-1)
        v_embed = v_embed * v_mask.unsqueeze(-1)
        k_by_v_mask = torch.bmm(k_mask.float().unsqueeze(-1), v_mask.float(
            ).unsqueeze(-1).transpose(1, 2))
        k_norm = F.normalize(k_embed, p=2, dim=2, eps=1e-10)
        v_norm = F.normalize(v_embed, p=2, dim=2, eps=1e-10)
        inter = (torch.bmm(k_norm, v_norm.transpose(1, 2)) * k_by_v_mask
            ).unsqueeze(-1)
        kernel_outputs = torch.exp(-(inter - self._mus) ** 2 / self._sigmas **
            2 / 2)
        kernel_outputs = kernel_outputs.sum(dim=2).clamp(min=1e-10).log(
            ) * 0.01
        logits = kernel_outputs.sum(dim=1)
        return logits


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


def get_init_inputs():
    return [[], {'embed_dim': 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 Dict
import torch.nn as nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda
reinterpret_tensor = torch._C._dynamo.guards._reinterpret_tensor


@triton.jit
def triton_poi_fused_clamp_min_linalg_vector_norm_mul_0(in_ptr0, in_ptr1,
    out_ptr0, xnumel, XBLOCK: tl.constexpr):
    xnumel = 16
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 4
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + 4 * x0, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x2, xmask)
    tmp4 = tl.load(in_ptr0 + (1 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp8 = tl.load(in_ptr0 + (2 + 4 * x0), xmask, eviction_policy='evict_last')
    tmp12 = tl.load(in_ptr0 + (3 + 4 * x0), xmask, eviction_policy='evict_last'
        )
    tmp2 = tmp0 * tmp1
    tmp3 = tmp2 * tmp2
    tmp5 = tmp4 * tmp1
    tmp6 = tmp5 * tmp5
    tmp7 = tmp3 + tmp6
    tmp9 = tmp8 * tmp1
    tmp10 = tmp9 * tmp9
    tmp11 = tmp7 + tmp10
    tmp13 = tmp12 * tmp1
    tmp14 = tmp13 * tmp13
    tmp15 = tmp11 + tmp14
    tmp16 = libdevice.sqrt(tmp15)
    tmp17 = 1e-10
    tmp18 = triton_helpers.maximum(tmp16, tmp17)
    tl.store(out_ptr0 + x2, tmp18, xmask)


@triton.jit
def triton_poi_fused_div_mul_1(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 64
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x3 = xindex % 16
    x4 = xindex // 4
    x5 = xindex
    tmp0 = tl.load(in_ptr0 + x3, xmask, eviction_policy='evict_last')
    tmp1 = tl.load(in_ptr1 + x4, xmask, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr2 + x4, xmask, eviction_policy='evict_last')
    tmp2 = tmp0 * tmp1
    tmp4 = tmp2 / tmp3
    tl.store(out_ptr0 + x5, tmp4, xmask)


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


@triton.jit
def triton_poi_fused_clamp_log_mul_sum_3(in_ptr0, out_ptr0, xnumel, XBLOCK:
    tl.constexpr):
    xnumel = 84
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x0 = xindex % 21
    x1 = xindex // 21
    x2 = xindex
    tmp0 = tl.load(in_ptr0 + (x0 + 84 * x1), xmask)
    tmp6 = tl.load(in_ptr0 + (21 + x0 + 84 * x1), xmask)
    tmp11 = tl.load(in_ptr0 + (42 + x0 + 84 * x1), xmask)
    tmp16 = tl.load(in_ptr0 + (63 + x0 + 84 * x1), xmask)
    tmp1 = 1e-10
    tmp2 = triton_helpers.maximum(tmp0, tmp1)
    tmp3 = tl_math.log(tmp2)
    tmp4 = 0.01
    tmp5 = tmp3 * tmp4
    tmp7 = triton_helpers.maximum(tmp6, tmp1)
    tmp8 = tl_math.log(tmp7)
    tmp9 = tmp8 * tmp4
    tmp10 = tmp5 + tmp9
    tmp12 = triton_helpers.maximum(tmp11, tmp1)
    tmp13 = tl_math.log(tmp12)
    tmp14 = tmp13 * tmp4
    tmp15 = tmp10 + tmp14
    tmp17 = triton_helpers.maximum(tmp16, tmp1)
    tmp18 = tl_math.log(tmp17)
    tmp19 = tmp18 * tmp4
    tmp20 = tmp15 + tmp19
    tl.store(out_ptr0 + x2, tmp20, xmask)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    assert_size_stride(arg2_1, (4, 4), (4, 1))
    assert_size_stride(arg3_1, (4, 4), (4, 1))
    assert_size_stride(arg4_1, (1, 1, 1, 21), (21, 21, 21, 1))
    assert_size_stride(arg5_1, (1, 1, 1, 21), (21, 21, 21, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_clamp_min_linalg_vector_norm_mul_0[grid(16)](arg1_1,
            arg0_1, buf0, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf1 = empty_strided_cuda((4, 4, 1), (4, 1, 16), torch.float32)
        triton_poi_fused_clamp_min_linalg_vector_norm_mul_0[grid(16)](arg3_1,
            arg2_1, buf1, 16, XBLOCK=16, num_warps=1, num_stages=1)
        buf2 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_div_mul_1[grid(64)](arg1_1, arg0_1, buf0, buf2, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del arg1_1
        del buf0
        buf3 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        triton_poi_fused_div_mul_1[grid(64)](arg3_1, arg2_1, buf1, buf3, 64,
            XBLOCK=64, num_warps=1, num_stages=1)
        del arg3_1
        del buf1
        buf4 = empty_strided_cuda((4, 4, 4), (16, 4, 1), torch.float32)
        extern_kernels.bmm(buf2, reinterpret_tensor(buf3, (4, 4, 4), (16, 1,
            4), 0), out=buf4)
        del buf2
        buf5 = buf3
        del buf3
        extern_kernels.bmm(reinterpret_tensor(arg0_1, (4, 4, 1), (4, 1, 1),
            0), reinterpret_tensor(arg2_1, (4, 1, 4), (4, 1, 1), 0), out=buf5)
        del arg0_1
        del arg2_1
        buf6 = empty_strided_cuda((4, 4, 21), (84, 21, 1), torch.float32)
        triton_poi_fused_div_exp_neg_pow_sub_sum_2[grid(336)](buf4, buf5,
            arg4_1, arg5_1, buf6, 336, XBLOCK=128, num_warps=4, num_stages=1)
        del arg4_1
        del arg5_1
        del buf4
        del buf5
        buf7 = empty_strided_cuda((4, 21), (21, 1), torch.float32)
        triton_poi_fused_clamp_log_mul_sum_3[grid(84)](buf6, buf7, 84,
            XBLOCK=128, num_warps=4, num_stages=1)
        del buf6
    return buf7,


class KernelMatcherNew(nn.Module):

    def __init__(self, embed_dim: 'int', kernel_num: 'int'=21) ->None:
        super(KernelMatcherNew, self).__init__()
        self._embed_dim = embed_dim
        self._kernel_num = kernel_num
        mus, sigmas = self.kernel_init(self._kernel_num)
        self._mus = nn.Parameter(mus, requires_grad=False)
        self._sigmas = nn.Parameter(sigmas, requires_grad=False)

    def kernel_init(self, kernel_num: 'int') ->Dict[str, torch.Tensor]:
        mus = [1]
        bin_size = 2.0 / (kernel_num - 1)
        mus.append(1 - bin_size / 2)
        for i in range(1, kernel_num - 1):
            mus.append(mus[i] - bin_size)
        mus = torch.tensor(mus).view(1, 1, 1, kernel_num)
        sigmas = [0.001]
        sigmas += [0.1] * (kernel_num - 1)
        sigmas = torch.tensor(sigmas).view(1, 1, 1, kernel_num)
        return mus, sigmas

    def forward(self, input_0, input_1, input_2, input_3):
        arg4_1 = self._mus
        arg5_1 = self._sigmas
        arg0_1 = input_0
        arg1_1 = input_1
        arg2_1 = input_2
        arg3_1 = input_3
        output = call([arg0_1, arg1_1, arg2_1, arg3_1, arg4_1, arg5_1])
        return output[0]
