transformers

Форк
0
83 строки · 2.4 Кб
1
# Copyright 2020 The HuggingFace Team. All rights reserved.
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
"""
15
A simple launcher script for TPU training
16

17
Inspired by https://github.com/pytorch/pytorch/blob/master/torch/distributed/launch.py
18

19
::
20
    >>> python xla_spawn.py --num_cores=NUM_CORES_YOU_HAVE
21
               YOUR_TRAINING_SCRIPT.py (--arg1 --arg2 --arg3 and all other
22
               arguments of your training script)
23

24
"""
25

26

27
import importlib
28
import sys
29
from argparse import REMAINDER, ArgumentParser
30
from pathlib import Path
31

32
import torch_xla.distributed.xla_multiprocessing as xmp
33

34

35
def parse_args():
36
    """
37
    Helper function parsing the command line options
38
    @retval ArgumentParser
39
    """
40
    parser = ArgumentParser(
41
        description=(
42
            "PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes"
43
        )
44
    )
45

46
    # Optional arguments for the launch helper
47
    parser.add_argument("--num_cores", type=int, default=1, help="Number of TPU cores to use (1 or 8).")
48

49
    # positional
50
    parser.add_argument(
51
        "training_script",
52
        type=str,
53
        help=(
54
            "The full path to the single TPU training "
55
            "program/script to be launched in parallel, "
56
            "followed by all the arguments for the "
57
            "training script"
58
        ),
59
    )
60

61
    # rest from the training program
62
    parser.add_argument("training_script_args", nargs=REMAINDER)
63

64
    return parser.parse_args()
65

66

67
def main():
68
    args = parse_args()
69

70
    # Import training_script as a module.
71
    script_fpath = Path(args.training_script)
72
    sys.path.append(str(script_fpath.parent.resolve()))
73
    mod_name = script_fpath.stem
74
    mod = importlib.import_module(mod_name)
75

76
    # Patch sys.argv
77
    sys.argv = [args.training_script] + args.training_script_args + ["--tpu_num_cores", str(args.num_cores)]
78

79
    xmp.spawn(mod._mp_fn, args=(), nprocs=args.num_cores)
80

81

82
if __name__ == "__main__":
83
    main()
84

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.