Skip to content

Commit 9956cba

Browse files
authored
Merge pull request #1758 from backend-developers-ltd/steep
sigmoid steepness adjustment
2 parents f9c02cc + 96e1894 commit 9956cba

File tree

16 files changed

+127
-26
lines changed

16 files changed

+127
-26
lines changed

docs/img/sigmoid_steepness.png

148 KB
Loading

hyperparameters.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ TxRateLimit: u64 = 1; // [1 @ 64,888]
77
### netuid 1 (text_prompting)
88
```rust
99
Rho: u16 = 10;
10-
AlphaSigmoidSteepness: u16 = 10.0
10+
AlphaSigmoidSteepness: i16 = 1000
1111
Kappa: u16 = 32_767; // 0.5 = 65535/2
1212
MaxAllowedUids: u16 = 1024;
1313
Issuance: u64 = 0;
@@ -48,7 +48,7 @@ WeightsSetRateLimit: u64 = 100;
4848
### netuid 3 (causallmnext)
4949
```rust
5050
Rho: u16 = 10;
51-
AlphaSigmoidSteepness: u16 = 10.0
51+
AlphaSigmoidSteepness: i16 = 1000
5252
Kappa: u16 = 32_767; // 0.5 = 65535/2
5353
MaxAllowedUids: u16 = 4096;
5454
Issuance: u64 = 0;

pallets/admin-utils/src/lib.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ pub mod pallet {
105105
MaxAllowedUIdsLessThanCurrentUIds,
106106
/// The maximum value for bonds moving average is reached
107107
BondsMovingAverageMaxReached,
108+
/// Only root can set negative sigmoid steepness values
109+
NegativeSigmoidSteepness,
108110
}
109111
/// Enum for specifying the type of precompile operation.
110112
#[derive(Encode, Decode, TypeInfo, Clone, PartialEq, Eq, Debug, Copy)]
@@ -1560,20 +1562,36 @@ pub mod pallet {
15601562
/// # Arguments
15611563
/// * `origin` - The origin of the call, which must be the root account.
15621564
/// * `netuid` - The unique identifier for the subnet.
1563-
/// * `steepness` - The new steepness for the alpha sigmoid function.
1565+
/// * `steepness` - The Steepness for the alpha sigmoid function. (range is 0-int16::MAX,
1566+
/// negative values are reserved for future use)
15641567
///
15651568
/// # Errors
15661569
/// * `BadOrigin` - If the caller is not the root account.
1570+
/// * `SubnetDoesNotExist` - If the specified subnet does not exist.
1571+
/// * `NegativeSigmoidSteepness` - If the steepness is negative and the caller is
1572+
/// root.
15671573
/// # Weight
15681574
/// Weight is handled by the `#[pallet::weight]` attribute.
15691575
#[pallet::call_index(68)]
15701576
#[pallet::weight((0, DispatchClass::Operational, Pays::No))]
15711577
pub fn sudo_set_alpha_sigmoid_steepness(
15721578
origin: OriginFor<T>,
15731579
netuid: NetUid,
1574-
steepness: u16,
1580+
steepness: i16,
15751581
) -> DispatchResult {
1576-
ensure_root(origin)?;
1582+
pallet_subtensor::Pallet::<T>::ensure_subnet_owner_or_root(origin.clone(), netuid)?;
1583+
1584+
ensure!(
1585+
pallet_subtensor::Pallet::<T>::if_subnet_exist(netuid),
1586+
Error::<T>::SubnetDoesNotExist
1587+
);
1588+
1589+
let is_root = ensure_root(origin).is_ok();
1590+
ensure!(
1591+
is_root || steepness >= 0,
1592+
Error::<T>::NegativeSigmoidSteepness
1593+
);
1594+
15771595
pallet_subtensor::Pallet::<T>::set_alpha_sigmoid_steepness(netuid, steepness);
15781596

15791597
log::debug!(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ parameter_types! {
8282
pub const TransactionByteFee: Balance = 100;
8383
pub const SDebug:u64 = 1;
8484
pub const InitialRho: u16 = 30;
85-
pub const InitialAlphaSigmoidSteepness: u16 = 10;
85+
pub const InitialAlphaSigmoidSteepness: i16 = 1000;
8686
pub const InitialKappa: u16 = 32_767;
8787
pub const InitialTempo: u16 = 0;
8888
pub const SelfOwnership: u64 = 2;

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

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,65 @@ fn test_sudo_set_liquid_alpha_enabled() {
11461146
});
11471147
}
11481148

1149+
#[test]
1150+
fn test_sudo_set_alpha_sigmoid_steepness() {
1151+
new_test_ext().execute_with(|| {
1152+
let netuid = NetUid::from(1);
1153+
let to_be_set: i16 = 5000;
1154+
add_network(netuid, 10);
1155+
let init_value = SubtensorModule::get_alpha_sigmoid_steepness(netuid);
1156+
assert_eq!(
1157+
AdminUtils::sudo_set_alpha_sigmoid_steepness(
1158+
<<Test as Config>::RuntimeOrigin>::signed(U256::from(1)),
1159+
netuid,
1160+
to_be_set
1161+
),
1162+
Err(DispatchError::BadOrigin)
1163+
);
1164+
assert_eq!(
1165+
AdminUtils::sudo_set_alpha_sigmoid_steepness(
1166+
<<Test as Config>::RuntimeOrigin>::root(),
1167+
netuid.next(),
1168+
to_be_set
1169+
),
1170+
Err(Error::<Test>::SubnetDoesNotExist.into())
1171+
);
1172+
1173+
let owner = U256::from(10);
1174+
pallet_subtensor::SubnetOwner::<Test>::insert(netuid, owner);
1175+
assert_eq!(
1176+
AdminUtils::sudo_set_alpha_sigmoid_steepness(
1177+
<<Test as Config>::RuntimeOrigin>::signed(owner),
1178+
netuid,
1179+
-to_be_set
1180+
),
1181+
Err(Error::<Test>::NegativeSigmoidSteepness.into())
1182+
);
1183+
assert_eq!(
1184+
SubtensorModule::get_alpha_sigmoid_steepness(netuid),
1185+
init_value
1186+
);
1187+
assert_ok!(AdminUtils::sudo_set_alpha_sigmoid_steepness(
1188+
<<Test as Config>::RuntimeOrigin>::root(),
1189+
netuid,
1190+
to_be_set
1191+
));
1192+
assert_eq!(
1193+
SubtensorModule::get_alpha_sigmoid_steepness(netuid),
1194+
to_be_set
1195+
);
1196+
assert_ok!(AdminUtils::sudo_set_alpha_sigmoid_steepness(
1197+
<<Test as Config>::RuntimeOrigin>::root(),
1198+
netuid,
1199+
-to_be_set
1200+
));
1201+
assert_eq!(
1202+
SubtensorModule::get_alpha_sigmoid_steepness(netuid),
1203+
-to_be_set
1204+
);
1205+
});
1206+
}
1207+
11491208
#[test]
11501209
fn test_set_alpha_values_dispatch_info_ok() {
11511210
new_test_ext().execute_with(|| {

pallets/subtensor/src/epoch/run_epoch.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,10 +1298,9 @@ impl<T: Config> Pallet<T> {
12981298
// sigmoid = 1. / (1. + e^(-steepness * (combined_diff - 0.5)))
12991299
let sigmoid = one.saturating_div(
13001300
one.saturating_add(safe_exp(
1301-
I32F32::from_num(-1).saturating_mul(
1302-
alpha_sigmoid_steepness
1303-
.saturating_mul(combined_diff.saturating_sub(I32F32::from_num(0.5))),
1304-
),
1301+
alpha_sigmoid_steepness
1302+
.saturating_div(I32F32::from_num(-100))
1303+
.saturating_mul(combined_diff.saturating_sub(I32F32::from_num(0.5))),
13051304
)),
13061305
);
13071306
let alpha =

pallets/subtensor/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ pub mod pallet {
599599
}
600600
#[pallet::type_value]
601601
/// Default value for alpha sigmoid steepness.
602-
pub fn DefaultAlphaSigmoidSteepness<T: Config>() -> u16 {
602+
pub fn DefaultAlphaSigmoidSteepness<T: Config>() -> i16 {
603603
T::InitialAlphaSigmoidSteepness::get()
604604
}
605605
#[pallet::type_value]
@@ -1296,7 +1296,7 @@ pub mod pallet {
12961296
#[pallet::storage]
12971297
/// --- MAP ( netuid ) --> AlphaSigmoidSteepness
12981298
pub type AlphaSigmoidSteepness<T> =
1299-
StorageMap<_, Identity, NetUid, u16, ValueQuery, DefaultAlphaSigmoidSteepness<T>>;
1299+
StorageMap<_, Identity, NetUid, i16, ValueQuery, DefaultAlphaSigmoidSteepness<T>>;
13001300
#[pallet::storage]
13011301
/// --- MAP ( netuid ) --> Kappa
13021302
pub type Kappa<T> = StorageMap<_, Identity, NetUid, u16, ValueQuery, DefaultKappa<T>>;

pallets/subtensor/src/macros/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ mod config {
107107
type InitialRho: Get<u16>;
108108
/// AlphaSigmoidSteepness constant.
109109
#[pallet::constant]
110-
type InitialAlphaSigmoidSteepness: Get<u16>;
110+
type InitialAlphaSigmoidSteepness: Get<i16>;
111111
/// Kappa constant.
112112
#[pallet::constant]
113113
type InitialKappa: Get<u16>;

pallets/subtensor/src/macros/events.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ mod events {
5151
/// Rho value is set.
5252
RhoSet(NetUid, u16),
5353
/// steepness of the sigmoid used to compute alpha values.
54-
AlphaSigmoidSteepnessSet(NetUid, u16),
54+
AlphaSigmoidSteepnessSet(NetUid, i16),
5555
/// Kappa is set for a subnet.
5656
KappaSet(NetUid, u16),
5757
/// minimum allowed weight is set for a subnet.

pallets/subtensor/src/tests/epoch.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2644,7 +2644,7 @@ fn setup_yuma_3_scenario(netuid: NetUid, n: u16, sparse: bool, max_stake: u64, s
26442644
SubtensorModule::set_min_allowed_weights(netuid, 1);
26452645
SubtensorModule::set_max_weight_limit(netuid, u16::MAX);
26462646
SubtensorModule::set_bonds_penalty(netuid, 0);
2647-
SubtensorModule::set_alpha_sigmoid_steepness(netuid, 10);
2647+
SubtensorModule::set_alpha_sigmoid_steepness(netuid, 1000);
26482648
SubtensorModule::set_bonds_moving_average(netuid, 975_000);
26492649

26502650
// === Register

0 commit comments

Comments
 (0)