Skip to content

Conversation

clayton-r
Copy link
Contributor

@clayton-r clayton-r commented Dec 1, 2020

References to other Issues or PRs

Fixes #20065
Closes #15484

Brief description of what is fixed or changed

This is a clean re-implementation of the torch-lambdify project without the conflicts from #15484 and #20065

Other comments

atan2 still not working and will require more extensive refactoring but this is a start.

Release Notes

  • printing
    • Allows sympy's lambdify function to operate on pytorch tensors. By example: f = lambdify(variables, expr, 'torch')
    • torch_code enables conversion between sympy function and the equivalent pytorch operation.

@sympy-bot
Copy link

sympy-bot commented Dec 1, 2020

Hi, I am the SymPy bot (v167). I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

Your release notes are in good order.

Here is what the release notes will look like:

This will be added to https://github.com/sympy/sympy/wiki/Release-Notes-for-1.11.

Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs

Fixes #20065
Closes #15484

#### Brief description of what is fixed or changed

This is a clean re-implementation of the torch-lambdify project without the conflicts from #15484 and  #20065

#### Other comments

atan2 still not working and will require more extensive refactoring but this is a start.

#### Release Notes

<!-- BEGIN RELEASE NOTES -->

* printing
  * Allows sympy's lambdify function to operate on pytorch tensors. By example: `f = lambdify(variables, expr, 'torch')` 
  * `torch_code` enables conversion between sympy function and the equivalent pytorch operation.

<!-- END RELEASE NOTES -->

@sympy-bot
Copy link

sympy-bot commented Dec 1, 2020

🟠

Hi, I am the SymPy bot (v167). I've noticed that some of your commits add or delete files. Since this is sometimes done unintentionally, I wanted to alert you about it.

This is an experimental feature of SymPy Bot. If you have any feedback on it, please comment at sympy/sympy-bot#75.

The following commits add new files:

  • df8464c:
    • sympy/printing/tests/test_torch.py
    • sympy/printing/torch.py
  • 5dba97f:
    • .circleci/config.yml
    • .github/workflows/ci-sage.yml
    • .github/workflows/comment-on-pr.yml
    • .github/workflows/docs-preview.yml
    • .github/workflows/release.yml
    • asv.conf.actions.json
    • bin/mailmap_check.py
    • bin/test_symengine.py
    • bin/test_tensorflow.py
    • doc/Dockerfile.htmldoc
    • doc/aptinstall.sh
    • doc/convert-svg-to-pdf.sh
    • doc/requirements.txt
    • doc/src/_pygments/styles.py
    • doc/src/_static/custom.css
    • doc/src/_templates/sidebar/versions.html
    • doc/src/explanation/active-deprecations.md
    • doc/src/explanation/index.rst
    • doc/src/guides/assumptions.rst
    • doc/src/guides/booleans.rst
    • doc/src/guides/contributing/build-docs.rst
    • doc/src/guides/contributing/debug.rst
    • doc/src/guides/contributing/dependencies.md
    • doc/src/guides/contributing/deprecations.md
    • doc/src/guides/contributing/dev-setup.rst
    • doc/src/guides/contributing/documentation-style-guide.rst
    • doc/src/guides/contributing/index.rst
    • doc/src/guides/contributing/make-a-contribution.rst
    • doc/src/guides/getting_started/index.rst
    • doc/src/guides/getting_started/install.md
    • doc/src/guides/index.rst
    • doc/src/modules/combinatorics/group_numbers.rst
    • doc/src/modules/integrals/index.rst
    • doc/src/modules/matrices/normalforms.rst
    • doc/src/modules/physics/continuum_mechanics/ildreaction.png
    • doc/src/modules/physics/continuum_mechanics/ildshear.png
    • doc/src/modules/physics/control/control_plots.rst
    • doc/src/modules/physics/mechanics/api/joint.rst
    • doc/src/modules/physics/mechanics/examples/multi_degree_freedom_holonomic_system.rst
    • doc/src/modules/physics/mechanics/examples/multidof-holonomic.png
    • doc/src/modules/polys/domainmatrix.rst
    • doc/src/modules/polys/numberfields.rst
    • doc/src/modules/solvers/index.rst
    • doc/src/modules/tensor/array_expressions.rst
    • doc/src/modules/utilities/exceptions.rst
    • doc/src/reference/index.rst
    • doc/src/reference/public/basics/index.rst
    • doc/src/reference/public/codegeneration/index.rst
    • doc/src/reference/public/logic/index.rst
    • doc/src/reference/public/matrices/index.rst
    • doc/src/reference/public/numbertheory/index.rst
    • doc/src/reference/public/physics/index.rst
    • doc/src/reference/public/topics/index.rst
    • doc/src/reference/public/utilities/index.rst
    • doc/src/tutorial/features.rst
    • doc/src/tutorial/next.rst
    • release/ci_release_script.sh
    • sympy/assumptions/facts.py
    • sympy/assumptions/tests/test_wrapper.py
    • sympy/assumptions/wrapper.py
    • sympy/calculus/accumulationbounds.py
    • sympy/calculus/tests/test_accumulationbounds.py
    • sympy/codegen/abstract_nodes.py
    • sympy/codegen/pynodes.py
    • sympy/codegen/tests/test_abstract_nodes.py
    • sympy/codegen/tests/test_pynodes.py
    • sympy/combinatorics/group_numbers.py
    • sympy/combinatorics/tests/test_group_numbers.py
  • bed133e:
    • bin/test_pyodide.mjs

The following commits delete files:

  • 5dba97f:
    • bin/authors_update.py
    • bin/mailmap_update.py
    • doc/ext/sympylive.py
    • doc/src/_static/default.css_t
    • doc/src/_templates/layout.html
    • doc/src/aboutus.rst
    • doc/src/install.rst
    • doc/src/modules/index.rst
    • doc/src/modules/physics/index.rst
    • doc/src/outreach.rst
    • doc/src/special_topics/index.rst
    • doc/src/wiki.rst
    • sympy/assumptions/relation/simplify.py
    • sympy/codegen/array_utils.py

If these files were added/deleted on purpose, you can ignore this message.

@clayton-r
Copy link
Contributor Author

I'm getting a timeout on the travis env build -- @oscarbenjamin how's the progress looking on the move to github actions? Should I bother finding a workaround?

@oscarbenjamin
Copy link
Collaborator

I've restarted the Travis jobs. We are using github actions now but it doesn't run all the same jobs as Travis so we are not ready to turn off Travis yet.

@clayton-r
Copy link
Contributor Author

@oscarbenjamin Thanks for your work on moving to actions -- looks like it's passing now.

@asmeurer
Copy link
Member

asmeurer commented Jan 4, 2021

Can pytorch be installed in actions? Or would that require moving actions to using conda (see also #20623)?

@asmeurer
Copy link
Member

asmeurer commented Jan 4, 2021

I'm assuming this should close #15484. I edited the description so it actually will.

@clayton-r
Copy link
Contributor Author

Thanks for that! Pip isn't the reccommended method for installing but is possible afaik:

pip install torch torchvision

@patrick-kidger
Copy link

patrick-kidger commented Feb 23, 2021

What's the status/stability of this PR? This is functionality we'd be interested in.
CC @MilesCranmer

(EDIT: for the sake of any future Googlers who end up here like I did: I ended up putting together something myself that converts SymPy expressions into PyTorch Modules and vice versa: sympytorch)

@asmeurer
Copy link
Member

I think the main blocking issue is getting the testing working on GitHub Actions. That may require some work on #20623.

@patrick-kidger do you have any functionality in sympytorch that isn't implemented here?

@patrick-kidger
Copy link

So I don't think the functionality here creates a trainable PyTorch Module, nor does it have a way to convert that back into a sympy expression after training, which are the two bits of functionality I particularly need.

(Conversely I am aware that this PR includes a bunch of things I didn't try to put into sympytorch.)

@MilesCranmer
Copy link

Here's the JAX equivalent of @patrick-kidger's idea for parametrized expressions: https://github.com/MilesCranmer/sympy2jax.

Having the constants be trainable makes lambdify very useful for optimization (e.g., symbolic regression https://github.com/MilesCranmer/pysr), which is why both sympytorch and sympy2jax record parameters in the expression rather than hardcoding them in.

Could this behaviour be added as a kwarg or something? e.g., record_parameters=True which will allow for parameters passed as argument (numpy, jax, other functional packages) or returns a trainable module with the parameters saved (pytorch)?

@oscarbenjamin
Copy link
Collaborator

It looks fairly easy to setup conda on actions if that is needed:
https://github.com/marketplace/actions/setup-miniconda

@asmeurer
Copy link
Member

asmeurer commented Mar 1, 2021

@MilesCranmer can you open a new issue about Jax support?

@patrick-kidger does it make sense for those features to be in SymPy (assuming this PR is merged)?

@patrick-kidger
Copy link

I think it would make sense. It seems to me like converting to a torch.nn.Module is the natural way to "pytorchify" a function. That's not what this PR does though, as mentioned above.

@oscarbenjamin
Copy link
Collaborator

Closing after #27760 was merged

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.

7 participants