Skip to content

Commit 073e363

Browse files
authored
Merge pull request #115 from web-genie-ai/feat/avg-score
Feat/avg score
2 parents 39a6ba6 + 48ef2d3 commit 073e363

File tree

3 files changed

+73
-38
lines changed

3 files changed

+73
-38
lines changed

neurons/validators/genie_validator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
SESSION_WINDOW_BLOCKS,
2020
BLOCK_IN_SECONDS,
2121
__VERSION__,
22+
MAX_NUMBER_OF_TASKS_PER_SESSION,
2223
)
2324
from webgenie.challenges import (
2425
AccuracyChallenge,
@@ -289,7 +290,6 @@ async def forward(self):
289290
else:
290291
task_index = self.neuron.score_manager.number_of_tasks
291292

292-
MAX_NUMBER_OF_TASKS_PER_SESSION = 18
293293
if task_index >= MAX_NUMBER_OF_TASKS_PER_SESSION:
294294
return
295295

neurons/validators/score_manager.py

Lines changed: 70 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ def load_scores(self):
5050
-1
5151
)
5252

53+
self.solved_tasks = data.get(
54+
f"solved_tasks",
55+
np.zeros(self.neuron.metagraph.n, dtype=np.float32),
56+
)
57+
5358
self.total_scores = data.get(
5459
f"total_scores_{__STATE_VERSION__}",
5560
np.zeros(self.neuron.metagraph.n, dtype=np.float32),
@@ -62,6 +67,7 @@ def load_scores(self):
6267
bt.logging.error(f"Error loading state: {e}")
6368
self.hotkeys = copy.deepcopy(self.neuron.metagraph.hotkeys)
6469
self.current_session = -1
70+
self.solved_tasks = np.zeros(self.neuron.metagraph.n, dtype=np.float32)
6571
self.total_scores = np.zeros(self.neuron.metagraph.n, dtype=np.float32)
6672
self.last_set_weights_session = -1
6773
self.number_of_tasks = 0
@@ -76,6 +82,7 @@ def save_scores(self):
7682
**{f"current_session": self.current_session},
7783
last_set_weights_session=self.last_set_weights_session,
7884
number_of_tasks=self.number_of_tasks,
85+
solved_tasks=self.solved_tasks,
7986
**{f"total_scores_{__STATE_VERSION__}": self.total_scores},
8087
session_results= self.session_results,
8188
allow_pickle=True,
@@ -91,6 +98,7 @@ def set_new_hotkeys(self, new_hotkeys: List[str]):
9198
for uid, hotkey in enumerate(self.hotkeys):
9299
if hotkey != new_hotkeys[uid]:
93100
self.total_scores[uid] = 0
101+
self.solved_tasks[uid] = 0
94102

95103
# Check to see if the metagraph has changed size.
96104
# If so, we need to add new hotkeys and moving averages.
@@ -100,6 +108,11 @@ def set_new_hotkeys(self, new_hotkeys: List[str]):
100108
new_total_scores[:min_len] = self.total_scores[:min_len]
101109
self.total_scores = new_total_scores
102110

111+
new_solved_tasks = np.zeros((len(new_hotkeys)))
112+
min_len = min(len(self.hotkeys), len(self.solved_tasks))
113+
new_solved_tasks[:min_len] = self.solved_tasks[:min_len]
114+
self.solved_tasks = new_solved_tasks
115+
103116
# Update the hotkeys.
104117
self.hotkeys = copy.deepcopy(new_hotkeys)
105118
with self.lock:
@@ -113,16 +126,27 @@ def update_scores(self, rewards: np.ndarray, uids: List[int], challenge: Challen
113126
# This is a new session, reset the scores and winners.
114127
self.current_session = session
115128
self.number_of_tasks = 0
129+
self.solved_tasks = np.zeros(self.neuron.metagraph.n, dtype=np.float32)
116130
self.total_scores = np.zeros(self.neuron.metagraph.n, dtype=np.float32)
117131
# Update accumulated scores and track best performer
118132
self.number_of_tasks += 1
119133
self.total_scores[uids] += rewards
134+
self.solved_tasks[uids] += 1
120135

136+
avg_scores = np.zeros(self.neuron.metagraph.n, dtype=np.float32)
137+
for uid in range(self.neuron.metagraph.n):
138+
if self.solved_tasks[uid] >= max(1, self.number_of_tasks / 2):
139+
avg_scores[uid] = self.total_scores[uid] / self.solved_tasks[uid]
140+
else:
141+
avg_scores[uid] = 0
142+
winner = np.argmax(avg_scores) if max(avg_scores) > 0 else -1
143+
121144
current_session_results = {
122145
"session": session,
123146
"competition_type": competition_type,
124147
"number_of_tasks": self.number_of_tasks,
125-
"winner": np.argmax(self.total_scores),
148+
"winner": winner,
149+
"solved_tasks": self.solved_tasks,
126150
"scores": self.total_scores,
127151
}
128152

@@ -162,42 +186,51 @@ def get_scores(self, session_upto: int):
162186
# return np.power(scores, 9)
163187

164188
def print_session_result(self, session_upto: int, console: Console):
165-
session_result = self.session_results[session_upto]
166-
167-
number_of_tasks = session_result["number_of_tasks"]
168-
session = session_result["session"]
169-
competition_type = session_result["competition_type"]
170-
winner = session_result["winner"]
171-
scores = session_result["scores"]
172-
173-
total_scores_table = Table(
174-
title=(
175-
f"📊 Total Scores Summary\n"
176-
f"🔄 Session: #{session}\n"
177-
f"📝 Number of Tasks: #{number_of_tasks}\n"
178-
f"🏆 Competition: {competition_type}\n"
179-
f"👑 Winner: #{winner}\n"
180-
),
181-
show_header=True,
182-
header_style="bold magenta",
183-
title_style="bold blue",
184-
border_style="blue"
185-
)
186-
187-
total_scores_table.add_column("Rank", justify="right", style="red", header_style="bold red")
188-
total_scores_table.add_column("UID", justify="right", style="cyan", header_style="bold cyan")
189-
total_scores_table.add_column("Total Score", justify="right", style="green")
190-
total_scores_table.add_column("Average Score", justify="right", style="yellow")
191-
scored_uids = [(uid, score) for uid, score in enumerate(scores) if score > 0]
192-
scored_uids.sort(key=lambda x: x[1], reverse=True)
193-
for rank, (uid, score) in enumerate(scored_uids):
194-
total_scores_table.add_row(
195-
str(rank + 1),
196-
str(uid),
197-
f"{score:.4f}",
198-
f"{score / number_of_tasks:.4f}",
189+
try:
190+
session_result = self.session_results[session_upto]
191+
192+
number_of_tasks = session_result["number_of_tasks"]
193+
session = session_result["session"]
194+
competition_type = session_result["competition_type"]
195+
winner = session_result["winner"]
196+
scores = session_result["scores"]
197+
solved_tasks = session_result["solved_tasks"]
198+
199+
avg_scores = np.zeros(self.neuron.metagraph.n, dtype=np.float32)
200+
for uid in range(self.neuron.metagraph.n):
201+
if solved_tasks[uid] >= max(1, number_of_tasks / 2):
202+
avg_scores[uid] = scores[uid] / solved_tasks[uid]
203+
else:
204+
avg_scores[uid] = 0
205+
206+
total_scores_table = Table(
207+
title=(
208+
f"📊 Total Scores Summary\n"
209+
f"🔄 Session: #{session}\n"
210+
f"📝 Number of Tasks: #{number_of_tasks}\n"
211+
f"🏆 Competition: {competition_type}\n"
212+
f"👑 Winner: #{winner}\n"
213+
),
214+
show_header=True,
215+
header_style="bold magenta",
216+
title_style="bold blue",
217+
border_style="blue"
199218
)
200-
console.print(total_scores_table)
219+
220+
total_scores_table.add_column("Rank", justify="right", style="red", header_style="bold red")
221+
total_scores_table.add_column("UID", justify="right", style="cyan", header_style="bold cyan")
222+
total_scores_table.add_column("Average Score", justify="right", style="yellow")
223+
scored_uids = [(uid, avg_scores[uid]) for uid in range(self.neuron.metagraph.n) if avg_scores[uid] > 0]
224+
scored_uids.sort(key=lambda x: x[1], reverse=True)
225+
for rank, (uid, score) in enumerate(scored_uids):
226+
total_scores_table.add_row(
227+
str(rank + 1),
228+
str(uid),
229+
f"{score:.4f}",
230+
)
231+
console.print(total_scores_table)
232+
except Exception as e:
233+
bt.logging.warning(f"Error printing session result: {e}")
201234

202235
def save_session_result_to_file(self, session_upto: int):
203236
try:
@@ -211,3 +244,4 @@ def save_session_result_to_file(self, session_upto: int):
211244
except Exception as e:
212245
bt.logging.error(f"Error saving session result to file: {e}")
213246
raise e
247+

webgenie/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
# Change this value when updating your code base.
55
# Define the version of the webgenie.
6-
__VERSION__ = "1.1.16" # version
6+
__VERSION__ = "1.1.17" # version
77

88
SPEC_VERSION = (
99
(1000 * int(__VERSION__.split(".")[0]))
@@ -87,3 +87,4 @@
8787

8888
NEURON_EPOCH_LENGTH = int(os.getenv("NEURON_EPOCH_LENGTH", 25)) # neuron epoch length
8989

90+
MAX_NUMBER_OF_TASKS_PER_SESSION = 18 # max number of tasks per session

0 commit comments

Comments
 (0)