文档

在Ray Cluster中提交Job

更新时间:

Ray支持构建可扩展的人工智能(AI)和Python应用程序,广泛应用于机器学习领域。您可以基于ACK集群创建Ray Cluster,并在本地提交Job执行分布式任务,用于训练模型、数据处理、模型评估等场景。在本地RayCluster提交Ray Job。

前提条件

基于ACK创建Ray Cluster

Ray Cluster内运行Job作业有多种方式。更多信息,请参见how do you use the ray-clientquick start useing the ray job cli

  1. 执行以下命令,查询Ray Cluster的Pod信息。

    kubectl get pod -n ${RAY_CLUSTER_NS}

    预期输出:

    NAME                                           READY   STATUS    RESTARTS   AGE
    myfirst-ray-cluster-head-v7pbw                 2/2     Running   0          39m
  2. 执行以下命令,您可以在本地终端连接到Pod内部的Bash Shell。

    请将Pod名称替换为您实际的Pod的名称。

    kubectl exec -it -n ${RAY_CLUSTER_NS} myfirst-ray-cluster-head-v7pbw -- bash
  3. 在Head Pod中使用echocat命令,保存my_script.py文件。

    import ray
    import os
    
    # 连接本地或者远程ray cluster
    ray.init()
    
    @ray.remote(num_cpus=1)
    class Counter:
        def __init__(self):
            self.name = "test_counter"
            self.counter = 0
    
        def increment(self):
            self.counter += 1
    
        def get_counter(self):
            return "{} got {}".format(self.name, self.counter)
    
    counter = Counter.remote()
    
    for _ in range(10000):
        counter.increment.remote()
        print(ray.get(counter.get_counter.remote()))
    
  4. 运行my_script.py脚本,执行分布式任务。

    python my_script.py
    # 预期输出
    2024-01-24 04:25:27,286	INFO worker.py:1329 -- Using address 127.0.0.1:6379 set in the environment variable RAY_ADDRESS
    2024-01-24 04:25:27,286	INFO worker.py:1458 -- Connecting to existing Ray cluster at address: 172.16.0.236:6379...
    2024-01-24 04:25:27,295	INFO worker.py:1633 -- Connected to Ray cluster. View the dashboard at http://172.16.0.236:8265
    test_counter got 0
    test_counter got 1
    test_counter got 2
    test_counter got 3
    
    ...

相关操作

  • 本页导读 (1)
文档反馈