svd#

Perform a singular value decomposition (SVD).

Added in version 0.6.0.

Note

svd is not currently supported by CUDA JIT fusion in MatX. The runtime libcusolverdx code-generation API used by MatX does not expose an SVD routine, so use a normal non-JIT executor path for SVD. Lazy projection members such as svd(A).U, svd(A).S, and svd(A).VT are available on normal executors and can be used in expressions with other operators.

template<typename OpA>
__MATX_INLINE__ auto matx::svd(const OpA &a, const SVDMode jobz = SVDMode::ALL, const SVDHostAlgo algo = SVDHostAlgo::DC)#

Perform a singular value decomposition (SVD) using cuSolver or a LAPACK host library.

The singular values within each vector are sorted in descending order.

If rank > 2, operations are batched.

Template Parameters:

OpA – Operator input type

Parameters:
  • a – Input operator of shape ... x m x n

  • jobz – Compute all, part, or none of matrices U and VT

  • algo – For Host SVD calls, whether to use more efficient divide-and-conquer based gesdd routine or the QR factorization based gesvd routine. gesdd can run significantly faster, especially for large matrices. However, gesdd requires \( O(\min(M,N) ^ 2) \) memory as compared to \( O(\max(M,N)) \) for gesvd, and it can have poorer accuracy in some cases. Ignored for CUDA SVD calls.

Returns:

Operator that produces U, S, and VT tensors. Regardless of jobz, all 3 tensors must be correctly setup for the operation and used with mtie(). S is computed for all SVDMode values, including SVDMode::NONE; U and VT are unavailable when SVDMode::NONE is selected. k = min(m, n)

  • U - The unitary matrix containing the left singular vectors. A tensor of shape ... x m x k for SVDMode::REDUCED and ... x m x m otherwise.

  • S - A tensor of shape ... x k containing the singular values in descending order. It must be of real type and match the inner type of the other tensors.

  • VT - The unitary matrix containing the right singular vectors. A tensor of shape ... x k x n for SVDMode::REDUCED and ... x n x n otherwise.

Enums#

The following enums are used for configuring the behavior of SVD operations.

enum class matx::SVDMode#

Modes for computing columns of U and rows of VT in Singular Value Decomposition (SVD). Corresponds to the LAPACK/cuSolver parameters jobu and jobvt. The same option is used for both jobu and jobvt in MatX. Singular values are computed for every mode; these options only control whether singular vectors are computed.

Values:

enumerator ALL#

Compute all columns of U and all rows of VT (Equivalent to jobu = jobvt = ‘A’)

enumerator REDUCED#

Compute only the first min(m,n) columns of U and rows of VT (Equivalent to jobu = jobvt = ‘S’)

enumerator NONE#

Compute singular values only; compute no columns of U or rows of VT (Equivalent to jobu = jobvt = ‘N’)

enum class matx::SVDHostAlgo#

Controls the LAPACK driver used for SVD on host.

Values:

enumerator QR#

QR-based method (corresponds to gesvd)

enumerator DC#

Divide and Conquer method (corresponds to gesdd)

Examples#

(mtie(Uv, Sv, VTv) = svd(Av)).run(this->exec);

Projection Examples#

The U, S, and VT projections can be composed with other operators to reconstruct the input matrix. This example is included from the ProjectionAPI unit test.

auto op = svd(A, SVDMode::REDUCED);
(UDVT = matmul(matmul(op.U, diag(as_type<TestType>(op.S))), op.VT)).run(this->exec);