Repository: pytorch
Issue: [PT2] Failed to capture graph in export with dynamic shape inputs for LLM models
Labels: triaged,oncall: pt2,module: dynamic shapes,export-triaged,oncall: export

### 🐛 Describe the bug

We aim to apply `pt2e quantization` for LLM models, but encounter challenges in capturing graphs with inputs of  dynamic shape despite setting constraints in `capture_pre_autograd_graph`. In LLM models, sentence generation is performed using `model.generate()`, which internally invokes `model.forward()` multiple times to generate tokens. The issue arises from the dynamic changes in input shape with each call to `model.forward()` inside `model.generate()`.

Assume that the size of the prompt is `x`, batch size is `bs`, number of beams for beam search is `nb`, and input shape change rule is as follows:

- Initial inputs shapes (for first token):
  - Input_ids: `(bs, x)`
  - Attention_mask: `(bs, x)`

- For i-th next tokens:
  - Input_ids: `(bs\*nb, 1)`
  - Position_ids: `(bs\*nb, 1)`
  - Attention_mask: `(bs*nb, x + i + 1)`
  - Past_key_values: `num_hidden_layers\*((bs\*nb, num_attention_heads, x+i, head_dim),(bs\*nb, num_attention_heads, x+i, head_dim))`

As shown in the input shape change rule, some shapes need to be treated as dynamic shapes when capturing graph so that the captured graph can be applied for all tokens generation. 

To make things easier, we tried to capture two graphs, one for the first token, and one for the next token. And it turns out that we can succeed to capture the graph for first token and it works fine. However, we are unable to capture expected graph for next tokens. 

### Unit Test
Here is a unit test that reproduces the issue I met.
```
import torch
from torch._inductor import config as inductor_config
from transformers import AutoModelForCausalLM
inductor_config.cpp_wrapper = True
from torch._export import capture_pre_autograd_graph, dynamic_dim
torch._dynamo.config.suppress_errors = True
model = AutoModelForCausalLM.from_pretrained("EleutherAI/gpt-j-6b")
past_key_values = tuple(
    (
        torch.zeros(
            [
                1,
                model.config.num_attention_heads,
                1,
                int(model.config.hidden_size / model.config.num_attention_heads),
            ]
        ).contiguous(),
        torch.zeros(
            [
                1,
                model.config.num_attention_heads,
                1,
                int(model.config.hidden_size / model.config.num_attention_heads),
            ]
        ).contiguous(),
    )
    for i in range(model.config.num_hidden_layers)
)
input_ids = torch.ones(1, 1).to(torch.long)
position_ids = torch.arange(1).unsqueeze(0)
attention_mask = torch.ones(1, input_ids.shape[1] + past_key_values[0][0].shape[2])
example_inputs = {
    "input_ids": input_ids,
    "attention_mask": attention_mask,
    "position_ids": position_ids,
    "past_key_values": past_key_values,
}
input_constraints = [
    dynamic_dim(example_inputs["attention_mask"], 1),
]
for i in range(model.config.num_hidden_layers):
    input_constraints.append(dynamic_dim(example_inputs["past_key_values"][i][0], 2))
    input_constraints.append(dynamic_dim(example_inputs["past_key_values"][i][1], 2))
exported_model = capture_pre_autograd_graph(
    model, (), kwargs=example_inputs, constraints=input_constraints
)
print(exported_model)

```

### Error Message
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR] Error while creating guard:
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR] Name: ''
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]     Source: shape_env
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]     Create Function: SHAPE_ENV
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]     Guard Types: None
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]     Code List: None
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]     Object Weakref: None
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]     Guarded Class Weakref: None
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR] Created at:
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]   File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 471, in transform
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]     tracer = InstructionTranslator(
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]   File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/symbolic_convert.py", line 2041, in __init__
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]     output=OutputGraph(
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]   File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/output_graph.py", line 297, in __init__
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]     self.init_ambient_guards()
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]   File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/output_graph.py", line 375, in init_ambient_guards
[2024-01-11 17:44:58,216] [0/0] torch._guards: [ERROR]     self.guards.add(ShapeEnvSource().make_guard(GuardBuilder.SHAPE_ENV))
Traceback (most recent call last):
  File "unit_test.py", line 52, in <module>
    exported_model = capture_pre_autograd_graph(
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_export/__init__.py", line 133, in capture_pre_autograd_graph
    m = torch._dynamo.export(
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py", line 1308, in inner
    raise constraint_violation_error
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py", line 1268, in inner
    result_traced = opt_f(*args, **kwargs)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py", line 417, in _fn
    return fn(*args, **kwargs)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
    return self._call_impl(*args, **kwargs)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
    return forward_call(*args, **kwargs)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/eval_frame.py", line 580, in catch_errors
    return callback(frame, cache_entry, hooks, frame_state)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 384, in _convert_frame_assert
    return _compile(
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 643, in _compile
    guarded_code = compile_inner(code, one_graph, hooks, transform)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/utils.py", line 246, in time_wrapper
    r = func(*args, **kwargs)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/convert_frame.py", line 623, in compile_inner
    check_fn = CheckFunctionManager(
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/guards.py", line 994, in __init__
    guard.create(builder)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_guards.py", line 246, in create
    return self.create_fn(builder, self)
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/_dynamo/guards.py", line 646, in SHAPE_ENV
    guards = output_graph.shape_env.produce_guards(
  File "/home/bzheng/anaconda3/envs/test/lib/python3.8/site-packages/torch/fx/experimental/symbolic_shapes.py", line 3019, in produce_guards
    raise ConstraintViolationError(
torch.fx.experimental.symbolic_shapes.ConstraintViolationError: Constraints violated (L['past_key_values'][7][0].size()[2], L['past_key_values'][14][0].size()[2], L['past_key_values'][4][1].size()[2], L['past_key_values'][19][0].size()[2], L['past_key_values'][12][0].size()[2], L['past_key_values'][5][1].size()[2], L['past_key_values'][16][1].size()[2], L['past_key_values'][17][1].size()[2], L['attention_mask'].size()[1], L['past_key_values'][1][1].size()[2], L['past_key_values'][2][1].size()[2], L['past_key_values'][3][0].size()[2], L['past_key_values'][4][0].size()[2], L['past_key_values'][18][1].size()[2], L['past_key_values'][3][1].size()[2], L['past_key_values'][25][1].size()[2], L['past_key_values'][8][0].size()[2], L['past_key_values'][20][0].size()[2], L['past_key_values'][11][1].size()[2], L['past_key_values'][12][1].size()[2], L['past_key_values'][0][1].size()[2], L['past_key_values'][1][0].size()[2], L['past_key_values'][27][1].size()[2], L['past_key_values'][23][0].size()[2], L['past_key_values'][21][0].size()[2], L['past_key_values'][10][0].size()[2], L['past_key_values'][2][0].size()[2], L['past_key_values'][0][0].size()[2], L['past_key_values'][13][0].size()[2], L['past_key_values'][9][1].size()[2], L['past_key_values'][11][0].size()[2], L['past_key_values'][22][0].size()[2], L['past_key_values'][21][1].size()[2], L['past_key_values'][20][1].size()[2], L['past_key_values'][27][0].size()[2], L['past_key_values'][13][1].size()[2], L['past_key_values'][25][0].size()[2], L['past_key_values'][24][1].size()[2], L['past_key_values'][6][0].size()[2], L['past_key_values'][18][0].size()[2], L['past_key_values'][5][0].size()[2], L['past_key_values'][7][1].size()[2], L['past_key_values'][26][0].size()[2], L['past_key_values'][6][1].size()[2], L['past_key_values'][10][1].size()[2], L['past_key_values'][9][0].size()[2], L['past_key_values'][23][1].size()[2], L['past_key_values'][15][0].size()[2], L['past_key_values'][8][1].size()[2], L['past_key_values'][15][1].size()[2], L['past_key_values'][22][1].size()[2], L['past_key_values'][26][1].size()[2], L['past_key_values'][14][1].size()[2], L['past_key_values'][17][0].size()[2], L['past_key_values'][24][0].size()[2], L['past_key_values'][19][1].size()[2], L['past_key_values'][16][0].size()[2])! For more information, run with TORCH_LOGS=dynamic.
  - Not all values of L['past_key_values'][0][0].size()[2] = L['past_key_values'][0][0].size()[2] in the specified range are valid because L['past_key_values'][0][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][0][1].size()[2] = L['past_key_values'][0][1].size()[2] in the specified range are valid because L['past_key_values'][0][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][1][0].size()[2] = L['past_key_values'][1][0].size()[2] in the specified range are valid because L['past_key_values'][1][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][1][1].size()[2] = L['past_key_values'][1][1].size()[2] in the specified range are valid because L['past_key_values'][1][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][2][0].size()[2] = L['past_key_values'][2][0].size()[2] in the specified range are valid because L['past_key_values'][2][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][2][1].size()[2] = L['past_key_values'][2][1].size()[2] in the specified range are valid because L['past_key_values'][2][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][3][0].size()[2] = L['past_key_values'][3][0].size()[2] in the specified range are valid because L['past_key_values'][3][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][3][1].size()[2] = L['past_key_values'][3][1].size()[2] in the specified range are valid because L['past_key_values'][3][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][4][0].size()[2] = L['past_key_values'][4][0].size()[2] in the specified range are valid because L['past_key_values'][4][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][4][1].size()[2] = L['past_key_values'][4][1].size()[2] in the specified range are valid because L['past_key_values'][4][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][5][0].size()[2] = L['past_key_values'][5][0].size()[2] in the specified range are valid because L['past_key_values'][5][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][5][1].size()[2] = L['past_key_values'][5][1].size()[2] in the specified range are valid because L['past_key_values'][5][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][6][0].size()[2] = L['past_key_values'][6][0].size()[2] in the specified range are valid because L['past_key_values'][6][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][6][1].size()[2] = L['past_key_values'][6][1].size()[2] in the specified range are valid because L['past_key_values'][6][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][7][0].size()[2] = L['past_key_values'][7][0].size()[2] in the specified range are valid because L['past_key_values'][7][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][7][1].size()[2] = L['past_key_values'][7][1].size()[2] in the specified range are valid because L['past_key_values'][7][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][8][0].size()[2] = L['past_key_values'][8][0].size()[2] in the specified range are valid because L['past_key_values'][8][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][8][1].size()[2] = L['past_key_values'][8][1].size()[2] in the specified range are valid because L['past_key_values'][8][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][9][0].size()[2] = L['past_key_values'][9][0].size()[2] in the specified range are valid because L['past_key_values'][9][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][9][1].size()[2] = L['past_key_values'][9][1].size()[2] in the specified range are valid because L['past_key_values'][9][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][10][0].size()[2] = L['past_key_values'][10][0].size()[2] in the specified range are valid because L['past_key_values'][10][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][10][1].size()[2] = L['past_key_values'][10][1].size()[2] in the specified range are valid because L['past_key_values'][10][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][11][0].size()[2] = L['past_key_values'][11][0].size()[2] in the specified range are valid because L['past_key_values'][11][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][11][1].size()[2] = L['past_key_values'][11][1].size()[2] in the specified range are valid because L['past_key_values'][11][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][12][0].size()[2] = L['past_key_values'][12][0].size()[2] in the specified range are valid because L['past_key_values'][12][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][12][1].size()[2] = L['past_key_values'][12][1].size()[2] in the specified range are valid because L['past_key_values'][12][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][13][0].size()[2] = L['past_key_values'][13][0].size()[2] in the specified range are valid because L['past_key_values'][13][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][13][1].size()[2] = L['past_key_values'][13][1].size()[2] in the specified range are valid because L['past_key_values'][13][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][14][0].size()[2] = L['past_key_values'][14][0].size()[2] in the specified range are valid because L['past_key_values'][14][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][14][1].size()[2] = L['past_key_values'][14][1].size()[2] in the specified range are valid because L['past_key_values'][14][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][15][0].size()[2] = L['past_key_values'][15][0].size()[2] in the specified range are valid because L['past_key_values'][15][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][15][1].size()[2] = L['past_key_values'][15][1].size()[2] in the specified range are valid because L['past_key_values'][15][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][16][0].size()[2] = L['past_key_values'][16][0].size()[2] in the specified range are valid because L['past_key_values'][16][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][16][1].size()[2] = L['past_key_values'][16][1].size()[2] in the specified range are valid because L['past_key_values'][16][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][17][0].size()[2] = L['past_key_values'][17][0].size()[2] in the specified range are valid because L['past_key_values'][17][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][17][1].size()[2] = L['past_key_values'][17][1].size()[2] in the specified range are valid because L['past_key_values'][17][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][18][0].size()[2] = L['past_key_values'][18][0].size()[2] in the specified range are valid because L['past_key_values'][18][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][18][1].size()[2] = L['past_key_values'][18][1].size()[2] in the specified range are valid because L['past_key_values'][18][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][19][0].size()[2] = L['past_key_values'][19][0].size()[2] in the specified range are valid because L['past_key_values'][19][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][19][1].size()[2] = L['past_key_values'][19][1].size()[2] in the specified range are valid because L['past_key_values'][19][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][20][0].size()[2] = L['past_key_values'][20][0].size()[2] in the specified range are valid because L['past_key_values'][20][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][20][1].size()[2] = L['past_key_values'][20][1].size()[2] in the specified range are valid because L['past_key_values'][20][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][21][0].size()[2] = L['past_key_values'][21][0].size()[2] in the specified range are valid because L['past_key_values'][21][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][21][1].size()[2] = L['past_key_values'][21][1].size()[2] in the specified range are valid because L['past_key_values'][21][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][22][0].size()[2] = L['past_key_values'][22][0].size()[2] in the specified range are valid because L['past_key_values'][22][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][22][1].size()[2] = L['past_key_values'][22][1].size()[2] in the specified range are valid because L['past_key_values'][22][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][23][0].size()[2] = L['past_key_values'][23][0].size()[2] in the specified range are valid because L['past_key_values'][23][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][23][1].size()[2] = L['past_key_values'][23][1].size()[2] in the specified range are valid because L['past_key_values'][23][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][24][0].size()[2] = L['past_key_values'][24][0].size()[2] in the specified range are valid because L['past_key_values'][24][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][24][1].size()[2] = L['past_key_values'][24][1].size()[2] in the specified range are valid because L['past_key_values'][24][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][25][0].size()[2] = L['past_key_values'][25][0].size()[2] in the specified range are valid because L['past_key_values'][25][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][25][1].size()[2] = L['past_key_values'][25][1].size()[2] in the specified range are valid because L['past_key_values'][25][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][26][0].size()[2] = L['past_key_values'][26][0].size()[2] in the specified range are valid because L['past_key_values'][26][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][26][1].size()[2] = L['past_key_values'][26][1].size()[2] in the specified range are valid because L['past_key_values'][26][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][27][0].size()[2] = L['past_key_values'][27][0].size()[2] in the specified range are valid because L['past_key_values'][27][0].size()[2] was inferred to be a constant (1).
  - Not all values of L['past_key_values'][27][1].size()[2] = L['past_key_values'][27][1].size()[2] in the specified range are valid because L['past_key_values'][27][1].size()[2] was inferred to be a constant (1).
  - Not all values of L['attention_mask'].size()[1] = L['attention_mask'].size()[1] in the specified range are valid because L['attention_mask'].size()[1] was inferred to be a constant (2).

The following dimensions have been specialized and CANNOT be dynamic.
```
def specializations(input_ids: Union[torch.LongTensor, NoneType] = None, past_key_values: Union[Tuple[Tuple[torch.Tensor]], NoneType] = None, attention_mask: Union[torch.FloatTensor, NoneType] = None, token_type_ids: Union[torch.LongTensor, NoneType] = None, position_ids: Union[torch.LongTensor, NoneType] = None, head_mask: Union[torch.FloatTensor, NoneType] = None, inputs_embeds: Union[torch.FloatTensor, NoneType] = None, labels: Union[torch.LongTensor, NoneType] = None, use_cache: Union[bool, NoneType] = None, output_attentions: Union[bool, NoneType] = None, output_hidden_states: Union[bool, NoneType] = None, return_dict: Union[bool, NoneType] = None):
    # input_ids:
    assert input_ids.size()[0] == 1
    assert input_ids.size()[1] == 1

    # past_key_values:
    assert past_key_values[0][0].size()[0] == 1
    assert past_key_values[0][0].size()[1] == 16
    assert past_key_values[0][0].size()[2] == 1
    assert past_key_values[0][0].size()[3] == 256
    assert past_key_values[0][1].size()[0] == 1
    assert past_key_values[0][1].size()[1] == 16
    assert past_key_values[0][1].size()[2] == 1
    assert past_key_values[0][1].size()[3] == 256
    assert past_key_values[10][0].size()[0] == 1
    assert past_key_values[10][0].size()[1] == 16
    assert past_key_values[10][0].size()[2] == 1
    assert past_key_values[10][0].size()[3] == 256
    assert past_key_values[10][1].size()[0] == 1
    assert past_key_values[10][1].size()[1] == 16
    assert past_key_values[10][1].size()[2] == 1
    assert past_key_values[10][1].size()[3] == 256
    assert past_key_values[11][0].size()[0] == 1
    assert past_key_values[11][0].size()[1] == 16
    assert past_key_values[11][0].size()[2] == 1
    assert past_key_values[11][0].size()[3] == 256
    assert past_key_values[11][1].size()[0] == 1
    assert past_key_values[11][1].size()[1] == 16
    assert past_key_values[11][1].size()[2] == 1
    assert past_key_values[11][1].size()[3] == 256
    assert past_key_values[12][0].size()[0] == 1
    assert past_key_values[12][0].size()[1] == 16
    assert past_key_values[12][0].size()[2] == 1
    assert past_key_values[12][0].size()[3] == 256
    assert past_key_values[12][1].size()[0] == 1
    assert past_key_values[12][1].size()[1] == 16
    assert past_key_values[12][1].size()[2] == 1
    assert past_key_values[12][1].size()[3] == 256
    assert past_key_values[13][0].size()[0] == 1
    assert past_key_values[13][0].size()[1] == 16
    assert past_key_values[13][0].size()[2] == 1
    assert past_key_values[13][0].size()[3] == 256
    assert past_key_values[13][1].size()[0] == 1
    assert past_key_values[13][1].size()[1] == 16
    assert past_key_values[13][1].size()[2] == 1
    assert past_key_values[13][1].size()[3] == 256
    assert past_key_values[14][0].size()[0] == 1
    assert past_key_values[14][0].size()[1] == 16
    assert past_key_values[14][0].size()[2] == 1
    assert past_key_values[14][0].size()[3] == 256
    assert past_key_values[14][1].size()[0] == 1
    assert past_key_values[14][1].size()[1] == 16
    assert past_key_values[14][1].size()[2] == 1
    assert past_key_values[14][1].size()[3] == 256
    assert past_key_values[15][0].size()[0] == 1
    assert past_key_values[15][0].size()[1] == 16
    assert past_key_values[15][0].size()[2] == 1
    assert past_key_values[15][0].size()[3] == 256
    assert past_key_values[15][1].size()[0] == 1
    assert past_key_values[15][1].size()[1] == 16
    assert past_key_values[15][1].size()[2] == 1
    assert past_key_values[15][1].size()[3] == 256
    assert past_key_values[16][0].size()[0] == 1
    assert past_key_values[16][0].size()[1] == 16
    assert past_key_values[16][0].size()[2] == 1
    assert past_key_values[16][0].size()[3] == 256
    assert past_key_values[16][1].size()[0] == 1
    assert past_key_values[16][1].size()[1] == 16
    assert past_key_values[16][1].size()[2] == 1
    assert past_key_values[16][1].size()[3] == 256
    assert past_key_values[17][0].size()[0] == 1
    assert past_key_values[17][0].size()[1] == 16
    assert past_key_values[17][0].size()[2] == 1
    assert past_key_values[17][0].size()[3] == 256
    assert past_key_values[17][1].size()[0] == 1
    assert past_key_values[17][1].size()[1] == 16
    assert past_key_values[17][1].size()[2] == 1
    assert past_key_values[17][1].size()[3] == 256
    assert past_key_values[18][0].size()[0] == 1
    assert past_key_values[18][0].size()[1] == 16
    assert past_key_values[18][0].size()[2] == 1
    assert past_key_values[18][0].size()[3] == 256
    assert past_key_values[18][1].size()[0] == 1
    assert past_key_values[18][1].size()[1] == 16
    assert past_key_values[18][1].size()[2] == 1
    assert past_key_values[18][1].size()[3] == 256
    assert past_key_values[19][0].size()[0] == 1
    assert past_key_values[19][0].size()[1] == 16
    assert past_key_values[19][0].size()[2] == 1
    assert past_key_values[19][0].size()[3] == 256
    assert past_key_values[19][1].size()[0] == 1
    assert past_key_values[19][1].size()[1] == 16
    assert past_key_values[19][1].size()[2] == 1
    assert past_key_values[19][1].size()[3] == 256
    assert past_key_values[1][0].size()[0] == 1
    assert past_key_values[1][0].size()[1] == 16
    assert past_key_values[1][0].size()[2] == 1
    assert past_key_values[1][0].size()[3] == 256
    assert past_key_values[1][1].size()[0] == 1
    assert past_key_values[1][1].size()[1] == 16
    assert past_key_values[1][1].size()[2] == 1
    assert past_key_values[1][1].size()[3] == 256
    assert past_key_values[20][0].size()[0] == 1
    assert past_key_values[20][0].size()[1] == 16
    assert past_key_values[20][0].size()[2] == 1
    assert past_key_values[20][0].size()[3] == 256
    assert past_key_values[20][1].size()[0] == 1
    assert past_key_values[20][1].size()[1] == 16
    assert past_key_values[20][1].size()[2] == 1
    assert past_key_values[20][1].size()[3] == 256
    assert past_key_values[21][0].size()[0] == 1
    assert past_key_values[21][0].size()[1] == 16
    assert past_key_values[21][0].size()[2] == 1
    assert past_key_values[21][0].size()[3] == 256
    assert past_key_values[21][1].size()[0] == 1
    assert past_key_values[21][1].size()[1] == 16
    assert past_key_values[21][1].size()[2] == 1
    assert past_key_values[21][1].size()[3] == 256
    assert past_key_values[22][0].size()[0] == 1
    assert past_key_values[22][0].size()[1] == 16
    assert past_key_values[22][0].size()[2] == 1
    assert past_key_values[22][0].size()[3] == 256
    assert past_key_values[22][1].size()[0] == 1
    assert past_key_values[22][1].size()[1] == 16
    assert past_key_values[22][1].size()[2] == 1
    assert past_key_values[22][1].size()[3] == 256
    assert past_key_values[23][0].size()[0] == 1
    assert past_key_values[23][0].size()[1] == 16
    assert past_key_values[23][0].size()[2] == 1
    assert past_key_values[23][0].size()[3] == 256
    assert past_key_values[23][1].size()[0] == 1
    assert past_key_values[23][1].size()[1] == 16
    assert past_key_values[23][1].size()[2] == 1
    assert past_key_values[23][1].size()[3] == 256
    assert past_key_values[24][0].size()[0] == 1
    assert past_key_values[24][0].size()[1] == 16
    assert past_key_values[24][0].size()[2] == 1
    assert past_key_values[24][0].size()[3] == 256
    assert past_key_values[24][1].size()[0] == 1
    assert past_key_values[24][1].size()[1] == 16
    assert past_key_values[24][1].size()[2] == 1
    assert past_key_values[24][1].size()[3] == 256
    assert past_key_values[25][0].size()[0] == 1
    assert past_key_values[25][0].size()[1] == 16
    assert past_key_values[25][0].size()[2] == 1
    assert past_key_values[25][0].size()[3] == 256
    assert past_key_values[25][1].size()[0] == 1
    assert past_key_values[25][1].size()[1] == 16
    assert past_key_values[25][1].size()[2] == 1
    assert past_key_values[25][1].size()[3] == 256
    assert past_key_values[26][0].size()[0] == 1
    assert past_key_values[26][0].size()[1] == 16
    assert past_key_values[26][0].size()[2] == 1
    assert past_key_values[26][0].size()[3] == 256
    assert past_key_values[26][1].size()[0] == 1
    assert past_key_values[26][1].size()[1] == 16
    assert past_key_values[26][1].size()[2] == 1
    assert past_key_values[26][1].size()[3] == 256
    assert past_key_values[27][0].size()[0] == 1
    assert past_key_values[27][0].size()[1] == 16
    assert past_key_values[27][0].size()[2] == 1
    assert past_key_values[27][0].size()[3] == 256
    assert past_key_values[27][1].size()[0] == 1
    assert past_key_values[27][1].size()[1] == 16
    assert past_key_values[27][1].size()[2] == 1
    assert past_key_values[27][1].size()[3] == 256
    assert past_key_values[2][0].size()[0] == 1
    assert past_key_values[2][0].size()[1] == 16
    assert past_key_values[2][0].size()[2] == 1
    assert past_key_values[2][0].size()[3] == 256
    assert past_key_values[2][1].size()[0] == 1
    assert past_key_values[2][1].size()[1] == 16
    assert past_key_values[2][1].size()[2] == 1
    assert past_key_values[2][1].size()[3] == 256
    assert past_key_values[3][0].size()[0] == 1
    assert past_key_values[3][0].size()[1] == 16
    assert past_key_values[3][0].size()[2] == 1
    assert past_key_values[3][0].size()[3] == 256
    assert past_key_values[3][1].size()[0] == 1
    assert past_key_values[3][1].size()[1] == 16
    assert past_key_values[3][1].size()[2] == 1
    assert past_key_values[3][1].size()[3] == 256
    assert past_key_values[4][0].size()[0] == 1
    assert past_key_values[4][0].size()[1] == 16
    assert past_key_values[4][0].size()[2] == 1
    assert past_key_values[4][0].size()[3] == 256
    assert past_key_values[4][1].size()[0] == 1
    assert past_key_values[4][1].size()[1] == 16
    assert past_key_values[4][1].size()[2] == 1
    assert past_key_values[4][1].size()[3] == 256
    assert past_key_values[5][0].size()[0] == 1
    assert past_key_values[5][0].size()[1] == 16
    assert past_key_values[5][0].size()[2] == 1
    assert past_key_values[5][0].size()[3] == 256
    assert past_key_values[5][1].size()[0] == 1
    assert past_key_values[5][1].size()[1] == 16
    assert past_key_values[5][1].size()[2] == 1
    assert past_key_values[5][1].size()[3] == 256
    assert past_key_values[6][0].size()[0] == 1
    assert past_key_values[6][0].size()[1] == 16
    assert past_key_values[6][0].size()[2] == 1
    assert past_key_values[6][0].size()[3] == 256
    assert past_key_values[6][1].size()[0] == 1
    assert past_key_values[6][1].size()[1] == 16
    assert past_key_values[6][1].size()[2] == 1
    assert past_key_values[6][1].size()[3] == 256
    assert past_key_values[7][0].size()[0] == 1
    assert past_key_values[7][0].size()[1] == 16
    assert past_key_values[7][0].size()[2] == 1
    assert past_key_values[7][0].size()[3] == 256
    assert past_key_values[7][1].size()[0] == 1
    assert past_key_values[7][1].size()[1] == 16
    assert past_key_values[7][1].size()[2] == 1
    assert past_key_values[7][1].size()[3] == 256
    assert past_key_values[8][0].size()[0] == 1
    assert past_key_values[8][0].size()[1] == 16
    assert past_key_values[8][0].size()[2] == 1
    assert past_key_values[8][0].size()[3] == 256
    assert past_key_values[8][1].size()[0] == 1
    assert past_key_values[8][1].size()[1] == 16
    assert past_key_values[8][1].size()[2] == 1
    assert past_key_values[8][1].size()[3] == 256
    assert past_key_values[9][0].size()[0] == 1
    assert past_key_values[9][0].size()[1] == 16
    assert past_key_values[9][0].size()[2] == 1
    assert past_key_values[9][0].size()[3] == 256
    assert past_key_values[9][1].size()[0] == 1
    assert past_key_values[9][1].size()[1] == 16
    assert past_key_values[9][1].size()[2] == 1
    assert past_key_values[9][1].size()[3] == 256

    # attention_mask:
    assert attention_mask.size()[0] == 1
    assert attention_mask.size()[1] == 2

    # position_ids:
    assert position_ids.size()[0] == 1
    assert position_ids.size()[1] == 1
```


### Versions
```
Collecting environment information...
PyTorch version: 2.3.0.dev20240104+cpu
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: Ubuntu 22.04.3 LTS (x86_64)
GCC version: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Clang version: Could not collect
CMake version: version 3.22.1
Libc version: glibc-2.35

Python version: 3.8.13 | packaged by conda-forge | (default, Mar 25 2022, 06:04:18)  [GCC 10.3.0] (64-bit runtime)
Python platform: Linux-6.2.0-39-generic-x86_64-with-glibc2.10
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True

CPU:
Architecture:                       x86_64
CPU op-mode(s):                     32-bit, 64-bit
Address sizes:                      52 bits physical, 57 bits virtual
Byte Order:                         Little Endian
CPU(s):                             160
On-line CPU(s) list:                0-159
Vendor ID:                          GenuineIntel
Model name:                         Intel(R) Xeon(R) Platinum 8380 CPU @ 2.30GHz
CPU family:                         6
Model:                              106
Thread(s) per core:                 2
Core(s) per socket:                 40
Socket(s):                          2
Stepping:                           6
CPU max MHz:                        3400.0000
CPU min MHz:                        800.0000
BogoMIPS:                           4600.00
Flags:                              fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid dca sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault epb cat_l3 invpcid_single ssbd mba ibrs ibpb stibp ibrs_enhanced fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid cqm rdt_a avx512f avx512dq rdseed adx smap avx512ifma clflushopt clwb intel_pt avx512cd sha_ni avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves cqm_llc cqm_occup_llc cqm_mbm_total cqm_mbm_local split_lock_detect wbnoinvd dtherm ida arat pln pts hwp hwp_act_window hwp_epp hwp_pkg_req avx512vbmi umip pku ospke avx512_vbmi2 gfni vaes vpclmulqdq avx512_vnni avx512_bitalg tme avx512_vpopcntdq la57 rdpid fsrm md_clear pconfig flush_l1d arch_capabilities
L1d cache:                          3.8 MiB (80 instances)
L1i cache:                          2.5 MiB (80 instances)
L2 cache:                           100 MiB (80 instances)
L3 cache:                           120 MiB (2 instances)
NUMA node(s):                       2
NUMA node0 CPU(s):                  0-39,80-119
NUMA node1 CPU(s):                  40-79,120-159
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit:        Not affected
Vulnerability L1tf:                 Not affected
Vulnerability Mds:                  Not affected
Vulnerability Meltdown:             Not affected
Vulnerability Mmio stale data:      Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Retbleed:             Not affected
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass:    Mitigation; Speculative Store Bypass disabled via prctl
Vulnerability Spectre v1:           Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2:           Mitigation; Enhanced IBRS, IBPB conditional, RSB filling, PBRSB-eIBRS SW sequence
Vulnerability Srbds:                Not affected
Vulnerability Tsx async abort:      Not affected

Versions of relevant libraries:
[pip3] bert-pytorch==0.0.1a4
[pip3] clip-anytorch==2.5.2
[pip3] CoCa-pytorch==0.0.7
[pip3] dalle2-pytorch==1.14.2
[pip3] ema-pytorch==0.2.2
[pip3] flake8==3.8.2
[pip3] flake8-bugbear==20.1.4
[pip3] flake8-coding==1.3.3
[pip3] flake8-comprehensions==3.3.0
[pip3] flake8-executable==2.0.4
[pip3] flake8-pyi==20.5.0
[pip3] functorch==1.14.0a0+b71aa0b
[pip3] intel-extension-for-pytorch==2.3.0+git61ac38f
[pip3] mypy-extensions==1.0.0
[pip3] numpy==1.21.2
[pip3] onnx==1.13.1
[pip3] open-clip-torch==2.20.0
[pip3] pytorch-transformers==1.2.0
[pip3] pytorch-warmup==0.1.1
[pip3] rotary-embedding-torch==0.2.1
[pip3] torch==2.3.0.dev20240104+cpu
[pip3] torch-fidelity==0.3.0
[pip3] torch_geometric==2.4.0
[pip3] torch-struct==0.5
[pip3] torchmetrics==0.11.4
[pip3] torchvision==0.18.0a0+6101183
[pip3] triton==2.0.0
[pip3] vector-quantize-pytorch==1.1.2
[conda] bert-pytorch              0.0.1a4                   dev_0    <develop>
[conda] clip-anytorch             2.5.2                    pypi_0    pypi
[conda] coca-pytorch              0.0.7                    pypi_0    pypi
[conda] dalle2-pytorch            1.14.2                   pypi_0    pypi
[conda] ema-pytorch               0.2.2                    pypi_0    pypi
[conda] functorch                 1.14.0a0+b71aa0b          pypi_0    pypi
[conda] intel-extension-for-pytorch 2.3.0+git61ac38f           dev_0    <develop>
[conda] mkl                       2023.1.0         h213fc3f_46343    defaults
[conda] mkl-include               2023.1.0                 pypi_0    pypi
[conda] mkl-static                2023.1.0                 pypi_0    pypi
[conda] numpy                     1.21.2                   pypi_0    pypi
[conda] open-clip-torch           2.20.0                   pypi_0    pypi
[conda] pytorch-transformers      1.2.0                    pypi_0    pypi
[conda] pytorch-warmup            0.1.1                    pypi_0    pypi
[conda] rotary-embedding-torch    0.2.1                    pypi_0    pypi
[conda] torch                     2.3.0.dev20240104+cpu          pypi_0    pypi
[conda] torch-fidelity            0.3.0                    pypi_0    pypi
[conda] torch-geometric           2.4.0                    pypi_0    pypi
[conda] torch-struct              0.5                      pypi_0    pypi
[conda] torchdata                 0.6.1                    pypi_0    pypi
[conda] torchmetrics              0.11.4                   pypi_0    pypi
[conda] torchvision               0.18.0a0+6101183          pypi_0    pypi
[conda] triton                    2.0.0                    pypi_0    pypi
[conda] vector-quantize-pytorch   1.1.2                    pypi_0    pypi
```

cc @ezyang @msaroufim @wconstab @bdhirsh @anijain2305 @zou3519 @avikchaudhuri @gmagogsfm @zhxchen17 @tugsbayasgalan @angelayi @suo @ydwu4