Skip to content
Open
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
9 changes: 9 additions & 0 deletions neurons/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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)
10 changes: 10 additions & 0 deletions neurons/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__()
Expand Down Expand Up @@ -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:
Expand Down
20 changes: 20 additions & 0 deletions openkaito/base/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand All @@ -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(
Expand Down
39 changes: 36 additions & 3 deletions openkaito/base/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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."""
Expand Down
10 changes: 10 additions & 0 deletions openkaito/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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",
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ requests
python-dateutil
numpy<2.0
datasets>=3.0
info-nce-pytorch>=0.1.4
info-nce-pytorch>=0.1.4
fiber