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

Space charge tests: improve backward-mode test ; add forward-mode test #339

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

RemiLehe
Copy link
Collaborator

@RemiLehe RemiLehe commented Feb 8, 2025

Description

This PR improves the space-charge tests:

  • It now tests the accuracy of the gradient calculation, for the backward-mode differentiation.
  • In addition, it adds a similar test for forward-mode differentiation.

Motivation and Context

Since Cheetah is built with pytorch, it supports backward-differentiation, but also forward differentiation (see this page). This could be useful since the scaling of forward differentiation is different from that of backward differentiation.

In addition, previous tests of backward-differentiation mode were simply checking that the differentiation ran without error, but they did not check the accuracy of the computed gradient. In this PR, we now test the accuracy of the gradient, by reusing the parameters of https://accelconf.web.cern.ch/hb2023/papers/thbp44.pdf, and by using an analytical formula to find the expected gradient.

Types of changes

New test

Checklist

  • I have updated the changelog accordingly (required).
  • My change requires a change to the documentation.
  • I have updated the tests accordingly (required for a bug fix or a new feature).
  • I have updated the documentation accordingly.
  • I have reformatted the code and checked that formatting passes (required).
  • I have have fixed all issues found by flake8 (required).
  • I have ensured that all pytest tests pass (required).
  • I have run pytest on a machine with a CUDA GPU and made sure all tests pass (required).
  • I have checked that the documentation builds (required).

@RemiLehe RemiLehe changed the title [WIP] Add a test for forward-mode differentiation in Cheetah Space charge tests: improve backward-mode test ; add forward-mode test Feb 9, 2025
@RemiLehe RemiLehe requested review from jank324 and cr-xu and removed request for jank324 February 9, 2025 04:15
num_particles=torch.tensor(10_000),
sigma_px=torch.tensor(2e-7),
sigma_py=torch.tensor(2e-7),

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This is reusing the same code as in the test test_cold_uniform_beam_expansion above.

@cr-xu
Copy link
Member

cr-xu commented Feb 11, 2025

Thanks for providing a forward AD test. I think similar tests could also be added to other parts, but we can do that in a separate PR.

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