File size: 15,251 Bytes
8c9ba62
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
# -*- coding: utf-8 -*-
"""Test cases for Synchronizer modules."""

import asyncio
import multiprocessing
import os
import shutil
import time
import unittest
from copy import deepcopy
from datetime import datetime
from typing import Dict, List

import ray
from parameterized import parameterized_class

from tests.tools import (
    TensorBoardParser,
    get_checkpoint_path,
    get_model_path,
    get_template_config,
    get_unittest_dataset_config,
)
from trinity.algorithm import ALGORITHM_TYPE
from trinity.cli.launcher import both, explore, train
from trinity.common.config import Config, ExperienceBufferConfig
from trinity.common.constants import StorageType, SyncMethod, SyncStyle
from trinity.explorer.explorer import Explorer
from trinity.trainer.trainer import Trainer
from trinity.utils.log import get_logger

logger = get_logger(__name__)
CHECKPOINT_ROOT_DIR = os.path.join(os.path.dirname(__file__), "temp_checkpoint_dir")


def trainer_monkey_patch(config: Config, max_steps: int, intervals: List[int]):
    async def new_sample_data(self):
        self.logger.info(f"Sample data for step {self.train_step_num + 1} started.")
        await asyncio.sleep(0.1)
        time.sleep(intervals[self.engine.global_steps - 1])
        self.logger.info(f"Sample data for step {self.train_step_num + 1} finished.")
        return [], {}, []

    async def new_train_step(self, exps) -> Dict:
        self.engine.algorithm = ALGORITHM_TYPE.get(config.algorithm.algorithm_type)
        self.engine.global_steps += 1
        self.logger.info(f"Training at step {self.engine.global_steps} started.")
        await asyncio.sleep(0.1)
        time.sleep(intervals[self.engine.global_steps - 1])
        metrics = {"actor/step": self.engine.global_steps}
        self.logger.info(f"Training at step {self.engine.global_steps} finished.")
        return metrics

    Trainer.train_step = new_train_step
    Trainer._sample_data = new_sample_data


def explorer_monkey_patch(config: Config, max_steps: int, intervals: List[int]):
    async def new_explore_step(self):
        if self.explore_step_num == max_steps:
            await self.save_checkpoint(sync_weight=False)
        self.explore_step_num += 1
        return self.explore_step_num <= max_steps

    def wrapper(old_save_checkpoint):
        async def new_save_checkpoint(self, sync_weight: bool = False):
            await asyncio.sleep(intervals.pop(0))
            await old_save_checkpoint(self, sync_weight)

        return new_save_checkpoint

    async def new_finish_explore_step(self, step: int, model_version: int) -> None:
        metric = {"rollout/model_version": model_version}
        self.monitor.log(metric, step=step)

    Explorer.explore_step = new_explore_step
    Explorer.save_checkpoint = wrapper(Explorer.save_checkpoint)
    Explorer._finish_explore_step = new_finish_explore_step


def run_trainer(config: Config, max_steps: int, intervals: List[int]) -> None:
    ray.init(ignore_reinit_error=True, namespace=config.ray_namespace)
    trainer_monkey_patch(config, max_steps, intervals)
    train(config)
    ray.shutdown()


def run_explorer(config: Config, max_steps: int, intervals: List[int]) -> None:
    ray.init(ignore_reinit_error=True, namespace=config.ray_namespace)
    explorer_monkey_patch(config, max_steps, intervals)
    explore(config)
    ray.shutdown()


def run_both(
    config: Config, max_steps: int, trainer_intervals: List[int], explorer_intervals: List[int]
) -> None:
    ray.init(ignore_reinit_error=True, namespace=config.ray_namespace)
    trainer_monkey_patch(config, max_steps, trainer_intervals)
    explorer_monkey_patch(config, max_steps, explorer_intervals)
    both(config)
    ray.shutdown()


class BaseTestSynchronizer(unittest.TestCase):
    def setUp(self):
        if multiprocessing.get_start_method(allow_none=True) != "spawn":
            multiprocessing.set_start_method("spawn", force=True)
        self.process_list = []

    def tearDown(self):
        ray.shutdown(_exiting_interpreter=True)
        if os.path.exists(CHECKPOINT_ROOT_DIR):
            shutil.rmtree(CHECKPOINT_ROOT_DIR, ignore_errors=True)
        for process in self.process_list:
            if process.is_alive():
                process.terminate()
                process.join(timeout=10)
                if process.is_alive():
                    process.kill()
                    process.join()


class TestSynchronizerExit(BaseTestSynchronizer):
    def test_synchronizer(self):
        config = get_template_config()
        config.project = "unittest"
        config.name = f"test_synchronizer_{datetime.now().strftime('%Y%m%d%H%M%S')}"
        config.checkpoint_root_dir = get_checkpoint_path()
        config.buffer.total_epochs = 1
        config.buffer.batch_size = 4
        config.cluster.gpu_per_node = 2
        config.cluster.node_num = 1
        config.model.model_path = get_model_path()
        config.buffer.explorer_input.taskset = get_unittest_dataset_config("countdown")
        config.buffer.trainer_input.experience_buffer = ExperienceBufferConfig(
            name="exp_buffer",
            storage_type=StorageType.QUEUE.value,
        )
        config.synchronizer.sync_method = SyncMethod.CHECKPOINT
        config.synchronizer.sync_style = SyncStyle.DYNAMIC_BY_EXPLORER
        config.synchronizer.sync_interval = 2
        config.trainer.save_interval = 100
        config.monitor.monitor_type = "tensorboard"
        trainer_config = deepcopy(config)
        trainer_config.mode = "train"
        trainer_config.buffer.train_batch_size = 4
        trainer_config.check_and_update()

        explorer1_config = deepcopy(config)
        explorer1_config.mode = "explore"
        explorer1_config.explorer.name = "explorer1"
        explorer1_config.explorer.rollout_model.engine_num = 1
        explorer1_config.explorer.rollout_model.tensor_parallel_size = 1
        explorer1_config.buffer.explorer_output = ExperienceBufferConfig(
            name="exp_buffer",
            storage_type=StorageType.QUEUE.value,
        )
        explorer1_config.check_and_update()

        trainer_process = multiprocessing.Process(
            target=run_trainer, args=(trainer_config, 8, [2, 1, 2, 1, 2, 1, 2, 1])
        )
        trainer_process.start()
        self.process_list.append(trainer_process)

        ray.init(ignore_reinit_error=True)
        while True:
            try:
                synchronizer = ray.get_actor("synchronizer", namespace=trainer_config.ray_namespace)
                break
            except ValueError:
                print("waiting for trainer to start.")
                time.sleep(5)
        explorer_process_1 = multiprocessing.Process(
            target=run_explorer,
            args=(explorer1_config, 8, [0, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5]),
        )
        explorer_process_1.start()
        self.process_list.append(explorer_process_1)

        self.assertEqual(
            synchronizer, ray.get_actor("synchronizer", namespace=trainer_config.ray_namespace)
        )
        for _ in range(12):  # Wait for up to 60 seconds
            try:
                explorer1 = ray.get_actor("explorer1", namespace=trainer_config.ray_namespace)
                ray.get(explorer1.is_alive.remote())
                break
            except ValueError:
                print("waiting for explorer1 to start.")
                time.sleep(5)

        trainer_process.join(timeout=200)
        self.assertEqual(
            synchronizer, ray.get_actor("synchronizer", namespace=trainer_config.ray_namespace)
        )

        explorer_process_1.join(timeout=200)
        time.sleep(6)
        with self.assertRaises(ValueError):
            ray.get_actor("synchronizer", namespace=trainer_config.ray_namespace)


@parameterized_class(
    (
        "sync_method",
        "sync_style",
        "max_steps",
        "trainer_intervals",
        "explorer1_intervals",
        "explorer2_intervals",
    ),
    [
        (
            SyncMethod.CHECKPOINT,
            SyncStyle.FIXED,
            8,
            [2, 1, 2, 1, 2, 1, 2, 1],
            [0, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5],
            [0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
        ),
        (
            SyncMethod.CHECKPOINT,
            SyncStyle.DYNAMIC_BY_EXPLORER,
            8,
            [2, 1, 2, 1, 2, 1, 2, 1],
            [0, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5],
            [0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
        ),
        (
            SyncMethod.MEMORY,
            SyncStyle.FIXED,
            8,
            [2, 1, 2, 1, 2, 1, 2, 1],
            [0, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5],
            [0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
        ),
        (
            SyncMethod.MEMORY,
            SyncStyle.DYNAMIC_BY_EXPLORER,
            8,
            [2, 1, 2, 1, 2, 1, 2, 1],
            [0, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5],
            [0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5],
        ),
    ],
)
class TestStateDictBasedSynchronizer(BaseTestSynchronizer):
    def test_synchronizer(self):
        config = get_template_config()
        config.project = "unittest"
        config.name = f"test_synchronizer_{datetime.now().strftime('%Y%m%d%H%M%S')}"
        config.checkpoint_root_dir = get_checkpoint_path()
        config.buffer.total_epochs = 1
        config.buffer.batch_size = 4
        config.cluster.gpu_per_node = 2
        config.cluster.node_num = 1
        config.model.model_path = get_model_path()
        config.buffer.explorer_input.taskset = get_unittest_dataset_config("countdown")
        config.buffer.trainer_input.experience_buffer = ExperienceBufferConfig(
            name="exp_buffer",
            storage_type=StorageType.QUEUE.value,
        )
        config.synchronizer.sync_method = self.sync_method
        config.synchronizer.sync_style = self.sync_style
        config.synchronizer.sync_interval = 2
        config.trainer.save_interval = 100
        config.trainer.total_steps = self.max_steps
        config.monitor.monitor_type = "tensorboard"
        trainer_config = deepcopy(config)
        trainer_config.mode = "train"
        trainer_config.buffer.train_batch_size = 4
        trainer_config.check_and_update()

        explorer1_config = deepcopy(config)
        explorer1_config.mode = "explore"
        explorer1_config.explorer.name = "explorer1"
        explorer1_config.explorer.rollout_model.engine_num = 1
        explorer1_config.explorer.rollout_model.tensor_parallel_size = 1
        explorer1_config.buffer.explorer_output = ExperienceBufferConfig(
            name="exp_buffer",
            storage_type=StorageType.QUEUE.value,
        )
        explorer2_config = deepcopy(explorer1_config)
        explorer2_config.explorer.name = "explorer2"
        explorer1_config.check_and_update()
        explorer2_config.check_and_update()

        trainer_process = multiprocessing.Process(
            target=run_trainer, args=(trainer_config, self.max_steps, self.trainer_intervals)
        )
        trainer_process.start()
        self.process_list.append(trainer_process)

        ray.init(ignore_reinit_error=True)
        while True:
            try:
                ray.get_actor("queue-exp_buffer", namespace=trainer_config.ray_namespace)
                break
            except ValueError:
                print("waiting for trainer to start.")
                time.sleep(5)
        explorer_process_1 = multiprocessing.Process(
            target=run_explorer,
            args=(explorer1_config, self.max_steps, self.explorer1_intervals),
        )
        explorer_process_1.start()
        self.process_list.append(explorer_process_1)
        explorer_process_2 = multiprocessing.Process(
            target=run_explorer, args=(explorer2_config, self.max_steps, self.explorer2_intervals)
        )
        explorer_process_2.start()
        self.process_list.append(explorer_process_2)

        explorer_process_1.join(timeout=200)
        explorer_process_2.join(timeout=200)
        trainer_process.join(timeout=200)

        # check the tensorboard
        parser = TensorBoardParser(
            os.path.join(trainer_config.monitor.cache_dir, "tensorboard", "trainer")
        )
        actor_metrics = parser.metric_list("actor")
        self.assertEqual(parser.metric_max_step(actor_metrics[0]), 8)
        parser = TensorBoardParser(
            os.path.join(explorer1_config.monitor.cache_dir, "tensorboard", "explorer1")
        )
        rollout_metrics = parser.metric_list("rollout")
        self.assertEqual(parser.metric_max_step(rollout_metrics[0]), 8)
        parser = TensorBoardParser(
            os.path.join(explorer2_config.monitor.cache_dir, "tensorboard", "explorer2")
        )
        rollout_metrics = parser.metric_list("rollout")
        self.assertEqual(parser.metric_max_step(rollout_metrics[0]), 8)


@parameterized_class(
    ("sync_style", "max_steps", "trainer_intervals", "explorer_intervals"),
    [
        (
            SyncStyle.FIXED,
            8,
            [2, 1, 2, 1, 2, 1, 2, 1],
            [0, 2.5, 2.5, 2.5, 2.5, 0],
        ),
        (
            SyncStyle.DYNAMIC_BY_EXPLORER,
            8,
            [2, 1, 2, 1, 2, 1, 2, 1],
            [0, 0.5, 0.5, 0.5, 0.5, 0],
        ),
    ],
)
class TestNCCLBasedSynchronizer(BaseTestSynchronizer):
    def test_synchronizer(self):
        config = get_template_config()
        config.project = "unittest"
        config.name = f"test_synchronizer_{datetime.now().strftime('%Y%m%d%H%M%S')}"
        config.checkpoint_root_dir = get_checkpoint_path()
        config.buffer.total_epochs = 1
        config.buffer.batch_size = 4
        config.trainer.total_steps = self.max_steps
        config.model.model_path = get_model_path()
        config.buffer.explorer_input.taskset = get_unittest_dataset_config("countdown")
        config.buffer.trainer_input.experience_buffer = ExperienceBufferConfig(
            name="exp_buffer",
            storage_type=StorageType.QUEUE.value,
        )
        config.synchronizer.sync_method = SyncMethod.NCCL
        config.synchronizer.sync_style = self.sync_style
        config.synchronizer.sync_interval = 2
        config.trainer.save_interval = 100
        config.monitor.monitor_type = "tensorboard"
        config.mode = "both"
        config.check_and_update()

        # TODO: test more interval cases
        both_process = multiprocessing.Process(
            target=run_both,
            args=(config, self.max_steps, self.trainer_intervals, self.explorer_intervals),
        )
        both_process.start()
        self.process_list.append(both_process)
        both_process.join(timeout=200)

        # check the tensorboard
        parser = TensorBoardParser(os.path.join(config.monitor.cache_dir, "tensorboard", "trainer"))
        actor_metrics = parser.metric_list("actor")
        self.assertEqual(parser.metric_max_step(actor_metrics[0]), 8)
        parser = TensorBoardParser(
            os.path.join(config.monitor.cache_dir, "tensorboard", "explorer")
        )
        rollout_metrics = parser.metric_list("rollout")
        self.assertEqual(parser.metric_max_step(rollout_metrics[0]), 8)