RASMUS/Finnish-ASR-Canary-v2
02.2k
1# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14 15"""16Evaluation script for Duplex EARTTS models.17 18This script computes standard speech evaluation metrics for a given Duplex19EARTTS checkpoint, including Word Error Rate (WER), Character Error Rate (CER),20speaker encoder cosine similarity (SECS), and ASR BLEU score.21 22The configuration file must define a valid ``validation_ds`` based on a Lhotse23dataset using one of the following dataset formats:24- Duplex S2S standard format25- ``s2s_duplex_overlap_as_s2s_duplex``26- ``lhotse_magpietts_data_as_continuation``27 28During evaluation, the script saves generated audio samples to29``exp_manager.explicit_log_dir`` as specified in the configuration. For each30utterance, the following audio files may be produced:31 32- Autoregressive inference output (``*.wav``)33- Teacher-forced output (``*_tf.wav``)34- Ground-truth reference audio (``*_gt.wav``)35 36Args:37 config-path (str): Path to the directory containing the YAML configuration file.38 config-name (str): Name of the YAML configuration file.39 checkpoint_path (str): Path to the Duplex EARTTS checkpoint file.40 41Usage:42 python duplex_eartts_eval.py \43 --config-path=conf/ \44 --config-name=duplex_eartts.yaml \45 ++checkpoint_path=duplex_eartts_results/duplex_eartts/model.ckpt46"""47 48import os49 50import torch51from lightning.pytorch import Trainer52from omegaconf import OmegaConf53 54from nemo.collections.speechlm2 import DataModule, DuplexEARTTSDataset55 56from nemo.collections.speechlm2.models.duplex_ear_tts import DuplexEARTTS57from nemo.core.config import hydra_runner58from nemo.utils.exp_manager import exp_manager59from nemo.utils.trainer_utils import resolve_trainer_cfg60 61torch.cuda.set_device(int(os.environ["LOCAL_RANK"]))62 63 64@hydra_runner(config_path="conf", config_name="duplex_eartts")65def inference(cfg):66 OmegaConf.resolve(cfg)67 torch.distributed.init_process_group(backend="nccl")68 torch.set_float32_matmul_precision("medium")69 torch.backends.cudnn.allow_tf32 = True70 trainer = Trainer(**resolve_trainer_cfg(cfg.trainer))71 log_dir = exp_manager(trainer, cfg.get("exp_manager", None))72 OmegaConf.save(cfg, log_dir / "exp_config.yaml")73 74 with trainer.init_module():75 if cfg.get("checkpoint_path", None):76 model = DuplexEARTTS.load_from_checkpoint(77 cfg.checkpoint_path,78 cfg=OmegaConf.to_container(cfg, resolve=True),79 )80 else:81 raise ValueError("For evaluation, you must provide `cfg.checkpoint_path`.")82 83 dataset = DuplexEARTTSDataset(84 tokenizer=model.tokenizer,85 frame_length=cfg.data.frame_length,86 source_sample_rate=cfg.data.source_sample_rate,87 target_sample_rate=cfg.data.target_sample_rate,88 input_roles=cfg.data.input_roles,89 output_roles=cfg.data.output_roles,90 add_text_bos_and_eos_in_each_turn=cfg.data.get("add_text_bos_and_eos_in_each_turn", True),91 add_audio_prompt=cfg.data.get("add_audio_prompt", True),92 audio_prompt_duration=cfg.data.get("audio_prompt_duration", 3),93 num_delay_speech_tokens=cfg.model.get("num_delay_speech_tokens", 2),94 )95 datamodule = DataModule(cfg.data, tokenizer=model.tokenizer, dataset=dataset)96 97 trainer.validate(model, datamodule)98 99 100if __name__ == "__main__":101 inference()102 