from flash_mla_interface import flash_mla_varlen_func, flash_mla_with_kvcache
from streaming_sparse_attn_interface import streaming_sparse_attn_varlen_func, streaming_sparse_attn_with_kvcache
full_attn_out = flash_mla_varlen_func_(
q, # [nnz_q, num_heads_q, head_dim_qk]
k, # [nnz_k, num_heads_k, head_dim_qk]
v, # [nnz_k, num_heads_v, head_dim_vo]
cu_seqlens_q,
cu_seqlens_k,
max_seqlen_q,
max_seqlen_k,
softmax_scale,
causal=True
)
stream_attn_out = streaming_sparse_attn_varlen_func_(
q,
k,
v,
cu_seqlens_q,
cu_seqlens_k,
max_seqlen_q,
max_seqlen_k,
softmax_scale,
causal=True
)
full_attn_out = flash_mla_with_kvcache(
q, # [batch_size, seqlen_q, num_heads_q, head_dim_nope + head_dim_rope]
blocked_k, # [num_pages, page_size, num_heads_k, head_dim_nope + head_dim_rope]
cache_seqlens,
block_table,
head_dim_nope,
softmax_scale,
causal=True
)
stream_attn_out = streaming_sparse_attn_with_kvcache(
q,
blocked_k,
cache_seqlens,
block_table,
head_dim_nope,
softmax_scale,
causal=True
)性能基准测试如下:
除了 LongCat-Flash-Thinking-2601 之外,我们还推出了一款高效的替代方案,名为 LongCat-Flash-Thinking-ZigZag。LongCat-Flash-Thinking-ZigZag 与 LongCat-Flash-Thinking-2601 并无本质区别,只是它通过 LongCat 之字形注意力(LoZA)得到了进一步增强。LoZA 本质上是一种稀疏注意力机制,旨在将任何现有的全注意力模型转换为计算预算相当有限的稀疏版本。在长上下文场景中,LoZA 能够在预填充密集型(例如检索增强生成)和解码密集型(例如工具集成推理)情况下均实现显著的速度提升。 具体而言,通过在训练中期使用 LoZA 使 LongCat-Flash-Thinking-ZigZag 与 LongCat-Flash-Thinking-2601 分道扬镳,我们将 LongCat-Flash-Thinking-ZigZag 打造为一个长上下文基础模型,它能够快速处理长序列的 tokens,从而实现高效的长程推理和长视野智能体能力。
LongCat 之字形注意力(LoZA)首先找出可以进行稀疏化且不会显著损害性能的层,其次对这些层进行稀疏化处理,然后通过进一步训练来弥补性能差距。整个过程与“彩票假设”中所描述的情况非常相似。理论上,一个经过中期训练的语言模型会被依次稀疏化、回退,并在中期重新训练以最大限度地恢复其完整性能。换句话说,校准在中期训练结束时开始,而训练则从中期训练的开始阶段重新启动,与从头开始训练相比,这只会带来相当微小的计算开销。
LoZA 使 LongCat-Flash-Thinking-ZigZag 实现了 50% 的稀疏度,理想情况下,注意力带来的计算量应减少一半。在注意力主导计算的长上下文环境中,效率最高可提升至原来的 2 倍。通过我们在 kernel 和引擎定制方面的努力,在下图中,对于 128K tokens 的上下文,流式稀疏注意力 kernel 在解码时的成本相比全注意力 kernel(即 FlashMLA)可至少降低 90%。同时,在端到端基准测试中,对于 256K tokens 的上下文,LongCat-Flash-Thinking-ZigZag 在预填充阶段实现了超过 50% 的速度提升,在解码阶段节省了超过 30% 的成本。
LoZA 不会为了速度而牺牲质量。在相关基准测试中,LongCat-Flash-Thinking-ZigZag 展现出与 LongCat-Flash-Thinking-2601 相当的性能。具体而言,LongCat-Flash-Thinking-ZigZag 也与 DeepSeek-V3.2 等其他竞争对手处于同一水平线上。并且,如下图所示,在各种不同的基准测试中,也实现了可观的成本节约。
| 基准测试 | DeepSeek-V3.2-Thinking | Kimi-K2-Thinking | Qwen3-235B-A22B-Thinking-2507 | GLM-4.7-Thinking | Claude-Opus-4.5-Thinking | Gemini-3-Pro | GPT-5.2-Thinking-xhigh | LongCat-Flash-Thinking-2601 | LongCat-Flash-Thinking-ZigZag |
|---|---|---|---|---|---|---|---|---|---|
| 架构 | MoE | MoE | MoE | MoE | - | - | - | MoE | MoE |
| 稀疏注意力 | ✅ | ❌ | ❌ | ❌ | - | - | - | ❌ | ✅ |
| 总参数数量 | 671B | 1T | 235B | 355B | - | - | - | 560B | |
| 激活参数数量 | 37B | 32B | 22B | 32B | - | - | - | 27B | |
| 工具辅助数学推理 | |||||||||
| AIME-25(平均@16) | 93.5* | 99.1† | 92.6* | 95.3* | 100.0 | 99.8 | 100.0 | 99.6 / 100.0‡ | 99.2 / - |
| HMMT-25(平均@16) | 93.5* | 95.1† | 83.9* | 98.1* | 98.6 | 99.8 | 99.6 | 93.4 / 97.5‡ | 93.5 / - |
| AMO-Bench EN(平均@16) | 51.9* | 56.0* | 47.8* | 62.4* | 66.0 | 72.5 | - | 61.6 / 66.0‡ | 60.4 / - |
| AMO-Bench CH(平均@16) | 52.0* | 51.8* | 28.8* | 35.1* | 67.7 | 74.9 | - | 56.8 / 67.5‡ | 58.3 / - |
| 智能体搜索 | |||||||||
| BrowseComp(通过率@1) | 51.4 / 67.6† | - / 60.2† | - | 52.0 / 67.5† | - | - | 65.8 / - | 56.6 / 73.1 | 55.2 / - |
| BrowseComp-zh(通过率@1) | 65.0 / - | - / 62.3† | - | 66.6 / - | - | - | - | 69.0 / 77.7 | 71.9 / - |
| 智能体工具使用 | |||||||||
| τ²-Retail(平均@4) | 81.8† | - | 71.9† | - | 88.9† | - | 82.0† | 88.6 | 86.8 |
| τ²-Airline(平均@4) | 63.8† | - | 58.6† | - | - | - | - | 76.5 | 76.5 |
| τ²-Telecom(平均@4) | 96.2† | - | 47.3 | - | 98.2† | - | 98.7† | 99.3 | 97.4 |
| τ²-平均(平均@4) | 80.6 | 74.3† | 59.3 | 87.4† | 82.4 | 90.7† | 80.6 | 88.2 | 86.9 |
| 通用问答 | |||||||||
| HLE 纯文本(无工具) | 24.1 | 24.4 | 17.8 | 26.9 | 32.0 | 40.3 | 34.5† | 25.2 | 25.8 |
| GPQA-Diamond(平均@16) | 86.9 | 85.4 | 80.5 | 84.9 | 86.9 | 91.9 | 92.9 | 80.5 / 85.2‡ | 80.6 / - |
| 代码生成 | |||||||||
| LCB(24.08–25.05)(平均@4) | 82.4 | 75.1 | 76.2 | 84.8 | 82.8 | 88.1 | - | 82.8 | 81.7 |
| OJBench(通过率@1) | 41.8 | 42.3 | 35.6 | 44.6 | 46.7 | 61.2 | - | 42.2 | 41.2 |
注:
为支持高级工具使用场景和复杂推理模式,我们对 tokenizer_config.json 文件中定义的对话模板进行了重大更新。
可通过 apply_chat_template 方法应用对话模板。以下是标准实现:
text = tokenizer.apply_chat_template(
messages,
tools=tools,
tokenize=False,
enable_thinking=True,
add_generation_prompt=True,
save_history_reasoning_content=False
)save_history_reasoning_content=True 来启用此功能。本示例展示了模板如何处理对话历史和思考内容。
from transformers import AutoModelForCausalLM, AutoTokenizer
model_name = "meituan-longcat/LongCat-Flash-Thinking-ZigZag"
# Load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained(model_name)
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Please tell me what is $$1 + 1$$ and $$2 \times 2$$?"},
{"role": "assistant", "reasoning_content": "This question is straightforward: $$1 + 1 = 2$$ and $$2 \times 2 = 4$$.", "content": "The answers are 2 and 4."},
{"role": "user", "content": "Check again?"}
]
text = tokenizer.apply_chat_template(
messages,
tokenize=False,
enable_thinking=True,
add_generation_prompt=True,
save_history_reasoning_content=False # Discard reasoning history to save tokens
)
# Template Output Structure:
# <longcat_system>You are a helpful assistant.<longcat_user>Please tell me what is $$1 + 1$$ and $$2 \times 2$$?<longcat_assistant>The answers are 2 and 4</longcat_s><longcat_user>Check again? /think_on <longcat_assistant><longcat_think>\n
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
# Generate response
generated_ids = model.generate(
**model_inputs,
max_new_tokens=32768
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
print(tokenizer.decode(output_ids, skip_special_tokens=True).strip("\n"))
# Example Output:
# The user wants a double-check. Since $$1 + 1 = 2$$ and $$2 \times 2 = 4$$ are basic arithmetic truths, the previous answer is correct.\n</longcat_think>\nI have verified the calculations: $$1 + 1 = 2$$ and $$2 \times 2 = 4$$. The initial answer remains correct.</longcat_s>本示例展示了如何在推理框架中集成函数调用。
tools = [
{
"type": "function",
"function": {
"name": "func_add",
"description": "Calculate the sum of two numbers",
"parameters": {
"type": "object",
"properties": {
"x1": {"type": "number", "description": "The first addend"},
"x2": {"type": "number", "description": "The second addend"}
},
"required": ["x1", "x2"]
}
}
}
]
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "Please tell me what is $$125679 + 234519$$?"},
{
"role": "assistant",
"reasoning_content": "This calculation requires precision; I will use the func_add tool.",
"tool_calls": [{"type": "function", "function": {"name": "func_add", "arguments": {"x1": 125679, "x2": 234519}}}]
},
{"role": "tool", "name": "func_add", "content": '{"ans": 360198}'}
]
text = tokenizer.apply_chat_template(
messages,
tools=tools,
tokenize=False,
enable_thinking=True,
add_generation_prompt=True,
save_history_reasoning_content=False
)
model_inputs = tokenizer([text], return_tensors="pt").to(model.device)
# Generate response based on tool result
generated_ids = model.generate(
**model_inputs,
max_new_tokens=32768
)
output_ids = generated_ids[0][len(model_inputs.input_ids[0]):].tolist()
print(tokenizer.decode(output_ids, skip_special_tokens=True).strip("\n"))我们已在SGLang中完成基础适配(PR),以支持LongCat-Flash-Thinking-ZigZag的部署。
补充说明:tilelang中的参考流式稀疏注意力预填充内核已在modeling_longcat.py中提供。
git clone https://github.com/HarryWu99/sglang.git
git checkout 415a1d07ca49cd6681d8e4b153ae74c780c7a4cfpython3 -m sglang.launch_server \
--model meituan-longcat/LongCat-Flash-Thinking-ZigZag \
--trust-remote-code \
--attention-backend zigzag_attn \
--enable-ep-moe \
--tp 8python3 -m sglang.launch_server \
--model meituan-longcat/LongCat-Flash-Thinking-ZigZag \
--trust-remote-code \
--attention-backend zigzag_attn \
--enable-ep-moe \
--tp 16 \
--nnodes 2 \
--node-rank $NODE_RANK \
--dist-init-addr $MASTER_IP:5000[!Note] 请将 MASTER_IP 替换为您集群的具体值。
您可以在我们的官方网站与 LongCat-Flash-Thinking-2601 进行对话:https://longcat.ai。 提交请求前,请开启“Think”按钮(中文为“深度思考”)。
模型权重以MIT 许可证发布。
除非另有说明,对本仓库的任何贡献均采用 MIT 许可证。本许可证不授予使用美团商标或专利的任何权利。
完整许可文本详见 LICENSE 文件。
本模型并非针对所有可能的下游应用进行专门设计或全面评估。
开发人员应考虑到大型语言模型的已知局限性,包括在不同语言上的性能差异,并在将模型部署到敏感或高风险场景前,仔细评估其准确性、安全性和公平性。开发人员和下游用户有责任了解并遵守与其使用场景相关的所有适用法律法规,包括但不限于数据保护、隐私和内容安全要求。
本模型卡片中的任何内容均不应被解释为修改或限制模型发布所依据的 MIT 许可证条款。
如有任何问题,请通过 longcat-team@meituan.com 与我们联系,或加入我们的微信群。
