Python中的分布式框架Ray的安装与使用
技术背景
假设我们在一个局域网内有多台工作站(不是服务器),那么有没有一个简单的方案可以实现一个小集群,提交分布式的任务呢?Ray为我们提供了一个很好的解决方案,允许你通过conda和Python灵活的构建集群环境,并提交分布式的任务。其基本架构为:
那么本文简单的介绍一下Ray的安装与基本使用。
安装
由于是一个Python的框架,Ray可以直接使用pip进行安装和管理:
$ python3 -m pip install ray[default]
但是需要注意的是,在所有需要构建集群的设备上,需要统一Python和Ray的版本,因此建议先使用conda创建同样的虚拟环境之后,再安装统一版本的ray。否则在添加集群节点的时候就有可能出现如下问题:
RuntimeError: Version mismatch: The cluster was started with:
Ray: 2.7.2
Python: 3.7.13
This process on node 172.17.0.2 was started with:
Ray: 2.7.2
Python: 3.7.5
启动和连接服务
一般在配置集群的时候可以先配置下密钥登陆:
$ ssh-keygen -t rsa
$ ssh-copy-id user_name@ip_address
就这么两步,就可以配置远程服务器ssh免密登陆(配置的过程中有可能需要输入一次密码)。然后在主节点(配置一个master节点)启动ray服务:
$ ray start --head --dashboard-host='0.0.0.0' --dashboard-port=8265
Usage stats collection is enabled. To disable this, add `--disable-usage-stats` to the command that starts the cluster, or run the following command: `ray disable-usage-stats` before starting the cluster. See https://docs.ray.io/en/master/cluster/usage-stats.html for more details.
Local node IP: xxx.xxx.xxx.xxx
--------------------
Ray runtime started.
--------------------
Next steps
To add another node to this Ray cluster, run
ray start --address='xxx.xxx.xxx.xxx:6379'
To connect to this Ray cluster:
import ray
ray.init()
To submit a Ray job using the Ray Jobs CLI:
RAY_ADDRESS='http://xxx.xxx.xxx.xxx:8265' ray job submit --working-dir . -- python my_script.py
See https://docs.ray.io/en/latest/cluster/running-applications/job-submission/index.html
for more information on submitting Ray jobs to the Ray cluster.
To terminate the Ray runtime, run
ray stop
To view the status of the cluster, use
ray status
To monitor and debug Ray, view the dashboard at
xxx.xxx.xxx.xxx:8265
If connection to the dashboard fails, check your firewall settings and network configuration.
这就启动完成了,并给你指示了下一步的操作,例如在另一个节点上配置添加到集群中,可以使用指令:
$ ray start --address='xxx.xxx.xxx.xxx:6379'
但是前面提到了,这里要求Python和Ray版本要一致,如果版本不一致就会出现这样的报错:
RuntimeError: Version mismatch: The cluster was started with:
Ray: 2.7.2
Python: 3.7.13
This process on node 172.17.0.2 was started with:
Ray: 2.7.2
Python: 3.7.5
到这里其实Ray集群就已经部署完成了,非常的简单方便。
基础使用
我们先用一个最简单的案例来测试一下:
# test_ray.py
import os
import ray
ray.init()
print('''This cluster consists of
{} nodes in total
{} CPU resources in total
'''.format(len(ray.nodes()), ray.cluster_resources()['CPU']))
这个Python脚本打印了远程节点的计算资源,那么我们可以用这样的方式去提交一个本地的job:
$ RAY_ADDRESS='http://xxx.xxx.xxx.xxx:8265' ray job submit --working-dir . -- python test_ray.py
Job submission server address: http://xxx.xxx.xxx.xxx:8265
2024-08-27 07:35:10,751 INFO dashboard_sdk.py:338 -- Uploading package gcs://_ray_pkg_4b79155b5de665ce.zip.
2024-08-27 07:35:10,751 INFO packaging.py:518 -- Creating a file package for local directory '.'.
-------------------------------------------------------
Job 'raysubmit_7Uqy8LjP4dxjZxGa' submitted successfully
-------------------------------------------------------
Next steps
Query the logs of the job:
ray job logs raysubmit_7Uqy8LjP4dxjZxGa
Query the status of the job:
ray job status raysubmit_7Uqy8LjP4dxjZxGa
Request the job to be stopped:
ray job stop raysubmit_7Uqy8LjP4dxjZxGa
Tailing logs until the job exits (disable with --no-wait):
2024-08-27 15:35:14,079 INFO worker.py:1330 -- Using address xxx.xxx.xxx.xxx:6379 set in the environment variable RAY_ADDRESS
2024-08-27 15:35:14,079 INFO worker.py:1458 -- Connecting to existing Ray cluster at address: xxx.xxx.xxx.xxx:6379...
2024-08-27 15:35:14,103 INFO worker.py:1639 -- Connected to Ray cluster. View the dashboard at http://xxx.xxx.xxx.xxx:8265
This cluster consists of
1 nodes in total
48.0 CPU resources in total
------------------------------------------
Job 'raysubmit_7Uqy8LjP4dxjZxGa' succeeded
------------------------------------------
这里的信息说明,远程的集群只有一个节点,该节点上有48个可用的CPU核资源。这些输出信息不仅可以在终端窗口上看到,还可以从这里给出的dashboard链接里面看到更加详细的任务管理情况:
这里也顺便提交一个输出软件位置信息的指令,确认下任务是在远程执行而不是在本地执行:
import ray
ray.init()
import numpy as np
print (np.__file__)
返回的日志为:
$ RAY_ADDRESS='http://xxx.xxx.xxx.xxx:8265' ray job submit --working-dir . -- python test_ray.py
Job submission server address: http://xxx.xxx.xxx.xxx:8265
2024-08-27 07:46:10,645 INFO dashboard_sdk.py:338 -- Uploading package gcs://_ray_pkg_5bba1a7144beb522.zip.
2024-08-27 07:46:10,658 INFO packaging.py:518 -- Creating a file package for local directory '.'.
-------------------------------------------------------
Job 'raysubmit_kQ3XgE4Hxp3dkmuU' submitted successfully
-------------------------------------------------------
Next steps
Query the logs of the job:
ray job logs raysubmit_kQ3XgE4Hxp3dkmuU
Query the status of the job:
ray job status raysubmit_kQ3XgE4Hxp3dkmuU
Request the job to be stopped:
ray job stop raysubmit_kQ3XgE4Hxp3dkmuU
Tailing logs until the job exits (disable with --no-wait):
2024-08-27 15:46:12,456 INFO worker.py:1330 -- Using address xxx.xxx.xxx.xxx:6379 set in the environment variable RAY_ADDRESS
2024-08-27 15:46:12,457 INFO worker.py:1458 -- Connecting to existing Ray cluster at address: xxx.xxx.xxx.xxx:6379...
2024-08-27 15:46:12,470 INFO worker.py:1639 -- Connected to Ray cluster. View the dashboard at http://xxx.xxx.xxx.xxx:8265
/home/dechin/anaconda3/envs/mindspore-latest/lib/python3.7/site-packages/numpy/__init__.py
------------------------------------------
Job 'raysubmit_kQ3XgE4Hxp3dkmuU' succeeded
------------------------------------------
$ python3 -m pip show numpy
Name: numpy
Version: 1.21.6
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email:
License: BSD
Location: /usr/local/python-3.7.5/lib/python3.7/site-packages
Requires:
Required-by: CyFES, h5py, hadder, matplotlib, mindinsight, mindspore, mindspore-serving, pandas, ray, scikit-learn, scipy
这里可以看到,提交的任务中numpy是保存在mindspore-latest虚拟环境中的,而本地的numpy不在虚拟环境中,说明任务确实是在远程执行的。类似的可以在dashboard上面看到提交日志:
接下来测试一下分布式框架ray的并发特性:
import ray
ray.init()
@ray.remote(num_returns=1)
def cpu_task():
import time
time.sleep(2)
import numpy as np
nums = 100000
arr = np.random.random((2, nums))
arr2 = arr[1]**2 + arr[0]**2
pi = np.where(arr2<=1, 1, 0).sum() * 4 / nums
return pi
num_conc = 10
res = ray.get([cpu_task.remote() for _ in range(num_conc)])
print (sum(res) / num_conc)
这个案例的内容是用蒙特卡洛算法计算圆周率的值,一次提交10个任务,每个任务中撒点100000个,并休眠2s。那么如果是顺序执行的话,理论上需要休眠20s。而这里提交任务之后,输出如下:
$ time RAY_ADDRESS='http://xxx.xxx.xxx.xxx:8265' ray job submit --working-dir . --entrypoint-num-cpus 10 -- python te
st_ray.py
Job submission server address: http://xxx.xxx.xxx.xxx:8265
2024-08-27 08:30:13,315 INFO dashboard_sdk.py:385 -- Package gcs://_ray_pkg_d66b052eb6944465.zip already exists, skipping upload.
-------------------------------------------------------
Job 'raysubmit_Ur6MAvP7DYiCT6Uz' submitted successfully
-------------------------------------------------------
Next steps
Query the logs of the job:
ray job logs raysubmit_Ur6MAvP7DYiCT6Uz
Query the status of the job:
ray job status raysubmit_Ur6MAvP7DYiCT6Uz
Request the job to be stopped:
ray job stop raysubmit_Ur6MAvP7DYiCT6Uz
Tailing logs until the job exits (disable with --no-wait):
2024-08-27 16:30:15,032 INFO worker.py:1330 -- Using address xxx.xxx.xxx.xxx:6379 set in the environment variable RAY_ADDRESS
2024-08-27 16:30:15,033 INFO worker.py:1458 -- Connecting to existing Ray cluster at address: xxx.xxx.xxx.xxx:6379...
2024-08-27 16:30:15,058 INFO worker.py:1639 -- Connected to Ray cluster. View the dashboard at http://xxx.xxx.xxx.xxx:8265
3.141656
------------------------------------------
Job 'raysubmit_Ur6MAvP7DYiCT6Uz' succeeded
------------------------------------------
real 0m7.656s
user 0m0.414s
sys 0m0.010s
总的运行时间在7.656秒,其中5s左右的时间是来自网络delay。所以实际上并发之后的总运行时间就在2s左右,跟单任务休眠的时间差不多。也就是说,远程提交的任务确实是并发执行的。最终返回的结果进行加和处理,得到的圆周率估计为:3.141656。而且除了普通的CPU任务之外,还可以上传GPU任务:
import ray
ray.init()
@ray.remote(num_returns=1, num_gpus=1)
def test_ms():
import os
os.environ['GLOG_v']='4'
os.environ['CUDA_VISIBLE_DEVICE']='0'
import mindspore as ms
ms.set_context(device_target="GPU", device_id=0)
a = ms.Tensor([1, 2, 3], ms.float32)
return a.asnumpy().sum()
res = ray.get(test_ms.remote())
ray.shutdown()
print (res)
这个任务是用mindspore简单创建了一个Tensor,并计算了Tensor的总和返回给本地,输出内容为:
$ RAY_ADDRESS='http://xxx.xxx.xxx.xxx:8265' ray job submit --working-dir . --entrypoint-num-gpus 1 -- python test_ray.py
Job submission server address: http://xxx.xxx.xxx.xxx:8265
2024-08-28 01:16:38,712 INFO dashboard_sdk.py:338 -- Uploading package gcs://_ray_pkg_10019cd9fa9bdc38.zip.
2024-08-28 01:16:38,712 INFO packaging.py:518 -- Creating a file package for local directory '.'.
-------------------------------------------------------
Job 'raysubmit_RUvkEqnkjNitKmnJ' submitted successfully
-------------------------------------------------------
Next steps
Query the logs of the job:
ray job logs raysubmit_RUvkEqnkjNitKmnJ
Query the status of the job:
ray job status raysubmit_RUvkEqnkjNitKmnJ
Request the job to be stopped:
ray job stop raysubmit_RUvkEqnkjNitKmnJ
Tailing logs until the job exits (disable with --no-wait):
2024-08-28 09:16:41,960 INFO worker.py:1330 -- Using address xxx.xxx.xxx.xxx:6379 set in the environment variable RAY_ADDRESS
2024-08-28 09:16:41,960 INFO worker.py:1458 -- Connecting to existing Ray cluster at address: xxx.xxx.xxx.xxx:6379...
2024-08-28 09:16:41,974 INFO worker.py:1639 -- Connected to Ray cluster. View the dashboard at http://xxx.xxx.xxx.xxx:8265
6.0
------------------------------------------
Job 'raysubmit_RUvkEqnkjNitKmnJ' succeeded
------------------------------------------
返回的计算结果是6.0,那么也是正确的。
查看和管理任务
前面的任务输出信息中,都有相应的job_id,我们可以根据这个job_id在主节点上面查看相关任务的执行情况:
$ ray job status raysubmit_RUvkEqnkjNitKmnJ
可以查看该任务的输出内容:
$ ray job logs raysubmit_RUvkEqnkjNitKmnJ
还可以终止该任务的运行:
$ ray job stop raysubmit_RUvkEqnkjNitKmnJ
总结概要
本文介绍了基于Python的分布式框架Ray的基本安装与使用。Ray框架下不仅可以通过conda和Python十分方便的构建一个集群,还可以自动的对分布式任务进行并发处理,且支持GPU分布式任务的提交,极大的简化了手动分布式开发的工作量。
版权声明
本文首发链接为:https://www.cnblogs.com/dechinphy/p/ray.html
作者ID:DechinPhy
更多原著文章:https://www.cnblogs.com/dechinphy/
请博主喝咖啡:https://www.cnblogs.com/dechinphy/gallery/image/379634.html
Python中的分布式框架Ray的安装与使用的更多相关文章
- 学界| UC Berkeley提出新型分布式框架Ray:实时动态学习的开端—— AI 应用的系统需求:支持(a)异质、并行计算,(b)动态任务图,(c)高吞吐量和低延迟的调度,以及(d)透明的容错性。
学界| UC Berkeley提出新型分布式框架Ray:实时动态学习的开端 from:https://baijia.baidu.com/s?id=1587367874517247282&wfr ...
- 取代 Python 多进程!伯克利开源分布式框架 Ray
Ray 由伯克利开源,是一个用于并行计算和分布式 Python 开发的开源项目.本文将介绍如何使用 Ray 轻松构建可从笔记本电脑扩展到大型集群的应用程序. 并行和分布式计算是现代应用程序的主要内容. ...
- python三大主流web框架之Django安装、项目搭建
这一篇我们将迎来python强大的web框架Django,相信大家都已经不陌生,本篇将介绍Django的安装及基础项目搭建,大神略过~ Django是需要我们手动pip安装的,首先我们来安装Djang ...
- Python中的Django框架中prefetch_related()函数对数据库查询的优化
实例的背景说明 假定一个个人信息系统,需要记录系统中各个人的故乡.居住地.以及到过的城市.数据库设计如下: Models.py 内容如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 1 ...
- Python 中 unittest 单元测试框架中需要知识点
现在正在使用 unittest 框架,我们来记录下这个框架的知识点: unittest 框架:我们在写接口用例的时候,会继承 unittest 当中的 TestCase 的类和方法,私有方法除外,来识 ...
- python中pygame模块的Linux下安装过程
一.使用pip安装Python包 大多数较新的Python版本都自带pip,因此首先可检查系统是否已经安装了pip.在Python3中,pip有时被称为pip3. 1.在Linux和OS X系统中检查 ...
- python中模块制作、发布、安装
模块的发布 需要在当前目录下 模块的安装 真实制作发布一个包 包的制作 (1)将写好的包放在moudelTest目录下 (2)moudelTest目录下创建一个setup.py文件(格式上面有介绍) ...
- python中面向对象知识框架
案列: 1 class Chinese: # 类的创建,类名首字母要大写 2 eye = 'black' # 类属性的创建 3 4 def __init__(self,hometown): # 类的初 ...
- python三大web框架Django,Flask,Flask,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架
Python几种主流框架 从GitHub中整理出的15个最受欢迎的Python开源框架.这些框架包括事件I/O,OLAP,Web开发,高性能网络通信,测试,爬虫等. Django: Python We ...
- Django,Flask,Tornado三大框架对比,Python几种主流框架,13个Python web框架比较,2018年Python web五大主流框架
Django 与 Tornado 各自的优缺点Django优点: 大和全(重量级框架)自带orm,template,view 需要的功能也可以去找第三方的app注重高效开发全自动化的管理后台(只需要使 ...
随机推荐
- ReST,以及RESTful的 简单介绍
什么是 ReST 阮一峰说的比较清楚,具体见他的博客文章. 二.名称 ReST这个词,是[Roy Thomas Fielding](http://en.wikipedia.org/wiki/Roy_F ...
- Nuxt3 的生命周期和钩子函数(四)
title: Nuxt3 的生命周期和钩子函数(四) date: 2024/6/28 updated: 2024/6/28 author: cmdragon excerpt: 概述了Nuxt3的六个关 ...
- LLM并行训练3-数据并行
前置知识 混合精度训练 在参数存储时采取fp32, 开始进行fp/bp时转成fp16运算, 拿到fp16梯度后再转回fp32更新参数. ZeRO对显存占用的估算: 模型状态: Weights(fp16 ...
- python基础-字典dict {key:value }
字典的定义和操作 字典的特性: 元素数量 支持多个 元素类型 key :value key:除字典外的任何类型 Value:任何类型 下标索引 不支持 重复元素 key不支持 可修改性 支持 数据有序 ...
- 【排行榜】Carla leaderboard 排行榜 运行与参与手把手教学
此分支主要供参与leaderboard排名使用,介绍如何构建队伍,提交自己代码,此部分较为简单,主要是基本教学与演示:后续可以参考更多的开源代码进行学习等. 基本参与此榜单的大多都是学校和实验室,还是 ...
- ubuntu22 python2 pyinstaller 打包报错:'NoneType' object has no attribute 'groups'
前言 最近有个需求,需要在 ubnutu22 上使用 pyinstaller 打包一个python2 的文件. 中间遇到了一些问题: pip2 install pyinstaller 报错 解决方案: ...
- API网关实践-网易云轻舟微服务
微服务最佳实践中,我们需要通过统一的 API 网关进行服务能力的共享,API 网关为用户提供发布.管理.保护和监控 API的能力,帮助用户在自己的多个系统之间,或者内部系统与合作伙伴以及第三方的系统之 ...
- 用这开源小书学 Docker,香!
> 最新.全面.通俗.可多端阅读的 Docker 教程小书.>> 编程导航开源仓库:https://github.com/liyupi/code-navDocker 可以说是一个改变 ...
- oeasy教您玩转vim - 64- # 参数argument
参数argument 回忆上次 上次了解了 窗口 window 窗口是用来装缓冲buffer的 buffer是在内存里面加载的硬盘文件 窗口的切分 :sp[lit] 水平切分 :vsp[lit] ...
- 【2024最新】4000字搞懂sora!一张脑图贯穿!
话不多说,上图! 下面就是对sora的具体阐释: Sora是OpenAI推出的一款革命性的视频生成模型,能够根据文本指令.静态图像或视频生成长达60秒的完整视频.这一模型基于扩散式模型和自注意力深度学 ...