cd /news/large-language-models/last-layer-hidden-state-gpt2 · home topics large-language-models article
[ARTICLE · art-72874] src=discuss.huggingface.co ↗ pub= topic=large-language-models verified=true sentiment=· neutral

Last layer hidden state: GPT2

A user investigating GPT-2's last hidden state in Hugging Face Transformers 3.1.0 found that the final hidden state (layer 12) is reported after the last decoder block but before the final LayerNorm (ln_f), contrary to some expectations. The user observed that applying ln_f manually produced values within precision limits of hidden state 12, suggesting the normalization has negligible effect.

read1 min views1 publishedJul 25, 2026
Last layer hidden state: GPT2
Image: Discuss (auto-discovered)

Hi, I am trying to understand exactly where the last last hidden output in GPT2 stems from. The following code outputs hidden states from the embedding layer and all layers (1+12=13 layers):

tokenizer = GPT2Tokenizer.from_pretrained(‘gpt2’)

model = GPT2Model.from_pretrained(‘gpt2’)

input_ids = torch.tensor(tokenizer.encode(sent))

outputs = model(input_ids, output_hidden_states=True, return_dict=True)

hidden_states = result_model[‘hidden_states’]

As GPT2 has a LayerNorm layer after the very last decoder block (ln_f: LayerNorm(768)), are the last hidden states extracted before this final normalization layer (after the last decoder block) or after this final normalization layer?

I am running Transformers 3.1.0.

Thanks so much!

From what I read, it seemed like GPT-2’s last hidden state would not have gone through a layer norm and that I would have to hook ln_f to have a comparable highest layer. However, when I did that, I found that every value from ln_f was within precision limitations of hidden state 12 (over 1290x16x768). Can anyone give a definitive answer to what is being reported as hidden state 12?

── more in #large-language-models 4 stories · sorted by recency
── more on @gpt-2 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/last-layer-hidden-st…] indexed:0 read:1min 2026-07-25 ·