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.triton_helpers import libdevice, math as tl_math
import torch.nn as nn
import torch.utils.data
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_per_fused_linalg_vector_norm_pow_rsub_sqrt_0(in_ptr0, out_ptr0,
    xnumel, rnumel, XBLOCK: tl.constexpr):
    RBLOCK: tl.constexpr = 4
    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
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp1 = tmp0 * tmp0
    tmp2 = 1.0
    tmp3 = tmp2 - tmp1
    tmp4 = libdevice.sqrt(tmp3)
    tmp5 = tl_math.abs(tmp4)
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tl.store(out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp8, None)


@triton.jit
def triton_per_fused_abs_add_linalg_vector_norm_mul_sub_1(in_out_ptr0,
    in_ptr0, in_ptr1, 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
    tmp0 = tl.load(in_ptr0 + r0, None)
    tmp2 = tl.load(in_ptr1 + r0, None)
    tmp9 = tl.load(in_out_ptr0 + 0)
    tmp10 = tl.broadcast_to(tmp9, [XBLOCK, 1])
    tmp1 = tl_math.abs(tmp0)
    tmp3 = tl_math.abs(tmp2)
    tmp4 = tmp1 - tmp3
    tmp5 = tmp4 * tmp4
    tmp6 = tl.broadcast_to(tmp5, [XBLOCK, RBLOCK])
    tmp8 = tl.sum(tmp6, 1)[:, None]
    tmp11 = libdevice.sqrt(tmp8)
    tmp12 = 0.1
    tmp13 = tmp11 * tmp12
    tmp14 = tmp10 + tmp13
    tl.debug_barrier()
    tl.store(in_out_ptr0 + tl.full([XBLOCK, 1], 0, tl.int32), tmp14, 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)
        buf0 = torch.ops.aten._linalg_svd.default(reinterpret_tensor(arg0_1,
            (4, 4), (1, 4), 0))
        del arg0_1
        buf1 = buf0[0]
        del buf0
        buf4 = torch.ops.aten._linalg_svd.default(reinterpret_tensor(arg1_1,
            (4, 4), (1, 4), 0))
        del arg1_1
        buf5 = buf4[0]
        del buf4
        buf8 = empty_strided_cuda((4, 4), (4, 1), torch.float32)
        extern_kernels.mm(reinterpret_tensor(buf1, (4, 4), (4, 1), 0), buf5,
            out=buf8)
        del buf1
        del buf5
        buf9 = torch.ops.aten._linalg_svd.default(buf8)
        del buf8
        buf10 = buf9[0]
        buf11 = buf9[1]
        buf12 = buf9[2]
        del buf9
        buf13 = empty_strided_cuda((), (), torch.float32)
        get_raw_stream(0)
        triton_per_fused_linalg_vector_norm_pow_rsub_sqrt_0[grid(1)](buf11,
            buf13, 1, 4, XBLOCK=1, num_warps=2, num_stages=1)
        del buf11
        buf15 = buf13
        del buf13
        triton_per_fused_abs_add_linalg_vector_norm_mul_sub_1[grid(1)](buf15,
            buf10, buf12, 1, 16, XBLOCK=1, num_warps=2, num_stages=1)
        del buf10
        del buf12
    return buf15,


class RepresentationSubspaceDistanceNew(nn.Module):
    """
    `Representation Subspace Distance (ICML 2021) <http://ise.thss.tsinghua.edu.cn/~mlong/doc/Representation-Subspace-Distance-for-Domain-Adaptation-Regression-icml21.pdf>`_

    Args:
        trade_off (float):  The trade-off value between Representation Subspace Distance
            and Base Mismatch Penalization. Default: 0.1

    Inputs:
        - f_s (tensor): feature representations on source domain, :math:`f^s`
        - f_t (tensor): feature representations on target domain, :math:`f^t`

    """

    def __init__(self, trade_off=0.1):
        super(RepresentationSubspaceDistanceNew, self).__init__()
        self.trade_off = trade_off

    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.0337