|
| 1 | +from typing import Any, Dict |
| 2 | +import torch |
| 3 | +import transformers |
| 4 | +from ...cache_helpers import make_dynamic_cache |
| 5 | + |
| 6 | + |
| 7 | +def get_phi2( |
| 8 | + batch_size: int = 1, |
| 9 | + sequence_length: int = 30, |
| 10 | + sequence_length2: int = 3, |
| 11 | + dynamic_rope: bool = False, |
| 12 | + **kwargs, |
| 13 | +) -> Dict[str, Any]: |
| 14 | + """ |
| 15 | + Gets a non initialized model |
| 16 | + similar to `microsoft/phi-2 <https://huggingface.co/microsoft/phi-2>`_ |
| 17 | +
|
| 18 | + :param batch_size: batch size |
| 19 | + :param sequence_length: sequence length |
| 20 | + :param sequence_length2: new sequence length |
| 21 | + :param dynamic_rope: use dynamic rope (see :class:`transformers.LlamaConfig`) |
| 22 | + :param kwargs: to overwrite the configuration, example ``num_hidden_layers=1`` |
| 23 | + :return: dictionary |
| 24 | +
|
| 25 | + See :ref:`l-plot-tiny-llm-export-patched` for an example with a similar model. |
| 26 | + """ |
| 27 | + config = { |
| 28 | + "_name_or_path": "microsoft/phi-2", |
| 29 | + "architectures": ["PhiForCausalLM"], |
| 30 | + "attention_dropout": 0.0, |
| 31 | + "bos_token_id": 50256, |
| 32 | + "embd_pdrop": 0.0, |
| 33 | + "eos_token_id": 50256, |
| 34 | + "hidden_act": "gelu_new", |
| 35 | + "hidden_size": 2560, |
| 36 | + "initializer_range": 0.02, |
| 37 | + "intermediate_size": 10240, |
| 38 | + "layer_norm_eps": 1e-05, |
| 39 | + "max_position_embeddings": 2048, |
| 40 | + "model_type": "phi", |
| 41 | + "num_attention_heads": 32, |
| 42 | + "num_hidden_layers": 32, |
| 43 | + "num_key_value_heads": 32, |
| 44 | + "partial_rotary_factor": 0.4, |
| 45 | + "qk_layernorm": False, |
| 46 | + "resid_pdrop": 0.1, |
| 47 | + "rope_scaling": {"rope_type": "dynamic", "factor": 10.0} if dynamic_rope else None, |
| 48 | + "rope_theta": 10000.0, |
| 49 | + "tie_word_embeddings": False, |
| 50 | + "torch_dtype": "float16", |
| 51 | + "transformers_version": "4.37.0", |
| 52 | + "use_cache": True, |
| 53 | + "vocab_size": 51200, |
| 54 | + } |
| 55 | + config.update(**kwargs) |
| 56 | + conf = transformers.PhiConfig(**config) |
| 57 | + model = transformers.PhiForCausalLM(conf) |
| 58 | + model.eval() |
| 59 | + |
| 60 | + # now the inputs |
| 61 | + cache_last_dim = 80 |
| 62 | + max_token_id = config["vocab_size"] - 1 |
| 63 | + n_layers = config["num_hidden_layers"] |
| 64 | + num_key_value_heads = config["num_key_value_heads"] |
| 65 | + |
| 66 | + batch = torch.export.Dim("batch", min=1, max=1024) |
| 67 | + seq_length = torch.export.Dim("seq_length", min=1, max=4096) |
| 68 | + cache_length = torch.export.Dim("cache_length", min=1, max=4096) |
| 69 | + |
| 70 | + shapes = { |
| 71 | + "input_ids": {0: batch, 1: seq_length}, |
| 72 | + "position_ids": { |
| 73 | + 0: batch, |
| 74 | + 1: torch.export.Dim.DYNAMIC, # cache_length + seq_length |
| 75 | + }, |
| 76 | + "attention_mask": { |
| 77 | + 0: batch, |
| 78 | + 1: torch.export.Dim.DYNAMIC, # cache_length + seq_length |
| 79 | + }, |
| 80 | + "past_key_values": [ |
| 81 | + [{0: batch, 2: cache_length} for _ in range(n_layers)], |
| 82 | + [{0: batch, 2: cache_length} for _ in range(n_layers)], |
| 83 | + ], |
| 84 | + } |
| 85 | + inputs = dict( |
| 86 | + input_ids=torch.randint(0, max_token_id, (batch_size, sequence_length2)).to( |
| 87 | + torch.int64 |
| 88 | + ), |
| 89 | + attention_mask=torch.ones((batch_size, sequence_length + sequence_length2)).to( |
| 90 | + torch.int64 |
| 91 | + ), |
| 92 | + position_ids=torch.arange(sequence_length, sequence_length + sequence_length2) |
| 93 | + .to(torch.int64) |
| 94 | + .expand((batch_size, -1)), |
| 95 | + past_key_values=make_dynamic_cache( |
| 96 | + [ |
| 97 | + ( |
| 98 | + torch.randn( |
| 99 | + batch_size, num_key_value_heads, sequence_length, cache_last_dim |
| 100 | + ), |
| 101 | + torch.randn( |
| 102 | + batch_size, num_key_value_heads, sequence_length, cache_last_dim |
| 103 | + ), |
| 104 | + ) |
| 105 | + for i in range(n_layers) |
| 106 | + ] |
| 107 | + ), |
| 108 | + ) |
| 109 | + return dict(inputs=inputs, model=model, dynamic_shapes=shapes) |
0 commit comments