Files

52 lines
4.1 KiB
Python

"""Isolated Vevo2 FM A/B test; upstream source and model are never modified."""
import os,json,sys,time,random,traceback
from pathlib import Path
r=Path(os.environ.get('VEVO2_TEST_ROOT','/data'))
os.environ.update(HOME=str(r),HF_HOME=str(r/'hf-cache'),HF_HUB_OFFLINE='1',TRANSFORMERS_OFFLINE='1',HF_HUB_DISABLE_IMPLICIT_TOKEN='1',OMP_NUM_THREADS='4',OPENBLAS_NUM_THREADS='1',NUMBA_NUM_THREADS='2',NUMBA_CACHE_DIR=str(r/'numba-cache'))
def status(state,phase):
p=r/'text-status.tmp';p.write_text(json.dumps(dict(state=state,phase=phase,time=time.time())));p.replace(r/'text-status.json')
try:
source=r/'checkout'/Path((r/'source-path.txt').read_text()).name;sys.path.insert(0,str(source));os.chdir(source)
import torch,numpy as np,librosa,soundfile as sf
cpu='--cpu' in sys.argv
if not cpu and not torch.cuda.is_available():raise RuntimeError('A free dedicated GPU is required.')
torch.set_num_threads(2 if cpu else 4);device=torch.device('cpu' if cpu else 'cuda:0')
gpu_name='CPU' if cpu else torch.cuda.get_device_properties(0).name
if not cpu:
free,total=torch.cuda.mem_get_info()
if free<10*1024**3:raise RuntimeError('Vevo2 test requires at least10GiB free GPU memory.')
# Official Whisper lookup cache; pre-fetched under CPU-only preparation.
cache=r/'.cache';cache.mkdir(exist_ok=True)
if not (cache/'whisper').exists():(cache/'whisper').symlink_to(r/'whisper-cache',target_is_directory=True)
ckpts=source/'ckpts';ckpts.mkdir(exist_ok=True)
if not (ckpts/'Vevo2').exists():(ckpts/'Vevo2').symlink_to(r/'model',target_is_directory=True)
# The text-FM config resolves whisper_stats.pt relative to its checkpoint directory.
os.chdir(r/'model/acoustic_modeling/fm_emilia101k_singnet7k_repa_text')
from models.svc.vevo2.vevo2_utils import Vevo2InferencePipeline,save_audio
status('running','Offiziellen FM-Konverter auf '+gpu_name+' laden')
start=time.monotonic();m=r/'model'
pipe=Vevo2InferencePipeline(content_style_tokenizer_ckpt_path=str(m/'tokenizer/contentstyle_fvq16384_12.5hz'),fmt_cfg_path=str(m/'acoustic_modeling/fm_emilia101k_singnet7k_repa_text/config.json'),fmt_ckpt_path=str(m/'acoustic_modeling/fm_emilia101k_singnet7k_repa_text'),vocoder_cfg_path=str(m/'vocoder/config.json'),vocoder_ckpt_path=str(m/'vocoder'),device=device)
results=dict(versions=json.loads((r/'versions.json').read_text()),gpu=gpu_name,model_load_seconds=round(time.monotonic()-start,3),flow_matching_steps=32,seed=20261019,cases=[])
a=r/'inputs/source.wav';b=r/'inputs/anna-reference.wav';out=r/'outputs';out.mkdir(exist_ok=True)
shift=int(pipe.get_shifted_steps(str(a),str(b)));results['official_pitch_shift_semitones']=shift
text=json.loads((r/'private-text.json').read_text())
for auto in (True,):
name='auto-text-conditioned';status('running','Vevo2: '+name)
random.seed(20261019);np.random.seed(20261019);torch.manual_seed(20261019);torch.cuda.manual_seed_all(20261019)
if not cpu:torch.cuda.reset_peak_memory_stats()
start=time.monotonic()
with torch.inference_mode():audio=pipe.inference_fm(src_wav_path=str(a),timbre_ref_wav_path=str(b),use_pitch_shift=auto,flow_matching_steps=32,src_wav_text=text['source'],timbre_ref_wav_text=text['reference'])
if not cpu:torch.cuda.synchronize()
elapsed=time.monotonic()-start
# Same -25 dBFS RMS normalization as the official save_audio function;
# soundfile writes PCM without requiring Torchaudio's optional TorchCodec.
pcm=audio.detach().cpu().numpy().reshape(-1)
rms=float(np.sqrt(np.mean(pcm**2)))
if rms>0:pcm=pcm*(10**(-25/20)/rms)
sf.write(out/(name+'.wav'),pcm,24000,subtype='PCM_16')
y,sr=sf.read(out/(name+'.wav'));case=dict(name=name,auto_pitch=auto,seconds=round(elapsed,3),output_seconds=round(len(y)/sr,4),sample_rate=sr,peak_allocated_mib=None if cpu else round(torch.cuda.max_memory_allocated()/1024**2),peak_reserved_mib=None if cpu else round(torch.cuda.max_memory_reserved()/1024**2))
results['cases'].append(case);(r/'text-results.json').write_text(json.dumps(results,indent=2))
status('complete','Textkonditionierte Vevo2-Ausgabe gespeichert; eigener Modellprozess endet')
except Exception as e:
status('failed',type(e).__name__+': '+str(e)[:400]);traceback.print_exc();sys.exit(1)