@@ -24,7 +24,6 @@ rendered properly in your Markdown viewer.
24
24
25
25
[ BertGeneration] ( https://huggingface.co/papers/1907.12461 ) leverages pretrained BERT checkpoints for sequence-to-sequence tasks with the [ ` EncoderDecoderModel ` ] architecture. BertGeneration adapts the [ ` BERT ` ] for generative tasks.
26
26
27
-
28
27
You can find all the original BERT checkpoints under the [ BERT] ( https://huggingface.co/collections/google/bert-release-64ff5e7a4be99045d1896dbc ) collection.
29
28
30
29
> [ !TIP]
@@ -38,37 +37,34 @@ The example below demonstrates how to use BertGeneration with [`EncoderDecoderMo
38
37
<hfoption id =" Pipeline " >
39
38
40
39
``` python
40
+ import torch
41
41
from transformers import pipeline
42
42
43
- # Use pipeline for text generation with BERT-based models
44
- generator = pipeline(" text2text-generation" , model = " google/roberta2roberta_L-24_discofuse" )
45
- result = generator(" This is the first sentence. This is the second sentence." )
46
- print (result[0 ][' generated_text' ])
43
+ pipeline = pipeline(
44
+ task = " text2text-generation" ,
45
+ model = " google/roberta2roberta_L-24_discofuse" ,
46
+ torch_dtype = torch.float16,
47
+ device = 0
48
+ )
49
+ pipeline(" Plants create energy through " )
47
50
```
48
51
49
52
</hfoption >
50
53
<hfoption id =" AutoModel " >
51
54
52
55
``` python
53
- from transformers import BertGenerationEncoder, BertGenerationDecoder, BertTokenizer, EncoderDecoderModel
54
-
55
- # Create encoder-decoder model from BERT checkpoints
56
- encoder = BertGenerationEncoder.from_pretrained(" google-bert/bert-large-uncased" , bos_token_id = 101 , eos_token_id = 102 )
57
- decoder = BertGenerationDecoder.from_pretrained(
58
- " google-bert/bert-large-uncased" , add_cross_attention = True , is_decoder = True , bos_token_id = 101 , eos_token_id = 102
59
- )
60
- model = EncoderDecoderModel(encoder = encoder, decoder = decoder)
56
+ import torch
57
+ from transformers import EncoderDecoderModel, AutoTokenizer
61
58
62
- # Create tokenizer
63
- tokenizer = BertTokenizer .from_pretrained(" google-bert/bert-large-uncased " )
59
+ model = EncoderDecoderModel.from_pretrained( " google/roberta2roberta_L-24_discofuse " , torch_dtype = " auto " )
60
+ tokenizer = AutoTokenizer .from_pretrained(" google/roberta2roberta_L-24_discofuse " )
64
61
65
- # Prepare input
66
- input_ids = tokenizer(" This is a long article to summarize" , add_special_tokens = False , return_tensors = " pt" ).input_ids
62
+ input_ids = tokenizer(
63
+ " Plants create energy through " , add_special_tokens = False , return_tensors = " pt"
64
+ ).input_ids
67
65
68
- # Generate summary
69
- outputs = model.generate(input_ids, max_length = 50 )
70
- summary = tokenizer.decode(outputs[0 ], skip_special_tokens = True )
71
- print (summary)
66
+ outputs = model.generate(input_ids)
67
+ print (tokenizer.decode(outputs[0 ]))
72
68
```
73
69
74
70
</hfoption >
@@ -86,53 +82,36 @@ Quantization reduces the memory burden of large models by representing the weigh
86
82
The example below uses [ BitsAndBytesConfig] ( ../quantizationbitsandbytes ) to quantize the weights to 4-bit.
87
83
88
84
``` python
89
- from transformers import BertGenerationEncoder, BertTokenizer, BitsAndBytesConfig
90
85
import torch
86
+ from transformers import EncoderDecoderModel, AutoTokenizer, BitsAndBytesConfig
91
87
92
88
# Configure 4-bit quantization
93
89
quantization_config = BitsAndBytesConfig(
94
90
load_in_4bit = True ,
95
91
bnb_4bit_compute_dtype = torch.float16
96
92
)
97
93
98
- # Load quantized model
99
- encoder = BertGenerationEncoder.from_pretrained(
100
- " google-bert/bert-large-uncased" ,
94
+ model = EncoderDecoderModel.from_pretrained(
95
+ " google/roberta2roberta_L-24_discofuse" ,
101
96
quantization_config = quantization_config,
102
- bos_token_id = 101 ,
103
- eos_token_id = 102
97
+ torch_dtype = " auto"
104
98
)
105
- tokenizer = BertTokenizer.from_pretrained(" google-bert/bert-large-uncased" )
99
+ tokenizer = AutoTokenizer.from_pretrained(" google/roberta2roberta_L-24_discofuse" )
100
+
101
+ input_ids = tokenizer(
102
+ " Plants create energy through " , add_special_tokens = False , return_tensors = " pt"
103
+ ).input_ids
104
+
105
+ outputs = model.generate(input_ids)
106
+ print (tokenizer.decode(outputs[0 ]))
106
107
```
107
108
108
109
## Notes
109
110
110
111
- [ ` BertGenerationEncoder ` ] and [ ` BertGenerationDecoder ` ] should be used in combination with [ ` EncoderDecoderModel ` ] for sequence-to-sequence tasks.
111
-
112
- <add code example here from https://huggingface.co/docs/transformers/model_doc/bert-generation#usage-examples-and-tips>
113
112
- For summarization, sentence splitting, sentence fusion and translation, no special tokens are required for the input.
114
113
- No EOS token should be added to the end of the input for most generation tasks.
115
114
116
- ``` python
117
- # Example of creating a complete encoder-decoder setup
118
- from transformers import EncoderDecoderModel, AutoTokenizer
119
-
120
- # Load pre-trained encoder-decoder model
121
- model = EncoderDecoderModel.from_pretrained(" google/roberta2roberta_L-24_discofuse" )
122
- tokenizer = AutoTokenizer.from_pretrained(" google/roberta2roberta_L-24_discofuse" )
123
-
124
- # Generate text
125
- input_text = " This is the first sentence. This is the second sentence."
126
- input_ids = tokenizer(input_text, add_special_tokens = False , return_tensors = " pt" ).input_ids
127
- outputs = model.generate(input_ids)
128
- result = tokenizer.decode(outputs[0 ])
129
- ```
130
-
131
- ## Resources
132
-
133
- - [ Original Paper: Leveraging Pre-trained Checkpoints for Sequence Generation Tasks] ( https://arxiv.org/abs/1907.12461 )
134
- - [ Google Research Blog Post] ( https://ai.googleblog.com/2020/01/leveraging-bert-for-sequence-generation.html )
135
-
136
115
## BertGenerationConfig
137
116
138
117
[[ autodoc]] BertGenerationConfig
0 commit comments