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

Add python bindings #110

Merged
merged 46 commits into from
Apr 17, 2024
Merged

Add python bindings #110

merged 46 commits into from
Apr 17, 2024

Conversation

hugary1995
Copy link
Collaborator

Add python bindings

  • Set up pybind11
  • Set up pytest

CMake configure

  • Let CMake generate config.h
  • Let CMake generate conftest.py for pytest
  • Use symlink for test artifacts when we are doing development (when .git folder is present)
  • Fix dynamic section name with symlink
  • Adapted the test workflow for pytest, let's see if it works

Copy link
Collaborator

@reverendbedford reverendbedford left a comment

Choose a reason for hiding this comment

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

  1. It may be worthwhile to setup the basic python packaging stuff now (i.e. a setup.py). I don't know if we'll ever want to make this available as a python package directly, but we might. And I'm not sure how compatible your directory layout here will be if we do that. You can look at NEML for a setup.py that works with cmake.
  2. What is the actual minimum torch and python version required? If we really need python3.11 that could be annoying, as it's not standard on Ubuntu yet.
  3. The tests error on python3.10 with a legitimate syntax error. Probably should fix that unless 3.11 is really needed.
  4. We should have the python test action loop over python versions in our valid range. Maybe the same thing with torch, but I'm okay with just forcing people to 2.1 if that's what's needed.

CMakeLists.txt Show resolved Hide resolved
CMakeLists.txt Show resolved Hide resolved
doc/content/_01_install.md Outdated Show resolved Hide resolved
include/neml2/tensors/WR2.h Outdated Show resolved Hide resolved
include/neml2/tensors/macros.h Outdated Show resolved Hide resolved
python/neml2_wrap/tensors/FixedDimTensor.h Outdated Show resolved Hide resolved
python/neml2_wrap/tensors/Rot.cxx Outdated Show resolved Hide resolved
python/neml2_wrap/tensors/tensors.cxx Outdated Show resolved Hide resolved
@hugary1995
Copy link
Collaborator Author

hugary1995 commented Feb 21, 2024

  1. It may be worthwhile to setup the basic python packaging stuff now (i.e. a setup.py). I don't know if we'll ever want to make this available as a python package directly, but we might. And I'm not sure how compatible your directory layout here will be if we do that. You can look at NEML for a setup.py that works with cmake.

I'll try to add a pyproject.toml

  1. What is the actual minimum torch and python version required? If we really need python3.11 that could be annoying, as it's not standard on Ubuntu yet.
  2. The tests error on python3.10 with a legitimate syntax error. Probably should fix that unless 3.11 is really needed.

I dropped the 3.11 requirement and fixed the unpacking syntax error.

  1. We should have the python test action loop over python versions in our valid range. Maybe the same thing with torch, but I'm okay with just forcing people to 2.1 if that's what's needed.

I agree, if github does not have any limit on the number of checks...

Copy link
Contributor

github-actions bot commented Feb 21, 2024

Test Results (ubuntu-latest-Release-ON)

    3 files  ±0      3 suites  ±0   24s ⏱️ -1s
  468 tests ±0    468 ✅ ±0  0 💤 ±0  0 ❌ ±0 
3 045 runs  ±0  3 045 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 6a60dd0. ± Comparison against base commit 65552df.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Feb 21, 2024

Test Results (ubuntu-latest-Debug-ON)

    3 files  ±0      3 suites  ±0   1m 12s ⏱️ -1s
  468 tests ±0    468 ✅ ±0  0 💤 ±0  0 ❌ ±0 
3 047 runs  ±0  3 047 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 6a60dd0. ± Comparison against base commit 65552df.

♻️ This comment has been updated with latest results.

@hugary1995
Copy link
Collaborator Author

lol I forgot ubuntu 22.04 does not have python 3.6 in the main universe. Should we switch to conda or just drop python 3.6 support? I'm fine with either solution.

Copy link
Contributor

github-actions bot commented Feb 21, 2024

Test Results (macos-latest-Release-ON)

    3 files  ±0      3 suites  ±0   1m 6s ⏱️ +4s
  468 tests ±0    468 ✅ ±0  0 💤 ±0  0 ❌ ±0 
3 045 runs  ±0  3 045 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 6a60dd0. ± Comparison against base commit 65552df.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Feb 21, 2024

Test Results (macos-latest-Debug-ON)

    3 files  ±0      3 suites  ±0   2m 3s ⏱️ -3s
  468 tests ±0    468 ✅ ±0  0 💤 ±0  0 ❌ ±0 
3 047 runs  ±0  3 047 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 6a60dd0. ± Comparison against base commit 65552df.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Feb 21, 2024

Test Results (macos-latest-Debug-OFF)

    3 files  ±0      3 suites  ±0   1m 8s ⏱️ -50s
  468 tests ±0    468 ✅ ±0  0 💤 ±0  0 ❌ ±0 
3 047 runs  ±0  3 047 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 6a60dd0. ± Comparison against base commit 65552df.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Feb 21, 2024

Test Results (macos-latest-Release-OFF)

    3 files  ±0      3 suites  ±0   37s ⏱️ +4s
  468 tests ±0    468 ✅ ±0  0 💤 ±0  0 ❌ ±0 
3 045 runs  ±0  3 045 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 6a60dd0. ± Comparison against base commit 65552df.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Feb 21, 2024

Test Results (ubuntu-latest-Release-OFF)

    3 files  ±0      3 suites  ±0   25s ⏱️ -1s
  468 tests ±0    468 ✅ ±0  0 💤 ±0  0 ❌ ±0 
3 045 runs  ±0  3 045 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 6a60dd0. ± Comparison against base commit 65552df.

♻️ This comment has been updated with latest results.

Copy link
Contributor

github-actions bot commented Feb 21, 2024

Test Results (ubuntu-latest-Debug-OFF)

    3 files  ±0      3 suites  ±0   1m 12s ⏱️ -1s
  468 tests ±0    468 ✅ ±0  0 💤 ±0  0 ❌ ±0 
3 047 runs  ±0  3 047 ✅ ±0  0 💤 ±0  0 ❌ ±0 

Results for commit 6a60dd0. ± Comparison against base commit 65552df.

♻️ This comment has been updated with latest results.

@hugary1995
Copy link
Collaborator Author

This is ready again.

Copy link
Collaborator

@reverendbedford reverendbedford left a comment

Choose a reason for hiding this comment

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

  1. Add check that the python source has been blacked (black --check).
  2. I have a fun bug where I installed the package, uninstalled it, and then tried to install it again. It will not build. The problem is with linking to torch. Delete the build folder and trying again fixed the issue. Uhh, maybe try it on Linux and see what happens?
  3. It would be nice to be able to construct both BatchTensor and the derived classes of FixedDimTensor from torch tensors and numpy arrays. I think you just need to implement the torch.tensor variant and then rely on the torch tensor constructor to handle the rest of the conversions. They must have a "ensure_tensor" or similar method like numpy. For BatchTensor the user would supply the tensor and an int indicating the separation between the batch and base dims. For FixedDimTensor we should be able to infer all the dimensions (and throw if necessary) without help.
  4. I don't think you test the torch.tensor view into our classes. A few simple tests would be nice (values are the same, if you change the tensor view you change the underlying neml2 tensor data, devices are the same).

.github/workflows/tests.yml Outdated Show resolved Hide resolved
.github/workflows/tests.yml Show resolved Hide resolved
CMakeLists.txt Outdated Show resolved Hide resolved
include/neml2/base/config.h.in Outdated Show resolved Hide resolved
python/CMakeLists.txt Outdated Show resolved Hide resolved
python/neml2_wrap/misc/math.cxx Outdated Show resolved Hide resolved
python/neml2_wrap/misc/types.h Outdated Show resolved Hide resolved
scripts/find_torch.py Outdated Show resolved Hide resolved
setup.py Outdated Show resolved Hide resolved
@hugary1995
Copy link
Collaborator Author

I searched online but couldn't find any way of locating the pytorch package without first finding python.

I would argue that what I have right now is fine, because if python is not found, it will merrily fall back to plan B (downloading a cpu libtorch). So strictly speaking, we are not introducing python as a dependency (if we are not going to compile python bindings).

@hugary1995
Copy link
Collaborator Author

Also note that I renamed the github workflow recipe "Formatting Check" to "C++ Formatting Check" (because we now have a "Python Formatting Check"). So you will need to modify the list of expected checks.

@hugary1995
Copy link
Collaborator Author

A few "new" things to review:

  1. I reworked the CMakeLists.txt to enable various build configurations, including wheels.
  2. I added bindings for Factory, HITParser, LabeledTensor, Model so that basic python workflow works, i.e. loading a model from an input file, use the loaded model to give value and derivatives, etc.
  3. I added a cmake-variants.yaml to the repo root for vscode users.

I let the wheels use a special rpath to avoid bundling all the libtorch dependencies into our wheel. We probably want to discuss about this.

@hugary1995
Copy link
Collaborator Author

I haven't added the "upload to pypi" step yet. We may want to play with the wheels on more machines before uploading to pypi...

@hugary1995
Copy link
Collaborator Author

At a higher level, we should probably think about how to auto-update python wheels to stay up-to-date with pytorch updates. I am also not entirely satisfied with the hard-coded torch versions scattered across the repository.

Copy link
Collaborator

@reverendbedford reverendbedford left a comment

Choose a reason for hiding this comment

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

  1. See comment on minimum cmake version.
  2. We should probably include "build and test" for python bindings in the github actions.
  3. I suggest have a load_model helper that combines load_input and get_model.
  4. Perhaps add a parameter partial type test to test_model.py as that's going to be important shortly.

I will make a new PR to actually upload the pypi package after we merge this.

Good work, nearly done!

CMakeLists.txt Outdated Show resolved Hide resolved
@hugary1995
Copy link
Collaborator Author

I opened a future issue after merging this PR: #130

Copy link
Contributor

Python Binding Test Results (macos-latest

63 tests   39 ✅  2s ⏱️
 1 suites  24 💤
 1 files     0 ❌

Results for commit c2a467d.

@hugary1995
Copy link
Collaborator Author

And also #131

Copy link
Contributor

Python Binding Test Results (ubuntu-latest)

63 tests   39 ✅  2s ⏱️
 1 suites  24 💤
 1 files     0 ❌

Results for commit 6a60dd0.

Copy link
Contributor

Python Binding Test Results (macos-latest)

63 tests   39 ✅  1m 20s ⏱️
 1 suites  24 💤
 1 files     0 ❌

Results for commit 6a60dd0.

@reverendbedford
Copy link
Collaborator

Coverage after merging pybind into main will be

87.74%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
include/neml2/base
   CrossRef.h80.43%100%77.78%100%
   DependencyResolver.h86.13%100%88.24%85.83%178, 180–181, 185, 188–190, 223, 250, 262, 272–273, 303, 305–306, 308–309
   Factory.h28%100%11.11%100%
   NEML2Object.h37.50%100%22.22%57.14%50, 63, 66
   OptionCollection.h0%100%0%0%40, 49
   OptionSet.h39.75%100%36.07%74.36%124, 127, 130, 142, 61, 66, 79, 81, 85, 89
   Registry.h83.83%100%83.61%100%
   Storage.h56.10%100%39.13%77.78%147–148, 150, 177
include/neml2/drivers
   Driver.h0%100%0%0%56
   TransientDriver.h85.71%100%100%75%149
include/neml2/misc
   error.h34.36%100%31.96%82.35%35–36, 38
   math.h100%100%100%100%
   parser_utils.h56.07%100%46.34%88%37–38, 40
   utils.h56.67%100%47.62%77.78%173, 239, 241–246, 267, 302, 304–307
include/neml2/models
   BufferStore.h34.43%100%18.92%58.33%113, 115, 117–121, 133, 147, 151
   ComposedModel.h100%100%100%100%
   Data.h100%100%100%100%
   Interpolation.h64.58%100%55.56%91.67%75
   LinearInterpolation.h20%100%8.57%100%
   Model.h85.19%100%75%89.47%106, 65
   NonlinearParameter.h10.53%100%5.56%100%
   ParameterStore.h21.43%100%5.41%52.63%132, 134, 136–140, 148, 152
   VariableStore.h71.15%100%64.24%89.47%236, 238–239, 260, 91, 97
include/neml2/models/crystallography
   CrystalGeometry.h0%100%0%0%90, 92
   MillerIndex.h100%100%100%100%
   crystallography.h100%100%100%100%
include/neml2/solvers
   Newton.h100%100%100%100%
   NonlinearSystem.h66.67%100%66.67%66.67%62
include/neml2/tensors
   BatchTensorBase.h60.61%100%46.76%93.22%212, 214, 256, 258
   FixedDimTensor.h28.35%100%20.73%70%146, 150, 162, 165, 170, 172, 177, 182, 66
   LabeledAxis.h0%100%0%0%142, 67, 72, 74–75, 77
   LabeledAxisAccessor.h80.56%100%77.27%85.71%46, 77
   LabeledTensor.h54.26%100%44.78%77.78%105, 109, 124, 173, 175–176
   Scalar.h44%100%34.55%70%82, 84–88
   TensorValue.h14%100%10.87%50%39, 63, 65, 75
   Transformable.h0%100%0%0%44
   Variable.h44.98%100%35.36%81.25%43–47, 49, 51, 89, 95
   VecBase.h80.95%100%50%100%
src/neml2/base
   CrossRef.cxx83.87%100%75%100%
   Factory.cxx100%100%100%100%
   HITParser.cxx100%100%100%100%
   NEML2Object.cxx90.91%100%100%88.89%34
   OptionCollection.cxx100%100%100%100%
   OptionSet.cxx83.72%100%81.82%84.38%118, 120, 130, 132, 35
   Registry.cxx100%100%100%100%
src/neml2/drivers
   Driver.cxx90%100%100%87.50%35
   TransientDriver.cxx85.38%100%100%83.87%190–191, 196–198, 201–202, 206–210, 212–217, 220, 225–229, 56
src/neml2/drivers/solid_mechanics
   LargeDeformationIncrementalSolidMechanicsDriver.cxx20.69%100%25%20.37%100, 102, 104, 108, 110–111, 113–114, 116–117, 120, 122–126, 46, 48–49, 51–52, 54, 56–57, 59, 61–62, 69–70, 72, 74–75, 77–78, 81, 83–85, 87–88, 92, 94, 96
   SolidMechanicsDriver.cxx80%100%100%78.69%104, 106–107, 110, 112, 114, 117, 119, 133–135, 43, 73
src/neml2/misc
   error.cxx100%100%100%100%
   math.cxx99.44%100%100%99.33%252
   parser_utils.cxx98.46%100%100%98.18%73
   types.cxx100%100%100%100%
   utils.cxx89.47%100%100%86.67%44, 52
src/neml2/models
   BackwardEulerTimeIntegration.cxx93.48%100%77.78%97.30%42
   BufferStore.cxx33.33%100%33.33%33.33%36, 38, 40, 44, 46, 48–50
   ComposedModel.cxx96.91%100%100%96.67%39, 52, 58
   Data.cxx100%100%100%100%
   ForceRate.cxx97.87%100%100%97.56%41
   ForwardEulerTimeIntegration.cxx97.50%100%100%97.06%41
   ImplicitUpdate.cxx98.18%100%100%98.04%39
   LinearInterpolation.cxx61.45%100%40.74%100%
   Model.cxx91.41%100%93.94%91.03%101–104, 137–139, 236, 238–241, 247, 370, 372–374, 376–377, 41
   NonlinearParameter.cxx61.90%100%55.56%100%
   ParameterStore.cxx48.08%100%22.73%66.67%101, 47, 49, 51–53, 66, 93, 95–96
   RotationMatrix.cxx95%100%100%94.12%38
   SR2Invariant.cxx96.23%100%100%96%104, 40
   StateRate.cxx93.62%100%66.67%97.56%41
   SumModel.cxx96.43%100%100%95.45%41
   VariableStore.cxx94.25%100%91.67%94.67%165–166, 63, 65
   WR2ExplicitExponentialTimeIntegration.cxx97.06%100%100%96.77%39
   WR2ImplicitExponentialTimeIntegration.cxx97.22%100%100%96.97%43
src/neml2/models/crystallography
   CrystalGeometry.cxx98.37%100%100%98.13%186, 49
   CubicCrystal.cxx92.31%100%100%90.91%48
   MillerIndex.cxx100%100%100%100%
   crystallography.cxx98.28%100%100%98.08%142
src/neml2/models/crystallography/user_tensors
   FillMillerIndex.cxx93.33%100%100%91.67%40
   SymmetryFromOrbifold.cxx90.91%100%100%88.89%41
src/neml2/models/solid_mechanics
   AssociativeIsotropicPlasticHardening.cxx96.43%100%100%96%40
   AssociativeKinematicPlasticHardening.cxx96.55%100%100%96.15%41
   AssociativePlasticFlow.cxx96.15%100%100%95.65%39
   ChabochePlasticHardening.cxx97.37%100%100%97.14%43
   ElasticStrain.cxx96.55%100%100%96.15%41
   Elasticity.cxx94.44%100%100%93.75%38
   FlowRule.cxx90%100%100%87.50%35
   GTNYieldFunction.cxx54.12%100%100%53.29%139, 159–162, 165, 168–171, 188, 191, 194, 197, 224–228, 231, 234–237, 249, 251, 253–258, 260–264, 266–267, 269–273, 285, 287, 289–290, 292–293, 295–296, 308–311, 313, 315–319, 321–325, 327–328, 330–331, 343, 45, 84–85, 88, 91, 94
   GursonCavitation.cxx96.55%100%100%96.15%40
   IsotropicHardening.cxx91.67%100%100%90%36
   IsotropicMandelStress.cxx100%100%100%100%
   KinematicHardening.cxx91.67%100%100%90%36
   LinearIsotropicElasticity.cxx96.15%100%100%95.65%39
   LinearIsotropicHardening.cxx94.12%100%100%92.86%37
   LinearKinematicHardening.cxx94.12%100%100%92.86%38
   

@reverendbedford reverendbedford merged commit b6b7ede into main Apr 17, 2024
36 checks passed
@reverendbedford reverendbedford deleted the pybind branch April 17, 2024 18:06
@hugary1995 hugary1995 mentioned this pull request Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants