-
Notifications
You must be signed in to change notification settings - Fork 301
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
fix PoW and other functions not working with USE_TORCH=0 despite torch being available #1917
Merged
thewhaleking
merged 4 commits into
opentensor:staging
from
backend-developers-ltd:improve_no_torch
May 22, 2024
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
bb758b3
fix PoW and other functions not working with USE_TORCH=0 despite torc…
mjurbanski-reef 730d24f
make legacy_torch_api_compat docstring more explicit
mjurbanski-reef 542eba8
fix test_normalize_with_max_weight__legacy_torch_api_compat flakeines…
mjurbanski-reef 03303de
Merge remote-tracking branch 'upstream/staging' into improve_no_torch
mjurbanski-reef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
# To use Torch functionality in bittensor, you must set the USE_TORCH flag to 1: | ||
USE_TORCH=1 | ||
|
||
# If set to 0 (or anything else), you will use the numpy functions. | ||
# This is generally what you want unless you have a specific reason for using torch | ||
# such as POW registration or legacy interoperability. | ||
# To use legacy Torch-based of bittensor, you must set USE_TORCH=1 | ||
USE_TORCH=0 | ||
# If set to 0 (or anything else than 1), it will use current, numpy-based, bittensor interface. | ||
# This is generally what you want unless you want legacy interoperability. | ||
# Please note that the legacy interface is deprecated, and is not tested nearly as much. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pytest | ||
|
||
from bittensor.utils.registration import LazyLoadedTorch | ||
|
||
|
||
class MockBittensorLogging: | ||
def __init__(self): | ||
self.messages = [] | ||
|
||
def error(self, message): | ||
self.messages.append(message) | ||
|
||
|
||
@pytest.fixture | ||
def mock_bittensor_logging(monkeypatch): | ||
mock_logger = MockBittensorLogging() | ||
monkeypatch.setattr("bittensor.btlogging", mock_logger) | ||
return mock_logger | ||
|
||
|
||
def test_lazy_loaded_torch__torch_installed(monkeypatch, mock_bittensor_logging): | ||
import torch | ||
|
||
lazy_torch = LazyLoadedTorch() | ||
|
||
assert bool(torch) is True | ||
|
||
assert lazy_torch.nn is torch.nn | ||
with pytest.raises(AttributeError): | ||
lazy_torch.no_such_thing | ||
|
||
|
||
def test_lazy_loaded_torch__no_torch(monkeypatch, mock_bittensor_logging): | ||
monkeypatch.setattr("bittensor.utils.registration._get_real_torch", lambda: None) | ||
|
||
torch = LazyLoadedTorch() | ||
|
||
assert not torch | ||
|
||
with pytest.raises(ImportError): | ||
torch.some_attribute | ||
|
||
# Check if the error message is logged correctly | ||
assert len(mock_bittensor_logging.messages) == 1 | ||
assert "This command requires torch." in mock_bittensor_logging.messages[0] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this approach of just having one implementation of a function and then translating only input and output will be much easier to maintain, but I leave it to others if they want to use it in more places or if they want to keep using
if use_torch()
all over the place