Dan commited on
Commit
17c1c5f
·
1 Parent(s): e8dec6b

Cap feature audio decode concurrency

Browse files
osu_feature_pipeline/README.md CHANGED
@@ -56,9 +56,9 @@ for its chunk. Shard writes still happen in the parent process, preserving the
56
  single commit boundary. The executor keeps only a bounded set of chunk futures
57
  in flight, so completed chunks cannot accumulate behind the writer.
58
 
59
- `--audio-workers` controls ffmpeg/audio extraction inside each active chunk.
60
- Audio blobs are claimed once across all workers, so shared audio is not decoded
61
- or written twice.
62
 
63
  `--row-group-size` controls the maximum rows converted and written to Parquet
64
  at once. The default is `65536`. Closed shard size is still controlled by
@@ -131,6 +131,11 @@ FEATURES_PARENT="$(dirname "$FEATURES_ROOT")"
131
 
132
  rm -rf "$FEATURES_PARENT/.v1.scratch"
133
  rm -f "$FEATURES_PARENT/.v1.feature.lock"
 
 
 
 
 
134
  ```
135
 
136
  Only remove the lock after `pgrep` shows no active feature job. If
 
56
  single commit boundary. The executor keeps only a bounded set of chunk futures
57
  in flight, so completed chunks cannot accumulate behind the writer.
58
 
59
+ `--audio-workers` controls ffmpeg/audio extraction across the whole process.
60
+ It is not a per-chunk multiplier. Audio blobs are claimed
61
+ once across all workers, so shared audio is not decoded or written twice.
62
 
63
  `--row-group-size` controls the maximum rows converted and written to Parquet
64
  at once. The default is `65536`. Closed shard size is still controlled by
 
131
 
132
  rm -rf "$FEATURES_PARENT/.v1.scratch"
133
  rm -f "$FEATURES_PARENT/.v1.feature.lock"
134
+ find /tmp -maxdepth 1 -type f \( \
135
+ -name 'tmp*mp3' -o -name 'tmp*ogg' -o -name 'tmp*wav' -o \
136
+ -name 'tmp*flac' -o -name 'tmp*m4a' -o -name 'tmp*aac' -o \
137
+ -name 'tmp*f32' \
138
+ \) -delete
139
  ```
140
 
141
  Only remove the lock after `pgrep` shows no active feature job. If
osu_feature_pipeline/osu_features/audio.py CHANGED
@@ -58,33 +58,36 @@ def decode_audio(audio_bytes: bytes, suffix: str, cfg: AudioConfig) -> tuple[np.
58
  with tempfile.NamedTemporaryFile(suffix=suffix or ".audio", delete=False) as tmp:
59
  tmp.write(audio_bytes)
60
  tmp_path = Path(tmp.name)
 
 
61
  try:
62
- proc = subprocess.run(
63
- [
64
- ffmpeg,
65
- "-hide_banner",
66
- "-loglevel",
67
- "error",
68
- "-i",
69
- str(tmp_path),
70
- "-f",
71
- "f32le",
72
- "-acodec",
73
- "pcm_f32le",
74
- "-ac",
75
- "1",
76
- "-ar",
77
- str(cfg.sample_rate),
78
- "-",
79
- ],
80
- stdout=subprocess.PIPE,
81
- stderr=subprocess.PIPE,
82
- check=False,
83
- )
 
84
  if proc.returncode != 0:
85
  err = proc.stderr.decode("utf-8", "replace").strip().splitlines()
86
  return None, "ffmpeg_decode_failed:" + (err[-1] if err else str(proc.returncode))
87
- y = np.frombuffer(proc.stdout, dtype=np.float32).copy()
88
  if y.size == 0:
89
  return None, "empty_audio"
90
  return np.nan_to_num(y, nan=0.0, posinf=0.0, neginf=0.0), "ok"
@@ -93,6 +96,10 @@ def decode_audio(audio_bytes: bytes, suffix: str, cfg: AudioConfig) -> tuple[np.
93
  tmp_path.unlink()
94
  except FileNotFoundError:
95
  pass
 
 
 
 
96
 
97
 
98
  def _frame_audio(y: np.ndarray, n_fft: int, hop: int) -> np.ndarray:
@@ -298,4 +305,3 @@ def summarize_pool(rows: list[dict[str, Any]] | None, start_ms: int, end_ms: int
298
  "audio_onset_mean": float(np.mean([r["onset_mean"] for r in selected])),
299
  "audio_spectral_centroid_mean": float(np.mean([r["spectral_centroid_mean"] for r in selected])),
300
  }
301
-
 
58
  with tempfile.NamedTemporaryFile(suffix=suffix or ".audio", delete=False) as tmp:
59
  tmp.write(audio_bytes)
60
  tmp_path = Path(tmp.name)
61
+ with tempfile.NamedTemporaryFile(suffix=".f32", delete=False) as raw_tmp:
62
+ raw_path = Path(raw_tmp.name)
63
  try:
64
+ with raw_path.open("wb") as raw:
65
+ proc = subprocess.run(
66
+ [
67
+ ffmpeg,
68
+ "-hide_banner",
69
+ "-loglevel",
70
+ "error",
71
+ "-i",
72
+ str(tmp_path),
73
+ "-f",
74
+ "f32le",
75
+ "-acodec",
76
+ "pcm_f32le",
77
+ "-ac",
78
+ "1",
79
+ "-ar",
80
+ str(cfg.sample_rate),
81
+ "-",
82
+ ],
83
+ stdout=raw,
84
+ stderr=subprocess.PIPE,
85
+ check=False,
86
+ )
87
  if proc.returncode != 0:
88
  err = proc.stderr.decode("utf-8", "replace").strip().splitlines()
89
  return None, "ffmpeg_decode_failed:" + (err[-1] if err else str(proc.returncode))
90
+ y = np.fromfile(raw_path, dtype=np.float32)
91
  if y.size == 0:
92
  return None, "empty_audio"
93
  return np.nan_to_num(y, nan=0.0, posinf=0.0, neginf=0.0), "ok"
 
96
  tmp_path.unlink()
97
  except FileNotFoundError:
98
  pass
99
+ try:
100
+ raw_path.unlink()
101
+ except FileNotFoundError:
102
+ pass
103
 
104
 
105
  def _frame_audio(y: np.ndarray, n_fft: int, hop: int) -> np.ndarray:
 
305
  "audio_onset_mean": float(np.mean([r["onset_mean"] for r in selected])),
306
  "audio_spectral_centroid_mean": float(np.mean([r["spectral_centroid_mean"] for r in selected])),
307
  }
 
osu_feature_pipeline/osu_features/builder.py CHANGED
@@ -180,6 +180,7 @@ class FeatureBuilder:
180
  self.audio_status_cache: dict[str, str] = {}
181
  self.audio_pool_cache: dict[str, list[dict[str, Any]]] = {}
182
  self.audio_cache_blobs: set[str] = set()
 
183
  self.audio_condition = threading.Condition()
184
  self.audio_progress: Any | None = None
185
  self.audio_status: dict[str, str] = {}
@@ -790,7 +791,8 @@ class FeatureBuilder:
790
 
791
  def run_one_safe(blob: str) -> tuple[str, dict[str, Any], list[dict[str, Any]], list[dict[str, Any]], str]:
792
  try:
793
- return run_one(blob)
 
794
  except Exception as exc:
795
  status = f"audio_error:{type(exc).__name__}:{exc}"
796
  return blob, missing_summary(blob, sources.get(blob), status), [], [], status
 
180
  self.audio_status_cache: dict[str, str] = {}
181
  self.audio_pool_cache: dict[str, list[dict[str, Any]]] = {}
182
  self.audio_cache_blobs: set[str] = set()
183
+ self.audio_decode_semaphore = threading.BoundedSemaphore(max(1, config.audio_workers))
184
  self.audio_condition = threading.Condition()
185
  self.audio_progress: Any | None = None
186
  self.audio_status: dict[str, str] = {}
 
791
 
792
  def run_one_safe(blob: str) -> tuple[str, dict[str, Any], list[dict[str, Any]], list[dict[str, Any]], str]:
793
  try:
794
+ with self.audio_decode_semaphore:
795
+ return run_one(blob)
796
  except Exception as exc:
797
  status = f"audio_error:{type(exc).__name__}:{exc}"
798
  return blob, missing_summary(blob, sources.get(blob), status), [], [], status