Skip to content
Closed
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
10 changes: 10 additions & 0 deletions pallets/subtensor/src/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,16 @@ benchmarks! {

}: sudo_set_bonds_moving_average(RawOrigin::<AccountIdOf<T>>::Root, netuid, bonds_moving_average)

benchmark_sudo_set_bonds_penalty {
let netuid: u16 = 1;
let bonds_penalty: u16 = 100;
let tempo: u16 = 1;
let modality: u16 = 0;

assert_ok!( Subtensor::<T>::do_add_network( RawOrigin::Root.into(), netuid.try_into().unwrap(), tempo.into(), modality.into()));

}: sudo_set_bonds_penalty(RawOrigin::<AccountIdOf<T>>::Root, netuid, bonds_penalty)

benchmark_sudo_set_max_allowed_validators {
let netuid: u16 = 1;
let tempo: u16 = 1;
Expand Down
46 changes: 34 additions & 12 deletions pallets/subtensor/src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,18 +131,22 @@ impl<T: Config> Pallet<T> {
// Compute preranks: r_j = SUM(i) w_ij * s_i
let preranks: Vec<I32F32> = matmul( &weights, &active_stake );

// Clip weights at majority consensus
let kappa: I32F32 = Self::get_float_kappa( netuid ); // consensus majority ratio, e.g. 51%.
// Consensus majority ratio, e.g. 51%.
let kappa: I32F32 = Self::get_float_kappa( netuid );
// Calculate consensus as stake-weighted median of weights.
let consensus: Vec<I32F32> = weighted_median_col( &active_stake, &weights, kappa );
inplace_col_clip( &mut weights, &consensus );
let validator_trust: Vec<I32F32> = row_sum( &weights );
// Clip weights at majority consensus.
let mut clipped_weights: Vec<Vec<I32F32>> = weights.clone();
inplace_col_clip( &mut clipped_weights, &consensus );
// Calculate validator trust as sum of clipped weights set by validator.
let validator_trust: Vec<I32F32> = row_sum( &clipped_weights );

// ====================================
// == Ranks, Server Trust, Incentive ==
// ====================================

// Compute ranks: r_j = SUM(i) w_ij * s_i
let mut ranks: Vec<I32F32> = matmul( &weights, &active_stake );
let mut ranks: Vec<I32F32> = matmul( &clipped_weights, &active_stake );

// Compute server trust: ratio of rank after vs. rank before.
let trust: Vec<I32F32> = vecdiv( &ranks, &preranks );
Expand All @@ -155,14 +159,21 @@ impl<T: Config> Pallet<T> {
// == Bonds and Dividends ==
// =========================

// Get validator bonds penalty in [0, 1].
let bonds_penalty: I32F32 = Self::get_float_bonds_penalty( netuid );
// Calculate weights for bonds, apply bonds penalty to weights.
// bonds_penalty = 0: weights_for_bonds = weights.clone()
// bonds_penalty = 1: weights_for_bonds = clipped_weights.clone()
let weights_for_bonds: Vec<Vec<I32F32>> = interpolate( &weights, &clipped_weights, bonds_penalty);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if the bonds penalty = 0.5, will the interpolate function return an average between weights and clipped weights?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is correct, the unit tests for interpolate has examples of this.


// Access network bonds.
let mut bonds: Vec<Vec<I32F32>> = Self::get_bonds( netuid );
inplace_mask_matrix( &outdated, &mut bonds ); // mask outdated bonds
inplace_col_normalize( &mut bonds ); // sum_i b_ij = 1
// log::trace!( "B:\n{:?}\n", &bonds );

// Compute bonds delta column normalized.
let mut bonds_delta: Vec<Vec<I32F32>> = row_hadamard( &weights, &active_stake ); // ΔB = W◦S
let mut bonds_delta: Vec<Vec<I32F32>> = row_hadamard( &weights_for_bonds, &active_stake ); // ΔB = W◦S
inplace_col_normalize( &mut bonds_delta ); // sum_i b_ij = 1
// log::trace!( "ΔB:\n{:?}\n", &bonds_delta );

Expand Down Expand Up @@ -411,23 +422,26 @@ impl<T: Config> Pallet<T> {
let preranks: Vec<I32F32> = matmul_sparse( &weights, &active_stake, n );
// log::trace!( "R (before): {:?}", &preranks );

// Clip weights at majority consensus
let kappa: I32F32 = Self::get_float_kappa( netuid ); // consensus majority ratio, e.g. 51%.
// Consensus majority ratio, e.g. 51%.
let kappa: I32F32 = Self::get_float_kappa( netuid );
// Calculate consensus as stake-weighted median of weights.
let consensus: Vec<I32F32> = weighted_median_col_sparse( &active_stake, &weights, n, kappa );
log::trace!( "C: {:?}", &consensus );

weights = col_clip_sparse( &weights, &consensus );
// Clip weights at majority consensus.
let clipped_weights: Vec<Vec<(u16, I32F32)>> = col_clip_sparse( &weights, &consensus );
// log::trace!( "W: {:?}", &weights );

let validator_trust: Vec<I32F32> = row_sum_sparse( &weights );
// Calculate validator trust as sum of clipped weights set by validator.
let validator_trust: Vec<I32F32> = row_sum_sparse( &clipped_weights );
log::trace!( "Tv: {:?}", &validator_trust );

// =============================
// == Ranks, Trust, Incentive ==
// =============================

// Compute ranks: r_j = SUM(i) w_ij * s_i.
let mut ranks: Vec<I32F32> = matmul_sparse( &weights, &active_stake, n );
let mut ranks: Vec<I32F32> = matmul_sparse( &clipped_weights, &active_stake, n );
// log::trace!( "R (after): {:?}", &ranks );

// Compute server trust: ratio of rank after vs. rank before.
Expand All @@ -442,6 +456,13 @@ impl<T: Config> Pallet<T> {
// == Bonds and Dividends ==
// =========================

// Get validator bonds penalty in [0, 1].
let bonds_penalty: I32F32 = Self::get_float_bonds_penalty( netuid );
// Calculate weights for bonds, apply bonds penalty to weights.
// bonds_penalty = 0: weights_for_bonds = weights.clone()
// bonds_penalty = 1: weights_for_bonds = clipped_weights.clone()
let weights_for_bonds: Vec<Vec<(u16, I32F32)>> = interpolate_sparse( &weights, &clipped_weights, n, bonds_penalty);

// Access network bonds.
let mut bonds: Vec<Vec<(u16, I32F32)>> = Self::get_bonds_sparse( netuid );
// log::trace!( "B: {:?}", &bonds );
Expand All @@ -455,7 +476,7 @@ impl<T: Config> Pallet<T> {
// log::trace!( "B (mask+norm): {:?}", &bonds );

// Compute bonds delta column normalized.
let mut bonds_delta: Vec<Vec<(u16, I32F32)>> = row_hadamard_sparse( &weights, &active_stake ); // ΔB = W◦S (outdated W masked)
let mut bonds_delta: Vec<Vec<(u16, I32F32)>> = row_hadamard_sparse( &weights_for_bonds, &active_stake ); // ΔB = W◦S (outdated W masked)
// log::trace!( "ΔB: {:?}", &bonds_delta );

// Normalize bonds delta.
Expand Down Expand Up @@ -577,6 +598,7 @@ impl<T: Config> Pallet<T> {

pub fn get_float_rho( netuid:u16 ) -> I32F32 { I32F32::from_num( Self::get_rho( netuid ) ) }
pub fn get_float_kappa( netuid:u16 ) -> I32F32 { I32F32::from_num( Self::get_kappa( netuid ) ) / I32F32::from_num( u16::MAX ) }
pub fn get_float_bonds_penalty( netuid:u16 ) -> I32F32 { I32F32::from_num( Self::get_bonds_penalty( netuid ) ) / I32F32::from_num( u16::MAX ) }

pub fn get_normalized_stake( netuid:u16 ) -> Vec<I32F32> {
let n: usize = Self::get_subnetwork_n( netuid ) as usize;
Expand Down
22 changes: 22 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ pub mod pallet {
type InitialAdjustmentInterval: Get<u16>;
#[pallet::constant] // Initial bonds moving average.
type InitialBondsMovingAverage: Get<u64>;
#[pallet::constant] // Initial bonds penalty.
type InitialBondsPenalty: Get<u16>;
#[pallet::constant] // Initial target registrations per interval.
type InitialTargetRegistrationsPerInterval: Get<u16>;
#[pallet::constant] // Rho constant.
Expand Down Expand Up @@ -530,6 +532,10 @@ pub mod pallet {
T::InitialBondsMovingAverage::get()
}
#[pallet::type_value]
pub fn DefaultBondsPenalty<T: Config>() -> u16 {
T::InitialBondsPenalty::get()
}
#[pallet::type_value]
pub fn DefaultValidatorPruneLen<T: Config>() -> u64 {
T::InitialValidatorPruneLen::get()
}
Expand Down Expand Up @@ -587,6 +593,9 @@ pub mod pallet {
#[pallet::storage] // --- MAP ( netuid ) --> bonds_moving_average
pub type BondsMovingAverage<T> =
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultBondsMovingAverage<T>>;
#[pallet::storage] // --- MAP ( netuid ) --> bonds_penalty
pub type BondsPenalty<T> =
StorageMap<_, Identity, u16, u16, ValueQuery, DefaultBondsPenalty<T>>;
#[pallet::storage] // --- MAP ( netuid ) --> weights_set_rate_limit
pub type WeightsSetRateLimit<T> =
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultWeightsSetRateLimit<T>>;
Expand Down Expand Up @@ -739,6 +748,7 @@ pub mod pallet {
WeightsSetRateLimitSet(u16, u64), // --- Event created when weights set rate limit has been set for a subnet.
ImmunityPeriodSet(u16, u16), // --- Event created when immunity period is set for a subnet.
BondsMovingAverageSet(u16, u64), // --- Event created when bonds moving average is set for a subnet.
BondsPenaltySet(u16, u16), // --- Event created when bonds penalty is set for a subnet.
MaxAllowedValidatorsSet(u16, u16), // --- Event created when setting the max number of allowed validators on a subnet.
AxonServed(u16, T::AccountId), // --- Event created when the axon server information is added to the network.
PrometheusServed(u16, T::AccountId), // --- Event created when the prometheus server information is added to the network.
Expand Down Expand Up @@ -1959,6 +1969,18 @@ pub mod pallet {
) -> DispatchResult {
Self::do_sudo_set_adjustment_alpha(origin, netuid, adjustment_alpha)
}

#[pallet::call_index(59)]
#[pallet::weight((Weight::from_ref_time(14_000_000)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this benchmarked? If not, will add it to the list of features that need benchmarking before we push latest chain upgrade.

Copy link
Contributor

@camfairchild camfairchild Aug 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not Edit: not my PR, oops

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this benchmarked? If not, will add it to the list of features that need benchmarking before we push latest chain upgrade.

No it wasn't benchmarked, but copied the time of another u16 hyperparam.

.saturating_add(T::DbWeight::get().reads(1))
.saturating_add(T::DbWeight::get().writes(1)), DispatchClass::Operational, Pays::No))]
pub fn sudo_set_bonds_penalty(
origin: OriginFor<T>,
netuid: u16,
bonds_penalty: u16,
) -> DispatchResult {
Self::do_sudo_set_bonds_penalty(origin, netuid, bonds_penalty)
}
}

// ---- Subtensor helper functions.
Expand Down
Loading