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

replace torch with numpy #1777

Merged

Conversation

andreea-popescu-reef
Copy link
Contributor

Description of the Change

Replaced torch with numpy through the codebase, except places where torch is needed.

By default pip install bittensor will not install torch.
Torch is now an optional dependency which can be installed alongside bittensor by specifying the [torch] extra flag.

Quantitative Performance Benefits

pip install bittensor[torch] (with cuda) takes up 5.5GB, while pip install bittensor (numpy only) takes up 680M and it's faster (for instance from 62s to 24s on my machine with all wheels cached)

Caveats

Commands which require torch will print an error message prompting the user to install torch if the package is missing.

CLI users upgrading bittensor and having torch available should not be affected - all commands will work the same.
Users installing bittensor from scratch and attempting to register will need the torch package. btcli s pow_register and btcli w faucet commands will explicitly prompt the user to install torch.

Library users would have to deal with the changes from torch to numpy (primary affecting the metagraph data). The migration is straightforward, see common replacements:

import torch                -> import numpy as np

torch.LongTensor            -> np.int64
torch.FloatTensor           -> np.float32
torch.Tensor                -> np.ndarray
torch.long                  -> np.int64
torch.float32               -> np.float32

torch.zeros(n)              -> np.zeros(n)
torch.nn.Parameter(torch.ones(x, y), requires_grad=False) -> np.ones((x, y))

torch.rand(n)               -> np.random.rand(n)
torch.randint(0, n, (x,))   -> np.random.randint(0, n, (x,))  
torch.where(x > 0, x, y)    -> numpy.where(x > 0, x, y)
torch.equal(x, y)           -> np.array_equal(x, y)
x.squeeze(dim=1)            -> x.squeeze(axis=1)
torch.quantile(x, y, dim=1, keepdim=True) -> np.quantile(x, y, axis=1, keepdims=True)

metagraph.W.size()          -> metagraph.W.shape

Verification Process

Installed it with and without [torch] in fresh venvs and manually tested btcli commands.
Tested on compute horde staging miner and validator.

Applicable Issues

Release Notes

  • BREAKING CHANGE: Metagraph attributes now return numpy.ndarrays instead of torch.tensors.
  • pip install bittensor will not install torch by default - this can be done with pip install bittensor[torch]
  • btcli s pow_register and btcli w faucet commands will fail if torch is not installed.

@mjurbanski-reef mjurbanski-reef mentioned this pull request Apr 10, 2024
4 tasks
@sepehr-opentensor sepehr-opentensor merged commit 39dda5b into opentensor:merge_cuda Apr 11, 2024
12 checks passed
@gus-opentensor gus-opentensor mentioned this pull request May 17, 2024
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.

None yet

2 participants