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
14 changes: 7 additions & 7 deletions pallets/subtensor/src/epoch/run_epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ impl<T: Config> Pallet<T> {

/// Output unnormalized sparse weights, input weights are assumed to be row max-upscaled in u16.
pub fn get_weights_sparse(netuid_index: NetUidStorageIndex) -> Vec<Vec<(u16, I32F32)>> {
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default();
let n = Self::get_subnetwork_n(netuid) as usize;
let mut weights: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
for (uid_i, weights_i) in
Expand All @@ -1080,7 +1080,7 @@ impl<T: Config> Pallet<T> {

/// Output unnormalized weights in [n, n] matrix, input weights are assumed to be row max-upscaled in u16.
pub fn get_weights(netuid_index: NetUidStorageIndex) -> Vec<Vec<I32F32>> {
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default();
let n = Self::get_subnetwork_n(netuid) as usize;
let mut weights: Vec<Vec<I32F32>> = vec![vec![I32F32::saturating_from_num(0.0); n]; n];
for (uid_i, weights_vec) in
Expand All @@ -1103,7 +1103,7 @@ impl<T: Config> Pallet<T> {

/// Output unnormalized sparse bonds, input bonds are assumed to be column max-upscaled in u16.
pub fn get_bonds_sparse(netuid_index: NetUidStorageIndex) -> Vec<Vec<(u16, I32F32)>> {
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default();
let n = Self::get_subnetwork_n(netuid) as usize;
let mut bonds: Vec<Vec<(u16, I32F32)>> = vec![vec![]; n];
for (uid_i, bonds_vec) in
Expand All @@ -1126,7 +1126,7 @@ impl<T: Config> Pallet<T> {

/// Output unnormalized bonds in [n, n] matrix, input bonds are assumed to be column max-upscaled in u16.
pub fn get_bonds(netuid_index: NetUidStorageIndex) -> Vec<Vec<I32F32>> {
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default();
let n: usize = Self::get_subnetwork_n(netuid) as usize;
let mut bonds: Vec<Vec<I32F32>> = vec![vec![I32F32::saturating_from_num(0.0); n]; n];
for (uid_i, bonds_vec) in
Expand Down Expand Up @@ -1188,7 +1188,7 @@ impl<T: Config> Pallet<T> {
bonds: &[Vec<(u16, I32F32)>],
netuid_index: NetUidStorageIndex,
) -> Vec<Vec<(u16, I32F32)>> {
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default();

// Retrieve the bonds moving average for the given network ID and scale it down.
let bonds_moving_average: I64F64 =
Expand Down Expand Up @@ -1301,7 +1301,7 @@ impl<T: Config> Pallet<T> {
bonds: &[Vec<(u16, I32F32)>],
consensus: &[I32F32],
) -> Vec<Vec<(u16, I32F32)>> {
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default();

// Check if Liquid Alpha is enabled, consensus is not empty, and contains non-zero values.
if LiquidAlphaOn::<T>::get(netuid)
Expand Down Expand Up @@ -1537,7 +1537,7 @@ impl<T: Config> Pallet<T> {
netuid_index: NetUidStorageIndex,
account_id: &T::AccountId,
) -> Result<(), DispatchError> {
let (netuid, _) = Self::get_netuid_and_subid(netuid_index).unwrap_or_default();
let (netuid, _) = Self::get_netuid_and_mecid(netuid_index).unwrap_or_default();

// check bonds reset enabled for this subnet
let bonds_reset_enabled: bool = Self::get_bonds_reset(netuid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub fn migrate_crv3_commits_add_block<T: Config>() -> Weight {
for (netuid_index, epoch, old_q) in CRV3WeightCommits::<T>::drain() {
total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(1, 1));

let (netuid, _) = Pallet::<T>::get_netuid_and_subid(netuid_index).unwrap_or_default();
let (netuid, _) = Pallet::<T>::get_netuid_and_mecid(netuid_index).unwrap_or_default();
let commit_block = Pallet::<T>::get_first_block_of_epoch(netuid, epoch);

// convert VecDeque<(who,cipher,rnd)> → VecDeque<(who,cb,cipher,rnd)>
Expand Down
48 changes: 24 additions & 24 deletions pallets/subtensor/src/subnets/mechanism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ pub type BalanceOf<T> =
<CurrencyOf<T> as fungible::Inspect<<T as frame_system::Config>::AccountId>>::Balance;

/// Theoretical maximum of subnets on bittensor. This value is used in indexed
/// storage of epoch values for sub-subnets as
/// storage of epoch values for mechanisms as
///
/// `storage_index = netuid + sub_id * GLOBAL_MAX_SUBNET_COUNT`
/// `storage_index = netuid + mecid * GLOBAL_MAX_SUBNET_COUNT`
///
/// For sub_id = 0 this index results in netuid and provides backward compatibility
/// for subnets with default sub-subnet count of 1.
/// For mecid = 0 this index results in netuid and provides backward compatibility
/// for subnets with default mechanism count of 1.
///
/// Changing this value will require a migration of all epoch maps.
///
Expand All @@ -32,8 +32,8 @@ pub const GLOBAL_MAX_SUBNET_COUNT: u16 = 4096;
pub const MAX_MECHANISM_COUNT_PER_SUBNET: u8 = 16;

impl<T: Config> Pallet<T> {
pub fn get_mechanism_storage_index(netuid: NetUid, sub_id: MechId) -> NetUidStorageIndex {
u16::from(sub_id)
pub fn get_mechanism_storage_index(netuid: NetUid, mecid: MechId) -> NetUidStorageIndex {
u16::from(mecid)
.saturating_mul(GLOBAL_MAX_SUBNET_COUNT)
.saturating_add(u16::from(netuid))
.into()
Expand All @@ -48,7 +48,7 @@ impl<T: Config> Pallet<T> {
}
}

pub fn get_netuid_and_subid(
pub fn get_netuid_and_mecid(
netuid_index: NetUidStorageIndex,
) -> Result<(NetUid, MechId), Error<T>> {
let maybe_netuid = u16::from(netuid_index).checked_rem(GLOBAL_MAX_SUBNET_COUNT);
Expand All @@ -61,13 +61,13 @@ impl<T: Config> Pallet<T> {
Error::<T>::MechanismDoesNotExist
);

// Extract sub_id
let sub_id_u8 = u8::try_from(u16::from(netuid_index).safe_div(GLOBAL_MAX_SUBNET_COUNT))
// Extract mecid
let mecid_u8 = u8::try_from(u16::from(netuid_index).safe_div(GLOBAL_MAX_SUBNET_COUNT))
.map_err(|_| Error::<T>::MechanismDoesNotExist)?;
let sub_id = MechId::from(sub_id_u8);
let mecid = MechId::from(mecid_u8);

if MechanismCountCurrent::<T>::get(netuid) > sub_id {
Ok((netuid, sub_id))
if MechanismCountCurrent::<T>::get(netuid) > mecid {
Ok((netuid, mecid))
} else {
Err(Error::<T>::MechanismDoesNotExist.into())
}
Expand All @@ -80,7 +80,7 @@ impl<T: Config> Pallet<T> {
MechanismCountCurrent::<T>::get(netuid)
}

pub fn ensure_mechanism_exists(netuid: NetUid, sub_id: MechId) -> DispatchResult {
pub fn ensure_mechanism_exists(netuid: NetUid, mecid: MechId) -> DispatchResult {
// Make sure the base subnet exists
ensure!(
Self::if_subnet_exist(netuid),
Expand All @@ -89,7 +89,7 @@ impl<T: Config> Pallet<T> {

// Make sure the mechanism limit is not exceeded
ensure!(
MechanismCountCurrent::<T>::get(netuid) > sub_id,
MechanismCountCurrent::<T>::get(netuid) > mecid,
Error::<T>::MechanismDoesNotExist
);
Ok(())
Expand Down Expand Up @@ -260,30 +260,30 @@ impl<T: Config> Pallet<T> {
.into_iter()
.enumerate()
// Run epoch function for each mechanism to distribute its portion of emissions
.flat_map(|(sub_id_usize, sub_emission)| {
let sub_id_u8: u8 = sub_id_usize.try_into().unwrap_or_default();
let sub_id = MechId::from(sub_id_u8);
.flat_map(|(mecid_usize, mech_emission)| {
let mecid_u8: u8 = mecid_usize.try_into().unwrap_or_default();
let mecid = MechId::from(mecid_u8);

// Run epoch function on the mechanism emission
let epoch_output = Self::epoch_mechanism(netuid, sub_id, sub_emission);
Self::persist_mechanism_epoch_terms(netuid, sub_id, epoch_output.as_map());
let epoch_output = Self::epoch_mechanism(netuid, mecid, mech_emission);
Self::persist_mechanism_epoch_terms(netuid, mecid, epoch_output.as_map());

// Calculate mechanism weight from the split emission (not the other way because preserving
// emission accuracy is the priority)
// For zero emission the first mechanism gets full weight
let sub_weight = U64F64::saturating_from_num(sub_emission).safe_div_or(
let mech_weight = U64F64::saturating_from_num(mech_emission).safe_div_or(
U64F64::saturating_from_num(rao_emission),
U64F64::saturating_from_num(if sub_id_u8 == 0 { 1 } else { 0 }),
U64F64::saturating_from_num(if mecid_u8 == 0 { 1 } else { 0 }),
);

// Produce an iterator of (hotkey, (terms, sub_weight)) tuples
// Produce an iterator of (hotkey, (terms, mech_weight)) tuples
epoch_output
.0
.into_iter()
.map(move |(hotkey, terms)| (hotkey, (terms, sub_weight)))
.map(move |(hotkey, terms)| (hotkey, (terms, mech_weight)))
})
// Consolidate the hotkey emissions into a single BTreeMap
.fold(BTreeMap::new(), |mut acc, (hotkey, (terms, sub_weight))| {
.fold(BTreeMap::new(), |mut acc, (hotkey, (terms, mech_weight))| {
acc.entry(hotkey)
.and_modify(|acc_terms| {
// Server and validator emission come from mechanism emission and need to be added up
Expand Down
4 changes: 2 additions & 2 deletions pallets/subtensor/src/subnets/weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,8 +1108,8 @@ impl<T: Config> Pallet<T> {
neuron_uid: u16,
current_block: u64,
) -> bool {
let maybe_netuid_and_subid = Self::get_netuid_and_subid(netuid_index);
if let Ok((netuid, _)) = maybe_netuid_and_subid
let maybe_netuid_and_mecid = Self::get_netuid_and_mecid(netuid_index);
if let Ok((netuid, _)) = maybe_netuid_and_mecid
&& Self::is_uid_exist_on_network(netuid, neuron_uid)
{
// --- 1. Ensure that the diff between current and last_set weights is greater than limit.
Expand Down
2 changes: 1 addition & 1 deletion pallets/subtensor/src/tests/mechanism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ fn test_netuid_and_subnet_from_index() {
);

let (netuid, mecid) =
SubtensorModule::get_netuid_and_subid(NetUidStorageIndex::from(*netuid_index))
SubtensorModule::get_netuid_and_mecid(NetUidStorageIndex::from(*netuid_index))
.unwrap();
assert_eq!(netuid, NetUid::from(expected_netuid));
assert_eq!(mecid, MechId::from(expected_subid));
Expand Down