|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import os |
| 8 | + |
| 9 | +from torchtitan.distributed import ParallelDims, utils as dist_utils |
| 10 | +from torchtitan.train import main, Trainer |
| 11 | + |
| 12 | + |
| 13 | +class FTTrainer(Trainer): |
| 14 | + def init_distributed(self) -> ParallelDims: |
| 15 | + job_config = self.job_config |
| 16 | + |
| 17 | + # determine the global ranks when fault tolerance is enabled |
| 18 | + global_ranks = [] |
| 19 | + ft_config = job_config.fault_tolerance |
| 20 | + if ft_config.enable: |
| 21 | + group_size = ft_config.group_size |
| 22 | + replica_id = ft_config.replica_id |
| 23 | + first_rank = replica_id * group_size |
| 24 | + last_rank = first_rank + group_size - 1 |
| 25 | + global_ranks = list(range(first_rank, last_rank + 1)) |
| 26 | + |
| 27 | + # init distributed and build meshes |
| 28 | + dist_utils.init_distributed( |
| 29 | + job_config.comm, |
| 30 | + enable_cpu_backend=job_config.training.enable_cpu_offload, |
| 31 | + base_folder=job_config.job.dump_folder, |
| 32 | + ranks=global_ranks, |
| 33 | + ) |
| 34 | + |
| 35 | + world_size = int(os.environ["WORLD_SIZE"]) |
| 36 | + parallelism_config = job_config.parallelism |
| 37 | + |
| 38 | + return ParallelDims( |
| 39 | + dp_shard=parallelism_config.data_parallel_shard_degree, |
| 40 | + dp_replicate=parallelism_config.data_parallel_replicate_degree, |
| 41 | + cp=parallelism_config.context_parallel_degree, |
| 42 | + tp=parallelism_config.tensor_parallel_degree, |
| 43 | + pp=parallelism_config.pipeline_parallel_degree, |
| 44 | + ep=parallelism_config.expert_parallel_degree, |
| 45 | + etp=parallelism_config.expert_tensor_parallel_degree, |
| 46 | + world_size=world_size, |
| 47 | + ) |
| 48 | + |
| 49 | + |
| 50 | +if __name__ == "__main__": |
| 51 | + main(FTTrainer) |
0 commit comments