o
�J�hg* � @ s d Z ddgZddlmZmZ ddlZddlmZmZ ddl m
Z
mZ dd ed
edee dee d
ef
dd�Z
dd ed
ee dee dee d
eeeef f
dd�Z dd ed
ee dee dee d
eeeef f
dd�Z dd ed
ee deded
eeeef f
dd�ZdS )zBImplement various linear algebra algorithms for low rank matrices.�svd_lowrank�pca_lowrank� )�Optional�TupleN)�
_linalg_utils�Tensor)�handle_torch_function�has_torch_function� �A�q�niter�M�returnc
C s� |du rdn|}| � � st�| �n| j}tj}tj| jd ||| jd�}|| |�}|dur4||||� }tj �
|�j}t|�D ]2} || j
|�}|durS|||j
|� }tj �
|�j}|| |�}|durj||||� }tj �
|�j}q?|S )a� Return tensor :math:`Q` with :math:`q` orthonormal columns such
that :math:`Q Q^H A` approximates :math:`A`. If :math:`M` is
specified, then :math:`Q` is such that :math:`Q Q^H (A - M)`
approximates :math:`A - M`. without instantiating any tensors
of the size of :math:`A` or :math:`M`.
.. note:: The implementation is based on the Algorithm 4.4 from
Halko et al., 2009.
.. note:: For an adequate approximation of a k-rank matrix
:math:`A`, where k is not known in advance but could be
estimated, the number of :math:`Q` columns, q, can be
choosen according to the following criteria: in general,
:math:`k <= q <= min(2*k, m, n)`. For large low-rank
matrices, take :math:`q = k + 5..10`. If k is
relatively small compared to :math:`min(m, n)`, choosing
:math:`q = k + 0..2` may be sufficient.
.. note:: To obtain repeatable results, reset the seed for the
pseudorandom number generator
Args::
A (Tensor): the input tensor of size :math:`(*, m, n)`
q (int): the dimension of subspace spanned by :math:`Q`
columns.
niter (int, optional): the number of subspace iterations to
conduct; ``niter`` must be a
nonnegative integer. In most cases, the
default value 2 is more than enough.
M (Tensor, optional): the input tensor's mean of size
:math:`(*, m, n)`.
References::
- Nathan Halko, Per-Gunnar Martinsson, and Joel Tropp, Finding
structure with randomness: probabilistic algorithms for
constructing approximate matrix decompositions,
arXiv:0909.4061 [math.NA; math.PR], 2009 (available at
`arXiv <http://arxiv.org/abs/0909.4061>`_).
Nr
�������dtype�device)�
is_complex�_utils�get_floating_dtyper �matmul�torch�randn�shaper �linalg�qr�Q�range�mH)
r r r
r r r �R�Xr �i� r# �LC:\pinokio\api\whisper-webui.git\app\env\lib\site-packages\torch\_lowrank.py�get_approximate_basis s$ 1
r% � c C s\ t j�� s&| |f}ttt|���t jtd�f�s&t|�r&t t
|| |||d�S t| |||d�S )a� Return the singular value decomposition ``(U, S, V)`` of a matrix,
batches of matrices, or a sparse matrix :math:`A` such that
:math:`A \approx U \operatorname{diag}(S) V^{\text{H}}`. In case :math:`M` is given, then
SVD is computed for the matrix :math:`A - M`.
.. note:: The implementation is based on the Algorithm 5.1 from
Halko et al., 2009.
.. note:: For an adequate approximation of a k-rank matrix
:math:`A`, where k is not known in advance but could be
estimated, the number of :math:`Q` columns, q, can be
choosen according to the following criteria: in general,
:math:`k <= q <= min(2*k, m, n)`. For large low-rank
matrices, take :math:`q = k + 5..10`. If k is
relatively small compared to :math:`min(m, n)`, choosing
:math:`q = k + 0..2` may be sufficient.
.. note:: This is a randomized method. To obtain repeatable results,
set the seed for the pseudorandom number generator
.. note:: In general, use the full-rank SVD implementation
:func:`torch.linalg.svd` for dense matrices due to its 10x
higher performance characteristics. The low-rank SVD
will be useful for huge sparse matrices that
:func:`torch.linalg.svd` cannot handle.
Args::
A (Tensor): the input tensor of size :math:`(*, m, n)`
q (int, optional): a slightly overestimated rank of A.
niter (int, optional): the number of subspace iterations to
conduct; niter must be a nonnegative
integer, and defaults to 2
M (Tensor, optional): the input tensor's mean of size
:math:`(*, m, n)`, which will be broadcasted
to the size of A in this function.
References::
- Nathan Halko, Per-Gunnar Martinsson, and Joel Tropp, Finding
structure with randomness: probabilistic algorithms for
constructing approximate matrix decompositions,
arXiv:0909.4061 [math.NA; math.PR], 2009 (available at
`arXiv <https://arxiv.org/abs/0909.4061>`_).
N)r r
r )r �jit�is_scripting�set�map�type�issubsetr r r r �_svd_lowrank)r r r
r Z
tensor_opsr# r# r$ r V s
5���c
C s� |d u rdn|}| j dd � \}}tj}|d ur|�| �� �}||k r-| j} |d ur-|j}t| |||d�}||j| �}|d urG|||j|� }tjj |dd�\} }
}|j}|�| �} ||k rc|| } }| |
|fS )Nr&