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 #1869

Open
thewhaleking opened this issue May 13, 2024 · 2 comments
Open

replace torch with numpy #1869

thewhaleking opened this issue May 13, 2024 · 2 comments
Assignees

Comments

@thewhaleking
Copy link
Contributor

#1777

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.
@thewhaleking
Copy link
Contributor Author

thewhaleking commented May 24, 2024

Related PRs:
#1917
#1919
#1937


#1904

@mjurbanski-reef
Copy link
Contributor

This was done and released already right? i.e. should be closed

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

No branches or pull requests

3 participants