Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2D SVD function #439

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

LeWerner42
Copy link

Category

  • New feature
  • Bugfix
  • Breaking change
  • Refactoring
  • Documentation
  • Other (please explain)

Description

Closes #436. This PR introduces a simple 2D SVD function. Some simulations only require 2D considerations.
Verified with the added test.
Simple Code for external testing:

import numpy as np
import warp as wp

N_tests = 1000

A_mat = np.random.randn(N_tests, 2, 2)/1e3
B_mat = np.identity(2, dtype=np.float64)
A_mat = A_mat + 0.5* B_mat

U_np, S_np, Vt_np = np.linalg.svd(A_mat, full_matrices=False)
B_mat = U_np @ (S_np[..., None]*Vt_np)

A_wp = wp.array(A_mat, dtype=wp.mat22d, shape=N_tests)
B_wp = wp.array(A_mat, dtype=wp.mat22d, shape=N_tests)
S_wp = wp.array(np.zeros((N_tests, 2)), dtype=wp.vec2d)

@wp.kernel
def SVD_wp(A:wp.array(dtype=wp.mat22d),
           B:wp.array(dtype=wp.mat22d),
           S_out:wp.array(dtype=wp.vec2d)):
    p = wp.tid()

    U = wp.mat22d()
    S = wp.vec2d()
    V = wp.mat22d()

    wp.svd2(A[p], U, S, V)
    B[p] = U * wp.diag(S) * wp.transpose(V)
    S_out[p] = S

    
wp.launch(kernel=SVD_wp, dim=N_tests, inputs=[A_wp, B_wp, S_wp])
A_wp = A_wp.numpy()
B_wp = B_wp.numpy()
S_wp = S_wp.numpy()

# avgError
print(f"mean err: {np.mean(np.abs(A_wp - B_wp))}")
print(f"std err:  {np.std(np.abs(A_wp - B_wp))}")

Limitations

Other Matrix operations are not yet implemented for 2D (e.g. qr, eig)

Changelog

  • svd.h: added 2d svd function + adj_svd2
  • subs: added 2d svd
  • functions.rst 2d svd description
  • builtins.py: added block for svd2
  • test_mat.py: test for svd2 copied from svd3 and modified accordingly

Before your PR is "Ready for review"

  • Do you agree to the terms under which contributions are accepted as described in Section 9 the Warp License?
  • Have you read the Contributor Guidelines?
  • Have you written any new necessary tests?
  • Have you added or updated any necessary documentation?
  • Have you added any files modified by compiling Warp and building the documentation to this PR (.e.g. stubs.py, functions.rst)?
  • Does your code pass ruff check and ruff format --check?

@LeWerner42 LeWerner42 changed the title Le werner42/svd 2d 2D SVD function Jan 16, 2025
@shi-eric shi-eric requested a review from mmacklin January 16, 2025 19:25
@mmacklin
Copy link
Collaborator

Very nice! Thank you, we will review and get this merged.

@LeWerner42
Copy link
Author

I am not quite sure, what the original issue with https://github.com/NVIDIA/warp/actions/runs/12816118282/job/35759531768 was.
I re-compiled the docs following the guidelines again with the last commit. (and added the forgotten entry in the changelog)

hope, this fixes it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[REQUEST] 2D SVD
2 participants