Upload init_model.py with huggingface_hub
Browse files- init_model.py +119 -0
init_model.py
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Initialize Deeplm model with config and BitNet quantization, save to safetensors.
|
| 3 |
+
"""
|
| 4 |
+
import sys
|
| 5 |
+
import os
|
| 6 |
+
import json
|
| 7 |
+
import torch
|
| 8 |
+
|
| 9 |
+
# Add deeplm to path
|
| 10 |
+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "deeplm"))
|
| 11 |
+
|
| 12 |
+
from deeplm.config import DeeplmConfig
|
| 13 |
+
from deeplm.model.deeplm import DeeplmModel
|
| 14 |
+
from deeplm.quantization.bitnet_quantize import apply_bitnet_quantization
|
| 15 |
+
|
| 16 |
+
def main():
|
| 17 |
+
print("Building DeeplmConfig...")
|
| 18 |
+
config = DeeplmConfig(
|
| 19 |
+
vocab_size=32000,
|
| 20 |
+
max_seq_length=4096,
|
| 21 |
+
dtype="float32",
|
| 22 |
+
)
|
| 23 |
+
config.architecture.num_layers = 10
|
| 24 |
+
config.architecture.hidden_size = 512
|
| 25 |
+
config.architecture.intermediate_size = 2048
|
| 26 |
+
config.architecture.num_attention_heads = 8
|
| 27 |
+
config.architecture.num_key_value_heads = 1
|
| 28 |
+
config.architecture.head_dim = 128
|
| 29 |
+
config.architecture.rope_head_dim = 64
|
| 30 |
+
config.architecture.nope_head_dim = 64
|
| 31 |
+
config.architecture.max_seq_length = 4096
|
| 32 |
+
config.architecture.rope_theta = 50000.0
|
| 33 |
+
|
| 34 |
+
config.mla.q_lora_rank = 192
|
| 35 |
+
config.mla.kv_lora_rank = 64
|
| 36 |
+
config.mla.qk_rope_head_dim = 64
|
| 37 |
+
config.mla.qk_nope_head_dim = 64
|
| 38 |
+
config.mla.v_head_dim = 128
|
| 39 |
+
config.mla.num_heads = 8
|
| 40 |
+
config.mla.kv_heads = 1
|
| 41 |
+
|
| 42 |
+
config.moe.num_routed_experts = 4
|
| 43 |
+
config.moe.num_shared_experts = 1
|
| 44 |
+
config.moe.top_k = 2
|
| 45 |
+
|
| 46 |
+
config.mtp.num_mtp_layers = 2
|
| 47 |
+
config.mtp.mtp_depth = 2
|
| 48 |
+
config.mtp.mtp_hidden_size = 512
|
| 49 |
+
|
| 50 |
+
config.output_heads.lm_head.type = "tied"
|
| 51 |
+
config.output_heads.lm_head.bias = False
|
| 52 |
+
|
| 53 |
+
print(f"Creating DeeplmModel...")
|
| 54 |
+
model = DeeplmModel(config)
|
| 55 |
+
|
| 56 |
+
total_params = model.num_parameters()
|
| 57 |
+
print(f"Total parameters: {total_params:,}")
|
| 58 |
+
|
| 59 |
+
print("Applying BitNet b1.58 ternary quantization (absmean)...")
|
| 60 |
+
stats = apply_bitnet_quantization(model, scale="absmean", verbose=True)
|
| 61 |
+
print(f"Quantized {stats['quantized']}/{stats['total_linear']} linear layers")
|
| 62 |
+
|
| 63 |
+
print("Saving to model.safetensors...")
|
| 64 |
+
from safetensors.torch import save_file
|
| 65 |
+
state_dict = model.state_dict()
|
| 66 |
+
save_file(state_dict, "model.safetensors")
|
| 67 |
+
|
| 68 |
+
# Save config.json
|
| 69 |
+
config_json = {
|
| 70 |
+
"architectures": ["DeeplmModel"],
|
| 71 |
+
"model_type": "deeplm",
|
| 72 |
+
"vocab_size": 32000,
|
| 73 |
+
"hidden_size": 512,
|
| 74 |
+
"intermediate_size": 2048,
|
| 75 |
+
"num_hidden_layers": 10,
|
| 76 |
+
"num_attention_heads": 8,
|
| 77 |
+
"num_key_value_heads": 1,
|
| 78 |
+
"max_position_embeddings": 4096,
|
| 79 |
+
"rms_norm_eps": 1e-06,
|
| 80 |
+
"rope_theta": 50000.0,
|
| 81 |
+
"rope_dim": 64,
|
| 82 |
+
"tie_word_embeddings": True,
|
| 83 |
+
"num_routed_experts": 4,
|
| 84 |
+
"num_shared_experts": 1,
|
| 85 |
+
"expert_topk": 2,
|
| 86 |
+
"q_lora_rank": 192,
|
| 87 |
+
"kv_lora_rank": 64,
|
| 88 |
+
"qk_rope_head_dim": 64,
|
| 89 |
+
"qk_nope_head_dim": 64,
|
| 90 |
+
"v_head_dim": 128,
|
| 91 |
+
"mtp_depth": 2,
|
| 92 |
+
"mtp_num_layers": 2,
|
| 93 |
+
"bitnet_quantized": True,
|
| 94 |
+
"bitnet_scale": "absmean",
|
| 95 |
+
}
|
| 96 |
+
with open("config.json", "w") as f:
|
| 97 |
+
json.dump(config_json, f, indent=2)
|
| 98 |
+
print("Saved config.json")
|
| 99 |
+
|
| 100 |
+
# Save generation_config.json
|
| 101 |
+
gen_config = {
|
| 102 |
+
"max_new_tokens": 512,
|
| 103 |
+
"do_sample": True,
|
| 104 |
+
"temperature": 0.7,
|
| 105 |
+
"top_p": 0.9,
|
| 106 |
+
"top_k": 50,
|
| 107 |
+
"repetition_penalty": 1.1,
|
| 108 |
+
"pad_token_id": 0,
|
| 109 |
+
"eos_token_id": 2,
|
| 110 |
+
"bos_token_id": 1,
|
| 111 |
+
}
|
| 112 |
+
with open("generation_config.json", "w") as f:
|
| 113 |
+
json.dump(gen_config, f, indent=2)
|
| 114 |
+
print("Saved generation_config.json")
|
| 115 |
+
|
| 116 |
+
print("Done!")
|
| 117 |
+
|
| 118 |
+
if __name__ == "__main__":
|
| 119 |
+
main()
|