From 105773e69e5491634f5ac88aba382b9f240fc5f8 Mon Sep 17 00:00:00 2001 From: Eugene Date: Thu, 3 Aug 2023 12:41:19 -0400 Subject: [PATCH 01/13] relevance updates --- openvalidators/forward.py | 11 +++++++++-- openvalidators/reward/relevance.py | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/openvalidators/forward.py b/openvalidators/forward.py index 376fc8c..1fd5551 100644 --- a/openvalidators/forward.py +++ b/openvalidators/forward.py @@ -62,7 +62,11 @@ def get_random_uids(self, k: int, exclude: List[int] = None) -> torch.LongTensor return uids -async def run_step(self, prompt: str, k: int, timeout: float, name: str, exclude: list = []): +async def run_step(self, prompt: str, k: int, timeout: float, name: str, exclude: list = [], base_prompt = None): + + if base_prompt == None: + base_prompt = prompt + bt.logging.debug("run_step", name) # Record event start time. @@ -90,7 +94,7 @@ async def run_step(self, prompt: str, k: int, timeout: float, name: str, exclude bt.logging.trace(str(reward_fn_i.name), reward_i.tolist()) for masking_fn_i in self.masking_functions: - mask_i = masking_fn_i.apply(prompt, responses, name).to(self.device) + mask_i = masking_fn_i.apply(base_prompt, responses, name).to(self.device) rewards *= mask_i # includes diversity if not self.config.neuron.disable_log_rewards: event[masking_fn_i.name] = mask_i.tolist() @@ -168,6 +172,7 @@ async def forward(self): ) base_text = augment_event["best"] + base_prompt = augment_event["best"] exclude = augment_event["uids"] for k in range(self.config.neuron.num_followup_steps): @@ -180,6 +185,7 @@ async def forward(self): k=self.config.neuron.followup_sample_size, timeout=self.config.neuron.followup_timeout, exclude=exclude, + base_prompt=base_prompt ) exclude += followup_event["uids"] @@ -192,6 +198,7 @@ async def forward(self): k=self.config.neuron.answer_sample_size, timeout=self.config.neuron.answer_timeout, exclude=exclude, + base_prompt=followup_event["best"] ) exclude += answer_event["uids"] diff --git a/openvalidators/reward/relevance.py b/openvalidators/reward/relevance.py index d41d50f..979aaa8 100644 --- a/openvalidators/reward/relevance.py +++ b/openvalidators/reward/relevance.py @@ -61,6 +61,7 @@ def __init__( self, device: str ): self.bounds = [-0.0246, 0.3] def get_rewards( self, prompt: str, completions: List[str], name: str ) -> torch.FloatTensor: + print('revelence prompt:',prompt) return torch.tensor( [self.reward( prompt, completion, name ) for completion in completions], dtype=torch.float32).to(self.device) def normalize_rewards( self, rewards: torch.FloatTensor ) -> torch.FloatTensor: From 85933bf7eb9e196cb9e1f1809dea23b577fd11e7 Mon Sep 17 00:00:00 2001 From: Eugene Date: Thu, 3 Aug 2023 13:00:21 -0400 Subject: [PATCH 02/13] more logging --- openvalidators/forward.py | 2 ++ openvalidators/reward/relevance.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/openvalidators/forward.py b/openvalidators/forward.py index 1fd5551..64c07db 100644 --- a/openvalidators/forward.py +++ b/openvalidators/forward.py @@ -212,3 +212,5 @@ async def forward(self): ) else: base_text = base_text + "\nQuestion:" + followup_event["best"] + "\nAnswer:" + answer_event["best"] + + print(base_text) diff --git a/openvalidators/reward/relevance.py b/openvalidators/reward/relevance.py index 979aaa8..2dd2c1d 100644 --- a/openvalidators/reward/relevance.py +++ b/openvalidators/reward/relevance.py @@ -61,7 +61,7 @@ def __init__( self, device: str ): self.bounds = [-0.0246, 0.3] def get_rewards( self, prompt: str, completions: List[str], name: str ) -> torch.FloatTensor: - print('revelence prompt:',prompt) + print(f'revelence prompt:{name}',prompt) return torch.tensor( [self.reward( prompt, completion, name ) for completion in completions], dtype=torch.float32).to(self.device) def normalize_rewards( self, rewards: torch.FloatTensor ) -> torch.FloatTensor: From ac623e9aa65be4dd693acff20ad4d935833a2c18 Mon Sep 17 00:00:00 2001 From: opentaco Date: Mon, 7 Aug 2023 18:17:12 +0200 Subject: [PATCH 03/13] Sync diversity model to disk --- openvalidators/utils.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/openvalidators/utils.py b/openvalidators/utils.py index 340a020..b1ae8ea 100644 --- a/openvalidators/utils.py +++ b/openvalidators/utils.py @@ -201,6 +201,15 @@ def save_state(self): gating_model_file_path = f"{self.config.neuron.full_path}/{gating_model_name}_gating_linear_layer.pth" torch.save(gating_model_linear_layer_dict, gating_model_file_path) + # Save diversity model. + diversity_model_dict = {"historic_embeddings": self.diversity_model.historic_embeddings} + diversity_model_file_path = f"{self.config.neuron.full_path}/diversity_model.pth" + torch.save(diversity_model_dict, diversity_model_file_path) + bt.logging.success( + prefix="Saved diversity model", + sufix=f"{diversity_model_file_path} [{self.diversity_model.historic_embeddings.shape}]", + ) + if not self.config.wandb.off: wandb.log({ "step": self.step, @@ -234,5 +243,15 @@ def load_state(self): prefix="Reloaded model", sufix=f"{ self.config.neuron.full_path }/model.torch", ) + + # Load diversity model. + diversity_model_file_path = f"{self.config.neuron.full_path}/diversity_model.pth" + diversity_model_dict = torch.load(diversity_model_file_path) + self.diversity_model.historic_embeddings = diversity_model_dict["historic_embeddings"] + bt.logging.success( + prefix="Reloaded diversity model", + sufix=f"{diversity_model_file_path} [{self.diversity_model.historic_embeddings.shape}]", + ) + except Exception as e: bt.logging.warning(f"Failed to load model with error: {e}") From 74749519bba43e35f0c026e4e8578e359f1ddf35 Mon Sep 17 00:00:00 2001 From: opentaco Date: Tue, 8 Aug 2023 17:14:20 +0200 Subject: [PATCH 04/13] Self diversity model --- openvalidators/neuron.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openvalidators/neuron.py b/openvalidators/neuron.py index 6ba439b..29e0a1c 100644 --- a/openvalidators/neuron.py +++ b/openvalidators/neuron.py @@ -201,7 +201,7 @@ def __init__(self): RelevanceRewardModel(device=self.device) if not self.config.neuron.relevance_off else MockRewardModel(RewardModelType.relevance.value) ) - diversity_model = ( + self.diversity_model = ( DiversityRewardModel(device=self.device) if not self.config.neuron.diversity_off else MockRewardModel(RewardModelType.diversity.value) ) @@ -210,7 +210,7 @@ def __init__(self): else MockRewardModel(RewardModelType.nsfw.value) ) - self.masking_functions = [self.blacklist, task_validator, relevance_model, diversity_model, nsfw_model] + self.masking_functions = [self.blacklist, task_validator, relevance_model, self.diversity_model, nsfw_model] bt.logging.debug(str(self.reward_functions)) bt.logging.debug(str(self.masking_functions)) From 9fc75d9036e8a650e4c72b67f8e3df94539e7b44 Mon Sep 17 00:00:00 2001 From: opentaco Date: Tue, 8 Aug 2023 17:19:57 +0200 Subject: [PATCH 05/13] Separate exceptions --- openvalidators/utils.py | 39 +++++++++++++++++++++++---------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/openvalidators/utils.py b/openvalidators/utils.py index b1ae8ea..398984b 100644 --- a/openvalidators/utils.py +++ b/openvalidators/utils.py @@ -194,27 +194,21 @@ def save_state(self): prefix="Saved model", sufix=f"{ self.config.neuron.full_path }/model.torch", ) + except Exception as e: + bt.logging.warning(f"Failed to save model with error: {e}") + try: # Save the gating model. gating_model_linear_layer_dict = self.gating_model.linear.state_dict() gating_model_name = self.config.gating.model_name.replace("/", "_") gating_model_file_path = f"{self.config.neuron.full_path}/{gating_model_name}_gating_linear_layer.pth" torch.save(gating_model_linear_layer_dict, gating_model_file_path) - # Save diversity model. - diversity_model_dict = {"historic_embeddings": self.diversity_model.historic_embeddings} - diversity_model_file_path = f"{self.config.neuron.full_path}/diversity_model.pth" - torch.save(diversity_model_dict, diversity_model_file_path) - bt.logging.success( - prefix="Saved diversity model", - sufix=f"{diversity_model_file_path} [{self.diversity_model.historic_embeddings.shape}]", - ) - if not self.config.wandb.off: wandb.log({ "step": self.step, "block": ttl_get_block(self), - **neuron_state_dict + **neuron_state_dict }) if not self.config.wandb.off and self.config.wandb.track_gating_model: model_artifact = wandb.Artifact(f"{gating_model_name}_gating_linear_layer", type="model") @@ -222,12 +216,23 @@ def save_state(self): self.wandb.log_artifact(model_artifact) bt.logging.success(prefix="Saved gating model", sufix=f"{gating_model_file_path}") + except Exception as e: + bt.logging.warning(f"Failed to save gating model with error: {e}") - #empty cache - torch.cuda.empty_cache() - + try: + # Save diversity model. + diversity_model_dict = {"historic_embeddings": self.diversity_model.historic_embeddings} + diversity_model_file_path = f"{self.config.neuron.full_path}/diversity_model.pth" + torch.save(diversity_model_dict, diversity_model_file_path) + bt.logging.success( + prefix="Saved diversity model", + sufix=f"{diversity_model_file_path} [{self.diversity_model.historic_embeddings.shape}]", + ) except Exception as e: - bt.logging.warning(f"Failed to save model with error: {e}") + bt.logging.warning(f"Failed to save diversity model with error: {e}") + + # empty cache + torch.cuda.empty_cache() def load_state(self): @@ -243,7 +248,10 @@ def load_state(self): prefix="Reloaded model", sufix=f"{ self.config.neuron.full_path }/model.torch", ) + except Exception as e: + bt.logging.warning(f"Failed to load model with error: {e}") + try: # Load diversity model. diversity_model_file_path = f"{self.config.neuron.full_path}/diversity_model.pth" diversity_model_dict = torch.load(diversity_model_file_path) @@ -252,6 +260,5 @@ def load_state(self): prefix="Reloaded diversity model", sufix=f"{diversity_model_file_path} [{self.diversity_model.historic_embeddings.shape}]", ) - except Exception as e: - bt.logging.warning(f"Failed to load model with error: {e}") + bt.logging.warning(f"Failed to load diversity model with error: {e}") From aa2e2b55b466641bb7d7bcf3b10f20bc11b5aacc Mon Sep 17 00:00:00 2001 From: opentaco Date: Tue, 8 Aug 2023 17:29:09 +0200 Subject: [PATCH 06/13] Print historic_embeddings shape --- openvalidators/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openvalidators/utils.py b/openvalidators/utils.py index 398984b..8bcdf1a 100644 --- a/openvalidators/utils.py +++ b/openvalidators/utils.py @@ -226,7 +226,7 @@ def save_state(self): torch.save(diversity_model_dict, diversity_model_file_path) bt.logging.success( prefix="Saved diversity model", - sufix=f"{diversity_model_file_path} [{self.diversity_model.historic_embeddings.shape}]", + sufix=f"{diversity_model_file_path} [{list(self.diversity_model.historic_embeddings.shape)}]", ) except Exception as e: bt.logging.warning(f"Failed to save diversity model with error: {e}") @@ -258,7 +258,7 @@ def load_state(self): self.diversity_model.historic_embeddings = diversity_model_dict["historic_embeddings"] bt.logging.success( prefix="Reloaded diversity model", - sufix=f"{diversity_model_file_path} [{self.diversity_model.historic_embeddings.shape}]", + sufix=f"{diversity_model_file_path} [{list(self.diversity_model.historic_embeddings.shape)}]", ) except Exception as e: bt.logging.warning(f"Failed to load diversity model with error: {e}") From 2ceb5d0097bb8182560d8952f3d8b9b57a7785ea Mon Sep 17 00:00:00 2001 From: opentaco Date: Tue, 8 Aug 2023 17:55:02 +0200 Subject: [PATCH 07/13] Print historic_embeddings shape --- openvalidators/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openvalidators/utils.py b/openvalidators/utils.py index 8bcdf1a..29cc89a 100644 --- a/openvalidators/utils.py +++ b/openvalidators/utils.py @@ -226,7 +226,7 @@ def save_state(self): torch.save(diversity_model_dict, diversity_model_file_path) bt.logging.success( prefix="Saved diversity model", - sufix=f"{diversity_model_file_path} [{list(self.diversity_model.historic_embeddings.shape)}]", + sufix=f"{diversity_model_file_path} {list(self.diversity_model.historic_embeddings.shape)}", ) except Exception as e: bt.logging.warning(f"Failed to save diversity model with error: {e}") @@ -258,7 +258,7 @@ def load_state(self): self.diversity_model.historic_embeddings = diversity_model_dict["historic_embeddings"] bt.logging.success( prefix="Reloaded diversity model", - sufix=f"{diversity_model_file_path} [{list(self.diversity_model.historic_embeddings.shape)}]", + sufix=f"{diversity_model_file_path} {list(self.diversity_model.historic_embeddings.shape)}", ) except Exception as e: bt.logging.warning(f"Failed to load diversity model with error: {e}") From 69aca46d4d68e4fdb67f521a002f300b07e23c4f Mon Sep 17 00:00:00 2001 From: opentaco Date: Tue, 8 Aug 2023 18:08:42 +0200 Subject: [PATCH 08/13] Tensorize neuron_weights --- openvalidators/utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/openvalidators/utils.py b/openvalidators/utils.py index 29cc89a..14b8398 100644 --- a/openvalidators/utils.py +++ b/openvalidators/utils.py @@ -241,8 +241,9 @@ def load_state(self): try: state_dict = torch.load(f"{self.config.neuron.full_path}/model.torch") # Check for nans in saved state dict - if not torch.isnan(state_dict["neuron_weights"]).any(): - self.moving_averaged_scores = state_dict["neuron_weights"].clone().detach() + neuron_weights = torch.tensor(state_dict["neuron_weights"]) + if not torch.isnan(neuron_weights).any(): + self.moving_averaged_scores = neuron_weights self.hotkeys = state_dict["neuron_hotkeys"] bt.logging.success( prefix="Reloaded model", From 2ad3604a6ed4e1adbed557522c9bcf7edc3738f3 Mon Sep 17 00:00:00 2001 From: opentaco Date: Tue, 8 Aug 2023 18:20:59 +0200 Subject: [PATCH 09/13] Device neuron_weights --- openvalidators/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openvalidators/utils.py b/openvalidators/utils.py index 14b8398..7e72972 100644 --- a/openvalidators/utils.py +++ b/openvalidators/utils.py @@ -243,7 +243,7 @@ def load_state(self): # Check for nans in saved state dict neuron_weights = torch.tensor(state_dict["neuron_weights"]) if not torch.isnan(neuron_weights).any(): - self.moving_averaged_scores = neuron_weights + self.moving_averaged_scores = neuron_weights.to(self.device) self.hotkeys = state_dict["neuron_hotkeys"] bt.logging.success( prefix="Reloaded model", From 7432eb8b7ae9621c6d8d6850a5b204f5863f6d37 Mon Sep 17 00:00:00 2001 From: opentaco Date: Tue, 8 Aug 2023 18:47:24 +0200 Subject: [PATCH 10/13] Device agnostic save --- openvalidators/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/openvalidators/utils.py b/openvalidators/utils.py index 7e72972..d008f55 100644 --- a/openvalidators/utils.py +++ b/openvalidators/utils.py @@ -221,7 +221,7 @@ def save_state(self): try: # Save diversity model. - diversity_model_dict = {"historic_embeddings": self.diversity_model.historic_embeddings} + diversity_model_dict = {"historic_embeddings": self.diversity_model.historic_embeddings.to('cpu')} diversity_model_file_path = f"{self.config.neuron.full_path}/diversity_model.pth" torch.save(diversity_model_dict, diversity_model_file_path) bt.logging.success( @@ -256,7 +256,7 @@ def load_state(self): # Load diversity model. diversity_model_file_path = f"{self.config.neuron.full_path}/diversity_model.pth" diversity_model_dict = torch.load(diversity_model_file_path) - self.diversity_model.historic_embeddings = diversity_model_dict["historic_embeddings"] + self.diversity_model.historic_embeddings = diversity_model_dict["historic_embeddings"].to(self.device) bt.logging.success( prefix="Reloaded diversity model", sufix=f"{diversity_model_file_path} {list(self.diversity_model.historic_embeddings.shape)}", From 4ff6ebb58d9abe5e8c99752eb6175ab689a0c7db Mon Sep 17 00:00:00 2001 From: Eugene Date: Tue, 8 Aug 2023 10:33:51 -0700 Subject: [PATCH 11/13] remove logging prints --- openvalidators/forward.py | 3 +-- openvalidators/reward/relevance.py | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/openvalidators/forward.py b/openvalidators/forward.py index 64c07db..59589b2 100644 --- a/openvalidators/forward.py +++ b/openvalidators/forward.py @@ -212,5 +212,4 @@ async def forward(self): ) else: base_text = base_text + "\nQuestion:" + followup_event["best"] + "\nAnswer:" + answer_event["best"] - - print(base_text) + \ No newline at end of file diff --git a/openvalidators/reward/relevance.py b/openvalidators/reward/relevance.py index 2dd2c1d..d41d50f 100644 --- a/openvalidators/reward/relevance.py +++ b/openvalidators/reward/relevance.py @@ -61,7 +61,6 @@ def __init__( self, device: str ): self.bounds = [-0.0246, 0.3] def get_rewards( self, prompt: str, completions: List[str], name: str ) -> torch.FloatTensor: - print(f'revelence prompt:{name}',prompt) return torch.tensor( [self.reward( prompt, completion, name ) for completion in completions], dtype=torch.float32).to(self.device) def normalize_rewards( self, rewards: torch.FloatTensor ) -> torch.FloatTensor: From e4e41732f3d29d95bcf59303c81ed54aa0189af4 Mon Sep 17 00:00:00 2001 From: p-ferreira <38992619+p-ferreira@users.noreply.github.com> Date: Tue, 8 Aug 2023 14:02:07 -0400 Subject: [PATCH 12/13] updates changelog for v1.1.5 --- CHANGELOG.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 27ad1ae..a4b745d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,12 @@ # Changelog -## 1.1.4 / 2023-08-07 +## 1.1.5 / 2023-08-08 ### What’s Changed +- Adds new keywords for the task validator by @p-ferreira in #119 +- Save historic embeddings on disk by @opentaco in #121 +- Updates relevance mechanism by @Eugene-hu in #122 + +## 1.1.4 / 2023-08-07 - HOTFIX: create and serve the axon at startup by @robertalanm in #120 From 2577f0b500dcc3142a255bffd84c53dcabd71fb5 Mon Sep 17 00:00:00 2001 From: p-ferreira <38992619+p-ferreira@users.noreply.github.com> Date: Tue, 8 Aug 2023 14:04:43 -0400 Subject: [PATCH 13/13] updates version --- openvalidators/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/openvalidators/__init__.py b/openvalidators/__init__.py index 7820940..4609769 100644 --- a/openvalidators/__init__.py +++ b/openvalidators/__init__.py @@ -28,6 +28,6 @@ from . import weights from . import event -__version__ = "1.1.4" +__version__ = "1.1.5" version_split = __version__.split(".") __spec_version__ = (1000 * int(version_split[0])) + (10 * int(version_split[1])) + (1 * int(version_split[2]))