code-HFD / scripts /data /build_continuous_audio_command_sources.py
Tinghong-Ye22's picture
Add files using upload-large-folder tool
9fc9873 verified
Raw History Blame Contribute Delete
27.2 kB
#!/usr/bin/env python3
"""Build continuous-background HFD speech WAVs and their exact time mapping.
This is the CPU preparation stage. It deliberately does not run Qwen. Every
mixed WAV is made from one uninterrupted background session, with speech
starting 0.2 seconds after the first 0.6-second listen window begins. The
following GPU stage can therefore reuse ``cache_omni_hidden_states.py``
unchanged for both mixed speech and pure background windows.
"""
from __future__ import annotations
import argparse
import hashlib
import json
import math
import os
import sqlite3
import sys
import wave
from concurrent.futures import ThreadPoolExecutor
from pathlib import Path
from typing import Any
import numpy as np
import pandas as pd
from scipy.signal import resample_poly
PROJECT_ROOT = Path(__file__).resolve().parents[2]
if str(PROJECT_ROOT) not in sys.path:
sys.path.insert(0, str(PROJECT_ROOT))
SAMPLE_RATE = 16_000
WINDOW_SECONDS = 0.6
STRIDE_SECONDS = 0.4
WINDOW_SAMPLES = int(round(WINDOW_SECONDS * SAMPLE_RATE))
STRIDE_SAMPLES = int(round(STRIDE_SECONDS * SAMPLE_RATE))
RTC_PREFIX_SAMPLES = WINDOW_SAMPLES - STRIDE_SAMPLES
BACKGROUND_SOURCE_NAME = "hfd_continuous_office_background"
SPEECH_SOURCE_NAME = "qwen3_tts_seed_audio_command"
TASK_NAME = "audio_command_text"
FORMAT = "hfd_continuous_background_assignment_v1"
def _normalise_audio_dir(value: str) -> str:
parts = Path(str(value).replace("\\", "/")).parts
try:
start = parts.index("seed_audio")
except ValueError:
return Path(str(value)).as_posix().strip("/")
return Path(*parts[start:]).as_posix()
def _write_json_atomic(path: Path, payload: Any) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
temporary = path.with_name(f".{path.name}.{os.getpid()}.tmp")
temporary.write_text(
json.dumps(payload, ensure_ascii=False, indent=2) + "\n",
encoding="utf-8",
)
os.replace(temporary, path)
def _write_jsonl_atomic(path: Path, rows: list[dict[str, Any]]) -> None:
path.parent.mkdir(parents=True, exist_ok=True)
temporary = path.with_name(f".{path.name}.{os.getpid()}.tmp")
with temporary.open("w", encoding="utf-8") as handle:
for row in rows:
handle.write(json.dumps(row, ensure_ascii=False) + "\n")
os.replace(temporary, path)
def _read_pcm16(path: Path) -> np.ndarray:
with wave.open(str(path), "rb") as handle:
if (
handle.getframerate() != SAMPLE_RATE
or handle.getnchannels() != 1
or handle.getsampwidth() != 2
or handle.getcomptype() != "NONE"
):
raise ValueError(f"Expected mono PCM16 {SAMPLE_RATE} Hz WAV: {path}")
return np.frombuffer(
handle.readframes(handle.getnframes()),
dtype="<i2",
).copy()
def _read_speech_float(path: Path) -> np.ndarray:
"""Decode the known mono PCM16 TTS corpus without one ffmpeg process/file."""
with wave.open(str(path), "rb") as handle:
channels = int(handle.getnchannels())
sample_width = int(handle.getsampwidth())
source_rate = int(handle.getframerate())
compression = str(handle.getcomptype())
frames = int(handle.getnframes())
raw = handle.readframes(frames)
if (
channels != 1
or sample_width != 2
or source_rate <= 0
or compression != "NONE"
):
raise ValueError(
"Expected uncompressed mono PCM16 TTS WAV: "
f"path={path} channels={channels} width={sample_width} "
f"rate={source_rate} compression={compression}"
)
speech = (
np.frombuffer(raw, dtype="<i2").astype(np.float32) / np.float32(32768.0)
)
if source_rate != SAMPLE_RATE:
divisor = math.gcd(source_rate, SAMPLE_RATE)
speech = resample_poly(
speech,
SAMPLE_RATE // divisor,
source_rate // divisor,
).astype(np.float32, copy=False)
expected = int(round(frames * SAMPLE_RATE / float(source_rate)))
if speech.size > expected:
speech = speech[:expected]
elif speech.size < expected:
speech = np.pad(speech, (0, expected - speech.size))
if speech.size <= 0 or not np.isfinite(speech).all():
raise RuntimeError(f"Invalid decoded speech source: {path}")
return np.ascontiguousarray(speech, dtype=np.float32)
def _write_pcm16_atomic(path: Path, pcm: np.ndarray) -> None:
value = np.asarray(pcm, dtype="<i2").reshape(-1)
path.parent.mkdir(parents=True, exist_ok=True)
temporary = path.with_name(f".{path.name}.{os.getpid()}.tmp")
with wave.open(str(temporary), "wb") as handle:
handle.setnchannels(1)
handle.setsampwidth(2)
handle.setframerate(SAMPLE_RATE)
handle.writeframes(value.tobytes())
os.replace(temporary, path)
def _sha256(path: Path) -> str:
digest = hashlib.sha256()
with path.open("rb") as handle:
for chunk in iter(lambda: handle.read(1024 * 1024), b""):
digest.update(chunk)
return digest.hexdigest()
def _load_valid_sources(
hidden_root: Path,
fallback_assignment_path: Path | None = None,
) -> dict[str, int]:
index_dir = (
hidden_root
/ "audio"
/ SPEECH_SOURCE_NAME
/ TASK_NAME
/ "sources"
)
result: dict[str, int] = {}
for index_path in sorted(index_dir.glob("index.ws*.rank*.sqlite")):
connection = sqlite3.connect(
f"file:{index_path}?mode=ro",
uri=True,
)
try:
rows = connection.execute(
"SELECT key, count FROM sources ORDER BY key"
).fetchall()
finally:
connection.close()
for key, count in rows:
result[str(key)] = int(count)
if result:
return result
# The legacy cache is only a one-time migration whitelist. Once the new
# production cache has passed validation it may be deleted, so retain the
# exact selected source set in the new assignment manifest and allow
# deterministic waveform rebuilding from that manifest.
if fallback_assignment_path is not None and fallback_assignment_path.is_file():
with fallback_assignment_path.open("r", encoding="utf-8") as handle:
for line in handle:
if not line.strip():
continue
row = json.loads(line)
key = str(row.get("source_key", "")).strip()
count = int(row.get("listen_window_count", 0))
if not key or count <= 0:
raise RuntimeError(
"Invalid fallback source assignment row in "
f"{fallback_assignment_path}"
)
previous = result.get(key)
if previous is not None and previous != count:
raise RuntimeError(
f"Conflicting fallback source count for {key}: "
f"{previous} != {count}"
)
result[key] = count
if result:
print(
"[source-list] Reusing the validated source set from "
f"{fallback_assignment_path}",
flush=True,
)
return result
raise RuntimeError(
"No valid HFD speech source index and no reusable assignment manifest: "
f"legacy_index={index_dir} fallback={fallback_assignment_path}"
)
def _load_motion_frames(metadata_csv: Path) -> dict[str, int]:
table = pd.read_csv(
metadata_csv,
usecols=lambda column: column in {"filename", "move_duration_frames"},
)
result: dict[str, int] = {}
for row in table.itertuples(index=False):
filename = str(getattr(row, "filename", "")).strip()
value = getattr(row, "move_duration_frames", 0)
if not filename:
continue
try:
result[filename] = int(value) if not pd.isna(value) else 0
except (TypeError, ValueError):
result[filename] = 0
return result
def _load_training_rows(
manifest_path: Path,
valid_directories: set[str],
motion_frames: dict[str, int],
) -> tuple[list[dict[str, Any]], dict[str, int]]:
rows: list[dict[str, Any]] = []
max_frames_by_directory: dict[str, int] = {}
with manifest_path.open("r", encoding="utf-8") as handle:
for line in handle:
if not line.strip():
continue
row = json.loads(line)
audio_dir = _normalise_audio_dir(str(row.get("audio_path", "")))
if audio_dir not in valid_directories:
continue
normalized = dict(row)
normalized["audio_path"] = audio_dir
rows.append(normalized)
filename = str(row.get("filename", "")).strip()
if filename in motion_frames:
max_frames_by_directory[audio_dir] = max(
max_frames_by_directory.get(audio_dir, 0),
int(motion_frames[filename]),
)
missing = sorted(valid_directories - set(max_frames_by_directory))
if missing:
raise RuntimeError(
"Valid speech directories have no matching motion metadata: "
f"count={len(missing)} first={missing[:5]}"
)
return rows, max_frames_by_directory
def _load_sessions(manifest_path: Path) -> list[dict[str, Any]]:
payload = json.loads(manifest_path.read_text(encoding="utf-8"))
if payload.get("format") != "hfd_continuous_background_sessions_v1":
raise RuntimeError(f"Unsupported background manifest: {manifest_path}")
sessions: list[dict[str, Any]] = []
for raw in payload.get("sessions", []):
row = dict(raw)
path = Path(str(row["path"])).expanduser().resolve()
pcm = _read_pcm16(path)
expected_count = (
0
if pcm.size < WINDOW_SAMPLES
else (pcm.size - WINDOW_SAMPLES) // STRIDE_SAMPLES + 1
)
if int(row.get("window_count", -1)) != expected_count:
raise RuntimeError(
f"Background window count mismatch for {path}: "
f"manifest={row.get('window_count')} actual={expected_count}"
)
row["path"] = str(path)
row["pcm"] = pcm
row["window_count"] = expected_count
sessions.append(row)
if not sessions:
raise RuntimeError(f"No background sessions in {manifest_path}")
return sessions
def _listen_window_count(speech_samples: int) -> int:
# Speech begins after the 0.2-second RTC/background prefix. Keep every
# window whose start precedes the final speech sample.
return int(
math.ceil(
(RTC_PREFIX_SAMPLES + int(speech_samples))
/ float(STRIDE_SAMPLES)
)
)
def _select_background_slot(
*,
source_key: str,
listen_windows: int,
max_motion_steps: int,
sessions: list[dict[str, Any]],
initial_idle_max_steps: int,
safety_steps: int,
seed: int,
) -> tuple[dict[str, Any], int]:
choices: list[tuple[dict[str, Any], int, int]] = []
total_slots = 0
required_after_start = (
int(listen_windows) + int(max_motion_steps) + int(safety_steps)
)
for session in sessions:
max_start = int(session["window_count"]) - required_after_start
count = max_start - int(initial_idle_max_steps) + 1
if count <= 0:
continue
choices.append((session, int(initial_idle_max_steps), count))
total_slots += count
if total_slots <= 0:
raise RuntimeError(
"No continuous background can cover source: "
f"key={source_key} listen={listen_windows} "
f"motion={max_motion_steps} idle={initial_idle_max_steps}"
)
digest = hashlib.sha256(
f"{int(seed)}::{source_key}".encode("utf-8")
).digest()
slot = int.from_bytes(digest[:8], "big") % total_slots
for session, first_start, count in choices:
if slot < count:
return session, first_start + slot
slot -= count
raise AssertionError("Background slot selection fell through")
def _build_one_source(
*,
source_key: str,
speech_root: Path,
mixed_root: Path,
max_motion_frames: int,
sessions: list[dict[str, Any]],
initial_idle_max_steps: int,
stationary_blend_frames: int,
safety_steps: int,
seed: int,
) -> dict[str, Any]:
original_path = speech_root / f"{source_key}.wav"
speech = _read_speech_float(original_path)
if speech.size <= 0:
raise RuntimeError(f"Empty speech source: {original_path}")
listen_windows = _listen_window_count(int(speech.size))
max_motion_steps = int(
math.ceil(
(
max(int(max_motion_frames), 1)
+ max(int(stationary_blend_frames) - 1, 1)
)
/ 20.0
)
)
session, listen_start = _select_background_slot(
source_key=source_key,
listen_windows=listen_windows,
max_motion_steps=max_motion_steps,
sessions=sessions,
initial_idle_max_steps=initial_idle_max_steps,
safety_steps=safety_steps,
seed=seed,
)
total_samples = (
(listen_windows - 1) * STRIDE_SAMPLES + WINDOW_SAMPLES
)
background_start = listen_start * STRIDE_SAMPLES
background_pcm = np.asarray(session["pcm"], dtype="<i2")[
background_start : background_start + total_samples
].copy()
if background_pcm.size != total_samples:
raise RuntimeError(
f"Background ended inside mixed source: {source_key}"
)
speech_peak = float(np.max(np.abs(speech), initial=0.0))
peak_limit = 10.0 ** (-3.0 / 20.0)
speech_scale = (
min(1.0, peak_limit / speech_peak)
if speech_peak > 0.0
else 1.0
)
mixed_pcm = background_pcm.copy()
speech_start = RTC_PREFIX_SAMPLES
speech_end = speech_start + int(speech.size)
background_float = (
background_pcm[speech_start:speech_end].astype(np.float32)
/ 32768.0
)
mixed_float = background_float + speech * np.float32(speech_scale)
if float(np.max(np.abs(mixed_float), initial=0.0)) >= 1.0:
raise RuntimeError(
f"Unsafe mixed peak for {source_key}; background must remain fixed"
)
mixed_pcm[speech_start:speech_end] = np.rint(
mixed_float * 32768.0
).clip(-32768, 32767).astype("<i2")
# Prove both phase boundaries use exactly the same PCM samples.
if not np.array_equal(
mixed_pcm[:RTC_PREFIX_SAMPLES],
np.asarray(session["pcm"])[
background_start : background_start + RTC_PREFIX_SAMPLES
],
):
raise RuntimeError(f"Idle/listen overlap mismatch for {source_key}")
next_background_start = (
listen_start + listen_windows
) * STRIDE_SAMPLES
if not np.array_equal(
mixed_pcm[-RTC_PREFIX_SAMPLES:],
np.asarray(session["pcm"])[
next_background_start :
next_background_start + RTC_PREFIX_SAMPLES
],
):
raise RuntimeError(f"Listen/motion overlap mismatch for {source_key}")
mixed_path = mixed_root / f"{source_key}.wav"
_write_pcm16_atomic(mixed_path, mixed_pcm)
return {
"format": FORMAT,
"source_key": source_key,
"audio_dir": Path(source_key).parent.as_posix(),
"original_audio_path": str(original_path.resolve()),
"mixed_audio_path": str(mixed_path.resolve()),
"mixed_audio_sha256": _sha256(mixed_path),
"speech_samples": int(speech.size),
"speech_scale": float(speech_scale),
"speech_offset_samples": RTC_PREFIX_SAMPLES,
"listen_window_count": listen_windows,
"background_key": str(session["key"]),
"background_sha256": str(session["sha256"]),
"background_window_count": int(session["window_count"]),
"background_listen_start_index": int(listen_start),
"initial_idle_max_steps": int(initial_idle_max_steps),
"max_motion_steps": max_motion_steps,
"safety_steps": int(safety_steps),
}
def _source_configs(
*,
output_root: Path,
mixed_manifest: Path,
background_manifest: Path,
) -> tuple[dict[str, Any], dict[str, Any]]:
mixed = {
"sources": [
{
"name": SPEECH_SOURCE_NAME,
"modality": "audio",
"dataset_root": str((output_root / "mixed_waveforms").resolve()),
"manifest_path": str(mixed_manifest.resolve()),
"allowed_tasks": [TASK_NAME],
"instruction_key": "content_short_description",
"audio_path_key": "audio_path",
"is_audio_dir_manifest": True,
"audio_file_extensions": [".wav"],
}
]
}
background = {
"sources": [
{
"name": BACKGROUND_SOURCE_NAME,
"modality": "audio",
"dataset_root": str((output_root / "background").resolve()),
"manifest_path": str(background_manifest.resolve()),
"allowed_tasks": [TASK_NAME],
"audio_path_key": "audio_path",
"is_audio_dir_manifest": False,
"audio_file_extensions": [".wav"],
}
]
}
return mixed, background
def build(args: argparse.Namespace) -> None:
output_root = args.output_root.expanduser().resolve()
manifests = output_root / "manifests"
mixed_root = output_root / "mixed_waveforms"
valid_sources = _load_valid_sources(
args.old_hidden_root.expanduser().resolve(),
manifests / "source_background_assignments.jsonl",
)
source_keys = sorted(valid_sources)
if int(args.limit_sources) > 0:
source_keys = source_keys[: int(args.limit_sources)]
valid_directories = {
Path(source_key).parent.as_posix()
for source_key in source_keys
}
motion_frames = _load_motion_frames(args.motion_metadata.expanduser().resolve())
training_rows, max_frames = _load_training_rows(
args.speech_manifest.expanduser().resolve(),
valid_directories,
motion_frames,
)
sessions = _load_sessions(args.background_manifest.expanduser().resolve())
def worker(source_key: str) -> dict[str, Any]:
audio_dir = Path(source_key).parent.as_posix()
return _build_one_source(
source_key=source_key,
speech_root=args.speech_root.expanduser().resolve(),
mixed_root=mixed_root,
max_motion_frames=max_frames[audio_dir],
sessions=sessions,
initial_idle_max_steps=int(args.initial_idle_max_steps),
stationary_blend_frames=int(args.stationary_blend_frames),
safety_steps=int(args.safety_steps),
seed=int(args.seed),
)
with ThreadPoolExecutor(max_workers=max(int(args.workers), 1)) as executor:
assignments = list(executor.map(worker, source_keys))
assignments.sort(key=lambda row: str(row["source_key"]))
assignment_path = manifests / "source_background_assignments.jsonl"
mixed_manifest = manifests / "mixed_metadata.jsonl"
background_jsonl = manifests / "background_sessions.jsonl"
_write_jsonl_atomic(assignment_path, assignments)
_write_jsonl_atomic(mixed_manifest, training_rows)
_write_jsonl_atomic(
background_jsonl,
[
{
"filename": str(session["key"]),
"audio_path": str(session["relative_path"]),
}
for session in sessions
],
)
mixed_config, background_config = _source_configs(
output_root=output_root,
mixed_manifest=mixed_manifest,
background_manifest=background_jsonl,
)
_write_json_atomic(manifests / "mixed_source_config.json", mixed_config)
_write_json_atomic(
manifests / "background_source_config.json",
background_config,
)
assignment_sha256 = _sha256(assignment_path)
report = {
"format": FORMAT,
"output_root": str(output_root),
"source_count": len(assignments),
"mixed_window_count": sum(
int(row["listen_window_count"]) for row in assignments
),
"background_session_count": len(sessions),
"background_window_count": sum(
int(session["window_count"]) for session in sessions
),
"assignment_path": str(assignment_path),
"assignment_sha256": assignment_sha256,
"initial_idle_max_steps": int(args.initial_idle_max_steps),
"stationary_blend_frames": int(args.stationary_blend_frames),
"safety_steps": int(args.safety_steps),
"seed": int(args.seed),
"limit_sources": int(args.limit_sources),
}
_write_json_atomic(output_root / "reports" / "waveform_build.json", report)
print(
"[ready] "
f"sources={report['source_count']:,} "
f"mixed_windows={report['mixed_window_count']:,} "
f"background_windows={report['background_window_count']:,} "
f"output={output_root}",
flush=True,
)
def verify(args: argparse.Namespace) -> None:
output_root = args.output_root.expanduser().resolve()
report_path = output_root / "reports" / "waveform_build.json"
report = json.loads(report_path.read_text(encoding="utf-8"))
assignment_path = Path(str(report["assignment_path"]))
if _sha256(assignment_path) != report["assignment_sha256"]:
raise RuntimeError("Assignment manifest changed after waveform build")
sessions = {
str(row["key"]): row
for row in _load_sessions(args.background_manifest.expanduser().resolve())
}
with assignment_path.open("r", encoding="utf-8") as handle:
assignments = [
json.loads(line)
for line in handle
if line.strip()
]
def verify_one(row: dict[str, Any]) -> None:
session = sessions[str(row["background_key"])]
mixed = _read_pcm16(Path(str(row["mixed_audio_path"])))
start = int(row["background_listen_start_index"]) * STRIDE_SAMPLES
expected = np.asarray(session["pcm"])[start : start + mixed.size]
speech_start = int(row["speech_offset_samples"])
speech_end = speech_start + int(row["speech_samples"])
speech = _read_speech_float(Path(str(row["original_audio_path"])))
if speech.size != int(row["speech_samples"]):
raise RuntimeError(
f"Speech length changed after build: {row['source_key']}"
)
speech_peak = float(np.max(np.abs(speech), initial=0.0))
expected_scale = (
min(1.0, (10.0 ** (-3.0 / 20.0)) / speech_peak)
if speech_peak > 0.0
else 1.0
)
if not math.isclose(
float(row["speech_scale"]),
expected_scale,
rel_tol=0.0,
abs_tol=1e-12,
):
raise RuntimeError(
f"Speech scale changed after build: {row['source_key']}"
)
if not np.array_equal(mixed[:speech_start], expected[:speech_start]):
raise RuntimeError(
f"Broken idle/listen boundary: {row['source_key']}"
)
if not np.array_equal(mixed[speech_end:], expected[speech_end:]):
raise RuntimeError(
f"Broken listen/motion background: {row['source_key']}"
)
expected_speech = (
expected[speech_start:speech_end].astype(np.float32)
/ np.float32(32768.0)
+ speech * np.float32(expected_scale)
)
expected_speech_pcm = np.rint(
expected_speech * 32768.0
).clip(-32768, 32767).astype("<i2")
if not np.array_equal(
mixed[speech_start:speech_end],
expected_speech_pcm,
):
raise RuntimeError(
f"Broken waveform-level speech/background mix: "
f"{row['source_key']}"
)
if (
int(row["background_listen_start_index"])
+ int(row["listen_window_count"])
+ int(row["max_motion_steps"])
+ int(row["safety_steps"])
> int(row["background_window_count"])
):
raise RuntimeError(
f"Background capacity is insufficient: {row['source_key']}"
)
with ThreadPoolExecutor(max_workers=max(int(args.workers), 1)) as executor:
count = sum(1 for _ in executor.map(verify_one, assignments))
if count != int(report["source_count"]):
raise RuntimeError(
f"Assignment count mismatch: verified={count} report={report['source_count']}"
)
print(
f"[verified] {count:,} continuous mixed WAV sources under {output_root}",
flush=True,
)
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--mode", choices=("build", "verify"), default="build")
parser.add_argument("--output-root", type=Path, required=True)
parser.add_argument("--background-manifest", type=Path, required=True)
parser.add_argument(
"--speech-root",
type=Path,
default=Path("/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/buwendong/user/code/qwen3-tts"),
)
parser.add_argument(
"--speech-manifest",
type=Path,
default=Path("/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/buwendong/user/code/qwen3-tts/metadata.jsonl"),
)
parser.add_argument(
"--motion-metadata",
type=Path,
default=Path(
"/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/code/Psi0/dataset/seed_all/"
"metadata/seed_metadata_v004.csv"
),
)
parser.add_argument(
"--old-hidden-root",
type=Path,
default=Path(
"/cpfs01/thscc/sharestorage/AIGC-06-01/Hithink_VideoGen_Group/user_workspace/yetinghong/videogen/hfd_audio_command_source"
),
help=(
"One-time read-only whitelist of the 41,694 validated WAV sources. "
"After the first successful build, the new assignment manifest is "
"used as an exact fallback so this legacy cache can be removed."
),
)
parser.add_argument("--workers", type=int, default=32)
parser.add_argument("--limit-sources", type=int, default=0)
parser.add_argument("--initial-idle-max-steps", type=int, default=5)
parser.add_argument("--stationary-blend-frames", type=int, default=20)
parser.add_argument("--safety-steps", type=int, default=10)
parser.add_argument("--seed", type=int, default=260730)
return parser.parse_args()
def main() -> None:
args = parse_args()
if args.mode == "build":
build(args)
else:
verify(args)
if __name__ == "__main__":
main()