Revert "Revert "Load clip models from the SDXL checkpoint""
This reverts commit 0891b2db7f.
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
{
|
||||
"_name_or_path": "openai/clip-vit-large-patch14",
|
||||
"architectures": [
|
||||
"CLIPTextModel"
|
||||
],
|
||||
"attention_dropout": 0.0,
|
||||
"bos_token_id": 0,
|
||||
"dropout": 0.0,
|
||||
"eos_token_id": 2,
|
||||
"hidden_act": "quick_gelu",
|
||||
"hidden_size": 768,
|
||||
"initializer_factor": 1.0,
|
||||
"initializer_range": 0.02,
|
||||
"intermediate_size": 3072,
|
||||
"layer_norm_eps": 1e-05,
|
||||
"max_position_embeddings": 77,
|
||||
"model_type": "clip_text_model",
|
||||
"num_attention_heads": 12,
|
||||
"num_hidden_layers": 12,
|
||||
"pad_token_id": 1,
|
||||
"projection_dim": 768,
|
||||
"torch_dtype": "float32",
|
||||
"transformers_version": "4.22.0.dev0",
|
||||
"vocab_size": 49408
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
{
|
||||
"_name_or_path": "clip-vit-large-patch14/",
|
||||
"architectures": [
|
||||
"CLIPModel"
|
||||
],
|
||||
"initializer_factor": 1.0,
|
||||
"logit_scale_init_value": 2.6592,
|
||||
"model_type": "clip",
|
||||
"projection_dim": 768,
|
||||
"text_config": {
|
||||
"_name_or_path": "",
|
||||
"add_cross_attention": false,
|
||||
"architectures": null,
|
||||
"attention_dropout": 0.0,
|
||||
"bad_words_ids": null,
|
||||
"bos_token_id": 0,
|
||||
"chunk_size_feed_forward": 0,
|
||||
"cross_attention_hidden_size": null,
|
||||
"decoder_start_token_id": null,
|
||||
"diversity_penalty": 0.0,
|
||||
"do_sample": false,
|
||||
"dropout": 0.0,
|
||||
"early_stopping": false,
|
||||
"encoder_no_repeat_ngram_size": 0,
|
||||
"eos_token_id": 2,
|
||||
"finetuning_task": null,
|
||||
"forced_bos_token_id": null,
|
||||
"forced_eos_token_id": null,
|
||||
"hidden_act": "quick_gelu",
|
||||
"hidden_size": 768,
|
||||
"id2label": {
|
||||
"0": "LABEL_0",
|
||||
"1": "LABEL_1"
|
||||
},
|
||||
"initializer_factor": 1.0,
|
||||
"initializer_range": 0.02,
|
||||
"intermediate_size": 3072,
|
||||
"is_decoder": false,
|
||||
"is_encoder_decoder": false,
|
||||
"label2id": {
|
||||
"LABEL_0": 0,
|
||||
"LABEL_1": 1
|
||||
},
|
||||
"layer_norm_eps": 1e-05,
|
||||
"length_penalty": 1.0,
|
||||
"max_length": 20,
|
||||
"max_position_embeddings": 77,
|
||||
"min_length": 0,
|
||||
"model_type": "clip_text_model",
|
||||
"no_repeat_ngram_size": 0,
|
||||
"num_attention_heads": 12,
|
||||
"num_beam_groups": 1,
|
||||
"num_beams": 1,
|
||||
"num_hidden_layers": 12,
|
||||
"num_return_sequences": 1,
|
||||
"output_attentions": false,
|
||||
"output_hidden_states": false,
|
||||
"output_scores": false,
|
||||
"pad_token_id": 1,
|
||||
"prefix": null,
|
||||
"problem_type": null,
|
||||
"projection_dim" : 768,
|
||||
"pruned_heads": {},
|
||||
"remove_invalid_values": false,
|
||||
"repetition_penalty": 1.0,
|
||||
"return_dict": true,
|
||||
"return_dict_in_generate": false,
|
||||
"sep_token_id": null,
|
||||
"task_specific_params": null,
|
||||
"temperature": 1.0,
|
||||
"tie_encoder_decoder": false,
|
||||
"tie_word_embeddings": true,
|
||||
"tokenizer_class": null,
|
||||
"top_k": 50,
|
||||
"top_p": 1.0,
|
||||
"torch_dtype": null,
|
||||
"torchscript": false,
|
||||
"transformers_version": "4.16.0.dev0",
|
||||
"use_bfloat16": false,
|
||||
"vocab_size": 49408
|
||||
},
|
||||
"text_config_dict": {
|
||||
"hidden_size": 768,
|
||||
"intermediate_size": 3072,
|
||||
"num_attention_heads": 12,
|
||||
"num_hidden_layers": 12,
|
||||
"projection_dim": 768
|
||||
},
|
||||
"torch_dtype": "float32",
|
||||
"transformers_version": null,
|
||||
"vision_config": {
|
||||
"_name_or_path": "",
|
||||
"add_cross_attention": false,
|
||||
"architectures": null,
|
||||
"attention_dropout": 0.0,
|
||||
"bad_words_ids": null,
|
||||
"bos_token_id": null,
|
||||
"chunk_size_feed_forward": 0,
|
||||
"cross_attention_hidden_size": null,
|
||||
"decoder_start_token_id": null,
|
||||
"diversity_penalty": 0.0,
|
||||
"do_sample": false,
|
||||
"dropout": 0.0,
|
||||
"early_stopping": false,
|
||||
"encoder_no_repeat_ngram_size": 0,
|
||||
"eos_token_id": null,
|
||||
"finetuning_task": null,
|
||||
"forced_bos_token_id": null,
|
||||
"forced_eos_token_id": null,
|
||||
"hidden_act": "quick_gelu",
|
||||
"hidden_size": 1024,
|
||||
"id2label": {
|
||||
"0": "LABEL_0",
|
||||
"1": "LABEL_1"
|
||||
},
|
||||
"image_size": 224,
|
||||
"initializer_factor": 1.0,
|
||||
"initializer_range": 0.02,
|
||||
"intermediate_size": 4096,
|
||||
"is_decoder": false,
|
||||
"is_encoder_decoder": false,
|
||||
"label2id": {
|
||||
"LABEL_0": 0,
|
||||
"LABEL_1": 1
|
||||
},
|
||||
"layer_norm_eps": 1e-05,
|
||||
"length_penalty": 1.0,
|
||||
"max_length": 20,
|
||||
"min_length": 0,
|
||||
"model_type": "clip_vision_model",
|
||||
"no_repeat_ngram_size": 0,
|
||||
"num_attention_heads": 16,
|
||||
"num_beam_groups": 1,
|
||||
"num_beams": 1,
|
||||
"num_hidden_layers": 24,
|
||||
"num_return_sequences": 1,
|
||||
"output_attentions": false,
|
||||
"output_hidden_states": false,
|
||||
"output_scores": false,
|
||||
"pad_token_id": null,
|
||||
"patch_size": 14,
|
||||
"prefix": null,
|
||||
"problem_type": null,
|
||||
"projection_dim" : 768,
|
||||
"pruned_heads": {},
|
||||
"remove_invalid_values": false,
|
||||
"repetition_penalty": 1.0,
|
||||
"return_dict": true,
|
||||
"return_dict_in_generate": false,
|
||||
"sep_token_id": null,
|
||||
"task_specific_params": null,
|
||||
"temperature": 1.0,
|
||||
"tie_encoder_decoder": false,
|
||||
"tie_word_embeddings": true,
|
||||
"tokenizer_class": null,
|
||||
"top_k": 50,
|
||||
"top_p": 1.0,
|
||||
"torch_dtype": null,
|
||||
"torchscript": false,
|
||||
"transformers_version": "4.16.0.dev0",
|
||||
"use_bfloat16": false
|
||||
},
|
||||
"vision_config_dict": {
|
||||
"hidden_size": 1024,
|
||||
"intermediate_size": 4096,
|
||||
"num_attention_heads": 16,
|
||||
"num_hidden_layers": 24,
|
||||
"patch_size": 14,
|
||||
"projection_dim": 768
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"crop_size": 224,
|
||||
"do_center_crop": true,
|
||||
"do_normalize": true,
|
||||
"do_resize": true,
|
||||
"feature_extractor_type": "CLIPFeatureExtractor",
|
||||
"image_mean": [
|
||||
0.48145466,
|
||||
0.4578275,
|
||||
0.40821073
|
||||
],
|
||||
"image_std": [
|
||||
0.26862954,
|
||||
0.26130258,
|
||||
0.27577711
|
||||
],
|
||||
"resample": 3,
|
||||
"size": 224
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
{"bos_token": {"content": "<|startoftext|>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "eos_token": {"content": "<|endoftext|>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "unk_token": {"content": "<|endoftext|>", "single_word": false, "lstrip": false, "rstrip": false, "normalized": true}, "pad_token": "<|endoftext|>"}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"unk_token": {
|
||||
"content": "<|endoftext|>",
|
||||
"single_word": false,
|
||||
"lstrip": false,
|
||||
"rstrip": false,
|
||||
"normalized": true,
|
||||
"__type": "AddedToken"
|
||||
},
|
||||
"bos_token": {
|
||||
"content": "<|startoftext|>",
|
||||
"single_word": false,
|
||||
"lstrip": false,
|
||||
"rstrip": false,
|
||||
"normalized": true,
|
||||
"__type": "AddedToken"
|
||||
},
|
||||
"eos_token": {
|
||||
"content": "<|endoftext|>",
|
||||
"single_word": false,
|
||||
"lstrip": false,
|
||||
"rstrip": false,
|
||||
"normalized": true,
|
||||
"__type": "AddedToken"
|
||||
},
|
||||
"pad_token": "<|endoftext|>",
|
||||
"add_prefix_space": false,
|
||||
"errors": "replace",
|
||||
"do_lower_case": true,
|
||||
"name_or_path": "openai/clip-vit-base-patch32",
|
||||
"model_max_length": 77,
|
||||
"special_tokens_map_file": "./special_tokens_map.json",
|
||||
"tokenizer_class": "CLIPTokenizer"
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
@@ -12,6 +12,13 @@ import torch.cuda
|
||||
from .sgm.util import instantiate_from_config
|
||||
from .SUPIR.util import convert_dtype, load_state_dict
|
||||
|
||||
from open_clip import CLIP, CLIPTextCfg
|
||||
from transformers import (
|
||||
CLIPTextModel,
|
||||
CLIPTokenizer,
|
||||
CLIPTextConfig,
|
||||
|
||||
)
|
||||
script_directory = os.path.dirname(os.path.abspath(__file__))
|
||||
|
||||
try:
|
||||
@@ -22,6 +29,41 @@ try:
|
||||
except:
|
||||
XFORMERS_IS_AVAILABLE = False
|
||||
|
||||
|
||||
def build_text_model_from_openai_state_dict(
|
||||
state_dict: dict,
|
||||
cast_dtype=torch.float16,
|
||||
):
|
||||
|
||||
embed_dim = state_dict["text_projection"].shape[1]
|
||||
context_length = state_dict["positional_embedding"].shape[0]
|
||||
vocab_size = state_dict["token_embedding.weight"].shape[0]
|
||||
transformer_width = state_dict["ln_final.weight"].shape[0]
|
||||
transformer_heads = transformer_width // 64
|
||||
transformer_layers = len(set(k.split(".")[2] for k in state_dict if k.startswith(f"transformer.resblocks")))
|
||||
|
||||
vision_cfg = None
|
||||
text_cfg = CLIPTextCfg(
|
||||
context_length=context_length,
|
||||
vocab_size=vocab_size,
|
||||
width=transformer_width,
|
||||
heads=transformer_heads,
|
||||
layers=transformer_layers,
|
||||
)
|
||||
model = CLIP(
|
||||
embed_dim,
|
||||
vision_cfg=vision_cfg,
|
||||
text_cfg=text_cfg,
|
||||
quick_gelu=True, # OpenAI models were trained with QuickGELU
|
||||
cast_dtype=cast_dtype,
|
||||
)
|
||||
|
||||
model.load_state_dict(state_dict, strict=False)
|
||||
model = model.eval()
|
||||
for param in model.parameters():
|
||||
param.requires_grad = False
|
||||
return model
|
||||
|
||||
class SUPIR_Upscale:
|
||||
upscale_methods = ["nearest-exact", "bilinear", "area", "bicubic", "lanczos"]
|
||||
|
||||
@@ -94,7 +136,6 @@ class SUPIR_Upscale:
|
||||
control_scale, cfg_scale_start, control_scale_start, restoration_scale, keep_model_loaded,
|
||||
a_prompt, n_prompt, sdxl_model, supir_model, use_tiled_vae, use_tiled_sampling=False, sampler_tile_size=128, sampler_tile_stride=64, captions="", diffusion_dtype="auto",
|
||||
encoder_dtype="auto", batch_size=1):
|
||||
|
||||
device = mm.get_torch_device()
|
||||
mm.unload_all_models()
|
||||
|
||||
@@ -103,6 +144,8 @@ class SUPIR_Upscale:
|
||||
|
||||
config_path = os.path.join(script_directory, "options/SUPIR_v0.yaml")
|
||||
config_path_tiled = os.path.join(script_directory, "options/SUPIR_v0_tiled.yaml")
|
||||
clip_config_path = os.path.join(script_directory, "configs/clip_vit_config.json")
|
||||
tokenizer_path = os.path.join(script_directory, "configs/tokenizer")
|
||||
|
||||
custom_config = {
|
||||
'sdxl_model': sdxl_model,
|
||||
@@ -172,9 +215,11 @@ class SUPIR_Upscale:
|
||||
config.model.params.diffusion_dtype = model_dtype
|
||||
|
||||
self.model = instantiate_from_config(config.model).cpu()
|
||||
|
||||
try:
|
||||
print(f'Attempting to load SUPIR model: [{SUPIR_MODEL_PATH}]')
|
||||
supir_state_dict = load_state_dict(SUPIR_MODEL_PATH)
|
||||
|
||||
except:
|
||||
raise Exception("Failed to load SUPIR model")
|
||||
try:
|
||||
@@ -185,11 +230,43 @@ class SUPIR_Upscale:
|
||||
self.model.load_state_dict(supir_state_dict, strict=False)
|
||||
self.model.load_state_dict(sdxl_state_dict, strict=False)
|
||||
|
||||
del supir_state_dict, sdxl_state_dict
|
||||
del supir_state_dict
|
||||
|
||||
#first clip model from SDXL checkpoint
|
||||
try:
|
||||
print("Loading first clip model from SDXL checkpoint")
|
||||
|
||||
replace_prefix = {}
|
||||
replace_prefix["conditioner.embedders.0.transformer."] = ""
|
||||
|
||||
sd = comfy.utils.state_dict_prefix_replace(sdxl_state_dict, replace_prefix, filter_keys=False)
|
||||
clip_text_config = CLIPTextConfig.from_pretrained(clip_config_path)
|
||||
self.model.conditioner.embedders[0].tokenizer = CLIPTokenizer.from_pretrained(tokenizer_path)
|
||||
self.model.conditioner.embedders[0].transformer = CLIPTextModel(clip_text_config)
|
||||
self.model.conditioner.embedders[0].transformer.load_state_dict(sd, strict=False)
|
||||
self.model.conditioner.embedders[0].eval()
|
||||
for param in self.model.conditioner.embedders[0].parameters():
|
||||
param.requires_grad = False
|
||||
except:
|
||||
raise Exception("Failed to load first clip model from SDXL checkpoint")
|
||||
|
||||
del sdxl_state_dict
|
||||
|
||||
#second clip model from SDXL checkpoint
|
||||
try:
|
||||
print("Loading second clip model from SDXL checkpoint")
|
||||
replace_prefix2 = {}
|
||||
replace_prefix2["conditioner.embedders.1.model."] = ""
|
||||
sd = comfy.utils.state_dict_prefix_replace(sd, replace_prefix2, filter_keys=True)
|
||||
clip_g = build_text_model_from_openai_state_dict(sd, cast_dtype=dtype)
|
||||
self.model.conditioner.embedders[1].model = clip_g
|
||||
except:
|
||||
raise Exception("Failed to load second clip model from SDXL checkpoint")
|
||||
|
||||
del sd, clip_g
|
||||
mm.soft_empty_cache()
|
||||
|
||||
try:
|
||||
# to dtype first then to device to reduce memory usage
|
||||
self.model.to(dtype)
|
||||
self.model.to(device)
|
||||
except Exception as e:
|
||||
@@ -209,9 +286,9 @@ class SUPIR_Upscale:
|
||||
B, H, W, C = image.shape
|
||||
new_height = H // 64 * 64
|
||||
new_width = W // 64 * 64
|
||||
image = image.permute(0, 3, 1, 2).contiguous().to(device)
|
||||
image = image.permute(0, 3, 1, 2).contiguous()
|
||||
resized_image = F.interpolate(image, size=(new_height, new_width), mode='bicubic', align_corners=False)
|
||||
|
||||
resized_image = resized_image.to(device)
|
||||
captions_list = []
|
||||
captions_list.append(captions)
|
||||
print("captions: ", captions_list)
|
||||
@@ -260,11 +337,11 @@ class SUPIR_Upscale:
|
||||
out_stacked = torch.cat(out, dim=0).cpu().to(torch.float32).permute(0, 2, 3, 1)
|
||||
else:
|
||||
out_stacked = torch.stack(out, dim=0).cpu().to(torch.float32).permute(0, 2, 3, 1)
|
||||
|
||||
final_image, = ImageScale.upscale(self, out_stacked, "lanczos", W, H, crop="disabled")
|
||||
|
||||
return (final_image,)
|
||||
|
||||
|
||||
NODE_CLASS_MAPPINGS = {
|
||||
"SUPIR_Upscale": SUPIR_Upscale
|
||||
}
|
||||
|
||||
@@ -461,12 +461,17 @@ class FrozenCLIPEmbedder(AbstractEmbModel):
|
||||
): # clip-vit-base-patch32
|
||||
super().__init__()
|
||||
assert layer in self.LAYERS
|
||||
self.tokenizer = CLIPTokenizer.from_pretrained(version if SDXL_CLIP1_PATH is None else SDXL_CLIP1_PATH)
|
||||
self.transformer = CLIPTextModel.from_pretrained(version if SDXL_CLIP1_PATH is None else SDXL_CLIP1_PATH)
|
||||
#self.tokenizer = CLIPTokenizer.from_pretrained(version if SDXL_CLIP1_PATH is None else SDXL_CLIP1_PATH)
|
||||
#self.transformer = CLIPTextModel.from_pretrained(version if SDXL_CLIP1_PATH is None else SDXL_CLIP1_PATH)
|
||||
#self.clip_text_config = CLIPTextConfig.from_pretrained("openai/clip-vit-large-patch14")
|
||||
#self.tokenizer = CLIPTokenizer.from_pretrained(version)
|
||||
self.tokenizer = None
|
||||
#self.transformer = CLIPTextModel(self.clip_text_config)
|
||||
self.transformer = None
|
||||
self.device = device
|
||||
self.max_length = max_length
|
||||
if freeze:
|
||||
self.freeze()
|
||||
#if freeze:
|
||||
# self.freeze()
|
||||
self.layer = layer
|
||||
self.layer_idx = layer_idx
|
||||
self.return_pooled = always_return_pooled
|
||||
@@ -529,19 +534,20 @@ class FrozenOpenCLIPEmbedder2(AbstractEmbModel):
|
||||
):
|
||||
super().__init__()
|
||||
assert layer in self.LAYERS
|
||||
model, _, _ = open_clip.create_model_and_transforms(
|
||||
arch,
|
||||
device=torch.device("cpu"),
|
||||
pretrained=version if SDXL_CLIP2_CKPT_PTH is None else SDXL_CLIP2_CKPT_PTH,
|
||||
)
|
||||
del model.visual
|
||||
self.model = model
|
||||
# model, _, _ = open_clip.create_model_and_transforms(
|
||||
# arch,
|
||||
# device=torch.device("cpu"),
|
||||
# pretrained=version if SDXL_CLIP2_CKPT_PTH is None else SDXL_CLIP2_CKPT_PTH,
|
||||
# )
|
||||
|
||||
# del model.visual
|
||||
# self.model = model
|
||||
self.model = None
|
||||
self.device = device
|
||||
self.max_length = max_length
|
||||
self.return_pooled = always_return_pooled
|
||||
if freeze:
|
||||
self.freeze()
|
||||
#if freeze:
|
||||
# self.freeze()
|
||||
self.layer = layer
|
||||
if self.layer == "last":
|
||||
self.layer_idx = 0
|
||||
|
||||
Reference in New Issue
Block a user