-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Add Differentiable Physics: Mass-Spring System example #1332
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
base: main
Are you sure you want to change the base?
Conversation
✅ Deploy Preview for pytorch-examples-preview canceled.
|
@msaroufim I have just updated the example with PR, request you to check if it fits in the "differentiable physics" section |
I don't see any testing changes that were made, you probably want to update this file https://github.com/pytorch/examples/blob/main/run_python_examples.sh I do also think it's worth making some visualization for the work and putting it in the main README |
I have updated the PR.Thanks |
Updated all the changes. |
@msaroufim , can you please review the PR? Please lemme know if something else is required for this example. Thanks :) |
@@ -0,0 +1 @@ | |||
torch |
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.
Hi @AbhiLegend,
matplotlib
dependency is missing.
|
||
|
||
def train(args): | ||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
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.
It would be better to use accelerator API here
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
device = torch.accelerator.current_accelerator() if torch.accelerator.is_available() else torch.device("cpu") | |
print(f"Using device: {device}") |
|
||
- Python 3.8+ | ||
- PyTorch | ||
|
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.
matplotlib is missing
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.
Add
pip install -r requirements.txt |
to be consistent with other examples
|
||
|
||
def evaluate(args): | ||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") |
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.
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | |
device = torch.accelerator.current_accelerator() if torch.accelerator.is_available() else torch.device("cpu") | |
print(f"Using device: {device}") |
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.
@jafraustro Final update:
- Added accelerator code to
mass_spring.py
- requirements.txt updated with matplotlib dependency
Ready for review
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.
Hi, I do not see matplotlib
in the requirements.txt file
@jafraustro I made the changes. |
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.
LGTM
run_python_examples.sh
Outdated
# | ||
# To test examples on CUDA accelerator, run as: | ||
# USE_CUDA=True ./run_python_examples.sh | ||
# To run specific examples: |
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 think these changes be better moved to separate PR. These are unrelated to the example being added and some are arguable.
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.
Made it to as it was.
run_python_examples.sh
Outdated
# | ||
# To test examples on hardware accelerator (CUDA, MPS, XPU, etc.), run as: | ||
# USE_ACCEL=True ./run_python_examples.sh | ||
# NOTE: USE_ACCEL relies on torch.accelerator API and not all examples are converted |
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.
Why a note was dropped? it highlights the planned work which is not yet done and I believe is helpful for maintenance.
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.
Changed
run_python_examples.sh
Outdated
# | ||
# Script requires uv to be installed. When executed, script will install prerequisites from |
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.
This highlights the convention that each example is expected to have requirements.txt
and discusses the behavior. Can this, please, be restored?
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.
restored to original
run_python_examples.sh
Outdated
# | ||
# To force script to create virtual environment for each example, run as: | ||
# To force venv per example: |
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.
venv
might be confused with the type of virtual environment. I would prefer to use full naming "virtual environment per example".
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.
changed
run_python_examples.sh
Outdated
# VIRTUAL_ENV=".venv" ./run_python_examples.sh | ||
# Script will remove environments it creates in a teardown step after execution of each example. |
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.
Again, I don't see why this needs to be dropped. That's explicit clarification of the behavior. It's helpful.
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.
Rectified.
run_python_examples.sh
Outdated
|
||
BASE_DIR="$(pwd)/$(dirname $0)" | ||
source $BASE_DIR/utils.sh | ||
|
||
# TODO: Leave only USE_ACCEL and drop USE_CUDA once all examples will be converted |
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.
This work is not completed. Why drop the TODO?
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.
Made the changes.
run_python_examples.sh
Outdated
echo "training..." | ||
uv run train.py --epochs 1 --dev_every 1 --no-bidirectional --dry-run || error "couldn't train snli" | ||
} | ||
|
||
function fx() { | ||
# uv run custom_tracer.py || error "fx custom tracer has failed" UnboundLocalError: local variable 'tabulate' referenced before assignment |
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.
cleanup, here and in few places below. But someone needs to clarify whether this can be dropped or not. In any case - better not to mix up such changes with the new example.
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.
Updated.
run_python_examples.sh
Outdated
pushd differentiable_physics | ||
python -m uv run mass_spring.py --mode train --epochs 5 --steps 3 || error "differentiable_physics example failed" | ||
popd |
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.
What's the reason to use different pattern for tests just for this example? One potential problem - python
might not actually exist on the system as typically you get python3
and need to create a symbolic link to it in one way or another.
pushd differentiable_physics | |
python -m uv run mass_spring.py --mode train --epochs 5 --steps 3 || error "differentiable_physics example failed" | |
popd | |
uv run mass_spring.py --mode train --epochs 5 --steps 3 || error "differentiable_physics example failed" |
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.
Rectified it.
@dvrogozh made the changes as perrequest. |
run_python_examples.sh
Outdated
exit 1 | ||
fi | ||
|
||
fi |
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.
add the empty line at the end
run_python_examples.sh
Outdated
@@ -154,6 +154,9 @@ function vision_transformer() { | |||
|
|||
function word_language_model() { | |||
uv run main.py --epochs 1 --dry-run $CUDA_FLAG --mps || error "word_language_model failed" | |||
for model in "RNN_TANH" "RNN_RELU" "LSTM" "GRU" "Transformer"; do |
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.
This change is unrelated with the PR. Remove. Or just rebase on top of latest main.
@@ -0,0 +1,3 @@ | |||
torch |
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.
Due to usage of torch.accelerator
:
torch | |
torch>=2.6 |
@dvrogozh @jafraustro I have made the changes. |
Summary
This PR adds a simple Differentiable Physics example implementing a Mass-Spring System using PyTorch.
Files Added
examples/differentiable_physics/mass_spring.py
examples/differentiable_physics/README.md
examples/differentiable_physics/requirements.txt
Features
Testing
mass_spring.py
runs successfully in bothtrain
andeval
modes.run_python_examples.sh
for CI testing (5 epochs, 3 steps for quick test).python -m uv
is used for compatibility).Notes
Thank you for reviewing!