diff --git a/neurons/miner.py b/neurons/miner.py index ee1a574..bb4c14f 100644 --- a/neurons/miner.py +++ b/neurons/miner.py @@ -39,6 +39,10 @@ from openkaito.utils.embeddings import openai_embeddings_tensor from openkaito.utils.version import compare_version, get_version +# Fiber +from fiber.miner import server +from fiber.miner.middleware import configure_extra_logging_middleware + class Miner(BaseMinerNeuron): """ @@ -244,6 +248,11 @@ def check_version(self, query): # This is the main function, which runs the miner. if __name__ == "__main__": with Miner() as miner: + # Configure extra logging middleware + configure_extra_logging_middleware(miner) + # Run the miner + server.run(miner) + # Print the miner info while True: miner.print_info() time.sleep(30) diff --git a/neurons/validator.py b/neurons/validator.py index 90b555d..5c35036 100644 --- a/neurons/validator.py +++ b/neurons/validator.py @@ -57,6 +57,10 @@ from openkaito.utils.embeddings import openai_embeddings_tensor +# Fiber +from fiber.validator import server +from fiber.validator.middleware import configure_extra_logging_middleware + class Validator(BaseValidatorNeuron): def __init__(self): super(Validator, self).__init__() @@ -450,6 +454,12 @@ def print_info(self): # The main function parses the configuration and runs the validator. if __name__ == "__main__": with Validator() as validator: + # Configure extra logging middleware + configure_extra_logging_middleware(validator) + + # Run the validator + server.run(validator) + while True: validator.print_info() if validator.should_exit: diff --git a/openkaito/base/miner.py b/openkaito/base/miner.py index 605de96..89a4436 100644 --- a/openkaito/base/miner.py +++ b/openkaito/base/miner.py @@ -33,6 +33,14 @@ TextEmbeddingSynapse, ) +# Fiber +from fiber.chain.chain_utils import load_hotkey_keypair +from fiber.chain.interface import get_substrate +from fiber.logging_utils import get_logger + + + +logger = get_logger(__name__) class BaseMinerNeuron(BaseNeuron): """ @@ -44,6 +52,18 @@ class BaseMinerNeuron(BaseNeuron): def __init__(self): super().__init__(config=self.config()) + # Get the substrate instance in fiber + self.substrate = get_substrate( + subtensor_network=self.config["subtensor.network"], + subtensor_address=self.config["subtensor.chain_endpoint"] + ) + + # Load the hotkey keypair in fiber + self.keypair = load_hotkey_keypair( + wallet_name=self.config["wallet.name"], + hotkey_name=self.config["wallet.hotkey"], + ) + # Warn if allowing incoming requests from anyone. if not self.config.blacklist.force_validator_permit: bt.logging.warning( diff --git a/openkaito/base/validator.py b/openkaito/base/validator.py index 69175b5..768c468 100644 --- a/openkaito/base/validator.py +++ b/openkaito/base/validator.py @@ -27,9 +27,18 @@ import bittensor as bt import torch +# Fiber +from fiber.chain.metagraph import Metagraph +from fiber.chain.weights import set_node_weights +from fiber.chain.interface import get_substrate +from fiber.chain.chain_utils import load_hotkey_keypair +from fiber.logging_utils import get_logger + from openkaito.base.neuron import BaseNeuron +logger = get_logger(__name__) + class BaseValidatorNeuron(BaseNeuron): """ Base class for Bittensor validators. Your validator should inherit from this class. @@ -40,6 +49,28 @@ class BaseValidatorNeuron(BaseNeuron): def __init__(self): super().__init__(config=self.config()) + # Load the hotkey keypair in fiber + self.keypair = load_hotkey_keypair( + wallet_name=self.config["wallet.name"], + hotkey_name=self.config["wallet.hotkey"], + ) + + # Get the substrate instance in fiber + self.substrate = get_substrate( + subtensor_network=self.config["subtensor.network"], + subtensor_address=self.config["subtensor.chain_endpoint"] + ) + + # Get the metagraph instance in fiber + self.metagraph = Metagraph( + self.substrate, + netuid=self.config["netuid"], + load_old_nodes=False, + ) + + # Sync the nodes in the metagraph + self.metagraph.sync_nodes() + # Save a copy of the hotkeys to local memory. self.hotkeys = copy.deepcopy(self.metagraph.hotkeys) @@ -137,7 +168,8 @@ def run(self): if self.should_exit: break - # Sync metagraph and potentially set weights. + # Sync metagraph and potentially set weights (fiber) + self.substrate = get_substrate(subtensor_address=self.substrate.url) self.sync() self.step += 1 @@ -243,7 +275,7 @@ def set_weights(self): bt.logging.debug("uint_uids", uint_uids) # Set the weights on chain via our subtensor connection. - result = self.subtensor.set_weights( + set_node_weights( wallet=self.wallet, netuid=self.config.netuid, uids=uint_uids, @@ -252,7 +284,8 @@ def set_weights(self): wait_for_inclusion=False, version_key=self.spec_version, ) - bt.logging.info(f"Set weights: {result}") + bt.logging.info("Set weights") + self.metagraph.sync_nodes() def resync_metagraph(self): """Resyncs the metagraph and updates the hotkeys and moving averages based on the new metagraph.""" diff --git a/openkaito/utils/config.py b/openkaito/utils/config.py index 94feaa8..4534ba3 100644 --- a/openkaito/utils/config.py +++ b/openkaito/utils/config.py @@ -22,6 +22,9 @@ import bittensor as bt from loguru import logger +# Fiber +from fiber.constants import FINNEY_NETWORK + def check_config(cls, config: "bt.Config"): r"""Checks/validates the config namespace object.""" @@ -106,6 +109,13 @@ def add_args(cls, parser): default=False, ) + parser.add_argument( + "--subtensor.network", + type=str, + help="Subtensor network", + default=FINNEY_NETWORK, + ) + if neuron_type == "validator": parser.add_argument( "--neuron.num_concurrent_forwards", diff --git a/requirements.txt b/requirements.txt index 9e79dc1..552993f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,4 +13,5 @@ requests python-dateutil numpy<2.0 datasets>=3.0 -info-nce-pytorch>=0.1.4 \ No newline at end of file +info-nce-pytorch>=0.1.4 +fiber \ No newline at end of file