Skip to content

Commit 982c4c2

Browse files
authored
Merge pull request #510 from opentensor/fix/thewhaleking/convert-from-strings
Convert hyperparams from strings
2 parents 371193d + 7e24c04 commit 982c4c2

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
@@ -540,3 +540,31 @@ def test_staking(local_chain, wallet_setup):
540540
assert yuma3_val["value"] is True
541541
assert yuma3_val["normalized_value"] is True
542542
print("✅ Passed staking and sudo commands")
543+
544+
change_arbitrary_hyperparam = exec_command_alice(
545+
command="sudo",
546+
sub_command="set",
547+
extra_args=[
548+
"--wallet-path",
549+
wallet_path_alice,
550+
"--wallet-name",
551+
wallet_alice.name,
552+
"--hotkey",
553+
wallet_alice.hotkey_str,
554+
"--chain",
555+
"ws://127.0.0.1:9945",
556+
"--netuid",
557+
netuid,
558+
"--param",
559+
"sudo_set_bonds_penalty", # arbitrary hyperparam
560+
"--value",
561+
"0", # int/float value
562+
"--no-prompt",
563+
"--json-output",
564+
],
565+
)
566+
change_arbitrary_hyperparam_json = json.loads(change_arbitrary_hyperparam.stdout)
567+
assert change_arbitrary_hyperparam_json["success"] is True, (
568+
change_arbitrary_hyperparam.stdout,
569+
change_arbitrary_hyperparam.stderr,
570+
)

0 commit comments

Comments
 (0)