Skip to content

Commit 7a34d08

Browse files
committed
Apply reviewer feedback: update code examples
1 parent 03ecae0 commit 7a34d08

File tree

1 file changed

+29
-50
lines changed

1 file changed

+29
-50
lines changed

docs/source/en/model_doc/bert-generation.md

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ rendered properly in your Markdown viewer.
2424

2525
[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.
2626

27-
2827
You can find all the original BERT checkpoints under the [BERT](https://huggingface.co/collections/google/bert-release-64ff5e7a4be99045d1896dbc) collection.
2928

3029
> [!TIP]
@@ -38,37 +37,34 @@ The example below demonstrates how to use BertGeneration with [`EncoderDecoderMo
3837
<hfoption id="Pipeline">
3938

4039
```python
40+
import torch
4141
from transformers import pipeline
4242

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 ")
4750
```
4851

4952
</hfoption>
5053
<hfoption id="AutoModel">
5154

5255
```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
6158

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")
6461

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
6765

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]))
7268
```
7369

7470
</hfoption>
@@ -86,53 +82,36 @@ Quantization reduces the memory burden of large models by representing the weigh
8682
The example below uses [BitsAndBytesConfig](../quantizationbitsandbytes) to quantize the weights to 4-bit.
8783

8884
```python
89-
from transformers import BertGenerationEncoder, BertTokenizer, BitsAndBytesConfig
9085
import torch
86+
from transformers import EncoderDecoderModel, AutoTokenizer, BitsAndBytesConfig
9187

9288
# Configure 4-bit quantization
9389
quantization_config = BitsAndBytesConfig(
9490
load_in_4bit=True,
9591
bnb_4bit_compute_dtype=torch.float16
9692
)
9793

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",
10196
quantization_config=quantization_config,
102-
bos_token_id=101,
103-
eos_token_id=102
97+
torch_dtype="auto"
10498
)
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]))
106107
```
107108

108109
## Notes
109110

110111
- [`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>
113112
- For summarization, sentence splitting, sentence fusion and translation, no special tokens are required for the input.
114113
- No EOS token should be added to the end of the input for most generation tasks.
115114

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-
136115
## BertGenerationConfig
137116

138117
[[autodoc]] BertGenerationConfig

0 commit comments

Comments
 (0)