Skip to content

Commit 794824a

Browse files
yuma bonds scale individually for sparse
1 parent 09fe03e commit 794824a

File tree

2 files changed

+34
-84
lines changed

2 files changed

+34
-84
lines changed

pallets/subtensor/src/epoch/math.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,20 @@ pub fn mat_ema_alpha_vec_sparse(
12011201
let one_minus_alpha: I32F32 = I32F32::from_num(1.0).saturating_sub(alpha_val);
12021202
// Compute the EMA component for the old value and add it to the row using saturating operations.
12031203
if let Some(row_val) = row.get_mut(*j as usize) {
1204-
*row_val = row_val.saturating_add(one_minus_alpha.saturating_mul(*value));
1204+
let decayed_val = one_minus_alpha.saturating_mul(*value);
1205+
let remaining_capacity = I32F32::from_num(1.0)
1206+
.saturating_sub(decayed_val)
1207+
.max(I32F32::from_num(0.0));
1208+
// Each validator can increase bonds by at most clamped_alpha per epoch towards the cap
1209+
// Validators allocate their purchase across miners based on weights
1210+
let purchase_increment = alpha_val.saturating_mul(*row_val);
1211+
1212+
// Ensure that purchase does not exceed remaining capacity
1213+
let purchase = purchase_increment.min(remaining_capacity);
1214+
1215+
*row_val = decayed_val
1216+
.saturating_add(purchase)
1217+
.min(I32F32::from_num(1.0));
12051218
}
12061219
log::trace!(
12071220
"old[{}][{}] * (1 - alpha[{}]) = {} * {} = {}",

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 20 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ impl<T: Config> Pallet<T> {
179179
// Compute the EMA of bonds using a normal alpha value.
180180
Self::compute_ema_bonds_normal(&weights.clone(), &bonds, netuid)
181181
};
182-
182+
// Normalize EMA bonds.
183183
inplace_col_normalize(&mut ema_bonds); // sum_i b_ij = 1
184184
log::trace!("emaB:\n{:?}\n", &ema_bonds);
185185

@@ -530,24 +530,30 @@ impl<T: Config> Pallet<T> {
530530
inplace_col_normalize_sparse(&mut bonds, n);
531531
log::trace!("B (mask+norm): {:?}", &bonds);
532532

533-
// Compute bonds delta column normalized.
534-
let mut bonds_delta: Vec<Vec<(u16, I32F32)>> = row_hadamard_sparse(&weights, &active_stake); // ΔB = W◦S (outdated W masked)
535-
log::trace!("ΔB: {:?}", &bonds_delta);
536-
537-
// Normalize bonds delta.
538-
inplace_col_normalize_sparse(&mut bonds_delta, n); // sum_i b_ij = 1
539-
log::trace!("ΔB (norm): {:?}", &bonds_delta);
540-
541533
// Compute the Exponential Moving Average (EMA) of bonds.
542-
let mut ema_bonds =
543-
Self::compute_ema_bonds_sparse(netuid, consensus.clone(), bonds_delta, bonds);
534+
let mut ema_bonds = if let Some(clamped_bonds_alpha) =
535+
Self::compute_liquid_alpha(netuid, consensus.clone())
536+
{
537+
// Compute the Exponential Moving Average (EMA) of bonds using the clamped alpha values.
538+
Self::compute_ema_bonds_with_liquid_alpha_sparse(
539+
&weights.clone(),
540+
&bonds,
541+
clamped_bonds_alpha,
542+
)
543+
} else {
544+
log::trace!("Using Bonds Moving Average");
545+
// Compute the EMA of bonds using a normal alpha value.
546+
Self::compute_ema_bonds_normal_sparse(&weights.clone(), &bonds, netuid)
547+
};
548+
544549
// Normalize EMA bonds.
545550
inplace_col_normalize_sparse(&mut ema_bonds, n); // sum_i b_ij = 1
546551
log::trace!("Exponential Moving Average Bonds: {:?}", &ema_bonds);
547552

548-
// Compute dividends: d_i = SUM(j) b_ij * inc_j.
549-
// range: I32F32(0, 1)
550-
let mut dividends: Vec<I32F32> = matmul_transpose_sparse(&ema_bonds, &incentive);
553+
// # === Dividend Calculation===
554+
let total_bonds_per_validator: Vec<I32F32> =
555+
matmul_transpose_sparse(&ema_bonds, &incentive);
556+
let mut dividends: Vec<I32F32> = vec_mul(&total_bonds_per_validator, &active_stake);
551557
inplace_normalize(&mut dividends);
552558
log::trace!("Dividends: {:?}", &dividends);
553559

@@ -1051,75 +1057,6 @@ impl<T: Config> Pallet<T> {
10511057
ema_bonds
10521058
}
10531059

1054-
/// Compute the Exponential Moving Average (EMA) of bonds based on the Liquid Alpha setting for a sparse matrix.
1055-
///
1056-
/// # Args:
1057-
/// * `netuid` - The network ID.
1058-
/// * `consensus` - A vector of consensus values.
1059-
/// * `bonds_delta` - A vector of bond deltas.
1060-
/// * `bonds` - A vector of bonds.
1061-
///
1062-
/// # Returns:
1063-
/// A vector of EMA bonds.
1064-
pub fn compute_ema_bonds_sparse(
1065-
netuid: u16,
1066-
consensus: Vec<I32F32>,
1067-
bonds_delta: Vec<Vec<(u16, I32F32)>>,
1068-
bonds: Vec<Vec<(u16, I32F32)>>,
1069-
) -> Vec<Vec<(u16, I32F32)>> {
1070-
// Check if Liquid Alpha is enabled, consensus is not empty, and contains non-zero values.
1071-
// This way we avoid the quantil function panic.
1072-
if LiquidAlphaOn::<T>::get(netuid)
1073-
&& !consensus.is_empty()
1074-
&& consensus.iter().any(|&c| c != I32F32::from_num(0))
1075-
{
1076-
// Calculate the 75th percentile (high) and 25th percentile (low) of the consensus values.
1077-
let consensus_high = quantile(&consensus, 0.75);
1078-
let consensus_low = quantile(&consensus, 0.25);
1079-
// Further check if the high and low consensus values meet the required conditions.
1080-
if (consensus_high > consensus_low) || consensus_high != 0 || consensus_low < 0 {
1081-
// if (consensus_high > consensus_low) || consensus_high != 0) || consensus_low != 0 {
1082-
// if (consensus_high > consensus_low) || consensus_low != 0 {
1083-
log::trace!("Using Liquid Alpha");
1084-
1085-
// Get the high and low alpha values for the network.
1086-
let (alpha_low, alpha_high): (I32F32, I32F32) = Self::get_alpha_values_32(netuid);
1087-
log::trace!("alpha_low: {:?} alpha_high: {:?}", alpha_low, alpha_high);
1088-
1089-
// Calculate the logistic function parameters 'a' and 'b' based on alpha and consensus values.
1090-
let (a, b) = Self::calculate_logistic_params(
1091-
alpha_high,
1092-
alpha_low,
1093-
consensus_high,
1094-
consensus_low,
1095-
);
1096-
1097-
// Compute the alpha values using the logistic function parameters.
1098-
let alpha = Self::compute_alpha_values(&consensus, a, b);
1099-
1100-
// Clamp the alpha values between alpha_high and alpha_low.
1101-
let clamped_alpha = Self::clamp_alpha_values(alpha, alpha_high, alpha_low);
1102-
1103-
// Compute the Exponential Moving Average (EMA) of bonds using the clamped alpha values.
1104-
Self::compute_ema_bonds_with_liquid_alpha_sparse(
1105-
&bonds_delta,
1106-
&bonds,
1107-
clamped_alpha,
1108-
)
1109-
} else {
1110-
log::trace!("Using Bonds Moving Average");
1111-
1112-
// Compute the EMA of bonds using a normal alpha value.
1113-
Self::compute_ema_bonds_normal_sparse(&bonds_delta, &bonds, netuid)
1114-
}
1115-
} else {
1116-
log::trace!("Using Bonds Moving Average");
1117-
1118-
// Compute the EMA of bonds using a normal alpha value.
1119-
Self::compute_ema_bonds_normal_sparse(&bonds_delta, &bonds, netuid)
1120-
}
1121-
}
1122-
11231060
/// Compute liquid alphas based on the Liquid Alpha setting.
11241061
///
11251062
/// # Args:

0 commit comments

Comments
 (0)