Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
andreea-popescu-reef committed Jan 19, 2025
1 parent 55410a3 commit a3c8488
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
23 changes: 20 additions & 3 deletions pallets/subtensor/src/epoch/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions pallets/subtensor/src/epoch/run_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,22 +162,29 @@ impl<T: Config> Pallet<T> {
// =========================

// Access network bonds.
// old bonds
let mut bonds: Vec<Vec<I32F32>> = 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<Vec<I32F32>> = 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<I32F32> = matmul_transpose(&ema_bonds, &incentive);
// D_normalized = D / (D.sum() + 1e-6)
inplace_normalize(&mut dividends);
log::trace!("D:\n{:?}\n", &dividends);

Expand Down Expand Up @@ -1154,6 +1161,7 @@ impl<T: Config> Pallet<T> {
);

// 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.
Expand Down

0 comments on commit a3c8488

Please sign in to comment.