在win10/win11上可以通过DirectML使用 AMD/Intel的核显或是独立显卡进行训练
首先要更新显卡驱动:
AMD驱动在这里更新,如果是核显选择具体CPU型号即可:
AMD核显驱动https://www.amd.com/en/support/download/drivers.htmlIntel CPU的驱动下载地址
Intel核显驱动https://www.intel.cn/content/www/cn/zh/search.html?ws=text#sort=relevancy&layout=table&f:@tabfilter=%5BDownloads%5D&f:@stm_10385_zh=%5B%E5%A4%84%E7%90%86%E5%99%A8%5D如果是安装TensorFlow 1.x版本可以参考微软官方方法
对应的 whl 文件在这里:
tensorflow-directmlhttps://pypi.tuna.tsinghua.edu.cn/simple/tensorflow-directml/
如果是TensorFlow 2.x,需要安装 tensorflow-directml-plugin,详细可以参考微软官方文档:
同样对于Pytorch也有对应的DirectML加速版本:
我安装的是 tensorflow-directml-1.15.8,在NVIDIA GTX 1070-Ti 显卡上分别测试了原始cuDNN进行训练和用 DML 进行训练的速度差异,写了一个6层的卷积神网用于做图像增强,并且在AMD最新的zen4架构CPU R7-7840H(核显 Radeon 780M 满血为12CU,实测只有6CU)上也进行了测试,训练每一轮的速度如下(1070Ti在cuda11.1上原生/dml分别35.2/44.9ms,更新cuda11.8后速度明显提升30.5/43.5ms),另外后面发现台式机的核显可以在主板BIOS里面设置打开iGPU,补上核显的数据(Intel的核显还是一如既往的烂)
cuDNN | directML | CPU | GPU-TFlops | CPU-TFlops | |
GTX 1070-Ti | 30.5 ms | 43.5 ms | - | 8.3 | - |
I7-8700K | - | 932 ms | 700 ms | 0.45 | 0.79 |
R7-5700U | - | 160 ms | 930 ms | 1.6-2.0 | 0.5-0.9 |
R7-7840H (6CU) | - | 53.5 ms | 426 ms | 4.14 | 1.24 |
上面GTX 1070Ti用的CPU是I7-8700K,速度要明显比R7-7840H慢,CPU版本用的TensorFlow是编译了avx2指令集的版本,可以在这里下载:
Tensorflow-1.14.0-avx2https://github.com/fo40225/tensorflow-windows-wheel/blob/master/1.14.0/py37/CPU/avx2/tensorflow-1.14.0-cp37-cp37m-win_amd64.whl另外测了一下家里笔记本上Intel I5-1135G7和GeForce MX450显卡的速度
cuDNN | directML | CPU | GPU-TFlops | CPU-TFlops | |
I5-1135G7 | - | 585 ms | 900 ms | 1.4-1.6 | 0.47 |
MX450 | 80 ms | 110 ms | - | 1.3-2.8 | - |
I7-11800H | - | 760 ms | 496 ms | 0.7-0.8 | 0.95 |
从上面两组数据可以看出,采用directML接口比原生cuDNN接口慢30%左右
另外多GPU在directML模式下,可以通过设置Session里gpu_options.visible_device_list 的方式指定运行哪个GPU:
def CreateSession(graph=None):
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = "1"
return tf.Session(config=config, graph=graph)
具体参考微软的文档: