Home

Awesome

<img src="flagopen.jpeg"> FlagAI CII Best Practices Python application GitHub release (release name instead of tag name)

简体中文


FlagAI (Fast LArge-scale General AI models) is a fast, easy-to-use and extensible toolkit for large-scale model. Our goal is to support training, fine-tuning, and deployment of large-scale models on various downstream tasks with multi-modality.

Why should I use FlagAI?

  1. Quickly Download Models via API

    FlagAI provides an API that allows you to quickly download pre-trained models and fine-tune them on a wide range of datasets collected from SuperGLUE and CLUE benchmarks for both Chinese and English text.

    FlagAI now supports over 30 mainstream models, including Language Model Aquila, multilingual text and image representation model AltCLIP, text-to-image generation model AltDiffusion Huggingface space, WuDao GLM (with a maximum of 10 billion parameters), EVA-CLIP, OPT, BERT, RoBERTa, GPT2, T5, ALM, and models from Huggingface Transformers, etc.

  2. Parallel train with fewer than 10 lines of code

    Backed by the four most popular data/model parallel libraries -- PyTorch, Deepspeed, Megatron-LM, BMTrain -- FlagAI allows for seamless integration between them, enabling users to parallel their training/testing process with fewer than ten lines of code.

  3. Conveniently use the few-shot learning toolkits

    FlagAI also provides prompt-learning toolkit for few-shot tasks.

  4. Particularly good at Chinese tasks

    These models can be applied to (Chinese/English) Text, for tasks like text classification, information extraction, question answering, summarization, and text generation, with a particular focus on Chinese tasks.

Toolkits and Pre-trained Models

The code is partially based on GLM, Transformerstimm and DeepSpeedExamples.

Toolkits

NameDescriptionExamples
GLM_custom_pvpCustomizing PET templatesREADME.md
GLM_ptuningp-tuning tool——
BMInf-generateAccelerating generationREADME.md

Pre-trained Models

ModelTaskTrainFinetuneInference/GenerateExamples
AquilaNatural Language ProcessingREADME.md
ALMArabic Text GenerationREADME.md
AltCLIPImage-Text MatchingREADME.md
AltCLIP-m18Image-Text MatchingREADME.md
AltDiffusionText-to-Image GenerationREADME.md
AltDiffusion-m18Text-to-Image Generation,supporting 18 languagesREADME.md
BERT-title-generation-englishEnglish Title GenerationREADME.md
CLIPImage-Text Matching——
CPM3-finetuneText Continuation——
CPM3-generateText Continuation——
CPM3_pretrainText Continuation——
CPM_1Text ContinuationREADME.md
EVA-CLIPImage-Text MatchingREADME.md
GalacticaText Continuation——
GLM-large-ch-blank-fillingBlank FillingTUTORIAL
GLM-large-ch-poetry-generationPoetry GenerationTUTORIAL
GLM-large-ch-title-generationTitle GenerationTUTORIAL
GLM-pretrainPre-Train——
GLM-seq2seqGeneration——
GLM-superglueClassification——
GPT-2-text-writtingText ContinuationTUTORIAL
GPT2-text-writtingText Continuation——
GPT2-title-generationTitle Generation——
OPTText ContinuationREADME.md
RoBERTa-base-ch-nerNamed Entity RecognitionTUTORIAL
RoBERTa-base-ch-semantic-matchingSemantic Similarity MatchingTUTORIAL
RoBERTa-base-ch-title-generationTitle GenerationTUTORIAL
RoBERTa-faqQuestion-AnswerREADME.md
Swinv1Image Classification——
Swinv2Image Classification——
T5-huggingface-11bTrainTUTORIAL
T5-title-generationTitle GenerationTUTORIAL
T5-flagai-11bPre-Train——
ViT-cifar100Pre-Train——

Contributing

Thanks for your interest in contributing! There are many ways to get involved; start with our contributor guidelines and then check these open issues for specific tasks.

Contact us

Welcome to raise your questions or feature requests on GitHub Issues , and share your experience on the Discussions board.

<img src="./wechat-qrcode.jpg" width = "200" height = "200" align=center />

Quick Start

We provide many models which are trained to perform different tasks. You can load these models by AutoLoader to make prediction. See more in FlagAI/quickstart.

Requirements and Installation

pip install -U flagai
git clone https://github.com/FlagAI-Open/FlagAI.git
python setup.py install
git clone https://github.com/NVIDIA/apex
cd apex
pip install -v --disable-pip-version-check --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" ./
git clone https://github.com/microsoft/DeepSpeed
cd DeepSpeed
DS_BUILD_CPU_ADAM=1 DS_BUILD_AIO=1 DS_BUILD_UTILS=1 pip install -e .
ds_report # check the deespeed status
git clone https://github.com/OpenBMB/BMTrain
cd BMTrain
python setup.py install
pip install bminf

pip install flash-attn
>>> vim ~/.ssh/config
Host 127.0.0.1
    Hostname 127.0.0.1
    Port 7110
    User root
>>> ssh-keygen -t rsa -C "xxx@xxx.com"

Load model and tokenizer

We provide the AutoLoad class to load the model and tokenizer quickly, for example:

from flagai.auto_model.auto_loader import AutoLoader

auto_loader = AutoLoader(
    task_name="title-generation",
    model_name="BERT-base-en"
)
model = auto_loader.get_model()
tokenizer = auto_loader.get_tokenizer()

This example is for the title_generation task, and you can also model other tasks by modifying the task_name. Then you can use the model and tokenizer to fine-tune or test.

Examples

1. Predictor

We provide the Predictor class to predict for different tasks, for example:

from flagai.model.predictor.predictor import Predictor
predictor = Predictor(model, tokenizer)
test_data = [
    "Four minutes after the red card, Emerson Royal nodded a corner into the path of the unmarked Kane at the far post, who nudged the ball in for his 12th goal in 17 North London derby appearances. Arteta's misery was compounded two minutes after half-time when Kane held the ball up in front of goal and teed up Son to smash a shot beyond a crowd of defenders to make it 3-0.The goal moved the South Korea talisman a goal behind Premier League top scorer Mohamed Salah on 21 for the season, and he looked perturbed when he was hauled off with 18 minutes remaining, receiving words of consolation from Pierre-Emile Hojbjerg.Once his frustrations have eased, Son and Spurs will look ahead to two final games in which they only need a point more than Arsenal to finish fourth.",
]

for text in test_data:
    print(
        predictor.predict_generate_beamsearch(text,
                                              out_max_length=50,
                                              beam_size=3))

This example is for the seq2seq task, where we can get beam-search results by calling the predict_generate_beamsearch function. In addition, we also support prediction for tasks such as NER and title generate.

2. NER

from flagai.auto_model.auto_loader import AutoLoader
from flagai.model.predictor.predictor import Predictor

task_name = "ner"
model_name = "RoBERTa-base-ch"
target = ["O", "B-LOC", "I-LOC", "B-ORG", "I-ORG", "B-PER", "I-PER"]
maxlen = 256

auto_loader = AutoLoader(task_name,
                         model_name=model_name,
                         load_pretrain_params=True,
                         class_num=len(target))

model = auto_loader.get_model()
tokenizer = auto_loader.get_tokenizer()

predictor = Predictor(model, tokenizer)

test_data = [
    "6月15日,河南省文物考古研究所曹操高陵文物队公开发表声明承认:“从来没有说过出土的珠子是墓主人的",
    "4月8日,北京冬奥会、冬残奥会总结表彰大会在人民大会堂隆重举行。习近平总书记出席大会并发表重要讲话。在讲话中,总书记充分肯定了北京冬奥会、冬残奥会取得的优异成绩,全面回顾了7年筹办备赛的不凡历程,深入总结了筹备举办北京冬奥会、冬残奥会的宝贵经验,深刻阐释了北京冬奥精神,对运用好冬奥遗产推动高质量发展提出明确要求。",
    "当地时间8日,欧盟委员会表示,欧盟各成员国政府现已冻结共计约300亿欧元与俄罗斯寡头及其他被制裁的俄方人员有关的资产。",
    "这一盘口状态下英国必发公司亚洲盘交易数据显示博洛尼亚热。而从欧赔投注看,也是主队热。巴勒莫两连败,",
]

for t in test_data:
    entities = predictor.predict_ner(t, target, maxlen=maxlen)
    result = {}
    for e in entities:
        if e[2] not in result:
            result[e[2]] = [t[e[0]:e[1] + 1]]
        else:
            result[e[2]].append(t[e[0]:e[1] + 1])
    print(f"result is {result}")

3. Semantic Matching example

from flagai.auto_model.auto_loader import AutoLoader
from flagai.model.predictor.predictor import Predictor

maxlen = 256

auto_loader = AutoLoader("semantic-matching",
                         model_name="RoBERTa-base-ch",
                         load_pretrain_params=True,
                         class_num=2)
model = auto_loader.get_model()
tokenizer = auto_loader.get_tokenizer()

predictor = Predictor(model, tokenizer)

test_data = [["后悔了吗", "你有没有后悔"], ["打开自动横屏", "开启移动数据"],
             ["我觉得你很聪明", "你聪明我是这么觉得"]]

for text_pair in test_data:
    print(predictor.predict_cls_classifier(text_pair))

LICENSE

The majority of FlagAI is licensed under the Apache 2.0 license, however portions of the project are available under separate license terms:

News

Platforms supported

<div align="center"> <img src="./examples/Aquila/img/merged_platform.jpg" height = "100" align=center /> </div>

Misc

↳ Stargazers, thank you for your support!

Stargazers repo roster for @FlagAI-Open/FlagAI

↳ Forkers, thank you for your support!

Forkers repo roster for @FlagAI-Open/FlagAI

↳ Star History

<div align="center">

Star History Chart]

</div>