From 30b557c8e1f03b4748ac3efca599ff51d66561cb Mon Sep 17 00:00:00 2001 From: Volpeon Date: Tue, 4 Apr 2023 07:30:43 +0200 Subject: TI: Bring back old embedding decay --- models/clip/embeddings.py | 15 +++++++-------- models/sparse.py | 14 ++++++++------ 2 files changed, 15 insertions(+), 14 deletions(-) (limited to 'models') diff --git a/models/clip/embeddings.py b/models/clip/embeddings.py index a356434..63a141f 100644 --- a/models/clip/embeddings.py +++ b/models/clip/embeddings.py @@ -37,7 +37,7 @@ def resize_embedding(old_embedding: nn.Embedding, new_num_embeddings: int, initi class ManagedCLIPTextEmbeddings(CLIPTextEmbeddings): - def __init__(self, config: CLIPTextConfig, embeddings: CLIPTextEmbeddings, alpha: float = 1.0): + def __init__(self, config: CLIPTextConfig, embeddings: CLIPTextEmbeddings): super().__init__(config) self.token_embedding = embeddings.token_embedding @@ -49,7 +49,6 @@ class ManagedCLIPTextEmbeddings(CLIPTextEmbeddings): device=self.token_embedding.weight.device, dtype=self.token_embedding.weight.dtype, ) - self.alpha = alpha def resize(self, size: int): self.token_override_embedding.resize(size) @@ -87,7 +86,7 @@ class ManagedCLIPTextEmbeddings(CLIPTextEmbeddings): token_ids = torch.tensor(token_ids, dtype=torch.long) self.token_embedding.weight.data[token_ids] = initializer - self.token_override_embedding.set(token_ids) + self.token_override_embedding.set(token_ids, initializer) def load_embed(self, input_ids: list[int], filename: Path): with safe_open(filename, framework="pt", device="cpu") as file: @@ -101,8 +100,8 @@ class ManagedCLIPTextEmbeddings(CLIPTextEmbeddings): embs, mask = self.token_override_embedding(input_ids) if embs is not None: input_ids = input_ids[mask] - self.token_embedding.weight.data[input_ids] += self.alpha * embs - self.token_override_embedding.unset(input_ids) + self.token_embedding.weight.data[input_ids] = embs + self.token_override_embedding.unset(input_ids) def get_embed(self, input_ids: Union[list[int], torch.LongTensor]): if isinstance(input_ids, list): @@ -111,7 +110,7 @@ class ManagedCLIPTextEmbeddings(CLIPTextEmbeddings): embs = self.token_embedding(input_ids) embs_override, mask = self.token_override_embedding(input_ids) if embs_override is not None: - embs[mask] += self.alpha * embs_override + embs[mask] = embs_override return embs @@ -135,7 +134,7 @@ class ManagedCLIPTextEmbeddings(CLIPTextEmbeddings): return embeddings -def patch_managed_embeddings(text_encoder: CLIPTextModel, alpha: float = 1.0) -> ManagedCLIPTextEmbeddings: - text_embeddings = ManagedCLIPTextEmbeddings(text_encoder.config, text_encoder.text_model.embeddings, alpha) +def patch_managed_embeddings(text_encoder: CLIPTextModel) -> ManagedCLIPTextEmbeddings: + text_embeddings = ManagedCLIPTextEmbeddings(text_encoder.config, text_encoder.text_model.embeddings) text_encoder.text_model.embeddings = text_embeddings return text_embeddings diff --git a/models/sparse.py b/models/sparse.py index 0b15454..8910316 100644 --- a/models/sparse.py +++ b/models/sparse.py @@ -13,10 +13,7 @@ class PseudoSparseEmbedding(nn.Module): self.params = nn.ParameterList() self.mapping = torch.zeros(0, device=device, dtype=torch.long) - def forward(self, input_ids: Optional[torch.LongTensor] = None): - if input_ids is None: - input_ids = torch.arange(self.mapping.shape[0]) - + def forward(self, input_ids: torch.LongTensor): ids = self.mapping[input_ids.to(self.mapping.device)] mask = ~(ids == -1) @@ -43,6 +40,12 @@ class PseudoSparseEmbedding(nn.Module): else: return [self.set(id) for id in input_ids] + if tensor is None: + tensor = torch.zeros(self.embedding_dim, device=self.mapping.device, dtype=self.dtype) + + if tensor.shape[-1] != self.embedding_dim: + raise ValueError(f"Expected tensor of shape [..., {self.embedding_dim}], but got [..., {tensor.shape[-1]}]") + id = self.mapping[input_ids] if id == -1: @@ -50,8 +53,7 @@ class PseudoSparseEmbedding(nn.Module): self.mapping[input_ids] = id self.params.append(torch.zeros(self.embedding_dim, device=self.mapping.device, dtype=self.dtype)) - self.params[id] = tensor if tensor is not None else torch.zeros( - self.embedding_dim, device=self.mapping.device, dtype=self.dtype) + self.params[id] = tensor def unset(self, input_ids: torch.LongTensor): self.mapping[input_ids] = -1 -- cgit v1.2.3-70-g09d2