Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: setting weights #266

Merged
merged 40 commits into from
Oct 13, 2024
Merged

fix: setting weights #266

merged 40 commits into from
Oct 13, 2024

Conversation

grantdfoster
Copy link
Contributor

@grantdfoster grantdfoster commented Oct 12, 2024

Description

validator/utils.py

A new utility file, utils.py, has been introduced. This file contains a function process_weights_for_netuid, which is responsible for processing weight tensors for a given subnet ID. Here's a breakdown of what this function does:

  • Purpose: The function processes weight tensors, applying constraints and normalization based on the subtensor and metagraph data. It ensures that the weights are within acceptable ranges for the network.

  • Parameters:

    • uids: Unique identifiers of the neurons.
    • weights: Weights associated with the user IDs.
    • netuid: The network UID for which weights are being processed.
    • subtensor: Subtensor instance to access blockchain data.
    • metagraph: Optional metagraph instance for additional network data.
    • exclude_quantile: Quantile threshold for excluding lower weights.
  • Functionality:

    • Fetches the latest metagraph if not provided.
    • Casts weights to floats.
    • Identifies non-zero weights and processes them.
    • Applies constraints based on network configuration parameters.
    • Excludes weights below a certain quantile threshold.
    • Returns processed weights and their corresponding UIDs.

Setting Weights in the Validator

In the BaseValidatorNeuron class, the process of setting weights has been updated to use the new utility function:

class BaseValidatorNeuron(BaseNeuron) {
    ...
    // Calculate the average reward for each uid across non-zero values.
    raw_weights = torch.nn.functional.normalize(self.scores, p=1, dim=0);

    // Process the raw weights to final_weights via subtensor limitations.
    (
        processed_weight_uids,
        processed_weights,
    ) = process_weights_for_netuid(
        uids=self.metagraph.uids,
        weights=raw_weights.to("cpu").numpy(),
        netuid=self.config.netuid,
        metagraph=self.metagraph,
    );

    // Convert to uint16 weights and uids.
    (
        uint_uids,
        uint_weights,
    ) = bt.utils.weight_utils.convert_weights_and_uids_for_emit(
        uids=processed_weight_uids, weights=processed_weights
    );

    // Set the weights on chain via our subtensor connection.
    result, msg = self.subtensor.set_weights(
        wallet=self.wallet,
        ...
    );
    ...
}
  • Normalization: The raw weights are normalized using PyTorch's normalize function.
  • Processing: The process_weights_for_netuid function is called to apply constraints and normalize the weights.
  • Conversion: The processed weights and UIDs are converted to a format suitable for emission on the blockchain.
  • Setting Weights: The final weights are set on the blockchain using the set_weights method of the subtensor.

These changes improve the robustness and accuracy of weight processing and ensure that the weights conform to network requirements before being set on the blockchain.

Signed commits

  • Yes, I signed my commits.

@grantdfoster grantdfoster enabled auto-merge (squash) October 12, 2024 16:34
@grantdfoster grantdfoster self-assigned this Oct 12, 2024
@grantdfoster grantdfoster merged commit f23f5ec into main Oct 13, 2024
3 checks passed
@grantdfoster grantdfoster deleted the fix--setting-weights branch October 13, 2024 22:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants