Skip to content

Commit 39c725c

Browse files
authored
Merge pull request #2158 from opentensor/refactor-neuron-deregistration
Emission-based Neuron Deregistration
2 parents 71dd6a7 + be221cd commit 39c725c

File tree

22 files changed

+819
-470
lines changed

22 files changed

+819
-470
lines changed

pallets/admin-utils/src/benchmarking.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -627,5 +627,23 @@ mod benchmarks {
627627
_(RawOrigin::Root, 1u16.into()/*netuid*/, 256u16/*max_n*/)/*sudo_trim_to_max_allowed_uids()*/;
628628
}
629629

630+
#[benchmark]
631+
fn sudo_set_min_non_immune_uids() {
632+
// disable admin freeze window
633+
pallet_subtensor::Pallet::<T>::set_admin_freeze_window(0);
634+
// create a network for netuid = 1
635+
pallet_subtensor::Pallet::<T>::init_new_network(
636+
1u16.into(), /* netuid */
637+
1u16, /* sudo_tempo */
638+
);
639+
640+
#[extrinsic_call]
641+
_(
642+
RawOrigin::Root,
643+
1u16.into(), /* netuid */
644+
12u16, /* min */
645+
); /* sudo_set_min_non_immune_uids() */
646+
}
647+
630648
//impl_benchmark_test_suite!(AdminUtils, crate::mock::new_test_ext(), crate::mock::Test);
631649
}

pallets/admin-utils/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2221,6 +2221,25 @@ pub mod pallet {
22212221
log::debug!("set_tao_flow_smoothing_factor( {smoothing_factor:?} ) ");
22222222
Ok(())
22232223
}
2224+
2225+
/// Sets the minimum number of non-immortal & non-immune UIDs that must remain in a subnet
2226+
#[pallet::call_index(84)]
2227+
#[pallet::weight((
2228+
Weight::from_parts(7_114_000, 0)
2229+
.saturating_add(<T as frame_system::Config>::DbWeight::get().writes(1))
2230+
.saturating_add(<T as frame_system::Config>::DbWeight::get().reads(0_u64)),
2231+
DispatchClass::Operational,
2232+
Pays::Yes
2233+
))]
2234+
pub fn sudo_set_min_non_immune_uids(
2235+
origin: OriginFor<T>,
2236+
netuid: NetUid,
2237+
min: u16,
2238+
) -> DispatchResult {
2239+
ensure_root(origin)?;
2240+
pallet_subtensor::Pallet::<T>::set_min_non_immune_uids(netuid, min);
2241+
Ok(())
2242+
}
22242243
}
22252244
}
22262245

pallets/admin-utils/src/tests/mod.rs

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2456,7 +2456,7 @@ fn test_trim_to_max_allowed_uids() {
24562456
register_ok_neuron(netuid, U256::from(n), U256::from(n + i), 0);
24572457
}
24582458

2459-
// Run some block to ensure stake weights are set and that we are past the immunity period
2459+
// Run some blocks to ensure stake weights are set and that we are past the immunity period
24602460
// for all neurons
24612461
run_to_block((ImmunityPeriod::<Test>::get(netuid) + 1).into());
24622462

@@ -2479,6 +2479,8 @@ fn test_trim_to_max_allowed_uids() {
24792479
let u64_values: Vec<u64> = values.iter().map(|&v| v as u64).collect();
24802480

24812481
Emission::<Test>::set(netuid, alpha_values);
2482+
// NOTE: `Rank`, `Trust`, and `PruningScores` are *not* trimmed anymore,
2483+
// but we can still populate them without asserting on them.
24822484
Rank::<Test>::insert(netuid, values.clone());
24832485
Trust::<Test>::insert(netuid, values.clone());
24842486
Consensus::<Test>::insert(netuid, values.clone());
@@ -2566,7 +2568,7 @@ fn test_trim_to_max_allowed_uids() {
25662568
assert_eq!(MaxAllowedUids::<Test>::get(netuid), new_max_n);
25672569

25682570
// Ensure the emission has been trimmed correctly, keeping the highest emitters
2569-
// and immune and compressed to the left
2571+
// (after respecting immunity/owner exclusions) and compressed to the left
25702572
assert_eq!(
25712573
Emission::<Test>::get(netuid),
25722574
vec![
@@ -2580,16 +2582,16 @@ fn test_trim_to_max_allowed_uids() {
25802582
74.into()
25812583
]
25822584
);
2583-
// Ensure rest of storage has been trimmed correctly
2585+
2586+
// Ensure rest of (active) storage has been trimmed correctly
25842587
let expected_values = vec![56, 91, 34, 77, 65, 88, 51, 74];
25852588
let expected_bools = vec![true, true, true, true, true, true, true, true];
25862589
let expected_u64_values = vec![56, 91, 34, 77, 65, 88, 51, 74];
2587-
assert_eq!(Rank::<Test>::get(netuid), expected_values);
2588-
assert_eq!(Trust::<Test>::get(netuid), expected_values);
2590+
2591+
// NOTE: Rank/Trust/PruningScores are no longer trimmed; do not assert on them.
25892592
assert_eq!(Active::<Test>::get(netuid), expected_bools);
25902593
assert_eq!(Consensus::<Test>::get(netuid), expected_values);
25912594
assert_eq!(Dividends::<Test>::get(netuid), expected_values);
2592-
assert_eq!(PruningScores::<Test>::get(netuid), expected_values);
25932595
assert_eq!(ValidatorTrust::<Test>::get(netuid), expected_values);
25942596
assert_eq!(ValidatorPermit::<Test>::get(netuid), expected_bools);
25952597
assert_eq!(StakeWeight::<Test>::get(netuid), expected_values);
@@ -2669,7 +2671,7 @@ fn test_trim_to_max_allowed_uids() {
26692671
assert_eq!(uid, Some(i));
26702672
}
26712673

2672-
// EVM association have been remapped correctly (uids: 7 -> 2, 14 -> 7)
2674+
// EVM association have been remapped correctly (uids: 6 -> 2, 14 -> 7)
26732675
assert_eq!(
26742676
AssociatedEvmAddress::<Test>::get(netuid, 2),
26752677
Some((sp_core::H160::from_slice(b"12345678901234567891"), now))
@@ -2865,3 +2867,23 @@ fn test_sudo_set_min_allowed_uids() {
28652867
);
28662868
});
28672869
}
2870+
2871+
#[test]
2872+
fn test_sudo_set_min_non_immune_uids() {
2873+
new_test_ext().execute_with(|| {
2874+
let netuid = NetUid::from(1);
2875+
add_network(netuid, 10);
2876+
2877+
let to_be_set: u16 = 12;
2878+
let init_value: u16 = SubtensorModule::get_min_non_immune_uids(netuid);
2879+
2880+
assert_ok!(AdminUtils::sudo_set_min_non_immune_uids(
2881+
<<Test as Config>::RuntimeOrigin>::root(),
2882+
netuid,
2883+
to_be_set
2884+
));
2885+
2886+
assert!(init_value != to_be_set);
2887+
assert_eq!(SubtensorModule::get_min_non_immune_uids(netuid), to_be_set);
2888+
});
2889+
}

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@ pub struct EpochTerms {
1818
pub stake_weight: u16,
1919
pub active: bool,
2020
pub emission: AlphaCurrency,
21-
pub rank: u16,
22-
pub trust: u16,
2321
pub consensus: u16,
24-
pub pruning_score: u16,
2522
pub validator_trust: u16,
2623
pub new_validator_permit: bool,
2724
pub bond: Vec<(u16, u16)>,
@@ -128,22 +125,16 @@ impl<T: Config> Pallet<T> {
128125

129126
let active = extract_from_sorted_terms!(terms_sorted, active);
130127
let emission = extract_from_sorted_terms!(terms_sorted, emission);
131-
let rank = extract_from_sorted_terms!(terms_sorted, rank);
132-
let trust = extract_from_sorted_terms!(terms_sorted, trust);
133128
let consensus = extract_from_sorted_terms!(terms_sorted, consensus);
134129
let dividend = extract_from_sorted_terms!(terms_sorted, dividend);
135-
let pruning_score = extract_from_sorted_terms!(terms_sorted, pruning_score);
136130
let validator_trust = extract_from_sorted_terms!(terms_sorted, validator_trust);
137131
let new_validator_permit = extract_from_sorted_terms!(terms_sorted, new_validator_permit);
138132
let stake_weight = extract_from_sorted_terms!(terms_sorted, stake_weight);
139133

140134
Active::<T>::insert(netuid, active.clone());
141135
Emission::<T>::insert(netuid, emission);
142-
Rank::<T>::insert(netuid, rank);
143-
Trust::<T>::insert(netuid, trust);
144136
Consensus::<T>::insert(netuid, consensus);
145137
Dividends::<T>::insert(netuid, dividend);
146-
PruningScores::<T>::insert(netuid, pruning_score);
147138
ValidatorTrust::<T>::insert(netuid, validator_trust);
148139
ValidatorPermit::<T>::insert(netuid, new_validator_permit);
149140
StakeWeight::<T>::insert(netuid, stake_weight);
@@ -325,9 +316,6 @@ impl<T: Config> Pallet<T> {
325316
// == Consensus, Validator Trust ==
326317
// ================================
327318

328-
// Compute preranks: r_j = SUM(i) w_ij * s_i
329-
let preranks: Vec<I32F32> = matmul(&weights, &active_stake);
330-
331319
// Consensus majority ratio, e.g. 51%.
332320
let kappa: I32F32 = Self::get_float_kappa(netuid);
333321
// Calculate consensus as stake-weighted median of weights.
@@ -345,9 +333,6 @@ impl<T: Config> Pallet<T> {
345333
// Compute ranks: r_j = SUM(i) w_ij * s_i
346334
let mut ranks: Vec<I32F32> = matmul(&clipped_weights, &active_stake);
347335

348-
// Compute server trust: ratio of rank after vs. rank before.
349-
let trust: Vec<I32F32> = vecdiv(&ranks, &preranks);
350-
351336
inplace_normalize(&mut ranks);
352337
let incentive: Vec<I32F32> = ranks.clone();
353338
log::trace!("I: {:?}", &incentive);
@@ -494,10 +479,6 @@ impl<T: Config> Pallet<T> {
494479
log::trace!("nCE: {:?}", &normalized_combined_emission);
495480
log::trace!("CE: {:?}", &combined_emission);
496481

497-
// Set pruning scores using combined emission scores.
498-
let pruning_scores: Vec<I32F32> = normalized_combined_emission.clone();
499-
log::trace!("P: {:?}", &pruning_scores);
500-
501482
// ===================
502483
// == Value storage ==
503484
// ===================
@@ -506,14 +487,6 @@ impl<T: Config> Pallet<T> {
506487
.iter()
507488
.map(|xi| fixed_proportion_to_u16(*xi))
508489
.collect::<Vec<u16>>();
509-
let cloned_ranks: Vec<u16> = ranks
510-
.iter()
511-
.map(|xi| fixed_proportion_to_u16(*xi))
512-
.collect::<Vec<u16>>();
513-
let cloned_trust: Vec<u16> = trust
514-
.iter()
515-
.map(|xi| fixed_proportion_to_u16(*xi))
516-
.collect::<Vec<u16>>();
517490
let cloned_consensus: Vec<u16> = consensus
518491
.iter()
519492
.map(|xi| fixed_proportion_to_u16(*xi))
@@ -526,20 +499,16 @@ impl<T: Config> Pallet<T> {
526499
.iter()
527500
.map(|xi| fixed_proportion_to_u16(*xi))
528501
.collect::<Vec<u16>>();
529-
let cloned_pruning_scores: Vec<u16> = vec_max_upscale_to_u16(&pruning_scores);
530502
let cloned_validator_trust: Vec<u16> = validator_trust
531503
.iter()
532504
.map(|xi| fixed_proportion_to_u16(*xi))
533505
.collect::<Vec<u16>>();
534506
StakeWeight::<T>::insert(netuid, cloned_stake_weight.clone());
535507
Active::<T>::insert(netuid, active.clone());
536508
Emission::<T>::insert(netuid, cloned_emission);
537-
Rank::<T>::insert(netuid, cloned_ranks);
538-
Trust::<T>::insert(netuid, cloned_trust);
539509
Consensus::<T>::insert(netuid, cloned_consensus);
540510
Incentive::<T>::insert(NetUidStorageIndex::from(netuid), cloned_incentive);
541511
Dividends::<T>::insert(netuid, cloned_dividends);
542-
PruningScores::<T>::insert(netuid, cloned_pruning_scores);
543512
ValidatorTrust::<T>::insert(netuid, cloned_validator_trust);
544513
ValidatorPermit::<T>::insert(netuid, new_validator_permits.clone());
545514

@@ -790,10 +759,6 @@ impl<T: Config> Pallet<T> {
790759
// == Consensus, Validator Trust ==
791760
// ================================
792761

793-
// Compute preranks: r_j = SUM(i) w_ij * s_i
794-
let preranks: Vec<I32F32> = matmul_sparse(&weights, &active_stake, n);
795-
log::trace!("Ranks (before): {:?}", &preranks);
796-
797762
// Consensus majority ratio, e.g. 51%.
798763
let kappa: I32F32 = Self::get_float_kappa(netuid);
799764
// Calculate consensus as stake-weighted median of weights.
@@ -814,11 +779,6 @@ impl<T: Config> Pallet<T> {
814779

815780
// Compute ranks: r_j = SUM(i) w_ij * s_i.
816781
let mut ranks: Vec<I32F32> = matmul_sparse(&clipped_weights, &active_stake, n);
817-
log::trace!("Ranks (after): {:?}", &ranks);
818-
819-
// Compute server trust: ratio of rank after vs. rank before.
820-
let trust: Vec<I32F32> = vecdiv(&ranks, &preranks); // range: I32F32(0, 1)
821-
log::trace!("Trust: {:?}", &trust);
822782

823783
inplace_normalize(&mut ranks); // range: I32F32(0, 1)
824784
let incentive: Vec<I32F32> = ranks.clone();
@@ -1004,10 +964,6 @@ impl<T: Config> Pallet<T> {
1004964
);
1005965
log::trace!("Combined Emission: {:?}", &combined_emission);
1006966

1007-
// Set pruning scores using combined emission scores.
1008-
let pruning_scores: Vec<I32F32> = normalized_combined_emission.clone();
1009-
log::trace!("Pruning Scores: {:?}", &pruning_scores);
1010-
1011967
// ===========================
1012968
// == Populate epoch output ==
1013969
// ===========================
@@ -1016,14 +972,6 @@ impl<T: Config> Pallet<T> {
1016972
.map(|xi| fixed_proportion_to_u16(*xi))
1017973
.collect::<Vec<u16>>();
1018974
let cloned_emission = combined_emission.clone();
1019-
let cloned_ranks: Vec<u16> = ranks
1020-
.iter()
1021-
.map(|xi| fixed_proportion_to_u16(*xi))
1022-
.collect::<Vec<u16>>();
1023-
let cloned_trust: Vec<u16> = trust
1024-
.iter()
1025-
.map(|xi| fixed_proportion_to_u16(*xi))
1026-
.collect::<Vec<u16>>();
1027975
let cloned_consensus: Vec<u16> = consensus
1028976
.iter()
1029977
.map(|xi| fixed_proportion_to_u16(*xi))
@@ -1036,7 +984,6 @@ impl<T: Config> Pallet<T> {
1036984
.iter()
1037985
.map(|xi| fixed_proportion_to_u16(*xi))
1038986
.collect::<Vec<u16>>();
1039-
let cloned_pruning_scores: Vec<u16> = vec_max_upscale_to_u16(&pruning_scores);
1040987
let cloned_validator_trust: Vec<u16> = validator_trust
1041988
.iter()
1042989
.map(|xi| fixed_proportion_to_u16(*xi))
@@ -1056,13 +1003,7 @@ impl<T: Config> Pallet<T> {
10561003
.unwrap_or_default();
10571004
terms.active = active.get(terms.uid).copied().unwrap_or_default();
10581005
terms.emission = cloned_emission.get(terms.uid).copied().unwrap_or_default();
1059-
terms.rank = cloned_ranks.get(terms.uid).copied().unwrap_or_default();
1060-
terms.trust = cloned_trust.get(terms.uid).copied().unwrap_or_default();
10611006
terms.consensus = cloned_consensus.get(terms.uid).copied().unwrap_or_default();
1062-
terms.pruning_score = cloned_pruning_scores
1063-
.get(terms.uid)
1064-
.copied()
1065-
.unwrap_or_default();
10661007
terms.validator_trust = cloned_validator_trust
10671008
.get(terms.uid)
10681009
.copied()

pallets/subtensor/src/lib.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1057,7 +1057,12 @@ pub mod pallet {
10571057
128
10581058
}
10591059

1060-
/// Global minimum activity cutoff value
1060+
/// Default value for MinNonImmuneUids.
1061+
#[pallet::type_value]
1062+
pub fn DefaultMinNonImmuneUids<T: Config>() -> u16 {
1063+
10u16
1064+
}
1065+
10611066
#[pallet::storage]
10621067
pub type MinActivityCutoff<T: Config> =
10631068
StorageValue<_, u16, ValueQuery, DefaultMinActivityCutoff<T>>;
@@ -2294,6 +2299,11 @@ pub mod pallet {
22942299
pub type NetworkRegistrationStartBlock<T> =
22952300
StorageValue<_, u64, ValueQuery, DefaultNetworkRegistrationStartBlock<T>>;
22962301

2302+
/// --- MAP ( netuid ) --> minimum required number of non-immortal & non-immune UIDs
2303+
#[pallet::storage]
2304+
pub type MinNonImmuneUids<T: Config> =
2305+
StorageMap<_, Identity, NetUid, u16, ValueQuery, DefaultMinNonImmuneUids<T>>;
2306+
22972307
/// ============================
22982308
/// ==== Subnet Mechanisms =====
22992309
/// ============================

0 commit comments

Comments
 (0)