LFM2-1.2B 多语言开源边缘高效混合模型解析
Liquid AI 发布的 LFM2-1.2B 是面向边缘设备的开源混合模型,支持多语言且内存效率高。文章涵盖模型架构、参数配置、聊天模板及工具使用流程,提供基于 Transformers 的 Python 运行与微调代码示例,并展示了在 MMLU、GSM8K 等基准测试中与 Qwen、Llama 等模型的对比数据,适合代理任务及对话场景应用。

Liquid AI 发布的 LFM2-1.2B 是面向边缘设备的开源混合模型,支持多语言且内存效率高。文章涵盖模型架构、参数配置、聊天模板及工具使用流程,提供基于 Transformers 的 Python 运行与微调代码示例,并展示了在 MMLU、GSM8K 等基准测试中与 Qwen、Llama 等模型的对比数据,适合代理任务及对话场景应用。


微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online
LFM2 是由 Liquid AI 开发的新一代混合模型,专为边缘人工智能和设备端部署而设计。它在质量、速度和内存效率方面树立了新的标准。此次公开了三个经过后训练的检查点权重,分别包含 3.5 亿、7 亿和 12 亿参数。这些模型具备以下关键特性,以助力创建基于人工智能的边缘应用:
鉴于其较小的模型尺寸,建议对 LFM2 模型进行针对特定用例的微调,以实现性能最大化。它们特别适合用于代理任务、数据提取、RAG(检索增强生成)、创意写作和多轮对话等场景。不过,不推荐将它们用于知识密集型任务或需要编程技能的任务。
| 属性 | 值 |
|---|---|
| 参数数量 | 1,170,340,608 |
| 层数 | 16(10 个卷积层 +6 个注意力层) |
| 上下文长度 | 32,768 个标记 |
| 词汇表大小 | 65,536 |
| 精度 | bfloat16 |
| 训练预算 | 10 万亿个标记 |
LFM2 采用 LFM 开放许可证 v1.0。
支持英语、阿拉伯语、中文、法语、德语、日语、韩语和西班牙语。
推荐使用以下参数:
LFM2 采用类似 ChatML 的聊天模板,示例如下:
<|startoftext|><|im_start|>system You are a helpful assistant trained by Liquid AI.<|im_end|> <|im_start|>user What is C. elegans?<|im_end|> <|im_start|>assistant It's a tiny nematode that lives in temperate soil environments.<|im_end|>
可使用 Hugging Face transformers 中的 apply_chat_template() 函数应用该模板。
工具使用包含四个主要步骤:
<|tool_list_start|> 和 <|tool_list_end|> 特殊标记之间的 JSON 对象),通常在系统提示中。<|tool_call_start|> 和 <|tool_call_end|> 特殊标记之间的 Python 列表),作为助手的回答。<|tool_response_start|> 和 <|tool_response_end|> 特殊标记之间的字符串),作为'工具'角色。以下是使用工具的对话示例:
<|startoftext|><|im_start|>system List of tools: <|tool_list_start|>[{"name": "get_candidate_status", "description": "Retrieve candidate status"}]<|tool_list_end|><|im_start|>user What is the current status of candidate ID 12345?<|im_end|> <|im_start|>assistant <|tool_call_start|>[get_candidate_status(candidate_id="12345")]<|tool_call_end|>Checking the response...<|im_start|>tool <|tool_response_start|>{"candidate_id": "12345", "status": "Interview Scheduled", "position": "Software Engineer"}<|tool_response_end|> <|im_start|>assistant The candidate with ID 12345 is currently in the "Interview Scheduled" stage for the position.
LFM2 是一种混合模型,采用乘法门控和短卷积结构,包含 10 个双门控短程 LIV 卷积块和 6 个分组查询注意力(GQA)块。
预训练数据大约包含 75% 的英语数据、20% 的多语言数据和 5% 的代码数据,这些数据来源于网络和授权材料。
要运行 LFM2,需要从源代码安装 Hugging Face transformers(v4.54.0.dev0)。可以通过以下命令进行更新或安装:
pip install "transformers @ git+https://github.com/huggingface/transformers.git@main"
以下是使用 transformers 在 Python 中生成答案的示例代码:
from transformers import AutoModelForCausalLM, AutoTokenizer
# 加载模型和分词器
model_id = "LiquidAI/LFM2-1.2B"
model = AutoModelForCausalLM.from_pretrained(
model_id,
device_map="auto",
torch_dtype="bfloat16",
trust_remote_code=True
# attn_implementation="flash_attention_2" <- 在兼容的 GPU 上取消注释
)
tokenizer = AutoTokenizer.from_pretrained(model_id)
# 生成答案
prompt = "What is C. elegans?"
input_ids = tokenizer.apply_chat_template(
[{"role":"user","content": prompt}],
add_generation_prompt=True,
return_tensors="pt",
tokenize=True
).to(model.device)
output = model.generate(
input_ids,
do_sample=True,
temperature=0.3,
min_p=0.15,
repetition_penalty=1.05,
max_new_tokens=512
)
print(tokenizer.decode(output[0], skip_special_tokens=False))
输出示例:
<|startoftext|><|im_start|>user What is C. elegans?<|im_end|> <|im_start|>assistant C. elegans, also known as Caenorhabditis elegans, is a small, free-living nematode worm (roundworm) that belongs to the phylum Nematoda.
还可以直接使用提供的 Colab 笔记本运行和测试模型。
建议针对具体用例对 LFM2 模型进行微调,以提升性能。以下是相关的笔记本资源:
LFM2 在不同评估类别中超越了同尺寸的其他模型。
在多个基准测试中,LFM2-1.2B、LFM2-700M 和 LFM2-350M 等模型与其他模型的性能对比如下:
| 模型 | MMLU | GPQA | IFEval | IFBench | GSM8K | MGSM | MMMLU |
|---|---|---|---|---|---|---|---|
| LFM2-350M | 43.43 | 27.46 | 65.12 | 16.41 | 30.1 | 29.52 | 37.99 |
| LFM2-700M | 49.9 | 28.48 | 72.23 | 20.56 | 46.4 | 45.36 | 43.28 |
| LFM2-1.2B | 55.23 | 31.47 | 74.89 | 20.7 | 58.3 | 55.04 | 46.73 |
| Qwen3-0.6B | 44.93 | 22.14 | 64.24 | 19.75 | 36.47 | 41.28 | 30.84 |
| Qwen3-1.7B | 59.11 | 27.72 | 73.98 | 21.27 | 51.4 | 66.56 | 46.51 |
| Llama-3.2-1B-Instruct | 46.6 | 28.84 | 52.39 | 16.86 | 35.71 | 29.12 | 38.15 |
| gemma-3-1b-it | 40.08 | 21.07 | 62.9 | 17.72 | 59.59 | 43.6 | 34.43 |
