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]


Measured latency_ms: 0.027381