Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[tune] Add custom field for serializations #4237

Merged
merged 6 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions python/ray/tune/automl/search_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def next_trials(self):
trial.best_result = None
trial.param_config = param_config
trial.extra_arg = extra_arg
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider instead making these fields native to trial.

trial.serialize_field_to_hex("results")
trial.serialize_field_to_hex("best_result")
trial.serialize_field_to_hex("param_config")
trial.serialize_field_to_hex("extra_arg")

trials.append(trial)
self._running_trials[trial.trial_id] = trial
Expand Down
26 changes: 11 additions & 15 deletions python/ray/tune/trial.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ def __init__(self,
self.error_file = None
self.num_failures = 0

self._nonjson_fields = [
"_checkpoint", "config", "loggers", "sync_function", "last_result"
]

self.trial_name = None
if trial_name_creator:
self.trial_name = trial_name_creator(self)
Expand Down Expand Up @@ -476,6 +480,10 @@ def set_verbose(self, verbose):
def is_finished(self):
return self.status in [Trial.TERMINATED, Trial.ERROR]

def serialize_field_to_hex(self, field):
"""Adds a field of self to whitelist when serializing."""
self._nonjson_fields += [field]

def __repr__(self):
return str(self)

Expand Down Expand Up @@ -509,17 +517,8 @@ def __getstate__(self):
state = self.__dict__.copy()
state["resources"] = resources_to_json(self.resources)

# These are non-pickleable entries.
pickle_data = {
"_checkpoint": self._checkpoint,
"config": self.config,
"loggers": self.loggers,
"sync_function": self.sync_function,
"last_result": self.last_result
}

for key, value in pickle_data.items():
state[key] = binary_to_hex(cloudpickle.dumps(value))
for key in self._nonjson_fields:
state[key] = binary_to_hex(cloudpickle.dumps(state.get(key)))

state["runner"] = None
state["result_logger"] = None
Expand All @@ -535,10 +534,7 @@ def __getstate__(self):
def __setstate__(self, state):
logger_started = state.pop("__logger_started__")
state["resources"] = json_to_resources(state["resources"])
for key in [
"_checkpoint", "config", "loggers", "sync_function",
"last_result"
]:
for key in self._nonjson_fields:
state[key] = cloudpickle.loads(hex_to_binary(state[key]))

self.__dict__.update(state)
Expand Down