Consensus Based Weights (Liquid Alpha)
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
- Implement a conditional EMA calculation that switches between a static and a dynamic alpha based on a configuration flag (
LiquidAlphaOn). - The dynamic alpha for each bond should be calculated as
1.0 - consensus_valuefor that bond. - Ensure that the new method handles sparse matrix formats efficiently.
- Integrate this new method into the existing epoch processing logic.
- Optionally, modify the
btcli s listcommand to display the alpha value used in the last EMA calculation for transparency and debugging.
Tasks
-
[ ] Add a configuration flag
LiquidAlphaOnto enable or disable dynamic alpha calculations. -
[ ] Implement the new
mat_ema_alpha_vec_sparsefunction to handle dynamic alpha values.
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
}
}
- [ ] Modify the
epochfunction to use the new EMA calculation method based on theLiquidAlphaOnflag.
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);
}
...
}
- [ ] Optionally, modify the
btcli s listcommand to show the alpha value.
Related Links
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.