From 140c947a5901e92928c6ddf4497603693f153430 Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 10 Jul 2023 14:07:45 -0700 Subject: [PATCH 01/18] self device --- openvalidators/reward/reward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openvalidators/reward/reward.py b/openvalidators/reward/reward.py index 537d94b..6a5b968 100644 --- a/openvalidators/reward/reward.py +++ b/openvalidators/reward/reward.py @@ -76,7 +76,7 @@ def normalize_rewards( self, rewards: torch.FloatTensor ) -> torch.FloatTensor: self.count = min(self.count_limit, self.count + new_count) # Standardize the rewards using the updated mean and variance. - rewards = rewards - self.mean + rewards = rewards - self.mean.to(self.device) if self.var > 0: rewards /= torch.sqrt(self.var) # Scale the standardized rewards to the range [0, 1] using the error function as a cumulative distribution function (CDF). From 3b7b500266b5c770f36da8128fad45afe9f2d935 Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 10 Jul 2023 14:13:25 -0700 Subject: [PATCH 02/18] cpu and detached --- openvalidators/reward/reward.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/openvalidators/reward/reward.py b/openvalidators/reward/reward.py index 6a5b968..f54eaef 100644 --- a/openvalidators/reward/reward.py +++ b/openvalidators/reward/reward.py @@ -52,6 +52,8 @@ def normalize_rewards( self, rewards: torch.FloatTensor ) -> torch.FloatTensor: - It standardizes the reward values using the updated mean and variance. - It then scales the standardized values to the 0-1 range using the error function (erf) as a CDF. """ + rewards = rewards.detach().cpu() + # Get the number of rewards (successful responses). new_count = rewards.numel() @@ -76,7 +78,7 @@ def normalize_rewards( self, rewards: torch.FloatTensor ) -> torch.FloatTensor: self.count = min(self.count_limit, self.count + new_count) # Standardize the rewards using the updated mean and variance. - rewards = rewards - self.mean.to(self.device) + rewards = rewards - self.mean if self.var > 0: rewards /= torch.sqrt(self.var) # Scale the standardized rewards to the range [0, 1] using the error function as a cumulative distribution function (CDF). From 60194b0dfcc2995c39419394e09ce2bb3713d63a Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 10 Jul 2023 14:29:31 -0700 Subject: [PATCH 03/18] cpu --- openvalidators/forward.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openvalidators/forward.py b/openvalidators/forward.py index 9fa8397..a05bd09 100644 --- a/openvalidators/forward.py +++ b/openvalidators/forward.py @@ -117,7 +117,7 @@ async def run_step(self, prompt: str, k: int, timeout: float, name: str, exclude "block": ttl_get_block(self), "step_length": time.time() - start_time, "prompt": prompt, - "uids": uids.tolist(), + "uids": uids.cpu().tolist(), "completions": completions, "completion_times": completion_times, "rewards": rewards.tolist(), From affc74322d83e29636c3564d487fe3d100541094 Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 10 Jul 2023 14:52:05 -0700 Subject: [PATCH 04/18] set trace --- openvalidators/forward.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/openvalidators/forward.py b/openvalidators/forward.py index a05bd09..3d32787 100644 --- a/openvalidators/forward.py +++ b/openvalidators/forward.py @@ -110,14 +110,15 @@ async def run_step(self, prompt: str, k: int, timeout: float, name: str, exclude self.moving_averaged_scores: torch.FloatTensor = alpha * scattered_rewards + (1 - alpha) * self.moving_averaged_scores.to( self.device ) - + import pdb + pdb.set_trace() # Log the step event. event.update( { "block": ttl_get_block(self), "step_length": time.time() - start_time, "prompt": prompt, - "uids": uids.cpu().tolist(), + "uids": uids.tolist(), "completions": completions, "completion_times": completion_times, "rewards": rewards.tolist(), From 733c13f5d6bd1ea0762ee096cae3478ce412cabe Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 10 Jul 2023 15:11:17 -0700 Subject: [PATCH 05/18] remove pdb --- openvalidators/forward.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/openvalidators/forward.py b/openvalidators/forward.py index 3d32787..9fa8397 100644 --- a/openvalidators/forward.py +++ b/openvalidators/forward.py @@ -110,8 +110,7 @@ async def run_step(self, prompt: str, k: int, timeout: float, name: str, exclude self.moving_averaged_scores: torch.FloatTensor = alpha * scattered_rewards + (1 - alpha) * self.moving_averaged_scores.to( self.device ) - import pdb - pdb.set_trace() + # Log the step event. event.update( { From 1143a3493cdc092706278c192e3b4a222d077d67 Mon Sep 17 00:00:00 2001 From: ifrit98 Date: Mon, 10 Jul 2023 22:46:25 +0000 Subject: [PATCH 06/18] pull num_uids from subtensor --- openvalidators/gating.py | 3 +-- openvalidators/neuron.py | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/openvalidators/gating.py b/openvalidators/gating.py index 482ad0e..54fe341 100644 --- a/openvalidators/gating.py +++ b/openvalidators/gating.py @@ -52,8 +52,7 @@ def add_args(cls, parser: argparse.ArgumentParser): parser.add_argument( "--gating.num_uids", type=int, - default=1024, - help="Number of uids to gate on", + help="Number of uids to gate on. Default is pulled from subtensor directly", ) parser.add_argument( "--gating.learning_rate", diff --git a/openvalidators/neuron.py b/openvalidators/neuron.py index 227d2c8..5e7b095 100644 --- a/openvalidators/neuron.py +++ b/openvalidators/neuron.py @@ -108,6 +108,8 @@ def __init__(self): # Init the gating model which learns which miners to select for each query. bt.logging.debug("loading", "gating_model") + if not self.config.gating.num_uids: + self.config.gating.num_uids = self.subtensor.query_subtensor("MaxAllowedUids", params=[self.config.netuid]) if self.config.neuron.mock_gating_model: self.gating_model = MockGatingModel(self.metagraph.n.item()) elif self.config.neuron.use_custom_gating_model: From 6ea89870c113f12763fa5df07a209a85186a3c3b Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 10 Jul 2023 16:20:57 -0700 Subject: [PATCH 07/18] num_uids --- openvalidators/gating.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openvalidators/gating.py b/openvalidators/gating.py index 482ad0e..907e903 100644 --- a/openvalidators/gating.py +++ b/openvalidators/gating.py @@ -137,7 +137,7 @@ def __init__( config = GatingModel.config() if model_name is not None: config.gating.model_name = model_name - config.gating.num_uids = num_uids if num_uids is not None else metagraph.n + config.gating.num_uids = num_uids if num_uids is not None else config.gating.num_uids self.config = config self.num_uids = config.gating.num_uids self.device = torch.device(self.config.neuron.device) From 68a756b9c1ef1ba3c9b1f53d991ae3b0e4dcc2d5 Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 10 Jul 2023 17:04:13 -0700 Subject: [PATCH 08/18] rewards --- openvalidators/reward/reward.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/openvalidators/reward/reward.py b/openvalidators/reward/reward.py index f54eaef..4400e7a 100644 --- a/openvalidators/reward/reward.py +++ b/openvalidators/reward/reward.py @@ -51,9 +51,7 @@ def normalize_rewards( self, rewards: torch.FloatTensor ) -> torch.FloatTensor: - This function uses Welford's online algorithm to update the mean and variance. - It standardizes the reward values using the updated mean and variance. - It then scales the standardized values to the 0-1 range using the error function (erf) as a CDF. - """ - rewards = rewards.detach().cpu() - + """ # Get the number of rewards (successful responses). new_count = rewards.numel() From e2be6e01287f5a2719cbf07d18ab04be85317d07 Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 10 Jul 2023 17:16:37 -0700 Subject: [PATCH 09/18] detach rewards --- openvalidators/reward/reward.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/openvalidators/reward/reward.py b/openvalidators/reward/reward.py index 4400e7a..54ef90d 100644 --- a/openvalidators/reward/reward.py +++ b/openvalidators/reward/reward.py @@ -52,6 +52,8 @@ def normalize_rewards( self, rewards: torch.FloatTensor ) -> torch.FloatTensor: - It standardizes the reward values using the updated mean and variance. - It then scales the standardized values to the 0-1 range using the error function (erf) as a CDF. """ + rewards = rewards.detach().cpu() + # Get the number of rewards (successful responses). new_count = rewards.numel() @@ -88,6 +90,7 @@ def apply( self, prompt: str, responses: List[ bt.DendriteCall ], name: str) -> """ Applies the reward model across each call. Unsuccessful responses are zeroed. """ # Get indices of correctly responding calls. + successful_completions_indices: List[int] = [ idx for idx, resp in enumerate(responses) if resp.is_success ] # Get all completions from responding calls. From 7858c1257d2c0c9d2a17e24589047c743fc9856a Mon Sep 17 00:00:00 2001 From: ifrit98 Date: Tue, 11 Jul 2023 15:23:31 +0000 Subject: [PATCH 10/18] change maxuids query -> current uids to avoid consensus issues --- openvalidators/neuron.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openvalidators/neuron.py b/openvalidators/neuron.py index 5e7b095..d09ba27 100644 --- a/openvalidators/neuron.py +++ b/openvalidators/neuron.py @@ -109,7 +109,7 @@ def __init__(self): # Init the gating model which learns which miners to select for each query. bt.logging.debug("loading", "gating_model") if not self.config.gating.num_uids: - self.config.gating.num_uids = self.subtensor.query_subtensor("MaxAllowedUids", params=[self.config.netuid]) + self.config.gating.num_uids = self.subtensor.subnetwork_n(self.config.netuid) if self.config.neuron.mock_gating_model: self.gating_model = MockGatingModel(self.metagraph.n.item()) elif self.config.neuron.use_custom_gating_model: From 92867ed54a0c7fbc4247eff04a7c0f31feb7e985 Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 11 Jul 2023 10:19:05 -0700 Subject: [PATCH 11/18] check if uids have enough for query --- openvalidators/forward.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/openvalidators/forward.py b/openvalidators/forward.py index 9fa8397..83faa51 100644 --- a/openvalidators/forward.py +++ b/openvalidators/forward.py @@ -42,15 +42,24 @@ def get_random_uids(self, k: int, exclude: List[int] = None) -> torch.LongTensor If `k` is larger than the number of available `uids`, set `k` to the number of available `uids`. """ candidate_uids = [] + avail_uids = [] for uid in range(self.metagraph.n.item()): uid_is_available = check_uid_availability(self.metagraph, uid, self.config.neuron.vpermit_tao_limit) uid_is_not_excluded = exclude is None or uid not in exclude - if uid_is_available and uid_is_not_excluded: - candidate_uids.append(uid) + if uid_is_available: + avail_uids.append(uid) + if uid_is_not_excluded: + candidate_uids.append(uid) + + # Check if candidate_uids contain enough for querying, if not grab all avaliable uids + if len(candidate_uids) > k: + available_uids = torch.tensor(candidate_uids, dtype=torch.int64).to(self.device) + else: + available_uids = torch.tensor(avail_uids, dtype=torch.int64).to(self.device) + - available_uids = torch.tensor(candidate_uids, dtype=torch.int64).to(self.device) uids = torch.tensor(random.sample(available_uids.tolist(), k), dtype=torch.int64) return uids From 010f178151369b0d08f10b79b444b2fa1255a18c Mon Sep 17 00:00:00 2001 From: Eugene Date: Thu, 13 Jul 2023 08:59:00 -0700 Subject: [PATCH 12/18] remvoe device error --- openvalidators/reward/reward.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/openvalidators/reward/reward.py b/openvalidators/reward/reward.py index 54ef90d..570b7bf 100644 --- a/openvalidators/reward/reward.py +++ b/openvalidators/reward/reward.py @@ -52,8 +52,6 @@ def normalize_rewards( self, rewards: torch.FloatTensor ) -> torch.FloatTensor: - It standardizes the reward values using the updated mean and variance. - It then scales the standardized values to the 0-1 range using the error function (erf) as a CDF. """ - rewards = rewards.detach().cpu() - # Get the number of rewards (successful responses). new_count = rewards.numel() From 7591dae4033a2544471faea120cbfb6a179ad1cb Mon Sep 17 00:00:00 2001 From: p-ferreira <38992619+p-ferreira@users.noreply.github.com> Date: Thu, 13 Jul 2023 12:10:23 -0400 Subject: [PATCH 13/18] replicates config.gating.num_uids to sentence embed gating model --- openvalidators/gating.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openvalidators/gating.py b/openvalidators/gating.py index 907e903..193e348 100644 --- a/openvalidators/gating.py +++ b/openvalidators/gating.py @@ -228,7 +228,7 @@ def __init__( config = SentenceEmbedGatingModel.config() if model_name is not None: config.gating.model_name = model_name - config.gating.num_uids = num_uids if num_uids is not None else metagraph.n + config.gating.num_uids = num_uids if num_uids is not None else config.gating.num_uids self.config = config self.num_uids = config.gating.num_uids self.device = torch.device(self.config.neuron.device) From 86b99344a2c534f0cf3200acca5f38655d55438f Mon Sep 17 00:00:00 2001 From: Eugene Date: Thu, 13 Jul 2023 11:28:06 -0700 Subject: [PATCH 14/18] empty cache after saving --- openvalidators/utils.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/openvalidators/utils.py b/openvalidators/utils.py index 341fbde..5eca03e 100644 --- a/openvalidators/utils.py +++ b/openvalidators/utils.py @@ -209,6 +209,10 @@ def save_state(self): self.wandb.log_artifact(model_artifact) bt.logging.success(prefix="Saved gating model", sufix=f"{gating_model_file_path}") + + #empty cache + torch.cuda.empty_cache() + except Exception as e: bt.logging.warning(f"Failed to save model with error: {e}") From 8d3b4b36311fe0c93512c5b668619b08a99e2a53 Mon Sep 17 00:00:00 2001 From: Eugene Date: Thu, 13 Jul 2023 12:58:46 -0700 Subject: [PATCH 15/18] testing --- openvalidators/neuron.py | 6 +++++- openvalidators/reward/diversity.py | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/openvalidators/neuron.py b/openvalidators/neuron.py index d09ba27..5c736bc 100644 --- a/openvalidators/neuron.py +++ b/openvalidators/neuron.py @@ -107,9 +107,13 @@ def __init__(self): bt.logging.debug(str(self.dataset)) # Init the gating model which learns which miners to select for each query. + print('self.config.gating.num_uids:', self.config.gating.num_uids) bt.logging.debug("loading", "gating_model") if not self.config.gating.num_uids: + print('GRABBING subnet n') self.config.gating.num_uids = self.subtensor.subnetwork_n(self.config.netuid) + print(self.config.gating.num_uids) + if self.config.neuron.mock_gating_model: self.gating_model = MockGatingModel(self.metagraph.n.item()) elif self.config.neuron.use_custom_gating_model: @@ -118,7 +122,7 @@ def __init__(self): self.gating_model = GatingModel(metagraph=self.metagraph, config=self.config).to(self.device) bt.logging.debug(str(self.gating_model)) - # Dendrite pool for querying the network during training. + # Dendrite pool for querying the network during training. bt.logging.debug("loading", "dendrite_pool") if self.config.neuron.mock_dendrite_pool: self.dendrite_pool = MockDendritePool() diff --git a/openvalidators/reward/diversity.py b/openvalidators/reward/diversity.py index 52eebd5..185c53b 100644 --- a/openvalidators/reward/diversity.py +++ b/openvalidators/reward/diversity.py @@ -90,7 +90,7 @@ def get_rewards( self, prompt: str, completions: List[str], name: str ) -> torch # Check if completions are empty, return 0 if so if len(completions) == 0: - return torch.tensor([]) + return torch.tensor([]).to(self.device) # Get embeddings for all completions. embeddings = self.get_embeddings( completions ) From eb6d5cc42ad9f2042779aa4ceea3a58d4e7f1430 Mon Sep 17 00:00:00 2001 From: Eugene Date: Thu, 13 Jul 2023 13:00:55 -0700 Subject: [PATCH 16/18] max n --- openvalidators/neuron.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openvalidators/neuron.py b/openvalidators/neuron.py index 5c736bc..2a36786 100644 --- a/openvalidators/neuron.py +++ b/openvalidators/neuron.py @@ -111,7 +111,7 @@ def __init__(self): bt.logging.debug("loading", "gating_model") if not self.config.gating.num_uids: print('GRABBING subnet n') - self.config.gating.num_uids = self.subtensor.subnetwork_n(self.config.netuid) + self.config.gating.num_uids = self.subtensor.max_n(self.config.netuid) print(self.config.gating.num_uids) if self.config.neuron.mock_gating_model: From 33852343bc1c28a4a6cc0d54b3c6cfc58d9ce855 Mon Sep 17 00:00:00 2001 From: Eugene Date: Thu, 13 Jul 2023 13:10:52 -0700 Subject: [PATCH 17/18] remove logging --- openvalidators/neuron.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/openvalidators/neuron.py b/openvalidators/neuron.py index 2a36786..f1a2405 100644 --- a/openvalidators/neuron.py +++ b/openvalidators/neuron.py @@ -107,12 +107,9 @@ def __init__(self): bt.logging.debug(str(self.dataset)) # Init the gating model which learns which miners to select for each query. - print('self.config.gating.num_uids:', self.config.gating.num_uids) bt.logging.debug("loading", "gating_model") if not self.config.gating.num_uids: - print('GRABBING subnet n') self.config.gating.num_uids = self.subtensor.max_n(self.config.netuid) - print(self.config.gating.num_uids) if self.config.neuron.mock_gating_model: self.gating_model = MockGatingModel(self.metagraph.n.item()) From 5531e8e892cb64f7098be0f6643b2daac3bc755b Mon Sep 17 00:00:00 2001 From: Eugene Date: Mon, 24 Jul 2023 08:51:27 -0700 Subject: [PATCH 18/18] comments --- openvalidators/forward.py | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/openvalidators/forward.py b/openvalidators/forward.py index 83faa51..376fc8c 100644 --- a/openvalidators/forward.py +++ b/openvalidators/forward.py @@ -52,15 +52,13 @@ def get_random_uids(self, k: int, exclude: List[int] = None) -> torch.LongTensor avail_uids.append(uid) if uid_is_not_excluded: candidate_uids.append(uid) - + # Check if candidate_uids contain enough for querying, if not grab all avaliable uids - if len(candidate_uids) > k: - available_uids = torch.tensor(candidate_uids, dtype=torch.int64).to(self.device) - else: - available_uids = torch.tensor(avail_uids, dtype=torch.int64).to(self.device) - + available_uids = candidate_uids + if len(candidate_uids) < k: + available_uids += random.sample([uid for uid in avail_uids if uid not in candidate_uids], k-len(candidate_uids)) - uids = torch.tensor(random.sample(available_uids.tolist(), k), dtype=torch.int64) + uids = torch.tensor(random.sample(available_uids, k), dtype=torch.int64) return uids