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

Make matrix mod 2 conversion to numpy faster & some semantic fixes #39152

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from

Conversation

user202729
Copy link
Contributor

@user202729 user202729 commented Dec 18, 2024

As in the title. Plus a few minor changes as needed.

Reuses numpy_util module from #38834 for the utility function…
in retrospect it might have been placed in the wrong place. (?)

📝 Checklist

  • The title is concise and informative.
  • The description explains in detail what this PR is about.
  • I have linked a relevant issue or discussion.
  • I have created tests covering the changes.
  • I have updated the documentation and checked the documentation preview.

⌛ Dependencies


Haven't been able to test on numpy 2.1 because of https://github.com/flintlib/flint/pull/2027

@@ -303,7 +304,7 @@ def process_block(block, src_in_lines, file_optional_tags, venv_explainer=''):
got = re.sub(r'(doctest:warning).*^( *DeprecationWarning:)',
r'\1...\n\2',
got, 1, re.DOTALL | re.MULTILINE)
got = got.splitlines() # got can't be the empty string
got = textwrap.dedent(got).splitlines() # got can't be the empty string
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Previously the fix made by the script would look like

sage: b = numpy.array(a); b
array([[ 0,  1,  2,  3],
[ 4,  5,  6,  7],
[ 8,  9, 10, 11]])

because each line is individually lstrip-ed. With the change it becomes

sage: b = numpy.array(a); b
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

Copy link

github-actions bot commented Dec 18, 2024

Documentation preview for this PR (built with commit 6d4be29; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

"""
if copy is not _MISSING:
from sage.misc.superseded import deprecation
deprecation(39152, "passing copy argument to numpy() is deprecated")
Copy link
Contributor Author

@user202729 user202729 Dec 18, 2024

Choose a reason for hiding this comment

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

I decide to deprecate this feature because:

  • copy=False was never supported in the first place (it always copies)
  • copy=False implicitly copies is incompatible with numpy 2.0 interface where np.array(..., copy=False) raises ValueError if a copy is made
  • copy=* doesn't work in numpy-based matrices (even after Fix matrix coercion with numpy 2.1 #38683 )
  • it seems dangerous to expose the internal array (which will change on mutation on the original object, which requires implementation to use the exact dtype otherwise user visible change will be seen)

20000
"""
from ..modules.numpy_util import mzd_matrix_to_numpy
return mzd_matrix_to_numpy(<uintptr_t>self._entries, dtype)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This method was the original plan (it overrides numpy() method of parent to provide a fast path)

return self._matrix_numpy.copy()
else:
return Matrix_dense.numpy(self, dtype=dtype)
return np.array(self._matrix_numpy, dtype=dtype)
Copy link
Contributor Author

@user202729 user202729 Dec 18, 2024

Choose a reason for hiding this comment

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

Simplification.

Also the old code made a call to generic Matrix_dense.numpy(self, dtype=dtype) method which is obviously much slower than this.

+ the old code documentation is in fact incorrect because the __array__ method of Matrix_numpy_dense class is not overridden, so the method is not called inside np.array(...). Of course it works because of the generic (slow) implementation.

Copy link
Member

Choose a reason for hiding this comment

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

what's wrong with flintlib/flint#2027 so that it "prevents testing" ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, nothing. I just mean that I need a version of flint after that pull request to allow testing, and I haven't gotten around to figure out how to install it from source yet (since latest version on conda-forge didn't have that pull request merged)

Copy link
Member

Choose a reason for hiding this comment

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

I think in Sage we have a workaround (some macro magic) for this Flint issue installed. (Flint still hasn't released a version with this fix merged)

@user202729 user202729 changed the title Make matrix mod 2 conversion to numpy faster Make matrix mod 2 conversion to numpy faster & some semantic fixes Dec 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants