speechbrain.integrations.k2_fsa.graph_compiler moduleο
Graph compiler class to create, store, and use k2 decoding graphs in speechbrain. Limits the output words to the ones in the lexicon.
This code is an extension, and therefore heavily inspired or taken from icefallβs (https://github.com/k2-fsa/icefall) graph compiler.
- Authors:
Pierre Champion 2023
Zeyu Zhao 2023
Georgios Karakasidis 2023
Summaryο
Classes:
This class is used to compile decoding graphs for CTC training. |
|
This abstract class is used to compile graphs for training and decoding. |
Referenceο
- class speechbrain.integrations.k2_fsa.graph_compiler.GraphCompiler[source]ο
Bases:
ABCThis abstract class is used to compile graphs for training and decoding.
- abstract property topo: k2.Fsaο
Return the topology used to compile the graph.
- abstract property deviceο
Return the device used to compile the graph.
- abstractmethod compile(texts: List[str], is_training: bool = True) Tuple[k2.Fsa, Tensor][source]ο
Compile the graph for the given texts.
- Parameters:
texts (List[str]) β
A list of strings. Each string contains a sentence for an utterance. A sentence consists of spaces separated words. An example
textslooks like:[βhello worldβ, βCTC training with k2β]
is_training (bool) β Indictating whether this is for training or not (OOV warning in training).
- Returns:
graph (GraphCompiler) β An FsaVec, the composition result of
self.ctc_topoand the transcript FSA.target_lens (Torch.tensor) β It is an long tensor of shape (batch,). It contains lengths of each target sequence.
- compile_HL(cache_dir: str | None = None, cache: bool = False)[source]ο
Compile the decoding graph by composing H with L. This is for decoding without language model.
- class speechbrain.integrations.k2_fsa.graph_compiler.CtcGraphCompiler(_lexicon: Lexicon, device: device, need_repeat_flag: bool = False)[source]ο
Bases:
GraphCompilerThis class is used to compile decoding graphs for CTC training.
- Parameters:
_lexicon (Lexicon) β It is built from
data/lang/lexicon.txt.device (torch.device) β The device to use for operations compiling transcripts to FSAs.
need_repeat_flag (bool) β If True, will add an attribute named
_is_repeat_token_to ctc_topo indicating whether this token is a repeat token in ctc graph. This attribute is needed to implement delay-penalty for phone-based ctc loss. See https://github.com/k2-fsa/k2/pull/1086 for more details. Note: The above change MUST be included in k2 to enable this flag so make sure you have an up-to-date version.
Example
>>> import torch >>> from speechbrain.integrations.k2_fsa.losses import ctc_k2 >>> from speechbrain.integrations.k2_fsa.graph_compiler import ( ... CtcGraphCompiler, ... ) >>> from speechbrain.integrations.k2_fsa.lexicon import Lexicon >>> from speechbrain.integrations.k2_fsa.prepare_lang import prepare_lang
>>> # Create a random batch of log-probs >>> batch_size = 4
>>> log_probs = torch.randn(batch_size, 100, 30) >>> log_probs.requires_grad = True >>> # Assume all utterances have the same length so no padding was needed. >>> input_lens = torch.ones(batch_size) >>> # Create a small lexicon containing only two words and write it to a file. >>> lang_tmpdir = getfixture("tmpdir") >>> lexicon_sample = "hello h e l l o\nworld w o r l d\n<UNK> <unk>" >>> lexicon_file = lang_tmpdir.join("lexicon.txt") >>> lexicon_file.write(lexicon_sample) >>> # Create a lang directory with the lexicon and L.pt, L_inv.pt, L_disambig.pt >>> prepare_lang(lang_tmpdir) >>> # Create a lexicon object >>> lexicon = Lexicon(lang_tmpdir) >>> # Create a random decoding graph >>> graph = CtcGraphCompiler( ... lexicon, ... log_probs.device, ... ) >>> isinstance(graph.topo, k2.Fsa) True
- property topoο
Return the ctc_topo.
- property lexiconο
Return the lexicon.
- property deviceο
Return the device used for compiling graphs.
- compile(texts: List[str], is_training: bool = True) Tuple[k2.Fsa, Tensor][source]ο
Build decoding graphs by composing ctc_topo with given transcripts.
- Parameters:
texts (List[str]) β
A list of strings. Each string contains a sentence for an utterance. A sentence consists of spaces separated words. An example
textslooks like:[βhello worldβ, βCTC training with k2β]
is_training (bool) β Indictating whether this is for training or not (OOV warning in training).
- Returns:
graph (GraphCompiler) β An FsaVec, the composition result of
self.ctc_topoand the transcript FSA.target_lens (Torch.tensor) β It is an long tensor of shape (batch,). It contains lengths of each target sequence.