How to contribute
How to contribute#
Everyone can contribute to Flax, and we value everyone’s contributions. You can contribute in many more ways than just writing code. Answering questions on our Discussions page, helping each other, and improving our documentation are extremely valuable to our ecosystem.
We also appreciate if you spread the word, for instance by starring our GitHub repo, 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#
We do all of our development using git, so basic knowledge is assumed.
Follow these steps to contribute code:
Create a Pull Request in your own branch#
Fork the Flax repository by clicking the ‘Fork’ button on the repository page. This creates a copy of the Flax repository in your own account.
Install Python >=3.6.
(Optional) Create a virtual environment or a Docker container. See
dev/README.mdfor 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.
Clone your local forked Flax repo, 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 ".[testing]" pip install -e . pip install -r docs/requirements.txt
Set up pre-commit hooks, this will run some automated checks during each
gitcommit and possibly update some files that require changes.
pip install pre-commit pre-commit install
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
Create a branch where you will develop from:
git checkout -b name-of-change
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:
Once your change is done, create a commit as follows (how to write a commit message):
git add file1.py file2.py ... git commit -m "Your commit message"
Then sync your code with the main repo:
git rebase upstream/main
Finally push your commit on your development branch and create a remote branch in your fork that you can use to create a Pull Request form:
git push --set-upstream origin name-of-change
After running the command, you should see a GitHub link in your terminal output that you can click on to create a Pull Request.
If you do not see this link in the terminal after doing a
git push, go to the GitHub web UI; there should be a button there that lets you turn the commit into a Pull Request yourself.
We use jupytext to maintain two synced copies of docs
docs/notebooks: one in the Jupyter Notebook (
.ipynb) format, and one in Markdown (
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 (
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.
Editing Markdown files (
For making smaller changes to the text content of the notebooks, it is easiest to edit the
.md versions using a text editor.
After editing either the
.md versions of the docs, sync the two versions
using jupytext by running
jupytext --sync on the updated
First, make sure you have jupytext (version 1.13.8) installed. The jupytext version should match the one specified in .pre-commit-config.yaml.
pip install jupytext==1.13.8
Then, if you worked on a Jupyter Notebook document, 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
To check that the
.ipynb files are properly synced, you can also use the
pre-commit framework to perform the same checks used
in the 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
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.
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:
Make the changes locally in your editor.
git commit -a --amend. This updates the commit contents and allows you to edit the commit message.
At this point,
git pushalone will result in an error. Instead, use
git push --force.
Check that it’s done: The changes to your commit should be immediately reflected in the Github web UI.
Too many commits in a PR#
If your PR has too many commits associated with it, then our build process may fail with an error message. This is because of two reasons:
We prefer to keep our commit history clean.
Our source sync process will fail if our commit tree is too large.
If you encounter this error message, you should squash your commits. To
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
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.