Criterion#

BaseClasses#

class ofasys.engine.criterion.base.BaseCriterion(task, cfg: Optional[CriterionConfig] = None)[source]#

Criterion module is responsible for calling the model and calculating the loss.

Parameters
  • task – the task corresponding to the criterion.

  • cfg (CriterionConfig) – the config to the criterion.

forward(model, sample, update_num=0, reduce=True)[source]#

Calling the model, calculating the loss and prepare logging_output for metrics.

Parameters
  • model – the model for criterion.

  • sample (Dict[str, Any]) – the batched samples for calculating loss.

  • update_num (Int) – the number of current update steps, default: 0.

  • reduce (Bool) – if true, it will return the sum of losses. Otherwise, it will return the loss item for each sample. default: true.

static logging_outputs_can_be_summed() bool[source]#

Whether the logging outputs returned by forward can be summed across workers prior to calling reduce_metrics. Setting this to True will improve distributed training speed.

classmethod reduce_metrics(logging_outputs: List[Dict[str, Any]], prefix_name=None) None[source]#

Aggregate logging outputs from data parallel training.

class ofasys.engine.criterion.base.CriterionConfig(_name: Union[str, NoneType] = None, is_active: bool = False, weight: float = 1.0)[source]#
is_active: bool = False#
weight: float = 1.0#

CrossEntropyLoss#

class ofasys.engine.criterion.label_smoothed_cross_entropy.LabelSmoothedCrossEntropyCriterion(task, cfg: CriterionConfig)[source]#

Bases: BaseCriterion

This criterion will compute label-smoothed cross entropy loss and return it.

Parameters
  • task – the task corresponding to the criterion.

  • cfg (CriterionConfig) – the config to the criterion.

forward(model, sample, update_num=0, reduce=True)[source]#

Compute the loss for the given sample.

Parameters
  • model – the model for criterion.

  • sample (Dict[str, Any]) – the batched samples for calculating loss.

  • update_num (Int) – the number of current update steps, default: 0.

  • reduce (Bool) – if true, it will return the sum of losses. Otherwise, it will return the loss item for each sample. default: true.

Returns

the calculated loss sample_size: this will be used as the denominator for the gradient logging_output: logging outputs to display while training

Return type

loss

static logging_outputs_can_be_summed() bool[source]#

Whether the logging outputs returned by forward can be summed across workers prior to calling reduce_metrics. Setting this to True will improve distributed training speed.

classmethod reduce_metrics(logging_outputs, prefix_name=None) None[source]#

Aggregate logging outputs from data parallel training.

class ofasys.engine.criterion.label_smoothed_cross_entropy.LabelSmoothedCrossEntropyCriterionConfig(_name: Optional[str] = None, is_active: bool = False, weight: float = 1.0, label_smoothing: float = 0.0, report_accuracy: bool = False, ignore_prefix_size: int = 0, sentence_avg: bool = False, drop_worst_ratio: float = 0.0, drop_worst_after: int = 0, constraint_range: Optional[str] = None)[source]#

Bases: CriterionConfig

Parameters
  • label_smoothing (Float) – epsilon for label smoothing. Default: 0.0.

  • report_accuracy (Bool) – whether to report accuracy metrics. Default: false.

  • ignore_prefix_size (Int) – ignore first N tokens. Default: 0.

  • sentence_avg (Bool) – if true, the gradient will be normalized by the number of sentences.

  • drop_worst_ratio (Float) – when update_num > drop_worst_after, the drop_worst_ratio * 100% worst sample will be discarded.

  • drop_worst_after (Int) – steps for discarding bad samples.

  • constraint_range (Optional[str]) – only [constraint_start, constraint_end) range in the vocabulary is involved in loss calculation.

constraint_range: Optional[str] = None#
drop_worst_after: int = 0#
drop_worst_ratio: float = 0.0#
ignore_prefix_size: int = 0#
label_smoothing: float = 0.0#
report_accuracy: bool = False#
sentence_avg: bool = False#

DiffusionLoss#

class ofasys.engine.criterion.diffusion_loss.DiffusionCriterion(task, cfg: DiffusionCriterionConfig)[source]#

Bases: BaseCriterion

Parameters
  • task – the task corresponding to the criterion.

  • cfg (CriterionConfig) – the config to the criterion.

forward(model, sample, update_num=0, reduce=True)[source]#

Compute the loss for the given sample.

Returns a tuple with three elements: 1) the loss 2) the sample size, which is used as the denominator for the gradient 3) logging outputs to display while training

static logging_outputs_can_be_summed() bool[source]#

Whether the logging outputs returned by forward can be summed across workers prior to calling reduce_metrics. Setting this to True will improve distributed training speed.

classmethod reduce_metrics(logging_outputs, prefix_name=None) None[source]#

Aggregate logging outputs from data parallel training.

ScstRewardLoss#

class ofasys.engine.criterion.scst_loss.ScstRewardCriterion(task, cfg: CriterionConfig)[source]#

Bases: BaseCriterion

This criterion computes Self-critical Sequence Training (SCST) loss, which is proposed for image captioning. Details for Self-critical Sequence Training for Image Captioning (https://arxiv.org/abs/1612.00563)

Parameters
  • task – the task corresponding to the criterion.

  • cfg (CriterionConfig) – the config to the criterion.

forward(model, sample, update_num=0, reduce=True)[source]#

Compute the loss for the given sample.

Returns a tuple with three elements: 1) the loss 2) the sample size, which is used as the denominator for the gradient 3) logging outputs to display while training

static logging_outputs_can_be_summed() bool[source]#

Whether the logging outputs returned by forward can be summed across workers prior to calling reduce_metrics. Setting this to True will improve distributed training speed.

classmethod reduce_metrics(logging_outputs, prefix_name=None) None[source]#

Aggregate logging outputs from data parallel training.

class ofasys.engine.criterion.scst_loss.ScstRewardCriterionConfig(_name: Union[str, NoneType] = None, is_active: bool = False, weight: float = 1.0, scst_cider_cached_tokens: str = 'coco-train-words.p', ignore_prefix_size: int = 0, sentence_avg: bool = False, constraint_range: Union[str, NoneType] = None)[source]#

Bases: CriterionConfig

constraint_range: Optional[str] = None#
ignore_prefix_size: int = 0#
scst_cider_cached_tokens: str = 'coco-train-words.p'#
sentence_avg: bool = False#

SpeechtoTextLoss#

class ofasys.engine.criterion.speech_to_text_loss.SpeechtoTextLoss(task, cfg: SpeechtoTextLossConfig)[source]#

Bases: BaseCriterion

Criterion for speech_to_text task. This criterion will compute label smoothed cross entropy loss and CTC loss, and return a weighted sum of these two.

Parameters
  • task – the task corresponding to the criterion.

  • cfg (CriterionConfig) – the config to the criterion.

forward(model, sample, update_num=0, reduce=True)[source]#

Calling the model, calculating the loss and prepare logging_output for metrics.

Parameters
  • model – the model for criterion.

  • sample (Dict[str, Any]) – the batched samples for calculating loss.

  • update_num (Int) – the number of current update steps, default: 0.

  • reduce (Bool) – if true, it will return the sum of losses. Otherwise, it will return the loss item for each sample. default: true.

get_normalized_probs_for_ctc(net_output, log_probs, dict_start=None, dict_end=None, blank_id=1)[source]#

Get normalized probabilities (or log probs) from a net’s output.

static logging_outputs_can_be_summed() bool[source]#

Whether the logging outputs returned by forward can be summed across workers prior to calling reduce_metrics. Setting this to True will improve distributed training speed.

classmethod reduce_metrics(logging_outputs, prefix_name=None) None[source]#

Aggregate logging outputs from data parallel training.

class ofasys.engine.criterion.speech_to_text_loss.SpeechtoTextLossConfig(_name: Union[str, NoneType] = None, is_active: bool = False, weight: float = 1.0, zero_infinity: bool = False, sentence_avg: bool = False, post_process: Union[str, NoneType] = 'sentencepiece', wer_kenlm_model: Union[str, NoneType] = None, wer_lexicon: Union[str, NoneType] = None, wer_lm_weight: float = 2.0, wer_word_score: float = -1.0, wer_args: Union[str, NoneType] = None, label_smoothing: float = 0.0, report_accuracy: bool = False, ignore_prefix_size: int = 0, ce_weight: float = 1.0, ctc_weight: float = 0.0, drop_worst_ratio: float = 0.0, drop_worst_after: int = 0, constraint_range: Union[str, NoneType] = None)[source]#

Bases: CriterionConfig

ce_weight: float = 1.0#
constraint_range: Optional[str] = None#
ctc_weight: float = 0.0#
drop_worst_after: int = 0#
drop_worst_ratio: float = 0.0#
ignore_prefix_size: int = 0#
label_smoothing: float = 0.0#
post_process: Optional[str] = 'sentencepiece'#
report_accuracy: bool = False#
sentence_avg: bool = False#
wer_args: Optional[str] = None#
wer_kenlm_model: Optional[str] = None#
wer_lexicon: Optional[str] = None#
wer_lm_weight: float = 2.0#
wer_word_score: float = -1.0#
zero_infinity: bool = False#

SpeechPretrainLoss#

class ofasys.engine.criterion.speech_pretrain_criterion.SpeechPretrainCriterion(task, cfg: SpeechPretrainCriterionConfig)[source]#

Bases: BaseCriterion

This criterion will compute masked audio models (MAM) loss and tacotron2 loss, and return a weighted sum of these two.

Parameters
  • task – the task corresponding to the criterion.

  • cfg (CriterionConfig) – the config to the criterion.

static aggregate_logging_outputs(logging_outputs)[source]#

Aggregate logging outputs from data parallel training.

forward(model, sample, reduction='sum', update_num=None, log_pred=False)[source]#

Compute the loss for the given sample. Returns a tuple with three elements: 1) the loss 2) the sample size, which is used as the denominator for the gradient 3) logging outputs to display while training

static logging_outputs_can_be_summed() bool[source]#

Whether the logging outputs returned by forward can be summed across workers prior to calling reduce_metrics. Setting this to True will improve distributed training speed.

classmethod reduce_metrics(logging_outputs, prefix_name=None) None[source]#

Aggregate logging outputs from data parallel training (copied from normal cross entropy).

class ofasys.engine.criterion.speech_pretrain_criterion.SpeechPretrainCriterionConfig(_name: Union[str, NoneType] = None, is_active: bool = False, weight: float = 1.0, bce_pos_weight: float = 1.0, use_guided_attention_loss: bool = False, guided_attention_loss_sigma: float = 0.4, ctc_weight: float = 0.0, sentence_avg: bool = False, pred_masked_weight: float = 1.0, pred_nomask_weight: float = 0.0, loss_weights: Union[List[float], NoneType] = <factory>, log_keys: List[str] = <factory>, mam_weight: float = 1.0, dec_weight: float = 1.0)[source]#

Bases: Tacotron2CriterionConfig

dec_weight: float = 1.0#
log_keys: List[str]#
loss_weights: Optional[List[float]]#
mam_weight: float = 1.0#
pred_masked_weight: float = 1.0#
pred_nomask_weight: float = 0.0#

Tacotron2Criterion#

class ofasys.engine.criterion.tacotron2_loss.OFATacotron2Criterion(task, cfg: Tacotron2CriterionConfig)[source]#

Bases: BaseCriterion

Parameters
  • task – the task corresponding to the criterion.

  • cfg (CriterionConfig) – the config to the criterion.

forward(model, sample, reduction='mean', update_num=None, pad=1, eos=2)[source]#

Calling the model, calculating the loss and prepare logging_output for metrics.

Parameters
  • model – the model for criterion.

  • sample (Dict[str, Any]) – the batched samples for calculating loss.

  • update_num (Int) – the number of current update steps, default: 0.

  • reduce (Bool) – if true, it will return the sum of losses. Otherwise, it will return the loss item for each sample. default: true.

static logging_outputs_can_be_summed() bool[source]#

Whether the logging outputs returned by forward can be summed across workers prior to calling reduce_metrics. Setting this to True will improve distributed training speed.

classmethod reduce_metrics(logging_outputs: List[Dict[str, Any]], prefix_name=None) None[source]#

Aggregate logging outputs from data parallel training.

class ofasys.engine.criterion.tacotron2_loss.Tacotron2CriterionConfig(_name: Union[str, NoneType] = None, is_active: bool = False, weight: float = 1.0, bce_pos_weight: float = 1.0, use_guided_attention_loss: bool = False, guided_attention_loss_sigma: float = 0.4, ctc_weight: float = 0.0, sentence_avg: bool = False)[source]#

Bases: CriterionConfig

bce_pos_weight: float = 1.0#
ctc_weight: float = 0.0#
guided_attention_loss_sigma: float = 0.4#
sentence_avg: bool = False#
use_guided_attention_loss: bool = False#