Skip to main content

Installing packages

Author

Marie-Hélène Burle

For this course, we have already installed the packages, but this section is important for you when you want to install packages on your machine or on the Alliance clusters.

On your machine

On your machine (but not on the Alliance clusters), I recommend that you use uv to create a Python project with your chosen Python version and all the necessary packages. uv installs packages much faster than pip and it is able to resolve dependencies very well. It also manages Python versions.

If you want more information on this, I gave a webinar on uv in May 2025.

Create a Python project (let’s call it jaxdl) and cd into it:

uv init --bare jaxdl
cd jaxdl

--bare creates a uv project without files that I am not interested in here such as a README and a main.py.

Install the packages:

uv add "jax[cuda13]" jax-ai-stack datasets matplotlib penzai torchvision tqdm transformers

Quick explanation of packages we are installing:

- jax[cuda13]           ➔ only if you want to run JAX on GPUs
- jax-ai-stack          ➔ installs JAX for the CPU (if not already installed for the GPU),
                                   Flax—the main NN library,
                                   Optax—optimizers & loss functions,
                                   Orbax—for checkpointing,
                                   Grain—to build efficient dataloaders,
                                   Chex—a library of utilities for JAX,
                                   ml_dtypes—NumPy dtype extensions for deep learning
- datasets              ➔ from Hugging Face—to load data
- matplotlib            ➔ to visualise samples
- penzai                ➔ to have interactive model display
- torchvision           ➔ to augment the data to prevent over-fitting
- tqdm                  ➔ progress bar
- transformers          ➔ from Hugging Face—to load pretrained weights

You will see that the dependencies have automatically populated a pyproject.toml file and that a virtual environment called .venv was created.

As long as you are within the project, you don’t need to activate that virtual environment: all you need to do is run python/jupyter lab/ipython/ptpython/a .py script after uv run.

Examples:

$ uv run python
$ uv run jupyter lab
$ uv run ptpython
$ uv run my_script.py

You can even launch Jupyter lab within a project without installing it in that project by using:

$ uv run --with jupyter jupyter lab

Alternatively, if you require a tighter pip equivalent, you can activate it as you would any other Python virtual environment:

source .venv/bin/activate

Sharing uv projects

To share your uv project environment (for instance for collaboration), put the pyproject.toml and uv.lock files under version control (or distribute them to your collaborators by other means): they pin every version of Python and the installed packages.

A collaborator can recreate the environment (install the proper Python version if missing and install all packages at the pinned versions) by cd-ing into the project and running:

$ uv sync

On an Alliance cluster

I already installed all the necessary packages in the training cluster to save time and space. The instructions for today thus differ from what you would normally do and production cluster instructions in the second tab are for your future reference only.

Look for available Python modules:

module spider python

Load the version of your choice:

module load python/3.13.2

The Hugging Face Datasets package uses PyArrow for efficiency. In order to install it, we also need to load an Arrow module.

Let’s see what versions are available:

module spider arrow

Any version should be fine. Let’s load the latest (as of April 2025):

module load arrow/19.0.1

I created a virtual Python environment with all necessary packages under /project. All you have to do today is activate it with:

source /project/def-sponsor00/env/bin/activate

Look for available Python modules:

module spider python

Load the version of your choice:

module load python/3.13.2

The Hugging Face Datasets package uses PyArrow for efficiency. In order to install it, we also need to load an Arrow module.

Let’s see what versions are available:

module spider arrow

Any version should be fine. Let’s load the latest (as of April 2025):

module load arrow/19.0.1

Create a Python virtual environment:

python -m venv ~/env

Activate it:

source ~/env/bin/activate

Update pip from wheel:

python -m pip install --upgrade pip --no-index

Whenever a Python wheel for a package is available on the Alliance clusters, you should use it instead of downloading the package from PyPI. To do this, simply add the --no-index flag to the install command.

You can see whether a wheel is available with avail_wheels <package> or look at the list of available wheels.

Advantages of wheels:

  • compiled for the clusters hardware,
  • ensures no missing or conflicting dependencies,
  • much faster installation.

Install libraries from wheel:

python -m pip install --no-index "jax[cuda]" jax-ai-stack[grain] datasets matplotlib penzai torchvision tqdm transformers

Don’t forget --no-index to install from wheels.

Quick explanation of packages we are installing:

- jax[cuda]             ➔ only if you want to run JAX on GPUs
- jax-ai-stack[grain]   ➔ installs JAX for the CPU (if not already installed for the GPU),
                                   Flax—the main NN library,
                                   Optax—optimizers & loss functions,
                                   Orbax—for checkpointing,
                                   Grain—to build efficient dataloaders,
                                   ml_dtypes—NumPy dtype extensions for deep learning
- datasets              ➔ from Hugging Face—to load data
- matplotlib            ➔ to visualise samples
- penzai                ➔ to have interactive model display
- torchvision           ➔ to augment the data to prevent over-fitting
- tqdm                  ➔ progress bar
- transformers          ➔ from Hugging Face—to load pretrained weights

Moving from your computer to the cluster

If you prototyped your project locally and want to move it to an Alliance cluster to run it at scale, here are some tips.

Environment

Install the packages in a Python virtual environment with venv and pip as shown above.

Code

Clone the project from a Git remote (or copy it with scp -r or rsync):

[local]$ scp -r </path/to/local/project> <user>@<cluster>.alliancecan.ca:~/<path/to/remote/project>

Data

Deep learning datasets often contain many small files (e.g. images), but clusters are slowed to a crawl by swarms of small files.

Pack the data directory into an archive on your machine and copy the archive to the cluster:

[local]$ tar cf data.tar -C </path/to/local/project/data>
[local]$ scp data.tar <user>@<cluster>.alliancecan.ca:~/projects/def-<user>/

If the archive is very large, you can use Globus for the transfer.

Then keep the archive as is on the cluster and only extract it inside jobs.