Update VibeVoice-tpu/src/preprocess_vibevoice_tpu.py
Browse files408 unmodified lines40941041141241341441541641741841942011 unmodified lines4324334344354364374389 unmodified lines44844945045145245345445545645745845946046146246346446546646762 unmodified lines5305315325335345355364 unmodified lines5415425435445455465474 unmodified lines552553554555556557558559560561562563564565566114 unmodified lines681682683684685686687688408 unmodified linesargs = self.args# ββ Step 1: CPU preprocessing via collator ββ# VibeVoiceCollator handles:# - Text tokenization via processor# - Audio loading & resampling to 24kHz# - Voice prompt processing# - Building input_ids, attention_mask, masks# - Acoustic/semantic feature extractionprocessed = self.collator(raw_batch)if not processed:11 unmodified lineselse:result[k] = v# ββ Step 3: Re-encode semantic features on TPU if placeholder ββif ("speech_semantic_tensors" in resultand "speech_tensors" in resultand result["speech_semantic_tensors"] is not None):9 unmodified lineslogger.debug(f"TPU semantic encoding failed, keeping zeros: {e}")# ββ Step 4: Re-encode acoustic features on TPU if needed ββ# Acoustic features are encoded via forward_speech_features in the collator# but the collator uses CPU. If acoustic_tokenizer is on TPU, we can# benefit from TPU speed for the acoustic encoding too.if (self.acoustic_tokenizer is not Noneand "speech_tensors" in resultand result["speech_tensors"] is not None):# The speech_tensors are raw waveforms β acoustic encoding is done# during training via model.forward_speech_features(). For preprocessing,# we ensure the raw waveforms are properly formatted on TPU.pass # Acoustic encoding happens during forward pass, waveforms are stored as-is# ββ Step 5: Convert non-float tensors back to CPU-compatible types ββ# Ensure integer/bool tensors stay as their original typesfor k in result:if isinstance(result[k], torch.Tensor):if result[k].dtype == torch.bfloat16:62 unmodified linesif sem_feat.dim() == 1:sem_feat = sem_feat.unsqueeze(0)# Pad/trim to match latent_lenT = sem_feat.shape[0]if T < latent_len:pad = torch.zeros(4 unmodified lineselif T > latent_len:sem_feat = sem_feat[:latent_len]# Ensure correct feature dimensionif sem_feat.shape[1] != sem_dim:if sem_feat.shape[1] < sem_dim:pad_d = torch.zeros(4 unmodified lineselse:sem_feat = sem_feat[:, :sem_dim]semantic_features.append(sem_feat.to(dtype=torch.bfloat16))except Exception as e:logger.debug(f"Semantic encode failed for sample {i}: {e}")semantic_features.append(torch.zeros(latent_len, sem_dim, device=device, dtype=torch.bfloat16))# Stack [B, T, D]if semantic_features:return torch.stack(semantic_features, dim=0)else:114 unmodified linesprint(f"{'='*70}\n")# ββ Launch on TPU ββ# Use only 1 process (rank 0) since preprocessing is sequential# but benefits from TPU compute power for tokenizersxmp.spawn(preprocess_single_process,args=(preprocess_args,),408 unmodified lines40941041141241341411 unmodified lines4264274284294304314329 unmodified lines44244344444544644744844945045145245362 unmodified lines5165175185195205215224 unmodified lines5275285295305315325334 unmodified lines538539540541542543544545546547548549550551552553554555556557558559560114 unmodified lines675676677678679680408 unmodified linesargs = self.args# ββ Step 1: CPU preprocessing via collator ββprocessed = self.collator(raw_batch)if not processed:11 unmodified lineselse:result[k] = v# ββ Step 3: Re-encode semantics on TPU if placeholder ββif ("speech_semantic_tensors" in resultand "speech_tensors" in resultand result["speech_semantic_tensors"] is not None):9 unmodified lineslogger.debug(f"TPU semantic encoding failed, keeping zeros: {e}")# ββ Step 4: Re-encode acoustic features on TPU if needed ββif (self.acoustic_tokenizer is not Noneand "speech_tensors" in resultand result["speech_tensors"] is not None):pass # Acoustic encoding happens during forward pass, waveforms are stored as-is# ββ Step 5: Convert non-float tensors back to CPU-compatible types ββfor k in result:if isinstance(result[k], torch.Tensor):if result[k].dtype == torch.bfloat16:62 unmodified linesif sem_feat.dim() == 1:sem_feat = sem_feat.unsqueeze(0)# 1. Pad/trim to match actual latent_len firstT = sem_feat.shape[0]if T < latent_len:pad = torch.zeros(4 unmodified lineselif T > latent_len:sem_feat = sem_feat[:latent_len]# 2. Ensure correct feature dimensionif sem_feat.shape[1] != sem_dim:if sem_feat.shape[1] < sem_dim:pad_d = torch.zeros(4 unmodified lineselse:sem_feat = sem_feat[:, :sem_dim]# 3. CRUCIAL: Pad from actual latent_len to max_latent_len so all batch items match!if latent_len < max_latent_len:pad_max = torch.zeros(max_latent_len - latent_len, sem_dim,device=device, dtype=sem_feat.dtype)sem_feat = torch.cat([sem_feat, pad_max], dim=0)semantic_features.append(sem_feat.to(dtype=torch.bfloat16))except Exception as e:logger.debug(f"Semantic encode failed for sample {i}: {e}")semantic_features.append(torch.zeros(max_latent_len, sem_dim, device=device, dtype=torch.bfloat16))# Stack [B, max_latent_len, D]if semantic_features:return torch.stack(semantic_features, dim=0)else:114 unmodified linesprint(f"{'='*70}\n")# ββ Launch on TPU ββxmp.spawn(preprocess_single_process,args=(preprocess_args,),
VibeVoice-tpu/src/preprocess_vibevoice_tpu.py+13-21