code: def test_svd():
      """Test for the SVD functions"""
      tol = 0.1
      tol_orthogonality = 0.01
  
      for svd in SVD_FUNS:
          if svd == "randomized_svd":
              decimal = 2
          else:
              decimal = 3
          sizes = [(100, 100), (100, 5), (10, 10), (10, 4), (5, 100)]
          n_eigenvecs = [90, 4, 5, 4, 5]
  
          for s, n in zip(sizes, n_eigenvecs):
              matrix = np.random.random(s)
              matrix_backend = T.tensor(matrix)
              fU, fS, fV = svd_interface(
                  matrix_backend, n_eigenvecs=n, method=svd
              )
              U, S, V = np.linalg.svd(matrix, full_matrices=True)
              U, S, V = U[:, :n], S[:n], V[:n, :]
  
              assert_array_almost_equal(
                  np.abs(S),
                  T.abs(fS),
                  decimal=decimal,
                  err_msg=f'eigenvals not correct for "{svd}" svd fun VS svd and backend="{tl.get_backend()}, for {n} eigenenvecs, and size {s}".',
              )
  
              # True reconstruction error (based on numpy SVD)
              true_rec_error = np.sum(
                  (matrix - np.dot(U, S.reshape((-1, 1)) * V)) ** 2
              )
              # Reconstruction error with the backend's SVD
              rec_error = T.sum(
                  (
                      matrix_backend
                      - T.dot(fU, T.reshape(fS, (-1, 1)) * fV)
                  )
                  ** 2
              )
              # Check that the two are similar
              assert_(
                  true_rec_error - rec_error <= tol,
                  msg='Reconstruction not correct for "{svd}" svd fun VS svd and backend="{tl.get_backend()}, for {n} eigenenvecs, and size {s}".',
              )
  
              # Check for orthogonality when relevant
              left_orthogonality_error = T.norm(
                  T.dot(T.transpose(fU), fU) - T.eye(n)
              )
              assert_(
                  left_orthogonality_error <= tol_orthogonality,
                  msg='Left eigenvecs not orthogonal for "{svd}" svd fun VS svd and backend="{tl.get_backend()}, for {n} eigenenvecs, and size {s}".',
              )
              right_orthogonality_error = T.norm(
                  T.dot(fV, T.transpose(fV)) - T.eye(n)
              )
              assert_(
                  right_orthogonality_error <= tol_orthogonality,
                  msg='Right eigenvecs not orthogonal for "{svd}" svd fun VS svd and backend="{tl.get_backend()}, for {n} eigenenvecs, and size {s}".',
              )
  
          # Should fail on non-matrices
          with assert_raises(ValueError):
              tensor = T.tensor(np.random.random((3, 3, 3)))
              svd_interface(tensor, n_eigenvecs=n, method=svd)
  
          # Test for singular matrices (some eigenvals will be zero)
          # Rank at most 5
          matrix = tl.dot(
              tl.randn((20, 5), seed=12), tl.randn((5, 20), seed=23)
          )
          U, S, V = tl.truncated_svd(
              matrix, n_eigenvecs=6, random_state=0
          )
          true_rec_error = tl.sum(
              (matrix - tl.dot(U, tl.reshape(S, (-1, 1)) * V)) ** 2
          )
          assert_(true_rec_error <= tol)
          assert_(
              np.isfinite(T.to_numpy(U)).all(),
              msg="Left singular vectors are not finite",
          )
          assert_(
              np.isfinite(T.to_numpy(V)).all(),
              msg="Right singular vectors are not finite",
          )
  
          # Test orthonormality when  max_dim > n_eigenvecs > matrix_rank
          matrix = tl.dot(
              tl.randn((4, 2), seed=1), tl.randn((2, 4), seed=12)
          )
          U, S, V = tl.truncated_svd(
              matrix, n_eigenvecs=3, random_state=0
          )
          left_orthogonality_error = T.norm(
              T.dot(T.transpose(U), U) - T.eye(3)
          )
          assert_(left_orthogonality_error <= tol_orthogonality)
          right_orthogonality_error = T.norm(
              T.dot(V, T.transpose(V)) - T.eye(3)
          )
          assert_(right_orthogonality_error <= tol_orthogonality)
  
          # Test if truncated_svd returns the same result for the same setting
          matrix = T.tensor(np.random.random((20, 5)))
          random_state = np.random.RandomState(0)
          U1, S1, V1 = tl.truncated_svd(
              matrix, n_eigenvecs=2, random_state=random_state
          )
          U2, S2, V2 = tl.truncated_svd(
              matrix, n_eigenvecs=2, random_state=0
          )
          assert_array_equal(U1, U2)
          assert_array_equal(S1, S2)
          assert_array_equal(V1, V2)
  
imports: from concurrent.futures import ThreadPoolExecutor
  import pytest
  from time import time
  import numpy
  from scipy.linalg import svd
  from scipy import special
  import tensorly
  from ..None import backend
  from ..testing import assert_allclose, assert_array_equal, assert_equal, assert_, assert_array_almost_equal, assert_raises
  from tensorly.tenalg.svd import SVD_FUNS, svd_interface
label_desc: Use image node_id 6 for calling a global function with example usage: test_svd() without return types