Giter Site home page Giter Site logo

Dynamically modifying the `.electron_object.executor` from an imported electron does not always work as expected about covalent HOT 5 OPEN

Andrew-S-Rosen avatar Andrew-S-Rosen commented on June 19, 2024
Dynamically modifying the `.electron_object.executor` from an imported electron does not always work as expected

from covalent.

Comments (5)

santoshkumarradha avatar santoshkumarradha commented on June 19, 2024 1

Thanks for this @Andrew-S-Rosen ideally it should be so that all kwargs of electrons are properties as well that can be set post creation. I will update the issue as a bug/feature-enhancement as well.

Cc: @kessler-frost / @jimmylism

from covalent.

santoshkumarradha avatar santoshkumarradha commented on June 19, 2024

CC: @cjao , any quick idea on why this would be the case for packages?

from covalent.

wjcunningham7 avatar wjcunningham7 commented on June 19, 2024

@Andrew-S-Rosen I think this may be due to how the objects are imported: https://stackoverflow.com/a/3536638

from covalent.

cjao avatar cjao commented on June 19, 2024

@Andrew-S-Rosen This is a fun question and boils down to how cloudpickle (Covalent's serialization mechanism) works. The key difference between your examples

(1) non-working

import covalent as ct
from mypackage import increment, subflow

@ct.lattice
def workflow(x, op):
    return subflow(x, op)

increment.electron_object.executor = "local"

ct.dispatch(workflow)(1, increment)

(2) working

import covalent as ct
from mypackage import increment

@ct.lattice
def workflow(x, op):
    return op(x)

increment.electron_object.executor = "local"

ct.dispatch(workflow)(1, increment)

is that (1) pickles the wrapped increment function -- as a parameter of the sub_workflow electron -- whereas in (2) only the underlying function is serialized, not the electron_object attribute. Because increment lives in an external module instead of __main__, Cloudpickle will serialize it by reference, not by value. Thus when the increment electron is unpickled in the executor that builds out sub_workflow, its attributes -- such as electron_object -- are whatever they are defined to be in mypackage.py.

Example:

mypackage.py

class Electron:
    def __init__(self):
        self.executor = "dask"

def my_func():
    pass

my_func.electron_object = Electron()

main.py

import cloudpickle
import mypackage

mypackage.my_func.electron_object.executor = "local"

with open("my_func.pkl", "wb") as f:
    cloudpickle.dump(mypackage.my_func, f)

exec.py

import pickle

with open("my_func.pkl", "rb") as f:
    my_func = pickle.load(f)
    print(my_func.electron_object.executor)
casey@AgnostiqHQ tests$ python main.py  && python exec.py
dask

But now if we instruct cloudpickle to serialize the mypackage module by value:

main.py

import cloudpickle
import mypackage

mypackage.my_func.electron_object.executor = "local"

cloudpickle.register_pickle_by_value(mypackage)

with open("my_func.pkl", "wb") as f:
    cloudpickle.dump(mypackage.my_func, f)

we get

casey@AgnostiqHQ tests$ python main.py  && python exec.py
local

from covalent.

Andrew-S-Rosen avatar Andrew-S-Rosen commented on June 19, 2024

@wjcunningham7, @cjao: Thank you both for the very insightful replies!

Because increment lives in an external module instead of main, Cloudpickle will serialize it by reference, not by value.

That's the main takeaway for sure. I didn't realize that --- subtle! In any case, this isn't a major issue, but I did want to flag it as a potential "gotcha." With this response, I feel pretty comfortable knowing what's going on now. Feel free to close if you wish!

from covalent.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.