ryanramos commited on
Commit
f7522c4
·
1 Parent(s): da33fce

Delete utils.py

Browse files
Files changed (1) hide show
  1. utils.py +0 -127
utils.py DELETED
@@ -1,127 +0,0 @@
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
- # Copyright (c) Meta Platforms, Inc. and affiliates.
8
- # All rights reserved.
9
- #
10
- # This source code is licensed under the BSD-style license found in the
11
- # LICENSE file in the root directory of this source tree.
12
-
13
- import json
14
- import os
15
-
16
- import torch
17
- import torch.distributed as dist
18
- from torch import nn
19
-
20
-
21
- def setup_for_distributed(is_master):
22
- """
23
- This function disables printing when not in master process
24
- """
25
- import builtins as __builtin__
26
-
27
- builtin_print = __builtin__.print
28
-
29
- def print(*args, **kwargs):
30
- force = kwargs.pop("force", False)
31
- if is_master or force:
32
- builtin_print(*args, **kwargs)
33
-
34
- __builtin__.print = print
35
-
36
-
37
- def is_dist_avail_and_initialized():
38
- if not dist.is_available():
39
- return False
40
- if not dist.is_initialized():
41
- return False
42
- return True
43
-
44
-
45
- def get_world_size():
46
- if not is_dist_avail_and_initialized():
47
- return 1
48
- return dist.get_world_size()
49
-
50
-
51
- def get_rank():
52
- if not is_dist_avail_and_initialized():
53
- return 0
54
- return dist.get_rank()
55
-
56
-
57
- def is_main_process():
58
- return get_rank() == 0
59
-
60
-
61
- def init_distributed_mode(args):
62
- if "RANK" in os.environ and "WORLD_SIZE" in os.environ:
63
- args["rank"] = int(os.environ["RANK"])
64
- args["world_size"] = int(os.environ["WORLD_SIZE"])
65
- args["gpu"] = int(os.environ["LOCAL_RANK"])
66
- elif "SLURM_PROCID" in os.environ:
67
- args["rank"] = int(os.environ["SLURM_PROCID"])
68
- args["gpu"] = args["rank"] % torch.cuda.device_count()
69
- else:
70
- print("Not using distributed mode")
71
- args["distributed"] = False
72
- return
73
-
74
- args["distributed"] = True
75
-
76
- torch.cuda.set_device(args["gpu"])
77
- args["dist_backend"] = "nccl"
78
- print(
79
- "| distributed init (rank {}): {}".format(args["rank"], args["dist_url"]),
80
- flush=True,
81
- )
82
- torch.distributed.init_process_group(
83
- backend=args["dist_backend"],
84
- init_method=args["dist_url"],
85
- world_size=args["world_size"],
86
- rank=args["rank"],
87
- )
88
- torch.distributed.barrier()
89
- setup_for_distributed(args["rank"] == 0)
90
-
91
-
92
- def save_result(result, directory, file_name):
93
- rank_path = os.path.join(directory, "{}_rank_{}.json".format(file_name, get_rank()))
94
- main_path = os.path.join(directory, "{}.json".format(file_name))
95
- json.dump(result, open(rank_path, "w"))
96
-
97
- if is_dist_avail_and_initialized():
98
- dist.barrier()
99
-
100
- if is_main_process():
101
- result = []
102
- for rank in range(get_world_size()):
103
- rank_path = os.path.join(
104
- directory, "{}_rank_{}.json".format(file_name, rank)
105
- )
106
- rank_res = json.load(open(rank_path, "r"))
107
- result += rank_res
108
- json.dump(result, open(main_path, "w"))
109
-
110
- if is_dist_avail_and_initialized():
111
- dist.barrier()
112
-
113
-
114
- def add_weight_decay(model: nn.Module, weight_decay: float) -> None:
115
- decay = []
116
- no_decay = []
117
- for name, param in model.named_parameters():
118
- if not param.requires_grad:
119
- continue # skip weight_decay for momentum models
120
- if len(param.shape) == 1 or name.endswith(".bias"):
121
- no_decay.append(param)
122
- else:
123
- decay.append(param)
124
- return [
125
- {"params": no_decay, "weight_decay": 0.0},
126
- {"params": decay, "weight_decay": weight_decay},
127
- ]