How to install nvidia apex on Google Colab

Question:

what I did is follow the instruction on the official github site

!git clone https://github.com/NVIDIA/apex
!cd apex
!pip install -v --no-cache-dir ./

it gives me the error:

ERROR: Directory './' is not installable. Neither 'setup.py' nor 'pyproject.toml' found.
Exception information:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/dist-packages/pip/_internal/cli/base_command.py", line 178, in main
    status = self.run(options, args)
  File "/usr/local/lib/python3.6/dist-packages/pip/_internal/commands/install.py", line 326, in run
    self.name, wheel_cache
  File "/usr/local/lib/python3.6/dist-packages/pip/_internal/cli/base_command.py", line 268, in populate_requirement_set
    wheel_cache=wheel_cache
  File "/usr/local/lib/python3.6/dist-packages/pip/_internal/req/constructors.py", line 248, in install_req_from_line
    "nor 'pyproject.toml' found." % name
pip._internal.exceptions.InstallationError: Directory './' is not installable. Neither 'setup.py' nor 'pyproject.toml' found.
Asked By: Tommy Yu

||

Answers:

Updated

First, create a file e.g. setup.sh as follows:

For apex with CUDA and C++ extensions:

%%writefile setup.sh

git clone https://github.com/NVIDIA/apex
cd apex
pip install -v --disable-pip-version-check --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./

Then, install it

!sh setup.sh

For Python-only build

%%writefile setup.sh

git clone https://github.com/NVIDIA/apex
cd apex
pip install -v --disable-pip-version-check --no-cache-dir ./

A Python-only build omits certain Fused kernels required to use apex.optimizers.FusedAdam, apex.normalization.FusedLayerNorm, etc.

Check apex quickstart.

Answered By: kHarshit

(wanted to just add a comment but I don’t have enough reputation…)

it works for me but the cd is actually not required. Also, I needed the two global options as suggested here: https://github.com/NVIDIA/apex/issues/86

%%writefile setup.sh

git clone https://github.com/NVIDIA/apex
pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./apex

then

!sh setup.sh

Worked for me after adding CUDA_HOME enviroment variable:

%%writefile setup.sh

export CUDA_HOME=/usr/local/cuda-10.1
git clone https://github.com/NVIDIA/apex
pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./apex
!sh setup.sh
Answered By: IvanS

In colab instead of using "!" use "%’ before cd command

!git clone https://github.com/NVIDIA/apex
%cd apex
!pip install -v --no-cache-dir ./

The above code will work just fine.

Answered By: ashish_roopan

The problem is with !cd apex. Use %cd apex instead.

Read this: https://stackoverflow.com/a/57212513/8690463

Answered By: Y Y

I tried a few options, but I liked the one in this website, which worked very well with fast_bert and torch:

try:
  import apex
except Exception:
  ! git clone https://github.com/NVIDIA/apex.git
  % cd apex
  !pip install --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" .
  %cd ..
Answered By: Sam S.

I use paperspace, and this worked for me:

!pip install git+https://github.com/NVIDIA/apex
Answered By: Usama Nadeem

The following worked for me in November, 2022.

apex.optimizers.FusedAdam, apex.normalization.FusedLayerNorm, etc. require CUDA and C++ extensions (see e.g., here). Thus, it’s not sufficient to install the Python-only built. To built apex the cuda version of PyTorch and apex must match, as explained here.

Query the version Ubuntu Colab is running on:

!lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.6 LTS
Release:    18.04
Codename:   bionic

To get the current cuda version run:

!nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2021 NVIDIA Corporation
Built on Sun_Feb_14_21:12:58_PST_2021
Cuda compilation tools, release 11.2, V11.2.152
Build cuda_11.2.r11.2/compiler.29618528_0 

Look-up the latest PyTorch built and compute plattform here.
enter image description here

Next, got to the cuda toolkit archive and configure a version that matches the cuda-version of PyTorch and your OS-Version.
enter image description here

Copy the installation instructions:

wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
sudo mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
wget https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda-repo-ubuntu1804-11-7-local_11.7.0-515.43.04-1_amd64.deb
sudo dpkg -i cuda-repo-ubuntu1804-11-7-local_11.7.0-515.43.04-1_amd64.deb
sudo cp /var/cuda-repo-ubuntu1804-11-7-local/cuda-*-keyring.gpg /usr/share/keyrings/
sudo apt-get update
sudo apt-get -y install cuda

Remove Sudo and change the last line to include your cuda-version e.g., !apt-get -y install cuda-11-7 (without exclamation mark if run in shell directly):

!wget https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64/cuda-ubuntu1804.pin
!mv cuda-ubuntu1804.pin /etc/apt/preferences.d/cuda-repository-pin-600
!wget https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda-repo-ubuntu1804-11-7-local_11.7.0-515.43.04-1_amd64.deb
!dpkg -i cuda-repo-ubuntu1804-11-7-local_11.7.0-515.43.04-1_amd64.deb
!cp /var/cuda-repo-ubuntu1804-11-7-local/cuda-*-keyring.gpg /usr/share/keyrings/
!apt-get update

Your cuda version will now be updated:

!nvcc --version

nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2022 NVIDIA Corporation
Built on Wed_Jun__8_16:49:14_PDT_2022
Cuda compilation tools, release 11.7, V11.7.99
Build cuda_11.7.r11.7/compiler.31442593_0

Next, updated the outdated Pytorch version in Google Colab:

!pip install torch -U

Build apex. Depending on what you might require fewer global options:

!git clone https://github.com/NVIDIA/apex.git && cd apex && pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" --global-option="--fast_multihead_attn" . && cd .. && rm -rf apex

...
Successfully installed apex-0.1

You can now import apex as desired:

from apex import optimizers, normalization
...
Answered By: KarelZe