MCPcopy Create free account
hub / github.com/OpenImagingLab/FlashVSR / tokenize_long_prompt

Function tokenize_long_prompt

diffsynth/prompters/base_prompter.py:6–35  ·  view source on GitHub ↗
(tokenizer, prompt, max_length=None)

Source from the content-addressed store, hash-verified

4
5
6def 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

Callers 2

encode_promptMethod · 0.85
encode_promptMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected