GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ENH: Improve np.linalg.det performance by eendebakpt · Pull Request #28649 · numpy/numpy

Conversation

@eendebakpt

@eendebakpt eendebakpt commented Apr 4, 2025

Copy link
Copy Markdown
Contributor

We can improve performance of np.linalg.det for small arrays by up to 40% with 3 changes:

  • Move _assert_stacked_2d check into _assert_stacked_square
  • Improve performance of _commonType by using a cache
  • Avoid r.astype(...) for scalar arguments making a copy of the data (and internally converting to an array and back)

In this PR we perform the first step.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@eendebakpt
ENH: Improve np.linalg.det performance
a4ff9e0
@github-actions github-actions Bot added the 01 - Enhancement label Apr 4, 2025
@eendebakpt
eendebakpt marked this pull request as draft April 4, 2025 15:18
eendebakpt
eendebakpt commented Apr 4, 2025
View reviewed changes
Comment thread numpy/linalg/_linalg.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@eendebakpt
Update numpy/linalg/_linalg.py
05abe63
@eendebakpt
eendebakpt marked this pull request as ready for review April 4, 2025 16:01
@tylerjereddy tylerjereddy added the component: numpy.linalg label Apr 4, 2025
tylerjereddy
tylerjereddy reviewed Apr 4, 2025
View reviewed changes

@tylerjereddy tylerjereddy left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

So far asv isn't showing much perf improvement on this branch on x86_64 Linux (i9-13900K):

asv continuous -E virtualenv -e -b "time_det.*" main linalg_refactor

BENCHMARKS NOT SIGNIFICANTLY CHANGED.

It may be because you're only making the first of the series of proposed changes.

Regardless of the performance changes, I suppose this is a reduction in lines of code, so maybe "ok" on its own anyway.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/linalg/_linalg.py Outdated
if issubclass(type_, inexact):
if isComplexType(type_):
is_complex = True
is_complex = is_complex or isComplexType(type_)

@tylerjereddy tylerjereddy Apr 4, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Is this really worth doing? It takes my brain longer to process and shouldn't matter much performance-wise?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mhvk mhvk Apr 4, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'm also not sure this routine is worth it, but if one goes for it, I'd start with something like

types = set(a.dtype.type for a in arrays)

which will generally reduce the number already, and then do

is_complex = any(isComplexType(type_) for type_ in types)

and something along similar lines for result_type (but with a check whether one can really not simply use the built-in np.result_type - not obvious to me).

But I'd do it in a separate PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eendebakpt eendebakpt Apr 9, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Interesting idea! It might not work out because the common use case is with len(arrays) just 1 or 2 and the set overhead is too large. I will remove this change here and test out in a new PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
mhvk
mhvk reviewed Apr 4, 2025
View reviewed changes

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I like the cleanup, though I'm not surprised it doesn't have that much of an effect on speed. A suggestion in-line for an extra (if very minor) performance boost.

Also, would suggest to do just the removal of _assert_stacked_square here.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/linalg/_linalg.py Outdated
if issubclass(type_, inexact):
if isComplexType(type_):
is_complex = True
is_complex = is_complex or isComplexType(type_)

@mhvk mhvk Apr 4, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'm also not sure this routine is worth it, but if one goes for it, I'd start with something like

types = set(a.dtype.type for a in arrays)

which will generally reduce the number already, and then do

is_complex = any(isComplexType(type_) for type_ in types)

and something along similar lines for result_type (but with a check whether one can really not simply use the built-in np.result_type - not obvious to me).

But I'd do it in a separate PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread numpy/linalg/_linalg.py
w = gufunc(a, signature=signature)
return w.astype(_realType(result_t), copy=False)

def _convertarray(a):

@mhvk mhvk Apr 4, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Nice catch that this is not actually used!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
mhvk
mhvk reviewed Apr 4, 2025
View reviewed changes
Comment thread numpy/linalg/_linalg.py Outdated

def _assert_stacked_square(*arrays):
for a in arrays:
if a.ndim < 2:

@mhvk mhvk Apr 4, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I like the combination. If one really wants to get out the most, it could be

try:
    m, n = a.shape[-2:]
except ValueError:
    riase LinAlgError(f"{a.ndim}-dimensional...") from None
if m != n:
   ...

Using that these days try/except has no cost if no exception is raised.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mhvk mhvk Apr 4, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sorry, pushed this after submitting the review - above is the most relevant comment! (and not very relevant at that!)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@eendebakpt eendebakpt Apr 9, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The _assert_stacked_square is about 10% faster using your suggestion, I updated the PR with this.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
eendebakpt added 5 commits April 9, 2025 09:32
@eendebakpt
revert change to complex detection
c020ed7
@eendebakpt
use suggestion
a760647
@eendebakpt
whitespace
3f094cd
@eendebakpt
add more small array benchmarks
ab6cc07
@eendebakpt
trigger build
2fc384f
mhvk
mhvk approved these changes Apr 9, 2025
View reviewed changes

@mhvk mhvk left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Looks good to me, thanks! Let's get it in.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mhvk
mhvk merged commit 422ca44 into numpy:main Apr 9, 2025
@eendebakpt

eendebakpt commented Apr 10, 2025

Copy link
Copy Markdown
Contributor Author

Thanks for reviewing! Next PR is #28686

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

MaanasArora pushed a commit to MaanasArora/numpy that referenced this pull request Apr 11, 2025
@eendebakpt @MaanasArora
ENH: Improve np.linalg.det performance by simplifying checks (numpy#2…
11e2b54
…8649)

* ENH: Improve np.linalg.det performance

* Update numpy/linalg/_linalg.py

* revert change to complex detection

* use suggestion

* whitespace

* add more small array benchmarks

* trigger build
@ngoldbaum ngoldbaum mentioned this pull request May 20, 2025
MNT: xfail flaky test on pypy #29014
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tylerjereddy tylerjereddy tylerjereddy left review comments
@mhvk mhvk mhvk approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

01 - Enhancement component: numpy.linalg

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@eendebakpt @mhvk @tylerjereddy
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.