|
| 1 | +import random |
| 2 | +import torch |
| 3 | +from transformers import AutoTokenizer |
| 4 | +from typing import Sequence, Tuple |
| 5 | + |
| 6 | +EXAMPLE_Text = ["best hotel in bay area", "here is an example of gpt2 model"] |
| 7 | + |
| 8 | + |
| 9 | +def get_tokenizer(model_name_or_path: str, cache_dir: str): |
| 10 | + tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, cache_dir=cache_dir) |
| 11 | + tokenizer.padding_side = "left" |
| 12 | + tokenizer.pad_token = tokenizer.eos_token |
| 13 | + return tokenizer |
| 14 | + |
| 15 | + |
| 16 | +def get_example_inputs( |
| 17 | + model_name_or_path: str, |
| 18 | + cache_dir: str, |
| 19 | + num_attention_heads: int, |
| 20 | + num_layer: int, |
| 21 | + hidden_size: int, |
| 22 | + device: str, |
| 23 | + prompt_text: Sequence[str] = EXAMPLE_Text, |
| 24 | +): |
| 25 | + tokenizer = get_tokenizer(model_name_or_path, cache_dir) |
| 26 | + encodings_dict = tokenizer.batch_encode_plus(prompt_text, padding=True) |
| 27 | + |
| 28 | + input_ids = torch.tensor(encodings_dict["input_ids"], dtype=torch.int32) |
| 29 | + attention_mask = torch.tensor(encodings_dict["attention_mask"], dtype=torch.int32) |
| 30 | + position_ids = attention_mask.long().cumsum(-1) - 1 |
| 31 | + position_ids.masked_fill_(position_ids < 0, 0) |
| 32 | + position_ids = position_ids.to(torch.int32) |
| 33 | + |
| 34 | + # Empty Past State for generating first word |
| 35 | + empty_past = [] |
| 36 | + batch_size = input_ids.size(0) |
| 37 | + sequence_length = input_ids.size(1) |
| 38 | + past_shape = [ |
| 39 | + 2, |
| 40 | + batch_size, |
| 41 | + num_attention_heads, |
| 42 | + 0, |
| 43 | + hidden_size // num_attention_heads, |
| 44 | + ] |
| 45 | + for i in range(num_layer): |
| 46 | + empty_past.append(torch.empty(past_shape).type(torch.float32).to(device)) |
| 47 | + |
| 48 | + return input_ids, attention_mask, position_ids, empty_past |
| 49 | + |
| 50 | + |
| 51 | +def get_dummy_inputs( |
| 52 | + batch_size: int, |
| 53 | + past_sequence_length: int, |
| 54 | + sequence_length: int, |
| 55 | + num_attention_heads: int, |
| 56 | + hidden_size: int, |
| 57 | + num_layer: int, |
| 58 | + vocab_size: int, |
| 59 | + device: torch.device, |
| 60 | + has_position_ids: bool = True, |
| 61 | + has_attention_mask: bool = True, |
| 62 | + input_ids_dtype: torch.dtype = torch.int64, |
| 63 | + position_ids_dtype: torch.dtype = torch.int64, |
| 64 | + attention_mask_dtype: torch.dtype = torch.int64, |
| 65 | +) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor, torch.Tensor]: |
| 66 | + """Create random inputs for GPT2 model. |
| 67 | + Returns torch tensors of input_ids, position_ids, attention_mask and a list of past state tensors. |
| 68 | + """ |
| 69 | + past_shape = [ |
| 70 | + 2, |
| 71 | + batch_size, |
| 72 | + num_attention_heads, |
| 73 | + past_sequence_length, |
| 74 | + int(hidden_size / num_attention_heads), |
| 75 | + ] |
| 76 | + |
| 77 | + past = [ |
| 78 | + (torch.rand(past_shape, dtype=torch.float32, device=device) * 2.0 - 1.0) |
| 79 | + for _ in range(num_layer) |
| 80 | + ] |
| 81 | + input_ids = torch.randint( |
| 82 | + low=0, |
| 83 | + high=vocab_size - 1, |
| 84 | + size=(batch_size, sequence_length), |
| 85 | + dtype=input_ids_dtype, |
| 86 | + device=device, |
| 87 | + ) |
| 88 | + |
| 89 | + attention_mask = None |
| 90 | + if has_attention_mask: |
| 91 | + total_sequence_length = past_sequence_length + sequence_length |
| 92 | + attention_mask = torch.ones( |
| 93 | + [batch_size, total_sequence_length], |
| 94 | + dtype=attention_mask_dtype, |
| 95 | + device=device, |
| 96 | + ) |
| 97 | + if total_sequence_length >= 2: |
| 98 | + padding_position = random.randint( |
| 99 | + 0, total_sequence_length - 1 |
| 100 | + ) # test input with padding. |
| 101 | + attention_mask[:, padding_position] = 0 |
| 102 | + |
| 103 | + # Deduce position_ids from attention mask |
| 104 | + position_ids = None |
| 105 | + if has_position_ids: |
| 106 | + position_ids = attention_mask.long().cumsum(-1) - 1 |
| 107 | + position_ids.masked_fill_(position_ids < 0, 0) |
| 108 | + position_ids = position_ids[:, past_sequence_length:].to(position_ids_dtype) |
| 109 | + |
| 110 | + return (input_ids, attention_mask, position_ids, past) |
0 commit comments