最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python中的分布式框架Ray的安裝與使用教程

 更新時間:2024年08月28日 12:24:45   作者:DECHIN  
Ray框架下不僅可以通過conda和Python十分方便的構建一個集群,還可以自動的對分布式任務進行并發(fā)處理,且支持GPU分布式任務的提交,本文介紹了基于Python的分布式框架Ray的基本安裝與使用,感興趣的朋友一起看看吧

技術背景

假設我們在一個局域網內有多臺工作站(不是服務器),那么有沒有一個簡單的方案可以實現(xiàn)一個小集群,提交分布式的任務呢?Ray為我們提供了一個很好的解決方案,允許你通過conda和Python靈活的構建集群環(huán)境,并提交分布式的任務。其基本架構為:

那么本文簡單的介紹一下Ray的安裝與基本使用。

安裝

由于是一個Python的框架,Ray可以直接使用pip進行安裝和管理:

$ python3 -m pip install ray[default]

但是需要注意的是,在所有需要構建集群的設備上,需要統(tǒng)一Python和Ray的版本,因此建議先使用conda創(chuàng)建同樣的虛擬環(huán)境之后,再安裝統(tǒng)一版本的ray。否則在添加集群節(jié)點的時候就有可能出現(xiàn)如下問題:

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免密登陸(配置的過程中有可能需要輸入一次密碼)。然后在主節(jié)點(配置一個master節(jié)點)啟動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.

這就啟動完成了,并給你指示了下一步的操作,例如在另一個節(jié)點上配置添加到集群中,可以使用指令:

$ ray start --address='xxx.xxx.xxx.xxx:6379'

但是前面提到了,這里要求Python和Ray版本要一致,如果版本不一致就會出現(xiàn)這樣的報錯:

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腳本打印了遠程節(jié)點的計算資源,那么我們可以用這樣的方式去提交一個本地的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
------------------------------------------

這里的信息說明,遠程的集群只有一個節(jié)點,該節(jié)點上有48個可用的CPU核資源。這些輸出信息不僅可以在終端窗口上看到,還可以從這里給出的dashboard鏈接里面看到更加詳細的任務管理情況:

這里也順便提交一個輸出軟件位置信息的指令,確認下任務是在遠程執(zhí)行而不是在本地執(zhí)行:

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虛擬環(huán)境中的,而本地的numpy不在虛擬環(huán)境中,說明任務確實是在遠程執(zhí)行的。類似的可以在dashboard上面看到提交日志:

接下來測試一下分布式框架ray的并發(fā)特性:

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。那么如果是順序執(zhí)行的話,理論上需要休眠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。所以實際上并發(fā)之后的總運行時間就在2s左右,跟單任務休眠的時間差不多。也就是說,遠程提交的任務確實是并發(fā)執(zhí)行的。最終返回的結果進行加和處理,得到的圓周率估計為: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簡單創(chuàng)建了一個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,我們可以根據(jù)這個job_id在主節(jié)點上面查看相關任務的執(zhí)行情況:

$ ray job status raysubmit_RUvkEqnkjNitKmnJ

可以查看該任務的輸出內容:

$ ray job logs raysubmit_RUvkEqnkjNitKmnJ

還可以終止該任務的運行:

$ ray job stop raysubmit_RUvkEqnkjNitKmnJ

總結概要

本文介紹了基于Python的分布式框架Ray的基本安裝與使用。Ray框架下不僅可以通過conda和Python十分方便的構建一個集群,還可以自動的對分布式任務進行并發(fā)處理,且支持GPU分布式任務的提交,極大的簡化了手動分布式開發(fā)的工作量。

版權聲明

本文首發(fā)鏈接為: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的安裝與使用教程的文章就介紹到這了,更多相關Python 分布式框架Ray內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python實現(xiàn)高效迭代固定大小記錄的專業(yè)指南

    Python實現(xiàn)高效迭代固定大小記錄的專業(yè)指南

    在數(shù)據(jù)處理、文件解析和網絡編程中,我們經常需要處理由固定長度記錄組成的數(shù)據(jù)塊,本文將詳細介紹一下Python如何實現(xiàn)高效迭代固定大小記錄,有需要的小伙伴可以了解下
    2025-09-09
  • 淺談python中的getattr函數(shù) hasattr函數(shù)

    淺談python中的getattr函數(shù) hasattr函數(shù)

    下面小編就為大家?guī)硪黄獪\談python中的getattr函數(shù) hasattr函數(shù)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-06-06
  • 關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

    關于win10在tensorflow的安裝及在pycharm中運行步驟詳解

    這篇文章主要介紹了關于win10在tensorflow的安裝及在pycharm中運行的步驟詳解,本文通過圖文并茂的形式給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • 詳解python--模擬輪盤抽獎游戲

    詳解python--模擬輪盤抽獎游戲

    這篇文章主要介紹了python模擬輪盤抽獎游戲,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2019-04-04
  • Python識別html主要文本框過程解析

    Python識別html主要文本框過程解析

    這篇文章主要介紹了python識別html主要文本框過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-02-02
  • 布同 統(tǒng)計英文單詞的個數(shù)的python代碼

    布同 統(tǒng)計英文單詞的個數(shù)的python代碼

    最近需要翻譯英文文章,所以需要統(tǒng)計單詞個數(shù)。索性寫了一段代碼在此,可以簡單的統(tǒng)計單詞的個數(shù)
    2011-03-03
  • 一文帶你解鎖Python文件匹配技巧

    一文帶你解鎖Python文件匹配技巧

    在日常的文件操作和數(shù)據(jù)處理中,文件匹配是一個非常常見的任務,本文將詳細介紹如何使用?Python?實現(xiàn)文件匹配,有需要的小伙伴可以參考下
    2024-12-12
  • 科學Python開發(fā)環(huán)境Spyder必知必會點

    科學Python開發(fā)環(huán)境Spyder必知必會點

    這篇文章主要為大家介紹了科學Python開發(fā)環(huán)境Spyder必知必會點及使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • Pytorch中view()函數(shù)的實現(xiàn)示例

    Pytorch中view()函數(shù)的實現(xiàn)示例

    view函數(shù)是一個極為重要的張量操作函數(shù),本文主要介紹了Pytorch中view()函數(shù)的實現(xiàn)示例,具有一定的參考價值,感興趣的可以了解一下
    2025-05-05
  • macos環(huán)境升級自己安裝的python3完整指南

    macos環(huán)境升級自己安裝的python3完整指南

    在MacOS系統(tǒng)上,保持Pip和Python版本的最新狀態(tài)對于順利進行Python開發(fā)至關重要,下面這篇文章主要介紹了macos環(huán)境升級自己安裝python3的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2026-04-04

最新評論

达日县| 宝清县| 阿拉善盟| 金乡县| 呼玛县| 手游| 延长县| 门源| 莲花县| 东源县| 丽江市| 宁强县| 长垣县| 海兴县| 元谋县| 平湖市| 桦川县| 沾益县| 温州市| 蓬溪县| 集贤县| 海城市| 黄梅县| 改则县| 安新县| 广饶县| 遂平县| 天柱县| 仪征市| 安义县| 富民县| 南川市| 三穗县| 淳安县| 峨眉山市| 洞口县| 廊坊市| 万州区| 合川市| 台南县| 潼关县|