【问题标题】:Update Tensorflow binary in virtual environment in PyCharm to use AVX2在 PyCharm 的虚拟环境中更新 Tensorflow 二进制文件以使用 AVX2
【发布时间】:2020-10-27 14:19:20
【问题描述】:

我的问题与here 有关,但我正在使用 PyCharm,并根据此guide,第 5 页,使用 Python 解释器设置我的虚拟环境。

当我运行我的 tensorflow 代码时,我收到警告:

您的 CPU 支持此 TensorFlow 二进制文件不支持的指令 编译使用:AVX2

我可以忽略它,但由于我的模型拟合很慢,我想利用它。但是,我不知道如何在这个虚拟环境 PyCharm 设置中更新我的系统以使用 AVX2?

【问题讨论】:

  • 您也可以介绍一下操作系统,这样我就可以帮助构建或安装它。
  • 我使用的是 Windows 10 x64。
  • 您能描述一下您的硬件吗?您是否使用 GPU 进行训练?你的 CPU 怎么样?

标签: python tensorflow pycharm avx2


【解决方案1】:

Anaconda/conda 作为包管理工具:

假设你已经在你的机器上安装了 anaconda/conda,如果没有按照这个 - https://docs.anaconda.com/anaconda/install/windows/

conda create --name tensorflow_optimized python=3.7
conda activate tensorflow_optimized

# you need intel's tensorflow version that's optimized to use SSE4.1 SSE4.2 AVX AVX2 FMA
conda install tensorflow-mkl -c anaconda

#run this to check if the installed version is using MKL, 
#which in turns uses all the optimizations that your system provide. 
python -c "import tensorflow as tf; tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)"

# you should see something like this as the output.
2020-07-14 19:19:43.059486: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations:  SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags.

pip3 作为包管理工具:

py -m venv tensorflow_optimized
.\tensorflow_optimized\Scripts\activate

#once the env is activated, you need intel's tensorflow version 
#that's optimized to use SSE4.1 SSE4.2 AVX AVX2 FMA
pip install intel-tensorflow

#run this to check if the installed version is using MKL, 
#which in turns uses all the optimizations that your system provide. 
py -c "import tensorflow as tf; tf.test.is_gpu_available(cuda_only=False, min_cuda_compute_capability=None)"

# you should see something like this as the output.
2020-07-14 19:19:43.059486: I tensorflow/core/platform/cpu_feature_guard.cc:145] This TensorFlow binary is optimized with Intel(R) MKL-DNN to use the following CPU instructions in performance critical operations:  SSE4.1 SSE4.2 AVX AVX2 FMA
To enable them in non-MKL-DNN operations, rebuild TensorFlow with the appropriate compiler flags.

一旦你有了这个,你就可以在pycharm中设置使用这个环境了。

在此之前,运行 Windows 上的where python,Linux 和 Mac 上的which python 当环境被激活时,应该为您提供解释器的路径。在 Pycharm 中, 转到首选项->项目:您的项目名称->项目解释器->单击设置符号->单击添加。

选择系统解释器->点击...->这将打开一个弹出窗口,询问python解释器的位置。

在位置路径中,粘贴来自where python的路径->点击确定

现在您应该看到安装在该环境中的所有软件包。

从下一次开始,如果您想为您的项目选择该解释器,请单击右下半部分显示 python3/python2(您的解释器名称)并选择您需要的解释器。

我建议你安装 Anaconda 作为你的默认包管理器,因为它可以让你在 Windows 机器上的 Python 开发生活更轻松,但你也可以使用 pip。

【讨论】:

  • 感谢您的帖子,我有一些问题:python=3.5。我需要 Python 3.7x,所以最好是 3.7.8。这也适用于代码中的 python=3.7.8 吗?我需要能够参加 Google Tensorflow 认证考试。这确实安装了 PyCharm 插件。当我现在安装了 Conda 时,我不确定这是否仍然有效?我在哪里输入您发布的代码,所以在 PyCharm 中“conda create --name tensorflow_optimized python=3.5”?在哪里输入“where python”?
  • 此外:我已经用普通的 Python 3.7.8 设置了一个 venv,并安装了一些额外的包。当我现在按照您的指示进行操作时,这些软件包/设置会保留吗?还是我必须重新安装该 venv 中的所有软件包?
  • 是的,你可以用你需要的版本替换它,anaconda 会用那个 python 版本设置环境。 Pycharm 插件和包管理没有任何依赖关系,因此它也可以与新设置一起使用。如果您想使用您已经创建的 venv,请从 pycharm 的底部状态栏打开一个终端,您应该会看到您的 venv 名称以这样的开头(env_name),从 pip install 开始。它将保留已安装的软件包。
  • 可以在pycharm提供的终端中运行代码。只要确保你已经激活了你的 venv 或 conda env。如果 pycharm 设置了你的 venv,那么在你的终端中它应该被预先激活。
  • 当我尝试在 PyCharm 中运行 pip install intel-tensorflow 时出现错误:SyntaxError: invalid syntax?
【解决方案2】:

如果您在训练期间的 CPU 利用率大部分时间都保持在 100% 以下,您甚至不必费心获取不同的 TF 二进制文件。

根据您运行的工作负载,您可能看不到使用 AVX2(或 AVX512)的任何好处。

AVX2 是一组大小为 256(位)的 CPU 向量指令。与 128 位流指令相比,您最多可以获得 x2 倍的收益。当谈到深度学习模型时,它们受内存带宽的限制,并且不会从切换到更大的寄存器大小中看到太多(如果有的话)好处。简单的检查方法:查看训练期间 CPU 利用率保持 100% 的时间。如果大多数时候它低于 100%,那么您可能已经受内存(或其他方面)限制。如果您的训练在 GPU 上运行,而 CPU 仅用于数据预处理和偶尔的操作,那么好处就更不明显了。

回到回答你的问题。更新 TF 二进制文件以充分利用最新的 CPU 架构、CUDA 版本、python 版本等的最佳方式是build tensorflow from source。这可能需要你几个小时的时间。这将是解决您的问题的官方且最有效的方法。

如果您对使用更好的 CPU 指令感到满意,您可以尝试从任何可以找到它们的地方安装不同的 3-rd 方二进制文件。安装 Conda 并将 pycharm 解释器指向 conda 安装将是选项之一。

【讨论】:

  • 感谢您指出这一点。但是,我的问题基本上是如何从 PyCharm 的 venv 内部的源代码构建 tensorflow。那么如何在 PyCharm 中更新 venv 中的 TF 二进制文件呢?
猜你喜欢
  • 2019-12-26
  • 2018-12-28
  • 2021-11-24
  • 1970-01-01
  • 2018-03-23
  • 2021-01-18
  • 2013-08-28
  • 2022-10-08
  • 1970-01-01
相关资源
最近更新 更多