Back to Transformers

GPT-NeoX

docs/source/en/model_doc/gpt_neox.md

5.8.012.3 KB
Original Source
<!--Copyright 2022 The HuggingFace Team. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. ⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be rendered properly in your Markdown viewer. -->

This model was released on 2022-04-14 and added to Hugging Face Transformers on 2022-05-24.

GPT-NeoX

<div class="flex flex-wrap space-x-1"> </div>

Overview

We introduce GPT-NeoX-20B, a 20 billion parameter autoregressive language model trained on the Pile, whose weights will be made freely and openly available to the public through a permissive license. It is, to the best of our knowledge, the largest dense autoregressive model that has publicly available weights at the time of submission. In this work, we describe GPT-NeoX-20B's architecture and training and evaluate its performance on a range of language-understanding, mathematics, and knowledge-based tasks. We find that GPT-NeoX-20B is a particularly powerful few-shot reasoner and gains far more in performance when evaluated five-shot than similarly sized GPT-3 and FairSeq models. We open-source the training and evaluation code, as well as the model weights, at https://github.com/EleutherAI/gpt-neox.

Development of the model was led by Sid Black, Stella Biderman and Eric Hallahan, and the model was trained with generous the support of CoreWeave.

GPT-NeoX-20B was trained with fp16, thus it is recommended to initialize the model as follows:

python
model = GPTNeoXForCausalLM.from_pretrained("EleutherAI/gpt-neox-20b", device_map="auto")

GPT-NeoX-20B also has a different tokenizer from the one used in GPT-J-6B and GPT-Neo. The new tokenizer allocates additional tokens to whitespace characters, making the model more suitable for certain tasks like code generation.

Usage example

The generate() method can be used to generate text using GPT Neo model.

python
from transformers import GPTNeoXForCausalLM, GPTNeoXTokenizerFast


model = GPTNeoXForCausalLM.from_pretrained("EleutherAI/gpt-neox-20b", device_map="auto")
tokenizer = GPTNeoXTokenizerFast.from_pretrained("EleutherAI/gpt-neox-20b")

prompt = "GPTNeoX20B is a 20B-parameter autoregressive Transformer model developed by EleutherAI."

input_ids = tokenizer(prompt, return_tensors="pt").to(model.device).input_ids

gen_tokens = model.generate(
    input_ids,
    do_sample=True,
    temperature=0.9,
    max_length=100,
)
gen_text = tokenizer.batch_decode(gen_tokens)[0]

Using Flash Attention 2

Flash Attention 2 is an faster, optimized version of the model.

Installation

First, check whether your hardware is compatible with Flash Attention 2. The latest list of compatible hardware can be found in the official documentation.

Next, install the latest version of Flash Attention 2:

bash
pip install -U flash-attn --no-build-isolation

Usage

To load a model using Flash Attention 2, we can pass the argument attn_implementation="flash_attention_2" to .from_pretrained. We'll also load the model in half-precision (e.g. torch.float16), since it results in almost no degradation to audio quality but significantly lower memory usage and faster inference:

python
from transformers import GPTNeoXForCausalLM


model = GPTNeoXForCausalLM.from_pretrained("EleutherAI/gpt-neox-20b", attn_implementation="flash_attention_2", device_map="auto")
...

Expected speedups

Below is an expected speedup diagram that compares pure inference time between the native implementation in transformers using stockmark/gpt-neox-japanese-1.4b checkpoint and the Flash Attention 2 version of the model using a sequence length of 2048.

<div style="text-align: center"> </div>

Using Scaled Dot Product Attention (SDPA)

PyTorch includes a native scaled dot-product attention (SDPA) operator as part of torch.nn.functional. This function encompasses several implementations that can be applied depending on the inputs and the hardware in use. See the official documentation or the GPU Inference page for more information.

SDPA is used by default for torch>=2.1.1 when an implementation is available, but you may also set attn_implementation="sdpa" in from_pretrained() to explicitly request SDPA to be used.

python
from transformers import GPTNeoXForCausalLM


model = GPTNeoXForCausalLM.from_pretrained("EleutherAI/gpt-neox-20b", attn_implementation="sdpa", device_map="auto")
...

For the best speedups, we recommend loading the model in half-precision (e.g. torch.float16 or torch.bfloat16).

On a local benchmark (rtx3080ti-16GB, PyTorch 2.2.1, OS Ubuntu 22.04) using float16 with pythia-410m-deduped, we saw the following speedups during training and inference.

Training

Batch sizeSeq lenTime per batch (Eager - s)Time per batch (SDPA - s)Speedup (%)Eager peak mem (MB)SDPA peak mem (MB)Mem saving (%)
11280.0240.01928.9451789.951789.950
12560.0390.03123.181845.831844.840.053
15120.080.05545.5242278.381953.7616.615
110240.190.10286.7774772.362408.3598.159
120480.5650.204177.09813484.13882.01247.348
21280.0370.03215.1211843.861844.78-0.05
22560.0670.05521.7061999.721951.672.462
25120.1440.09650.0463613.162406.7750.125
210240.3660.19389.6668707.553878.86124.487
22048OOM0.379/OOM6825.13SDPA does not OOM
41280.060.05411.5391947.61952.06-0.228
42560.1190.09328.0723008.392405.9925.038
45120.2750.18747.1456290.583877.2962.242
41024OOM0.36/OOM6821.98SDPA does not OOM
42048OOM0.731/OOM12705.1SDPA does not OOM

Inference

Batch sizeSeq lenPer token latency Eager (ms)Per token latency SDPA (ms)Speedup (%)Mem Eager (MB)Mem SDPA (MB)Mem saved (%)
11286.5695.85812.14974.831974.8260
12567.0095.86319.5421029.011028.080.09
15127.1575.96519.9831137.541137.520.001
110247.5236.50615.6371329.31329.260.003
120489.2719.2050.7131752.471734.511.036
21287.2395.95921.4931044.81028.371.597
22567.2286.03619.7571167.321137.732.601
25127.5386.69312.6281352.931329.551.758
210248.9168.6323.2911752.561734.621.034
2204812.62812.6060.1812558.722545.80.508
41287.2786.04620.3731168.411137.792.691
42567.6146.58815.5741353.11329.791.753
45128.7988.1448.0281752.761734.851.032
4102411.76511.3034.092558.962546.040.508
4204819.56817.73510.334175.54165.260.246

Resources

GPTNeoXConfig

[[autodoc]] GPTNeoXConfig

GPTNeoXTokenizer

[[autodoc]] GPTNeoXTokenizer

GPTNeoXTokenizerFast

[[autodoc]] GPTNeoXTokenizerFast

GPTNeoXModel

[[autodoc]] GPTNeoXModel - forward

GPTNeoXForCausalLM

[[autodoc]] GPTNeoXForCausalLM - forward

GPTNeoXForQuestionAnswering

[[autodoc]] GPTNeoXForQuestionAnswering - forward

GPTNeoXForSequenceClassification

[[autodoc]] GPTNeoXForSequenceClassification - forward

GPTNeoXForTokenClassification

[[autodoc]] GPTNeoXForTokenClassification - forward