Giter VIP home page Giter VIP logo

automatic-mixed-precision-training---pytorch's Introduction

Automatic Mixed Precision Training

The default datatype when training a pytorch model is float32 , this can be costly so we have a workaround without having to sacrifice the precision of the training data which is AMP

What is Floating points operations ?

  1. Allows training deep learning models using a combination of 32-bit (float32) and 16-bit (float16) data types.
  2. Shorter training time: Mixed precision can provide a 1.5x to 5.5x speedup compared to training in full 32-bit precision on NVIDIA GPUs
  3. Lower memory requirements: The reduced precision allows training larger models, larger batches, or larger inputs within the same memory budget

benefits :

  • Shorter training time;
  • Lower memory requirements, enabling larger batch sizes, larger models, or larger inputs

Components :

  1. torch.autocast
  2. torch.cuda.amp.GradScaler

[AutoCast]

Instances of torch.autocast enable autocasting for chosen regions. Autocasting automatically chooses the precision for GPU operations to improve performance while maintaining accuracy.

[Gradient Scaling]

If the forward pass for a particular op has float16 inputs, the backward pass for that op will produce float16 gradients. Gradient values with small magnitudes may not be representable in float16. These values will flush to zero (“underflow”), so the update for the corresponding parameters will be lost.

To prevent underflow, “gradient scaling” multiplies the network’s loss(es) by a scale factor and invokes a backward pass on the scaled loss(es). Gradients flowing backward through the network are then scaled by the same factor. In other words, gradient values have a larger magnitude, so they don’t flush to zero.

Additional resources :

  1. https://pytorch.org/blog/what-every-user-should-know-about-mixed-precision-training-in-pytorch/
  2. https://pytorch.org/docs/stable/notes/amp_examples.html
  3. https://pytorch.org/blog/accelerating-training-on-nvidia-gpus-with-pytorch-automatic-mixed-precision/
  4. https://medium.com/@furkangozukara/what-is-the-difference-between-fp16-and-bf16-here-a-good-explanation-for-you-d75ac7ec30fa
  5. https://lih-verma.medium.com/pytorchs-magic-with-automatic-mixed-precision-b3bef6f4b1fd

automatic-mixed-precision-training---pytorch's People

Contributors

amr-abdellatif avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.