English to Spanish translation with Helsinki-NLP/opus-mt-en-es, converted to ONNX for ONNX Runtime on the CPU. As well as each next token, the decoder outputs the attention the token paid to each source piece, from one decoder layer. That can be used to align words between a sentence and its translation, for example to find the translation of one word in context ("bank" in "the bank of the river" → "orilla"), or to highlight corresponding words in both texts.
This is a conversion of the model by the Language Technology Research Group at the University of Helsinki
(OPUS-MT), at revision 5bc4493d463cf000c1f0b50f8d56886a392ed4ab. Its weights are the
original's, quantized to 8 bits. This conversion is not affiliated with the University of Helsinki. Licensed
under the Apache License 2.0, like the original: see LICENSE.
| File | |
|---|---|
encoder.onnx |
Source ids input_ids [1, S] → cross [L, 2, H, S, D]: each decoder layer's attention keys and values for the source. |
decoder.onnx |
One step of greedy decoding: input_id [1], position [1], past [L, 2, H, T, D], cross → logits [V], present [L, 2, H, T+1, D], attention [S]. |
source.spm |
The SentencePiece model that splits English text into pieces. |
vocab.json |
The id of each piece, shared by source and target. |
model.json |
Sizes and special token ids: decoding starts with decoderStartTokenId and stops at eosTokenId; padTokenId must never be chosen. |
L = 6 layers, H = 8 heads, D = 64; past starts with T = 0. attention is
the cross-attention of decoder layer 4 (counting from 0), averaged over its heads, while the
step's token was generated.
To translate, tokenize with source.spm, map the pieces to ids with vocab.json and append eosTokenId. Run the
encoder once, then the decoder from position 0 with decoderStartTokenId, feeding back present as past and the
highest-scoring token as input_id until it is eosTokenId. Join the target pieces and replace ▁ with spaces.
To align a source word, sum each target piece's attention over the word's source pieces. For each target word,
average that over its pieces, renormalized over the source words that are not only punctuation, and take the target
word with the highest score. Words are what SentencePiece puts between spaces, punctuation included.
The weights of every matrix multiplication are 8-bit, in blocks of 32 values with their own scale (ONNX Runtime's
MatMulNBits, accuracy_level 4), and the embedding tables are 8-bit. On 62 test sentences, the
greedy translations are the original's in all but 2.
Aligning an English word to its Spanish counterpart with the method above, on labelled cases: 60/60 on the cases the method was chosen with, and 40/42 on held-out ones. The cases are 102 words in 62 sentences, each labelled and then checked by one person, so treat these figures as a rough indication. Attention-based alignment is approximate. It is weakest where one word is translated by several, such as "smaller" matched to "más" in "más pequeños", and it only picks a single target word.
| Size | Median time per sentence | Slowest | |
|---|---|---|---|
| fp32 export | 581 MB | 84 ms | 165 ms |
| This model | 159 MB | 49 ms | 93 ms |
Measured on AMD Ryzen 5 1600 Six-Core Processor with 4 threads and ONNX Runtime 1.31.0, on sentences of 5 to 17 words.
decoder.onnx 97 MBencoder.onnx 59 MBHelsinki-NLP/opus-mt-en-es