https://github.com/cloneofsimo/ezmup

Simple implementation of muP, based on Spectral Condition for Feature Learning. The implementation is SGD only, dont use it for Adam

https://github.com/cloneofsimo/ezmup

Science Score: 23.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
    Links to: arxiv.org
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (12.1%) to scientific vocabulary
Last synced: 11 months ago · JSON representation

Repository

Simple implementation of muP, based on Spectral Condition for Feature Learning. The implementation is SGD only, dont use it for Adam

Basic Info
Statistics
  • Stars: 75
  • Watchers: 5
  • Forks: 3
  • Open Issues: 4
  • Releases: 0
Created over 2 years ago · Last pushed about 2 years ago
Metadata Files
Readme

README.md

Minimal Implementation of muP (Maximal Update Parametrization) that happens to be also Easy

This is radical implementation of the muP algorithm (Maximal Update Parametrization) for the paper Tensor Programs V: Tuning Large Neural Networks via Zero-Shot Hyperparameter Transfer and A Spectral Condition for Feature Learning, series of research driven by Greg Yang. This is not an official implementation, which can be found here.

What is muP?

  1. You want to train a large NN with pytorch (you have a width as a hyperparameter)
  2. You want to find optimal hyperparameters, such as learning rate, adam beta, lr scheduler, init varaince, etc (see list of hparams that are relevent here. Note that not all hparams are relevant to muP!)
  3. So of course you are going to search for the optimal hparams using the small model, and apply it to the big model... right?

Alt text

But as you all know, with default pytorch settings, you can't do this! You often have to search the hparams for the big model, because hparams you find on the small model is often not applicable to the big model.

However, this can be fixed! muP is a method of scaling width so that you can search the hyperparams on the smaller model (small width), and then apply it to the big model (large width): you can transfer the hparams from small model to big model!, that saves you a lot of time and money!

Alt text

You see? in practice, your optimal Learing rate found with width 128 does not transfer to width 8192. But with muP, you can transfer the hyperparameters from small model to big model! Figure directly from Tensor Programs V

How to use this repo?

This package suggest the following approach:

First, in your project, change the config so that some weird large enough prime number represents the varying width. By weird, I mean such number should not be used in your other hyperparameters of model shapes. 47 is such good number.

```python

from ezmup import Ezmup

model = MyModel(ffwidth = 47 * 32, hiddendim = 47 * 8, headdim = 47, numlayers = 4, ...)

```

Then, you can use the ezmup package so that it changes all of the width variable to value you want.

```python mupengine = Ezmup(widthbasis = 47, model = model) # this will treat 47 as a width variable. i.e., replace 47 occuring in all of the model as a variable length.

do hyperparameter-search, such as scaling, with fixed width. here, we take 32.

mupengine.changewidth_as(32)

optimizer = mupengine.getoptimizer(lr = test_lr)

now after you get the best parameters, you can apply them to your desired width.

mupengine.changewidth_as(1024)

if you want to use the model for other purposes, say, other frameworks just save them as state-dict, safetensors, etc.

torch.save(model.state_dict(), 'model.pt')

```

In that way, we can

  1. Use muP in our own projects without too much hassle (just set the varying-width you desire to be prime number like 47! Explained later)
  2. Check the correctness of muP via Coord-Checking

Oh well, this is exactly how you use this package! The code is very minimal, so do have a look as well.

Installation

bash python3 -m pip install ezmup+https://github.com/cloneofsimo/ezmup.git

Other methods:

The code does two things:

  1. Without needing to replace the code-layers in your implementation, it changes the width variable to the value you want, by actually replacing the neural network parameters into desired ones. While doing that, it will use a standard scaling InitStd, which is also some value you can change (per layer!)

  2. It also tells you the learning-rate scaling factor you need per layer, which is a varying factor depending on the width.

Say you have know that you want different learning rate for different layers such as Embedding and FFN.

Then, you can do the following:

```python

You can do it manually by getting the parametername, lrscaling dictionary.

mupengine = Ezmup(widthbasis = 47, model = model) mupscaling = mupengine.lrscalingdict

lr_dict = { 'embedding' : 1e-3, 'ffn' : 1e-4 }

optimizergroups = [ {'params' : [p] , 'lr' : lrdict[name] * mupscaling[pname] } for pname, p in model.namedparameters() ] optimizer = Adam(optimizer_groups, **kwargs) ```

List of hyperparameters

Not all hyperparameters are relevant to muP. Here is the list of hyperparameters that are relevant to muP:

Alt text

So what is with the prime number?

Ok so the difficulty of muP implementation for the drop-in replacement is that you need to change the width variable automagically. muP requires you to identify which fan_in or fan_out refers to infinite-width.

So, the idea is to use a prime number as a width variable, which is then used as an indicator via dividability : if some shape is multiple of this prime number, then it is infinite-width. With design choice, users don't have to manually specify which shape is infinite-width, nor change the code.

Coord-Checking

You can see how to run the coord checking in the example.py file. The result should looks as:

Alt text

For Developers

Installation

bash git clone https://github.com/cloneofsimo/ezmup.git cd ezmup python3 -m pip install ".[dev]"

Build docs

bash sphinx-apidoc -f -o docs/source ezmup cd docs make html

Update requirements.txt and requirements-dev.txt from pyproject.toml (Use pip-compile)

bash pip-compile -o requirements.txt pyproject.toml pip-compile --extra dev -o requirements-dev.txt pyproject.toml

Owner

  • Name: Simo Ryu
  • Login: cloneofsimo
  • Kind: user
  • Company: Corca AI

Cats are Turing machines cloneofsimo@gmail.com

GitHub Events

Total
  • Watch event: 13
  • Fork event: 1
Last Year
  • Watch event: 13
  • Fork event: 1

Committers

Last synced: over 1 year ago

All Time
  • Total Commits: 14
  • Total Committers: 2
  • Avg Commits per committer: 7.0
  • Development Distribution Score (DDS): 0.286
Past Year
  • Commits: 0
  • Committers: 0
  • Avg Commits per committer: 0.0
  • Development Distribution Score (DDS): 0.0
Top Committers
Name Email Commits
Jongsu Liam Kim j****8@g****m 10
Simo Ryu c****o@g****m 4

Issues and Pull Requests

Last synced: over 1 year ago

All Time
  • Total issues: 3
  • Total pull requests: 2
  • Average time to close issues: N/A
  • Average time to close pull requests: 1 day
  • Total issue authors: 2
  • Total pull request authors: 2
  • Average comments per issue: 1.0
  • Average comments per pull request: 0.5
  • Merged pull requests: 1
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 3
  • Pull requests: 1
  • Average time to close issues: N/A
  • Average time to close pull requests: N/A
  • Issue authors: 2
  • Pull request authors: 1
  • Average comments per issue: 1.0
  • Average comments per pull request: 0.0
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • sxyu (2)
  • saeejithnair (1)
Pull Request Authors
  • appleparan (2)
  • emergenz (2)
Top Labels
Issue Labels
Pull Request Labels

Dependencies

requirements.txt pypi
  • numpy *
  • pandas *
  • torch *
setup.py pypi
  • for *
  • open *
  • str *
.github/workflows/tag-and-release.yml actions
  • actions/checkout v4 composite
  • ghalactic/github-release-from-tag v5 composite
  • shogo82148/actions-check-permissions v1 composite
pyproject.toml pypi
  • matplotlib *
  • numpy *
  • pandas *
  • torch *
requirements-dev.txt pypi
  • accessible-pygments ==0.0.4 development
  • alabaster ==0.7.13 development
  • babel ==2.14.0 development
  • beautifulsoup4 ==4.12.2 development
  • black ==23.12.1 development
  • build ==1.0.3 development
  • certifi ==2023.11.17 development
  • charset-normalizer ==3.3.2 development
  • click ==8.1.7 development
  • contourpy ==1.2.0 development
  • coverage ==7.4.0 development
  • cycler ==0.12.1 development
  • docutils ==0.20.1 development
  • filelock ==3.13.1 development
  • fonttools ==4.47.0 development
  • fsspec ==2023.12.2 development
  • idna ==3.6 development
  • imagesize ==1.4.1 development
  • iniconfig ==2.0.0 development
  • jinja2 ==3.1.2 development
  • kiwisolver ==1.4.5 development
  • markdown-it-py ==3.0.0 development
  • markupsafe ==2.1.3 development
  • matplotlib ==3.8.2 development
  • mdit-py-plugins ==0.4.0 development
  • mdurl ==0.1.2 development
  • mpmath ==1.3.0 development
  • mypy-extensions ==1.0.0 development
  • myst-parser ==2.0.0 development
  • networkx ==3.2.1 development
  • numpy ==1.26.3 development
  • nvidia-cublas-cu12 ==12.1.3.1 development
  • nvidia-cuda-cupti-cu12 ==12.1.105 development
  • nvidia-cuda-nvrtc-cu12 ==12.1.105 development
  • nvidia-cuda-runtime-cu12 ==12.1.105 development
  • nvidia-cudnn-cu12 ==8.9.2.26 development
  • nvidia-cufft-cu12 ==11.0.2.54 development
  • nvidia-curand-cu12 ==10.3.2.106 development
  • nvidia-cusolver-cu12 ==11.4.5.107 development
  • nvidia-cusparse-cu12 ==12.1.0.106 development
  • nvidia-nccl-cu12 ==2.18.1 development
  • nvidia-nvjitlink-cu12 ==12.3.101 development
  • nvidia-nvtx-cu12 ==12.1.105 development
  • packaging ==23.2 development
  • pandas ==2.1.4 development
  • pathspec ==0.12.1 development
  • pillow ==10.2.0 development
  • pip-autoremove ==0.10.0 development
  • pip-tools ==7.3.0 development
  • platformdirs ==4.1.0 development
  • pluggy ==1.3.0 development
  • pydata-sphinx-theme ==0.15.1 development
  • pygments ==2.17.2 development
  • pyparsing ==3.1.1 development
  • pyproject-hooks ==1.0.0 development
  • pytest ==7.4.4 development
  • pytest-cov ==4.1.0 development
  • pytest-html ==4.1.1 development
  • pytest-metadata ==3.0.0 development
  • python-dateutil ==2.8.2 development
  • pytz ==2023.3.post1 development
  • pyyaml ==6.0.1 development
  • requests ==2.31.0 development
  • ruff ==0.1.11 development
  • six ==1.16.0 development
  • snowballstemmer ==2.2.0 development
  • soupsieve ==2.5 development
  • sphinx ==7.2.6 development
  • sphinxcontrib-applehelp ==1.0.7 development
  • sphinxcontrib-devhelp ==1.0.5 development
  • sphinxcontrib-htmlhelp ==2.0.4 development
  • sphinxcontrib-jsmath ==1.0.1 development
  • sphinxcontrib-qthelp ==1.0.6 development
  • sphinxcontrib-serializinghtml ==1.1.9 development
  • sphinxcontrib-youtube ==1.4.1 development
  • sympy ==1.12 development
  • torch ==2.1.2 development
  • triton ==2.1.0 development
  • typing-extensions ==4.9.0 development
  • tzdata ==2023.4 development
  • urllib3 ==2.1.0 development
  • wheel ==0.42.0 development