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

Problem in candidate-based generation on GENRE using transformers >= 4.36.0 #103

Open
y0uCeF opened this issue Jan 20, 2024 · 1 comment

Comments

@y0uCeF
Copy link

y0uCeF commented Jan 20, 2024

Since version v4.36.0 of huggingface transformers, it is not allowed to have prefix_allowed_tokens_fn return an empty set of tokens 27797.
When doing non-free generation on GENRE, which is based on candidates, the output of the lambda assigned to prefix_allowed_tokens_fn may indeed be an empty list, raising a ValueError with the following message:

f"`prefix_allowed_tokens_fn` returned an empty list for batch ID {batch_id}."
"This means that the constraint is unsatisfiable. Please check your implementation"
f"of `prefix_allowed_tokens_fn` "

I have not reproduced the case for free generation, and it seems the code I use for per-mention Trie creation is no longer presented in examples.ipynb :

trie = Trie([
                  [2] + model.encode(e).tolist()[1:]
                  for e in doc["candidates"]
          ])
          if doc["candidates"] else Trie([[2] + self.model.encode("NIL").tolist()[1:]])

Anyway, it is recommended to stick to versions < v4.36.0 (such as v4.35.2) if one falls into that error.
This condition may be added to requirements.txt

@Klaifer
Copy link

Klaifer commented Jul 19, 2024

I had this same problem with the GENRE huggingface implementation, and I developed a workaround.

I'm going to insert it here because I think it might help someone, or someone might adjust it to work in this implementation.

The implementation I'm referring to is: https://huggingface.co/facebook/mgenre-wiki

There is the following instruction:

outputs = model.generate(
    **tokenizer(sentences, return_tensors="pt"),
    num_beams=5,
    num_return_sequences=5,
    # OPTIONAL: use constrained beam search
    prefix_allowed_tokens_fn=lambda batch_id, sent: trie.get(sent.tolist()),
)

The solution is to replace with:

outputs = model.generate(
    **tokenizer(sentences, return_tensors="pt"),
    num_beams=5,
    num_return_sequences=5,
    # OPTIONAL: use constrained beam search
    prefix_allowed_tokens_fn=lambda batch_id, sent: trie.get(sent.tolist()) or [tokenizer.eos_token_id],
)

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