Skip to main content

Tree

Trait Tree 

Source
pub trait Tree:
    Leaves
    + Clone
    + 'static {
    // Required method
    fn visit_mut(&mut self, v: &mut VisitMut<'_>);

    // Provided methods
    fn for_each(&self, f: impl FnMut(&str, &Array)) { ... }
    fn map(&self, f: impl FnMut(&str, &Array) -> Array) -> Self { ... }
    fn leaves(&self) -> Vec<Array> { ... }
    fn paths(&self) -> Vec<String> { ... }
    fn flatten(&self) -> Vec<(String, Array)> { ... }
    fn unflatten(
        &self,
        leaves: impl IntoIterator<Item = Array>,
    ) -> Result<Self, Error> { ... }
    fn load_flat(
        &mut self,
        named: impl IntoIterator<Item = (String, Array)>,
        mode: LoadMode,
    ) -> Result<(), Error> { ... }
    fn len(&self) -> usize { ... }
    fn is_empty(&self) -> bool { ... }
    fn def(&self) -> TreeDef { ... }
}
Expand description

An owned tree of arrays: rebuilt with new leaves of the same structure, mutated in place.

Implemented for Array, Param, (), Option<T>, Box<T>, Vec<T>, [T; N], tuples up to 8 elements, BTreeMap<String, T> and HashMap<String, T> (visited in sorted key order), and user types with #[derive(Tree)].

Required Methods§

Source

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Reports every leaf (and child tree and mode flag) to v mutably, in the order of Leaves::visit.

Provided Methods§

Source

fn for_each(&self, f: impl FnMut(&str, &Array))

Calls f(path, leaf) for every leaf, in order.

Source

fn map(&self, f: impl FnMut(&str, &Array) -> Array) -> Self

The tree with every leaf replaced by f(path, leaf) (same structure; parameters keep their frozen flag).

Source

fn leaves(&self) -> Vec<Array>

The leaves, in order.

Source

fn paths(&self) -> Vec<String>

The leaves’ paths, in order.

Source

fn flatten(&self) -> Vec<(String, Array)>

The leaves with their paths, in order (MLX’s tree_flatten).

Source

fn unflatten( &self, leaves: impl IntoIterator<Item = Array>, ) -> Result<Self, Error>

A tree of this structure (self is the template) with leaves in order: exactly Tree::len of them, else an error naming both counts.

Source

fn load_flat( &mut self, named: impl IntoIterator<Item = (String, Array)>, mode: LoadMode, ) -> Result<(), Error>

Replaces leaves by path. LoadMode::Strict requires exactly the tree’s paths with matching shapes; LoadMode::Partial assigns the matching names and ignores unknown ones (shapes must still match). Loaded dtypes are kept. On any problem the error lists every one (missing, unknown and mismatched names, sorted) and the tree is unchanged. Duplicate paths (in the tree or in named) are an error.

Source

fn len(&self) -> usize

The number of leaves.

Source

fn is_empty(&self) -> bool

Whether the tree has no leaves.

Source

fn def(&self) -> TreeDef

The tree’s structure.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Tree for ()

Source§

fn visit_mut(&mut self, _: &mut VisitMut<'_>)

Source§

impl Tree for Box<dyn Optimizer>

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<A, B, C, D, E, F, G, H> Tree for (A, B, C, D, E, F, G, H)
where A: Tree, B: Tree, C: Tree, D: Tree, E: Tree, F: Tree, G: Tree, H: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<A, B, C, D, E, F, G> Tree for (A, B, C, D, E, F, G)
where A: Tree, B: Tree, C: Tree, D: Tree, E: Tree, F: Tree, G: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<A, B, C, D, E, F> Tree for (A, B, C, D, E, F)
where A: Tree, B: Tree, C: Tree, D: Tree, E: Tree, F: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<A, B, C, D, E> Tree for (A, B, C, D, E)
where A: Tree, B: Tree, C: Tree, D: Tree, E: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<A, B, C, D> Tree for (A, B, C, D)
where A: Tree, B: Tree, C: Tree, D: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<A, B, C> Tree for (A, B, C)
where A: Tree, B: Tree, C: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<A, B> Tree for (A, B)
where A: Tree, B: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<A> Tree for (A,)
where A: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<T, S> Tree for HashMap<String, T, S>
where T: Tree, S: BuildHasher + Clone + 'static,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<T, const N: usize> Tree for [T; N]
where T: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<T> Tree for BTreeMap<String, T>
where T: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<T> Tree for Box<T>
where T: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

impl<T> Tree for Option<T>
where T: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Source§

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

Source§

fn visit_mut(&mut self, _: &mut VisitMut<'_>)

Source§

impl<T> Tree for Vec<T>
where T: Tree,

Source§

fn visit_mut(&mut self, v: &mut VisitMut<'_>)

Implementors§

Source§

impl Tree for Adam
where Self: Clone,

Source§

impl Tree for AdamSlot
where Self: Clone,

Source§

impl Tree for AdamW
where Self: Clone,

Source§

impl Tree for Array

Source§

impl Tree for Dropout
where Self: Clone,

Source§

impl Tree for Embedding
where Self: Clone,

Source§

impl Tree for LayerNorm
where Self: Clone,

Source§

impl Tree for Linear
where Self: Clone,

Source§

impl Tree for Lion
where Self: Clone,

Source§

impl Tree for LoraBase
where Self: Clone,

Source§

impl Tree for LoraLinear
where Self: Clone,

Source§

impl Tree for MomentumSlot
where Self: Clone,

Source§

impl Tree for MultiOptimizer
where Self: Clone,

Source§

impl Tree for Muon
where Self: Clone,

Source§

impl Tree for Param

Source§

impl Tree for QuantizedEmbedding
where Self: Clone,

Source§

impl Tree for QuantizedLinear
where Self: Clone,

Source§

impl Tree for RmsNorm
where Self: Clone,

Source§

impl Tree for Rope
where Self: Clone,

Source§

impl Tree for Sgd
where Self: Clone,

Source§

impl Tree for SgdSlot
where Self: Clone,

Source§

impl<S> Tree for OptimizerState<S>
where BTreeMap<String, S>: Tree, S: 'static + Tree, Self: Clone,