macOS High Sierra 10.13.6 にて eGPU (NVIDIA) を使う

目的は、DeepLearning (cifar10_train.py 動きました)
# 環境
- MacBook Pro (15-inch, 2016)
- macOS High Sierra 10.13.6（17G65）
- GeForce GTX 1080

私が購入したものは、下記です。
[GIGABYTE ビデオカード GTX1080搭載 外付VGA BOX GV-N1080IXEB-8GD] (https://www.amazon.co.jp/gp/product/B076ZBYVYB/ref=as_li_tl?ie=UTF8&camp=247&creative=1211&creativeASIN=B076ZBYVYB&linkCode=as2&tag=aniani0ec-22&linkId=569c1d344ca94fc7f1b8e06fcfa163f5)

# 注意
検証は、まだ十分に行なっておりません。
eGPU が認識でき、外部モニターに画面出力できている状態です。
macOS High Sierra 10.13.6（17G3015）で、認識しなくなりました。
（一旦、TimeMachineで戻しました。今の所、17G65 でしか動きません。）

# 手順
基本的には、下記のスクリプトを使用させていただきました。
https://github.com/learex/macOS-eGPU/tree/master#macos-egpush

- まずは、バックアップ。(TimeMachineを使用して、現状保存が良いとおもいます。)
- SIPの無効化。再起動時に command + R で起動させ、
`ユーティリティ`メニューの`ターミナル`を選択し、下記を実行する。

```
csrutil disable; reboot
```

- スクリプト実行

```
bash <(curl -s https://raw.githubusercontent.com/learex/macOS-eGPU/master/macOS-eGPU.sh)
```

- スクリプト実行時のログは、以下です。
  - ２度目の実行なので、ライセンスアグリーメントは、省略。及び、スクリプトの実行のみ。
  - 途中、eGPU の抜き差しの指示があるので、行なってください。

```
$ macos-egpu 

macOS-eGPU.sh (v1.5)

Accept license terms...                                                 [done]
Killing all other running programs...                                   [OK]
Internet connection established...                                      [YES]
Fetching system information...
   macOS info                                                           [done]
   system integrity protection                                          [done]
   thunderbolt version                                                  [done]
   GPU information                                                      [done]
   installed eGPU software                                              [done]
   installed patches                                                    [done]
   installed programs                                                   [done]
Setting internal switches...
Automatic eGPU information fetching...
   locking script execution                                             [done]
   elevating privileges
   Password:
   checking for elevated privileges                                     [OK]
   preparing secure eGPU connection                                     [done]
   waiting 20 seconds for user to connect eGPU
   20..19..18..17..16..15..14..13..12..11..10..9..8..7..6..5..4..3..2..1..0
   fetching eGPU information                                            [done]
   preparing secure eGPU disconnection                                  [done]
   waiting 20 seconds for user to disconnect eGPU
   20..19..18..17..16..15..14..13..12..11..10..9..8..7..6..5..4..3..2..1..0
   stetting switches                                                    [done]
   opening script execution lock                                        [done]
Fetching CUDA needs...
   fetching CUDA requiring apps list                                    [done]
   preparing matching                                                   [done]
   matching                                                             [done]
Checking for incompatibilies and up to date software...
   NVIDIA drivers                                                       [install scheduled]
   NVIDIA eGPU enabler                                                  [skip, incompatible]
   AMD legacy drivers                                                   [skip]
   T82 unblocker                                                        [skip]
   NVIDIA dGPU deactivator                                              [skip]
   macOS 10.13.4/.5 NVIDIA patch                                        [skip, incompatible]
   macOS 10.13.4+ thunderbolt 1/2 unlock                                [skip, incompatible]
   CUDA software
      CUDA drivers                                                      [skip]
      CUDA developer driver                                             [skip]
      CUDA toolkit                                                      [skip]
      CUDA samples                                                      [skip]
   thunderbolt daemon                                                   [skip]
   IO PCIE Tunnelled patch                                              [install scheduled]
Checking if SIP is sufficently disabled...                              [OK]

Download external content...
--- NVIDIA drivers ---
######################################################################## 100.0%

Checking for elevated privileges...

Uninstalling...
Installing...
   NVIDIA driver                                                        [done]
   IO PCIE Tunnelled patch                                              [done]
Patching...
deactivating auto-updates...

Finish...
   cleaning system                                                      [done]
Rebuilding caches
   kext cache                                                           [done]
   system cache                                                         [done]
A reboot will soon be performed...
5..4..3..2..1..0
reboot: / is busy updating, waiting for lock (this might take approx 15-30s)...
reboot: / is busy updating; waiting for lock
```

# 結果

<img width="812" alt="スクリーンショット 2018-08-19 15.49.11.png" src="https://qiita-image-store.s3.amazonaws.com/0/281890/8600d39e-9ae0-42c8-0711-c67c969fd72c.png">
<img width="827" alt="スクリーンショット 2018-08-19 15.49.50.png" src="https://qiita-image-store.s3.amazonaws.com/0/281890/6cbad20d-b15c-b2c3-5dd5-6fabd293c784.png">
<img width="822" alt="スクリーンショット 2018-08-19 15.50.06.png" src="https://qiita-image-store.s3.amazonaws.com/0/281890/c7318fd0-88b7-96b8-d1bc-8cd9af6f491a.png">

# おまけ

tensorflow r1.10 を最新CUDAの仕様でコンパイルに成功しました。

python 3.6.6
CUDA Toolkit 10.0 
cuDNN v7.3.1 (Sept 28, 2018), for CUDA 10.0
TF_NCCL_VERSION 2.3.5

作成したパッケージです。
tensorflow-1.10.1-cp36-cp36m-macosx_10_13_x86_64.whl

CPU 版に比べて、６倍くらいの処理スピードです。（こんなもんなのですかね？）

```
python cifar10_train.py 
Filling queue with 20000 CIFAR images before starting to train. This will take a few minutes.
2018-10-05 23:04:13.815498: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:858] OS X does not support NUMA - returning NUMA node zero
2018-10-05 23:04:13.815687: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1405] Found device 0 with properties: 
name: GeForce GTX 1080 major: 6 minor: 1 memoryClockRate(GHz): 1.7335
pciBusID: 0000:46:00.0
totalMemory: 8.00GiB freeMemory: 5.71GiB
2018-10-05 23:04:13.815706: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1484] Adding visible gpu devices: 0
2018-10-05 23:04:14.187784: I tensorflow/core/common_runtime/gpu/gpu_device.cc:965] Device interconnect StreamExecutor with strength 1 edge matrix:
2018-10-05 23:04:14.187826: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971]      0 
2018-10-05 23:04:14.187831: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] 0:   N 
2018-10-05 23:04:14.188000: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1097] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 5481 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1080, pci bus id: 0000:46:00.0, compute capability: 6.1)
2018-10-05 23:04:17.923348: step 0, loss = 4.68 (139.6 examples/sec; 0.917 sec/batch)
2018-10-05 23:04:18.492055: step 10, loss = 4.60 (2250.7 examples/sec; 0.057 sec/batch)
2018-10-05 23:04:18.914545: step 20, loss = 4.62 (3029.7 examples/sec; 0.042 sec/batch)
2018-10-05 23:04:19.332597: step 30, loss = 4.60 (3061.8 examples/sec; 0.042 sec/batch)
2018-10-05 23:04:19.756255: step 40, loss = 4.38 (3021.3 examples/sec; 0.042 sec/batch)
2018-10-05 23:04:20.198415: step 50, loss = 4.42 (2894.9 examples/sec; 0.044 sec/batch)
2018-10-05 23:04:20.622042: step 60, loss = 4.40 (3021.5 examples/sec; 0.042 sec/batch)
2018-10-05 23:04:21.040028: step 70, loss = 4.21 (3062.3 examples/sec; 0.042 sec/batch)
2018-10-05 23:04:21.449060: step 80, loss = 4.19 (3129.3 examples/sec; 0.041 sec/batch)
2018-10-05 23:04:21.868983: step 90, loss = 4.12 (3048.2 examples/sec; 0.042 sec/batch)
2018-10-05 23:04:22.424941: step 100, loss = 4.13 (2302.3 examples/sec; 0.056 sec/batch)
```