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

sagemath-mpmath #35906

Closed
wants to merge 10 commits into from
Closed

sagemath-mpmath #35906

wants to merge 10 commits into from

Conversation

mkoeppe
Copy link
Contributor

@mkoeppe mkoeppe commented Jul 5, 2023

📚 Description

Instead of relying on mpmath's mechanism to select a backend, which is problematic when other libraries use mpmath too (#25445), we create an isolated copy of mpmath in sage.libs.mpmath._vendor.mpmath. The copy is created at build/sdist time using the pypa vendoring tool; we do not put a copy of the sources into our git source tree.

All uses of mpmath in the Sage library are updated. The Sage library no longer imports from mpmath and no longer sets environment variables that force mpmath (when imported by another library) to use the Sage backend.

The library is intended to be shipped by the pip-installable distribution sagemath-mpmath (added here on the PR), which only has sagemath-categories as a Python dependency.
Downstream projects that wish to use this version of mpmath should declare a dependency (install-requires) on sagemath-mpmath and use import sage.libs.mpmath.all instead of import mpmath.

📝 Checklist

  • The title is concise, informative, and self-explanatory.
  • 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 accordingly.

⌛ Dependencies

@github-actions
Copy link

github-actions bot commented Jul 7, 2023

Documentation preview for this PR (built with commit 1e3218c; changes) is ready! 🎉

@dimpase
Copy link
Member

dimpase commented Jul 8, 2023

what kinds of simplifications could be done on mpmath side here - assuming this is merged?

@mkoeppe
Copy link
Contributor Author

mkoeppe commented Jul 8, 2023

what kinds of simplifications could be done on mpmath side here - assuming this is merged?

I would suggest that they remove the condition 'SAGE_ROOT' in os.environ from https://github.com/mpmath/mpmath/blob/master/mpmath/libmp/backend.py#L41 (in fact, this could be done already now because Sage has not depended on this mechanism since #32799 (because of the needs of downstream, @antonio-rojas @kiwifb)

Other than that, I don't think anything gets simpler for mpmath itself; but for users of mpmath and Sage, the situation becomes clearer and more robust.

@dimpase
Copy link
Member

dimpase commented Jul 10, 2023

I don't like vendoring, and this looks way overblown anyway.
Can the same be achieved by modifying upstream? (I have commit rights in the upstream)

@dimpase
Copy link
Member

dimpase commented Jul 10, 2023

before doing anything here, we should check whether just getting rid of Sage backend in mpmath (which is a very old hack) is the right thing to do. I.e. whether there is a serious speed regression, something I doubt.

@mkoeppe
Copy link
Contributor Author

mkoeppe commented Jul 10, 2023

we should check whether just getting rid of Sage backend in mpmath (which is a very old hack) is the right thing to do.

+1. Do you know how to benchmark it, or should we get help from upstream?

@dimpase
Copy link
Member

dimpase commented Jul 10, 2023

it's a mixed bag - some tests are slower, some are faster. E.g. here are 5 slowest tests from
mpmath/mpmath/tests/test_torture.py

(sage-sh) dima@hilbert:sage-dev$ MPMATH_NOSAGE=yes pytest test_torture.py --durations=5
==================================================================== test session starts =====================================================================
platform linux -- Python 3.11.4, pytest-7.4.0, pluggy-1.0.0
rootdir: /mnt/opt/Sage/sage-dev
collected 90 items                                                                                                                                           

test_torture.py ..........................................................................................                                             [100%]

==================================================================== slowest 5 durations =====================================================================
60.31s call     test_torture.py::test_asymp[<lambda>-150-False23]
12.87s call     test_torture.py::test_asymp[<lambda>-150-False9]
12.16s call     test_torture.py::test_asymp[<lambda>-90-False]
10.97s call     test_torture.py::test_asymp[<lambda>-150-False37]
10.76s call     test_torture.py::test_asymp[f_wrapped-90-False]
=============================================================== 90 passed in 286.13s (0:04:46) ===============================================================
(sage-sh) dima@hilbert:sage-dev$ pytest test_torture.py --durations=5
==================================================================== test session starts =====================================================================
platform linux -- Python 3.11.4, pytest-7.4.0, pluggy-1.0.0
rootdir: /mnt/opt/Sage/sage-dev
collected 90 items                                                                                                                                           

test_torture.py ..........................................................................................                                             [100%]

==================================================================== slowest 5 durations =====================================================================
59.20s call     test_torture.py::test_asymp[<lambda>-150-False23]
12.18s call     test_torture.py::test_asymp[<lambda>-150-False9]
10.12s call     test_torture.py::test_asymp[<lambda>-90-False]
9.85s call     test_torture.py::test_asymp[<lambda>-150-False37]
9.53s call     test_torture.py::test_asymp[barnesg-90-False]
=============================================================== 90 passed in 239.50s (0:03:59) ===============================================================

So overall with Sage backend it's about 20% faster. OTOH one test there is must faster without Sage backend:

(sage-sh) dima@hilbert:sage-dev$ MPMATH_NOSAGE=yes pytest test_torture.py -k test_bernoulli_huge
==================================================================== test session starts =====================================================================
platform linux -- Python 3.11.4, pytest-7.4.0, pluggy-1.0.0
rootdir: /mnt/opt/Sage/sage-dev
collected 90 items / 89 deselected / 1 selected                                                                                                              

test_torture.py .                                                                                                                                      [100%]

============================================================== 1 passed, 89 deselected in 0.32s ==============================================================
(sage-sh) dima@hilbert:sage-dev$ pytest test_torture.py -k test_bernoulli_huge
==================================================================== test session starts =====================================================================
platform linux -- Python 3.11.4, pytest-7.4.0, pluggy-1.0.0
rootdir: /mnt/opt/Sage/sage-dev
collected 90 items / 89 deselected / 1 selected                                                                                                              

test_torture.py .                                                                                                                                      [100%]

============================================================== 1 passed, 89 deselected in 4.08s ==============================================================

@@ -0,0 +1,4 @@
$(PYTHON) rich click toml | $(PYTHON_TOOLCHAIN)
Copy link
Member

Choose a reason for hiding this comment

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

There is no toml package in Sage. Should this be tomli instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the toml package was removed from Sage in the meantime; we'll have to re-add it for sagemath-mpmath

@@ -0,0 +1,4 @@
$(PYTHON) markdown_it_py | $(PYTHON_TOOLCHAIN)
Copy link
Member

Choose a reason for hiding this comment

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

This seems to require poetry-core?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Not any more, I've now made it a wheel package, so there's no more build dependency.

@mkoeppe mkoeppe self-assigned this Oct 8, 2023
@mkoeppe mkoeppe force-pushed the sagemath_mpmath branch 2 times, most recently from 4f66aaa to 984cf52 Compare October 9, 2023 00:03
@mkoeppe
Copy link
Contributor Author

mkoeppe commented Oct 9, 2023

partially squashed

@skirpichev
Copy link

@dimpase, I assume you did tests without gmpy2. Is there advantages of using sage backend c.f. gmpy?

we should check whether just getting rid of Sage backend in mpmath

Which will be a simplest solution for mpmath/mpmath#705...

@mkoeppe
Copy link
Contributor Author

mkoeppe commented May 29, 2024

Closing this PR as this change appears to be slated for inclusion in mpmath 1.4:

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.

Incompatibilities in Sage mpmath cause downstream issues in SymPy
4 participants