Skip to main content

bittensor

Project description

Bittensor

Discord Chat PyPI version License: MIT


Internet-scale Neural Networks

DiscordDocsNetworkResearchCode

This repository contains Bittensor's python API which can be used to 1) Query the Bittensor network as a client 2) Run and build Bittensor miners & validators for mining TAO, 3) Pull network state information and 4) Manage TAO wallets, balances, transfers etc.

Bittensor is a mining network (like Bitcoin) with inbaked incentives which are designed to drive miners to provide value; which, in our network, is achieved by hosting trained or training machine learning models, which can be queried by clients seeking inference over inputs (i.e. text-generation, or numerical embeddings from a large foundation model like GPT-NeoX-20B).

The use of token based incentives is by design, built-in to drive the network's size and as a means of distributing the value generated by the network directly to the individuals producing that value without intermediary. The network is open to those who participate and no individual or group has full power of what it learns, who can profit from it, or access it.

To learn more about Bittensor read our [paper].(https://drive.google.com/file/d/1VnsobL6lIAAqcA1_Tbm8AYIQscfJV4KU/view).

1. Documentation

https://docs.bittensor.com/

2. Install

Three ways to install Bittensor

  1. Through the installer:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/opentensor/bittensor/master/scripts/install.sh)"
  1. With pip:
$ pip3 install bittensor
  1. From source:
$ git clone https://github.com/opentensor/bittensor.git
$ python3 -m pip install -e bittensor/

3. Using Bittensor

The following examples showcase how to use the Bittensor API for 3 separate purposes.

3.1. Client

Querying the network for generations.

import bittensor
wallet = bittensor.wallet().create_if_non_existent()
graph = bittensor.metagraph().sync()
print ( bittensor.dendrite( wallet = wallet ).generate
        ( 
            endpoints = graph.endpoints[graph.incentive.sort()[1][-1]],  // The highest ranked peer.
            prompt = "The quick brown fox jumped over the lazy dog", 
            num_to_generate = 20
        )
)

Querying the network for representations.

import bittensor
wallet = bittensor.wallet().create_if_non_existent()
graph = bittensor.metagraph().sync()
print ( bittensor.dendrite( wallet = wallet ).text_last_hidden_state
        (
            endpoints = graph.endpoints[graph.incentive.sort()[1][-1]],  // The highest ranked peer.
            inputs = "The quick brown fox jumped over the lazy dog"
        )
)
...
// Apply model. 
...
loss.backward() // Accumulate gradients on endpoints.

3.2. Server

Serving a custom model.

import bittensor
import torch
from transformers import GPT2Model, GPT2Config

model = GPT2Model( GPT2Config(vocab_size = bittensor.__vocab_size__, n_embd = bittensor.__network_dim__ , n_head = 8))
optimizer = torch.optim.SGD( [ {"params": model.parameters()} ], lr = 0.01 )

def forward_text( pubkey, inputs_x ):
    return model( inputs_x )
  
def backward_text( pubkey, inputs_x, grads_dy ):
    with torch.enable_grad():
        outputs_y = model( inputs_x.to(device) ).last_hidden_state
        torch.autograd.backward (
            tensors = [ outputs_y.to(device) ],
            grad_tensors = [ grads_dy.to(device) ]
        )
        optimizer.step()
        optimizer.zero_grad() 

wallet = bittensor.wallet().create().register()
axon = bittensor.axon (
    wallet = wallet,
    forward_text = forward_text,
    backward_text = backward_text
).start().serve()

3.3. Validator

Validating models by setting weights.

import bittensor
import torch

graph = bittensor.metagraph().sync()
dataset = bittensor.dataset()
chain_weights = torch.ones( [graph.n.item()], dtype = torch.float32 )

for batch in dataset.dataloader( 10 ):
    ...
    // Train chain_weights.
    ...
bittensor.subtensor().set_weights (
    weights = chain_weights,
    uids = graph.uids,
    wait_for_inclusion = True,
    wallet = bittensor.wallet(),
)

4. Features

4.1. CLI

Creating a new wallet.

$ btcli new_coldkey
$ btcli new_hotkey

Listing your wallets

$ btcli list

Registering a wallet

$ btcli register

Running a miner

$ btcli run

Checking balances

$ btcli overview

Checking the incentive mechanism.

$ btcli metagraph

Transfering funds

$ btcli transfer

Staking/Unstaking from a hotkey

$ btcli stake
$ btcli unstake

4.2. Selecting the network to join

There are two open Bittensor networks: staging (Nobunaga) and main (Nakamoto, Local).

  • Nobunaga (staging)
  • Nakamoto (main)
  • Local (localhost, mirrors nakamoto)
$ export NETWORK=local 
$ python (..) --subtensor.network $NETWORK
or
>> btcli run --subtensor.network $NETWORK

4.3. Running a template miner

The following command will run Bittensor's template miner

$ cd bittensor
$ python ./bittensor/_neuron/text/template_miner/main.py

or

>> import bittensor
>> bittensor.neurons.text.template_miner.neuron().run()

OR with customized settings

$ cd bittensor
$ python3 ./bittensor/_neuron/text/template_miner/main.py --wallet.name <WALLET NAME> --wallet.hotkey <HOTKEY NAME>

For the full list of settings, please run

$ python3 ~/.bittensor/bittensor/bittensor/_neuron/neurons/text/template_miner/main.py --help

4.4. Running a template server

The template server follows a similar structure as the template miner.

$ cd bittensor
$ python3 ./bittensor/_neuron/text/core_server/main.py --wallet.name <WALLET NAME> --wallet.hotkey <HOTKEY NAME>

or

>> import bittensor
>> bittensor.neurons.text.core_server.neuron().run()

For the full list of settings, please run

$ cd bittensor
$ python3 ./bittensor/_neuron/text/core_server/main.py --help

4.5. Serving an endpoint on the network

Endpoints are served to the bittensor network through the axon. The axon is instantiated via a wallet which holds an account on the Bittensor network.

import bittensor

wallet = bittensor.wallet().create().register()
axon = bittensor.axon (
    wallet = wallet,
    forward_text = forward_text,
    backward_text = backward_text
).start().serve()

4.6. Syncing with the chain/ Finding the ranks/stake/uids of other nodes

Information from the chain is collected/formated by the metagraph.

btcli metagraph

and

import bittensor

meta = bittensor.metagraph()
meta.sync()

# --- uid ---
print(meta.uids)

# --- hotkeys ---
print(meta.hotkeys)

# --- ranks ---
print(meta.R)

# --- stake ---
print(meta.S)

4.7. Finding and creating the endpoints for other nodes in the network

import bittensor

meta = bittensor.metagraph()
meta.sync()

### Address for the node uid 0
endpoint_as_tensor = meta.endpoints[0]
endpoint_as_object = meta.endpoint_objs[0]

4.8. Querying others in the network

import bittensor

meta = bittensor.metagraph()
meta.sync()

### Address for the node uid 0
endpoint_0 = meta.endpoints[0]

### Creating the wallet, and dendrite
wallet = bittensor.wallet().create().register()
den = bittensor.dendrite(wallet = wallet)
representations, _, _ = den.forward_text (
    endpoints = endpoint_0,
    inputs = "Hello World"
)

5. Release

The release manager should follow the instructions of the RELEASE_GUIDELINES.md document.

6. License

The MIT License (MIT) Copyright © 2021 Yuma Rao

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

7. Acknowledgments

learning-at-home/hivemind

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bittensor-3.6.3.tar.gz (269.7 kB view details)

Uploaded Source

Built Distribution

bittensor-3.6.3-py3-none-any.whl (348.4 kB view details)

Uploaded Python 3

File details

Details for the file bittensor-3.6.3.tar.gz.

File metadata

  • Download URL: bittensor-3.6.3.tar.gz
  • Upload date:
  • Size: 269.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for bittensor-3.6.3.tar.gz
Algorithm Hash digest
SHA256 860d9a110fe32abd8987a38ad956993dfaaed21491b3a00fae10cbfe9bbf5252
MD5 d156119f90bb8604619b33a46dd5e114
BLAKE2b-256 79754207a01a27d7161faad0ec7cbb6a025e895bdc928e82e7ce4ed748ed8965

See more details on using hashes here.

File details

Details for the file bittensor-3.6.3-py3-none-any.whl.

File metadata

  • Download URL: bittensor-3.6.3-py3-none-any.whl
  • Upload date:
  • Size: 348.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.10

File hashes

Hashes for bittensor-3.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 38c4b928c5f8162e49bd68bed6794dc54cf8e1b0bc62a62c843c9521f3f21a4e
MD5 ac60985509f52fb4199ee46080319186
BLAKE2b-256 4299eba9b9f6628d46a977cd0cb3d8e061250509c763032664ab042b55acc353

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page