Upload README.md with huggingface_hub
Browse files
README.md
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- zh
|
| 4 |
+
license: gpl-3.0
|
| 5 |
+
library_name: coremltools
|
| 6 |
+
tags:
|
| 7 |
+
- coreml
|
| 8 |
+
- bert
|
| 9 |
+
- token-classification
|
| 10 |
+
- word-segmentation
|
| 11 |
+
- pos-tagging
|
| 12 |
+
- named-entity-recognition
|
| 13 |
+
- traditional-chinese
|
| 14 |
+
- ckip
|
| 15 |
+
- apple-neural-engine
|
| 16 |
+
- ios
|
| 17 |
+
datasets:
|
| 18 |
+
- ckiplab/ckip-transformers
|
| 19 |
+
base_model:
|
| 20 |
+
- ckiplab/bert-base-chinese-ws
|
| 21 |
+
- ckiplab/bert-base-chinese-pos
|
| 22 |
+
- ckiplab/bert-base-chinese-ner
|
| 23 |
+
---
|
| 24 |
+
|
| 25 |
+
# CKIP BERT-base CoreML — 繁體中文 WS/POS/NER for iOS/macOS
|
| 26 |
+
|
| 27 |
+
Apple CoreML 版本的 CKIP BERT-base 繁體中文 NLP 模型,可在 iOS/macOS 上透過 Apple Neural Engine (ANE) 執行。
|
| 28 |
+
|
| 29 |
+
從 [ckiplab/ckip-transformers](https://github.com/ckiplab/ckip-transformers) 轉換,經由 [ckip-mlx](https://huggingface.co/FakeRockert543/ckip-mlx) 中繼。
|
| 30 |
+
|
| 31 |
+
## 模型說明
|
| 32 |
+
|
| 33 |
+
| 任務 | 說明 | 標籤數 | 原始模型 |
|
| 34 |
+
|------|------|------:|---------|
|
| 35 |
+
| WS | 中文斷詞 | 2 (B/I) | ckiplab/bert-base-chinese-ws |
|
| 36 |
+
| POS | 詞性標注 | 60 | ckiplab/bert-base-chinese-pos |
|
| 37 |
+
| NER | 命名實體辨識 | 73 (BIOES) | ckiplab/bert-base-chinese-ner |
|
| 38 |
+
|
| 39 |
+
所有模型支援動態序列長度 1–512。
|
| 40 |
+
|
| 41 |
+
## 可用版本
|
| 42 |
+
|
| 43 |
+
| 版本 | 單模型大小 | 精度 (vs fp32) | 建議用途 |
|
| 44 |
+
|------|--------:|--------------|---------|
|
| 45 |
+
| fp32 | 388 MB | baseline (與 MLX fp32 100% 一致) | 追求完全精度 |
|
| 46 |
+
| fp16 | 194 MB | WS 100% / POS 99.97% / NER 99.99% | **推薦預設** ⚡ |
|
| 47 |
+
| q8 | 98 MB | WS 99.96% / POS 98.83% / NER 99.76% | 低記憶體 iPhone |
|
| 48 |
+
|
| 49 |
+
## 速度
|
| 50 |
+
|
| 51 |
+
測試環境:Apple M4 Max / 128GB / macOS 26.3.1
|
| 52 |
+
測試資料:維基百科「臺灣」條目,36,245 字
|
| 53 |
+
|
| 54 |
+
| Framework | fp32 | fp16 |
|
| 55 |
+
|-----------|-----:|-----:|
|
| 56 |
+
| **CoreML** | 2,879 ms | **2,352 ms** ⚡ |
|
| 57 |
+
| MLX | 2,869 ms | 3,092 ms |
|
| 58 |
+
| HF Transformers (MPS) | 3,532 ms | 3,096 ms |
|
| 59 |
+
| CKIP 官方 (MPS) | 14,926 ms | 11,850 ms |
|
| 60 |
+
|
| 61 |
+
CoreML fp16 是所有框架中最快的,比 CKIP 官方快 **6.3 倍**。
|
| 62 |
+
|
| 63 |
+
## 使用方式
|
| 64 |
+
|
| 65 |
+
### Python
|
| 66 |
+
|
| 67 |
+
```python
|
| 68 |
+
import coremltools as ct
|
| 69 |
+
import numpy as np
|
| 70 |
+
|
| 71 |
+
model = ct.models.MLModel("ckip_ws_fp16.mlpackage")
|
| 72 |
+
|
| 73 |
+
text = "台積電今天股價上漲三十元"
|
| 74 |
+
input_ids = np.array([[101] + [vocab[ch] for ch in text] + [102]])
|
| 75 |
+
attention_mask = np.ones_like(input_ids)
|
| 76 |
+
|
| 77 |
+
out = model.predict({"input_ids": input_ids, "attention_mask": attention_mask})
|
| 78 |
+
preds = np.argmax(out["logits"], axis=-1)[0]
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
### Swift / iOS
|
| 82 |
+
|
| 83 |
+
```swift
|
| 84 |
+
let model = try MLModel(contentsOf: modelURL)
|
| 85 |
+
let input = try MLDictionaryFeatureProvider(dictionary: [
|
| 86 |
+
"input_ids": MLMultiArray(inputIds),
|
| 87 |
+
"attention_mask": MLMultiArray(attentionMask)
|
| 88 |
+
])
|
| 89 |
+
let output = try model.prediction(from: input)
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
## 量化精度詳細測試
|
| 93 |
+
|
| 94 |
+
以維基百科「臺灣」條目 36,245 字測試,與 fp32 逐 token 比對(共 36,389 tokens):
|
| 95 |
+
|
| 96 |
+
### fp16
|
| 97 |
+
- WS: 1 token 不同 (100.00%)
|
| 98 |
+
- POS: 11 tokens 不同 (99.97%)
|
| 99 |
+
- NER: 3 tokens 不同 (99.99%)
|
| 100 |
+
|
| 101 |
+
### q8
|
| 102 |
+
- WS: 13 tokens 不同 (99.96%)
|
| 103 |
+
- POS: 425 tokens 不同 (98.83%)
|
| 104 |
+
- NER: 89 tokens 不同 (99.76%)
|
| 105 |
+
|
| 106 |
+
## 跨框架驗證
|
| 107 |
+
|
| 108 |
+
CoreML fp32 與 MLX fp32、HF Transformers fp32 的 WS/POS/NER 輸出**完全一致**,確認轉換正確。
|
| 109 |
+
|
| 110 |
+
## 相關專案
|
| 111 |
+
|
| 112 |
+
- [FakeRockert543/ckip-mlx](https://huggingface.co/FakeRockert543/ckip-mlx) — MLX 版本(桌面推薦)
|
| 113 |
+
- [FakeRocket543/ckip-coreml](https://github.com/FakeRocket543/ckip-coreml) — 原始碼與轉換腳本
|
| 114 |
+
|
| 115 |
+
## 授權
|
| 116 |
+
|
| 117 |
+
[GPL-3.0](https://www.gnu.org/licenses/gpl-3.0.html),依循原始 [ckiplab/ckip-transformers](https://github.com/ckiplab/ckip-transformers) 授權。
|
| 118 |
+
|
| 119 |
+
## 致謝
|
| 120 |
+
|
| 121 |
+
- [CKIP Lab, 中央研究院資訊科學研究所](https://ckip.iis.sinica.edu.tw/) — 原始模型
|
| 122 |
+
- [Apple CoreML](https://developer.apple.com/documentation/coreml) — 推論框架
|