The multilingual input-method model of Zhiyi IME: 8 ModernBERT layers of width 384 (the mmBERT-small tokenizer) that do three things:
char_logits), in Simplified
and Traditional Chinese and Japanese;It is ML-Flash-1103 with the
next-word decoder added; word choice and composition stay about where ML-Flash-1103 is. The decoder shares Flash's
token table and reads the context block that word choice already computes and caches, so word choice and next words
run from one ONNX file. Zhiyi IME uses this model since version 1.7.0 (the slim graph
onnx/slim/laya.one.next.slim.onnx). The GitHub release
model-ml-flash-next-preview-1114
has the same files, and its release notes have every test result. All Zhiyi models:
https://github.com/dd1000001000/zhiyi_models
| File | |
|---|---|
model.safetensors |
all weights, unquantized (171 MB, matrices F16): quantize the linear layers to Q8_0 when loading (below) to get the accuracy measured here |
config.json |
the structure (with encoder.layer_types), prefix_mask, shared_head, next_word_decoder, special tokens, sequence settings, ml (language tags, typed-text length, tone stride), next_word (the decoder) and quantize_q8_0: the tensors to quantize |
next_rows.json |
the decoder's rows (= the token table's rows): token ids, pieces, the six languages' masks (allow_*, first_*, cont_*), the end row, langs, cjk, lang_tags |
tokenizer.json |
mmBERT's tokenizer (Gemma vocabulary, 256,000 tokens), unchanged |
unified_vocab.json |
the 3,805 units, 28 letters, 9,067 characters and the other tables |
test_vectors.safetensors |
16 word-choice / composition inputs with the fp32 ONNX model's outputs and 14 next-word inputs with PyTorch's 5 words, to check an implementation |
candle_ref/ |
a reference forward pass in Rust (candle 0.11), version 0.8.0 |
onnx/int8/ |
the ONNX graphs, int8: laya.one.next.w8.onnx (94 MB, one graph: word choice and composition with a KV cache, and the next-word decoder), laya.int8g.onnx (75 MB, word choice and composition, the whole sequence at once), with their tokenizer and JSON files |
onnx/slim/ |
laya.one.next.slim.onnx (64 MB): the same one graph with the token table and the unit embeddings in 4 bits; same inputs and outputs, about the same speed; the file Zhiyi IME ships |
example.py |
the quick start below |
LICENSE |
Apache License 2.0 |
Word choice, composition and next words with the int8 graphs of onnx/int8/ (the code is in example.py):
pip install onnxruntime tokenizers numpy huggingface_hub
python example.py
D = os.path.join(snapshot_download("zhiyi-ime/zhiyi-ml-flash", allow_patterns=["onnx/int8/*"]), "onnx", "int8")
tok = Tokenizer.from_file(os.path.join(D, "tokenizer.json")) # context, tags, typed text and candidates
BOS, EOS, MASK = 2, 1, 4
# 1. Word choice: <bos> context <eos> <mask> x syllables <eos> <mask> cand1 <mask> cand2 ... <eos>
# (laya.int8g.onnx; each candidate's score is read at the <mask> before it)
print(choose("今天天气很好,我们去公园", ["san", "bu"], ["散步", "散布", "三部"]))
# 2. Composition: <bos> "已输入的上文: " + context <eos> <mask> x syllables <eos>; one character per slot
print(compose("明天早上八点", "wo men zai xue xiao men kou jian mian".split()))
# 3. Next words (laya.one.next.w8.onnx): context mode once with need_cross, then is_step in a beam search
print(next_words("今天天气很好,我们一起去"))
print(next_words("Thank you very much for your", "en"))
print(next_words("今天天氣很好,我們一起去", "hant"))
print(next_words("我们明天去公", min_chars=1)) # completion: the typed start of the word ends the context
Output (ONNX Runtime 1.24.4, CPU):
[('散步', 2.66...), ('散布', -6.25...), ('三部', -7.56...)]
我们在学校门口见面
[('参加', -2.44...), ('看看', -2.46...), ('旅游', -3.16...), ('旅行', -3.72...), ('一个', -4.11...)]
[('support', -2.70...), ('kind', -3.29...), ('help', -3.37...), ('time', -3.39...), ('feedback', -3.49...)]
[('看看', -1.87...), ('參加', -3.10...), ('旅行', -3.58...), ('這裡', -3.64...), ('試試', -3.96...)]
[('司', -1.07...), ('园', -2.13...), ('寓', -2.52...), ('共', -3.21...), ('开', -3.84...)]
The calls build the inputs of test_vectors.safetensors cases 0, 5, next0, next3, next4 and next12 (token
for token); the int8 graph's words differ a little from the vectors' fp32 ones (next0: 一个 in place of 试试,
the first two swapped). Japanese, Korean and Vietnamese work the same way (next_words(text, "ja"), "ko", "vi"):
the language's tag from next_rows.json lang_tags goes after <bos>. For the slim set, load
onnx/slim/laya.one.next.slim.onnx in place of the w8 graph (same inputs); its next words for the first context are
看看, 参加, 试试, 旅游, 旅行.
One input is_step (bool scalar) picks the mode; an If node runs only that mode's branch. Every input has a small
default, so ONNX Runtime lists them as overridable initializers, not as graph inputs (get_overridable_initializers());
feed them by name as usual and leave out what the mode does not use.
is_step false (the default): word choice and composition with a KV cache: input_ids, unit_ids, past_enc_k /
_v (8 × 1 × 6 × P × 64), past_choice_k / _v (2 × 1 × 6 × P × 64), past_mask, marker_pos, slot_pos →
logits, char_logits, present_0 … present_3 (the encoder's and the head's keys and values), and
cross_k_out / cross_v_out (4 × 1 × 6 × T × 64): the decoder's cross-attention keys and values of the tokens just
run, computed only when the input need_cross (bool scalar, default false) is true. Run the leading block
<bos> … <eos> once per context (no past) to get the cache, then on each key only the rest with the cache as the
past.need_cross true on <bos> [tag] encode(context)[last 128 tokens] <eos>
with unit ids 0 (Korean, Vietnamese, English: the context's trailing spaces cut first). This is the block word choice
runs, so an input method gets the next word's keys and values in the same call as its context cache.is_step true: one decoder step for N hypotheses: prev (N, the previous row; any value at position 0), pos
(N, 0 = start), dec_past_k / dec_past_v (4 × N × 6 × P × 64) with dec_past_mask (N × P) = the earlier steps (at
position 0 a placeholder of length 1 with the mask false), cross_k / cross_v / cross_mask = the context's, one
copy per hypothesis → logits (N × 126,963 rows), present_0 / present_1 = the decoder's keys and values with this
step appended.Quantization: the encoder, head and decoder matrices are dynamic int8 (DynamicQuantizeMatMul); only the shared token
table is weight-only (MatMulNBits in blocks of 32: 8 bits in w8, 4 bits in slim, where the unit embeddings are 4-bit
too), and the inputs read their rows from the read-out's quantized weight, so the table is stored once. Both
files were checked on the first 300 rows of each test set (the numbers are in the release notes).
Speed (ONNX Runtime 1.24.4, CPU, AMD Ryzen 5 9600X, median): laya.one.next.w8.onnx word choice 5.8 ms once per
context and 4.4 ms a key on 4 threads (7.8 / 5.4 ms on 1), laya.one.next.slim.onnx 5.7 / 4.2 ms (7.8 / 5.3 ms); a
whole next-word prediction (the context once, then the beam search's steps, time inside ONNX Runtime) 12 ms Chinese /
11 ms English on 4 threads (18 / 16 ms on 1) with either file.
One RTX 5070 Ti, from ML-Flash-1103, the whole model (all 8 layers) with the new decoder for three epochs over 10.5 million next-word rows: 5.6 M Simplified Chinese, 3.27 M English, 0.66 M Traditional Chinese, 0.5 M Japanese, 0.25 M Korean, 0.25 M Vietnamese. Targets 90% from the top 30 next words of a Qwen3 base model (1.7B for Simplified Chinese and English, 4B for the others, which beat 1.7B and 8B on these languages in a 300-row comparison), 10% from the text's word; 20% of every language's rows cut inside their word, for completion. Every step also trains on Flash's own word-choice and composition rows with their stored teacher scores (80% soft), so that word choice and composition do not drift.
Names are the PyTorch ones. Matrices F16, vectors and the decoder's 1-D tensors F32.
| Tensor | Shape | |
|---|---|---|
encoder.embeddings.tok_embeddings.weight |
126963 × 384 | pruned rows (see vocab_remap); also the decoder's input table and read-out |
vocab_remap |
256000 | I32: token id → row (tokens outside the kept set → <unk>'s row) |
encoder.*, unit_emb.weight, tone_emb.weight, type_emb.weight |
as ML-Flash-1103 (8 layers; type_emb[0] is the one used) |
|
head.layers.{0,1}.*, scorer.*, out.*, char_emb.weight, char_bias |
as ML-Flash-1103: the shared head, word choice, composition | |
next.mem_in.0 (384 → 384), next.mem_in.1 (LayerNorm) |
the decoder's memory | |
next.tok_in (384 → 384) |
a row's embedding → the decoder's input | |
next.start |
384 | the input at position 0 |
next.pos.weight |
22 × 384 | learned positions |
next.layers.N.* (N = 0…3) |
nn.TransformerDecoderLayer: self_attn, multihead_attn (in_proj_weight = q, k, v stacked, 1152 × 384), linear1 (384 → 1536), linear2, norm1-norm3 |
|
next.out.0 (LayerNorm), next.out.1 (384 → 384) |
the decoder's output | |
next.bias |
126963 | the read-out's bias, one per row; −10000 on the rows no language writes |
The 69 tensors listed in config.json quantize_q8_0 (every linear weight of the encoder, the head and the decoder)
go to Q8_0: blocks of 32 along the input dimension, one f16 scale per block; so does the decoder's read-out (the token
table). With candle:
use candle_core::quantized::{GgmlDType, QMatMul, QTensor};
let w = safetensors.load(name, &device)?.to_dtype(DType::F32)?;
let w = QMatMul::from_qtensor(QTensor::quantize(&w, GgmlDType::Q8_0)?)?;
// w.forward(&x) = x · wᵀ, then add the bias if the layer has one
Keep the embeddings unquantized where they are indexed and everything 1-D in F32.
One sequence at a time, with the prefix mask: positions up to and including the first <eos> attend only to
themselves, in every encoder and head layer; the positions after them attend to everything.
x = tok_emb[vocab_remap[input_ids]] + unit_emb[unit_ids % 3833] + tone_emb[unit_ids / 3833], then LayerNorm.full_attention), the others a window of ±64 (sliding_attention); exact GELU in the gated MLP; no biases.h0 = encoder output + type_emb[0], then the two head layers (nn.TransformerEncoderLayer,
norm_first=True, 6 heads, feed-forward 1536, ReLU), with the prefix mask.score = scorer.3(gelu(scorer.1(scorer.0(h0[marker])))) at each candidate's <mask>. Higher is
better.char_logits = out.1(out.0(h0[slot])) · char_embᵀ + char_bias, compared within the characters the
slot's unit spells in the row's language.Caching the context. Run the block alone once per context and keep each layer's keys and values; on each key run only the positions after it.
<bos> [encode(tag)] encode(context)[last 128 tokens] <eos> with unit ids 0 (Korean,
Vietnamese, English: the context's trailing spaces cut first), through the encoder: m = LayerNorm(next.mem_in.0( encoder output + type_emb[0])), (T, 384). It is the block word choice runs, so its encoder states come with the
context cache. Each decoder layer's cross-attention keys and values are m through rows 384-767 and 768-1151 of
its multihead_attn.in_proj_weight (and bias): once per context.next.start; position j ≥ 1 is next.tok_in(E[r_j]), E =
tok_embeddings, r_j the row written at step j; plus next.pos.weight[j].nn.TransformerDecoderLayer, norm_first=True, 6 heads of 64, LayerNorm eps 1e-5, no dropout):
x += self_attn(norm1(x)) causal; x += multihead_attn(norm2(x), memory); x += linear2(relu(linear1(norm3(x)))).logits = next.out.1(next.out.0(x)) · Eᵀ + next.bias over the 126,963 rows.Search (as example.py and candle_ref): beam 8, k = 5 words, at most 6 tokens a word; the language's masks from
next_rows.json (disallowed rows −10⁹ before the log-softmax):
zh), Traditional (hant), Japanese (ja): any allow_* row, then allow_* rows or the end. A
word is its rows' pieces joined (none of them starts with ▁), kept with at least 2 characters (completion: 1).en), Korean (ko): a first_* row (a ▁ word start) first, then cont_* rows (pieces) or the end.
Vietnamese (vi): a first_* row first, then any allow_* row (more ▁ syllables: words of several syllables)
or the end. The word is the pieces joined with ▁ as a space, stripped; English and Vietnamese lower-cased.allow_* row (Chinese, Japanese), a cont_* piece (English, Korean) or any allow_vi row; the
words found are the rest of the word.Token ids are tokenizer.json's, <bos> (2) as [CLS], <eos> (1) as [SEP], <mask> (4); text encoded without
special tokens. A row in Traditional Chinese, Japanese, Korean or Vietnamese starts with its tag as plain text
(<zh-Hant>, <ja>, <ko>, <vi>); Simplified Chinese and English have none. The composition prefix
已输入的上文: ("text typed so far: ") is literal model input.
word choice (zh, en) <bos> encode(context)[last 128] <eos> <mask> × typed units <eos> <mask> encode(" 候选1")[:16] … <eos>
word choice (others) <bos> encode(tag) encode(context)[last 128] <eos> encode(typed text)[:24] <eos> <mask> encode(" 候选1")[:16] … <eos>
composition <bos> [encode(tag)] encode("已输入的上文: " + context) <eos> <mask> × slots <eos>
next word <bos> [encode(tag)] encode(context)[last 128] <eos>
test_vectors.safetensors: word-choice / composition cases i = 0…15 (i.input_ids full-vocabulary ids,
i.unit_ids, i.marker_pos, i.slot_pos, the fp32 ONNX i.logits / i.char_logits); next-word cases nextN,
N = 0…13: nextN.input_ids (the prefix block), nextN.mode (the language's index in langs: 0 zh, 1 en, 2 hant,
3 ja, 4 ko, 5 vi), nextN.min_chars, nextN.comp (1 = completion; cases 12 and 13) and PyTorch's 5 words
nextN.text (UTF-8, one a line) with nextN.logp. The Q8_0 model agrees with the word-choice and composition outputs
to about 0.31 and picks the same best candidates and characters; on the next-word cases it gives the same first word
in 13 of 14 (case 3 swaps the first two), the same 5 words in 13 (in the same order in 9).
The model alone, not the IME end to end (the release notes have those). Word choice and composition: the first 300
rows of each Traditional, Japanese, Korean and Vietnamese test file and every third row of the Simplified Chinese and
English sets; fp32 ONNX = the whole graph exported from these weights; Q8_0 = this file through candle_ref. Next
word: the first 200 rows of each language's next-word test set and 100 words cut inside (completion), PyTorch fp32.
| Word choice, first candidate right | fp32 ONNX | Q8_0 (candle_ref) |
|---|---|---|
| Simplified Chinese, words and word pairs (2175) | 87.1% | 87.1% |
| Simplified Chinese, long input (749) | 82.4% | 82.1% |
| English (729) | 90.7% | 90.8% |
| Simplified Chinese, IME's real candidates (545) | 88.8% | 88.6% |
| Simplified Chinese homophones with the previous sentence (267) | 89.5% | 89.5% |
| English word completion (280) | 86.8% | 86.4% |
| Traditional, pinyin (luna_pinyin) (300) | 83.7% | 83.7% |
| Traditional, Zhuyin (bopomofo_tw) (300) | 89.0% | 88.7% |
| Traditional, Zhuyin (libchewing) (300) | 94.3% | 94.3% |
| Traditional, Jyutping (jyut6ping3) (300) | 88.0% | 87.7% |
| Traditional, Cangjie 5 (300) | 97.0% | 97.0% |
| Traditional, Quick 5 (300) | 86.3% | 86.0% |
| Traditional, Cangjie 3 (300) | 83.3% | 83.7% |
| Traditional, Quick 3 (300) | 78.0% | 78.0% |
| Japanese, conversion (Mozc) (300) | 93.0% | 92.7% |
| Japanese, prediction (Mozc) (300) | 86.0% | 86.0% |
| Japanese, whole sentence (Mozc) (300) | 95.0% | 95.0% |
| Korean, word completion (300) | 65.0% | 65.0% |
| Vietnamese, without diacritics (300) | 90.0% | 90.0% |
| Vietnamese, first letters (300) | 72.3% | 71.7% |
| Composition, whole rows right / characters right | fp32 ONNX | Q8_0 (candle_ref) |
|---|---|---|
| Simplified, long pinyin in full (300) | 58.3% / 85.3% | 58.3% / 85.3% |
| Traditional, pinyin (300) | 40.7% / 73.3% | 40.3% / 73.2% |
| Traditional, Zhuyin (70% with tones) (300) | 68.0% / 91.7% | 68.0% / 91.7% |
| Traditional, Jyutping (300) | 44.0% / 74.6% | 43.7% / 74.4% |
| Japanese, kana (300) | 72.7% / 96.3% | 72.7% / 96.3% |
| Next word, the text's word first (@1) / among the 5 (@5) | PyTorch fp32 | Q8_0 (candle_ref) |
Q8_0's first word = fp32's |
|---|---|---|---|
| Simplified Chinese (200) | 14.5 / 25.0% | 14.0 / 25.0% | 96.0% |
| English (200) | 14.0 / 36.5% | 14.5 / 36.5% | 96.0% |
| Traditional Chinese (200) | 10.5 / 21.5% | 10.5 / 21.5% | 96.0% |
| Japanese (200) | 9.5 / 18.5% | 9.5 / 18.5% | 92.5% |
| Korean (200) | 6.5 / 14.5% | 7.5 / 14.5% | 95.5% |
| Vietnamese (200) | 19.5 / 36.5% | 17.5 / 35.5% | 82.0% |
| Completion, Simplified Chinese (100) | 48 / 71% | 48 / 72% | 100% |
| Completion, English (100) | 31 / 41% | 31 / 40% | 91% |
| Completion, Traditional Chinese (100) | 52 / 76% | 53 / 76% | 98% |
| Completion, Japanese (100) | 39 / 65% | 40 / 63% | 94% |
| Completion, Korean (100) | 28 / 45% | 28 / 46% | 98% |
| Completion, Vietnamese (100) | 58 / 73% | 55 / 71% | 91% |
On the full next-word test sets (PyTorch fp32; @1 / @5): Simplified Chinese 12.5 / 24.4% (4,941 rows), English 21.5 / 40.4%, Traditional Chinese 11.6 / 22.5%, Japanese 10.9 / 21.3%, Korean 8.4 / 15.4%, Vietnamese 14.1 / 29.1% (3,000 rows each). Many continuations make sense and only the text's word counts, so the next-word scores are low; the teacher Qwen3-1.7B-Base (1.7B parameters, beam 32) gets 26.2 / 45.5% (Simplified Chinese) and 39.2 / 64.9% (English).
On the public neutral word-choice test sets (v2, v3 web, v4 chat, v5 hard; PyTorch fp32, whole page, first candidate right):
| v2 | v3 | v4 | v5 | |
|---|---|---|---|---|
| ML-Flash-1103 | 93.53% | 93.74% | 91.06% | 71.55% |
| ml-flash-next-preview-1114 | 93.46% | 93.58% | 90.78% | 72.32% |
candle_ref: candle's quantized kernels choose their SIMD code at compile time, so build with
RUSTFLAGS="-C target-cpu=native" (or at least x86-64-v3), otherwise the Q8_0 dot products run scalar code.
cd candle_ref
RUSTFLAGS="-C target-cpu=native" cargo build --release
target/release/zhiyi-ref ../model.safetensors ../test_vectors.safetensors out.safetensors
candle_ref runs the whole sequence with the mask (it does not cache) and reruns the decoder over each hypothesis'
prefix at every step; the outputs are those of the cached runs.
Word choice on the public neutral test sets, each model alone (no engine, no n-gram), against the theoretical compute
to score a row: of the Zhiyi models only the four maintained ones are shown, pumpkin
(zhiyi-ml-pumpkin), Flash
(zhiyi-ml-flash), Mini
(zhiyi-zhen-mini) and Feather
(zhiyi-zhen-feather), each scored with its released ONNX file
(this model: onnx/int8/laya.int8g.onnx), so the points can differ a little from the numbers above. The other models (the file of each,
its license and how it is run) and the scoring code: https://github.com/dd1000001000/zhiyi_models/tree/main/comparisons/models
These weights are Zhiyi IME's own work and are licensed under the Apache License 2.0 (LICENSE). The reference
code in candle_ref/ is GPL-3.0-only, like Zhiyi IME (https://github.com/dd1000001000/zhiyi_ime/blob/main/LICENSE).
The upstream parts keep their licenses: the Laya architecture (https://huggingface.co/convaiinnovations/laya)
Apache-2.0, the mmBERT-small encoder and tokenizer (Marone et al., 2025; https://huggingface.co/jhu-clsp/mmBERT-small)
MIT.
Teachers (only their scores and words were used; none of their weights are in these files): Qwen3.5-9B (https://huggingface.co/Qwen/Qwen3.5-9B) fine-tuned with LoRA for word choice and composition (ML-Flash-1103's rows), Qwen3-1.7B-Base (https://huggingface.co/Qwen/Qwen3-1.7B-Base; Simplified Chinese, English) and Qwen3-4B-Base (https://huggingface.co/Qwen/Qwen3-4B-Base; Traditional Chinese, Japanese, Korean, Vietnamese) for next words, all by the Qwen team (Alibaba Cloud), Apache-2.0. Next-word text: Chinese text of C4 (Raffel et al.) and LCCC, English text of FineWeb (CC-MAIN-2025-26) and C4, and FineWeb-2 (Penedo et al., ODC-By) in the other languages; the next words segmented with jieba (Chinese), SudachiPy (Japanese) and pyvi (Vietnamese). These programs and data only produced training rows; none of them is in these files.
model.safetensors 171 MBonnx/int8/laya.one.next.w8.onnx 94 MBonnx/int8/laya.int8g.onnx 75 MBonnx/slim/laya.one.next.slim.onnx 64 MBtest_vectors.safetensors 2 MBjhu-clsp/mmBERT-small