【问题标题】:How can I run Tensorflow on one single core?如何在单核上运行 Tensorflow?
【发布时间】:2016-11-06 08:50:29
【问题描述】:

我在集群上使用 Tensorflow,我想告诉 Tensorflow 只在一个单核上运行(即使有更多可用核)。

有人知道这是否可能吗?

【问题讨论】:

    标签: tensorflow core


    【解决方案1】:

    为了在单个 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 进行测试。

    【讨论】:

    • 不幸的是,这在 WIndows 10 (tf 1.5.0) 上运行时似乎没有效果。没有办法让其他程序免费使用内核是个问题。
    • @LiamRoche 我认为这不应该发生。您可能想在 tensorflow GitHub 存储库中提出问题。
    • 不需要加device_count={'GPU': 0}吗?
    • 对于 tf v2:tf.config.threading.set_inter_op_parallelism_threads(1) tf.config.threading.set_intra_op_parallelism_threads(1)
    • 按照您的代码使用以下 CPU,我得到 8 个线程。我怎样才能只得到1个线程?版本:Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz 核心数:4 核心启用:4 线程数:8 多核硬件线程
    【解决方案2】:

    您可以在创建会话时通过在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())
    

    【讨论】:

    • 我试过这个,但它不起作用。如果我向集群提交作业,Tensorflow 仍然可以在一个节点的所有可用内核上运行。我执行以下操作: init = tf.initialize_all_variables() #launch the graph config = tf.ConfigProto(device_count={'CPU': 1}) sess = tf.Session(config=config) sess.run(init)跨度>
    • 我也遇到了同样的问题。 tf.ConfigProto(device_count={'CPU': 1}) 不生效。 intra_op_parallelism_threads=1 和 inter_op_parallelism_threads=1 确实生效。
    【解决方案3】:

    是的,可以通过线程关联来实现。线程亲和性允许您决定由 cpu 的哪个特定核心执行哪个特定线程。对于线程关联,您可以在 Linux 上使用“taskset”或“numatcl”。你也可以使用https://man7.org/linux/man-pages/man2/sched_setaffinity.2.htmlhttps://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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-01-17
      • 2016-10-06
      • 1970-01-01
      • 2017-05-26
      • 2019-01-14
      • 2018-04-10
      • 1970-01-01
      相关资源
      最近更新 更多