| 419 | self.modifier_token_id = modifier_token_id |
| 420 | |
| 421 | def add_token(self, initializer_token): |
| 422 | initializer_token_id = [] |
| 423 | for modifier_token_, initializer_token_ in zip(self.modifier_token, initializer_token): |
| 424 | # Add the placeholder token in tokenizer |
| 425 | num_added_tokens = self.tokenizer.add_tokens(modifier_token_) |
| 426 | if num_added_tokens == 0: |
| 427 | raise ValueError( |
| 428 | f"The tokenizer already contains the token {modifier_token_}. Please pass a different" |
| 429 | " `modifier_token` that is not already in the tokenizer." |
| 430 | ) |
| 431 | |
| 432 | # Convert the initializer_token, placeholder_token to ids |
| 433 | token_ids = self.tokenizer.encode([initializer_token_], add_special_tokens=False) |
| 434 | # Check if initializer_token is a single token or a sequence of tokens |
| 435 | if len(token_ids) > 1: |
| 436 | raise ValueError("The initializer token must be a single token.") |
| 437 | |
| 438 | self.modifier_token_id.append(self.tokenizer.convert_tokens_to_ids(modifier_token_)) |
| 439 | initializer_token_id.append(token_ids[0]) |
| 440 | # Resize the token embeddings as we are adding new special tokens to the tokenizer |
| 441 | self.text_encoder.resize_token_embeddings(len(self.tokenizer)) |
| 442 | |
| 443 | # Initialise the newly added placeholder token with the embeddings of the initializer token |
| 444 | token_embeds = self.text_encoder.get_input_embeddings().weight.data |
| 445 | for (x, y) in zip(self.modifier_token_id, initializer_token_id): |
| 446 | token_embeds[x] = token_embeds[y] |
| 447 | |
| 448 | def save_pretrained(self, save_path, freeze_model="crossattn_kv", save_text_encoder=False, all=False): |
| 449 | if all: |