def _add_token(
        self, placeholder_token: str, initializer_token: str, num_vectors_per_token: int, initialize_parameters: bool
    ):
        r"""Add placeholder tokens to the tokenizer.
        borrowed from https://github.com/huggingface/diffusers/blob/main/
        examples/textual_inversion/textual_inversion.py#L669 # noqa
        """
        assert num_vectors_per_token >= 1, "num_vectors_per_token should be greater than 0"

        placeholder_tokens = [placeholder_token]

        # create dummy tokens for multi-vector
        additional_tokens = []
        for i in range(1, num_vectors_per_token):
            additional_tokens.append(f"{placeholder_token}_{i}")
        placeholder_tokens += additional_tokens

        # add dummy tokens into tokenizer
        num_added_tokens = self.tokenizer.add_tokens(placeholder_tokens)
        assert num_added_tokens == num_vectors_per_token, (
            f"The tokenizer already contains the token {placeholder_token}. Please pass a different"
            f" `placeholder_token` that is not already in the tokenizer."
        )

        # Convert the initializer_token, placeholder_token to ids
        # Check if initializer_token is a single token or a sequence of tokens
        token_ids = self.tokenizer.encode(initializer_token, add_special_tokens=False)
        assert len(token_ids) == 1, "The initializer token must be a single token."

        initializer_token_id = token_ids[0]
        placeholder_token_ids = self.tokenizer.convert_tokens_to_ids(placeholder_tokens)

        # skip initialization on text_encoder for trained models
        if initialize_parameters:
            # Resize the token embeddings as we are adding new special tokens to the tokenizer
            self.text_encoder.resize_token_embeddings(len(self.tokenizer))

            # Initialise the newly added placeholder token with the embeddings of the initializer token
            token_embeds = self.text_encoder.get_input_embeddings().weight.data
            with torch.no_grad():
                for token_id in placeholder_token_ids:
                    token_embeds[token_id] = token_embeds[initializer_token_id].clone()

        return placeholder_token_ids
Add placeholder tokens to the tokenizer.
        borrowed from https://github.com/huggingface/diffusers/blob/main/
        examples/textual_inversion/textual_inversion.py#L669 # noqa
        
powerpaint/utils/loaders.py