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

Add ocr_subnet dir, and TODOs #2

Merged
merged 1 commit into from
Dec 8, 2023
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
33 changes: 33 additions & 0 deletions ocr_subnet/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# The MIT License (MIT)
# Copyright © 2023 Yuma Rao
# TODO(developer): Set your name
# Copyright © 2023 <your name>

# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the “Software”), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all copies or substantial portions of
# the Software.

# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

# TODO(developer): Change this value when updating your code base.
# Define the version of the template module.
__version__ = "0.0.0"
version_split = __version__.split(".")
__spec_version__ = (
(1000 * int(version_split[0]))
+ (10 * int(version_split[1]))
+ (1 * int(version_split[2]))
)

# Import all submodules.
from . import protocol
from . import base
from . import validator
Empty file added ocr_subnet/base/__init__.py
Empty file.
215 changes: 215 additions & 0 deletions ocr_subnet/base/miner.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
# The MIT License (MIT)
# Copyright © 2023 Yuma Rao

# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
# documentation files (the “Software”), to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in all copies or substantial portions of
# the Software.

# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
# THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.

import time
import torch
import asyncio
import threading
import traceback

import bittensor as bt

from ocr_subnet.base.neuron import BaseNeuron


class BaseMinerNeuron(BaseNeuron):
"""
Base class for Bittensor miners.
"""

def __init__(self, config=None):
super().__init__(config=config)

# Warn if allowing incoming requests from anyone.
if not self.config.blacklist.force_validator_permit:
bt.logging.warning(
"You are allowing non-validators to send requests to your miner. This is a security risk."
)
if self.config.blacklist.allow_non_registered:
bt.logging.warning(
"You are allowing non-registered entities to send requests to your miner. This is a security risk."
)

# The axon handles request processing, allowing validators to send this miner requests.
self.axon = bt.axon(wallet=self.wallet, port=self.config.axon.port)

# Attach determiners which functions are called when servicing a request.
bt.logging.info(f"Attaching forward function to miner axon.")
self.axon.attach(
forward_fn=self.forward,
blacklist_fn=self.blacklist,
priority_fn=self.priority,
)
bt.logging.info(f"Axon created: {self.axon}")

# Instantiate runners
self.should_exit: bool = False
self.is_running: bool = False
self.thread: threading.Thread = None
self.lock = asyncio.Lock()

def run(self):
"""
Initiates and manages the main loop for the miner on the Bittensor network. The main loop handles graceful shutdown on keyboard interrupts and logs unforeseen errors.

This function performs the following primary tasks:
1. Check for registration on the Bittensor network.
2. Starts the miner's axon, making it active on the network.
3. Periodically resynchronizes with the chain; updating the metagraph with the latest network state and setting weights.

The miner continues its operations until `should_exit` is set to True or an external interruption occurs.
During each epoch of its operation, the miner waits for new blocks on the Bittensor network, updates its
knowledge of the network (metagraph), and sets its weights. This process ensures the miner remains active
and up-to-date with the network's latest state.

Note:
- The function leverages the global configurations set during the initialization of the miner.
- The miner's axon serves as its interface to the Bittensor network, handling incoming and outgoing requests.

Raises:
KeyboardInterrupt: If the miner is stopped by a manual interruption.
Exception: For unforeseen errors during the miner's operation, which are logged for diagnosis.
"""

# Check that miner is registered on the network.
self.sync()

# Serve passes the axon information to the network + netuid we are hosting on.
# This will auto-update if the axon port of external ip have changed.
bt.logging.info(
f"Serving miner axon {self.axon} on network: {self.config.subtensor.chain_endpoint} with netuid: {self.config.netuid}"
)
self.axon.serve(netuid=self.config.netuid, subtensor=self.subtensor)

# Start starts the miner's axon, making it active on the network.
self.axon.start()

bt.logging.info(f"Miner starting at block: {self.block}")

# This loop maintains the miner's operations until intentionally stopped.
try:
while not self.should_exit:
while (
self.block - self.metagraph.last_update[self.uid]
< self.config.neuron.epoch_length
):
# Wait before checking again.
time.sleep(1)

# Check if we should exit.
if self.should_exit:
break

# Sync metagraph and potentially set weights.
self.sync()
self.step += 1

# If someone intentionally stops the miner, it'll safely terminate operations.
except KeyboardInterrupt:
self.axon.stop()
bt.logging.success("Miner killed by keyboard interrupt.")
exit()

# In case of unforeseen errors, the miner will log the error and continue operations.
except Exception as e:
bt.logging.error(traceback.format_exc())

def run_in_background_thread(self):
"""
Starts the miner's operations in a separate background thread.
This is useful for non-blocking operations.
"""
if not self.is_running:
bt.logging.debug("Starting miner in background thread.")
self.should_exit = False
self.thread = threading.Thread(target=self.run, daemon=True)
self.thread.start()
self.is_running = True
bt.logging.debug("Started")

def stop_run_thread(self):
"""
Stops the miner's operations that are running in the background thread.
"""
if self.is_running:
bt.logging.debug("Stopping miner in background thread.")
self.should_exit = True
self.thread.join(5)
self.is_running = False
bt.logging.debug("Stopped")

def __enter__(self):
"""
Starts the miner's operations in a background thread upon entering the context.
This method facilitates the use of the miner in a 'with' statement.
"""
self.run_in_background_thread()
return self

def __exit__(self, exc_type, exc_value, traceback):
"""
Stops the miner's background operations upon exiting the context.
This method facilitates the use of the miner in a 'with' statement.

Args:
exc_type: The type of the exception that caused the context to be exited.
None if the context was exited without an exception.
exc_value: The instance of the exception that caused the context to be exited.
None if the context was exited without an exception.
traceback: A traceback object encoding the stack trace.
None if the context was exited without an exception.
"""
self.stop_run_thread()

def set_weights(self):
"""
Self-assigns a weight of 1 to the current miner (identified by its UID) and
a weight of 0 to all other peers in the network. The weights determine the trust level the miner assigns to other nodes on the network.

Raises:
Exception: If there's an error while setting weights, the exception is logged for diagnosis.
"""
try:
# --- query the chain for the most current number of peers on the network
chain_weights = torch.zeros(
self.subtensor.subnetwork_n(netuid=self.metagraph.netuid)
)
chain_weights[self.uid] = 1

# --- Set weights.
self.subtensor.set_weights(
wallet=self.wallet,
netuid=self.metagraph.netuid,
uids=torch.arange(0, len(chain_weights)),
weights=chain_weights.to("cpu"),
wait_for_inclusion=False,
version_key=self.spec_version,
)

except Exception as e:
bt.logging.error(
f"Failed to set weights on chain with exception: { e }"
)

bt.logging.info(f"Set weights: {chain_weights}")

def resync_metagraph(self):
"""Resyncs the metagraph and updates the hotkeys and moving averages based on the new metagraph."""
bt.logging.info("resync_metagraph()")

# Sync the metagraph.
self.metagraph.sync(subtensor=self.subtensor)
Loading