Class SequenceDecoder
A composite decoder that applies multiple decoders in sequence to process tokens. Each decoder in the sequence processes the output from the previous decoder, creating a pipeline for token transformation.
Implements
Inherited Members
Namespace: Unity.InferenceEngine.Tokenization.Decoders
Assembly: Unity.InferenceEngine.Tokenization.dll
Syntax
public class SequenceDecoder : IDecoder
Remarks
This class implements the Composite pattern to chain multiple decoders together. The decoders are applied in the order they are provided in the constructor. Object pooling is used internally to optimize memory allocation for intermediate results.
Examples
var decoder1 = new SomeDecoder();
var decoder2 = new AnotherDecoder();
var sequenceDecoder = new SequenceDecoder(decoder1, decoder2);
var tokens = new List<string> { "token1", "token2" };
var output = new List<string>();
sequenceDecoder.Decode(tokens, output.AsOutput());
Constructors
SequenceDecoder(params IDecoder[])
Initializes a new instance of the SequenceDecoder class with the specified sequence of decoders.
Declaration
public SequenceDecoder(params IDecoder[] decoders)
Parameters
| Type | Name | Description |
|---|---|---|
| IDecoder[] | decoders | A sequence of decoders to be applied in order. Each decoder will process the output from the previous decoder in the sequence. |
Remarks
The decoders will be applied in the exact order they are provided. A copy of the decoder array is made internally to prevent external modifications.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
Methods
Decode(IReadOnlyList<string>, Output<string>)
Decodes the input tokens by applying each decoder in the sequence.
Declaration
public void Decode(IReadOnlyList<string> tokens, Output<string> output)
Parameters
| Type | Name | Description |
|---|---|---|
| IReadOnlyList<string> | tokens | The input tokens to be decoded. This collection is processed by the first decoder, and subsequent decoders process the output from the previous decoder. |
| Output<string> | output | The output collection where the final decoded results will be added. This will contain the tokens after being processed by all decoders in sequence. |
Remarks
The decoding process works as follows:
- The input tokens are copied to an intermediate buffer
- Each decoder in the sequence processes the current intermediate buffer
- The output of each decoder becomes the input for the next decoder
- The final result is added to the output collection
Two intermediate List<string> instances are used and swapped between iterations to avoid unnecessary memory allocations. These lists are obtained from an object pool and automatically returned when the method completes.
Examples
var tokens = new List<string> { "Hello", "World" };
var output = new List<string>();
decoder.Decode(tokens, output.AsOutput());
// output now contains the tokens processed by all decoders in sequence