[Feature Share / Mod] Low-VRAM Symbolic Best-of-N (Bo4/Bo8) Score Ranking Loop for YuE2
Leave a light on by the water 5.mp3
Hello! I wanted to share a simple, lightweight modification for pipeline.py that implements a Best-of-N (BoN) symbolic score-ranking loop for the YuE2 pipeline.
Instead of running heavy acoustic audio synthesis for multiple parallel variations (which easily crashes lower-VRAM cards like a 6GB laptop GPU), this tweak intercepts the generation pipeline early during the fast, text-based ABC musical notation phase. It drafts N independent text scores in system memory using cycled random seeds, filters out corrupt or un-orchestrated sheets, selects the single most structurally complete layout, and passes only that gold-standard candidate to the slow audio synthesis phase.
This brings the statistical selection benefits of YuE's native Best-of-8 benchmark straight to resource-constrained local machines without the massive time or hardware penalty of baking multiple audio tracks.
Prerequisites:
Ensure symusic is installed in your virtual environment to handle advanced score parsing workflows:
bash
pip install symusic
Implementation Steps in pipeline.py:
(Wan2GP implementation of YuE2) Open models/TTS/yue2/pipeline.py and:
- Insert the following line in the import section (after Line 19):
import random
- replace lines 128–130 (inside the core generation block),
else:
abc_ids = self._tokens(token_prefixes(request, self.tokenizer), self.generation_config.abc, seed, "abc", callback)
abc = self.tokenizer.decode(abc_ids)with the following optimized snippet:
else:
N = 4 # Best-of-N (BoN) limit (Scale up to 8 or 16 on RTX 3060/3080 workstations)
# print(f"************ ENTERING SYMBOLIC BEST-OF-8 SCORE RANKING MODE ************")
# print(f"************ ENTERING SYMBOLIC BEST-OF-16 SCORE RANKING MODE ************")
print(f"************ ENTERING SYMBOLIC BEST-OF-{N} SCORE RANKING MODE ************")
candidate_runs = []
# Scan style prompt for instrumental keywords
is_instrumental = any(k in alt_prompt.lower() for k in ["instrumental", "no vocal", "no vocals", "backing track"])
# Compose 8 independent symbolic sheet music score candidates
# for i in range(8):
# for i in range(16):
# for i in range(4):
for i in range(N):
if self._abort_requested():
# raise InterruptedError("YuE2 Best-of-8 score planning interrupted.")
# raise InterruptedError("YuE2 Best-of-16 score planning interrupted.")
raise InterruptedError(f"YuE2 Best-of-{N} score planning interrupted.")
# print(f"[Bo8 Ranker] Drafting symbolic musical notation candidate {i+1}/8...")
# print(f"[Bo16 Ranker] Drafting symbolic musical notation candidate {i+1}/16...")
print(f"[Bo{N} Ranker] Drafting symbolic musical notation candidate {i+1}/{N}...")
# Cycle random seeds to generate distinct structural compositions
# Dynamically draw a fresh, secure seed tracking coordinate within YuE2 parameters
# This guarantees maximum variance while shielding against overflow crashes
run_seed = random.randint(0, 999999999)
# Generate the lightweight text token sheet music array
tokens_draft = self._tokens(token_prefixes(request, self.tokenizer), self.generation_config.abc, run_seed, "abc", callback)
score_string = self.tokenizer.decode(tokens_draft)
# Calculate the baseline semantic complexity metric of the draft string
clean_string = score_string.strip()
string_length = len(clean_string)
# Calculate structural entropy proxy using zlib compression bytes
# Long but endlessly repeating garbage strings will compress down heavily, collapsing their score
compressed_length = len(zlib.compress(clean_string.encode('utf-8')))
entropy_density = compressed_length / max(1, string_length)
# Core Metric: Balance raw length with unique structural richness (entropy)
# This prevents the loop from blindly favoring long but looping text patterns
# Elevate density to the 3rd power to exponentially favor unique information over raw text padding
quality_score = string_length * (entropy_density ** 3)
# Check for YuE2's explicit structural voice and instrumental tracking staves
has_vocal = "V: Vocal" in score_string
has_inst = "V: Ins" in score_string
# Structural validations: Instantly eliminate dead or un-orchestrated sheets
if not has_inst or (not is_instrumental and not has_vocal):
quality_score -= 10000 # Enforce a heavy layout drop
# Live tracking printout for candidate comparison
print(f"[Bo{N} Ranker] Candidate {i+1}/{N} Evaluation Profile -> Length: {string_length} chars | Unique Entropy Metric: {quality_score:.4f}")
candidate_runs.append({
"ids": tokens_draft,
"abc": score_string,
"score": quality_score,
"seed_used": run_seed,
"candidate_no": i+1
})
# Pick the absolute gold-standard candidate based on composition depth
best_candidate = max(candidate_runs, key=lambda x: x["score"])
abc_ids = best_candidate["ids"]
abc = best_candidate["abc"]
seed = best_candidate["seed_used"]
del candidate_runs
# print(f"[Bo8 Ranker] Selection complete! Winning score candidate used seed: {seed} (Weight Metric: {best_candidate['score']}).")
# print(f"[Bo16 Ranker] Selection complete! Winning score candidate used seed: {seed} (Weight Metric: {best_candidate['score']}).")
print(f"[Bo{N} Ranker] Selection complete! Winning score candidate is: {best_candidate['candidate_no']}/{N} (Weight Metric: {best_candidate['score']:.1f}).")
print("************ RESUMING STANDARD ACOUSTIC AUDIO SYNTHESIS ************")Tested and verified on Windows 11 running a Python 3.11 environment. It completely eliminates abrupt mid-track cutting issues by forcing the model to select a well-paced structural plan before the heavy audio synthesis loop initializes!
Check the attached song (mp3 file, link right after the screenshots attached in the beginning). Enjoy! :)
Source: multimodal-art-projection/YuE