|
| 1 | +<!--Copyright 2022 The HuggingFace Team. All rights reserved. |
| 2 | + |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with |
| 4 | +the License. You may obtain a copy of the License at |
| 5 | + |
| 6 | +http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + |
| 8 | +Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on |
| 9 | +an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 10 | +specific language governing permissions and limitations under the License. |
| 11 | +--> |
| 12 | + |
| 13 | +# LongT5 |
| 14 | + |
| 15 | +## Overview |
| 16 | + |
| 17 | +The LongT5 model was proposed in [LongT5: Efficient Text-To-Text Transformer for Long Sequences](https://arxiv.org/abs/2112.07916) |
| 18 | +by Mandy Guo, Joshua Ainslie, David Uthus, Santiago Ontanon, Jianmo Ni, Yun-Hsuan Sung and Yinfei Yang. It's an |
| 19 | +encoder-decoder transformer pre-trained in a text-to-text denoising generative setting. LongT5 model is an extension of |
| 20 | +T5 model, and it enables using one of the two different efficient attention mechanisms - (1) Local attention, or (2) |
| 21 | +Transient-Global attention. |
| 22 | + |
| 23 | + |
| 24 | +The abstract from the paper is the following: |
| 25 | + |
| 26 | +*Recent work has shown that either (1) increasing the input length or (2) increasing model size can improve the |
| 27 | +performance of Transformer-based neural models. In this paper, we present a new model, called LongT5, with which we |
| 28 | +explore the effects of scaling both the input length and model size at the same time. Specifically, we integrated |
| 29 | +attention ideas from long-input transformers (ETC), and adopted pre-training strategies from summarization pre-training |
| 30 | +(PEGASUS) into the scalable T5 architecture. The result is a new attention mechanism we call {\em Transient Global} |
| 31 | +(TGlobal), which mimics ETC's local/global attention mechanism, but without requiring additional side-inputs. We are |
| 32 | +able to achieve state-of-the-art results on several summarization tasks and outperform the original T5 models on |
| 33 | +question answering tasks.* |
| 34 | + |
| 35 | +Tips: |
| 36 | + |
| 37 | +- [`LongT5ForConditionalGeneration`] is an extension of [`T5ForConditionalGeneration`] exchanging the traditional |
| 38 | +encoder *self-attention* layer with efficient either *local* attention or *transient-global* (*tglobal*) attention. |
| 39 | +- Unlike the T5 model, LongT5 does not use a task prefix. Furthermore, it uses a different pre-training objective |
| 40 | +inspired by the pre-training of `[PegasusForConditionalGeneration]`. |
| 41 | +- LongT5 model is designed to work efficiently and very well on long-range *sequence-to-sequence* tasks where the |
| 42 | +input sequence exceeds commonly used 512 tokens. It is capable of handling input sequences of a length up to 16,384 tokens. |
| 43 | +- For *Local Attention*, the sparse sliding-window local attention operation allows a given token to attend only `r` |
| 44 | +tokens to the left and right of it (with `r=127` by default). *Local Attention* does not introduce any new parameters |
| 45 | +to the model. The complexity of the mechanism is linear in input sequence length `l`: `O(l*r)`. |
| 46 | +- *Transient Global Attention* is an extension of the *Local Attention*. It, furthermore, allows each input token to |
| 47 | +interact with all other tokens in the layer. This is achieved via splitting an input sequence into blocks of a fixed |
| 48 | +length `k` (with a default `k=16`). Then, a global token for such a block is obtained via summing and normalizing the embeddings of every token |
| 49 | +in the block. Thanks to this, the attention allows each token to attend to both nearby tokens like in Local attention, and |
| 50 | +also every global token like in the case of standard global attention (*transient* represents the fact the global tokens |
| 51 | +are constructed dynamically within each attention operation). As a consequence, *TGlobal* attention introduces |
| 52 | +a few new parameters -- global relative position biases and a layer normalization for global token's embedding. |
| 53 | +The complexity of this mechanism is `O(l(r + l/k))`. |
| 54 | +- An example showing how to evaluate a fine-tuned LongT5 model on the [pubmed dataset](https://huggingface.co/datasets/scientific_papers) is below. |
| 55 | + |
| 56 | +```python |
| 57 | +>>> import evaluate |
| 58 | +>>> from datasets import load_dataset |
| 59 | +>>> from transformers import AutoTokenizer, LongT5ForConditionalGeneration |
| 60 | +
|
| 61 | +>>> dataset = load_dataset("scientific_papers", "pubmed", split="validation") |
| 62 | +>>> model = ( |
| 63 | +... LongT5ForConditionalGeneration.from_pretrained("Stancld/longt5-tglobal-large-16384-pubmed-3k_steps") |
| 64 | +... .to("cuda") |
| 65 | +... .half() |
| 66 | +... ) |
| 67 | +>>> tokenizer = AutoTokenizer.from_pretrained("Stancld/longt5-tglobal-large-16384-pubmed-3k_steps") |
| 68 | +
|
| 69 | + |
| 70 | +>>> def generate_answers(batch): |
| 71 | +... inputs_dict = tokenizer( |
| 72 | +... batch["article"], max_length=16384, padding="max_length", truncation=True, return_tensors="pt" |
| 73 | +... ) |
| 74 | +... input_ids = inputs_dict.input_ids.to("cuda") |
| 75 | +... attention_mask = inputs_dict.attention_mask.to("cuda") |
| 76 | +... output_ids = model.generate(input_ids, attention_mask=attention_mask, max_length=512, num_beams=2) |
| 77 | +... batch["predicted_abstract"] = tokenizer.batch_decode(output_ids, skip_special_tokens=True) |
| 78 | +... return batch |
| 79 | + |
| 80 | + |
| 81 | +>>> result = dataset.map(generate_answer, batched=True, batch_size=2) |
| 82 | +>>> rouge = evaluate.load("rouge") |
| 83 | +>>> rouge.compute(predictions=result["predicted_abstract"], references=result["abstract"]) |
| 84 | +``` |
| 85 | +
|
| 86 | +This model was contributed by [stancld](https://huggingface.co/stancld). |
| 87 | +The original code can be found [here](https://github.com/google-research/longt5). |
| 88 | +
|
| 89 | +
|
| 90 | +## LongT5Config |
| 91 | +
|
| 92 | +[[autodoc]] LongT5Config |
| 93 | +
|
| 94 | +## LongT5Model |
| 95 | +
|
| 96 | +[[autodoc]] LongT5Model |
| 97 | + - forward |
| 98 | +
|
| 99 | +## LongT5ForConditionalGeneration |
| 100 | +
|
| 101 | +[[autodoc]] LongT5ForConditionalGeneration |
| 102 | + - forward |
| 103 | +
|
| 104 | +## LongT5EncoderModel |
| 105 | +
|
| 106 | +[[autodoc]] LongT5EncoderModel |
| 107 | + - forward |
| 108 | +
|
| 109 | +## FlaxLongT5Model |
| 110 | +
|
| 111 | +[[autodoc]] FlaxLongT5Model |
| 112 | + - __call__ |
| 113 | + - encode |
| 114 | + - decode |
| 115 | +
|
| 116 | +## FlaxLongT5ForConditionalGeneration |
| 117 | +
|
| 118 | +[[autodoc]] FlaxLongT5ForConditionalGeneration |
| 119 | + - __call__ |
| 120 | + - encode |
| 121 | + - decode |
0 commit comments