0c-ja-punct

Japanese punctuation insertion model for the 0C input method. It reads a token sequence and predicts, for every token, whether 、, 。 or ? should be inserted after that token. It never rewrites, deletes, merges or reorders input characters.

Architecture: D512 transformer encoder, 8 layers, 8 heads, FFN 1024, 32,768-piece public-core SentencePiece vocabulary, 33,865,732 parameters. Trained by the 0C project on the public-core Japanese corpus only (CC BY 4.0 and public domain; no CC BY-SA, no Mozc, no NEologd).

Labels and decoding

index label inserted after the token
0 none nothing
1 comma 、
2 period 。
3 question ?

Decoding rules from the training code:

  1. Tokenize the stripped text and append <eos> (id 3) for the model only.
  2. Take the argmax per position; drop the appended <eos> row.
  3. Force none on whitespace-only tokens and on <eos>.
  4. Output text is ''.join(surface + mark). No other string operation is applied, so removing the three marks always recovers the input exactly.

Tensor contract

name dtype shape meaning
input_ids int64 [batch, seq] public-core SentencePiece ids
attention_mask int64 [batch, seq] 1 real token, 0 padding
logits float32 [batch, seq, 4] per-token logits in label order
  • Batch and sequence axes are dynamic. Sequence length 1 to 512 is accepted because the learned position table has 512 entries.
  • There is no BOS. Positions are 0..seq-1; the training window is 256 tokens. Pad with id 0 and mask 0 if a batch row is shorter.
  • Special ids: pad=0, unk=1, bos=2, eos=3.
  • The mark for position i goes after token i. Do not shift logits.

Usage

import numpy as np
import onnxruntime as ort
import sentencepiece as spm

sp = spm.SentencePieceProcessor(model_file="public_core_ja.model")
session = ort.InferenceSession(
    "model.int8.onnx", providers=["CPUExecutionProvider"]
)
PAD, EOS = 0, 3
marks = [None, "、", "。", "?"]

text = "今ζ—₯γ―γ„γ„ε€©ζ°—γ§γ™γ­ζ˜Žζ—₯γ―ι›¨γŒι™γ‚‹γ§γ—γ‚‡γ†"
proto = sp.encode(text, out_type="immutable_proto")
tokens = [(piece.id, piece.surface) for piece in proto.pieces]
ids = [token for token, _ in tokens] + [EOS]

feed_ids = np.asarray([ids], dtype=np.int64)
feed_mask = np.ones_like(feed_ids)
logits = session.run(
    None, {"input_ids": feed_ids, "attention_mask": feed_mask}
)[0]

labels = np.argmax(logits[0, : len(tokens)], axis=-1)
for index, (token, surface) in enumerate(tokens):
    piece = sp.id_to_piece(int(token))
    if piece.replace("\u2581", "").strip() == "":
        labels[index] = 0

output = "".join(
    surface + (marks[label] or "")
    for (_, surface), label in zip(tokens, labels)
)
assert output.translate({ord(mark): None for mark in "γ€γ€‚οΌŸ"}) == text

Long input: split into segments of at most 255 tokens plus <eos>, preferably at existing sentence boundaries, and call the graph once per segment. The training data packed 256-token windows the same way. Quality across a window boundary has not been measured, so avoid cutting inside a short clause when the caller can choose.

Quality

Evaluation uses the P2-24A packed data, token accuracy and per-mark precision / recall / F1. macro F1 is the mean F1 of comma, period and question; none is excluded, matching the training code.

Full validation split, 75,631 windows, 18,746,968 scored tokens:

label support precision recall F1
none 17,029,290 0.9941 0.9762 0.9850
comma 、 1,166,798 0.7412 0.9166 0.8196
period 。 548,865 0.9296 0.9796 0.9539
question ? 2,015 0.5155 0.6610 0.5793
  • macro F1 0.7843, token accuracy 0.9725, validation loss 0.06783.

Held-out test split, 78,058 windows, 19,350,011 tokens:

label support precision recall F1
none 17,598,516 0.9939 0.9770 0.9854
comma 、 1,205,558 0.7458 0.9142 0.8215
period 。 543,456 0.9343 0.9808 0.9570
question ? 2,481 0.5362 0.6844 0.6013
  • macro F1 0.7932, token accuracy 0.9732, test loss 0.06657.

? is rare in the public-core corpus (0.013 % of training positions), so its F1 is lower and noisier than the other marks. The model was selected on validation; the test split was not used for model choice.

The checkpoint's own 2,048-window validation evaluation reports macro F1 0.7710. The full-split numbers above use the same checkpoint with every validation window. The training run's final 2,048-window value 0.7648 belongs to the last step (116,703), not to this checkpoint (114,000).

No-edit guarantee

Decoding appends strings; it cannot change an input character. This was verified end-to-end through the ONNX files on 3,000 held-out documents (374,682 tokens):

path inserted marks changed characters surface mismatch input not recovered
PyTorch checkpoint 40,607 0 0 0
model.onnx fp32 40,640 0 0 0
model.int8.onnx 40,385 0 0 0

The re-tokenization check also matched the stored training ids for all 3,000 documents (tokenizer_mismatches = 0).

Parity

fp32 ONNX vs PyTorch on 240 validation windows (61,440 token positions), feeding the identical int64 arrays to both runtimes:

  • token-id hash identical;
  • max absolute logit difference 2.38e-5 (threshold 1e-4), mean 2.37e-6;
  • argmax agreement 100 %; insertion macro F1 identical (0.7961).

int8 is informational, not parity-exact: max logit difference 0.693, mean 0.046, argmax agreement 99.881 %, insertion macro F1 0.7851 on the same 240 windows. Dynamic qint8 has no calibration set, so this gap is expected. Use fp32 if bit-level agreement with the training model matters.

Latency (int8, CPU)

ONNX Runtime CPU, 8 intra-op threads, one window per forward pass, 200 validation windows, 5 passes, least-contended pass. Host: NVIDIA GB10 (aarch64), load average 5.83 at measurement, so numbers are conservative.

metric at seq 256 median p95
model.int8.onnx, one 256-token window 11.881 ms 11.997 ms
per 1,000 tokens 46.41 ms 46.86 ms
per 1,000 characters (1.995 chars/token measured) 23.26 ms β€”
throughput 21,548 tokens/s β€”

Insertion cost scales with the number of forward passes, not with the number of marks. A text of N tokens needs ceil(N / 256) windows, i.e. 3.906 windows per 1k tokens at the training length. Within a window the token embeddings and the FFN grow linearly with sequence length and self-attention with its square; the measured sweep:

seq median / window median per 1k tokens median per 1k chars
64 2.824 ms 44.13 ms 22.12 ms
128 5.261 ms 41.10 ms 20.60 ms
192 7.788 ms 40.56 ms 20.33 ms
256 11.884 ms 46.42 ms 23.27 ms
384 19.803 ms 51.57 ms 25.85 ms
512 31.640 ms 61.80 ms 30.98 ms

The log-log slope between 64 and 512 tokens is 1.16. Short windows are slightly more efficient per token down to 192, then attention cost dominates; doubling 256 to 512 raises per-token cost by 33 %. fp32 latency was not measured because fp32 is not shipped.

Files

file size note
model.onnx 135,599,640 B fp32, dynamic batch/seq, parity reference
model.int8.onnx 34,240,656 B dynamic qint8, no calibration; shipping file
vocab.json see manifest SentencePiece id-to-piece table
manifest.json see manifest contract, SHAs, quality, parity, latency, licence
LICENSE MIT
NOTICE training-data and lexicon attribution, no-edit statement

int8 is 34.24 MB, inside the 60 MB per-model budget. ONNX weights are not committed to the training git repository; pin the Hugging Face revision and verify each file SHA-256 from manifest.json.

Licence

MIT for the weights, ONNX graphs and export code. The training corpus is CC BY 4.0 and public domain; the public-core lexicon sources (UniDic BSD-3-Clause, IPADic / ICOT, Unicode Unihan, 0C curated CC BY 4.0) are attributed in NOTICE for the combined public-core distribution but contributed no training data to this checkpoint. See NOTICE for full notices and the modification statement.

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. πŸ™‹ Ask for provider support