iT邦幫忙

2023 iThome 鐵人賽

DAY 23
0
AI & Data

MLOps/LLMOps - 從零開始系列 第 23

Day23 - 使用一個簡單範例說明 Run Local and Run with Ray Remote

  • 分享至 

  • xImage
  •  

我們會透過 Fibonnaci Sequence 來說明兩者的差異。老師,程式碼請了。

import os
import time
import ray

# Normal Python
def fibonacci_local(sequence_size):
    fibonacci = []
    for i in range(0, sequence_size):
        if i < 2:
            fibonacci.append(i)
            continue
        fibonacci.append(fibonacci[i-1]+fibonacci[i-2])
    return sequence_size

# Ray task
@ray.remote
def fibonacci_distributed(sequence_size):
    fibonacci = []
    for i in range(0, sequence_size):
        if i < 2:
            fibonacci.append(i)
            continue
        fibonacci.append(fibonacci[i-1]+fibonacci[i-2])
    return sequence_size

# Normal Python
def run_local(sequence_size):
    start_time = time.time()
    results = [fibonacci_local(sequence_size) for _ in range(os.cpu_count())]
    duration = time.time() - start_time
    print('Sequence size: {}, Local execution time: {}'.format(sequence_size, duration))

# Ray
def run_remote(sequence_size):
    # Starting Ray
    ray.init()
    start_time = time.time()
    results = ray.get([fibonacci_distributed.remote(sequence_size) for _ in range(os.cpu_count())])
    duration = time.time() - start_time
    print('Sequence size: {}, Remote execution time: {}'.format(sequence_size, duration))  


if __name__ == '__main__':
    print('CPU count: {}'.format(os.cpu_count()))
    run_local(100000)
    run_remote(100000)

說明一下:

  • 透過 ray init 開始透過 Ray API 來執行程式碼。
  • 透過 @ray.remote 這個 tag 將一個 function 變成一個 Ray task。
  • 透過 ray.get 來取得 Ray task 的結果。

其中,Ray 會自動將 task 分配到不同的 CPU core 上執行,並且會自動將結果合併。所以理論上當 core 數量越多,執行時間越短。

來看看效果吧!

在一台舊款筆電上執行 (Intel Core i5 5300U/2.3 GHz, dual core):

CPU count: 4
Sequence size: 100000, Local execution time: 1.6849534511566162
2023-10-06 15:45:30,365	INFO worker.py:1458 -- Connecting to existing Ray cluster at address: 172.16.100.216:6379...
2023-10-06 15:45:30,378	INFO worker.py:1633 -- Connected to Ray cluster. View the dashboard at 127.0.0.1:8265 
Sequence size: 100000, Remote execution time: 3.135549306869507

Dual core 的筆電,Local: 1.68s, Remote: 3.14s
好慘烈 :~

改在 CNTUG 的機器 G1.xlarge 執行結果如下:

CPU count: 16
Sequence size: 100000, Local execution time: 7.383708953857422
2023-10-06 07:45:28,615	INFO worker.py:1458 -- Connecting to existing Ray cluster at address: 103.122.117.204:6379...
2023-10-06 07:45:28,654	INFO worker.py:1633 -- Connected to Ray cluster. View the dashboard at 127.0.0.1:8265 
Sequence size: 100000, Remote execution time: 1.7547025680541992

Local: 7.38s
Remote: 1.75s

可以看到當 worker 數量增加時 (或是 core 數量增加時),Remote 的執行時間會比 Local 快很多。

Rerference:


上一篇
Day22 - Ray Cluster Management 操作
下一篇
Day24 - Ray Serve
系列文
MLOps/LLMOps - 從零開始30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言