语言: fr
标签:
- camembert
- 长上下文
pipeline_tag: fill-mask
LSG 模型
Transformers >= 4.36.1
此模型依赖自定义建模文件,需添加 trust_remote_code=True
参见 #13467
LSG 论文发布于 ArXiv。
GitHub/转换脚本可在此 链接 获取。
此模型基于 CamemBERT-base 调整而来,尚未进行额外预训练。它使用相同数量的参数/层及相同的分词器。
该模型能高效处理长序列,速度与效率优于 Longformer 或 BigBird(来自 Transformers),并依赖局部+稀疏+全局注意力机制(LSG)。
模型要求序列长度为块大小的整数倍。具备“自适应”功能,可自动填充序列(配置中 adaptive=True)。但建议通过分词器截断输入(truncation=True),并可选按块大小倍数填充(pad_to_multiple_of=...)。
支持编码器-解码器结构,但未全面测试。
基于 PyTorch 实现。

使用
模型依赖自定义建模文件,使用时需添加 trust_remote_code=True。
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained("ccdv/lsg-camembert-base-4096", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-camembert-base-4096")
参数
可调整以下参数:
- 全局标记数(num_global_tokens=1)
- 局部块大小(block_size=128)
- 稀疏块大小(sparse_block_size=128)
- 稀疏因子(sparsity_factor=2)
- 屏蔽首标记(因与首个全局标记冗余,mask_first_token=True)
- 详见 config.json 文件
默认参数实际表现良好。若内存不足,可减小块大小、增加稀疏因子并移除注意力得分矩阵中的 dropout。
from transformers import AutoModel
model = AutoModel.from_pretrained("ccdv/lsg-camembert-base-4096",
trust_remote_code=True,
num_global_tokens=16,
block_size=64,
sparse_block_size=64,
attention_probs_dropout_prob=0.0,
sparsity_factor=4,
sparsity_type="none",
mask_first_token=True
)
稀疏选择类型
共6种稀疏选择模式,最佳类型因任务而异。
若 sparse_block_size=0
或 sparsity_type="none"
,则仅考虑局部注意力。
注意:序列长度 < 2*block_size 时,类型无效。
sparsity_type="bos_pooling"
(新)
- 使用 BOS 标记加权平均池化
- 通常表现最佳,尤其配合较大稀疏因子(8, 16, 32)
- 附加参数:无
sparsity_type="norm"
,选择高范数标记
sparsity_type="pooling"
,通过平均池化合并标记
sparsity_type="lsh"
,使用 LSH 算法聚类相似标记
- 适合大稀疏因子(4+)
- LSH 依赖随机投影,不同种子可能导致推理差异
- 附加参数:
- lsg_num_pre_rounds=1,计算质心前预合并标记n次
sparsity_type="stride"
,每头采用步进机制
- 每头按稀疏因子步进选择不同标记
- 稀疏因子 > 头数时不推荐
sparsity_type="block_stride"
,每头采用块步进机制
- 每头按稀疏因子步进选择块标记
- 稀疏因子 > 头数时不推荐
任务
填充掩码示例:
from transformers import FillMaskPipeline, AutoModelForMaskedLM, AutoTokenizer
model = AutoModelForMaskedLM.from_pretrained("ccdv/lsg-camembert-base-4096", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-camembert-base-4096")
SENTENCES = "Paris est la <mask> de la France."
pipeline = FillMaskPipeline(model, tokenizer)
output = pipeline(SENTENCES)
> 'Paris est la capitale de la France.'
分类示例:
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained("ccdv/lsg-camembert-base-4096",
trust_remote_code=True,
pool_with_global=True,
)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-camembert-base-4096")
SENTENCE = "This is a test for sequence classification. " * 300
token_ids = tokenizer(
SENTENCE,
return_tensors="pt",
truncation=True
)
output = model(**token_ids)
> SequenceClassifierOutput(loss=None, logits=tensor([[-0.3051, -0.1762]], grad_fn=<AddmmBackward>), hidden_states=None, attentions=None)
训练全局标记
仅训练全局标记和分类头:
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained("ccdv/lsg-camembert-base-4096",
trust_remote_code=True,
pool_with_global=True,
num_global_tokens=16
)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-camembert-base-4096")
for name, param in model.named_parameters():
if "global_embeddings" not in name:
param.requires_grad = False
else:
param.required_grad = True
CamemBERT
@inproceedings{Martin_2020,
doi = {10.18653/v1/2020.acl-main.645},
url = {https://doi.org/10.18653%2Fv1%2F2020.acl-main.645},
year = 2020,
publisher = {Association for Computational Linguistics},
author = {Louis Martin and Benjamin Muller and Pedro Javier Ortiz Su{\'{a}}rez and Yoann Dupont and Laurent Romary and {\'{E}}ric de la Clergeri and Djam{\'{e}} Seddah and Beno{\^{\i}}t Sagot},
title = {{CamemBERT}: a Tasty French Language Model},
booktitle = {Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics}
}