|
| 1 | +# Ray + RAGEN |
| 2 | + |
| 3 | +This example shows how use `dstack` and [RAGEN :material-arrow-top-right-thin:{ .external }](https://github.com/RAGEN-AI/RAGEN){:target="_blank"} |
| 4 | +to fine-tune an agent on mulitiple nodes. |
| 5 | + |
| 6 | +Under the hood `RAGEN` uses [verl :material-arrow-top-right-thin:{ .external }](https://github.com/volcengine/verl){:target="_blank"} for Reinforcement Learning and [Ray :material-arrow-top-right-thin:{ .external }](https://docs.ray.io/en/latest/){:target="_blank"} for ditributed training. |
| 7 | + |
| 8 | +## Create fleet |
| 9 | + |
| 10 | +Before submitted disributed training runs, make sure to create a fleet with a `placement` set to `cluster`. |
| 11 | + |
| 12 | +> For more detials on how to use clusters with `dstack`, check the [Clusters](https://dstack.ai/docs/guides/clusters) guide. |
| 13 | +
|
| 14 | +## Run a Ray cluster |
| 15 | + |
| 16 | +If you want to use Ray with `dstack`, you have to first run a Ray cluster. |
| 17 | + |
| 18 | +The task below runs a Ray cluster on an existing fleet: |
| 19 | + |
| 20 | +<div editor-title="examples/distributed-training/ray-ragen/.dstack.yml"> |
| 21 | + |
| 22 | +```yaml |
| 23 | +type: task |
| 24 | +name: ray-ragen-cluster |
| 25 | + |
| 26 | +nodes: 2 |
| 27 | + |
| 28 | +env: |
| 29 | +- WANDB_API_KEY |
| 30 | +image: whatcanyousee/verl:ngc-cu124-vllm0.8.5-sglang0.4.6-mcore0.12.0-te2.2 |
| 31 | +commands: |
| 32 | + - wget -O miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh |
| 33 | + - bash miniconda.sh -b -p /workflow/miniconda |
| 34 | + - eval "$(/workflow/miniconda/bin/conda shell.bash hook)" |
| 35 | + - git clone https://github.com/RAGEN-AI/RAGEN.git |
| 36 | + - cd RAGEN |
| 37 | + - bash scripts/setup_ragen.sh |
| 38 | + - conda activate ragen |
| 39 | + - cd verl |
| 40 | + - pip install --no-deps -e . |
| 41 | + - pip install hf_transfer hf_xet |
| 42 | + - pip uninstall -y ray |
| 43 | + - pip install -U "ray[default]" |
| 44 | + - | |
| 45 | + if [ $DSTACK_NODE_RANK = 0 ]; then |
| 46 | + ray start --head --port=6379; |
| 47 | + else |
| 48 | + ray start --address=$DSTACK_MASTER_NODE_IP:6379 |
| 49 | + fi |
| 50 | +
|
| 51 | +# Expose Ray dashboard port |
| 52 | +ports: |
| 53 | + - 8265 |
| 54 | + |
| 55 | +resources: |
| 56 | + gpu: 80GB:8 |
| 57 | + shm_size: 128GB |
| 58 | + |
| 59 | +# Save checkpoints on the instance |
| 60 | +volumes: |
| 61 | + - /checkpoints:/checkpoints |
| 62 | +``` |
| 63 | +
|
| 64 | +</div> |
| 65 | +
|
| 66 | +We are using verl's docker image for vLLM with FSDP. See [Installation :material-arrow-top-right-thin:{ .external }](https://verl.readthedocs.io/en/latest/start/install.html){:target="_blank"} for more. |
| 67 | +
|
| 68 | +The `RAGEN` setup script `scripts/setup_ragen.sh` isolates dependencies within Conda environment. |
| 69 | + |
| 70 | +Note that the Ray setup in the RAGEN environment is missing the dashboard, so we reinstall it using `ray[default]`. |
| 71 | + |
| 72 | +Now, if you run this task via `dstack apply`, it will automatically forward the Ray's dashboard port to `localhost:8265`. |
| 73 | + |
| 74 | +<div class="termy"> |
| 75 | + |
| 76 | +```shell |
| 77 | +$ dstack apply -f examples/distributed-training/ray-ragen/.dstack.yml |
| 78 | +``` |
| 79 | + |
| 80 | +</div> |
| 81 | + |
| 82 | +As long as the `dstack apply` is attached, you can use `localhost:8265` to submit Ray jobs for execution. |
| 83 | +If `dstack apply` is detached, you can use `dstack attach` to re-attach. |
| 84 | + |
| 85 | +## Submit Ray jobs |
| 86 | + |
| 87 | +Before you can submit Ray jobs, ensure to install `ray` locally: |
| 88 | + |
| 89 | +<div class="termy"> |
| 90 | + |
| 91 | +```shell |
| 92 | +$ pip install ray |
| 93 | +``` |
| 94 | + |
| 95 | +</div> |
| 96 | + |
| 97 | +Now you can submit the training job to the Ray cluster which is available at `localhost:8265`: |
| 98 | + |
| 99 | +<div class="termy"> |
| 100 | + |
| 101 | +```shell |
| 102 | +$ RAY_ADDRESS=http://localhost:8265 |
| 103 | +$ ray job submit \ |
| 104 | + -- bash -c "\ |
| 105 | + export PYTHONPATH=/workflow/RAGEN; \ |
| 106 | + cd /workflow/RAGEN; \ |
| 107 | + /workflow/miniconda/envs/ragen/bin/python train.py \ |
| 108 | + --config-name base \ |
| 109 | + system.CUDA_VISIBLE_DEVICES=[0,1,2,3,4,5,6,7] \ |
| 110 | + model_path=Qwen/Qwen2.5-7B-Instruct \ |
| 111 | + trainer.experiment_name=agent-fine-tuning-Qwen2.5-7B \ |
| 112 | + trainer.n_gpus_per_node=8 \ |
| 113 | + trainer.nnodes=2 \ |
| 114 | + micro_batch_size_per_gpu=2 \ |
| 115 | + trainer.default_local_dir=/checkpoints \ |
| 116 | + trainer.save_freq=50 \ |
| 117 | + actor_rollout_ref.rollout.tp_size_check=False \ |
| 118 | + actor_rollout_ref.rollout.tensor_model_parallel_size=4" |
| 119 | +``` |
| 120 | + |
| 121 | +</div> |
| 122 | + |
| 123 | +!!! info "Training parameters" |
| 124 | + 1. `actor_rollout_ref.rollout.tensor_model_parallel_size=4`, because `Qwen/Qwen2.5-7B-Instruct` has 28 attention heads and number of attention heads should be divisible by `tensor_model_parallel_size` |
| 125 | + 2. `actor_rollout_ref.rollout.tp_size_check=False`, if True `tensor_model_parallel_size` should be equal to `trainer.n_gpus_per_node` |
| 126 | + 3. `micro_batch_size_per_gpu=2`, to keep the RAGEN-paper's `rollout_filter_ratio` and `es_manager` settings as it is for world size `16` |
| 127 | + |
| 128 | +Using Ray via `dstack` is a powerful way to get access to the rich Ray ecosystem while benefiting from `dstack`'s provisioning capabilities. |
| 129 | + |
| 130 | +!!! info "What's next" |
| 131 | + 1. Check the [Clusters](https://dstack.ai/docs/guides/clusters) guide |
| 132 | + 2. Read about [distributed tasks](https://dstack.ai/docs/concepts/tasks#distributed-tasks) and [fleets](https://dstack.ai/docs/concepts/fleets) |
| 133 | + 3. Browse Ray's [docs :material-arrow-top-right-thin:{ .external }](https://docs.ray.io/en/latest/train/examples.html){:target="_blank"} for other examples. |
0 commit comments