-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexamples.py
More file actions
303 lines (241 loc) · 10.2 KB
/
examples.py
File metadata and controls
303 lines (241 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
Example Usage of Mobile Translation Model
Demonstrates tokenizer training, model creation, and inference
"""
import torch
from pathlib import Path
from lingolite.translation_tokenizer import TranslationTokenizer
from lingolite.mobile_translation_model import create_model
from lingolite.training import TranslationDataset, TranslationTrainer, collate_fn
from torch.utils.data import DataLoader
def example_tokenizer_training():
"""Example: Train a tokenizer on multilingual corpus."""
print("=" * 80)
print("EXAMPLE 1: TOKENIZER TRAINING")
print("=" * 80)
# Create tokenizer
tokenizer = TranslationTokenizer(
languages=['en', 'es', 'fr', 'de', 'it', 'da'],
vocab_size=24000,
model_prefix='translation_tokenizer'
)
# In practice, you would train on real corpus files
# tokenizer.train([
# 'data/corpus_en.txt',
# 'data/corpus_es.txt',
# 'data/corpus_fr.txt',
# 'data/corpus_de.txt',
# 'data/corpus_it.txt',
# ])
print("✓ Tokenizer created")
print(f" Languages: {tokenizer.languages}")
print(f" Target vocab size: {tokenizer.vocab_size}")
print(f" Special tokens: {len(tokenizer.special_tokens)}")
# Example encoding/decoding (with trained model)
# text = "Hello, how are you?"
# tokens = tokenizer.encode(text, src_lang='en', tgt_lang='es')
# decoded = tokenizer.decode(tokens)
print("\nNote: To use tokenizer, first train it on your corpus:")
print(" tokenizer.train(['corpus1.txt', 'corpus2.txt', ...])")
def example_model_creation():
"""Example: Create models of different sizes."""
print("\n" + "=" * 80)
print("EXAMPLE 2: MODEL CREATION")
print("=" * 80)
vocab_size = 24000
print("\nAvailable model sizes:")
print("-" * 80)
for size in ['tiny', 'small', 'medium']:
model = create_model(vocab_size=vocab_size, model_size=size)
params = model.count_parameters()
# Calculate sizes
fp32_mb = params['total'] * 4 / (1024**2)
int8_mb = params['total'] * 1 / (1024**2)
print(f"\n{size.upper()}:")
print(f" Parameters: {params['total']:,} ({params['total']/1e6:.1f}M)")
print(f" FP32 size: {fp32_mb:.1f} MB")
print(f" INT8 size: {int8_mb:.1f} MB")
print(f" Encoder layers: {model.encoder.layers.__len__()}")
print(f" Decoder layers: {model.decoder.layers.__len__()}")
def example_inference():
"""Example: Use model for translation inference."""
print("\n" + "=" * 80)
print("EXAMPLE 3: TRANSLATION INFERENCE")
print("=" * 80)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print(f"\nDevice: {device}")
# Create model
vocab_size = 24000
model = create_model(vocab_size=vocab_size, model_size='small')
model = model.to(device)
model.eval()
print("✓ Model created and loaded")
# In practice, you would:
# 1. Load trained tokenizer
# tokenizer = TranslationTokenizer.from_pretrained('./tokenizer')
# 2. Tokenize input
# text = "Hello, how are you?"
# encoded = tokenizer.batch_encode([text], src_lang='en', tgt_lang='es')
# src_ids = encoded['input_ids'].to(device)
# src_mask = encoded['attention_mask'].to(device)
# 3. Generate translation
# generated = model.generate(
# src_input_ids=src_ids,
# src_attention_mask=src_mask,
# max_length=128,
# sos_token_id=tokenizer.sos_token_id,
# eos_token_id=tokenizer.eos_token_id,
# )
# 4. Decode output
# translation = tokenizer.decode(generated[0].tolist(), skip_special_tokens=True)
# print(f"Translation: {translation}")
# Simulated inference
batch = 2
src_len = 32
src_ids = torch.randint(0, vocab_size, (batch, src_len)).to(device)
src_mask = torch.ones(batch, src_len).to(device)
with torch.no_grad():
generated = model.generate(
src_input_ids=src_ids,
src_attention_mask=src_mask,
max_length=50,
sos_token_id=1,
eos_token_id=2,
)
print(f"✓ Generation successful")
print(f" Input shape: {src_ids.shape}")
print(f" Generated shape: {generated.shape}")
def example_training_setup():
"""Example: Set up training pipeline."""
print("\n" + "=" * 80)
print("EXAMPLE 4: TRAINING SETUP")
print("=" * 80)
# This is a conceptual example showing the training pipeline
print("\nTraining Pipeline:")
print("-" * 80)
print("\n1. Prepare Data:")
print(" # Load parallel corpus")
print(" data = [")
print(" {'src_text': 'Hello', 'tgt_text': 'Hola', 'src_lang': 'en', 'tgt_lang': 'es'},")
print(" # ... more examples")
print(" ]")
print("\n2. Create Dataset:")
print(" tokenizer = TranslationTokenizer.from_pretrained('./tokenizer')")
print(" dataset = TranslationDataset(data, tokenizer, max_length=128)")
print(" train_loader = DataLoader(dataset, batch_size=32, collate_fn=collate_fn)")
print("\n3. Create Model:")
print(" model = create_model(vocab_size=24000, model_size='small')")
print("\n4. Create Trainer:")
print(" trainer = TranslationTrainer(")
print(" model=model,")
print(" train_loader=train_loader,")
print(" val_loader=val_loader,")
print(" learning_rate=3e-4,")
print(" device='cuda'")
print(" )")
print("\n5. Train:")
print(" trainer.train(num_epochs=10)")
print("\n6. Save Model:")
print(" torch.save(model.state_dict(), 'translation_model.pt')")
def example_quantization():
"""Example: Quantize model for mobile deployment."""
print("\n" + "=" * 80)
print("EXAMPLE 5: MODEL QUANTIZATION")
print("=" * 80)
device = torch.device('cpu') # Quantization typically done on CPU
# Create model
vocab_size = 24000
model = create_model(vocab_size=vocab_size, model_size='small')
model = model.to(device)
print("✓ Original model created")
params = model.count_parameters()
fp32_size = params['total'] * 4 / (1024**2)
print(f" FP32 size: {fp32_size:.1f} MB")
# Quantization (conceptual - requires additional setup)
print("\nQuantization Steps:")
print("-" * 80)
print("\n1. Dynamic Quantization (simplest):")
print(" quantized_model = torch.quantization.quantize_dynamic(")
print(" model,")
print(" {torch.nn.Linear},")
print(" dtype=torch.qint8")
print(" )")
print(" # ~4x size reduction, ~2-3x speedup")
print("\n2. Quantization-Aware Training (best quality):")
print(" model.qconfig = torch.quantization.get_default_qat_qconfig('fbgemm')")
print(" model_prepared = torch.quantization.prepare_qat(model)")
print(" # Train for a few epochs")
print(" model_quantized = torch.quantization.convert(model_prepared)")
print("\n3. Export for Mobile:")
print(" # TorchScript")
print(" traced = torch.jit.trace(model_quantized, example_inputs)")
print(" traced.save('model_mobile.pt')")
print("")
print(" # ONNX")
print(" torch.onnx.export(model, example_inputs, 'model.onnx')")
print("")
print(" # TensorFlow Lite")
print(" # Convert ONNX -> TF -> TFLite")
int8_size = params['total'] * 1 / (1024**2)
print(f"\n✓ Expected INT8 size: {int8_size:.1f} MB (~4x reduction)")
def example_complete_workflow():
"""Example: Complete workflow from data to deployment."""
print("\n" + "=" * 80)
print("EXAMPLE 6: COMPLETE WORKFLOW")
print("=" * 80)
print("\n📋 STEP-BY-STEP GUIDE")
print("=" * 80)
print("\n1ï¸âƒ£ DATA PREPARATION")
print("-" * 80)
print(" • Collect parallel corpus (e.g., from OPUS, CCMatrix)")
print(" • Format: List of {'src_text', 'tgt_text', 'src_lang', 'tgt_lang'}")
print(" • Recommended: 1M+ sentence pairs per language pair")
print("\n2ï¸âƒ£ TOKENIZER TRAINING")
print("-" * 80)
print(" tokenizer = TranslationTokenizer(languages=['en', 'es', 'fr'])")
print(" tokenizer.train(['corpus_en.txt', 'corpus_es.txt', 'corpus_fr.txt'])")
print(" tokenizer.save('./tokenizer')")
print("\n3ï¸âƒ£ MODEL TRAINING")
print("-" * 80)
print(" model = create_model(vocab_size=24000, model_size='small')")
print(" trainer = TranslationTrainer(model, train_loader, val_loader)")
print(" trainer.train(num_epochs=10)")
print(" # Takes: 1-2 weeks on single GPU for 'small' model")
print("\n4ï¸âƒ£ EVALUATION")
print("-" * 80)
print(" # Compute BLEU score on test set")
print(" from sacrebleu import corpus_bleu")
print(" translations = [model.translate(src) for src in test_sources]")
print(" bleu = corpus_bleu(translations, [test_references])")
print("\n5ï¸âƒ£ QUANTIZATION")
print("-" * 80)
print(" quantized_model = quantize_model(model)")
print(" # Size: ~60MB INT8 vs ~240MB FP32")
print("\n6ï¸âƒ£ EXPORT FOR MOBILE")
print("-" * 80)
print(" # iOS/Android")
print(" traced = torch.jit.trace(quantized_model, example_input)")
print(" traced.save('translation_model_mobile.pt')")
print("\n7ï¸âƒ£ MOBILE APP INTEGRATION")
print("-" * 80)
print(" // iOS (Swift)")
print(" let model = try! TorchModule(fileAtPath: modelPath)")
print(" let output = model.predict(input)")
print("")
print(" // Android (Kotlin)")
print(" val module = LiteModuleLoader.load(modelPath)")
print(" val output = module.forward(IValue.from(input))")
print("\n✓ End-to-End Pipeline Complete!")
if __name__ == "__main__":
# Run all examples
example_tokenizer_training()
example_model_creation()
example_inference()
example_training_setup()
example_quantization()
example_complete_workflow()
print("\n" + "=" * 80)
print("📚 For more details, see README.md")
print("=" * 80)
#!/usr/bin/env python3
# mypy: ignore-errors