silvanus930 commited on
Commit ·
f0084c4
1
Parent(s): 8089b41
Category-aware eviction for the real-content-library capture cap
Browse filesPlain FIFO eviction (oldest file wins) lost the only samples of rare
(codec, threshold) categories mid-calibration more than once this session --
whatever combo wasn't actively flowing got evicted by whatever was, even
though one rare clip is worth more for calibration than one more of an
already well-represented category.
Reserves a minimum of 3 samples per category (needs >=2 to compute a
rate-distortion slope at all) and only evicts from categories already above
their own reserve, picking the globally oldest sample among the most
over-represented category. Verified against the live 80-clip library: with
av1/89.0 at 30 samples vs hevc/89.0 at 5, it correctly evicts from the
former, not blind oldest-first.
- neurons/miner.py +52 -3
neurons/miner.py
CHANGED
|
@@ -2,6 +2,7 @@ import asyncio
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
import posixpath
|
|
|
|
| 5 |
import shutil
|
| 6 |
import threading
|
| 7 |
import time
|
|
@@ -137,6 +138,16 @@ REAL_CONTENT_LIBRARY_PATH = Path(
|
|
| 137 |
REAL_CONTENT_LIBRARY_MAX_FILES = int(
|
| 138 |
os.getenv("MINER_REAL_CONTENT_LIBRARY_MAX_FILES", "80")
|
| 139 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
|
| 141 |
HOST_SHARED_VOLUME_PATH = Path(
|
| 142 |
os.getenv("MINER_SHARED_VOLUME_PATH")
|
|
@@ -600,12 +611,50 @@ class Miner(BaseMiner):
|
|
| 600 |
REAL_CONTENT_LIBRARY_PATH.glob("*.mp4"), key=lambda p: p.stat().st_mtime
|
| 601 |
)
|
| 602 |
while len(samples) > REAL_CONTENT_LIBRARY_MAX_FILES:
|
| 603 |
-
|
| 604 |
-
|
| 605 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 606 |
except Exception as e:
|
| 607 |
logger.warning(f"Failed to capture real content sample for {task_id}: {e}")
|
| 608 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 609 |
def _get_s3_client(self):
|
| 610 |
client_kwargs = {
|
| 611 |
"region_name": S3_REGION,
|
|
|
|
| 2 |
import json
|
| 3 |
import os
|
| 4 |
import posixpath
|
| 5 |
+
import re
|
| 6 |
import shutil
|
| 7 |
import threading
|
| 8 |
import time
|
|
|
|
| 138 |
REAL_CONTENT_LIBRARY_MAX_FILES = int(
|
| 139 |
os.getenv("MINER_REAL_CONTENT_LIBRARY_MAX_FILES", "80")
|
| 140 |
)
|
| 141 |
+
# Plain FIFO eviction lost the only samples of rare (codec, threshold)
|
| 142 |
+
# categories mid-calibration more than once in practice -- whatever combo
|
| 143 |
+
# wasn't actively flowing got evicted by whatever was, even though a single
|
| 144 |
+
# rare clip is worth far more for calibration than one more of an already
|
| 145 |
+
# well-represented category. Reserve a minimum per category (needs >=2 to
|
| 146 |
+
# ever compute a rate-distortion slope at all) and only evict from
|
| 147 |
+
# categories already above their own reserve.
|
| 148 |
+
REAL_CONTENT_LIBRARY_MIN_PER_CATEGORY = int(
|
| 149 |
+
os.getenv("MINER_REAL_CONTENT_LIBRARY_MIN_PER_CATEGORY", "3")
|
| 150 |
+
)
|
| 151 |
|
| 152 |
HOST_SHARED_VOLUME_PATH = Path(
|
| 153 |
os.getenv("MINER_SHARED_VOLUME_PATH")
|
|
|
|
| 611 |
REAL_CONTENT_LIBRARY_PATH.glob("*.mp4"), key=lambda p: p.stat().st_mtime
|
| 612 |
)
|
| 613 |
while len(samples) > REAL_CONTENT_LIBRARY_MAX_FILES:
|
| 614 |
+
victim = self._pick_eviction_victim(samples)
|
| 615 |
+
if victim is None:
|
| 616 |
+
# Every category is already at or below its reserve --
|
| 617 |
+
# can't evict without breaking that guarantee. Only
|
| 618 |
+
# reachable if distinct categories * reserve > cap (e.g.
|
| 619 |
+
# >26 categories at reserve=3, cap=80), which isn't
|
| 620 |
+
# expected in practice; fall back to plain oldest-first
|
| 621 |
+
# rather than growing unbounded.
|
| 622 |
+
victim = samples[0]
|
| 623 |
+
samples.remove(victim)
|
| 624 |
+
victim.unlink(missing_ok=True)
|
| 625 |
+
Path(f"{victim}.json").unlink(missing_ok=True)
|
| 626 |
except Exception as e:
|
| 627 |
logger.warning(f"Failed to capture real content sample for {task_id}: {e}")
|
| 628 |
|
| 629 |
+
@staticmethod
|
| 630 |
+
def _sample_category(mp4_path: Path) -> tuple[str, str]:
|
| 631 |
+
try:
|
| 632 |
+
meta = json.loads(Path(f"{mp4_path}.json").read_text())
|
| 633 |
+
return (str(meta.get("codec", "unknown")), str(meta.get("vmaf_threshold", "unknown")))
|
| 634 |
+
except Exception:
|
| 635 |
+
match = re.match(r".+_([a-zA-Z0-9]+)_thr([0-9.]+)\.mp4$", mp4_path.name)
|
| 636 |
+
if match:
|
| 637 |
+
return (match.group(1), match.group(2))
|
| 638 |
+
return ("unknown", "unknown")
|
| 639 |
+
|
| 640 |
+
@classmethod
|
| 641 |
+
def _pick_eviction_victim(cls, samples: list[Path]) -> Path | None:
|
| 642 |
+
counts: dict[tuple[str, str], int] = {}
|
| 643 |
+
for p in samples:
|
| 644 |
+
cat = cls._sample_category(p)
|
| 645 |
+
counts[cat] = counts.get(cat, 0) + 1
|
| 646 |
+
# Oldest sample belonging to whichever category is furthest above
|
| 647 |
+
# its own reserve, so the categories closest to their floor are
|
| 648 |
+
# protected first.
|
| 649 |
+
best: Path | None = None
|
| 650 |
+
best_surplus = 0
|
| 651 |
+
for p in samples: # already sorted oldest-first
|
| 652 |
+
surplus = counts[cls._sample_category(p)] - REAL_CONTENT_LIBRARY_MIN_PER_CATEGORY
|
| 653 |
+
if surplus > best_surplus:
|
| 654 |
+
best = p
|
| 655 |
+
best_surplus = surplus
|
| 656 |
+
return best
|
| 657 |
+
|
| 658 |
def _get_s3_client(self):
|
| 659 |
client_kwargs = {
|
| 660 |
"region_name": S3_REGION,
|