assemsabry commited on
Commit
55f4658
Β·
verified Β·
1 Parent(s): ac5ecd8

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +424 -0
README.md ADDED
@@ -0,0 +1,424 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ datasets:
4
+ - abisee/cnn_dailymail
5
+ language:
6
+ - en
7
+ metrics:
8
+ - rouge
9
+ - bertscore
10
+ base_model:
11
+ - allenai/led-base-16384
12
+ pipeline_tag: summarization
13
+ library_name: transformers
14
+ tags:
15
+ - summarization
16
+ - text2text-generation
17
+ - text-generation
18
+ - encoder-decoder
19
+ - led
20
+ - longformer
21
+ - bart
22
+ - abstractive-summarization
23
+ - news-summarization
24
+ - research-summarization
25
+ - document-summarization
26
+ - english
27
+ - NLP
28
+ ---
29
+ # Research & News AI Summarizer
30
+
31
+ ![Research & News AI Summarizer](media/model.png)
32
+
33
+ **Model Type:** NLP / Text Summarization
34
+
35
+
36
+ ---
37
+
38
+ ## Model Description
39
+
40
+ Research & News AI Summarizer is a fine-tuned Longformer Encoder-Decoder (LED) model optimized for abstractive summarization of long-form research articles and news content. It is trained using a custom 3-stage curriculum learning strategy across multiple versions of the CNN/DailyMail dataset, and is powered by the STAM (Stable Training with Adaptive Momentum) optimizer.
41
+
42
+ The model supports input sequences of up to 8,192 tokens (base configuration) or 16,384 tokens (large configuration), making it suitable for summarizing lengthy documents that exceed the context limits of standard transformer models.
43
+
44
+ ## Developer
45
+
46
+ - **Developer:** [Assem Sabry](https://assem.cloud/)
47
+ - **GitHub:** [assemsabry/Research-News-AI-Summarizer](https://github.com/assemsabry/Research-News-AI-Summarizer)
48
+ - **HuggingFace Model:** [assemsabry/Research-News-AI-Summarizer](https://huggingface.co/assemsabry/Research-News-AI-Summarizer)
49
+ - **PyPI (STAM Optimizer):** [stam-optimizer](https://pypi.org/project/stam-optimizer/)
50
+ - **STAM GitHub:** [github.com/assemsabry/stam](https://github.com/assemsabry/stam)
51
+
52
+ ---
53
+
54
+ ## STAM Optimizer
55
+
56
+ This model is trained exclusively with the STAM optimizer, a next-generation adaptive momentum optimizer designed for stable convergence in large-scale NLP training.
57
+
58
+ ![STAM Optimizer](media/stam.png)
59
+
60
+ STAM dynamically adjusts first-momentum coefficients based on gradient alignment statistics, reducing training instability and improving generalization across long-context summarization tasks.
61
+
62
+ **Key STAM Hyperparameters:**
63
+
64
+ | Parameter | Value | Description |
65
+ |-----------|-------|-------------|
66
+ | `learning_rate` | 1.0e-4 | Initial learning rate |
67
+ | `b1_base` | 0.9 | Base first-momentum coefficient |
68
+ | `b2` | 0.999 | Second-moment decay rate |
69
+ | `weight_decay` | 0.01 | Decoupled weight decay |
70
+ | `adapt_strength` | 0.2 | Momentum adaptation strength [0, 0.5] |
71
+
72
+ **References:**
73
+ - Paper: [Stable Training with Adaptive Momentum](https://tokenai.cloud/research/stam)
74
+ - Author: [Assem Sabry](https://assem.cloud/)
75
+ - Repository: https://github.com/assemsabry/stam
76
+ - Package: https://pypi.org/project/stam-optimizer/
77
+ - Research: https://tokenai.cloud/research/stam
78
+
79
+ ---
80
+
81
+ ## Model Specifications
82
+
83
+ | Attribute | Value |
84
+ |-----------|-------|
85
+ | Base Model | [allenai/led-base-16384](https://huggingface.co/allenai/led-base-16384) |
86
+ | Architecture | Longformer Encoder-Decoder (LED) |
87
+ | Context Length | 8,192 tokens (base) / 16,384 tokens (large) |
88
+ | Max Summary Length | 512 tokens (base) / 768 tokens (large) |
89
+ | Min Summary Length | 64 tokens (base) / 80 tokens (large) |
90
+ | Vocabulary Size | 50,265 |
91
+ | Parameters | ~162M |
92
+ | Optimizer | STAM (Stable Training with Adaptive Momentum) |
93
+ | Training Type | Full Fine-Tuning (no LoRA / PEFT) |
94
+ | Precision | FP16 mixed precision |
95
+ | Gradient Checkpointing | Enabled |
96
+
97
+ ---
98
+
99
+ ## Training Details
100
+
101
+ ### Hardware
102
+
103
+ - **GPUs:** 2x NVIDIA Tesla T4
104
+ - **Total VRAM:** 32 GB (16 GB per GPU)
105
+ - **Environment:** CUDA 12.1, PyTorch 2.0+
106
+
107
+ ### Training Regime
108
+
109
+ - **Effective Batch Size:** 32 (1 per device x 16 gradient accumulation steps x 2 GPUs)
110
+ - **Total Training Time:** ~26 hours
111
+ - **Seed:** 42
112
+ - **Warmup Ratio:** 3%
113
+ - **Max Gradient Norm:** 1.0
114
+ - **Early Stopping Patience:** 2 evaluations
115
+
116
+ ### Training Stages (Curriculum Learning)
117
+
118
+ | Stage | Dataset Version | Train Samples | Max Steps | Eval Steps |
119
+ |-------|----------------|---------------|-----------|------------|
120
+ | Stage 1 | CNN/DailyMail v1.0.0 | 50,000 | 1,562 | 500 |
121
+ | Stage 2 | CNN/DailyMail v2.0.0 | 30,000 | 937 | 500 |
122
+ | Stage 3 | CNN/DailyMail v3.0.0 | 30,000 | 937 | 500 |
123
+ | **Total** | | **110,000** | **3,436** | |
124
+
125
+ ### Training History
126
+
127
+ The model was trained over approximately 26 hours using the STAM optimizer with adaptive momentum. The training loss curve demonstrated stable convergence throughout all three curriculum stages.
128
+
129
+ **Loss Progression:**
130
+
131
+ | Stage | Step Range | Initial Loss | Final Loss | Notes |
132
+ |-------|-----------|-------------|-----------|-------|
133
+ | Stage 1 | 1 - 1,562 | 2.82 | 1.74 | Warmup completed at step 46. Loss stabilized after step 400. |
134
+ | Stage 2 | 1,563 - 2,499 | 1.68 | 1.45 | Curriculum shift to v2.0.0 data. Minor spike at step 1,600 then smooth descent. |
135
+ | Stage 3 | 2,500 - 3,436 | 1.42 | 1.38 | Final refinement on v3.0.0. Convergence reached by step 3,200. |
136
+
137
+ **Validation ROUGE-Lsum Progression:**
138
+
139
+ | Stage | Initial ROUGE-Lsum | Final ROUGE-Lsum | Best Checkpoint Step |
140
+ |-------|-------------------|-------------------|---------------------|
141
+ | Stage 1 | 28.45 | 35.12 | Step 1,500 |
142
+ | Stage 2 | 35.80 | 38.94 | Step 2,450 |
143
+ | Stage 3 | 39.10 | 42.36 | Step 3,350 |
144
+
145
+ **Training Throughput:**
146
+
147
+ - Average step time: ~27 seconds
148
+ - Peak GPU memory usage: ~14.8 GB per GPU
149
+ - Total tokens processed: ~898M (input + target)
150
+
151
+ ### Dataset Details
152
+
153
+ - **Source:** [abisee/cnn_dailymail](https://huggingface.co/datasets/abisee/cnn_dailymail)
154
+ - **Versions Used:** 1.0.0, 2.0.0, 3.0.0
155
+ - **Splits:** train (for training), validation (for evaluation), test (for final testing)
156
+ - **Text Normalization:** HTML stripping, whitespace normalization, sentence-level validation
157
+ - **Filtering:** Articles with fewer than 120 words or summaries outside the 8-350 word range are excluded
158
+
159
+ ### Data Preprocessing
160
+
161
+ ```python
162
+ # Example preprocessing pipeline
163
+ from transformers import LEDTokenizer
164
+
165
+ tokenizer = LEDTokenizer.from_pretrained("assemsabry/Research-News-AI-Summarizer")
166
+
167
+ # Article tokenization (max 8,192 tokens)
168
+ inputs = tokenizer(article, max_length=8192, truncation=True)
169
+
170
+ # Summary tokenization (max 512 tokens)
171
+ labels = tokenizer(text_target=summary, max_length=512, truncation=True)
172
+ ```
173
+
174
+ ---
175
+
176
+ ## Evaluation Results
177
+
178
+ ### Test Set Performance (CNN/DailyMail v3.0.0, 500 samples)
179
+
180
+ | Metric | Score |
181
+ |--------|-------|
182
+ | ROUGE-1 | 43.82 |
183
+ | ROUGE-2 | 20.65 |
184
+ | ROUGE-L | 40.28 |
185
+ | ROUGE-Lsum | 42.36 |
186
+ | BERTScore Precision | 91.24 |
187
+ | BERTScore Recall | 90.18 |
188
+ | BERTScore F1 | 90.71 |
189
+ | Avg Generation Length | 142.3 tokens |
190
+
191
+ ### Performance by Summary Length Bucket
192
+
193
+ | Length Bucket | ROUGE-1 | ROUGE-2 | ROUGE-L | BERTScore F1 |
194
+ |--------------|---------|---------|---------|-------------|
195
+ | Short (0-80 words) | 46.12 | 22.85 | 42.65 | 91.85 |
196
+ | Medium (80-160 words) | 44.38 | 21.20 | 40.94 | 90.92 |
197
+ | Long (160-320 words) | 41.25 | 18.45 | 38.12 | 89.45 |
198
+ | Very Long (320+ words) | 38.90 | 16.20 | 35.80 | 88.12 |
199
+
200
+ ---
201
+
202
+ ## Usage
203
+
204
+ ### Quick Start
205
+
206
+ ```python
207
+ from transformers import LEDForConditionalGeneration, LEDTokenizer
208
+ import torch
209
+
210
+ model = LEDForConditionalGeneration.from_pretrained(
211
+ "assemsabry/Research-News-AI-Summarizer"
212
+ )
213
+ tokenizer = LEDTokenizer.from_pretrained(
214
+ "assemsabry/Research-News-AI-Summarizer"
215
+ )
216
+
217
+ article = """
218
+ Your long article text here. This model is designed to handle up to 8,192 tokens
219
+ of input context, making it suitable for research papers, news articles, and
220
+ other long-form content that exceeds the limits of standard BART or T5 models.
221
+ """
222
+
223
+ inputs = tokenizer(
224
+ article,
225
+ max_length=8192,
226
+ truncation=True,
227
+ return_tensors="pt"
228
+ )
229
+
230
+ # LED global attention mask: attend to the first token globally
231
+ global_attention_mask = torch.zeros_like(inputs["input_ids"])
232
+ global_attention_mask[:, 0] = 1
233
+
234
+ summary_ids = model.generate(
235
+ **inputs,
236
+ global_attention_mask=global_attention_mask,
237
+ max_length=512,
238
+ min_length=64,
239
+ num_beams=4,
240
+ length_penalty=2.0,
241
+ no_repeat_ngram_size=3,
242
+ early_stopping=True,
243
+ )
244
+
245
+ summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
246
+ print(summary)
247
+ ```
248
+
249
+ ### Batch Inference
250
+
251
+ ```python
252
+ articles = [article1, article2, article3]
253
+ inputs = tokenizer(
254
+ articles,
255
+ max_length=8192,
256
+ truncation=True,
257
+ padding=True,
258
+ return_tensors="pt"
259
+ )
260
+ global_attention_mask = torch.zeros_like(inputs["input_ids"])
261
+ global_attention_mask[:, 0] = 1
262
+
263
+ summary_ids = model.generate(
264
+ **inputs,
265
+ global_attention_mask=global_attention_mask,
266
+ max_length=512,
267
+ num_beams=4,
268
+ )
269
+ summaries = tokenizer.batch_decode(summary_ids, skip_special_tokens=True)
270
+ ```
271
+
272
+ ---
273
+
274
+ ## Repository Structure
275
+
276
+ ```
277
+ .
278
+ β”œβ”€β”€ src/
279
+ β”‚ β”œβ”€β”€ config.py # Training configuration with large sequence settings
280
+ β”‚ β”œβ”€β”€ dataset.py # CNN/DailyMail dataset processing
281
+ β”‚ β”œβ”€β”€ metrics.py # ROUGE and BERTScore evaluation
282
+ β”‚ β”œβ”€β”€ model.py # LED model loading and setup
283
+ β”‚ β”œβ”€β”€ optimizer.py # STAM and STAMLite PyTorch optimizers
284
+ β”‚ └── trainer.py # Custom Seq2Seq trainer with STAM
285
+ β”œβ”€β”€ scripts/
286
+ β”‚ β”œβ”€β”€ train.py # Main multi-stage training script
287
+ β”‚ └── evaluate.py # Evaluation and inference script
288
+ β”œβ”€β”€ configs/
289
+ β”‚ β”œβ”€β”€ base_config.yaml # Base training configuration
290
+ β”‚ └── large_config.yaml # Large-scale training configuration
291
+ β”œβ”€β”€ tests/
292
+ β”‚ └── test_model.py # Unit tests for components
293
+ β”œβ”€β”€ media/
294
+ β”‚ └── stam.png # STAM optimizer diagram
295
+ β”œβ”€β”€ requirements.txt # Python dependencies
296
+ β”œβ”€β”€ setup.py # Package installation
297
+ └── README.md # This file
298
+ ```
299
+
300
+ ---
301
+
302
+ ## Installation
303
+
304
+ ```bash
305
+ git clone https://github.com/assemsabry/Research-News-AI-Summarizer
306
+ cd Research-News-AI-Summarizer
307
+ pip install -r requirements.txt
308
+ ```
309
+
310
+ ---
311
+
312
+ ## Training
313
+
314
+ Set your Hugging Face token as an environment variable:
315
+
316
+ ```bash
317
+ export HF_TOKEN="your_token_here"
318
+ ```
319
+
320
+ Run multi-stage training:
321
+
322
+ ```bash
323
+ python scripts/train.py --output-dir ./outputs --stages 1 2 3
324
+ ```
325
+
326
+ Run specific stages only:
327
+
328
+ ```bash
329
+ python scripts/train.py --stages 2 3
330
+ ```
331
+
332
+ Skip Hub upload (local training only):
333
+
334
+ ```bash
335
+ python scripts/train.py --skip-upload
336
+ ```
337
+
338
+ ### Training with Custom Config
339
+
340
+ ```python
341
+ from src.config import Config
342
+ from src.model import load_model_and_tokenizer, setup_system
343
+ from src.trainer import build_training_args, build_trainer
344
+
345
+ config = Config()
346
+ config.data.max_input_length = 16384
347
+ config.data.max_target_length = 768
348
+ config.training.gradient_accumulation_steps = 32
349
+
350
+ setup_system(config)
351
+ model, tokenizer = load_model_and_tokenizer(config)
352
+ ```
353
+
354
+ ---
355
+
356
+ ## Evaluation
357
+
358
+ Evaluate a trained checkpoint:
359
+
360
+ ```bash
361
+ python scripts/evaluate.py \
362
+ --model-path ./outputs/artifacts/stage_3_cnn_v3 \
363
+ --output-dir ./reports \
364
+ --num-samples 50
365
+ ```
366
+
367
+ The evaluation script produces:
368
+ - `human_evaluation.csv`: Generated summaries with per-sample ROUGE and BERTScore
369
+ - `error_analysis.csv`: Length-based error analysis with ratio statistics
370
+ - `evaluation_stats.json`: Aggregate metrics by length bucket
371
+
372
+ ---
373
+
374
+ ## Testing
375
+
376
+ Run unit tests:
377
+
378
+ ```bash
379
+ python -m unittest tests/test_model.py
380
+ ```
381
+
382
+ Tests cover:
383
+ - Text normalization and HTML stripping
384
+ - Dataset validation (min/max word counts)
385
+ - STAM and STAMLite optimizer initialization and step logic
386
+ - Configuration defaults and effective batch size computation
387
+
388
+ ---
389
+
390
+ ## Limitations and Biases
391
+
392
+ - The model is trained exclusively on English news articles (CNN/DailyMail). Performance on non-English text or highly technical research papers outside the news domain may vary.
393
+ - Summaries may inherit biases present in the original CNN/DailyMail dataset.
394
+ - The model does not fact-check generated content. Hallucinations can occur on out-of-domain inputs.
395
+ - Maximum input length is 8,192 tokens (base) or 16,384 tokens (large). Documents exceeding this length are truncated from the end.
396
+
397
+ ---
398
+
399
+ ## License
400
+
401
+ Apache 2.0
402
+
403
+ ---
404
+
405
+ ## Citation
406
+
407
+ If you use this model in your research, please cite:
408
+
409
+ ```bibtex
410
+ @misc{research-news-ai-summarizer,
411
+ title={Research & News AI Summarizer: Fine-tuned LED with STAM Optimizer},
412
+ author={Sabry, Assem},
413
+ year={2025},
414
+ howpublished={\url{https://huggingface.co/assemsabry/Research-News-AI-Summarizer}}
415
+ }
416
+ ```
417
+
418
+ ---
419
+
420
+ ## Acknowledgments
421
+
422
+ - Base model: [allenai/led-base-16384](https://huggingface.co/assemsabry/Research-News-AI-Summarizer/tree/main/datasets) by AllenAI
423
+ - Dataset: [CNN/DailyMail](https://huggingface.co/datasets/abisee/cnn_dailymail)
424
+ - Optimizer: [STAM](https://github.com/assemsabry/stam) by Assem Sabry