forked from facebookresearch/coconut
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
159 lines (129 loc) · 4.58 KB
/
Copy pathutils.py
File metadata and controls
159 lines (129 loc) · 4.58 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
import datetime
import hashlib
import json
import os
import random
import subprocess
from typing import Any, Dict, Iterable, Mapping, MutableMapping, Optional, Tuple, Union
import numpy as np
import torch
try:
from omegaconf import DictConfig, ListConfig, OmegaConf # type: ignore
except ImportError: # pragma: no cover - OmegaConf is optional at import time
DictConfig = None # type: ignore
ListConfig = None # type: ignore
OmegaConf = None # type: ignore
def set_seed(seed_value):
random.seed(seed_value)
np.random.seed(seed_value)
torch.manual_seed(seed_value)
os.environ["PYTHONHASHSEED"] = str(seed_value)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def _materialize_config(config: Any) -> Any:
if OmegaConf is not None and isinstance(config, (DictConfig, ListConfig)):
return OmegaConf.to_container(config, resolve=True)
return config
def _flatten(
data: Any,
parent_key: str = "",
sep: str = ".",
) -> Dict[str, Any]:
items: Dict[str, Any] = {}
if isinstance(data, Mapping):
for key in sorted(data.keys()):
value = data[key]
new_key = f"{parent_key}{sep}{key}" if parent_key else str(key)
items.update(_flatten(value, new_key, sep=sep))
elif isinstance(data, (list, tuple)):
for index, value in enumerate(data):
new_key = f"{parent_key}{sep}{index}" if parent_key else str(index)
items.update(_flatten(value, new_key, sep=sep))
else:
items[parent_key] = data
return items
def canonicalize_config(
config: Any,
ignore_keys: Optional[Iterable[str]] = None,
) -> Dict[str, Any]:
materialized = _materialize_config(config)
flattened = _flatten(materialized)
ignore = set(ignore_keys or [])
result: Dict[str, Any] = {}
for key, value in flattened.items():
if key in ignore:
continue
result[key] = value
return result
def _encode_json(data: Any) -> str:
def _default(obj: Any) -> Any:
if isinstance(obj, (datetime.datetime, datetime.date)):
return obj.isoformat()
return str(obj)
return json.dumps(
data,
sort_keys=True,
separators=(",", ":"),
default=_default,
)
def get_git_metadata(repo_root: Optional[str] = None) -> Dict[str, Any]:
def _run_git(args: Iterable[str]) -> Tuple[int, str]:
try:
result = subprocess.run(
["git", *args],
cwd=repo_root,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=False,
text=True,
)
return result.returncode, result.stdout.strip()
except (FileNotFoundError, OSError): # pragma: no cover - git not available
return 1, ""
metadata: Dict[str, Any] = {
"commit": None,
"is_dirty": False,
"diff_sha": None,
"repo_root": repo_root,
}
code, stdout = _run_git(["rev-parse", "--show-toplevel"])
if code == 0:
repo_root = stdout
metadata["repo_root"] = repo_root
code, commit = _run_git(["rev-parse", "HEAD"])
if code == 0 and commit:
metadata["commit"] = commit
code, status_output = _run_git(["status", "--porcelain"])
is_dirty = code == 0 and bool(status_output)
metadata["is_dirty"] = is_dirty
if is_dirty:
code, diff_output = _run_git(["diff", "HEAD"])
if code == 0:
diff_sha = hashlib.sha256(diff_output.encode("utf-8")).hexdigest()
metadata["diff_sha"] = diff_sha
return metadata
def compute_experiment_signature(
config: Any,
*,
ignore_keys: Optional[Iterable[str]] = None,
git_metadata: Optional[Dict[str, Any]] = None,
extra: Optional[MutableMapping[str, Any]] = None,
hash_len: int = 16,
) -> str:
canonical_config = canonicalize_config(config, ignore_keys=ignore_keys)
payload: Dict[str, Any] = {
"config": canonical_config,
"git": git_metadata or get_git_metadata(),
}
if extra:
payload["extra"] = extra
serialized = _encode_json(payload)
digest = hashlib.sha256(serialized.encode("utf-8")).hexdigest()
return digest[:hash_len]
_BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
def generate_attempt_id(length: int = 8) -> str:
if length <= 0:
raise ValueError("length must be positive")
return "".join(random.choice(_BASE62_ALPHABET) for _ in range(length))