Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
Code for creating the tiny model:
|
| 2 |
+
|
| 3 |
+
```python
|
| 4 |
+
"""
|
| 5 |
+
Create tiny random phi-4-mini-instruct model which preserves longrope
|
| 6 |
+
"""
|
| 7 |
+
|
| 8 |
+
import os
|
| 9 |
+
import torch
|
| 10 |
+
|
| 11 |
+
torch.set_default_dtype(torch.float32)
|
| 12 |
+
|
| 13 |
+
from transformers import AutoTokenizer, AutoConfig, Phi3ForCausalLM, set_seed
|
| 14 |
+
from optimum.intel import OVModelForCausalLM
|
| 15 |
+
|
| 16 |
+
model_id = "microsoft/Phi-4-mini-instruct"
|
| 17 |
+
output_dir = "phi-4-mini-tiny-random"
|
| 18 |
+
ov_output_dir = output_dir + "-ov"
|
| 19 |
+
|
| 20 |
+
set_seed(0)
|
| 21 |
+
|
| 22 |
+
# === Step 1: Define tiny model config ===
|
| 23 |
+
config = AutoConfig.from_pretrained(model_id)
|
| 24 |
+
config.num_hidden_layers = 4
|
| 25 |
+
config.num_attention_heads = 4
|
| 26 |
+
config.num_key_value_heads = 2
|
| 27 |
+
config.hidden_size = 64
|
| 28 |
+
config.intermediate_size = 128
|
| 29 |
+
config.initializer_range = 0.1
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
if config.rope_scaling:
|
| 33 |
+
config.rope_scaling['short_factor'] = config.rope_scaling['short_factor'][::8]
|
| 34 |
+
config.rope_scaling['long_factor'] = config.rope_scaling['long_factor'][::8]
|
| 35 |
+
|
| 36 |
+
# === Step 2: Create model from config ===
|
| 37 |
+
model = Phi3ForCausalLM(config)
|
| 38 |
+
|
| 39 |
+
# === Step 3: Load or create tokenizer ===
|
| 40 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 41 |
+
|
| 42 |
+
# === Step 4: Save model and tokenizer ===
|
| 43 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 44 |
+
model.save_pretrained(output_dir)
|
| 45 |
+
tokenizer.save_pretrained(output_dir)
|
| 46 |
+
```
|