Views
No views yet
1vllm serve tiny-random/longcat-flash \
2 --trust-remote-code \
3 --enable-expert-parallel \
4 --tensor-parallel-size 1 \
5 --speculative_config '{"model": "tiny-random/longcat-flash", "num_speculative_tokens": 1, "method":"longcat_flash_mtp"}'
61python3 -m sglang.launch_server \
2 --model tiny-random/longcat-flash \
3 --trust-remote-code \
4 --attention-backend flashinfer \
5 --enable-ep-moe \
6 --tp 1 \
7 --speculative-draft-model-path tiny-random/longcat-flash \
8 --speculative-algorithm NEXTN \
9 --speculative-num-draft-tokens 2 \
10 --speculative-num-steps 1 \
11 --speculative-eagle-topk 11import torch
2import transformers
3
4model_id = "tiny-random/longcat-flash"
5pipe = transformers.pipelines.pipeline(
6 'text-generation',
7 model=model_id,
8 trust_remote_code=True,
9 device_map='cuda',
10 torch_dtype=torch.bfloat16,
11)
12past_key_values = transformers.DynamicCache(config=None) # set config to None
13r = pipe('Hello, world!', past_key_values=past_key_values, max_new_tokens=32)
14print(r)1import json
2from copy import deepcopy
3from pathlib import Path
4
5import torch
6import torch.nn as nn
7from huggingface_hub import file_exists, hf_hub_download
8from transformers import (
9 AutoConfig,
10 AutoModelForCausalLM,
11 AutoProcessor,
12 AutoTokenizer,
13 GenerationConfig,
14 set_seed,
15)
16from transformers.models.glm4_moe.modeling_glm4_moe import Glm4MoeRMSNorm
17source_model_id = "meituan-longcat/LongCat-Flash-Chat"
18save_folder = "/tmp/tiny-random/longcat-flash"
19
20Path(save_folder).mkdir(parents=True, exist_ok=True)
21tokenizer = AutoTokenizer.from_pretrained(source_model_id, trust_remote_code=True)
22tokenizer.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 = json.load(f)
26for k, v in config_json['auto_map'].items():
27 config_json['auto_map'][k] = f'{source_model_id}--{v}'
28config_json.update({
29 'num_layers': 2,
30 'hidden_size': 8,
31 'ffn_hidden_size': 64,
32 'expert_ffn_hidden_size': 64,
33 'num_attention_heads': 4,
34 'kv_lora_rank': 384,
35 'n_routed_experts': 32,
36 'q_lora_rank': 32,
37 'qk_nope_head_dim': 64,
38 'qk_rope_head_dim': 192, # vllm mla kernel supports 576 only, FA supports head dim <= 256
39 'v_head_dim': 64,
40 'moe_topk': 12,
41 'zero_expert_num': 16,
42})
43# del config_json['quantization_config']
44with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f:
45 json.dump(config_json, f, indent=2)
46
47config = AutoConfig.from_pretrained(
48 save_folder,
49 trust_remote_code=True,
50)
51print(config)
52torch.set_default_dtype(torch.bfloat16)
53model = AutoModelForCausalLM.from_config(config, trust_remote_code=True)
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 )
58model = model.cpu()
59# MTP
60model.model.mtp = nn.ModuleDict({
61 "layers": nn.ModuleList([nn.ModuleDict(dict(
62 eh_proj=nn.Linear(config.hidden_size * 2, config.hidden_size, bias=False),
63 enorm=nn.ModuleDict({"m": nn.RMSNorm(config.hidden_size)}),
64 hnorm=nn.ModuleDict({"m": nn.RMSNorm(config.hidden_size)}),
65 input_layernorm=nn.RMSNorm(config.hidden_size),
66 post_attention_layernorm=nn.RMSNorm(config.hidden_size),
67 self_attn=deepcopy(model.model.layers[0].self_attn[0]),
68 transformer_layer=nn.ModuleDict({"mlp": deepcopy(model.model.layers[0].mlps[0])}),
69 ))]),
70 "norm": nn.RMSNorm(config.hidden_size),
71})
72for i in range(config.num_layers):
73 model.model.layers[i].mlp.router = model.model.layers[i].mlp.router.float()
74 # model.model.layers[i].mlp.router.e_score_correction_bias = torch.zeros((config.n_routed_experts + config.zero_expert_num)).float()
75set_seed(42)
76with torch.no_grad():
77 for name, p in sorted(model.named_parameters()):
78 torch.nn.init.normal_(p, 0, 0.1)
79 print(name, p.shape, p.dtype)
80model.model.mtp.embed_tokens = deepcopy(model.model.embed_tokens)
81
82model.save_pretrained(save_folder)
83torch.set_default_dtype(torch.float32)
84
85for n, m in model.named_modules():
86 if 'LongcatFlashMLA' in str(type(m)):
87 print(n, m.layer_idx)
88
89with open(f"{save_folder}/config.json", "r", encoding='utf-8') as f:
90 config_json = json.load(f)
91 config_json['auto_map'] = {k: v.split('--')[-1] for k, v in config_json['auto_map'].items()}
92with open(f"{save_folder}/config.json", "w", encoding='utf-8') as f:
93 json.dump(config_json, f, indent=2)1LongcatFlashForCausalLM(
2 (model): LongcatFlashModel(
3 (embed_tokens): Embedding(131072, 8)
4 (layers): ModuleList(
5 (0-1): 2 x LongcatFlashDecoderLayer(
6 (mlp): LongcatFlashMoE(
7 (experts): ModuleList(
8 (0-31): 32 x LongcatFlashMLP(
9 (gate_proj): Linear(in_features=8, out_features=64, bias=False)
10 (up_proj): Linear(in_features=8, out_features=64, bias=False)
11 (down_proj): Linear(in_features=64, out_features=8, bias=False)
12 (act_fn): SiLU()
13 )
14 )
15 (router): LongcatFlashTopkRouter(
16 (classifier): Linear(in_features=8, out_features=48, bias=False)
17 )
18 )
19 (self_attn): ModuleList(
20 (0-1): 2 x LongcatFlashMLA(
21 (q_a_proj): Linear(in_features=8, out_features=32, bias=False)
22 (q_a_layernorm): LongcatFlashRMSNorm((32,), eps=1e-06)
23 (q_b_proj): Linear(in_features=32, out_features=1024, bias=False)
24 (kv_a_proj_with_mqa): Linear(in_features=8, out_features=576, bias=False)
25 (kv_a_layernorm): LongcatFlashRMSNorm((384,), eps=1e-06)
26 (kv_b_proj): Linear(in_features=384, out_features=512, bias=False)
27 (o_proj): Linear(in_features=256, out_features=8, bias=False)
28 )
29 )
30 (mlps): ModuleList(
31 (0-1): 2 x LongcatFlashMLP(
32 (gate_proj): Linear(in_features=8, out_features=64, bias=False)
33 (up_proj): Linear(in_features=8, out_features=64, bias=False)
34 (down_proj): Linear(in_features=64, out_features=8, bias=False)
35 (act_fn): SiLU()
36 )
37 )
38 (input_layernorm): ModuleList(
39 (0-1): 2 x LongcatFlashRMSNorm((8,), eps=1e-05)
40 )
41 (post_attention_layernorm): ModuleList(
42 (0-1): 2 x LongcatFlashRMSNorm((8,), eps=1e-05)
43 )
44 )
45 )
46 (norm): LongcatFlashRMSNorm((8,), eps=1e-05)
47 (rotary_emb): LongcatFlashRotaryEmbedding()
48 (mtp): ModuleDict(
49 (layers): ModuleList(
50 (0): ModuleDict(
51 (eh_proj): Linear(in_features=16, out_features=8, bias=False)
52 (enorm): ModuleDict(
53 (m): RMSNorm((8,), eps=None, elementwise_affine=True)
54 )
55 (hnorm): ModuleDict(
56 (m): RMSNorm((8,), eps=None, elementwise_affine=True)
57 )
58 (input_layernorm): RMSNorm((8,), eps=None, elementwise_affine=True)
59 (post_attention_layernorm): RMSNorm((8,), eps=None, elementwise_affine=True)
60 (self_attn): LongcatFlashMLA(
61 (q_a_proj): Linear(in_features=8, out_features=32, bias=False)
62 (q_a_layernorm): LongcatFlashRMSNorm((32,), eps=1e-06)
63 (q_b_proj): Linear(in_features=32, out_features=1024, bias=False)
64 (kv_a_proj_with_mqa): Linear(in_features=8, out_features=576, bias=False)
65 (kv_a_layernorm): LongcatFlashRMSNorm((384,), eps=1e-06)
66 (kv_b_proj): Linear(in_features=384, out_features=512, bias=False)
67 (o_proj): Linear(in_features=256, out_features=8, bias=False)
68 )
69 (transformer_layer): ModuleDict(
70 (mlp): LongcatFlashMLP(
71 (gate_proj): Linear(in_features=8, out_features=64, bias=False)
72 (up_proj): Linear(in_features=8, out_features=64, bias=False)
73 (down_proj): Linear(in_features=64, out_features=8, bias=False)
74 (act_fn): SiLU()
75 )
76 )
77 )
78 )
79 (norm): RMSNorm((8,), eps=None, elementwise_affine=True)
80 (embed_tokens): Embedding(131072, 8)
81 )
82 )
83 (lm_head): Linear(in_features=8, out_features=131072, bias=False)
84)