From a3c8488012f02aa4795c9a5270452d2f8d5c3b80 Mon Sep 17 00:00:00 2001 From: Andreea Popescu Date: Sun, 19 Jan 2025 03:28:31 +0000 Subject: [PATCH] wip --- pallets/subtensor/src/epoch/math.rs | 23 ++++++++++++++++++++--- pallets/subtensor/src/epoch/run_epoch.rs | 8 ++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/pallets/subtensor/src/epoch/math.rs b/pallets/subtensor/src/epoch/math.rs index 57d0f6b6f..4c47d77b2 100644 --- a/pallets/subtensor/src/epoch/math.rs +++ b/pallets/subtensor/src/epoch/math.rs @@ -1252,14 +1252,31 @@ pub fn mat_ema_alpha_vec( let one_minus_alpha = I32F32::from_num(1.0).saturating_sub(alpha_val); // Compute the EMA for the current element using saturating operations. + // B_ema = clamped_alpha * B + (1 - clamped_alpha) * B_old if let (Some(new_val), Some(old_val), Some(result_val)) = ( new_row.get(j), old_row.get(j), result.get_mut(i).and_then(|row| row.get_mut(j)), ) { - *result_val = alpha_val - .saturating_mul(*new_val) - .saturating_add(one_minus_alpha.saturating_mul(*old_val)); + // *result_val = purchase_increment.saturating_add(decayed_val); + + let decayed_val = one_minus_alpha.saturating_mul(*old_val); + let remaining_capacity = I32F32::from_num(1.0) + .saturating_sub(decayed_val) + .max(I32F32::from_num(0.0)); + + // Each validator can increase bonds by at most clamped_alpha per epoch towards the cap + // Validators allocate their purchase across miners based on weights + let purchase_increment = alpha_val.saturating_mul(*new_val); + + // Ensure that purchase does not exceed remaining capacity + let purchase = purchase_increment.min(remaining_capacity); + + // B = B_decayed + purchase + // B = torch.clamp(B, max=1.0) + *result_val = decayed_val + .saturating_add(purchase) + .min(I32F32::from_num(1.0)); } } } diff --git a/pallets/subtensor/src/epoch/run_epoch.rs b/pallets/subtensor/src/epoch/run_epoch.rs index be06ed59f..4789b1ba4 100644 --- a/pallets/subtensor/src/epoch/run_epoch.rs +++ b/pallets/subtensor/src/epoch/run_epoch.rs @@ -162,22 +162,29 @@ impl Pallet { // ========================= // Access network bonds. + // old bonds let mut bonds: Vec> = Self::get_bonds(netuid); inplace_mask_matrix(&outdated, &mut bonds); // mask outdated bonds inplace_col_normalize(&mut bonds); // sum_i b_ij = 1 log::trace!("B:\n{:?}\n", &bonds); // Compute bonds delta column normalized. + // bonds delta let mut bonds_delta: Vec> = row_hadamard(&weights, &active_stake); // ΔB = W◦S inplace_col_normalize(&mut bonds_delta); // sum_i b_ij = 1 log::trace!("ΔB:\n{:?}\n", &bonds_delta); + + // Compute the Exponential Moving Average (EMA) of bonds. let mut ema_bonds = Self::compute_ema_bonds(netuid, consensus.clone(), bonds_delta, bonds); inplace_col_normalize(&mut ema_bonds); // sum_i b_ij = 1 log::trace!("emaB:\n{:?}\n", &ema_bonds); // Compute dividends: d_i = SUM(j) b_ij * inc_j + // # === Dividend Calculation=== + // D = (B_ema * I).sum(dim=1) let mut dividends: Vec = matmul_transpose(&ema_bonds, &incentive); + // D_normalized = D / (D.sum() + 1e-6) inplace_normalize(&mut dividends); log::trace!("D:\n{:?}\n", ÷nds); @@ -1154,6 +1161,7 @@ impl Pallet { ); // Compute the alpha values using the logistic function parameters. + // alpha = 1 / (1 + math.e ** (-a * C + b)) # alpha to the old weight let alpha = Self::compute_alpha_values(&consensus, a, b); // Clamp the alpha values between alpha_high and alpha_low.