PyTorch is a open-source software library based on the Torch library for machine learning. Developed by Facebook's AI Research Lab (FAIR), the library is popular for computer vision and natural language processing. PyTorch's website where documentation and more examples can be found. Documentation for Python can be found on its official website. Currently, version 7 of Torch is available on the clusters, however see the SLURM script below to see how to build your own enviroment.
python
.
Python files end in *.py, and can be run from the command line using
python filename.py
where filename is the name of the Python file.
module load python3/anaconda/2020.02
conda create --name torch-env pytorch torchvision cudatoolkit=10.2 --channel pytorch
source activate torch-env
Once the virtual environment is created, it can be launched at any time by ensuring that the python3 module is
loaded, using the command
module load python3/anaconda/2020.02
conda create --name torch-env pytorch torchvision cudatoolkit=10.2 --channel pytorch
source activate torch-env
pip install package-name
pip install SQLAlchemy
conda install package-name
conda install NumPy
source deactivate
import torch
print("Welcome to PyTorch!")
#Define tensor 1 5x5
t1 = torch.rand(5)
#Print tensor size
print(t1.size())
print(f"\nAddition Example")
#Print tensor 1
print(t1)
print("+")
#Define tensor 2 5x5
t2 = torch.rand(5)
#Print tensor 2
print(t2)
print("=")
#Add tensors
print(torch.add(t1, t2))
print(f"\nMultiplication Example")
#Begin multiplication
print(t1)
print("*")
print(t2)
print("=")
#Multiply tensors
print(torch.multiply(t1, t2))
3. Prepare the submission script, which is the script that is submitted to the Slurm scheduler as a job in order
to run the Python script. The linked repository provides the script job.sh as an example.
#!/bin/bash
#SBATCH --job-name=torch_test
#SBATCH -o r_out%j.out
#SBATCH -e r_err%j.err
#SBATCH -N 1
#SBATCH --ntasks-per-node=4
#SBATCH -p defq,defq-48core
module purge
module load python3/anaconda/2020.02
#Create PyTorch Enviroment
conda create --name torch-env pytorch torchvision cudatoolkit=10.2 --channel pytorch
#Activate that environment
source activate torch-env
#Run script
python example.py
#Exit the conda system
conda deactivate
4. Submit the job using: sbatch job.sh
5. Examine the results.