Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fastblocks when setting root set weights in e2e tests #2464

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion bittensor/core/extrinsics/root.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ def _do_set_root_weights(
version_key: int = version_as_int,
wait_for_inclusion: bool = False,
wait_for_finalization: bool = False,
period: int = 5,
) -> tuple[bool, Optional[str]]:
"""
Internal method to send a transaction to the Bittensor blockchain, setting weights for specified neurons on root. This method constructs and submits the transaction, handling retries and blockchain communication.
Expand Down Expand Up @@ -153,7 +154,7 @@ def _do_set_root_weights(
extrinsic = self.substrate.create_signed_extrinsic(
call=call,
keypair=wallet.coldkey,
era={"period": 5},
era={"period": period},
)
response = self.substrate.submit_extrinsic(
extrinsic,
Expand Down
33 changes: 18 additions & 15 deletions tests/e2e_tests/test_root_set_weights.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import sys
import numpy as np
import pytest

from bittensor.core.subtensor import Subtensor
Expand All @@ -9,12 +8,15 @@
wait_epoch,
sudo_set_hyperparameter_values,
)
from bittensor.core.extrinsics.root import _do_set_root_weights
from tests.e2e_tests.utils.e2e_test_utils import (
setup_wallet,
template_path,
templates_repo,
)

FAST_BLOCKS_SPEEDUP_FACTOR = 5

"""
Verifies:

Expand Down Expand Up @@ -66,7 +68,8 @@ async def test_root_reg_hyperparams(local_chain):
default_tempo = 360

# 0.2 for root network, 0.8 for sn 1
weights = [0.2, 0.8]
# Corresponding to [0.2, 0.8]
weights = [16384, 65535]

# Create Alice, SN1 owner and root network member
alice_keypair, alice_wallet = setup_wallet("//Alice")
Expand Down Expand Up @@ -149,24 +152,24 @@ async def test_root_reg_hyperparams(local_chain):
# Wait until next epoch so we can set root weights
await wait_epoch(subtensor)

# Set root weights for netuids 0, 1
assert subtensor.root_set_weights(
alice_wallet,
[0, 1],
weights,
wait_for_inclusion=False,
# Set root weights to root network (0) and sn 1
assert _do_set_root_weights(
subtensor,
wallet=alice_wallet,
uids=[0, 1],
vals=weights,
netuid=0,
version_key=0,
wait_for_inclusion=True,
wait_for_finalization=True,
)
period=5 * FAST_BLOCKS_SPEEDUP_FACTOR,
) == (True, "Successfully set weights.")

# Query the weights from the chain
weights_raw = local_chain.query("SubtensorModule", "Weights", [0, 0]).serialize()

weights_array = np.array(weights_raw)
normalized_weights = weights_array[:, 1] / max(np.sum(weights_array, axis=0)[1], 1)
rounded_weights = [round(weight, 1) for weight in normalized_weights]
weights_set = local_chain.query("SubtensorModule", "Weights", [0, 0]).serialize()

# Assert correct weights were set for root and sn 1
assert weights == rounded_weights
assert [val[1] for val in weights_set] == weights

# Register Bob as miner
bob_keypair, bob_wallet = setup_wallet("//Bob")
Expand Down
Loading