ユーザ用ツール

サイト用ツール


ai:pytorch

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン前のリビジョン
次のリビジョン
前のリビジョン
ai:pytorch [2019/12/29 16:50] – [PyTorch] ogaai:pytorch [2021/11/27 20:45] (現在) – [ROCm 4.3 + PyTorch 1.11] oga
行 14: 行 14:
 ===== Install 手順 ===== ===== Install 手順 =====
  
-  * Python, <nowiki>C++</nowki> ともに公式サイトよりビルド済みバイナリをダウンロード可能。+  * Python, <nowiki>C++</nowiki> ともに公式サイトよりビルド済みバイナリをダウンロード可能。
   * Linux, Windows, macOS   * Linux, Windows, macOS
 +    * GPU は CUDA のみ。ROCm 版もあるらしい。
  
  
 +===== メモ =====
  
 +CPU と GPU 間のメモリ転送は明示的に記述する必要あり。ただし非常に簡単。
 +
 +<code cpp>
 +float  batch_size[ 3*32*32 ];
 +torch::Tensor  inputs_cpu= torch::from_blob( float_array, { batch_size, 3, 32, 32 }, torch::ScalarType::Float );
 +auto  inputs_gpu= inputs_cpu.to( torch::kCUDA );
 +
 +auto  outputs_gpu= model->forward( inputs_gpu );
 +auto  outputs_cpu= outputs_gpu.to( torch::kCPU );
 +</code>
 +
 +
 +Model (Module) の定義は class ~Impl を使い、TORCH_MODULE() マクロで定義する。
 +
 +<code cpp>
 +#include  <torch/torch.h>
 +
 +class ModelFCImpl : public torch::nn::Module {
 +    torch::nn::Linear  fc1= nullptr;
 +    torch::nn::Linear  fc2= nullptr;
 +    ~
 +public:
 +    ModelFC()
 +    {
 +        fc1= register_module( "fc1", torch::nn::Linear( 512, 256 ) );
 +        fc2= register_module( "fc2", torch::nn::Linear( 256, 10 ) );
 +        ~
 +    }
 +    torch::Tensor  forward( const torch::Tensor& x )
 +    {
 +        ~
 +        x= torch::relu( fc1( x ) );
 +        x= fc2( x );
 +        return  x;
 +    }
 +};
 +
 +TORCH_MODULE( ModelFC );
 +</code>
 +
 +Shape 定義は NCHW 形式で IntArrayRef を使う。<nowiki>std::vector<int64_t></nowiki> が利用可能。
 +
 +TORCH_MODULE() で定義しておけば直接 torch::save() や torch::load() が使える。
 +
 +
 +===== C++ API sample =====
 +
 +https://github.com/hiroog/cppapimnist/tree/master/pytorch_cpp
 +
 +
 +
 +
 +====== RADEON (ROCm) で PyTorch を使う方法 ======
 +
 +RADEON で PyTorch の <nowiki>C++ API</nowiki> を使う。
 +
 +
 +  * [[https://wlog.flatlib.jp/archive/1/2020-1-4]]
 +
 +
 +==== ROCm 4.3 + PyTorch 1.11 (2021/11/27) ====
 +
 +  * Ubuntu 20.04LTS RADEON Vega 56
 +
 +<code>
 +$ sudo apt update
 +$ sudo apt dist-upgrade
 +$ sudo apt install libnuma-dev
 +$ sudo reboot
 +$ sudo apt install wget gnupg2
 +$ wget -q -O - https://repo.radeon.com/rocm/rocm.gpg.key | sudo apt-key add -
 +$ echo 'deb [arch=amd64] https://repo.radeon.com/rocm/apt/4.3/ ubuntu main' | sudo tee /etc/apt/sources.list.d/rocm.list
 +$ sudo apt update
 +$ sudo rocminfo
 +$ sudo apt install rocm-dkms
 +$ sudo reboot
 +$ sudo apt install git cmake
 +$ sudo apt install python3-pip
 +$ pip3 install setuptools numpy wheel pyyaml
 +$ git clone --recursive https://github.com/pytorch/pytorch
 +$ cd pytorch
 +$ sudo apt install rccl rocm-libs
 +$ pip3 install typing_extensions
 +$ python3 tools/amd_build/build_amd.py
 +$ ROCclr_DIR=/opt/rocm/rocclr RCCL_DIR=/opt/rocm/rccl/lib/cmake/rccl/ PYTORCH_ROCM_ARCH=gfx900 hip_DIR=/opt/rocm/hip/cmake/ USE_NVCC=OFF BUILD_CAFFE2_OPS=0 PATH=/usr/lib/ccache/:$PATH USE_CUDA=OFF python3 setup.py bdist_wheel
 +</code>
  
ai/pytorch.1577605801.txt.gz · 最終更新: 2019/12/29 16:50 by oga

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki