Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frozen Embeddings with InversionFromLogitsModel is incorrect #70

Open
themurtazanazir opened this issue Oct 1, 2024 · 2 comments
Open

Comments

@themurtazanazir
Copy link

in tokenize_data.py

def embed_dataset_batch(model: InversionModel, batch: Dict) -> Dict:
    assert "input_ids" in batch.keys(), f"invalid keys {batch.keys()}"
    assert hasattr(model, "call_embedding_model")

    input_ids = batch["input_ids"]
    inputs_str = model.tokenizer.batch_decode(input_ids, skip_special_tokens=True)
    emb_input_ids = model.embedder_tokenizer(
        inputs_str,
        max_length=model.config.max_seq_length,
        truncation=True,
        padding="max_length",
        return_tensors="pt",
    ).to(next(model.parameters()).device)

    with torch.no_grad():
        batch["frozen_embeddings"] = model.call_embedding_model(**emb_input_ids)
    return batch

the tokens of embedder are sent to call_embedding_model.

But in models/inversion_from_logits.py

    def call_embedding_model(
        self,
        input_ids: torch.Tensor,
        attention_mask: torch.Tensor,
    ) -> torch.Tensor:
        embedder = self.embedder

        inputs_str = self.tokenizer.batch_decode(input_ids, skip_special_tokens=True)
        emb_input_ids = self.embedder_tokenizer(
            inputs_str,
            max_length=self.config.max_seq_length,
            truncation=True,
            padding="max_length",
            return_tensors="pt",
        ).to(next(self.parameters()).device)

        model_output = embedder(**emb_input_ids)
        return self._process_embedder_output(model_output, emb_input_ids.attention_mask)

This function expects the model.tokenizer's, not model.embedder_tokenizers's tokens.

This causes gibberish tokens to be sent to embedder.

@themurtazanazir
Copy link
Author

themurtazanazir commented Oct 1, 2024

I can raise a PR if needed.

@jxmorris12
Copy link
Owner

@themurtazanazir thank you for finding this – a pull request would be amazing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants