From 9410b5668d8c4b16f01f9c1db7db32e3c6549c2d Mon Sep 17 00:00:00 2001 From: Marius Eriksen Date: Wed, 12 Nov 2025 14:25:22 -0800 Subject: [PATCH 1/3] [hyperactor] mesh: define `resource::Resource`, and mesh::Mesh in terms of it We formalize a resource controller behavior: ``` /// A trait that bundles a set of types that together define a resource. pub trait Resource { /// The spec specification for this resource. type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; /// The state for this resource. type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; } // A behavior defining the interface for a mesh controller. hyperactor::behavior!( Controller, CreateOrUpdate, GetState, Stop, ); ``` Anything that is a controller should behave-as a `resource::Controller`. We then formalize the mesh controller behavior as a specialization of a resource controller, by implementing `Resource` for any mesh: ``` impl Resource for M { type Spec = Spec; type State = State; } ``` This resolves to the same set of bindings and aliases (except we use `resource::Controller` rather than `mesh::Controller`) -- the existing behaviors continue to assert. Differential Revision: [D86905562](https://our.internmc.facebook.com/intern/diff/D86905562/) **NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D86905562/)! [ghstack-poisoned] --- hyperactor_mesh/src/resource.rs | 17 +++++++++++++++++ hyperactor_mesh/src/resource/mesh.rs | 15 +++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/hyperactor_mesh/src/resource.rs b/hyperactor_mesh/src/resource.rs index d4edb73b3..f15e054d0 100644 --- a/hyperactor_mesh/src/resource.rs +++ b/hyperactor_mesh/src/resource.rs @@ -334,6 +334,23 @@ where } } +/// A trait that bundles a set of types that together define a resource. +pub trait Resource { + /// The spec specification for this resource. + type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; + + /// The state for this resource. + type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; +} + +// A behavior defining the interface for a mesh controller. +hyperactor::behavior!( + Controller, + CreateOrUpdate, + GetState, + Stop, +); + /// RankedValues compactly represents rank-indexed values of type T. /// It stores contiguous values in a set of intervals; thus it is /// efficient and compact when the cardinality of T-typed values is diff --git a/hyperactor_mesh/src/resource/mesh.rs b/hyperactor_mesh/src/resource/mesh.rs index 736ddc9a3..58cc6df67 100644 --- a/hyperactor_mesh/src/resource/mesh.rs +++ b/hyperactor_mesh/src/resource/mesh.rs @@ -21,6 +21,7 @@ use serde::Serialize; use crate::resource::CreateOrUpdate; use crate::resource::GetState; +use crate::resource::Resource; use crate::resource::Status; use crate::resource::Stop; use crate::v1::ValueMesh; @@ -49,17 +50,14 @@ pub trait Mesh { /// The mesh-specific specification for this resource. type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; - /// The mesh-specific state for thsi resource. + /// The mesh-specific state for this resource. type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; } -// A behavior defining the interface for a mesh controller. -hyperactor::behavior!( - Controller, - CreateOrUpdate>, - GetState>, - Stop, -); +impl Resource for M { + type Spec = Spec; + type State = State; +} #[cfg(test)] mod test { @@ -68,6 +66,7 @@ mod test { use hyperactor::Handler; use super::*; + use crate::resource::Controller; // Consider upstreaming this into `hyperactor` -- lightweight handler definitions // can be quite useful. From b9d1d7ab7d1a6b6e5898d3c615837b7737302af5 Mon Sep 17 00:00:00 2001 From: Marius Eriksen Date: Wed, 12 Nov 2025 15:22:32 -0800 Subject: [PATCH 2/3] Update on "[hyperactor] mesh: define `resource::Resource`, and mesh::Mesh in terms of it" We formalize a resource controller behavior: ``` /// A trait that bundles a set of types that together define a resource. pub trait Resource { /// The spec specification for this resource. type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; /// The state for this resource. type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; } // A behavior defining the interface for a mesh controller. hyperactor::behavior!( Controller, CreateOrUpdate, GetState, Stop, ); ``` Anything that is a controller should behave-as a `resource::Controller`. We then formalize the mesh controller behavior as a specialization of a resource controller, by implementing `Resource` for any mesh: ``` impl Resource for M { type Spec = Spec; type State = State; } ``` This resolves to the same set of bindings and aliases (except we use `resource::Controller` rather than `mesh::Controller`) -- the existing behaviors continue to assert. Differential Revision: [D86905562](https://our.internmc.facebook.com/intern/diff/D86905562/) **NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D86905562/)! [ghstack-poisoned] --- hyperactor_mesh/src/resource/mesh.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hyperactor_mesh/src/resource/mesh.rs b/hyperactor_mesh/src/resource/mesh.rs index 58cc6df67..6062f75e2 100644 --- a/hyperactor_mesh/src/resource/mesh.rs +++ b/hyperactor_mesh/src/resource/mesh.rs @@ -19,11 +19,11 @@ use ndslice::Extent; use serde::Deserialize; use serde::Serialize; -use crate::resource::CreateOrUpdate; -use crate::resource::GetState; + + use crate::resource::Resource; use crate::resource::Status; -use crate::resource::Stop; + use crate::v1::ValueMesh; /// Mesh specs From dfa15b0337d66f7e86f098ecd3d8fc824008a188 Mon Sep 17 00:00:00 2001 From: Marius Eriksen Date: Wed, 12 Nov 2025 15:45:40 -0800 Subject: [PATCH 3/3] Update on "[hyperactor] mesh: define `resource::Resource`, and mesh::Mesh in terms of it" We formalize a resource controller behavior: ``` /// A trait that bundles a set of types that together define a resource. pub trait Resource { /// The spec specification for this resource. type Spec: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; /// The state for this resource. type State: Named + Serialize + for<'de> Deserialize<'de> + Send + Sync + std::fmt::Debug; } // A behavior defining the interface for a mesh controller. hyperactor::behavior!( Controller, CreateOrUpdate, GetState, Stop, ); ``` Anything that is a controller should behave-as a `resource::Controller`. We then formalize the mesh controller behavior as a specialization of a resource controller, by implementing `Resource` for any mesh: ``` impl Resource for M { type Spec = Spec; type State = State; } ``` This resolves to the same set of bindings and aliases (except we use `resource::Controller` rather than `mesh::Controller`) -- the existing behaviors continue to assert. Differential Revision: [D86905562](https://our.internmc.facebook.com/intern/diff/D86905562/) **NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D86905562/)! [ghstack-poisoned] --- hyperactor_mesh/src/resource/mesh.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/hyperactor_mesh/src/resource/mesh.rs b/hyperactor_mesh/src/resource/mesh.rs index 6062f75e2..3f463ce54 100644 --- a/hyperactor_mesh/src/resource/mesh.rs +++ b/hyperactor_mesh/src/resource/mesh.rs @@ -19,11 +19,8 @@ use ndslice::Extent; use serde::Deserialize; use serde::Serialize; - - use crate::resource::Resource; use crate::resource::Status; - use crate::v1::ValueMesh; /// Mesh specs @@ -67,6 +64,9 @@ mod test { use super::*; use crate::resource::Controller; + use crate::resource::CreateOrUpdate; + use crate::resource::GetState; + use crate::resource::Stop; // Consider upstreaming this into `hyperactor` -- lightweight handler definitions // can be quite useful.