forked from thebaselab/codeapp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconvert_deepseek_to_coreml.py
More file actions
283 lines (228 loc) · 8.93 KB
/
Copy pathconvert_deepseek_to_coreml.py
File metadata and controls
283 lines (228 loc) · 8.93 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
#!/usr/bin/env python3
"""
DeepSeek R1 Distill Llama 8B to Core ML Converter
This script converts the DeepSeek-R1-Distill-Llama-8B model to Core ML format
for use on iOS devices.
Requirements:
pip install coremltools torch transformers huggingface_hub accelerate
Usage:
python convert_deepseek_to_coreml.py [--quantize] [--output-dir ./models]
"""
import argparse
import os
import sys
from pathlib import Path
try:
import torch
import coremltools as ct
from transformers import AutoModelForCausalLM, AutoTokenizer
from huggingface_hub import snapshot_download
except ImportError as e:
print(f"Error: Missing required package: {e}")
print("\nPlease install required packages:")
print("pip install coremltools torch transformers huggingface_hub accelerate")
sys.exit(1)
# Model configuration
MODEL_ID = "deepseek-ai/DeepSeek-R1-Distill-Llama-8B"
MAX_LENGTH = 2048 # Maximum sequence length
BATCH_SIZE = 1
class DeepSeekCoreMLConverter:
"""Converter for DeepSeek models to Core ML format."""
def __init__(self, model_id: str, output_dir: str, quantize: bool = True):
self.model_id = model_id
self.output_dir = Path(output_dir)
self.output_dir.mkdir(parents=True, exist_ok=True)
self.quantize = quantize
def download_model(self):
"""Download the model from Hugging Face."""
print(f"📥 Downloading {self.model_id}...")
print("This may take a while (model is ~15GB)...")
try:
model_path = snapshot_download(
repo_id=self.model_id,
allow_patterns=["*.json", "*.bin", "*.model", "*.safetensors"],
cache_dir=self.output_dir / "cache"
)
print(f"✅ Model downloaded to: {model_path}")
return model_path
except Exception as e:
print(f"❌ Error downloading model: {e}")
raise
def load_pytorch_model(self, model_path: str):
"""Load the PyTorch model."""
print("\n🔧 Loading PyTorch model...")
try:
# Load tokenizer
tokenizer = AutoTokenizer.from_pretrained(
model_path,
trust_remote_code=True
)
# Load model with 4-bit quantization for memory efficiency
model = AutoModelForCausalLM.from_pretrained(
model_path,
torch_dtype=torch.float16,
device_map="auto",
low_cpu_mem_usage=True,
trust_remote_code=True
)
model.eval()
print("✅ Model loaded successfully")
return model, tokenizer
except Exception as e:
print(f"❌ Error loading model: {e}")
raise
def trace_model(self, model, tokenizer):
"""Trace the model for Core ML conversion."""
print("\n📝 Tracing model...")
try:
# Create example input
example_text = "Hello, how can I help you?"
inputs = tokenizer(
example_text,
return_tensors="pt",
max_length=MAX_LENGTH,
padding="max_length",
truncation=True
)
# Get input IDs
input_ids = inputs["input_ids"]
# Trace the model
with torch.no_grad():
traced_model = torch.jit.trace(
model,
(input_ids,),
strict=False
)
print("✅ Model traced successfully")
return traced_model, input_ids
except Exception as e:
print(f"❌ Error tracing model: {e}")
print("\nNote: Some models may not support tracing.")
print("Try using torch.jit.script instead or use the alternative method.")
raise
def convert_to_coreml(self, traced_model, example_input):
"""Convert traced model to Core ML."""
print("\n🔄 Converting to Core ML...")
print("This may take 10-30 minutes...")
try:
# Define input type
input_shape = ct.Shape(shape=(BATCH_SIZE, MAX_LENGTH))
# Convert to Core ML
coreml_model = ct.convert(
traced_model,
inputs=[ct.TensorType(name="input_ids", shape=input_shape, dtype=int)],
minimum_deployment_target=ct.target.iOS17,
compute_units=ct.ComputeUnit.ALL, # Use CPU, GPU, and ANE
)
# Add metadata
coreml_model.user_defined_metadata["model_name"] = "DeepSeek-R1-Distill-Llama-8B"
coreml_model.user_defined_metadata["model_type"] = "language_model"
coreml_model.user_defined_metadata["max_length"] = str(MAX_LENGTH)
print("✅ Conversion successful")
return coreml_model
except Exception as e:
print(f"❌ Error converting to Core ML: {e}")
raise
def quantize_model(self, coreml_model):
"""Apply 4-bit quantization to reduce model size."""
if not self.quantize:
return coreml_model
print("\n⚡ Applying 4-bit quantization...")
try:
# Apply weight quantization
quantized_model = ct.compression.compress_weights(
coreml_model,
mode=ct.compression.CompressionMode.INT4,
)
print("✅ Quantization complete")
return quantized_model
except Exception as e:
print(f"⚠️ Warning: Quantization failed: {e}")
print("Continuing with unquantized model...")
return coreml_model
def save_model(self, coreml_model, filename="DeepSeekR1.mlpackage"):
"""Save the Core ML model."""
output_path = self.output_dir / filename
print(f"\n💾 Saving model to {output_path}...")
try:
coreml_model.save(str(output_path))
print(f"✅ Model saved successfully")
print(f"\n📊 Model size: {self._get_dir_size(output_path):.2f} MB")
return output_path
except Exception as e:
print(f"❌ Error saving model: {e}")
raise
def _get_dir_size(self, path):
"""Calculate directory size in MB."""
total = 0
for entry in Path(path).rglob('*'):
if entry.is_file():
total += entry.stat().st_size
return total / (1024 * 1024)
def run_conversion(self):
"""Run the complete conversion pipeline."""
print("=" * 60)
print("DeepSeek R1 to Core ML Converter")
print("=" * 60)
try:
# Step 1: Download model
model_path = self.download_model()
# Step 2: Load PyTorch model
model, tokenizer = self.load_pytorch_model(model_path)
# Step 3: Trace model
traced_model, example_input = self.trace_model(model, tokenizer)
# Step 4: Convert to Core ML
coreml_model = self.convert_to_coreml(traced_model, example_input)
# Step 5: Quantize (optional)
coreml_model = self.quantize_model(coreml_model)
# Step 6: Save model
output_path = self.save_model(coreml_model)
print("\n" + "=" * 60)
print("✨ CONVERSION COMPLETE!")
print("=" * 60)
print(f"\nYour Core ML model is ready at:")
print(f" {output_path}")
print("\nNext steps:")
print("1. Transfer the .mlpackage to your iOS device")
print("2. In CodeApp: Settings > AI Model > Load Local Model")
print("3. Select the .mlpackage file")
print("4. Start chatting with AI!")
return output_path
except Exception as e:
print(f"\n❌ Conversion failed: {e}")
print("\nTroubleshooting:")
print("1. Ensure you have enough RAM (16GB+ recommended)")
print("2. Check you have enough disk space (~20GB)")
print("3. Try running with --no-quantize flag")
print("4. Check the error message above for details")
return None
def main():
parser = argparse.ArgumentParser(
description="Convert DeepSeek R1 Distill Llama 8B to Core ML"
)
parser.add_argument(
"--output-dir",
default="./coreml_models",
help="Output directory for the converted model"
)
parser.add_argument(
"--no-quantize",
action="store_true",
help="Disable 4-bit quantization (larger but potentially more accurate)"
)
parser.add_argument(
"--model-id",
default=MODEL_ID,
help="Hugging Face model ID to convert"
)
args = parser.parse_args()
# Create converter
converter = DeepSeekCoreMLConverter(
model_id=args.model_id,
output_dir=args.output_dir,
quantize=not args.no_quantize
)
# Run conversion
converter.run_conversion()
if __name__ == "__main__":
main()