From 43a13cf5cbb11b0f52896ecc2752bc9334171c66 Mon Sep 17 00:00:00 2001 From: Andreea Popescu Date: Wed, 15 Jan 2025 14:19:24 +0000 Subject: [PATCH] add bonds_penalty admin-utils --- hyperparameters.md | 8 +++-- pallets/admin-utils/src/benchmarking.rs | 8 +++++ pallets/admin-utils/src/lib.rs | 25 +++++++++++++ pallets/admin-utils/src/tests/mod.rs | 14 ++++---- pallets/admin-utils/src/weights.rs | 27 ++++++++++++++ pallets/subtensor/src/epoch/math.rs | 4 +-- pallets/subtensor/src/tests/epoch.rs | 10 +++--- pallets/subtensor/src/tests/math.rs | 48 ++++++++++++------------- 8 files changed, 103 insertions(+), 41 deletions(-) diff --git a/hyperparameters.md b/hyperparameters.md index 96f955437..c8d2ce110 100644 --- a/hyperparameters.md +++ b/hyperparameters.md @@ -7,7 +7,7 @@ TxRateLimit: u64 = 1; // [1 @ 64,888] ### netuid 1 (text_prompting) ```rust Rho: u16 = 10; -Kappa: u16 = 32_767; // 0.5 = 65535/2 +Kappa: u16 = 32_767; // 0.5 = 65535/2 MaxAllowedUids: u16 = 1024; Issuance: u64 = 0; MinAllowedWeights: u16 = 8; @@ -32,10 +32,11 @@ ActivityCutoff: u16 = 5000; MaxRegistrationsPerBlock: u16 = 1; PruningScore : u16 = u16::MAX; BondsMovingAverage: u64 = 900_000; +BondsPenalty: u16 = 0; WeightsVersionKey: u64 = 1020; MinDifficulty: u64 = 10_000_000; MaxDifficulty: u64 = u64::MAX / 4; -ServingRateLimit: u64 = 10; +ServingRateLimit: u64 = 10; Burn: u64 = 1_000_000_000; // 1 tao MinBurn: u64 = 1_000_000_000; // 1 tao MaxBurn: u64 = 100_000_000_000; // 100 tao @@ -45,7 +46,7 @@ WeightsSetRateLimit: u64 = 100; ### netuid 3 (causallmnext) ```rust Rho: u16 = 10; -Kappa: u16 = 32_767; // 0.5 = 65535/2 +Kappa: u16 = 32_767; // 0.5 = 65535/2 MaxAllowedUids: u16 = 4096; Issuance: u64 = 0; MinAllowedWeights: u16 = 50; @@ -70,6 +71,7 @@ ActivityCutoff: u16 = 5000; // [5000 @ 7,163] MaxRegistrationsPerBlock: u16 = 1; PruningScore : u16 = u16::MAX; BondsMovingAverage: u64 = 900_000; +BondsPenalty: u16 = 0; WeightsVersionKey: u64 = 400; MinDifficulty: u64 = 10_000_000; MaxDifficulty: u64 = u64::MAX / 4; diff --git a/pallets/admin-utils/src/benchmarking.rs b/pallets/admin-utils/src/benchmarking.rs index abbcd0f16..af9b68051 100644 --- a/pallets/admin-utils/src/benchmarking.rs +++ b/pallets/admin-utils/src/benchmarking.rs @@ -100,6 +100,14 @@ mod benchmarks { _(RawOrigin::Root, 1u16/*netuid*/, 100u64/*bonds_moving_average*/)/*sudo_set_bonds_moving_average*/; } + #[benchmark] + fn sudo_set_bonds_penalty() { + pallet_subtensor::Pallet::::init_new_network(1u16 /*netuid*/, 1u16 /*tempo*/); + + #[extrinsic_call] + _(RawOrigin::Root, 1u16/*netuid*/, 100u16/*bonds_penalty*/)/*sudo_set_bonds_penalty*/; + } + #[benchmark] fn sudo_set_max_allowed_validators() { pallet_subtensor::Pallet::::init_new_network(1u16 /*netuid*/, 1u16 /*tempo*/); diff --git a/pallets/admin-utils/src/lib.rs b/pallets/admin-utils/src/lib.rs index f6b132148..7de39aa38 100644 --- a/pallets/admin-utils/src/lib.rs +++ b/pallets/admin-utils/src/lib.rs @@ -685,6 +685,31 @@ pub mod pallet { Ok(()) } + /// The extrinsic sets the bonds penalty for a subnet. + /// It is only callable by the root account or subnet owner. + /// The extrinsic will call the Subtensor pallet to set the bonds penalty. + #[pallet::call_index(60)] + #[pallet::weight(::WeightInfo::sudo_set_bonds_penalty())] + pub fn sudo_set_bonds_penalty( + origin: OriginFor, + netuid: u16, + bonds_penalty: u16, + ) -> DispatchResult { + pallet_subtensor::Pallet::::ensure_subnet_owner_or_root(origin, netuid)?; + + ensure!( + pallet_subtensor::Pallet::::if_subnet_exist(netuid), + Error::::SubnetDoesNotExist + ); + pallet_subtensor::Pallet::::set_bonds_penalty(netuid, bonds_penalty); + log::debug!( + "BondsPenalty( netuid: {:?} bonds_penalty: {:?} ) ", + netuid, + bonds_penalty + ); + Ok(()) + } + /// The extrinsic sets the maximum registrations per block for a subnet. /// It is only callable by the root account. /// The extrinsic will call the Subtensor pallet to set the maximum registrations per block. diff --git a/pallets/admin-utils/src/tests/mod.rs b/pallets/admin-utils/src/tests/mod.rs index e1294678b..a3b771444 100644 --- a/pallets/admin-utils/src/tests/mod.rs +++ b/pallets/admin-utils/src/tests/mod.rs @@ -746,26 +746,26 @@ fn test_sudo_set_bonds_penalty() { new_test_ext().execute_with(|| { let netuid: u16 = 1; let to_be_set: u16 = 10; + add_network(netuid, 10); let init_value: u16 = SubtensorModule::get_bonds_penalty(netuid); - add_network(netuid, 10, 0); assert_eq!( - SubtensorModule::sudo_set_bonds_penalty( - <::RuntimeOrigin>::signed(U256::from(0)), + AdminUtils::sudo_set_bonds_penalty( + <::RuntimeOrigin>::signed(U256::from(1)), netuid, to_be_set ), - Err(DispatchError::BadOrigin.into()) + Err(DispatchError::BadOrigin) ); assert_eq!( - SubtensorModule::sudo_set_bonds_penalty( + AdminUtils::sudo_set_bonds_penalty( <::RuntimeOrigin>::root(), netuid + 1, to_be_set ), - Err(Error::::NetworkDoesNotExist.into()) + Err(Error::::SubnetDoesNotExist.into()) ); assert_eq!(SubtensorModule::get_bonds_penalty(netuid), init_value); - assert_ok!(SubtensorModule::sudo_set_bonds_penalty( + assert_ok!(AdminUtils::sudo_set_bonds_penalty( <::RuntimeOrigin>::root(), netuid, to_be_set diff --git a/pallets/admin-utils/src/weights.rs b/pallets/admin-utils/src/weights.rs index cb7017023..6ef952354 100644 --- a/pallets/admin-utils/src/weights.rs +++ b/pallets/admin-utils/src/weights.rs @@ -42,6 +42,7 @@ pub trait WeightInfo { fn sudo_set_weights_set_rate_limit() -> Weight; fn sudo_set_weights_version_key() -> Weight; fn sudo_set_bonds_moving_average() -> Weight; + fn sudo_set_bonds_penalty() -> Weight; fn sudo_set_max_allowed_validators() -> Weight; fn sudo_set_difficulty() -> Weight; fn sudo_set_adjustment_interval() -> Weight; @@ -182,6 +183,19 @@ impl WeightInfo for SubstrateWeight { } /// Storage: SubtensorModule NetworksAdded (r:1 w:0) /// Proof Skipped: SubtensorModule NetworksAdded (max_values: None, max_size: None, mode: Measured) + /// Storage: SubtensorModule BondsPenalty (r:0 w:1) + /// Proof Skipped: SubtensorModule BondsPenalty (max_values: None, max_size: None, mode: Measured) + fn sudo_set_bonds_penalty() -> Weight { + // Proof Size summary in bytes: + // Measured: `1111` + // Estimated: `4697` + // Minimum execution time: 46_099_000 picoseconds. + Weight::from_parts(47_510_000, 4697) + .saturating_add(T::DbWeight::get().reads(1_u64)) + .saturating_add(T::DbWeight::get().writes(1_u64)) + } + /// Storage: SubtensorModule NetworksAdded (r:1 w:0) + /// Proof Skipped: SubtensorModule NetworksAdded (max_values: None, max_size: None, mode: Measured) /// Storage: SubtensorModule MaxAllowedUids (r:1 w:0) /// Proof Skipped: SubtensorModule MaxAllowedUids (max_values: None, max_size: None, mode: Measured) /// Storage: SubtensorModule MaxAllowedValidators (r:0 w:1) @@ -559,6 +573,19 @@ impl WeightInfo for () { } /// Storage: SubtensorModule NetworksAdded (r:1 w:0) /// Proof Skipped: SubtensorModule NetworksAdded (max_values: None, max_size: None, mode: Measured) + /// Storage: SubtensorModule BondsPenalty (r:0 w:1) + /// Proof Skipped: SubtensorModule BondsPenalty (max_values: None, max_size: None, mode: Measured) + fn sudo_set_bonds_penalty() -> Weight { + // Proof Size summary in bytes: + // Measured: `1111` + // Estimated: `4697` + // Minimum execution time: 46_099_000 picoseconds. + Weight::from_parts(47_510_000, 4697) + .saturating_add(RocksDbWeight::get().reads(1_u64)) + .saturating_add(RocksDbWeight::get().writes(1_u64)) + } + /// Storage: SubtensorModule NetworksAdded (r:1 w:0) + /// Proof Skipped: SubtensorModule NetworksAdded (max_values: None, max_size: None, mode: Measured) /// Storage: SubtensorModule MaxAllowedUids (r:1 w:0) /// Proof Skipped: SubtensorModule MaxAllowedUids (max_values: None, max_size: None, mode: Measured) /// Storage: SubtensorModule MaxAllowedValidators (r:0 w:1) diff --git a/pallets/subtensor/src/epoch/math.rs b/pallets/subtensor/src/epoch/math.rs index 3f7c5d89d..616a9b78b 100644 --- a/pallets/subtensor/src/epoch/math.rs +++ b/pallets/subtensor/src/epoch/math.rs @@ -1023,7 +1023,7 @@ pub fn weighted_median_col_sparse( } // Element-wise interpolation of two matrices: Result = A + ratio * (B - A). -// ratio is has intended range [0, 1] +// ratio has intended range [0, 1] // ratio=0: Result = A // ratio=1: Result = B #[allow(dead_code)] @@ -1055,7 +1055,7 @@ pub fn interpolate(mat1: &[Vec], mat2: &[Vec], ratio: I32F32) -> } // Element-wise interpolation of two sparse matrices: Result = A + ratio * (B - A). -// ratio is has intended range [0, 1] +// ratio has intended range [0, 1] // ratio=0: Result = A // ratio=1: Result = B #[allow(dead_code)] diff --git a/pallets/subtensor/src/tests/epoch.rs b/pallets/subtensor/src/tests/epoch.rs index 379936738..7973a44e1 100644 --- a/pallets/subtensor/src/tests/epoch.rs +++ b/pallets/subtensor/src/tests/epoch.rs @@ -756,7 +756,7 @@ fn test_512_graph_random_weights() { for interleave in 0..3 { // server-self weight off/on for server_self in [false, true] { - for bonds_penalty in vec![0, u16::MAX / 2, u16::MAX] { + for bonds_penalty in [0, u16::MAX / 2, u16::MAX] { let (validators, servers) = distribute_nodes( validators_n as usize, network_n as usize, @@ -783,9 +783,9 @@ fn test_512_graph_random_weights() { epochs, 1, server_self, - &vec![], + &[], false, - &vec![], + &[], false, true, interleave as u64, @@ -814,9 +814,9 @@ fn test_512_graph_random_weights() { epochs, 1, server_self, - &vec![], + &[], false, - &vec![], + &[], false, true, interleave as u64, diff --git a/pallets/subtensor/src/tests/math.rs b/pallets/subtensor/src/tests/math.rs index 6eb51bc29..84fcc4aac 100644 --- a/pallets/subtensor/src/tests/math.rs +++ b/pallets/subtensor/src/tests/math.rs @@ -1803,18 +1803,18 @@ fn test_math_interpolate() { assert_mat_compare(&result, &target, I32F32::from_num(0)); let target: Vec = vec![ - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, ]; let ratio = I32F32::from_num(0.9999998808); let target = vec_to_mat_fixed(&target, 4, false); @@ -1925,18 +1925,18 @@ fn test_math_interpolate_sparse() { assert_sparse_mat_compare(&result, &target, I32F32::from_num(0)); let target: Vec = vec![ - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, - 0.9999998808, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, + 0.999_999_9, ]; let ratio = I32F32::from_num(0.9999998808); let target = vec_to_sparse_mat_fixed(&target, 4, false);