PyTorch: Difference between revisions

From Alliance Doc
Jump to navigation Jump to search
(Marked this version for translation)
(note for target device)
Line 23: Line 23:
:1. Load a python module, either <tt>python/2.7</tt>, <tt>python/3.5</tt>, <tt>python/3.6</tt> or <tt>python/3.7</tt>
:1. Load a python module, either <tt>python/2.7</tt>, <tt>python/3.5</tt>, <tt>python/3.6</tt> or <tt>python/3.7</tt>
:2. Create and start a [[Python#Creating_and_using_a_virtual_environment|virtual environment]].
:2. Create and start a [[Python#Creating_and_using_a_virtual_environment|virtual environment]].
:3. Install PyTorch in the virtual environment with <code>pip install</code>. For both GPU and CPU support:
:3. Install PyTorch in the virtual environment with <code>pip install</code>.  
==== GPU ====
:{{Command|prompt=(venv) [name@server ~]|pip install numpy torch_gpu --no-index}}
:{{Command|prompt=(venv) [name@server ~]|pip install numpy torch_gpu --no-index}}
:If you only need CPU support:
==== CPU only ====
:{{Command|prompt=(venv) [name@server ~]|pip install numpy torch_cpu --no-index}}
:{{Command|prompt=(venv) [name@server ~]|pip install numpy torch_cpu --no-index}}
'''Note''': Do not install both <tt>torch_cpu</tt> and <tt>torch_gpu</tt>.


====Extra==== <!--T:21-->
====Extra==== <!--T:21-->

Revision as of 14:53, 18 January 2019

Other languages:

PyTorch is a Python package that provides two high-level features:

  • Tensor computation (like NumPy) with strong GPU acceleration
  • Deep neural networks built on a tape-based autograd system

PyTorch has a distant connection with Torch, but for all practical purposes you can treat them as separate packages.

Installation

Latest available wheels

To see the latest version of PyTorch that we have built:

Question.png
[name@server ~]$ avail_wheels "torch*"

For more information on listing wheels, see listing available wheels.

Pre-build

The preferred option is to install it using the python wheel that we compile, as follows:

1. Load a python module, either python/2.7, python/3.5, python/3.6 or python/3.7
2. Create and start a virtual environment.
3. Install PyTorch in the virtual environment with pip install.

GPU

Question.png
(venv) [name@server ~] pip install numpy torch_gpu --no-index

CPU only

Question.png
(venv) [name@server ~] pip install numpy torch_cpu --no-index

Note: Do not install both torch_cpu and torch_gpu.

Extra

In addition to torch_cpu or torch_gpu, you can install torchvision, torchtext and torchaudio:

Question.png
(venv) [name@server ~] pip install numpy six torch_cpu torchvision torchtext torchaudio --no-index

Note: For torchaudio, torch_cpu==0.4.0 or torch_gpu==0.4.0 is required.

libtorch

libtorch.so is included in the wheel. Once Pytorch is installed in a virtual environment, you can find it at: venv/lib/python3.6/site-packages/torch/lib/libtorch.so where venv is the virtual environment.

Job submission

Once the setup is completed, you can submit a PyTorch job with

Question.png
[name@server ~]$ sbatch pytorch-test.sh

Here is an example of a job submission script using the python wheel, with a virtual environment in $HOME/pytorch:

File : pytorch-test.sh

#!/bin/bash
#SBATCH --gres=gpu:1       # Request GPU "generic resources"
#SBATCH --cpus-per-task=6  # Cores proportional to GPUs: 6 on Cedar, 16 on Graham.
#SBATCH --mem=32000M       # Memory proportional to GPUs: 32000 Cedar, 64000 Graham.
#SBATCH --time=0-03:00
#SBATCH --output=%N-%j.out
module load python/3.6
source $HOME/pytorch/bin/activate
python ./pytorch-test.py


The Python script pytorch-test.py has the form

File : pytorch-test.py

import torch
x = torch.Tensor(5, 3)
print(x)
y = torch.rand(5, 3)
print(y)
# let us run the following only if CUDA is available
if torch.cuda.is_available():
    x = x.cuda()
    y = y.cuda()
    print(x + y)