Bittensor Component Breakdown

Bittensor consists of several components that allow neurons to communicate with peers (other neurons) and send/receive machine knowledge. This section goes through each component and describes its role in the neuron, its inputs , its outputs, and its context within the big picture of the Bittensor network.

These components can all be found in bittensor/bittensor.

Axon class

Analogous to the Axon terminal in a biological neuron cell, the axon class is responsible for deploying a Synapse instance and receiving machine knowledge from other synapses in the network (also called remote synapses, we will be using these terms interchangeably).

The main job of the Axon class is to process forward and backward requests through a set of local synapses. This requests come from remote neurons.

__init__ (self, config: bittensor.Config)

  • Initializes the configuration according to arguments passed to main.py in the Neuron.
  • Initializes gRPC server objects using the config.axon_port passed to it.
  • Sets up local synapses and the serving Axon thread.

__del__ (self)

  • Stops the entire Axon terminal server and deletes local synapses.

_stop (self)

  • Stop the Axon terminal and the gRPC server.

_serve (self)

  • Start the gRPC server.

serve (self)

  • Add a synapse to the set of synapses presently being served by this Axon terminal.
  • Creates a new bittensor_pb2.Synapse proto and adds it to the list of local synapses.

Forward (self, request, context)

The Forward call takes the TensorMessage request and looks up the local synapse that this request is targetting. In this context, the local synapse is a synapse belonging to this neuron. Once it has found the synapse, it deserializes the Tensor in the TensorMessage and makes a forward call on the target synapse. It then serializes the local synapse’s response (a vector of the output of its last layer) and returns it.

Inputs:
  • TensorMessage request coming from a remote synapse.
  • congrpc.ServicerContext context : coming from a remote neuron.
Outputs:
  • Serialized response of the local synapse.

Dendrite class

Also analogous to the Dendrite in a biological neuron cell, the Dendrite class is responsible for sending dataset batches across the network to remote synapses.

forward_text, forward_image, and forward_tensor

As the function name implies, each of these functions is responsible for forwarding a different modality across the gRPC network to peer neurons.

Each function takes as input a list of peer synapses and a list of torch tensors that need to be sent to the peer. The list of peer synapses can be retrieved with a call to metagraph.get_synapses() as described in Bittensor Architecture.

def forward_text(self, synapses: List[bittensor_pb2.Synapse], x: List[ List[str] ]) -> List[torch.Tensor]:
    """ forward tensor processes """
    return self.forward(synapses, x, bittensor_pb2.Modality.TEXT)

def forward_image(self, synapses: List[bittensor_pb2.Synapse], x: List[ torch.Tensor ]) -> List[torch.Tensor]:
    """ forward tensor processes """
    return self.forward(synapses, x, bittensor_pb2.Modality.IMAGE)

def forward_tensor(self, synapses: List[bittensor_pb2.Synapse], x: List[ torch.Tensor ]) -> List[torch.Tensor]:
    """ forward tensor processes """
    return self.forward(synapses, x, bittensor_pb2.Modality.TENSOR)

forward (self, synapses, x, mode)

The main functionality behind the Dendrite component of Bittensor, the forward() call forwards mini-batches of the dataset by invoking _RemoteModuleCall out to the target remote synapses and returns their results.

Inputs:
  • List[bittensor_pb2.Synapse] synapses : A list of remote synapses running in remote neurons.
  • List[ object ] x : A list of objects to be sent through gRPC to the target remote synapses.
  • bittensor_pb2.Modality mode : Modality of the data being sent (i.e. Text, Image, etc.)
Outputs:
  • The response of each remote synapse. Given that each minibatch runs through the remote synapse’s local model, the output back is a vector output of that remote synapse’s forward() call.

RemoteSynapse class

This class bundles a grpc connection to a remote neuron as a standard auto-grad torch.nn.Module. Making it possible to send data to that neuron and even calculate its partial derivative on a backwards pass.

__init__ (self, synapses, config)

This function sets up the synapse’s remote address and a gRPC channel for communication.

Inputs:
  • bittensor_pb2.Synapse synapse: The target remote synapse to which we are bundling a gRPC connection.
  • bittensor.Config config : Run configuration of the local neuron.
Outputs:
  • None

forward (self, object, mode)

Makes a _RemoteModuleCall to send the batch of inputs over to the remote synapse.

Inputs:
  • Object inputs : The mini-batch of data to be sent to the remote synapse. Note that it is of type object as

    it can be of type string or image data. More data types will be integrated in later versions.

  • mode : The modality of the data being sent.

Outputs
  • torch.Tensor outputs : A Tensor of outputs from the remote synapses.

_RemoteModuleCall class

This class is responsible for invoking the actual stub to call remote synapses. As an Autograd differentiable function, the forward call passes tensors, while the backward pass computes the gradients to get the partial derivative of the remote synapses’ output.

forward (ctx, RemoteSynapse, dummy, inputs, mode)

This call passes tensors to the remote synapse. Note that it also takes a dummy tensor to prevent torch from excluding the synapse from backward if no other inputs require grad. This method first serializes the inputs, sends them across the wire, then deserializes the outputs of the remote synapses.

Inputs:
  • ctx : Object that can be used to stash information for backward computation.
  • RemoteSynapse caller : The calling synapse of type RemoteSynapse.
  • torch.Tensor dummy : A dummy tensor to trigger autograd in the remote synapse.
  • List[ object ] x : A list of objects to be sent through gRPC to the target remote synapses.
  • bittensor_pb2.Modality mode : Modality of the data being sent (i.e. Text, Image, etc.)
Outputs:
  • The response of each remote synapse. Given that each minibatch runs through the remote synapse’s local model, the output back is a vector output of that remote synapse’s forward() call.

backward (ctx, grads)

The backward function pass computes the gradients of the remote synapse’s outputs to get their partial derivative (that is, the gradients). However, at the moment this function does not apply the gradients to the Synapse. Note that if the modality is text, then the derivative would be 0 and thus it returns a none tuple. However if the modality is Image, Tensor, or otherwise, then the gradients of the remote synapses are returned.

Inputs:
  • ctx : Object that can be used to stash information for backward computation.
  • torch.Tensor grads : gradients of the local neuron.
Outputs:
  • The response of each remote synapse.

Metagraph class

The Metagraph class is responsible for allowing Neurons to connect to each other and – later – to the blockchain, however this is still under development.

__init__(self, config)

Initializes a new metagraph proof-of-work object cache, initializes gRPC metagraph servicer, and loads config or default configs.

start (self)

Starts two threads:

  1. The gossip thread that will communicate with and find potential peers on the network. Target method _update().
  2. The gRPC server thread.

update(self)

This keeps the metagraph record of the local neuron up to date by discovering peers using gossip protocol and removing stale peers.

stop (self)

Stops the gossip thread. This is typically called at the end of Bittensor’s execution cycle as part of cleanup.

do_gossip (self)

Sends a gossip query to a random peer and records their response into the metagraph via a sink() call.

do_clean (self, ttl)

Checks whether a peer hasn’t sent a heart beat signal in a given time (i.e. timed out). If it hasn’t, then the neuron deletes it from its list of peers.

Inputs:
  • Integer ttl: Time to live for a given target neuron (defaults to 5 minutes).
Outputs:
  • None

_sink (self, request)

Records a peer’s gossip request to the local neurons list of synapses and peers, and sets its latest heartbeat time.

get_peers(self, n)

Returns the number of active peer endpoints in the network.

Inputs:
  • Integer n : maximum number of peers to return. This defaults to 10 peers.
Outputs:
  • None

get_synapses(self, n)

Returns the number of active synapses in the network.

Inputs:
  • Integer n : maximum number of synapses to return. This defaults to 1000 synapses.
Outputs:
  • None

getweights(self, synapses)

Retrieves the weights for a list of synapses in the network.

Inputs:
  • List[bittensor_pb2.Synapse] synapses : List of remote synapse endpoints.
Outputs:
  • List(Float) results : List of remote synapse weights.

setweights(self, synapses)

Sets the weights for a list of synapses in the network.

Inputs:
  • List[bittensor_pb2.Synapse] synapses : List of remote synapse endpoints.
  • torch.Tensor weights : Weights to set to each synapse.
Outputs:
  • None

subscribe(self, synapse)

Subscribes a synapse class object to the neuron’s metagraph.

Inputs:
  • bittensor_pb2.Synapse synapses : Synapse to subscribe to metagraph.
Outputs:
  • None

Synapse class

This is the superclass of every custom deep learning model that users will write on Bittensor. More specifically, each deep learning model (read: neuron, or miner) will extend this class to gain the capabilities of communicating with other bittensor neurons.

__init__ (self)

Initializes the Synapse public key and identifies whether the current machine is cuda-capable (i.e. can we train on the GPU?).

forward_text (self), forward_image (self), and forward_tensor (self)

These methods are not implemented in this class, but are defined as contractual methods where one (or all) of them should be implemented by the user and their model they are building (See examples like MNIST).

Inputs:
  • torch.Tensor inputs : The batch of data to be sent through the forward() call of the custom model.

call_forward (self, inputs, modality)

Apply forward pass to the bittensor.synapse given inputs and modality. The modality passed to this function determines whether it calls forward_text (self), forward_image (self), or forward_tensor (self). Therefore, one of them must be implemented in the subclass (custom) model.

Inputs:
  • Object inputs : The batch of data to be sent through the forward() call of the custom model.
  • modality : The modality of the data being sent.
Outputs:
  • torch.Tensor Outputs : The output of the forward() call of the model.

call_backward (self, inputs, grads)

Apply a backward pass to the synapse given grads and inputs. Presently, this only returns a tensor of zeros as it is not being used.

Inputs:
  • Object inputs : The batch of data to be sent through the forward() call of the custom model.
  • grads : Gradients of the remote synapses.
Outputs:
  • torch.zeros((1,1)) : A tensor of zeros.