Skip to content

Commit 31a4382

Browse files
thewhalekingibraheem-abe
authored andcommitted
Merge pull request #510 from opentensor/fix/thewhaleking/convert-from-strings
Convert hyperparams from strings
1 parent b815484 commit 31a4382

File tree

3 files changed

+41
-3
lines changed

3 files changed

+41
-3
lines changed

bittensor_cli/src/bittensor/utils.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ def u64_normalized_float(x: int) -> float:
122122
return float(x) / float(U64_MAX)
123123

124124

125+
def string_to_u64(value: str) -> int:
126+
"""Converts a string to u64"""
127+
return float_to_u64(float(value))
128+
129+
125130
def float_to_u64(value: float) -> int:
126131
"""Converts a float to a u64 int"""
127132
# Ensure the input is within the expected range
@@ -142,6 +147,11 @@ def u64_to_float(value: int) -> float:
142147
return min(value / u64_max, 1.0) # Ensure the result is never greater than 1.0
143148

144149

150+
def string_to_u16(value: str) -> int:
151+
"""Converts a string to a u16 int"""
152+
return float_to_u16(float(value))
153+
154+
145155
def float_to_u16(value: float) -> int:
146156
# Ensure the input is within the expected range
147157
if not (0 <= value <= 1):

bittensor_cli/src/commands/sudo.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
normalize_hyperparameters,
1919
unlock_key,
2020
blocks_to_duration,
21-
float_to_u64,
22-
float_to_u16,
2321
json_console,
22+
string_to_u16,
23+
string_to_u64,
2424
)
2525

2626
if TYPE_CHECKING:
@@ -108,7 +108,7 @@ def type_converter_with_retry(type_, val, arg_name):
108108
except ValueError:
109109
return type_converter_with_retry(type_, None, arg_name)
110110

111-
arg_types = {"bool": string_to_bool, "u16": float_to_u16, "u64": float_to_u64}
111+
arg_types = {"bool": string_to_bool, "u16": string_to_u16, "u64": string_to_u64}
112112
arg_type_output = {"bool": "bool", "u16": "float", "u64": "float"}
113113

114114
call_crafter = {"netuid": netuid}

tests/e2e_tests/test_staking_sudo.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,3 +533,31 @@ def test_staking(local_chain, wallet_setup):
533533
assert yuma3_val["value"] is True
534534
assert yuma3_val["normalized_value"] is True
535535
print("✅ Passed staking and sudo commands")
536+
537+
change_arbitrary_hyperparam = exec_command_alice(
538+
command="sudo",
539+
sub_command="set",
540+
extra_args=[
541+
"--wallet-path",
542+
wallet_path_alice,
543+
"--wallet-name",
544+
wallet_alice.name,
545+
"--hotkey",
546+
wallet_alice.hotkey_str,
547+
"--chain",
548+
"ws://127.0.0.1:9945",
549+
"--netuid",
550+
netuid,
551+
"--param",
552+
"sudo_set_bonds_penalty", # arbitrary hyperparam
553+
"--value",
554+
"0", # int/float value
555+
"--no-prompt",
556+
"--json-output",
557+
],
558+
)
559+
change_arbitrary_hyperparam_json = json.loads(change_arbitrary_hyperparam.stdout)
560+
assert change_arbitrary_hyperparam_json["success"] is True, (
561+
change_arbitrary_hyperparam.stdout,
562+
change_arbitrary_hyperparam.stderr,
563+
)

0 commit comments

Comments
 (0)