bittensor.utils.weight_utils#
Conversion for weight between chain representation and np.array or torch.Tensor
Attributes#
Functions#
|
Converts bond and uids from chain representation into a np.array. |
|
Converts root weights and uids from chain representation into a np.array or torch FloatTensor (inverse operation from convert_weights_and_uids_for_emit) |
|
Converts weights and uids from chain representation into a np.array (inverse operation from convert_weights_and_uids_for_emit). |
|
Converts weights into integer u32 representation that sum to MAX_INT_WEIGHT. |
|
Generate a valid commit hash from the provided weights. |
|
Normalizes the tensor x so that sum(x) = 1 and the max value is not greater than the limit. |
|
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.
- 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]
- 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).
- bittensor.utils.weight_utils.convert_weights_and_uids_for_emit(uids, weights)#
Converts weights into integer u32 representation that sum to MAX_INT_WEIGHT.
- bittensor.utils.weight_utils.generate_weight_hash(address, netuid, uids, values, version_key, salt)#
Generate a valid commit hash from the provided weights.
- Parameters:
- Returns:
The generated commit hash.
- Return type:
- 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]]]