Views
No views yet
| File path | Size |
|---|---|
| model.safetensors | 17.6MB |
1# Multi-token prediction is supported
2model_id=tiny-random/glm-5.1
3vllm serve $model_id \
4 --tensor-parallel-size 2 \
5 --speculative-config.method mtp \
6 --speculative-config.num_speculative_tokens 1 \
7 --tool-call-parser glm47 \
8 --reasoning-parser glm45 \
9 --enable-auto-tool-choice1# Multi-token prediction is supported
2model_id=tiny-random/glm-5.1
3python3 -m sglang.launch_server --model-path $model_id --tp-size 2 \
4 --tool-call-parser glm47 \
5 --reasoning-parser glm45 \
6 --speculative-algorithm EAGLE \
7 --speculative-num-steps 3 \
8 --speculative-eagle-topk 1 \
9 --speculative-num-draft-tokens 41import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_id = "tiny-random/glm-5.1"
5device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6tokenizer = AutoTokenizer.from_pretrained(model_id)
7input_ids = torch.randint(1000, 2000, size=(1, 2333), dtype=torch.long).to(device) # trigger DSA
8model = AutoModelForCausalLM.from_pretrained(
9 model_id,
10 dtype=torch.bfloat16,
11 device_map=device,
12)
13generated_ids = model.generate(input_ids, max_new_tokens=8)
14output_text = tokenizer.decode(generated_ids[0][input_ids.shape[1]:])
15print(output_text)1import json
2from copy import deepcopy
3from pathlib import Path
4
5import accelerate
6import torch
7import torch.nn as nn
8from huggingface_hub import file_exists, hf_hub_download
9from transformers import (
10 AutoConfig,
11 AutoModelForCausalLM,
12 AutoProcessor,
13 GenerationConfig,
14 set_seed,
15)
16
17source_model_id = "zai-org/GLM-5.1"
18save_folder = "/tmp/tiny-random/glm-51"
19
20processor = AutoProcessor.from_pretrained(
21 source_model_id, trust_remote_code=True)
22processor.save_pretrained(save_folder)
23
24with open(hf_hub_download(source_model_id, filename='config.json', repo_type='model'), 'r', encoding='utf-8') as f:
25 config_json: dict = json.load(f)
26
27config_json.update({
28 "first_k_dense_replace": 1,
29 "mlp_layer_types": ['dense'] + ['sparse'],
30 "hidden_size": 8,
31 "index_n_heads": 4,
32 "intermediate_size": 32,
33 "moe_intermediate_size": 32,
34 "num_hidden_layers": 2,
35 "num_attention_heads": 8,
36 'num_key_value_heads': 8,
37 'q_lora_rank': 32,
38 'tie_word_embeddings': False,
39})
40with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f:
41 json.dump(config_json, f, indent=2)
42
43config = AutoConfig.from_pretrained(
44 save_folder,
45 trust_remote_code=True,
46)
47print(config)
48torch.set_default_dtype(torch.bfloat16)
49model = AutoModelForCausalLM.from_config(config, dtype=torch.bfloat16)
50torch.set_default_dtype(torch.float32)
51
52if file_exists(filename="generation_config.json", repo_id=source_model_id, repo_type='model'):
53 model.generation_config = GenerationConfig.from_pretrained(
54 source_model_id, trust_remote_code=True,
55 )
56 model.generation_config.do_sample = True
57 print(model.generation_config)
58
59model = model.cpu()
60set_seed(42)
61n_params = sum(p.numel() for p in model.parameters())
62with torch.no_grad():
63 for name, p in sorted(model.named_parameters()):
64 torch.nn.init.normal_(p, 0, 0.2)
65 print(name, p.shape, p.numel() / n_params * 100, '%')
66# MTP
67set_seed(42)
68model.model.layers.append(nn.ModuleDict(dict(
69 shared_head=nn.ModuleDict(dict(
70 norm=nn.RMSNorm(config.hidden_size),
71 # head=deepcopy(model.model.embed_tokens),
72 )),
73 # embed_tokens=deepcopy(model.model.embed_tokens),
74 eh_proj=nn.Linear(config.hidden_size * 2,
75 config.hidden_size, bias=False),
76 enorm=nn.RMSNorm(config.hidden_size),
77 hnorm=nn.RMSNorm(config.hidden_size),
78 input_layernorm=nn.RMSNorm(config.hidden_size),
79 post_attention_layernorm=nn.RMSNorm(config.hidden_size),
80 self_attn=deepcopy(model.model.layers[1].self_attn),
81 mlp=deepcopy(model.model.layers[1].mlp),
82)))
83for i in range(1, len(model.model.layers)):
84 model.model.layers[i].mlp.gate.e_score_correction_bias = torch.rand_like(
85 model.model.layers[i].mlp.gate.e_score_correction_bias).float()
86model.save_pretrained(save_folder)
87print(model)1GlmMoeDsaForCausalLM(
2 (model): GlmMoeDsaModel(
3 (embed_tokens): Embedding(154880, 8, padding_idx=154820)
4 (layers): ModuleList(
5 (0): GlmMoeDsaDecoderLayer(
6 (self_attn): GlmMoeDsaAttention(
7 (q_a_proj): Linear(in_features=8, out_features=32, bias=False)
8 (q_a_layernorm): GlmMoeDsaRMSNorm((32,), eps=1e-06)
9 (q_b_proj): Linear(in_features=32, out_features=2048, bias=False)
10 (kv_a_proj_with_mqa): Linear(in_features=8, out_features=576, bias=False)
11 (kv_a_layernorm): GlmMoeDsaRMSNorm((512,), eps=1e-06)
12 (kv_b_proj): Linear(in_features=512, out_features=3584, bias=False)
13 (o_proj): Linear(in_features=2048, out_features=8, bias=False)
14 (indexer): GlmMoeDsaIndexer(
15 (wq_b): Linear(in_features=32, out_features=512, bias=False)
16 (wk): Linear(in_features=8, out_features=128, bias=False)
17 (k_norm): LayerNorm((128,), eps=1e-06, elementwise_affine=True)
18 (weights_proj): Linear(in_features=8, out_features=4, bias=False)
19 )
20 )
21 (mlp): GlmMoeDsaMLP(
22 (gate_proj): Linear(in_features=8, out_features=32, bias=False)
23 (up_proj): Linear(in_features=8, out_features=32, bias=False)
24 (down_proj): Linear(in_features=32, out_features=8, bias=False)
25 (act_fn): SiLUActivation()
26 )
27 (input_layernorm): GlmMoeDsaRMSNorm((8,), eps=1e-05)
28 (post_attention_layernorm): GlmMoeDsaRMSNorm((8,), eps=1e-05)
29 )
30 (1): GlmMoeDsaDecoderLayer(
31 (self_attn): GlmMoeDsaAttention(
32 (q_a_proj): Linear(in_features=8, out_features=32, bias=False)
33 (q_a_layernorm): GlmMoeDsaRMSNorm((32,), eps=1e-06)
34 (q_b_proj): Linear(in_features=32, out_features=2048, bias=False)
35 (kv_a_proj_with_mqa): Linear(in_features=8, out_features=576, bias=False)
36 (kv_a_layernorm): GlmMoeDsaRMSNorm((512,), eps=1e-06)
37 (kv_b_proj): Linear(in_features=512, out_features=3584, bias=False)
38 (o_proj): Linear(in_features=2048, out_features=8, bias=False)
39 (indexer): GlmMoeDsaIndexer(
40 (wq_b): Linear(in_features=32, out_features=512, bias=False)
41 (wk): Linear(in_features=8, out_features=128, bias=False)
42 (k_norm): LayerNorm((128,), eps=1e-06, elementwise_affine=True)
43 (weights_proj): Linear(in_features=8, out_features=4, bias=False)
44 )
45 )
46 (mlp): GlmMoeDsaMoE(
47 (experts): GlmMoeDsaNaiveMoe(
48 (act_fn): SiLUActivation()
49 )
50 (gate): GlmMoeDsaTopkRouter()
51 (shared_experts): GlmMoeDsaMLP(
52 (gate_proj): Linear(in_features=8, out_features=32, bias=False)
53 (up_proj): Linear(in_features=8, out_features=32, bias=False)
54 (down_proj): Linear(in_features=32, out_features=8, bias=False)
55 (act_fn): SiLUActivation()
56 )
57 )
58 (input_layernorm): GlmMoeDsaRMSNorm((8,), eps=1e-05)
59 (post_attention_layernorm): GlmMoeDsaRMSNorm((8,), eps=1e-05)
60 )
61 (2): ModuleDict(
62 (shared_head): ModuleDict(
63 (norm): RMSNorm((8,), eps=None, elementwise_affine=True)
64 )
65 (eh_proj): Linear(in_features=16, out_features=8, bias=False)
66 (enorm): RMSNorm((8,), eps=None, elementwise_affine=True)
67 (hnorm): RMSNorm((8,), eps=None, elementwise_affine=True)
68 (input_layernorm): RMSNorm((8,), eps=None, elementwise_affine=True)
69 (post_attention_layernorm): RMSNorm((8,), eps=None, elementwise_affine=True)
70 (self_attn): GlmMoeDsaAttention(
71 (q_a_proj): Linear(in_features=8, out_features=32, bias=False)
72 (q_a_layernorm): GlmMoeDsaRMSNorm((32,), eps=1e-06)
73 (q_b_proj): Linear(in_features=32, out_features=2048, bias=False)
74 (kv_a_proj_with_mqa): Linear(in_features=8, out_features=576, bias=False)
75 (kv_a_layernorm): GlmMoeDsaRMSNorm((512,), eps=1e-06)
76 (kv_b_proj): Linear(in_features=512, out_features=3584, bias=False)
77 (o_proj): Linear(in_features=2048, out_features=8, bias=False)
78 (indexer): GlmMoeDsaIndexer(
79 (wq_b): Linear(in_features=32, out_features=512, bias=False)
80 (wk): Linear(in_features=8, out_features=128, bias=False)
81 (k_norm): LayerNorm((128,), eps=1e-06, elementwise_affine=True)
82 (weights_proj): Linear(in_features=8, out_features=4, bias=False)
83 )
84 )
85 (mlp): GlmMoeDsaMoE(
86 (experts): GlmMoeDsaNaiveMoe(
87 (act_fn): SiLUActivation()
88 )
89 (gate): GlmMoeDsaTopkRouter()
90 (shared_experts): GlmMoeDsaMLP(
91 (gate_proj): Linear(in_features=8, out_features=32, bias=False)
92 (up_proj): Linear(in_features=8, out_features=32, bias=False)
93 (down_proj): Linear(in_features=32, out_features=8, bias=False)
94 (act_fn): SiLUActivation()
95 )
96 )
97 )
98 )
99 (norm): GlmMoeDsaRMSNorm((8,), eps=1e-05)
100 (rotary_emb): GlmMoeDsaRotaryEmbedding()
101 )
102 (lm_head): Linear(in_features=8, out_features=154880, bias=False)
103)