espnet2.lm package¶
espnet2.lm.abs_model¶

class
espnet2.lm.abs_model.
AbsLM
(*args, **kwargs)[source]¶ Bases:
torch.nn.modules.module.Module
,espnet.nets.scorer_interface.BatchScorerInterface
,abc.ABC
The abstract LM class
To share the loss calculation way among different models, We uses delegate pattern here: The instance of this class should be passed to “LanguageModel”
>>> from espnet2.lm.abs_model import AbsLM >>> lm = AbsLM() >>> model = LanguageESPnetModel(lm=lm)
This “model” is one of mediator objects for “Task” class.
Initializes internal Module state, shared by both nn.Module and ScriptModule.

abstract
forward
(input: torch.Tensor, hidden: torch.Tensor) → Tuple[torch.Tensor, torch.Tensor][source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

abstract
espnet2.lm.__init__¶
espnet2.lm.seq_rnn_lm¶
Sequential implementation of Recurrent Neural Network Language Model.

class
espnet2.lm.seq_rnn_lm.
SequentialRNNLM
(vocab_size: int, unit: int = 650, nhid: Optional[int] = None, nlayers: int = 2, dropout_rate: float = 0.0, tie_weights: bool = False, rnn_type: str = 'lstm', ignore_id: int = 0)[source]¶ Bases:
espnet2.lm.abs_model.AbsLM
Sequential RNNLM.
See also

batch_score
(ys: torch.Tensor, states: torch.Tensor, xs: torch.Tensor) → Tuple[torch.Tensor, torch.Tensor][source]¶ Score new token batch.
 Parameters:
ys (torch.Tensor) – torch.int64 prefix tokens (n_batch, ylen).
states (List[Any]) – Scorer states for prefix tokens.
xs (torch.Tensor) – The encoder feature that generates ys (n_batch, xlen, n_feat).
 Returns:
 Tuple of
batchfied scores for next token with shape of (n_batch, n_vocab) and next state list for ys.
 Return type:
tuple[torch.Tensor, List[Any]]

forward
(input: torch.Tensor, hidden: torch.Tensor) → Tuple[torch.Tensor, torch.Tensor][source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

score
(y: torch.Tensor, state: Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]], x: torch.Tensor) → Tuple[torch.Tensor, Union[torch.Tensor, Tuple[torch.Tensor, torch.Tensor]]][source]¶ Score new token.
 Parameters:
y – 1D torch.int64 prefix tokens.
state – Scorer state for prefix tokens
x – 2D encoder feature that generates ys.
 Returns:
 Tuple of
torch.float32 scores for next token (n_vocab) and next state for ys

espnet2.lm.espnet_model_multitask¶

class
espnet2.lm.espnet_model_multitask.
ESPnetMultitaskLanguageModel
(lm: espnet2.lm.abs_model.AbsLM, vocab_size: int, token_list: Union[Tuple[str, ...], List[str]], ignore_id: int = 0, lsm_weight: float = 0.0, length_normalized_loss: bool = False, sos_syms: List[str] = ['<generatetext>', '<generatespeech>'], eos_sym: str = '<sos/eos>')[source]¶ Bases:
espnet2.train.abs_espnet_model.AbsESPnetModel

batchify_nll
(text: torch.Tensor, text_lengths: torch.Tensor, batch_size: int = 100) → Tuple[torch.Tensor, torch.Tensor][source]¶ Compute negative log likelihood(nll) from transformer language model
To avoid OOM, this fuction seperate the input into batches. Then call nll for each batch and combine and return results. :param text: (Batch, Length) :param text_lengths: (Batch,) :param batch_size: int, samples each batch contain when computing nll,
you may change this to avoid OOM or increase

collect_feats
(text: torch.Tensor, text_lengths: torch.Tensor, **kwargs) → Dict[str, torch.Tensor][source]¶

forward
(text: torch.Tensor, text_lengths: torch.Tensor, **kwargs) → Tuple[torch.Tensor, Dict[str, torch.Tensor], torch.Tensor][source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

nll
(text: torch.Tensor, text_lengths: torch.Tensor, max_length: Optional[int] = None) → Tuple[torch.Tensor, torch.Tensor][source]¶ Compute negative log likelihood (nll)
 NOTE(yifan): We only use nll to calculate perplexity,
so there is no condition in each sentence.
Normally, this function is called in batchify_nll. :param text: (Batch, Length) :param text_lengths: (Batch,) :param max_lengths: int

espnet2.lm.huggingface_pretrained_opt_lm¶

class
espnet2.lm.huggingface_pretrained_opt_lm.
HuggingfaceOPTModel
(vocab_size: int, opt_name: str)[source]¶ Bases:
espnet2.lm.abs_model.AbsLM

batch_score
(ys: torch.Tensor, states: List[Any], xs: torch.Tensor) → Tuple[torch.Tensor, List[Any]][source]¶ Score new token batch.
 Parameters:
ys (torch.Tensor) – torch.int64 prefix tokens (n_batch, ylen).
states (List[Any]) – Scorer states for prefix tokens.
xs (torch.Tensor) – The encoder feature that generates ys (n_batch, xlen, n_feat).
 Returns:
 Tuple of
batchfied scores for next token with shape of (n_batch, vocab_size) and next state list for ys.
 Return type:
tuple[torch.Tensor, List[Any]]

forward
(input: torch.Tensor, hidden: None) → Tuple[torch.Tensor, None][source]¶ Compute LM loss value from buffer sequences.
 Parameters:
input (torch.Tensor) – Input ids. (batch, len)
hidden (torch.Tensor) – Target ids. (batch, len)

score
(y: torch.Tensor, state: Any, x: torch.Tensor) → Tuple[torch.Tensor, Any][source]¶ Score new token.
 Parameters:
y (torch.Tensor) – 1D torch.int64 prefix tokens.
state – Scorer state for prefix tokens
x (torch.Tensor) – encoder feature that generates ys.
 Returns:
 Tuple of
torch.float32 scores for next token (vocab_size) and next state for ys
 Return type:
tuple[torch.Tensor, Any]

espnet2.lm.transformer_lm¶

class
espnet2.lm.transformer_lm.
TransformerLM
(vocab_size: int, pos_enc: str = None, embed_unit: int = 128, att_unit: int = 256, head: int = 2, unit: int = 1024, layer: int = 4, dropout_rate: float = 0.1, positional_dropout_rate: float = 0.1, attention_dropout_rate: float = 0.1)[source]¶ Bases:
espnet2.lm.abs_model.AbsLM

batch_score
(ys: torch.Tensor, states: List[Any], xs: torch.Tensor) → Tuple[torch.Tensor, List[Any]][source]¶ Score new token batch.
 Parameters:
ys (torch.Tensor) – torch.int64 prefix tokens (n_batch, ylen).
states (List[Any]) – Scorer states for prefix tokens.
xs (torch.Tensor) – The encoder feature that generates ys (n_batch, xlen, n_feat).
 Returns:
 Tuple of
batchfied scores for next token with shape of (n_batch, vocab_size) and next state list for ys.
 Return type:
tuple[torch.Tensor, List[Any]]

forward
(input: torch.Tensor, hidden: None) → Tuple[torch.Tensor, None][source]¶ Compute LM loss value from buffer sequences.
 Parameters:
input (torch.Tensor) – Input ids. (batch, len)
hidden (torch.Tensor) – Target ids. (batch, len)

score
(y: torch.Tensor, state: Any, x: torch.Tensor) → Tuple[torch.Tensor, Any][source]¶ Score new token.
 Parameters:
y (torch.Tensor) – 1D torch.int64 prefix tokens.
state – Scorer state for prefix tokens
x (torch.Tensor) – encoder feature that generates ys.
 Returns:
 Tuple of
torch.float32 scores for next token (vocab_size) and next state for ys
 Return type:
tuple[torch.Tensor, Any]

espnet2.lm.espnet_model¶

class
espnet2.lm.espnet_model.
ESPnetLanguageModel
(lm: espnet2.lm.abs_model.AbsLM, vocab_size: int, ignore_id: int = 0)[source]¶ Bases:
espnet2.train.abs_espnet_model.AbsESPnetModel

batchify_nll
(text: torch.Tensor, text_lengths: torch.Tensor, batch_size: int = 100) → Tuple[torch.Tensor, torch.Tensor][source]¶ Compute negative log likelihood(nll) from transformer language model
To avoid OOM, this fuction seperate the input into batches. Then call nll for each batch and combine and return results. :param text: (Batch, Length) :param text_lengths: (Batch,) :param batch_size: int, samples each batch contain when computing nll,
you may change this to avoid OOM or increase

collect_feats
(text: torch.Tensor, text_lengths: torch.Tensor, **kwargs) → Dict[str, torch.Tensor][source]¶

forward
(text: torch.Tensor, text_lengths: torch.Tensor, **kwargs) → Tuple[torch.Tensor, Dict[str, torch.Tensor], torch.Tensor][source]¶ Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

nll
(text: torch.Tensor, text_lengths: torch.Tensor, max_length: Optional[int] = None) → Tuple[torch.Tensor, torch.Tensor][source]¶ Compute negative log likelihood(nll)
Normally, this function is called in batchify_nll. :param text: (Batch, Length) :param text_lengths: (Batch,) :param max_lengths: int
