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

Set device fails when used after set backend #374

Closed
stavros11 opened this issue Apr 5, 2021 · 4 comments
Closed

Set device fails when used after set backend #374

stavros11 opened this issue Apr 5, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@stavros11
Copy link
Member

Describe the bug
qibo.set_device() does not have the expected behavior when used in the same script with qibo.set_backend().

To Reproduce

import qibo
qibo.set_backend("custom")
qibo.set_device("/CPU:0")
print(qibo.get_device())

will print "/GPU:0" if executed in a machine with GPU enabled, as if the qibo.set_device instruction is ignored. If set_backend is not used then set_device works as intended.

@stavros11 stavros11 added the bug Something isn't working label Apr 5, 2021
@scarrazza
Copy link
Member

I can reproduce this issue, and this sounds like the _CONSTRUCTED_BACKENDS and K are not in sync.

@scarrazza
Copy link
Member

A simpler approach could be this: https://docs.python.org/3/faq/programming.html#id11

But probably will break many things, including user custom code.

@scarrazza
Copy link
Member

scarrazza commented Apr 6, 2021

Here some short version of the "singleton" approach we discussed today.

We place the following code in the __init__.py where my_backend_object is one of our backend inherited classes (e.g. TensorflowBackend):

class backend:
    my_backend_object = 0
    _instance = None

    def __new__(cls, *args, **kwargs):
        if cls._instance is None:
            cls._instance = super(backend, cls).__new__(
                cls, *args, **kwargs)
        return cls._instance

    def __repr__(self):
        return repr(self.my_backend_object)


K = backend()

Then we can import K as usual and get the right object. @stavros11 what do you think?

@stavros11
Copy link
Member Author

Thanks for the details on this. I actually just pushed an update in #370 which I think is very close to this idea, excluding the __new__ method which I think can be easily added. Please have a look and if you agree I can continue with this approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants