bittensor
bittensor copied to clipboard
AttributeError: 'float' object has no attribute 'to_bytes' when encoding float values in Vec object
Describe the bug
When attempting to encode a list of float values using the Vec object with sub_type="U16", the script encounters an AttributeError because the to_bytes method is being called on float objects, which do not have this method.
To Reproduce
- Follow the documentation at Bittensor Docs - Commit Reveal.
- Run the following Python script:
import bittensor as bt
wallet = bt.wallet(name="validator")
subtensor = bt.subtensor(network="127.0.0.1:9945")
def main():
enabled_result = subtensor.set_hyperparameter(
wallet=wallet,
netuid=1,
parameter="commit_reveal_weights_enabled",
value=True
)
print(">>> enabled_result", enabled_result)
# Set interval
interval_result = subtensor.set_hyperparameter(
wallet=wallet,
netuid=1,
parameter="commit_reveal_weights_interval",
value=370
)
print(">>> interval_result", interval_result)
# Set rate limit
rate_limit_result = subtensor.set_hyperparameter(
wallet=wallet,
netuid=1,
parameter="weights_rate_limit",
value=0
)
print(">>> rate_limit_result", rate_limit_result)
# Run the commit weights operation
weights = [0.1, 0.2, 0.3, 0.4]
weight_uids = [1, 2, 3, 4]
salt = [18, 179, 107, 0, 165, 211, 141, 197]
success, message = subtensor.commit_weights(
wallet=wallet,
netuid=1,
uids=weight_uids,
weights=weights,
salt=salt
)
print(">>> 1 success, message", success, message)
# Run the reveal weights operation.
success, message = subtensor.reveal_weights(
wallet=wallet,
netuid=1,
uids=weight_uids,
weights=weights,
salt=salt
)
print(">>> 2 success, message", success, message)
if __name__ == "__main__":
main()
- Observe the following error:
bittensor/utils/weight_utils.py:388 in <listcomp>
385 │
386 │ vec_values = Vec(data=None, sub_type="U16")
387 │ vec_values.value = [
❱ 388 │ │ U16(ScaleBytes(value.to_bytes(2, "little"))) for value in values
389 │ ]
390 │ values = ScaleBytes(vec_values.encode().data)
391
AttributeError: 'float' object has no attribute 'to_bytes'
Expected behavior
The commit and reveal weights operations should be completed without errors.
Screenshots
No response
Environment
Python 3.10.10, macOS, Bittensor 7.3.0
Additional context
Suggested Fix: Convert the float values to integers before calling the to_bytes method.
vec_values = Vec(data=None, sub_type="U16")
vec_values.value = [
U16(ScaleBytes(int(value).to_bytes(2, "little"))) for value in values
]
values = ScaleBytes(vec_values.encode().data)