Update demo.py
Browse files
demo.py
CHANGED
|
@@ -1,12 +1,7 @@
|
|
| 1 |
-
|
| 2 |
#!/usr/bin/env python3
|
| 3 |
"""
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
The ultimate showcase script that flexes ALL of HyperMambaLM's superpowers!
|
| 7 |
-
Sit back, grab some popcorn, and watch this beast in action. 🍿
|
| 8 |
-
|
| 9 |
-
Warning: May cause excessive excitement about AI capabilities!
|
| 10 |
"""
|
| 11 |
|
| 12 |
import torch
|
|
@@ -17,12 +12,12 @@ import time
|
|
| 17 |
import json
|
| 18 |
|
| 19 |
def main():
|
| 20 |
-
print("
|
| 21 |
-
print("
|
| 22 |
-
print("
|
| 23 |
|
| 24 |
-
# 1.
|
| 25 |
-
print("\
|
| 26 |
config = HyperMambaConfig(
|
| 27 |
vocab_size=32000,
|
| 28 |
d_model=768,
|
|
@@ -37,44 +32,37 @@ def main():
|
|
| 37 |
neural_architecture_search=True
|
| 38 |
)
|
| 39 |
|
| 40 |
-
print(
|
| 41 |
-
print(f"
|
| 42 |
-
print(f"
|
| 43 |
-
print(f"
|
| 44 |
-
print(f" - Meta-learning: {config.meta_learning}")
|
| 45 |
-
print(f" - Few-shot adaptation: {config.few_shot_adaptation}")
|
| 46 |
|
| 47 |
-
# 2.
|
| 48 |
-
print("\
|
| 49 |
model = HyperMambaLM(config)
|
| 50 |
|
| 51 |
-
# 3. Model
|
| 52 |
-
print("\
|
| 53 |
-
|
| 54 |
-
print(
|
| 55 |
-
print(f"
|
| 56 |
-
print(f"
|
| 57 |
-
print(f"
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
# 4. Tạo tokenizer
|
| 63 |
-
print("\n🔤 STEP 4: Creating Advanced BPE Tokenizer...")
|
| 64 |
tokenizer = AdvancedBPETokenizer(config.vocab_size)
|
| 65 |
|
| 66 |
-
|
| 67 |
-
test_text = "Xin chào! Tôi là HyperMambaLM, một siêu model AI."
|
| 68 |
tokens = tokenizer.encode(test_text)
|
| 69 |
decoded = tokenizer.decode(tokens)
|
| 70 |
|
| 71 |
-
print(
|
| 72 |
-
print(f"
|
| 73 |
-
print(f"
|
| 74 |
-
print(f" - Decoded text: {decoded}")
|
| 75 |
|
| 76 |
-
# 5. Basic inference
|
| 77 |
-
print("\
|
| 78 |
batch_size, seq_len = 2, 128
|
| 79 |
input_ids = torch.randint(0, config.vocab_size, (batch_size, seq_len))
|
| 80 |
|
|
@@ -87,85 +75,51 @@ def main():
|
|
| 87 |
|
| 88 |
end_time = time.time()
|
| 89 |
|
| 90 |
-
print(
|
| 91 |
-
print(f"
|
| 92 |
-
print(f"
|
| 93 |
-
print(f"
|
| 94 |
-
print(f" - Throughput: {batch_size * seq_len / (end_time - start_time):.0f} tokens/sec")
|
| 95 |
|
| 96 |
# 6. Performance benchmark
|
| 97 |
-
print("\
|
| 98 |
profiler = ModelProfiler()
|
| 99 |
|
| 100 |
benchmark_results = profiler.benchmark_inference(model, input_ids, num_runs=10)
|
| 101 |
|
| 102 |
-
print(
|
| 103 |
-
print(f"
|
| 104 |
-
print(f"
|
| 105 |
-
print(f" - Batch size: {benchmark_results['batch_size']}")
|
| 106 |
-
print(f" - Sequence length: {benchmark_results['sequence_length']}")
|
| 107 |
|
| 108 |
-
# 7. Few-shot learning
|
| 109 |
-
print("\
|
| 110 |
|
| 111 |
-
# Tạo few-shot data
|
| 112 |
few_shot_loader = FewShotDataLoader(support_size=5, query_size=3)
|
| 113 |
|
| 114 |
-
# Sample texts cho few-shot learning
|
| 115 |
sample_texts = [
|
| 116 |
-
"
|
| 117 |
-
"
|
| 118 |
-
"HyperMambaLM
|
| 119 |
-
"
|
| 120 |
-
"Deep
|
| 121 |
-
"Query 1:
|
| 122 |
-
"Query 2:
|
| 123 |
-
"Query 3:
|
| 124 |
]
|
| 125 |
|
| 126 |
batch = few_shot_loader.create_few_shot_batch(sample_texts, tokenizer)
|
| 127 |
|
| 128 |
-
print(
|
| 129 |
-
print(f"
|
| 130 |
-
print(f"
|
| 131 |
-
print(f" - Support size: {batch['support_size']}")
|
| 132 |
-
print(f" - Query size: {batch['query_size']}")
|
| 133 |
-
|
| 134 |
-
# Test few-shot adaptation
|
| 135 |
-
support_examples = [
|
| 136 |
-
(torch.randint(0, config.vocab_size, (1, 20)),
|
| 137 |
-
torch.randint(0, config.vocab_size, (1, 20)))
|
| 138 |
-
for _ in range(5)
|
| 139 |
-
]
|
| 140 |
-
|
| 141 |
-
query = torch.randint(0, config.vocab_size, (1, 20))
|
| 142 |
-
|
| 143 |
-
print("\n🧠 Testing Meta-Learning Adaptation...")
|
| 144 |
-
start_time = time.time()
|
| 145 |
-
|
| 146 |
-
adapted_logits = model.few_shot_adapt(
|
| 147 |
-
support_examples=support_examples,
|
| 148 |
-
query=query,
|
| 149 |
-
adaptation_steps=3
|
| 150 |
-
)
|
| 151 |
-
|
| 152 |
-
end_time = time.time()
|
| 153 |
-
|
| 154 |
-
print(f"✅ Meta-learning adaptation completed!")
|
| 155 |
-
print(f" - Adaptation time: {(end_time - start_time)*1000:.2f}ms")
|
| 156 |
-
print(f" - Support examples: {len(support_examples)}")
|
| 157 |
-
print(f" - Adaptation steps: 3")
|
| 158 |
-
print(f" - Output shape: {adapted_logits.shape}")
|
| 159 |
|
| 160 |
-
# 8. Text generation
|
| 161 |
-
print("\
|
| 162 |
|
| 163 |
-
|
| 164 |
-
prompt_text = "Tôi là HyperMambaLM và tôi có thể"
|
| 165 |
prompt_tokens = tokenizer.encode(prompt_text)
|
| 166 |
prompt_tensor = torch.tensor([prompt_tokens])
|
| 167 |
|
| 168 |
-
print(f"
|
| 169 |
|
| 170 |
start_time = time.time()
|
| 171 |
|
|
@@ -181,110 +135,50 @@ def main():
|
|
| 181 |
|
| 182 |
generated_text = tokenizer.decode(generated[0].tolist())
|
| 183 |
|
| 184 |
-
print(
|
| 185 |
-
print(f"
|
| 186 |
-
print(f"
|
| 187 |
-
print(f" - Generated text: {generated_text}")
|
| 188 |
-
|
| 189 |
-
# 9. Continual learning demo
|
| 190 |
-
print("\n🔄 STEP 9: Continual Learning Demo...")
|
| 191 |
-
|
| 192 |
-
# Tạo new data cho continual learning
|
| 193 |
-
new_data = torch.randint(0, config.vocab_size, (5, 50))
|
| 194 |
-
|
| 195 |
-
print("🧠 Computing Fisher Information for EWC...")
|
| 196 |
-
start_time = time.time()
|
| 197 |
-
|
| 198 |
-
ewc_loss_fn = model.continual_learn(new_data)
|
| 199 |
-
|
| 200 |
-
end_time = time.time()
|
| 201 |
|
| 202 |
-
|
| 203 |
-
print(
|
| 204 |
-
print(f" - New data shape: {new_data.shape}")
|
| 205 |
-
print(f" - EWC loss function created!")
|
| 206 |
-
|
| 207 |
-
# 10. Memory usage analysis
|
| 208 |
-
print("\n💾 STEP 10: Memory Usage Analysis...")
|
| 209 |
-
|
| 210 |
-
if torch.cuda.is_available():
|
| 211 |
-
torch.cuda.empty_cache()
|
| 212 |
-
memory_allocated = torch.cuda.memory_allocated() / 1024**2
|
| 213 |
-
memory_reserved = torch.cuda.memory_reserved() / 1024**2
|
| 214 |
-
|
| 215 |
-
print(f"✅ GPU Memory Analysis:")
|
| 216 |
-
print(f" - Memory allocated: {memory_allocated:.1f} MB")
|
| 217 |
-
print(f" - Memory reserved: {memory_reserved:.1f} MB")
|
| 218 |
-
else:
|
| 219 |
-
print(f"✅ Running on CPU")
|
| 220 |
-
print(f" - Model size: {stats['model_size_mb']:.1f} MB")
|
| 221 |
-
|
| 222 |
-
# 11. Export model info
|
| 223 |
-
print("\n💾 STEP 11: Exporting Model Information...")
|
| 224 |
|
| 225 |
model_info = {
|
| 226 |
"model_name": "HyperMambaLM-300M",
|
| 227 |
"version": "1.0.0",
|
| 228 |
-
"architecture": "Hyper Mamba",
|
| 229 |
"parameters": stats['total_parameters'],
|
| 230 |
"model_size_mb": stats['model_size_mb'],
|
| 231 |
"features": stats['features'],
|
| 232 |
-
"config": {
|
| 233 |
-
"vocab_size": config.vocab_size,
|
| 234 |
-
"d_model": config.d_model,
|
| 235 |
-
"n_layer": config.n_layer,
|
| 236 |
-
"d_state": config.d_state,
|
| 237 |
-
"d_conv": config.d_conv,
|
| 238 |
-
"expand": config.expand,
|
| 239 |
-
"meta_learning": config.meta_learning,
|
| 240 |
-
"few_shot_adaptation": config.few_shot_adaptation,
|
| 241 |
-
"knowledge_distillation": config.knowledge_distillation,
|
| 242 |
-
"progressive_learning": config.progressive_learning,
|
| 243 |
-
"neural_architecture_search": config.neural_architecture_search
|
| 244 |
-
},
|
| 245 |
"benchmark": {
|
| 246 |
"inference_time_ms": benchmark_results['avg_time_ms'],
|
| 247 |
-
"throughput_tokens_per_sec": benchmark_results['throughput_tokens_per_sec']
|
| 248 |
-
"batch_size": benchmark_results['batch_size'],
|
| 249 |
-
"sequence_length": benchmark_results['sequence_length']
|
| 250 |
}
|
| 251 |
}
|
| 252 |
|
| 253 |
-
with open("hypermamba_info.json", "w"
|
| 254 |
-
json.dump(model_info, f, indent=2
|
| 255 |
-
|
| 256 |
-
print(
|
| 257 |
-
|
| 258 |
-
#
|
| 259 |
-
print("\n
|
| 260 |
-
print("
|
| 261 |
-
print("
|
| 262 |
-
|
| 263 |
-
print(f"\
|
| 264 |
-
print(f"
|
| 265 |
-
print(f"
|
| 266 |
-
print(f"
|
| 267 |
-
print(f"
|
| 268 |
-
print(f"
|
| 269 |
-
|
| 270 |
-
print(f"
|
| 271 |
-
print(f"
|
| 272 |
-
print(f"
|
| 273 |
-
|
| 274 |
-
print(f"
|
| 275 |
-
print(f"
|
| 276 |
-
print(f"
|
| 277 |
-
|
| 278 |
-
print(f"\n📞 Ready for Hugging Face upload! 🤗")
|
| 279 |
-
print(f"📁 Files created:")
|
| 280 |
-
print(f" - config.json")
|
| 281 |
-
print(f" - modeling_hypermamba.py")
|
| 282 |
-
print(f" - modeling_utils.py")
|
| 283 |
-
print(f" - __init__.py")
|
| 284 |
-
print(f" - README.md")
|
| 285 |
-
print(f" - demo.py")
|
| 286 |
-
print(f" - hypermamba_info.json")
|
| 287 |
|
| 288 |
|
| 289 |
if __name__ == "__main__":
|
| 290 |
-
main()
|
|
|
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
HyperMambaLM Demo Script
|
| 4 |
+
Simple demo for language model
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
"""
|
| 6 |
|
| 7 |
import torch
|
|
|
|
| 12 |
import json
|
| 13 |
|
| 14 |
def main():
|
| 15 |
+
print("=" * 50)
|
| 16 |
+
print(" HYPERMAMBALM-300M DEMO")
|
| 17 |
+
print("=" * 50)
|
| 18 |
|
| 19 |
+
# 1. Create model config
|
| 20 |
+
print("\n1. Creating model configuration...")
|
| 21 |
config = HyperMambaConfig(
|
| 22 |
vocab_size=32000,
|
| 23 |
d_model=768,
|
|
|
|
| 32 |
neural_architecture_search=True
|
| 33 |
)
|
| 34 |
|
| 35 |
+
print("Config created successfully!")
|
| 36 |
+
print(f" - Vocab size: {config.vocab_size:,}")
|
| 37 |
+
print(f" - Model dim: {config.d_model}")
|
| 38 |
+
print(f" - Layers: {config.n_layer}")
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
# 2. Initialize model
|
| 41 |
+
print("\n2. Initializing model...")
|
| 42 |
model = HyperMambaLM(config)
|
| 43 |
|
| 44 |
+
# 3. Model stats
|
| 45 |
+
print("\n3. Model statistics...")
|
| 46 |
+
stats = model.get_memory_usage()
|
| 47 |
+
print("Model created successfully!")
|
| 48 |
+
print(f" - Total params: {stats['total_parameters']:,}")
|
| 49 |
+
print(f" - Model size: {stats['model_size_mb']:.1f} MB")
|
| 50 |
+
print(f" - Features: {len(stats['features'])}")
|
| 51 |
+
|
| 52 |
+
# 4. Create tokenizer
|
| 53 |
+
print("\n4. Creating tokenizer...")
|
|
|
|
|
|
|
|
|
|
| 54 |
tokenizer = AdvancedBPETokenizer(config.vocab_size)
|
| 55 |
|
| 56 |
+
test_text = "Hello! I am HyperMambaLM AI model."
|
|
|
|
| 57 |
tokens = tokenizer.encode(test_text)
|
| 58 |
decoded = tokenizer.decode(tokens)
|
| 59 |
|
| 60 |
+
print("Tokenizer created!")
|
| 61 |
+
print(f" - Test text: {test_text}")
|
| 62 |
+
print(f" - Tokens: {tokens[:10]}...")
|
|
|
|
| 63 |
|
| 64 |
+
# 5. Basic inference
|
| 65 |
+
print("\n5. Basic inference test...")
|
| 66 |
batch_size, seq_len = 2, 128
|
| 67 |
input_ids = torch.randint(0, config.vocab_size, (batch_size, seq_len))
|
| 68 |
|
|
|
|
| 75 |
|
| 76 |
end_time = time.time()
|
| 77 |
|
| 78 |
+
print("Inference completed!")
|
| 79 |
+
print(f" - Input shape: {input_ids.shape}")
|
| 80 |
+
print(f" - Output shape: {logits.shape}")
|
| 81 |
+
print(f" - Time: {(end_time - start_time)*1000:.2f}ms")
|
|
|
|
| 82 |
|
| 83 |
# 6. Performance benchmark
|
| 84 |
+
print("\n6. Performance benchmark...")
|
| 85 |
profiler = ModelProfiler()
|
| 86 |
|
| 87 |
benchmark_results = profiler.benchmark_inference(model, input_ids, num_runs=10)
|
| 88 |
|
| 89 |
+
print("Benchmark completed!")
|
| 90 |
+
print(f" - Avg time: {benchmark_results['avg_time_ms']:.2f}ms")
|
| 91 |
+
print(f" - Throughput: {benchmark_results['throughput_tokens_per_sec']:.0f} tokens/sec")
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
# 7. Few-shot learning
|
| 94 |
+
print("\n7. Few-shot learning demo...")
|
| 95 |
|
|
|
|
| 96 |
few_shot_loader = FewShotDataLoader(support_size=5, query_size=3)
|
| 97 |
|
|
|
|
| 98 |
sample_texts = [
|
| 99 |
+
"Today is beautiful!",
|
| 100 |
+
"I like machine learning.",
|
| 101 |
+
"HyperMambaLM is great.",
|
| 102 |
+
"AI is interesting.",
|
| 103 |
+
"Deep learning is growing.",
|
| 104 |
+
"Query 1: Today I want",
|
| 105 |
+
"Query 2: ML helps",
|
| 106 |
+
"Query 3: Future of AI"
|
| 107 |
]
|
| 108 |
|
| 109 |
batch = few_shot_loader.create_few_shot_batch(sample_texts, tokenizer)
|
| 110 |
|
| 111 |
+
print("Few-shot batch created!")
|
| 112 |
+
print(f" - Support shape: {batch['support_set'].shape}")
|
| 113 |
+
print(f" - Query shape: {batch['query_set'].shape}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
+
# 8. Text generation
|
| 116 |
+
print("\n8. Text generation demo...")
|
| 117 |
|
| 118 |
+
prompt_text = "I am HyperMambaLM and I can"
|
|
|
|
| 119 |
prompt_tokens = tokenizer.encode(prompt_text)
|
| 120 |
prompt_tensor = torch.tensor([prompt_tokens])
|
| 121 |
|
| 122 |
+
print(f"Generating from: '{prompt_text}'")
|
| 123 |
|
| 124 |
start_time = time.time()
|
| 125 |
|
|
|
|
| 135 |
|
| 136 |
generated_text = tokenizer.decode(generated[0].tolist())
|
| 137 |
|
| 138 |
+
print("Text generation completed!")
|
| 139 |
+
print(f" - Time: {(end_time - start_time)*1000:.2f}ms")
|
| 140 |
+
print(f" - Generated: {generated_text}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
+
# 9. Export model info
|
| 143 |
+
print("\n9. Exporting model info...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
|
| 145 |
model_info = {
|
| 146 |
"model_name": "HyperMambaLM-300M",
|
| 147 |
"version": "1.0.0",
|
|
|
|
| 148 |
"parameters": stats['total_parameters'],
|
| 149 |
"model_size_mb": stats['model_size_mb'],
|
| 150 |
"features": stats['features'],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
"benchmark": {
|
| 152 |
"inference_time_ms": benchmark_results['avg_time_ms'],
|
| 153 |
+
"throughput_tokens_per_sec": benchmark_results['throughput_tokens_per_sec']
|
|
|
|
|
|
|
| 154 |
}
|
| 155 |
}
|
| 156 |
|
| 157 |
+
with open("hypermamba_info.json", "w") as f:
|
| 158 |
+
json.dump(model_info, f, indent=2)
|
| 159 |
+
|
| 160 |
+
print("Model info exported to 'hypermamba_info.json'")
|
| 161 |
+
|
| 162 |
+
# 10. Summary
|
| 163 |
+
print("\n" + "=" * 50)
|
| 164 |
+
print(" DEMO COMPLETED SUCCESSFULLY!")
|
| 165 |
+
print("=" * 50)
|
| 166 |
+
|
| 167 |
+
print(f"\nSummary:")
|
| 168 |
+
print(f" Model: HyperMambaLM-300M")
|
| 169 |
+
print(f" Parameters: {stats['total_parameters']:,}")
|
| 170 |
+
print(f" Size: {stats['model_size_mb']:.1f} MB")
|
| 171 |
+
print(f" Speed: {benchmark_results['throughput_tokens_per_sec']:.0f} tokens/sec")
|
| 172 |
+
print(f" Features: {len(stats['features'])} capabilities")
|
| 173 |
+
|
| 174 |
+
print(f"\nFiles created:")
|
| 175 |
+
print(f" - config.json")
|
| 176 |
+
print(f" - modeling_hypermamba.py")
|
| 177 |
+
print(f" - modeling_utils.py")
|
| 178 |
+
print(f" - __init__.py")
|
| 179 |
+
print(f" - demo.py")
|
| 180 |
+
print(f" - hypermamba_info.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
|
| 183 |
if __name__ == "__main__":
|
| 184 |
+
main()
|