bittensor.utils.weight_utils#

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

Attributes#

Functions#

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 (inverse operation from convert_weights_and_uids_for_emit)

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

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_for_netuid(uids, weights, netuid, ...)

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.

Module Contents#

bittensor.utils.weight_utils.U16_MAX = 65535#
bittensor.utils.weight_utils.U32_MAX = 4294967295#
bittensor.utils.weight_utils.convert_bond_uids_and_vals_to_tensor(n, uids, bonds)#

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

Converts root weights and uids from chain representation into a np.array or torch FloatTensor (inverse operation from convert_weights_and_uids_for_emit) :param n: number of neurons on network. :type n: int :param uids: Tensor of uids as destinations for passed weights. :type uids: list[int] :param weights: Tensor of weights. :type weights: list[int] :param subnets: list of subnets on the network. :type subnets: list[int]

Returns:

Converted row weights.

Return type:

row_weights (np.float32)

Parameters:
bittensor.utils.weight_utils.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).

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

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

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

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_for_netuid(uids, weights, netuid, subtensor, metagraph=None, exclude_quantile=0)#

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]]]