0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

VisualStudioCodeでPytorchのRustバインディングの実行環境を構築する(Ubuntu編)

Posted at

はじめに

本記事はPytorchのRustバインディングをUbuntu版VisualStudioCode上で試してみました。
真っ更な状態のUbuntuからRustバインディングを実行する方法までを初学者向けに極力丁寧に書いています。

対象者

PythonからRustに乗り換えたいなーと考えているRust初学者

環境情報

バージョン 備考
Ubuntu 20.04.3 LTS
Rustc 1.56.1 Rustコンパイラ
※下記Rustインストール手順にて自動的にインストールされる
Rustup 1.24.3 Rustインストーラおよびバージョン管理ツール
※下記Rustインストール手順にて自動的にインストールされる
Cargo 1.56.0 Rustビルドツール 
※下記Rustインストール手順にて自動的にインストールされる
VisualStudioCode 1.62.3
Rust support for VisualStudioCode 0.7.8 VSCode拡張機能Rustツール
CodeLLDB 1.6.10 VSCode拡張機能Rustデバッガー

Pytorch/Rustバインディング

今回実行するPytorch/Rustバインディングはこちら
tch-rs

Rust実行環境を構築する

Rustをインストールする

下記URLに表示されているコマンドをターミナル上から実行してRustをインストールする
https://www.rust-lang.org/ja/tools/install
Rustダウンロード.png
下記画面が表示されるので1を入力する(未入力のままでも可)
Rustインストール.png
下記画面が表示されたら完了
Rustインストール2.png

Rustがインストールされたか確認するために下記コマンドをする

rustc -V
rustup -V
cargo --version

VisualStudioCodeをインストールする

下記URLの「.deb」よりdebファイルをダウンロードしVisualStudioCodeをインストールする
https://code.visualstudio.com/Download
VSCodeダウンロード.png
VSCodeインストール.png

VisualStudioCodeの拡張機能をインストールする

アプリケーションからVisualStudioCodeを実行する
VisualStudioCode.png
左のメニューより拡張機能を選択する
VisualStudioCode2.png
Rust support for VisualStudioCode(拡張機能の名称はRustとなっている)をインストールする
RustforVisualStudioCode.png
CodeLLDBをインストールする
CodeLLDB.png
※おまけ
VisualStudioCodeはデフォルトでは英語表記だが
JapaneseLanguagePack for VisualStudioCodeをインストールすることで日本語化できる
Screenshot from 2021-11-20 01-00-28.png

GitリポジトリのクローンおよびMNISTのダウンロードを行う

「エクスプローラー」⇒「Gitリポジトリのクローン」の順に選択し、
検索窓に「https://github.com/LaurentMazare/tch-rs.git 」を入力してEnterを押下する
VSCode_Gitクローン2.png

複製先となる任意のパスを選択し「リポジトリの場所を選択」を選択する
VSCode_Gitクローン3.png

複製したプロジェクトを開く
VSCode_Gitクローン4.png

下記URLよりMNISTのデータセットをダウンロードする
http://yann.lecun.com/exdb/mnist/
MNIST.png

tch-rs直下にdataフォルダを作成し、先程ダウンロードした.gzファイルを解凍して配置する
MNISTダウンロード.png

CodeLLDBの設定を行う

tch-rs/examples/mnist/main.rsを開く
※拡張子が.rsとなっているファイルならば何でも問題ないが、後続の作業のためこのファイルを選択する
VSCode_torch.png

「実行」⇒「デバッグの開始」を選択する
VSCode_デバッグ.png
初回起動時はデバッグ構成ファイルが存在しないため作成ダイアログが表示される
画像のように「OK」⇒「はい(Y)」を選択することで自動的にCodeLLDBが構成ファイルを作成する
※CodeLLDBをインストールする前に同様の操作をした場合は全く別の構成ファイルが生成されてしまうため削除すること
VSCodeデバッグ2.png
VSCodeデバッグ3.png
VSCodeデバッグ4.png

出力されたlaunch.jsonはプロジェクト全体をビルドしてしまう設定となるため、tch-rs/examples/mnist配下のビルド設定のみを残して他の設定を削除する
VSCodeデバッグ5.png
下記は書き換えた例
※CodeLLDBのバージョンによって内容が変わる可能性があるため異なるバージョンを利用する場合は
自動出力されたファイルからtch-rs/examples/mnist配下のビルド設定のみを残して他の設定を削除することを推奨

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug example 'mnist'",
            "cargo": {
                "args": [
                    "build",
                    "--example=mnist",
                    "--package=tch"
                ],
                "filter": {
                    "name": "mnist",
                    "kind": "example"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        },
        {
            "type": "lldb",
            "request": "launch",
            "name": "Debug unit tests in example 'mnist'",
            "cargo": {
                "args": [
                    "test",
                    "--no-run",
                    "--example=mnist",
                    "--package=tch"
                ],
                "filter": {
                    "name": "mnist",
                    "kind": "example"
                }
            },
            "args": [],
            "cwd": "${workspaceFolder}"
        }
    ]
}

赤枠内の任意の行をクリックしブレークポイントを設定する
※MNISTの学習を実行するだけならば不要
VSCodeデバッグ6.png

Pytorch/Rustバインディングを実行する

再度「実行」⇒「デバッグの開始」を選択する
VSCodeデバッグ7.png

VSCodeデバッグ8.png

実行結果

VSCodeデバッグ9.png

以上です

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?