Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 43 additions & 2 deletions doc/search_v2.rst
Original file line number Diff line number Diff line change
Expand Up @@ -496,8 +496,8 @@ built-in way to do this.

Built-in label scorer types include:

* ``no-context-onnx``, ``fixed-context-onnx``, ``stateful-onnx``, ``state-managed-onnx``: forward
features (and, depending on type, label history/hidden state) through an ONNX model.
* ``no-context-onnx``, ``fixed-context-onnx``, ``full-context-onnx``, ``stateful-onnx``, ``state-managed-onnx``:
forward features (and, depending on type, label history/hidden state) through an ONNX model.
* ``encoder-decoder`` / ``encoder-only``: wrap a separate encoder (see :ref:`Encoders` below) that
pre-processes features, combined with a decoder label scorer (or no decoder, for encoder-only models).
* ``ctc-prefix``: wraps a time-synchronous CTC scorer and derives label-synchronous prefix scores from it
Expand Down Expand Up @@ -646,6 +646,47 @@ history instead of a recurrent state.
loop-updates-history = true
onnx-model.session.file = /path/to/transducer_predictor.onnx

full-context-onnx
^^^^^^^^^^^^^^^^^^

Like ``fixed-context-onnx``, but forwards the *entire* label history through the ONNX model instead of a
fixed-size window -- the history is never truncated. The acoustic input can be given to the model in one of two
ways, depending on which inputs are mapped in the model's ONNX I/O spec (both can also be mapped at once, in
which case the model receives both):

* ``input-feature``: only the input feature at the hypothesis' current timestep is fed in, re-selected on every
scoring call. This works incrementally as features arrive.
* ``encoder-states`` / ``encoder-states-size``: the complete input sequence collected so far is fed in as one
tensor, together with its length. Since this requires the whole sequence, scoring only starts once all
features of the segment have been passed (i.e. after ``signalNoMoreFeatures``/``finishSegment``), and the
input buffer is never trimmed.

Since histories in a batch generally differ in length, the ``history`` input is always padded to the longest history
in the batch, and the mandatory ``history-size`` input tells the model the true length of each entry, so the model
itself is responsible for handling the padding correctly (e.g. via masking).

* ``onnx-model.io-map.input-feature`` / ``encoder-states`` / ``encoder-states-size`` / ``history`` /
``history-size`` / ``scores``: ONNX tensor names for the respective inputs/outputs. ``history``,
``history-size`` and ``scores`` are always required; at least one of ``input-feature`` or ``encoder-states``
must be mapped as well.
* ``start-label-index`` (int): label index used to seed the very first scoring context, whose history then
consists of this single label. Default ``0``.
* ``blank-updates-history`` / ``silence-updates-history`` / ``loop-updates-history`` / ``vertical-label-transition``
/ ``max-batch-size``: same meaning and defaults as for ``fixed-context-onnx`` above.
* Default :ref:`transition-preset <Transition types and presets>`: ``aed``.

.. code-block:: ini

[*.search-algorithm.label-scorer]
type = full-context-onnx
start-label-index = 0
onnx-model.session.file = /path/to/model.onnx
onnx-model.io-map.encoder-states = encoder_states
onnx-model.io-map.encoder-states-size = encoder_states_size
onnx-model.io-map.history = history
onnx-model.io-map.history-size = history_size
onnx-model.io-map.scores = scores

stateful-onnx
^^^^^^^^^^^^^^

Expand Down
2 changes: 1 addition & 1 deletion src/Nn/LabelScorer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ target_sources(
if(${MODULE_ONNX})
target_sources(
RasrNn
PRIVATE FixedContextOnnxLabelScorer.cc NoContextOnnxLabelScorer.cc
PRIVATE FixedContextOnnxLabelScorer.cc
FullContextOnnxLabelScorer.cc
NoContextOnnxLabelScorer.cc
StatefulOnnxLabelScorer.cc
StateManagedOnnxLabelScorer.cc
Expand Down
28 changes: 0 additions & 28 deletions src/Nn/LabelScorer/FixedContextOnnxLabelScorer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,6 @@

namespace Nn {

SeqStepScoringContext::SeqStepScoringContext()
: labelSeq(), currentStep(0ul) {}

SeqStepScoringContext::SeqStepScoringContext(std::vector<LabelIndex> const& seq, Speech::TimeframeIndex step)
: labelSeq(seq), currentStep(step) {}

SeqStepScoringContext::SeqStepScoringContext(std::vector<LabelIndex>&& seq, Speech::TimeframeIndex step)
: labelSeq(std::move(seq)), currentStep(step) {}

bool SeqStepScoringContext::isEqual(ScoringContextRef const& other) const {
auto* otherPtr = dynamic_cast<SeqStepScoringContext const*>(other.get());
if (otherPtr == nullptr) {
return false;
}

if (currentStep != otherPtr->currentStep) {
return false;
}

return labelSeqEqual(labelSeq, otherPtr->labelSeq);
}

size_t SeqStepScoringContext::hash() const {
return Core::combineHashes(currentStep, labelSeqHash(labelSeq));
}

typedef Core::Ref<SeqStepScoringContext const> SeqStepScoringContextRef;

const Core::ParameterInt FixedContextOnnxLabelScorer::paramStartLabelIndex(
"start-label-index",
"Initial history in the first step is filled with this label index.",
Expand Down
17 changes: 0 additions & 17 deletions src/Nn/LabelScorer/FixedContextOnnxLabelScorer.hh
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,6 @@

namespace Nn {

/*
* Scoring context that describes a sequence of previously observed labels as well as the current decoding step
*/
struct SeqStepScoringContext : public ScoringContext {
std::vector<LabelIndex> labelSeq;
Speech::TimeframeIndex currentStep;

SeqStepScoringContext();
SeqStepScoringContext(std::vector<LabelIndex> const& seq, Speech::TimeframeIndex step);
SeqStepScoringContext(std::vector<LabelIndex>&& seq, Speech::TimeframeIndex step);

bool isEqual(ScoringContextRef const& other) const override;
size_t hash() const override;
};

typedef Core::Ref<SeqStepScoringContext const> SeqStepScoringContextRef;

/*
* Label Scorer that performs scoring by forwarding the input feature at the current timestep together
* with a fixed-size sequence of history tokens through an ONNX model.
Expand Down
Loading
Loading