Skip to content
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
38 changes: 38 additions & 0 deletions pallets/admin-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1298,6 +1298,44 @@ pub mod pallet {
res
}

/// Sets the liquid alpha consensus mode for a subnet.
///
/// This extrinsic allows the subnet owner or root to configure which consensus values
/// are used when computing liquid alpha bonding matrices.
///
/// # Arguments
/// * `origin` - The origin of the call, which must be the subnet owner or root.
/// * `netuid` - The unique identifier of the subnet.
/// * `mode` - The consensus mode to set.
///
/// # Errors
/// * `BadOrigin` - If the caller is not the subnet owner or root.
#[pallet::call_index(91)]
#[pallet::weight((0, DispatchClass::Normal, Pays::No))]
pub fn sudo_set_liquid_alpha_consensus_mode(
origin: OriginFor<T>,
netuid: NetUid,
mode: pallet_subtensor::ConsensusMode,
) -> DispatchResult {
let maybe_owner = pallet_subtensor::Pallet::<T>::ensure_sn_owner_or_root_with_limits(
origin.clone(),
netuid,
&[Hyperparameter::LiquidAlphaConsensusMode.into()],
)?;
pallet_subtensor::Pallet::<T>::ensure_admin_window_open(netuid)?;
let res = pallet_subtensor::Pallet::<T>::do_set_liquid_alpha_consensus_mode(
origin, netuid, mode,
);
if res.is_ok() {
pallet_subtensor::Pallet::<T>::record_owner_rl(
maybe_owner,
netuid,
&[Hyperparameter::LiquidAlphaConsensusMode.into()],
);
}
res
}

/// Sets the duration of the dissolve network schedule.
///
/// This extrinsic allows the root account to set the duration for the dissolve network schedule.
Expand Down
98 changes: 92 additions & 6 deletions pallets/subtensor/src/epoch/run_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1286,9 +1286,21 @@ impl<T: Config> Pallet<T> {
.iter()
.any(|&c| c != I32F32::saturating_from_num(0))
{
// Liquid Alpha is enabled, compute the liquid alphas matrix.
let alphas: Vec<Vec<I32F32>> =
Self::compute_liquid_alpha_values(netuid, weights, bonds, consensus);
// Liquid Alpha is enabled, compute the appropriate consensus for liquid alpha based on mode
let consensus_for_liquid_alpha =
Self::compute_consensus_for_liquid_alpha(netuid, consensus);
log::trace!(
"consensus_for_liquid_alpha: {:?}",
&consensus_for_liquid_alpha
);

// Compute the liquid alphas matrix.
let alphas: Vec<Vec<I32F32>> = Self::compute_liquid_alpha_values(
netuid,
weights,
bonds,
&consensus_for_liquid_alpha,
);
log::trace!("alphas: {:?}", &alphas);

// Compute the Exponential Moving Average (EMA) of bonds using the provided clamped alpha values.
Expand Down Expand Up @@ -1328,9 +1340,21 @@ impl<T: Config> Pallet<T> {
.iter()
.any(|&c| c != I32F32::saturating_from_num(0))
{
// Liquid Alpha is enabled, compute the liquid alphas matrix.
let alphas: Vec<Vec<I32F32>> =
Self::compute_liquid_alpha_values_sparse(netuid, weights, bonds, consensus);
// Liquid Alpha is enabled, compute the appropriate consensus for liquid alpha based on mode
let consensus_for_liquid_alpha =
Self::compute_consensus_for_liquid_alpha(netuid, consensus);
log::trace!(
"consensus_for_liquid_alpha: {:?}",
&consensus_for_liquid_alpha
);

// Compute the liquid alphas matrix.
let alphas: Vec<Vec<I32F32>> = Self::compute_liquid_alpha_values_sparse(
netuid,
weights,
bonds,
&consensus_for_liquid_alpha,
);
log::trace!("alphas: {:?}", &alphas);

// Compute the Exponential Moving Average (EMA) of bonds using the provided clamped alpha values.
Expand All @@ -1344,6 +1368,54 @@ impl<T: Config> Pallet<T> {
}
}

/// Compute the consensus to use for liquid alpha calculation based on the configured mode
///
/// # Args:
/// * `netuid` - The network ID.
/// * `current_consensus` - The current in-memory consensus values.
///
/// # Returns:
/// A vector of consensus values to use for liquid alpha calculation
pub(crate) fn compute_consensus_for_liquid_alpha(
netuid: NetUid,
current_consensus: &[I32F32],
) -> Vec<I32F32> {
let mode = Self::get_liquid_alpha_consensus_mode(netuid);

match mode {
ConsensusMode::Current => {
// Use the in-memory consensus (current behavior)
current_consensus.to_vec()
}
ConsensusMode::Previous => {
// Use consensus from storage
let previous_consensus_u16 = Consensus::<T>::get(netuid);
previous_consensus_u16
.iter()
.map(|&c| {
I32F32::saturating_from_num(c)
.safe_div(I32F32::saturating_from_num(u16::MAX))
})
.collect()
}
ConsensusMode::Auto => {
// Auto mode: Previous if bond_penalty == u16::MAX (i.e. 1.0), otherwise Current
if Self::get_bonds_penalty(netuid) == u16::MAX {
let previous_consensus_u16 = Consensus::<T>::get(netuid);
previous_consensus_u16
.iter()
.map(|&c| {
I32F32::saturating_from_num(c)
.safe_div(I32F32::saturating_from_num(u16::MAX))
})
.collect()
} else {
current_consensus.to_vec()
}
}
}
}

/// Compute liquid alphas matrix
/// There is a separate alpha param for each validator-miner binding
///
Expand Down Expand Up @@ -1551,6 +1623,20 @@ impl<T: Config> Pallet<T> {
Ok(())
}

pub fn do_set_liquid_alpha_consensus_mode(
origin: OriginFor<T>,
netuid: NetUid,
mode: ConsensusMode,
) -> Result<(), DispatchError> {
Self::ensure_subnet_owner_or_root(origin, netuid)?;

Self::set_liquid_alpha_consensus_mode(netuid, mode);
Self::deposit_event(Event::LiquidAlphaConsensusModeSet { netuid, mode });

log::debug!("LiquidAlphaConsensusModeSet( netuid: {netuid:?}, mode: {mode:?} )");
Ok(())
}

pub fn do_reset_bonds(
netuid_index: NetUidStorageIndex,
account_id: &T::AccountId,
Expand Down
35 changes: 35 additions & 0 deletions pallets/subtensor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,30 @@ pub mod pallet {
},
}

/// Enum for consensus mode used in liquid alpha calculation
#[derive(
Encode,
Decode,
DecodeWithMemTracking,
Default,
TypeInfo,
Clone,
Copy,
PartialEq,
Eq,
Debug,
MaxEncodedLen,
)]
pub enum ConsensusMode {
/// Use current in-memory consensus (current behavior)
Current,
/// Use previous consensus from storage
Previous,
/// Auto mode: Previous if bond_penalty == 1, otherwise Current
#[default]
Auto,
}

/// The Max Burn HalfLife Settable
#[pallet::type_value]
pub fn MaxBurnHalfLife<T: Config>() -> u16 {
Expand Down Expand Up @@ -975,6 +999,12 @@ pub mod pallet {
(45875, 58982)
}

/// Default consensus mode for liquid alpha calculation
#[pallet::type_value]
pub fn DefaultConsensusMode<T: Config>() -> ConsensusMode {
ConsensusMode::default()
}

/// Default value for coldkey swap announcement delay.
#[pallet::type_value]
pub fn DefaultColdkeySwapAnnouncementDelay<T: Config>() -> BlockNumberFor<T> {
Expand Down Expand Up @@ -1942,6 +1972,11 @@ pub mod pallet {
pub type AlphaValues<T> =
StorageMap<_, Identity, NetUid, (u16, u16), ValueQuery, DefaultAlphaValues<T>>;

/// MAP ( netuid ) --> consensus mode for liquid alpha calculation
#[pallet::storage]
pub type LiquidAlphaConsensusMode<T> =
StorageMap<_, Identity, NetUid, ConsensusMode, ValueQuery, DefaultConsensusMode<T>>;

/// --- MAP ( netuid ) --> If subtoken trading enabled
#[pallet::storage]
pub type SubtokenEnabled<T> =
Expand Down
8 changes: 8 additions & 0 deletions pallets/subtensor/src/macros/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -562,5 +562,13 @@ mod events {
/// The burn increase multiplier value for neuron registration.
burn_increase_mult: u64,
},

/// Liquid alpha consensus mode set for a subnet.
LiquidAlphaConsensusModeSet {
/// The subnet identifier.
netuid: NetUid,
/// The new consensus mode.
mode: ConsensusMode,
},
}
}
Loading
Loading