Add numba dispatch for expm#2148
Merged
Merged
Conversation
Member
Author
|
shit pycharm made the branch on upstream again, sorry about that. |
ricardoV94
reviewed
May 19, 2026
ricardoV94
reviewed
May 19, 2026
Member
Author
Microbench: import numba
import numpy as np
from numba.np.linalg import _copy_to_fortran_order
@numba.njit
def stdlib(A):
return _copy_to_fortran_order(A)
@numba.njit
def manual(A):
n, m = A.shape
out = np.empty((n, m), dtype=A.dtype).T # f-contig view of fresh buffer
for j in range(m):
for i in range(n):
out[i, j] = A[i, j]
return out
def best_us(fn, A, repeat=5, number=200):
t = Timer(lambda: fn(A))
fn(A) # warm
return min(t.repeat(repeat=repeat, number=number)) / number * 1e6
def main():
rng = np.random.default_rng(0)
layouts = {
"C-contig": lambda A: A,
"F-contig": np.asfortranarray,
"strided ": lambda A: np.repeat(A, 2, axis=0)[::2],
}
print(f"{'n':>5} {'layout':<9} {'stdlib':>11} {'manual':>10} {'speedup':>8}")
print("-" * 50)
for n in (4, 16, 64, 256, 1024):
A = rng.normal(size=(n, n))
for label, prep in layouts.items():
V = prep(A)
assert np.array_equal(stdlib(V), manual(V))
t_std = best_us(stdlib, V)
t_man = best_us(manual, V)
print(
f"{n:>5} {label:<9} {t_std:>8.2f} us {t_man:>7.2f} us"
f" {t_std / t_man:>6.2f}x"
)
print()Results: For large matrices where a copy is actually needed, it matters. Otherwise a push. |
Member
Author
|
i was trying to wring every possible us out to get this the numba version to be as fast as scipy. They have a nice fused C kernel for doing this Pade scaling stuff, so it was non-trivial to beat. Happy to revert for now and revisit. In fact i'd rather revert for now, it's a weird out of scope thing for this PR. |
Member
|
your strided test is only fair if you stride something that started as C and something that started as F, as the optimal order of iteration will depend on that. Although I don't know what's the worst case scenario since you are constrained by the output layout |
ricardoV94
approved these changes
May 20, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
It comes up in continuous time statespace models and ODEs
Performance: