# 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/zd/czdd3hvknhxzafinmrlltncrckeidw5p7kov7vpp4uyi35drhhfl.py
# Topologically Sorted Source Nodes: [sub, pow_1, L2_distance, neg, sum_2, bandwidth, bandwidth_1, bandwidth_temp, truediv_1, exp, add, neg_1, bandwidth_temp_1, truediv_2, exp_1, add_1, neg_2, bandwidth_temp_2, truediv_3, exp_2, add_2, neg_3, bandwidth_temp_3, truediv_4, exp_3, add_3, neg_4, bandwidth_temp_4, truediv_5, exp_4, kernels], Original ATen: [aten.sub, aten.pow, aten.sum, aten.neg, aten.div, aten.mul, aten.exp, aten.add]
# Source node to ATen node mapping:
#   L2_distance => sum_1
#   add => add
#   add_1 => add_1
#   add_2 => add_2
#   add_3 => add_3
#   bandwidth => div
#   bandwidth_1 => div_1
#   bandwidth_temp => mul
#   bandwidth_temp_1 => mul_1
#   bandwidth_temp_2 => mul_2
#   bandwidth_temp_3 => mul_3
#   bandwidth_temp_4 => mul_4
#   exp => exp
#   exp_1 => exp_1
#   exp_2 => exp_2
#   exp_3 => exp_3
#   exp_4 => exp_4
#   kernels => add_4
#   neg => neg
#   neg_1 => neg_1
#   neg_2 => neg_2
#   neg_3 => neg_3
#   neg_4 => neg_4
#   pow_1 => pow_1
#   sub => sub
#   sum_2 => sum_2
#   truediv_1 => div_2
#   truediv_2 => div_3
#   truediv_3 => div_4
#   truediv_4 => div_5
#   truediv_5 => div_6
# Graph fragment:
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%expand, %expand_1), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sub, 2), kwargs = {})
#   %sum_1 : [num_users=6] = call_function[target=torch.ops.aten.sum.dim_IntList](args = (%pow_1, [2]), kwargs = {})
#   %neg : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%sum_1,), kwargs = {})
#   %sum_2 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%sum_1,), kwargs = {})
#   %div : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%sum_2, 56), kwargs = {})
#   %div_1 : [num_users=5] = call_function[target=torch.ops.aten.div.Tensor](args = (%div, 4.0), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_1, 1.0), kwargs = {})
#   %div_2 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%neg, %mul), kwargs = {})
#   %exp : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%div_2,), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%exp, 0), kwargs = {})
#   %neg_1 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%sum_1,), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_1, 2.0), kwargs = {})
#   %div_3 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%neg_1, %mul_1), kwargs = {})
#   %exp_1 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%div_3,), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add, %exp_1), kwargs = {})
#   %neg_2 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%sum_1,), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_1, 4.0), kwargs = {})
#   %div_4 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%neg_2, %mul_2), kwargs = {})
#   %exp_2 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%div_4,), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_1, %exp_2), kwargs = {})
#   %neg_3 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%sum_1,), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_1, 8.0), kwargs = {})
#   %div_5 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%neg_3, %mul_3), kwargs = {})
#   %exp_3 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%div_5,), kwargs = {})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_2, %exp_3), kwargs = {})
#   %neg_4 : [num_users=1] = call_function[target=torch.ops.aten.neg.default](args = (%sum_1,), kwargs = {})
#   %mul_4 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%div_1, 16.0), kwargs = {})
#   %div_6 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%neg_4, %mul_4), kwargs = {})
#   %exp_4 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%div_6,), kwargs = {})
#   %add_4 : [num_users=4] = call_function[target=torch.ops.aten.add.Tensor](args = (%add_3, %exp_4), kwargs = {})
triton_per_fused_add_div_exp_mul_neg_pow_sub_sum_0 = async_compile.triton('triton_per_fused_add_div_exp_mul_neg_pow_sub_sum_0', '''
import triton
import triton.language as tl
from triton.compiler.compiler import AttrsDescriptor

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

@triton_heuristics.persistent_reduction(
    size_hints=[1, 64],
    reduction_hint=ReductionHint.INNER,
    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': {3: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 4), equal_to_1=(3,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_add_div_exp_mul_neg_pow_sub_sum_0', 'mutated_arg_names': [], 'no_x_dim': False, 'num_load': 16, '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_div_exp_mul_neg_pow_sub_sum_0(in_ptr0, in_ptr1, out_ptr2, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 1
    rnumel = 64
    RBLOCK: tl.constexpr = 64
    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 % 8
    r1 = (rindex // 8)
    r2 = rindex
    tmp0 = r0
    tmp1 = tl.full([1, 1], 0, tl.int64)
    tmp2 = tmp0 >= tmp1
    tmp3 = tl.full([1, 1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (tl.broadcast_to(4*r0, [XBLOCK, RBLOCK])), tmp4, eviction_policy='evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tmp7 = tl.full([1, 1], 8, tl.int64)
    tmp8 = tmp0 < tmp7
    tmp9 = tl.load(in_ptr1 + (tl.broadcast_to(4*((-4) + r0), [XBLOCK, RBLOCK])), tmp6, eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp11 = r1
    tmp12 = tmp11 >= tmp1
    tmp13 = tmp11 < tmp3
    tmp14 = tl.load(in_ptr0 + (tl.broadcast_to(4*r1, [XBLOCK, RBLOCK])), tmp13, eviction_policy='evict_last', other=0.0)
    tmp15 = tmp11 >= tmp3
    tmp16 = tmp11 < tmp7
    tmp17 = tl.load(in_ptr1 + (tl.broadcast_to(4*((-4) + r1), [XBLOCK, RBLOCK])), tmp15, eviction_policy='evict_last', other=0.0)
    tmp18 = tl.where(tmp13, tmp14, tmp17)
    tmp19 = tmp10 - tmp18
    tmp20 = tmp19 * tmp19
    tmp21 = tl.load(in_ptr0 + (tl.broadcast_to(1 + (4*r0), [XBLOCK, RBLOCK])), tmp4, eviction_policy='evict_last', other=0.0)
    tmp22 = tl.load(in_ptr1 + (tl.broadcast_to(1 + (4*((-4) + r0)), [XBLOCK, RBLOCK])), tmp6, eviction_policy='evict_last', other=0.0)
    tmp23 = tl.where(tmp4, tmp21, tmp22)
    tmp24 = tl.load(in_ptr0 + (tl.broadcast_to(1 + (4*r1), [XBLOCK, RBLOCK])), tmp13, eviction_policy='evict_last', other=0.0)
    tmp25 = tl.load(in_ptr1 + (tl.broadcast_to(1 + (4*((-4) + r1)), [XBLOCK, RBLOCK])), tmp15, eviction_policy='evict_last', other=0.0)
    tmp26 = tl.where(tmp13, tmp24, tmp25)
    tmp27 = tmp23 - tmp26
    tmp28 = tmp27 * tmp27
    tmp29 = tmp20 + tmp28
    tmp30 = tl.load(in_ptr0 + (tl.broadcast_to(2 + (4*r0), [XBLOCK, RBLOCK])), tmp4, eviction_policy='evict_last', other=0.0)
    tmp31 = tl.load(in_ptr1 + (tl.broadcast_to(2 + (4*((-4) + r0)), [XBLOCK, RBLOCK])), tmp6, eviction_policy='evict_last', other=0.0)
    tmp32 = tl.where(tmp4, tmp30, tmp31)
    tmp33 = tl.load(in_ptr0 + (tl.broadcast_to(2 + (4*r1), [XBLOCK, RBLOCK])), tmp13, eviction_policy='evict_last', other=0.0)
    tmp34 = tl.load(in_ptr1 + (tl.broadcast_to(2 + (4*((-4) + r1)), [XBLOCK, RBLOCK])), tmp15, eviction_policy='evict_last', other=0.0)
    tmp35 = tl.where(tmp13, tmp33, tmp34)
    tmp36 = tmp32 - tmp35
    tmp37 = tmp36 * tmp36
    tmp38 = tmp29 + tmp37
    tmp39 = tl.load(in_ptr0 + (tl.broadcast_to(3 + (4*r0), [XBLOCK, RBLOCK])), tmp4, eviction_policy='evict_last', other=0.0)
    tmp40 = tl.load(in_ptr1 + (tl.broadcast_to(3 + (4*((-4) + r0)), [XBLOCK, RBLOCK])), tmp6, eviction_policy='evict_last', other=0.0)
    tmp41 = tl.where(tmp4, tmp39, tmp40)
    tmp42 = tl.load(in_ptr0 + (tl.broadcast_to(3 + (4*r1), [XBLOCK, RBLOCK])), tmp13, eviction_policy='evict_last', other=0.0)
    tmp43 = tl.load(in_ptr1 + (tl.broadcast_to(3 + (4*((-4) + r1)), [XBLOCK, RBLOCK])), tmp15, eviction_policy='evict_last', other=0.0)
    tmp44 = tl.where(tmp13, tmp42, tmp43)
    tmp45 = tmp41 - tmp44
    tmp46 = tmp45 * tmp45
    tmp47 = tmp38 + tmp46
    tmp48 = tl.broadcast_to(tmp47, [XBLOCK, RBLOCK])
    tmp50 = tl.sum(tmp48, 1)[:, None]
    tmp51 = -tmp47
    tmp52 = 0.017857142857142856
    tmp53 = tmp50 * tmp52
    tmp54 = 0.25
    tmp55 = tmp53 * tmp54
    tmp56 = 1.0
    tmp57 = tmp55 * tmp56
    tmp58 = tmp51 / tmp57
    tmp59 = tl_math.exp(tmp58)
    tmp60 = 0.0
    tmp61 = tmp59 + tmp60
    tmp62 = 2.0
    tmp63 = tmp55 * tmp62
    tmp64 = tmp51 / tmp63
    tmp65 = tl_math.exp(tmp64)
    tmp66 = tmp61 + tmp65
    tmp67 = 4.0
    tmp68 = tmp55 * tmp67
    tmp69 = tmp51 / tmp68
    tmp70 = tl_math.exp(tmp69)
    tmp71 = tmp66 + tmp70
    tmp72 = 8.0
    tmp73 = tmp55 * tmp72
    tmp74 = tmp51 / tmp73
    tmp75 = tl_math.exp(tmp74)
    tmp76 = tmp71 + tmp75
    tmp77 = 16.0
    tmp78 = tmp55 * tmp77
    tmp79 = tmp51 / tmp78
    tmp80 = tl_math.exp(tmp79)
    tmp81 = tmp76 + tmp80
    tl.store(out_ptr2 + (tl.broadcast_to(r2, [XBLOCK, RBLOCK])), tmp81, None)
''', device_str='cuda')


# kernel path: runs/run_shard_4/inductor_cache/jy/cjyqfq5gwwuff77mju24f262rapbhsztxnaf2jirrfie5og56rao.py
# Topologically Sorted Source Nodes: [XX, YY, add_5, XY, sub_1, YX, loss], Original ATen: [aten.mean, aten.add, aten.sub]
# Source node to ATen node mapping:
#   XX => mean
#   XY => mean_2
#   YX => mean_3
#   YY => mean_1
#   add_5 => add_5
#   loss => sub_2
#   sub_1 => sub_1
# Graph fragment:
#   %mean : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%slice_2,), kwargs = {})
#   %mean_1 : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%slice_4,), kwargs = {})
#   %add_5 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%mean, %mean_1), kwargs = {})
#   %mean_2 : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%slice_6,), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%add_5, %mean_2), kwargs = {})
#   %mean_3 : [num_users=1] = call_function[target=torch.ops.aten.mean.default](args = (%slice_8,), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%sub_1, %mean_3), kwargs = {})
triton_per_fused_add_mean_sub_1 = async_compile.triton('triton_per_fused_add_mean_sub_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.persistent_reduction(
    size_hints=[1, 16],
    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_add_mean_sub_1', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': False, 'num_load': 4, 'num_reduction': 4, '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_mean_sub_1(in_out_ptr0, in_ptr0, xnumel, rnumel, XBLOCK : tl.constexpr):
    xnumel = 1
    rnumel = 16
    RBLOCK: tl.constexpr = 16
    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 % 4
    r1 = (rindex // 4)
    tmp0 = tl.load(in_ptr0 + (r0 + (8*r1)), None)
    tmp4 = tl.load(in_ptr0 + (36 + r0 + (8*r1)), None)
    tmp8 = tl.load(in_ptr0 + (4 + r0 + (8*r1)), None)
    tmp12 = tl.load(in_ptr0 + (32 + r0 + (8*r1)), None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp7 = tl.sum(tmp5, 1)[:, None]
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.sum(tmp9, 1)[:, None]
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.sum(tmp13, 1)[:, None]
    tmp16 = 16.0
    tmp17 = tmp3 / tmp16
    tmp18 = tmp7 / tmp16
    tmp19 = tmp17 + tmp18
    tmp20 = tmp11 / tmp16
    tmp21 = tmp19 - tmp20
    tmp22 = tmp15 / tmp16
    tmp23 = tmp21 - tmp22
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([XBLOCK, 1], 0, tl.int32)), tmp23, 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, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf2 = empty_strided_cuda((8, 8), (8, 1), torch.float32)
        # Topologically Sorted Source Nodes: [sub, pow_1, L2_distance, neg, sum_2, bandwidth, bandwidth_1, bandwidth_temp, truediv_1, exp, add, neg_1, bandwidth_temp_1, truediv_2, exp_1, add_1, neg_2, bandwidth_temp_2, truediv_3, exp_2, add_2, neg_3, bandwidth_temp_3, truediv_4, exp_3, add_3, neg_4, bandwidth_temp_4, truediv_5, exp_4, kernels], Original ATen: [aten.sub, aten.pow, aten.sum, aten.neg, aten.div, aten.mul, aten.exp, aten.add]
        stream0 = get_raw_stream(0)
        triton_per_fused_add_div_exp_mul_neg_pow_sub_sum_0.run(arg0_1, arg1_1, buf2, 1, 64, grid=grid(1), stream=stream0)
        del arg0_1
        del arg1_1
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf7 = buf3; del buf3  # reuse
        # Topologically Sorted Source Nodes: [XX, YY, add_5, XY, sub_1, YX, loss], Original ATen: [aten.mean, aten.add, aten.sub]
        triton_per_fused_add_mean_sub_1.run(buf7, buf2, 1, 16, grid=grid(1), stream=stream0)
        del buf2
    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)
    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 torch import nn


class MMD(nn.Module):

    def __init__(self):
        super().__init__()

    def _guassian_kernel(self, source, target, kernel_mul=2.0, kernel_num=5,
        fix_sigma=None):
        n_samples = int(source.size()[0]) + int(target.size()[0])
        total = torch.cat([source, target], dim=0)
        total0 = total.unsqueeze(0).expand(int(total.size(0)), int(total.
            size(0)), int(total.size(1)))
        total1 = total.unsqueeze(1).expand(int(total.size(0)), int(total.
            size(0)), int(total.size(1)))
        L2_distance = ((total0 - total1) ** 2).sum(2)
        if fix_sigma:
            bandwidth = fix_sigma
        else:
            bandwidth = torch.sum(L2_distance.data) / (n_samples ** 2 -
                n_samples)
        bandwidth /= kernel_mul ** (kernel_num // 2)
        bandwidth_list = [(bandwidth * kernel_mul ** i) for i in range(
            kernel_num)]
        kernel_val = [torch.exp(-L2_distance / bandwidth_temp) for
            bandwidth_temp in bandwidth_list]
        return sum(kernel_val)

    def forward(self, source, target, kernel_mul=2.0, kernel_num=5,
        fix_sigma=None):
        source_num = int(source.size()[0])
        target_num = int(target.size()[0])
        kernels = self._guassian_kernel(source, target, kernel_mul=
            kernel_mul, kernel_num=kernel_num, fix_sigma=fix_sigma)
        XX = torch.mean(kernels[:source_num, :source_num])
        YY = torch.mean(kernels[target_num:, target_num:])
        XY = torch.mean(kernels[:target_num, source_num:])
        YX = torch.mean(kernels[source_num:, :target_num])
        loss = XX + YY - XY - YX
        return loss

    def get_parameters(self):
        return [{'params': self.parameters(), 'lr_mult': 10}]


def get_inputs():
    return [torch.rand([4, 4]), torch.rand([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.triton_helpers import math as tl_math
from torch import nn
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_per_fused_add_div_exp_mul_neg_pow_sub_sum_0(in_ptr0, in_ptr1,
    out_ptr2, xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 64
    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 % 8
    r1 = rindex // 8
    r2 = rindex
    tmp0 = r0
    tl.full([1, 1], 0, tl.int64)
    tmp3 = tl.full([1, 1], 4, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + tl.broadcast_to(4 * r0, [XBLOCK, RBLOCK]),
        tmp4, eviction_policy='evict_last', other=0.0)
    tmp6 = tmp0 >= tmp3
    tl.full([1, 1], 8, tl.int64)
    tmp9 = tl.load(in_ptr1 + tl.broadcast_to(4 * (-4 + r0), [XBLOCK, RBLOCK
        ]), tmp6, eviction_policy='evict_last', other=0.0)
    tmp10 = tl.where(tmp4, tmp5, tmp9)
    tmp11 = r1
    tmp13 = tmp11 < tmp3
    tmp14 = tl.load(in_ptr0 + tl.broadcast_to(4 * r1, [XBLOCK, RBLOCK]),
        tmp13, eviction_policy='evict_last', other=0.0)
    tmp15 = tmp11 >= tmp3
    tmp17 = tl.load(in_ptr1 + tl.broadcast_to(4 * (-4 + r1), [XBLOCK,
        RBLOCK]), tmp15, eviction_policy='evict_last', other=0.0)
    tmp18 = tl.where(tmp13, tmp14, tmp17)
    tmp19 = tmp10 - tmp18
    tmp20 = tmp19 * tmp19
    tmp21 = tl.load(in_ptr0 + tl.broadcast_to(1 + 4 * r0, [XBLOCK, RBLOCK]),
        tmp4, eviction_policy='evict_last', other=0.0)
    tmp22 = tl.load(in_ptr1 + tl.broadcast_to(1 + 4 * (-4 + r0), [XBLOCK,
        RBLOCK]), tmp6, eviction_policy='evict_last', other=0.0)
    tmp23 = tl.where(tmp4, tmp21, tmp22)
    tmp24 = tl.load(in_ptr0 + tl.broadcast_to(1 + 4 * r1, [XBLOCK, RBLOCK]),
        tmp13, eviction_policy='evict_last', other=0.0)
    tmp25 = tl.load(in_ptr1 + tl.broadcast_to(1 + 4 * (-4 + r1), [XBLOCK,
        RBLOCK]), tmp15, eviction_policy='evict_last', other=0.0)
    tmp26 = tl.where(tmp13, tmp24, tmp25)
    tmp27 = tmp23 - tmp26
    tmp28 = tmp27 * tmp27
    tmp29 = tmp20 + tmp28
    tmp30 = tl.load(in_ptr0 + tl.broadcast_to(2 + 4 * r0, [XBLOCK, RBLOCK]),
        tmp4, eviction_policy='evict_last', other=0.0)
    tmp31 = tl.load(in_ptr1 + tl.broadcast_to(2 + 4 * (-4 + r0), [XBLOCK,
        RBLOCK]), tmp6, eviction_policy='evict_last', other=0.0)
    tmp32 = tl.where(tmp4, tmp30, tmp31)
    tmp33 = tl.load(in_ptr0 + tl.broadcast_to(2 + 4 * r1, [XBLOCK, RBLOCK]),
        tmp13, eviction_policy='evict_last', other=0.0)
    tmp34 = tl.load(in_ptr1 + tl.broadcast_to(2 + 4 * (-4 + r1), [XBLOCK,
        RBLOCK]), tmp15, eviction_policy='evict_last', other=0.0)
    tmp35 = tl.where(tmp13, tmp33, tmp34)
    tmp36 = tmp32 - tmp35
    tmp37 = tmp36 * tmp36
    tmp38 = tmp29 + tmp37
    tmp39 = tl.load(in_ptr0 + tl.broadcast_to(3 + 4 * r0, [XBLOCK, RBLOCK]),
        tmp4, eviction_policy='evict_last', other=0.0)
    tmp40 = tl.load(in_ptr1 + tl.broadcast_to(3 + 4 * (-4 + r0), [XBLOCK,
        RBLOCK]), tmp6, eviction_policy='evict_last', other=0.0)
    tmp41 = tl.where(tmp4, tmp39, tmp40)
    tmp42 = tl.load(in_ptr0 + tl.broadcast_to(3 + 4 * r1, [XBLOCK, RBLOCK]),
        tmp13, eviction_policy='evict_last', other=0.0)
    tmp43 = tl.load(in_ptr1 + tl.broadcast_to(3 + 4 * (-4 + r1), [XBLOCK,
        RBLOCK]), tmp15, eviction_policy='evict_last', other=0.0)
    tmp44 = tl.where(tmp13, tmp42, tmp43)
    tmp45 = tmp41 - tmp44
    tmp46 = tmp45 * tmp45
    tmp47 = tmp38 + tmp46
    tmp48 = tl.broadcast_to(tmp47, [XBLOCK, RBLOCK])
    tmp50 = tl.sum(tmp48, 1)[:, None]
    tmp51 = -tmp47
    tmp52 = 0.017857142857142856
    tmp53 = tmp50 * tmp52
    tmp54 = 0.25
    tmp55 = tmp53 * tmp54
    tmp56 = 1.0
    tmp57 = tmp55 * tmp56
    tmp58 = tmp51 / tmp57
    tmp59 = tl_math.exp(tmp58)
    tmp60 = 0.0
    tmp61 = tmp59 + tmp60
    tmp62 = 2.0
    tmp63 = tmp55 * tmp62
    tmp64 = tmp51 / tmp63
    tmp65 = tl_math.exp(tmp64)
    tmp66 = tmp61 + tmp65
    tmp67 = 4.0
    tmp68 = tmp55 * tmp67
    tmp69 = tmp51 / tmp68
    tmp70 = tl_math.exp(tmp69)
    tmp71 = tmp66 + tmp70
    tmp72 = 8.0
    tmp73 = tmp55 * tmp72
    tmp74 = tmp51 / tmp73
    tmp75 = tl_math.exp(tmp74)
    tmp76 = tmp71 + tmp75
    tmp77 = 16.0
    tmp78 = tmp55 * tmp77
    tmp79 = tmp51 / tmp78
    tmp80 = tl_math.exp(tmp79)
    tmp81 = tmp76 + tmp80
    tl.store(out_ptr2 + tl.broadcast_to(r2, [XBLOCK, RBLOCK]), tmp81, None)


@triton.jit
def triton_per_fused_add_mean_sub_1(in_out_ptr0, in_ptr0, xnumel, rnumel,
    XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 16
    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 % 4
    r1 = rindex // 4
    tmp0 = tl.load(in_ptr0 + (r0 + 8 * r1), None)
    tmp4 = tl.load(in_ptr0 + (36 + r0 + 8 * r1), None)
    tmp8 = tl.load(in_ptr0 + (4 + r0 + 8 * r1), None)
    tmp12 = tl.load(in_ptr0 + (32 + r0 + 8 * r1), None)
    tmp1 = tl.broadcast_to(tmp0, [XBLOCK, RBLOCK])
    tmp3 = tl.sum(tmp1, 1)[:, None]
    tmp5 = tl.broadcast_to(tmp4, [XBLOCK, RBLOCK])
    tmp7 = tl.sum(tmp5, 1)[:, None]
    tmp9 = tl.broadcast_to(tmp8, [XBLOCK, RBLOCK])
    tmp11 = tl.sum(tmp9, 1)[:, None]
    tmp13 = tl.broadcast_to(tmp12, [XBLOCK, RBLOCK])
    tmp15 = tl.sum(tmp13, 1)[:, None]
    tmp16 = 16.0
    tmp17 = tmp3 / tmp16
    tmp18 = tmp7 / tmp16
    tmp19 = tmp17 + tmp18
    tmp20 = tmp11 / tmp16
    tmp21 = tmp19 - tmp20
    tmp22 = tmp15 / tmp16
    tmp23 = tmp21 - tmp22
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp23, None)


def call(args):
    arg0_1, arg1_1 = args
    args.clear()
    assert_size_stride(arg0_1, (4, 4), (4, 1))
    assert_size_stride(arg1_1, (4, 4), (4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf2 = empty_strided_cuda((8, 8), (8, 1), torch.float32)
        get_raw_stream(0)
        triton_per_fused_add_div_exp_mul_neg_pow_sub_sum_0[grid(1)](arg0_1,
            arg1_1, buf2, 1, 64, XBLOCK=1, num_warps=2, num_stages=1)
        del arg0_1
        del arg1_1
        buf3 = empty_strided_cuda((), (), torch.float32)
        buf7 = buf3
        del buf3
        triton_per_fused_add_mean_sub_1[grid(1)](buf7, buf2, 1, 16, XBLOCK=
            1, num_warps=2, num_stages=1)
        del buf2
    return buf7,


class MMDNew(nn.Module):

    def __init__(self):
        super().__init__()

    def _guassian_kernel(self, source, target, kernel_mul=2.0, kernel_num=5,
        fix_sigma=None):
        n_samples = int(source.size()[0]) + int(target.size()[0])
        total = torch.cat([source, target], dim=0)
        total0 = total.unsqueeze(0).expand(int(total.size(0)), int(total.
            size(0)), int(total.size(1)))
        total1 = total.unsqueeze(1).expand(int(total.size(0)), int(total.
            size(0)), int(total.size(1)))
        L2_distance = ((total0 - total1) ** 2).sum(2)
        if fix_sigma:
            bandwidth = fix_sigma
        else:
            bandwidth = torch.sum(L2_distance.data) / (n_samples ** 2 -
                n_samples)
        bandwidth /= kernel_mul ** (kernel_num // 2)
        bandwidth_list = [(bandwidth * kernel_mul ** i) for i in range(
            kernel_num)]
        kernel_val = [torch.exp(-L2_distance / bandwidth_temp) for
            bandwidth_temp in bandwidth_list]
        return sum(kernel_val)

    def get_parameters(self):
        return [{'params': self.parameters(), 'lr_mult': 10}]

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