Upload mobile/export_mobile.py with huggingface_hub
Browse files- mobile/export_mobile.py +71 -0
mobile/export_mobile.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Export AuraMind models for mobile deployment
|
| 4 |
+
Creates optimized .ptl files for PyTorch Mobile
|
| 5 |
+
"""
|
| 6 |
+
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
def export_for_mobile(model_name: str, variant: str):
|
| 12 |
+
"""Export model for mobile deployment"""
|
| 13 |
+
|
| 14 |
+
print(f"Exporting {model_name} ({variant}) for mobile...")
|
| 15 |
+
|
| 16 |
+
# Load model
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
+
model = AutoModelForCausalLM.from_pretrained(
|
| 19 |
+
model_name,
|
| 20 |
+
torch_dtype=torch.float16,
|
| 21 |
+
device_map="cpu",
|
| 22 |
+
low_cpu_mem_usage=True
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Prepare for mobile export
|
| 26 |
+
model.eval()
|
| 27 |
+
|
| 28 |
+
# Create example input
|
| 29 |
+
example_text = "[Assistant Mode] Help me with my tasks"
|
| 30 |
+
example_input = tokenizer(
|
| 31 |
+
example_text,
|
| 32 |
+
return_tensors="pt",
|
| 33 |
+
max_length=512,
|
| 34 |
+
truncation=True
|
| 35 |
+
)["input_ids"]
|
| 36 |
+
|
| 37 |
+
# Trace the model
|
| 38 |
+
traced_model = torch.jit.trace(model, example_input)
|
| 39 |
+
|
| 40 |
+
# Optimize for mobile
|
| 41 |
+
optimized_model = torch.jit.optimize_for_mobile(traced_model)
|
| 42 |
+
|
| 43 |
+
# Save mobile-optimized model
|
| 44 |
+
output_path = f"auramind_{variant}_mobile.ptl"
|
| 45 |
+
optimized_model._save_for_lite_interpreter(output_path)
|
| 46 |
+
|
| 47 |
+
print(f"✅ Mobile model saved: {output_path}")
|
| 48 |
+
|
| 49 |
+
# Create metadata file
|
| 50 |
+
metadata = {
|
| 51 |
+
"model_name": model_name,
|
| 52 |
+
"variant": variant,
|
| 53 |
+
"tokenizer_vocab_size": tokenizer.vocab_size,
|
| 54 |
+
"max_length": 512,
|
| 55 |
+
"export_date": torch.jit.get_jit_operator_version(),
|
| 56 |
+
"pytorch_version": torch.__version__
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
import json
|
| 60 |
+
with open(f"auramind_{variant}_metadata.json", "w") as f:
|
| 61 |
+
json.dump(metadata, f, indent=2)
|
| 62 |
+
|
| 63 |
+
return output_path
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
variants = ["270m", "180m", "90m"]
|
| 67 |
+
|
| 68 |
+
for variant in variants:
|
| 69 |
+
export_for_mobile("zail-ai/Auramind", variant)
|
| 70 |
+
|
| 71 |
+
print("\n✅ All mobile exports completed!")
|