kevinwatt commited on
Commit
3533e34
·
verified ·
1 Parent(s): df4b9f0

Release D512 weights continued on corpus v2 (s2b32768-r512, runtime schema v2)

Browse files
Files changed (3) hide show
  1. README.md +23 -4
  2. manifest.json +6 -2
  3. model.onnx +2 -2
README.md CHANGED
@@ -11,7 +11,7 @@ tags:
11
  - traditional-chinese
12
  ---
13
 
14
- # 0c D512 Zhuyin Candidate Reranker (PF512)
15
 
16
  **Project website:** [0c.biggo.com](https://0c.biggo.com)
17
 
@@ -20,7 +20,11 @@ Given confirmed preceding text (up to 64 characters), Zhuyin readings, and suppl
20
  the model computes log P(candidate | context, reading). These scores can be combined with an input-method engine's
21
  scores to decide whether to override its default candidate. A candidate source is required; the model does not generate candidates itself.
22
 
23
- - Architecture: a dedicated causal Transformer decoder, 8 layers, hidden size 512, 8 attention heads, FFN size 768, pretrained from random initialization and then fine-tuned for candidate ranking
 
 
 
 
24
  - Format: ONNX (opset 18), CPU inference
25
  - Vocabulary: 27,857 characters and 416 Zhuyin bases (`vocab.json`)
26
 
@@ -28,7 +32,7 @@ scores to decide whether to override its default candidate. A candidate source i
28
 
29
  | File | Description |
30
  |---|---|
31
- | `model.onnx` | Candidate scoring model; SHA-256 `71af1f0e77b2752cc71cbc04bf19c8e8f2e765662246f766e65a2822bab4e66e` |
32
  | `vocab.json` | Special tokens, character vocabulary, and Zhuyin bases; SHA-256 `556dffaffbf4c05211a846dcee55ef735cea68c5754f9bffcc6f9588124172b3` |
33
  | `manifest.json` | Operating points, input limits, and file hashes |
34
 
@@ -45,7 +49,7 @@ The first run downloads the model from Hugging Face (approximately 130 MB). Subs
45
  ## Usage
46
 
47
  Save the complete example below as `rank.py` and run it. Supply one string per Zhuyin syllable
48
- (for example, `ㄍㄨㄥ` and `ㄩㄢˊ`). Each candidate must have exactly one character per syllable.
49
 
50
  - With supplied candidates only: use `rank()` to order homophonic candidates by model score and find the best match for the context.
51
  - With an input-method engine: use `choose()` to combine engine and model scores, overriding the engine default only when the required score margin is met.
@@ -63,15 +67,23 @@ vocab = json.load(open(f"{path}/vocab.json", encoding="utf-8"))
63
  CHARS = {c: i + 8 for i, c in enumerate(vocab["characters"])} # IDs 0–7 are special tokens; 4 = UNK
64
  BASES = {b: i + 2 for i, b in enumerate(vocab["bases"])}
65
  TONES = {"ˊ": 2, "ˇ": 3, "ˋ": 4, "˙": 5} # No tone mark = first tone
 
66
  NEUTRAL = set(manifest.get("neutral_tolerant_last", [])) # Final particles are often typed with first tone
67
  session = ort.InferenceSession(f"{path}/model.onnx", providers=["CPUExecutionProvider"])
68
 
69
 
70
  def syllable(s):
 
 
71
  base = s[:-1] if s[-1] in TONES else s
72
  return BASES[base], TONES.get(s[-1], 1)
73
 
74
 
 
 
 
 
 
75
  def log_prob(context, reading, candidate):
76
  # [BOS] context [SEP] reading×n [OUT] first n−1 candidate characters.
77
  # Return log P(candidate | context, reading).
@@ -95,6 +107,8 @@ def log_prob(context, reading, candidate):
95
  def score(context, reading, candidate):
96
  s = log_prob(context, reading, candidate)
97
  last = reading[-1]
 
 
98
  base = last[:-1] if last[-1] in TONES else last
99
  if base in NEUTRAL and last[-1] != "˙": # Also score the final syllable with neutral tone and keep the higher score
100
  s = max(s, log_prob(context, reading[:-1] + [base + "˙"], candidate))
@@ -122,6 +136,10 @@ print(rank("下週就是", ["ㄑㄧˊ", "ㄓㄨㄥ", "ㄎㄠˇ"], ["其中考",
122
  print(rank("", ["ㄐㄧㄣ", "ㄊㄧㄢ", "ㄊㄧㄢ", "ㄑㄧˋ", "ㄏㄣˇ", "ㄏㄠˇ"],
123
  ["金天天氣很好", "今天天氣很好", "今天天器很好"])) # ['今天天氣很好', ...]
124
 
 
 
 
 
125
  # With an input-method engine, override candidates[0] only when the fused score warrants it.
126
  candidates = ["公元", "公園"]
127
  print(candidates[choose("今天天氣很好我們去", ["ㄍㄨㄥ", "ㄩㄢˊ"], candidates, [-7.0, -7.5])]) # 公園
@@ -134,6 +152,7 @@ print(candidates[choose("今天天氣很好我們去", ["ㄍㄨㄥ", "ㄩㄢˊ"]
134
  - If any candidate contains a character outside the vocabulary, `choose()` retains the engine default for the entire case.
135
  - Training candidates came from the 0c dictionary (`dictionary_sha256 = 517e6c8bb5cbb145c0554edc3cc91c2b55fc753dd42e11194d83f6b0e119aea3`). Calibrate w and τ again when using a different engine's score scale.
136
  - Shared candidate prefixes can also be scored together as a tree, using an ancestor attention mask and multiple `target_node`/`target_ids` rows. Scores match individual candidate scoring.
 
137
 
138
  ## License and Release Scope
139
 
 
11
  - traditional-chinese
12
  ---
13
 
14
+ # 0c D512 Zhuyin Candidate Reranker, Tone-Optional
15
 
16
  **Project website:** [0c.biggo.com](https://0c.biggo.com)
17
 
 
20
  the model computes log P(candidate | context, reading). These scores can be combined with an input-method engine's
21
  scores to decide whether to override its default candidate. A candidate source is required; the model does not generate candidates itself.
22
 
23
+ Tones are optional. Each syllable may carry its tone mark, omit the tone entirely (unknown tone), or mix both within one reading,
24
+ so the same model serves full-tone, toneless, and partially toned typing. Without a tone mark a syllable means first tone,
25
+ as in earlier releases; append `?` (or use `toneless()`) to mark a tone that was not typed.
26
+
27
+ - Architecture: a dedicated causal Transformer decoder, 8 layers, hidden size 512, 8 attention heads, FFN size 768, pretrained from random initialization, further pretrained on additional text with tone-optional reading tasks, and then fine-tuned for candidate ranking
28
  - Format: ONNX (opset 18), CPU inference
29
  - Vocabulary: 27,857 characters and 416 Zhuyin bases (`vocab.json`)
30
 
 
32
 
33
  | File | Description |
34
  |---|---|
35
+ | `model.onnx` | Candidate scoring model; SHA-256 `4a80caf18acbaddde10c908dcbf35919908d616e94618cdde71fc3409c969261` |
36
  | `vocab.json` | Special tokens, character vocabulary, and Zhuyin bases; SHA-256 `556dffaffbf4c05211a846dcee55ef735cea68c5754f9bffcc6f9588124172b3` |
37
  | `manifest.json` | Operating points, input limits, and file hashes |
38
 
 
49
  ## Usage
50
 
51
  Save the complete example below as `rank.py` and run it. Supply one string per Zhuyin syllable
52
+ (for example, `ㄍㄨㄥ` and `ㄩㄢˊ`, or `ㄩㄢ?` when the tone was not typed). Each candidate must have exactly one character per syllable.
53
 
54
  - With supplied candidates only: use `rank()` to order homophonic candidates by model score and find the best match for the context.
55
  - With an input-method engine: use `choose()` to combine engine and model scores, overriding the engine default only when the required score margin is met.
 
67
  CHARS = {c: i + 8 for i, c in enumerate(vocab["characters"])} # IDs 0–7 are special tokens; 4 = UNK
68
  BASES = {b: i + 2 for i, b in enumerate(vocab["bases"])}
69
  TONES = {"ˊ": 2, "ˇ": 3, "ˋ": 4, "˙": 5} # No tone mark = first tone
70
+ UNKNOWN = "?" # Trailing "?" = tone not typed (tone id 0)
71
  NEUTRAL = set(manifest.get("neutral_tolerant_last", [])) # Final particles are often typed with first tone
72
  session = ort.InferenceSession(f"{path}/model.onnx", providers=["CPUExecutionProvider"])
73
 
74
 
75
  def syllable(s):
76
+ if s.endswith(UNKNOWN):
77
+ return BASES[s[:-1]], 0
78
  base = s[:-1] if s[-1] in TONES else s
79
  return BASES[base], TONES.get(s[-1], 1)
80
 
81
 
82
+ def toneless(reading):
83
+ # Mark every syllable as tone-unknown, e.g. for users who type without tones.
84
+ return [s.rstrip("ˊˇˋ˙" + UNKNOWN) + UNKNOWN for s in reading]
85
+
86
+
87
  def log_prob(context, reading, candidate):
88
  # [BOS] context [SEP] reading×n [OUT] first n−1 candidate characters.
89
  # Return log P(candidate | context, reading).
 
107
  def score(context, reading, candidate):
108
  s = log_prob(context, reading, candidate)
109
  last = reading[-1]
110
+ if last.endswith(UNKNOWN): # An unknown final tone already covers the neutral tone
111
+ return s
112
  base = last[:-1] if last[-1] in TONES else last
113
  if base in NEUTRAL and last[-1] != "˙": # Also score the final syllable with neutral tone and keep the higher score
114
  s = max(s, log_prob(context, reading[:-1] + [base + "˙"], candidate))
 
136
  print(rank("", ["ㄐㄧㄣ", "ㄊㄧㄢ", "ㄊㄧㄢ", "ㄑㄧˋ", "ㄏㄣˇ", "ㄏㄠˇ"],
137
  ["金天天氣很好", "今天天氣很好", "今天天器很好"])) # ['今天天氣很好', ...]
138
 
139
+ # Tones are optional: mark untyped tones with "?" (or use toneless()); tones may be mixed per syllable.
140
+ print(rank("下週就是", toneless(["ㄑㄧ", "ㄓㄨㄥ", "ㄎㄠ"]), ["其中考", "期中考"])) # ['期中考', '其中考']
141
+ print(rank("這家餐廳的", ["ㄘㄞ?", "ㄏㄣˇ", "ㄏㄠ?", "ㄔ"], ["才很好吃", "菜很好吃"])) # ['菜很好吃', '才很好吃']
142
+
143
  # With an input-method engine, override candidates[0] only when the fused score warrants it.
144
  candidates = ["公元", "公園"]
145
  print(candidates[choose("今天天氣很好我們去", ["ㄍㄨㄥ", "ㄩㄢˊ"], candidates, [-7.0, -7.5])]) # 公園
 
152
  - If any candidate contains a character outside the vocabulary, `choose()` retains the engine default for the entire case.
153
  - Training candidates came from the 0c dictionary (`dictionary_sha256 = 517e6c8bb5cbb145c0554edc3cc91c2b55fc753dd42e11194d83f6b0e119aea3`). Calibrate w and τ again when using a different engine's score scale.
154
  - Shared candidate prefixes can also be scored together as a tree, using an ancestor attention mask and multiple `target_node`/`target_ids` rows. Scores match individual candidate scoring.
155
+ - An unknown tone is encoded as tone id 0 (`tone_policy` in `manifest.json`). A final syllable with an unknown tone is not rescored as a neutral tone.
156
 
157
  ## License and Release Scope
158
 
manifest.json CHANGED
@@ -1,12 +1,16 @@
1
  {
2
- "schema": "zuin-sequence-runtime-v1",
3
  "context_policy": "confirmed_left_64",
 
 
 
 
4
  "allow_partial": true,
5
  "dictionary_sha256": "517e6c8bb5cbb145c0554edc3cc91c2b55fc753dd42e11194d83f6b0e119aea3",
6
  "weight": 2.0,
7
  "margin": 0.0,
8
  "files": {
9
- "model.onnx": "71af1f0e77b2752cc71cbc04bf19c8e8f2e765662246f766e65a2822bab4e66e",
10
  "vocab.json": "556dffaffbf4c05211a846dcee55ef735cea68c5754f9bffcc6f9588124172b3"
11
  },
12
  "spec": {
 
1
  {
2
+ "schema": "zuin-sequence-runtime-v2",
3
  "context_policy": "confirmed_left_64",
4
+ "tone_policy": {
5
+ "schema": "zuin-tone-mask-v1",
6
+ "unknown_tone_id": 0
7
+ },
8
  "allow_partial": true,
9
  "dictionary_sha256": "517e6c8bb5cbb145c0554edc3cc91c2b55fc753dd42e11194d83f6b0e119aea3",
10
  "weight": 2.0,
11
  "margin": 0.0,
12
  "files": {
13
+ "model.onnx": "4a80caf18acbaddde10c908dcbf35919908d616e94618cdde71fc3409c969261",
14
  "vocab.json": "556dffaffbf4c05211a846dcee55ef735cea68c5754f9bffcc6f9588124172b3"
15
  },
16
  "spec": {
model.onnx CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:71af1f0e77b2752cc71cbc04bf19c8e8f2e765662246f766e65a2822bab4e66e
3
- size 130723134
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4a80caf18acbaddde10c908dcbf35919908d616e94618cdde71fc3409c969261
3
+ size 130723137