Skip to content

Add numba dispatch for SVD#2136

Open
jessegrabowski wants to merge 1 commit into
pymc-devs:mainfrom
jessegrabowski:numba-svd
Open

Add numba dispatch for SVD#2136
jessegrabowski wants to merge 1 commit into
pymc-devs:mainfrom
jessegrabowski:numba-svd

Conversation

@jessegrabowski
Copy link
Copy Markdown
Member

Fun fact, there are no decompositions left for me to implement when i'm bored.

@jessegrabowski jessegrabowski requested a review from ricardoV94 May 15, 2026 03:58
@ricardoV94
Copy link
Copy Markdown
Member

ricardoV94 commented May 15, 2026

Fun fact, there are no decompositions left for me to implement when i'm bored

What about https://en.wikipedia.org/wiki/CUR_matrix_approximation

https://en.wikipedia.org/wiki/Interpolative_decomposition

https://en.wikipedia.org/wiki/Polar_decomposition

https://en.wikipedia.org/wiki/Category:Matrix_decompositions

# np.linalg.svd always returns real-valued singular values, even for complex input.
# The Op may declare s as complex (matching input dtype), but numba returns the real
# component dtype, so we must match that to avoid type unification errors.
# Casting discrete input to float allocates a new buffer, so in-place is moot.
Copy link
Copy Markdown
Member

@ricardoV94 ricardoV94 May 15, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hum? if you skip overwrite a in integer inputs you do two copies? the one to float and the one for the output?

you could say that if you cast you can always in place because it's a fresh buffer? opposite of what you did.

(does astype accept order=F). if not we should maybe implement our own version that does.

Actually I've missed this opt in other places?

For another time, should these cast be part of the graph? like expand dims that elemwise adds? then they 1) could be fused or rendered useless or what have you and 2) the numba dispatchers never need to worry about it

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let me check

if discrete_input:
x = x.astype(out_dtype)
return np.linalg.svd(x, full_matrices)
return _svd_gesdd_full(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth a (one time) bench we are doing better than the numba dispatch?

op = svd.SVD(
full_matrices=full_matrices,
compute_uv=compute_uv,
overwrite_a=overwrite_a,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest using the actual inplace path with requires starting with a regular Blockwise and just seeing the root input as mutable. Then check the destroy map of the compiled function?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so not starting with inplace graph

@jessegrabowski
Copy link
Copy Markdown
Member Author

What about https://en.wikipedia.org/wiki/CUR_matrix_approximation

There's also https://en.wikipedia.org/wiki/Cholesky_decomposition#LDL_decomposition

You are right though, I have a long and fruitful career of implementing matrix decompositions that nobody asked for still stretching out ahead of me. Bullet dodged!

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.

2 participants