12
12
from typing import Tuple
13
13
import bittensor as bt
14
14
from bittensor import StreamingSynapse
15
+
15
16
import cortext
16
17
from starlette .types import Send
17
18
@@ -72,7 +73,7 @@ def __init__(self, config, cache: QueryResponseCache, loop=None):
72
73
self .weights_rate_limit = self .node_query ('SubtensorModule' , 'WeightsSetRateLimit' , [self .netuid ])
73
74
74
75
# Set up async-related attributes
75
- self .lock = asyncio .Lock ()
76
+ self .lock = threading .Lock ()
76
77
self .loop = loop or asyncio .get_event_loop ()
77
78
78
79
# Initialize shared query database
@@ -95,11 +96,14 @@ def __init__(self, config, cache: QueryResponseCache, loop=None):
95
96
daemon_thread = threading .Thread (target = self .saving_resp_answers_from_miners )
96
97
daemon_thread .start ()
97
98
98
- synthetic_thread = threading .Thread (target = self .process_synthetic_tasks )
99
- synthetic_thread .start ()
99
+ # synthetic_thread = threading.Thread(target=self.process_synthetic_tasks)
100
+ # synthetic_thread.start()
101
+ self .loop .create_task (self .perform_synthetic_queries ())
102
+
103
+ # organic_thread = threading.Thread(target=self.start_axon_server)
104
+ # organic_thread.start()
105
+ self .loop .create_task (self .consume_organic_queries ())
100
106
101
- organic_thread = threading .Thread (target = self .start_axon_server )
102
- organic_thread .start ()
103
107
104
108
def start_axon_server (self ):
105
109
asyncio .run (self .consume_organic_queries ())
@@ -134,6 +138,7 @@ async def refresh_metagraph(self):
134
138
await self .run_sync_in_async (lambda : self .metagraph .sync ())
135
139
136
140
async def initialize_uids_and_capacities (self ):
141
+ bt .logging .info ("start initializing uids and capacities" )
137
142
self .available_uid_to_axons = await self .get_available_uids ()
138
143
self .uids_to_query = list (self .available_uid_to_axons .keys ())
139
144
bt .logging .info (f"Available UIDs: { list (self .available_uid_to_axons .keys ())} " )
@@ -175,7 +180,8 @@ def is_epoch_end(self):
175
180
async def update_and_refresh (self ):
176
181
await self .update_weights ()
177
182
bt .logging .info ("Refreshing metagraph..." )
178
- await self .refresh_metagraph ()
183
+
184
+ self .metagraph .sync ()
179
185
await self .initialize_uids_and_capacities ()
180
186
bt .logging .info ("Metagraph refreshed." )
181
187
@@ -300,6 +306,8 @@ async def perform_synthetic_queries(self):
300
306
continue
301
307
self .set_up_next_block_to_wait ()
302
308
# await asyncio.sleep(432)
309
+ bt .logging .debug ("start synthetic queries" )
310
+ self .loop = asyncio .get_event_loop ()
303
311
self .loop .create_task (self .perform_synthetic_queries_one_cycle ())
304
312
305
313
def pop_synthetic_tasks_max_100_per_miner (self , synthetic_tasks ):
@@ -399,10 +407,11 @@ async def set_weights(self, scores):
399
407
wallet = self .wallet ,
400
408
uids = self .metagraph .uids ,
401
409
weights = self .moving_average_scores ,
402
- wait_for_inclusion = True ,
410
+ wait_for_inclusion = False ,
403
411
version_key = cortext .__weights_version__ ,
404
412
)
405
- bt .logging .info (f"done setting weights: { success } , { msg } . { time .time () - start_time } elaspsed for updating weights." )
413
+ bt .logging .info (
414
+ f"done setting weights: { success } , { msg } . { time .time () - start_time } elaspsed for updating weights." )
406
415
407
416
def blacklist_prompt (self , synapse : StreamPrompting ) -> Tuple [bool , str ]:
408
417
blacklist = self .base_blacklist (synapse , cortext .PROMPT_BLACKLIST_STAKE )
@@ -596,24 +605,45 @@ async def process_queries_from_database(self):
596
605
if not self .query_database :
597
606
bt .logging .debug ("no data in query_database. so continue..." )
598
607
continue
599
- if not self .is_epoch_end ():
600
- bt .logging .debug ("no end of epoch. so continue..." )
601
- continue
602
608
if not self .synthetic_task_done :
603
609
bt .logging .debug ("wait for synthetic tasks to complete." )
604
610
continue
611
+ if not self .is_epoch_end ():
612
+ bt .logging .debug ("no end of epoch. so continue..." )
613
+ continue
605
614
606
615
bt .logging .info (f"start scoring process..." )
607
616
608
617
queries_to_process = self .query_database .copy ()
609
618
self .query_database .clear ()
610
619
620
+
611
621
self .synthetic_task_done = False
612
622
bt .logging .info ("start scoring process" )
613
623
start_time = time .time ()
614
624
625
+ # remove query_resps where len of resp is 0
626
+ empty_uid_model_items = []
627
+ for item in queries_to_process :
628
+ uid = item .get ("uid" )
629
+ resp = item .get ("response" )
630
+ model = item .get ("synapse" ).model
631
+ if not resp :
632
+ empty_uid_model_items .append ((uid , model ))
633
+
634
+ items_to_score = []
635
+ for item in queries_to_process :
636
+ uid = item .get ("uid" )
637
+ model = item .get ("synapse" ).model
638
+ if (uid , model ) in empty_uid_model_items :
639
+ bt .logging .trace (
640
+ f"this miner { uid } has at least 1 empty response for model { model } . so being scored as 0." )
641
+ continue
642
+ items_to_score .append (item )
643
+ bt .logging .info (f"total len of datas to score: { len (items_to_score )} " )
644
+
615
645
# with all query_respones, select one per uid, provider, model randomly and score them.
616
- score_tasks = self .get_scoring_tasks_from_query_responses (queries_to_process )
646
+ score_tasks = self .get_scoring_tasks_from_query_responses (items_to_score )
617
647
618
648
resps = await asyncio .gather (* score_tasks , return_exceptions = True )
619
649
resps = [item for item in resps if item is not None ]
@@ -623,6 +653,12 @@ async def process_queries_from_database(self):
623
653
if self .total_scores .get (uid ) is not None :
624
654
self .total_scores [uid ] += score
625
655
self .score_counts [uid ] += 1
656
+
657
+ for uid in self .uid_to_capacity :
658
+ if self .total_scores .get (uid ) is None :
659
+ self .total_scores [uid ] = 0
660
+ self .score_counts [uid ] = 1
661
+
626
662
bt .logging .info (
627
663
f"current total score are { self .total_scores } . total time of scoring is { time .time () - start_time } " )
628
664
self .saving_datas = queries_to_process .copy ()
0 commit comments