Spaces:
Running
Running
Create change_the_model
#9
by
Kiruthick18
- opened
- change_the_model +137 -0
change_the_model
ADDED
@@ -0,0 +1,137 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# π Speed Optimized Summarization with DistilBART
|
2 |
+
|
3 |
+
The BART model is quite large (~1.6GB) and slow. I optimized it with a much faster, lighter model and better performance settings.
|
4 |
+
|
5 |
+
---
|
6 |
+
|
7 |
+
## π Major Speed Optimizations Applied
|
8 |
+
|
9 |
+
### 1. Faster Model
|
10 |
+
- **Switched from** `facebook/bart-large-cnn` (**~1.6GB**)
|
11 |
+
- **To** `sshleifer/distilbart-cnn-12-6` (**~400MB**)
|
12 |
+
- π₯ **6x smaller model size** = Much faster loading and inference
|
13 |
+
|
14 |
+
### 2. Processing Optimizations
|
15 |
+
- **Smaller chunks:** 512 words vs 900 (faster processing)
|
16 |
+
- **Limited chunks:** Max 5 chunks processed (prevents hanging on huge docs)
|
17 |
+
- **Faster tokenization:** Word count instead of full tokenization for chunking
|
18 |
+
- **Reduced beam search:** 2 beams instead of 4 (2x faster)
|
19 |
+
|
20 |
+
### 3. Smart Summarization
|
21 |
+
- **Shorter summaries:** Reduced max lengths across all modes
|
22 |
+
- **Skip final summary:** For documents with β€2 chunks (saves time)
|
23 |
+
- **Early stopping:** Enabled for faster convergence
|
24 |
+
- **Progress tracking:** Shows which chunk is being processed
|
25 |
+
|
26 |
+
### 4. Memory & Performance
|
27 |
+
- **Float16 precision:** Used when GPU is available (faster inference)
|
28 |
+
- **Optimized pipeline:** Better model loading with fallback
|
29 |
+
- **`optimum` library added:** For additional speed improvements
|
30 |
+
|
31 |
+
---
|
32 |
+
|
33 |
+
## β‘ Expected Speed Improvements
|
34 |
+
|
35 |
+
| Task | Before | After |
|
36 |
+
|-------------------|----------------------|------------------------------|
|
37 |
+
| Model loading | ~30+ seconds | ~10 seconds |
|
38 |
+
| PDF processing | Minutes | ~5β15 seconds |
|
39 |
+
| Memory usage | ~1.6GB | ~400MB |
|
40 |
+
| Overall speed | Slow | π 5β10x faster |
|
41 |
+
|
42 |
+
---
|
43 |
+
|
44 |
+
## 𧬠What is DistilBART?
|
45 |
+
|
46 |
+
**DistilBART** is a **compressed version of the BART model** designed to be **lighter and faster** while retaining most of BARTβs performance. Itβs the result of **model distillation**, where a smaller model (the *student*) learns from a larger one (the *teacher*), in this case, `facebook/bart-large`.
|
47 |
+
|
48 |
+
| Attribute | Description |
|
49 |
+
|------------------|---------------------------------------------------------------------|
|
50 |
+
| **Full Name** | Distilled BART |
|
51 |
+
| **Base Model** | `facebook/bart-large` |
|
52 |
+
| **Distilled By** | Hugging Face π€ |
|
53 |
+
| **Purpose** | Faster inference and smaller footprint for tasks like summarization |
|
54 |
+
| **Architecture** | Encoder-decoder Transformer, like BART, but with fewer layers |
|
55 |
+
|
56 |
+
---
|
57 |
+
|
58 |
+
## βοΈ Key Differences: BART vs DistilBART
|
59 |
+
|
60 |
+
| Feature | BART (Large) | DistilBART |
|
61 |
+
|----------------|--------------|------------------------|
|
62 |
+
| Encoder Layers | 12 | 6 |
|
63 |
+
| Decoder Layers | 12 | 6 |
|
64 |
+
| Parameters | ~406M | ~222M |
|
65 |
+
| Model Size | ~1.6GB | ~400MB (~55% smaller) |
|
66 |
+
| Speed | Slower | ~2x faster |
|
67 |
+
| Performance | Very high | Slight drop (~1β2%) |
|
68 |
+
|
69 |
+
---
|
70 |
+
|
71 |
+
## π― Use Cases
|
72 |
+
|
73 |
+
- β
**Text Summarization** (primary use case)
|
74 |
+
- π **Translation** (basic use)
|
75 |
+
- β‘ Ideal for **edge devices** or **real-time systems** where speed & size matter
|
76 |
+
|
77 |
+
---
|
78 |
+
|
79 |
+
## π§ͺ Example: Summarization with DistilBART
|
80 |
+
|
81 |
+
You can easily use DistilBART with Hugging Face Transformers:
|
82 |
+
|
83 |
+
```python
|
84 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
85 |
+
|
86 |
+
# Load pretrained DistilBART model
|
87 |
+
tokenizer = AutoTokenizer.from_pretrained("sshleifer/distilbart-cnn-12-6")
|
88 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("sshleifer/distilbart-cnn-12-6")
|
89 |
+
|
90 |
+
# Input text
|
91 |
+
ARTICLE = "The Indian Space Research Organisation (ISRO) launched a new satellite today from the Satish Dhawan Space Centre..."
|
92 |
+
|
93 |
+
# Tokenize and summarize
|
94 |
+
inputs = tokenizer([ARTICLE], max_length=1024, return_tensors="pt", truncation=True)
|
95 |
+
summary_ids = model.generate(
|
96 |
+
inputs["input_ids"],
|
97 |
+
max_length=150,
|
98 |
+
min_length=40,
|
99 |
+
length_penalty=2.0,
|
100 |
+
num_beams=4,
|
101 |
+
early_stopping=True
|
102 |
+
)
|
103 |
+
|
104 |
+
print(tokenizer.decode(summary_ids[0], skip_special_tokens=True))
|
105 |
+
````
|
106 |
+
|
107 |
+
---
|
108 |
+
|
109 |
+
## π¦ Available Variants
|
110 |
+
|
111 |
+
| Model Name | Task | Description |
|
112 |
+
| --------------------------------- | ---------------------------- | ---------------------------------------- |
|
113 |
+
| `sshleifer/distilbart-cnn-12-6` | Summarization | Distilled from `facebook/bart-large-cnn` |
|
114 |
+
| `philschmid/distilbart-xsum-12-6` | Summarization (XSUM dataset) | Short, abstractive summaries |
|
115 |
+
|
116 |
+
π [Find more on Hugging Face Model Hub](https://huggingface.co/models?search=distilbart)
|
117 |
+
|
118 |
+
---
|
119 |
+
|
120 |
+
## π Summary
|
121 |
+
|
122 |
+
* π§ **DistilBART** is a distilled, faster version of **BART**
|
123 |
+
* π§© Ideal for summarization tasks with lower memory and latency requirements
|
124 |
+
* π‘ Trained using **knowledge distillation** from `facebook/bart-large`
|
125 |
+
* βοΈ Works well in apps needing faster performance without significant loss in quality
|
126 |
+
|
127 |
+
---
|
128 |
+
|
129 |
+
β
**Try it now β it should be significantly faster!** πββοΈπ¨
|
130 |
+
|
131 |
+
```
|
132 |
+
|
133 |
+
|
134 |
+
|
135 |
+
|
136 |
+
Thank You
|
137 |
+
```
|