简体   繁体   English

在 Apple 芯片(M1 Max)上使用 Python 时,有没有办法防止 ray.init() 挂起?

[英]Is there a way to prevent ray.init() from hanging when using Python on Apple silicon (the M1 Max)?

So I am trying to run ray[rllib] in a Jupyter notebook (in a Miniforge virtual environment) on Apple silicon (the M1 Max).因此,我尝试在 Apple 芯片(M1 Max)上的 Jupyter 笔记本(在 Miniforge 虚拟环境中)中运行 ray[rllib]。 Although I can import ray normally into the notebook, the very next step (of running ray.init() ) causes the notebook to hang.尽管我可以import ray正常导入到笔记本中,但紧接着的下一步(运行ray.init() )导致笔记本挂起。 No error is returned-- ray.init() never completes.不返回任何错误ray.init()永远不会完成。 Is there a fix for this?有解决办法吗?

This is my first time using Ray.这是我第一次使用 Ray。 I don't think the notebook or the commands I am entering is the issue because the notebook came pre-made from an instructor, and I have managed to get an identical notebook to run normally in a Miniforge environment on Windows 10.我不认为笔记本或我输入的命令是问题,因为笔记本是教师预制的,我已经设法让一个相同的笔记本在 Windows 10 的 Miniforge 环境中正常运行。

I followed advice from developers at Ray M1 Mac (Apple Silicon) Support to install Miniforge for the M1 and create a virtual environment.我听从了 Ray M1 Mac (Apple Silicon) Support开发人员的建议,为 M1 安装了 Miniforge并创建了一个虚拟环境。 I also leveraged this thread What is the proper way to install TensorFlow on Apple M1 in 2022 to devise a strategy for installing applications I need for a reinforcement learning application.我还利用了这个线程What is the proper way to install TensorFlow on Apple M1 in 2022 to devise 一种安装强化学习应用程序所需的应用程序的策略。 Here are the contents of an environment.yml file I used to set up the Miniforge virtual environment:以下是我用来设置 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

The steps I used in Terminal for creating the virtual environment were these:我在终端中用于创建虚拟环境的步骤如下:

# 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)

So I created the virtual environment and installed all the dependencies with no errors, as far as I could tell:所以我创建了虚拟环境并安装了所有依赖项,没有错误,据我所知:

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

Last step (while working in the rl_course2 environment) using Terminal: launch Jupyter...最后一步(在rl_course2环境中工作时)使用终端:启动 Jupyter...

(rl_course2) MacBook-Pro ~$ jupyter notebook

Now, in the Jupyter/Python notebook (Chrome browser):现在,在 Jupyter/Python 笔记本(Chrome 浏览器)中:

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

So I tried similar steps in the same environment using Terminal (no notebook):因此,我使用终端(无笔记本)在同一环境中尝试了类似的步骤:

(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]

Is there a way to fix this and run Ray normally in my Jupyter environment?有没有办法解决这个问题并在我的 Jupyter 环境中正常运行 Ray?

Update 1: Just now, I was able to run the simple TensorFlow test script recommended by Apple (see Get started with tensorflow-metal ) using the virtual environment discussed above, and five epochs of training completed with no errors in about two minutes on an M1 Max with 64 GB of memory, so the environment appears to be working fine.更新 1:刚才,我能够使用上面讨论的虚拟环境运行 Apple 推荐的简单 TensorFlow 测试脚本(请参阅tensorflow-metal 入门),并且在大约两分钟内完成了五个训练周期,没有错误M1 Max 具有 64 GB 的 memory,因此环境似乎运行良好。 Perhaps the issue involves Ray?也许这个问题涉及雷?

I have found one of possibly several answers to my question.我已经找到了我的问题的几个可能答案之一。 Changing the environment.yml file (described above) slightly to import ray[rllib] rather than ray[rllib]==1.11 enabled Jupyter notebook to run ray.init() normally and execute the remainder of the code in the notebook.稍微更改environment.yml文件(如上所述)以导入ray[rllib]而不是ray[rllib]==1.11使 Jupyter notebook 能够正常运行ray.init()并执行 notebook 中的其余代码。 It appears there was a bug in ray[rllib] version 1.11 that prevented ray.init() from running on the M1 Max under some circumstances. ray[rllib] 版本 1.11 中似乎存在一个错误,导致 ray.init() 在某些情况下无法在 M1 Max 上运行。

So to summarize: to overcome a hang involving ray.init() on Apple Silicon (M1 Max), I was able to solve it by modifying the environment.yml file to this:总结一下:为了克服 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

I subsequently created a Miniforge environment using the procedure described above.我随后使用上述过程创建了一个 Miniforge 环境。 Python version 3.9.15 and Ray version 2.1.0 were installed in the notebook automatically, and the notebook ran normally on the M1 Max.笔记本自动安装了Python 3.9.15版和Ray 2.1.0版,在M1 Max上运行正常。

Update (12/09/2022): I recently learned from another source in the community that the following environment.yml is also effective for installing a stable version of Ray (1.11.0) with Gym (0.21.0) and TensorFlow (2.10.0) on Apple Silicon using the Miniforge environment described above.更新(12/09/2022):我最近从社区的另一个来源了解到,以下environment.yml对于安装稳定版本的Ray(1.11.0)和Gym(0.21.0)和TensorFlow(2.10)也有效.0) 在 Apple Silicon 上使用上述 Miniforge 环境。 Using this enviornment.yml will omit tensorflow-metal so your GPUs may not be employed explicitly, however Ray will likely work smoothly with Gym environments and TensorFlow.使用此 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Apple Silicon M1 上的 Python Tensorflow 和 OpenCV - Python Tensorflow and OpenCV on Apple Silicon M1 python Apple 错误 TensorFlow on M1 (apple silicon) - python errors in Apple TensorFlow on M1 (apple silicon) Mac M1(Apple Silicon)上的 AWS SAM Python 调试 - AWS SAM Python debug on Mac M1 (Apple Silicon) python 密码学未在 Apple Silicon M1 PRO 上安装 pkcs7 - python cryptography not installing pkcs7 on Apple Silicon M1 PRO 使用 docker 在苹果硅 M1 上构建错误 - Build Error on apple silicon M1 with docker 在具有 M1 芯片(基于 ARM 的 Apple Silicon)的 Mac 上安装早期版本的 Python(3.8 之前)失败 - Installation of earlier versions of Python (prior to 3.8) fails on Mac with M1 Chip (ARM based Apple Silicon) 使用 Apple Silicon M1 的 Docker 内部的 Discord.py 分段错误 - Discord.py segmentation fault inside Docker using Apple Silicon M1 如何在 Apple Silicon (ARM / M1) 上安装 SciPy - How to install SciPy on Apple Silicon (ARM / M1) 使用 python 请求库时 Apple Mac Book Pro M1 芯片分段错误 - Apple Mac Book Pro M1 Chip segmentation Error when using python requests library 无法在 Apple M1 芯片上安装 google-cloud-aiplatform - 在 grpcio 上失败 - Can't install google-cloud-aiplatform on Apple M1 silicon - Fails on grpcio
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM