From 4e44f132339209deb9c6d060ed043ded662cee4c Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 13 Aug 2024 13:09:45 -0700 Subject: [PATCH 1/3] Optimize imports --- bittensor/__init__.py | 2 +- bittensor/core/chain_data.py | 4 +- bittensor/core/config.py | 9 ++-- bittensor/core/settings.py | 5 +-- bittensor/core/subtensor.py | 41 +++++++++---------- bittensor/core/tensor.py | 8 ++-- bittensor/core/threadpool.py | 17 ++++---- bittensor/utils/__init__.py | 1 - bittensor/utils/btlogging/format.py | 1 - bittensor/utils/btlogging/loggingmachine.py | 1 - .../deprecated/extrinsics/set_weights.py | 2 +- .../utils/deprecated/extrinsics/transfer.py | 2 +- bittensor/utils/version.py | 3 +- 13 files changed, 46 insertions(+), 50 deletions(-) diff --git a/bittensor/__init__.py b/bittensor/__init__.py index b131d13f0a..cf2cb8ce7f 100644 --- a/bittensor/__init__.py +++ b/bittensor/__init__.py @@ -19,8 +19,8 @@ import warnings from .core.settings import __version__, version_split, DEFAULTS -from .utils.deprecated import * from .utils.btlogging import logging +from .utils.deprecated import * def __getattr__(name): diff --git a/bittensor/core/chain_data.py b/bittensor/core/chain_data.py index 6d4e6103cb..b01dcc6652 100644 --- a/bittensor/core/chain_data.py +++ b/bittensor/core/chain_data.py @@ -30,11 +30,11 @@ from scalecodec.types import GenericCall from scalecodec.utils.ss58 import ss58_encode -from .settings import SS58_FORMAT from bittensor.utils import networking as net, RAOPERTAO, u16_normalized_float from bittensor.utils.balance import Balance -from bittensor.utils.registration import torch, use_torch from bittensor.utils.btlogging import logging +from bittensor.utils.registration import torch, use_torch +from .settings import SS58_FORMAT custom_rpc_type_registry = { "types": { diff --git a/bittensor/core/config.py b/bittensor/core/config.py index 61afb5f051..9bfa25f67f 100644 --- a/bittensor/core/config.py +++ b/bittensor/core/config.py @@ -20,14 +20,15 @@ # 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 argparse +import copy import os import sys -import yaml -import copy from copy import deepcopy -from munch import DefaultMunch from typing import List, Optional, Dict, Any, TypeVar, Type -import argparse + +import yaml +from munch import DefaultMunch class InvalidConfigFile(Exception): diff --git a/bittensor/core/settings.py b/bittensor/core/settings.py index 254868b7fe..cc6ab0769e 100644 --- a/bittensor/core/settings.py +++ b/bittensor/core/settings.py @@ -20,11 +20,10 @@ import os import re from pathlib import Path -from rich.console import Console -from rich.traceback import install from munch import munchify - +from rich.console import Console +from rich.traceback import install # Rich console. __console__ = Console() diff --git a/bittensor/core/subtensor.py b/bittensor/core/subtensor.py index f50d449d7c..73dfaa3341 100644 --- a/bittensor/core/subtensor.py +++ b/bittensor/core/subtensor.py @@ -37,6 +37,26 @@ from scalecodec.types import ScaleType from substrateinterface.base import QueryMapResult, SubstrateInterface +from bittensor.core import settings +from bittensor.core.axon import Axon +from bittensor.core.chain_data import ( + NeuronInfo, + PrometheusInfo, + SubnetHyperparameters, + NeuronInfoLite, + custom_rpc_type_registry, +) +from bittensor.core.config import Config +from bittensor.core.metagraph import Metagraph +from bittensor.core.types import AxonServeCallParams, PrometheusServeCallParams +from bittensor.utils import torch, format_error_message +from bittensor.utils import u16_normalized_float, networking +from bittensor.utils.balance import Balance +from bittensor.utils.btlogging import logging +from bittensor.utils.deprecated.extrinsics.commit_weights import ( + commit_weights_extrinsic, + reveal_weights_extrinsic, +) from bittensor.utils.deprecated.extrinsics.prometheus import ( prometheus_extrinsic, ) @@ -49,31 +69,10 @@ from bittensor.utils.deprecated.extrinsics.set_weights import ( set_weights_extrinsic, ) -from bittensor.utils.deprecated.extrinsics.commit_weights import ( - commit_weights_extrinsic, - reveal_weights_extrinsic, -) from bittensor.utils.deprecated.extrinsics.transfer import ( transfer_extrinsic, ) from bittensor.utils.weight_utils import generate_weight_hash -from bittensor.core import settings -from bittensor.core.axon import Axon -from bittensor.core.chain_data import ( - NeuronInfo, - PrometheusInfo, - SubnetHyperparameters, - NeuronInfoLite, - custom_rpc_type_registry, -) -from bittensor.core.config import Config -from bittensor.core.metagraph import Metagraph -from bittensor.core.types import AxonServeCallParams, PrometheusServeCallParams -from bittensor.utils import u16_normalized_float, networking -from bittensor.utils import torch, format_error_message -from bittensor.utils.balance import Balance -from bittensor.utils.btlogging import logging - KEY_NONCE: Dict[str, int] = {} diff --git a/bittensor/core/tensor.py b/bittensor/core/tensor.py index e50947e719..c3bb29edbf 100644 --- a/bittensor/core/tensor.py +++ b/bittensor/core/tensor.py @@ -15,14 +15,16 @@ # 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 numpy as np import base64 +from typing import Optional, Union, List + import msgpack import msgpack_numpy -from typing import Optional, Union, List -from bittensor.utils.registration import torch, use_torch +import numpy as np from pydantic import ConfigDict, BaseModel, Field, field_validator +from bittensor.utils.registration import torch, use_torch + class DTypes(dict): def __init__(self, *args, **kwargs): diff --git a/bittensor/core/threadpool.py b/bittensor/core/threadpool.py index 4a8b1d3022..17e5535096 100644 --- a/bittensor/core/threadpool.py +++ b/bittensor/core/threadpool.py @@ -5,23 +5,22 @@ __author__ = "Brian Quinlan (brian@sweetapp.com)" +import argparse +import itertools +import logging import os -import sys -import time import queue import random -import weakref -import logging -import argparse -import itertools +import sys import threading - -from typing import Callable +import time +import weakref from concurrent.futures import _base +from typing import Callable from bittensor.core.config import Config -from bittensor.utils.btlogging.defines import BITTENSOR_LOGGER_NAME from bittensor.core.settings import BLOCKTIME +from bittensor.utils.btlogging.defines import BITTENSOR_LOGGER_NAME # Workers are created as daemon threads. This is done to allow the interpreter # to exit when there are still idle threads in a ThreadPoolExecutor's thread diff --git a/bittensor/utils/__init__.py b/bittensor/utils/__init__.py index 3089aff707..2423b6b39f 100644 --- a/bittensor/utils/__init__.py +++ b/bittensor/utils/__init__.py @@ -27,7 +27,6 @@ from .registration import torch, use_torch from .version import version_checking, check_version, VersionCheckError - RAOPERTAO = 1e9 U16_MAX = 65535 U64_MAX = 18446744073709551615 diff --git a/bittensor/utils/btlogging/format.py b/bittensor/utils/btlogging/format.py index 131085f178..cbde7e9eb3 100644 --- a/bittensor/utils/btlogging/format.py +++ b/bittensor/utils/btlogging/format.py @@ -27,7 +27,6 @@ from colorama import init, Fore, Back, Style - init(autoreset=True) TRACE_LEVEL_NUM: int = 5 diff --git a/bittensor/utils/btlogging/loggingmachine.py b/bittensor/utils/btlogging/loggingmachine.py index adbb4e929f..b9e0408ceb 100644 --- a/bittensor/utils/btlogging/loggingmachine.py +++ b/bittensor/utils/btlogging/loggingmachine.py @@ -34,7 +34,6 @@ from statemachine import State, StateMachine from bittensor.core.config import Config - from .defines import ( BITTENSOR_LOGGER_NAME, DATE_FORMAT, diff --git a/bittensor/utils/deprecated/extrinsics/set_weights.py b/bittensor/utils/deprecated/extrinsics/set_weights.py index 15681dbb15..f62a23d444 100644 --- a/bittensor/utils/deprecated/extrinsics/set_weights.py +++ b/bittensor/utils/deprecated/extrinsics/set_weights.py @@ -23,8 +23,8 @@ from numpy.typing import NDArray from rich.prompt import Confirm -from bittensor.utils import weight_utils from bittensor.core.settings import bt_console +from bittensor.utils import weight_utils from bittensor.utils.btlogging import logging from bittensor.utils.registration import torch, use_torch diff --git a/bittensor/utils/deprecated/extrinsics/transfer.py b/bittensor/utils/deprecated/extrinsics/transfer.py index 7f7decccfe..3f6c964d9c 100644 --- a/bittensor/utils/deprecated/extrinsics/transfer.py +++ b/bittensor/utils/deprecated/extrinsics/transfer.py @@ -17,13 +17,13 @@ from typing import Union, TYPE_CHECKING +from bittensor_wallet import Wallet from rich.prompt import Confirm from bittensor.core.settings import bt_console, NETWORK_EXPLORER_MAP from bittensor.utils import get_explorer_url_for_network from bittensor.utils import is_valid_bittensor_address_or_public_key from bittensor.utils.balance import Balance -from bittensor_wallet import Wallet # For annotation purposes if TYPE_CHECKING: diff --git a/bittensor/utils/version.py b/bittensor/utils/version.py index f0ff70763b..8ffd969cfc 100644 --- a/bittensor/utils/version.py +++ b/bittensor/utils/version.py @@ -22,8 +22,7 @@ import requests from packaging.version import Version -from bittensor.core.settings import __version__ -from bittensor.core.settings import PIPADDRESS +from bittensor.core.settings import __version__, PIPADDRESS from bittensor.utils.btlogging import logging VERSION_CHECK_THRESHOLD = 86400 From 0e8b230af7688f0f373f461a06bd42358e14db29 Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 13 Aug 2024 13:14:48 -0700 Subject: [PATCH 2/3] Add the opportunity to run `python - m bittensor` --- bittensor/__main__.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 bittensor/__main__.py diff --git a/bittensor/__main__.py b/bittensor/__main__.py new file mode 100644 index 0000000000..05d664c9de --- /dev/null +++ b/bittensor/__main__.py @@ -0,0 +1,21 @@ +# The MIT License (MIT) +# Copyright © 2024 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 +# 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. + +from bittensor import __version__ + +if __name__ == "__main__": + print(f"Bittensor SDK version: {__version__}") From 7791b2d146393ccdcd1a0b5db2283b2396f76f73 Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 13 Aug 2024 13:21:18 -0700 Subject: [PATCH 3/3] replace deps link from https to ssh --- requirements/prod.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements/prod.txt b/requirements/prod.txt index 4ef4a6f5e9..854f30e3ee 100644 --- a/requirements/prod.txt +++ b/requirements/prod.txt @@ -33,4 +33,4 @@ termcolor tqdm uvicorn wheel -git+https://github.com/opentensor/btwallet.git@main#egg=bittensor-wallet +git+ssh://git@github.com/opentensor/btwallet.git@main#egg=bittensor-wallet