Skip to main content

MixtureOfExperts

Struct MixtureOfExperts 

Source
pub struct MixtureOfExperts<E, G>
where E: Estimator, G: GatingNetwork,
{ /* private fields */ }
Expand description

Mixture of Experts ensemble

Implementations§

Source§

impl<E, G> MixtureOfExperts<E, G>
where E: Estimator, G: GatingNetwork,

Source

pub fn builder() -> MoeBuilder<E, G>

Source

pub fn n_experts(&self) -> usize

Source

pub fn config(&self) -> &MoeConfig

Source

pub fn predict(&self, input: &[f32]) -> f32

Source

pub fn predict_batch(&self, inputs: &Matrix<f32>) -> Vector<f32>

Predict for a batch of inputs.

Returns predictions and optionally the expert routing decisions.

§Arguments
  • inputs - Matrix of shape [n_samples, n_features]
§Returns

Vector of predictions, one per input sample.

Source

pub fn compute_load_balance_loss(&self, inputs: &Matrix<f32>) -> f32

Compute load balancing auxiliary loss.

Encourages even distribution of inputs across experts to prevent expert collapse (all inputs routed to single expert).

Switch Transformer (Fedus et al. 2021, Eq. 4-6) auxiliary loss: Loss = alpha * N * sum_i(f_i * P_i) where:

  • N = number of experts
  • alpha = load_balance_weight
  • f_i = fraction of tokens dispatched to expert i (hard top-k)
  • P_i = MEAN router softmax probability for expert i over ALL tokens (full softmax, every expert, every token — NOT top-k-only)
§Arguments
  • inputs - Matrix of shape [n_samples, n_features]
§Returns

Load balance loss value (lower is more balanced)

Source

pub fn expert_usage(&self, inputs: &Matrix<f32>) -> Vec<f32>

Get expert usage statistics for a batch of inputs.

Returns the fraction of inputs routed to each expert.

§Arguments
  • inputs - Matrix of shape [n_samples, n_features]
§Returns

Vector of usage fractions, one per expert (sums to top_k).

Source

pub fn get_routing_weights(&self, input: &[f32]) -> Vec<f32>

Get routing weights for a single input (useful for debugging/visualization).

§Arguments
  • input - Feature vector
§Returns

Vector of gating weights for each expert.

Source§

impl<E, G> MixtureOfExperts<E, G>
where E: Estimator + Clone, G: GatingNetwork,

Source

pub fn fit( &mut self, _x: &Matrix<f32>, _y: &Vector<f32>, ) -> Result<(), AprenderError>

Fit MoE using pre-trained experts.

This is a simple two-stage training approach:

  1. Experts are assumed to be pre-trained (passed in via builder)
  2. No gating training is performed (uses initial weights)

For more sophisticated training, use separate expert training followed by MoE construction.

§Arguments
  • _x - Training features (unused in this simple implementation)
  • _y - Training labels (unused in this simple implementation)
Source§

impl<E, G> MixtureOfExperts<E, G>

Source

pub fn save<P>(&self, path: P) -> Result<(), AprenderError>
where P: AsRef<Path>,

Source

pub fn load<P>(path: P) -> Result<MixtureOfExperts<E, G>, AprenderError>
where P: AsRef<Path>,

Source

pub fn save_apr<P>(&self, path: P) -> Result<(), AprenderError>
where P: AsRef<Path>,

Trait Implementations§

Source§

impl<E, G> Debug for MixtureOfExperts<E, G>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<E, G> Freeze for MixtureOfExperts<E, G>
where Vec<E>: Freeze, G: Freeze,

§

impl<E, G> RefUnwindSafe for MixtureOfExperts<E, G>

§

impl<E, G> Send for MixtureOfExperts<E, G>
where Vec<E>: Send,

§

impl<E, G> Sync for MixtureOfExperts<E, G>
where Vec<E>: Sync,

§

impl<E, G> Unpin for MixtureOfExperts<E, G>
where Vec<E>: Unpin, G: Unpin,

§

impl<E, G> UnsafeUnpin for MixtureOfExperts<E, G>
where Vec<E>: UnsafeUnpin, G: UnsafeUnpin,

§

impl<E, G> UnwindSafe for MixtureOfExperts<E, G>
where Vec<E>: UnwindSafe, G: UnwindSafe,

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
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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,