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

[WIP] Enable Skorch+Dask-ML #5748

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions distributed/protocol/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,9 @@ def _register_cudf():
def _register_cuml():
with suppress(ImportError):
from cuml.comm import serialize


@dask_serialize.register_lazy("skorch")
@dask_deserialize.register_lazy("skorch")
def _register_skorch():
from . import skorch
28 changes: 28 additions & 0 deletions distributed/protocol/skorch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import cloudpickle
VibhuJawa marked this conversation as resolved.
Show resolved Hide resolved
import skorch
from .serialize import dask_serialize, dask_deserialize

@dask_serialize.register(skorch.NeuralNet)
def serialize_skorch(x):
VibhuJawa marked this conversation as resolved.
Show resolved Hide resolved
has_module = hasattr(x, "module_")
headers = {"has_module": has_module}
VibhuJawa marked this conversation as resolved.
Show resolved Hide resolved
if has_module:
module = x.__dict__.pop("module_")
Copy link
Member

Choose a reason for hiding this comment

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

Curious why module_ can't be pickled on its own. Is there any more info on the issues encountered by leaving this?

Also any downside to (temporarily) modifying a user-provided object here?

Copy link
Author

Choose a reason for hiding this comment

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

Curious why module_ can't be pickled on its own. Is there any more info on the issues encountered by leaving this?

So module's is an interactively defined class on client so its namespace is often __main__ . For eg. __main__. MyModule.

Pickle has problems pickling when interactively defined classes when they are set as an attributes of another object. as it tries to look up the class in the namespace. See eg for trace.
By pickling it on its own we are able to serialize successfully

Also any downside to (temporarily) modifying a user-provided object here?

The only side effect i can think of is if the class is redefined in the worker's name-space causing undefined behavior while de-serializing on the worker. I doubt that will really happen in real workflows.

FWIW, I have added a test to verify that at-least for the class is the same after deserialization.

Copy link
Member

Choose a reason for hiding this comment

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

Is this just an issue with pickle? Does cloudpickle run into this issue or does it work ok?

try:
VibhuJawa marked this conversation as resolved.
Show resolved Hide resolved
frames = [cloudpickle.dumps(x)]
frames = frames + [cloudpickle.dumps(module)]
VibhuJawa marked this conversation as resolved.
Show resolved Hide resolved
finally:
x.__dict__["module_"] = module
else:
frames = [cloudpickle.dumps(x)]
VibhuJawa marked this conversation as resolved.
Show resolved Hide resolved

return headers, frames


@dask_deserialize.register(skorch.NeuralNet)
def deserialize_skorch(header, frames):
model = cloudpickle.loads(frames[0])
if header["has_module"]:
module = cloudpickle.loads(frames[1])
VibhuJawa marked this conversation as resolved.
Show resolved Hide resolved
model.module_ = module
return model