Elffuss BraidHub

kikocis/SpatialLM public

main · 2.5 KiBRawPermalink
1# Copyright 2024 the LlamaFactory team.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7# http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15import os
16import sys
17import random
18import subprocess
19from copy import deepcopy
20
21from spatiallm.tuner import trainer
22from spatiallm.tuner.framework import logging
23from spatiallm.tuner.framework.utils import get_device_count, is_env_enabled
24from spatiallm.tuner.trainer import run_exp # use absolute import
25
26logger = logging.get_logger(__name__)
27
28
29def main():
30 force_torchrun = os.getenv("FORCE_TORCHRUN", "0").lower() in ["true", "1"]
31 if force_torchrun or get_device_count() > 1:
32 master_addr = os.getenv("MASTER_ADDR", "127.0.0.1")
33 master_port = os.getenv("MASTER_PORT", str(random.randint(20001, 29999)))
34 logger.info_rank0(
35 f"Initializing distributed tasks at: {master_addr}:{master_port}"
36 )
37 standalone = os.getenv("STANDALONE", "0").lower() in ["true", "1"]
38
39 env = deepcopy(os.environ)
40 if is_env_enabled("OPTIM_TORCH", "1"):
41 # optimize DDP, see https://zhuanlan.zhihu.com/p/671834539
42 env["PYTORCH_CUDA_ALLOC_CONF"] = "expandable_segments:True"
43 env["TORCH_NCCL_AVOID_RECORD_STREAMS"] = "1"
44
45 command = (
46 "torchrun {standalone} --nnodes {nnodes} --node_rank {node_rank} --nproc_per_node {nproc_per_node} "
47 "--master_addr {master_addr} --master_port {master_port} {file_name} {args}"
48 ).format(
49 standalone="--standalone" if standalone else "",
50 nnodes=os.getenv("NNODES", "1"),
51 node_rank=os.getenv("NODE_RANK", "0"),
52 nproc_per_node=os.getenv("NPROC_PER_NODE", str(get_device_count())),
53 master_addr=master_addr,
54 master_port=master_port,
55 file_name=trainer.__file__,
56 args=" ".join(sys.argv[1:]),
57 env=env,
58 check=True,
59 )
60 logger.info_rank0(f"Running command: {command}")
61 process = subprocess.run(command.split())
62 sys.exit(process.returncode)
63 else:
64 run_exp()
65
66
67if __name__ == "__main__":
68 main()