BertGeneration
This model was released on 2019-07-29 and added to Hugging Face Transformers on 2020-11-16.
BertGeneration
Section titled “BertGeneration”BertGeneration leverages pretrained BERT checkpoints for sequence-to-sequence tasks with the EncoderDecoderModel architecture. BertGeneration adapts the BERT for generative tasks.
You can find all the original BERT checkpoints under the BERT collection.
Click on the BertGeneration models in the right sidebar for more examples of how to apply BertGeneration to different sequence generation tasks.
The example below demonstrates how to use BertGeneration with EncoderDecoderModel for sequence-to-sequence tasks.
import torchfrom transformers import pipeline
pipeline = pipeline( task="text2text-generation", model="google/roberta2roberta_L-24_discofuse", dtype=torch.float16, device=0)pipeline("Plants create energy through ")import torchfrom transformers import EncoderDecoderModel, AutoTokenizer
model = EncoderDecoderModel.from_pretrained("google/roberta2roberta_L-24_discofuse", dtype="auto")tokenizer = AutoTokenizer.from_pretrained("google/roberta2roberta_L-24_discofuse")
input_ids = tokenizer( "Plants create energy through ", add_special_tokens=False, return_tensors="pt").input_ids
outputs = model.generate(input_ids)print(tokenizer.decode(outputs[0]))echo -e "Plants create energy through " | transformers run --task text2text-generation --model "google/roberta2roberta_L-24_discofuse" --device 0Quantization reduces the memory burden of large models by representing the weights in a lower precision. Refer to the Quantization overview for more available quantization backends.
The example below uses BitsAndBytesConfig to quantize the weights to 4-bit.
import torchfrom transformers import EncoderDecoderModel, AutoTokenizer, BitsAndBytesConfig
# Configure 4-bit quantizationquantization_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_compute_dtype=torch.float16)
model = EncoderDecoderModel.from_pretrained( "google/roberta2roberta_L-24_discofuse", quantization_config=quantization_config, dtype="auto")tokenizer = AutoTokenizer.from_pretrained("google/roberta2roberta_L-24_discofuse")
input_ids = tokenizer( "Plants create energy through ", add_special_tokens=False, return_tensors="pt").input_ids
outputs = model.generate(input_ids)print(tokenizer.decode(outputs[0]))-
BertGenerationEncoderandBertGenerationDecodershould be used in combination withEncoderDecoderModelfor sequence-to-sequence tasks.from transformers import BertGenerationEncoder, BertGenerationDecoder, BertTokenizer, EncoderDecoderModel# leverage checkpoints for Bert2Bert model# use BERT's cls token as BOS token and sep token as EOS tokenencoder = BertGenerationEncoder.from_pretrained("google-bert/bert-large-uncased", bos_token_id=101, eos_token_id=102)# add cross attention layers and use BERT's cls token as BOS token and sep token as EOS tokendecoder = BertGenerationDecoder.from_pretrained("google-bert/bert-large-uncased", add_cross_attention=True, is_decoder=True, bos_token_id=101, eos_token_id=102)bert2bert = EncoderDecoderModel(encoder=encoder, decoder=decoder)# create tokenizertokenizer = BertTokenizer.from_pretrained("google-bert/bert-large-uncased")input_ids = tokenizer("This is a long article to summarize", add_special_tokens=False, return_tensors="pt").input_idslabels = tokenizer("This is a short summary", return_tensors="pt").input_ids# trainloss = bert2bert(input_ids=input_ids, decoder_input_ids=labels, labels=labels).lossloss.backward() -
For summarization, sentence splitting, sentence fusion and translation, no special tokens are required for the input.
-
No EOS token should be added to the end of the input for most generation tasks.
BertGenerationConfig
Section titled “BertGenerationConfig”[[autodoc]] BertGenerationConfig
BertGenerationTokenizer
Section titled “BertGenerationTokenizer”[[autodoc]] BertGenerationTokenizer - save_vocabulary
BertGenerationEncoder
Section titled “BertGenerationEncoder”[[autodoc]] BertGenerationEncoder - forward
BertGenerationDecoder
Section titled “BertGenerationDecoder”[[autodoc]] BertGenerationDecoder - forward