mbrs.decoders package#

Submodules#

Module contents#

class mbrs.decoders.DecoderAggregateMBR(cfg: ~mbrs.decoders.base.DecoderBase.Config, metric: ~mbrs.metrics.base.MetricBase, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: DecoderMBR

MBR decoding with reference aggregation.

  • Time complexity: O(N)

  • Space complexity: O(N)

References

J. DeNero et al., 2009, “Fast Consensus Decoding over Translation Forests”. https://aclanthology.org/P09-1064/

J. Vamvas and R. Sennrich, 2024, “Linear-time Minimum Bayes Risk Decoding with Reference Aggregation”. https://arxiv.org/abs/2402.04251

decode(hypotheses: list[str], references: list[str], source: str | None = None, nbest: int = 1, reference_lprobs: Tensor | None = None) Output[source]#

Select the n-best hypotheses based on the strategy.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • references (list[str]) – References.

  • source (str, optional) – A source.

  • nbest (int) – Return the n-best hypotheses.

  • reference_lprobs (Tensor, optional) – Log-probabilities for each reference sample. The shape must be (len(references),). See https://arxiv.org/abs/2311.05263.

Returns:

The n-best hypotheses.

Return type:

DecoderAggregateMBR.Output

class mbrs.decoders.DecoderBase(cfg: ~mbrs.decoders.base.DecoderBase.Config, metric: ~mbrs.metrics.base.MetricBase, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: ABC

Decoder base class.

class Config[source]#

Bases: object

Configuration for the decoder.

class Output(idx: list[int], sentence: list[str], score: list[float])[source]#

Bases: object

  • idx (list[int]): Index numbers of the n-best hypotheses.

  • sentence (list[str]): Sentences of the n-best hypotheses.

  • score (list[float]): Scores of the n-best hypotheses.

idx: list[int]#
score: list[float]#
sentence: list[str]#
argbest(x: Tensor) Tensor[source]#

Return the index of the best element.

Parameters:

x (Tensor) – Input 1-D array.

Returns:

A scalar tensor of the best index.

Return type:

Tensor

property maximize: bool#

Return True when maximizing the objective score.

select(hypotheses: list[str], expected_scores: Tensor, nbest: int = 1, source: str | None = None, **kwargs) Output[source]#

Select the final output list.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • expected_scores (Tensor) – The expected scores for each hypothesis.

  • nbest (int) – Return the n-best hypotheses based on the selection rule.

  • source (str, optional) – A source.

  • maximize (bool) – Whether maximize the scores or not.

Returns:

Outputs.

Return type:

Selector.Output

superior(a: float, b: float) bool[source]#

Return whether the score a is superior to the score b.

Parameters:
Returns:

Return True when a is superior to b.

Return type:

bool

topk(x: Tensor, k: int = 1) tuple[list[float], list[int]][source]#

Return the top-k best elements and corresponding indices.

Parameters:
  • x (Tensor) – Input 1-D array.

  • k (int) – Return the top-k values and indices.

Returns:

tuple[list[float], list[int]]
  • list[float]: The top-k values.

  • list[int]: The top-k indices.

class mbrs.decoders.DecoderCentroidMBR(cfg: ~mbrs.decoders.centroid_mbr.DecoderCentroidMBR.Config, metric: ~mbrs.metrics.base.MetricAggregatableCache, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: DecoderMBR

Centroid-Based MBR decoder class.

  • Time complexity: O(Nk)

  • Space complexity: O(Nk)

where k << N.

References

H. Deguchi et al., 2024. “Centroid-Based Efficient Minimum Bayes Risk Decoding”. https://aclanthology.org/2024.findings-acl.654

class Config(kmeans: ~mbrs.modules.kmeans.Kmeans.Config = <factory>, count_weight: bool = False)[source]#

Bases: Config

Configuration for the decoder.

  • kmeans (Kmeans.Config): Configuration for k-means.

  • count_weight: (bool) Weight the scores with counts.

count_weight: bool = False#
kmeans: Config#
cfg: Config#
decode(hypotheses: list[str], references: list[str], source: str | None = None, nbest: int = 1, reference_lprobs: Tensor | None = None) Output[source]#

Select the n-best hypotheses based on the strategy.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • references (list[str]) – References.

  • source (str, optional) – A source.

  • nbest (int) – Return the n-best hypotheses.

  • reference_lprobs (Tensor, optional) – Log-probabilities for each reference sample. The shape must be (len(references),). See https://arxiv.org/abs/2311.05263.

Returns:

The n-best hypotheses.

Return type:

DecoderCentroidMBR.Output

class mbrs.decoders.DecoderMBR(cfg: ~mbrs.decoders.base.DecoderBase.Config, metric: ~mbrs.metrics.base.MetricBase, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: DecoderReferenceBased

Naive MBR decoder class.

  • Time complexity: O(N^2)

  • Space complexity: O(N^2)

References

S. Kumar and W. Byrne, 2004, “Minimum Bayes-Risk Decoding for Statistical Machine Translation”. https://aclanthology.org/N04-1022

B. Eikema and W. Aziz, 2020, “Is MAP Decoding All You Need? The Inadequacy of the Mode in Neural Machine Translation”. https://aclanthology.org/2020.coling-main.398

decode(hypotheses: list[str], references: list[str], source: str | None = None, nbest: int = 1, reference_lprobs: Tensor | None = None) Output[source]#

Select the n-best hypotheses based on the strategy.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • references (list[str]) – References.

  • source (str, optional) – A source.

  • nbest (int) – Return the n-best hypotheses.

  • reference_lprobs (Tensor, optional) – Log-probabilities for each reference sample. The shape must be (len(references),). See https://arxiv.org/abs/2311.05263.

Returns:

The n-best hypotheses.

Return type:

DecoderMBR.Output

class mbrs.decoders.DecoderProbabilisticMBR(cfg: ~mbrs.decoders.base.DecoderBase.Config, metric: ~mbrs.metrics.base.MetricBase, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: DecoderMBR

Probabilistic MBR decoder using alternating least squares (ALS) approximation.

References

F. Trabelsi et al., 2024, “Efficient Minimum Bayes Risk Decoding using Low-Rank Matrix Completion Algorithms”. https://arxiv.org/abs/2406.02832

class Config(reduction_factor: float = 8.0, regularization_weight: float = 0.1, rank: int = 8, niter: int = 10, seed: int = 0)[source]#

Bases: Config

Configuration for the decoder.

  • reduction_factor (float): Reduction factor. The computational budget will be reduced to 1 / reduction_factor.

  • regularization_weight (float): Weight of L2 regularization.

  • rank (int): Rank of the factarized matrices.

  • niter (int): The number of alternating steps performed.

  • seed (int): Random seed.

niter: int = 10#
rank: int = 8#
reduction_factor: float = 8.0#
regularization_weight: float = 0.1#
seed: int = 0#
cfg: Config#
decode(hypotheses: list[str], references: list[str], source: str | None = None, nbest: int = 1, reference_lprobs: Tensor | None = None) Output[source]#

Select the n-best hypotheses based on the strategy.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • references (list[str]) – References.

  • source (str, optional) – A source.

  • nbest (int) – Return the n-best hypotheses.

  • reference_lprobs (Tensor, optional) – Log-probabilities for each reference sample. The shape must be (len(references),). See https://arxiv.org/abs/2311.05263.

Returns:

The n-best hypotheses.

Return type:

DecoderMBR.Output

pairwise_scores_probabilistic(hypotheses: list[str], references: list[str], source: str | None = None) Tensor[source]#

Compute approximated pairwise scores using the probabilistic MBR algorithm.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • references (list[str]) – References.

  • source (str, optional) – A source.

Returns:

Approximated pairwise scores of shape (H, R).

Return type:

Tensor

class mbrs.decoders.DecoderPruningMBR(cfg: ~mbrs.decoders.pruning_mbr.DecoderPruningMBR.Config, metric: ~mbrs.metrics.base.Metric, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: DecoderMBR

Pruning MBR decoder class.

References

J. Cheng and A. Vlachos, 2023, “Faster Minimum Bayes Risk Decoding with Confidence-based Pruning”. https://aclanthology.org/2023.emnlp-main.767/

class Config(alpha: float = 0.99, sampling_scheduler: list[int] = <factory>, num_bootstrap_samples: int = 500, seed: int = 0)[source]#

Bases: Config

Configuration for the decoder.

  • alpha (float): Prune hypotheses based on this confidence threshold.

  • sampling_shceduler (list[int]): Sample size scheduler. For each step, the number of samples will be the t-th number.

  • num_boostrap_samples (int): Number of boostrap samples.

  • seed (int): Random seed for bootstrap sampling.

alpha: float = 0.99#
num_bootstrap_samples: int = 500#
sampling_scheduler: list[int]#
seed: int = 0#
cfg: Config#
decode(hypotheses: list[str], references: list[str], source: str | None = None, nbest: int = 1, reference_lprobs: Tensor | None = None) Output[source]#

Select the n-best hypotheses based on the strategy.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • references (list[str]) – References.

  • source (str, optional) – A source.

  • nbest (int) – Return the n-best hypotheses.

  • reference_lprobs (Tensor, optional) – Log-probabilities for each reference sample. The shape must be (len(references),). See https://arxiv.org/abs/2311.05263.

Returns:

The n-best hypotheses.

Return type:

DecoderMBR.Output

decode_pruning(hypotheses: list[str], references: list[str], source: str | None = None, nbest: int = 1, reference_lprobs: Tensor | None = None) tuple[list[float], list[int]][source]#

Select the n-best hypotheses using pruning MBR decoding.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • references (list[str]) – References.

  • source (str, optional) – A source.

  • nbest (int) – Return the n-best hypotheses.

  • reference_lprobs (Tensor, optional) – Log-probabilities for each reference sample. The shape must be (len(references),). See https://arxiv.org/abs/2311.05263.

Returns:

Top-k scores. - list[int]: Top-k indices.

Return type:

  • list[float]

class mbrs.decoders.DecoderReferenceBased(cfg: ~mbrs.decoders.base.DecoderBase.Config, metric: ~mbrs.metrics.base.MetricBase, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: DecoderBase

Decoder base class for strategies that use references like MBR decoding.

abstract decode(hypotheses: list[str], references: list[str], source: str | None = None, nbest: int = 1, reference_lprobs: Tensor | None = None) Output[source]#

Select the n-best hypotheses based on the strategy.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • references (list[str]) – References.

  • source (str, optional) – A source.

  • nbest (int) – Return the n-best hypotheses.

  • reference_lprobs (Tensor, optional) – Log-probabilities for each reference sample. The shape must be (len(references),). See https://arxiv.org/abs/2311.05263.

Returns:

The n-best hypotheses.

Return type:

Decoder.Output

metric: Metric#
class mbrs.decoders.DecoderReferenceless(cfg: ~mbrs.decoders.base.DecoderBase.Config, metric: ~mbrs.metrics.base.MetricBase, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: DecoderBase

Decoder base class for reference-free strategies.

abstract decode(hypotheses: list[str], source: str, nbest: int = 1) Output[source]#

Select the n-best hypotheses based on the strategy.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • source (str) – A source.

  • nbest (int) – Return the n-best hypotheses.

Returns:

The n-best hypotheses.

Return type:

Decoder.Output

metric: MetricReferenceless#
class mbrs.decoders.DecoderRerank(cfg: ~mbrs.decoders.base.DecoderBase.Config, metric: ~mbrs.metrics.base.MetricBase, selector: ~mbrs.selectors.base.Selector = <mbrs.selectors.nbest.SelectorNbest object>)[source]#

Bases: DecoderReferenceless

Reranking decoder class.

  • Time complexity: O(N)

  • Space complexity: O(N)

decode(hypotheses: list[str], source: str, nbest: int = 1) Output[source]#

Select the n-best hypotheses based on the strategy.

Parameters:
  • hypotheses (list[str]) – Hypotheses.

  • source (str) – A source.

  • nbest (int) – Return the n-best hypotheses.

Returns:

The n-best hypotheses.

Return type:

DecoderRerank.Output

mbrs.decoders.get_decoder(name: str) type[T]#

Get a class type.

Parameters:

name – A registered name.

Returns:

Class type.

Return type:

type[T]

mbrs.decoders.register(name: str) Callable[[type[T]], type[T]]#

Register a type as the given name.

Parameters:

name (str) – The name of a type.

Returns:

Register decorator function.

Return type:

Callable[[type[T]], type[T]]

Raises:

ValueError – The type is already registered.