How to contribute#

Everyone can contribute to Flax, and the Flax development team values everyone’s contributions! You can contribute in many more ways than just writing code. Answering questions on the Flax GitHub Discussions page, helping each other, and improving Flax documentation are extremely valuable to the Flax ecosystem.

We also appreciate if you spread the word, for instance by starring the Flax GitHub repository, or referencing Flax in blog posts of projects that used it.

This project follows Google’s Open Source Community Guidelines.

Ways to contribute#

We welcome pull requests (PRs), in particular for those issues marked as PR-ready. For other proposals, you should first open a GitHub Issue or a GitHub Discussion to start a conversation about your planned contribution.

Contributing code using pull requests#

The Flax development team performs all development using Git. To contribute, you should have basic knowledge of Git and GitHub. (You can learn how to set up Git by following Git’s official Getting Started - First-Time Git Setup and GitHub’s Set Up Git guides.)

To contribute code to Flax on GitHub, follow these steps:

To create a pull request from a fork#

  1. Using GitHub’s web UI, fork the Flax repository by clicking the ‘Fork’ button on the github.com/google/flax repository page. This creates a fork (a copy) of the Flax repository in your own GitHub.

    Reference: Creating a pull request from a fork.

  2. Install Python >=3.7.

  3. (Optional) Create a virtual environment or a Docker container. See dev/README.md for details on how to set up a Docker Container. To set up a virtual environment, run the following:

    python3 -m virtualenv env
    . env/bin/activate
    

    This ensures all your dependencies are installed in this environment.

  4. Clone your local forked Flax repo with git clone. Then, install the required packages with PyPi. This enables you to immediately test the code after modifying it:

    git clone https://github.com/YOUR_USERNAME/flax
    cd flax
    pip install -e ".[all]"
    pip install -e ".[testing]"
    pip install -r docs/requirements.txt
    
  5. Set up pre-commit hooks, this will run some automated checks during each git commit and possibly update some files that require changes.

    pip install pre-commit
    pre-commit install
    
  6. Add the Google Flax repo (not your fork) as an upstream remote, so you can use it to sync your changes.

    git remote add upstream http://www.github.com/google/flax
    
  7. Create a branch, such as my_development_branch, you will develop from:

    git checkout -b my_development_branch
    
  8. Implement your changes using your favorite editor (we recommend Visual Studio Code).

    Make sure the tests pass by running the following command from the top of the repository:

    ./tests/run_all_tests.sh
    
  9. Once you finish making changes, don’t forget to create commits (learn how to write a commit message):

    git add file1.py file2.py ...
    # or use `git add .` to add all changed files
    git commit -m "Your commit message"
    

    Then sync your code with the main repository:

    git fetch upstream
    git rebase upstream/main
    
  10. Finally, push your commit on your my_development_branch, and create a remote branch in your fork that you can use to create a pull request from:

git push --set-upstream origin my_development_branch

After running the command, you should get a GitHub link in your (VS Code) terminal output for creating a pull request. If you don’t receive a link after git push, use the GitHub web UI to create a pull request.

  1. Make sure your pull request passes the Flax PR checklist. If so, create a pull request from the Flax repository and send it for review. Consult GitHub Help for more information on using pull requests.

You can learn more in GitHub’s Creating a pull request from a fork . documentation.

Updating Jupyter Notebooks#

We use jupytext to maintain two synced copies of docs in docs/notebooks: one in the Jupyter Notebook (.ipynb) format, and one in Markdown (.md).

The former can be opened and executed directly in Google Colab. Markdown makes it easier to track changes/diffs within version control and, for example, GitHub web UI, since .ipynb files are based on JSON.

Editing Jupyter Notebooks (.ipynb)#

For making large changes that substantially modify code and outputs, it’s recommended to edit the notebooks in Jupyter or in Colab.

If you choose to work in Colab, go to File and click Upload notebook, then pick your file. After loading it into Colab and editing it, make sure you run the cells, and that there aren’t any errors. Click on Runtime, then select Run all. After you finish, click File > Download > Download ipynb. You may also want to test that the file executes properly by using sphinx-build, as explained above.

After you make changes in your Jupyter Notebook, follow the steps Syncing notebooks below.

Editing Markdown files (.md)#

For making smaller changes to the text content of the notebooks, it is easiest to edit the .md versions using a text editor.

After you make changes in your Markdown file, follow the steps Syncing notebooks below.

Syncing notebooks#

After editing either the .ipynb or .md versions of the docs, sync the two versions using jupytext by running jupytext --sync on the updated notebooks.

First, make sure you have jupytext installed. The jupytext version should match the one specified in .pre-commit-config.yaml (currently, it is v1.13.8).

pip install jupytext==1.13.8

Then, after you have made your changes in the Jupyter Notebook, sync the contents with its Markdown-equivalent file by running the following command:

jupytext --sync path/to/the/file.ipynb

Similarly, to sync your Markdown file with its Jupyter Notebook version, run:

jupytext --sync path/to/the/file.md

Note that if you receive an error, and it is the first time you worked in a Jupyter Notebook, you may need to (re)create a synced copy of the document (which is explained in detail in Creating new notebooks section below):

jupytext --set-formats ipynb,md:myst path/to/the/notebook.ipynb

Once you’re finished with syncing the .md and .ipynb files, you can check that they are properly synced using the pre-commit framework to perform the same checks used in the Flax GitHub CI:

git add docs -u  # pre-commit runs on files in git staging.
pre-commit run jupytext

Creating new notebooks#

If you are adding a new Jupyter Notebook to the documentation, you can use jupytext --set-formats. It can set up both the Jupyter Notebook (.ipynb) and Markdown (.md) versions of the file:

jupytext --set-formats ipynb,md:myst path/to/the/notebook.ipynb

This works by adding a "jupytext" metadata field to the notebook file which specifies the desired formats. The jupytext --sync command can then recognize them when invoked.

After you make changes in your file(s), follow the steps from the Syncing notebooks section above to keep the contents of both Markdown and Jupyter Notebook files in sync.

Notebooks within the Sphinx build#

Some of the notebooks are built automatically as part of the pre-submit checks and as part of the Read the Docs build. The build will fail if cells raise errors. If the errors are intentional, you can either catch them, or tag the cell with raises-exceptions metadata (example PR). You have to add this metadata by hand in the .ipynb file. It will be preserved when somebody else re-saves the notebook.

We exclude some notebooks from the build because, for example, they contain long computations. See exclude_patterns in conf.py.

Updating the pull request contents#

Every pull request should ideally be limited to just one commit, so if you have multiple commits please squash them.

Assuming you now have only one commit in your pull request, and want to add changes requested during review:

  1. Make the changes locally in your editor.

  2. Run git commit -a --amend. This updates the commit contents and allows you to edit the commit message.

  3. At this point, git push alone will result in an error. Instead, use git push --force.

  4. Check that it’s done: The changes to your commit should be immediately reflected in the Github web UI.

Troubleshooting#

Too many commits in a pull request#

If your PR has too many commits associated with it (for example, more than five), you need to squash them. Otherwise, the Flax docs build process may fail with an error message. This is because of the following reasons:

  • There are more than five commits in your pull request; and

  • The Flax source sync process fails when the commit tree is too large.

To squash your commits, you can rebase your branch to main and create a new commit containing all your changes, run the following command:

git rebase main && git reset --soft main && git commit

This will apply all your changes to the main branch. Note that if you had to resolve any conflicts while working on your change (for instance, you did a pull upstream main which led to conflict), then you will have to resolve these conflicts again.

After you have successfully rebased your branch, you should push your changes. And because you changed the commit history, you may have to use git push --force.

Contributor License Agreement#

Contributions to this project must be accompanied by a Contributor License Agreement. You (or your employer) retain the copyright to your contribution; this simply gives us permission to use and redistribute your contributions as part of the project. Head over to https://cla.developers.google.com/ to see your current agreements on file or to sign a new one.

You generally only need to submit a CLA once, so if you’ve already submitted one (even if it was for a different project), you probably don’t need to do it again.