Skip to content

btlogging/loggingmachine.py: Fix bw compat API.#2155

Merged
thewhaleking merged 3 commits intoopentensor:stagingfrom
coldint:PRs001
Aug 2, 2024
Merged

btlogging/loggingmachine.py: Fix bw compat API.#2155
thewhaleking merged 3 commits intoopentensor:stagingfrom
coldint:PRs001

Conversation

@mvds00
Copy link

@mvds00 mvds00 commented Jul 21, 2024

The bt logging mechanism breaks regular logging in various ways, that are solved by this commit.

Bug

Logging from outside of bittensor is broken, for instance, retry logs using:

logger.warning('%s, retrying in %s seconds...', e, _delay)

and this will output the literal %s, as the args are hijacked by the attempt at bw compatibility in bittensor, because the args e and _delay would land on kwargs prefix and suffix.

Also, all logging originates quite visibly from loggingmachine.py, which, in a literal sense, is correct, but not very helpful information.

Description of the Change

  • add stacklevel=2 so that the filename of the originating call is logged (requires Python >= 3.8)
  • move *args before prefix and suffix kwargs so regular logging calls using args, such as in retry, still work as intended
  • additional commit to get rid of the " - " prefixes and suffixes that are added when prefix and/or suffix are not used
  • handles an issue in CPython < 3.11 with logging.exception()

Alternate Designs

Reworking the logging entirely is also a possibility. But this seemed to be the easy fix.

Possible Drawbacks

Any code relying on prefix and suffix also being positional arguments, is now broken. This is intentional.

Verification Process

I ran code that relies on bittensor and btlogging to verify that logging is now better.

Release Notes

  • Fixed and improved logging

stacklevel = 2
if (
platform.python_implementation() == "CPython"
and platform.sys.version_info.major == 3
Copy link
Collaborator

@basfroman basfroman Jul 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend using sys.implementation and sys.version_info for checking the Python implementation and version instead of platform. This approach is more direct, avoids extra imports, and improves performance by leveraging attributes directly available in the sys module, which is standard and efficient for such checks.

Changes:

if (sys.implementation.name == "cpython" and
    sys.version_info.major == 3 and
    sys.version_info.minor < 11):
    # do something

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Syntax is a bit different but as suggested by CI.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perfect. Also pls add unit tests related with new method and updated ones.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean tests for the logging functionality? They are not part of CI but they all pass. Of the CI tests two tests failed but they seem unrelated: "get current emissions and validate that Alice has gotten tao" and "assert updated_axon.ip == external_ip", could it be that something else is off there? I just pushed a unit test that checks the fixed logging elements.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My personal recommendation is to use the following command before sending a PR:
clear && python3 -m ruff format . && mypy --ignore-missing-imports bittensor/ --python-version=3.9 && mypy --ignore-missing-imports bittensor/ --python-version=3.10 && mypy --ignore-missing-imports bittensor/ --python-version=3.11 && flake8 bittensor/ --count

I'm surprised that your implementation affected and broke tests that were not related to this. Let's try the following:

  • merget staging to your PR (I see This branch is out-of-date with the base branch below);
  • use locally the command I indicated above;

I expect there will be no broken tests.

@heeres heeres force-pushed the PRs001 branch 3 times, most recently from 1a7f15c to 92211ae Compare July 22, 2024 22:12
@basfroman
Copy link
Collaborator

@mvds00, @heeres pls fix ruff complaint using command ruff format . in your env

@mvds00
Copy link
Author

mvds00 commented Jul 22, 2024

@mvds00, @heeres pls fix ruff complaint using command ruff format . in your env

Why? We didn't touch the files ruff is complaining about. I think this commit is the culprit:

commit f02c115673d10ca923e94308448e66102a1c11d6
Author: Benjamin Himes <benhimes@opentensor.dev>
Date:   Mon Jul 22 15:39:10 2024 +0200

...

 KEY_NONCE: Dict[str, int] = {}
 
-#######
-# Monkey patch in caching the convert_type_string method
-#######
-if hasattr(RuntimeConfiguration, "convert_type_string"):
-    original_convert_type_string = RuntimeConfiguration.convert_type_string
-
-    @functools.lru_cache(maxsize=None)
-    def convert_type_string(_, name):
-        return original_convert_type_string(name)
-
-    RuntimeConfiguration.convert_type_string = convert_type_string
-#######
 
 
 class ParamWithTypes(TypedDict):

Because the removal of the comments and code makes the newlines form three empty lines which is obviously Very Bad.

How could it have come through CI?

@basfroman
Copy link
Collaborator

@mvds00, @heeres pls fix ruff complaint using command ruff format . in your env

Why? We didn't touch the files ruff is complaining about. I think this commit is the culprit:

commit f02c115673d10ca923e94308448e66102a1c11d6
Author: Benjamin Himes <benhimes@opentensor.dev>
Date:   Mon Jul 22 15:39:10 2024 +0200

...

 KEY_NONCE: Dict[str, int] = {}
 
-#######
-# Monkey patch in caching the convert_type_string method
-#######
-if hasattr(RuntimeConfiguration, "convert_type_string"):
-    original_convert_type_string = RuntimeConfiguration.convert_type_string
-
-    @functools.lru_cache(maxsize=None)
-    def convert_type_string(_, name):
-        return original_convert_type_string(name)
-
-    RuntimeConfiguration.convert_type_string = convert_type_string
-#######
 
 
 class ParamWithTypes(TypedDict):

Because the removal of the comments and code makes the newlines form three empty lines which is obviously Very Bad.

How could it have come through CI?

I guess know what is happening. We have extra line which is the reason for ruff complaint. This PR should to fix that. After I merge it, you need to update your branch from staging and all the checkouts.

@mvds00
Copy link
Author

mvds00 commented Jul 22, 2024

I rebased against staging after the ruff issue was fixed.

@basfroman
Copy link
Collaborator

Thank you for helping me discover the issue!

@heeres heeres force-pushed the PRs001 branch 2 times, most recently from 3368510 to d1a9fd9 Compare July 23, 2024 22:52
Copy link
Collaborator

@basfroman basfroman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls return back e2e test and e2e utils.

@mvds00
Copy link
Author

mvds00 commented Jul 24, 2024

pls return back e2e test and e2e utils.

Why? The e2e test I removed refers to a function that was removed from subtensor, so it will always fail. The e2e utils contains a bug in epoch calculation, probably causing the weight setting test to fail. So without these fixes it will never pass CI.

@basfroman
Copy link
Collaborator

pls return back e2e test and e2e utils.

Why? The e2e test I removed refers to a function that was removed from subtensor, so it will always fail. The e2e utils contains a bug in epoch calculation, probably causing the weight setting test to fail. So without these fixes it will never pass CI.

Now there is a process of reorganizing the nodes on the subtensor side. This will be up and running soon for testing and the tests will be successful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are the majority of changes to this file relevant to this PR?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are needed to fix issues in CI. I encountered these issues while analyzing the possible reasons why the original PR didn't come through CI. Should I change the text of the original PR?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the general rule is "1 PR should do 1 thing". Sometimes that "1 thing" can be large, sometimes small. Are these changes needed to fix the CI only as it relates to what this PR also fixes, or are they more general? They appear to be more general, from my looking.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are more general indeed. Wasn't aware of the PR rule - I only split it at the commit level. Shall I split them into 3 or 4 PRs?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. I would probably just do two. The test_swap_hotkey.py file will fail until it is fixed on the subtensor side, but just don't worry about it for the moment.

@heeres heeres force-pushed the PRs001 branch 2 times, most recently from ffd8497 to bfe8bcd Compare July 24, 2024 18:23
thewhaleking
thewhaleking previously approved these changes Jul 24, 2024
@ibraheem-abe
Copy link
Contributor

@mvds00 Can you please update the branch with base staging? Thank you!

@mvds00
Copy link
Author

mvds00 commented Jul 27, 2024

@mvds00 Can you please update the branch with base staging? Thank you!

Just did, and automated the process towards the future.

@heeres heeres force-pushed the PRs001 branch 4 times, most recently from fa39deb to 9a5c6d7 Compare August 1, 2024 22:51
μ added 3 commits August 2, 2024 00:31
- add stacklevel=2 so that the filename of the originating call is
  logged (requires Python >= 3.8)
- move *args before prefix and suffix kwargs so regular logging calls
  using args, such as in retry, still work as intended

To elaborate on the last point, retry does the following:

    logger.warning('%s, retrying in %s seconds...', e, _delay)

and the args e and _delay would land on kwargs prefix and suffix without
this patch.

Any code relying on prefix and suffix also being positional arguments,
is now broken.

Note that logger.exception() calls through to logger.error() without
compensating for this additional internal call when determining the
calling file, on CPython < 3.11. This code works around this issue.
In case prefix and suffix are not used, messages are still prefixed and
suffixed with " - ".

This patch addresses that and only injects separators between non-empty
strings.
Add a unit test for:
- correct filename in loglines
- prefix= and suffix= kwargs
- format string functionality
@thewhaleking thewhaleking merged commit 50b11cc into opentensor:staging Aug 2, 2024
basfroman pushed a commit that referenced this pull request Aug 7, 2024
@ibraheem-abe ibraheem-abe mentioned this pull request Aug 23, 2024
ibraheem-abe added a commit that referenced this pull request Oct 1, 2024
* Make version.py bittensor import independent

* Update setup.py, replace file with the __version__

* Update settings.py

* Make axon independent of bittensor import. Fix axon tests.

* Make chain_data.py independent of bittensor import. Fix chain_data tests.

* Make dendrite.py independent of bittensor import. Fix dendrite tests.

* Make staking.py independent of bittensor import. Fix staking tests.

* Make subtensor.py independent of bittensor import. Fix subtensor tests.

* Make prometheus.py independent of bittensor import. Fix prometheus tests.

* Axon refactoring

* Move subnets.py into bittensor/utils since this module is not used within the bittensor package, but is actively used by the community.

* Make synapse.py independent of bittensor import. Fix synapse tests.

* Make tensor.py independent of bittensor import. Fix tensor tests.

* Change name `bittensor.utils.hash` -> `bittensor.utils.get_hash`

* Moved from using `import bittensor` (withing bittensor codebase) to direct imports of modules and other objects. It helped to avoid namespace conflicts.

* Add backwards compatibility for 'bittensor.api.extrinsics' as 'bittensor.extrinsics'

* Move the contents of `wallet_utils.py` to `bittensor/utils/__init__.py`, since it is used directly from utils.

* Delete `bccli` staff and related tests. Fix setup.py.

* Fix tests.

* Makes the `bittensor.utils.mock` subpackage available as `bittensor.mock` for backwards compatibility.

* Check for participation before nomination call

* Ruff formatting. Fix ruff's complaints.

* Removing obsolete, unused (community and bittensor) code

* Move backwards compatibility related content to `bittensor/utils/backwards_compatibility` subpackage

* btlogging/loggingmachine.py improvement from #2155

* Fixes after `Merge streaming fix to staging` from #2183

* Review fix.

* Remove black from dev requirements.

* black back

* Remove space from prod.txt. Remove black from dev.txt

* Add dendrite reference to backwords compatibility

* Fix the usage of env vars in default settings.

* ruff

* Change the subpackage name to `deprecated`

* Update __version__ until 8.0.0

* Move `subnets.py` to `bittensor/utils` as it is considered more utility than deprecated.

* Rename test module

* Add aliases mapping test

* ruff

* Init: Adds initial e2e tests

* Ruff

* Fixes LOCALNET_SH_PATH env_var

* Adds module description

* Adds commit-reveal back and adds e2e for liquid alpha & commit reveal

* Review suggestions implemented

* Fixes logging string

* Enhance: Switch from format() to f-strings

* ruff

* Optimize imports

* Add the opportunity to run `python - m bittensor`

* replace deps link from https to ssh

* Fixes metagraph save/load for neurons, added e2e tests (metagraph + extrinsics)

* Renamed test

* Adds missing statements

* increase subtensor.py test coverage until 80%

* ruff

* add tests

* ruff

* add tests _do_serve_prometheus

* add tests neuron_for_uid

* sort tests by group

* sort tests by group

* remove unused import

* add test for `get_neuron_for_pubkey_and_subnet`

* Add unit tests to subtensor.py

* Fix ruff + `test_do_set_weights_no_waits` description

* add tests

* ruff + add tests

* ruff

* add test

* fix unused imports

* Config refactoring

* Remove duplicated tests

* add test

* remove duplicated import

* remove unused functions

* remove unused functions + add tests

* ruff

* fix type annotation

* add tests

* Add tests and remove unused functions

* fix test

* ruff

* Add balance tests, small refactoring

* Add init test for bittensor

* ruff

* Add tests for utils.weight_utils.py

* add tests for bittensor.utils.subnets

* ruff

* update test file

* Decoupling chain_data.py to sub-package

* Fix test

* ruff

* review fix

* update `requirements/prod.txt`

* Update localnet entrypoint port

* Add substrate reconnection logic

* Fix tests + add tests

* Ruff formatter

* add docstring to the decorator

* update requirements

* update requirements

* update requirements

* cleanup __init__.py

* - move `extrinsics` from deprecated into `bittensor/core`;
- update `deprecated` sub-package to `deprecated.py` module.

* update ssh to https in requirements

* add reconnection logic for correctly closed connection

* remove unused import

* fix pagination

* Move do_* methods into the related extrinsic module (commit_weights.py).

* Move do_* methods into the related extrinsic module (bittensor/core/extrinsics/set_weights.py).

* Move do_* methods into the related extrinsic module (bittensor/core/extrinsics/transfer.py). + ruff

* Move do_* methods into the related extrinsic module (bittensor/core/extrinsics/serving.py)

* Move do_* methods into the related extrinsic module (bittensor/core/extrinsics/prometheus.py)

* bug fixes

* extrinsics refactoring

* bug fix

* ruff

* fix

* removed exit sys call for ConnectionRefusedError in _get_substrate

* remove unused import

* change the log message

* Corrected arguments order in logging methods + test

* fix integration test

* ruff

* comments fixes

* fix `bittensor/core/subtensor.py:445: error: Argument "logger" to "retry" has incompatible type "LoggingMachine"; expected "Logger | None"  [arg-type]`

* integrate `bt_decode` into BTSDK

* fix logger linter checker

* Reverts logging enhancement

* remove unused code and tests

* remove unused imports

* modification of error formatter, moved the submit_extrinsic call to utils.py, fixed the subtensor.py regarding extrinsics calls, fixed tests

* ruff

* Improved logic for concatenating message, prefix, and suffix in bittensor logging + test

* fix + ruff

* remove unused import

* improve btlogging concat test

* docs AxonInfo

* docs DelegateInfo

* DelegateInfoLite

* IPInfo

* NeuronInfo

* NeuronInfo + NeuronInfoLite

* PrometheusInfo

* ProposalVoteData

* ScheduledColdkeySwapInfo

* ProposalVoteData

* StakeInfo

* StakeInfo + SubnetHyperparameters

* bittensor/core/chain_data/utils.py

* bittensor/core/axon.py

* bittensor/core/axon.py

* bittensor/core/config.py

* bittensor/core/dendrite.py

* bittensor/core/metagraph.py

* bittensor/core/stream.py

* bittensor/core/subtensor.py

* bittensor/core/synapse.py

* bittensor/core/tensor.py

* replace Tuple, Dict, List, Set to python native types

* bittensor/__init__.py

* bittensor/utils/weight_utils.py

* bittensor/utils/version.py

* bittensor/utils/btlogging

* fix

* remove wrong `optional` from docstring's annotations

* apply camel case rule

* bittensor/core/extrinsics/commit_weights.py

* bittensor/core/extrinsics/prometheus.py

* bittensor/core/extrinsics/serving.py

* axon improvement (add annotation)

* README updates for SDK

* bittensor/core/extrinsics/set_weights.py

* bittensor/core/extrinsics/transfer.py

* fix

* weight_utils.py

* check up and fix

* fix Axon Signature

* fix axon

* delete unused import

* update GPG

* remove unused code, remove old tests, add new tests

* fix

* temp commented line

* fix review comments

* fix review comments

* Adding TOC and working in review comments.

* Adding installation options

* Formatting adjustments for html display

* Ruff formatted

* Example reformatted

* update metagraph

* update dendrite.py

* Update README.md

Co-authored-by: garrett-opentensor <156717492+garrett-opentensor@users.noreply.github.com>

* Update README.md

Co-authored-by: garrett-opentensor <156717492+garrett-opentensor@users.noreply.github.com>

* Update README.md

* Update axon.py

* fix tests/unit_tests/test_axon.py

* Update README.md

* set `WARNING` level ad default logging level

* fix test

* implement warning version, do info the same as default level

* fix

* ruff

* cleanup

* Change warning to debug for local connection information

* update deps

* remove unused file

---------

Co-authored-by: ibraheem-opentensor <165814940+ibraheem-opentensor@users.noreply.github.com>
Co-authored-by: ibraheem-opentensor <ibraheem@opentensor.dev>
Co-authored-by: Watchmaker <rajk@opentensor.dev>
Co-authored-by: garrett-opentensor <156717492+garrett-opentensor@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants