aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFampai <unknown>2022-10-08 14:28:22 -0400
committerAUTOMATIC1111 <16777216c@gmail.com>2022-10-08 22:10:37 +0300
commit1371d7608b402d6f15c200ec2f5fde4579836a05 (patch)
tree206e783f3b4e483180df025c9a2595d773ab1ee9
parentb458fa48fe5734a872bca83061d702609cb52940 (diff)
Added ability to ignore last n layers in FrozenCLIPEmbedder
-rw-r--r--modules/sd_hijack.py11
-rw-r--r--modules/shared.py1
2 files changed, 10 insertions, 2 deletions
diff --git a/modules/sd_hijack.py b/modules/sd_hijack.py
index 307cc67d..f12a9696 100644
--- a/modules/sd_hijack.py
+++ b/modules/sd_hijack.py
@@ -281,8 +281,15 @@ class FrozenCLIPEmbedderWithCustomWords(torch.nn.Module):
remade_batch_tokens_of_same_length = [x + [self.wrapped.tokenizer.eos_token_id] * (target_token_count - len(x)) for x in remade_batch_tokens]
tokens = torch.asarray(remade_batch_tokens_of_same_length).to(device)
- outputs = self.wrapped.transformer(input_ids=tokens, position_ids=position_ids)
- z = outputs.last_hidden_state
+
+ tmp = -opts.CLIP_ignore_last_layers
+ if (opts.CLIP_ignore_last_layers == 0):
+ outputs = self.wrapped.transformer(input_ids=tokens, position_ids=position_ids)
+ z = outputs.last_hidden_state
+ else:
+ outputs = self.wrapped.transformer(input_ids=tokens, position_ids=position_ids, output_hidden_states=tmp)
+ z = outputs.hidden_states[tmp]
+ z = self.wrapped.transformer.text_model.final_layer_norm(z)
# restoring original mean is likely not correct, but it seems to work well to prevent artifacts that happen otherwise
batch_multipliers_of_same_length = [x + [1.0] * (target_token_count - len(x)) for x in batch_multipliers]
diff --git a/modules/shared.py b/modules/shared.py
index 8f941226..af8dc744 100644
--- a/modules/shared.py
+++ b/modules/shared.py
@@ -225,6 +225,7 @@ options_templates.update(options_section(('sd', "Stable Diffusion"), {
"use_old_emphasis_implementation": OptionInfo(False, "Use old emphasis implementation. Can be useful to reproduce old seeds."),
"enable_batch_seeds": OptionInfo(True, "Make K-diffusion samplers produce same images in a batch as when making a single image"),
"filter_nsfw": OptionInfo(False, "Filter NSFW content"),
+ 'CLIP_ignore_last_layers': OptionInfo(0, "Ignore last layers of CLIP model", gr.Slider, {"minimum": 0, "maximum": 5, "step": 1}),
"random_artist_categories": OptionInfo([], "Allowed categories for random artists selection when using the Roll button", gr.CheckboxGroup, {"choices": artist_db.categories()}),
}))