pytorch-optimizer

repository·main·Indexed 19 days ago

https://github.com/kozistr/pytorch_optimizer

A production-focused optimization toolkit for PyTorch providing over 100 optimizers, 10+ learning rate schedulers, and 10+ loss functions through a consistent API. It includes advanced research-oriented algorithms such as Sophia, GaLore, Lion, and SAM, as well as 8-bit optimizers via bitsandbytes integration. The library supports high-level helper functions like create_optimizer() and provides foreach implementations for CUDA speedups.

Tokens
11.4K
Snippets
31
Records
64
Agent score
63%

What's inside pytorch-optimizer

  1. Compare Foreach vs Regular Optimizer performance

    main

    The pytorch_optimizer library supports foreach implementations for its optimizers. Using foreach=True typically provides a speedup in execution time on CUDA devices by utilizing vectorized operations, though it may increase peak memory usage.

    Based on benchmarks using an MLP model (29M parameters) on an NVIDIA GeForce GTX 1060, the average speedup for foreach implementations is approximately 1.13x.

    Key Trade-offs:

    • Speed: Most optimizers (e.g., AdaFactor, Lion, SGDW) show significant speedups (ranging from 1.1x to 1.3x).
    • Memory: foreach often results in higher peak memory consumption (e.g., AdaFactor saw a +75.3% increase). Some optimizers like LARS and SGDW show 0.0% memory difference.
    • Exceptions: Some optimizers like Adan and ADOPT may perform slightly slower or show increased memory overhead when using foreach.
  2. Use the `foreach` feature for faster training

    main

    Many optimizers in pytorch_optimizer (including AdaFactor, StableAdamW, Lion, AdaBelief, and Amos) now support the foreach implementation. This can improve training speed by 1.1x to 1.5x at the cost of moderate memory usage.

    • Default behavior: Like official PyTorch optimizers, the default is foreach=None. When set to None, CUDA paths will automatically prefer the foreach implementation over the standard for-loop implementation.
    • Disabling foreach: If you require the legacy for-loop behavior, explicitly set foreach=False.
  3. View optimizer performance visualizations

    main
    The pytorch_optimizer repository provides visual comparisons of various optimizers performing on standard benchmark functions, specifically the Rastrigin and Rosenbrock functions. These visualizations can help in understanding the convergence behavior and landscape traversal characteristics of different optimization algorithms available in the library.
  4. Install pytorch-optimizer with bitsandbytes support

    main

    To use the 8-bit bitsandbytes optimizers, you must install the package with the bitsandbytes extra. This enables support for the following 8 optimizers:

    • bnb_adagrad8bit
    • bnb_adam8bit
    • bnb_adamw8bit
    • bnb_lion8bit
    • bnb_lamb8bit
    • bnb_lars8bit
    • bnb_rmsprop8bit
    • bnb_sgd8bit
    pip3 install pytorch-optimizer[bitsandbytes]
  5. Run optimizer visualizations

    main

    To run the built-in visualizations for the optimizers, execute one of the following commands from the project root:

    1. Using just: just visualize
    2. Using python module execution: python3 -m examples.visualize_optimizers
    # Option 1
    just visualize
    
    # Option 2
    python3 -m examples.visualize_optimizers
  6. Install pytorch-optimizer

    main

    Install the toolkit using pip.

    Requirements:

    • Python >=3.8
    • PyTorch >=1.10

    Optional Integrations: Some features require additional packages which are not installed by default:

    • bitsandbytes
    • q-galore-torch
    • torchao
    pip install pytorch-optimizer
  7. Use rectified versions of Lamb and diffGrad via the rectify parameter

    main

    In version 2.7.0, rectified versions of certain optimizers were merged into their original classes. Instead of using separate classes, you can now use the original optimizer and enable the rectified behavior by setting the rectify=True parameter.

    Specifically:

    • diffGrad now replaces the previous diffRGrad + diffGrad distinction.
    • Lamb now replaces the previous RaLamb + Lamb distinction.

    Example usage:

    # To use the rectified version of Lamb
    optimizer = Lamb(params, lr=1e-3, rectify=True)
    
    # To use the rectified version of diffGrad
    optimizer = diffGrad(params, lr=1e-3, rectify=True)
  8. Updated import paths for utility functions and projectors

    main

    Due to a major refactor, several utilities and projectors that were previously available via direct imports from pytorch_optimizer have been moved to specific submodules. If your code relies on these, you must update your import statements:

    Old PathNew Path
    pytorch_optimizer.GaLoreProjectorpytorch_optimizer.optimizers.galore.GaLoreProjector
    pytorch_optimizer.gradfilter_emapytorch_optimizer.optimizers.grokfast.gradfilter_ema
    pytorch_optimizer.gradfilter_mapytorch_optimizer.optimizers.grokfast.gradfilter_ma
    pytorch_optimizer.l2_projectionpytorch_optimizer.optimizers.alig.l2_projection
    pytorch_optimizer.flatten_gradpytorch_optimizer.optimizers.pcgrad.flatten_grad
    pytorch_optimizer.un_flatten_gradpytorch_optimizer.optimizers.pcgrad.un_flatten_grad
    pytorch_optimizer.reduce_max_except_dimpytorch_optimizer.optimizers.sm3.reduce_max_except_dim
    pytorch_optimizer.neuron_normpytorch_optimizer.optimizers.nero.neuron_norm
    pytorch_optimizer.neuron_meanpytorch_optimizer.optimizers.nero.neuron_mean
    pytorch_optimizer.[Shampoo stuff]pytorch_optimizer.optimizers.shampoo_utils.[Shampoo stuff] (e.g., Graft, BlockPartitioner, PreConditioner)
  9. Configure `zero_power_via_newton_schulz_5` coefficients

    main

    The Muon, DistributedMuon, AdaMuon, and AdaGO optimizers now support various coefficient presets for the zero_power_via_newton_schulz_5 method. You can use these presets or provide custom schedules via the ns_coeffs parameter.

    Available Presets:

    • original
    • quintic
    • polar_express
    • polar_express_safer