(tokenizer, prompt, max_length=None)
| 4 | |
| 5 | |
| 6 | def tokenize_long_prompt(tokenizer, prompt, max_length=None): |
| 7 | # Get model_max_length from self.tokenizer |
| 8 | length = tokenizer.model_max_length if max_length is None else max_length |
| 9 | |
| 10 | # To avoid the warning. set self.tokenizer.model_max_length to +oo. |
| 11 | tokenizer.model_max_length = 99999999 |
| 12 | |
| 13 | # Tokenize it! |
| 14 | input_ids = tokenizer(prompt, return_tensors="pt").input_ids |
| 15 | |
| 16 | # Determine the real length. |
| 17 | max_length = (input_ids.shape[1] + length - 1) // length * length |
| 18 | |
| 19 | # Restore tokenizer.model_max_length |
| 20 | tokenizer.model_max_length = length |
| 21 | |
| 22 | # Tokenize it again with fixed length. |
| 23 | input_ids = tokenizer( |
| 24 | prompt, |
| 25 | return_tensors="pt", |
| 26 | padding="max_length", |
| 27 | max_length=max_length, |
| 28 | truncation=True |
| 29 | ).input_ids |
| 30 | |
| 31 | # Reshape input_ids to fit the text encoder. |
| 32 | num_sentence = input_ids.shape[1] // length |
| 33 | input_ids = input_ids.reshape((num_sentence, length)) |
| 34 | |
| 35 | return input_ids |
| 36 | |
| 37 | |
| 38 |
no outgoing calls
no test coverage detected