Upload folder using huggingface_hub
Browse filesFiles changed (7)
- text-encoder/config.jsonAdded+31
- text-encoder/model.safetensorsAdded+3
- text-encoder/utils.pyAdded+372
- utils.pyAdded+372
- vision-encoder/config.jsonAdded+16
- vision-encoder/model.safetensorsAdded+3
- vision-encoder/utils.pyAdded+372
12345678910111213141516171819202122232425262728293031{ "architectures": [ "CLIPTextEncoderOnly" ], "attention_dropout": 0.0, "auto_map": { "AutoConfig": "utils.CLIPTextEncoderOnlyConfig", "AutoModel": "utils.CLIPTextEncoderOnly" }, "bos_token_id": 49406, "eos_token_id": 49407, "frozen": false, "hidden_act": "quick_gelu", "hidden_size": 512, "initializer_factor": 1.0, "initializer_range": 0.02, "intermediate_size": 2048, "layer_norm_eps": 1e-05, "lora": null, "max_position_embeddings": 77, "model_name": "openai/clip-vit-base-patch32", "model_type": "clip_custom_text_model", "num_attention_heads": 8, "num_hidden_layers": 12, "pad_token_id": 1, "pretrained": false, "projection_dim": 512, "torch_dtype": "float32", "transformers_version": "4.40.1", "vocab_size": 49408}123version https://git-lfs.github.com/spec/v1oid sha256:ff4551a48d4740de63ad639eb6c5a86173e88f386b55ab2c1877a394cccc0329size 253736912123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372from transformers import AutoConfig, AutoModel, PretrainedConfig, CLIPTextConfig, CLIPVisionConfig, PreTrainedModel, CLIPTextModelWithProjection, CLIPVisionModelWithProjectionfrom transformers.utils import ModelOutputimport torchimport open_clipfrom dataclasses import dataclassimport safetensors.torchfrom peft import get_peft_config, get_peft_model, LoraConfig, TaskTypeimport os
HF_SAFE_WEIGHTS_NAME = "open_clip_model.safetensors"HF_SAFE_WEIGHTS_NAME_PRIOR = "prior_model.safetensors"
@dataclassclass PriorTransformerOutput(ModelOutput): """ The output of [`PriorTransformer`].
Args: predicted_image_embedding (`torch.FloatTensor` of shape `(batch_size, embedding_dim)`): The predicted CLIP image embedding conditioned on the CLIP text embedding input. """
predicted_image_embedding: torch.FloatTensor
@dataclassclass TextEncoderOutput(ModelOutput): """ Output class for CLIPTextEncoderOnly model to store the outputs in a Hugging Face transformer style.
Attributes: prompt_embeds (torch.Tensor): The embeddings of the input prompts. last_hidden_states (torch.Tensor): The last hidden states from the model. """ text_embeds: torch.FloatTensor = None last_hidden_state: torch.FloatTensor = None
class CLIPTextEncoderOnlyConfig(CLIPTextConfig): model_type = "clip_custom_text_model"
def __init__(self, model_name: str = None, pretrained: bool = True, frozen: bool = False, lora: dict = None, **kwargs): self.model_name = model_name self.pretrained = pretrained self.frozen = frozen self.lora = lora super().__init__(**kwargs)
class CLIPTextEncoderOnly(PreTrainedModel): config_class = CLIPTextEncoderOnlyConfig
def __init__(self, config): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__(config) if config.pretrained: self.model = CLIPTextModelWithProjection.from_pretrained(config.model_name) else: base_cfg = CLIPTextConfig.from_pretrained(config.model_name) self.model = CLIPTextModelWithProjection(base_cfg)
if config.lora: l_config = LoraConfig( r=config.lora.lora_r, lora_alpha=config.lora.lora_alpha, target_modules=[ "k_proj", "v_proj", "q_proj", "out_proj", "fc1", "fc2", "visual_projection", "text_projection" ], lora_dropout=config.lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, input_ids, attention_mask=None, position_ids=None): """ Forward pass of the model.
:param input_ids: Indices of input sequence tokens in the vocabulary. :param attention_mask: Mask to avoid performing attention on padding token indices. :param token_type_ids: Segment token indices to indicate first and second portions of the inputs. :return: Outputs of the model. """ outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, output_hidden_states=True) return TextEncoderOutput(text_embeds=outputs.text_embeds, last_hidden_state=outputs.last_hidden_state)
class CustomTextEncoderOnlyConfig(PretrainedConfig): model_type = "whole_custom_text_model"
def __init__(self, model_name: str = None, pretrained: bool = True, frozen: bool = False, output_hidden_size: int = 512, last_hidden_state: bool = False, lora: dict = None, **kwargs): self.model_name = model_name self.pretrained = pretrained self.frozen = frozen self.output_hidden_size = output_hidden_size self.last_hidden_state = last_hidden_state self.lora = lora super().__init__(**kwargs)
class CustomTextEncoderOnly(PreTrainedModel): config_class = CustomTextEncoderOnlyConfig
def __init__(self, config): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__(config)
self.last_hidden_state = config.last_hidden_state
if config.pretrained: self.model = AutoModel.from_pretrained(config.model_name) if config.frozen: for param in self.model.parameters(): param.requires_grad = False else: self.model = AutoModel(config)
self.fc1 = torch.nn.Linear(self.model.config.hidden_size, config.output_hidden_size) if config.last_hidden_state: self.fc2 = torch.nn.Linear(self.model.config.hidden_size, config.output_hidden_size)
if config.lora: l_config = LoraConfig( task_type=TaskType.FEATURE_EXTRACTION, r=config.lora.lora_r, lora_alpha=config.lora.lora_alpha, lora_dropout=config.lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, input_ids, attention_mask=None, token_type_ids=None): """ Forward pass of the model.
:param input_ids: Indices of input sequence tokens in the vocabulary. :param attention_mask: Mask to avoid performing attention on padding token indices. :param token_type_ids: Segment token indices to indicate first and second portions of the inputs. :return: Outputs of the model. """ outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, output_hidden_states=True) text_embeds = self.fc1(outputs[1]) last_hidden_state = None if self.last_hidden_state: last_hidden_state = self.fc2(outputs[0]) else: last_hidden_state = outputs[0] return TextEncoderOutput(text_embeds=text_embeds, last_hidden_state=last_hidden_state)
class CLIPVisionEncoderOnlyConfig(PretrainedConfig): model_type = "clip_custom_vision_model"
def __init__(self, model_name: str = None, pretrained: bool = True, frozen: bool = False, lora: dict = None, **kwargs): self.model_name = model_name self.pretrained = pretrained self.frozen = frozen self.lora = lora super().__init__(**kwargs)
class CLIPVisionEncoderOnly(PreTrainedModel): config_class = CLIPVisionEncoderOnlyConfig
def __init__(self, config): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__(config) if config.pretrained: self.model = CLIPVisionModelWithProjection.from_pretrained(config.model_name) else: base_cfg = CLIPVisionConfig.from_pretrained(config.model_name) self.model = CLIPVisionModelWithProjection(base_cfg)
if config.lora: l_config = LoraConfig( r=config.lora.lora_r, lora_alpha=config.lora.lora_alpha, target_modules=[ "k_proj", "v_proj", "q_proj", "out_proj", "fc1", "fc2", "visual_projection", "text_projection" ], lora_dropout=config.lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, data): """ Forward pass of the model. """ return self.model(**data).image_embeds def parameters(self): return self.model.parameters()
class OpenCLIPVisionEncoderOnly(torch.nn.Module): def __init__(self, model_name: str, pretrained: bool = True, frozen: bool = False, lora: dict = None): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__() if pretrained: model, _ = open_clip.create_model_from_pretrained(f"hf-hub:{model_name}") model = model.visual else: raise NotImplemented self.model = model
if lora: l_config = LoraConfig( r=lora.lora_r, lora_alpha=lora.lora_alpha, target_modules=[ "k_proj", "v_proj", "q_proj", "out_proj", "fc1", "fc2", "visual_projection", "text_projection" ], lora_dropout=lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, image): """ Forward pass of the model. """ return self.model(image) def save_pretrained(self, save_dir): tensors = self.model.state_dict() safetensors.torch.save_file(tensors, save_dir / HF_SAFE_WEIGHTS_NAME)
class CustomPriorModel(torch.nn.Module): def __init__(self, in_hidden_state, out_hidden_state): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__() mid_hidden_state = max(in_hidden_state, out_hidden_state)
self.fc1 = torch.nn.Linear(in_hidden_state*2, mid_hidden_state) self.relu = torch.nn.ReLU() self.fc2 = torch.nn.Linear(mid_hidden_state, out_hidden_state) def reinitialize_model(self): for name, param in self.named_parameters(): if param.requires_grad: if len(param.shape) > 1: torch.nn.init.xavier_uniform_(param) else: if 'weight' in name: torch.nn.init.normal_(param) else: torch.nn.init.zeros_(param)
def forward(self, feats): """ Forward pass of the model. """ return PriorTransformerOutput(predicted_image_embedding=self.fc2(self.relu(self.fc1(feats)))) def save_pretrained(self, save_dir): pass # tensors = self.state_dict() # safetensors.torch.save_file(tensors, os.path.join(save_dir, HF_SAFE_WEIGHTS_NAME_PRIOR))
def test_text_model(register=False, upload=False): # register the classes if register: AutoConfig.register("clip_custom_text_model", CLIPTextEncoderOnlyConfig) AutoModel.register(CLIPTextEncoderOnlyConfig, CLIPTextEncoderOnly) CLIPTextEncoderOnlyConfig.register_for_auto_class() CLIPTextEncoderOnly.register_for_auto_class("AutoModel")
if upload: # Initialize the model model_name = "openai/clip-vit-base-patch32" pretrained=True lora=None
cfg = CLIPTextEncoderOnlyConfig(model_name=model_name, pretrained=pretrained, lora=lora) model = CLIPTextEncoderOnly(cfg) model.push_to_hub("test-text-hf-upload")
model = CLIPTextEncoderOnly.from_pretrained("mpatel57/test-text-hf-upload", force_download=True)
def test_custom_text_model(register=False, upload=False): # register the classes if register: AutoConfig.register("whole_custom_text_model", CustomTextEncoderOnlyConfig) AutoModel.register(CustomTextEncoderOnlyConfig, CustomTextEncoderOnly) CustomTextEncoderOnlyConfig.register_for_auto_class() CustomTextEncoderOnly.register_for_auto_class("AutoModel")
if upload: # Initialize the model model_name = "google-bert/bert-base-uncased" pretrained=True frozen=False output_hidden_size=512 last_hidden_state=False
lora=None
cfg = CustomTextEncoderOnlyConfig(model_name=model_name, pretrained=pretrained, frozen=frozen, output_hidden_size=output_hidden_size, last_hidden_state=last_hidden_state, lora=lora) model = CustomTextEncoderOnly(cfg) model.push_to_hub("test-text-hf-upload")
model = CustomTextEncoderOnly.from_pretrained("mpatel57/test-text-hf-upload", force_download=True)
def test_vision_model(register=False, upload=False): # register the classes if register: AutoConfig.register("clip_custom_vision_model", CLIPVisionEncoderOnlyConfig) AutoModel.register(CLIPVisionEncoderOnlyConfig, CLIPVisionEncoderOnly) CLIPVisionEncoderOnlyConfig.register_for_auto_class() CLIPVisionEncoderOnly.register_for_auto_class("AutoModel")
if upload: # Initialize the model model_name = "openai/clip-vit-base-patch32" pretrained=True lora=None
cfg = CLIPVisionEncoderOnlyConfig(model_name=model_name, pretrained=pretrained, lora=lora) model = CLIPVisionEncoderOnly(cfg) model.push_to_hub("test-vision-hf-upload")
model = CLIPVisionEncoderOnly.from_pretrained("mpatel57/test-vision-hf-upload", force_download=True)
if __name__ == "__main__": test_custom_text_model(register=False, upload=True) # test_text_model(register=False, upload=True) # test_vision_model(register=False, upload=True)123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372from transformers import AutoConfig, AutoModel, PretrainedConfig, CLIPTextConfig, CLIPVisionConfig, PreTrainedModel, CLIPTextModelWithProjection, CLIPVisionModelWithProjectionfrom transformers.utils import ModelOutputimport torchimport open_clipfrom dataclasses import dataclassimport safetensors.torchfrom peft import get_peft_config, get_peft_model, LoraConfig, TaskTypeimport os
HF_SAFE_WEIGHTS_NAME = "open_clip_model.safetensors"HF_SAFE_WEIGHTS_NAME_PRIOR = "prior_model.safetensors"
@dataclassclass PriorTransformerOutput(ModelOutput): """ The output of [`PriorTransformer`].
Args: predicted_image_embedding (`torch.FloatTensor` of shape `(batch_size, embedding_dim)`): The predicted CLIP image embedding conditioned on the CLIP text embedding input. """
predicted_image_embedding: torch.FloatTensor
@dataclassclass TextEncoderOutput(ModelOutput): """ Output class for CLIPTextEncoderOnly model to store the outputs in a Hugging Face transformer style.
Attributes: prompt_embeds (torch.Tensor): The embeddings of the input prompts. last_hidden_states (torch.Tensor): The last hidden states from the model. """ text_embeds: torch.FloatTensor = None last_hidden_state: torch.FloatTensor = None
class CLIPTextEncoderOnlyConfig(CLIPTextConfig): model_type = "clip_custom_text_model"
def __init__(self, model_name: str = None, pretrained: bool = True, frozen: bool = False, lora: dict = None, **kwargs): self.model_name = model_name self.pretrained = pretrained self.frozen = frozen self.lora = lora super().__init__(**kwargs)
class CLIPTextEncoderOnly(PreTrainedModel): config_class = CLIPTextEncoderOnlyConfig
def __init__(self, config): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__(config) if config.pretrained: self.model = CLIPTextModelWithProjection.from_pretrained(config.model_name) else: base_cfg = CLIPTextConfig.from_pretrained(config.model_name) self.model = CLIPTextModelWithProjection(base_cfg)
if config.lora: l_config = LoraConfig( r=config.lora.lora_r, lora_alpha=config.lora.lora_alpha, target_modules=[ "k_proj", "v_proj", "q_proj", "out_proj", "fc1", "fc2", "visual_projection", "text_projection" ], lora_dropout=config.lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, input_ids, attention_mask=None, position_ids=None): """ Forward pass of the model.
:param input_ids: Indices of input sequence tokens in the vocabulary. :param attention_mask: Mask to avoid performing attention on padding token indices. :param token_type_ids: Segment token indices to indicate first and second portions of the inputs. :return: Outputs of the model. """ outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, output_hidden_states=True) return TextEncoderOutput(text_embeds=outputs.text_embeds, last_hidden_state=outputs.last_hidden_state)
class CustomTextEncoderOnlyConfig(PretrainedConfig): model_type = "whole_custom_text_model"
def __init__(self, model_name: str = None, pretrained: bool = True, frozen: bool = False, output_hidden_size: int = 512, last_hidden_state: bool = False, lora: dict = None, **kwargs): self.model_name = model_name self.pretrained = pretrained self.frozen = frozen self.output_hidden_size = output_hidden_size self.last_hidden_state = last_hidden_state self.lora = lora super().__init__(**kwargs)
class CustomTextEncoderOnly(PreTrainedModel): config_class = CustomTextEncoderOnlyConfig
def __init__(self, config): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__(config)
self.last_hidden_state = config.last_hidden_state
if config.pretrained: self.model = AutoModel.from_pretrained(config.model_name) if config.frozen: for param in self.model.parameters(): param.requires_grad = False else: self.model = AutoModel(config)
self.fc1 = torch.nn.Linear(self.model.config.hidden_size, config.output_hidden_size) if config.last_hidden_state: self.fc2 = torch.nn.Linear(self.model.config.hidden_size, config.output_hidden_size)
if config.lora: l_config = LoraConfig( task_type=TaskType.FEATURE_EXTRACTION, r=config.lora.lora_r, lora_alpha=config.lora.lora_alpha, lora_dropout=config.lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, input_ids, attention_mask=None, token_type_ids=None): """ Forward pass of the model.
:param input_ids: Indices of input sequence tokens in the vocabulary. :param attention_mask: Mask to avoid performing attention on padding token indices. :param token_type_ids: Segment token indices to indicate first and second portions of the inputs. :return: Outputs of the model. """ outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, output_hidden_states=True) text_embeds = self.fc1(outputs[1]) last_hidden_state = None if self.last_hidden_state: last_hidden_state = self.fc2(outputs[0]) else: last_hidden_state = outputs[0] return TextEncoderOutput(text_embeds=text_embeds, last_hidden_state=last_hidden_state)
class CLIPVisionEncoderOnlyConfig(PretrainedConfig): model_type = "clip_custom_vision_model"
def __init__(self, model_name: str = None, pretrained: bool = True, frozen: bool = False, lora: dict = None, **kwargs): self.model_name = model_name self.pretrained = pretrained self.frozen = frozen self.lora = lora super().__init__(**kwargs)
class CLIPVisionEncoderOnly(PreTrainedModel): config_class = CLIPVisionEncoderOnlyConfig
def __init__(self, config): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__(config) if config.pretrained: self.model = CLIPVisionModelWithProjection.from_pretrained(config.model_name) else: base_cfg = CLIPVisionConfig.from_pretrained(config.model_name) self.model = CLIPVisionModelWithProjection(base_cfg)
if config.lora: l_config = LoraConfig( r=config.lora.lora_r, lora_alpha=config.lora.lora_alpha, target_modules=[ "k_proj", "v_proj", "q_proj", "out_proj", "fc1", "fc2", "visual_projection", "text_projection" ], lora_dropout=config.lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, data): """ Forward pass of the model. """ return self.model(**data).image_embeds def parameters(self): return self.model.parameters()
class OpenCLIPVisionEncoderOnly(torch.nn.Module): def __init__(self, model_name: str, pretrained: bool = True, frozen: bool = False, lora: dict = None): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__() if pretrained: model, _ = open_clip.create_model_from_pretrained(f"hf-hub:{model_name}") model = model.visual else: raise NotImplemented self.model = model
if lora: l_config = LoraConfig( r=lora.lora_r, lora_alpha=lora.lora_alpha, target_modules=[ "k_proj", "v_proj", "q_proj", "out_proj", "fc1", "fc2", "visual_projection", "text_projection" ], lora_dropout=lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, image): """ Forward pass of the model. """ return self.model(image) def save_pretrained(self, save_dir): tensors = self.model.state_dict() safetensors.torch.save_file(tensors, save_dir / HF_SAFE_WEIGHTS_NAME)
class CustomPriorModel(torch.nn.Module): def __init__(self, in_hidden_state, out_hidden_state): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__() mid_hidden_state = max(in_hidden_state, out_hidden_state)
self.fc1 = torch.nn.Linear(in_hidden_state*2, mid_hidden_state) self.relu = torch.nn.ReLU() self.fc2 = torch.nn.Linear(mid_hidden_state, out_hidden_state) def reinitialize_model(self): for name, param in self.named_parameters(): if param.requires_grad: if len(param.shape) > 1: torch.nn.init.xavier_uniform_(param) else: if 'weight' in name: torch.nn.init.normal_(param) else: torch.nn.init.zeros_(param)
def forward(self, feats): """ Forward pass of the model. """ return PriorTransformerOutput(predicted_image_embedding=self.fc2(self.relu(self.fc1(feats)))) def save_pretrained(self, save_dir): pass # tensors = self.state_dict() # safetensors.torch.save_file(tensors, os.path.join(save_dir, HF_SAFE_WEIGHTS_NAME_PRIOR))
def test_text_model(register=False, upload=False): # register the classes if register: AutoConfig.register("clip_custom_text_model", CLIPTextEncoderOnlyConfig) AutoModel.register(CLIPTextEncoderOnlyConfig, CLIPTextEncoderOnly) CLIPTextEncoderOnlyConfig.register_for_auto_class() CLIPTextEncoderOnly.register_for_auto_class("AutoModel")
if upload: # Initialize the model model_name = "openai/clip-vit-base-patch32" pretrained=True lora=None
cfg = CLIPTextEncoderOnlyConfig(model_name=model_name, pretrained=pretrained, lora=lora) model = CLIPTextEncoderOnly(cfg) model.push_to_hub("test-text-hf-upload")
model = CLIPTextEncoderOnly.from_pretrained("mpatel57/test-text-hf-upload", force_download=True)
def test_custom_text_model(register=False, upload=False): # register the classes if register: AutoConfig.register("whole_custom_text_model", CustomTextEncoderOnlyConfig) AutoModel.register(CustomTextEncoderOnlyConfig, CustomTextEncoderOnly) CustomTextEncoderOnlyConfig.register_for_auto_class() CustomTextEncoderOnly.register_for_auto_class("AutoModel")
if upload: # Initialize the model model_name = "google-bert/bert-base-uncased" pretrained=True frozen=False output_hidden_size=512 last_hidden_state=False
lora=None
cfg = CustomTextEncoderOnlyConfig(model_name=model_name, pretrained=pretrained, frozen=frozen, output_hidden_size=output_hidden_size, last_hidden_state=last_hidden_state, lora=lora) model = CustomTextEncoderOnly(cfg) model.push_to_hub("test-text-hf-upload")
model = CustomTextEncoderOnly.from_pretrained("mpatel57/test-text-hf-upload", force_download=True)
def test_vision_model(register=False, upload=False): # register the classes if register: AutoConfig.register("clip_custom_vision_model", CLIPVisionEncoderOnlyConfig) AutoModel.register(CLIPVisionEncoderOnlyConfig, CLIPVisionEncoderOnly) CLIPVisionEncoderOnlyConfig.register_for_auto_class() CLIPVisionEncoderOnly.register_for_auto_class("AutoModel")
if upload: # Initialize the model model_name = "openai/clip-vit-base-patch32" pretrained=True lora=None
cfg = CLIPVisionEncoderOnlyConfig(model_name=model_name, pretrained=pretrained, lora=lora) model = CLIPVisionEncoderOnly(cfg) model.push_to_hub("test-vision-hf-upload")
model = CLIPVisionEncoderOnly.from_pretrained("mpatel57/test-vision-hf-upload", force_download=True)
if __name__ == "__main__": test_custom_text_model(register=False, upload=True) # test_text_model(register=False, upload=True) # test_vision_model(register=False, upload=True)12345678910111213141516{ "architectures": [ "CLIPVisionEncoderOnly" ], "auto_map": { "AutoConfig": "utils.CLIPVisionEncoderOnlyConfig", "AutoModel": "utils.CLIPVisionEncoderOnly" }, "frozen": false, "lora": null, "model_name": "openai/clip-vit-base-patch32", "model_type": "clip_custom_vision_model", "pretrained": false, "torch_dtype": "float32", "transformers_version": "4.40.1"}123version https://git-lfs.github.com/spec/v1oid sha256:e0b1e08b34142e78ec2be6d98da877d5758b0dee893bcf98f1d3556adadf5e67size 351421984123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372from transformers import AutoConfig, AutoModel, PretrainedConfig, CLIPTextConfig, CLIPVisionConfig, PreTrainedModel, CLIPTextModelWithProjection, CLIPVisionModelWithProjectionfrom transformers.utils import ModelOutputimport torchimport open_clipfrom dataclasses import dataclassimport safetensors.torchfrom peft import get_peft_config, get_peft_model, LoraConfig, TaskTypeimport os
HF_SAFE_WEIGHTS_NAME = "open_clip_model.safetensors"HF_SAFE_WEIGHTS_NAME_PRIOR = "prior_model.safetensors"
@dataclassclass PriorTransformerOutput(ModelOutput): """ The output of [`PriorTransformer`].
Args: predicted_image_embedding (`torch.FloatTensor` of shape `(batch_size, embedding_dim)`): The predicted CLIP image embedding conditioned on the CLIP text embedding input. """
predicted_image_embedding: torch.FloatTensor
@dataclassclass TextEncoderOutput(ModelOutput): """ Output class for CLIPTextEncoderOnly model to store the outputs in a Hugging Face transformer style.
Attributes: prompt_embeds (torch.Tensor): The embeddings of the input prompts. last_hidden_states (torch.Tensor): The last hidden states from the model. """ text_embeds: torch.FloatTensor = None last_hidden_state: torch.FloatTensor = None
class CLIPTextEncoderOnlyConfig(CLIPTextConfig): model_type = "clip_custom_text_model"
def __init__(self, model_name: str = None, pretrained: bool = True, frozen: bool = False, lora: dict = None, **kwargs): self.model_name = model_name self.pretrained = pretrained self.frozen = frozen self.lora = lora super().__init__(**kwargs)
class CLIPTextEncoderOnly(PreTrainedModel): config_class = CLIPTextEncoderOnlyConfig
def __init__(self, config): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__(config) if config.pretrained: self.model = CLIPTextModelWithProjection.from_pretrained(config.model_name) else: base_cfg = CLIPTextConfig.from_pretrained(config.model_name) self.model = CLIPTextModelWithProjection(base_cfg)
if config.lora: l_config = LoraConfig( r=config.lora.lora_r, lora_alpha=config.lora.lora_alpha, target_modules=[ "k_proj", "v_proj", "q_proj", "out_proj", "fc1", "fc2", "visual_projection", "text_projection" ], lora_dropout=config.lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, input_ids, attention_mask=None, position_ids=None): """ Forward pass of the model.
:param input_ids: Indices of input sequence tokens in the vocabulary. :param attention_mask: Mask to avoid performing attention on padding token indices. :param token_type_ids: Segment token indices to indicate first and second portions of the inputs. :return: Outputs of the model. """ outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, position_ids=position_ids, output_hidden_states=True) return TextEncoderOutput(text_embeds=outputs.text_embeds, last_hidden_state=outputs.last_hidden_state)
class CustomTextEncoderOnlyConfig(PretrainedConfig): model_type = "whole_custom_text_model"
def __init__(self, model_name: str = None, pretrained: bool = True, frozen: bool = False, output_hidden_size: int = 512, last_hidden_state: bool = False, lora: dict = None, **kwargs): self.model_name = model_name self.pretrained = pretrained self.frozen = frozen self.output_hidden_size = output_hidden_size self.last_hidden_state = last_hidden_state self.lora = lora super().__init__(**kwargs)
class CustomTextEncoderOnly(PreTrainedModel): config_class = CustomTextEncoderOnlyConfig
def __init__(self, config): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__(config)
self.last_hidden_state = config.last_hidden_state
if config.pretrained: self.model = AutoModel.from_pretrained(config.model_name) if config.frozen: for param in self.model.parameters(): param.requires_grad = False else: self.model = AutoModel(config)
self.fc1 = torch.nn.Linear(self.model.config.hidden_size, config.output_hidden_size) if config.last_hidden_state: self.fc2 = torch.nn.Linear(self.model.config.hidden_size, config.output_hidden_size)
if config.lora: l_config = LoraConfig( task_type=TaskType.FEATURE_EXTRACTION, r=config.lora.lora_r, lora_alpha=config.lora.lora_alpha, lora_dropout=config.lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, input_ids, attention_mask=None, token_type_ids=None): """ Forward pass of the model.
:param input_ids: Indices of input sequence tokens in the vocabulary. :param attention_mask: Mask to avoid performing attention on padding token indices. :param token_type_ids: Segment token indices to indicate first and second portions of the inputs. :return: Outputs of the model. """ outputs = self.model(input_ids=input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids, output_hidden_states=True) text_embeds = self.fc1(outputs[1]) last_hidden_state = None if self.last_hidden_state: last_hidden_state = self.fc2(outputs[0]) else: last_hidden_state = outputs[0] return TextEncoderOutput(text_embeds=text_embeds, last_hidden_state=last_hidden_state)
class CLIPVisionEncoderOnlyConfig(PretrainedConfig): model_type = "clip_custom_vision_model"
def __init__(self, model_name: str = None, pretrained: bool = True, frozen: bool = False, lora: dict = None, **kwargs): self.model_name = model_name self.pretrained = pretrained self.frozen = frozen self.lora = lora super().__init__(**kwargs)
class CLIPVisionEncoderOnly(PreTrainedModel): config_class = CLIPVisionEncoderOnlyConfig
def __init__(self, config): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__(config) if config.pretrained: self.model = CLIPVisionModelWithProjection.from_pretrained(config.model_name) else: base_cfg = CLIPVisionConfig.from_pretrained(config.model_name) self.model = CLIPVisionModelWithProjection(base_cfg)
if config.lora: l_config = LoraConfig( r=config.lora.lora_r, lora_alpha=config.lora.lora_alpha, target_modules=[ "k_proj", "v_proj", "q_proj", "out_proj", "fc1", "fc2", "visual_projection", "text_projection" ], lora_dropout=config.lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, data): """ Forward pass of the model. """ return self.model(**data).image_embeds def parameters(self): return self.model.parameters()
class OpenCLIPVisionEncoderOnly(torch.nn.Module): def __init__(self, model_name: str, pretrained: bool = True, frozen: bool = False, lora: dict = None): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__() if pretrained: model, _ = open_clip.create_model_from_pretrained(f"hf-hub:{model_name}") model = model.visual else: raise NotImplemented self.model = model
if lora: l_config = LoraConfig( r=lora.lora_r, lora_alpha=lora.lora_alpha, target_modules=[ "k_proj", "v_proj", "q_proj", "out_proj", "fc1", "fc2", "visual_projection", "text_projection" ], lora_dropout=lora.lora_dropout, bias="lora_only", ) self.model = get_peft_model(self.model, l_config)
def forward(self, image): """ Forward pass of the model. """ return self.model(image) def save_pretrained(self, save_dir): tensors = self.model.state_dict() safetensors.torch.save_file(tensors, save_dir / HF_SAFE_WEIGHTS_NAME)
class CustomPriorModel(torch.nn.Module): def __init__(self, in_hidden_state, out_hidden_state): """ Initializes the Hugging Face text encoder for CLIP model, inheriting from PreTrainedModel.
:param model_name: The name or path of the pretrained model. :param pretrained: Whether to load the pretrained weights. """ super().__init__() mid_hidden_state = max(in_hidden_state, out_hidden_state)
self.fc1 = torch.nn.Linear(in_hidden_state*2, mid_hidden_state) self.relu = torch.nn.ReLU() self.fc2 = torch.nn.Linear(mid_hidden_state, out_hidden_state) def reinitialize_model(self): for name, param in self.named_parameters(): if param.requires_grad: if len(param.shape) > 1: torch.nn.init.xavier_uniform_(param) else: if 'weight' in name: torch.nn.init.normal_(param) else: torch.nn.init.zeros_(param)
def forward(self, feats): """ Forward pass of the model. """ return PriorTransformerOutput(predicted_image_embedding=self.fc2(self.relu(self.fc1(feats)))) def save_pretrained(self, save_dir): pass # tensors = self.state_dict() # safetensors.torch.save_file(tensors, os.path.join(save_dir, HF_SAFE_WEIGHTS_NAME_PRIOR))
def test_text_model(register=False, upload=False): # register the classes if register: AutoConfig.register("clip_custom_text_model", CLIPTextEncoderOnlyConfig) AutoModel.register(CLIPTextEncoderOnlyConfig, CLIPTextEncoderOnly) CLIPTextEncoderOnlyConfig.register_for_auto_class() CLIPTextEncoderOnly.register_for_auto_class("AutoModel")
if upload: # Initialize the model model_name = "openai/clip-vit-base-patch32" pretrained=True lora=None
cfg = CLIPTextEncoderOnlyConfig(model_name=model_name, pretrained=pretrained, lora=lora) model = CLIPTextEncoderOnly(cfg) model.push_to_hub("test-text-hf-upload")
model = CLIPTextEncoderOnly.from_pretrained("mpatel57/test-text-hf-upload", force_download=True)
def test_custom_text_model(register=False, upload=False): # register the classes if register: AutoConfig.register("whole_custom_text_model", CustomTextEncoderOnlyConfig) AutoModel.register(CustomTextEncoderOnlyConfig, CustomTextEncoderOnly) CustomTextEncoderOnlyConfig.register_for_auto_class() CustomTextEncoderOnly.register_for_auto_class("AutoModel")
if upload: # Initialize the model model_name = "google-bert/bert-base-uncased" pretrained=True frozen=False output_hidden_size=512 last_hidden_state=False
lora=None
cfg = CustomTextEncoderOnlyConfig(model_name=model_name, pretrained=pretrained, frozen=frozen, output_hidden_size=output_hidden_size, last_hidden_state=last_hidden_state, lora=lora) model = CustomTextEncoderOnly(cfg) model.push_to_hub("test-text-hf-upload")
model = CustomTextEncoderOnly.from_pretrained("mpatel57/test-text-hf-upload", force_download=True)
def test_vision_model(register=False, upload=False): # register the classes if register: AutoConfig.register("clip_custom_vision_model", CLIPVisionEncoderOnlyConfig) AutoModel.register(CLIPVisionEncoderOnlyConfig, CLIPVisionEncoderOnly) CLIPVisionEncoderOnlyConfig.register_for_auto_class() CLIPVisionEncoderOnly.register_for_auto_class("AutoModel")
if upload: # Initialize the model model_name = "openai/clip-vit-base-patch32" pretrained=True lora=None
cfg = CLIPVisionEncoderOnlyConfig(model_name=model_name, pretrained=pretrained, lora=lora) model = CLIPVisionEncoderOnly(cfg) model.push_to_hub("test-vision-hf-upload")
model = CLIPVisionEncoderOnly.from_pretrained("mpatel57/test-vision-hf-upload", force_download=True)
if __name__ == "__main__": test_custom_text_model(register=False, upload=True) # test_text_model(register=False, upload=True) # test_vision_model(register=False, upload=True)text-encoder/config.jsonAdded+31
text-encoder/model.safetensorsAdded+3
text-encoder/utils.pyAdded+372
utils.pyAdded+372
vision-encoder/config.jsonAdded+16
vision-encoder/model.safetensorsAdded+3
vision-encoder/utils.pyAdded+372