bittensor.utils.weight_utils#

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

Attributes#

Functions#

normalize_max_weight(x[, limit])

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

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_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_bond_uids_and_vals_to_tensor(n, uids, bonds)

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

convert_weights_and_uids_for_emit(uids, weights)

Converts weights into integer u32 representation that sum to MAX_INT_WEIGHT.

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

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

Generate a valid commit hash from the provided weights.

Module Contents#

bittensor.utils.weight_utils.U32_MAX = 4294967295#
bittensor.utils.weight_utils.U16_MAX = 65535#
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.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) :param n: int:

number of neurons on network.

Parameters:
  • uids (List[int],) – Tensor of uids as destinations for passed weights.

  • weights (List[int],) – Tensor of weights.

  • n (int)

Returns:

Converted row weights.

Return type:

row_weights ( np.float32 or torch.FloatTensor )

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) :param n: int:

number of neurons on network.

Parameters:
  • 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

  • n (int)

Returns:

Converted row weights.

Return type:

row_weights ( np.float32 )

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. :param n: int:

number of neurons on network.

Parameters:
  • uids (List[int],) – Tensor of uids as destinations for passed bonds.

  • bonds (List[int],) – Tensor of bonds.

  • n (int)

Returns:

Converted row bonds.

Return type:

row_bonds ( np.float32 )

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. :param uids: Tensor of uids as destinations for passed weights. :type uids: np.int64, :param weights: Tensor of weights. :type weights: np.float32,

Returns:

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

Weights as a list.

Return type:

weight_uids (List[int])

Parameters:
  • uids (Union[numpy.typing.NDArray[numpy.int64], bittensor.utils.registration.torch.LongTensor])

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

bittensor.utils.weight_utils.process_weights_for_netuid(uids, weights, netuid, subtensor, metagraph=None, exclude_quantile=0)[source]#
Parameters:
  • uids (Union[numpy.typing.NDArray[numpy.int64], bittensor.utils.registration.torch.Tensor])

  • weights (Union[numpy.typing.NDArray[numpy.float32], bittensor.utils.registration.torch.Tensor])

  • netuid (int)

  • subtensor (bittensor.subtensor)

  • metagraph (bittensor.metagraph)

  • exclude_quantile (int)

Return type:

Union[Tuple[bittensor.utils.registration.torch.Tensor, bittensor.utils.registration.torch.FloatTensor], Tuple[numpy.typing.NDArray[numpy.int64], numpy.typing.NDArray[numpy.float32]]]

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