masahiroid commited on
Commit
eb887dd
·
verified ·
1 Parent(s): b84238f

Reformat README: EN/JA stacked sections with anchor links

Browse files
Files changed (1) hide show
  1. README.md +84 -26
README.md CHANGED
@@ -2,6 +2,7 @@
2
  license: mit
3
  language:
4
  - en
 
5
  tags:
6
  - mlx
7
  - token-classification
@@ -13,16 +14,16 @@ pipeline_tag: token-classification
13
 
14
  # bert-base-ner-mlx
15
 
 
 
 
 
16
  This is an unofficial [MLX](https://github.com/ml-explore/mlx) conversion of
17
  [dslim/bert-base-NER](https://huggingface.co/dslim/bert-base-NER) (a standard
18
  BERT-base with a named-entity-recognition head on top, trained on CoNLL-2003:
19
  PER/ORG/LOC/MISC). All credit for the original model goes to its author.
20
 
21
- [dslim/bert-base-NER](https://huggingface.co/dslim/bert-base-NER)(標準的なBERT-baseに
22
- 固有表現抽出ヘッドを乗せたモデル。CoNLL-2003、PER/ORG/LOC/MISCの4種)の
23
- [MLX](https://github.com/ml-explore/mlx)版です。
24
-
25
- ## This cannot be loaded with mlx-embeddings / mlx-embeddingsでは読み込めません
26
 
27
  `mlx-embeddings` targets pooled embedding/reranker outputs and doesn't
28
  support a per-token classification head. This model was **reimplemented from
@@ -30,12 +31,7 @@ scratch for MLX** and requires the bundled `bert_ner_mlx.py`. The
30
  architecture itself is simple: a standard post-norm BERT encoder plus a
31
  linear classification head.
32
 
33
- `mlx-embeddings`はembedding/reranker用のpooling出力をターゲットにしており、トークン単位の
34
- 分類ヘッド(各トークンに対して独立にラベルを出す構成)には対応していないため、**MLXでの
35
- 実装をゼロから書き起こして**変換しています。同梱の`bert_ner_mlx.py`が必要です。構造自体は
36
- 標準的なpost-norm BERTエンコーダー + 線形分類ヘッドとシンプルです。
37
-
38
- ## Usage / 使い方
39
 
40
  ```python
41
  import mlx.core as mx
@@ -65,17 +61,12 @@ for tok, label_id in zip(tokens, labels):
65
  Labels are the same as the original model: `{0: "O", 1: "B-MISC", 2: "I-MISC",
66
  3: "B-PER", 4: "I-PER", 5: "B-ORG", 6: "I-ORG", 7: "B-LOC", 8: "I-LOC"}`.
67
 
68
- ラベルは元モデルと同じ: `{0: "O", 1: "B-MISC", 2: "I-MISC", 3: "B-PER", 4: "I-PER",
69
- 5: "B-ORG", 6: "I-ORG", 7: "B-LOC", 8: "I-LOC"}`。
70
-
71
- ## Accuracy / 精度検証
72
 
73
  Compared against the PyTorch fp32 reference on a real sentence containing
74
  person/location/organization entities:
75
 
76
- PyTorch fp32リファレンスと、実際の文章1件(人名・地名・組織名を含む)で比較:
77
-
78
- | Precision / 精度 | Logits cosine sim. | Label agreement |
79
  |---|---|---|
80
  | MLX fp32 | 1.0 | 100% |
81
  | MLX fp16 (this release) | 0.99999994 | 100% |
@@ -85,12 +76,7 @@ doesn't fit in fp16) caused `0 * (-inf) = NaN` at unmasked (`mask=1`)
85
  positions. Fixed by switching to `-1e4`, which fits fp16's range (full
86
  details are in the conversion-toolkit repo, not `SECURITY.md`).
87
 
88
- fp16変換の際、attention maskのマスク値に`-1e9`のようなfp16で表現できない大きな負数を使うと、
89
- マスクされていない(`mask=1`)位置で`0 * (-inf)`が発生してNaNになる罠があった。
90
- fp16の範囲内に収まる`-1e4`に変更して解消している(詳細は`SECURITY.md`ではなく、変換ノウハウ
91
- リポジトリに記載)。
92
-
93
- ## Specs
94
 
95
  | Item | Value |
96
  |---|---|
@@ -98,9 +84,81 @@ fp16の範囲内に収まる`-1e4`に変更して解消している(詳細は`
98
  | Precision | float16 |
99
  | Framework | MLX (from-scratch `bert_ner_mlx.py`) |
100
 
101
- ## Notes
102
 
103
  - This is a community conversion, not an official release from the original author.
104
- - 本変換は非公式のコミュニティ版です。
105
  - Security audit uses [model-audit-lite](https://github.com/masahirocom/model-audit-lite)
106
  (see `SECURITY.md` for details).
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  license: mit
3
  language:
4
  - en
5
+ - ja
6
  tags:
7
  - mlx
8
  - token-classification
 
14
 
15
  # bert-base-ner-mlx
16
 
17
+ [English](#model-summary) | [日本語](#モデルの概要)
18
+
19
+ ## Model Summary
20
+
21
  This is an unofficial [MLX](https://github.com/ml-explore/mlx) conversion of
22
  [dslim/bert-base-NER](https://huggingface.co/dslim/bert-base-NER) (a standard
23
  BERT-base with a named-entity-recognition head on top, trained on CoNLL-2003:
24
  PER/ORG/LOC/MISC). All credit for the original model goes to its author.
25
 
26
+ ### This cannot be loaded with mlx-embeddings
 
 
 
 
27
 
28
  `mlx-embeddings` targets pooled embedding/reranker outputs and doesn't
29
  support a per-token classification head. This model was **reimplemented from
 
31
  architecture itself is simple: a standard post-norm BERT encoder plus a
32
  linear classification head.
33
 
34
+ ### Usage
 
 
 
 
 
35
 
36
  ```python
37
  import mlx.core as mx
 
61
  Labels are the same as the original model: `{0: "O", 1: "B-MISC", 2: "I-MISC",
62
  3: "B-PER", 4: "I-PER", 5: "B-ORG", 6: "I-ORG", 7: "B-LOC", 8: "I-LOC"}`.
63
 
64
+ ### Accuracy
 
 
 
65
 
66
  Compared against the PyTorch fp32 reference on a real sentence containing
67
  person/location/organization entities:
68
 
69
+ | Precision | Logits cosine sim. | Label agreement |
 
 
70
  |---|---|---|
71
  | MLX fp32 | 1.0 | 100% |
72
  | MLX fp16 (this release) | 0.99999994 | 100% |
 
76
  positions. Fixed by switching to `-1e4`, which fits fp16's range (full
77
  details are in the conversion-toolkit repo, not `SECURITY.md`).
78
 
79
+ ### Specs
 
 
 
 
 
80
 
81
  | Item | Value |
82
  |---|---|
 
84
  | Precision | float16 |
85
  | Framework | MLX (from-scratch `bert_ner_mlx.py`) |
86
 
87
+ ### Notes
88
 
89
  - This is a community conversion, not an official release from the original author.
 
90
  - Security audit uses [model-audit-lite](https://github.com/masahirocom/model-audit-lite)
91
  (see `SECURITY.md` for details).
92
+
93
+ ---
94
+
95
+ ## モデルの概要
96
+
97
+ [dslim/bert-base-NER](https://huggingface.co/dslim/bert-base-NER)(標準的なBERT-baseに
98
+ 固有表現抽出ヘッドを乗せたモデル。CoNLL-2003、PER/ORG/LOC/MISCの4種)の
99
+ [MLX](https://github.com/ml-explore/mlx)版です。元モデルの著作権はその作者に帰属します。
100
+
101
+ ### mlx-embeddingsでは読み込めません
102
+
103
+ `mlx-embeddings`はembedding/reranker用のpooling出力をターゲットにしており、トークン単位の
104
+ 分類ヘッド(各トークンに対して独立にラベルを出す構成)には対応していないため、**MLXでの
105
+ 実装をゼロから書き起こして**変換しています。同梱の`bert_ner_mlx.py`が必要です。構造自体は
106
+ 標準的なpost-norm BERTエンコーダー + 線形分類ヘッドとシンプルです。
107
+
108
+ ### 使い方
109
+
110
+ ```python
111
+ import mlx.core as mx
112
+ from mlx.utils import tree_unflatten
113
+ from transformers import AutoTokenizer
114
+
115
+ from bert_ner_mlx import BertNerMLX
116
+
117
+ tokenizer = AutoTokenizer.from_pretrained("dslim/bert-base-NER")
118
+ model = BertNerMLX()
119
+ weights = mx.load("model.safetensors")
120
+ model.update(tree_unflatten(list(weights.items())))
121
+ mx.eval(model.parameters())
122
+
123
+ text = "My name is Wolfgang and I live in Berlin, working at Hugging Face."
124
+ inputs = tokenizer(text, return_tensors="np")
125
+ input_ids = mx.array(inputs["input_ids"])
126
+ attention_mask = mx.array(inputs["attention_mask"])
127
+
128
+ logits = model(input_ids, attention_mask=attention_mask) # (1, seq_len, 9)
129
+ labels = logits.argmax(-1)[0].tolist()
130
+ tokens = tokenizer.convert_ids_to_tokens(inputs["input_ids"][0])
131
+ for tok, label_id in zip(tokens, labels):
132
+ print(tok, model.config.id2label[label_id] if hasattr(model, "config") else label_id)
133
+ ```
134
+
135
+ ラベルは元モデルと同じ: `{0: "O", 1: "B-MISC", 2: "I-MISC", 3: "B-PER", 4: "I-PER",
136
+ 5: "B-ORG", 6: "I-ORG", 7: "B-LOC", 8: "I-LOC"}`。
137
+
138
+ ### 精度検証
139
+
140
+ PyTorch fp32リファレンスと、実際の文章1件(人名・地名・組織名を含む)で比較:
141
+
142
+ | 精度 | Logitsコサイン類似度 | ラベル一致率 |
143
+ |---|---|---|
144
+ | MLX fp32 | 1.0 | 100% |
145
+ | MLX fp16(本リリース) | 0.99999994 | 100% |
146
+
147
+ fp16変換の際、attention maskのマスク値に`-1e9`のようなfp16で表現できない大きな負数を使うと、
148
+ マスクされていない(`mask=1`)位置で`0 * (-inf)`が発生してNaNになる罠があった。
149
+ fp16の範囲内に収まる`-1e4`に変更して解消している(詳細は`SECURITY.md`ではなく、変換ノウハウ
150
+ リポジトリに記載)。
151
+
152
+ ### Specs
153
+
154
+ | Item | Value |
155
+ |---|---|
156
+ | ベースモデル | `dslim/bert-base-NER`(BERT-base、108M params) |
157
+ | 精度 | float16 |
158
+ | フレームワーク | MLX(ゼロから実装した`bert_ner_mlx.py`) |
159
+
160
+ ### 備考
161
+
162
+ - 本変換は非公式のコミュニティ版です。
163
+ - セキュリティー監査には[model-audit-lite](https://github.com/masahirocom/model-audit-lite)を
164
+ 使用しています(詳細は`SECURITY.md`)。