PyTorch: Difference between revisions

Jump to navigation Jump to search
337 bytes removed ,  1 year ago
no edit summary
(--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-->


==== Get the library ==== <!--T:40-->
==== Setup environment ==== <!--T:40-->
 
</translate>
wget https://download.pytorch.org/libtorch/cu100/libtorch-shared-with-deps-latest.zip
unzip libtorch-shared-with-deps-latest.zip
cd libtorch
export LIBTORCH_ROOT=$(pwd)
<translate>
 
<!--T:308-->
Notice that the variable <code>LIBTORCH_ROOT</code> is used in the example below.


<!--T:42-->
Load the modules required by Libtorch, then install PyTorch in a Python virtual environment:
A workaround is needed for compiling on our clusters. Apply this patch:
  module load gcc cuda/11.4 cmake protobuf cudnn python/3.10
 
virtualenv --no-download --clear ~/ENV && source ~/ENV/bin/activate
</translate>
pip install --no-index torch numpy
sed -i -e 's/\/usr\/local\/cuda\/lib64\/libculibos.a;dl;\/usr\/local\/cuda\/lib64\/libculibos.a;//g' share/cmake/Caffe2/Caffe2Targets.cmake
<translate>


==== 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:


</translate>
{{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-app)
project(example)


find_package(Torch REQUIRED)
find_package(Torch REQUIRED)


add_executable(example-app example-app.cpp)
add_executable(example example.cpp)
target_link_libraries(example-app "${TORCH_LIBRARIES}")
target_link_libraries(example "${TORCH_LIBRARIES}")
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
set_property(TARGET example PROPERTY CXX_STANDARD 14)
}}
}}
<translate>
<!--T:52-->
Load the necessary modules:
module load cmake intel/2018.3 cuda/10 cudnn


<!--T:54-->
<!--T:54-->
Compile the program:
With the python virtualenv activated, configure the project and compile the program:
mkdir build
</translate>
  cd build
  cmake -B build -S . -DCMAKE_PREFIX_PATH=$VIRTUAL_ENV/lib/python3.10/site-packages
cmake -DCMAKE_PREFIX_PATH="$LIBTORCH_ROOT;$EBROOTCUDA;$EBROOTCUDNN" ..
  cmake --build build
  make


<!--T:56-->
<!--T:56-->
Run the program:
Run the program:
  ./example-app
  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/
</translate>
cc_staff
282

edits

Navigation menu