tinystory / configurator.py
GPT-14's picture
Internship
cce791c
"""
Poor Man's Configurator. Probably a terrible idea. Example usage:
$ python train.py config/override_file.py --batch_size=32
this will first run config/override_file.py, then override batch_size to 32
The code in this file will be run as follows from e.g. train.py:
>>> exec(open('configurator.py').read())
So it's not a Python module, it's just shuttling this code away from train.py
The code in this script then overrides the globals()
I know people are not going to love this, I just really dislike configuration
complexity and having to prepend config. to every single variable. If someone
comes up with a better simple Python solution I am all ears.
"""
import sys
from ast import literal_eval
for arg in sys.argv[1:]:
if '=' not in arg: #ํŒŒ์ผ ์ž…๋ ฅ๋ฐ›์„๋•Œ๋Š” ์ด๊ฑฐ if๋ฌธ ํ•œ๋ฒˆ๋งŒ ์‹คํ–‰๋  ๊ฒƒ
# assume it's the name of a config file
assert not arg.startswith('--') # --๊ฐ€ ์—†์–ด์•ผ ์˜ฌ๋ฐ”๋ฅธ ์ฝ”๋“œ!
#configuration์„ --compile=False๋กœ ํ•˜๋‚˜์”ฉ ์ง€์ •์•ˆํ•˜๊ณ  ํŒŒ์ผ๋กœ ํ•  ๊ฒƒ
config_file = arg
print(f"Overriding config with {config_file}:")
with open(config_file) as f:
print(f.read())
exec(open(config_file).read())
#read ํ•จ์ˆ˜๋Š” ํŒŒ์ผ๋‚ด์˜ ํ…์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ๊ทธ๊ฒƒ์„
#ํ•œ์ค„ ํ•œ์ค„์”ฉ ์‹คํ–‰ํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด exec๋ฅผ ์“ด๋‹ค.
else: #์˜ต์…˜ ์—ฌ๋Ÿฌ๊ฐœ ์ง์ ‘ ์ง€์ •ํ–ˆ์„ ๊ฒฝ์šฐ else๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ๋Œ ๊ฒƒ.
# assume it's a --key=value argument
assert arg.startswith('--')
key, val = arg.split('=')
key = key[2:]
if key in globals():
try:
# attempt to eval it it (e.g. if bool, number, or etc)
attempt = literal_eval(val)
except (SyntaxError, ValueError):
# if that goes wrong, just use the string
attempt = val
# ensure the types match ok
assert type(attempt) == type(globals()[key])
# cross fingers
print(f"Overriding: {key} = {attempt}")
globals()[key] = attempt
else:
raise ValueError(f"Unknown config key: {key}")