Skip to content

[14/n] tensor engine {handle_cast, handle} -> handle #484

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 1 commit into
base: gh/zdevito/29/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
2 changes: 1 addition & 1 deletion hyperactor_mesh/src/actor_mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ pub(crate) mod test_util {
cx: &Context<Self>,
GetRank(ok, reply): GetRank,
) -> Result<(), anyhow::Error> {
let (rank, _) = cx.cast_info()?;
let (rank, _) = cx.cast_info();
reply.send(cx, rank)?;
anyhow::ensure!(ok, "intentional error!"); // If `!ok` exit with `Err()`.
Ok(())
Expand Down
34 changes: 11 additions & 23 deletions hyperactor_mesh/src/comm/multicast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,32 +196,20 @@ pub fn set_cast_info_on_headers(headers: &mut Attrs, rank: usize, shape: Shape,
}

pub trait CastInfo {
/// Get the cast rank and cast shape, returning an error
/// if the relevant info isn't available.
fn cast_info(&self) -> anyhow::Result<(usize, Shape)>;

/// Get the cast rank and cast shape, returning None
/// if the relevant info isn't available.
fn maybe_cast_info(&self) -> Option<(usize, Shape)>;
/// Get the cast rank and cast shape.
/// If something wasn't explicitly sent via a cast, then
/// we represent it as the only member of a 0-dimensonal cast shape,
/// which is the same as a singleton.
fn cast_info(&self) -> (usize, Shape);
}

impl<A: Actor> CastInfo for Context<'_, A> {
fn cast_info(&self) -> anyhow::Result<(usize, Shape)> {
fn cast_info(&self) -> (usize, Shape) {
let headers = self.headers();
let rank = headers
.get(CAST_RANK)
.ok_or_else(|| anyhow::anyhow!("{} not found in headers", CAST_RANK.name()))?;
let shape = headers
.get(CAST_SHAPE)
.ok_or_else(|| anyhow::anyhow!("{} not found in headers", CAST_SHAPE.name()))?
.clone();
Ok((*rank, shape))
}

fn maybe_cast_info(&self) -> Option<(usize, Shape)> {
let headers = self.headers();
headers
.get(CAST_RANK)
.map(|rank| headers.get(CAST_SHAPE).map(|shape| (*rank, shape.clone())))?
match (headers.get(CAST_RANK), headers.get(CAST_SHAPE)) {
(Some(rank), Some(shape)) => (*rank, shape.clone()),
(None, None) => (0, Shape::unity()),
_ => panic!("Expected either both rank and shape or neither"),
}
}
}
44 changes: 16 additions & 28 deletions monarch_hyperactor/src/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use hyperactor::message::Unbind;
use hyperactor_mesh::comm::multicast::CastInfo;
use monarch_types::PickledPyObject;
use monarch_types::SerializablePyErr;
use ndslice::Shape;
use pyo3::conversion::IntoPyObjectExt;
use pyo3::exceptions::PyBaseException;
use pyo3::exceptions::PyRuntimeError;
Expand Down Expand Up @@ -408,34 +409,21 @@ impl Handler<PythonMessage> for PythonActor {
let (sender, receiver) = oneshot::channel();

let future = Python::with_gil(|py| -> Result<_, SerializablePyErr> {
let awaitable = match cx.maybe_cast_info() {
Some((rank, shape)) => self.actor.call_method(
py,
"handle_cast",
(
mailbox,
rank,
PyShape::from(shape),
message,
PanicFlag {
sender: Some(sender),
},
),
None,
)?,
None => self.actor.call_method(
py,
"handle",
(
mailbox,
message,
PanicFlag {
sender: Some(sender),
},
),
None,
)?,
};
let (rank, shape) = cx.cast_info();
let awaitable = self.actor.call_method(
py,
"handle",
(
mailbox,
rank,
PyShape::from(shape),
message,
PanicFlag {
sender: Some(sender),
},
),
None,
)?;

pyo3_async_runtimes::into_future_with_locals(
self.get_task_locals(py),
Expand Down
2 changes: 1 addition & 1 deletion monarch_tensor_worker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl Handler<AssignRankMessage> for WorkerActor {
cx: &hyperactor::Context<Self>,
_: AssignRankMessage,
) -> anyhow::Result<()> {
let (rank, shape) = cx.cast_info()?;
let (rank, shape) = cx.cast_info();
self.rank = rank;
self.respond_with_python_message = true;
Python::with_gil(|py| {
Expand Down
3 changes: 0 additions & 3 deletions python/monarch/_rust_bindings/monarch_hyperactor/actor.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,6 @@ class PanicFlag:

class Actor(Protocol):
async def handle(
self, mailbox: Mailbox, message: PythonMessage, panic_flag: PanicFlag
) -> None: ...
async def handle_cast(
self,
mailbox: Mailbox,
rank: int,
Expand Down
5 changes: 0 additions & 5 deletions python/monarch/_src/actor/actor_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,11 +571,6 @@ def __init__(self) -> None:
self.instance: object | None = None

async def handle(
self, mailbox: Mailbox, message: PythonMessage, panic_flag: PanicFlag
) -> None:
return await self.handle_cast(mailbox, 0, singleton_shape, message, panic_flag)

async def handle_cast(
self,
mailbox: Mailbox,
rank: int,
Expand Down
5 changes: 0 additions & 5 deletions python/tests/_monarch/test_hyperactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@

class MyActor:
async def handle(
self, mailbox: Mailbox, message: PythonMessage, panic_flag: PanicFlag
) -> None:
raise NotImplementedError()

async def handle_cast(
self,
mailbox: Mailbox,
rank: int,
Expand Down
5 changes: 0 additions & 5 deletions python/tests/_monarch/test_mailbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,6 @@ async def recv_message() -> str:

class MyActor:
async def handle(
self, mailbox: Mailbox, message: PythonMessage, panic_flag: PanicFlag
) -> None:
return None

async def handle_cast(
self,
mailbox: Mailbox,
rank: int,
Expand Down