Open minniekabra opened 5 months ago
Thanks for raising this issue, the support for beam search is yet in progress.
The error message is below
ValueError: All stacks are empty, so the only token accepted is EOS(2), but got 539
Here, I describe how to integrate support for beam search with grammar-constrained decoding in case we have volunteer wants to contribute :)
At present, our library utilizes a logit_processor
to influence the decoding process. This processor uses an underlying parser to determine permissible tokens at each step.
While effective for various decoding/sampling methods, it doesn't suit constrained beam search.
The incompatibility of the constrained logit processor with beam search is complex and relates to the mechanics of beam search itself. However, this detail is not central to this feature, as our focus is on employing the Constraint
class from Hugging Face.
Credit goes to @chanwkimlab for developing the constrained beam search and providing a robust abstraction along with a comprehensive blog post: https://huggingface.co/blog/constrained-beam-search
The procedure involves:
class GrammarConstraint
and conducting tests.GrammarConstraint
instead of GrammarConstraintLogitProcessor
during inference and testing.class GrammarConstraint(Constraint):
def __init__(self, token_ids: List[int]):
super(Constraint, self).__init__()
...
def advance(self):
...
def does_advance(self, token_id: int):
...
def update(self, token_id: int):
...
def reset(self):
self.completed = False
self.fulfilled_idx = 0
def remaining(self):
# For grammar constrained decoding, determining the exact number of remaining tokens may be challenging, but it should not pose a significant issue.
Here are some example implementation of Constraints
in HF library: https://github.com/huggingface/transformers/blob/c60749d6a67d223d65a2fb6105c2459f3469a30d/src/transformers/generation/beam_constraints.py#L129
That's it !