Skip to content

Add backend to nums.init. #295

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 nums/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,14 @@ def init(
address: Optional[str] = None,
num_cpus: Optional[int] = None,
cluster_shape: Optional[tuple] = None,
backend: Optional[str] = None,
):
# pylint: disable = import-outside-toplevel
import nums.core.settings as settings

if backend is not None:
assert backend in {"serial", "ray", "dask", "mpi"}
Copy link
Member

Choose a reason for hiding this comment

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

We should raise a ValueError instead of an AssertionError. From the Google style guide:

Do not use assert statements for validating argument values of a public API. assert is used to ensure internal correctness, not to enforce correct usage nor to indicate that some unexpected event occurred.

(We use Black, but this is still applicable)

Copy link
Member

Choose a reason for hiding this comment

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

We should also include a descriptive error message if the users inputs an incorrect value. e.g., f"Expected backend to be one of {BACKEND_NAMES}, but got {backend}."

settings.backend_name = backend
if cluster_shape is not None:
settings.cluster_shape = cluster_shape
settings.num_cpus = num_cpus
Expand Down
17 changes: 17 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,23 @@
# pylint: disable=import-outside-toplevel


def test_init():
import nums
from nums.core import application_manager
from nums.core.backends.serial import SerialBackend
from nums.core.backends.ray import RayBackend

nums.init(backend="serial")
instance = application_manager.instance()
assert isinstance(instance.km.backend, SerialBackend)
application_manager.destroy()

nums.init(backend="ray")
instance = application_manager.instance()
assert isinstance(instance.km.backend, RayBackend)
application_manager.destroy()


def test_rwd():
import nums
from nums.core import application_manager
Expand Down