Package: pai-easynlp
EasyNLP is a Comprehensive and Easy-to-use NLP ToolkitEasyNLPEasyNLP is an easy-to-use NLP development and application toolkit in PyTorch, first released inside Alibaba in 2021. It is built with scalable distributed training strategies and supports a comprehensive suite of NLP algorithms for various NLP applications. EasyNLP integrates knowledge distillation and few-shot learning for landing large pre-trained models and provides a unified framework of model training, inference, and deployment for real-world applications. It has powered more than 10 BUs and more than 20 business scenarios within the Alibaba group. It is seamlessly integrated toPlatform of AI (PAI)products, including PAI-DSW for development, PAI-DLC for cloud-native training, PAI-EAS for serving, and PAI-Designer for zero-code model training.Main FeaturesEasy to use and highly customizable:In addition to providing easy-to-use and concise commands to call cutting-edge models, it also abstracts certain custom modules such as AppZoo and ModelZoo to make it easy to build NLP applications. It is equipped with the PAI PyTorch distributed training framework TorchAccelerator to speed up distributed training.Compatible with open-source libraries:EasyNLP has APIs to support the training of models from Huggingface/Transformers with the PAI distributed framework. It also supports the pre-trained models inEasyTransferModelZoo.Knowledge-injected pre-training:The PAI team has a lot of research on knowledge-injected pre-training, and builds a knowledge-injected model that wins first place in the CCF knowledge pre-training competition. EasyNLP integrates these cutting-edge knowledge pre-trained models, including DKPLM and KGBERT.Landing large pre-trained models:EasyNLP provides few-shot learning capabilities, allowing users to finetune large models with only a few samples to achieve good results. At the same time, it provides knowledge distillation functions to help quickly distill large models to a small and efficient model to facilitate online deployment.InstallationYou can either install it from pip$pipinstallpai-easynlp(tobereleased)or setup from the source：$gitclonehttps://github.com/alibaba/EasyNLP.git
$cdEasyNLP
$pythonsetup.pyinstallThis repo is tested on Python3.6, PyTorch >= 1.8.Quick StartNow let's show how to use just a few lines of code to build a text classification model based on BERT.fromeasynlp.coreimportTrainerfromeasynlp.appzooimportClassificationDataset,SequenceClassificationfromeasynlp.utilsimportinitialize_easynlpargs=initialize_easynlp()train_dataset=ClassificationDataset(pretrained_model_name_or_path=args.pretrained_model_name_or_path,data_file=args.tables,max_seq_length=args.sequence_length,input_schema=args.input_schema,first_sequence=args.first_sequence,label_name=args.label_name,label_enumerate_values=args.label_enumerate_values,is_training=True)model=SequenceClassification(pretrained_model_name_or_path=args.pretrained_model_name_or_path)Trainer(model=model,train_dataset=train_dataset).train()Then you can run the code:pythonmain.py\--modetrain\--tables=train_toy.tsv\--input_schema=label:str:1,sid1:str:1,sid2:str:1,sent1:str:1,sent2:str:1\--first_sequence=sent1\--label_name=label\--label_enumerate_values=0,1\--checkpoint_dir=./tmp/\--epoch_num=1\--app_name=text_classify\--user_defined_parameters='pretrain_model_name_or_path=bert-tiny-uncased'You can also use AppZoo Command Line Tools to quickly train an App model. Take text classification on SST-2 dataset as an example. First you can download thetrain.tsv, anddev.tsv, then start training:$easynlp\--mode=train\--worker_gpu=1\--tables=train.tsv,dev.tsv\--input_schema=label:str:1,sid1:str:1,sid2:str:1,sent1:str:1,sent2:str:1\--first_sequence=sent1\--label_name=label\--label_enumerate_values=0,1\--checkpoint_dir=./classification_model\--epoch_num=1\--sequence_length=128\--app_name=text_classify\--user_defined_parameters='pretrain_model_name_or_path=bert-small-uncased'And then predict:$easynlp\--mode=predict\--tables=dev.tsv\--outputs=dev.pred.tsv\--input_schema=label:str:1,sid1:str:1,sid2:str:1,sent1:str:1,sent2:str:1\--output_schema=predictions,probabilities,logits,output\--append_cols=label\--first_sequence=sent1\--checkpoint_path=./classification_model\--app_name=text_classifyTo learn more about the usage of AppZoo, please refer to ourdocumentation.ModelZooEasyNLP currently provides the following models in ModelZoo:PAI-BERT-zh (from Alibaba PAI): pre-trained BERT models with a large Chinese corpus.DKPLM (from Alibaba PAI): released with the paperDKPLM: Decomposable Knowledge-enhanced Pre-trained Language Model for Natural Language Understandingby Taolin Zhang, Chengyu Wang, Nan Hu, Minghui Qiu, Chengguang Tang, Xiaofeng He and Jun Huang.KGBERT (from Alibaba Damo Academy & PAI): pre-train BERT models with knowledge graph embeddings injected.BERT (from Google): released with the paperBERT: Pre-training of Deep Bidirectional Transformers for Language Understandingby Jacob Devlin, Ming-Wei Chang, Kenton Lee and Kristina Toutanova.RoBERTa (from Facebook): released with the paperRoBERTa: A Robustly Optimized BERT Pretraining Approachby Yinhan Liu, Myle Ott, Naman Goyal, Jingfei Du, Mandar Joshi, Danqi Chen, Omer Levy, Mike Lewis, Luke Zettlemoyer and Veselin Stoyanov.Chinese RoBERTa (from HFL): the Chinese version of RoBERTa.MacBERT (from HFL): released with the paperRevisiting Pre-trained Models for Chinese Natural Language Processingby Yiming Cui, Wanxiang Che, Ting Liu, Bing Qin, Shijin Wang and Guoping Hu.WOBERT (from ZhuiyiTechnology): the word-based BERT for the Chinese language.FashionBERT (from Alibaba PAI & ICBU): in progress.GEEP (from Alibaba PAI): in progress.Please refer to thisreadmefor the usage of these models in EasyNLP.
Meanwhile, EasyNLP supports to load pretrained models from Huggingface/Transformers, please refer tothis tutorialfor details.Landing Large Pre-trained ModelsEasyNLP provide few-shot learning and knowledge distillation to help land large pre-trained models.PET(from LMU Munich and Sulzer GmbH): released with the paperExploiting Cloze Questions for Few Shot Text Classification and Natural Language Inferenceby Timo Schick and Hinrich Schutze. We have made some slight modifications to make the algorithm suitable for the Chinese language.P-Tuning(from Tsinghua University, Beijing Academy of AI, MIT and Recurrent AI, Ltd.): released with the paperGPT Understands, Tooby Xiao Liu, Yanan Zheng, Zhengxiao Du, Ming Ding, Yujie Qian, Zhilin Yang and Jie Tang. We have made some slight modifications to make the algorithm suitable for the Chinese language.CP-Tuning(from Alibaba PAI): released with the paperMaking Pre-trained Language Models End-to-end Few-shot Learners with Contrastive Prompt Tuningby Ziyun Xu, Chengyu Wang, Minghui Qiu, Fuli Luo, Runxin Xu, Songfang Huang and Jun Huang.Vanilla KD(from Alibaba PAI): distilling the logits of large BERT-style models to smaller ones.Meta KD(from Alibaba PAI): released with the paperMeta-KD: A Meta Knowledge Distillation Framework for Language Model Compression across Domainsby Haojie Pan, Chengyu Wang, Minghui Qiu, Yichang Zhang, Yaliang Li and Jun Huang.Data Augmentation(from Alibaba PAI): augmentating the data based on the MLM head of pre-trained language models.CLUE BenchmarkEasyNLP providesa simple toolkitto benchmark clue datasets. You can simply use just this command to benchmark CLUE dataset.# Format: bash run_clue.sh device_id train/predict dataset# e.g.:bashrun_clue.sh0traincslWe've tested a bert model (bert-base-chinese) on the datasets, the results of dev set are:TaskAFQMCCMNLICSLIFLYTEKOCNLITNEWSWSCP72.17%79.10%80.93%60.22%78.31%57.52%63.49%F152.96%79.10%81.71%60.22%78.30%57.52%77.67%Here is the detailedCLUE benchmark example.Tutorials自定义文本分类示例QuickStart-文本分类QuickStart-PAI DSWQuickStart-MaxCompute/ODPS数据AppZoo-文本向量化AppZoo-文本分类/匹配AppZoo-序列标注AppZoo-GEEP文本分类基础预训练实践知识预训练实践知识蒸馏实践跨任务知识蒸馏实践小样本学习实践Rapidformer模型训练加速实践API docs:http://atp-modelzoo-sh.oss-cn-shanghai.aliyuncs.com/release/easynlp/easynlp_docs/html/index.htmlLicenseThis project is licensed under theApache License (Version 2.0). This toolkit also contains some code modified from other repos under other open-source licenses. See theNOTICEfile for more information.ChangeLogEasyNLP v0.0.3 was released in 01/04/2022. Please refer totag_v0.0.3for more details and history.Contact UsScan the following QR codes to join Dingtalk discussion group. The group discussions are mostly in Chinese, but English is also welcomed.ReferenceEasyTransfer:https://github.com/alibaba/EasyTransferDKPLM:https://arxiv.org/abs/2112.01047MetaKD:https://arxiv.org/abs/2012.01266CP-Tuning:https://arxiv.org/abs/2204.00166FashionBERT:https://arxiv.org/abs/2005.09801