Skip to content

[monarch] implement __len__ for MeshTrait #729

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 3 commits into
base: gh/suo/71/base
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
3 changes: 0 additions & 3 deletions python/monarch/_src/actor/actor_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,9 +444,6 @@ def items(self) -> Iterable[Tuple[Point, R]]:
def __iter__(self) -> Iterator[Tuple[Point, R]]:
return iter(self.items())

def __len__(self) -> int:
return len(self._shape)

def __repr__(self) -> str:
return f"ValueMesh({self._shape})"

Expand Down
3 changes: 3 additions & 0 deletions python/monarch/_src/actor/shape.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,5 +224,8 @@ def size(self, dim: Union[None, str, Sequence[str]] = None) -> int:
def sizes(self) -> dict[str, int]:
return dict(zip(self._labels, self._ndslice.sizes))

def __len__(self) -> int:
return len(self._ndslice)


__all__ = ["NDSlice", "Shape", "MeshTrait"]
38 changes: 38 additions & 0 deletions python/tests/test_mesh_trait.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.

from typing import Iterable

from monarch._src.actor.shape import MeshTrait, NDSlice, Shape, Slice


class Mesh(MeshTrait):
"""
A simple implementor of MeshTrait.
"""

def __init__(self, shape: Shape, values: list[int]) -> None:
self._shape = shape
self._values = values

def _new_with_shape(self, shape: Shape) -> "Mesh":
return Mesh(shape, self._values)

@property
def _ndslice(self) -> NDSlice:
return self._shape.ndslice

@property
def _labels(self) -> Iterable[str]:
return self._shape.labels


def test_len() -> None:
s = Slice(offset=0, sizes=[2, 3], strides=[3, 1])
shape = Shape(["label0", "label1"], s)

mesh = Mesh(shape, [1, 2, 3, 4, 5, 6])
assert 6 == len(mesh)
5 changes: 5 additions & 0 deletions python/tests/test_python_actors.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,8 @@ async def consume():

def test_python_task_tuple() -> None:
PythonTask.from_coroutine(consume()).block_on()

def test_mesh_len():
proc_mesh = local_proc_mesh(gpus=12).get()
s = proc_mesh.spawn("sync_actor", SyncActor).get()
assert 12 == len(s)