From 6b5f219ecad3826abaf557468b6d7fd0c0c78330 Mon Sep 17 00:00:00 2001 From: konrad0960 Date: Wed, 21 May 2025 14:53:57 +0200 Subject: [PATCH 1/2] Add local state logic for last updated block --- template/base/neuron.py | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/template/base/neuron.py b/template/base/neuron.py index 364bdcdf..b1c1ae09 100644 --- a/template/base/neuron.py +++ b/template/base/neuron.py @@ -108,6 +108,9 @@ def __init__(self, config=None): ) self.step = 0 + self._last_updated_block = self.metagraph.last_update[self.uid] + + @abstractmethod async def forward(self, synapse: bt.Synapse) -> bt.Synapse: ... @@ -125,9 +128,12 @@ def sync(self): if self.should_sync_metagraph(): self.resync_metagraph() + self._last_updated_block = self.block if self.should_set_weights(): self.set_weights() + self._last_updated_block = self.block + # Always save state. self.save_state() @@ -148,9 +154,10 @@ def should_sync_metagraph(self): """ Check if enough epoch blocks have elapsed since the last checkpoint to sync. """ - return ( - self.block - self.metagraph.last_update[self.uid] - ) > self.config.neuron.epoch_length + elapsed = self.block - self._last_updated_block + + # Only set weights if epoch has passed + return elapsed > self.config.neuron.epoch_length def should_set_weights(self) -> bool: # Don't set weights on initialization. @@ -161,12 +168,10 @@ def should_set_weights(self) -> bool: if self.config.neuron.disable_set_weights: return False - # Define appropriate logic for when set weights. - return ( - (self.block - self.metagraph.last_update[self.uid]) - > self.config.neuron.epoch_length - and self.neuron_type != "MinerNeuron" - ) # don't set weights if you're a miner + elapsed = self.block - self._last_updated_block + + # Only set weights if epoch has passed and this isn't a MinerNeuron. + return elapsed > self.config.neuron.epoch_length and self.neuron_type != "MinerNeuron" def save_state(self): bt.logging.trace( @@ -177,3 +182,4 @@ def load_state(self): bt.logging.trace( "load_state() not implemented for this neuron. You can implement this function to load model checkpoints or other useful data." ) + From d4eb5db4a4dc1e193ef36a9dab4a9b22ab823e72 Mon Sep 17 00:00:00 2001 From: konrad0960 <71330299+konrad0960@users.noreply.github.com> Date: Wed, 21 May 2025 15:03:37 +0200 Subject: [PATCH 2/2] Update neuron.py --- template/base/neuron.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/template/base/neuron.py b/template/base/neuron.py index b1c1ae09..0c30478b 100644 --- a/template/base/neuron.py +++ b/template/base/neuron.py @@ -110,7 +110,6 @@ def __init__(self, config=None): self._last_updated_block = self.metagraph.last_update[self.uid] - @abstractmethod async def forward(self, synapse: bt.Synapse) -> bt.Synapse: ... @@ -134,7 +133,6 @@ def sync(self): self.set_weights() self._last_updated_block = self.block - # Always save state. self.save_state() @@ -182,4 +180,3 @@ def load_state(self): bt.logging.trace( "load_state() not implemented for this neuron. You can implement this function to load model checkpoints or other useful data." ) -