2018년 10월 5일 금요일

AI를 해주는 AI, H2O Driverless AI의 설치와 사용법

H2O Driverless AI의 설치 및 구동, 그리고 모든 사무실에 수북히 쌓여있는 spreadsheet data를 이용한 training은 매우 쉽습니다.

먼저, 다음과 같이 H2O DriverlessAI 설치 파일을 download 받습니다.  저는 여기서 IBM POWER8 CPU와 Pascal P100 GPU를 장착한 Minsky 서버를 사용했습니다.

[root@p57a22 data]# wget https://s3.amazonaws.com/artifacts.h2o.ai/releases/ai/h2o/dai/rel-1.3.1-12/ppc64le-centos7/dai-1.3.1-linux-ppc64le.sh


(rpm을 download 받아서 해도 됩니다만, 이게 TAR SH를 download 받는 것이 여러모로 가장 편리합니다.)


이것을 수행하면 그 directory를 DRIVERLESS_AI_HOME으로 하여 필요한 binary engine이 설치됩니다.  다른 directory에 설치하고 싶으시면 ./dai-1.3.1-linux-ppc64le.sh "설치_디렉토리_이름"과 같이 directory 이름을 직접 적어도 됩니다.

[root@p57a22 data]# chmod a+x dai-1.3.1-linux-ppc64le.sh

[root@p57a22 data]# ./dai-1.3.1-linux-ppc64le.sh
Extracting to dai-1.3.1-linux-ppc64le ...
...
Starting Driverless AI

    run-dai.sh

Stopping Driverless AI

    kill-dai.sh

Debugging Driverless AI

    cat log/dai.out
    cat log/h2o.out
    cat log/procsy.out


설치된 directory로 들어가보면 start와 stop에 필요한 shell script와 함께 각종 jar 및 python 파일들이 들어있습니다.  기본적으로 H2O DriverlessAI는 python과 java로 되어 있으며, 독자적인 python engine (v3.6.1)과 jre (v1.8)을 가지고 있습니다.  물론 이것들은 산업 표준 그대로의 것들입니다.

[root@p57a22 data]# cd dai-1.3.1-linux-ppc64le

[root@p57a22 dai-1.3.1-linux-ppc64le]# ls
bin             cuda-9.2                  h2o.jar      log                 README_TAR_SH.txt  src
BUILD_INFO.txt  dai-env.sh                include      mojo2-runtime.jar   README.txt         start-dai.sh
config.toml     docs                      jre          procsy              README_WSL.txt     start-h2o.sh
cpu-only        h2oai_autoreport          kill-dai.sh  python              run-dai.sh         start-procsy.sh
cuda-8.0        h2oai-dai-connectors.jar  lib          README_DOCKER.txt   sample_data        VERSION.txt
cuda-9.0        h2oai_scorer              LICENSE      README_RPM_DEB.txt  share              vis-data-server.jar


dai-env.sh를 수행하면 필요한 환경 변수들이 자동 설정됩니다.

[root@p57a22 dai-1.3.1-linux-ppc64le]# ./dai-env.sh
======================================================================
DRIVERLESS_AI_HOME is /home/data/dai-1.3.1-linux-ppc64le
DRIVERLESS_AI_CONFIG_FILE is /home/data/dai-1.3.1-linux-ppc64le/config.toml
DRIVERLESS_AI_JAVA_HOME is /home/data/dai-1.3.1-linux-ppc64le/jre
JAVA_HOME is /home/data/dai-1.3.1-linux-ppc64le/jre
CUDA Version is cuda-9.2
DRIVERLESS_AI_H2O_XMX is 233580m
DRIVERLESS_AI_H2O_PORT is 54321
DRIVERLESS_AI_PROCSY_PORT is 8080
OMP_NUM_THREADS is 16
OPENBLAS_MAIN_FREE is 1
LANG is en_US.UTF-8
MAGIC is /home/data/dai-1.3.1-linux-ppc64le/share/misc/magic
HOME is /root
uid=0(root) gid=0(root) groups=0(root),2001(powerai)
======================================================================

이제 run-dai.sh를 수행하면 H2O DAI가 시작됩니다.

[root@p57a22 dai-1.3.1-linux-ppc64le]# ./run-dai.sh
======================================================================
DRIVERLESS_AI_HOME is /home/data/dai-1.3.1-linux-ppc64le
DRIVERLESS_AI_CONFIG_FILE is /home/data/dai-1.3.1-linux-ppc64le/config.toml
DRIVERLESS_AI_JAVA_HOME is /home/data/dai-1.3.1-linux-ppc64le/jre
JAVA_HOME is /home/data/dai-1.3.1-linux-ppc64le/jre
CUDA Version is cuda-9.2
DRIVERLESS_AI_H2O_XMX is 233580m
DRIVERLESS_AI_H2O_PORT is 54321
DRIVERLESS_AI_PROCSY_PORT is 8080
OMP_NUM_THREADS is 16
OPENBLAS_MAIN_FREE is 1
LANG is en_US.UTF-8
MAGIC is /home/data/dai-1.3.1-linux-ppc64le/share/misc/magic
HOME is /root
uid=0(root) gid=0(root) groups=0(root),2001(powerai)
======================================================================
Started.


이후에는 http://129.40.XX.XX:12345 와 같이 12345 포트로 web browser를 통해 dataset을 import하고 training (H2O에서는 experiment라고 합니다)을 하면 됩니다.

(아래 그림에 나오는 IP address는 VPN으로만 접근 가능한 주소이니 공연히 두들겨 보지 마십시요... 시간낭비입니다 ㅋ)




맨 처음 나오는 나오는 화면은 login 화면인데, 여기서의 user id와 passwd는 임의로 직접 여기서 정하시면 됩니다.  이 user id는 OS의 user id가 아니라, 그냥 DriverlessAI의 user id입니다.  이 user id는 login 했을 때 내가 볼 수 있는 dataset과 train된 model들 등을 구분하기 위한 것이며, 또한 H2O DAI의 license의 단위가 됩니다.  DAI의 license는 CPU core나 server box 수가 아니라 user 단위로 됩니다.

Login 이후에는 dataset부터 import 하게 되어 있습니다.  PC에서 upload하셔도 되고, 미리 서버에 upload해둔 file을 서버의 filesystem에서 가져와도 됩니다.  모든 CSV 포맷 파일을 그대로 사용하실 수 있습니다.  Excel 파일이 있다면 그건 수작업으로 CSV 포맷으로 저장하셔서 사용하셔야 합니다.  여기서는 https://timeseries.weebly.com/data-sets.html 에서 얻은 각 숙박시설의 점유율 data를 가지고 해보겠습니다.   이는 달별로 호텔, 모텔, 야영장, 배낭족 등의 점유율을 정리한 100여줄의 매우 작은 자료입니다.




Dataset이 import되면 즉각 자동으로 각 column들의 Min/Max/Avg/Dev 등을 계산해주는 것은 물론, 다양한 형태로 visualize까지 할 수 있습니다.   가령 아래 그림은 각 column 간의 상관관계를 보여주는 도표입니다.




이제 predict 메뉴를 눌러 이 dataset으로 model을 training하겠습니다.


여기서 지정해줄 것은 사실상 딱 하나, 어떤 column에 대해서 향후 예측을 하고 싶으냐만 정하시면 됩니다.  여기서는 Holiday Park Occupancy Rate라는 칼럼으로 하겠습니다.




나머지는 옵션들입니다.  가령 이 spreadsheet의 칼럼 중 시간 부분이 있다면 그걸 Time 칼럼으로 지정하시는 것이 좋습니다.  Time 칼럼을 Auto로 놓으면 아예 자동으로 어떤 칼럼이 시간 부분인지 탐지하여 설정합니다. 


그리고 조종할 부분은 3개의 라디오 다이얼 같은 것 뿐입니다.  왼쪽부터 Accuracy (정확도), Time (시간), Interpretability (기계학습 해석) 다이얼인데, 1~10까지의 '강약'을 조절하는 방식으로 되어 있습니다.   가령 Accuracy를 높일 경우 사용되는 알고리즘이 GLM과 XGBoost에서 XGBoost로 바뀐다든지, Time을 늘릴 경우 training 반복 회수가 12번에서 523번으로 바뀐다든지 하는 식입니다.  어려운 세부 튜닝을 자동으로 해주므로 우리는 그냥 보고서 제출 때까지 시간 여유가 얼마나 있는지 등만 결정하면 되는 것입니다.




물론 아래와 같이 expert setting을 할 수도 있습니다.  Tensorflow라든가 RuleFit 같은 것들은 현재 alpha 버전으로 제공됩니다.



Training을 시작하면 왼쪽 아래에는 validation, 즉 기본적으로 RMSE (Root Mean Square Error)가 display되면서 error가 점차 줄어드는 것을 보실 수 있습니다.  오른쪽 아래에는 data 내의 실제 값과, training 회수가 반복되면서 prediction하는 값의 상관 관계가 그래프로 보여집니다.  처음에는 저 그래프는 분산도가 크게 보여지다가, training이 잘 되면 점차 직선으로 수렴되는 것을 보실 수 있습니다.   또한 중앙 상단의 모델 완성%를 보여주는 계기판 위에는 "3912개의 feature에 대해 896개 모델 중 118개에 대해 평가를 완료"라는 메시지가 보이는데, 이 숫자들은 training이 진행되면서 최적화를 거쳐 계속 동적으로 변화됩니다.


H2O DAI에서는 필요에 따라 띄엄띄엄 GPU를 사용합니다.   다음과 같이 nvidia-smi로도 그 사용 모습을 모니터링할 수 있고, 또 오른쪽 하단의 'GPU usage' tab을 클릭하면 GPU 사용률을 GPU별로 볼 수 있습니다.


Fri Oct  5 00:51:19 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 396.26                 Driver Version: 396.26                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla P100-SXM2...  On   | 00000002:01:00.0 Off |                    0 |
| N/A   31C    P0    39W / 300W |    600MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla P100-SXM2...  On   | 00000003:01:00.0 Off |                    0 |
| N/A   33C    P0    42W / 300W |    608MiB / 16280MiB |     35%      Default |
+-------------------------------+----------------------+----------------------+
|   2  Tesla P100-SXM2...  On   | 0000000A:01:00.0 Off |                    0 |
| N/A   30C    P0    38W / 300W |    604MiB / 16280MiB |     20%      Default |
+-------------------------------+----------------------+----------------------+
|   3  Tesla P100-SXM2...  On   | 0000000B:01:00.0 Off |                    0 |
| N/A   33C    P0    29W / 300W |     10MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0     18750      C   ...uild_cv_model_subprocess-end(prot=True)   295MiB |
|    0     18969      C   ...uild_cv_model_subprocess-end(prot=True)   295MiB |
|    1     18749      C   ...uild_cv_model_subprocess-end(prot=True)   295MiB |
|    1     18970      C   ...el-running(prot=False)-XGBoostModel-fit   303MiB |
|    2     18748      C   ...uild_cv_model_subprocess-end(prot=True)   295MiB |
|    2     18971      C   ...ng(prot=False)-RuleFitModel-fit_glm-fit   299MiB |
+-----------------------------------------------------------------------------+



Training(DAI에서는 Experiment라고 함)이 끝나면 다음과 같이 당장 prediction을 할 수 있는 "Score on another dataset"이라는 메뉴가 제공됩니다.  여기에 training시킨 dataset과 같은 명칭과 종류의 column과 data를 가진 test dataset을 넣으면 아까 정한 target column에 대해 prediction을 해줍니다.   저는 여기에 미리 따로 잘라서 준비해둔 occupancy test dataset을 입력해보았습니다.   그 결과로 다음과 같이 해당 column에 대한 예측값을 CSV로 download 받을 수 있습니다.





이 값을 실제 수치와 비교를 해보면 대략 다음과 같이 나옵니다.  100여줄 정도의 data로 10여분 training한 것치고는 괜찮은 예측치입니다.



또한, 이런 예측을 이렇게 DAI 서버의 web interface 말고, 여기서 train된 모델을 다른 서버로 옮겨서 python 혹은 java client program을 거쳐서 수행할 수도 있습니다.  이에 대해서는 다른 posting에서 다루겠습니다.


2018년 9월 10일 월요일

ILSVRC2012 dataset 중 강아지 사진만을 이용한 짧은 TF inception v3 테스트 방법



다음과 같이 benchmark/tensorflow 디렉토리에 들어가서, exec_img.sh를 수행하시면 됩니다.  이때 아래와 같이 nohup으로 수행하시면 도중에 연결 세션이 끊어져도 백그라운드로 job은 계속 수행될 뿐만 아니라, 수행 기록이 nohup.out에도 기록되므로 편리하실 것입니다.

[root@ac922 tensorflow]# pwd
/home/files/ilsvrc12/tensorflow

[root@ac922 tensorflow]# ls
benchmark  exec_img.sh  models  nohup.out.final.tf  output_final

[root@ac922 tensorflow]# nohup ./exec_img.sh &

위와 같이 exec_img.sh를 한번 수행하시면 그 속에서 아래와 같이 ./models/run_model.sh 스크립트가 batch_size=128로 순차적으로 GPU 개수 4, 2, 1에 대해서 각각 1번씩 총 3번을 수행합니다.  각 수행이 끝날 때마다 time 명령에 의해 수행 시간에 걸린 시간이 nohup.out에 기록됩니다.   원래 NVIDIA에서 준 script를 수행해보니, 매번 exec을 수행할 때마다 output directory를 새로 만들어야 제대로 수행되는 것 같아 아래와 같이 exec 수행시마다 ouput directory를 다른 이름으로 옮기고 새로 output을 만드는 문장을 추가했습니다.

mkdir output
time  exec  tf inception3  128  4  0
mv output output4gpu
mkdir output
time  exec  tf inception3  128  2  0
mv output output2gpu
mkdir output
time  exec  tf inception3  128  1  0
mv output output1gpu


결과 확인은 ouput directory에 쌓이는 아래의 log를 보셔도 되고, nohup.out을 보셔도 됩니다.  이 script에서는 total images/sec이 python 자체적으로 합산되어 표시되므로 그것을 기록하시면 됩니다.   단, python에서 계산되는 Elapsed Time은 일부 로직이 잘못되어 분:초 단위만 맞고 시간 단위는 9시간으로 나오니 그건 무시하십시요. 

이 테스트를 위해 필요한 python code 및 model file을 아래 google drive에 올려 놓았습니다.

https://drive.google.com/open?id=1DNn-Nv4rlOiv2NLqk6Y0j2ANlJjw9VP6

그리고 이 테스트를 위해 필요한 종류별로 labeling된 강아지 사진을 tfrecord 포맷으로 만든 dataset을 아래 google drive에 올려 놓았습니다.

https://drive.google.com/open?id=1rQcxAWeNbByy0Yooj6IbROyVRsdQPn5-

위 dataset을 추출하고 tfrecord로 포맷하는 과정은 아래에 정리되어 있습니다.

http://hwengineer.blogspot.com/2018/04/ilsvrc2012imgtraint3tar-training-dataset.html


** 별첨 :  tfrecord file들의 이름과 size

[root@ac922 ilsvrc12]# cd tfrecord/

[root@ac922 tfrecord]# ls -l | head
total 1509860
-rw-rw-r--. 1 1001 1001  6920780 Apr 11 19:20 train-00000-of-00120
-rw-rw-r--. 1 1001 1001  6422535 Apr 11 19:20 train-00001-of-00120
-rw-rw-r--. 1 1001 1001  6959007 Apr 11 19:21 train-00002-of-00120
-rw-rw-r--. 1 1001 1001  6885268 Apr 11 19:21 train-00003-of-00120
-rw-rw-r--. 1 1001 1001  5969364 Apr 11 19:21 train-00004-of-00120
-rw-rw-r--. 1 1001 1001  6143260 Apr 11 19:21 train-00005-of-00120
-rw-rw-r--. 1 1001 1001  6123517 Apr 11 19:21 train-00006-of-00120
-rw-rw-r--. 1 1001 1001  8585788 Apr 11 19:21 train-00007-of-00120
-rw-rw-r--. 1 1001 1001  6149957 Apr 11 19:21 train-00008-of-00120

[root@ac922 tfrecord]# ls -l | tail
-rw-rw-r--. 1 1001 1001 24124729 Apr 11 19:20 validation-00022-of-00032
-rw-rw-r--. 1 1001 1001 23741822 Apr 11 19:20 validation-00023-of-00032
-rw-rw-r--. 1 1001 1001 24759230 Apr 11 19:20 validation-00024-of-00032
-rw-rw-r--. 1 1001 1001 25225023 Apr 11 19:20 validation-00025-of-00032
-rw-rw-r--. 1 1001 1001 25273559 Apr 11 19:20 validation-00026-of-00032
-rw-rw-r--. 1 1001 1001 26820464 Apr 11 19:20 validation-00027-of-00032
-rw-rw-r--. 1 1001 1001 24115323 Apr 11 19:20 validation-00028-of-00032
-rw-rw-r--. 1 1001 1001 24459085 Apr 11 19:20 validation-00029-of-00032
-rw-rw-r--. 1 1001 1001 25246485 Apr 11 19:20 validation-00030-of-00032
-rw-rw-r--. 1 1001 1001 23561132 Apr 11 19:20 validation-00031-of-00032

[root@ac922 tfrecord]# du -sm .
1475    .


2018년 9월 6일 목요일

Poughkeepsie 센터에서 LSF cluster 이용하여 tensorflow 수행하는 방법 안내

** 이는 IBM Poughkeepsie benchmark center의 cluster에서 LSF를 이용해 테스트를 수행하시는 분들을 위한 guide입니다.


먼저 별도로 통보받으신 VPN SW와 VPN id/passwd를 이용해 Poughkeepsie benchmark center cluster에 VPN 연결을 하셔야 합니다.

그 다음에, 이미 통보받으신대로 p10login4.pbm.ihost.com 라는 주소로 putty 등을 이용해 ssh 접속을 하십시요.   (아래 그림 참조)



접속할 때의 userid 및 passwd는 VPN id/passwd와는 별도로 통보받으신 userid/passwd (서버 linux OS용) 입니다.

p10login4에 접속하신 뒤, 먼저 anaconda3를 설치하시기 바랍니다.  설치하실 때 default로 제시되는 directory를 그대로 이용하시는 것이 좋습니다.  이유는 /gpfs/gpfs_gl4_16mb/ 밑의 directory가 GPFS(요즘 이름은 Spectrum Scale)라는 공유파일시스템로서 cluster 내의 모든 work node에 mount 되어 있는 것이기 때문입니다.  거기에 설치해야 나중에 어느 work node에서 job을 수행하더라도 이 anaconda를 사용하실 수 있습니다.

[b8p217za@p10login4 ~]$ wget https://repo.continuum.io/archive/Anaconda3-5.2.0-Linux-ppc64le.sh

[b8p217za@p10login4 ~]$ chmod a+x Anaconda3-5.2.0-Linux-ppc64le.sh

[b8p217za@p10login4 ~]$ ./Anaconda3-5.2.0-Linux-ppc64le.sh
...
[/gpfs/gpfs_gl4_16mb/b8p217/b8p217za/anaconda3] >>>
...
Do you wish the installer to prepend the Anaconda3 install location
to PATH in your /gpfs/gpfs_gl4_16mb/b8p217/b8p217za/.bashrc ? [yes|no]
[no] >>> yes

설치가 끝난 뒤에는 .bashrc을 다시 수행하셔서 기본 python이 anaconda3에서 나오는 것인지 확인하십시요.

[b8p217za@p10login4 ~]$ . /gpfs/gpfs_gl4_16mb/b8p217/b8p217za/.bashrc

[b8p217za@p10login4 ~]$ which python
~/anaconda3/bin/python

그 다음으로는 tensorflow 운용에 필요한 dependent package들을 설치하셔야 합니다.  다음의 명령을 내리시면 됩니다.

[b8p217za@p10login4 ~]$ /opt/DL/tensorflow/bin/install_dependencies
...

The following NEW packages will be INSTALLED:

    absl-py:                    0.1.10-py36_0              file://opt/DL/conda-pkgs
    astor:                      0.6.2-py_0                 file://opt/DL/conda-pkgs
    blas:                       1.0-openblas
    gast:                       0.2.0-py36_0               file://opt/DL/conda-pkgs
    grpcio:                     1.10.0-py36hf484d3e_0      file://opt/DL/conda-pkgs
    libprotobuf:                3.5.0-hf484d3e_0           file://opt/DL/conda-pkgs
    powerai-tensorflow-prereqs: 1.8.0_31721.7987738-py36_0 file:///opt/DL/tensorflow/conda-pkgs
    protobuf:                   3.5.0-py36_0               file://opt/DL/conda-pkgs
    termcolor:                  1.1.0-py36_0               file://opt/DL/conda-pkgs
    toposort:                   1.5-py36_0                 file://opt/DL/conda-pkgs
...
Proceed ([y]/n)? y


이제 tensorflow를 사용하시기 위해 환경변수 설정 script를 다음과 같이 수행하십시요.   이 script를 수행하시면 아래와 같이 PATH, PYTHONPATH들이 설정되면서 PowerAI에 포함된 tensorflow를 사용하실 수 있게 됩니다.

[b8p217za@p10login4 ~]$ source /opt/DL/tensorflow/bin/tensorflow-activate

[b8p217za@p10login4 ~]$ env | grep PATH
MANPATH=/vol/ibmplatform/lsf/10.1/man:/usr/share/lmod/lmod/share/man::
MODULEPATH_ROOT=/gpfs/gpfs_gl4_16mb/lmod/P8
LD_LIBRARY_PATH=/vol/ibmplatform/lsf/10.1/linux3.10-glibc2.17-ppc64le/lib:/usr/local/cuda-9.2/lib64:/usr/local/cuda-9.2/extras/CUPTI/lib64:/opt/DL/tensorflow/lib
PATH=/gpfs/gpfs_gl4_16mb/b8p217/b8p217za/anaconda3/bin:/vol/ibmplatform/lsf/10.1/linux3.10-glibc2.17-ppc64le/etc:/vol/ibmplatform/lsf/10.1/linux3.10-glibc2.17-ppc64le/bin:/usr/lib64/ccache:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/ibutils/bin:/gpfs/gpfs_gl4_16mb/b8p217/b8p217za/.local/bin:/gpfs/gpfs_gl4_16mb/b8p217/b8p217za/bin:/opt/DL/tensorflow/bin
MODULEPATH=/etc/modulefiles:/usr/share/modulefiles:/gpfs/gpfs_gl4_16mb/lmod/P8/Linux:/gpfs/gpfs_gl4_16mb/lmod/P8/Core:/gpfs/gpfs_gl4_16mb/lmod/P8/rhel/7.5/core
PYTHONPATH=/opt/DL/tensorflow/lib/python3.6/site-packages:/opt/DL/tensorflow/lib/python3.6/site-packages/external/protobuf_archive/python:/opt/DL/tensorflow/lib/python3.6/site-packages/external/six_archive

이제 tensorflow를 사용하실 준비가 끝났습니다.   그러나 이 서버에서 직접 tensorflow training을 수행하시면 안 됩니다.  이 서버는 어디까지나 login 서버일 뿐, V100이 장착된 AC922 서버가 아닙니다.  다음과 같이 job scheduler인 LSF에게 job을 submit 하는 방식으로 training을 수행하셔야 합니다.

먼저, 수행하실 tensorflow python code를 확인하십시요.   여기서는 아래의 cnn_mnist_lms.py를 LSF로 수행하는 것을 해보겠습니다.  참고로, 이 cnn_mnist_lms.py는 /opt/DL/tensorflow/lib/python3.6/site-packages/tensorflow/examples/tutorials/layers/cnn_mnist.py를 LMS로 변환한 example code입니다.

[b8p217za@p10login4 ~]$ ls -l /opt/DL/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/lms/examples/cnn_mnist_lms.py
-rw-r--r-- 1 root root 6203 Jun  5 08:31 /opt/DL/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/lms/examples/cnn_mnist_lms.py

위 code를 LSF로 수행하시려면 다음과 같이 하시면 됩니다.   즉 아래의 파란색 부분만 덧붙이시면 됩니다.

[b8p217za@p10login4 ~]$ bsub -R "rusage[ngpus_excl_p=4]" -q ac922_v100 -o ./1.out -e ./1.err python /opt/DL/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/lms/examples/cnn_mnist_lms.py
Job <243511> is submitted to queue <ac922_v100>.

-q는 queue 이름입니다.  안내받으신 대로 ac922_v100을 쓰시면 됩니다.  -o는 output file을, -e는 error message file을 지정합니다.

결과물은 위에서 지정한 대로라면 login 서버의 현재 directory에 1.out이라는 file에 저장되고, 혹시 error가 생겼다면 그 메시지는 1.err라는 file에 저장됩니다.

그리고 "rusage[ngpus_excl_p=4]" 라는 것은 GPU 4개를 exclusive mode로 쓰려고 하니 가용한 GPU 개수가 4개인 서버에서 수행하라는 뜻입니다.  만약 1개만 쓰실 거라면 그 숫자를 1로 지정하시면 됩니다.  만약 MPI로 작성하신 code를 mpirun 명령을 통해 돌리시는 거라면, AC922 1대에 설치된 GPU 개수가 4장이지만 ngpus_excl_p=8 또는 12 등 4보다 더 큰 숫자로 두셔도 됩니다.  물론 보통의 deep learning training에서는 4개 이하를 쓰시겠지요.

위의 job number에 대해서 bhist 명령을 내리면 job이 어느 work node로 allocation되었는지 또 언제 시작되었고 (만약 끝났다면) 언제 끝났는지 알 수 있습니다.  아래의 경우는 p10a02라는 work node에 할당되었으나, 뭔가 문제가 있어서 시작된지 6초만에 fail 났습니다.

[b8p217za@p10login4 ~]$ bhist -l 243511

Job <243511>, User <b8p217za>, Project <default>, Command <python /opt/DL/tenso
                     rflow/lib/python3.6/site-packages/tensorflow/contrib/lms/e
                     xamples/cnn_mnist_lms.py>
Wed Sep  5 22:45:59: Submitted from host <p10login4>, to Queue <ac922_v100>, CW
                     D <$HOME>, Output File <./1.out>, Error File <./1.err>, Re
                     quested Resources <rusage[ngpus_excl_p=4]>;
Wed Sep  5 22:45:59: Dispatched 1 Task(s) on Host(s) <p10a02>, Allocated 1 Slot
                     (s) on Host(s) <p10a02>, Effective RES_REQ <select[type ==
                      local] order[r15s:pg] rusage[ngpus_excl_p=4.00] >;
Wed Sep  5 22:46:00: Starting (Pid 7426);
Wed Sep  5 22:46:06: External Message "p10a02:gpus=0,1,2,3;" was posted from "b
                     8p217za" to message box 0;
Wed Sep  5 22:46:06: Running with execution home </gpfs/gpfs_gl4_16mb/b8p217/b8
                     p217za>, Execution CWD </gpfs/gpfs_gl4_16mb/b8p217/b8p217z
                     a>, Execution Pid <7426>;
Wed Sep  5 22:46:06: Exited with exit code 2. The CPU time used is 0.4 seconds;
Wed Sep  5 22:46:06: Completed <exit>;

MEMORY USAGE:
MAX MEM: 7 Mbytes;  AVG MEM: 7 Mbytes

Summary of time in seconds spent in various states by  Wed Sep  5 22:46:06
  PEND     PSUSP    RUN      USUSP    SSUSP    UNKWN    TOTAL
  0        0        7        0        0        0        7

Error가 난 이유는 아래와 같이 해당 example code가 work node인 p10a02 서버의 local directory에 설치되어 있지 않기 때문입니다.

[b8p217za@p10login4 ~]$ cat 1.err
python: can't open file '/opt/DL/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/lms/examples/cnn_mnist_lms.py': [Errno 2] No such file or directory

위 error를 수정하기 위해 POK center에 연락하여 각 node들에서 # yum install power-mldl-py3로 필요 fileset들의 update를 요청했습니다.   (sudo 권한이 필요한 일입니다.)

---------------

POK center로부터 update 수행했다는 연락을 받았고, 다시 수행해본 수행 결과는 아래와 같습니다.

[b8p217za@p10login4 ~]$ bsub -R "rusage[ngpus_excl_p=4]" -q ac922_v100 -o ./1.out -e ./1.err python /opt/DL/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/lms/examples/cnn_mnist_lms.py
Job <244143> is submitted to queue <ac922_v100>.


[b8p217za@p10login4 ~]$ bhist -l 244143

Job <244143>, User <b8p217za>, Project <default>, Command <python /opt/DL/tenso
                     rflow/lib/python3.6/site-packages/tensorflow/contrib/lms/e
                     xamples/cnn_mnist_lms.py>
Thu Sep  6 21:27:03: Submitted from host <p10login4>, to Queue <ac922_v100>, CW
                     D <$HOME>, Output File <./1.out>, Error File <./1.err>, Re
                     quested Resources <rusage[ngpus_excl_p=4]>;
Thu Sep  6 21:27:04: Dispatched 1 Task(s) on Host(s) <p10a01>, Allocated 1 Slot
                     (s) on Host(s) <p10a01>, Effective RES_REQ <select[type ==
                      local] order[r15s:pg] rusage[ngpus_excl_p=4.00] >;
Thu Sep  6 21:27:05: Starting (Pid 76857);
Thu Sep  6 21:27:12: External Message "p10a01:gpus=0,1,2,3;" was posted from "b
                     8p217za" to message box 0;
Thu Sep  6 21:27:12: Running with execution home </gpfs/gpfs_gl4_16mb/b8p217/b8
                     p217za>, Execution CWD </gpfs/gpfs_gl4_16mb/b8p217/b8p217z
                     a>, Execution Pid <76857>;
Thu Sep  6 21:29:28: Done successfully. The CPU time used is 204.2 seconds;
Thu Sep  6 21:29:35: Post job process done successfully;

MEMORY USAGE:
MAX MEM: 4.2 Gbytes;  AVG MEM: 3 Gbytes

Summary of time in seconds spent in various states by  Thu Sep  6 21:29:35
  PEND     PSUSP    RUN      USUSP    SSUSP    UNKWN    TOTAL
  1        0        144      0        0        0        145


1초 만에 "p10a01" node로 dispatch 되어 2분 16초 만에 성공적으로 training을 완료했습니다.  

결과물이 저장되는 1.out 파일을 열어보면 아래와 같은 내용이 담겨 있습니다.  


[b8p217za@p10login4 ~]$ cat 1.out

Sender: LSF System <p10lsf@p10a01>
Subject: Job 244143: <python /opt/DL/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/lms/examples/cnn_mnist_lms.py> in cluster <pok_tc_cloud> Done

Job <python /opt/DL/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/lms/examples/cnn_mnist_lms.py> was submitted from host <p10login4> by user <b8p217za> in cluster <pok_tc_cloud> at Thu Sep  6 21:27:03 2018.
Job was executed on host(s) <p10a01>, in queue <ac922_v100>, as user <b8p217za> in cluster <pok_tc_cloud> at Thu Sep  6 21:27:04 2018.
</gpfs/gpfs_gl4_16mb/b8p217/b8p217za> was used as the home directory.
</gpfs/gpfs_gl4_16mb/b8p217/b8p217za> was used as the working directory.
Started at Thu Sep  6 21:27:04 2018.
Terminated at Thu Sep  6 21:29:29 2018.
Results reported at Thu Sep  6 21:29:29 2018.

Your job looked like:

------------------------------------------------------------
# LSBATCH: User input
python /opt/DL/tensorflow/lib/python3.6/site-packages/tensorflow/contrib/lms/examples/cnn_mnist_lms.py
------------------------------------------------------------

Successfully completed.

Resource usage summary:

    CPU time :                                   204.17 sec.
    Max Memory :                                 4393 MB
    Average Memory :                             3106.20 MB
    Total Requested Memory :                     -
    Delta Memory :                               -
    Max Swap :                                   -
    Max Processes :                              3
    Max Threads :                                346
    Run time :                                   147 sec.
    Turnaround time :                            146 sec.

The output (if any) follows:

Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting MNIST-data/train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting MNIST-data/train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting MNIST-data/t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting MNIST-data/t10k-labels-idx1-ubyte.gz
Saving graph to: /tmp/tmpeuq76pnh
{'accuracy': 0.96890002, 'loss': 0.10233325, 'global_step': 20000}


PS:

Read file <./1.err> for stderr output of this job.


그리고 training 과정 중의 메시지는 아래와 같이 1.err에 저장됩니다.   여기서는 일부만 copy & paste 했습니다.

[b8p217za@p10login4 ~]$ cat 1.err
...
INFO:tensorflow:Saving checkpoints for 20000 into /tmp/tmpeuq76pnh/model.ckpt.
INFO:tensorflow:Loss for final step: 0.103957.
INFO:tensorflow:Calling model_fn.
INFO:tensorflow:Done calling model_fn.
INFO:tensorflow:Starting evaluation at 2018-09-07-01:29:27
INFO:tensorflow:Graph was finalized.
2018-09-06 21:29:27.207180: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1435] Adding visible gpu devices: 0, 1, 2, 3
2018-09-06 21:29:27.207305: I tensorflow/core/common_runtime/gpu/gpu_device.cc:923] ...
2018-09-06 21:29:27.208973: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1053] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:3 with 14127 MB memory) -> physical GPU (device: 3, name: Tesla V100-SXM2-16GB, pci bus id: 0035:04:00.0, compute capability: 7.0)
INFO:tensorflow:Restoring parameters from /tmp/tmpeuq76pnh/model.ckpt-20000
INFO:tensorflow:Running local_init_op.
INFO:tensorflow:Done running local_init_op.
INFO:tensorflow:Finished evaluation at 2018-09-07-01:29:27
INFO:tensorflow:Saving dict for global step 20000: accuracy = 0.9689, global_step = 20000, loss = 0.102333


2018년 8월 30일 목요일

무료 ppc64le Cloud - Power Development Cloud (PDC)


x86 기반으로 개발한 code를 ppc64le 기반의 Redhat/Ubuntu/SuSE 등으로 포팅 및 컴파일하려면 일단 ppc64le 기반의 개발 장비가 필요합니다.

이런 용도로 사용하실 수 있는 무료 ppc64le linux Cloud가 이미 있습니다.   IBM에서 제공하는 Power Development Cloud (PDC)입니다.

이는 POWER8 1-core짜리의 작은 가상머신을 2주간 무료로 사용할 수 있도록 해주는 platform 입니다.  2주가 끝난 뒤에는 다시 또 신청하면 다시 2주간 더 사용할 수 있습니다.  단, 사용하던 disk의 내용은 일단 지워지는데, 지워지기 전에 "Save Image" 기능을 이용하여 save 해놓았다가 가상머신을 재신청할 때 그 saved image를 이용하여 가상머신을 달라고 요청하면 기존 image를 그대로 또 사용하실 수 있습니다.

신청을 하기 위해서는 먼저 IBM PartnerWorld에 회원으로 가입하셔야 합니다.   신청 및 가상머신 신청은 아래 URL에서 하시면 됩니다.

https://www-356.ibm.com/partnerworld/wps/servlet/ContentHandler/stg_com_sys_power-development-platform

위 URL에서 가운데의 "Go to the Program to get started"를 클릭하시면 됩니다.   그러면 VPN 설치 후에 ssh로 연결될 수 있는 가상머신의 IP와 passwd가 제공됩니다.   신청부터 실제 access까지는 통상 2~3시간 정도가 걸리는 것 같습니다.

아쉬운 점은 disk size가 35GB로 제한되어 있다는 점입니다.  써보면 항상 이 부분이 가장 아쉽던데, 공짜이니 그 정도도 고맙게 쓰고 있습니다.







2018년 8월 28일 화요일

nvidia-docker를 위한 Redhat 7.5 ppc64le docker image의 build


** 이 문서에서 서술하는 방식으로 만든 이미지들의 완성본을 다음의 주소에서 받으실 수 있습니다.   IBM GPU 서버인 AC922이나 Minsky 서버를 위한 Redhat 7.5 ppc64le + CUDA 9.2 + cudnn 7.2.1 + NCCL 2.2 + PowerAI 5.2 + Anaconda 2 (py2) or Anaconda3 (py3) 기반으로 만들어진 이미지들입니다.

https://hub.docker.com/r/bsyu/rhel75_ppc64le_cuda92_all_py2_powerai52/
https://hub.docker.com/r/bsyu/rhel75_ppc64le_cuda92_all_py3_powerai52/

Pull 할 때는 다음과 같이 하시면 됩니다.

# docker pull bsyu/rhel75_ppc64le_cuda92_all_py2_powerai52:v0.4     (python2.7)
# docker pull bsyu/rhel75_ppc64le_cuda92_all_py3_powerai52:v0.3     (python3.6)

아래는 이 docker image들을 만들 때의 과정입니다.

----------------------

먼저, 현재 host 서버의 OS(여기서는 Redhat 7.5 ALT ppc64le)를 기반으로 docker base image를 만듭니다.  아래 github에서 제공되는 mkimage-yum.sh를 사용하면 쉽습니다.

https://github.com/moby/moby/blob/master/contrib/mkimage-yum.sh

[root@ING ~]# cd docker

[root@ING docker]# vi mkimage-yum.sh    #  https://github.com/moby/moby/blob/master/contrib/mkimage-yum.sh의 script를 copy & paste

[root@ING docker]# chmod +x mkimage-yum.sh

[root@ING docker]# ./mkimage-yum.sh baserehel75alt
...
Running transaction
  Installing : libgcc-4.8.5-28.el7_5.1.ppc64le                                                                         1/243
  Installing : redhat-release-server-7.5-7.el7a.ppc64le                                                                2/243
  Installing : setup-2.8.71-9.el7.noarch                                                                               3/243
  Installing : filesystem-3.2-25.el7.ppc64le                                                                           4/243
  Installing : tzdata-2018e-3.el7.noarch                                                                               5/243
  Installing : basesystem-10.0-7.el7.noarch                                                                            6/243
...
  xz-libs.ppc64le 0:5.2.2-1.el7                                       yum-metadata-parser.ppc64le 0:1.1.4-10.el7
  zlib.ppc64le 0:1.2.7-17.el7

Complete!
+ [[ -n '' ]]
+ yum -c /etc/yum.conf --installroot=/tmp/mkimage-yum.sh.Oh71td -y clean all
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-manager
This system is not registered with an entitlement server. You can use subscription-manager to register.
There are no enabled repos.
 Run "yum repolist all" to see the repos you have.
 To enable Red Hat Subscription Management repositories:
     subscription-manager repos --enable <repo>
 To enable custom repositories:
     yum-config-manager --enable <repo>

Script가 위와 같이 성공적으로 완료된 뒤에 message에 표시된 /tmp 밑의 directory로 가보면 아래와 같이 mini-OS image가 마련되어 있는 것을 보실 수 있습니다. 
이제 이걸 tar로 말아올린 뒤 docker import 명령으로 import 해주면 됩니다.

[root@ING docker]# cd /tmp/mkimage-yum.sh.Oh71td

[root@ING mkimage-yum.sh.Oh71td]# ls -ltr
total 12
dr-xr-xr-x  2 root root    6 Dec 15  2017 sys
drwxr-xr-x  2 root root    6 Dec 15  2017 srv
dr-xr-xr-x  2 root root    6 Dec 15  2017 proc
drwxr-xr-x  2 root root    6 Dec 15  2017 opt
drwxr-xr-x  2 root root    6 Dec 15  2017 mnt
drwxr-xr-x  2 root root    6 Dec 15  2017 media
drwxr-xr-x  2 root root    6 Dec 15  2017 home
drwxr-xr-x  2 root root  180 Dec 15  2017 dev
dr-xr-xr-x  2 root root    6 Dec 15  2017 boot
lrwxrwxrwx  1 root root    7 Aug  9 09:47 bin -> usr/bin
lrwxrwxrwx  1 root root    8 Aug  9 09:47 sbin -> usr/sbin
lrwxrwxrwx  1 root root    9 Aug  9 09:47 lib64 -> usr/lib64
lrwxrwxrwx  1 root root    7 Aug  9 09:47 lib -> usr/lib
drwxr-xr-x 13 root root  207 Aug  9 09:47 usr
drwxrwxrwt  7 root root  117 Aug  9 09:47 tmp
drwxr-xr-x 19 root root  335 Aug  9 09:47 var
drwxr-xr-x 15 root root  261 Aug  9 09:47 run
drwxr-xr-x 68 root root 8192 Aug  9 09:47 etc
dr-xr-x---  2 root root   91 Aug  9 09:47 root

[root@ING mkimage-yum.sh.Oh71td]# tar -zcf /tmp/rhel7_ppc64le.tar.gz .

[root@ING mkimage-yum.sh.Oh71td]# ls -l /tmp/rhel7_ppc64le.tar.gz
-rw-r--r-- 1 root root 224266718 Aug  9 10:15 /tmp/rhel7_ppc64le.tar.gz

[root@ING mkimage-yum.sh.Oh71td]# docker import /tmp/rhel7_ppc64le.tar.gz bsyu/rhel75_ppc64le:base

[root@ING mkimage-yum.sh.Oh71td]# docker images
REPOSITORY                 TAG                         IMAGE ID            CREATED             SIZE
bsyu/rhel75_ppc64le        base                        3bf1baa60960        4 seconds ago       715 MB

이제 이 base image를 기반으로 dockerfile을 편집해서 CUDA 명령을 쓸 수 있는 nvidia-docker image를 만들면 됩니다.

그러기 위해서는 먼저 이 docker base image에서도 yum 명령을 써야 하는데, 기본적으로 Redhat에서 제공되는 /etc/yum.repos.d/redhat.repo 파일은 매번 yum을 기동할 때마다 자동으로 blank로 reset 되어버립니다.   따라서 이 파일 이름말고, 다른 이름, 가령 /etc/yum.repos.d/new.repo라는 이름으로 host OS에 있는 /etc/yum.repos.d/redhat.repo 파일을 그대로 저장해두면 host OS에서처럼 외부 YUM repository를 자유롭게 쓸 수 있습니다.   물론 이때 /etc/pki/entitlement/*-key.pem 등의 필요 ceritificate 파일들도 함께 docker image 속의 /etc/pki/entitlement에 copy 해두어야 합니다.   가령 다음과 같이 하면 됩니다.

[bsyu@p57a22 ~]# docker run -ti --rm -v /etc:/mnt bsyu/rhel75_ppc64le:base bash

----inside docker container------

[root@7e3b00f3fac2 ~]# cp /mnt/pki/entitlement/*.pem /etc/pki/entitlement

[root@7e3b00f3fac2 ~]# cp /mnt/yum.repos.d/redhat.repo /etc/yum.repos.d/new.repo    # 절대 redhat.repo라는 이름으로 copy하면 안 됩니다.

[root@7e3b00f3fac2 ~]# rpm -Uvh /mnt/epel-release-7-11.noarch.rpm   # epel-release-7-11.noarch.rpm 파일의 경우 host의 /etc 밑에 미리 copy해둡니다.

[root@7e3b00f3fac2 ~]# yum install wget curl cmake cmake3 make automake autoconf bzip2 zip unzip git gcc gcc-c++ gfortran yum-utils   # 기본적으로 필요한 package들을 미리 설치해둡니다.

----outside docker container------

[bsyu@p57a22 ~]# docker ps
CONTAINER ID        IMAGE                   COMMAND             CREATED             STATUS              PORTS              NAMES
7e3b00f3fac2        bsyu/rhel75_ppc64le:base bash   "bash"              23 hours ago        Up 23 hours                  angry_bose

[bsyu@p57a22 ~]# docker commit 7e3b00f3fac2 bsyu/rhel75_ppc64le:base_v6

이제 dockerfile을 이용하여 nvidia-docker 이미지를 만듭니다.  먼저 (다른 이름도 상관없습니다만) docker라는 directory를 만들고, 거기에 docker image 속에 설치할 cuda*.rpm 등을 미리 copy 해둡니다.  그리고나서 dockerfile.cuda92 파일을 다음과 같이 만듭니다.

[bsyu@p57a22 ~]# mkdir /home/docker && cd /home/docker   

[root@p57a22 docker]# vi dockerfile.cuda92
FROM bsyu/rhel75_ppc64le:base_v6

# Update image
# 'local' means rhel local repository
# To use these repos below in building process, you must set up the RHEL subscription and enable these repos on your host system first.
RUN yum-config-manager --enable rhel-7-for-power-9-rpms/7Server/ppc64le
RUN yum-config-manager --enable rhel-7-for-power-9-optional-rpms/7Server/ppc64le
RUN yum-config-manager --enable rhel-7-for-power-9-extras-rpms/7Server/ppc64le
RUN yum-config-manager --enable epel/ppc64le

RUN mkdir /tmp/temp
COPY cuda-repo-rhel7-9-2-local* /tmp/temp/
RUN rpm -Uvh /tmp/temp/cuda-repo-rhel7-9-2-local*
RUN yum install -y cuda

# nvidia-docker 1.0
LABEL com.nvidia.volumes.needed="nvidia_driver"
LABEL com.nvidia.cuda.version="${CUDA_VERSION}"

RUN echo "/usr/local/nvidia/lib" >> /etc/ld.so.conf.d/nvidia.conf && \
    echo "/usr/local/nvidia/lib64" >> /etc/ld.so.conf.d/nvidia.conf

ENV PATH /usr/local/nvidia/bin:/usr/local/cuda/bin:${PATH}
ENV LD_LIBRARY_PATH /usr/local/nvidia/lib:/usr/local/nvidia/lib64:/usr/local/cuda/lib64/stubs

# nvidia-container-runtime
ENV NVIDIA_VISIBLE_DEVICES all
ENV NVIDIA_DRIVER_CAPABILITIES compute,utility
ENV NVIDIA_REQUIRE_CUDA "cuda>=9.2"

RUN yum clean all &&  rm -rf /var/cache/yum/* /tmp/temp
RUN rpm -e cuda-repo-rhel7-9-2-local

이제 위의 dockerfile.cuda92 파일을 이용하여 다음과 같이 bsyu/rhel75_ppc64le_cuda92_all:v0.1라는 이름으로 docker image를 build합니다.

[root@p57a22 docker]# docker build -t bsyu/rhel75_ppc64le_cuda92_all:v0.1 -f dockerfile.cuda92 .
Sending build context to Docker daemon 1.705 GB
Step 1/24 : FROM docker.io/bsyu/rhel75_ppc64le:base_v5
 ---> fc88cbad6b18
....
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
 ---> f26c0a2167a0
Removing intermediate container bb911c868f27
Step 26/26 : RUN rpm -e cuda-repo-rhel7-9-2-local
 ---> Running in 6bceb027f2f1

 ---> deaf06286bf4
Removing intermediate container 6bceb027f2f1
Successfully built deaf06286bf4

다음과 같이 지정한 이름의 docker image가 생성된 것을 확인합니다.

[root@p57a22 docker]# docker images | grep bsyu
bsyu/rhel75_ppc64le_cuda92_all                 v0.1                        deaf06286bf4        45 seconds ago      8.11 GB
bsyu/rhel75_ppc64le                       base_v6                     872374a229f2        8 minutes ago       1.35 GB

이제 nvidia-docker로 이 image를 구동하여 nvidia-smi가 제대로 작동하는지 확인합니다.   그리고 PowerAI나 libcudnn* libnccl2* 등 필요 SW를 더 설치한 뒤 commit 하여 필요한 image를 추가로 만듭니다.

[bsyu@p57a22 data]$ nvidia-docker run -ti --rm -v /home/bsyu/files:/mnt bsyu/rhel75_ppc64le_cuda92_all:v0.1 bash

----inside docker container------

[root@e8a6f36fb132 /]# nvidia-smi
Thu Aug 23 01:28:52 2018
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 396.26                 Driver Version: 396.26                    |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla P100-SXM2...  Off  | 00000002:01:00.0 Off |                    0 |
| N/A   28C    P0    29W / 300W |     10MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   1  Tesla P100-SXM2...  Off  | 00000003:01:00.0 Off |                    0 |
| N/A   30C    P0    32W / 300W |     10MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   2  Tesla P100-SXM2...  Off  | 0000000A:01:00.0 Off |                    0 |
| N/A   28C    P0    29W / 300W |     10MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
|   3  Tesla P100-SXM2...  Off  | 0000000B:01:00.0 Off |                    0 |
| N/A   31C    P0    29W / 300W |     10MiB / 16280MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+

+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+


2018년 8월 22일 수요일

PowerAI 5.2 tensorflow의 LMS 테스트

Caffe와는 달리 tensorflow는 그 자체가 python library이므로, tensorflow에서 LMS를 사용하기 위해서는 python coding이 필요하며, 그를 위한 가이드를 이전 posting에 올린 바 있습니다.  이번에는 그렇게 적용된 example code를 이용하여 LMS가 주는 효과를 테스트 해보겠습니다.

먼저, PowerAI에서 제공되는 High Performance Model을 아래 script를 이용해서 원하는 위치로 copy합니다.

[[bsyu@p57a22 ~]$ /opt/DL/tensorflow-performance-models/bin/tensorflow-install-models ~/models

여기서 제공되는 tf_cnn_benchmarks.py를 다음과 같이 이용해서 간단한 테스트가 가능합니다.   이 python code에서는 data_dir을 지정하지 않을 경우 synthetic data, 즉 임의로 합성한 가상 data를 이용해서 training을 합니다.  여기서 image_size=6944라는 것은 6944 * 6944 = 약 48 megapixel의 이미지 크기를 지정하는 것입니다.  Color image의 경우 1 pixel이 3 byte이고, black/white image인 경우 1 pixel = 1 byte입니다.   아래와 같이 image_size=6944로 주면 140MB 정도 되는 큰 이미지이므로, batch_size=1이라고 해도 당연히 out-of-memory error가 나는 것을 보실 수 있습니다.

[bsyu@p57a22 tf_cnn_benchmarks]$ python tf_cnn_benchmarks.py --batch_size=1 --num_batches=30 --model=googlenet –num_gpus=1 --image_size=6944
TensorFlow:  1.8
Model:       googlenet
Dataset:     imagenet (synthetic)
Mode:        training
...
ResourceExhaustedError (see above for traceback): OOM when allocating tensor with shape[1,480,868,868] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
         [[Node: v0/tower_0/cg/incept_v10_1/concat = ConcatV2[N=4, T=DT_FLOAT, Tidx=DT_INT32, _device="/job:localhost/replica:0/task:0/device:GPU:0"](v0/tower_0/cg/incept_v10_1/conv9/Relu, v0/tower_0/cg/incept_v10_1/conv11/Relu, v0/tower_0/cg/incept_v10_1/conv13/Relu, v0/tower_0/cg/incept_v10_1/conv14/Relu, v0/tower_0/cg/incept_v10_8/concat/axis)]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info.


환경에 따라 다르겠습니다만, googlenet model에서 16GB의 GPU memory로 처리가능한 image 크기는 대략 5000^2 즉 25 megapixel 정도로 알려져 있습니다.  그러나 LMS를 이용하면 더 큰 크기도 error 없이 처리가 가능합니다.

[bsyu@p57a22 tf_cnn_benchmarks]$ time CUDA_VISIBLE_DEVICES=2 python tf_cnn_benchmarks.py --batch_size=1 --num_batches=30 --model=googlenet  --num_gpus=1 --lms=True --image_size=8192
TensorFlow:  1.8
Model:       googlenet
Dataset:     imagenet (synthetic)
…
I0820 08:25:47.774425 70366577052288 tf_logging.py:116] [LMS][0] n_tensors: all tensors
I0820 08:25:47.774482 70366577052288 tf_logging.py:116] [LMS][0] lb: 1
I0820 08:25:49.216497 70366577052288 tf_logging.py:116] [LMS][0] Edited model is valid and logically equivalent to the original one
I0820 08:25:49.216679 70366577052288 tf_logging.py:116] [LMS][0] Added 227 ops into the model
I0820 08:25:49.216750 70366577052288 tf_logging.py:116] [LMS][0] Editing model for LMS, took: 1442.2016143798828 ms
I0820 08:25:49.216805 70366577052288 tf_logging.py:116] [LMS][0] 83 tensors will be swapped out(in) to(from) the host
…
----------------------------------------------------------------
total images/sec: 0.21
----------------------------------------------------------------
real    3m44.957s


저 위 message에서 보시듯이, tensorflow LMS에서도 일부 tuning 가능한 요소가 있습니다.

n_tensors :
host 서버의 RAM으로 swap-out 했다가 필요시 swap-in 할 tensor의 개수입니다.  많을 수록 GPU memory를 적게 쓰지만, 대신 training 성능은 나빠집니다.  Default 값은 -1로서, 일단 모든 tensor를 다 swap 대상으로 삼은 뒤, 초기 estimation 후 자동으로 적절한 값을 산정하여 사용합니다.

lb :
Lower Bound의 약자로서, 미리 swap-in 시켜놓을 tensor의 개수입니다.  많을 수록 성능은 좋아지겠지만, 대신 GPU memory를 많이 쓰게 됩니다.  Default 값은 1입니다.


이 tf_cnn_benchmarks.py에서는 저 tuning 요소들도 parameter로 받아들이게 되어 있습니다.  다음과 같이 lb를 3으로 해서 해보겠습니다.  확실히 좀 더 빨라지는 것을 보실 수 있습니다.

[bsyu@p57a22 tf_cnn_benchmarks]$ time CUDA_VISIBLE_DEVICES=2 python tf_cnn_benchmarks.py --batch_size=1 --num_batches=30 --model=googlenet  --num_gpus=1 --lms=True --image_size=8192 --lms_lb=3
…
I0821 07:06:26.437339 70366510140032 tf_logging.py:116] [LMS][0] Editing model for LMS
I0821 07:06:26.437493 70366510140032 tf_logging.py:116] [LMS][0] n_tensors: all tensors
I0821 07:06:26.437549 70366510140032 tf_logging.py:116] [LMS][0] lb: 3
I0821 07:06:27.872007 70366510140032 tf_logging.py:116] [LMS][0] Edited model is valid and logically equivalent to the original one
I0821 07:06:27.872166 70366510140032 tf_logging.py:116] [LMS][0] Added 227 ops into the model
I0821 07:06:27.872235 70366510140032 tf_logging.py:116] [LMS][0] Editing model for LMS, took: 1434.621810913086 ms
I0821 07:06:27.872288 70366510140032 tf_logging.py:116] [LMS][0] 83 tensors will be swapped out(in) to(from) the host
…
----------------------------------------------------------------
total images/sec: 0.23
----------------------------------------------------------------
real    3m28.466s


참고로 n_tensors는 일부러 크게 키워도 여전히 83개의 tensor만을 처리하고, 또 lb를 5로 주니 LMS에서도 out-of-memory error가 나더군요.

PowerAI 5.2의 caffe-ibm에서의 LMS 테스트 (cifar10)

먼저, cifar10 dataset을 준비합니다.  보통 caffe에 포함되어 있는 get_cifar10.sh를 수행하면 lmdb로 포맷된 dataset을 일사천리로 download 받을 수 있습니다.

[bsyu@p57a22 caffe-ibm]$ pwd
/opt/DL/caffe-ibm

[bsyu@p57a22 caffe-ibm]$ ./data/cifar10/get_cifar10.sh
Downloading...
--2018-08-21 04:14:32--  http://www.cs.toronto.edu/~kriz/cifar-10-binary.tar.gz
Resolving www.cs.toronto.edu (www.cs.toronto.edu)... 128.100.3.30
Connecting to www.cs.toronto.edu (www.cs.toronto.edu)|128.100.3.30|:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 170052171 (162M) [application/x-gzip]
Saving to: ‘cifar-10-binary.tar.gz’

100%[============================================================================>] 170,052,171 37.4MB/s   in 4.6s

2018-08-21 04:14:37 (35.4 MB/s) - ‘cifar-10-binary.tar.gz’ saved [170052171/170052171]

Unzipping...
Done.

LMS의 효용성을 보기 위해서는 image size가 커야 합니다만, cifar10에 포함된 image들은 6만개의 32x32 칼러 이미지들로서 size가 매우 작습니다.  대신 한번에 GPU에서 처리되는 image 개수인 batch_size를 크게 하여 GPU memory를 꽉 차게 해보겠습니다.

[bsyu@p57a22 caffe-ibm]$ vi examples/cifar10/cifar10_full_train_test.prototxt
...
  data_param {
    source: "examples/cifar10/cifar10_train_lmdb"
    batch_size: 22000   #  원래는 100
    backend: LMDB
  }

[bsyu@p57a22 caffe-ibm]$ which caffe
/opt/DL/caffe-ibm/bin/caffe

이제 수행해봅니다.  batch_size: 22000 정도로는 LMS 없이도 잘 수행되는 것을 보실 수 있습니다.

[bsyu@p57a22 caffe-ibm]$ caffe train -gpu 2 --solver=examples/cifar10/cifar10_full_solver.prototxt
…
I0821 03:51:25.516746 52459 solver.cpp:497] Snapshotting to HDF5 file examples/cifar10/cifar10_full_iter_200.caffemodel.h5
I0821 03:51:28.164131 52459 sgd_solver.cpp:373] Snapshotting solver state to HDF5 file examples/cifar10/cifar10_full_iter_200.solverstate.h5
I0821 03:51:28.753823 52459 solver.cpp:336] Iteration 200, loss = 1.71708
I0821 03:51:28.753847 52459 solver.cpp:341] Optimization Done.
I0821 03:51:28.753859 52459 caffe.cpp:421] Optimization Done.


이때 nvidia-smi로 GPU memory 사용량을 보면 거의 아슬아슬하게 GPU memory가 꽉 찬 것을 보실 수 있습니다.





이번에는 batch_size: 24000으로 늘려서 다시 동일한 training을 수행해봅니다.  이번에는 error가 납니다.

[bsyu@p57a22 caffe-ibm]$ caffe train -gpu 2 --solver=examples/cifar10/cifar10_full_solver.prototxt
…
F0821 04:26:41.953693 60726 syncedmem.cpp:569] Check failed: error == cudaSuccess (2 vs. 0)  out of memory
*** Check failure stack trace: ***
    @     0x3fffa30acf8c  (unknown)
    @     0x3fffa30afa0c  (unknown)
    @     0x3fffa30ac9b4  (unknown)
    @     0x3fffa30b0634  (unknown)
    @     0x3fffaac7c154  caffe::SyncedMemory::get_gpu_ptr()
    @     0x3fffaac77650  caffe::SyncedMemory::mutable_gpu_data()
    @     0x3fffaaa8aa28  caffe::Blob<>::mutable_gpu_diff()
    @     0x3fffaaced94c  caffe::PoolingLayer<>::Backward_gpu()
    @     0x3fffaac207a4  caffe::Net<>::BackwardFromTo()
    @     0x3fffaac20ad8  caffe::Net<>::Backward()
    @     0x3fffaac5aabc  caffe::Solver<>::Step()
    @     0x3fffaac5b328  caffe::Solver<>::Solve()
    @         0x1000e9e4  train()
    @         0x1000a848  main
    @     0x3fff88155100  generic_start_main.isra.0
    @     0x3fff881552f4  __libc_start_main
    @              (nil)  (unknown)


이번에는 역시 batch_size: 24000으로 하되, training시킬 때 -lms라는 옵션을 넣어서 수행합니다.   이번에는 LMS 관련 message가 나오면서, 문제없이 training이 완료되는 것을 보실 수 있습니다.

[bsyu@p57a22 caffe-ibm]$ caffe train -gpu 2 -lms --solver=examples/cifar10/cifar10_full_solver.prototxt
…
I0821 04:30:40.643229 75342 syncedmem.cpp:349] [LMS:2] allocate: size=786432008 [count=80 demanded=159% allocated=35% available=19%]
I0821 04:30:42.045287 75342 syncedmem.cpp:349] [LMS:2] allocate: size=786432008 [count=80 demanded=159% allocated=39% available=36%]
I0821 04:30:44.427603 75342 syncedmem.cpp:349] [LMS:2] allocate: size=3145728008 [count=80 demanded=159% allocated=53% available=61%]
I0821 04:30:44.605427 75342 solver.cpp:244] Iteration 0 (0 iter/s, 0s/200 iters), loss = 2.30264
I0821 04:30:44.605461 75342 solver.cpp:263]     Train net output #0: loss = 2.30264 (* 1 = 2.30264 loss)
I0821 04:30:44.605484 75342 sgd_solver.cpp:128] Iteration 0, lr = 0.001
I0821 04:30:44.636401 75346 data_layer.cpp:86] Restarting data prefetching from start.
…
I0821 04:42:50.161826 75342 solver.cpp:497] Snapshotting to HDF5 file examples/cifar10/cifar10_full_iter_200.caffemodel.h5
I0821 04:42:50.164942 75342 sgd_solver.cpp:373] Snapshotting solver state to HDF5 file examples/cifar10/cifar10_full_iter_200.solverstate.h5
I0821 04:42:50.908159 75342 solver.cpp:336] Iteration 200, loss = 1.72997
I0821 04:42:50.908186 75342 solver.cpp:341] Optimization Done.
I0821 04:42:50.908195 75342 caffe.cpp:421] Optimization Done.

그리고 LMS 덕분에 GPU memory 사용량이 확 줄어든 것을 보실 수 있습니다.





그러나 lms를 쓸 경우, 분명히 성능은 다소 느려집니다.  PCIe 대신 NVLink를 쓴다고 해도, GPU 메모리보다 host 서버의 DRAM이 느린 것이 당연하니까요.  그러나 lms도 약간이나마 튜닝을 할 수는 있습니다.  주로 다음의 2가지 추가 옵션을 쓰시면 됩니다.

-lms_size_threshold <size in KB> : Default는 1000.
여기에 명기하는 것보다 작은 크기의 memory chunk는 LMS에 의해 swap-out/in 되지 않고 GPU 메모리에 상주하게 하라는 뜻입니다. 

-lms_exclude <size in MB> : Default는 0.
GPU 메모리 크기에서 이 값을 뺀 크기가 LMS cache로 사용되는 GPU memory 할당량의 soft limit입니다.  이 값을 작게 할 수록 GPU 메모리 사용량이 커져 성능이 좋아집니다.


가령 아래의 명령어 옵션은 crop size 2240x2240, batch size 5인 고해상도 이미지 dataset에서 GoogleNet model을 사용할 경우 가장 좋은 LMS 성능을 낸 example입니다.  그러나 모델의 신경망 구조와 data 크기, batch_size와 GPU 메모리 크기에 따라 이상적인 튜닝값은 다 다릅니다.

$ caffe train -solver=solver.prototxt -gpu all -lms -lms_size_threshold 1000 -lms_exclude 1400


실제 cifar10에서는 어떨까요 ?  먼저 lms 없이 수행할 수 있는 최대 크기의 batch_size로 non-LMS caffe training을 해보겠습니다.  그럴 경우, LMS에서 사용했던 batch_size 24000 * max_iter 200와 동일한 개수의 image를 non-LMS에서 처리하기 위해서는  batch_size 22000 * max_iter 219를 쓰시면 됩니다.

[bsyu@p57a22 caffe-ibm]$ time caffe train -gpu 2 --solver=examples/cifar10/cifar10_full_solver.prototxt
…
real    11m46.926s


이와 거의 같은 개수의 image인 batch_size 24000 * max_iter 200를 LMS로 별도 튜닝 옵션 없이 처리해보겠습니다.  확실히 조금 더 느립니다.

[bsyu@p57a22 caffe-ibm]$ time caffe train -gpu 2 -lms --solver=examples/cifar10/cifar10_full_solver.prototxt
…
real    12m12.219s

이번에는 동일한 training에 -lms_size_threshold와 -lms_exclude를 추가해서 수행해봅니다.  성능이 거의 non-LMS만큼 빨라진 것이 보입니다.

[bsyu@p57a22 caffe-ibm]$ time caffe train -gpu 2 -lms -lms_size_threshold 1000 -lms_exclude 1400 --solver=examples/cifar10/cifar10_full_solver.prototxt
…
real    11m47.405s

그리고 더 큰 batch_size를 사용함에도 LMS에 의해 줄어들었던 GPU memory 사용량이 거의 non-LMS 수준으로 다시 늘어난 것이 보입니다.