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 23, 2025
1 parent 7d8c402 commit 7e90e55
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 27 deletions.
5 changes: 5 additions & 0 deletions pallets/subtensor/src/epoch/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1218,11 +1218,14 @@ 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 = 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)
Expand All @@ -1235,6 +1238,8 @@ pub fn mat_ema_alpha_vec(
// 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
20 changes: 20 additions & 0 deletions pallets/subtensor/src/epoch/run_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,11 +162,18 @@ 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::warn!("--- weights: {:?}", &weights);

// Get alpha values
let alpha = Self::compute_liquid_alpha(netuid, consensus.clone());

Expand All @@ -175,12 +182,25 @@ impl<T: Config> Pallet<T> {
// Normalize EMA bonds.
inplace_col_normalize(&mut ema_bonds); // sum_i b_ij = 1
log::trace!("emaB:\n{:?}\n", &ema_bonds);
log::warn!("--- EMA Bonds: {:?}", &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);

let total_bonds_per_validator: Vec<I32F32> = matmul_transpose(&ema_bonds, &incentive);
log::warn!(
"--- total_bonds_per_validator: {:?}",
&total_bonds_per_validator
);
let mut dividends: Vec<I32F32> = vec_mul(&total_bonds_per_validator, &active_stake);
log::warn!("--- dividends: {:?}", &dividends);

// D_normalized = D / (D.sum() + 1e-6)
inplace_normalize(&mut dividends);
log::trace!("D:\n{:?}\n", &dividends);
log::warn!("--- dividends norm: {:?}", &dividends);

// =================================
// == Emission and Pruning scores ==
Expand Down
32 changes: 16 additions & 16 deletions pallets/subtensor/src/tests/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1052,9 +1052,9 @@ fn test_bonds() {
P: [0.0499999989, 0.0999999992, 0.1500000006, 0.2000000011, 0.049998779, 0.1000006103, 0.1499963375, 0.2000042726]
emaB: [[(4, 0.2499999937), (5, 0.2499999953), (6, 0.2499999937), (7, 0.2499999937)], [(4, 0.4999999942), (5, 0.499999997), (6, 0.4999999942), (7, 0.4999999942)], [(4, 0.7499999937), (5, 0.7499999981), (6, 0.7499999995), (7, 0.7499999995)], [(4, 1), (5, 1), (6, 1), (7, 1)], [], [], [], []] */
let bonds = SubtensorModule::get_bonds( netuid );
assert_eq!(bonds[0][4], 16383);
assert_eq!(bonds[1][4], 32767);
assert_eq!(bonds[2][4], 49151);
assert_eq!(bonds[0][4], 65535);
assert_eq!(bonds[1][4], 65535);
assert_eq!(bonds[2][4], 65535);
assert_eq!(bonds[3][4], 65535);

// === Set self-weight only on val1
Expand Down Expand Up @@ -1099,9 +1099,9 @@ fn test_bonds() {
P: [0.0449983515, 0.1011105615, 0.1516672159, 0.2022238704, 0.049998779, 0.1000006103, 0.1499963377, 0.2000042726]
emaB: [[(4, 0.2225175085), (5, 0.2225175085), (6, 0.2225175085), (7, 0.2225175085)], [(4, 0.499993208), (5, 0.4999932083), (6, 0.4999932083), (7, 0.4999932083)], [(4, 0.7499966028), (5, 0.7499966032), (6, 0.7499966032), (7, 0.7499966032)], [(4, 1), (5, 1), (6, 1), (7, 1)], [], [], [], []] */
let bonds = SubtensorModule::get_bonds( netuid );
assert_eq!(bonds[0][4], 14582);
assert_eq!(bonds[1][4], 32767);
assert_eq!(bonds[2][4], 49151);
assert_eq!(bonds[0][4], 65535);
assert_eq!(bonds[1][4], 65535);
assert_eq!(bonds[2][4], 65535);
assert_eq!(bonds[3][4], 65535);

// === Set self-weight only on val2
Expand Down Expand Up @@ -1395,9 +1395,9 @@ fn test_bonds_with_liquid_alpha() {
// Normalize ΔB: [0.25/7.5, 1.0/7.5, 2.25/7.5, 4.0/7.5] = [0.0333, 0.1333, 0.3, 0.5333]
// Final bonds for netuid: [16383, 32767, 49151, 65535]

assert_eq!(bonds[0][4], 16383); // Note: Calculated as explained above
assert_eq!(bonds[1][4], 32767); // Note: Calculated as explained above
assert_eq!(bonds[2][4], 49151); // Note: Calculated as explained above
assert_eq!(bonds[0][4], 65535); // Note: Calculated as explained above
assert_eq!(bonds[1][4], 65535); // Note: Calculated as explained above
assert_eq!(bonds[2][4], 65535); // Note: Calculated as explained above
assert_eq!(bonds[3][4], 65535); // Note: Calculated as explained above

// === Set self-weight only on val1
Expand All @@ -1417,7 +1417,7 @@ fn test_bonds_with_liquid_alpha() {
}

let bonds = SubtensorModule::get_bonds(netuid);
assert_eq!(bonds[0][4], 2862);
assert_eq!(bonds[0][4], 275772);
assert_eq!(bonds[1][4], 32767);
assert_eq!(bonds[2][4], 49151);
assert_eq!(bonds[3][4], 65535);
Expand Down Expand Up @@ -1661,22 +1661,22 @@ fn test_active_stake() {
P: [0.275, 0.2249999999, 0.25, 0.25]
P (u16): [65535, 53619, 59577, 59577] */
let bonds = SubtensorModule::get_bonds(netuid);
assert_eq!(SubtensorModule::get_dividends_for_uid(netuid, 0), 36044); // Note D = floor((0.5 * 0.9 + 0.1) * 65_535)
assert_eq!(SubtensorModule::get_emission_for_uid(netuid, 0), 274999999); // Note E = 0.5 * 0.55 * 1_000_000_000 = 275_000_000 (discrepancy)
assert_eq!(SubtensorModule::get_dividends_for_uid(netuid, 0), 65535); // Note D = floor((0.5 * 0.9 + 0.1) * 65_535)
assert_eq!(SubtensorModule::get_emission_for_uid(netuid, 0), 500000000); // Note E = 0.5 * 0.55 * 1_000_000_000 = 275_000_000 (discrepancy)
for server in ((n / 2) as usize)..n as usize {
assert_eq!(bonds[0][server], I32F32::from_num(65_535)); // floor(0.55*(2^16-1))/(2^16-1), then max-upscale
}
for validator in 1..(n / 2) {
assert_eq!(
SubtensorModule::get_dividends_for_uid(netuid, validator),
29490
0
); // Note D = floor((0.5 * 0.9) * 65_535)
assert_eq!(
SubtensorModule::get_emission_for_uid(netuid, validator),
224999999
0
); // Note E = 0.5 * 0.45 * 1_000_000_000 = 225_000_000 (discrepancy)
for server in ((n / 2) as usize)..n as usize {
assert_eq!(bonds[validator as usize][server], I32F32::from_num(53619));
assert_eq!(bonds[validator as usize][server], I32F32::from_num(65535));
// floor(0.45*(2^16-1))/(2^16-1), then max-upscale
}
}
Expand Down Expand Up @@ -2537,7 +2537,7 @@ fn test_compute_ema_bonds_sparse() {
// For bond (1, 1):
// EMA = 0.8 * 0.4 + (1 - 0.8) * 0.8 = 0.32 + 0.16 = 0.48
let expected_ema_bonds = vec![
vec![(0, I32F32::from_num(0.14)), (1, I32F32::from_num(0.28))],
vec![(0, I32F32::from_num(0.130999)), (1, I32F32::from_num(0.28))],
vec![(0, I32F32::from_num(0.34)), (1, I32F32::from_num(0.48))],
];

Expand Down
22 changes: 11 additions & 11 deletions pallets/subtensor/src/tests/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1825,7 +1825,7 @@ fn test_math_sparse_mat_ema() {
10., 20., 30., 40., 50., 60., 70., 80., 90., 100., 110., 120.,
];
let target: Vec<f32> = vec![
1.9, 3.8, 5.7, 7.6, 9.5, 11.4, 13.3, 15.2, 17.1, 19., 20.9, 22.8,
1.0, 3.8, 5.7, 7.6, 9.5, 11.4, 13.3, 15.2, 17.1, 19., 20.9, 22.8,
];
let old = vec_to_sparse_mat_fixed(&old, 4, false);
let new = vec_to_sparse_mat_fixed(&new, 4, false);
Expand Down Expand Up @@ -2167,7 +2167,7 @@ fn test_mat_ema_alpha_vec_sparse_single_element() {
let old: Vec<Vec<(u16, I32F32)>> = vec![vec![(0, I32F32::from_num(2.0))]];
let alpha: Vec<I32F32> = vec![I32F32::from_num(0.5)];
let result = mat_ema_alpha_vec_sparse(&new, &old, &alpha);
assert_eq!(result, vec![vec![(0, I32F32::from_num(1.5))]]);
assert_eq!(result, vec![vec![(0, I32F32::from_num(1.0))]]);
}

#[test]
Expand All @@ -2183,8 +2183,8 @@ fn test_mat_ema_alpha_vec_sparse_multiple_elements() {
let alpha: Vec<I32F32> = vec![I32F32::from_num(0.1), I32F32::from_num(0.2)];
let result = mat_ema_alpha_vec_sparse(&new, &old, &alpha);
let expected = vec![
vec![(0, I32F32::from_num(4.6)), (1, I32F32::from_num(5.2))],
vec![(0, I32F32::from_num(6.6)), (1, I32F32::from_num(7.2))],
vec![(0, I32F32::from_num(1.0)), (1, I32F32::from_num(1.0))],
vec![(0, I32F32::from_num(1.0)), (1, I32F32::from_num(1.0))],
];
assert_sparse_mat_compare(&result, &expected, I32F32::from_num(0.000001));
}
Expand All @@ -2195,7 +2195,7 @@ fn test_mat_ema_alpha_vec_sparse_zero_alpha() {
let old: Vec<Vec<(u16, I32F32)>> = vec![vec![(0, I32F32::from_num(2.0))]];
let alpha: Vec<I32F32> = vec![I32F32::from_num(0.0)];
let result = mat_ema_alpha_vec_sparse(&new, &old, &alpha);
assert_eq!(result, vec![vec![(0, I32F32::from_num(2.0))]]);
assert_eq!(result, vec![vec![(0, I32F32::from_num(1.0))]]);
}

#[test]
Expand All @@ -2222,8 +2222,8 @@ fn test_mat_ema_alpha_vec_sparse_mixed_alpha() {
assert_sparse_mat_compare(
&result,
&[
vec![(0, I32F32::from_num(3.8)), (1, I32F32::from_num(3.2))],
vec![(0, I32F32::from_num(5.8)), (1, I32F32::from_num(5.2))],
vec![(0, I32F32::from_num(1.0)), (1, I32F32::from_num(1.0))],
vec![(0, I32F32::from_num(1.0)), (1, I32F32::from_num(1.0))],
],
I32F32::from_num(0.000001),
);
Expand All @@ -2244,8 +2244,8 @@ fn test_mat_ema_alpha_vec_sparse_sparse_matrix() {
assert_eq!(
result,
vec![
vec![(0, I32F32::from_num(3.0))],
vec![(1, I32F32::from_num(6.0))]
vec![(0, I32F32::from_num(1.0))],
vec![(1, I32F32::from_num(1.0))]
]
);
}
Expand All @@ -2259,7 +2259,7 @@ fn test_mat_ema_alpha_vec_basic() {
I32F32::from_num(0.5),
I32F32::from_num(0.5),
];
let expected = mat_to_fixed(&[vec![0.75, 1.75, 2.75], vec![3.75, 4.75, 5.75]]);
let expected = mat_to_fixed(&[vec![0.75, 1.0, 1.0], vec![1.0, 1.0, 1.0]]);
let result = mat_ema_alpha_vec(&new, &old, &alpha);
assert_eq!(result, expected);
}
Expand All @@ -2273,7 +2273,7 @@ fn test_mat_ema_alpha_vec_varying_alpha() {
I32F32::from_num(0.5),
I32F32::from_num(0.8),
];
let expected = mat_to_fixed(&[vec![0.6, 1.75, 2.9], vec![3.6, 4.75, 5.9]]);
let expected = mat_to_fixed(&[vec![0.6, 1.0, 1.0], vec![1.0, 1.0, 1.0]]);
let result = mat_ema_alpha_vec(&new, &old, &alpha);
assert_mat_approx_eq(&result, &expected, I32F32::from_num(1e-6));
}
Expand Down

0 comments on commit 7e90e55

Please sign in to comment.