[tune] Support Configuration Merging (#3584)

* merge configs

* deep merge

* lint

* add resolve

* test
This commit is contained in:
Richard Liaw
2018-12-26 20:07:11 +09:00
committed by Eric Liang
parent 4ce3818be5
commit 6e2d7a9ba1
6 changed files with 94 additions and 46 deletions
+36
View File
@@ -3,6 +3,7 @@ from __future__ import division
from __future__ import print_function
import base64
import copy
import numpy as np
import ray
@@ -35,6 +36,41 @@ def get_pinned_object(pinned_id):
ObjectID(base64.b64decode(pinned_id[len(PINNED_OBJECT_PREFIX):]))))
def merge_dicts(d1, d2):
"""Returns a new dict that is d1 and d2 deep merged."""
merged = copy.deepcopy(d1)
deep_update(merged, d2, True, [])
return merged
def deep_update(original, new_dict, new_keys_allowed, whitelist):
"""Updates original dict with values from new_dict recursively.
If new key is introduced in new_dict, then if new_keys_allowed is not
True, an error will be thrown. Further, for sub-dicts, if the key is
in the whitelist, then new subkeys can be introduced.
Args:
original (dict): Dictionary with default values.
new_dict (dict): Dictionary with values to be updated
new_keys_allowed (bool): Whether new keys are allowed.
whitelist (list): List of keys that correspond to dict values
where new subkeys can be introduced. This is only at
the top level.
"""
for k, value in new_dict.items():
if k not in original:
if not new_keys_allowed:
raise Exception("Unknown config parameter `{}` ".format(k))
if type(original.get(k)) is dict:
if k in whitelist:
deep_update(original[k], value, True, [])
else:
deep_update(original[k], value, new_keys_allowed, [])
else:
original[k] = value
return original
def _to_pinnable(obj):
"""Converts obj to a form that can be pinned in object store memory.