-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathdistributed_config.py
More file actions
44 lines (35 loc) · 1.82 KB
/
distributed_config.py
File metadata and controls
44 lines (35 loc) · 1.82 KB
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
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: LicenseRef-Apache2
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
from dataclasses import dataclass, field
logger = logging.getLogger(__name__)
@dataclass(frozen=True)
class DistributedConfig:
"""Class to track distributed ranks and handle basic distributed training setup.
If torch distributed environment variables are not set, we set them to default values for single-process training.
Attributes:
rank: The rank of the process.
local_rank: The local rank of the process.
world_size: The total number of processes.
"""
rank: int = field(default_factory=lambda: int(os.environ.setdefault("RANK", "0")))
local_rank: int = field(default_factory=lambda: int(os.environ.setdefault("LOCAL_RANK", "0")))
world_size: int = field(default_factory=lambda: int(os.environ.setdefault("WORLD_SIZE", "1")))
_master_addr: str = field(default_factory=lambda: os.environ.setdefault("MASTER_ADDR", "localhost"))
_master_port: str = field(default_factory=lambda: os.environ.setdefault("MASTER_PORT", "12355"))
def is_main_process(self) -> bool:
"""This is the global rank 0 process, to be used for wandb logging, etc."""
return self.rank == 0