# 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_8/inductor_cache/b7/cb7i4pf3kjpgs5yl6jdlrdjcmezn4u75kg4xt2hgdolfmosqezx4.py
# Topologically Sorted Source Nodes: [sub, pow_1, add, div, add_1, log, add_2, mul, exp_2, vaa, sub_2, pow_3, add_6, div_2, add_7, log_2, add_8, mul_2, exp_4, vbb, add_9, sub_1, pow_2, add_3, div_1, add_4, log_1, add_5, mul_1, exp_3, vab, mul_3, loss], Original ATen: [aten.sub, aten.pow, aten.add, aten.div, aten.log, aten.mul, aten.exp, aten.sum]
# Source node to ATen node mapping:
#   add => add
#   add_1 => add_1
#   add_2 => add_2
#   add_3 => add_3
#   add_4 => add_4
#   add_5 => add_5
#   add_6 => add_6
#   add_7 => add_7
#   add_8 => add_8
#   add_9 => add_9
#   div => div_2
#   div_1 => div_3
#   div_2 => div_4
#   exp_2 => exp_2
#   exp_3 => exp_3
#   exp_4 => exp_4
#   log => log
#   log_1 => log_1
#   log_2 => log_2
#   loss => sub_3
#   mul => mul
#   mul_1 => mul_1
#   mul_2 => mul_2
#   mul_3 => mul_3
#   pow_1 => pow_1
#   pow_2 => pow_2
#   pow_3 => pow_3
#   sub => sub
#   sub_1 => sub_1
#   sub_2 => sub_2
#   vaa => sum_1
#   vab => sum_2
#   vbb => sum_3
# Graph fragment:
#   %sub : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%view, %view_1), kwargs = {})
#   %pow_1 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sub, 2), kwargs = {})
#   %add : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%view_2, %view_3), kwargs = {})
#   %div_2 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%pow_1, %add), kwargs = {})
#   %add_1 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%view_2, %view_3), kwargs = {})
#   %log : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%add_1,), kwargs = {})
#   %add_2 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%div_2, %log), kwargs = {})
#   %mul : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%add_2, -0.5), kwargs = {})
#   %exp_2 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%mul,), kwargs = {})
#   %sum_1 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%exp_2,), kwargs = {})
#   %sub_2 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%view_4, %view_5), kwargs = {})
#   %pow_3 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sub_2, 2), kwargs = {})
#   %add_6 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%view_6, %view_7), kwargs = {})
#   %div_4 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%pow_3, %add_6), kwargs = {})
#   %add_7 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%view_6, %view_7), kwargs = {})
#   %log_2 : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%add_7,), kwargs = {})
#   %add_8 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%div_4, %log_2), kwargs = {})
#   %mul_2 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%add_8, -0.5), kwargs = {})
#   %exp_4 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%mul_2,), kwargs = {})
#   %sum_3 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%exp_4,), kwargs = {})
#   %add_9 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%sum_1, %sum_3), kwargs = {})
#   %sub_1 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%view, %view_5), kwargs = {})
#   %pow_2 : [num_users=1] = call_function[target=torch.ops.aten.pow.Tensor_Scalar](args = (%sub_1, 2), kwargs = {})
#   %add_3 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%view_2, %view_7), kwargs = {})
#   %div_3 : [num_users=1] = call_function[target=torch.ops.aten.div.Tensor](args = (%pow_2, %add_3), kwargs = {})
#   %add_4 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%view_2, %view_7), kwargs = {})
#   %log_1 : [num_users=1] = call_function[target=torch.ops.aten.log.default](args = (%add_4,), kwargs = {})
#   %add_5 : [num_users=1] = call_function[target=torch.ops.aten.add.Tensor](args = (%div_3, %log_1), kwargs = {})
#   %mul_1 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%add_5, -0.5), kwargs = {})
#   %exp_3 : [num_users=1] = call_function[target=torch.ops.aten.exp.default](args = (%mul_1,), kwargs = {})
#   %sum_2 : [num_users=1] = call_function[target=torch.ops.aten.sum.default](args = (%exp_3,), kwargs = {})
#   %mul_3 : [num_users=1] = call_function[target=torch.ops.aten.mul.Tensor](args = (%sum_2, 2.0), kwargs = {})
#   %sub_3 : [num_users=1] = call_function[target=torch.ops.aten.sub.Tensor](args = (%add_9, %mul_3), kwargs = {})
triton_per_fused_add_div_exp_log_mul_pow_sub_sum_0 = async_compile.triton('triton_per_fused_add_div_exp_log_mul_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, 1024],
    reduction_hint=ReductionHint.INNER,
    filename=__file__,
    triton_meta={'signature': {0: '*fp32', 1: '*fp32', 2: '*fp32', 3: '*fp32', 4: '*fp32', 5: 'i32', 6: 'i32'}, 'device': DeviceProperties(type='cuda', index=0, cc=80, major=8, regs_per_multiprocessor=65536, max_threads_per_multi_processor=2048, multi_processor_count=108), 'constants': {5: 1}, 'configs': [AttrsDescriptor(divisible_by_16=(0, 1, 2, 3, 4, 6), equal_to_1=(5,))]},
    inductor_meta={'autotune_hints': set(), 'kernel_name': 'triton_per_fused_add_div_exp_log_mul_pow_sub_sum_0', 'mutated_arg_names': ['in_out_ptr0'], 'no_x_dim': True, 'num_load': 8, 'num_reduction': 3, '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_log_mul_pow_sub_sum_0(in_out_ptr0, in_ptr0, in_ptr1, in_ptr2, in_ptr3, xnumel, rnumel):
    xnumel = 1
    XBLOCK: tl.constexpr = 1
    rnumel = 1024
    RBLOCK: tl.constexpr = 1024
    xoffset = tl.program_id(0) * XBLOCK
    xindex = tl.full([1], xoffset, tl.int32)
    xmask = tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    roffset = 0
    rmask = tl.full([RBLOCK], True, tl.int1)
    r0 = rindex % 64
    r2 = (rindex // 256)
    r3 = rindex % 256
    tmp0 = tl.load(in_ptr0 + (r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp3 = tl.load(in_ptr0 + (r3), None, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr1 + (r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp11 = tl.load(in_ptr1 + (r3), None, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr2 + (r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp27 = tl.load(in_ptr2 + (r3), None, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr3 + (r0 + (64*r2)), None, eviction_policy='evict_last')
    tmp35 = tl.load(in_ptr3 + (r3), None, eviction_policy='evict_last')
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3 * tmp1
    tmp5 = tmp2 - tmp4
    tmp6 = tmp5 * tmp5
    tmp8 = tl_math.exp(tmp7)
    tmp9 = tmp8 * tmp1
    tmp10 = tmp9 * tmp1
    tmp12 = tl_math.exp(tmp11)
    tmp13 = tmp12 * tmp1
    tmp14 = tmp13 * tmp1
    tmp15 = tmp10 + tmp14
    tmp16 = tmp6 / tmp15
    tmp17 = tl_math.log(tmp15)
    tmp18 = tmp16 + tmp17
    tmp19 = -0.5
    tmp20 = tmp18 * tmp19
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tl.broadcast_to(tmp21, [RBLOCK])
    tmp24 = triton_helpers.promote_to_tensor(tl.sum(tmp22, 0))
    tmp26 = tmp25 * tmp1
    tmp28 = tmp27 * tmp1
    tmp29 = tmp26 - tmp28
    tmp30 = tmp29 * tmp29
    tmp32 = tl_math.exp(tmp31)
    tmp33 = tmp32 * tmp1
    tmp34 = tmp33 * tmp1
    tmp36 = tl_math.exp(tmp35)
    tmp37 = tmp36 * tmp1
    tmp38 = tmp37 * tmp1
    tmp39 = tmp34 + tmp38
    tmp40 = tmp30 / tmp39
    tmp41 = tl_math.log(tmp39)
    tmp42 = tmp40 + tmp41
    tmp43 = tmp42 * tmp19
    tmp44 = tl_math.exp(tmp43)
    tmp45 = tl.broadcast_to(tmp44, [RBLOCK])
    tmp47 = triton_helpers.promote_to_tensor(tl.sum(tmp45, 0))
    tmp48 = tmp2 - tmp28
    tmp49 = tmp48 * tmp48
    tmp50 = tmp10 + tmp38
    tmp51 = tmp49 / tmp50
    tmp52 = tl_math.log(tmp50)
    tmp53 = tmp51 + tmp52
    tmp54 = tmp53 * tmp19
    tmp55 = tl_math.exp(tmp54)
    tmp56 = tl.broadcast_to(tmp55, [RBLOCK])
    tmp58 = triton_helpers.promote_to_tensor(tl.sum(tmp56, 0))
    tmp59 = tmp24 + tmp47
    tmp60 = 2.0
    tmp61 = tmp58 * tmp60
    tmp62 = tmp59 - tmp61
    tl.debug_barrier()
    tl.store(in_out_ptr0 + (tl.full([1], 0, tl.int32)), tmp62, None)
''', device_str='cuda')


async_compile.wait(globals())
del async_compile

def call(args):
    arg0_1, arg1_1, arg2_1, arg3_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))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf3 = buf0; del buf0  # reuse
        # Topologically Sorted Source Nodes: [sub, pow_1, add, div, add_1, log, add_2, mul, exp_2, vaa, sub_2, pow_3, add_6, div_2, add_7, log_2, add_8, mul_2, exp_4, vbb, add_9, sub_1, pow_2, add_3, div_1, add_4, log_1, add_5, mul_1, exp_3, vab, mul_3, loss], Original ATen: [aten.sub, aten.pow, aten.add, aten.div, aten.log, aten.mul, aten.exp, aten.sum]
        stream0 = get_raw_stream(0)
        triton_per_fused_add_div_exp_log_mul_pow_sub_sum_0.run(buf3, arg0_1, arg2_1, arg1_1, arg3_1, 1, 1024, grid=grid(1), stream=stream0)
        del arg0_1
        del arg1_1
        del arg2_1
        del arg3_1
    return (buf3, )


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)
    arg2_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    arg3_1 = rand_strided((4, 4, 4, 4), (64, 16, 4, 1), device='cuda:0', dtype=torch.float32)
    fn = lambda: call([arg0_1, arg1_1, arg2_1, arg3_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
import torch.nn as nn


class Gaussian_Distance(nn.Module):

    def __init__(self, kern=1):
        super(Gaussian_Distance, self).__init__()
        self.kern = kern
        self.avgpool = nn.AvgPool2d(kernel_size=kern, stride=kern)

    def forward(self, mu_a, logvar_a, mu_b, logvar_b):
        mu_a = self.avgpool(mu_a)
        mu_b = self.avgpool(mu_b)
        var_a = self.avgpool(torch.exp(logvar_a)) / (self.kern * self.kern)
        var_b = self.avgpool(torch.exp(logvar_b)) / (self.kern * self.kern)
        mu_a1 = mu_a.view(mu_a.size(0), 1, -1)
        mu_a2 = mu_a.view(1, mu_a.size(0), -1)
        var_a1 = var_a.view(var_a.size(0), 1, -1)
        var_a2 = var_a.view(1, var_a.size(0), -1)
        mu_b1 = mu_b.view(mu_b.size(0), 1, -1)
        mu_b2 = mu_b.view(1, mu_b.size(0), -1)
        var_b1 = var_b.view(var_b.size(0), 1, -1)
        var_b2 = var_b.view(1, var_b.size(0), -1)
        vaa = torch.sum(torch.exp(torch.mul(torch.add(torch.div(torch.pow(
            mu_a1 - mu_a2, 2), var_a1 + var_a2), torch.log(var_a1 + var_a2)
            ), -0.5)))
        vab = torch.sum(torch.exp(torch.mul(torch.add(torch.div(torch.pow(
            mu_a1 - mu_b2, 2), var_a1 + var_b2), torch.log(var_a1 + var_b2)
            ), -0.5)))
        vbb = torch.sum(torch.exp(torch.mul(torch.add(torch.div(torch.pow(
            mu_b1 - mu_b2, 2), var_b1 + var_b2), torch.log(var_b1 + var_b2)
            ), -0.5)))
        loss = vaa + vbb - torch.mul(vab, 2.0)
        return loss


def get_inputs():
    return [torch.rand([4, 4, 4, 4]), torch.rand([4, 4, 4, 4]), 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 math as tl_math
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


@triton.jit
def triton_per_fused_add_div_exp_log_mul_pow_sub_sum_0(in_out_ptr0, in_ptr0,
    in_ptr1, in_ptr2, in_ptr3, xnumel, rnumel):
    XBLOCK: tl.constexpr = 1
    RBLOCK: tl.constexpr = 1024
    xoffset = tl.program_id(0) * XBLOCK
    tl.full([1], xoffset, tl.int32)
    tl.full([RBLOCK], True, tl.int1)
    rindex = tl.arange(0, RBLOCK)[:]
    tl.full([RBLOCK], True, tl.int1)
    r0 = rindex % 64
    r2 = rindex // 256
    r3 = rindex % 256
    tmp0 = tl.load(in_ptr0 + (r0 + 64 * r2), None, eviction_policy='evict_last'
        )
    tmp3 = tl.load(in_ptr0 + r3, None, eviction_policy='evict_last')
    tmp7 = tl.load(in_ptr1 + (r0 + 64 * r2), None, eviction_policy='evict_last'
        )
    tmp11 = tl.load(in_ptr1 + r3, None, eviction_policy='evict_last')
    tmp25 = tl.load(in_ptr2 + (r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp27 = tl.load(in_ptr2 + r3, None, eviction_policy='evict_last')
    tmp31 = tl.load(in_ptr3 + (r0 + 64 * r2), None, eviction_policy=
        'evict_last')
    tmp35 = tl.load(in_ptr3 + r3, None, eviction_policy='evict_last')
    tmp1 = 1.0
    tmp2 = tmp0 * tmp1
    tmp4 = tmp3 * tmp1
    tmp5 = tmp2 - tmp4
    tmp6 = tmp5 * tmp5
    tmp8 = tl_math.exp(tmp7)
    tmp9 = tmp8 * tmp1
    tmp10 = tmp9 * tmp1
    tmp12 = tl_math.exp(tmp11)
    tmp13 = tmp12 * tmp1
    tmp14 = tmp13 * tmp1
    tmp15 = tmp10 + tmp14
    tmp16 = tmp6 / tmp15
    tmp17 = tl_math.log(tmp15)
    tmp18 = tmp16 + tmp17
    tmp19 = -0.5
    tmp20 = tmp18 * tmp19
    tmp21 = tl_math.exp(tmp20)
    tmp22 = tl.broadcast_to(tmp21, [RBLOCK])
    tmp24 = triton_helpers.promote_to_tensor(tl.sum(tmp22, 0))
    tmp26 = tmp25 * tmp1
    tmp28 = tmp27 * tmp1
    tmp29 = tmp26 - tmp28
    tmp30 = tmp29 * tmp29
    tmp32 = tl_math.exp(tmp31)
    tmp33 = tmp32 * tmp1
    tmp34 = tmp33 * tmp1
    tmp36 = tl_math.exp(tmp35)
    tmp37 = tmp36 * tmp1
    tmp38 = tmp37 * tmp1
    tmp39 = tmp34 + tmp38
    tmp40 = tmp30 / tmp39
    tmp41 = tl_math.log(tmp39)
    tmp42 = tmp40 + tmp41
    tmp43 = tmp42 * tmp19
    tmp44 = tl_math.exp(tmp43)
    tmp45 = tl.broadcast_to(tmp44, [RBLOCK])
    tmp47 = triton_helpers.promote_to_tensor(tl.sum(tmp45, 0))
    tmp48 = tmp2 - tmp28
    tmp49 = tmp48 * tmp48
    tmp50 = tmp10 + tmp38
    tmp51 = tmp49 / tmp50
    tmp52 = tl_math.log(tmp50)
    tmp53 = tmp51 + tmp52
    tmp54 = tmp53 * tmp19
    tmp55 = tl_math.exp(tmp54)
    tmp56 = tl.broadcast_to(tmp55, [RBLOCK])
    tmp58 = triton_helpers.promote_to_tensor(tl.sum(tmp56, 0))
    tmp59 = tmp24 + tmp47
    tmp60 = 2.0
    tmp61 = tmp58 * tmp60
    tmp62 = tmp59 - tmp61
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([1], 0, tl.int32), tmp62, None)


def call(args):
    arg0_1, arg1_1, arg2_1, arg3_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))
    assert_size_stride(arg2_1, (4, 4, 4, 4), (64, 16, 4, 1))
    assert_size_stride(arg3_1, (4, 4, 4, 4), (64, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((), (), torch.float32)
        buf3 = buf0
        del buf0
        get_raw_stream(0)
        triton_per_fused_add_div_exp_log_mul_pow_sub_sum_0[grid(1)](buf3,
            arg0_1, arg2_1, arg1_1, arg3_1, 1, 1024, num_warps=8, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
        del arg3_1
    return buf3,


class Gaussian_DistanceNew(nn.Module):

    def __init__(self, kern=1):
        super(Gaussian_DistanceNew, self).__init__()
        self.kern = kern
        self.avgpool = nn.AvgPool2d(kernel_size=kern, stride=kern)

    def forward(self, input_0, input_1, input_2, input_3):
        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])
        return output[0]
