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

Black formatting #102

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
12 changes: 3 additions & 9 deletions docs/stream_tutorial/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ async def query_synapse(my_uid, wallet_name, hotkey, network, netuid):
wallet = bt.wallet(name=wallet_name, hotkey=hotkey)

# instantiate the metagraph with provided network and netuid
metagraph = bt.metagraph(
netuid=netuid, network=network, sync=True, lite=False
)
metagraph = bt.metagraph(netuid=netuid, network=network, sync=True, lite=False)

# Grab the axon you're serving
axon = metagraph.axons[my_uid]
Expand All @@ -40,9 +38,7 @@ async def query_synapse(my_uid, wallet_name, hotkey, network, netuid):
dendrite = bt.dendrite(wallet=wallet)

async def main():
responses = await dendrite(
[axon], syn, deserialize=False, streaming=True
)
responses = await dendrite([axon], syn, deserialize=False, streaming=True)

for resp in responses:
i = 0
Expand Down Expand Up @@ -73,9 +69,7 @@ async def main():
required=True,
help="Your unique miner ID on the chain",
)
parser.add_argument(
"--netuid", type=int, required=True, help="Network Unique ID"
)
parser.add_argument("--netuid", type=int, required=True, help="Network Unique ID")
parser.add_argument(
"--wallet_name", type=str, default="default", help="Name of the wallet"
)
Expand Down
4 changes: 1 addition & 3 deletions docs/stream_tutorial/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ def get_config() -> "bt.Config":
help="Chain endpoint to connect to.",
)
# Adds override arguments for network and netuid.
parser.add_argument(
"--netuid", type=int, default=1, help="The chain subnet uid."
)
parser.add_argument("--netuid", type=int, default=1, help="The chain subnet uid.")

parser.add_argument(
"--miner.root",
Expand Down
12 changes: 3 additions & 9 deletions docs/stream_tutorial/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ def __init__(self, config=None, axon=None, wallet=None, subtensor=None):
bt.logging.info(f"Running miner on uid: {self.my_subnet_uid}")

# The axon handles request processing, allowing validators to send this process requests.
self.axon = axon or bt.axon(
wallet=self.wallet, port=self.config.axon.port
)
self.axon = axon or 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 axon.")
print(f"Attaching forward function to axon. {self._prompt}")
Expand Down Expand Up @@ -161,9 +159,7 @@ def run(self):
self.axon.serve(netuid=self.config.netuid, subtensor=self.subtensor)

# Start starts the miner's axon, making it active on the network.
bt.logging.info(
f"Starting axon server on port: {self.config.axon.port}"
)
bt.logging.info(f"Starting axon server on port: {self.config.axon.port}")
self.axon.start()

# --- Run until should_exit = True.
Expand Down Expand Up @@ -346,9 +342,7 @@ async def _prompt(text: str, send: Send):
processing steps or modify how tokens are sent back to the client.
"""
bt.logging.trace("HI. _PROMPT()")
input_ids = tokenizer(
text, return_tensors="pt"
).input_ids.squeeze()
input_ids = tokenizer(text, return_tensors="pt").input_ids.squeeze()
buffer = []
bt.logging.debug(f"Input text: {text}")
bt.logging.debug(f"Input ids: {input_ids}")
Expand Down
4 changes: 1 addition & 3 deletions docs/stream_tutorial/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ async def process_streaming_response(self, response: StreamingResponse):
"""
if self.completion is None:
self.completion = ""
bt.logging.debug(
"Processing streaming response (StreamingSynapse base class)."
)
bt.logging.debug("Processing streaming response (StreamingSynapse base class).")
async for chunk in response.content.iter_any():
bt.logging.debug(f"Processing chunk: {chunk}")
tokens = chunk.decode("utf-8").split("\n")
Expand Down
2 changes: 1 addition & 1 deletion neurons/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ async def priority(self, synapse: template.protocol.Dummy) -> float:
if synapse.dendrite is None or synapse.dendrite.hotkey is None:
bt.logging.warning("Received a request without a dendrite or hotkey.")
return 0.0

# TODO(developer): Define how miners should prioritize requests.
caller_uid = self.metagraph.hotkeys.index(
synapse.dendrite.hotkey
Expand Down
1 change: 1 addition & 0 deletions neurons/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

# import base validator class which takes care of most of the boilerplate
from template.base.validator import BaseValidatorNeuron

# Bittensor Validator Template:
from template.validator import forward

Expand Down
4 changes: 1 addition & 3 deletions template/api/dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def prepare_synapse(self, dummy_input: int) -> Dummy:
synapse.dummy_input = dummy_input
return synapse

def process_responses(
self, responses: List[Union["bt.Synapse", Any]]
) -> List[int]:
def process_responses(self, responses: List[Union["bt.Synapse", Any]]) -> List[int]:
outputs = []
for response in responses:
if response.dendrite.status_code != 200:
Expand Down
37 changes: 9 additions & 28 deletions template/api/examples/subnet21.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,13 @@ def prepare_synapse(

return synapse

def process_responses(
self, responses: List[Union["bt.Synapse", Any]]
) -> str:
def process_responses(self, responses: List[Union["bt.Synapse", Any]]) -> str:
success = False
failure_modes = {"code": [], "message": []}
for response in responses:
if response.dendrite.status_code != 200:
failure_modes["code"].append(response.dendrite.status_code)
failure_modes["message"].append(
response.dendrite.status_message
)
failure_modes["message"].append(response.dendrite.status_message)
continue

stored_cid = (
Expand Down Expand Up @@ -107,35 +103,24 @@ def prepare_synapse(self, cid: str) -> RetrieveUser:
synapse = RetrieveUser(data_hash=cid)
return synapse

def process_responses(
self, responses: List[Union["bt.Synapse", Any]]
) -> bytes:
def process_responses(self, responses: List[Union["bt.Synapse", Any]]) -> bytes:
success = False
decrypted_data = b""
for response in responses:
bt.logging.trace(f"response: {response.dendrite.dict()}")
if (
response.dendrite.status_code != 200
or response.encrypted_data is None
):
if response.dendrite.status_code != 200 or response.encrypted_data is None:
continue

# Decrypt the response
bt.logging.trace(
f"encrypted_data: {response.encrypted_data[:100]}"
)
bt.logging.trace(f"encrypted_data: {response.encrypted_data[:100]}")
encrypted_data = base64.b64decode(response.encrypted_data)
bt.logging.debug(
f"encryption_payload: {response.encryption_payload}"
)
bt.logging.debug(f"encryption_payload: {response.encryption_payload}")
if (
response.encryption_payload is None
or response.encryption_payload == ""
or response.encryption_payload == "{}"
):
bt.logging.warning(
"No encryption payload found. Unencrypted data."
)
bt.logging.warning("No encryption payload found. Unencrypted data.")
decrypted_data = encrypted_data
else:
decrypted_data = decrypt_data_with_private_key(
Expand All @@ -148,18 +133,14 @@ def process_responses(
break

if success:
bt.logging.info(
f"Returning retrieved data: {decrypted_data[:100]}"
)
bt.logging.info(f"Returning retrieved data: {decrypted_data[:100]}")
else:
bt.logging.error("Failed to retrieve data.")

return decrypted_data


async def test_store_and_retrieve(
netuid: int = 22, wallet: "bt.wallet" = None
):
async def test_store_and_retrieve(netuid: int = 22, wallet: "bt.wallet" = None):
# Example usage
wallet = wallet or bt.wallet()

Expand Down
13 changes: 4 additions & 9 deletions template/api/get_query_axons.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ async def ping_uids(dendrite, metagraph, uids, timeout=3):
bt.logging.debug(f"ping() failed uids : {failed_uids}")
return successful_uids, failed_uids


async def get_query_api_nodes(dendrite, metagraph, n=0.1, timeout=3):
"""
Fetches the available API nodes to query for the particular subnet.
Expand All @@ -75,13 +76,9 @@ async def get_query_api_nodes(dendrite, metagraph, n=0.1, timeout=3):
Returns:
list: A list of UIDs representing the available API nodes.
"""
bt.logging.debug(
f"Fetching available API nodes for subnet {metagraph.netuid}"
)
bt.logging.debug(f"Fetching available API nodes for subnet {metagraph.netuid}")
vtrust_uids = [
uid.item()
for uid in metagraph.uids
if metagraph.validator_trust[uid] > 0
uid.item() for uid in metagraph.uids if metagraph.validator_trust[uid] > 0
]
top_uids = np.where(metagraph.S > np.quantile(metagraph.S, 1 - n))[0].tolist()
init_query_uids = set(top_uids).intersection(set(vtrust_uids))
Expand All @@ -96,9 +93,7 @@ async def get_query_api_nodes(dendrite, metagraph, n=0.1, timeout=3):
return query_uids


async def get_query_api_axons(
wallet, metagraph=None, n=0.1, timeout=3, uids=None
):
async def get_query_api_axons(wallet, metagraph=None, n=0.1, timeout=3, uids=None):
"""
Retrieves the axons of query API nodes based on their availability and stake.

Expand Down
6 changes: 5 additions & 1 deletion template/base/miner.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from typing import Union


class BaseMinerNeuron(BaseNeuron):
"""
Base class for Bittensor miners.
Expand All @@ -53,7 +54,10 @@ def __init__(self, config=None):
"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, config=self.config() if callable(self.config) else self.config)
self.axon = bt.axon(
wallet=self.wallet,
config=self.config() if callable(self.config) else self.config,
)

# Attach determiners which functions are called when servicing a request.
bt.logging.info(f"Attaching forward function to miner axon.")
Expand Down
18 changes: 5 additions & 13 deletions template/base/neuron.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,8 @@ def __init__(self, config=None):
# The wallet holds the cryptographic key pairs for the miner.
if self.config.mock:
self.wallet = bt.MockWallet(config=self.config)
self.subtensor = MockSubtensor(
self.config.netuid, wallet=self.wallet
)
self.metagraph = MockMetagraph(
self.config.netuid, subtensor=self.subtensor
)
self.subtensor = MockSubtensor(self.config.netuid, wallet=self.wallet)
self.metagraph = MockMetagraph(self.config.netuid, subtensor=self.subtensor)
else:
self.wallet = bt.wallet(config=self.config)
self.subtensor = bt.subtensor(config=self.config)
Expand All @@ -100,9 +96,7 @@ def __init__(self, config=None):
self.check_registered()

# Each miner gets a unique identity (UID) in the network for differentiation.
self.uid = self.metagraph.hotkeys.index(
self.wallet.hotkey.ss58_address
)
self.uid = self.metagraph.hotkeys.index(self.wallet.hotkey.ss58_address)
bt.logging.info(
f"Running neuron on subnet: {self.config.netuid} with uid {self.uid} using network: {self.subtensor.chain_endpoint}"
)
Expand Down Expand Up @@ -163,10 +157,8 @@ def should_set_weights(self) -> bool:

# Define appropriate logic for when set weights.
return (
(self.block - self.metagraph.last_update[self.uid])
> self.config.neuron.epoch_length
and self.neuron_type != "MinerNeuron"
) # don't set weights if you're a miner
self.block - self.metagraph.last_update[self.uid]
) > self.config.neuron.epoch_length and self.neuron_type != "MinerNeuron" # don't set weights if you're a miner

def save_state(self):
bt.logging.warning(
Expand Down
49 changes: 28 additions & 21 deletions template/base/utils/weight_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
U16_MAX = 65535


def normalize_max_weight(
x: np.ndarray, limit: float = 0.1
) -> np.ndarray:
def normalize_max_weight(x: np.ndarray, limit: float = 0.1) -> np.ndarray:
r"""Normalizes the numpy array x so that sum(x) = 1 and the max value is not greater than the limit.
Args:
x (:obj:`np.ndarray`):
Expand Down Expand Up @@ -44,7 +42,7 @@ def normalize_max_weight(

# Determine the cutoff based on the index
cutoff_scale = (limit * cumsum[n_values - 1] - epsilon) / (
1 - (limit * (len(estimation) - n_values))
1 - (limit * (len(estimation) - n_values))
)
cutoff = cutoff_scale * values.sum()

Expand All @@ -57,7 +55,7 @@ def normalize_max_weight(


def convert_weights_and_uids_for_emit(
uids: np.ndarray, weights: np.ndarray
uids: np.ndarray, weights: np.ndarray
) -> Tuple[List[int], List[int]]:
r"""Converts weights into integer u32 representation that sum to MAX_INT_WEIGHT.
Args:
Expand Down Expand Up @@ -105,7 +103,9 @@ def convert_weights_and_uids_for_emit(
weights = [
float(value) / max_weight for value in weights
] # max-upscale values (max_weight = 1).
bittensor.logging.debug(f"setting on chain max: {max_weight} and weights: {weights}")
bittensor.logging.debug(
f"setting on chain max: {max_weight} and weights: {weights}"
)

weight_vals = []
weight_uids = []
Expand All @@ -123,15 +123,26 @@ def convert_weights_and_uids_for_emit(


def process_weights_for_netuid(
uids,
weights: np.ndarray,
netuid: int,
subtensor: "bittensor.subtensor",
metagraph: "bittensor.metagraph" = None,
exclude_quantile: int = 0,
) -> Union[tuple[ndarray[Any, dtype[Any]], Union[
Union[ndarray[Any, dtype[floating[Any]]], ndarray[Any, dtype[complexfloating[Any, Any]]]], Any]], tuple[
ndarray[Any, dtype[Any]], ndarray], tuple[Any, ndarray]]:
uids,
weights: np.ndarray,
netuid: int,
subtensor: "bittensor.subtensor",
metagraph: "bittensor.metagraph" = None,
exclude_quantile: int = 0,
) -> Union[
tuple[
ndarray[Any, dtype[Any]],
Union[
Union[
ndarray[Any, dtype[floating[Any]]],
ndarray[Any, dtype[complexfloating[Any, Any]]],
],
Any,
],
],
tuple[ndarray[Any, dtype[Any]], ndarray],
tuple[Any, ndarray],
]:
bittensor.logging.debug("process_weights_for_netuid()")
bittensor.logging.debug("weights", weights)
bittensor.logging.debug("netuid", netuid)
Expand Down Expand Up @@ -169,14 +180,10 @@ def process_weights_for_netuid(
bittensor.logging.warning(
"No non-zero weights less then min allowed weight, returning all ones."
)
weights = (
np.ones(metagraph.n) * 1e-5
) # creating minimum even non-zero weights
weights = np.ones(metagraph.n) * 1e-5 # creating minimum even non-zero weights
weights[non_zero_weight_idx] += non_zero_weights
bittensor.logging.debug("final_weights", weights)
normalized_weights = normalize_max_weight(
x=weights, limit=max_weight_limit
)
normalized_weights = normalize_max_weight(x=weights, limit=max_weight_limit)
return np.arange(len(normalized_weights)), normalized_weights

bittensor.logging.debug("non_zero_weights", non_zero_weights)
Expand Down
Loading