| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| import os |
| import random |
| import warnings |
| from copy import deepcopy |
| from dataclasses import dataclass |
| from functools import partial |
| from typing import Any, List, Optional, Sequence, Tuple, Union |
|
|
| import lhotse |
| import numpy as np |
| import torch |
| from lhotse import CutSet, RecordingSet |
| from lhotse.cut import Cut |
| from lhotse.dataset import ( |
| ClippingTransform, |
| Compress, |
| CutConcatenate, |
| DynamicBucketingSampler, |
| DynamicCutSampler, |
| IterableDatasetWrapper, |
| LowpassUsingResampling, |
| ReverbWithImpulseResponse, |
| RoundRobinSampler, |
| ZipSampler, |
| make_worker_init_fn, |
| ) |
| from lhotse.dataset.dataloading import resolve_seed |
| from lhotse.dataset.sampling.base import CutSampler, SamplingConstraint, TimeConstraint |
| from lhotse.lazy import LazyFlattener |
| from lhotse.utils import fastcopy, fix_random_seed |
| from omegaconf import DictConfig, OmegaConf |
|
|
| from nemo.collections.common.data.lhotse.cutset import ( |
| IncompleteConfigError, |
| guess_parse_cutset, |
| read_cutset_from_config, |
| ) |
| from nemo.collections.common.data.lhotse.sampling import ( |
| BucketingFilter, |
| CERFilter, |
| ContextSpeakerSimilarityFilter, |
| DurationFilter, |
| FixedBucketBatchSizeConstraint2D, |
| MultimodalFixedBucketBatchSizeConstraint2D, |
| MultimodalSamplingConstraint, |
| TokenCountFilter, |
| TokenPerSecondFilter, |
| TokenPerTokenFilter, |
| ValidationStatusFilter, |
| ) |
| from nemo.collections.common.data.prompt_fn import apply_prompt_format_fn |
| from nemo.collections.common.prompts import PromptFormatter |
| from nemo.collections.common.tokenizers.aggregate_tokenizer import TokenizerWrapper |
| from nemo.utils import logging |
|
|
|
|
| @dataclass |
| class LhotseDataLoadingConfig: |
| """ |
| Structured config used for OmegaConf schema validation. |
| It's also a single source of truth for reading default option values. |
| The options not supported anymore but present, e.g., in old configs, |
| will be emitted in a DeprecationWarning and ignored. |
| """ |
|
|
| |
| |
| input_cfg: Any = None |
| manifest_filepath: Any = None |
| tarred_audio_filepaths: Any = None |
| |
| cuts_path: str | None = None |
| shar_path: Any = None |
| |
| skip_missing_manifest_entries: bool = False |
| tarred_random_access: bool = False |
| |
| |
| batch_size: int | None = None |
| |
| batch_duration: float | None = None |
| quadratic_duration: float | None = None |
| |
| use_bucketing: bool = False |
| bucket_batch_size: list[int] | None = None |
| num_buckets: int = 30 |
| num_cuts_for_bins_estimate: int = 10000 |
| bucket_duration_bins: Any = None |
| bucket_buffer_size: int = 10000 |
| concurrent_bucketing: bool = True |
| bucketing_2d_strict_mode: bool = True |
| |
| shuffle_buffer_size: int | None = 10000 |
| drop_last: bool = False |
| shard_seed: int | str = "trng" |
| max_open_streams: int | None = None |
| cuda_expandable_segments: bool = True |
| |
| |
| |
| reweight_temperature: Any = None |
| |
| |
| |
| multi_config: bool = False |
| sampler_fusion: str = "round_robin" |
| sampler_weights: dict[str, float] | None = None |
|
|
| |
| pretokenize: bool = True |
| prompt_format: str | None = None |
| use_multimodal_sampling: bool = False |
| audio_locator_tag: str | None = None |
| token_equivalent_duration: float | None = None |
| batch_tokens: int | None = None |
| quadratic_factor: float | None = None |
| |
| |
| cut_text_into_windows_tokens: int | None = None |
|
|
| |
| |
| min_duration: float | None = -1 |
| max_duration: float | None = float("inf") |
| min_tps: int = -1 |
| max_tps: Any = float("inf") |
| |
| min_tokens: int | None = None |
| max_tokens: int | None = None |
| |
| |
| measure_total_length: bool = True |
| min_tpt: int = -1 |
| max_tpt: Any = float("inf") |
|
|
| |
| max_cer: float | None = float("inf") |
| min_context_speaker_similarity: float | None = -1 |
|
|
| |
| keep: str = "pass" |
|
|
| |
| shuffle: bool = False |
| sample_rate: int = 16000 |
| seed: int | str = 0 |
| num_workers: int = 0 |
| pin_memory: bool = False |
| channel_selector: int | str | None = None |
|
|
| |
| |
| noise_path: Any | None = ( |
| None |
| |
| ) |
| noise_snr: tuple[float, float] = (10.0, 20.0) |
| noise_mix_prob: float = 0.5 |
| |
| perturb_speed: bool = False |
| |
| concatenate_samples: bool = False |
| concatenate_gap_seconds: float = 0.1 |
| concatenate_duration_factor: float = 1.0 |
| concatenate_merge_supervisions: bool = True |
| db_norm: Optional[float] = -25.0 |
| |
| |
| truncate_duration: Optional[float] = None |
| truncate_offset_type: str = "random" |
| |
| |
| cut_into_windows_duration: Optional[float] = None |
| cut_into_windows_hop: Optional[float] = None |
| |
| keep_excessive_supervisions: bool = ( |
| True |
| ) |
| |
| |
| |
| rir_enabled: bool = False |
| rir_path: str | None = None |
| rir_prob: float = 0.5 |
| |
| pad_min_duration: Optional[float] = None |
| pad_direction: str = "right" |
| |
| lowpass_enabled: bool = False |
| lowpass_frequencies_interval: Tuple[float, float] = (3500.0, 8000.0) |
| lowpass_prob: float = 0.5 |
| |
| |
| |
| |
| compression_enabled: bool = False |
| compression_prob: float = 0.5 |
| compression_level_interval: Tuple[float, float] = (0.8, 0.99) |
| compression_codecs: Tuple[str] = ("opus",) |
| compression_codec_weights: Optional[List[float]] = None |
| compression_enable_for_custom_fields: bool = False |
| |
| clipping_enabled: bool = False |
| clipping_gain_db: Tuple[float, float] = (0.0, 24.0) |
| clipping_normalize: bool = True |
| clipping_oversampling: Optional[int] = 2 |
| clipping_prob_hard: float = 0.5 |
| clipping_prob: float = 0.5 |
|
|
| |
| text_field: str = "text" |
| lang_field: str = "lang" |
| |
| |
| metadata_only: bool = False |
| |
| |
| |
| |
| force_finite: bool = False |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| force_map_dataset: bool = False |
| force_iterable_dataset: bool = False |
| |
| |
| |
| |
| |
| |
| |
| slice_length: Optional[int] = None |
|
|
|
|
| def determine_use_iterable_dataset(use_iterable_dataset: bool, config: DictConfig) -> bool: |
| """Determine whether to use iterable dataset for a given configuration.""" |
| assert not ( |
| config.force_map_dataset and config.force_iterable_dataset |
| ), "Conflicting options: force_map_dataset=True and force_iterable_dataset=True" |
| use_iterable_dataset = (use_iterable_dataset or config.force_iterable_dataset) and not config.force_map_dataset |
| return use_iterable_dataset |
|
|
|
|
| def get_lhotse_dataloader_from_config( |
| config: Union[dict, DictConfig], |
| global_rank: int, |
| world_size: int, |
| dataset: torch.utils.data.Dataset, |
| tokenizer=None, |
| ) -> torch.utils.data.DataLoader: |
| """ |
| Set up a Lhotse training dataloader. |
| |
| Expects a typical NeMo dataset configuration format, with additional fields: "use_lhotse=True". |
| Some fields in the original NeMo configuration may be ignored. |
| |
| The ``dataset`` parameter should be an instance of a Lhotse-compatible PyTorch Dataset class. |
| It only needs to define the following method ``__getitem__(self, cuts: CutSet) -> Dict[str, torch.Tensor]``. |
| This dataset is not expected to hold a reference to any actual data; it may be interpreted as a function |
| mapping a Lhotse CutSet into a mini-batch of tensors. |
| |
| For an example, see: :class:`nemo.collections.asr.data.audio_to_text_lhotse.LhotseSpeechToTextBpeDataset`, |
| which is constructed from just a tokenizer and essentially loads and collates audio and tokenizes the transcript. |
| |
| The ``tokenizer`` is used both for audio and text datasets for on-the-fly tokenization. |
| This allows us to stratify the bucketing by the count of input/output tokens (depending on modality). |
| If "prompt_format" is additionally provided in the config, we will also apply a prompt formatter. |
| Note that ``tokenizer`` can be any tokenizer type (e.g. both SentencePiece and Aggregate tokenizers work). |
| """ |
| if not isinstance(config, DictConfig): |
| config = OmegaConf.create(config) |
|
|
| |
| maybe_set_cuda_expandable_segments(enabled=config.get("cuda_expandable_segments", True)) |
|
|
| if config.get("multi_config", False): |
| return get_lhotse_dataloader_from_multi_config( |
| top_level_config=config, |
| global_rank=global_rank, |
| world_size=world_size, |
| dataset=dataset, |
| tokenizer=tokenizer, |
| ) |
| else: |
| return get_lhotse_dataloader_from_single_config( |
| config=config, global_rank=global_rank, world_size=world_size, dataset=dataset, tokenizer=tokenizer |
| ) |
|
|
|
|
| def get_lhotse_dataloader_from_single_config( |
| config: DictConfig, |
| global_rank: int, |
| world_size: int, |
| dataset: torch.utils.data.Dataset, |
| tokenizer=None, |
| ) -> torch.utils.data.DataLoader: |
| """ |
| Set up a Lhotse training dataloader. |
| |
| Expects a typical NeMo dataset configuration format, with additional fields: "use_lhotse=True". |
| Some fields in the original NeMo configuration may be ignored. |
| |
| The ``dataset`` parameter should be an instance of a Lhotse-compatible PyTorch Dataset class. |
| It only needs to define the following method ``__getitem__(self, cuts: CutSet) -> Dict[str, torch.Tensor]``. |
| This dataset is not expected to hold a reference to any actual data; it may be interpreted as a function |
| mapping a Lhotse CutSet into a mini-batch of tensors. |
| |
| For an example, see: :class:`nemo.collections.asr.data.audio_to_text_lhotse.LhotseSpeechToTextBpeDataset`, |
| which is constructed from just a tokenizer and essentially loads and collates audio and tokenizes the transcript. |
| |
| The ``tokenizer`` is used when text-only datasets are included in dataloading. |
| In these cases we will tokenize ``TextExample``s before sampling mini-batches so that |
| we can account for their number of tokens. |
| Note: this behaviour might eventually be extended to audio datasets too. |
| |
| Note that ``tokenizer`` can be any tokenizer type (e.g. both SentencePiece and Aggregate tokenizers work). |
| """ |
| logging.info("We will be using a Lhotse DataLoader.") |
| config = make_structured_with_schema_warnings(config) |
|
|
| |
| config.seed = resolve_seed(config.seed) |
| fix_random_seed(config.seed) |
|
|
| sampler, use_iterable_dataset = get_lhotse_sampler_from_config( |
| config=config, global_rank=global_rank, world_size=world_size, tokenizer=tokenizer |
| ) |
|
|
| |
| if use_iterable_dataset: |
| |
| |
| |
| |
| |
| |
| |
| dloader_kwargs = dict( |
| dataset=IterableDatasetWrapper(dataset=dataset, sampler=sampler), |
| worker_init_fn=make_worker_init_fn(rank=global_rank, world_size=world_size, seed=config.seed), |
| persistent_workers=config.num_workers > 0, |
| ) |
| else: |
| |
| |
| |
| dloader_kwargs = dict(dataset=dataset, sampler=sampler) |
| dloader = torch.utils.data.DataLoader( |
| **dloader_kwargs, |
| batch_size=None, |
| num_workers=config.num_workers, |
| pin_memory=config.pin_memory, |
| ) |
|
|
| return dloader |
|
|
|
|
| def get_lhotse_dataloader_from_multi_config( |
| top_level_config: DictConfig, |
| global_rank: int, |
| world_size: int, |
| dataset: torch.utils.data.Dataset, |
| tokenizer=None, |
| ) -> torch.utils.data.DataLoader: |
| """ |
| Set up a Lhotse training dataloder. |
| |
| It works similarly to :func:`get_lhotse_dataloader_from_config`, except that |
| you can provide multiple configs to set up different sampling, batching, and |
| augmentation settings for every dataset and decide how to merge them. |
| |
| The expected format is that the ``configs`` is a dict of group name -> actual config. |
| |
| The first config is treated as a "main" config that determines the RNG, CUDA allocator, |
| and sampler fusion settings. |
| """ |
|
|
| def gather_shared_opts(): |
| """ |
| In multi-config setting, the top-level config defines several attributes that overwrite |
| the ones present in sub-configs. |
| """ |
| assert all(k in top_level_config for k in ["seed", "shard_seed", "shuffle"]), ( |
| "In a multi-config setting (multi_config=True), the top-level namespace (typically train_ds)" |
| "must define at least 'seed', 'shard_seed', and 'shuffle' keys that will be " |
| "shared by all sub-configs." |
| ) |
| overwriting_opts = [ |
| "seed", |
| "shard_seed", |
| "num_workers", |
| "pin_memory", |
| "shuffle", |
| "sampler_fusion", |
| "sampler_weights", |
| "multi_config", |
| "metadata_only", |
| "force_finite", |
| ] |
| defaults = OmegaConf.structured(LhotseDataLoadingConfig) |
| top_level_config["seed"] = resolve_seed(top_level_config["seed"]) |
| return OmegaConf.create({k: top_level_config.get(k, defaults[k]) for k in overwriting_opts}) |
|
|
| shared_opts = gather_shared_opts() |
| fix_random_seed(shared_opts.seed) |
|
|
| configs = { |
| name: c |
| for name, c in top_level_config.items() |
| if isinstance(c, DictConfig) and name not in ("sampler_weights",) |
| } |
|
|
| source_samplers, source_use_iterable_dataset = {}, [] |
| for name, config in configs.items(): |
| try: |
| expanded_config = make_structured_with_schema_warnings(config) |
| for k, v in shared_opts.items(): |
| expanded_config[k] = v |
| s, t = get_lhotse_sampler_from_config( |
| config=expanded_config, global_rank=global_rank, world_size=world_size, tokenizer=tokenizer |
| ) |
| except IncompleteConfigError as e: |
| raise IncompleteConfigError( |
| "Cannot create a sampler for one of the sub-configs in a multi_config setup." |
| f"The problematic config is under key={name} and has the following contents: {config}" |
| ) from e |
| source_samplers[name] = s |
| source_use_iterable_dataset.append(t) |
|
|
| assert all(st == source_use_iterable_dataset[0] for st in source_use_iterable_dataset[1:]), ( |
| "When using multiple input_cfg sources ensure they are all tarred or non-tarred (can't mix). " |
| "You can provide force_iterable_dataset=True to each namespace to fix." |
| ) |
| use_iterable_dataset = all(source_use_iterable_dataset) |
| if shared_opts.sampler_fusion == "zip": |
| sampler = ZipSampler(*source_samplers.values()) |
| elif shared_opts.sampler_fusion == "round_robin": |
| sampler = RoundRobinSampler(*source_samplers.values()) |
| elif shared_opts.sampler_fusion == "randomized_round_robin": |
| _samplers, _weights = [], [] |
| for key in source_samplers.keys(): |
| _samplers.append(source_samplers[key]) |
| if shared_opts.sampler_weights is not None: |
| _weights.append(shared_opts.sampler_weights[key]) |
| sampler = RoundRobinSampler( |
| *_samplers, |
| randomize=_weights if len(_weights) > 0 else True, |
| seed=shared_opts.seed, |
| ) |
| else: |
| raise RuntimeError(f"Unsupported sampler fusion strategy: {shared_opts.sampler_fusion}") |
|
|
| |
| if use_iterable_dataset: |
| |
| |
| |
| |
| |
| |
| |
| dloader_kwargs = dict( |
| dataset=IterableDatasetWrapper(dataset=dataset, sampler=sampler), |
| worker_init_fn=make_worker_init_fn(rank=global_rank, world_size=world_size, seed=shared_opts.seed), |
| persistent_workers=shared_opts.num_workers > 0, |
| ) |
| else: |
| |
| |
| |
| dloader_kwargs = dict(dataset=dataset, sampler=sampler) |
| dloader = torch.utils.data.DataLoader( |
| **dloader_kwargs, |
| batch_size=None, |
| num_workers=shared_opts.num_workers, |
| pin_memory=shared_opts.pin_memory, |
| ) |
|
|
| return dloader |
|
|
|
|
| def get_lhotse_sampler_from_config(config, global_rank, world_size, tokenizer=None) -> tuple[CutSampler, bool]: |
| """Create a CutSampler from a dataloader config.""" |
| |
| cuts, use_iterable_dataset = read_cutset_from_config(config) |
| use_iterable_dataset = determine_use_iterable_dataset(use_iterable_dataset, config) |
|
|
| _auto_detect_bucketing_and_validate_batch_size(config) |
|
|
| |
| if config.channel_selector is not None: |
| logging.info('Using channel selector %s.', config.channel_selector) |
| cuts = cuts.map(partial(_select_channel, channel_selector=config.channel_selector)) |
|
|
| |
| cuts = cuts.map(partial(resample, sampling_rate=config.sample_rate), apply_fn=None) |
|
|
| |
| cuts = CutSet(LazyFlattener(cuts.map(_flatten_alt_text, apply_fn=None))) |
|
|
| if config.use_multimodal_sampling: |
| assert tokenizer is not None, ( |
| "You must pass a tokenizer to `get_lhotse_dataloader_from_config` in order to" |
| "read text-only datasets (enabled via use_multimodal_dataloading)" |
| ) |
|
|
| if tokenizer is not None and config.pretokenize: |
| if not use_iterable_dataset: |
| logging.warning( |
| "You are using a non-tarred dataset and requested tokenization during data sampling " |
| "(pretokenize=True). This will cause the tokenization to happen in the main (GPU) process," |
| "possibly impacting the training speed if your tokenizer is very large." |
| "If the impact is noticable, set pretokenize=False in dataloader config." |
| "(note: that will disable token-per-second filtering and 2D bucketing features)" |
| ) |
|
|
| if config.use_multimodal_sampling and config.cut_text_into_windows_tokens is not None: |
| cuts = CutSet( |
| LazyFlattener( |
| cuts.map( |
| partial( |
| _cut_text_into_windows, |
| num_tokens=config.cut_text_into_windows_tokens, |
| tokenizer=tokenizer, |
| ), |
| apply_fn=None, |
| ) |
| ) |
| ) |
|
|
| if config.prompt_format is not None: |
| cuts = cuts.map( |
| partial(tokenize_with_prompt, tokenizer=tokenizer, prompt_format=config.prompt_format), apply_fn=None |
| ) |
| else: |
| if not isinstance(tokenizer, TokenizerWrapper): |
| tokenizer = TokenizerWrapper(tokenizer) |
| cuts = cuts.map(partial(tokenize, tokenizer=tokenizer), apply_fn=None) |
|
|
| |
| |
| if config.noise_path is not None: |
| noise = guess_parse_cutset(config.noise_path) |
| |
| noise = noise.resample(config.sample_rate) |
| cuts = cuts.mix( |
| cuts=noise, |
| snr=tuple(config.noise_snr), |
| mix_prob=config.noise_mix_prob, |
| seed=config.shard_seed, |
| random_mix_offset=True, |
| ) |
|
|
| |
| |
| |
| |
| if config.perturb_speed: |
| cuts = CutSet.mux( |
| cuts, |
| cuts.perturb_speed(0.9), |
| cuts.perturb_speed(1.1), |
| ) |
|
|
| |
| if config.truncate_duration is not None: |
| cuts = cuts.truncate( |
| max_duration=config.truncate_duration, |
| offset_type=config.truncate_offset_type, |
| keep_excessive_supervisions=config.keep_excessive_supervisions, |
| ) |
| if config.cut_into_windows_duration is not None: |
| cuts = cuts.cut_into_windows( |
| duration=config.cut_into_windows_duration, |
| hop=config.cut_into_windows_hop, |
| keep_excessive_supervisions=config.keep_excessive_supervisions, |
| ) |
|
|
| if config.pad_min_duration is not None: |
| cuts = cuts.pad(duration=config.pad_min_duration, direction=config.pad_direction, preserve_id=True) |
|
|
| |
| |
| cuts = cuts.filter(DurationFilter(config.min_duration, config.max_duration)) |
| cuts = cuts.filter( |
| TokenCountFilter(config.min_tokens, config.max_tokens, measure_total_length=config.measure_total_length) |
| ) |
|
|
| |
| cuts = cuts.filter(ValidationStatusFilter(config.keep)) |
| |
| cuts = cuts.filter(CERFilter(config.max_cer)) |
| |
| cuts = cuts.filter(ContextSpeakerSimilarityFilter(config.min_context_speaker_similarity)) |
|
|
| if tokenizer is not None and config.pretokenize: |
| cuts = cuts.filter(TokenPerSecondFilter(config.min_tps, config.max_tps)) |
| cuts = cuts.filter(TokenPerTokenFilter(config.min_tpt, config.max_tpt)) |
|
|
| |
| |
| bucket_duration_bins = determine_bucket_duration_bins(config) |
| cuts, constraint = determine_sampling_constraint(cuts, bucket_duration_bins, config) |
|
|
| |
| if config.use_bucketing: |
| |
| |
| |
| |
| logging.info( |
| f"Creating a Lhotse DynamicBucketingSampler " |
| f"(max_batch_duration={config.batch_duration} max_batch_size={config.batch_size})" |
| ) |
| |
| sampler = DynamicBucketingSampler( |
| cuts, |
| constraint=constraint, |
| shuffle=config.shuffle, |
| drop_last=config.drop_last, |
| shuffle_buffer_size=config.shuffle_buffer_size, |
| seed=config.shard_seed, |
| num_buckets=config.num_buckets, |
| duration_bins=determine_bucket_duration_bins(config), |
| num_cuts_for_bins_estimate=config.num_cuts_for_bins_estimate, |
| buffer_size=config.bucket_buffer_size, |
| concurrent=config.concurrent_bucketing, |
| rank=0 if use_iterable_dataset else global_rank, |
| world_size=1 if use_iterable_dataset else world_size, |
| ) |
| else: |
| |
| |
| |
| logging.info( |
| f"Creating a Lhotse DynamicCutSampler (bucketing is disabled, " |
| f"(max_batch_duration={config.batch_duration} max_batch_size={config.batch_size})" |
| ) |
| sampler = DynamicCutSampler( |
| cuts, |
| constraint=constraint, |
| shuffle=config.shuffle, |
| drop_last=config.drop_last, |
| shuffle_buffer_size=config.shuffle_buffer_size, |
| seed=config.shard_seed, |
| rank=0 if use_iterable_dataset else global_rank, |
| world_size=1 if use_iterable_dataset else world_size, |
| ) |
|
|
| if config.concatenate_samples: |
| |
| |
| |
| |
| |
| |
| |
| sampler = sampler.map( |
| CutConcatenate( |
| gap=config.concatenate_gap_seconds, |
| duration_factor=config.concatenate_duration_factor, |
| ) |
| ) |
| if config.db_norm is not None: |
| sampler = sampler.map(partial(_normalize_loudness, db_norm=config.db_norm)) |
| if config.concatenate_merge_supervisions: |
| sampler = sampler.map(_merge_supervisions) |
|
|
| if config.lowpass_enabled: |
| if lhotse.get_current_resampling_backend() != "libsox": |
| logging.warning( |
| "Lowpass augmentation works best with libsox backend. Consider setting resamping backend in Lhotse to libsox." |
| ) |
| sampler = sampler.map( |
| LowpassUsingResampling( |
| frequencies_interval=OmegaConf.to_container(config.lowpass_frequencies_interval), |
| p=config.lowpass_prob, |
| seed=config.shard_seed, |
| ) |
| ) |
|
|
| if config.clipping_enabled: |
| sampler = sampler.map( |
| ClippingTransform( |
| gain_db=OmegaConf.to_container(config.clipping_gain_db), |
| normalize=config.clipping_normalize, |
| p=config.clipping_prob, |
| p_hard=config.clipping_prob_hard, |
| oversampling=config.clipping_oversampling, |
| seed=config.shard_seed, |
| ) |
| ) |
|
|
| if config.rir_enabled: |
| sampler = sampler.map( |
| ReverbWithImpulseResponse( |
| rir_recordings=RecordingSet.from_file(config.rir_path) if config.rir_path is not None else None, |
| p=config.rir_prob, |
| randgen=random.Random(config.seed), |
| ) |
| ) |
|
|
| if config.compression_enabled: |
| sampler = sampler.map( |
| Compress( |
| codecs=OmegaConf.to_container(config.compression_codecs), |
| p=config.compression_prob, |
| compression_level=OmegaConf.to_container(config.compression_level_interval), |
| codec_weights=( |
| OmegaConf.to_container(config.compression_codec_weights) |
| if config.compression_codec_weights |
| else config.compression_codec_weights |
| ), |
| compress_custom_fields=config.compression_enable_for_custom_fields, |
| seed=config.shard_seed, |
| ) |
| ) |
|
|
| return sampler, use_iterable_dataset |
|
|
|
|
| def determine_sampling_constraint(cuts: CutSet, bucket_duration_bins, config) -> tuple[CutSet, SamplingConstraint]: |
| """ |
| Select an appropriate sampling strategy (constraint) for Lhotse samplers based on the configuration. |
| Sampling constraint affects the batch size (static/dynamic) and bucketing behaviour (1D/2D). |
| It is the appropriate customization point to introduce support of other modalities, |
| as it defines a method for example sequence length measurement (audio duration, text tokens, etc.). |
| |
| Some constraints apply extra filter on ``cuts`` which is why we accept and return the ``CutSet``. |
| |
| Lhotse's default is :class:`TimeConstraint` for regular audio data, other available options are |
| multimodal constraints (joint text + audio) and their 2D bucketing extensions. |
| """ |
| if config.use_multimodal_sampling: |
| if config.bucket_batch_size is not None: |
| assert ( |
| bucket_duration_bins is not None |
| ), "Cannot use bucket_batch_size option if bucket_duration_bins are not provided." |
| constraint = MultimodalFixedBucketBatchSizeConstraint2D( |
| max_seq_len_buckets=bucket_duration_bins, |
| batch_sizes=config.bucket_batch_size, |
| token_equivalent_duration=config.token_equivalent_duration, |
| strict_2d=config.bucketing_2d_strict_mode, |
| max_ratio=config.max_tpt if isinstance(config.max_tpt, Sequence) else None, |
| measure_total_length=config.measure_total_length, |
| ) |
| cuts = cuts.filter(BucketingFilter(constraint)) |
| else: |
| constraint = MultimodalSamplingConstraint( |
| token_equivalent_duration=config.token_equivalent_duration, |
| batch_size=config.batch_size, |
| batch_tokens=config.batch_tokens, |
| quadratic_factor=config.quadratic_factor, |
| measure_total_length=config.measure_total_length, |
| ) |
| else: |
| if config.bucket_batch_size is not None: |
| assert ( |
| bucket_duration_bins is not None |
| ), "Cannot use bucket_batch_size option if bucket_duration_bins are not provided." |
| constraint = FixedBucketBatchSizeConstraint2D( |
| max_seq_len_buckets=bucket_duration_bins, |
| batch_sizes=config.bucket_batch_size, |
| strict_2d=config.bucketing_2d_strict_mode, |
| max_ratio=config.max_tps if isinstance(config.max_tps, Sequence) else None, |
| ) |
| cuts = cuts.filter(BucketingFilter(constraint)) |
| else: |
| constraint = TimeConstraint( |
| max_cuts=config.batch_size, |
| max_duration=config.batch_duration, |
| quadratic_duration=config.quadratic_duration, |
| ) |
| return cuts, constraint |
|
|
|
|
| def _auto_detect_bucketing_and_validate_batch_size(config) -> None: |
| """ |
| Auto-enable ``use_bucketing`` when bucketing params are set, and validate |
| that at least one valid batch size combination is configured. |
| """ |
| |
| if not config.use_bucketing: |
| if config.bucket_batch_size is not None: |
| logging.info("Auto-enabling use_bucketing=True because bucket_batch_size is set.") |
| config.use_bucketing = True |
| elif config.bucket_duration_bins is not None: |
| logging.info("Auto-enabling use_bucketing=True because bucket_duration_bins is set.") |
| config.use_bucketing = True |
|
|
| |
| has_batch_size = config.batch_size is not None |
| has_batch_duration = not config.use_multimodal_sampling and config.batch_duration is not None |
| has_bucket_config = config.bucket_duration_bins is not None and config.bucket_batch_size is not None |
| has_batch_tokens = config.use_multimodal_sampling and config.batch_tokens is not None |
| if not (has_batch_size or has_batch_duration or has_bucket_config or has_batch_tokens): |
| raise ValueError( |
| "Batch size is not configured. Please set one of the following:\n" |
| " 1. batch_size\n" |
| " 2. batch_duration (when use_multimodal_sampling=False)\n" |
| " 3. bucket_duration_bins and bucket_batch_size (enables bucketing)\n" |
| " 4. batch_tokens (when use_multimodal_sampling=True)" |
| ) |
|
|
|
|
| def determine_bucket_duration_bins(config): |
| """ |
| Returns appropriate bucket bins based on configuration. |
| If user provided them explicitly, we just pass them along; |
| otherwise, we try to create provisional bins when min/max duration is available. |
| We might return None if it's impossible to determine the bins without computing data statistics, |
| in which case it will be automatically done at the start of training (but may take a few minutes). |
| """ |
| if config.bucket_duration_bins is not None: |
| |
| ans = OmegaConf.to_container(config.bucket_duration_bins) |
| if isinstance(ans[0], Sequence): |
| |
| |
| ans = [tuple(item) for item in ans] |
| return ans |
| |
| if config.use_multimodal_sampling: |
| |
| |
| |
| return None |
| elif config.max_duration is not None and config.max_duration < float("inf"): |
| |
| |
| begin = config.min_duration if config.min_duration is not None and config.min_duration > 0 else 0.0 |
| end = config.max_duration |
| return np.linspace(begin, end, config.num_buckets + 1)[1:-1].tolist() |
| else: |
| |
| |
| |
| return None |
|
|
|
|
| def make_structured_with_schema_warnings(config: Union[DictConfig, dict]) -> DictConfig: |
| """ |
| Checks the schema and fills missing default option values. |
| Warns the user if any of the fields are not supported by the current schema |
| but does not raise exceptions. |
| """ |
| default = OmegaConf.structured(LhotseDataLoadingConfig) |
| if not isinstance(config, DictConfig): |
| config = DictConfig(config) |
|
|
| |
| supported_keys = set(OmegaConf.to_container(default).keys()) |
| received_keys = set(OmegaConf.to_container(config).keys()) |
| unsupported_keys = received_keys - supported_keys |
| unsupported_keys.discard("use_lhotse") |
| if unsupported_keys: |
| logging.warning( |
| f"The following configuration keys are ignored by Lhotse dataloader: {','.join(unsupported_keys)}", |
| ) |
| config = OmegaConf.masked_copy(config, list(supported_keys)) |
|
|
| config = OmegaConf.merge(default, config) |
|
|
| if config.get("tarred_random_access", False): |
| logging.warning( |
| "Option 'tarred_random_access' is deprecated and replaced with 'skip_missing_manifest_entries'.", |
| ) |
| config.skip_missing_manifest_entries = True |
| if config.skip_missing_manifest_entries: |
| logging.warning( |
| "Note: skip_missing_manifest_entries is set to True. " |
| "If any of your manifests and tar files are mismatched, the entire " |
| "tar file will be skipped without warning. It's your responsibility " |
| "to ensure data integrity with this setting." |
| ) |
|
|
| return config |
|
|
|
|
| def tokenize(example, tokenizer): |
| """Return the text in the example according to the provided tokenizer.""" |
| if isinstance(example, Cut): |
| for s in example.supervisions: |
| if s.text is not None: |
| s.tokens = np.asarray(tokenizer(s.text, s.language)) |
| elif hasattr(example, "tokenize") and callable(example.tokenize): |
| example = example.tokenize(tokenizer) |
| else: |
| raise RuntimeError(f"Unsupported type of example: {type(example)}") |
| return example |
|
|
|
|
| def tokenize_with_prompt(example, tokenizer, prompt_format: str | PromptFormatter): |
| """Tokenize the example with the provided tokenizer and prompt format.""" |
| if isinstance(prompt_format, str): |
| prompt_format = PromptFormatter.resolve(prompt_format)(tokenizer) |
| encoded = apply_prompt_format_fn(example, prompt_format) |
| for key, value in encoded.items(): |
| setattr(example, key, value) |
| return example |
|
|
|
|
| |
| |
| |
| |
|
|
|
|
| def _normalize_loudness(cuts: CutSet, db_norm: float) -> CutSet: |
| return cuts.normalize_loudness(target=db_norm, mix_first=False) |
|
|
|
|
| def _merge_supervisions(cuts: CutSet) -> CutSet: |
| return cuts.merge_supervisions() |
|
|
|
|
| def _flatten_alt_text(cut) -> list: |
| ans = [cut] |
| if not isinstance(cut, Cut) or cut.custom is None or cut.custom.get("alt_text") is None: |
| return ans |
| cut = cut.move_to_memory(audio_format="wav") |
| |
| paired_text = cut.custom.pop("alt_text") |
| for data in paired_text.values(): |
| |
| data = data.copy() |
| text_instance = cut.map_supervisions(lambda s: fastcopy(s, text=data["text"], language=data["lang"])) |
| text_instance.custom = {"text": data.pop("text"), "lang": data.pop("lang"), **data} |
| ans.append(text_instance) |
| return ans |
|
|
|
|
| def maybe_set_cuda_expandable_segments(enabled: bool): |
| """ |
| Configures PyTorch memory allocator to expand existing allocated segments |
| instead of re-allocating them when tensor shape grows. |
| This can help speed up the training when sequence length and/or batch size change often, |
| and makes GPU more robust towards OOM. |
| |
| See here for more details: |
| pytorch.org/docs/stable/notes/cuda.html#optimizing-memory-usage-with-pytorch-cuda-alloc-conf |
| """ |
| if enabled and torch.cuda.is_available(): |
| if ( |
| (value := os.environ.get("PYTORCH_CUDA_ALLOC_CONF")) is not None |
| and len(value) > 0 |
| and "expandable_segments:True" not in value |
| ): |
| warnings.warn( |
| "You have set PYTORCH_CUDA_ALLOC_CONF without expandable_segments:True option. " |
| "We're setting that option anyway. To disable it, set cuda_expandable_segments=False " |
| "in NeMo dataloader configuration." |
| ) |
|
|
| try: |
| torch.cuda.memory._set_allocator_settings("expandable_segments:True") |
| except RuntimeError: |
| logging.info( |
| "Failed to set expandable_segments:True for PyTorch CUDA allocator. " |
| "You may get training speed improvements if you enable this " |
| ) |
|
|
|
|
| def resample(example, sampling_rate): |
| from nemo.collections.common.data.lhotse.text_adapters import NeMoMultimodalConversation |
|
|
| if isinstance(example, Cut): |
| return example.resample(sampling_rate) |
| elif isinstance(example, NeMoMultimodalConversation): |
| for turn in example.turns: |
| if hasattr(turn, "cut"): |
| turn.cut = turn.cut.resample(sampling_rate) |
| return example |
| else: |
| return example |
|
|
|
|
| def _select_channel(cut, channel_selector: int | str) -> list: |
| if isinstance(channel_selector, int): |
| channel_idx = channel_selector |
| elif isinstance(channel_selector, str): |
| if channel_selector in cut.custom: |
| channel_idx = cut.custom[channel_selector] |
| else: |
| raise ValueError(f"Channel selector {channel_selector} not found in cut.custom") |
|
|
| if channel_idx >= cut.num_channels: |
| raise ValueError( |
| f"Channel index {channel_idx} is larger than the actual number of channels {cut.num_channels}" |
| ) |
|
|
| if cut.num_channels == 1: |
| |
| return cut |
| else: |
| |
| return cut.with_channels(channel_idx) |
|
|
|
|
| def _cut_text_into_windows(cut, num_tokens: int, tokenizer) -> list: |
| """Split cut.text into chunks of num_tokens, creating new cuts with copied attributes from the original cut. |
| |
| This only applies to pretraining data without chat template. |
| |
| Args: |
| cut: TextExample, the cut object containing text to split |
| num_tokens: The number of tokens per chunk |
| tokenizer: The tokenizer to use to convert tokens to text |
| |
| Returns: |
| list: A list of new cut objects, each containing a chunk of tokens |
| """ |
| tokens = tokenizer.text_to_ids(cut.text) |
| ans = [] |
| for i in range(0, len(tokens), num_tokens): |
| new_cut = type(cut)( |
| text=tokenizer.ids_to_text(tokens[i : i + num_tokens]), |
| language=cut.language, |
| custom=deepcopy(cut.custom), |
| ) |
| ans.append(new_cut) |
| return ans |
|
|