-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
NeMo + Lhotse integration #7880
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
252fd43
Lhotse integration squashed PR
pzelasko 0c31f27
Code review - Som
pzelasko b85a4c5
Merge branch 'main' into feature/lhotse-integration
pzelasko ffcf5d4
Update copyright headers to 2024
pzelasko ebc0dea
Merge branch 'feature/lhotse-integration' of https://github.com/pzela…
pzelasko b3cfc8a
Fix NLP imports
pzelasko bcac686
Code review - Vahid
pzelasko 24fbf53
Merge branch 'main' into feature/lhotse-integration
pzelasko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
# Copyright (c) 2024, 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. | ||
|
||
from typing import Dict, Optional, Tuple | ||
|
||
import torch.utils.data | ||
from lhotse.dataset import AudioSamples | ||
from lhotse.dataset.collation import collate_vectors | ||
|
||
from nemo.collections.common.tokenizers.aggregate_tokenizer import AggregateTokenizer | ||
from nemo.collections.common.tokenizers.tokenizer_spec import TokenizerSpec | ||
from nemo.core.neural_types import AudioSignal, LabelsType, LengthsType, NeuralType | ||
|
||
|
||
class LhotseSpeechToTextBpeDataset(torch.utils.data.Dataset): | ||
""" | ||
This dataset is based on BPE datasets from audio_to_text.py. | ||
Unlike native NeMo datasets, Lhotse dataset defines only the mapping from | ||
a CutSet (meta-data) to a mini-batch with PyTorch tensors. | ||
Specifically, it performs tokenization, I/O, augmentation, and feature extraction (if any). | ||
Managing data, sampling, de-duplication across workers/nodes etc. is all handled | ||
by Lhotse samplers instead. | ||
""" | ||
|
||
@property | ||
def output_types(self) -> Optional[Dict[str, NeuralType]]: | ||
return { | ||
'audio_signal': NeuralType(('B', 'T'), AudioSignal()), | ||
'a_sig_length': NeuralType(tuple('B'), LengthsType()), | ||
'transcripts': NeuralType(('B', 'T'), LabelsType()), | ||
'transcript_length': NeuralType(tuple('B'), LengthsType()), | ||
'sample_id': NeuralType(tuple('B'), LengthsType(), optional=True), | ||
} | ||
|
||
def __init__(self, tokenizer): | ||
super().__init__() | ||
self.tokenizer = TokenizerWrapper(tokenizer) | ||
self.load_audio = AudioSamples(fault_tolerant=True) | ||
|
||
def __getitem__(self, cuts) -> Tuple[torch.Tensor, ...]: | ||
audio, audio_lens, cuts = self.load_audio(cuts) | ||
tokens = [torch.as_tensor(self.tokenizer(c.supervisions[0].text, c.supervisions[0].language)) for c in cuts] | ||
token_lens = torch.tensor([t.size(0) for t in tokens], dtype=torch.long) | ||
tokens = collate_vectors(tokens, padding_value=0) | ||
return audio, audio_lens, tokens, token_lens | ||
|
||
|
||
class TokenizerWrapper: | ||
""" | ||
Provide a unified interface for NeMo Tokenizer, AggregateTokenizer, and (char) Parser. | ||
""" | ||
|
||
def __init__(self, tokenizer): | ||
self._tokenizer = tokenizer | ||
if isinstance(tokenizer, AggregateTokenizer): | ||
self._impl = self._call_agg_tokenizer | ||
elif isinstance(tokenizer, TokenizerSpec): | ||
self._impl = self._call_tokenizer | ||
else: | ||
self._impl = self._call_parser | ||
|
||
def __call__(self, text: str, lang: str | None = None): | ||
return self._impl(text, lang) | ||
|
||
def _call_agg_tokenizer(self, text: str, lang: str | None = None): | ||
assert lang is not None, "Expected 'lang' to be set for AggregateTokenizer." | ||
return self._tokenizer.text_to_ids(text, lang) | ||
|
||
def _call_tokenizer(self, text: str, lang: str | None = None): | ||
return self._tokenizer.text_to_ids(text) | ||
|
||
def _call_parser(self, text: str, lang: str | None = None): | ||
return self._tokenizer(text) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this needed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it prevents Lhotse from pulling/checking torchaudio.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the user need to set it when not using the dockers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only if they can't have torchaudio installed for some reason. IIUC the issue with torchaudio mostly stems from upstream docker images not having it pre-built and the need to build from source when building NeMo containers (which may fail for a number of reasons). But outside these docker images it's straightforward to pip/conda install torchaudio alongside torch. WDYT?