Merge branch 'main' into refactor_use_compel

This commit is contained in:
Jonathan 2023-02-21 21:14:28 -06:00 committed by GitHub
commit a461875abd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 25 deletions

View File

@ -975,7 +975,7 @@ class Generate:
ti_path, defer_injecting_tokens=True
)
print(
f'>> Textual inversion triggers: {", ".join(self.model.textual_inversion_manager.get_all_trigger_strings())}'
f'>> Textual inversion triggers: {", ".join(sorted(self.model.textual_inversion_manager.get_all_trigger_strings()))}'
)
self.model_name = model_name

View File

@ -35,6 +35,7 @@ class TextualInversionManager(BaseTextualInversionManager):
self.text_encoder = text_encoder
self.full_precision = full_precision
self.hf_concepts_library = HuggingFaceConceptsLibrary()
self.trigger_to_sourcefile = dict()
default_textual_inversions: list[TextualInversion] = []
self.textual_inversions = default_textual_inversions
@ -60,15 +61,17 @@ class TextualInversionManager(BaseTextualInversionManager):
def get_all_trigger_strings(self) -> list[str]:
return [ti.trigger_string for ti in self.textual_inversions]
def load_textual_inversion(self, ckpt_path: Union[str,Path], defer_injecting_tokens: bool = False):
def load_textual_inversion(
self, ckpt_path: Union[str, Path], defer_injecting_tokens: bool = False
):
ckpt_path = Path(ckpt_path)
if not ckpt_path.is_file():
return
if str(ckpt_path).endswith(".DS_Store"):
return
try:
scan_result = scan_file_path(str(ckpt_path))
if scan_result.infected_files == 1:
@ -90,27 +93,45 @@ class TextualInversionManager(BaseTextualInversionManager):
return
elif (
self.text_encoder.get_input_embeddings().weight.data[0].shape[0]
!= embedding_info['token_dim']
!= embedding_info["token_dim"]
):
print(
f"** Notice: {ckpt_path.parents[0].name}/{ckpt_path.name} was trained on a model with an incompatible token dimension: {self.text_encoder.get_input_embeddings().weight.data[0].shape[0]} vs {embedding_info['token_dim']}."
)
return
if embedding_info:
try:
self._add_textual_inversion(
embedding_info["name"],
embedding_info["embedding"],
defer_injecting_tokens=defer_injecting_tokens,
)
except ValueError as e:
print(f' | Ignoring incompatible embedding {embedding_info["name"]}')
print(f" | The error was {str(e)}")
else:
print(
f">> Failed to load embedding located at {str(ckpt_path)}. Unsupported file."
# Resolve the situation in which an earlier embedding has claimed the same
# trigger string. We replace the trigger with '<source_file>', as we used to.
trigger_str = embedding_info["name"]
sourcefile = (
f"{ckpt_path.parent.name}/{ckpt_path.name}"
if ckpt_path.name == "learned_embeds.bin"
else ckpt_path.name
)
if trigger_str in self.trigger_to_sourcefile:
replacement_trigger_str = (
f"<{ckpt_path.parent.name}>"
if ckpt_path.name == "learned_embeds.bin"
else f"<{ckpt_path.stem}>"
)
print(
f">> {sourcefile}: Trigger token '{trigger_str}' is already claimed by '{self.trigger_to_sourcefile[trigger_str]}'. Trigger this concept with {replacement_trigger_str}"
)
trigger_str = replacement_trigger_str
try:
self._add_textual_inversion(
trigger_str,
embedding_info["embedding"],
defer_injecting_tokens=defer_injecting_tokens,
)
# remember which source file claims this trigger
self.trigger_to_sourcefile[trigger_str] = sourcefile
except ValueError as e:
print(f' | Ignoring incompatible embedding {embedding_info["name"]}')
print(f" | The error was {str(e)}")
def _add_textual_inversion(
self, trigger_str, embedding, defer_injecting_tokens=False
@ -123,7 +144,7 @@ class TextualInversionManager(BaseTextualInversionManager):
"""
if trigger_str in [ti.trigger_string for ti in self.textual_inversions]:
print(
f">> TextualInversionManager refusing to overwrite already-loaded token '{trigger_str}'"
f"** TextualInversionManager refusing to overwrite already-loaded token '{trigger_str}'"
)
return
if not self.full_precision:
@ -132,7 +153,7 @@ class TextualInversionManager(BaseTextualInversionManager):
embedding = embedding.unsqueeze(0)
elif len(embedding.shape) > 2:
raise ValueError(
f"TextualInversionManager cannot add {trigger_str} because the embedding shape {embedding.shape} is incorrect. The embedding must have shape [token_dim] or [V, token_dim] where V is vector length and token_dim is 768 for SD1 or 1280 for SD2."
f"** TextualInversionManager cannot add {trigger_str} because the embedding shape {embedding.shape} is incorrect. The embedding must have shape [token_dim] or [V, token_dim] where V is vector length and token_dim is 768 for SD1 or 1280 for SD2."
)
try:
@ -148,7 +169,7 @@ class TextualInversionManager(BaseTextualInversionManager):
else:
traceback.print_exc()
print(
f">> TextualInversionManager was unable to add a textual inversion with trigger string {trigger_str}."
f"** TextualInversionManager was unable to add a textual inversion with trigger string {trigger_str}."
)
raise
@ -295,7 +316,7 @@ class TextualInversionManager(BaseTextualInversionManager):
elif file_type == "bin":
return self._parse_embedding_bin(embedding_file)
else:
print(f">> Not a recognized embedding file: {embedding_file}")
print(f"** Notice: unrecognized embedding file format: {embedding_file}")
return None
def _parse_embedding_pt(self, embedding_file):
@ -356,8 +377,9 @@ class TextualInversionManager(BaseTextualInversionManager):
embedding_info = None
else:
for token in list(embedding_ckpt.keys()):
embedding_info["name"] = token or os.path.basename(
os.path.splitext(embedding_file)[0]
embedding_info["name"] = (
token
or f"<{os.path.basename(os.path.splitext(embedding_file)[0])}>"
)
embedding_info["embedding"] = embedding_ckpt[token]
embedding_info[
@ -381,7 +403,7 @@ class TextualInversionManager(BaseTextualInversionManager):
embedding_info["name"] = (
token
if token != "*"
else os.path.basename(os.path.splitext(embedding_file)[0])
else f"<{os.path.basename(os.path.splitext(embedding_file)[0])}>"
)
embedding_info["embedding"] = embedding_ckpt[
"string_to_param"