opentensor / subtensor

Bittensor Blockchain Layer
The Unlicense
145 stars 149 forks source link

Consensus Based Weights (Liquid Alpha) #417

Closed distributedstatemachine closed 3 months ago

distributedstatemachine commented 4 months ago

Description

We need to modify the calculation of the exponential moving average (EMA) of bonds in our blockchain system to make it more responsive to the current consensus. Currently, the EMA calculation uses a static alpha value derived from a predefined bonds moving average. The proposed change involves using a dynamic alpha value based on the consensus values, which are calculated during each epoch. This dynamic alpha will adjust more quickly to changes in network conditions, potentially leading to a more adaptive and responsive system.

Acceptance Criteria

Tasks

    pub fn mat_ema_alpha_vec_sparse(
        new: &Vec<Vec<(u16, I32F32)>>,
        old: &Vec<Vec<(u16, I32F32)>>,
        alpha: &Vec<I32F32>
    ) -> Vec<Vec<(u16, I32F32)>> {
        assert!(new.len() == old.len());
        let n = new.len(); // assume square matrix, rows=cols
        let zero: I32F32 = I32F32::from_num(0.0);
        let mut result: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
        for i in 0..new.len() {
            let mut row: Vec<I32F32> = vec![zero; n];
            for (j, value) in new[i].iter() {
                let alpha_val: I32F32 = alpha[*j as usize];
                row[*j as usize] += alpha_val * value;
            }
            for (j, value) in old[i].iter() {
                let one_minus_alpha: I32F32 = I32F32::from_num(1.0) - alpha[*j as usize];
                row[*j as usize] += one_minus_alpha * value;
            }
            for (j, value) in row.iter().enumerate() {
                if *value > zero {
                    result[i].push((j as u16, *value))
                }
            }
        }
        result
    }
}
impl<T: Config> Pallet<T> {
    pub fn epoch(netuid: u16, rao_emission: u64) -> Vec<(T::AccountId, u64, u64)> {
        ...
        let consensus: Vec<I32F32> = weighted_median_col_sparse(&active_stake, &weights, n, kappa);
        log::trace!("C: {:?}", &consensus);

        let mut ema_bonds: Vec<Vec<(u16, I32F32)>>;
        if LiquidAlphaOn::<T>::get(netuid) {
            let alpha: Vec<I32F32> = consensus
                .iter()
                .map(|c: &I32F32| I32F32::from_num(1.0) - c)
                .collect();
            ema_bonds = mat_ema_alpha_vec_sparse(&bonds_delta, &bonds, &alpha);
        } else {
            let alpha: I32F32 = I32F32::from_num(1.0) - I32F32::from_num(bonds_moving_average);
            ema_bonds = mat_ema(&bonds_delta, &bonds, alpha);
        }
        ...
    }

Related Links

Mananitade commented 4 months ago

Regarding "The dynamic alpha for each bond should be calculated as 1.0 - consensus_value for that bond": what about the case where 1.0 - consensus_value gives a negative value? Based on my reading of the YC2 paper, the consensus "\bar W_j" can take values above 1.