Arpan Tripathi commited on
Commit
c5414cc
·
unverified ·
1 Parent(s): 31d2567

deduplicate compression miner scoring entries (#165)

Browse files
neurons/validator.py CHANGED
@@ -34,6 +34,10 @@ from sqlalchemy import desc, asc, func, select, delete
34
  from sqlalchemy.ext.asyncio import AsyncSession
35
  from datetime import datetime, timedelta
36
  from services.dashboard.server import send_upscaling_data_to_dashboard, send_compression_data_to_dashboard
 
 
 
 
37
  from services.video_scheduler.redis_utils import (
38
  get_redis_connection,
39
  get_organic_upscaling_queue_size,
@@ -1080,6 +1084,7 @@ class Validator(base.BaseValidator):
1080
  f"{num_miners} miners in {scoring_batch_count} batches of up to {scoring_batch_size}"
1081
  )
1082
 
 
1083
  for batch_idx, batch_start in enumerate(range(0, total_scoring_responses, scoring_batch_size), start=1):
1084
  batch_end = min(batch_start + scoring_batch_size, total_scoring_responses)
1085
  batch_uids = flat_uids[batch_start:batch_end]
@@ -1112,6 +1117,7 @@ class Validator(base.BaseValidator):
1112
  round_id,
1113
  distorted_urls=flat_distorted_urls[batch_start:batch_end],
1114
  duplicate_url_reasons=batch_duplicate_url_reasons,
 
1115
  )
1116
 
1117
  logger.info(
@@ -1331,6 +1337,7 @@ class Validator(base.BaseValidator):
1331
  round_id: str,
1332
  distorted_urls: list[str] | None = None,
1333
  duplicate_url_reasons: dict[int, str] | None = None,
 
1334
  ):
1335
  if distorted_urls is None:
1336
  distorted_urls = []
@@ -1402,6 +1409,36 @@ class Validator(base.BaseValidator):
1402
  lambda idx: duplicate_url_reasons[idx],
1403
  "No reason provided",
1404
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1405
 
1406
  logger.info(f"Updating miner manager with {len(compression_rates)} compression miner scores after synthetic requests processing")
1407
 
 
34
  from sqlalchemy.ext.asyncio import AsyncSession
35
  from datetime import datetime, timedelta
36
  from services.dashboard.server import send_upscaling_data_to_dashboard, send_compression_data_to_dashboard
37
+ from services.scoring.compression_score_cache import (
38
+ CompressionScoreCache,
39
+ find_duplicate_compression_scores,
40
+ )
41
  from services.video_scheduler.redis_utils import (
42
  get_redis_connection,
43
  get_organic_upscaling_queue_size,
 
1084
  f"{num_miners} miners in {scoring_batch_count} batches of up to {scoring_batch_size}"
1085
  )
1086
 
1087
+ compression_score_cache: CompressionScoreCache = {}
1088
  for batch_idx, batch_start in enumerate(range(0, total_scoring_responses, scoring_batch_size), start=1):
1089
  batch_end = min(batch_start + scoring_batch_size, total_scoring_responses)
1090
  batch_uids = flat_uids[batch_start:batch_end]
 
1117
  round_id,
1118
  distorted_urls=flat_distorted_urls[batch_start:batch_end],
1119
  duplicate_url_reasons=batch_duplicate_url_reasons,
1120
+ compression_score_cache=compression_score_cache,
1121
  )
1122
 
1123
  logger.info(
 
1337
  round_id: str,
1338
  distorted_urls: list[str] | None = None,
1339
  duplicate_url_reasons: dict[int, str] | None = None,
1340
+ compression_score_cache: CompressionScoreCache | None = None,
1341
  ):
1342
  if distorted_urls is None:
1343
  distorted_urls = []
 
1409
  lambda idx: duplicate_url_reasons[idx],
1410
  "No reason provided",
1411
  )
1412
+
1413
+ if compression_score_cache is None:
1414
+ compression_score_cache = {}
1415
+
1416
+ duplicate_score_owners = find_duplicate_compression_scores(
1417
+ uids,
1418
+ video_ids,
1419
+ vmaf_scores,
1420
+ base_vmaf_scores,
1421
+ vmaf_thresholds,
1422
+ compression_rates,
1423
+ final_scores,
1424
+ compression_score_cache,
1425
+ )
1426
+ for idx, first_uid in duplicate_score_owners.items():
1427
+ duplicate_reason = (
1428
+ "MINER FAILURE: duplicate synthetic compression score metrics "
1429
+ f"for input {video_ids[idx]}; matches earlier UID {first_uid}"
1430
+ )
1431
+ logger.warning(
1432
+ f"UID {uids[idx]}: {duplicate_reason}. Assigning score of 0."
1433
+ )
1434
+ final_scores[idx] = 0.0
1435
+ reasons[idx] = f"{duplicate_reason}; original scorer result: {reasons[idx]}"
1436
+
1437
+ if duplicate_score_owners:
1438
+ logger.info(
1439
+ f"Zeroed {len(duplicate_score_owners)} duplicate synthetic "
1440
+ "compression results based on exact scoring metrics"
1441
+ )
1442
 
1443
  logger.info(f"Updating miner manager with {len(compression_rates)} compression miner scores after synthetic requests processing")
1444
 
services/scoring/compression_score_cache.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections.abc import MutableMapping, Sequence
2
+ from typing import TypeAlias
3
+
4
+
5
+ CompressionScoreSignature: TypeAlias = tuple[
6
+ float,
7
+ float | None,
8
+ float,
9
+ float,
10
+ float,
11
+ ]
12
+ CompressionScoreCache: TypeAlias = MutableMapping[
13
+ str,
14
+ MutableMapping[CompressionScoreSignature, int],
15
+ ]
16
+
17
+
18
+ def find_duplicate_compression_scores(
19
+ uids: Sequence[int],
20
+ synthetic_input_ids: Sequence[str],
21
+ vmaf_scores: Sequence[float],
22
+ base_vmaf_scores: Sequence[float | None],
23
+ vmaf_thresholds: Sequence[float],
24
+ compression_rates: Sequence[float],
25
+ final_scores: Sequence[float],
26
+ cache: CompressionScoreCache,
27
+ ) -> dict[int, int]:
28
+ """Return result indices whose exact score signature was claimed by an earlier UID.
29
+
30
+ The cache is partitioned by synthetic input so two different challenges can
31
+ legitimately produce the same metrics. Non-positive results do not represent
32
+ a successful solution and therefore neither claim nor consume a slot.
33
+ """
34
+ lengths = {
35
+ len(uids),
36
+ len(synthetic_input_ids),
37
+ len(vmaf_scores),
38
+ len(base_vmaf_scores),
39
+ len(vmaf_thresholds),
40
+ len(compression_rates),
41
+ len(final_scores),
42
+ }
43
+ if len(lengths) != 1:
44
+ raise ValueError("Compression score fields must have equal lengths")
45
+
46
+ duplicate_owners: dict[int, int] = {}
47
+ for idx, (
48
+ uid,
49
+ synthetic_input_id,
50
+ vmaf_score,
51
+ base_vmaf_score,
52
+ vmaf_threshold,
53
+ compression_rate,
54
+ final_score,
55
+ ) in enumerate(
56
+ zip(
57
+ uids,
58
+ synthetic_input_ids,
59
+ vmaf_scores,
60
+ base_vmaf_scores,
61
+ vmaf_thresholds,
62
+ compression_rates,
63
+ final_scores,
64
+ )
65
+ ):
66
+ if not synthetic_input_id or final_score <= 0:
67
+ continue
68
+
69
+ signature: CompressionScoreSignature = (
70
+ vmaf_score,
71
+ base_vmaf_score,
72
+ vmaf_threshold,
73
+ compression_rate,
74
+ final_score,
75
+ )
76
+ input_cache = cache.setdefault(synthetic_input_id, {})
77
+ first_uid = input_cache.setdefault(signature, uid)
78
+ if first_uid != uid:
79
+ duplicate_owners[idx] = first_uid
80
+
81
+ return duplicate_owners
tests/test_compression_score_cache.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from services.scoring.compression_score_cache import (
4
+ find_duplicate_compression_scores,
5
+ )
6
+
7
+
8
+ class CompressionScoreCacheTests(unittest.TestCase):
9
+ def find_duplicates(
10
+ self,
11
+ cache,
12
+ uid,
13
+ input_id="video-1",
14
+ vmaf_score=85.89,
15
+ base_vmaf_score=88.22,
16
+ vmaf_threshold=85.0,
17
+ compression_rate=0.0907,
18
+ final_score=0.5399,
19
+ ):
20
+ return find_duplicate_compression_scores(
21
+ [uid],
22
+ [input_id],
23
+ [vmaf_score],
24
+ [base_vmaf_score],
25
+ [vmaf_threshold],
26
+ [compression_rate],
27
+ [final_score],
28
+ cache,
29
+ )
30
+
31
+ def test_subsequent_uid_is_duplicate_across_scorer_batches(self):
32
+ cache = {}
33
+
34
+ self.assertEqual(self.find_duplicates(cache, uid=83), {})
35
+ self.assertEqual(self.find_duplicates(cache, uid=41), {0: 83})
36
+
37
+ def test_subsequent_uid_loses_each_matching_synthetic_input_slot(self):
38
+ cache = {}
39
+ input_ids = [f"video-{idx}" for idx in range(5)]
40
+ vmaf_scores = [85.89, 85.16, 85.88, 85.13, 85.04]
41
+ base_vmaf_scores = [88.22, 87.50, 88.20, 87.46, 87.45]
42
+ compression_rates = [0.0907, 0.0708, 0.0905, 0.0713, 0.0787]
43
+ final_scores = [0.5399, 0.6341, 0.5409, 0.6307, 0.5887]
44
+
45
+ first_duplicates = find_duplicate_compression_scores(
46
+ [83] * 5,
47
+ input_ids,
48
+ vmaf_scores,
49
+ base_vmaf_scores,
50
+ [85.0] * 5,
51
+ compression_rates,
52
+ final_scores,
53
+ cache,
54
+ )
55
+ subsequent_duplicates = find_duplicate_compression_scores(
56
+ [41] * 5,
57
+ input_ids,
58
+ vmaf_scores,
59
+ base_vmaf_scores,
60
+ [85.0] * 5,
61
+ compression_rates,
62
+ final_scores,
63
+ cache,
64
+ )
65
+
66
+ self.assertEqual(first_duplicates, {})
67
+ self.assertEqual(subsequent_duplicates, {idx: 83 for idx in range(5)})
68
+
69
+ def test_same_metrics_for_different_inputs_get_separate_slots(self):
70
+ cache = {}
71
+
72
+ self.assertEqual(self.find_duplicates(cache, uid=83, input_id="video-1"), {})
73
+ self.assertEqual(self.find_duplicates(cache, uid=41, input_id="video-2"), {})
74
+
75
+ def test_any_different_metric_gets_a_separate_slot(self):
76
+ cache = {}
77
+
78
+ self.assertEqual(self.find_duplicates(cache, uid=83), {})
79
+ self.assertEqual(
80
+ self.find_duplicates(cache, uid=41, compression_rate=0.0908),
81
+ {},
82
+ )
83
+
84
+ def test_non_positive_result_does_not_claim_a_slot(self):
85
+ cache = {}
86
+
87
+ self.assertEqual(self.find_duplicates(cache, uid=83, final_score=0.0), {})
88
+ self.assertEqual(self.find_duplicates(cache, uid=41), {})
89
+ self.assertEqual(cache["video-1"].popitem()[1], 41)
90
+
91
+ def test_same_uid_can_reuse_its_own_signature(self):
92
+ cache = {}
93
+
94
+ self.assertEqual(self.find_duplicates(cache, uid=83), {})
95
+ self.assertEqual(self.find_duplicates(cache, uid=83), {})
96
+
97
+
98
+ if __name__ == "__main__":
99
+ unittest.main()