bittensor.utils.weight_utils#

Conversion for weight between chain representation and np.array or torch.Tensor

Attributes#

Functions#

convert_and_normalize_weights_and_uids(uids, weights)

Converts weights and uids to numpy arrays if they are not already.

convert_bond_uids_and_vals_to_tensor(n, uids, bonds)

Converts bond and uids from chain representation into a np.array.

convert_root_weight_uids_and_vals_to_tensor(n, uids, ...)

Converts root weights and uids from chain representation into a np.array or torch FloatTensor

convert_uids_and_weights(uids, weights)

Converts netuids and weights to numpy arrays if they are not already.

convert_weight_uids_and_vals_to_tensor(n, uids, weights)

Converts weights and uids from chain representation into a np.array (inverse operation from

convert_weights_and_uids_for_emit(uids, weights)

Converts weights into integer u32 representation that sum to MAX_INT_WEIGHT.

generate_weight_hash(address, netuid, uids, values, ...)

Generate a valid commit hash from the provided weights.

normalize_max_weight(x[, limit])

Normalizes the tensor x so that sum(x) = 1 and the max value is not greater than the limit.

process_weights(uids, weights, num_neurons, ...[, ...])

Processes weight tensors for a given weights and UID arrays and hyperparams, applying constraints

process_weights_for_netuid(uids, weights, netuid, ...)

Processes weight tensors for a given subnet id using the provided weight and UID arrays, applying constraints

Module Contents#

bittensor.utils.weight_utils.U16_MAX = 65535#
bittensor.utils.weight_utils.U32_MAX = 4294967295#
bittensor.utils.weight_utils.convert_and_normalize_weights_and_uids(uids, weights)#

Converts weights and uids to numpy arrays if they are not already.

Parameters:
  • uids (Union[NDArray[np.int64], torch.LongTensor, list]) – The uint64 uids of destination neurons.

  • weights (Union[NDArray[np.float32], torch.FloatTensor, list]) – The weights to set. These must be float s and correspond to the passed uid s.

Returns:

Bytes converted weights and uids

Return type:

weight_uids, weight_vals

bittensor.utils.weight_utils.convert_bond_uids_and_vals_to_tensor(n, uids, bonds)[source]#

Converts bond and uids from chain representation into a np.array.

Parameters:
  • n (int) – number of neurons on network.

  • uids (list[int]) – Tensor of uids as destinations for passed bonds.

  • bonds (list[int]) – Tensor of bonds.

Returns:

Converted row bonds.

Return type:

row_bonds (np.float32)

bittensor.utils.weight_utils.convert_root_weight_uids_and_vals_to_tensor(n, uids, weights, subnets)[source]#

Converts root weights and uids from chain representation into a np.array or torch FloatTensor (inverse operation from convert_weights_and_uids_for_emit)

Parameters:
  • n (int) – number of neurons on network.

  • uids (list[int]) – Tensor of uids as destinations for passed weights.

  • weights (list[int]) – Tensor of weights.

  • subnets (list[int]) – list of subnets on the network.

Returns:

Converted row weights.

Return type:

row_weights (np.float32)

bittensor.utils.weight_utils.convert_uids_and_weights(uids, weights)#

Converts netuids and weights to numpy arrays if they are not already.

Parameters:
  • uids (Union[NDArray[np.int64], list]) – The uint64 uids of destination neurons.

  • weights (Union[NDArray[np.float32], list]) – The weights to set. These must be floated.

Returns:

Bytes converted netuids and weights.

Return type:

tuple[ndarray, ndarray]

bittensor.utils.weight_utils.convert_weight_uids_and_vals_to_tensor(n, uids, weights)[source]#

Converts weights and uids from chain representation into a np.array (inverse operation from convert_weights_and_uids_for_emit).

Parameters:
  • n (int) – number of neurons on network.

  • uids (list[int]) – Tensor of uids as destinations for passed weights.

  • weights (list[int]) – Tensor of weights.

Returns:

Converted row weights.

Return type:

row_weights (np.float32 or torch.FloatTensor)

bittensor.utils.weight_utils.convert_weights_and_uids_for_emit(uids, weights)[source]#

Converts weights into integer u32 representation that sum to MAX_INT_WEIGHT.

Parameters:
  • uids (np.int64) – Tensor of uids as destinations for passed weights.

  • weights (np.float32) – Tensor of weights.

Returns:

Uids as a list. weight_vals (list[int]): Weights as a list.

Return type:

weight_uids (list[int])

bittensor.utils.weight_utils.generate_weight_hash(address, netuid, uids, values, version_key, salt)[source]#

Generate a valid commit hash from the provided weights.

Parameters:
  • address (str) – The account identifier. Wallet ss58_address.

  • netuid (int) – The network unique identifier.

  • uids (list[int]) – The list of UIDs.

  • salt (list[int]) – The salt to add to hash.

  • values (list[int]) – The list of weight values.

  • version_key (int) – The version key.

Returns:

The generated commit hash.

Return type:

str

bittensor.utils.weight_utils.normalize_max_weight(x, limit=0.1)[source]#

Normalizes the tensor x so that sum(x) = 1 and the max value is not greater than the limit. :param x: Tensor to be max_value normalized. :type x: np.float32 :param limit: float: Max value after normalization.

Returns:

Normalized x tensor.

Return type:

y (np.float32)

Parameters:
  • x (Union[numpy.typing.NDArray[numpy.float32], bittensor.utils.registration.torch.FloatTensor])

  • limit (float)

bittensor.utils.weight_utils.process_weights(uids, weights, num_neurons, min_allowed_weights, max_weight_limit, exclude_quantile=0)#

Processes weight tensors for a given weights and UID arrays and hyperparams, applying constraints and normalization based on the subtensor and metagraph data. This function can handle both NumPy arrays and PyTorch tensors.

Parameters:
  • uids (Union[NDArray[np.int64], "torch.Tensor"]) – Array of unique identifiers of the neurons.

  • weights (Union[NDArray[np.float32], "torch.Tensor"]) – Array of weights associated with the user IDs.

  • num_neurons (int) – The number of neurons in the network.

  • min_allowed_weights (Optional[int]) – Subnet hyperparam Minimum number of allowed weights.

  • max_weight_limit (Optional[float]) – Subnet hyperparam Maximum weight limit.

  • exclude_quantile (int) – Quantile threshold for excluding lower weights. Defaults to 0.

Returns:

tuple

containing the array of user IDs and the corresponding normalized weights. The data type of the return matches the type of the input weights (NumPy or PyTorch).

Return type:

Union[tuple[“torch.Tensor”, “torch.FloatTensor”], tuple[NDArray[np.int64], NDArray[np.float32]]]

bittensor.utils.weight_utils.process_weights_for_netuid(uids, weights, netuid, subtensor, metagraph=None, exclude_quantile=0)[source]#

Processes weight tensors for a given subnet id using the provided weight and UID arrays, applying constraints and normalization based on the subtensor and metagraph data. This function can handle both NumPy arrays and PyTorch tensors.

Parameters:
  • uids (Union[NDArray[np.int64], "torch.Tensor"]) – Array of unique identifiers of the neurons.

  • weights (Union[NDArray[np.float32], "torch.Tensor"]) – Array of weights associated with the user IDs.

  • netuid (int) – The network uid to process weights for.

  • subtensor (Subtensor) – Subtensor instance to access blockchain data.

  • metagraph (Optional[Metagraph]) – Metagraph instance for additional network data. If None, it is fetched from the subtensor using the netuid.

  • exclude_quantile (int) – Quantile threshold for excluding lower weights. Defaults to 0.

Returns:

tuple

containing the array of user IDs and the corresponding normalized weights. The data type of the return matches the type of the input weights (NumPy or PyTorch).

Return type:

Union[tuple[“torch.Tensor”, “torch.FloatTensor”], tuple[NDArray[np.int64], NDArray[np.float32]]]