Skip to content

Commit 7e90e55

Browse files
wip
1 parent 7d8c402 commit 7e90e55

File tree

4 files changed

+52
-27
lines changed

4 files changed

+52
-27
lines changed

pallets/subtensor/src/epoch/math.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,11 +1218,14 @@ pub fn mat_ema_alpha_vec(
12181218
let one_minus_alpha = I32F32::from_num(1.0).saturating_sub(alpha_val);
12191219

12201220
// Compute the EMA for the current element using saturating operations.
1221+
// B_ema = clamped_alpha * B + (1 - clamped_alpha) * B_old
12211222
if let (Some(new_val), Some(old_val), Some(result_val)) = (
12221223
new_row.get(j),
12231224
old_row.get(j),
12241225
result.get_mut(i).and_then(|row| row.get_mut(j)),
12251226
) {
1227+
// *result_val = purchase_increment.saturating_add(decayed_val);
1228+
12261229
let decayed_val = one_minus_alpha.saturating_mul(*old_val);
12271230
let remaining_capacity = I32F32::from_num(1.0)
12281231
.saturating_sub(decayed_val)
@@ -1235,6 +1238,8 @@ pub fn mat_ema_alpha_vec(
12351238
// Ensure that purchase does not exceed remaining capacity
12361239
let purchase = purchase_increment.min(remaining_capacity);
12371240

1241+
// B = B_decayed + purchase
1242+
// B = torch.clamp(B, max=1.0)
12381243
*result_val = decayed_val
12391244
.saturating_add(purchase)
12401245
.min(I32F32::from_num(1.0));

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,18 @@ impl<T: Config> Pallet<T> {
162162
// =========================
163163

164164
// Access network bonds.
165+
// old bonds
165166
let mut bonds: Vec<Vec<I32F32>> = Self::get_bonds(netuid);
166167
inplace_mask_matrix(&outdated, &mut bonds); // mask outdated bonds
167168
inplace_col_normalize(&mut bonds); // sum_i b_ij = 1
168169
log::trace!("B:\n{:?}\n", &bonds);
169170

171+
// Compute bonds delta column normalized.
172+
// bonds delta
173+
// let mut bonds_delta: Vec<Vec<I32F32>> = row_hadamard(&weights, &active_stake); // ΔB = W◦S
174+
// inplace_col_normalize(&mut bonds_delta); // sum_i b_ij = 1
175+
// log::warn!("--- weights: {:?}", &weights);
176+
170177
// Get alpha values
171178
let alpha = Self::compute_liquid_alpha(netuid, consensus.clone());
172179

@@ -175,12 +182,25 @@ impl<T: Config> Pallet<T> {
175182
// Normalize EMA bonds.
176183
inplace_col_normalize(&mut ema_bonds); // sum_i b_ij = 1
177184
log::trace!("emaB:\n{:?}\n", &ema_bonds);
185+
log::warn!("--- EMA Bonds: {:?}", &ema_bonds);
178186

187+
// Compute dividends: d_i = SUM(j) b_ij * inc_j
179188
// # === Dividend Calculation===
189+
// D = (B_ema * I).sum(dim=1)
190+
// let mut dividends: Vec<I32F32> = matmul_transpose(&ema_bonds, &incentive);
191+
180192
let total_bonds_per_validator: Vec<I32F32> = matmul_transpose(&ema_bonds, &incentive);
193+
log::warn!(
194+
"--- total_bonds_per_validator: {:?}",
195+
&total_bonds_per_validator
196+
);
181197
let mut dividends: Vec<I32F32> = vec_mul(&total_bonds_per_validator, &active_stake);
198+
log::warn!("--- dividends: {:?}", &dividends);
199+
200+
// D_normalized = D / (D.sum() + 1e-6)
182201
inplace_normalize(&mut dividends);
183202
log::trace!("D:\n{:?}\n", &dividends);
203+
log::warn!("--- dividends norm: {:?}", &dividends);
184204

185205
// =================================
186206
// == Emission and Pruning scores ==

pallets/subtensor/src/tests/epoch.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,9 +1052,9 @@ fn test_bonds() {
10521052
P: [0.0499999989, 0.0999999992, 0.1500000006, 0.2000000011, 0.049998779, 0.1000006103, 0.1499963375, 0.2000042726]
10531053
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)], [], [], [], []] */
10541054
let bonds = SubtensorModule::get_bonds( netuid );
1055-
assert_eq!(bonds[0][4], 16383);
1056-
assert_eq!(bonds[1][4], 32767);
1057-
assert_eq!(bonds[2][4], 49151);
1055+
assert_eq!(bonds[0][4], 65535);
1056+
assert_eq!(bonds[1][4], 65535);
1057+
assert_eq!(bonds[2][4], 65535);
10581058
assert_eq!(bonds[3][4], 65535);
10591059

10601060
// === Set self-weight only on val1
@@ -1099,9 +1099,9 @@ fn test_bonds() {
10991099
P: [0.0449983515, 0.1011105615, 0.1516672159, 0.2022238704, 0.049998779, 0.1000006103, 0.1499963377, 0.2000042726]
11001100
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)], [], [], [], []] */
11011101
let bonds = SubtensorModule::get_bonds( netuid );
1102-
assert_eq!(bonds[0][4], 14582);
1103-
assert_eq!(bonds[1][4], 32767);
1104-
assert_eq!(bonds[2][4], 49151);
1102+
assert_eq!(bonds[0][4], 65535);
1103+
assert_eq!(bonds[1][4], 65535);
1104+
assert_eq!(bonds[2][4], 65535);
11051105
assert_eq!(bonds[3][4], 65535);
11061106

11071107
// === Set self-weight only on val2
@@ -1395,9 +1395,9 @@ fn test_bonds_with_liquid_alpha() {
13951395
// 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]
13961396
// Final bonds for netuid: [16383, 32767, 49151, 65535]
13971397

1398-
assert_eq!(bonds[0][4], 16383); // Note: Calculated as explained above
1399-
assert_eq!(bonds[1][4], 32767); // Note: Calculated as explained above
1400-
assert_eq!(bonds[2][4], 49151); // Note: Calculated as explained above
1398+
assert_eq!(bonds[0][4], 65535); // Note: Calculated as explained above
1399+
assert_eq!(bonds[1][4], 65535); // Note: Calculated as explained above
1400+
assert_eq!(bonds[2][4], 65535); // Note: Calculated as explained above
14011401
assert_eq!(bonds[3][4], 65535); // Note: Calculated as explained above
14021402

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

14191419
let bonds = SubtensorModule::get_bonds(netuid);
1420-
assert_eq!(bonds[0][4], 2862);
1420+
assert_eq!(bonds[0][4], 275772);
14211421
assert_eq!(bonds[1][4], 32767);
14221422
assert_eq!(bonds[2][4], 49151);
14231423
assert_eq!(bonds[3][4], 65535);
@@ -1661,22 +1661,22 @@ fn test_active_stake() {
16611661
P: [0.275, 0.2249999999, 0.25, 0.25]
16621662
P (u16): [65535, 53619, 59577, 59577] */
16631663
let bonds = SubtensorModule::get_bonds(netuid);
1664-
assert_eq!(SubtensorModule::get_dividends_for_uid(netuid, 0), 36044); // Note D = floor((0.5 * 0.9 + 0.1) * 65_535)
1665-
assert_eq!(SubtensorModule::get_emission_for_uid(netuid, 0), 274999999); // Note E = 0.5 * 0.55 * 1_000_000_000 = 275_000_000 (discrepancy)
1664+
assert_eq!(SubtensorModule::get_dividends_for_uid(netuid, 0), 65535); // Note D = floor((0.5 * 0.9 + 0.1) * 65_535)
1665+
assert_eq!(SubtensorModule::get_emission_for_uid(netuid, 0), 500000000); // Note E = 0.5 * 0.55 * 1_000_000_000 = 275_000_000 (discrepancy)
16661666
for server in ((n / 2) as usize)..n as usize {
16671667
assert_eq!(bonds[0][server], I32F32::from_num(65_535)); // floor(0.55*(2^16-1))/(2^16-1), then max-upscale
16681668
}
16691669
for validator in 1..(n / 2) {
16701670
assert_eq!(
16711671
SubtensorModule::get_dividends_for_uid(netuid, validator),
1672-
29490
1672+
0
16731673
); // Note D = floor((0.5 * 0.9) * 65_535)
16741674
assert_eq!(
16751675
SubtensorModule::get_emission_for_uid(netuid, validator),
1676-
224999999
1676+
0
16771677
); // Note E = 0.5 * 0.45 * 1_000_000_000 = 225_000_000 (discrepancy)
16781678
for server in ((n / 2) as usize)..n as usize {
1679-
assert_eq!(bonds[validator as usize][server], I32F32::from_num(53619));
1679+
assert_eq!(bonds[validator as usize][server], I32F32::from_num(65535));
16801680
// floor(0.45*(2^16-1))/(2^16-1), then max-upscale
16811681
}
16821682
}
@@ -2537,7 +2537,7 @@ fn test_compute_ema_bonds_sparse() {
25372537
// For bond (1, 1):
25382538
// EMA = 0.8 * 0.4 + (1 - 0.8) * 0.8 = 0.32 + 0.16 = 0.48
25392539
let expected_ema_bonds = vec![
2540-
vec![(0, I32F32::from_num(0.14)), (1, I32F32::from_num(0.28))],
2540+
vec![(0, I32F32::from_num(0.130999)), (1, I32F32::from_num(0.28))],
25412541
vec![(0, I32F32::from_num(0.34)), (1, I32F32::from_num(0.48))],
25422542
];
25432543

pallets/subtensor/src/tests/math.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1825,7 +1825,7 @@ fn test_math_sparse_mat_ema() {
18251825
10., 20., 30., 40., 50., 60., 70., 80., 90., 100., 110., 120.,
18261826
];
18271827
let target: Vec<f32> = vec![
1828-
1.9, 3.8, 5.7, 7.6, 9.5, 11.4, 13.3, 15.2, 17.1, 19., 20.9, 22.8,
1828+
1.0, 3.8, 5.7, 7.6, 9.5, 11.4, 13.3, 15.2, 17.1, 19., 20.9, 22.8,
18291829
];
18301830
let old = vec_to_sparse_mat_fixed(&old, 4, false);
18311831
let new = vec_to_sparse_mat_fixed(&new, 4, false);
@@ -2167,7 +2167,7 @@ fn test_mat_ema_alpha_vec_sparse_single_element() {
21672167
let old: Vec<Vec<(u16, I32F32)>> = vec![vec![(0, I32F32::from_num(2.0))]];
21682168
let alpha: Vec<I32F32> = vec![I32F32::from_num(0.5)];
21692169
let result = mat_ema_alpha_vec_sparse(&new, &old, &alpha);
2170-
assert_eq!(result, vec![vec![(0, I32F32::from_num(1.5))]]);
2170+
assert_eq!(result, vec![vec![(0, I32F32::from_num(1.0))]]);
21712171
}
21722172

21732173
#[test]
@@ -2183,8 +2183,8 @@ fn test_mat_ema_alpha_vec_sparse_multiple_elements() {
21832183
let alpha: Vec<I32F32> = vec![I32F32::from_num(0.1), I32F32::from_num(0.2)];
21842184
let result = mat_ema_alpha_vec_sparse(&new, &old, &alpha);
21852185
let expected = vec![
2186-
vec![(0, I32F32::from_num(4.6)), (1, I32F32::from_num(5.2))],
2187-
vec![(0, I32F32::from_num(6.6)), (1, I32F32::from_num(7.2))],
2186+
vec![(0, I32F32::from_num(1.0)), (1, I32F32::from_num(1.0))],
2187+
vec![(0, I32F32::from_num(1.0)), (1, I32F32::from_num(1.0))],
21882188
];
21892189
assert_sparse_mat_compare(&result, &expected, I32F32::from_num(0.000001));
21902190
}
@@ -2195,7 +2195,7 @@ fn test_mat_ema_alpha_vec_sparse_zero_alpha() {
21952195
let old: Vec<Vec<(u16, I32F32)>> = vec![vec![(0, I32F32::from_num(2.0))]];
21962196
let alpha: Vec<I32F32> = vec![I32F32::from_num(0.0)];
21972197
let result = mat_ema_alpha_vec_sparse(&new, &old, &alpha);
2198-
assert_eq!(result, vec![vec![(0, I32F32::from_num(2.0))]]);
2198+
assert_eq!(result, vec![vec![(0, I32F32::from_num(1.0))]]);
21992199
}
22002200

22012201
#[test]
@@ -2222,8 +2222,8 @@ fn test_mat_ema_alpha_vec_sparse_mixed_alpha() {
22222222
assert_sparse_mat_compare(
22232223
&result,
22242224
&[
2225-
vec![(0, I32F32::from_num(3.8)), (1, I32F32::from_num(3.2))],
2226-
vec![(0, I32F32::from_num(5.8)), (1, I32F32::from_num(5.2))],
2225+
vec![(0, I32F32::from_num(1.0)), (1, I32F32::from_num(1.0))],
2226+
vec![(0, I32F32::from_num(1.0)), (1, I32F32::from_num(1.0))],
22272227
],
22282228
I32F32::from_num(0.000001),
22292229
);
@@ -2244,8 +2244,8 @@ fn test_mat_ema_alpha_vec_sparse_sparse_matrix() {
22442244
assert_eq!(
22452245
result,
22462246
vec![
2247-
vec![(0, I32F32::from_num(3.0))],
2248-
vec![(1, I32F32::from_num(6.0))]
2247+
vec![(0, I32F32::from_num(1.0))],
2248+
vec![(1, I32F32::from_num(1.0))]
22492249
]
22502250
);
22512251
}
@@ -2259,7 +2259,7 @@ fn test_mat_ema_alpha_vec_basic() {
22592259
I32F32::from_num(0.5),
22602260
I32F32::from_num(0.5),
22612261
];
2262-
let expected = mat_to_fixed(&[vec![0.75, 1.75, 2.75], vec![3.75, 4.75, 5.75]]);
2262+
let expected = mat_to_fixed(&[vec![0.75, 1.0, 1.0], vec![1.0, 1.0, 1.0]]);
22632263
let result = mat_ema_alpha_vec(&new, &old, &alpha);
22642264
assert_eq!(result, expected);
22652265
}
@@ -2273,7 +2273,7 @@ fn test_mat_ema_alpha_vec_varying_alpha() {
22732273
I32F32::from_num(0.5),
22742274
I32F32::from_num(0.8),
22752275
];
2276-
let expected = mat_to_fixed(&[vec![0.6, 1.75, 2.9], vec![3.6, 4.75, 5.9]]);
2276+
let expected = mat_to_fixed(&[vec![0.6, 1.0, 1.0], vec![1.0, 1.0, 1.0]]);
22772277
let result = mat_ema_alpha_vec(&new, &old, &alpha);
22782278
assert_mat_approx_eq(&result, &expected, I32F32::from_num(1e-6));
22792279
}

0 commit comments

Comments
 (0)