• Stars
    star
    591
  • Rank 75,679 (Top 2 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created almost 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Flexible components pairing 🤗 Transformers with ⚡ Pytorch Lightning

Deprecation notice 🔒

This repository has been archived (read-only) on Nov 21, 2022. Thanks to everyone who contributed to lightning-transformers, we feel it's time to move on.

🤗 Transformers can already be easily trained using the Lightning ⚡ Trainer. Here's a recent example from the community: https://sachinruk.github.io/blog/deep-learning/2022/11/07/t5-for-grammar-correction.html. Note that there are no limitations or workarounds, things just work out of the box.

The lightning-transformers repo explored the possibility to provide task-specific modules and pre-baked defaults, at the cost of introducing extra abstractions. In the spirit of keeping ourselves focused, these abstractions are not something we wish to continue supporting.

If you liked lightning-transformers and want to continue developing it in the future, feel free to fork the repo and choose another name for the project.


Flexible components pairing 🤗 Transformers with Pytorch Lightning


DocsCommunity


Installation

pip install lightning-transformers
From Source
git clone https://github.com/PyTorchLightning/lightning-transformers.git
cd lightning-transformers
pip install .

What is Lightning-Transformers

Lightning Transformers provides LightningModules, LightningDataModules and Strategies to use 🤗 Transformers with the PyTorch Lightning Trainer.

Quick Recipes

Train bert-base-cased on the CARER emotion dataset using the Text Classification task.

import pytorch_lightning as pl
from transformers import AutoTokenizer

from lightning_transformers.task.nlp.text_classification import (
    TextClassificationDataModule,
    TextClassificationTransformer,
)

tokenizer = AutoTokenizer.from_pretrained(
    pretrained_model_name_or_path="bert-base-cased"
)
dm = TextClassificationDataModule(
    batch_size=1,
    dataset_name="emotion",
    max_length=512,
    tokenizer=tokenizer,
)
model = TextClassificationTransformer(
    pretrained_model_name_or_path="bert-base-cased", num_labels=dm.num_classes
)

trainer = pl.Trainer(accelerator="auto", devices="auto", max_epochs=1)

trainer.fit(model, dm)

Train a pre-trained mt5-base backbone on the WMT16 dataset using the Translation task.

import pytorch_lightning as pl
from transformers import AutoTokenizer

from lightning_transformers.task.nlp.translation import (
    TranslationTransformer,
    WMT16TranslationDataModule,
)

tokenizer = AutoTokenizer.from_pretrained(
    pretrained_model_name_or_path="google/mt5-base"
)
model = TranslationTransformer(
    pretrained_model_name_or_path="google/mt5-base",
    n_gram=4,
    smooth=False,
    val_target_max_length=142,
    num_beams=None,
    compute_generate_metrics=True,
)
dm = WMT16TranslationDataModule(
    # WMT translation datasets: ['cs-en', 'de-en', 'fi-en', 'ro-en', 'ru-en', 'tr-en']
    dataset_config_name="ro-en",
    source_language="en",
    target_language="ro",
    max_source_length=128,
    max_target_length=128,
    padding="max_length",
    tokenizer=tokenizer,
)
trainer = pl.Trainer(accelerator="auto", devices="auto", max_epochs=1)

trainer.fit(model, dm)

Lightning Transformers supports a bunch of 🤗 tasks and datasets. See the documentation.

Billion Parameter Model Support

Big Model Inference

It's really easy to enable large model support for the pre-built LightningModule 🤗 tasks.

Below is an example to enable automatic model partitioning (across CPU/GPU and even leveraging disk space) to run text generation using a 6B parameter model.

import torch
from accelerate import init_empty_weights
from transformers import AutoTokenizer

from lightning_transformers.task.nlp.language_modeling import (
    LanguageModelingTransformer,
)

with init_empty_weights():
    model = LanguageModelingTransformer(
        pretrained_model_name_or_path="EleutherAI/gpt-j-6B",
        tokenizer=AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B"),
        low_cpu_mem_usage=True,
        device_map="auto",  # automatically partitions the model based on the available hardware.
    )

output = model.generate("Hello, my name is", device=torch.device("cuda"))
print(model.tokenizer.decode(output[0].tolist()))

For more information see Big Transformers Model Inference.

Big Model Training with DeepSpeed

Below is an example of how you can train a 6B parameter transformer model using Lightning Transformers and DeepSpeed.

import pytorch_lightning as pl
from transformers import AutoTokenizer

from lightning_transformers.task.nlp.language_modeling import (
    LanguageModelingDataModule,
    LanguageModelingTransformer,
)

tokenizer = AutoTokenizer.from_pretrained(pretrained_model_name_or_path="gpt2")

model = LanguageModelingTransformer(
    pretrained_model_name_or_path="EleutherAI/gpt-j-6B",
    tokenizer=AutoTokenizer.from_pretrained("EleutherAI/gpt-j-6B"),
    deepspeed_sharding=True,  # defer initialization of the model to shard/load pre-train weights
)

dm = LanguageModelingDataModule(
    batch_size=1,
    dataset_name="wikitext",
    dataset_config_name="wikitext-2-raw-v1",
    tokenizer=tokenizer,
)
trainer = pl.Trainer(
    accelerator="gpu",
    devices="auto",
    strategy="deepspeed_stage_3",
    precision=16,
    max_epochs=1,
)

trainer.fit(model, dm)

For more information see DeepSpeed Training with Big Transformers Models or the Model Parallelism documentation.

Contribute

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Community

For help or questions, join our huge community on Slack!

More Repositories

1

lightning-flash

Your PyTorch AI Factory - Flash enables you to easily configure and run complex AI recipes for over 15 tasks across 7 data domains
Python
1,733
star
2

lightning-bolts

Toolbox of models, callbacks, and datasets for AI/ML researchers.
Python
1,547
star
3

stable-diffusion-deploy

Learn to serve Stable Diffusion models on cloud infrastructure at scale. This Lightning App shows load-balancing, orchestrating, pre-provisioning, dynamic batching, GPU-inference, micro-services working together via the Lightning Apps framework.
Python
328
star
4

Echo

Production-ready audio and video transcription app that can run on your laptop or in the cloud.
TypeScript
67
star
5

lightning-GPT

Train and run GPTs with Lightning
Python
63
star
6

lightning-Covid19

Classification for covid-19 chest X-ray images using Lightning
Python
56
star
7

paper-AAVAE

Python
46
star
8

Research-template

Quickest way to share everything about your research within a single app
Jupyter Notebook
37
star
9

lightning-diffusion_component

Python
25
star
10

DiffusionWithAutoscaler

DiffusionWithAutoscaler
Python
23
star
11

Pose-app

A Lightning app for animal pose estimation.
Python
18
star
12

Training-Studio_app

Lightning HPO & Training Studio App
Python
17
star
13

Research-poster

Quickest way to share everything about your research within a single app
Python
16
star
14

Triton-Server_component

Triton Server Component for lightning.ai
Python
14
star
15

lightning-quick-start

Python
12
star
16

InVideo-search_app

Python
12
star
17

CVPR22-MAE_research-poster

Jupyter Notebook
11
star
18

Flashy_app

Perform Image/Text Classification with ⚡ Flash built using ⚡ AI
Python
10
star
19

open-bio-ml-workshop

Python
9
star
20

Serve_component

Python
9
star
21

HackerNews_app

Jupyter Notebook
8
star
22

lightning-gpt3

Python
7
star
23

dataumbrella22-intro-pytorch

Code for Data Umbrella Talk "Intro to PyTorch and LightningLite
Jupyter Notebook
6
star
24

API-Access-UI_component

TypeScript
6
star
25

Slack-Command-Bot-component

With this component you can create a Slack bot and enable interactivity with the Slash Commands.
Python
5
star
26

Jupyter_component

Jupyter notebook component
Python
5
star
27

Text-Prediction_component

Python
4
star
28

Text-Classification-component

Python
4
star
29

lightning-template-react

TypeScript
4
star
30

AnimeGAN-v2_research-poster

Jupyter Notebook
4
star
31

Redis_component

Redis component for lightning
Python
4
star
32

MarkDown-poster_component

This component lets you make posters from Markdown files.
Python
4
star
33

CodeRunner_app

A sample app which uses Monaco Editor integration, and runs the script on frame from webcam
Python
4
star
34

Finetune-miniLM

Python
4
star
35

Hate-Speech-Detection_app

Python
4
star
36

lightning-Bagua

Lightning Training strategy for Bagua
Python
3
star
37

DeepChecks_app

Deepchecks App with Lightning
Python
3
star
38

lightning-LLMs

Python
3
star
39

Collaborate_app

JavaScript
3
star
40

TLDR-component

Components for training large language models
Python
3
star
41

Telegram-Messenger_component

Python
3
star
42

JupyterLite_component

Jupyter Lite instance runs completely in your browser, powered by Pyodide
Python
3
star
43

FaceNet_research-poster

Jupyter Notebook
3
star
44

Gradio-template

Python
3
star
45

ODSC

tutorial for converting PyTorch to Lightning
Jupyter Notebook
3
star
46

Dalle-mini-poster

🥑 Dalle Mini Poster App | Generate Images from Text Prompt ⚡️
Jupyter Notebook
2
star
47

ICML22-BLIP_research-poster

Python
2
star
48

YOLOv5_research-poster

This app is a research poster demo of YoloV5 Object Detection model by Ultralytics. It showcases a notebook, a blog, and a model demo where you can upload photos to get a bounding box visualized output image.
Jupyter Notebook
2
star
49

video-streaming_component

Python
2
star
50

BigQuery_component

Python
2
star
51

grid-tutorials

Python
2
star
52

ICML22-OFA_research-poster

Jupyter Notebook
2
star
53

Finetune-miniLM-gallery

Python
1
star
54

lightning-aws

Python
1
star
55

Flash-Serve_component

LAI component for FlashServe
Python
1
star
56

AWS-s3_component

Lightning component to interact with s3.
Python
1
star
57

lightning-Galvatron

Python
1
star
58

Sample_component

Python
1
star
59

Youtube_component

Used to interface with youtube
Python
1
star
60

tgr

Python
1
star