Possibly wrong position_ids shape in GPT2Model doc
Author: kkew3
Pull request: no

The docs [GPT2Model > position_ids](https://huggingface.co/docs/transformers/v4.51.3/model_doc/gpt2#transformers.GPT2Model.forward.position_ids) and [GPT2LMHeadModel > position_ids](https://huggingface.co/docs/transformers/v4.51.3/model_doc/gpt2#transformers.GPT2LMHeadModel.forward.position_ids) appear to be wrong.
IMO the `position_ids` shape should be `(batch_size, input_ids_length)` rather than `(batch_size, sequence_length)`, since in the code:

https://github.com/huggingface/transformers/blob/fee1190601b5d04ec6d3f7f58fd22788d7f3236d/src/transformers/models/gpt2/modeling_gpt2.py#L918-L921

clearly `input_ids` and `position_ids` should have the same shape.

Attached below is the screenshot of the doc website for `v4.51.3`:

![Image](https://github.com/user-attachments/assets/a9f62dd4-b0da-40e4-9da0-86b643c50559)

Comments:
Hi @kkew3, yes, this seems like a mistake! There can be confusion in the docs because `sequence_length` and `input_ids_length` are identical when `past_key_values` are not being used.

Would you be willing to open a PR to fix it? If you could search for the same error in other model docs too that would be even better!

I'm glad to fix it, and will see if I can find similar fixes for other models.

@kkew3 @Rocketknight1 
I have a doubt.

Documentation mentions: 
> If `past_key_values` is used, only `input_ids` that do not have their past calculated should be passed as `input_ids`.

> `input_ids_length` = `sequence_length` if `past_key_values` is None else `past_key_values[0][0].shape[-2]` (`sequence_length` of input past key value states)

Isn't it that when `past_key_values` is present, `past_key_values[0][0].shape[-2]` is also same as `sequence_length`?
In that case, when `past_key_values` is present, `input_ids_length` should be length of `input_ids`.

When `past_key_values` is present, `input_ids_length` will be the length of `input_ids`, and `sequence_length` equals `past_key_values[0][0].shape[-2] + input_ids_length`. Otherwise, `input_ids_length` equals `sequence_length`. Does it help?

I understood.

https://huggingface.co/docs/transformers/main/en/model_doc/gpt2#transformers.GPT2Model.forward.input_ids mentions

>` input_ids_length` = `sequence_length` if `past_key_values` is None else `past_key_values[0][0].shape[-2]` (`sequence_length` of input past key value states). Indices of input sequence tokens in the vocabulary.

My question is:
Shouldn't the **else** part of the above definition mentioned in the above documentation change to what you have mentioned i.e. length of `input_ids`? 

`past_key_values[0][0].shape[-2]` will not be equal to the length of `input_ids` (i.e. `input_ids_length`) any time by definition. The sequence length of `past_key_values` is the length of tokens given before, whereas the length of `input_ids` is the length of tokens newly provided. The tokens implied by `past_key_values` and `input_ids` are disjoint sets of tokens.

Indeed, the else part of the doc appears to read:

> `input_ids_length` = `past_key_values[0][0].shape[-2]` (`sequence_length` of input past key value states). Indices of input sequence tokens in the vocabulary.

I guess this is the bugbear that causes your confusion, which IMHO is kind of misleading.