The goal of the MindSpore Transformers suite is to build a comprehensive development kit for large model training, fine-tuning, evaluation, inference, and deployment: It provides mainstream pre-trained Transformer models and state-of-the-art downstream task applications in the industry, covering rich parallel features. It aims to help users easily achieve large model training and innovative research and development.
Based on MindSpore's built-in parallel technology and modular design, the MindSpore Transformers suite has the following features:
If you have any suggestions for MindSpore Transformers, please contact us through issues, and we will handle them promptly.
The currently supported model list is as follows:
| Model | Task (task name) | Model (model name) |
|---|---|---|
| LLama2 | text_generation | llama2_7b <br>llama2_13b <br>llama2_7b_lora <br>llama2_13b_lora <br>llama2_70b |
| GLM2 | text_generation | glm2_6b<br>glm2_6b_lora |
| CodeGeex2 | text_generation | codegeex2_6b |
| LLama | text_generation | llama_7b <br>llama_13b <br>llama_7b_lora |
| GLM | text_generation | glm_6b<br>glm_6b_lora |
| Bloom | text_generation | bloom_560m<br>bloom_7.1b <br> |
| GPT2 | text_generation | gpt2_small <br>gpt2_13b <br> |
| PanGuAlpha | text_generation | pangualpha_2_6_b<br>pangualpha_13b |
| BLIP2 | contrastive_language_image_pretrain<br> zero_shot_image_classification | blip2_stage1_vit_g |
| CLIP | contrastive_language_image_pretrain<br> zero_shot_image_classification | clip_vit_b_32<br>clip_vit_b_16 <br>clip_vit_l_14<br>clip_vit_l_14@336 |
| BERT | masked_language_modeling<br>text_classification <br>token_classification <br>question_answering | bert_base_uncased <br>txtcls_bert_base_uncased<br>txtcls_bert_base_uncased_mnli <br>tokcls_bert_base_chinese<br>tokcls_bert_base_chinese_cluener <br>qa_bert_base_uncased<br>qa_bert_base_chinese_uncased |
| T5 | translation | t5_small |
| sam | segment_anything | sam_vit_b <br>sam_vit_l <br>sam_vit_h |
| MAE | masked_image_modeling | mae_vit_base_p16 |
| VIT | image_classification | vit_base_p16 |
| Swin | image_classification | swin_base_p4w7 |
The currently supported model list in research is as follows:
| Model | Task (task name) | Model (model name) |
|---|---|---|
| skywork | text_generation | skywork_13b |
| Baichuan2 | text_generation | baichuan2_7b <br>baichuan2_13b <br>baichuan2_7b_lora <br>baichuan2_13b_lora |
| Baichuan | text_generation | baichuan_7b <br>baichuan_13b |
| Qwen | text_generation | qwen_7b |
| Wizardcoder | text_generation | wizardcoder_15b |
| Internlm | text_generation | Internlm_7b |
| ziya | text_generation | ziya_13b |
Source code compilation installation is supported. Users can execute the following commands to install the package
git clone -b dev https://gitee.com/mindspore/mindformers.git cd mindformers bash build.sh
Docker download command
docker pull swr.cn-central-221.ovaijisuan.com/mindformers/mindformers0.8.0_mindspore2.2.0:aarch_20231025
Create container
# --device is used to control the NPU card number and range of the specified container # -v is used to map directories outside the container # --name is used to customize the container name docker run -it -u root \ --ipc=host \ --network host \ --device=/dev/davinci0 \ --device=/dev/davinci1 \ --device=/dev/davinci2 \ --device=/dev/davinci3 \ --device=/dev/davinci4 \ --device=/dev/davinci5 \ --device=/dev/davinci6 \ --device=/dev/davinci7 \ --device=/dev/davinci_manager \ --device=/dev/devmm_svm \ --device=/dev/hisi_hdc \ -v /etc/localtime:/etc/localtime \ -v /usr/local/Ascend/driver:/usr/local/Ascend/driver \ -v /var/log/npu/:/usr/slog \ -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \ --name {请手动输入容器名称} \ swr.cn-central-221.ovaijisuan.com/mindformers/mindformers0.8.0_mindspore2.2.0:aarch_20231025 \ /bin/bash
| 版本对应关系 | MindFormers | MindPet | MindSpore | Python | 芯片 | 备注 |
|---|---|---|---|---|---|---|
| 版本号 | dev | 1.0.2 | 2.2 | 3.9 | Ascend 910A/B | 开发分支(非稳定版本) |
| 版本号 | 0.8 | 1.0.2 | 2.2 | 3.9 | Ascend 910A/B | 发布版本分支 |
MindFormers套件提供两种使用和开发方式,为开发者提供灵活且简洁的使用方式和高阶开发接口。
用户可以直接克隆整个仓库,按照以下步骤即可运行套件中已支持的任意configs模型任务配置文件,方便用户快速使用和开发:
准备工作
git clone -b dev https://gitee.com/mindspore/mindformers.git cd mindformers
步骤2:准备相应任务的数据集,请参考docs目录下各模型的README.md文档准备相应数据集
步骤3:修改配置文件configs/{model_name}/run_{model_name}_***.yaml中数据集路径
步骤4:如果要使用分布式训练,则需提前生成RANK_TABLE_FILE
# 不包含8本身,生成0~7卡的hccl json文件 python mindformers/tools/hccl_tools.py --device_num [0,8)
单卡启动:统一接口启动,根据模型CONFIG完成任意模型的单卡训练、微调、评估、推理流程
# 训练启动,run_status支持train、finetuen、eval、predict四个关键字,以分别完成模型训练、评估、推理功能,默认使用配置文件中的run_mode python run_mindformer.py --config {CONFIG_PATH} --run_mode {train/finetune/eval/predict}
# 8卡分布式运行,DEVICE_RANGE = [0,8),不包含8本身 cd scripts bash run_distribute.sh RANK_TABLE_FILE CONFIG_PATH DEVICE_RANGE RUN_MODE
RANK_TABLE_FILE: 由mindformers/tools/hccl_tools.py生成的分布式json文件 CONFIG_PATH: 为configs文件夹下面的{model_name}/run_*.yaml配置文件 DEVICE_ID: 为设备卡,范围为0~7 DEVICE_RANGE: 为单机分布式卡的范围,如[0,8]为8卡分布式,不包含8本身 RUN_MODE: 为任务运行状态,支持关键字train\finetune\eval\predict\export
详细高阶API使用教程请参考:MindFormers大模型使用教程
准备工作
具体安装请参考第二章。
准备相应任务的数据集,请参考docs目录下各模型的README.md文档准备相应数据集。
Trainer快速入门
用户可以通过以上方式安装mindformers库,然后利用Trainer高阶接口执行模型任务的训练、微调、评估、推理功能。
用户可使用Trainer.train或者Trainer.finetune接口完成模型的训练/微调/断点续训。
import mindspore; mindspore.set_context(mode=0, device_id=0) from mindformers import Trainer cls_trainer = Trainer(task='image_classification', # 已支持的任务名 model='vit_base_p16', # 已支持的模型名 train_dataset="/data/imageNet-1k/train", # 传入标准的训练数据集路径,默认支持ImageNet数据集格式 eval_dataset="/data/imageNet-1k/val") # 传入标准的评估数据集路径,默认支持ImageNet数据集格式 # 示例1:开启训练复现流程 cls_trainer.train() # 示例2:加载集成的mae权重,开启微调流程 cls_trainer.finetune(finetune_checkpoint='mae_vit_base_p16') # 示例3:开启断点续训功能 cls_trainer.train(train_checkpoint=True, resume_training=True)
用户可使用Trainer.evaluate接口完成模型的评估流程。
import mindspore; mindspore.set_context(mode=0, device_id=0) from mindformers import Trainer cls_trainer = Trainer(task='image_classification', # 已支持的任务名 model='vit_base_p16', # 已支持的模型名 eval_dataset="/data/imageNet-1k/val") # 传入标准的评估数据集路径,默认支持ImageNet数据集格式 # 示例1:开启评估已集成模型权重的复现流程 cls_trainer.evaluate() # 示例2:开启评估训练得到的最后一个权重 cls_trainer.evaluate(eval_checkpoint=True) # 示例3:开启评估指定的模型权重 cls_trainer.evaluate(eval_checkpoint='./output/checkpoint/rank_0/mindformers.ckpt')
结果打印示例(已集成的vit_base_p16模型权重评估分数): Top1 Accuracy=0.8317
用户可使用Trainer.predict接口完成模型的推理流程。
import mindspore; mindspore.set_context(mode=0, device_id=0) from mindformers import Trainer
cls_trainer = Trainer(task='image_classification', # 已支持的任务名 model='vit_base_p16') # 已支持的模型名 input_data = './cat.png' # 一张猫的图片
predict_result_d = cls_trainer.predict(input_data=input_data)
predict_result_b = cls_trainer.predict(input_data=input_data, predict_checkpoint=True)
predict_result_c = cls_trainer.predict(input_data=input_data, predict_checkpoint='./output/checkpoint/rank_0/mindformers.ckpt') print(predict_result_d)
```text
结果打印示例(已集成的vit_base_p16模型权重推理结果):
{'label': 'cat', score: 0.99}
MindFormers套件为用户提供了已集成模型的pipeline推理接口,方便用户体验大模型推理服务。
# 以gpt2 small为例 import mindspore; mindspore.set_context(mode=0, device_id=0) from mindformers.pipeline import pipeline pipeline_task = pipeline(task="text_generation", model="gpt2") pipeline_result = pipeline_task("An increasing sequence: one,", do_sample=False, max_length=20) print(pipeline_result)
结果打印示例(已集成的gpt2模型权重推理结果): [{'text_generation_text': ['An increasing sequence: one, two, three, four, five, six, seven, eight,']}]
MindFormers套件为用户提供了高阶AutoClass类,包含AutoConfig、AutoModel、AutoProcessor、AutoTokenizer四类,方便开 发者进行调用。
from mindformers import AutoConfig # 获取gpt2的模型配置 gpt2_config = AutoConfig.from_pretrained('gpt2') # 获取vit_base_p16的模型配置 vit_base_p16_config = AutoConfig.from_pretrained('vit_base_p16')
from mindformers import AutoModel # 利用from_pretrained功能实现模型的实例化(默认加载对应权重) gpt2 = AutoModel.from_pretrained('gpt2') # 利用from_config功能实现模型的实例化(默认加载对应权重) gpt2_config = AutoConfig.from_pretrained('gpt2') gpt2 = AutoModel.from_config(gpt2_config) # 利用save_pretrained功能保存模型对应配置 gpt2.save_pretrained('./gpt2', save_name='gpt2')
from mindformers import AutoProcessor # 通过模型名关键字获取对应模型预处理过程(实例化gpt2的预处理过程,通常用于Trainer/pipeline推理入参) gpt2_processor_a = AutoProcessor.from_pretrained('gpt2') # 通过yaml文件获取相应的预处理过程 gpt2_processor_b = AutoProcessor.from_pretrained('configs/gpt2/run_gpt2.yaml')
from mindformers import AutoTokenizer # 通过模型名关键字获取对应模型预处理过程(实例化gpt2的tokenizer,通常用于Trainer/pipeline推理入参) gpt2_tokenizer = AutoTokenizer.from_pretrained('gpt2')
五、贡献
欢迎参与社区贡献,可参考MindSpore贡献要求贡献者Wiki。
六、许可证


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率, 让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频


实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。


选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


最强AI数据分析助手
小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。


像人一样思考的AI智能体
imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。


AI数字人视频创作平台
Keevx 一款开箱即用的AI数字人视频创作平台,广泛适用于电商广告、企业培训与社媒宣传,让全球企业与个人创作者无需拍摄剪辑,就能快速生成多语言、高质量的专业视频。
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号