• SwissArmyTransformer瑞士军刀工具箱使用手册


    Introduction sat(SwissArmyTransformer)是一个灵活而强大的库,用于开发您自己的Transformer变体。
    sat是以“瑞士军刀”命名的,这意味着所有型号(例如BERT、GPT、T5、GLM、CogView、ViT…)共享相同的backone代码,并通过一些超轻量级的mixin满足多种用途。
    sat由deepspeed ZeRO和模型并行性提供支持,旨在为大模型(100M\~20B参数)的预训练和微调提供最佳实践。

    从 SwissArmyTransformer 0.2.x 迁移到 0.3.x

    1. 导入时将包名称从 SwissArmyTransformer 更改为 sat,例如从 sat 导入 get_args。
    2. 删除脚本中的所有--sandwich-ln,使用layernorm-order='sandwich'。
    3. 更改顺序 from_pretrained(args, name) => from_pretrained(name, args)。
    4. 我们可以直接使用 from sat.model import AutoModel;model, args = AutoModel.from_pretrained('roberta-base') 以 仅模型模式 加载模型,而不是先初始化 sat。

    安装

    pip install SwissArmyTransformer

    特征

    添加与模型无关的组件,例如前缀调整,只需一行!

    前缀调整(或 P 调整)通过在每个注意力层中添加可训练参数来改进微调。使用我们的库可以轻松地将其应用于 GLM 分类(或任何其他)模型。

    1. class ClassificationModel(GLMModel): # can also be BertModel, RobertaModel, etc.
    2. def __init__(self, args, transformer=None, **kwargs):
    3. super().__init__(args, transformer=transformer, **kwargs)
    4. self.add_mixin('classification_head', MLPHeadMixin(args.hidden_size, 2048, 1))
    5. # Arm an arbitrary model with Prefix-tuning with this line!
    6. self.add_mixin('prefix-tuning', PrefixTuningMixin(args.num_layers, args.hidden_size // args.num_attention_heads, args.num_attention_heads, args.prefix_len))

    GPT 和其他自回归模型在训练和推理过程中的行为有所不同。在推理过程中,文本是逐个令牌生成的,我们需要缓存以前的状态以提高效率。使用我们的库,您只需要考虑训练期间的行为(教师强制),并通过添加 mixin 将其转换为缓存的自回归模型:

    1. model, args = AutoModel.from_pretrained('glm-10b-chinese', args)
    2. model.add_mixin('auto-regressive', CachedAutoregressiveMixin())
    3. # Generate a sequence with beam search
    4. from sat.generation.autoregressive_sampling import filling_sequence
    5. from sat.generation.sampling_strategies import BeamSearchStrategy
    6. output, *mems = filling_sequence(model, input_seq,
    7. batch_size=args.batch_size,
    8. strategy=BeamSearchStrategy(args.batch_size))

    使用最少的代码构建基于 Transformer 的模型。我们提到了 GLM,它与标准转换器(称为 BaseModel)仅在位置嵌入(和训练损失)上有所不同。我们在编码的时候只需要关注相关的部分就可以了。

    扩展整个定义:

    1. class BlockPositionEmbeddingMixin(BaseMixin):
    2. # Here define parameters for the mixin
    3. def __init__(self, max_sequence_length, hidden_size, init_method_std=0.02):
    4. super(BlockPositionEmbeddingMixin, self).__init__()
    5. self.max_sequence_length = max_sequence_length
    6. self.hidden_size = hidden_size
    7. self.block_position_embeddings = torch.nn.Embedding(max_sequence_length, hidden_size)
    8. torch.nn.init.normal_(self.block_position_embeddings.weight, mean=0.0, std=init_method_std)
    9. # Here define the method for the mixin
    10. def position_embedding_forward(self, position_ids, **kwargs):
    11. position_ids, block_position_ids = position_ids[:, 0], position_ids[:, 1]
    12. position_embeddings = self.transformer.position_embeddings(position_ids)
    13. block_position_embeddings = self.block_position_embeddings(block_position_ids)
    14. return position_embeddings + block_position_embeddings
    15. class GLMModel(BaseModel):
    16. def __init__(self, args, transformer=None, parallel_output=True):
    17. super().__init__(args, transformer=transformer, parallel_output=parallel_output)
    18. self.add_mixin('block_position_embedding',
    19. BlockPositionEmbeddingMixin(args.max_sequence_length, args.hidden_size)
    20. ) # Add the mixin for GLM

    全方位的培训支持。 sat 旨在提供预训练和微调的最佳实践,您只需要完成forward_step 和 create_dataset_function,但可以使用超参数来更改有用的训练配置。
    通过指定 --num_nodes、--num_gpus 和一个简单的主机文件,将训练扩展到多个 GPU 或节点。
    DeepSpeed 和模型并行性。
    ZeRO-2 和激活检查点的更好集成。
    自动扩展和改组训练数据和内存映射。
    成功支持CogView2和CogVideo的训练。
    目前唯一支持在 GPU 上微调 T5-10B 的开源代码库。

    快速浏览

    在 sat 中使用 Bert(用于推理)的最典型的 python 文件如下:

    1. # @File: inference_bert.py
    2. from sat import get_args, get_tokenizer, AutoModel
    3. # Parse args, initialize the environment. This is necessary.
    4. args = get_args()
    5. # Automatically download and load model. Will also dump model-related hyperparameters to args.
    6. model, args = AutoModel.from_pretrained('bert-base-uncased', args)
    7. # Get the BertTokenizer according to args.tokenizer_type (automatically set).
    8. tokenizer = get_tokenizer(args)
    9. # Here to use bert as you want!
    10. # ...

    然后我们可以通过以下方式运行代码

    SAT_HOME=/path/to/download python inference_bert.py --mode inference

    所有官方支持的模型名称都在 urls.py 中。

    1. # @File: finetune_bert.py
    2. from sat import get_args, get_tokenizer, AutoModel
    3. from sat.model.mixins import MLPHeadMixin
    4. def create_dataset_function(path, args):
    5. # Here to load the dataset
    6. # ...
    7. assert isinstance(dataset, torch.utils.data.Dataset)
    8. return dataset
    9. def forward_step(data_iterator, model, args, timers):
    10. inputs = next(data_iterator) # from the dataset of create_dataset_function.
    11. loss, *others = model(inputs)
    12. return loss
    13. # Parse args, initialize the environment. This is necessary.
    14. args = get_args()
    15. model, args = AutoModel.from_pretrained('bert-base-uncased', args)
    16. tokenizer = get_tokenizer(args)
    17. # Here to use bert as you want!
    18. model.del_mixin('bert-final')
    19. model.add_mixin('classification_head', MLPHeadMixin(args.hidden_size, 2048, 1))
    20. # ONE LINE to train!
    21. # args already includes hyperparams such as lr, train-iters, zero-stage ...
    22. training_main(args,
    23. model_cls=model,
    24. forward_step_function=forward_step, # user define
    25. create_dataset_function=create_dataset_function # user define
    26. )

    然后我们可以通过以下方式运行代码

    1. deepspeed --include localhost:0,1 finetune_bert.py \
    2. --experiment-name ftbert \
    3. --mode finetune --train-iters 1000 --save /path/to/save \
    4. --train-data /path/to/train --valid-data /path/to/valid \
    5. --lr 0.00002 --batch-size 8 --zero-stage 1 --fp16

    这里我们在 GPU 0,1 上使用数据并行。我们还可以通过 --hostfile/path/to/hostfile 在许多互连的机器上启动训练。请参阅教程了解更多详细信息。
    要编写自己的模型,您只需要考虑与标准 Transformer 的差异。例如,如果你有一个改进注意力操作的想法:

    1. from sat.model import BaseMixin
    2. class MyAttention(BaseMixin):
    3. def __init__(self, hidden_size):
    4. super(MyAttention, self).__init__()
    5. # MyAttention may needs some new params, e.g. a learnable alpha.
    6. self.learnable_alpha = torch.nn.Parameter(torch.ones(hidden_size))
    7. # This is a hook function, the name `attention_fn` is special.
    8. def attention_fn(q, k, v, mask, dropout=None, **kwargs):
    9. # Code for my attention.
    10. # ...
    11. return attention_results

    这里的attention_fn是一个钩子函数,用新函数替换默认动作。所有可用的钩子都在transformer_defaults.py中。现在我们可以使用 add_mixin 将更改应用到所有转换器,例如 BERT、Vit 和 CogView。请参阅教程了解更多详细信息。

    教程

    • How to use pretrained models collected in sat?
    • Why and how to train models in sat?

    Citation

    Currently we don't have a paper, so you don't need to formally cite us!~

    If this project helps your research or engineering, use \footnote{https://github.com/THUDM/SwissArmyTransformer} to mention us and recommend SwissArmyTransformer to others.

    The tutorial for contributing sat is on the way!

    The project is based on (a user of) DeepSpeed, Megatron-LM and Huggingface transformers. Thanks for their awesome work.

    训练指导

    The Training API

    我们提供了一个简单但功能强大的训练APItraining_main(),它不仅限于我们的Transformer模型,还适用于任何torch.nn.Module

    1. from sat import get_args, training_main
    2. from sat.model import AutoModel, BaseModel
    3. args = get_args()
    4. # to pretrain from scratch, give a class obj
    5. model = BaseModel
    6. # to finetuned from a given model, give a torch.nn.Module
    7. model = AutoModel.from_pretrained('bert-base-uncased', args)
    8. training_main(args,
    9. model_cls=model,
    10. forward_step_function=forward_step,
    11. create_dataset_function=dataset_func,
    12. handle_metrics_function=None,
    13. init_function=None
    14. )

    以上是使用 sat 的标准训练计划的(不完整)示例。 Training_main 接受 5 个参数:(必需)model_cls:继承 torch.nn.Module 的类型对象,或我们训练的 torch.nn.Module 对象。
    (必需)forward_step_function:一个自定义函数,输入 data_iterator、model、args、timers、returns loss、{'metric0': m0, ...}。
    (必填)create_dataset_function:返回一个torch.utils.data.Dataset用于加载。我们的库会自动将数据分配给多个worker,并将数据迭代器交给forward_step_function。
    (可选)handle_metrics_function:在评估过程中处理特殊指标。
    (可选)init_function:在训练之前更改模型的钩子,对于继续训练很有用。
    有关完整示例,请参阅 Finetune BERT 示例。

  • 相关阅读:
    基于数组寻址的SCL算法实现S7-1200_1500中的配方功能Recipe
    flink自定义窗口分配器
    7个自定义定时任务并发送消息至邮箱或企业微信案例(crontab和at)
    Apple - Core Image
    ZooKeeper 7:数据读写——原子广播协议ZAB
    生命在于折腾——皮卡丘靶场源码审计(二)
    字符串
    idea相关配置-----java
    “益路同行”栏目专访第11期——柳州市雨花敬老服务中心陈勇梅
    计算机毕业设计Java计算机培训管理(源码+系统+mysql数据库+lw文档)
  • 原文地址:https://blog.csdn.net/qq_33440910/article/details/134248035