@@ -457,13 +457,14 @@ def load_embed(embedding_name, embedding_directory, embedding_size, embed_key=No
457457 return embed_out
458458
459459class SDTokenizer :
460- def __init__ (self , tokenizer_path = None , max_length = 77 , pad_with_end = True , embedding_directory = None , embedding_size = 768 , embedding_key = 'clip_l' , tokenizer_class = CLIPTokenizer , has_start_token = True , has_end_token = True , pad_to_max_length = True , min_length = None , pad_token = None , end_token = None , tokenizer_data = {}, tokenizer_args = {}):
460+ def __init__ (self , tokenizer_path = None , max_length = 77 , pad_with_end = True , embedding_directory = None , embedding_size = 768 , embedding_key = 'clip_l' , tokenizer_class = CLIPTokenizer , has_start_token = True , has_end_token = True , pad_to_max_length = True , min_length = None , pad_token = None , end_token = None , min_padding = None , tokenizer_data = {}, tokenizer_args = {}):
461461 if tokenizer_path is None :
462462 tokenizer_path = os .path .join (os .path .dirname (os .path .realpath (__file__ )), "sd1_tokenizer" )
463463 self .tokenizer = tokenizer_class .from_pretrained (tokenizer_path , ** tokenizer_args )
464464 self .max_length = tokenizer_data .get ("{}_max_length" .format (embedding_key ), max_length )
465465 self .min_length = min_length
466466 self .end_token = None
467+ self .min_padding = min_padding
467468
468469 empty = self .tokenizer ('' )["input_ids" ]
469470 self .tokenizer_adds_end_token = has_end_token
@@ -518,13 +519,15 @@ def _try_get_embedding(self, embedding_name:str):
518519 return (embed , leftover )
519520
520521
521- def tokenize_with_weights (self , text :str , return_word_ids = False , ** kwargs ):
522+ def tokenize_with_weights (self , text :str , return_word_ids = False , tokenizer_options = {}, ** kwargs ):
522523 '''
523524 Takes a prompt and converts it to a list of (token, weight, word id) elements.
524525 Tokens can both be integer tokens and pre computed CLIP tensors.
525526 Word id values are unique per word and embedding, where the id 0 is reserved for non word tokens.
526527 Returned list has the dimensions NxM where M is the input size of CLIP
527528 '''
529+ min_length = tokenizer_options .get ("{}_min_length" .format (self .embedding_key ), self .min_length )
530+ min_padding = tokenizer_options .get ("{}_min_padding" .format (self .embedding_key ), self .min_padding )
528531
529532 text = escape_important (text )
530533 parsed_weights = token_weights (text , 1.0 )
@@ -603,10 +606,12 @@ def tokenize_with_weights(self, text:str, return_word_ids=False, **kwargs):
603606 #fill last batch
604607 if self .end_token is not None :
605608 batch .append ((self .end_token , 1.0 , 0 ))
606- if self .pad_to_max_length :
609+ if min_padding is not None :
610+ batch .extend ([(self .pad_token , 1.0 , 0 )] * min_padding )
611+ if self .pad_to_max_length and len (batch ) < self .max_length :
607612 batch .extend ([(self .pad_token , 1.0 , 0 )] * (self .max_length - len (batch )))
608- if self . min_length is not None and len (batch ) < self . min_length :
609- batch .extend ([(self .pad_token , 1.0 , 0 )] * (self . min_length - len (batch )))
613+ if min_length is not None and len (batch ) < min_length :
614+ batch .extend ([(self .pad_token , 1.0 , 0 )] * (min_length - len (batch )))
610615
611616 if not return_word_ids :
612617 batched_tokens = [[(t , w ) for t , w ,_ in x ] for x in batched_tokens ]
@@ -634,7 +639,7 @@ def __init__(self, embedding_directory=None, tokenizer_data={}, clip_name="l", t
634639
635640 def tokenize_with_weights (self , text :str , return_word_ids = False , ** kwargs ):
636641 out = {}
637- out [self .clip_name ] = getattr (self , self .clip ).tokenize_with_weights (text , return_word_ids )
642+ out [self .clip_name ] = getattr (self , self .clip ).tokenize_with_weights (text , return_word_ids , ** kwargs )
638643 return out
639644
640645 def untokenize (self , token_weight_pair ):
0 commit comments