Skip to content

Commit

Permalink
Merge pull request #899 from opentensor/nobunaga
Browse files Browse the repository at this point in the history
Nobunaga Release V3.3.3
  • Loading branch information
opentaco committed Sep 5, 2022
2 parents 0ea20ea + 8eb8a31 commit 67b49ae
Show file tree
Hide file tree
Showing 27 changed files with 432 additions and 223 deletions.
9 changes: 7 additions & 2 deletions bin/btcli
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
#!/usr/bin/env python3

import bittensor
import sys

import bittensor

if __name__ == '__main__':
bittensor.cli().run()
args = sys.argv[1:]
bittensor.cli(args=args).run()

# The MIT License (MIT)
# Copyright © 2021 Yuma Rao
# Copyright © 2022 Opentensor Foundation

# 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
Expand Down
2 changes: 1 addition & 1 deletion bittensor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from rich.console import Console

# Bittensor code and protocol version.
__version__ = '3.0.0'
__version__ = '3.3.3'
version_split = __version__.split(".")
__version_as_int__ = (100 * int(version_split[0])) + (10 * int(version_split[1])) + (1 * int(version_split[2]))

Expand Down
36 changes: 29 additions & 7 deletions bittensor/_axon/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ def __new__(
synapse_causal_lm: 'Callable' = None,
synapse_causal_lm_next: 'Callable' = None,
synapse_seq_2_seq: 'Callable' = None,
synapse_lasthidden_timeout: int = None,
synapse_causallm_timeout: int = None,
synapse_causallmnext_timeout: int = None,
synapse_seq2seq_timeout: int = None,
synapse_checks: 'Callable' = None,
thread_pool: 'futures.ThreadPoolExecutor' = None,
priority_threadpool: 'bittensor.prioritythreadpool' = None,
server: 'grpc._Server' = None,
port: int = None,
ip: str = None,
Expand Down Expand Up @@ -120,6 +125,10 @@ def __new__(
config.axon.forward_timeout = forward_timeout if forward_timeout != None else config.axon.forward_timeout
config.axon.backward_timeout = backward_timeout if backward_timeout != None else config.axon.backward_timeout
config.axon.compression = compression if compression != None else config.axon.compression
config.axon.lasthidden_timeout = synapse_lasthidden_timeout if synapse_lasthidden_timeout != None else config.axon.lasthidden_timeout
config.axon.causallm_timeout = synapse_causallm_timeout if synapse_causallm_timeout != None else config.axon.causallm_timeout
config.axon.causallmnext_timeout = synapse_causallmnext_timeout if synapse_causallmnext_timeout is not None else config.axon.causallmnext_timeout
config.axon.seq2seq_timeout = synapse_seq2seq_timeout if synapse_seq2seq_timeout != None else config.axon.seq2seq_timeout
axon.check_config( config )

# Determine the grpc compression algorithm
Expand Down Expand Up @@ -147,15 +156,20 @@ def __new__(
synapses[bittensor.proto.Synapse.SynapseType.TEXT_CAUSAL_LM] = synapse_causal_lm
synapses[bittensor.proto.Synapse.SynapseType.TEXT_CAUSAL_LM_NEXT] = synapse_causal_lm_next
synapses[bittensor.proto.Synapse.SynapseType.TEXT_SEQ_2_SEQ] = synapse_seq_2_seq

synapse_timeouts = {
bittensor.proto.Synapse.SynapseType.TEXT_LAST_HIDDEN_STATE: config.axon.lasthidden_timeout,
bittensor.proto.Synapse.SynapseType.TEXT_CAUSAL_LM: config.axon.causallm_timeout,
bittensor.proto.Synapse.SynapseType.TEXT_CAUSAL_LM_NEXT: config.axon.causallmnext_timeout,
bittensor.proto.Synapse.SynapseType.TEXT_SEQ_2_SEQ: config.axon.seq2seq_timeout
}

synapse_check_function = synapse_checks if synapse_checks != None else axon.default_synapse_check

if priority != None:
if priority != None and priority_threadpool == None:
priority_threadpool = bittensor.prioritythreadpool(config=config)
else:
priority_threadpool = None

axon_instance = axon_impl.Axon(
axon_instance = axon_impl.Axon(
wallet = wallet,
server = server,
ip = config.axon.ip,
Expand All @@ -164,6 +178,7 @@ def __new__(
backward = backward_text,
synapses = synapses,
synapse_checks = synapse_check_function,
synapse_timeouts = synapse_timeouts,
priority = priority,
priority_threadpool = priority_threadpool,
forward_timeout = config.axon.forward_timeout,
Expand Down Expand Up @@ -217,6 +232,14 @@ def add_args( cls, parser: argparse.ArgumentParser, prefix: str = None ):
help='''maximum size of tasks in priority queue''', default = bittensor.defaults.axon.priority.maxsize)
parser.add_argument('--' + prefix_str + 'axon.compression', type=str,
help='''Which compression algorithm to use for compression (gzip, deflate, NoCompression) ''', default = bittensor.defaults.axon.compression)
parser.add_argument('--' + prefix_str + 'axon.lasthidden_timeout', type = int,
help='Timeout for last hidden synapse', default= bittensor.__blocktime__)
parser.add_argument('--' + prefix_str + 'axon.causallm_timeout', type = int,
help='Timeout for causallm synapse', default= bittensor.__blocktime__)
parser.add_argument('--' + prefix_str + 'axon.causallmnext_timeout', type = int,
help='Timeout for causallmnext synapse', default= bittensor.__blocktime__)
parser.add_argument('--' + prefix_str + 'axon.seq2seq_timeout', type = int,
help='Timeout for seq2seq synapse', default= 3*bittensor.__blocktime__)
except argparse.ArgumentError:
# re-parsing arguments.
pass
Expand All @@ -227,13 +250,13 @@ def add_args( cls, parser: argparse.ArgumentParser, prefix: str = None ):
def add_defaults(cls, defaults):
""" Adds parser defaults to object from enviroment variables.
"""
defaults.axon = bittensor.config()
defaults.axon = bittensor.Config()
defaults.axon.port = os.getenv('BT_AXON_PORT') if os.getenv('BT_AXON_PORT') != None else 8091
defaults.axon.ip = os.getenv('BT_AXON_IP') if os.getenv('BT_AXON_IP') != None else '[::]'
defaults.axon.max_workers = os.getenv('BT_AXON_MAX_WORERS') if os.getenv('BT_AXON_MAX_WORERS') != None else 10
defaults.axon.maximum_concurrent_rpcs = os.getenv('BT_AXON_MAXIMUM_CONCURRENT_RPCS') if os.getenv('BT_AXON_MAXIMUM_CONCURRENT_RPCS') != None else 400

defaults.axon.priority = bittensor.config()
defaults.axon.priority = bittensor.Config()
defaults.axon.priority.max_workers = os.getenv('BT_AXON_PRIORITY_MAX_WORKERS') if os.getenv('BT_AXON_PRIORITY_MAX_WORKERS') != None else 10
defaults.axon.priority.maxsize = os.getenv('BT_AXON_PRIORITY_MAXSIZE') if os.getenv('BT_AXON_PRIORITY_MAXSIZE') != None else -1

Expand Down Expand Up @@ -325,7 +348,6 @@ def intercept_service(self, continuation, handler_call_details):
self.message = str(e)
return self._deny


def vertification(self,meta):
r"""vertification of signature in metadata. Uses the pubkey and nounce
"""
Expand Down
17 changes: 10 additions & 7 deletions bittensor/_axon/axon_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

import bittensor
import bittensor.utils.stats as stat_utils
from datetime import datetime

logger = logger.opt(colors=True)

Expand All @@ -48,6 +49,7 @@ def __init__(
backward: 'Callable',
synapses: dict,
synapse_checks: 'Callable',
synapse_timeouts: dict,
priority: 'Callable' = None,
priority_threadpool: 'bittensor.prioritythreadpool' = None,
forward_timeout: int = None,
Expand Down Expand Up @@ -81,6 +83,7 @@ def __init__(
self.backward_timeout = backward_timeout
self.synapse_callbacks = synapses
self.synapse_checks = synapse_checks
self.synapse_timeouts = synapse_timeouts
self.stats = self._init_stats()
self.started = None
self.optimizer_step = None
Expand Down Expand Up @@ -184,6 +187,7 @@ def _forward(self, request):
synapse_responses = [ synapse.empty() for synapse in synapses ] # We fill nones for non success.
synapse_is_response = [ False for _ in synapses ]
synapse_call_times = [ 0 for _ in synapses ]
synapse_timeout = min( [self.synapse_timeouts[s.synapse_type] for s in synapses] + [bittensor.__blocktime__] )
start_time = clock.time()

# ==================================================================
Expand All @@ -199,7 +203,7 @@ def check_if_should_return() -> bool:
# ==============================================================
# ==== Function which prints all log statements per synapse ====
# ==============================================================
def finalize_codes_stats_and_logs():
def finalize_codes_stats_and_logs( message = None):
for index, synapse in enumerate( synapses ):
request.synapses [ index ].return_code = synapse_codes[ index ] # Set synapse wire proto codes.
request.synapses [ index ].message = synapse_messages[ index ] # Set synapse wire proto message
Expand All @@ -212,7 +216,7 @@ def finalize_codes_stats_and_logs():
pubkey = request.hotkey,
inputs = synapse_inputs [index] ,
outputs = None if synapse_responses[index] == None else list( synapse_responses[index].shape ),
message = synapse_messages[ index ],
message = synapse_messages[ index ] if message == None else message,
synapse = synapse.synapse_type
)

Expand Down Expand Up @@ -280,9 +284,9 @@ def finalize_codes_stats_and_logs():
inputs_x = deserialized_forward_tensors,
synapses = synapses,
priority = priority,
hotkey= request.hotkey
hotkey = request.hotkey
)
forward_response_tensors, forward_codes, forward_messages = future.result( timeout= self.forward_timeout )
forward_response_tensors, forward_codes, forward_messages = future.result( timeout = synapse_timeout - (clock.time() - start_time) )
else:

forward_response_tensors, forward_codes, forward_messages = self.forward_callback(
Expand Down Expand Up @@ -318,11 +322,10 @@ def finalize_codes_stats_and_logs():
except Exception as e:
code = bittensor.proto.ReturnCode.UnknownException
call_time = clock.time() - start_time
message = str ( e )
synapse_codes = [code for _ in synapses ]
synapse_call_times = [call_time for _ in synapses ]
synapse_messages = [ message for _ in synapses ]
finalize_codes_stats_and_logs()
synapse_messages = [ 'Exception on Server' for _ in synapses ]
finalize_codes_stats_and_logs(message = str(e))
return [], bittensor.proto.ReturnCode.UnknownException, request.synapses

# =================================================
Expand Down
24 changes: 15 additions & 9 deletions bittensor/_cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
# The MIT License (MIT)
# Copyright © 2021 Yuma Rao
# Copyright © 2022 Opentensor Foundation

# 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
Expand All @@ -21,41 +22,46 @@
import argparse
import os
import sys
from typing import List
from typing import List, Optional

import bittensor
import torch
from rich.prompt import Confirm, Prompt
from substrateinterface.utils.ss58 import ss58_decode, ss58_encode

from . import cli_impl

console = bittensor.__console__

class cli:
"""
Create and init the CLI class, which handles the coldkey, hotkey and tau transfer
Create and init the CLI class, which handles the coldkey, hotkey and tao transfer
"""
def __new__(
cls,
config: 'bittensor.Config' = None,
cls,
config: Optional['bittensor.Config'] = None,
args: Optional[List[str]] = None,
) -> 'bittensor.CLI':
r""" Creates a new bittensor.cli from passed arguments.
Args:
config (:obj:`bittensor.Config`, `optional`):
bittensor.cli.config()
args (`List[str]`, `optional`):
The arguments to parse from the command line.
"""
if config == None:
config = cli.config()
config = cli.config(args)
cli.check_config( config )
return cli_impl.CLI( config = config)

@staticmethod
def config() -> 'bittensor.config':
def config(args: List[str]) -> 'bittensor.config':
""" From the argument parser, add config to bittensor.executor and local config
Return: bittensor.config object
"""
parser = argparse.ArgumentParser(description="Bittensor cli", usage="btcli <command> <command args>", add_help=True)
parser = argparse.ArgumentParser(
description=f"bittensor cli v{bittensor.__version__}",
usage="btcli <command> <command args>",
add_help=True)

cmd_parsers = parser.add_subparsers(dest='command')
overview_parser = cmd_parsers.add_parser(
Expand Down Expand Up @@ -611,7 +617,7 @@ def config() -> 'bittensor.config':
required=False
)

return bittensor.config( parser )
return bittensor.config( parser, args=args )

@staticmethod
def check_config (config: 'bittensor.Config'):
Expand Down
2 changes: 1 addition & 1 deletion bittensor/_cli/cli_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ def unstake( self ):
if not self.config.no_prompt:
if not Confirm.ask("Do you want to unstake from the following keys:\n" + \
"".join([
f" [bold white]- {wallet.hotkey_str}: {amount}𝜏[/bold white]\n" for wallet, amount in zip(final_wallets, final_amounts)
f" [bold white]- {wallet.hotkey_str}: {amount.tao}𝜏[/bold white]\n" for wallet, amount in zip(final_wallets, final_amounts)
])
):
return None
Expand Down
47 changes: 39 additions & 8 deletions bittensor/_config/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
# The MIT License (MIT)
# Copyright © 2021 Yuma Rao
# Copyright © 2022 Opentensor Foundation

# 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
Expand All @@ -19,12 +20,14 @@
# DEALINGS IN THE SOFTWARE.

import os
from argparse import ArgumentParser
import sys
from argparse import ArgumentParser, Namespace
from typing import List, Optional

import bittensor
import yaml
from loguru import logger

import bittensor
from . import config_impl

logger = logger.opt(colors=True)
Expand All @@ -37,13 +40,15 @@ class InvalidConfigFile(Exception):
""" In place of YAMLError
"""

def __new__( cls, parser: ArgumentParser = None, strict: bool = False ):
def __new__( cls, parser: ArgumentParser = None, strict: bool = False, args: Optional[List[str]] = None ):
r""" Translates the passed parser into a nested Bittensor config.
Args:
parser (argparse.Parser):
Command line parser object.
strict (bool):
If true, the command line arguments are strictly parsed.
args (list of str):
Command line arguments.
Returns:
config (bittensor.Config):
Nested config object created from parser arguments.
Expand All @@ -69,8 +74,15 @@ def __new__( cls, parser: ArgumentParser = None, strict: bool = False ):
except Exception as e:
config_file_path = None

# Get args from argv if not passed in.
if args == None:
args = sys.argv[1:]

# Parse args not strict
params = cls.__parse_args__(args=args, parser=parser, strict=False)

# 2. Optionally check for --strict, if stict we will parse the args strictly.
strict = parser.parse_known_args()[0].strict
strict = params.strict

if config_file_path != None:
config_file_path = os.path.expanduser(config_file_path)
Expand All @@ -83,10 +95,8 @@ def __new__( cls, parser: ArgumentParser = None, strict: bool = False ):
print('Error in loading: {} using default parser settings'.format(e))

# 2. Continue with loading in params.
if not strict:
params = parser.parse_known_args()[0]
else:
params = parser.parse_args()
params = cls.__parse_args__(args=args, parser=parser, strict=strict)

_config = config_impl.Config()

# Splits params on dot syntax i.e neuron.axon_port
Expand All @@ -107,6 +117,27 @@ def __new__( cls, parser: ArgumentParser = None, strict: bool = False ):

return _config

@staticmethod
def __parse_args__( args: List[str], parser: ArgumentParser = None, strict: bool = False) -> Namespace:
"""Parses the passed args use the passed parser.
Args:
args (List[str]):
List of arguments to parse.
parser (argparse.ArgumentParser):
Command line parser object.
strict (bool):
If true, the command line arguments are strictly parsed.
Returns:
Namespace:
Namespace object created from parser arguments.
"""
if not strict:
params = parser.parse_known_args(args=args)[0]
else:
params = parser.parse_args(args=args)

return params

@staticmethod
def full():
""" From the parser, add arguments to multiple bittensor sub-modules
Expand Down
1 change: 0 additions & 1 deletion bittensor/_dendrite/dendrite_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -860,4 +860,3 @@ def to_wandb( self ):
except Exception as e:
bittensor.logging.error( prefix='failed dendrite.to_wandb()', sufix = str(e))
return {}

1 change: 0 additions & 1 deletion bittensor/_neuron/text/core_server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ def __init__(
causallmnext = None,
seq2seq = None,
synapse_list = None,

):
if config == None: config = server.config()
config = config;
Expand Down
Loading

0 comments on commit 67b49ae

Please sign in to comment.