Glazkov commited on
Commit
ee06cb1
·
verified ·
1 Parent(s): 274eb52

Add README.md

Browse files
Files changed (1) hide show
  1. README.md +193 -0
README.md ADDED
@@ -0,0 +1,193 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ language:
4
+ - en
5
+ base_model: answerdotai/ModernBERT-base
6
+ tags:
7
+ - masked-language-modeling
8
+ - summarization-evaluation
9
+ - entity-infilling
10
+ - mars
11
+ - modernbert
12
+ library_name: transformers
13
+ pipeline_tag: fill-mask
14
+ datasets:
15
+ - cnn_dailymail
16
+ - xsum
17
+ - multi_news
18
+ - samsum
19
+ ---
20
+
21
+ # MARS — Shared Cross-Attention (ModernBERT-base)
22
+
23
+ Best single-model checkpoint from the MARS (Masked Accuracy Recovery Score)
24
+ research project: a **shared-encoder cross-attention** model for evaluating
25
+ text summarization quality through masked entity recovery.
26
+
27
+ A summary is "good" if a separate language model can recover the entities
28
+ that were masked out of the original article using only the summary as
29
+ context. The MARS score is the composite metric on those reconstructions.
30
+
31
+ ## Results
32
+
33
+ Evaluated on a 1,500-sample CNN/DailyMail subset (seed=42):
34
+
35
+ | Model | MARSv2 | Notes |
36
+ |----------------------------------------|-----------|--------------------------------|
37
+ | baseline (merged input, non-shared) | ~52 | original reference |
38
+ | **shared cross-attention (this model)**| **57.02** | **best single model, 2L × 3 epochs** |
39
+ | 4-way ensemble (1L+2L+6L+baseline) | ~59.0 | aggregate champion |
40
+
41
+ Single-model copy-ceiling analysis: this checkpoint achieves 56.17% strict
42
+ entity recall on the eval subset, with an estimated 72% achievable if a
43
+ perfect copy-from-summary mechanism were attached.
44
+
45
+ ## Architecture
46
+
47
+ - Single `answerdotai/ModernBERT-base` encoder (149M params), shared between
48
+ the summary and the masked-text streams.
49
+ - 2 cross-attention layers where masked-text queries attend to
50
+ summary keys/values.
51
+ - Linear vocabulary projection head over the resized vocab
52
+ (50,368 base + 21 special tokens = 50,389).
53
+
54
+ Total: ~190M parameters, single safetensors-equivalent torch checkpoint
55
+ (`model.pt`, ~770 MB fp32).
56
+
57
+ ### Special tokens
58
+
59
+ - `[ENTMASK]` — generic mask
60
+ - `[ENTSTART]` / `[ENTEND]` — multi-token entity boundaries
61
+ - `[ENTMASK_<TYPE>]` for 18 spaCy NER types: `PERSON, ORG, GPE, LOC, DATE,
62
+ TIME, MONEY, QUANTITY, PERCENT, CARDINAL, ORDINAL, EVENT, WORK_OF_ART,
63
+ LAW, LANGUAGE, FAC, PRODUCT, NORP`
64
+
65
+ ## Usage
66
+
67
+ ### Install
68
+
69
+ ```bash
70
+ pip install transformers torch huggingface_hub
71
+ ```
72
+
73
+ Download the two helper files from this repo: `modeling_mars.py` and
74
+ `inference.py` (they are not auto-loaded by `AutoModel` because the
75
+ architecture is custom).
76
+
77
+ ### Quick example
78
+
79
+ ```python
80
+ from inference import MarsInference
81
+
82
+ inf = MarsInference("Glazkov/mars-shared-cross-attention-modernbert")
83
+
84
+ summary = (
85
+ "The president announced a new climate policy in Washington on Tuesday, "
86
+ "promising to cut emissions by 40% by 2030."
87
+ )
88
+ masked_text = (
89
+ "<mask> announced a new climate policy in <mask> on <mask>, "
90
+ "promising to cut emissions by <mask> by <mask>."
91
+ )
92
+ entity_types = ["PERSON", "GPE", "DATE", "PERCENT", "DATE"]
93
+
94
+ predictions, confidences = inf.predict(
95
+ summary, masked_text,
96
+ entity_types=entity_types,
97
+ return_confidence=True,
98
+ )
99
+ for t, p, c in zip(entity_types, predictions, confidences):
100
+ print(f" [{t}] -> {p!r} (conf={c:.2f})")
101
+ ```
102
+
103
+ ### Manual loading
104
+
105
+ ```python
106
+ from modeling_mars import load_model_from_checkpoint
107
+
108
+ model, tokenizer, device = load_model_from_checkpoint(
109
+ "Glazkov/mars-shared-cross-attention-modernbert"
110
+ )
111
+
112
+ # Both inputs go through the SAME encoder; the masked stream cross-attends
113
+ # to the summary stream via the 2 cross-attention layers.
114
+ summary_enc = tokenizer("the summary text", return_tensors="pt").to(device)
115
+ masked_enc = tokenizer(
116
+ "the original text with [ENTSTART] [ENTMASK_PERSON] removed",
117
+ return_tensors="pt",
118
+ ).to(device)
119
+
120
+ with torch.no_grad():
121
+ out = model(
122
+ summary_input_ids=summary_enc["input_ids"],
123
+ summary_attention_mask=summary_enc["attention_mask"],
124
+ masked_input_ids=masked_enc["input_ids"],
125
+ masked_attention_mask=masked_enc["attention_mask"],
126
+ )
127
+ logits = out.logits # [batch, seq_len, vocab]
128
+ ```
129
+
130
+ ### Scoring a summary (MARS-style)
131
+
132
+ ```python
133
+ import spacy
134
+ import re
135
+
136
+ nlp = spacy.load("en_core_web_sm")
137
+
138
+ def mask_entities(text: str):
139
+ doc = nlp(text)
140
+ masked, types, golds = text, [], []
141
+ # iterate in reverse so character offsets remain valid
142
+ for ent in sorted(doc.ents, key=lambda e: -e.start_char):
143
+ masked = masked[:ent.start_char] + "<mask>" + masked[ent.end_char:]
144
+ types.insert(0, ent.label_)
145
+ golds.insert(0, ent.text)
146
+ return masked, types, golds
147
+
148
+ article = "..."
149
+ summary = "..."
150
+
151
+ masked_text, types, gold = mask_entities(article)
152
+ preds = inf.predict(summary, masked_text, entity_types=types)
153
+ recall = sum(p.lower() == g.lower() for p, g in zip(preds, gold)) / max(1, len(gold))
154
+ print(f"Entity recall: {recall:.2%}")
155
+ ```
156
+
157
+ Higher recall = the summary preserves more of the original article's
158
+ factual content. This is the core signal behind the MARS metric.
159
+
160
+ ## Training data
161
+
162
+ Train splits of four English summarization datasets:
163
+ - CNN/DailyMail
164
+ - XSum
165
+ - Multi-News
166
+ - SAMSum
167
+
168
+ Entities were extracted with spaCy `en_core_web_sm` NER and replaced with
169
+ typed mask tokens. The model was trained for 3 epochs at LR 5e-5,
170
+ batch size 8, on a single A100 (~24 h wall time).
171
+
172
+ ## Limitations
173
+
174
+ - English only. Multilingual transfer was not tested.
175
+ - Max sequence length 1024 (ModernBERT). Long articles get truncated.
176
+ - The model exhibits "confident hallucination" of plausible-but-wrong
177
+ same-type entities (e.g. PERSON → wrong person). PERSON error rate is
178
+ ~60% on held-out eval.
179
+ - Best as a relative-comparison metric across summaries, not as an
180
+ absolute factuality judgment on any single summary.
181
+
182
+ ## Citation
183
+
184
+ Internal research project. If you use this checkpoint, please cite it as:
185
+
186
+ ```bibtex
187
+ @misc{mars2026,
188
+ title = {MARS: Masked Accuracy Recovery Score for Summarization},
189
+ author = {Glazkov, Nikita},
190
+ year = {2026},
191
+ url = {https://huggingface.co/Glazkov/mars-shared-cross-attention-modernbert}
192
+ }
193
+ ```