【发布时间】:2016-11-06 08:50:29
【问题描述】:
我在集群上使用 Tensorflow,我想告诉 Tensorflow 只在一个单核上运行(即使有更多可用核)。
有人知道这是否可能吗?
【问题讨论】:
标签: tensorflow core
我在集群上使用 Tensorflow,我想告诉 Tensorflow 只在一个单核上运行(即使有更多可用核)。
有人知道这是否可能吗?
【问题讨论】:
标签: tensorflow core
为了在单个 CPU 线程上运行 Tensorflow,我使用:
session_conf = tf.ConfigProto(
intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1)
sess = tf.Session(config=session_conf)
device_count 限制正在使用的 CPU 的数量,而不是内核或线程的数量。
tensorflow/tensorflow/core/protobuf/config.proto 说:
message ConfigProto {
// Map from device type name (e.g., "CPU" or "GPU" ) to maximum
// number of devices of that type to use. If a particular device
// type is not found in the map, the system picks an appropriate
// number.
map<string, int32> device_count = 1;
在 Linux 上,您可以运行 sudo dmidecode -t 4 | egrep -i "Designation|Intel|core|thread" 来查看您有多少 CPU/内核/线程,例如下面有2个CPU,每个有8个核心,每个有2个线程,总共有2*8*2=32个线程:
fra@s:~$ sudo dmidecode -t 4 | egrep -i "Designation|Intel|core|thread"
Socket Designation: CPU1
Manufacturer: Intel
HTT (Multi-threading)
Version: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
Core Count: 8
Core Enabled: 8
Thread Count: 16
Multi-Core
Hardware Thread
Socket Designation: CPU2
Manufacturer: Intel
HTT (Multi-threading)
Version: Intel(R) Xeon(R) CPU E5-2667 v4 @ 3.20GHz
Core Count: 8
Core Enabled: 8
Thread Count: 16
Multi-Core
Hardware Thread
在 Ubuntu 14.04.5 LTS x64 和 Ubuntu 16.04 LTS x64 上使用 Tensorflow 0.12.1 和 1.0.0 进行测试。
【讨论】:
device_count={'GPU': 0}吗?
tf.config.threading.set_inter_op_parallelism_threads(1) tf.config.threading.set_intra_op_parallelism_threads(1)
您可以在创建会话时通过在ConfigProto 中传递适当的device_count 作为config 参数来限制TensorFlow 使用的特定类型的设备数量。例如,您可以限制 CPU 设备的数量,如下所示:
config = tf.ConfigProto(device_count={'CPU': 1})
sess = tf.Session(config=config)
with sess.as_default():
print(tf.constant(42).eval())
【讨论】:
是的,可以通过线程关联来实现。线程亲和性允许您决定由 cpu 的哪个特定核心执行哪个特定线程。对于线程关联,您可以在 Linux 上使用“taskset”或“numatcl”。你也可以使用https://man7.org/linux/man-pages/man2/sched_setaffinity.2.html和https://man7.org/linux/man-pages/man3/pthread_setaffinity_np.3.html
以下代码不会指示/指示 Tensorflow 仅在一个内核上运行。
TensorFlow 1
session_conf = tf.ConfigProto(
intra_op_parallelism_threads=1,
inter_op_parallelism_threads=1)
sess = tf.Session(config=session_conf)
TensorFlow 2
import os
# reduce number of threads
os.environ['TF_NUM_INTEROP_THREADS'] = '1'
os.environ['TF_NUM_INTRAOP_THREADS'] = '1'
import tensorflow
这将总共生成至少 N 个线程,其中 N 是 cpu 内核的数量。大多数时候只有一个线程在运行,而其他线程处于睡眠模式。
来源: https://github.com/tensorflow/tensorflow/issues/42510 https://github.com/tensorflow/tensorflow/issues/33627
【讨论】: