File size: 13,607 Bytes
f5d2dd3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | # Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import multiprocessing as mp
import re
from typing import Any, Dict, List, Optional
import numpy as np
import torch
from lhotse import CutSet
# Use NeMo's force alignment utilities instead of torchaudio
from nemo.collections.asr.models.asr_model import ASRModel
from nemo.collections.asr.parts.utils.aligner_utils import (
add_t_start_end_to_utt_obj,
get_batch_variables,
viterbi_decoding,
)
class ForceAligner:
"""
Force alignment utility using NeMo CTC-based ASR models for speech-to-text alignment.
"""
def __init__(
self,
asr_model: Optional[ASRModel] = None,
device: str = None,
frame_length: float = 0.02,
asr_model_name: str = "stt_en_fastconformer_ctc_large",
):
"""
Initialize the ForceAligner.
Args:
asr_model: NeMo ASR model instance for alignment. If None, will load from asr_model_name
device: Device to run alignment on (default: auto-detect)
frame_length: Frame length in seconds for timestamp conversion
asr_model_name: Name of the NeMo ASR model to load if asr_model is None
"""
self.device = device or ("cuda" if torch.cuda.is_available() else "cpu")
self.frame_length = frame_length
self.asr_model_name = asr_model_name
self.asr_model = asr_model
self.output_timestep_duration = None
self._model_loaded = False
def _load_asr_model(self):
"""Load the NeMo ASR model."""
try:
if self.device == 'cuda' and mp.get_start_method(allow_none=True) == 'fork':
logging.warning(
"Detected 'fork' multiprocessing start method with CUDA device. "
"To avoid CUDA re-initialization errors in worker processes, "
"falling back to CPU for force alignment. "
"To use CUDA, set mp.set_start_method('spawn', force=True) in your main training script "
"before creating the DataLoader."
)
self.device = 'cpu'
device = torch.device(self.device)
logging.info(f"Loading NeMo ASR model '{self.asr_model_name}' for force alignment on device {device}")
if self.asr_model is None:
# Load ASR model from pretrained
self.asr_model = ASRModel.from_pretrained(self.asr_model_name, map_location=device)
else:
self.asr_model = self.asr_model.to(device)
self.asr_model.eval()
# Calculate output timestep duration
try:
self.output_timestep_duration = (
self.asr_model.cfg['preprocessor']['window_stride'] * self.asr_model.encoder.subsampling_factor
)
except Exception as e:
# Default fallback based on typical FastConformer settings
self.output_timestep_duration = 0.04
logging.warning(
f"Could not calculate output_timestep_duration from model config: {e}. "
f"Using default {self.output_timestep_duration}s"
)
logging.info(
f"NeMo ASR model loaded successfully for force alignment. "
f"Output timestep duration: {self.output_timestep_duration}s"
)
except Exception as e:
logging.error(f"Failed to load NeMo ASR model for force alignment: {e}")
self.asr_model = None
raise
def batch_force_align_user_audio(self, cuts: CutSet, source_sample_rate: int = 16000) -> CutSet:
"""
Perform batched force alignment on all user audio segments.
Collects all user segments, writes temp files, runs a single batched
get_batch_variables + viterbi_decoding call, then maps results back.
Args:
cuts: CutSet containing all cuts to process
source_sample_rate: Source sample rate of the audio
Returns:
CutSet with updated supervision texts (timestamped where alignment succeeded)
"""
if not self._model_loaded:
self._load_asr_model()
self._model_loaded = True
if self.asr_model is None:
logging.warning("ASR model not available for force alignment, returning empty cutset")
return CutSet.from_cuts([])
# Collect all user supervisions
user_supervisions = []
user_cuts = []
for cut in cuts:
for supervision in cut.supervisions:
if supervision.speaker.lower() == "user":
user_supervisions.append(supervision)
user_cuts.append(cut)
if not user_supervisions:
logging.info("No user supervisions found for force alignment")
return cuts
logging.info(f"Performing batched force alignment on {len(user_supervisions)} user audio segments")
# Prepare all audio arrays and texts for batched processing
audio_arrays = []
normalized_texts = []
valid_indices = [] # track which supervisions have valid audio/text
target_sample_rate = 16000
for i, (supervision, cut) in enumerate(zip(user_supervisions, user_cuts)):
try:
text = self._strip_timestamps(supervision.text)
normalized_text = self._normalize_transcript(text)
if not normalized_text.strip():
logging.warning(f"Text became empty after normalization: {supervision.text}")
continue
user_cut = cut.truncate(offset=supervision.start, duration=supervision.duration)
audio = user_cut.load_audio()
if audio.ndim > 1:
audio = audio.mean(axis=0)
if source_sample_rate != target_sample_rate:
from scipy import signal
num_samples = int(len(audio) * target_sample_rate / source_sample_rate)
audio = signal.resample(audio, num_samples)
# Add silence padding for better alignment at the end
silence_samples = int(0.64 * target_sample_rate)
audio = np.concatenate([audio, np.zeros(silence_samples)])
audio_arrays.append(audio)
normalized_texts.append(normalized_text)
valid_indices.append(i)
except Exception as e:
logging.error(f"Failed to prepare segment {i} for alignment: {e}")
if not audio_arrays:
logging.warning("No valid segments to align")
return cuts
# Batched ASR inference + Viterbi decoding
success_count = 0
failed_count = 0
try:
(
log_probs_batch,
y_batch,
T_batch,
U_batch,
utt_obj_batch,
output_timestep_duration,
) = get_batch_variables(
audio=audio_arrays,
model=self.asr_model,
gt_text_batch=normalized_texts,
align_using_pred_text=False,
output_timestep_duration=self.output_timestep_duration,
)
alignments_batch = viterbi_decoding(
log_probs_batch=log_probs_batch,
y_batch=y_batch,
T_batch=T_batch,
U_batch=U_batch,
viterbi_device=torch.device(self.device),
)
# Map results back to supervisions
for batch_idx, orig_idx in enumerate(valid_indices):
try:
if batch_idx >= len(alignments_batch) or batch_idx >= len(utt_obj_batch):
failed_count += 1
continue
utt_obj = utt_obj_batch[batch_idx]
if not utt_obj.token_ids_with_blanks:
failed_count += 1
continue
alignment = alignments_batch[batch_idx]
utt_obj = add_t_start_end_to_utt_obj(utt_obj, alignment, output_timestep_duration)
word_segments = self._extract_word_timestamps(utt_obj)
if word_segments:
timestamped_text = self._convert_alignment_to_timestamped_text(
word_segments, user_supervisions[orig_idx].text
)
user_supervisions[orig_idx].text = timestamped_text
success_count += 1
else:
failed_count += 1
except Exception as e:
logging.error(f"Failed to process alignment for segment {orig_idx}: {e}")
failed_count += 1
except Exception as e:
logging.error(f"Batched force alignment failed: {e}")
failed_count = len(valid_indices)
finally:
if self.device == 'cuda' and torch.cuda.is_available():
torch.cuda.empty_cache()
if failed_count > 0:
logging.warning(
f"Force alignment failed for {failed_count}/{len(user_supervisions)} user segments. "
f"Keeping original text for failed alignments."
)
else:
logging.info(f"Force alignment succeeded for all {success_count} user segments.")
return cuts
def _extract_word_timestamps(self, utt_obj) -> List[Dict[str, Any]]:
"""
Extract word-level timestamps from the utterance object returned by NeMo aligner.
Args:
utt_obj: Utterance object with timing information
Returns:
List of word segments with timing information
"""
word_segments = []
for segment_or_token in utt_obj.segments_and_tokens:
# Check if this is a Segment object (has words_and_tokens attribute)
if hasattr(segment_or_token, 'words_and_tokens'):
segment = segment_or_token
for word_or_token in segment.words_and_tokens:
# Check if this is a Word object (has 'text' and timing attributes)
if hasattr(word_or_token, 'text') and hasattr(word_or_token, 't_start'):
word = word_or_token
# Skip CTC blank tokens and include only words with valid timing
if (
word.text not in ('<b>', '')
and word.t_start is not None
and word.t_end is not None
and word.t_start >= 0
and word.t_end >= 0
):
word_segments.append(
{
'word': word.text,
'start': word.t_start,
'end': word.t_end,
'score': 1.0, # NeMo CTC alignment doesn't provide confidence scores
}
)
return word_segments
def _normalize_transcript(self, transcript: str) -> str:
"""
Normalize transcript for the ASR model's tokenizer.
Keeps it simple to match common ASR preprocessing.
"""
text = transcript.lower()
# Remove special characters except apostrophes and spaces
text = re.sub(r"[^a-z' ]", " ", text)
# Collapse multiple spaces
text = re.sub(r' +', ' ', text)
return text.strip()
def _convert_alignment_to_timestamped_text(
self, alignment_result: List[Dict[str, Any]], original_text: str
) -> str:
"""
Convert alignment results to timestamped text format.
Args:
alignment_result: List of word segments with timing information
original_text: Original text without timestamps
Returns:
Text with timestamp tokens in the format <|start_frame|>word<|end_frame|>
"""
timestamped_words = []
for word_seg in alignment_result:
word = word_seg["word"]
start_frame = int(word_seg["start"] / self.frame_length)
end_frame = int(word_seg["end"] / self.frame_length)
timestamped_words.append(f"<|{start_frame}|> {word} <|{end_frame}|>")
return " ".join(timestamped_words)
def _strip_timestamps(self, text: str) -> str:
"""
Strip timestamp tokens from text.
Args:
text: Text that may contain timestamp tokens
Returns:
Text with timestamp tokens removed
"""
text = re.sub(r'<\|[0-9]+\|>', '', text)
text = re.sub(r' +', ' ', text)
return text.strip()
|