import torch
from torch import nn
from torch.optim.lr_scheduler import *


class BridgeFeatLoss(nn.Module):

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

    def forward(self, feats_s, feats_t, feats_mixed, lam):
        dist_mixed2s = ((feats_mixed - feats_s) ** 2).sum(1, keepdim=True)
        dist_mixed2t = ((feats_mixed - feats_t) ** 2).sum(1, keepdim=True)
        dist_mixed2s = dist_mixed2s.clamp(min=1e-12).sqrt()
        dist_mixed2t = dist_mixed2t.clamp(min=1e-12).sqrt()
        dist_mixed = torch.cat((dist_mixed2s, dist_mixed2t), 1)
        lam_dist_mixed = (lam * dist_mixed).sum(1, keepdim=True)
        loss = lam_dist_mixed.mean()
        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, 2, 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
from torch import nn
from torch.optim.lr_scheduler import *
assert_size_stride = torch._C._dynamo.guards.assert_size_stride
empty_strided_cuda = torch._C._dynamo.guards._empty_strided_cuda


@triton.jit
def triton_poi_fused_cat_0(in_ptr0, in_ptr1, in_ptr2, out_ptr0, xnumel,
    XBLOCK: tl.constexpr):
    xnumel = 128
    xoffset = tl.program_id(0) * XBLOCK
    xindex = xoffset + tl.arange(0, XBLOCK)[:]
    xmask = xindex < xnumel
    x1 = xindex // 16 % 2
    x0 = xindex % 16
    x2 = xindex // 32
    x3 = xindex
    tmp0 = x1
    tl.full([1], 0, tl.int64)
    tmp3 = tl.full([1], 1, tl.int64)
    tmp4 = tmp0 < tmp3
    tmp5 = tl.load(in_ptr0 + (x0 + 64 * x2), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp6 = tl.load(in_ptr1 + (x0 + 64 * x2), tmp4 & xmask, eviction_policy=
        'evict_last', other=0.0)
    tmp7 = tmp5 - tmp6
    tmp8 = tmp7 * tmp7
    tmp9 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp10 = tl.load(in_ptr1 + (16 + x0 + 64 * x2), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp11 = tmp9 - tmp10
    tmp12 = tmp11 * tmp11
    tmp13 = tmp8 + tmp12
    tmp14 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp15 = tl.load(in_ptr1 + (32 + x0 + 64 * x2), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp16 = tmp14 - tmp15
    tmp17 = tmp16 * tmp16
    tmp18 = tmp13 + tmp17
    tmp19 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp20 = tl.load(in_ptr1 + (48 + x0 + 64 * x2), tmp4 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp21 = tmp19 - tmp20
    tmp22 = tmp21 * tmp21
    tmp23 = tmp18 + tmp22
    tmp24 = 1e-12
    tmp25 = triton_helpers.maximum(tmp23, tmp24)
    tmp26 = libdevice.sqrt(tmp25)
    tmp27 = tl.full(tmp26.shape, 0.0, tmp26.dtype)
    tmp28 = tl.where(tmp4, tmp26, tmp27)
    tmp29 = tmp0 >= tmp3
    tl.full([1], 2, tl.int64)
    tmp32 = tl.load(in_ptr0 + (x0 + 64 * x2), tmp29 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp33 = tl.load(in_ptr2 + (x0 + 64 * x2), tmp29 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp34 = tmp32 - tmp33
    tmp35 = tmp34 * tmp34
    tmp36 = tl.load(in_ptr0 + (16 + x0 + 64 * x2), tmp29 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp37 = tl.load(in_ptr2 + (16 + x0 + 64 * x2), tmp29 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp38 = tmp36 - tmp37
    tmp39 = tmp38 * tmp38
    tmp40 = tmp35 + tmp39
    tmp41 = tl.load(in_ptr0 + (32 + x0 + 64 * x2), tmp29 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp42 = tl.load(in_ptr2 + (32 + x0 + 64 * x2), tmp29 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp43 = tmp41 - tmp42
    tmp44 = tmp43 * tmp43
    tmp45 = tmp40 + tmp44
    tmp46 = tl.load(in_ptr0 + (48 + x0 + 64 * x2), tmp29 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp47 = tl.load(in_ptr2 + (48 + x0 + 64 * x2), tmp29 & xmask,
        eviction_policy='evict_last', other=0.0)
    tmp48 = tmp46 - tmp47
    tmp49 = tmp48 * tmp48
    tmp50 = tmp45 + tmp49
    tmp51 = triton_helpers.maximum(tmp50, tmp24)
    tmp52 = libdevice.sqrt(tmp51)
    tmp53 = tl.full(tmp52.shape, 0.0, tmp52.dtype)
    tmp54 = tl.where(tmp29, tmp52, tmp53)
    tmp55 = tl.where(tmp4, tmp28, tmp54)
    tl.store(out_ptr0 + x3, tmp55, xmask)


@triton.jit
def triton_per_fused_mean_mul_sum_1(in_out_ptr0, in_ptr0, in_ptr1, 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 % 16
    r1 = rindex // 16
    tmp0 = tl.load(in_ptr0 + (r0 + 32 * r1), None)
    tmp1 = tl.load(in_ptr1 + (r0 + 32 * r1), None)
    tmp3 = tl.load(in_ptr0 + (16 + r0 + 32 * r1), None)
    tmp4 = tl.load(in_ptr1 + (16 + r0 + 32 * r1), None)
    tmp2 = tmp0 * tmp1
    tmp5 = tmp3 * tmp4
    tmp6 = tmp2 + tmp5
    tmp7 = tl.broadcast_to(tmp6, [XBLOCK, RBLOCK])
    tmp9 = tl.sum(tmp7, 1)[:, None]
    tmp10 = 64.0
    tmp11 = tmp9 / tmp10
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp11, 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, 2, 4, 4), (32, 16, 4, 1))
    with torch.cuda._DeviceGuard(0):
        torch.cuda.set_device(0)
        buf0 = empty_strided_cuda((4, 2, 4, 4), (32, 16, 4, 1), torch.float32)
        get_raw_stream(0)
        triton_poi_fused_cat_0[grid(128)](arg0_1, arg1_1, arg2_1, buf0, 128,
            XBLOCK=128, num_warps=4, num_stages=1)
        del arg0_1
        del arg1_1
        del arg2_1
        buf1 = empty_strided_cuda((), (), torch.float32)
        buf2 = buf1
        del buf1
        triton_per_fused_mean_mul_sum_1[grid(1)](buf2, arg3_1, buf0, 1, 64,
            XBLOCK=1, num_warps=2, num_stages=1)
        del arg3_1
        del buf0
    return buf2,


class BridgeFeatLossNew(nn.Module):

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

    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]
