【问题标题】:Is there a way to prevent ray.init() from hanging when using Python on Apple silicon (the M1 Max)?在 Apple 芯片(M1 Max)上使用 Python 时,有没有办法防止 ray.init() 挂起?
【发布时间】:2022-12-10 04:00:04
【问题描述】:

因此,我尝试在 Apple 芯片(M1 Max)上的 Jupyter 笔记本(在 Miniforge 虚拟环境中)中运行 ray[rllib]。尽管我可以import ray正常进入笔记本,但紧接着的下一步(运行ray.init())导致笔记本挂起。不返回任何错误--ray.init() 永远不会完成。有解决办法吗?

这是我第一次使用 Ray。我不认为笔记本或我输入的命令是问题,因为笔记本是教师预制的,我已经设法让一个相同的笔记本在 Windows 10 的 Miniforge 环境中正常运行。

我听从了 Ray M1 Mac (Apple Silicon) Support 开发人员的建议,安装了 Miniforge for the M1 并创建了一个虚拟环境。我还利用这个线程 What is the proper way to install TensorFlow on Apple M1 in 2022 设计了一个策略来安装强化学习应用程序所需的应用程序。以下是我用来设置 Miniforge 虚拟环境的 environment.yml 文件的内容:

name: tf-metal
channels:
  - apple
  - conda-forge
dependencies:
  - python=3.9
  - gym-all=0.21.0
  - pip
  - tensorflow-deps

  ## uncommented for use with Jupyter
  - ipykernel

  ## PyPI packages
  - pip:
    - jupyterlab
    - ray[rllib]==1.11
    - tensorflow-macos
    - tensorflow-metal

我在终端中用于创建虚拟环境的步骤如下:

# Download Miniforge3-MacOSX-arm64.sh and make it executable:
chmod u+x ./Miniforge3-MacOSX-arm64.sh

# run Miniforge
./Miniforge3-MacOSX-arm64.sh
# (or update it) ./Miniforge3-MacOSX-arm64.sh -u

# accept terms and conditions...
# run 'conda init' by entering 'yes'
# configure conda (then close and reopen Terminal):
conda config --set auto_activate_base false
# confirm '~/.bash_profile' reflects miniforge settings
# good-to-go...

# set up virtual environment
conda create --name rl_course2  # (choose any name you want)
# confirm acceptability of location (enter 'yes')
# activate env:
conda activate rl_course2
# configure channels (settings recommended by an instructor)
conda config --env --add channels conda-forge
conda config --env --set channel_priority strict
# install dependencies using environment.yml file shown above:
conda env update --name rl_course2 --file '/Users/.../environment.yml'
# check output for errors...(none found via text search)

因此,据我所知,我创建了虚拟环境并安装了所有依赖项,没有出现任何错误:

Successfully installed MarkupSafe-2.1.1 PyWavelets-1.4.1 Send2Trash-1.8.0 absl-py-1.3.0 anyio-3.6.2 argon2-cffi-21.3.0 argon2-cffi-bindings-21.2.0 astunparse-1.6.3 async-timeout-4.0.2 attrs-22.1.0 babel-2.11.0 beautifulsoup4-4.11.1 bleach-5.0.1 cachetools-5.2.0 certifi-2022.9.24 cffi-1.15.1 charset-normalizer-2.1.1 click-8.1.3 contourpy-1.0.6 cycler-0.11.0 defusedxml-0.7.1 dm-tree-0.1.7 fastjsonschema-2.16.2 filelock-3.8.0 flatbuffers-22.10.26 fonttools-4.38.0 gast-0.4.0 google-auth-2.14.1 google-auth-oauthlib-0.4.6 google-pasta-0.2.0 grpcio-1.43.0 idna-3.4 imageio-2.22.4 importlib-metadata-5.0.0 ipython-genutils-0.2.0 jinja2-3.1.2 json5-0.9.10 jsonschema-4.17.1 jupyter-server-1.23.3 jupyterlab-3.5.0 jupyterlab-pygments-0.2.2 jupyterlab-server-2.16.3 keras-2.10.0 keras-preprocessing-1.1.2 kiwisolver-1.4.4 libclang-14.0.6 markdown-3.4.1 matplotlib-3.6.2 mistune-2.0.4 msgpack-1.0.4 nbclassic-0.4.8 nbclient-0.7.0 nbconvert-7.2.5 nbformat-5.7.0 networkx-2.8.8 notebook-6.5.2 notebook-shim-0.2.2 oauthlib-3.2.2 opt-einsum-3.3.0 pandas-1.5.1 pandocfilters-1.5.0 pillow-9.3.0 prometheus-client-0.15.0 pyasn1-0.4.8 pyasn1-modules-0.2.8 pycparser-2.21 pyrsistent-0.19.2 pytz-2022.6 pyyaml-6.0 ray-1.11.0 redis-4.3.5 requests-2.28.1 requests-oauthlib-1.3.1 rsa-4.9 scikit-image-0.19.3 sniffio-1.3.0 soupsieve-2.3.2.post1 tabulate-0.9.0 tensorboard-2.10.1 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.1 tensorboardX-2.5.1 tensorflow-estimator-2.10.0 tensorflow-macos-2.10.0 tensorflow-metal-0.6.0 termcolor-2.1.1 terminado-0.17.0 tifffile-2022.10.10 tinycss2-1.2.1 tomli-2.0.1 typing-extensions-4.4.0 urllib3-1.26.12 webencodings-0.5.1 websocket-client-1.4.2 werkzeug-2.2.2 wrapt-1.14.1 zipp-3.10.0

最后一步(在 rl_course2 环境中工作时)使用终端:启动 Jupyter...

(rl_course2) MacBook-Pro ~$ jupyter notebook

现在,在 Jupyter/Python 笔记本(Chrome 浏览器)中:

import ray   # works!
ray.init()   # never completes (no errors)!

因此,我使用终端(无笔记本)在同一环境中尝试了类似的步骤:

(rl_course2) MacBook-Pro ~$ python3
Python 3.9.15 | packaged by conda-forge | (main, Nov 22 2022, 08:48:25) 
[Clang 14.0.6 ] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> import ray
>>> ray.init()
[no errors, but never completes]

有没有办法解决这个问题并在我的 Jupyter 环境中正常运行 Ray?

更新 1:刚才,我能够使用上面讨论的虚拟环境运行 Apple 推荐的简单 TensorFlow 测试脚本(参见Get started with tensorflow-metal),并且在 M1 Max 上大约两分钟内完成了五个训练周期,没有错误64 GB 内存,所以环境看起来运行良好。也许这个问题涉及雷?

【问题讨论】:

    标签: python tensorflow apple-m1 ray mini-forge


    【解决方案1】:

    我按照上面的步骤在我的 Mac M1 中运行了 environment.yml 文件并且 以下问题消失:

    错误:找不到满足张量流要求的版本(来自版本:无) 错误:找不到与 tensorflow 匹配的分布

    它似乎工作正常!

    谢谢!

    【讨论】:

    • 出色的。很高兴提供的步骤对您有所帮助。如果他们这样做了,那么对有帮助的答案进行投票并使用该答案的“添加评论”功能而不是创建新答案在其下输入您的评论可能会稍微好一些。这将有助于其他人理解您的言论的背景。谢谢!
    【解决方案2】:

    我已经找到了我的问题的几个可能答案之一。稍微更改 environment.yml 文件(如上所述)以导入 ray[rllib] 而不是 ray[rllib]==1.11 使 Jupyter 笔记本能够正常运行 ray.init() 并执行笔记本中的其余代码。 ray[rllib] 版本 1.11 中似乎存在一个错误,在某些情况下阻止 ray.init() 在 M1 Max 上运行。

    总结一下:为了克服 Apple Silicon (M1 Max) 上涉及 ray.init() 的挂起,我能够通过将 environment.yml 文件修改为以下内容来解决它:

    name: tf-metal
    channels:
      - apple
      - conda-forge
    dependencies:
      - python=3.9
      - gym-all=0.21.0
      - pip
      - tensorflow-deps
    
      ## uncommented for use with Jupyter
      - ipykernel
    
      ## PyPI packages
      - pip:
        - jupyterlab
        - ray[rllib]
        - tensorflow-macos
        - tensorflow-metal
    

    我随后使用上述过程创建了一个 Miniforge 环境。笔记本自动安装了Python 3.9.15版本和Ray 2.1.0版本,笔记本在M1 Max上运行正常。

    更新(12/09/2022):我最近从社区的另一个来源了解到,以下environment.yml对于安装稳定版本的Ray(1.11.0)与Gym(0.21.0)和TensorFlow(2.10)也有效.0) 在 Apple Silicon 上使用上述 Miniforge 环境。使用此 enviornment.yml 将省略 tensorflow-metal,因此您的 GPU 可能不会明确使用,但 Ray 可能会在 Gym 环境和 TensorFlow 中顺利工作。

    dependencies:
      - python=3.9
      - gym-all=0.21.0
      - grpcio=1.43.0
      - pip
      - pip:
          - jupyterlab
          - ray[rllib]==1.11
          - tensorflow-macos==2.10.0
    

    【讨论】:

      猜你喜欢
      • 2022-08-16
      • 1970-01-01
      • 2022-07-19
      • 2021-08-04
      • 2021-08-16
      • 2021-07-24
      • 1970-01-01
      • 2021-05-11
      • 2022-11-10
      相关资源
      最近更新 更多