Skip to content

GPT-NeoX

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

PyTorch SDPA

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:

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

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.

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

>>> from transformers import GPTNeoXForCausalLM, GPTNeoXTokenizerFast
>>> model = GPTNeoXForCausalLM.from_pretrained("EleutherAI/gpt-neox-20b")
>>> 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").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]

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

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:

Terminal window
pip install -U flash-attn --no-build-isolation

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:

>>> from transformers import GPTNeoXForCausalLM, GPTNeoXTokenizerFast
model = GPTNeoXForCausalLM.from_pretrained("EleutherAI/gpt-neox-20b", dtype=torch.float16, attn_implementation="flash_attention_2").to(device)
...

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.

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.

from transformers import GPTNeoXForCausalLM
model = GPTNeoXForCausalLM.from_pretrained("EleutherAI/gpt-neox-20b", dtype=torch.float16, attn_implementation="sdpa")
...

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.

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
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

[[autodoc]] GPTNeoXConfig

[[autodoc]] GPTNeoXTokenizer

[[autodoc]] GPTNeoXTokenizerFast

[[autodoc]] GPTNeoXModel - forward

[[autodoc]] GPTNeoXForCausalLM - forward

[[autodoc]] GPTNeoXForQuestionAnswering - forward

[[autodoc]] GPTNeoXForSequenceClassification - forward

[[autodoc]] GPTNeoXForTokenClassification - forward