Export Phi-3.5-mini-instruct piece by piece

torch.export.export() often breaks on big models because there are control flows or instructions breaking the propagation of dynamic shapes (see …). The function usually gives an indication where the model implementation can be fixed but in case, that is not possible, we can try to export the model piece by piece: every module is converted separately from its submodule. A model can be exported even if one of its submodules cannot.

Model

import pprint
from typing import Any, Dict
import torch
import torch._export.tools
import transformers
from onnx_diagnostic.helpers.cache_helper import make_dynamic_cache
from experimental_experiment.helpers import string_type
from experimental_experiment.torch_interpreter.piece_by_piece import (
    trace_execution_piece_by_piece,
)


def get_phi35_untrained(batch_size: int = 2, **kwargs) -> Dict[str, Any]:
    """
    Gets a non initialized model with two sets of inputs and different shapes.

    :param batch_size: batch size
    :param kwargs: to overwrite the configuration, example ``num_hidden_layers=1``
    :return: dictionary

    See `Phi-3.5-mini-instruct/config.json
    <https://huggingface.co/microsoft/Phi-3.5-mini-instruct/blob/main/config.json>`_.
    """
    config = {
        "_name_or_path": "Phi-3.5-mini-instruct",
        "architectures": ["Phi3ForCausalLM"],
        "attention_dropout": 0.0,
        "auto_map": {
            "AutoConfig": "configuration_phi3.Phi3Config",
            "AutoModelForCausalLM": "modeling_phi3.Phi3ForCausalLM",
        },
        "bos_token_id": 1,
        "embd_pdrop": 0.0,
        "eos_token_id": 32000,
        "hidden_act": "silu",
        "hidden_size": 3072,
        "initializer_range": 0.02,
        "intermediate_size": 8192,
        "max_position_embeddings": 131072,
        "model_type": "phi3",
        "num_attention_heads": 32,
        "num_hidden_layers": 32,
        "num_key_value_heads": 32,
        "original_max_position_embeddings": 4096,
        "pad_token_id": 32000,
        "resid_pdrop": 0.0,
        "rms_norm_eps": 1e-05,
        "rope_scaling": {
            "long_factor": [
                1.0800000429153442,
                1.1100000143051147,
                1.1399999856948853,
                1.340000033378601,
                1.5899999141693115,
                1.600000023841858,
                1.6200000047683716,
                2.620000123977661,
                3.2300000190734863,
                3.2300000190734863,
                4.789999961853027,
                7.400000095367432,
                7.700000286102295,
                9.09000015258789,
                12.199999809265137,
                17.670000076293945,
                24.46000099182129,
                28.57000160217285,
                30.420001983642578,
                30.840002059936523,
                32.590003967285156,
                32.93000411987305,
                42.320003509521484,
                44.96000289916992,
                50.340003967285156,
                50.45000457763672,
                57.55000305175781,
                57.93000411987305,
                58.21000289916992,
                60.1400032043457,
                62.61000442504883,
                62.62000274658203,
                62.71000289916992,
                63.1400032043457,
                63.1400032043457,
                63.77000427246094,
                63.93000411987305,
                63.96000289916992,
                63.970001220703125,
                64.02999877929688,
                64.06999969482422,
                64.08000183105469,
                64.12000274658203,
                64.41000366210938,
                64.4800033569336,
                64.51000213623047,
                64.52999877929688,
                64.83999633789062,
            ],
            "short_factor": [
                1.0,
                1.0199999809265137,
                1.0299999713897705,
                1.0299999713897705,
                1.0499999523162842,
                1.0499999523162842,
                1.0499999523162842,
                1.0499999523162842,
                1.0499999523162842,
                1.0699999332427979,
                1.0999999046325684,
                1.1099998950958252,
                1.1599998474121094,
                1.1599998474121094,
                1.1699998378753662,
                1.2899998426437378,
                1.339999794960022,
                1.679999828338623,
                1.7899998426437378,
                1.8199998140335083,
                1.8499997854232788,
                1.8799997568130493,
                1.9099997282028198,
                1.9399996995925903,
                1.9899996519088745,
                2.0199997425079346,
                2.0199997425079346,
                2.0199997425079346,
                2.0199997425079346,
                2.0199997425079346,
                2.0199997425079346,
                2.0299997329711914,
                2.0299997329711914,
                2.0299997329711914,
                2.0299997329711914,
                2.0299997329711914,
                2.0299997329711914,
                2.0299997329711914,
                2.0299997329711914,
                2.0299997329711914,
                2.0799996852874756,
                2.0899996757507324,
                2.189999580383301,
                2.2199995517730713,
                2.5899994373321533,
                2.729999542236328,
                2.749999523162842,
                2.8399994373321533,
            ],
            "type": "longrope",
        },
        "rope_theta": 10000.0,
        "sliding_window": 262144,
        "tie_word_embeddings": False,
        "torch_dtype": "bfloat16",
        "use_cache": True,
        "attention_bias": False,
        "vocab_size": 32064,
    }
    config.update(**kwargs)
    conf = transformers.Phi3Config(**config)
    model = transformers.Phi3ForCausalLM(conf)
    model.eval()

    cache = make_dynamic_cache(
        [
            (torch.randn(batch_size, 32, 30, 96), torch.randn(batch_size, 32, 30, 96))
            for i in range(config["num_hidden_layers"])
        ]
    )
    cache2 = make_dynamic_cache(
        [
            (torch.randn(batch_size + 1, 32, 31, 96), torch.randn(batch_size + 1, 32, 31, 96))
            for i in range(config["num_hidden_layers"])
        ]
    )

    inputs = dict(
        input_ids=torch.randint(0, 32064, (batch_size, 3)).to(torch.int64),
        attention_mask=torch.ones((batch_size, 33)).to(torch.int64),
        past_key_values=cache,
    )
    inputs2 = dict(
        input_ids=torch.randint(0, 32064, (batch_size + 1, 4)).to(torch.int64),
        attention_mask=torch.ones((batch_size + 1, 35)).to(torch.int64),
        past_key_values=cache2,
    )
    return dict(inputs=inputs, model=model, inputs2=inputs2)


data = get_phi35_untrained(num_hidden_layers=2)
model, inputs, inputs2 = data["model"], data["inputs"], data["inputs2"]

print(string_type(inputs, with_shape=True))
dict(input_ids:T7s2x3,attention_mask:T7s2x33,past_key_values:DynamicCache(key_cache=#2[T1s2x32x30x96,T1s2x32x30x96], value_cache=#2[T1s2x32x30x96,T1s2x32x30x96]))

Dynamic Shapes

We want to infer the dynamic shapes from the two sets of inputs we gave. For that, we use a function to trace the execution of the model including its submodules. It is going to execute the model twice with the two sets of inputs and stores every intermediate input and output.

[_trace_forward_execution] -trace-  M:__main__-Phi3ForCausalLM.forward
[_trace_forward_execution] -trace- .. M:model-Phi3Model.forward
[_trace_forward_execution] -trace- .... M:embed_tokens-Embedding.forward
[_trace_forward_execution] -trace- .... M:layers[0]-Phi3DecoderLayer.forward
[_trace_forward_execution] -trace- ...... M:self_attn-Phi3Attention.forward
[_trace_forward_execution] -trace- ........ M:o_proj-Linear.forward
[_trace_forward_execution] -trace- ........ M:qkv_proj-Linear.forward
[_trace_forward_execution] -trace- ...... M:mlp-Phi3MLP.forward
[_trace_forward_execution] -trace- ........ M:gate_up_proj-Linear.forward
[_trace_forward_execution] -trace- ........ M:down_proj-Linear.forward
[_trace_forward_execution] -trace- ........ M:activation_fn-SiLUActivation.forward
[_trace_forward_execution] -trace- ...... M:input_layernorm-Phi3RMSNorm.forward
[_trace_forward_execution] -trace- ...... M:post_attention_layernorm-Phi3RMSNorm.forward
[_trace_forward_execution] -trace- ...... M:resid_attn_dropout-Dropout.forward
[_trace_forward_execution] -trace- ...... M:resid_mlp_dropout-Dropout.forward
[_trace_forward_execution] -trace- .... M:layers[1]-Phi3DecoderLayer.forward
[_trace_forward_execution] -trace- ...... M:self_attn-Phi3Attention.forward
[_trace_forward_execution] -trace- ........ M:o_proj-Linear.forward
[_trace_forward_execution] -trace- ........ M:qkv_proj-Linear.forward
[_trace_forward_execution] -trace- ...... M:mlp-Phi3MLP.forward
[_trace_forward_execution] -trace- ........ M:gate_up_proj-Linear.forward
[_trace_forward_execution] -trace- ........ M:down_proj-Linear.forward
[_trace_forward_execution] -trace- ........ M:activation_fn-SiLUActivation.forward
[_trace_forward_execution] -trace- ...... M:input_layernorm-Phi3RMSNorm.forward
[_trace_forward_execution] -trace- ...... M:post_attention_layernorm-Phi3RMSNorm.forward
[_trace_forward_execution] -trace- ...... M:resid_attn_dropout-Dropout.forward
[_trace_forward_execution] -trace- ...... M:resid_mlp_dropout-Dropout.forward
[_trace_forward_execution] -trace- .... M:norm-Phi3RMSNorm.forward
[_trace_forward_execution] -trace- .... M:rotary_emb-Phi3RotaryEmbedding.forward
[_trace_forward_execution] -trace- .. M:lm_head-Linear.forward
[trace_execution_piece_by_piece] run with dict(args:(),kwargs:dict(input_ids:T7s2x3,attention_mask:T7s2x33,past_key_values:DynamicCache(key_cache=#2[T1s2x32x30x96,T1s2x32x30x96], value_cache=#2[T1s2x32x30x96,T1s2x32x30x96])))
[__main__:Phi3ForCausalLM] > **dict(input_ids:T7r2,attention_mask:T7r2,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]))
[model:Phi3Model]   > **dict(input_ids:T7r2,attention_mask:T7r2,position_ids:None,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]),inputs_embeds:None,use_cache:None,cache_position:None)
[embed_tokens:Embedding]     > T7r2
[embed_tokens:Embedding]     < T1r3
[rotary_emb:Phi3RotaryEmbedding]     > *(T1r3,T7r2)
[rotary_emb:Phi3RotaryEmbedding]     < *(T1r3,T1r3)
[layers[0]:Phi3DecoderLayer]     > *(T1r3,), **dict(attention_mask:T9r4,position_ids:T7r2,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]),use_cache:bool,cache_position:T7r1,position_embeddings:(T1r3,T1r3))
[input_layernorm:Phi3RMSNorm]       > T1r3
[input_layernorm:Phi3RMSNorm]       < T1r3
[self_attn:Phi3Attention]       > **dict(hidden_states:T1r3,attention_mask:T9r4,position_ids:T7r2,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]),use_cache:bool,cache_position:T7r1,position_embeddings:(T1r3,T1r3))
[qkv_proj:Linear]         > T1r3
[qkv_proj:Linear]         < T1r3
[o_proj:Linear]         > T1r3
[o_proj:Linear]         < T1r3
[self_attn:Phi3Attention]       < *(T1r3,None)
[resid_attn_dropout:Dropout]       > T1r3
[resid_attn_dropout:Dropout]       < T1r3
[post_attention_layernorm:Phi3RMSNorm]       > T1r3
[post_attention_layernorm:Phi3RMSNorm]       < T1r3
[mlp:Phi3MLP]       > T1r3
[gate_up_proj:Linear]         > T1r3
[gate_up_proj:Linear]         < T1r3
[activation_fn:SiLUActivation]         > T1r3
[activation_fn:SiLUActivation]         < T1r3
[down_proj:Linear]         > T1r3
[down_proj:Linear]         < T1r3
[mlp:Phi3MLP]       < T1r3
[resid_mlp_dropout:Dropout]       > T1r3
[resid_mlp_dropout:Dropout]       < T1r3
[layers[0]:Phi3DecoderLayer]     < T1r3
[layers[1]:Phi3DecoderLayer]     > *(T1r3,), **dict(attention_mask:T9r4,position_ids:T7r2,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]),use_cache:bool,cache_position:T7r1,position_embeddings:(T1r3,T1r3))
[input_layernorm:Phi3RMSNorm]       > T1r3
[input_layernorm:Phi3RMSNorm]       < T1r3
[self_attn:Phi3Attention]       > **dict(hidden_states:T1r3,attention_mask:T9r4,position_ids:T7r2,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]),use_cache:bool,cache_position:T7r1,position_embeddings:(T1r3,T1r3))
[qkv_proj:Linear]         > T1r3
[qkv_proj:Linear]         < T1r3
[o_proj:Linear]         > T1r3
[o_proj:Linear]         < T1r3
[self_attn:Phi3Attention]       < *(T1r3,None)
[resid_attn_dropout:Dropout]       > T1r3
[resid_attn_dropout:Dropout]       < T1r3
[post_attention_layernorm:Phi3RMSNorm]       > T1r3
[post_attention_layernorm:Phi3RMSNorm]       < T1r3
[mlp:Phi3MLP]       > T1r3
[gate_up_proj:Linear]         > T1r3
[gate_up_proj:Linear]         < T1r3
[activation_fn:SiLUActivation]         > T1r3
[activation_fn:SiLUActivation]         < T1r3
[down_proj:Linear]         > T1r3
[down_proj:Linear]         < T1r3
[mlp:Phi3MLP]       < T1r3
[resid_mlp_dropout:Dropout]       > T1r3
[resid_mlp_dropout:Dropout]       < T1r3
[layers[1]:Phi3DecoderLayer]     < T1r3
[norm:Phi3RMSNorm]     > T1r3
[norm:Phi3RMSNorm]     < T1r3
[model:Phi3Model]   < *BaseModelOutputWithPast(last_hidden_state:T1r3,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]))
[lm_head:Linear]   > T1r3
[lm_head:Linear]   < T1r3
[__main__:Phi3ForCausalLM] < *CausalLMOutputWithPast(logits:T1r3,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]))
[trace_execution_piece_by_piece] run with dict(args:(),kwargs:dict(input_ids:T7s3x4,attention_mask:T7s3x35,past_key_values:DynamicCache(key_cache=#2[T1s3x32x31x96,T1s3x32x31x96], value_cache=#2[T1s3x32x31x96,T1s3x32x31x96])))
[__main__:Phi3ForCausalLM] > **dict(input_ids:T7r2,attention_mask:T7r2,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]))
[model:Phi3Model]   > **dict(input_ids:T7r2,attention_mask:T7r2,position_ids:None,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]),inputs_embeds:None,use_cache:None,cache_position:None)
[embed_tokens:Embedding]     > T7r2
[embed_tokens:Embedding]     < T1r3
[rotary_emb:Phi3RotaryEmbedding]     > *(T1r3,T7r2)
[rotary_emb:Phi3RotaryEmbedding]     < *(T1r3,T1r3)
[layers[0]:Phi3DecoderLayer]     > *(T1r3,), **dict(attention_mask:T9r4,position_ids:T7r2,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]),use_cache:bool,cache_position:T7r1,position_embeddings:(T1r3,T1r3))
[input_layernorm:Phi3RMSNorm]       > T1r3
[input_layernorm:Phi3RMSNorm]       < T1r3
[self_attn:Phi3Attention]       > **dict(hidden_states:T1r3,attention_mask:T9r4,position_ids:T7r2,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]),use_cache:bool,cache_position:T7r1,position_embeddings:(T1r3,T1r3))
[qkv_proj:Linear]         > T1r3
[qkv_proj:Linear]         < T1r3
[o_proj:Linear]         > T1r3
[o_proj:Linear]         < T1r3
[self_attn:Phi3Attention]       < *(T1r3,None)
[resid_attn_dropout:Dropout]       > T1r3
[resid_attn_dropout:Dropout]       < T1r3
[post_attention_layernorm:Phi3RMSNorm]       > T1r3
[post_attention_layernorm:Phi3RMSNorm]       < T1r3
[mlp:Phi3MLP]       > T1r3
[gate_up_proj:Linear]         > T1r3
[gate_up_proj:Linear]         < T1r3
[activation_fn:SiLUActivation]         > T1r3
[activation_fn:SiLUActivation]         < T1r3
[down_proj:Linear]         > T1r3
[down_proj:Linear]         < T1r3
[mlp:Phi3MLP]       < T1r3
[resid_mlp_dropout:Dropout]       > T1r3
[resid_mlp_dropout:Dropout]       < T1r3
[layers[0]:Phi3DecoderLayer]     < T1r3
[layers[1]:Phi3DecoderLayer]     > *(T1r3,), **dict(attention_mask:T9r4,position_ids:T7r2,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]),use_cache:bool,cache_position:T7r1,position_embeddings:(T1r3,T1r3))
[input_layernorm:Phi3RMSNorm]       > T1r3
[input_layernorm:Phi3RMSNorm]       < T1r3
[self_attn:Phi3Attention]       > **dict(hidden_states:T1r3,attention_mask:T9r4,position_ids:T7r2,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]),use_cache:bool,cache_position:T7r1,position_embeddings:(T1r3,T1r3))
[qkv_proj:Linear]         > T1r3
[qkv_proj:Linear]         < T1r3
[o_proj:Linear]         > T1r3
[o_proj:Linear]         < T1r3
[self_attn:Phi3Attention]       < *(T1r3,None)
[resid_attn_dropout:Dropout]       > T1r3
[resid_attn_dropout:Dropout]       < T1r3
[post_attention_layernorm:Phi3RMSNorm]       > T1r3
[post_attention_layernorm:Phi3RMSNorm]       < T1r3
[mlp:Phi3MLP]       > T1r3
[gate_up_proj:Linear]         > T1r3
[gate_up_proj:Linear]         < T1r3
[activation_fn:SiLUActivation]         > T1r3
[activation_fn:SiLUActivation]         < T1r3
[down_proj:Linear]         > T1r3
[down_proj:Linear]         < T1r3
[mlp:Phi3MLP]       < T1r3
[resid_mlp_dropout:Dropout]       > T1r3
[resid_mlp_dropout:Dropout]       < T1r3
[layers[1]:Phi3DecoderLayer]     < T1r3
[norm:Phi3RMSNorm]     > T1r3
[norm:Phi3RMSNorm]     < T1r3
[model:Phi3Model]   < *BaseModelOutputWithPast(last_hidden_state:T1r3,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]))
[lm_head:Linear]   > T1r3
[lm_head:Linear]   < T1r3
[__main__:Phi3ForCausalLM] < *CausalLMOutputWithPast(logits:T1r3,past_key_values:DynamicCache(key_cache=#2[T1r4,T1r4], value_cache=#2[T1r4,T1r4]))
[trace_forward_execution] traced execution of model Phi3ForCausalLM
>>> __main__: Phi3ForCausalLM
  > ((),dict(input_ids:CT7s2x3[507,31319:A16171.5],attention_mask:CT7s2x33[1,1:A1.0],past_key_values:DynamicCache(key_cache=#2[CT1s2x32x30x96[-4.465545177459717,4.057265758514404:A0.0007159979157076545],CT1s2x32x30x96[-4.467886924743652,4.720084190368652:A-2.9030142023030015e-06]], value_cache=#2[CT1s2x32x30x96[-4.517740726470947,4.218741416931152:A-0.0006990449411434267],CT1s2x32x30x96[-4.35960054397583,4.4602131843566895:A0.0017125505371854471]])))
  > ((),dict(input_ids:CT7s3x4[3082,29391:A18824.416666666668],attention_mask:CT7s3x35[1,1:A1.0],past_key_values:DynamicCache(key_cache=#2[CT1s3x32x31x96[-4.662174701690674,4.428377628326416:A-0.0034959197779404635],CT1s3x32x31x96[-4.522449970245361,4.264554977416992:A-0.002481214040555478]], value_cache=#2[CT1s3x32x31x96[-4.631809234619141,4.268094539642334:A-0.00029668831490536814],CT1s3x32x31x96[-5.316957473754883,4.8129777908325195:A-0.003804903293625481]])))
    >>> model: Phi3Model
      > ((),dict(input_ids:CT7s2x3[507,31319:A16171.5],attention_mask:CT7s2x33[1,1:A1.0],position_ids:None,past_key_values:DynamicCache(key_cache=#2[CT1s2x32x30x96[-4.465545177459717,4.057265758514404:A0.0007159979157076545],CT1s2x32x30x96[-4.467886924743652,4.720084190368652:A-2.9030142023030015e-06]], value_cache=#2[CT1s2x32x30x96[-4.517740726470947,4.218741416931152:A-0.0006990449411434267],CT1s2x32x30x96[-4.35960054397583,4.4602131843566895:A0.0017125505371854471]]),inputs_embeds:None,use_cache:None,cache_position:None))
      > ((),dict(input_ids:CT7s3x4[3082,29391:A18824.416666666668],attention_mask:CT7s3x35[1,1:A1.0],position_ids:None,past_key_values:DynamicCache(key_cache=#2[CT1s3x32x31x96[-4.662174701690674,4.428377628326416:A-0.0034959197779404635],CT1s3x32x31x96[-4.522449970245361,4.264554977416992:A-0.002481214040555478]], value_cache=#2[CT1s3x32x31x96[-4.631809234619141,4.268094539642334:A-0.00029668831490536814],CT1s3x32x31x96[-5.316957473754883,4.8129777908325195:A-0.003804903293625481]]),inputs_embeds:None,use_cache:None,cache_position:None))
        >>> embed_tokens: Embedding
          > ((CT7s2x3[507,31319:A16171.5],),{})
          > ((CT7s3x4[3082,29391:A18824.416666666668],),{})
          < (CT1s2x3x3072[-0.07626397162675858,0.07296812534332275:A-3.689262259222293e-05],)
          < (CT1s3x4x3072[-0.09224457293748856,0.08149274438619614:A-0.00010771516626686652],)
        <<<
        >>> layers[0]: Phi3DecoderLayer
          > ((CT1s2x3x3072[-0.07626397162675858,0.07296812534332275:A-3.689262259222293e-05],),dict(attention_mask:CT9s2x1x3x33[False,True:A0.9696969696969697],position_ids:CT7s1x3[30,32:A31.0],past_key_values:DynamicCache(key_cache=#2[CT1s2x32x30x96[-4.465545177459717,4.057265758514404:A0.0007159979157076545],CT1s2x32x30x96[-4.467886924743652,4.720084190368652:A-2.9030142023030015e-06]], value_cache=#2[CT1s2x32x30x96[-4.517740726470947,4.218741416931152:A-0.0006990449411434267],CT1s2x32x30x96[-4.35960054397583,4.4602131843566895:A0.0017125505371854471]]),use_cache:bool=True,cache_position:CT7s3[30,32:A31.0],position_embeddings:(CT1s1x3x96[-1.1855769157409668,1.1902371644973755:A0.746652018013669],CT1s1x3x96[-1.1887905597686768,1.190193772315979:A0.1589894221542636])))
          > ((CT1s3x4x3072[-0.09224457293748856,0.08149274438619614:A-0.00010771516626686652],),dict(attention_mask:CT9s3x1x4x35[False,True:A0.9571428571428572],position_ids:CT7s1x4[31,34:A32.5],past_key_values:DynamicCache(key_cache=#2[CT1s3x32x31x96[-4.662174701690674,4.428377628326416:A-0.0034959197779404635],CT1s3x32x31x96[-4.522449970245361,4.264554977416992:A-0.002481214040555478]], value_cache=#2[CT1s3x32x31x96[-4.631809234619141,4.268094539642334:A-0.00029668831490536814],CT1s3x32x31x96[-5.316957473754883,4.8129777908325195:A-0.003804903293625481]]),use_cache:bool=True,cache_position:CT7s4[31,34:A32.5],position_embeddings:(CT1s1x4x96[-1.1855769157409668,1.190237045288086:A0.7129333875218435],CT1s1x4x96[-1.1719439029693604,1.1902378797531128:A0.18296290554159592])))
            >>> self_attn: Phi3Attention
              > ((),dict(hidden_states:CT1s2x3x3072[-3.783019781112671,3.565221071243286:A-0.0018069703635737887],attention_mask:CT9s2x1x3x33[False,True:A0.9696969696969697],position_ids:CT7s1x3[30,32:A31.0],past_key_values:DynamicCache(key_cache=#2[CT1s2x32x30x96[-4.465545177459717,4.057265758514404:A0.0007159979157076545],CT1s2x32x30x96[-4.467886924743652,4.720084190368652:A-2.9030142023030015e-06]], value_cache=#2[CT1s2x32x30x96[-4.517740726470947,4.218741416931152:A-0.0006990449411434267],CT1s2x32x30x96[-4.35960054397583,4.4602131843566895:A0.0017125505371854471]]),use_cache:bool=True,cache_position:CT7s3[30,32:A31.0],position_embeddings:(CT1s1x3x96[-1.1855769157409668,1.1902371644973755:A0.746652018013669],CT1s1x3x96[-1.1887905597686768,1.190193772315979:A0.1589894221542636])))
              > ((),dict(hidden_states:CT1s3x4x3072[-4.443816661834717,4.056170463562012:A-0.005361890270728864],attention_mask:CT9s3x1x4x35[False,True:A0.9571428571428572],position_ids:CT7s1x4[31,34:A32.5],past_key_values:DynamicCache(key_cache=#2[CT1s3x32x31x96[-4.662174701690674,4.428377628326416:A-0.0034959197779404635],CT1s3x32x31x96[-4.522449970245361,4.264554977416992:A-0.002481214040555478]], value_cache=#2[CT1s3x32x31x96[-4.631809234619141,4.268094539642334:A-0.00029668831490536814],CT1s3x32x31x96[-5.316957473754883,4.8129777908325195:A-0.003804903293625481]]),use_cache:bool=True,cache_position:CT7s4[31,34:A32.5],position_embeddings:(CT1s1x4x96[-1.1855769157409668,1.190237045288086:A0.7129333875218435],CT1s1x4x96[-1.1719439029693604,1.1902378797531128:A0.18296290554159592])))
                >>> o_proj: Linear
                  > ((CT1s2x3x3072[-2.2603085041046143,2.0475239753723145:A-0.0008883428078339466],),{})
                  > ((CT1s3x4x3072[-2.245011568069458,2.13265061378479:A-0.0008563481390419082],),{})
                  < (CT1s2x3x3072[-1.569084882736206,1.5451966524124146:A-0.0039206560408931385],)
                  < (CT1s3x4x3072[-1.807442545890808,1.5120015144348145:A0.000409557451929585],)
                <<<
                >>> qkv_proj: Linear
                  > ((CT1s2x3x3072[-3.783019781112671,3.565221071243286:A-0.0018069703635737887],),{})
                  > ((CT1s3x4x3072[-4.443816661834717,4.056170463562012:A-0.005361890270728864],),{})
                  < (CT1s2x3x9216[-4.903987884521484,5.047000408172607:A-0.0037297510212797366],)
                  < (CT1s3x4x9216[-4.425784587860107,4.825686454772949:A0.0015161730776901128],)
                <<<
              < (CT1s2x3x3072[-1.569084882736206,1.5451966524124146:A-0.0039206560408931385],None)
              < (CT1s3x4x3072[-1.807442545890808,1.5120015144348145:A0.000409557451929585],None)
            <<<
            >>> mlp: Phi3MLP
              > ((CT1s2x3x3072[-4.080776214599609,3.891044855117798:A-0.01037505980212493],),{})
              > ((CT1s3x4x3072[-4.547957897186279,3.9305286407470703:A0.0004034293294156536],),{})
                >>> gate_up_proj: Linear
                  > ((CT1s2x3x3072[-4.080776214599609,3.891044855117798:A-0.01037505980212493],),{})
                  > ((CT1s3x4x3072[-4.547957897186279,3.9305286407470703:A0.0004034293294156536],),{})
                  < (CT1s2x3x16384[-4.730355262756348,4.846550464630127:A-0.000785505145168249],)
                  < (CT1s3x4x16384[-4.877781867980957,4.554588794708252:A0.0016664399843620004],)
                <<<
                >>> down_proj: Linear
                  > ((CT1s2x3x8192[-9.154955863952637,12.709978103637695:A-0.004634474402878486],),{})
                  > ((CT1s3x4x8192[-9.155027389526367,11.09277057647705:A0.0019418056024517155],),{})
                  < (CT1s2x3x3072[-5.099157810211182,5.868416786193848:A-0.005633440847683839],)
                  < (CT1s3x4x3072[-5.447781085968018,5.121474742889404:A0.004263810524741353],)
                <<<
                >>> activation_fn: SiLUActivation
                  > ((CT1s2x3x8192[-4.490071773529053,4.846550464630127:A-0.003992798204258463],),{})
                  > ((CT1s3x4x8192[-4.877781867980957,4.554588794708252:A0.00020859190073470776],),{})
                  < (CT1s2x3x8192[-0.27846455574035645,4.808775424957275:A0.24274485934891235],)
                  < (CT1s3x4x8192[-0.27846455574035645,4.507178783416748:A0.2457273621407878],)
                <<<
              < (CT1s2x3x3072[-5.099157810211182,5.868416786193848:A-0.005633440847683839],)
              < (CT1s3x4x3072[-5.447781085968018,5.121474742889404:A0.004263810524741353],)
            <<<
            >>> input_layernorm: Phi3RMSNorm
              > ((CT1s2x3x3072[-0.07626397162675858,0.07296812534332275:A-3.689262259222293e-05],),{})
              > ((CT1s3x4x3072[-0.09224457293748856,0.08149274438619614:A-0.00010771516626686652],),{})
              < (CT1s2x3x3072[-3.783019781112671,3.565221071243286:A-0.0018069703635737887],)
              < (CT1s3x4x3072[-4.443816661834717,4.056170463562012:A-0.005361890270728864],)
            <<<
            >>> post_attention_layernorm: Phi3RMSNorm
              > ((CT1s2x3x3072[-1.6129931211471558,1.5474786758422852:A-0.003957548700933937],),{})
              > ((CT1s3x4x3072[-1.7693893909454346,1.5163228511810303:A0.0003018422545753512],),{})
              < (CT1s2x3x3072[-4.080776214599609,3.891044855117798:A-0.01037505980212493],)
              < (CT1s3x4x3072[-4.547957897186279,3.9305286407470703:A0.0004034293294156536],)
            <<<
            >>> resid_attn_dropout: Dropout
              > ((CT1s2x3x3072[-1.569084882736206,1.5451966524124146:A-0.0039206560408931385],),{})
              > ((CT1s3x4x3072[-1.807442545890808,1.5120015144348145:A0.000409557451929585],),{})
              < (CT1s2x3x3072[-1.569084882736206,1.5451966524124146:A-0.0039206560408931385],)
              < (CT1s3x4x3072[-1.807442545890808,1.5120015144348145:A0.000409557451929585],)
            <<<
            >>> resid_mlp_dropout: Dropout
              > ((CT1s2x3x3072[-5.099157810211182,5.868416786193848:A-0.005633440847683839],),{})
              > ((CT1s3x4x3072[-5.447781085968018,5.121474742889404:A0.004263810524741353],),{})
              < (CT1s2x3x3072[-5.099157810211182,5.868416786193848:A-0.005633440847683839],)
              < (CT1s3x4x3072[-5.447781085968018,5.121474742889404:A0.004263810524741353],)
            <<<
          < (CT1s2x3x3072[-5.115391254425049,6.402807235717773:A-0.00959098940287125],)
          < (CT1s3x4x3072[-5.675037384033203,5.982346534729004:A0.004565652693991574],)
        <<<
        >>> layers[1]: Phi3DecoderLayer
          > ((CT1s2x3x3072[-5.115391254425049,6.402807235717773:A-0.00959098940287125],),dict(attention_mask:CT9s2x1x3x33[False,True:A0.9696969696969697],position_ids:CT7s1x3[30,32:A31.0],past_key_values:DynamicCache(key_cache=#2[CT1s2x32x33x96[-5.243246555328369,5.216899394989014:A-0.0008380057157270766],CT1s2x32x30x96[-4.467886924743652,4.720084190368652:A-2.9030142023030015e-06]], value_cache=#2[CT1s2x32x33x96[-4.903987884521484,4.691673755645752:A-0.0011286081004508537],CT1s2x32x30x96[-4.35960054397583,4.4602131843566895:A0.0017125505371854471]]),use_cache:bool=True,cache_position:CT7s3[30,32:A31.0],position_embeddings:(CT1s1x3x96[-1.1855769157409668,1.1902371644973755:A0.746652018013669],CT1s1x3x96[-1.1887905597686768,1.190193772315979:A0.1589894221542636])))
          > ((CT1s3x4x3072[-5.675037384033203,5.982346534729004:A0.004565652693991574],),dict(attention_mask:CT9s3x1x4x35[False,True:A0.9571428571428572],position_ids:CT7s1x4[31,34:A32.5],past_key_values:DynamicCache(key_cache=#2[CT1s3x32x35x96[-5.2977495193481445,5.638122081756592:A-0.0022213943482518434],CT1s3x32x31x96[-4.522449970245361,4.264554977416992:A-0.002481214040555478]], value_cache=#2[CT1s3x32x35x96[-4.631809234619141,4.825686454772949:A0.00020694482039367723],CT1s3x32x31x96[-5.316957473754883,4.8129777908325195:A-0.003804903293625481]]),use_cache:bool=True,cache_position:CT7s4[31,34:A32.5],position_embeddings:(CT1s1x4x96[-1.1855769157409668,1.190237045288086:A0.7129333875218435],CT1s1x4x96[-1.1719439029693604,1.1902378797531128:A0.18296290554159592])))
            >>> self_attn: Phi3Attention
              > ((),dict(hidden_states:CT1s2x3x3072[-3.692824363708496,4.645721912384033:A-0.006849586979686567],attention_mask:CT9s2x1x3x33[False,True:A0.9696969696969697],position_ids:CT7s1x3[30,32:A31.0],past_key_values:DynamicCache(key_cache=#2[CT1s2x32x33x96[-5.243246555328369,5.216899394989014:A-0.0008380057157270766],CT1s2x32x30x96[-4.467886924743652,4.720084190368652:A-2.9030142023030015e-06]], value_cache=#2[CT1s2x32x33x96[-4.903987884521484,4.691673755645752:A-0.0011286081004508537],CT1s2x32x30x96[-4.35960054397583,4.4602131843566895:A0.0017125505371854471]]),use_cache:bool=True,cache_position:CT7s3[30,32:A31.0],position_embeddings:(CT1s1x3x96[-1.1855769157409668,1.1902371644973755:A0.746652018013669],CT1s1x3x96[-1.1887905597686768,1.190193772315979:A0.1589894221542636])))
              > ((),dict(hidden_states:CT1s3x4x3072[-4.032126426696777,4.3049540519714355:A0.0032200051591207116],attention_mask:CT9s3x1x4x35[False,True:A0.9571428571428572],position_ids:CT7s1x4[31,34:A32.5],past_key_values:DynamicCache(key_cache=#2[CT1s3x32x35x96[-5.2977495193481445,5.638122081756592:A-0.0022213943482518434],CT1s3x32x31x96[-4.522449970245361,4.264554977416992:A-0.002481214040555478]], value_cache=#2[CT1s3x32x35x96[-4.631809234619141,4.825686454772949:A0.00020694482039367723],CT1s3x32x31x96[-5.316957473754883,4.8129777908325195:A-0.003804903293625481]]),use_cache:bool=True,cache_position:CT7s4[31,34:A32.5],position_embeddings:(CT1s1x4x96[-1.1855769157409668,1.190237045288086:A0.7129333875218435],CT1s1x4x96[-1.1719439029693604,1.1902378797531128:A0.18296290554159592])))
                >>> o_proj: Linear
                  > ((CT1s2x3x3072[-1.9148494005203247,1.9142004251480103:A0.002065079880343706],),{})
                  > ((CT1s3x4x3072[-2.328655481338501,1.8774923086166382:A-0.0018731078297801556],),{})
                  < (CT1s2x3x3072[-1.719269037246704,1.4660935401916504:A-0.0038470371489564867],)
                  < (CT1s3x4x3072[-1.5977507829666138,1.5770745277404785:A0.004415256080695447],)
                <<<
                >>> qkv_proj: Linear
                  > ((CT1s2x3x3072[-3.692824363708496,4.645721912384033:A-0.006849586979686567],),{})
                  > ((CT1s3x4x3072[-4.032126426696777,4.3049540519714355:A0.0032200051591207116],),{})
                  < (CT1s2x3x9216[-4.772463798522949,4.502849578857422:A-0.011448141658581368],)
                  < (CT1s3x4x9216[-4.546624183654785,5.012377738952637:A-0.006584659571861569],)
                <<<
              < (CT1s2x3x3072[-1.719269037246704,1.4660935401916504:A-0.0038470371489564867],None)
              < (CT1s3x4x3072[-1.5977507829666138,1.5770745277404785:A0.004415256080695447],None)
            <<<
            >>> mlp: Phi3MLP
              > ((CT1s2x3x3072[-3.7153704166412354,4.575687408447266:A-0.009262047859905524],),{})
              > ((CT1s3x4x3072[-3.7643024921417236,4.266655921936035:A0.006118759006482939],),{})
                >>> gate_up_proj: Linear
                  > ((CT1s2x3x3072[-3.7153704166412354,4.575687408447266:A-0.009262047859905524],),{})
                  > ((CT1s3x4x3072[-3.7643024921417236,4.266655921936035:A0.006118759006482939],),{})
                  < (CT1s2x3x16384[-5.145788192749023,4.845174789428711:A0.009646031746124587],)
                  < (CT1s3x4x16384[-5.195202350616455,4.568906307220459:A0.0014517907274621915],)
                <<<
                >>> down_proj: Linear
                  > ((CT1s2x3x8192[-10.810708045959473,9.12816047668457:A-0.00023827384149119565],),{})
                  > ((CT1s3x4x8192[-9.432348251342773,12.139277458190918:A0.0004174245607357985],),{})
                  < (CT1s2x3x3072[-5.995305061340332,5.33147668838501:A-0.003057508847834672],)
                  < (CT1s3x4x3072[-5.230845928192139,5.353349685668945:A-0.016111877306924625],)
                <<<
                >>> activation_fn: SiLUActivation
                  > ((CT1s2x3x8192[-4.308592796325684,4.845174789428711:A0.008163898182194393],),{})
                  > ((CT1s3x4x8192[-4.877556800842285,4.540010929107666:A0.005127051427541811],),{})
                  < (CT1s2x3x8192[-0.27846455574035645,4.807358741760254:A0.24811603383206315],)
                  < (CT1s3x4x8192[-0.27846455574035645,4.492065906524658:A0.24814261865139484],)
                <<<
              < (CT1s2x3x3072[-5.995305061340332,5.33147668838501:A-0.003057508847834672],)
              < (CT1s3x4x3072[-5.230845928192139,5.353349685668945:A-0.016111877306924625],)
            <<<
            >>> input_layernorm: Phi3RMSNorm
              > ((CT1s2x3x3072[-5.115391254425049,6.402807235717773:A-0.00959098940287125],),{})
              > ((CT1s3x4x3072[-5.675037384033203,5.982346534729004:A0.004565652693991574],),{})
              < (CT1s2x3x3072[-3.692824363708496,4.645721912384033:A-0.006849586979686567],)
              < (CT1s3x4x3072[-4.032126426696777,4.3049540519714355:A0.0032200051591207116],)
            <<<
            >>> post_attention_layernorm: Phi3RMSNorm
              > ((CT1s2x3x3072[-5.4467339515686035,6.48449182510376:A-0.013438026726109657],),{})
              > ((CT1s3x4x3072[-5.403885364532471,6.139309883117676:A0.008980908598965066],),{})
              < (CT1s2x3x3072[-3.7153704166412354,4.575687408447266:A-0.009262047859905524],)
              < (CT1s3x4x3072[-3.7643024921417236,4.266655921936035:A0.006118759006482939],)
            <<<
            >>> resid_attn_dropout: Dropout
              > ((CT1s2x3x3072[-1.719269037246704,1.4660935401916504:A-0.0038470371489564867],),{})
              > ((CT1s3x4x3072[-1.5977507829666138,1.5770745277404785:A0.004415256080695447],),{})
              < (CT1s2x3x3072[-1.719269037246704,1.4660935401916504:A-0.0038470371489564867],)
              < (CT1s3x4x3072[-1.5977507829666138,1.5770745277404785:A0.004415256080695447],)
            <<<
            >>> resid_mlp_dropout: Dropout
              > ((CT1s2x3x3072[-5.995305061340332,5.33147668838501:A-0.003057508847834672],),{})
              > ((CT1s3x4x3072[-5.230845928192139,5.353349685668945:A-0.016111877306924625],),{})
              < (CT1s2x3x3072[-5.995305061340332,5.33147668838501:A-0.003057508847834672],)
              < (CT1s3x4x3072[-5.230845928192139,5.353349685668945:A-0.016111877306924625],)
            <<<
          < (CT1s2x3x3072[-8.706647872924805,8.32397747039795:A-0.01649553544161285],)
          < (CT1s3x4x3072[-8.708154678344727,9.680032730102539:A-0.007130968992618768],)
        <<<
        >>> norm: Phi3RMSNorm
          > ((CT1s2x3x3072[-8.706647872924805,8.32397747039795:A-0.01649553544161285],),{})
          > ((CT1s3x4x3072[-8.708154678344727,9.680032730102539:A-0.007130968992618768],),{})
          < (CT1s2x3x3072[-4.431250095367432,4.241793155670166:A-0.008410615578096328],)
          < (CT1s3x4x3072[-4.300024509429932,4.754427433013916:A-0.003576264563900761],)
        <<<
        >>> rotary_emb: Phi3RotaryEmbedding
          > ((CT1s2x3x3072[-0.07626397162675858,0.07296812534332275:A-3.689262259222293e-05],CT7s1x3[30,32:A31.0]),{})
          > ((CT1s3x4x3072[-0.09224457293748856,0.08149274438619614:A-0.00010771516626686652],CT7s1x4[31,34:A32.5]),{})
          < (CT1s1x3x96[-1.1855769157409668,1.1902371644973755:A0.746652018013669],CT1s1x3x96[-1.1887905597686768,1.190193772315979:A0.1589894221542636])
          < (CT1s1x4x96[-1.1855769157409668,1.190237045288086:A0.7129333875218435],CT1s1x4x96[-1.1719439029693604,1.1902378797531128:A0.18296290554159592])
        <<<
      < (dict(last_hidden_state:CT1s2x3x3072[-4.431250095367432,4.241793155670166:A-0.008410615578096328],past_key_values:DynamicCache(key_cache=#2[CT1s2x32x33x96[-5.243246555328369,5.216899394989014:A-0.0008380057157270766],CT1s2x32x33x96[-4.581390380859375,5.112067222595215:A-0.0005753138876627784]], value_cache=#2[CT1s2x32x33x96[-4.903987884521484,4.691673755645752:A-0.0011286081004508537],CT1s2x32x33x96[-4.35960054397583,4.502186298370361:A0.0023477824063049342]])),)
      < (dict(last_hidden_state:CT1s3x4x3072[-4.300024509429932,4.754427433013916:A-0.003576264563900761],past_key_values:DynamicCache(key_cache=#2[CT1s3x32x35x96[-5.2977495193481445,5.638122081756592:A-0.0022213943482518434],CT1s3x32x35x96[-5.407881736755371,5.356963157653809:A-0.002788695160612269]], value_cache=#2[CT1s3x32x35x96[-4.631809234619141,4.825686454772949:A0.00020694482039367723],CT1s3x32x35x96[-5.316957473754883,5.012377738952637:A-0.004497091284961408]])),)
    <<<
    >>> lm_head: Linear
      > ((CT1s2x3x3072[-4.431250095367432,4.241793155670166:A-0.008410615578096328],),{})
      > ((CT1s3x4x3072[-4.300024509429932,4.754427433013916:A-0.003576264563900761],),{})
      < (CT1s2x3x32064[-5.049499034881592,5.194516658782959:A-0.0010708118826330687],)
      < (CT1s3x4x32064[-5.062286376953125,5.413645267486572:A0.00036001507699741825],)
    <<<
  < (dict(logits:CT1s2x3x32064[-5.049499034881592,5.194516658782959:A-0.0010708118826330687],past_key_values:DynamicCache(key_cache=#2[CT1s2x32x33x96[-5.243246555328369,5.216899394989014:A-0.0008380057157270766],CT1s2x32x33x96[-4.581390380859375,5.112067222595215:A-0.0005753138876627784]], value_cache=#2[CT1s2x32x33x96[-4.903987884521484,4.691673755645752:A-0.0011286081004508537],CT1s2x32x33x96[-4.35960054397583,4.502186298370361:A0.0023477824063049342]])),)
  < (dict(logits:CT1s3x4x32064[-5.062286376953125,5.413645267486572:A0.00036001507699741825],past_key_values:DynamicCache(key_cache=#2[CT1s3x32x35x96[-5.2977495193481445,5.638122081756592:A-0.0022213943482518434],CT1s3x32x35x96[-5.407881736755371,5.356963157653809:A-0.002788695160612269]], value_cache=#2[CT1s3x32x35x96[-4.631809234619141,4.825686454772949:A0.00020694482039367723],CT1s3x32x35x96[-5.316957473754883,5.012377738952637:A-0.004497091284961408]])),)
<<<
[_untrace_forward_execution]  M:__main__-Phi3ForCausalLM
[_untrace_forward_execution] .. M:model-Phi3Model
[_untrace_forward_execution] .... M:embed_tokens-Embedding
[_untrace_forward_execution] .... M:layers[0]-Phi3DecoderLayer
[_untrace_forward_execution] ...... M:self_attn-Phi3Attention
[_untrace_forward_execution] ........ M:o_proj-Linear
[_untrace_forward_execution] ........ M:qkv_proj-Linear
[_untrace_forward_execution] ...... M:mlp-Phi3MLP
[_untrace_forward_execution] ........ M:gate_up_proj-Linear
[_untrace_forward_execution] ........ M:down_proj-Linear
[_untrace_forward_execution] ........ M:activation_fn-SiLUActivation
[_untrace_forward_execution] ...... M:input_layernorm-Phi3RMSNorm
[_untrace_forward_execution] ...... M:post_attention_layernorm-Phi3RMSNorm
[_untrace_forward_execution] ...... M:resid_attn_dropout-Dropout
[_untrace_forward_execution] ...... M:resid_mlp_dropout-Dropout
[_untrace_forward_execution] .... M:layers[1]-Phi3DecoderLayer
[_untrace_forward_execution] ...... M:self_attn-Phi3Attention
[_untrace_forward_execution] ........ M:o_proj-Linear
[_untrace_forward_execution] ........ M:qkv_proj-Linear
[_untrace_forward_execution] ...... M:mlp-Phi3MLP
[_untrace_forward_execution] ........ M:gate_up_proj-Linear
[_untrace_forward_execution] ........ M:down_proj-Linear
[_untrace_forward_execution] ........ M:activation_fn-SiLUActivation
[_untrace_forward_execution] ...... M:input_layernorm-Phi3RMSNorm
[_untrace_forward_execution] ...... M:post_attention_layernorm-Phi3RMSNorm
[_untrace_forward_execution] ...... M:resid_attn_dropout-Dropout
[_untrace_forward_execution] ...... M:resid_mlp_dropout-Dropout
[_untrace_forward_execution] .... M:norm-Phi3RMSNorm
[_untrace_forward_execution] .... M:rotary_emb-Phi3RotaryEmbedding
[_untrace_forward_execution] .. M:lm_head-Linear

Now we keep in memory every input/output for the submodules, we can guess the dynamic shapes for every of them. The final ones:

The dynamic shapes are:
((),
 {'attention_mask': {0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},
  'input_ids': {0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},
  'past_key_values': [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)},
                       {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}],
                      [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)},
                       {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]]})

And all the dynamic shapes all along the traced submodules.

print(
    diag.pretty_text(
        with_dynamic_shape=True,
        with_shape=False,
        with_min_max=False,
        with_device=False,
        with_inputs=False,
    ).replace("<_DimHint.DYNAMIC: 3>", "DYN")
)
>>> __main__: Phi3ForCausalLM
  DS=((), {'attention_mask': {0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)}, 'input_ids': {0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)}, 'past_key_values': [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]]})
    >>> model: Phi3Model
      DS=((), {'attention_mask': {0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)}, 'cache_position': None, 'input_ids': {0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)}, 'inputs_embeds': None, 'past_key_values': [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]], 'position_ids': None, 'use_cache': None})
        >>> embed_tokens: Embedding: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
        >>> layers[0]: Phi3DecoderLayer
          DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {'attention_mask': {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC), 3: DimHint(DYNAMIC)}, 'cache_position': {0: DimHint(DYNAMIC)}, 'past_key_values': [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]], 'position_embeddings': ({1: DimHint(DYNAMIC)}, {1: DimHint(DYNAMIC)}), 'position_ids': {1: DimHint(DYNAMIC)}, 'use_cache': None})
            >>> self_attn: Phi3Attention
              DS=((), {'attention_mask': {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC), 3: DimHint(DYNAMIC)}, 'cache_position': {0: DimHint(DYNAMIC)}, 'hidden_states': {0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)}, 'past_key_values': [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]], 'position_embeddings': ({1: DimHint(DYNAMIC)}, {1: DimHint(DYNAMIC)}), 'position_ids': {1: DimHint(DYNAMIC)}, 'use_cache': None})
                >>> o_proj: Linear: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
                >>> qkv_proj: Linear: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
            <<<
            >>> mlp: Phi3MLP
              DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {})
                >>> gate_up_proj: Linear: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
                >>> down_proj: Linear: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
                >>> activation_fn: SiLUActivation: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
            <<<
            >>> input_layernorm: Phi3RMSNorm: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
            >>> post_attention_layernorm: Phi3RMSNorm: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
            >>> resid_attn_dropout: Dropout: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
            >>> resid_mlp_dropout: Dropout: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
        <<<
        >>> layers[1]: Phi3DecoderLayer
          DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {'attention_mask': {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC), 3: DimHint(DYNAMIC)}, 'cache_position': {0: DimHint(DYNAMIC)}, 'past_key_values': [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]], 'position_embeddings': ({1: DimHint(DYNAMIC)}, {1: DimHint(DYNAMIC)}), 'position_ids': {1: DimHint(DYNAMIC)}, 'use_cache': None})
            >>> self_attn: Phi3Attention
              DS=((), {'attention_mask': {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC), 3: DimHint(DYNAMIC)}, 'cache_position': {0: DimHint(DYNAMIC)}, 'hidden_states': {0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)}, 'past_key_values': [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]], 'position_embeddings': ({1: DimHint(DYNAMIC)}, {1: DimHint(DYNAMIC)}), 'position_ids': {1: DimHint(DYNAMIC)}, 'use_cache': None})
                >>> o_proj: Linear: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
                >>> qkv_proj: Linear: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
            <<<
            >>> mlp: Phi3MLP
              DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {})
                >>> gate_up_proj: Linear: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
                >>> down_proj: Linear: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
                >>> activation_fn: SiLUActivation: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
            <<<
            >>> input_layernorm: Phi3RMSNorm: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
            >>> post_attention_layernorm: Phi3RMSNorm: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
            >>> resid_attn_dropout: Dropout: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
            >>> resid_mlp_dropout: Dropout: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
        <<<
        >>> norm: Phi3RMSNorm: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
        >>> rotary_emb: Phi3RotaryEmbedding: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)}, {1: DimHint(DYNAMIC)}), {}) <<<
    <<<
    >>> lm_head: Linear: DS=(({0: DimHint(DYNAMIC), 1: DimHint(DYNAMIC)},), {}) <<<
<<<

Evaluate the export

In many cases, the export (to torch.fx.Graph, to ONNX) does not work on the first try. We need a way to understand how much the model can be exported. It can be used to evaluate the how much code needs to be rewritten or patched to be exportable. The verbosity can be increase to show dynamic shapes, results of the discrepancies. Let’s display the module and its submodule first.

print(
    diag.pretty_text(
        with_dynamic_shape=False,
        with_shape=False,
        with_min_max=False,
        with_device=False,
        with_inputs=False,
    )
)
>>> __main__: Phi3ForCausalLM
    >>> model: Phi3Model
        >>> embed_tokens: Embedding <<<
        >>> layers[0]: Phi3DecoderLayer
            >>> self_attn: Phi3Attention
                >>> o_proj: Linear <<<
                >>> qkv_proj: Linear <<<
            <<<
            >>> mlp: Phi3MLP
                >>> gate_up_proj: Linear <<<
                >>> down_proj: Linear <<<
                >>> activation_fn: SiLUActivation <<<
            <<<
            >>> input_layernorm: Phi3RMSNorm <<<
            >>> post_attention_layernorm: Phi3RMSNorm <<<
            >>> resid_attn_dropout: Dropout <<<
            >>> resid_mlp_dropout: Dropout <<<
        <<<
        >>> layers[1]: Phi3DecoderLayer
            >>> self_attn: Phi3Attention
                >>> o_proj: Linear <<<
                >>> qkv_proj: Linear <<<
            <<<
            >>> mlp: Phi3MLP
                >>> gate_up_proj: Linear <<<
                >>> down_proj: Linear <<<
                >>> activation_fn: SiLUActivation <<<
            <<<
            >>> input_layernorm: Phi3RMSNorm <<<
            >>> post_attention_layernorm: Phi3RMSNorm <<<
            >>> resid_attn_dropout: Dropout <<<
            >>> resid_mlp_dropout: Dropout <<<
        <<<
        >>> norm: Phi3RMSNorm <<<
        >>> rotary_emb: Phi3RotaryEmbedding <<<
    <<<
    >>> lm_head: Linear <<<
<<<

The we try to export to see the submodule failing the whole model. We can pickle the failing model and restore it to speedup the refactoring to make it work.

print("----------------------")
ep = diag.try_export(
    exporter="fx",
    use_dynamic_shapes=True,
    exporter_kwargs=dict(strict=False),
    verbose=1,
)
----------------------

[torch_export] export starts with backed_size_oblivious=False
[try_export-FX]  M:__main__-Phi3ForCausalLM --- FAIL, step=EXPORT, reason=Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None) --- For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation ---  --- The error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.['Traceback (most recent call last):\n', '  File "~/github/experimental-experiment/experimental_experiment/torch_interpreter/piece_by_piece.py", line 1572, in _try_export_no_bypass_export\n    ep = torch_export(\n         ^^^^^^^^^^^^^\n', '  File "~/github/experimental-experiment/experimental_experiment/export_helpers.py", line 152, in torch_export\n    return torch.export.export(\n           ^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 311, in export\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 277, in export\n    return _export(\n           ^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2292, in _export\n    ep = _export_for_training(\n         ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2101, in _export_for_training\n    export_artifact = export_func(\n                      ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1987, in _non_strict_export\n    ) = make_fake_inputs(\n        ^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_export/non_strict_utils.py", line 407, in make_fake_inputs\n    _check_dynamic_shapes(combined_args, dynamic_shapes)\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1049, in _check_dynamic_shapes\n    _tree_map_with_path(check_shape, combined_args, dynamic_shapes, tree_name="inputs")\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 632, in _tree_map_with_path\n    return tree_map_with_path(f, tree, *dynamic_shapes, is_leaf=is_leaf)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in tree_map_with_path\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 1199, in unflatten\n    leaves = list(leaves)\n             ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in <genexpr>\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n                              ^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 629, in f\n    return func(path, t, *dynamic_shapes)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1042, in check_shape\n    raise UserError(\n', "torch._dynamo.exc.UserError: Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None)\nFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation\n\nThe error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.\n"]
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] .. M:model-Phi3Model --- FAIL, step=EXPORT, reason=Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None) --- For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation ---  --- The error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.['Traceback (most recent call last):\n', '  File "~/github/experimental-experiment/experimental_experiment/torch_interpreter/piece_by_piece.py", line 1572, in _try_export_no_bypass_export\n    ep = torch_export(\n         ^^^^^^^^^^^^^\n', '  File "~/github/experimental-experiment/experimental_experiment/export_helpers.py", line 152, in torch_export\n    return torch.export.export(\n           ^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 311, in export\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 277, in export\n    return _export(\n           ^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2292, in _export\n    ep = _export_for_training(\n         ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2101, in _export_for_training\n    export_artifact = export_func(\n                      ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1987, in _non_strict_export\n    ) = make_fake_inputs(\n        ^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_export/non_strict_utils.py", line 407, in make_fake_inputs\n    _check_dynamic_shapes(combined_args, dynamic_shapes)\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1049, in _check_dynamic_shapes\n    _tree_map_with_path(check_shape, combined_args, dynamic_shapes, tree_name="inputs")\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 632, in _tree_map_with_path\n    return tree_map_with_path(f, tree, *dynamic_shapes, is_leaf=is_leaf)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in tree_map_with_path\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 1199, in unflatten\n    leaves = list(leaves)\n             ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in <genexpr>\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n                              ^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 629, in f\n    return func(path, t, *dynamic_shapes)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1042, in check_shape\n    raise UserError(\n', "torch._dynamo.exc.UserError: Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None)\nFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation\n\nThe error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.\n"]
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] .... M:embed_tokens-Embedding --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] .... M:layers[0]-Phi3DecoderLayer --- FAIL, step=EXPORT, reason=Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None) --- For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation ---  --- The error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.['Traceback (most recent call last):\n', '  File "~/github/experimental-experiment/experimental_experiment/torch_interpreter/piece_by_piece.py", line 1572, in _try_export_no_bypass_export\n    ep = torch_export(\n         ^^^^^^^^^^^^^\n', '  File "~/github/experimental-experiment/experimental_experiment/export_helpers.py", line 152, in torch_export\n    return torch.export.export(\n           ^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 311, in export\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 277, in export\n    return _export(\n           ^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2292, in _export\n    ep = _export_for_training(\n         ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2101, in _export_for_training\n    export_artifact = export_func(\n                      ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1987, in _non_strict_export\n    ) = make_fake_inputs(\n        ^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_export/non_strict_utils.py", line 407, in make_fake_inputs\n    _check_dynamic_shapes(combined_args, dynamic_shapes)\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1049, in _check_dynamic_shapes\n    _tree_map_with_path(check_shape, combined_args, dynamic_shapes, tree_name="inputs")\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 632, in _tree_map_with_path\n    return tree_map_with_path(f, tree, *dynamic_shapes, is_leaf=is_leaf)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in tree_map_with_path\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 1199, in unflatten\n    leaves = list(leaves)\n             ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in <genexpr>\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n                              ^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 629, in f\n    return func(path, t, *dynamic_shapes)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1042, in check_shape\n    raise UserError(\n', "torch._dynamo.exc.UserError: Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None)\nFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation\n\nThe error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.\n"]
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:self_attn-Phi3Attention --- FAIL, step=EXPORT, reason=Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None) --- For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation ---  --- The error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.['Traceback (most recent call last):\n', '  File "~/github/experimental-experiment/experimental_experiment/torch_interpreter/piece_by_piece.py", line 1572, in _try_export_no_bypass_export\n    ep = torch_export(\n         ^^^^^^^^^^^^^\n', '  File "~/github/experimental-experiment/experimental_experiment/export_helpers.py", line 152, in torch_export\n    return torch.export.export(\n           ^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 311, in export\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 277, in export\n    return _export(\n           ^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2292, in _export\n    ep = _export_for_training(\n         ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2101, in _export_for_training\n    export_artifact = export_func(\n                      ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1987, in _non_strict_export\n    ) = make_fake_inputs(\n        ^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_export/non_strict_utils.py", line 407, in make_fake_inputs\n    _check_dynamic_shapes(combined_args, dynamic_shapes)\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1049, in _check_dynamic_shapes\n    _tree_map_with_path(check_shape, combined_args, dynamic_shapes, tree_name="inputs")\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 632, in _tree_map_with_path\n    return tree_map_with_path(f, tree, *dynamic_shapes, is_leaf=is_leaf)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in tree_map_with_path\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 1199, in unflatten\n    leaves = list(leaves)\n             ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in <genexpr>\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n                              ^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 629, in f\n    return func(path, t, *dynamic_shapes)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1042, in check_shape\n    raise UserError(\n', "torch._dynamo.exc.UserError: Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None)\nFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation\n\nThe error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.\n"]
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ........ M:o_proj-Linear --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ........ M:qkv_proj-Linear --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:mlp-Phi3MLP --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:input_layernorm-Phi3RMSNorm --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:post_attention_layernorm-Phi3RMSNorm --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:resid_attn_dropout-Dropout --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:resid_mlp_dropout-Dropout --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] .... M:layers[1]-Phi3DecoderLayer --- FAIL, step=EXPORT, reason=Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None) --- For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation ---  --- The error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.['Traceback (most recent call last):\n', '  File "~/github/experimental-experiment/experimental_experiment/torch_interpreter/piece_by_piece.py", line 1572, in _try_export_no_bypass_export\n    ep = torch_export(\n         ^^^^^^^^^^^^^\n', '  File "~/github/experimental-experiment/experimental_experiment/export_helpers.py", line 152, in torch_export\n    return torch.export.export(\n           ^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 311, in export\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 277, in export\n    return _export(\n           ^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2292, in _export\n    ep = _export_for_training(\n         ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2101, in _export_for_training\n    export_artifact = export_func(\n                      ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1987, in _non_strict_export\n    ) = make_fake_inputs(\n        ^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_export/non_strict_utils.py", line 407, in make_fake_inputs\n    _check_dynamic_shapes(combined_args, dynamic_shapes)\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1049, in _check_dynamic_shapes\n    _tree_map_with_path(check_shape, combined_args, dynamic_shapes, tree_name="inputs")\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 632, in _tree_map_with_path\n    return tree_map_with_path(f, tree, *dynamic_shapes, is_leaf=is_leaf)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in tree_map_with_path\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 1199, in unflatten\n    leaves = list(leaves)\n             ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in <genexpr>\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n                              ^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 629, in f\n    return func(path, t, *dynamic_shapes)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1042, in check_shape\n    raise UserError(\n', "torch._dynamo.exc.UserError: Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None)\nFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation\n\nThe error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.\n"]
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:self_attn-Phi3Attention --- FAIL, step=EXPORT, reason=Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None) --- For more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation ---  --- The error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.['Traceback (most recent call last):\n', '  File "~/github/experimental-experiment/experimental_experiment/torch_interpreter/piece_by_piece.py", line 1572, in _try_export_no_bypass_export\n    ep = torch_export(\n         ^^^^^^^^^^^^^\n', '  File "~/github/experimental-experiment/experimental_experiment/export_helpers.py", line 152, in torch_export\n    return torch.export.export(\n           ^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 311, in export\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 277, in export\n    return _export(\n           ^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2292, in _export\n    ep = _export_for_training(\n         ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2101, in _export_for_training\n    export_artifact = export_func(\n                      ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1987, in _non_strict_export\n    ) = make_fake_inputs(\n        ^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_export/non_strict_utils.py", line 407, in make_fake_inputs\n    _check_dynamic_shapes(combined_args, dynamic_shapes)\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1049, in _check_dynamic_shapes\n    _tree_map_with_path(check_shape, combined_args, dynamic_shapes, tree_name="inputs")\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 632, in _tree_map_with_path\n    return tree_map_with_path(f, tree, *dynamic_shapes, is_leaf=is_leaf)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in tree_map_with_path\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 1199, in unflatten\n    leaves = list(leaves)\n             ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_pytree.py", line 2059, in <genexpr>\n    return treespec.unflatten(func(*xs) for xs in zip(*all_keypath_leaves))\n                              ^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 629, in f\n    return func(path, t, *dynamic_shapes)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/dynamic_shapes.py", line 1042, in check_shape\n    raise UserError(\n', "torch._dynamo.exc.UserError: Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}], [{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}]] specified at `dynamic_shapes['past_key_values']` to non-tensor type <class 'transformers.cache_utils.DynamicCache'> at `inputs['past_key_values']` (expected None)\nFor more information about this error, see: https://pytorch.org/docs/main/generated/exportdb/index.html#dynamic-shapes-validation\n\nThe error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.\n"]
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ........ M:o_proj-Linear --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ........ M:qkv_proj-Linear --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:mlp-Phi3MLP --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:input_layernorm-Phi3RMSNorm --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:post_attention_layernorm-Phi3RMSNorm --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:resid_attn_dropout-Dropout --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] ...... M:resid_mlp_dropout-Dropout --- OK:
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] .... M:norm-Phi3RMSNorm --- OK:
[torch_export] export starts with backed_size_oblivious=False



def forward(self, arg0_1: "f32[48]", arg1_1: "f32[s77, s27, 3072]", arg2_1: "i64[1, s9]"):
    # No stacktrace found for following nodes
    _set_grad_enabled = torch._C._set_grad_enabled(False);  _set_grad_enabled = None
    max_1: "i64[]" = torch.ops.aten.max.default(arg2_1);  arg2_1 = None
    add: "i64[]" = torch.ops.aten.add.Tensor(max_1, 1);  max_1 = None
    gt: "b8[]" = torch.ops.aten.gt.Scalar(add, 4096);  add = None
    ne: "b8[]" = torch.ops.aten.ne.Scalar(gt, 0);  gt = None
    item: "Sym(Eq(u0, 1))" = torch.ops.aten.item.default(ne);  ne = item = None
    _set_grad_enabled_1 = torch._C._set_grad_enabled(True);  _set_grad_enabled_1 = None




def forward(self, arg0_1: "f32[48]", arg1_1: "f32[s77, s27, 3072]", arg2_1: "i64[1, s9]"):
    # No stacktrace found for following nodes
    _set_grad_enabled = torch._C._set_grad_enabled(False);  _set_grad_enabled = None
    max_1: "i64[]" = torch.ops.aten.max.default(arg2_1);  arg2_1 = None
    add: "i64[]" = torch.ops.aten.add.Tensor(max_1, 1);  max_1 = None
    gt: "b8[]" = torch.ops.aten.gt.Scalar(add, 4096);  add = None
    ne: "b8[]" = torch.ops.aten.ne.Scalar(gt, 0);  gt = None
    item: "Sym(Eq(u0, 1))" = torch.ops.aten.item.default(ne);  ne = item = None
    _set_grad_enabled_1 = torch._C._set_grad_enabled(True);  _set_grad_enabled_1 = None

[try_export-FX] .... M:rotary_emb-Phi3RotaryEmbedding --- FAIL, step=EXPORT, reason=Could not guard on data-dependent expression Eq(u0, 1) (unhinted: Eq(u0, 1)).  (Size-like symbols: none) ---  --- consider using data-dependent friendly APIs such as guard_or_false, guard_or_true and statically_known_trueCaused by: (_export/non_strict_utils.py:1118 in __torch_function__) --- For more information, run with TORCH_LOGS="dynamic" --- For extended logs when we create symbols, also add TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL="u0" --- If you suspect the guard was triggered from C++, add TORCHDYNAMO_EXTENDED_DEBUG_CPP=1 --- For more debugging help, see https://docs.google.com/document/d/1HSuTTVvYH1pTew89Rtpeu84Ht3nQEFTYhAX3Ypa_xJs/edit?usp=sharing ---  --- For C++ stack trace, run with TORCHDYNAMO_EXTENDED_DEBUG_CPP=1 ---  --- The following call raised this error: ---   File "~/github/transformers/src/transformers/modeling_rope_utils.py", line 50, in longrope_frequency_update ---     if seq_len > original_max_position_embeddings: ---  ---  --- The error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.['Traceback (most recent call last):\n', '  File "~/github/experimental-experiment/experimental_experiment/torch_interpreter/piece_by_piece.py", line 1572, in _try_export_no_bypass_export\n    ep = torch_export(\n         ^^^^^^^^^^^^^\n', '  File "~/github/experimental-experiment/experimental_experiment/export_helpers.py", line 152, in torch_export\n    return torch.export.export(\n           ^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 311, in export\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/__init__.py", line 277, in export\n    return _export(\n           ^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2292, in _export\n    ep = _export_for_training(\n         ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1190, in wrapper\n    raise e\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1156, in wrapper\n    ep = fn(*args, **kwargs)\n         ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/exported_program.py", line 124, in wrapper\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2101, in _export_for_training\n    export_artifact = export_func(\n                      ^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 2032, in _non_strict_export\n    aten_export_artifact = _to_aten_func(  # type: ignore[operator]\n                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1823, in _export_to_aten_ir_make_fx\n    gm, graph_signature = transform(_make_fx_helper)(\n                          ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1952, in _aot_export_non_strict\n    gm, sig = aot_export(wrapped_mod, args, kwargs=kwargs, **flags)\n              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1736, in _make_fx_helper\n    gm = make_fx(\n         ^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py", line 2455, in wrapped\n    return make_fx_tracer.trace(f, *args)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py", line 2382, in trace\n    return self._trace_inner(f, *args)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py", line 2343, in _trace_inner\n    t = dispatch_trace(\n        ^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_compile.py", line 54, in inner\n    return disable_fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_dynamo/eval_frame.py", line 1104, in _fn\n    return fn(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py", line 1321, in dispatch_trace\n    graph = tracer.trace(root, concrete_args)  # type: ignore[arg-type]\n            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py", line 1935, in trace\n    res = super().trace(root, concrete_args)\n          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/_symbolic_trace.py", line 869, in trace\n    (self.create_arg(fn(*args)),),\n                     ^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py", line 1379, in wrapped\n    out = f(*tensors)  # type:ignore[call-arg]\n          ^^^^^^^^^^^\n', '  File "<string>", line 1, in <lambda>\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1623, in wrapped_fn\n    return tuple(flat_fn(*args))\n                 ^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_functorch/_aot_autograd/utils.py", line 189, in flat_fn\n    tree_out = fn(*args, **kwargs)\n               ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_functorch/_aot_autograd/graph_capture_wrappers.py", line 1357, in functional_call\n    out = mod(*args[params_len:], **kwargs)\n          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/_symbolic_trace.py", line 844, in module_call_wrapper\n    return self.call_module(mod, forward, args, kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py", line 2022, in call_module\n    return Tracer.call_module(self, m, forward, args, kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/_symbolic_trace.py", line 560, in call_module\n    ret_val = forward(*args, **kwargs)\n              ^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/_symbolic_trace.py", line 837, in forward\n    return _orig_module_call(mod, *args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1780, in _wrapped_call_impl\n    return self._call_impl(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1791, in _call_impl\n    return forward_call(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/export/_trace.py", line 1936, in forward\n    tree_out = mod(*args, **kwargs)\n               ^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/_symbolic_trace.py", line 844, in module_call_wrapper\n    return self.call_module(mod, forward, args, kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py", line 2022, in call_module\n    return Tracer.call_module(self, m, forward, args, kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/_symbolic_trace.py", line 560, in call_module\n    ret_val = forward(*args, **kwargs)\n              ^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/_symbolic_trace.py", line 837, in forward\n    return _orig_module_call(mod, *args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1780, in _wrapped_call_impl\n    return self._call_impl(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/nn/modules/module.py", line 1791, in _call_impl\n    return forward_call(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/utils/_contextlib.py", line 122, in decorate_context\n    return func(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/github/transformers/src/transformers/modeling_rope_utils.py", line 86, in wrapper\n    longrope_frequency_update(self, position_ids, device=x.device)\n', '  File "~/github/transformers/src/transformers/modeling_rope_utils.py", line 50, in longrope_frequency_update\n    if seq_len > original_max_position_embeddings:\n       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py", line 1428, in __torch_function__\n    return func(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/proxy_tensor.py", line 1499, in __torch_function__\n    return func(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/_export/non_strict_utils.py", line 1118, in __torch_function__\n    return func(*args, **kwargs)\n           ^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/sym_node.py", line 538, in guard_bool\n    r = self.evaluate()\n        ^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/sym_node.py", line 512, in evaluate\n    return self.shape_env.evaluate_sym_node(self, size_oblivious)\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/symbolic_shapes.py", line 7296, in evaluate_sym_node\n    return self.evaluate_expr(\n           ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/symbolic_shapes.py", line 7396, in evaluate_expr\n    return self._inner_evaluate_expr(\n           ^^^^^^^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/recording.py", line 272, in wrapper\n    return retlog(fn(*args, **kwargs))\n                  ^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/symbolic_shapes.py", line 7419, in _inner_evaluate_expr\n    return self._evaluate_expr(\n           ^^^^^^^^^^^^^^^^^^^^\n', '  File "~/vv/this312/lib/python3.12/site-packages/torch/fx/experimental/symbolic_shapes.py", line 7640, in _evaluate_expr\n    raise self._make_data_dependent_error(\n', 'torch.fx.experimental.symbolic_shapes.GuardOnDataDependentSymNode: Could not guard on data-dependent expression Eq(u0, 1) (unhinted: Eq(u0, 1)).  (Size-like symbols: none)\n\nconsider using data-dependent friendly APIs such as guard_or_false, guard_or_true and statically_known_trueCaused by: (_export/non_strict_utils.py:1118 in __torch_function__)\nFor more information, run with TORCH_LOGS="dynamic"\nFor extended logs when we create symbols, also add TORCHDYNAMO_EXTENDED_DEBUG_CREATE_SYMBOL="u0"\nIf you suspect the guard was triggered from C++, add TORCHDYNAMO_EXTENDED_DEBUG_CPP=1\nFor more debugging help, see https://docs.google.com/document/d/1HSuTTVvYH1pTew89Rtpeu84Ht3nQEFTYhAX3Ypa_xJs/edit?usp=sharing\n\nFor C++ stack trace, run with TORCHDYNAMO_EXTENDED_DEBUG_CPP=1\n\nThe following call raised this error:\n  File "~/github/transformers/src/transformers/modeling_rope_utils.py", line 50, in longrope_frequency_update\n    if seq_len > original_max_position_embeddings:\n\n\nThe error above occurred when calling torch.export.export. If you would like to view some more information about this error, and get a list of all other errors that may occur in your export call, you can replace your `export()` call with `draft_export()`.\n']
[try_export-FX] .... M:rotary_emb-Phi3RotaryEmbedding --- FAIL: Could not guard on data-depend...
[torch_export] export starts with backed_size_oblivious=False
[try_export-FX] .. M:lm_head-Linear --- OK:

Let’s display a report.

print(f"success: {ep.status}")
print(diag.get_export_report())
success: 2
__main__                         Phi3ForCausalLM       FAIL -- step=EXPORT, reason='Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHin...'
..model                          Phi3Model             FAIL -- step=EXPORT, reason='Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHin...'
....embed_tokens                 Embedding             OK -- ExportedProgram
....layers[0]                    Phi3DecoderLayer      FAIL -- step=EXPORT, reason='Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHin...'
......self_attn                  Phi3Attention         FAIL -- step=EXPORT, reason='Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHin...'
........o_proj                   Linear                OK -- ExportedProgram
........qkv_proj                 Linear                OK -- ExportedProgram
......mlp                        Phi3MLP               OK -- ExportedProgram
........gate_up_proj             Linear                <OK-2i-0>
........down_proj                Linear                <OK-2i-0>
........activation_fn            SiLUActivation        <OK-2i-0>
......input_layernorm            Phi3RMSNorm           OK -- ExportedProgram
......post_attention_layernorm   Phi3RMSNorm           OK -- ExportedProgram
......resid_attn_dropout         Dropout               OK -- ExportedProgram
......resid_mlp_dropout          Dropout               OK -- ExportedProgram
....layers[1]                    Phi3DecoderLayer      FAIL -- step=EXPORT, reason='Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHin...'
......self_attn                  Phi3Attention         FAIL -- step=EXPORT, reason='Cannot associate shape [[{0: DimHint(DYNAMIC), 2: DimHint(DYNAMIC)}, {0: DimHint(DYNAMIC), 2: DimHin...'
........o_proj                   Linear                OK -- ExportedProgram
........qkv_proj                 Linear                OK -- ExportedProgram
......mlp                        Phi3MLP               OK -- ExportedProgram
........gate_up_proj             Linear                <OK-2i-0>
........down_proj                Linear                <OK-2i-0>
........activation_fn            SiLUActivation        <OK-2i-0>
......input_layernorm            Phi3RMSNorm           OK -- ExportedProgram
......post_attention_layernorm   Phi3RMSNorm           OK -- ExportedProgram
......resid_attn_dropout         Dropout               OK -- ExportedProgram
......resid_mlp_dropout          Dropout               OK -- ExportedProgram
....norm                         Phi3RMSNorm           OK -- ExportedProgram
....rotary_emb                   Phi3RotaryEmbedding   FAIL -- step=EXPORT, reason='Could not guard on data-dependent expression Eq(u0, 1) (unhinted: Eq(u0, 1)).  (Size-like symbols: n...'
..lm_head                        Linear                OK -- ExportedProgram

Replace the failing module by a custom op

The main module is not exportable because one piece cannot be exported. But maybe if we assume it works, maybe everything else is working. So let’s try to replace this class by a custom op. This will be something for another example.

Total running time of the script: (0 minutes 5.488 seconds)

Related examples

Export Phi-3.5-mini-instruct with report_exportability

Export Phi-3.5-mini-instruct with report_exportability

Export Phi-3.5-mini-instruct with draft_export

Export Phi-3.5-mini-instruct with draft_export

Check the exporter on a dummy from HuggingFace

Check the exporter on a dummy from HuggingFace

to_onnx and submodules from LLMs

to_onnx and submodules from LLMs

to_onnx and a model with a test

to_onnx and a model with a test

Gallery generated by Sphinx-Gallery