Skip to content

Commit

Permalink
Fix issue 4810 (microsoft#4866)
Browse files Browse the repository at this point in the history
liuzhe-lz authored May 20, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent efd8c7c commit b4559f6
Showing 3 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion nni/algorithms/hpo/tpe_tuner.py
Original file line number Diff line number Diff line change
@@ -171,7 +171,7 @@ def update_search_space(self, space):
def generate_parameters(self, parameter_id, **kwargs):
if self.liar and self._running_params:
# give a fake loss for each concurrently running paramater set
history = {key: records.copy() for key, records in self._history.items()} # copy history
history = defaultdict(list, {key: records.copy() for key, records in self._history.items()}) # copy history
lie = self.liar.lie()
for param in self._running_params.values():
for key, value in param.items():
9 changes: 8 additions & 1 deletion nni/common/serializer.py
Original file line number Diff line number Diff line change
@@ -392,7 +392,8 @@ def _dump(*, obj: Any, fp: Optional[Any], use_trace: bool, pickle_size_limit: in
return json_tricks.dumps(obj, obj_encoders=encoders, **json_tricks_kwargs)


def load(string: Optional[str] = None, *, fp: Optional[Any] = None, ignore_comments: bool = True, **json_tricks_kwargs) -> Any:
def load(string: Optional[str] = None, *, fp: Optional[Any] = None,
preserve_order: bool = False, ignore_comments: bool = True, **json_tricks_kwargs) -> Any:
"""
Load the string or from file, and convert it to a complex data structure.
At least one of string or fp has to be not none.
@@ -403,6 +404,10 @@ def load(string: Optional[str] = None, *, fp: Optional[Any] = None, ignore_comme
JSON string to parse. Can be set to none if fp is used.
fp : str
File path to load JSON from. Can be set to none if string is used.
preserve_order : bool
`json_tricks parameter <https://json-tricks.readthedocs.io/en/latest/#order>`_
to use ``OrderedDict`` instead of ``dict``.
The order is in fact always preserved even when this is False.
ignore_comments : bool
Remove comments (starting with ``#`` or ``//``). Default is true.
@@ -427,6 +432,8 @@ def load(string: Optional[str] = None, *, fp: Optional[Any] = None, ignore_comme
_json_tricks_any_object_decode
]

# there was an issue that the user code does not accept ordered dict, and 3.7+ dict has guaranteed order
json_tricks_kwargs['preserve_order'] = preserve_order
# to bypass a deprecation warning in json-tricks
json_tricks_kwargs['ignore_comments'] = ignore_comments

13 changes: 13 additions & 0 deletions test/ut/sdk/test_serializer.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import OrderedDict
import math
import os
import pickle
@@ -24,6 +25,18 @@
from imported._test_serializer_py38 import test_positional_only


def test_ordered_json():
items = [
('a', 1),
('c', 3),
('b', 2),
]
orig = OrderedDict(items)
json = nni.dump(orig)
loaded = nni.load(json)
assert list(loaded.items()) == items


@nni.trace
class SimpleClass:
def __init__(self, a, b=1):

0 comments on commit b4559f6

Please sign in to comment.