Skip to content

Commit 27ddaff

Browse files
committed
Simplify bonds calculation (no subsubnet logic for ema)
1 parent 2c447fd commit 27ddaff

File tree

6 files changed

+52
-58
lines changed

6 files changed

+52
-58
lines changed

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ impl<T: Config> Pallet<T> {
286286
let owner_uid: Option<u16> = Self::get_owner_uid(netuid);
287287

288288
// Access network weights row unnormalized.
289-
let mut weights: Vec<Vec<I32F32>> = Self::get_weights(netuid);
289+
let mut weights: Vec<Vec<I32F32>> = Self::get_weights(netuid_index);
290290
log::trace!("W: {:?}", &weights);
291291

292292
// Mask weights that are not from permitted validators.
@@ -363,7 +363,7 @@ impl<T: Config> Pallet<T> {
363363
log::trace!("B: {:?}", &bonds);
364364

365365
// Compute the Exponential Moving Average (EMA) of bonds.
366-
ema_bonds = Self::compute_bonds(netuid_index, &weights_for_bonds, &bonds, &consensus);
366+
ema_bonds = Self::compute_bonds(netuid, &weights_for_bonds, &bonds, &consensus);
367367
log::trace!("emaB: {:?}", &ema_bonds);
368368

369369
// Normalize EMA bonds.
@@ -397,7 +397,7 @@ impl<T: Config> Pallet<T> {
397397
log::trace!("ΔB: {:?}", &bonds_delta);
398398

399399
// Compute the Exponential Moving Average (EMA) of bonds.
400-
ema_bonds = Self::compute_ema_bonds_normal(&bonds_delta, &bonds, netuid_index);
400+
ema_bonds = Self::compute_ema_bonds_normal(&bonds_delta, &bonds, netuid);
401401
inplace_col_normalize(&mut ema_bonds); // sum_i b_ij = 1
402402
log::trace!("emaB: {:?}", &ema_bonds);
403403

@@ -706,7 +706,7 @@ impl<T: Config> Pallet<T> {
706706
let owner_uid: Option<u16> = Self::get_owner_uid(netuid);
707707

708708
// Access network weights row unnormalized.
709-
let mut weights: Vec<Vec<(u16, I32F32)>> = Self::get_weights_sparse(netuid);
709+
let mut weights: Vec<Vec<(u16, I32F32)>> = Self::get_weights_sparse(netuid_index);
710710
log::trace!("Weights: {:?}", &weights);
711711

712712
// Mask weights that are not from permitted validators.
@@ -1608,47 +1608,42 @@ impl<T: Config> Pallet<T> {
16081608
}
16091609

16101610
/// Output unnormalized sparse weights, input weights are assumed to be row max-upscaled in u16.
1611-
pub fn get_weights_sparse(netuid: NetUid) -> Vec<Vec<(u16, I32F32)>> {
1611+
pub fn get_weights_sparse(netuid_index: NetUidStorageIndex) -> Vec<Vec<(u16, I32F32)>> {
1612+
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
16121613
let n = Self::get_subnetwork_n(netuid) as usize;
16131614
let mut weights: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
1614-
for (uid_i, weights_i) in <Weights<T> as IterableStorageDoubleMap<
1615-
NetUidStorageIndex,
1616-
u16,
1617-
Vec<(u16, u16)>,
1618-
>>::iter_prefix(NetUidStorageIndex::from(netuid))
1615+
for (uid_i, weights_i) in Weights::<T>::iter_prefix(netuid_index)
16191616
.filter(|(uid_i, _)| *uid_i < n as u16)
16201617
{
16211618
for (uid_j, weight_ij) in weights_i.iter().filter(|(uid_j, _)| *uid_j < n as u16) {
1622-
weights
1623-
.get_mut(uid_i as usize)
1624-
.expect("uid_i is filtered to be less than n; qed")
1625-
.push((*uid_j, I32F32::saturating_from_num(*weight_ij)));
1619+
if let Some(row) = weights.get_mut(uid_i as usize) {
1620+
row.push((*uid_j, I32F32::saturating_from_num(*weight_ij)));
1621+
} else {
1622+
log::error!("uid_i {:?} is filtered to be less than n", uid_i);
1623+
}
16261624
}
16271625
}
16281626
weights
16291627
}
16301628

16311629
/// Output unnormalized weights in [n, n] matrix, input weights are assumed to be row max-upscaled in u16.
1632-
pub fn get_weights(netuid: NetUid) -> Vec<Vec<I32F32>> {
1630+
pub fn get_weights(netuid_index: NetUidStorageIndex) -> Vec<Vec<I32F32>> {
1631+
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
16331632
let n = Self::get_subnetwork_n(netuid) as usize;
16341633
let mut weights: Vec<Vec<I32F32>> = vec![vec![I32F32::saturating_from_num(0.0); n]; n];
1635-
for (uid_i, weights_vec) in <Weights<T> as IterableStorageDoubleMap<
1636-
NetUidStorageIndex,
1637-
u16,
1638-
Vec<(u16, u16)>,
1639-
>>::iter_prefix(NetUidStorageIndex::from(netuid))
1634+
for (uid_i, weights_vec) in Weights::<T>::iter_prefix(netuid_index)
16401635
.filter(|(uid_i, _)| *uid_i < n as u16)
16411636
{
16421637
for (uid_j, weight_ij) in weights_vec
16431638
.into_iter()
16441639
.filter(|(uid_j, _)| *uid_j < n as u16)
16451640
{
1646-
*weights
1641+
if let Some(cell) = weights
16471642
.get_mut(uid_i as usize)
1648-
.expect("uid_i is filtered to be less than n; qed")
1649-
.get_mut(uid_j as usize)
1650-
.expect("uid_j is filtered to be less than n; qed") =
1651-
I32F32::saturating_from_num(weight_ij);
1643+
.and_then(|row| row.get_mut(uid_j as usize))
1644+
{
1645+
*cell = I32F32::saturating_from_num(weight_ij);
1646+
}
16521647
}
16531648
}
16541649
weights
@@ -1730,8 +1725,10 @@ impl<T: Config> Pallet<T> {
17301725
pub fn compute_ema_bonds_normal_sparse(
17311726
bonds_delta: &[Vec<(u16, I32F32)>],
17321727
bonds: &[Vec<(u16, I32F32)>],
1733-
netuid: NetUidStorageIndex,
1728+
netuid_index: NetUidStorageIndex,
17341729
) -> Vec<Vec<(u16, I32F32)>> {
1730+
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
1731+
17351732
// Retrieve the bonds moving average for the given network ID and scale it down.
17361733
let bonds_moving_average: I64F64 =
17371734
I64F64::saturating_from_num(Self::get_bonds_moving_average(netuid))
@@ -1764,7 +1761,7 @@ impl<T: Config> Pallet<T> {
17641761
pub fn compute_ema_bonds_normal(
17651762
bonds_delta: &[Vec<I32F32>],
17661763
bonds: &[Vec<I32F32>],
1767-
netuid: NetUidStorageIndex,
1764+
netuid: NetUid,
17681765
) -> Vec<Vec<I32F32>> {
17691766
// Retrieve the bonds moving average for the given network ID and scale it down.
17701767
let bonds_moving_average: I64F64 =
@@ -1798,13 +1795,11 @@ impl<T: Config> Pallet<T> {
17981795
/// # Returns:
17991796
/// A vector of EMA bonds.
18001797
pub fn compute_bonds(
1801-
netuid_index: NetUidStorageIndex,
1798+
netuid: NetUid,
18021799
weights: &[Vec<I32F32>], // weights_for_bonds
18031800
bonds: &[Vec<I32F32>],
18041801
consensus: &[I32F32],
18051802
) -> Vec<Vec<I32F32>> {
1806-
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
1807-
18081803
// Check if Liquid Alpha is enabled, consensus is not empty, and contains non-zero values.
18091804
if LiquidAlphaOn::<T>::get(netuid)
18101805
&& !consensus.is_empty()
@@ -1821,7 +1816,7 @@ impl<T: Config> Pallet<T> {
18211816
mat_ema_alpha(weights, bonds, &alphas)
18221817
} else {
18231818
// Liquid Alpha is disabled, compute the liquid alpha value.
1824-
let alpha: I32F32 = Self::compute_disabled_liquid_alpha(netuid_index);
1819+
let alpha: I32F32 = Self::compute_disabled_liquid_alpha(netuid);
18251820

18261821
// Compute the Exponential Moving Average (EMA) of bonds using the calculated alpha value.
18271822
mat_ema(weights, bonds, alpha)
@@ -1863,7 +1858,7 @@ impl<T: Config> Pallet<T> {
18631858
mat_ema_alpha_sparse(weights, bonds, &alphas)
18641859
} else {
18651860
// Liquid Alpha is disabled, compute the liquid alpha value.
1866-
let alpha: I32F32 = Self::compute_disabled_liquid_alpha(netuid_index);
1861+
let alpha: I32F32 = Self::compute_disabled_liquid_alpha(netuid);
18671862

18681863
// Compute the Exponential Moving Average (EMA) of bonds using the calculated alpha value.
18691864
mat_ema_sparse(weights, bonds, alpha)
@@ -2018,7 +2013,7 @@ impl<T: Config> Pallet<T> {
20182013
clamp_value(alpha, alpha_low, alpha_high)
20192014
}
20202015

2021-
pub fn compute_disabled_liquid_alpha(netuid: NetUidStorageIndex) -> I32F32 {
2016+
pub fn compute_disabled_liquid_alpha(netuid: NetUid) -> I32F32 {
20222017
// Retrieve the bonds moving average for the given network ID and scale it down.
20232018
let bonds_moving_average: I64F64 = I64F64::from_num(Self::get_bonds_moving_average(netuid))
20242019
.saturating_div(I64F64::from_num(1_000_000));

pallets/subtensor/src/rpc_info/metagraph.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ impl<T: Config> Pallet<T> {
727727
liquid_alpha_enabled: Self::get_liquid_alpha_enabled(netuid), // Bonds liquid enabled.
728728
alpha_high: Self::get_alpha_values(netuid).1.into(), // Alpha param high
729729
alpha_low: Self::get_alpha_values(netuid).0.into(), // Alpha param low
730-
bonds_moving_avg: Self::get_bonds_moving_average(NetUidStorageIndex::from(netuid))
730+
bonds_moving_avg: Self::get_bonds_moving_average(netuid)
731731
.into(), // Bonds moving avg
732732

733733
// Metagraph info.
@@ -1115,7 +1115,7 @@ impl<T: Config> Pallet<T> {
11151115
Some(SelectiveMetagraphIndex::BondsMovingAvg) => SelectiveMetagraph {
11161116
netuid: netuid.into(),
11171117
bonds_moving_avg: Some(
1118-
Self::get_bonds_moving_average(NetUidStorageIndex::from(netuid)).into(),
1118+
Self::get_bonds_moving_average(netuid).into(),
11191119
),
11201120
..Default::default()
11211121
},

pallets/subtensor/src/rpc_info/subnet_info.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use frame_support::storage::IterableStorageMap;
44
extern crate alloc;
55
use codec::Compact;
66
use substrate_fixed::types::I32F32;
7-
use subtensor_runtime_common::{NetUid, NetUidStorageIndex, TaoCurrency};
7+
use subtensor_runtime_common::{NetUid, TaoCurrency};
88

99
#[freeze_struct("edd6bd3273dfea76")]
1010
#[derive(Decode, Encode, PartialEq, Eq, Clone, Debug, TypeInfo)]
@@ -286,7 +286,7 @@ impl<T: Config> Pallet<T> {
286286
let target_regs_per_interval = Self::get_target_registrations_per_interval(netuid);
287287
let min_burn = Self::get_min_burn(netuid);
288288
let max_burn = Self::get_max_burn(netuid);
289-
let bonds_moving_avg = Self::get_bonds_moving_average(NetUidStorageIndex::from(netuid));
289+
let bonds_moving_avg = Self::get_bonds_moving_average(netuid);
290290
let max_regs_per_block = Self::get_max_registrations_per_block(netuid);
291291
let serving_rate_limit = Self::get_serving_rate_limit(netuid);
292292
let max_validators = Self::get_max_allowed_validators(netuid);
@@ -349,7 +349,7 @@ impl<T: Config> Pallet<T> {
349349
let target_regs_per_interval = Self::get_target_registrations_per_interval(netuid);
350350
let min_burn = Self::get_min_burn(netuid);
351351
let max_burn = Self::get_max_burn(netuid);
352-
let bonds_moving_avg = Self::get_bonds_moving_average(NetUidStorageIndex::from(netuid));
352+
let bonds_moving_avg = Self::get_bonds_moving_average(netuid);
353353
let max_regs_per_block = Self::get_max_registrations_per_block(netuid);
354354
let serving_rate_limit = Self::get_serving_rate_limit(netuid);
355355
let max_validators = Self::get_max_allowed_validators(netuid);

pallets/subtensor/src/tests/epoch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3712,7 +3712,7 @@ fn test_epoch_no_mask_when_commit_reveal_disabled() {
37123712
for _ in 0..3 {
37133713
SubtensorModule::epoch(netuid, 1.into());
37143714
assert!(
3715-
!SubtensorModule::get_weights_sparse(netuid)[0].is_empty(),
3715+
!SubtensorModule::get_weights_sparse(netuid.into())[0].is_empty(),
37163716
"row visible when CR disabled"
37173717
);
37183718
run_to_block(System::block_number() + tempo as u64 + 1);

pallets/subtensor/src/tests/weights.rs

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1488,7 +1488,7 @@ fn test_set_weights_sum_larger_than_u16_max() {
14881488
assert_ok!(result);
14891489

14901490
// Get max-upscaled unnormalized weights.
1491-
let all_weights: Vec<Vec<I32F32>> = SubtensorModule::get_weights(netuid);
1491+
let all_weights: Vec<Vec<I32F32>> = SubtensorModule::get_weights(netuid.into());
14921492
let weights_set: &[I32F32] = &all_weights[neuron_uid as usize];
14931493
assert_eq!(weights_set[0], I32F32::from_num(u16::MAX));
14941494
assert_eq!(weights_set[1], I32F32::from_num(u16::MAX));
@@ -5113,7 +5113,7 @@ fn test_reveal_crv3_commits_success() {
51135113
// Step epochs to run the epoch via the blockstep
51145114
step_epochs(3, netuid);
51155115

5116-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
5116+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
51175117
let weights = weights_sparse.get(neuron_uid1 as usize).cloned().unwrap_or_default();
51185118

51195119
assert!(
@@ -5235,7 +5235,7 @@ fn test_reveal_crv3_commits_cannot_reveal_after_reveal_epoch() {
52355235
step_epochs(3, netuid);
52365236

52375237
// Verify that weights are not set
5238-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
5238+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
52395239
let weights = weights_sparse
52405240
.get(neuron_uid1 as usize)
52415241
.cloned()
@@ -5270,7 +5270,7 @@ fn test_reveal_crv3_commits_cannot_reveal_after_reveal_epoch() {
52705270
assert_ok!(SubtensorModule::reveal_crv3_commits(netuid));
52715271

52725272
// Verify that the weights for the neuron have not been set
5273-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
5273+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
52745274
let weights = weights_sparse
52755275
.get(neuron_uid1 as usize)
52765276
.cloned()
@@ -5607,7 +5607,7 @@ fn test_reveal_crv3_commits_decryption_failure() {
56075607

56085608
let neuron_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey)
56095609
.expect("Failed to get neuron UID for hotkey") as usize;
5610-
let weights_matrix = SubtensorModule::get_weights(netuid);
5610+
let weights_matrix = SubtensorModule::get_weights(netuid.into());
56115611
let weights = weights_matrix.get(neuron_uid).cloned().unwrap_or_default();
56125612
assert!(weights.iter().all(|&w| w == I32F32::from_num(0)));
56135613
});
@@ -5720,7 +5720,7 @@ fn test_reveal_crv3_commits_multiple_commits_some_fail_some_succeed() {
57205720
// Verify that weights are set for hotkey1
57215721
let neuron_uid1 = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey1)
57225722
.expect("Failed to get neuron UID for hotkey1") as usize;
5723-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
5723+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
57245724
let weights1 = weights_sparse.get(neuron_uid1).cloned().unwrap_or_default();
57255725
assert!(
57265726
!weights1.is_empty(),
@@ -5815,7 +5815,7 @@ fn test_reveal_crv3_commits_do_set_weights_failure() {
58155815
// Verify that weights are not set due to `do_set_weights` failure
58165816
let neuron_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey)
58175817
.expect("Failed to get neuron UID for hotkey") as usize;
5818-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
5818+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
58195819
let weights = weights_sparse.get(neuron_uid).cloned().unwrap_or_default();
58205820
assert!(
58215821
weights.is_empty(),
@@ -5893,7 +5893,7 @@ fn test_reveal_crv3_commits_payload_decoding_failure() {
58935893
// Verify that weights are not set
58945894
let neuron_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey)
58955895
.expect("Failed to get neuron UID for hotkey") as usize;
5896-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
5896+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
58975897
let weights = weights_sparse.get(neuron_uid).cloned().unwrap_or_default();
58985898
assert!(
58995899
weights.is_empty(),
@@ -5975,7 +5975,7 @@ fn test_reveal_crv3_commits_signature_deserialization_failure() {
59755975
// Verify that weights are not set
59765976
let neuron_uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hotkey)
59775977
.expect("Failed to get neuron UID for hotkey") as usize;
5978-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
5978+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
59795979
let weights = weights_sparse.get(neuron_uid).cloned().unwrap_or_default();
59805980
assert!(
59815981
weights.is_empty(),
@@ -6040,7 +6040,7 @@ fn test_reveal_crv3_commits_with_empty_commit_queue() {
60406040

60416041
step_epochs(2, netuid);
60426042

6043-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
6043+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
60446044
assert!(
60456045
weights_sparse.is_empty(),
60466046
"Weights should be empty as there were no commits to reveal"
@@ -6127,7 +6127,7 @@ fn test_reveal_crv3_commits_with_incorrect_identity_message() {
61276127

61286128
// Verify that weights are not set due to decryption failure
61296129
let neuron_uid = neuron_uid as usize;
6130-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
6130+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
61316131
let weights = weights_sparse.get(neuron_uid).cloned().unwrap_or_default();
61326132
assert!(
61336133
weights.is_empty(),
@@ -6337,7 +6337,7 @@ fn test_reveal_crv3_commits_multiple_valid_commits_all_processed() {
63376337
step_epochs(2, netuid);
63386338

63396339
// ───── assertions ───────────────────────────────────────────────────
6340-
let w_sparse = SubtensorModule::get_weights_sparse(netuid);
6340+
let w_sparse = SubtensorModule::get_weights_sparse(netuid.into());
63416341
for hk in hotkeys {
63426342
let uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, &hk).unwrap() as usize;
63436343
assert!(
@@ -6452,7 +6452,7 @@ fn test_reveal_crv3_commits_max_neurons() {
64526452
step_epochs(2, netuid);
64536453

64546454
// ───── verify weights ───────────────────────────────────────────────
6455-
let w_sparse = SubtensorModule::get_weights_sparse(netuid);
6455+
let w_sparse = SubtensorModule::get_weights_sparse(netuid.into());
64566456
for hk in &committing_hotkeys {
64576457
let uid = SubtensorModule::get_uid_for_net_and_hotkey(netuid, hk).unwrap() as usize;
64586458
assert!(
@@ -6682,7 +6682,7 @@ fn test_reveal_crv3_commits_hotkey_check() {
66826682
// Step epochs to run the epoch via the blockstep
66836683
step_epochs(3, netuid);
66846684

6685-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
6685+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
66866686
let weights = weights_sparse.get(neuron_uid1 as usize).cloned().unwrap_or_default();
66876687

66886688
assert!(
@@ -6799,7 +6799,7 @@ fn test_reveal_crv3_commits_hotkey_check() {
67996799
// Step epochs to run the epoch via the blockstep
68006800
step_epochs(3, netuid);
68016801

6802-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
6802+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
68036803
let weights = weights_sparse.get(neuron_uid1 as usize).cloned().unwrap_or_default();
68046804

68056805
assert!(
@@ -6937,7 +6937,7 @@ fn test_reveal_crv3_commits_retry_on_missing_pulse() {
69376937

69386938
step_block(1); // automatic reveal runs here
69396939

6940-
let weights = SubtensorModule::get_weights_sparse(netuid)
6940+
let weights = SubtensorModule::get_weights_sparse(netuid.into())
69416941
.get(uid as usize)
69426942
.cloned()
69436943
.unwrap_or_default();
@@ -7072,7 +7072,7 @@ fn test_reveal_crv3_commits_legacy_payload_success() {
70727072
// ─────────────────────────────────────
70737073
// 5 ▸ assertions
70747074
// ─────────────────────────────────────
7075-
let weights_sparse = SubtensorModule::get_weights_sparse(netuid);
7075+
let weights_sparse = SubtensorModule::get_weights_sparse(netuid.into());
70767076
let w1 = weights_sparse
70777077
.get(uid1 as usize)
70787078
.cloned()

pallets/subtensor/src/utils/misc.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,8 +576,7 @@ impl<T: Config> Pallet<T> {
576576
));
577577
}
578578

579-
pub fn get_bonds_moving_average(netuid_index: NetUidStorageIndex) -> u64 {
580-
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
579+
pub fn get_bonds_moving_average(netuid: NetUid) -> u64 {
581580
BondsMovingAverage::<T>::get(netuid)
582581
}
583582
pub fn set_bonds_moving_average(netuid: NetUid, bonds_moving_average: u64) {

0 commit comments

Comments
 (0)