Skip to content

replace torch with numpy #1869

@thewhaleking

Description

@thewhaleking

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

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions