cc_staff
282
edits
(--cpus-per-task is compulsory for srun since Slurm 22.05) |
No edit summary |
||
Line 2,123: | Line 2,123: | ||
LibTorch allows one to implement both C++ extensions to PyTorch and '''pure C++ machine learning applications'''. It contains "all headers, libraries and CMake configuration files required to depend on PyTorch", as described in the [https://pytorch.org/cppdocs/installing.html documentation]. | LibTorch allows one to implement both C++ extensions to PyTorch and '''pure C++ machine learning applications'''. It contains "all headers, libraries and CMake configuration files required to depend on PyTorch", as described in the [https://pytorch.org/cppdocs/installing.html documentation]. | ||
=== How to use LibTorch === <!--T:39--> | === How to use LibTorch === <!--T:39--> | ||
==== | ==== Setup environment ==== <!--T:40--> | ||
Load the modules required by Libtorch, then install PyTorch in a Python virtual environment: | |||
module load gcc cuda/11.4 cmake protobuf cudnn python/3.10 | |||
virtualenv --no-download --clear ~/ENV && source ~/ENV/bin/activate | |||
pip install --no-index torch numpy | |||
==== Compile a minimal example ==== <!--T:44--> | ==== Compile a minimal example ==== <!--T:44--> | ||
Line 2,149: | Line 2,138: | ||
Create the following two files: | Create the following two files: | ||
{{File | {{File | ||
|name=example-app.cpp | |name=example-app.cpp | ||
Line 2,157: | Line 2,145: | ||
#include <iostream> | #include <iostream> | ||
int main() { | int main() | ||
{ | |||
torch::Device device(torch::kCPU); | torch::Device device(torch::kCPU); | ||
if (torch::cuda::is_available()) { | if (torch::cuda::is_available()) | ||
{ | |||
std::cout << "CUDA is available! Using GPU." << std::endl; | std::cout << "CUDA is available! Using GPU." << std::endl; | ||
device = torch::Device(torch::kCUDA); | device = torch::Device(torch::kCUDA); | ||
Line 2,174: | Line 2,164: | ||
|contents= | |contents= | ||
cmake_minimum_required(VERSION 3.0 FATAL_ERROR) | cmake_minimum_required(VERSION 3.0 FATAL_ERROR) | ||
project(example | project(example) | ||
find_package(Torch REQUIRED) | find_package(Torch REQUIRED) | ||
add_executable(example | add_executable(example example.cpp) | ||
target_link_libraries(example | target_link_libraries(example "${TORCH_LIBRARIES}") | ||
set_property(TARGET example | set_property(TARGET example PROPERTY CXX_STANDARD 14) | ||
}} | }} | ||
<!--T:54--> | <!--T:54--> | ||
With the python virtualenv activated, configure the project and compile the program: | |||
</translate> | |||
cmake -B build -S . -DCMAKE_PREFIX_PATH=$VIRTUAL_ENV/lib/python3.10/site-packages | |||
cmake --build build | |||
<!--T:56--> | <!--T:56--> | ||
Run the program: | Run the program: | ||
build/example | |||
<!--T:58--> | <!--T:58--> | ||
To test an application with CUDA, request an [[Running_jobs#Interactive_jobs|interactive job]] with a [[Using_GPUs_with_Slurm|GPU]]. | To test an application with CUDA, request an [[Running_jobs#Interactive_jobs|interactive job]] with a [[Using_GPUs_with_Slurm|GPU]]. | ||
=== Resources === <!--T:59--> | === Resources === <!--T:59--> | ||
Line 2,206: | Line 2,191: | ||
<!--T:60--> | <!--T:60--> | ||
https://pytorch.org/cppdocs/ | https://pytorch.org/cppdocs/ | ||