Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BondsPenalty hyperparam #6

Open
wants to merge 3 commits into
base: devnet-ready
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions hyperparameters.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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;
Expand All @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions pallets/admin-utils/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<T>::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::<T>::init_new_network(1u16 /*netuid*/, 1u16 /*tempo*/);
Expand Down
25 changes: 25 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(<T as Config>::WeightInfo::sudo_set_bonds_penalty())]
pub fn sudo_set_bonds_penalty(
origin: OriginFor<T>,
netuid: u16,
bonds_penalty: u16,
) -> DispatchResult {
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin, netuid)?;

ensure!(
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
Error::<T>::SubnetDoesNotExist
);
pallet_subtensor::Pallet::<T>::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.
Expand Down
2 changes: 2 additions & 0 deletions pallets/admin-utils/src/tests/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ parameter_types! {
pub const InitialImmunityPeriod: u16 = 2;
pub const InitialMaxAllowedUids: u16 = 2;
pub const InitialBondsMovingAverage: u64 = 900_000;
pub const InitialBondsPenalty: u16 = 0;
pub const InitialStakePruningMin: u16 = 0;
pub const InitialFoundationDistribution: u64 = 0;
pub const InitialDefaultDelegateTake: u16 = 11_796; // 18% honest number.
Expand Down Expand Up @@ -163,6 +164,7 @@ impl pallet_subtensor::Config for Test {
type InitialMaxRegistrationsPerBlock = InitialMaxRegistrationsPerBlock;
type InitialPruningScore = InitialPruningScore;
type InitialBondsMovingAverage = InitialBondsMovingAverage;
type InitialBondsPenalty = InitialBondsPenalty;
type InitialMaxAllowedValidators = InitialMaxAllowedValidators;
type InitialDefaultDelegateTake = InitialDefaultDelegateTake;
type InitialMinDelegateTake = InitialMinDelegateTake;
Expand Down
33 changes: 33 additions & 0 deletions pallets/admin-utils/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,39 @@ fn test_sudo_set_bonds_moving_average() {
});
}

#[test]
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);
assert_eq!(
AdminUtils::sudo_set_bonds_penalty(
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
netuid,
to_be_set
),
Err(DispatchError::BadOrigin)
);
assert_eq!(
AdminUtils::sudo_set_bonds_penalty(
<<Test as Config>::RuntimeOrigin>::root(),
netuid + 1,
to_be_set
),
Err(Error::<Test>::SubnetDoesNotExist.into())
);
assert_eq!(SubtensorModule::get_bonds_penalty(netuid), init_value);
assert_ok!(AdminUtils::sudo_set_bonds_penalty(
<<Test as Config>::RuntimeOrigin>::root(),
netuid,
to_be_set
));
assert_eq!(SubtensorModule::get_bonds_penalty(netuid), to_be_set);
});
}

#[test]
fn test_sudo_set_rao_recycled() {
new_test_ext().execute_with(|| {
Expand Down
27 changes: 27 additions & 0 deletions pallets/admin-utils/src/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -182,6 +183,19 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
}
/// 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)
Expand Down Expand Up @@ -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)
Expand Down
84 changes: 84 additions & 0 deletions pallets/subtensor/src/epoch/math.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,90 @@ pub fn weighted_median_col_sparse(
median
}

// Element-wise interpolation of two matrices: Result = A + ratio * (B - A).
// ratio is has intended range [0, 1]
// ratio=0: Result = A
// ratio=1: Result = B
#[allow(dead_code)]
pub fn interpolate(mat1: &[Vec<I32F32>], mat2: &[Vec<I32F32>], ratio: I32F32) -> Vec<Vec<I32F32>> {
if ratio == I32F32::from_num(0) {
return mat1.to_owned();
}
if ratio == I32F32::from_num(1) {
return mat2.to_owned();
}
assert!(mat1.len() == mat2.len());
if mat1.is_empty() {
return vec![vec![]; 1];
}
if mat1.first().unwrap_or(&vec![]).is_empty() {
return vec![vec![]; 1];
}
let mut result: Vec<Vec<I32F32>> =
vec![vec![I32F32::from_num(0); mat1.first().unwrap_or(&vec![]).len()]; mat1.len()];
for (i, (row1, row2)) in mat1.iter().zip(mat2.iter()).enumerate() {
assert!(row1.len() == row2.len());
for (j, (&v1, &v2)) in row1.iter().zip(row2.iter()).enumerate() {
if let Some(res) = result.get_mut(i).unwrap_or(&mut vec![]).get_mut(j) {
*res = v1.saturating_add(ratio.saturating_mul(v2.saturating_sub(v1)));
}
}
}
result
}

// Element-wise interpolation of two sparse matrices: Result = A + ratio * (B - A).
// ratio is has intended range [0, 1]
// ratio=0: Result = A
// ratio=1: Result = B
#[allow(dead_code)]
pub fn interpolate_sparse(
mat1: &[Vec<(u16, I32F32)>],
mat2: &[Vec<(u16, I32F32)>],
columns: u16,
ratio: I32F32,
) -> Vec<Vec<(u16, I32F32)>> {
if ratio == I32F32::from_num(0) {
return mat1.to_owned();
}
if ratio == I32F32::from_num(1) {
return mat2.to_owned();
}
assert!(mat1.len() == mat2.len());
let rows = mat1.len();
let zero: I32F32 = I32F32::from_num(0);
let mut result: Vec<Vec<(u16, I32F32)>> = vec![vec![]; rows];
for i in 0..rows {
let mut row1: Vec<I32F32> = vec![zero; columns as usize];
if let Some(row) = mat1.get(i) {
for (j, value) in row {
if let Some(entry) = row1.get_mut(*j as usize) {
*entry = *value;
}
}
}
let mut row2: Vec<I32F32> = vec![zero; columns as usize];
if let Some(row) = mat2.get(i) {
for (j, value) in row {
if let Some(entry) = row2.get_mut(*j as usize) {
*entry = *value;
}
}
}
for j in 0..columns as usize {
let v1 = row1.get(j).unwrap_or(&zero);
let v2 = row2.get(j).unwrap_or(&zero);
let interp = v1.saturating_add(ratio.saturating_mul(v2.saturating_sub(*v1)));
if zero < interp {
if let Some(res) = result.get_mut(i) {
res.push((j as u16, interp));
}
}
}
}
result
}

// Element-wise product of two matrices.
#[allow(dead_code)]
pub fn hadamard(mat1: &[Vec<I32F32>], mat2: &[Vec<I32F32>]) -> Vec<Vec<I32F32>> {
Expand Down
Loading