LoginSignup
15
4

More than 1 year has passed since last update.

数々の苦難を乗り越えTorch for R with CUDA on Dockerを完成させたというお話

Last updated at Posted at 2020-12-06

この記事はFujitsu Advent Calendar 2020 6日目の記事です。

Deep Learningと言えばPythonの独壇場というイメージがずっと有りましたが,ここに来てR言語でもDeep Learningのモデルを構築,学習,推論が出来る様になったということを知りました。早速RStdio環境をNVIDIA Dockerを用いて構築し,「Torch for R1」を試してみたいと思います。しかし,それは思いの他難儀なことになろうとは当初思っていませんでした。。。

:warning: 本記事で紹介する成果!?は以下から利用出来ます。

:warning: 2021/7現在: CUDA 10及びCUDA 11に対応しました。

tokyor/rstudioイメージファイルをベースにコンテナを構築

当初以下のDockerfile(tokyor/rstudio)とdocker-compose.ymlを用いて試しました。GPUを利用出来るNVIDIA Docker2に対応出来れば完成すると考えたからです。しかし,筆者の考えは甘かったことがすぐに証明されます。

tokyor/rstudioのイメージファイルをベースに作成したDockerfile
FROM tokyor/rstudio

RUN apt update && apt upgrade -y
RUN apt install -y vim wget curl git
docker-compose.yml
version: "2.4"
services:
    rstudio_docker:
        build:
            context: .
            dockerfile: Dockerfile
        runtime: nvidia
        environment:
            - NVIDIA_VISIBLE_DEVICES=all
            - NVIDIA_DRIVER_CAPABILITIES=all
        ports:
            - 8787:8787
        volumes:

構築したコンテナにTorch for Rをインストールしてみます。

install.packages("torch")

実行結果
> install.packages("torch")
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/src/contrib/torch_0.1.1.tar.gz'
Content type 'application/x-gzip' length 5014778 bytes (4.8 MB)
==================================================
downloaded 4.8 MB

* installing *source* package ‘torch’ ...
** package ‘torch’ successfully unpacked and MD5 sums checked
** libs
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c RcppExports.cpp -o RcppExports.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c autograd.cpp -o autograd.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c contrib.cpp -o contrib.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c cuda.cpp -o cuda.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c device.cpp -o device.o
In file included from device.cpp:2:0:
torch_types.h:7:0: warning: "LANTERN_HOST_HANDLER" redefined
 #define LANTERN_HOST_HANDLER lantern_host_handler();

In file included from device.cpp:1:0:
lantern/lantern.h:35:0: note: this is the location of the previous definition
 #define LANTERN_HOST_HANDLER

In file included from device.cpp:1:0:
lantern/lantern.h: In function ‘bool lanternInit(const string&, std::__cxx11::string*)’:
lantern/lantern.h:4097:6: note: variable tracking size limit exceeded with -fvar-tracking-assignments, retrying without
 bool lanternInit(const std::string &libPath, std::string *pError)
      ^~~~~~~~~~~
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c dimname_list.cpp -o dimname_list.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c dtype.cpp -o dtype.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c gen-namespace.cpp -o gen-namespace.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c generator.cpp -o generator.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c indexing.cpp -o indexing.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c lantern.cpp -o lantern.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c layout.cpp -o layout.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c memory_format.cpp -o memory_format.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c nn_utils_rnn.cpp -o nn_utils_rnn.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c qscheme.cpp -o qscheme.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c quantization.cpp -o quantization.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c reduction.cpp -o reduction.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c save.cpp -o save.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c scalar.cpp -o scalar.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c storage.cpp -o storage.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c tensor.cpp -o tensor.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c tensor_list.cpp -o tensor_list.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c tensor_options.cpp -o tensor_options.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c utils.cpp -o utils.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c variable_list.cpp -o variable_list.o
g++ -std=gnu++11 -shared -L/usr/local/lib/R/lib -L/usr/local/lib -o torch.so RcppExports.o autograd.o contrib.o cuda.o device.o dimname_list.o dtype.o gen-namespace.o generator.o indexing.o lantern.o layout.o memory_format.o nn_utils_rnn.o qscheme.o quantization.o reduction.o save.o scalar.o storage.o tensor.o tensor_list.o tensor_options.o utils.o variable_list.o -L/usr/local/lib/R/lib -lR
Renaming torch lib to torchpkg
"/usr/local/lib/R/bin/Rscript" "../tools/renamelib.R"
installing to /usr/local/lib/R/site-library/torch/libs
** R
** inst
** preparing package for lazy loading
Error : 'pairlist2' is not an exported object from 'namespace:rlang'
Error : unable to load R code in package ‘torch’
ERROR: lazy loading failed for package ‘torch’
* removing ‘/usr/local/lib/R/site-library/torch’
Warning in install.packages :
  installation of package ‘torch’ had non-zero exit status

The downloaded source packages are in
    ‘/tmp/Rtmpvhb8H5/downloaded_packages’
>

上記のコマンドではエラーに阻まれてインストール出来ませんでしたので,ドキュメントでダメだった場合はこれと書いてあったコマンドでインストールを試みました。

remotes::install_github("mlverse/torch")

実行結果

> remotes::install_github("mlverse/torch")
Downloading GitHub repo mlverse/torch@master
Installing 6 packages: bit, bit64, R6, Rcpp, rlang, withr
Installing packages into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
trying URL 'https://cran.rstudio.com/src/contrib/bit_4.0.4.tar.gz'
Content type 'application/x-gzip' length 279723 bytes (273 KB)
==================================================
downloaded 273 KB

trying URL 'https://cran.rstudio.com/src/contrib/bit64_4.0.5.tar.gz'
Content type 'application/x-gzip' length 135091 bytes (131 KB)
==================================================
downloaded 131 KB

trying URL 'https://cran.rstudio.com/src/contrib/R6_2.5.0.tar.gz'
Content type 'application/x-gzip' length 63361 bytes (61 KB)
==================================================
downloaded 61 KB

trying URL 'https://cran.rstudio.com/src/contrib/Rcpp_1.0.5.tar.gz'
Content type 'application/x-gzip' length 2950521 bytes (2.8 MB)
==================================================
downloaded 2.8 MB

trying URL 'https://cran.rstudio.com/src/contrib/rlang_0.4.8.tar.gz'
Content type 'application/x-gzip' length 847517 bytes (827 KB)
==================================================
downloaded 827 KB

trying URL 'https://cran.rstudio.com/src/contrib/withr_2.3.0.tar.gz'
Content type 'application/x-gzip' length 91443 bytes (89 KB)
==================================================
downloaded 89 KB

* installing *source* package ‘bit’ ...
** package ‘bit’ successfully unpacked and MD5 sums checked
** libs
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c attrutil.c -o attrutil.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c bit.c -o bit.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c chunkutil.c -o chunkutil.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c clone.c -o clone.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c init.c -o init.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c integerutil.c -o integerutil.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c merge.c -o merge.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c rle.c -o rle.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c sort.c -o sort.o
gcc -shared -L/usr/local/lib/R/lib -L/usr/local/lib -o bit.so attrutil.o bit.o chunkutil.o clone.o init.o integerutil.o merge.o rle.o sort.o -L/usr/local/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/bit/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (bit)
* installing *source* package ‘R6’ ...
** package ‘R6’ successfully unpacked and MD5 sums checked
** R
** preparing package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** testing if installed package can be loaded
* DONE (R6)
* installing *source* package ‘Rcpp’ ...
** package ‘Rcpp’ successfully unpacked and MD5 sums checked
** libs
g++  -I/usr/local/lib/R/include -DNDEBUG -I../inst/include/  -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c api.cpp -o api.o
g++  -I/usr/local/lib/R/include -DNDEBUG -I../inst/include/  -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c attributes.cpp -o attributes.o
g++  -I/usr/local/lib/R/include -DNDEBUG -I../inst/include/  -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c barrier.cpp -o barrier.o
g++  -I/usr/local/lib/R/include -DNDEBUG -I../inst/include/  -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c date.cpp -o date.o
g++  -I/usr/local/lib/R/include -DNDEBUG -I../inst/include/  -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c module.cpp -o module.o
g++  -I/usr/local/lib/R/include -DNDEBUG -I../inst/include/  -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c rcpp_init.cpp -o rcpp_init.o
g++ -shared -L/usr/local/lib/R/lib -L/usr/local/lib -o Rcpp.so api.o attributes.o barrier.o date.o module.o rcpp_init.o -L/usr/local/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/Rcpp/libs
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (Rcpp)
* installing *source* package ‘rlang’ ...
** package ‘rlang’ successfully unpacked and MD5 sums checked
** libs
gcc -I/usr/local/lib/R/include -DNDEBUG -I./lib/  -I/usr/local/include  -fvisibility=hidden -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c capture.c -o capture.o
gcc -I/usr/local/lib/R/include -DNDEBUG -I./lib/  -I/usr/local/include  -fvisibility=hidden -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c export.c -o export.o
gcc -I/usr/local/lib/R/include -DNDEBUG -I./lib/  -I/usr/local/include  -fvisibility=hidden -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c internal.c -o internal.o
gcc -I/usr/local/lib/R/include -DNDEBUG -I./lib/  -I/usr/local/include  -fvisibility=hidden -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c lib.c -o lib.o
gcc -I/usr/local/lib/R/include -DNDEBUG -I./lib/  -I/usr/local/include  -fvisibility=hidden -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c version.c -o version.o
gcc -shared -L/usr/local/lib/R/lib -L/usr/local/lib -o rlang.so capture.o export.o internal.o lib.o version.o -L/usr/local/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/rlang/libs
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** testing if installed package can be loaded
* DONE (rlang)
* installing *source* package ‘withr’ ...
** package ‘withr’ successfully unpacked and MD5 sums checked
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (withr)
* installing *source* package ‘bit64’ ...
** package ‘bit64’ successfully unpacked and MD5 sums checked
** libs
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c bsearch.c -o bsearch.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c cache.c -o cache.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c hash64.c -o hash64.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c init.c -o init.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c integer64.c -o integer64.o
integer64.c:270:1: warning: ‘no_sanitize’ attribute directive ignored [-Wattributes]
 __attribute__((no_sanitize("signed-integer-overflow"))) SEXP plus_integer64(SEXP e1_, SEXP e2_, SEXP ret_){
 ^~~~~~~~~~~~~
integer64.c:285:1: warning: ‘no_sanitize’ attribute directive ignored [-Wattributes]
 __attribute__((no_sanitize("signed-integer-overflow"))) SEXP minus_integer64(SEXP e1_, SEXP e2_, SEXP ret_){
 ^~~~~~~~~~~~~
integer64.c:300:1: warning: ‘no_sanitize’ attribute directive ignored [-Wattributes]
 __attribute__((no_sanitize("signed-integer-overflow"))) SEXP diff_integer64(SEXP x_, SEXP lag_, SEXP n_, SEXP ret_){
 ^~~~~~~~~~~~~
integer64.c:347:1: warning: ‘no_sanitize’ attribute directive ignored [-Wattributes]
 __attribute__((no_sanitize("signed-integer-overflow"))) SEXP times_integer64_integer64(SEXP e1_, SEXP e2_, SEXP ret_){
 ^~~~~~~~~~~~~
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c sort64.c -o sort64.o
gcc -I/usr/local/lib/R/include -DNDEBUG   -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g  -c sortuse64.c -o sortuse64.o
gcc -shared -L/usr/local/lib/R/lib -L/usr/local/lib -o bit64.so bsearch.o cache.o hash64.o init.o integer64.o sort64.o sortuse64.o -lm -L/usr/local/lib/R/lib -lR
installing to /usr/local/lib/R/site-library/bit64/libs
** R
** data
** exec
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** testing if installed package can be loaded
* DONE (bit64)

The downloaded source packages are in
    ‘/tmp/Rtmpvhb8H5/downloaded_packages’
Installing package into ‘/usr/local/lib/R/site-library’
(as ‘lib’ is unspecified)
* installing *source* package ‘torch’ ...
** libs
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c RcppExports.cpp -o RcppExports.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c autograd.cpp -o autograd.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c contrib.cpp -o contrib.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c cuda.cpp -o cuda.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c device.cpp -o device.o
In file included from device.cpp:2:0:
torch_types.h:7:0: warning: "LANTERN_HOST_HANDLER" redefined
 #define LANTERN_HOST_HANDLER lantern_host_handler();

In file included from device.cpp:1:0:
lantern/lantern.h:35:0: note: this is the location of the previous definition
 #define LANTERN_HOST_HANDLER

In file included from device.cpp:1:0:
lantern/lantern.h: In function ‘bool lanternInit(const string&, std::__cxx11::string*)’:
lantern/lantern.h:4949:6: note: variable tracking size limit exceeded with -fvar-tracking-assignments, retrying without
 bool lanternInit(const std::string &libPath, std::string *pError)
      ^~~~~~~~~~~
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c dimname_list.cpp -o dimname_list.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c dtype.cpp -o dtype.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c gen-namespace.cpp -o gen-namespace.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c generator.cpp -o generator.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c indexing.cpp -o indexing.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c lantern.cpp -o lantern.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c layout.cpp -o layout.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c memory_format.cpp -o memory_format.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c nn_utils_rnn.cpp -o nn_utils_rnn.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c qscheme.cpp -o qscheme.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c quantization.cpp -o quantization.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c reduction.cpp -o reduction.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c save.cpp -o save.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c scalar.cpp -o scalar.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c storage.cpp -o storage.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c tensor.cpp -o tensor.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c tensor_list.cpp -o tensor_list.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c tensor_options.cpp -o tensor_options.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c utils.cpp -o utils.o
g++ -std=gnu++11 -I/usr/local/lib/R/include -DNDEBUG  -I"/usr/local/lib/R/site-library/Rcpp/include" -I/usr/local/include   -fpic  -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g -c variable_list.cpp -o variable_list.o
g++ -std=gnu++11 -shared -L/usr/local/lib/R/lib -L/usr/local/lib -o torch.so RcppExports.o autograd.o contrib.o cuda.o device.o dimname_list.o dtype.o gen-namespace.o generator.o indexing.o lantern.o layout.o memory_format.o nn_utils_rnn.o qscheme.o quantization.o reduction.o save.o scalar.o storage.o tensor.o tensor_list.o tensor_options.o utils.o variable_list.o -L/usr/local/lib/R/lib -lR
Renaming torch lib to torchpkg
"/usr/local/lib/R/bin/Rscript" "../tools/renamelib.R"
installing to /usr/local/lib/R/site-library/torch/libs
** R
** preparing package for lazy loading
** help
*** installing help indices
*** copying figures
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (torch)
>

インストールが無事完了しました。さて気を取り直してTorch for RでDeep Learningだと意気込んだ矢先に以下の警告が。。。最新のCUDA11.0には対応していないので,CUDA10.x,9.2のいずれかの環境で構築しないとGPUは動かないですよという内容。

> library("torch")
Cuda 11.0 detected but torch only supports: cpu, 10.1, 10.2, 9.2
trying URL 'https://download.pytorch.org/libtorch/cpu/libtorch-cxx11-abi-shared-with-deps-1.7.0%2Bcpu.zip'
Content type 'application/zip' length 165327275 bytes (157.7 MB)
==================================================
downloaded 157.7 MB

trying URL 'https://storage.googleapis.com/torch-lantern-builds/refs/heads/master/latest/Linux-cpu.zip'
Content type 'application/zip' length 1531419 bytes (1.5 MB)
==================================================
downloaded 1.5 MB

CUDA10.xをインストールして再度チャレンジしようと思った矢先に以下の問題に突き当たります。。。

CUDA10.xはDebianでは動かない

CUDA10.xをインストールしようと思い,Ubuntu18.04におけるインストール方法を公式サイトから調べてDockerfileに反映しましたが,エラーで通りません。。。実はtokyor/rstudioのご先祖様のイメージはDebian2ベースとなっています。DebianにCUDA10.xをインストールしようとした所,対応していないことが分かりました。。。(詳細は下図)

CUDA10.1ではそもそもDebianが選べない。。。
CUDA10.1

CUDA11はDebianに対応しているが,Torch for Rが対応していない。。。
CUDA11

Ubuntu18ベースに新たなイメージをビルド

Ubuntu18.04で尚且つCUDA10.2がインストールされているイメージをベースにRStudioのコンテナイメージをビルドして,Torch for Rが利用出来るコンテナ環境を構築することにしました。実際に作成したDockerfileとdocker-compose.ymlを以下に示します。今回作成したDockerfileは以下のコンテナイメージファイルをベースに筆者が独自に改良して作成しました。イメージ4つ分の内容を1つのDockerfileに詰め込んだので長い長いソースとなってしまいました。。。:scream: :scream_cat: :scream:

  • ベースとしたイメージ: nvidia/cuda:10.1-cudnn7-devel

  • 参考としたイメージ

  • 参考イメージを元に以下の改良を追加

    • Ubuntuには非対応なので削除したライブラリ
      • libicu63
      • libjpeg62-turbo
    • Ubuntu向けに追加したライブラリ
      • libreadline-dev
      • xorg-dev
      • libbz2-dev
      • liblzma-dev
      • libpcre2-dev
      • libcurl4-openssl-dev
    • 記載を削除した処理
      • autoremove/autoclean処理(∵ 何故かCUDA関連の実行ファイルまで消えてしまうため)
    • torch/torch vision及びその他必用なライブラリのインストール
FROM nvidia/cuda:10.1-cudnn7-devel

ARG ROOT_PASSWD
ARG PASSWD

# Set up R(Reference rocker/r-ver)
ARG R_VERSION_FULL=4.0.2
ARG R_VERSION=4
ENV CRAN=${CRAN:-https://cran.rstudio.com}
ENV LC_ALL=en_US.UTF-8
ENV LANG=en_US.UTF-8 
ENV TERM=xterm
ENV DEBIAN_FRONTEND=noninteractive   

RUN apt-get update && apt-get install -y --no-install-recommends bash-completion libbz2-dev \
    ca-certificates file fonts-texgyre g++ gfortran gsfonts libblas-dev libbz2-1.0 libcurl4 \
    libcurl4-openssl-dev libjpeg-dev liblzma-dev libopenblas-dev libpangocairo-1.0-0 libpcre2-dev \
    libpcre3 libpng16-16 libreadline-dev libtiff5 liblzma5 locales make unzip wget xorg-dev zip zlib1g \
  && BUILDDEPS="curl default-jdk libbz2-dev libcairo2-dev libcurl4-openssl-dev libpango1.0-dev libjpeg-dev \
    libicu-dev libpcre3-dev libpng-dev libreadline-dev libtiff5-dev liblzma-dev libx11-dev libxt-dev perl \
    tcl8.6-dev tk8.6-dev texinfo texlive-extra-utils texlive-fonts-recommended texlive-fonts-extra \
    texlive-latex-recommended x11proto-core-dev xauth xfonts-base xvfb zlib1g-dev" \
  && echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen && locale-gen en_US.utf8 && /usr/sbin/update-locale LANG=en_US.UTF-8 \
  && apt-get install -y --no-install-recommends $BUILDDEPS \
  && cd tmp/ \
## Download source code
  &&  wget https://cran.r-project.org/src/base/R-${R_VERSION}/R-${R_VERSION_FULL}.tar.gz \
## Extract source code
  && tar -xf R-${R_VERSION_FULL}.tar.gz \
  && cd R-${R_VERSION_FULL} \
## Set compiler flags
  && R_PAPERSIZE=letter \
    R_BATCHSAVE="--no-save --no-restore" R_BROWSER=xdg-open PAGER=/usr/bin/pager PERL=/usr/bin/perl R_UNZIPCMD=/usr/bin/unzip \
    R_ZIPCMD=/usr/bin/zip R_PRINTCMD=/usr/bin/lpr LIBnn=lib AWK=/usr/bin/awk \
    CFLAGS="-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g" \
    CXXFLAGS="-g -O2 -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2 -g" \
## Configure options
    ./configure --enable-R-shlib --enable-memory-profiling --with-readline --with-blas --with-tcltk --disable-nls --with-recommended-packages \
## Build and install
  && make \
  && make install \
## Add a library directory (for user-installed packages)
  && mkdir -p /usr/local/lib/R/site-library \
  && chown root:staff /usr/local/lib/R/site-library \
  && chmod g+ws /usr/local/lib/R/site-library \
## Fix library path
  && sed -i '/^R_LIBS_USER=.*$/d' /usr/local/lib/R/etc/Renviron \
  && echo "R_LIBS_USER=\${R_LIBS_USER-'/usr/local/lib/R/site-library'}" >> /usr/local/lib/R/etc/Renviron \
  && echo "R_LIBS=\${R_LIBS-'/usr/local/lib/R/site-library:/usr/local/lib/R/library:/usr/lib/R/library'}" >> /usr/local/lib/R/etc/Renviron \
## Set configured CRAN mirror
  && if [ -z "$BUILD_DATE" ]; then MRAN=$CRAN; \
    else MRAN=https://mran.microsoft.com/snapshot/${BUILD_DATE}; fi \
  && echo MRAN=$MRAN >> /etc/environment \
  && echo "options(repos = c(CRAN='$MRAN'), download.file.method = 'libcurl')" >> /usr/local/lib/R/etc/Rprofile.site \
## Use littler installation scripts
  && Rscript -e "install.packages(c('littler', 'docopt'), repo = '$CRAN')" \
  && ln -s /usr/local/lib/R/site-library/littler/examples/install2.r /usr/local/bin/install2.r \
  && ln -s /usr/local/lib/R/site-library/littler/examples/installGithub.r /usr/local/bin/installGithub.r \
  && ln -s /usr/local/lib/R/site-library/littler/bin/r /usr/local/bin/r \
## Clean up from R source install
  && cd / \
  && rm -rf /tmp/* \
  && rm -rf /var/lib/apt/lists/*

# Set up RStudio(Reference rocker/rstudio)
ARG RSTUDIO_VERSION
ENV RSTUDIO_VERSION=${RSTUDIO_VERSION:-1.2.5042}
ARG S6_VERSION
ARG PANDOC_TEMPLATES_VERSION
ENV S6_VERSION=${S6_VERSION:-v1.21.7.0}
ENV S6_BEHAVIOUR_IF_STAGE2_FAILS=2
ENV PATH=/usr/lib/rstudio-server/bin:$PATH
ENV PANDOC_TEMPLATES_VERSION=${PANDOC_TEMPLATES_VERSION:-2.9}

## Download and install RStudio server & dependencies
## Attempts to get detect latest version, otherwise falls back to version given in $VER
## Symlink pandoc, pandoc-citeproc so they are available system-wide
RUN apt-get update && apt-get install -y --no-install-recommends git libapparmor1 libclang-dev libedit2 libssl-dev \
  lsb-release multiarch-support psmisc procps python-setuptools sudo \
  && if [ -z "$RSTUDIO_VERSION" ]; \
    then RSTUDIO_URL="https://www.rstudio.org/download/latest/stable/server/bionic/rstudio-server-latest-amd64.deb"; \
    else RSTUDIO_URL="http://download2.rstudio.org/server/bionic/amd64/rstudio-server-${RSTUDIO_VERSION}-amd64.deb"; fi \
  && wget -q $RSTUDIO_URL \
  && dpkg -i rstudio-server-*-amd64.deb \
  && rm rstudio-server-*-amd64.deb \
## Symlink pandoc & standard pandoc templates for use system-wide
  && ln -s /usr/lib/rstudio-server/bin/pandoc/pandoc /usr/local/bin \
  && ln -s /usr/lib/rstudio-server/bin/pandoc/pandoc-citeproc /usr/local/bin \
  && git clone --recursive --branch ${PANDOC_TEMPLATES_VERSION} https://github.com/jgm/pandoc-templates \
  && mkdir -p /opt/pandoc/templates \
  && cp -r pandoc-templates*/* /opt/pandoc/templates && rm -rf pandoc-templates* \
  && mkdir /root/.pandoc && ln -s /opt/pandoc/templates /root/.pandoc/templates \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/ \
## RStudio wants an /etc/R, will populate from $R_HOME/etc
  && mkdir -p /etc/R \
## Write config files in $R_HOME/etc
  && echo '\n\
    \n# Configure httr to perform out-of-band authentication if HTTR_LOCALHOST \
    \n# is not set since a redirect to localhost may not work depending upon \
    \n# where this Docker container is running. \
    \nif(is.na(Sys.getenv("HTTR_LOCALHOST", unset=NA))) { \
    \n  options(httr_oob_default = TRUE) \
    \n}' >> /usr/local/lib/R/etc/Rprofile.site \
  && echo "PATH=${PATH}" >> /usr/local/lib/R/etc/Renviron \
## Need to configure non-root user for RStudio
  && useradd rstudio \
  && echo "rstudio:rstudio" | chpasswd \
  && mkdir /home/rstudio \
  && chown rstudio:rstudio /home/rstudio \
  && addgroup rstudio staff \
## Prevent rstudio from deciding to use /usr/bin/R if a user apt-get installs a package
  &&  echo 'rsession-which-r=/usr/local/bin/R' >> /etc/rstudio/rserver.conf \
## use more robust file locking to avoid errors when using shared volumes:
  && echo 'lock-type=advisory' >> /etc/rstudio/file-locks \
## configure git not to request password each time
  && git config --system credential.helper 'cache --timeout=3600' \
  && git config --system push.default simple \
## Set up S6 init system
  && wget -P /tmp/ https://github.com/just-containers/s6-overlay/releases/download/${S6_VERSION}/s6-overlay-amd64.tar.gz \
  && tar xzf /tmp/s6-overlay-amd64.tar.gz -C / \
  && mkdir -p /etc/services.d/rstudio \
  && echo '#!/usr/bin/with-contenv bash \
          \n## load /etc/environment vars first: \
          \n for line in $( cat /etc/environment ) ; do export $line ; done \
          \n exec /usr/lib/rstudio-server/bin/rserver --server-daemonize 0' \
          > /etc/services.d/rstudio/run \
  && echo '#!/bin/bash \
          \n rstudio-server stop' \
          > /etc/services.d/rstudio/finish \
  && mkdir -p /home/rstudio/.rstudio/monitored/user-settings \
  && echo 'alwaysSaveHistory="0" \
          \nloadRData="0" \
          \nsaveAction="0"' \
          > /home/rstudio/.rstudio/monitored/user-settings/user-settings \
  && chown -R rstudio:rstudio /home/rstudio/.rstudio

RUN wget -P /etc/cont-init.d/ -O userconf https://github.com/rocker-org/rocker-versioned/blob/master/rstudio/userconf.sh

## running with "-e ADD=shiny" adds shiny server
RUN wget -P /etc/cont-init.d/ -O add https://github.com/rocker-org/rocker-versioned/blob/master/rstudio/add_shiny.sh
RUN wget -P /etc/rstudio/ https://github.com/rocker-org/rocker-versioned/blob/master/rstudio/disable_auth_rserver.conf
RUN wget -P /usr/lib/rstudio-server/bin/ -O pam-helper https://github.com/rocker-org/rocker-versioned/blob/master/rstudio/pam-helper.sh

EXPOSE 8787

## automatically link a shared volume for kitematic users
VOLUME /home/rstudio/kitematic

# Install some requires(Reference rocker/tidyverse)
RUN apt-get update -qq && apt-get -y --no-install-recommends install \
  libxml2-dev libcairo2-dev libsqlite-dev libmariadbd-dev libmariadbclient-dev \
  libpq-dev libssh2-1-dev unixodbc-dev libsasl2-dev \
  && Rscript -e "install.packages(c('tidyverse', 'dplyr', 'devtools', 'formatR', \
        'remotes', 'selectr', 'caTools', 'BiocManager'))"

# Set up localize(Reference tokyor/rstudio)
# Change environment to Japanese(Character and DateTime)
ENV LANG ja_JP.UTF-8
ENV LC_ALL ja_JP.UTF-8
RUN sed -i '$d' /etc/locale.gen && echo "ja_JP.UTF-8 UTF-8" >> /etc/locale.gen \
  && locale-gen ja_JP.UTF-8 && /usr/sbin/update-locale LANG=ja_JP.UTF-8 LANGUAGE="ja_JP:ja"
RUN /bin/bash -c "source /etc/default/locale"
RUN ln -sf  /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

# Install ipaexfont and some requires
RUN apt-get update && apt-get install -y fonts-ipaexfont vim curl

# Install packages
RUN Rscript -e "install.packages(c('githubinstall', 'ranger'))"

# Install torch for R
RUN Rscript -e "install.packages('torch')"
RUN Rscript -e "remotes::install_github('mlverse/torchvision')"
RUN echo 'root:'${ROOT_PASSWD} | chpasswd
RUN echo 'rstudio:'${PASSWD} | chpasswd
RUN echo "rstudio ALL=(ALL:ALL) ALL" >> /etc/sudoers
RUN gpasswd -a rstudio sudo

CMD ["/init"]

version: "2.4"
services:
    rstudio_docker:
        build:
            context: .
            dockerfile: Dockerfile
        runtime: nvidia
        environment:
            - NVIDIA_VISIBLE_DEVICES=all
            - NVIDIA_DRIVER_CAPABILITIES=all
        ports:
            - 8787:8787
        volumes:
            - ../rstudio_home:/home/rstudio
        restart: always

また,コマンド入力が面倒くさいのでイメージビルド用に以下のシェルファイルも作成しました。rstudio/rstudioが認証情報だとGoogle Chromeの警告(パスワードの弱さに関する)が出るので変更することにします。auth.txtというファイルを作成し,そこにパスワードを記載することで,コマンドの履歴にパスワードの内容が残らない様にしています。

#!/bin/bash

if [ "${1}" = "-nc" ]; then
    cat auth.txt | xargs -n 2 sh -c 'docker-compose build --no-cache --build-arg ROOT_PASSWD=$0 --build-arg PASSWD=$1'
else
    cat auth.txt | xargs -n 2 sh -c 'docker-compose build --build-arg ROOT_PASSWD=$0 --build-arg PASSWD=$1'
fi

CUDAが有効にならない苦難の歴史

上記のDockerfileをビルドしてコンテナを起動すればいよいよGPU版のTorch for RをDocker上で利用出来る様になる筈でした。しかし,実際に待っていたのは,以下の図の様な状況です。。。

CUDA False

万事休す。。。一縷の望みを託すべくGitHubにIssueを上げてみました。詳細は以下を参照して下さい。(本家RStudio社のエンジニアとの議論になるとは夢にも思っていませんでした。RStudio社がブラジルの会社だったという事実を今回初めて知りました。)

結局解決。。。しかし,その方法が。。。

Issueを見て頂くとお分かり頂けますが,結局cuda_is_available() == FalseとなるトラブルはDockerfileをリビルド(厳密にはキャッシュされているコンテナイメージを使用しない)を実施すると解決しました。但し,キャッシュを用いずにリビルドする作業は何度も実施していましたが,解決に至っていなかったのが,ある日もう一度やってみようと思ってやってみたら解決したので,Docker Hubに上がっていたイメージファイルにバグが有ったか,キャッシュを使わないビルドをやっているつもりでキャッシュを使ってしまっていたのかもしれません。どっちにしてもキャッシュされていたコンテナイメージは壊れていたということはこのことから分かりました。

Torch for R CUDA

以下の図の様にGPUのデバイス番号が得られている。Rはリストのインデックスは1から開始する仕様であることが知られていますが,CUDA DeviceのIDは0から開始するという奇妙な仕様です。
GPU Env

2020/12/7追記: 苦難の歴史が認められ本家のリポジトリに。。。

ダメ元でコミュニティーのリポジトリに成果物を追加出来ないか聞いてみた所,何と!!あっさり許可が下りてしまいました。地球の裏側のエンジニア3に筆者の苦闘の成果が認められた瞬間です。(参照)

image.png

Pull Requestを出してみた!!

御言葉に甘えてPull Requestを出してみました。マージされれば本家のリポジトリから環境構築ツールが利用出来ます。請うご期待!!

Pull Request

Mergeされました!!(2020/12/7)

無事Mergeされました。めでたしめでたし。但し環境構築ガイドを追加するという追加タスクが。。。(Pull Requestに書いてしまったので。。。しかも,in Englishで。。。)

image.png

環境構築ガイドもMergeされました(2020/12/8)

環境構築ガイドを作成(in Englishで)し,Pull Requestを出した所,無事Mergeされました。

image.png

まとめ

Torch for RをGPUモードで実行出来る環境をDockerでお手軽に構築出来る様にDockerfile, docker-compose.yml等の必用なファイルの作成を今回実施しました。これもTorch for RがCUDA11に対応すれば,お役御免になるかもしれませんが,それまでは今回作成した環境でTorch for Rを用いてDNNで遊んでみようと思います。

今後の展望

  • MLP(Multi Layer Perceptron)で手書き文字認識を試してみる
  • DNNで手書き文字認識を試してみる
  • もう少し複雑なデータセットでDNNを試す
  • Kaggleでも活用してみる
  • 友人に利用を紹介する etc...

Reference


  1. Tensor Flowよりも先にTorchがR言語に対応したのは意外な感じがしました。ただ,Torchは元々Luaベースのフレームワークであったため,多言語化は自然なことなのかもしれません。 

  2. Debianもパッケージマネージャーがapt-get,aptを用いているのでてっきりUbuntuをベースにしているものと思っていたら,よくよく調べてみるとDebianベースでした。。。 

  3. 図中のDaniel Falbel(dfalbel)氏はRStudio社のエンジニアでブラジルのエンジニアとのこと。 

15
4
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
15
4