AiSpace

February 16, 2023 · View on GitHub



GitHub Documentation GitHub release

AiSpace provides highly configurable framework for deep learning model development, deployment and conveniently use of pre-trained models (bert, albert, opt, etc.).

Table of Contents

Features

  • Highly configurable, we manage all hyperparameters with inheritable Configuration files.
  • All modules are registerable, including models, dataset, losses, optimizers, metrics, callbacks, etc.
  • Standardized process
  • Multi-GPU Training
  • K-fold cross validation training
  • Integrate lr finder
  • Integrate multiple pre-trained models, including chinese
  • Simple and fast deployment using BentoML
  • Integrated Chinese benchmarks CLUE

Requirements

git clone https://github.com/yingyuankai/AiSpace.git
cd AiSpace && pip install -r requirements

Instructions

Training

python -u aispace/trainer.py \
    --schedule train_and_eval \
    --config_name CONFIG_NAME \
    --config_dir CONFIG_DIR \
    [--experiment_name EXPERIMENT_NAME] \
    [--model_name MODEL_NAME] \
    [--gpus GPUS] 

Output file structure

The default output path is save, which may has multiple output directories under name as:

{experiment_name}_{model_name}_{dataset_name}_{random_seed}_{id}

Where id indicates the sequence number of the experiment for the same task, increasing from 0.

Take the text classification task as an example, the output file structure is similar to the following:

experiment_name: test

model_name: bert_for_classification

dataset_name: glue_zh/tnews

random_seed: 119

id: 0
test_bert_for_classification_glue_zh__tnews_119_0
├── checkpoint                  # 1. checkpoints
│   ├── checkpoint
│   ├── ckpt_1.data-00000-of-00002
│   ├── ckpt_1.data-00001-of-00002
│   ├── ckpt_1.index
|   ...
├── deploy                      # 2. Bentoml depolyment directory
│   └── BertTextClassificationService
│       └── 20191208180211_B6FC81
├── hparams.json                # 3. Json file of all hyperparameters
├── logs                        # 4. general or tensorboard log directory
│   ├── errors.log              # error log file
│   ├── info.log                # info log file
│   ├── train                
│   │   ├── events.out.tfevents.1574839601.jshd-60-31.179552.14276.v2
│   │   ├── events.out.tfevents.1574839753.jshd-60-31.profile-empty
│   └── validation
│       └── events.out.tfevents.1574839787.jshd-60-31.179552.151385.v2
├── model_saved                 # 5. last model saved
│   ├── checkpoint
│   ├── model.data-00000-of-00002
│   ├── model.data-00001-of-00002
│   └── model.index
└── reports                     # 6. Eval reports for every output or task
    └── output_1_classlabel     # For example, text classification task
        ├── confusion_matrix.txt
        ├── per_class_stats.json
        └── stats.json

Training with resumed model

python -u aispace/trainer.py \
    --schedule train_and_eval \
    --config_name CONFIG_NAME \
    --config_dir CONFIG_DIR \
    --model_resume_path MODEL_RESUME_PATH \
    [--experiment_name EXPERIMENT_NAME] \
    [--model_name MODEL_NAME] \
    [--gpus GPUS] 

--model_resume_path is a path to initialization model.

lr finder

Firstly, use optimizer adma and open lr_finder callback.

policy:
    name: "base"
        
optimizer:
  name: adam
    
callbacks:
    lr_finder:
      switch: true

Then run training policy as base.

Lastly, you can find lr_finder.jpg in you workspace.

K-fold cross validation training

Firstly, Replace training default policy form base to:

training:
  policy:
    name: "k-fold"
    config:
      k: 5

The k is the number of fold. Your can refer to the configuration file in:

./confis/glue_zh/tnews_k_fold.yml

Then run training script as usual.

Average checkpoints

python -u aispace/trainer.py \
    --schedule avg_checkpoints \
    --config_name CONFIG_NAME \
    --config_dir CONFIG_DIR \
    --prefix_or_checkpoints PREFIX_OR_CHECKPOINGS \
    [--ckpt_weights CKPT_WEIGHTS] \
    [--experiment_name EXPERIMENT_NAME] \
    [--model_name MODEL_NAME] \
    [--gpus GPUS] 

--prefix_or_checkpoints is paths to multiple checkpoints separated by comma.

--ckpt_weights is weights same order as the prefix_or_checkpoints.

Deployment

Generate deployment files before deployment, you need to specify the model path (--model_resume_path) to be deployed like following.

python -u aispace/trainer.py \
    --schedule deploy \
    --config_name CONFIG_NAME \
    --config_dir CONFIG_DIR \
    --model_resume_path MODEL_RESUME_PATH \
    [--experiment_name EXPERIMENT_NAME] \
    [--model_name MODEL_NAME] \
    [--gpus GPUS] 

We use BentoML as deploy tool, so your must implement the deploy function in your model class.

Configuration

All the configurations are in configs, in which base (./configs/default/base.yml) is the most basic, any configuration downstream includes this configuration directly or indirectly. Before you start, it is best to read this configuration carefully.

Your can use includes field to load other configurations, then the current configuration inherits the configurations and overrides the same configuration fields. Just like class inheritance, a function of the same name in a subclass overrides a function of the parent class.

The syntax is like this:

merge configuration of bert_huggingface into current.

includes:
  - "../pretrain/bert_huggingface.yml"     # relative path

Datasets

DatasetInfoRef
glue_zh/afqmcAnt Financial Question Matching Corpus(蚂蚁金融语义相似度)https://github.com/CLUEbenchmark/CLUE
glue_zh/tnewsTNEWS 今日头条中文新闻(短文)分类https://github.com/CLUEbenchmark/CLUE
glue_zh/iflytekIFLYTEK' 长文本分类https://github.com/CLUEbenchmark/CLUE
glue_zh/cmnliCMNLI 语言推理任务https://github.com/CLUEbenchmark/CLUE
glue_zh/copaCOPA 因果推断-中文版https://github.com/CLUEbenchmark/CLUE
glue_zh/wscWSC Winograd模式挑战中文版https://github.com/CLUEbenchmark/CLUE
glue_zh/cslCSL 论文关键词识别https://github.com/CLUEbenchmark/CLUE
glue_zh/cmrc2018Reading Comprehension for Simplified Chinese 简体中文阅读理解任务https://github.com/CLUEbenchmark/CLUE
glue_zh/drcd繁体阅读理解任务https://github.com/CLUEbenchmark/CLUE
glue_zh/chid成语阅读理解填空 Chinese IDiom Dataset for Cloze Testhttps://github.com/CLUEbenchmark/CLUE
glue_zh/c3中文多选阅读理解https://github.com/CLUEbenchmark/CLUE
Dureader/robust首个关注阅读理解模型鲁棒性的中文数据集https://aistudio.baidu.com/aistudio/competition/detail/49
Dureader/yesno一个以观点极性判断为目标任务的数据集https://aistudio.baidu.com/aistudio/competition/detail/49
LSTC_2020/DuEE_trigger从自然语言文本中抽取事件并识别事件类型https://aistudio.baidu.com/aistudio/competition/detail/32
LSTC_2020/DuEE_role从自然语言文本中抽取事件元素https://aistudio.baidu.com/aistudio/competition/detail/32

Pretrained

We have integrated multiple pre-trained language models and are constantly expanding。

Model#Model#Chinese modelDownload manually?RefsStatus
bert131notransformersDone
albert80notransformersDone
albert_chinese99yesalbert_zhDone
bert_wwm44yesChinese-BERT-wwmDone
xlnet20notransformersProcessing
xlnet_chinese22yesChinese-PreTrained-XLNetsDone
ernie42yesERNIEDone
NEZHA44yesNEZHADone
TinyBERT---TinyBERTProcessing
electra_chinese44yesChinese-ELECTRDone

For those models that need to be downloaded manually, download, unzip them and modify the path in the corresponding configuration.

Some pre-trained models don't have tensorflow versions, I converted them and made them available for download。

ModelRefstf version
ERNIE_Base_en_stable-2.0.0ERNIEbaidu yun
ERNIE_stable-1.0.1ERNIEbaidu yun
ERNIE_1.0_max-len-512ERNIEbaidu yun
ERNIE_Large_en_stable-2.0.0ERNIEbaidu yun

Examples

We have implemented some tasks in the CLUE (The Chinese General Language Understanding Evaluation (GLUE) benchmark).

Please refer to Examples Doc.

Take glue_zh/tnews as an example:

Tnews is a task of Chinese GLUE, which is a short text classification task from ByteDance.

Run Tnews classification

python -u aispace/trainer.py \
    --experiment_name test \
    --model_name bert_for_classification \
    --schedule train_and_eval \
    --config_name tnews \
    --config_dir ./configs/glue_zh \
    --gpus 0 1 2 3  \

Specify different pretrained model, please change includes and pretrained.name in config file.

ModelAccuracyMacro_precisionMacro_recallMacro_f1
bert-base-chinese-huggingface65.02064.98762.48463.017
albert_base_zh62.16062.51459.26760.377
albert_base_zh_additional_36k_steps61.76061.72358.53459.273
albert_small_zh_google62.62063.81958.99259.387
albert_large_zh61.83061.98059.84360.200
albert_tiny60.11057.11855.55956.077
albert_tiny_489k61.13057.87557.20057.332
albert_tiny_zh_google60.86059.50057.55657.702
albert_xlarge_zh_177k63.38063.60360.16860.596
albert_xlarge_zh_183k63.21067.16159.22059.599
chinese_wwm64.00062.74764.50963.042
chinese_wwm_ext65.02065.04862.01762.688
chinese_roberta_wwm_ext64.86064.81963.27563.591
chinese_roberta_wwm_large_ext65.70062.34261.52761.664
ERNIE_stable-1.0.166.33066.90363.70464.524
ERNIE_1.0_max-len-51266.01065.30162.23062.884
chinese_xlnet_base65.11064.37764.86264.169
chinese_xlnet_mid66.00066.37763.87464.708
chinese_electra_small60.37060.22357.16157.206
chinese_electra_small_ex59.90058.07855.52556.194
chinese_electra_base60.50060.09058.26758.909
chinese_electra_large60.50060.36257.65358.336
nezha-base58.94057.90955.65055.630
nezha-base-wwm58.80060.06054.85955.831

NOTE: The hyper-parameters used here have not been fine-tuned.

Todos

  • More complete and detailed documentation;
  • More pretrained models;
  • More evaluations of CLUE;
  • More Chinese dataset;
  • Support Pytorch;
  • Improve the tokenizer to make it more versatile;
  • Build AiSpace server, make it can train and configure using UI.

Contact Author

Mail: yingyuankai@aliyun.com

Wechat: woshimoming1991

Citing

@misc{AiSpace,
  author = {yuankai ying},
  title = {AiSpace: Highly configurable framework for deep learning model development and deployment},
  year = {2020},
  publisher = {GitHub},
  journal = {GitHub repository},
  howpublished = {\url{https://github.com/yingyuankai/AiSpace}},
}

Refs