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 options for eval and gradient required #78

Closed
wants to merge 3 commits into from

Conversation

ElliottKasoar
Copy link
Contributor

@ElliottKasoar ElliottKasoar commented Dec 5, 2023

Resolves #73

Adds flags in all(?) functions that operate on tensors (tensor creation, model loading, forward) to optionally disable autograd, which should improve performance for inference.

Also adds a similar flag to set evaluation mode for the loaded model.

  • Evaluation mode

    • Contrary to my initial comments in Loaded TorchScript missing no_grad context  #73, from testing evaluation mode does appear to be preserved, both between saving and loading TorchScript, and when applied to the loaded model.
    • In most cases evaluation mode is therefore likely to already be set, but I think it's useful to have the option to change it, particularly if FTorch may be extended to facilitate training (Training functionality #22).
  • NoGradMode

    • Enabling or disabling gradients is more complicated, as it defined via a context manager, which only appears to define the behaviour within its own scope, and so it seems necessary to enable/disable gradients before every code block that operates on tensors (similar to the Python equivalent with torch.no_grad():).
  • InferenceMode

    • No changes are currently included, but it would be good to support InferenceMode too eventually, as it should provide further performance benefits over NoGradMode.
    • However, it has stricter requirements, and the mode was only added (as a beta) in PyTorch 1.9, so we would need to be careful if we want to support older versions.
  • Model freezing

    • No changes are currently included, and less directly applicable to the main FTorch library, although there are still interactions e.g. freezing the model can allow InferenceMode to be enabled when loading the model.
    • Freezing is currently the "default" when tracing in pt2ts.py, but not for scripting, despite potentially improving performance.
    • Freezing appears to (sometimes) introduce numerical errors when saving the reloading (differences ~10^-6), and can seem to lead to issues loading with Forpy too.

(For more general explanation of autograd/evaluation mode, see autograd mechanics).

Note: I've also removed the old, commented out torch_from_blob function.

@ElliottKasoar ElliottKasoar marked this pull request as ready for review December 12, 2023 11:22
Copy link
Member

@TomMelt TomMelt left a comment

Choose a reason for hiding this comment

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

thanks @ElliottKasoar , these changes look great to me

@jatkinson1000
Copy link
Member

jatkinson1000 commented Dec 18, 2023

I'll review this in the new year.
I see @TomMelt is happy, but there are a lot of changes so I also want to be sure I've understood them.

@jatkinson1000
Copy link
Member

A new PR rebasing this work on top of the latest changes to resolve conflicts is at #103

@jatkinson1000
Copy link
Member

Since #103 is progressing I am closing this.

jatkinson1000 added a commit that referenced this pull request Apr 8, 2024
 Add options for eval and gradient required - Rebase of #78

Allows users to enable/disable gradient calculations on Torch objects for efficiency, and set models to evaluation mode.
Defaults are gradients off and eval mode on.
@jatkinson1000 jatkinson1000 deleted the add-opt-options branch April 9, 2024 15:48
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.

Loaded TorchScript missing no_grad context
3 participants