Tensorflow 2.0 100% 使用所有 CPU 核心

Jon*_*n R 6 cpu preprocessor tensorflow

我的 Tensorflow 模型大量使用数据预处理,这些数据预处理应在 CPU 上完成,以使 GPU 开放用于训练。

top - 09:57:54 up 16:23,  1 user,  load average: 3,67, 1,57, 0,67
Tasks: 400 total,   1 running, 399 sleeping,   0 stopped,   0 zombie
%Cpu(s): 19,1 us,  2,8 sy,  0,0 ni, 78,1 id,  0,0 wa,  0,0 hi,  0,0 si,  0,0 st
MiB Mem :  32049,7 total,    314,6 free,   5162,9 used,  26572,2 buff/cache
MiB Swap:   6779,0 total,   6556,0 free,    223,0 used.  25716,1 avail Mem 

    PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND                                                                                                                
  17604 joro      20   0   22,1g   2,3g 704896 S 331,2   7,2   4:39.33 python  
Run Code Online (Sandbox Code Playgroud)

这就是顶部向我展示的。我想让这个 python 进程在所有核心上使用至少 90% 的可用 CPU。如何才能实现这一目标?

GPU利用率较好,在90%左右。虽然不知道为什么不是100%

Mon Aug 10 10:00:13 2020       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 440.100      Driver Version: 440.100      CUDA Version: 10.2     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  GeForce RTX 208...  Off  | 00000000:01:00.0  On |                  N/A |
| 35%   41C    P2    90W / 260W |  10515MiB / 11016MiB |     11%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|    0      1128      G   /usr/lib/xorg/Xorg                           102MiB |
|    0      1648      G   /usr/lib/xorg/Xorg                           380MiB |
|    0      1848      G   /usr/bin/gnome-shell                         279MiB |
|    0     10633      G   ...uest-channel-token=1206236727             266MiB |
|    0     13794      G   /usr/lib/firefox/firefox                       6MiB |
|    0     17604      C   python                                      9457MiB |
+-----------------------------------------------------------------------------+

Run Code Online (Sandbox Code Playgroud)

我找到的只是张量流 1.0 的解决方案:

sess = tf.Session(config=tf.ConfigProto(
  intra_op_parallelism_threads=NUM_THREADS))
Run Code Online (Sandbox Code Playgroud)

我有一个 Intel 9900k 和一个 RTX 2080 Ti,并使用 Ubuntu 20.04

E:当我在顶部添加以下代码时,它使用 1 个核心 100%

tf.config.threading.set_intra_op_parallelism_threads(1)
tf.config.threading.set_inter_op_parallelism_threads(1)
Run Code Online (Sandbox Code Playgroud)

但再次将此数字增加到 16 仅利用所有核心约 30%

Sri*_*rma 7

只是设置set_intra_op_parallelism_threads 并且set_inter_op_parallelism_threads 对我不起作用。如果其他人在同一个地方,经过多次努力解决同样的问题,下面的代码对我有用,将张量流的 CPU 使用率限制在 500% 以下:

import os
import tensorflow as tf
num_threads = 5
os.environ["OMP_NUM_THREADS"] = "5"
os.environ["TF_NUM_INTRAOP_THREADS"] = "5"
os.environ["TF_NUM_INTEROP_THREADS"] = "5"

tf.config.threading.set_inter_op_parallelism_threads(
    num_threads
)
tf.config.threading.set_intra_op_parallelism_threads(
    num_threads
)
tf.config.set_soft_device_placement(True)
Run Code Online (Sandbox Code Playgroud)


Mei*_*wjn 1

这可能有很多问题,我通过以下方式为我解决了它:

tf.config.threading.set_intra_op_parallelism_threads(<Your_Physical_Core_Count>) tf.config.threading.set_inter_op_parallelism_threads(<Your_Physical_Core_Count>)

两者都与您的物理核心数有关。您不希望超线程用于高度矢量化的操作,因为当没有任何间隙时,您无法从并行操作中受益。

“在高水平的矢量化下,执行间隙的数量非常小,并且可能没有足够的机会来弥补由于 HT 争用增加而造成的任何损失。”

来源:Saini 等人,NASAA 高级超级计算部门出版,2011 年:超线程对生产应用中处理器资源利用率的影响

编辑:我不再确定两者之一是否必须为 1。但其中一个 100% 需要设置为物理。