Views
No views yet
| File path | Size |
|---|---|
| model.safetensors | 26.7MB |
1# Multi-token prediction is supported
2model_id=tiny-random/glm-5.2
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.2
3python3 -m sglang.launch_server \
4 --model-path $model_id \
5 --tp-size 2 \
6 --tool-call-parser glm47 \
7 --reasoning-parser glm45 \
8 --speculative-algorithm EAGLE \
9 --speculative-num-steps 3 \
10 --speculative-eagle-topk 1 \
11 --speculative-num-draft-tokens 41import torch
2from transformers import AutoModelForCausalLM, AutoTokenizer
3
4model_id = "tiny-random/glm-5.2"
5device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
6print('Using device:', device)
7tokenizer = AutoTokenizer.from_pretrained(model_id)
8input_ids = torch.randint(1000, 2000, size=(1, 2333), dtype=torch.long).to(device) # trigger DSA
9model = AutoModelForCausalLM.from_pretrained(
10 model_id,
11 dtype=torch.bfloat16,
12 device_map=device,
13)
14generated_ids = model.generate(input_ids, max_new_tokens=8) # pyright: ignore[reportAttributeAccessIssue]
15output_text = tokenizer.decode(generated_ids[0][input_ids.shape[1]:])
16print(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.2"
18save_folder = "/tmp/tiny-random/glm-52" # pyright: ignore[reportUnusedExpression]
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'] * 3,
30 "hidden_size": 8,
31 "index_n_heads": 32,
32 "indexer_types": ['full'] + ['shared'] * 3,
33 "index_topk_pattern": ['F'] + ['S'] * 3,
34 "intermediate_size": 32,
35 "moe_intermediate_size": 32,
36 "num_hidden_layers": 4,
37 "num_attention_heads": 8,
38 'num_key_value_heads': 8,
39 'q_lora_rank': 32,
40 'tie_word_embeddings': False,
41})
42with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f:
43 json.dump(config_json, f, indent=2)
44
45config = AutoConfig.from_pretrained(
46 save_folder,
47 trust_remote_code=True,
48)
49print(config)
50torch.set_default_dtype(torch.bfloat16)
51model = AutoModelForCausalLM.from_config(config, dtype=torch.bfloat16)
52torch.set_default_dtype(torch.float32)
53
54if file_exists(filename="generation_config.json", repo_id=source_model_id, repo_type='model'):
55 model.generation_config = GenerationConfig.from_pretrained(
56 source_model_id, trust_remote_code=True,
57 )
58 model.generation_config.do_sample = True
59 print(model.generation_config)
60
61model = model.cpu()
62set_seed(42)
63n_params = sum(p.numel() for p in model.parameters())
64with torch.no_grad():
65 for name, p in sorted(model.named_parameters()):
66 torch.nn.init.normal_(p, 0, 0.2)
67 mb = p.numel() / 1024 / 1024 * p.element_size()
68 print(name, p.shape, f'{p.numel() / n_params:.2%}', f'{mb:.2f}MB')
69# MTP
70set_seed(42)
71model.model.layers.append(nn.ModuleDict(dict(
72 shared_head=nn.ModuleDict(dict(
73 norm=nn.RMSNorm(config.hidden_size),
74 # head=deepcopy(model.model.embed_tokens),
75 )),
76 # embed_tokens=deepcopy(model.model.embed_tokens),
77 eh_proj=nn.Linear(config.hidden_size * 2,
78 config.hidden_size, bias=False),
79 enorm=nn.RMSNorm(config.hidden_size),
80 hnorm=nn.RMSNorm(config.hidden_size),
81 input_layernorm=nn.RMSNorm(config.hidden_size),
82 post_attention_layernorm=nn.RMSNorm(config.hidden_size),
83 self_attn=deepcopy(model.model.layers[0].self_attn),
84 mlp=deepcopy(model.model.layers[1].mlp),
85)))
86for i in range(1, len(model.model.layers)):
87 model.model.layers[i].mlp.gate.e_score_correction_bias = torch.rand_like(
88 model.model.layers[i].mlp.gate.e_score_correction_bias).float()
89model.save_pretrained(save_folder)
90print(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=4096, 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=32, 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-3): 3 x 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 )
40 (mlp): GlmMoeDsaMoE(
41 (experts): GlmMoeDsaExperts(
42 (act_fn): SiLUActivation()
43 )
44 (gate): GlmMoeDsaTopkRouter()
45 (shared_experts): GlmMoeDsaMLP(
46 (gate_proj): Linear(in_features=8, out_features=32, bias=False)
47 (up_proj): Linear(in_features=8, out_features=32, bias=False)
48 (down_proj): Linear(in_features=32, out_features=8, bias=False)
49 (act_fn): SiLUActivation()
50 )
51 )
52 (input_layernorm): GlmMoeDsaRMSNorm((8,), eps=1e-05)
53 (post_attention_layernorm): GlmMoeDsaRMSNorm((8,), eps=1e-05)
54 )
55 (4): ModuleDict(
56 (shared_head): ModuleDict(
57 (norm): RMSNorm((8,), eps=None, elementwise_affine=True)
58 )
59 (eh_proj): Linear(in_features=16, out_features=8, bias=False)
60 (enorm): RMSNorm((8,), eps=None, elementwise_affine=True)
61 (hnorm): RMSNorm((8,), eps=None, elementwise_affine=True)
62 (input_layernorm): RMSNorm((8,), eps=None, elementwise_affine=True)
63 (post_attention_layernorm): RMSNorm((8,), eps=None, elementwise_affine=True)
64 (self_attn): GlmMoeDsaAttention(
65 (q_a_proj): Linear(in_features=8, out_features=32, bias=False)
66 (q_a_layernorm): GlmMoeDsaRMSNorm((32,), eps=1e-06)
67 (q_b_proj): Linear(in_features=32, out_features=2048, bias=False)
68 (kv_a_proj_with_mqa): Linear(in_features=8, out_features=576, bias=False)
69 (kv_a_layernorm): GlmMoeDsaRMSNorm((512,), eps=1e-06)
70 (kv_b_proj): Linear(in_features=512, out_features=3584, bias=False)
71 (o_proj): Linear(in_features=2048, out_features=8, bias=False)
72 (indexer): GlmMoeDsaIndexer(
73 (wq_b): Linear(in_features=32, out_features=4096, bias=False)
74 (wk): Linear(in_features=8, out_features=128, bias=False)
75 (k_norm): LayerNorm((128,), eps=1e-06, elementwise_affine=True)
76 (weights_proj): Linear(in_features=8, out_features=32, bias=False)
77 )
78 )
79 (mlp): GlmMoeDsaMoE(
80 (experts): GlmMoeDsaExperts(
81 (act_fn): SiLUActivation()
82 )
83 (gate): GlmMoeDsaTopkRouter()
84 (shared_experts): GlmMoeDsaMLP(
85 (gate_proj): Linear(in_features=8, out_features=32, bias=False)
86 (up_proj): Linear(in_features=8, out_features=32, bias=False)
87 (down_proj): Linear(in_features=32, out_features=8, bias=False)
88 (act_fn): SiLUActivation()
89 )
90 )
91 )
92 )
93 (norm): GlmMoeDsaRMSNorm((8,), eps=1e-05)
94 (rotary_emb): GlmMoeDsaRotaryEmbedding()
95 )
96 (lm_head): Linear(in_features=8, out_features=154880, bias=False)
97)