Skip to content
This repository was archived by the owner on Sep 26, 2024. It is now read-only.

Commit 24e4468

Browse files
Merge pull request #216 from LambdaLabs/virtual-envs-containers
Add FAQ: "How do I use virtual environments and containers?"
2 parents c15a599 + 97507db commit 24e4468

File tree

3 files changed

+288
-0
lines changed

3 files changed

+288
-0
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
title: "How do I create and run a Docker container?"
3+
type: docs
4+
tags:
5+
- Docker
6+
- NVIDIA
7+
- TensorFlow
8+
---
9+
10+
To create and run a Docker container:
11+
12+
1. Install Docker and
13+
[NVIDIA Container Toolkit](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/overview.html)
14+
by running:
15+
16+
```bash
17+
sudo apt -y update && sudo apt -y install docker.io nvidia-container-toolkit && \
18+
sudo systemctl daemon-reload && \
19+
sudo systemctl restart docker
20+
```
21+
22+
1. Add your user to the `docker` group by running:
23+
24+
```bash
25+
sudo adduser "$(id -un)" docker
26+
```
27+
28+
Then, exit and reopen a shell (terminal) so that your user can create and
29+
run Docker containers.
30+
31+
1. Locate the Docker image for the container you want to create. For example,
32+
the [NVIDIA NGC Catalog](https://catalog.ngc.nvidia.com) has
33+
[images for creating TensorFlow NGC containers](https://catalog.ngc.nvidia.com/orgs/nvidia/containers/tensorflow/tags).
34+
35+
1. Create a container from the Docker image, and run a command in the
36+
container, by running:
37+
38+
```bash
39+
docker run --gpus all -it IMAGE COMMAND
40+
```
41+
42+
Replace **IMAGE** with the URL to the image for the container you want to
43+
create.
44+
45+
Replace **COMMAND** with the command you want to run in the container.
46+
47+
For example, to create a
48+
[TensorFlow NGC container](https://catalog.ngc.nvidia.com/orgs/nvidia/containers/tensorflow)
49+
and run a command to get the container's TensorFlow build information, run:
50+
51+
```bash
52+
docker run --gpus all -it nvcr.io/nvidia/tensorflow:23.05-tf2-py3 python -c "import tensorflow as tf ; sys_details = tf.sysconfig.get_build_info() ; print(sys_details)"
53+
```
54+
55+
You should see output similar to the following:
56+
57+
```
58+
================
59+
== TensorFlow ==
60+
================
61+
62+
NVIDIA Release 23.05-tf2 (build 59341886)
63+
TensorFlow Version 2.12.0
64+
65+
Container image Copyright (c) 2023, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
66+
Copyright 2017-2023 The TensorFlow Authors. All rights reserved.
67+
68+
Various files include modifications (c) NVIDIA CORPORATION & AFFILIATES. All rights reserved.
69+
70+
This container image and its contents are governed by the NVIDIA Deep Learning Container License.
71+
By pulling and using the container, you accept the terms and conditions of this license:
72+
https://developer.nvidia.com/ngc/nvidia-deep-learning-container-license
73+
74+
NOTE: CUDA Forward Compatibility mode ENABLED.
75+
Using CUDA 12.1 driver version 530.30.02 with kernel driver version 525.85.12.
76+
See https://docs.nvidia.com/deploy/cuda-compatibility/ for details.
77+
78+
NOTE: The SHMEM allocation limit is set to the default of 64MB. This may be
79+
insufficient for TensorFlow. NVIDIA recommends the use of the following flags:
80+
docker run --gpus all --ipc=host --ulimit memlock=-1 --ulimit stack=67108864 ...
81+
82+
2023-06-08 17:09:50.643793: I tensorflow/core/util/port.cc:110] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable `TF_ENABLE_ONEDNN_OPTS=0`.
83+
2023-06-08 17:09:50.680974: I tensorflow/core/platform/cpu_feature_guard.cc:183] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
84+
To enable the following instructions: SSE3 SSE4.1 SSE4.2 AVX, in other operations, rebuild TensorFlow with the appropriate compiler flags.
85+
OrderedDict([('cpu_compiler', '/opt/rh/devtoolset-9/root/usr/bin/gcc'), ('cuda_compute_capabilities', ['sm_52', 'sm_60', 'sm_61', 'sm_70', 'sm_75', 'sm_80', 'sm_86', 'compute_90']), ('cuda_version', '12.1'), ('cudnn_version', '8'), ('is_cuda_build', True), ('is_rocm_build', False), ('is_tensorrt_build', True)])
86+
```
87+
88+
{{% alert title="Tip" color="success" %}}
89+
See the [Docker documentation](https://docs.docker.com/) to learn more about
90+
using Docker.
91+
92+
You can also check out the Lambda blog post:
93+
[NVIDIA NGC Tutorial: Run A PyTorch Docker Container Using Nvidia-Container-Toolkit On Ubuntu](https://lambdalabs.com/blog/nvidia-ngc-tutorial-run-pytorch-docker-container-using-nvidia-container-toolkit-on-ubuntu).
94+
{{% /alert %}}
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
title: "How do I create a conda virtual environment?"
3+
type: docs
4+
tags:
5+
- CUDA
6+
- PyTorch
7+
---
8+
9+
To create a conda virtual environment:
10+
11+
1. Download the latest version of
12+
[Miniconda3](https://docs.conda.io/en/latest/miniconda.html) by running:
13+
14+
```bash
15+
curl -O https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
16+
```
17+
18+
Then, install Miniconda3 by running the command:
19+
20+
```bash
21+
sh Miniconda3-latest-Linux-x86_64.sh
22+
```
23+
24+
Follow the installer prompts. Install Miniconda3 in the default location.
25+
Allow the installer to initialize Miniconda3.
26+
27+
1. If you want to create a conda virtual environment immediately after
28+
installing Miniconda3, you need to load the changes made to your `.bashrc`.
29+
30+
You can either:
31+
32+
- Exit and reopen your shell (terminal).
33+
- Run `source ~/.bashrc`.
34+
35+
{{% alert title="Tip" color="success" %}}
36+
For compatibility with the
37+
[Python venv module]({{< relref "create-python-virtual-environment" >}}),
38+
it's recommended that you disable automatic activation of the conda base
39+
environment by running:
40+
41+
```bash
42+
conda config --set auto_activate_base false
43+
```
44+
{{% /alert %}}
45+
46+
1. Create a conda virtual environment using Miniconda3 by running:
47+
48+
```bash
49+
conda create OPTIONS -n NAME PACKAGES
50+
```
51+
52+
Replace **NAME** with the name you want to give your virtual environment.
53+
54+
Replace **PACKAGES** with the list of packages you want to install in your
55+
virtual environment.
56+
57+
(Optional) Replace **OPTIONS** with options for the `conda create` command.
58+
[See the `conda create` documentation](https://docs.conda.io/projects/conda/en/latest/commands/create.html)
59+
to learn more about available options.
60+
61+
For example, to create a conda virtual environment for PyTorch® with CUDA
62+
11.8, run the below command and follow the prompts:
63+
64+
```bash
65+
conda create -c pytorch -c nvidia -n pytorch+cuda_11-8 pytorch torchvision torchaudio pytorch-cuda=11.8
66+
```
67+
1. Activate the conda virtual environment by running:
68+
69+
```bash
70+
conda activate NAME
71+
```
72+
73+
Replace **NAME** with the name of the virtual environment created in the
74+
previous step.
75+
76+
For instance, to activate the example PyTorch with CUDA 11.8 virtual
77+
environment mentioned in the previous step, run:
78+
79+
```bash
80+
conda activate pytorch+cuda_11-8
81+
```
82+
83+
Once activated, you can test the example virtual environment is working by
84+
running:
85+
86+
```bash
87+
python -c 'import torch ; print("\nIs available: ", torch.cuda.is_available()) ; print("Pytorch CUDA Compiled version: ", torch._C._cuda_getCompiledVersion()) ; print("Pytorch version: ", torch.__version__) ; print("pytorch file: ", torch.__file__) ; num_of_gpus = torch.cuda.device_count(); print("Number of GPUs: ",num_of_gpus)'
88+
```
89+
90+
You should see output similar to:
91+
92+
```
93+
Is available: True
94+
Pytorch CUDA Compiled version: 11080
95+
Pytorch version: 2.0.1
96+
pytorch file: /home/ubuntu/miniconda3/envs/pytorch+cuda_11-8/lib/python3.11/site-packages/torch/__init__.py
97+
Number of GPUs: 1
98+
```
99+
100+
{{% alert title="Note" color="info" %}}
101+
Locally installed packages can conflict with packages installed in virtual
102+
environments. For this reason, it's recommended to uninstall locally installed
103+
packages by running:
104+
105+
To uninstall packages installed locally for your user only, run:
106+
107+
```bash
108+
pip uninstall -y $(pip -v list | grep ${HOME}/.local | awk '{printf "%s ", $1}')
109+
```
110+
111+
To uninstall packages installed locally, system-wide (for all users), run:
112+
113+
```bash
114+
sudo pip uninstall -y $(pip -v list | grep /usr/local | awk '{printf "%s ", $1}')
115+
```
116+
{{% /alert %}}
117+
118+
{{% alert title="Warning" color="warning" %}}
119+
**Don't run the above uninstall commands on Lambda GPU Cloud on-demand
120+
instances!**
121+
122+
The above uninstall commands remove all locally installed packages and, on
123+
on-demand instances, break programs including pip and JupyterLab.
124+
{{% /alert %}}
125+
126+
{{% alert title="Tip" color="success" %}}
127+
[See the Conda documentation](https://docs.conda.io/projects/conda/en/stable/commands.html)
128+
to learn more about how to manage conda virtual environments.
129+
{{% /alert %}}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
---
2+
title: "How do I create a Python virtual environment?"
3+
type: docs
4+
---
5+
6+
1. Create a Python virtual environment using the `venv` module by running:
7+
8+
```bash
9+
python -m venv --system-site-packages NAME
10+
```
11+
12+
Replace **NAME** with the name you want to give to your virtual
13+
environment.
14+
15+
{{% alert title="Note" color="info" %}}
16+
The command, above, creates a virtual environment that has access to Lambda
17+
Stack packages and packages installed from Ubuntu repositories.
18+
19+
To create a virtual environment that _doesn't_ have access to Lambda Stack
20+
and Ubuntu packages, omit the `--system-site-packages` option.
21+
{{% /alert %}}
22+
23+
1. Activate the virtual environment by running:
24+
25+
```bash
26+
./NAME/bin/activate
27+
```
28+
29+
Replace **NAME** with the name you gave your virtual environment in the
30+
previous step.
31+
32+
Python packages you install in your virtual environment are isolated from
33+
the base environment and other virtual environments.
34+
35+
{{% alert title="Note" color="info" %}}
36+
Locally installed packages can conflict with packages installed in virtual
37+
environments. For this reason, it's recommended to uninstall locally installed
38+
packages by running:
39+
40+
To uninstall packages installed locally for your user only, run:
41+
42+
```bash
43+
pip uninstall -y $(pip -v list | grep ${HOME}/.local | awk '{printf "%s ", $1}')
44+
```
45+
46+
To uninstall packages installed locally, system-wide (for all users), run:
47+
48+
```bash
49+
sudo pip uninstall -y $(pip -v list | grep /usr/local | awk '{printf "%s ", $1}')
50+
```
51+
{{% /alert %}}
52+
53+
{{% alert title="Warning" color="warning" %}}
54+
**Don't run the above uninstall commands on Lambda GPU Cloud on-demand
55+
instances!**
56+
57+
The above uninstall commands remove all locally installed packages and, on
58+
on-demand instances, break programs including pip and JupyterLab.
59+
{{% /alert %}}
60+
61+
{{% alert title="Tip" color="success" %}}
62+
See the
63+
[Python venv module documentation](https://docs.python.org/3/library/venv.html)
64+
to learn more about Python virtual environments.
65+
{{% /alert %}}

0 commit comments

Comments
 (0)