|
| 1 | +from typing import Any, Callable, Dict, Optional, Tuple |
| 2 | +import torch |
| 3 | +from ..helpers.config_helper import update_config, check_hasattr |
| 4 | + |
| 5 | +__TASK__ = "feature-extraction" |
| 6 | + |
| 7 | + |
| 8 | +def reduce_model_config(config: Any) -> Dict[str, Any]: |
| 9 | + """Reduces a model size.""" |
| 10 | + check_hasattr(config, "num_attention_heads", "num_hidden_layers") |
| 11 | + kwargs = dict( |
| 12 | + num_hidden_layers=min(config.num_hidden_layers, 2), |
| 13 | + num_attention_heads=min(config.num_attention_heads, 4), |
| 14 | + ) |
| 15 | + update_config(config, kwargs) |
| 16 | + return kwargs |
| 17 | + |
| 18 | + |
| 19 | +def get_inputs( |
| 20 | + model: torch.nn.Module, |
| 21 | + config: Optional[Any], |
| 22 | + batch_size: int, |
| 23 | + sequence_length: int, |
| 24 | + dummy_max_token_id: int, |
| 25 | + **kwargs, # unused |
| 26 | +): |
| 27 | + """ |
| 28 | + Generates inputs for task ``feature-extraction``. |
| 29 | + Example: |
| 30 | +
|
| 31 | + :: |
| 32 | +
|
| 33 | + input_ids:T7s1x13[101,72654:A16789.23076923077], |
| 34 | + token_type_ids:T7s1x13[0,0:A0.0], |
| 35 | + attention_mask:T7s1x13[1,1:A1.0]) |
| 36 | + """ |
| 37 | + batch = torch.export.Dim("batch", min=1, max=1024) |
| 38 | + seq_length = "sequence_length" |
| 39 | + shapes = { |
| 40 | + "input_ids": {0: batch, 1: seq_length}, |
| 41 | + "attention_mask": {0: batch, 1: seq_length}, |
| 42 | + } |
| 43 | + inputs = dict( |
| 44 | + input_ids=torch.randint(0, dummy_max_token_id, (batch_size, sequence_length)).to( |
| 45 | + torch.int64 |
| 46 | + ), |
| 47 | + attention_mask=torch.ones((batch_size, sequence_length)).to(torch.int64), |
| 48 | + ) |
| 49 | + return dict(inputs=inputs, dynamic_shapes=shapes) |
| 50 | + |
| 51 | + |
| 52 | +def random_input_kwargs(config: Any) -> Tuple[Dict[str, Any], Callable]: |
| 53 | + """ |
| 54 | + Inputs kwargs. |
| 55 | +
|
| 56 | + If the configuration is None, the function selects typical dimensions. |
| 57 | + """ |
| 58 | + if config is not None: |
| 59 | + check_hasattr(config, "vocab_size") |
| 60 | + kwargs = dict( |
| 61 | + batch_size=2, |
| 62 | + sequence_length=30, |
| 63 | + dummy_max_token_id=31999 if config is None else (config.vocab_size - 1), |
| 64 | + ) |
| 65 | + return kwargs, get_inputs |
0 commit comments