🚀 LSG模型
LSG模型基于BART-large
进行了调整,适用于编码器 - 解码器任务,无需额外的预训练。它能够处理长序列,并且比来自模型中心的Longformer (LED) 或BigBird (Pegasus) 更快、更高效,依赖于局部 + 稀疏 + 全局注意力 (LSG) 机制。
⚠️ 重要提示
此模型依赖于自定义建模文件,需要添加trust_remote_code=True
才能使用。
此模型需要Transformers >= 4.36.1
。
请参考 #13467。
🚀 快速开始
此模型依赖于自定义建模文件,使用时需要添加trust_remote_code=True
。
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained("ccdv/lsg-bart-large-4096", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-bart-large-4096")
✨ 主要特性
- 该模型改编自 BART-large,用于编码器 - 解码器任务,无需额外预训练,使用相同数量的参数/层和相同的分词器。
- 能够处理长序列,并且比Longformer (LED) 或BigBird (Pegasus) 更快、更高效,依赖于局部 + 稀疏 + 全局注意力 (LSG)。
- 模型要求序列长度是块大小的倍数,若需要可自动填充序列(配置中
adaptive=True
),不过建议使用分词器截断输入(truncation=True
),并可选择按块大小的倍数进行填充(pad_to_multiple_of=...
)。
📦 安装指南
此模型依赖于自定义建模文件,使用时需要添加trust_remote_code=True
,同时需要Transformers >= 4.36.1
。
💻 使用示例
基础用法
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained("ccdv/lsg-bart-large-4096", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-bart-large-4096")
高级用法
from transformers import AutoModel
model = AutoModel.from_pretrained("ccdv/lsg-bart-large-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
)
Seq2Seq摘要任务示例
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
model = AutoModelForSeq2SeqLM.from_pretrained("ccdv/lsg-bart-large-4096",
trust_remote_code=True,
pass_global_tokens_to_decoder=True,
)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-bart-large-4096")
SENTENCE = "This is a test sequence to test the model. " * 300
token_ids = tokenizer(
SENTENCE,
return_tensors="pt",
truncation=True
)
output = model(**token_ids)
分类任务示例
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained("ccdv/lsg-bart-large-4096",
trust_remote_code=True,
pass_global_tokens_to_decoder=True,
)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-bart-large-4096")
SENTENCE = "This is a test sequence to test the model. " * 300
token_ids = tokenizer(
SENTENCE,
return_tensors="pt",
padding="max_length",
truncation=True
)
output = model(**token_ids)
> SequenceClassifierOutput(loss=None, logits=tensor([[-0.3051, -0.1762]], grad_fn=<AddmmBackward>), hidden_states=None, attentions=None)
🔧 技术细节
参数设置
可以更改各种参数,例如:
- 全局令牌数量 (
num_global_tokens=1
)
- 局部块大小 (
block_size=128
)
- 稀疏块大小 (
sparse_block_size=128
)
- 稀疏因子 (
sparsity_factor=2
)
- 掩码第一个令牌 (
mask first token since it is redundant with the first global token
)
- 更多参数可查看
config.json
文件
默认参数在实践中效果良好。如果内存不足,可以减小块大小、增加稀疏因子并去除注意力分数矩阵中的丢弃率。
稀疏选择类型
有6种不同的稀疏选择模式,最佳类型取决于具体任务。
- 若
sparse_block_size=0
或sparsity_type="none"
,则仅考虑局部注意力。
- 注意,对于长度 < 2 * 块大小的序列,稀疏选择类型没有影响。
稀疏选择类型 |
描述 |
适用稀疏因子 |
附加参数 |
sparsity_type="bos_pooling" (新) |
使用BOS令牌进行加权平均池化 |
通常较大 (8, 16, 32) |
无 |
sparsity_type="norm" |
选择范数最高的令牌 |
较小 (2 到 4) |
无 |
sparsity_type="pooling" |
使用平均池化合并令牌 |
较小 (2 到 4) |
无 |
sparsity_type="lsh" |
使用LSH算法对相似令牌进行聚类 |
较大 (4+) |
lsg_num_pre_rounds=1 (在计算质心之前合并令牌n次) |
sparsity_type="stride" |
每个头使用按稀疏因子跨步的不同令牌 |
不建议sparsify_factor > num_heads |
无 |
sparsity_type="block_stride" |
每个头使用按稀疏因子跨步的令牌块 |
不建议sparsify_factor > num_heads |
无 |
📚 详细文档
- LSG ArXiv 论文。
- Github/转换脚本可在这个 链接 获取。
📄 许可证
BART引用
@article{DBLP:journals/corr/abs-1910-13461,
author = {Mike Lewis and
Yinhan Liu and
Naman Goyal and
Marjan Ghazvininejad and
Abdelrahman Mohamed and
Omer Levy and
Veselin Stoyanov and
Luke Zettlemoyer},
title = {{BART:} Denoising Sequence-to-Sequence Pre-training for Natural Language
Generation, Translation, and Comprehension},
journal = {CoRR},
volume = {abs/1910.13461},
year = {2019},
url = {http://arxiv.org/abs/1910.13461},
eprinttype = {arXiv},
eprint = {1910.13461},
timestamp = {Thu, 31 Oct 2019 14:02:26 +0100},
biburl = {https://dblp.org/rec/journals/corr/abs-1910-13461.bib},
bibsource = {dblp computer science bibliography, https://dblp.org}
}