Skip to main content

PythonProcess

Struct PythonProcess 

Source
pub struct PythonProcess { /* private fields */ }
Expand description

A persistent Python child process that executes filter commands.

Implementations§

Source§

impl PythonProcess

Source

pub fn spawn( python_path: &str, filters: &[(String, Vec<u8>, bool)], ) -> Result<Self>

Spawn a Python daemon and load filters into it.

Source

pub fn fit( &mut self, node_id: &str, data: &Value, y: Option<&Value>, ) -> Result<Value>

Fit the filter loaded under node_id on data (and optional labels y), returning what its fit returned — the trained state.

Source

pub fn forward( &mut self, node_id: &str, data: &Value, state: &Value, ) -> Result<Value>

Run the filter’s forward on data with a previously trained state, returning its output.

Source

pub fn composite_fit( &mut self, node_ids: &[String], data: &Value, y: Option<&Value>, ) -> Result<(Value, HashMap<String, Value>)>

Fit a chain of filters in one command: each trainable filter fits, then forwards to feed the next; if torch is importable the daemon follows with one autograd forward/backward pass over the chain. Returns the chain’s output plus each node’s serialized state (torch state_dict bytes when available, cloudpickle otherwise). One round-trip — intermediate values never cross the process boundary, and the autograd graph stays whole.

Source

pub fn batched_fit( &mut self, node_ids: &[String], data: &Value, y: Option<&Value>, batch_size: usize, ) -> Result<(Value, HashMap<String, Value>)>

Batched fit: send full dataset + batch_size, daemon splits internally. Model loaded ONCE, batches processed in a loop.

Source

pub fn composite_forward( &mut self, node_ids: &[String], data: &Value, ) -> Result<Value>

Forward data through a chain of filters, in order, inside one command — the composite counterpart of PythonProcess::forward.

Source

pub fn get_state(&mut self, node_id: &str) -> Result<Value>

Extract one filter’s state.

A materialized DifferentiableFilter answers with its own state convention — Value::Json({"weights_b64": …}), the dict its forward reads back and the local fit path writes — so a state read off a worker is loadable by a local graph. Anything else is opaque bytes: a torch state_dict when the filter has one, the cloudpickled filter otherwise.

Source

pub fn set_state(&mut self, node_id: &str, state: &Value) -> Result<()>

Load what PythonProcess::get_state produced back into the filter — how FedAvg-style aggregated states reach a worker.

Mirrors get_state in both of its forms: a Value::Json state goes as-is (and {"weights_b64": …} is loaded into the filter’s module), bytes go base64. Anything else is an encoding error.

Source

pub fn get_gradients(&mut self, node_id: &str) -> Result<Value>

Collect the filter’s current gradients, one nested list per named parameter, for AllReduce aggregation.

Returns Value::Json({param_name: nested list}) rather than the torch pickle this used to send. The aggregator is in Rust ([somatize_runtime::strategy]), and a pickle is opaque to it: the average of two Value::Bytes blobs is not a thing that can be computed, so the round died at the aggregation step having done all the work. Plain JSON also makes the average independent of the torch version each worker happens to have installed.

A filter with no parameters, or no gradient on them, is an error rather than Value::Empty. Returning empty meant the average was taken over nothing and applied as nothing, and the round reported success — a data-parallel step that trained no one. The daemon says which of the three it is; this passes that on.

Source

pub fn apply_gradients( &mut self, node_id: &str, gradients: &Value, ) -> Result<()>

Hand aggregated gradients (the post-AllReduce mean of what PythonProcess::get_gradients returned) to the filter: the daemon writes them onto the matching parameters and steps the optimizer, so the replica actually moves.

Source

pub fn shutdown(&mut self)

Ask the daemon to exit its command loop. Best-effort — the reply is ignored, and Drop kills the child regardless.

Source

pub fn node_ids(&self) -> &[String]

The node ids of the filters loaded into this process, in load order.

Trait Implementations§

Source§

impl Drop for PythonProcess

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AsAny for T
where T: Any,

§

fn as_any(&self) -> &(dyn Any + 'static)

The receiver as &dyn Any, ready for downcast_ref.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,