Skip to content

Commit f6e6ee4

Browse files
authored
Merge pull request #1392 from opentensor/feat/ema-slowdown-hyperparam
Ema slowdown hyperparam
2 parents 47dacb6 + de04903 commit f6e6ee4

File tree

9 files changed

+99
-5
lines changed

9 files changed

+99
-5
lines changed

pallets/admin-utils/src/lib.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,35 @@ pub mod pallet {
14411441
);
14421442
Ok(())
14431443
}
1444+
1445+
///
1446+
///
1447+
/// # Arguments
1448+
/// * `origin` - The origin of the call, which must be the root account.
1449+
/// * `ema_alpha_period` - Number of blocks for EMA price to halve
1450+
///
1451+
/// # Errors
1452+
/// * `BadOrigin` - If the caller is not the root account.
1453+
///
1454+
/// # Weight
1455+
/// Weight is handled by the `#[pallet::weight]` attribute.
1456+
#[pallet::call_index(65)]
1457+
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
1458+
pub fn sudo_set_ema_price_halving_period(
1459+
origin: OriginFor<T>,
1460+
netuid: u16,
1461+
ema_halving: u64,
1462+
) -> DispatchResult {
1463+
ensure_root(origin)?;
1464+
pallet_subtensor::EMAPriceHalvingBlocks::<T>::set(netuid, ema_halving);
1465+
1466+
log::debug!(
1467+
"EMAPriceHalvingBlocks( netuid: {:?}, ema_halving: {:?} )",
1468+
netuid,
1469+
ema_halving
1470+
);
1471+
Ok(())
1472+
}
14441473
}
14451474
}
14461475

pallets/admin-utils/src/tests/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ parameter_types! {
134134
pub const InitialColdkeySwapScheduleDuration: u64 = 5 * 24 * 60 * 60 / 12; // 5 days
135135
pub const InitialDissolveNetworkScheduleDuration: u64 = 5 * 24 * 60 * 60 / 12; // 5 days
136136
pub const InitialTaoWeight: u64 = u64::MAX/10; // 10% global weight.
137+
pub const InitialEmaPriceHalvingPeriod: u64 = 201_600_u64; // 4 weeks
137138
}
138139

139140
impl pallet_subtensor::Config for Test {
@@ -197,6 +198,7 @@ impl pallet_subtensor::Config for Test {
197198
type InitialColdkeySwapScheduleDuration = InitialColdkeySwapScheduleDuration;
198199
type InitialDissolveNetworkScheduleDuration = InitialDissolveNetworkScheduleDuration;
199200
type InitialTaoWeight = InitialTaoWeight;
201+
type InitialEmaPriceHalvingPeriod = InitialEmaPriceHalvingPeriod;
200202
}
201203

202204
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]

pallets/admin-utils/src/tests/mod.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1669,3 +1669,45 @@ fn test_sudo_set_subnet_owner_hotkey() {
16691669
);
16701670
});
16711671
}
1672+
1673+
// cargo test --package pallet-admin-utils --lib -- tests::test_sudo_set_ema_halving --exact --show-output
1674+
#[test]
1675+
fn test_sudo_set_ema_halving() {
1676+
new_test_ext().execute_with(|| {
1677+
let netuid: u16 = 1;
1678+
let to_be_set: u64 = 10;
1679+
add_network(netuid, 10);
1680+
1681+
let value_before: u64 = pallet_subtensor::EMAPriceHalvingBlocks::<Test>::get(netuid);
1682+
assert_eq!(
1683+
AdminUtils::sudo_set_ema_price_halving_period(
1684+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1685+
netuid,
1686+
to_be_set
1687+
),
1688+
Err(DispatchError::BadOrigin)
1689+
);
1690+
let value_after_0: u64 = pallet_subtensor::EMAPriceHalvingBlocks::<Test>::get(netuid);
1691+
assert_eq!(value_after_0, value_before);
1692+
1693+
let owner = U256::from(10);
1694+
pallet_subtensor::SubnetOwner::<Test>::insert(netuid, owner);
1695+
assert_eq!(
1696+
AdminUtils::sudo_set_ema_price_halving_period(
1697+
<<Test as Config>::RuntimeOrigin>::signed(owner),
1698+
netuid,
1699+
to_be_set
1700+
),
1701+
Err(DispatchError::BadOrigin)
1702+
);
1703+
let value_after_1: u64 = pallet_subtensor::EMAPriceHalvingBlocks::<Test>::get(netuid);
1704+
assert_eq!(value_after_1, value_before);
1705+
assert_ok!(AdminUtils::sudo_set_ema_price_halving_period(
1706+
<<Test as Config>::RuntimeOrigin>::root(),
1707+
netuid,
1708+
to_be_set
1709+
));
1710+
let value_after_2: u64 = pallet_subtensor::EMAPriceHalvingBlocks::<Test>::get(netuid);
1711+
assert_eq!(value_after_2, to_be_set);
1712+
});
1713+
}

pallets/subtensor/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,11 @@ pub mod pallet {
396396
0
397397
}
398398
#[pallet::type_value]
399+
/// Default EMA price halving blocks
400+
pub fn DefaultEMAPriceMovingBlocks<T: Config>() -> u64 {
401+
T::InitialEmaPriceHalvingPeriod::get()
402+
}
403+
#[pallet::type_value]
399404
/// Default registrations this block.
400405
pub fn DefaultBurn<T: Config>() -> u64 {
401406
T::InitialBurn::get()
@@ -1294,6 +1299,10 @@ pub mod pallet {
12941299
pub type RegistrationsThisBlock<T> =
12951300
StorageMap<_, Identity, u16, u16, ValueQuery, DefaultRegistrationsThisBlock<T>>;
12961301
#[pallet::storage]
1302+
/// --- MAP ( netuid ) --> Halving time of average moving price.
1303+
pub type EMAPriceHalvingBlocks<T> =
1304+
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultEMAPriceMovingBlocks<T>>;
1305+
#[pallet::storage]
12971306
/// --- MAP ( netuid ) --> global_RAO_recycled_for_registration
12981307
pub type RAORecycledForRegistration<T> =
12991308
StorageMap<_, Identity, u16, u64, ValueQuery, DefaultRAORecycledForRegistration<T>>;

pallets/subtensor/src/macros/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,5 +207,8 @@ mod config {
207207
/// Initial TAO weight.
208208
#[pallet::constant]
209209
type InitialTaoWeight: Get<u64>;
210+
/// Initial EMA price halving period
211+
#[pallet::constant]
212+
type InitialEmaPriceHalvingPeriod: Get<u64>;
210213
}
211214
}

pallets/subtensor/src/staking/stake_utils.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,15 @@ impl<T: Config> Pallet<T> {
6060
let blocks_since_registration = I96F32::saturating_from_num(
6161
Self::get_current_block_as_u64().saturating_sub(NetworkRegisteredAt::<T>::get(netuid)),
6262
);
63-
// 7200 * 14 = 100_800 is the halving time
63+
64+
// Use halving time hyperparameter. The meaning of this parameter can be best explained under
65+
// the assumption of a constant price and SubnetMovingAlpha == 0.5: It is how many blocks it
66+
// will take in order for the distance between current EMA of price and current price to shorten
67+
// by half.
68+
let halving_time = EMAPriceHalvingBlocks::<T>::get(netuid);
6469
let alpha: I96F32 =
6570
SubnetMovingAlpha::<T>::get().saturating_mul(blocks_since_registration.safe_div(
66-
blocks_since_registration.saturating_add(I96F32::saturating_from_num(100_800)),
71+
blocks_since_registration.saturating_add(I96F32::saturating_from_num(halving_time)),
6772
));
6873
let minus_alpha: I96F32 = I96F32::saturating_from_num(1.0).saturating_sub(alpha);
6974
let current_price: I96F32 = alpha

pallets/subtensor/src/tests/coinbase.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,8 +218,8 @@ fn test_coinbase_moving_prices() {
218218
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
219219
SubnetMovingAlpha::<Test>::set(I96F32::from_num(0.1));
220220

221-
// EMA price 14 days after registration
222-
System::set_block_number(7_200 * 14);
221+
// EMA price 28 days after registration
222+
System::set_block_number(7_200 * 28);
223223

224224
// Run moving 14 times.
225225
for _ in 0..14 {
@@ -274,7 +274,7 @@ fn test_update_moving_price_after_time() {
274274
SubnetMovingPrice::<Test>::insert(netuid, I96F32::from_num(0));
275275

276276
// Registered long time ago
277-
System::set_block_number(72_000_500);
277+
System::set_block_number(144_000_500);
278278
NetworkRegisteredAt::<Test>::insert(netuid, 500);
279279

280280
SubtensorModule::update_moving_price(netuid);

pallets/subtensor/src/tests/mock.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ parameter_types! {
184184
pub const InitialColdkeySwapScheduleDuration: u64 = 5 * 24 * 60 * 60 / 12; // Default as 5 days
185185
pub const InitialDissolveNetworkScheduleDuration: u64 = 5 * 24 * 60 * 60 / 12; // Default as 5 days
186186
pub const InitialTaoWeight: u64 = 0; // 100% global weight.
187+
pub const InitialEmaPriceHalvingPeriod: u64 = 201_600_u64; // 4 weeks
187188
}
188189

189190
// Configure collective pallet for council
@@ -406,6 +407,7 @@ impl crate::Config for Test {
406407
type InitialColdkeySwapScheduleDuration = InitialColdkeySwapScheduleDuration;
407408
type InitialDissolveNetworkScheduleDuration = InitialDissolveNetworkScheduleDuration;
408409
type InitialTaoWeight = InitialTaoWeight;
410+
type InitialEmaPriceHalvingPeriod = InitialEmaPriceHalvingPeriod;
409411
}
410412

411413
pub struct OriginPrivilegeCmp;

runtime/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,7 @@ parameter_types! {
10171017
pub const InitialColdkeySwapScheduleDuration: BlockNumber = 5 * 24 * 60 * 60 / 12; // 5 days
10181018
pub const InitialDissolveNetworkScheduleDuration: BlockNumber = 5 * 24 * 60 * 60 / 12; // 5 days
10191019
pub const SubtensorInitialTaoWeight: u64 = 971_718_665_099_567_868; // 0.05267697438728329% tao weight.
1020+
pub const InitialEmaPriceHalvingPeriod: u64 = 201_600_u64; // 4 weeks
10201021
}
10211022

10221023
impl pallet_subtensor::Config for Runtime {
@@ -1080,6 +1081,7 @@ impl pallet_subtensor::Config for Runtime {
10801081
type Preimages = Preimage;
10811082
type InitialColdkeySwapScheduleDuration = InitialColdkeySwapScheduleDuration;
10821083
type InitialDissolveNetworkScheduleDuration = InitialDissolveNetworkScheduleDuration;
1084+
type InitialEmaPriceHalvingPeriod = InitialEmaPriceHalvingPeriod;
10831085
}
10841086

10851087
use sp_runtime::BoundedVec;

0 commit comments

Comments
 (0)