forked from kohya-ss/sd-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fdadf4d
commit 6b3134e
Showing
8 changed files
with
52 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import logging | ||
from safetensors.torch import load_file | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def inject_embedding(model, strategy, tokenizer, placeholder, embed_file, embed_key): | ||
embed_state_dict = load_file(embed_file) | ||
if not embed_key in embed_state_dict: | ||
raise Exception(f"{embed_key} not found in {embed_file}") | ||
embed = embed_state_dict[embed_key] | ||
placeholders = [f"{placeholder.replace(' ', '_')}_{i}" for i in range(0, len(embed))] | ||
tokenizer.add_tokens(placeholders) | ||
indexes = tokenizer.convert_tokens_to_ids(placeholders) | ||
if (model.get_input_embeddings().num_embeddings <= len(tokenizer)): | ||
model.resize_token_embeddings(len(tokenizer)) | ||
logger.info(f"Expanded model embeddings to : {model.get_input_embeddings().num_embeddings}") | ||
for e, i in zip(embed, indexes): | ||
model.get_input_embeddings().weight.data[i] = e | ||
logger.info(f"Added custom embedding for {placeholder} to {embed_key} as token(s) {indexes}") | ||
strategy.add_replacement(placeholder, " ".join(placeholders)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters