Skip to content

Commit da68441

Browse files
committed
mask recently registered bonds
1 parent 00b0e32 commit da68441

File tree

3 files changed

+53
-32
lines changed

3 files changed

+53
-32
lines changed

pallets/subtensor/src/epoch/math.rs

+18
Original file line numberDiff line numberDiff line change
@@ -549,6 +549,24 @@ pub fn inplace_mask_rows(mask: &[bool], matrix: &mut [Vec<I32F32>]) {
549549
});
550550
}
551551

552+
// Apply column mask to matrix, mask=true will mask out, i.e. set to 0.
553+
// Assumes each column has the same length.
554+
#[allow(dead_code)]
555+
pub fn inplace_mask_cols(mask: &[bool], matrix: &mut [Vec<I32F32>]) {
556+
let Some(first_row) = matrix.first() else {
557+
return;
558+
};
559+
assert_eq!(mask.len(), first_row.len());
560+
let zero: I32F32 = I32F32::saturating_from_num(0);
561+
matrix.iter_mut().for_each(|row_elem| {
562+
row_elem.iter_mut().zip(mask).for_each(|(elem, mask_col)| {
563+
if *mask_col {
564+
*elem = zero;
565+
}
566+
});
567+
});
568+
}
569+
552570
// Mask out the diagonal of the input matrix in-place.
553571
#[allow(dead_code)]
554572
pub fn inplace_mask_diag(matrix: &mut [Vec<I32F32>]) {

pallets/subtensor/src/epoch/run_epoch.rs

+34-32
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ impl<T: Config> Pallet<T> {
2222
let current_block: u64 = Self::get_current_block_as_u64();
2323
log::trace!("current_block:\n{:?}\n", current_block);
2424

25+
// Get tempo.
26+
let tempo: u64 = Self::get_tempo(netuid).into();
27+
log::trace!("tempo:\n{:?}\n", tempo);
28+
2529
// Get activity cutoff.
2630
let activity_cutoff: u64 = Self::get_activity_cutoff(netuid) as u64;
2731
log::trace!("activity_cutoff:\n{:?}\n", activity_cutoff);
@@ -44,7 +48,7 @@ impl<T: Config> Pallet<T> {
4448
let block_at_registration: Vec<u64> = Self::get_block_at_registration(netuid);
4549
log::trace!("Block at registration:\n{:?}\n", &block_at_registration);
4650

47-
// Outdated matrix, updated_ij=True if i has last updated (weights) after j has last registered.
51+
// Outdated matrix, outdated_ij=True if i has last updated (weights) after j has last registered.
4852
let outdated: Vec<Vec<bool>> = last_update
4953
.iter()
5054
.map(|updated| {
@@ -56,6 +60,16 @@ impl<T: Config> Pallet<T> {
5660
.collect();
5761
log::trace!("Outdated:\n{:?}\n", &outdated);
5862

63+
// Recently registered matrix, recently_ij=True if last_tempo was *before* j was last registered.
64+
// Mask if: the last tempo block happened *before* the registration block
65+
// ==> last_tempo <= registered
66+
let last_tempo: u64 = current_block.saturating_sub(tempo);
67+
let recently_registered: Vec<bool> = block_at_registration
68+
.iter()
69+
.map(|registered| last_tempo <= *registered)
70+
.collect();
71+
log::trace!("Recently registered:\n{:?}\n", &recently_registered);
72+
5973
// ===========
6074
// == Stake ==
6175
// ===========
@@ -185,17 +199,16 @@ impl<T: Config> Pallet<T> {
185199

186200
// Access network bonds.
187201
let mut bonds: Vec<Vec<I32F32>> = Self::get_bonds(netuid);
188-
inplace_mask_matrix(&outdated, &mut bonds); // mask outdated bonds
189-
inplace_col_normalize(&mut bonds); // sum_i b_ij = 1
190-
log::trace!("B:\n{:?}\n", &bonds);
202+
203+
// Remove bonds referring to neurons that have registered since last tempo.
204+
inplace_mask_cols(&recently_registered, &mut bonds); // mask recently registered bonds
191205

192206
// Compute bonds delta column normalized.
193-
let mut bonds_delta: Vec<Vec<I32F32>> = row_hadamard(&weights_for_bonds, &active_stake); // ΔB = W◦S
194-
inplace_col_normalize(&mut bonds_delta); // sum_i b_ij = 1
207+
let bonds_delta: Vec<Vec<I32F32>> = row_hadamard(&weights_for_bonds, &active_stake); // ΔB = W◦S
195208
log::trace!("ΔB:\n{:?}\n", &bonds_delta);
209+
196210
// Compute the Exponential Moving Average (EMA) of bonds.
197-
let mut ema_bonds = Self::compute_ema_bonds(netuid, consensus.clone(), bonds_delta, bonds);
198-
inplace_col_normalize(&mut ema_bonds); // sum_i b_ij = 1
211+
let ema_bonds = Self::compute_ema_bonds(netuid, consensus.clone(), bonds_delta, bonds);
199212
log::trace!("emaB:\n{:?}\n", &ema_bonds);
200213

201214
// Compute dividends: d_i = SUM(j) b_ij * inc_j
@@ -326,10 +339,6 @@ impl<T: Config> Pallet<T> {
326339
ValidatorTrust::<T>::insert(netuid, cloned_validator_trust);
327340
ValidatorPermit::<T>::insert(netuid, new_validator_permits.clone());
328341

329-
// Column max-upscale EMA bonds for storage: max_i w_ij = 1.
330-
inplace_col_max_upscale(&mut ema_bonds);
331-
// Then normalize.
332-
inplace_col_normalize(&mut ema_bonds);
333342
new_validator_permits
334343
.iter()
335344
.zip(validator_permits)
@@ -388,6 +397,10 @@ impl<T: Config> Pallet<T> {
388397
let current_block: u64 = Self::get_current_block_as_u64();
389398
log::trace!("current_block: {:?}", current_block);
390399

400+
// Get tempo.
401+
let tempo: u64 = Self::get_tempo(netuid).into();
402+
log::trace!("tempo: {:?}", tempo);
403+
391404
// Get activity cutoff.
392405
let activity_cutoff: u64 = Self::get_activity_cutoff(netuid) as u64;
393406
log::trace!("activity_cutoff: {:?}", activity_cutoff);
@@ -550,33 +563,26 @@ impl<T: Config> Pallet<T> {
550563
let mut bonds: Vec<Vec<(u16, I32F32)>> = Self::get_bonds_sparse(netuid);
551564
log::trace!("B: {:?}", &bonds);
552565

553-
// Remove bonds referring to deregistered neurons.
554-
bonds = vec_mask_sparse_matrix(
566+
// Remove bonds referring to neurons that have registered since last tempo.
567+
// Mask if: the last tempo block happened *before* the registration block
568+
// ==> last_tempo <= registered
569+
let last_tempo: u64 = current_block.saturating_sub(tempo);
570+
bonds = scalar_vec_mask_sparse_matrix(
555571
&bonds,
556-
&last_update,
572+
last_tempo,
557573
&block_at_registration,
558-
&|updated, registered| updated <= registered,
574+
&|last_tempo, registered| last_tempo <= registered,
559575
);
560576
log::trace!("B (outdatedmask): {:?}", &bonds);
561577

562-
// Normalize remaining bonds: sum_i b_ij = 1.
563-
inplace_col_normalize_sparse(&mut bonds, n);
564-
log::trace!("B (mask+norm): {:?}", &bonds);
565-
566578
// Compute bonds delta column normalized.
567-
let mut bonds_delta: Vec<Vec<(u16, I32F32)>> =
579+
let bonds_delta: Vec<Vec<(u16, I32F32)>> =
568580
row_hadamard_sparse(&weights_for_bonds, &active_stake); // ΔB = W◦S (outdated W masked)
569581
log::trace!("ΔB: {:?}", &bonds_delta);
570582

571-
// Normalize bonds delta.
572-
inplace_col_normalize_sparse(&mut bonds_delta, n); // sum_i b_ij = 1
573-
log::trace!("ΔB (norm): {:?}", &bonds_delta);
574-
575583
// Compute the Exponential Moving Average (EMA) of bonds.
576-
let mut ema_bonds =
584+
let ema_bonds =
577585
Self::compute_ema_bonds_sparse(netuid, consensus.clone(), bonds_delta, bonds);
578-
// Normalize EMA bonds.
579-
inplace_col_normalize_sparse(&mut ema_bonds, n); // sum_i b_ij = 1
580586
log::trace!("Exponential Moving Average Bonds: {:?}", &ema_bonds);
581587

582588
// Compute dividends: d_i = SUM(j) b_ij * inc_j.
@@ -714,10 +720,6 @@ impl<T: Config> Pallet<T> {
714720
ValidatorTrust::<T>::insert(netuid, cloned_validator_trust);
715721
ValidatorPermit::<T>::insert(netuid, new_validator_permits.clone());
716722

717-
// Column max-upscale EMA bonds for storage: max_i w_ij = 1.
718-
inplace_col_max_upscale_sparse(&mut ema_bonds, n);
719-
// Then normalize.
720-
inplace_col_normalize_sparse(&mut ema_bonds, n);
721723
new_validator_permits
722724
.iter()
723725
.zip(validator_permits)

pallets/subtensor/src/subnets/uids.rs

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ impl<T: Config> Pallet<T> {
2323
Consensus::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
2424
Incentive::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
2525
Dividends::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
26+
Bonds::<T>::mutate(netuid, |v| Self::set_element_at(v, neuron_index, 0));
2627
}
2728

2829
/// Replace the neuron under this uid.

0 commit comments

Comments
 (0)