【问题标题】:Suppress tensorflow-metal initialization messages抑制 tensorflow-metal 初始化消息
【发布时间】:2023-02-03 06:17:33
【问题描述】:

我有一台 macbook pro m1 max 并通过tensowflow-apple-download 安装了 tensorflow。

每次运行使用 keras 或 tensorflow 的脚本时,我都会收到以下日志:

>>>  python script.py
Metal device set to: Apple M1 Max

systemMemory: 32.00 GB
maxCacheSize: 10.67 GB

2022-09-06 02:35:38.603010: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:305] Could not identify NUMA node of platform GPU ID 0, defaulting to 0. Your kernel may not have been built with NUMA support.
2022-09-06 02:35:38.603133: I tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.cc:271] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 0 MB memory) -> physical PluggableDevice (device: 0, name: METAL, pci bus id: <undefined>)
2022-09-06 02:35:38.659578: W tensorflow/core/platform/profile_utils/cpu_utils.cc:128] Failed to get CPU frequency: 0 Hz
2022-09-06 02:35:38.684142: I tensorflow/core/grappler/optimizers/custom_graph_optimizer_registry.cc:113] Plugin optimizer for device_type GPU is enabled.

大多数日志都是无关紧要的,我可以通过以下方式抑制一些日志:

import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

但是我不能禁用这些日志信息:

>>>  python script.py
Metal device set to: Apple M1 Max

systemMemory: 32.00 GB
maxCacheSize: 10.67 GB

有没有办法让keras和tensorflow-metal静音?我不想要这些日志。

我已经尝试过但没有成功:

import tensorflow as tf
tf.get_logger().setLevel('ERROR')
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
tf.autograph.set_verbosity(2)

import logging
logging.getLogger('tensorflow').setLevel(logging.ERROR)

注意:卸载 tensorflow-metal 似乎会抑制日志信息,但该解决方案并不是最佳解决方案,因为 tensorflow 将在 CPU 而不是 GPU 上运行...

>>> pip uninstall tensorflow-metal

我认为现在的日志记录是由于调用 keras Sequential():

from keras.models import Sequential

model = Sequential()

【问题讨论】:

  • 显然,您现在想要抑制的不是日志消息,因为它们前面没有任何上下文信息(日期和严重性),就像您显示的其他输出行一样。因此,它们很可能只是 print() 直接转到 stdout 的语句。我不熟悉有问题的包,所以我不知道它是否可以提供抑制该信息的方法,但调整日志记录系统无济于事。您可以使用 python script.py &gt; /dev/null. 抑制所有输出,但我猜这比您想要的更蛮力。
  • 哦哦我明白了!!我会尝试找到一种方法来抑制来自 tensorflow/keras 的打印!谢谢 :)
  • 消息?寻找相同的修复

标签: python tensorflow keras apple-m1


【解决方案1】:

tensorflow-metal 包在文件描述符级别写入标准输出。使用取自here 的代码,我终于成功地让 Metal 永远沉默了。只需将此代码添加到您的脚本中,然后相应地打开 TF 设备

import os
import sys
from contextlib import contextmanager

def fileno(file_or_fd):
    fd = getattr(file_or_fd, 'fileno', lambda: file_or_fd)()
    if not isinstance(fd, int):
        raise ValueError("Expected a file (`.fileno()`) or a file descriptor")
    return fd

@contextmanager
def stdout_redirected(to=os.devnull, stdout=None):
    stdout = sys.stdout if stdout is not None else None
    stdout_fd = fileno(stdout)
    # copy stdout_fd before it is overwritten
    #NOTE: `copied` is inheritable on Windows when duplicating a standard stream
    with os.fdopen(os.dup(stdout_fd), 'wb') as copied:
        stdout.flush()  # flush library buffers that dup2 knows nothing about
        try:
            os.dup2(fileno(to), stdout_fd)  # $ exec >&to
        except ValueError:  # filename
            with open(to, 'wb') as to_file:
                os.dup2(to_file.fileno(), stdout_fd)  # $ exec > to
        try:
            yield stdout # allow code to be run with the redirected stdout
        finally:
            # restore stdout to its previous value
            #NOTE: dup2 makes stdout_fd inheritable unconditionally
            stdout.flush()
            os.dup2(copied.fileno(), stdout_fd)  # $ exec >&copied

将以上内容添加到您的脚本中,然后在您进行设备放置时:


with tf.device('/device:GPU:0') as f, stdout_redirected(f):
    # Run stuff on the GPU

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 2021-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-16
    相关资源
    最近更新 更多