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

Target prefix-constrained generation #172

Merged
merged 1 commit into from
Apr 19, 2023
Merged

Target prefix-constrained generation #172

merged 1 commit into from
Apr 19, 2023

Conversation

gsarti
Copy link
Member

@gsarti gsarti commented Apr 19, 2023

Description

This PR introduces a generate_from_target_prefix argument in the model.attribute method and the inseq attribute CLI, allowing the usage of pre-specified generated_text as a prefix for the model to complete, rather than a finished generation.

This functionality exploits the capabilities of the generate function in 🤗 transformers, and is restricted to encoder-decoder models only, since controlling the prefix is handled through the input_text parameter for decoder-only models. The same behavior could already be achieved in inseq v0.4 by providing explicit decoder_input_ids as generation_args.

Current behavior:

>>> import inseq

>>> m = inseq.load_model("Helsinki-NLP/opus-mt-en-it", "saliency")
>>> prefix_ids = m.encode("Hey mondo!", as_targets=True)
>>> out = model.attribute(
...	   "Hello world! My name is Inseq",
...	   generation_args={"decoder_input_ids": prefix_ids.input_ids}
...)
>>> out.info["generated_texts"]
['Hey mondo! mi chiamo Inseq.']

New behavior using generate_from_target_prefix:

>>> import inseq

>>> m = inseq.load_model("Helsinki-NLP/opus-mt-en-it", "saliency")
>>> out = model.attribute(
...    "Hello world! My name is Inseq",
...    "Hey mondo!",
...    generate_from_target_prefix=True
... )
>>> out.info["generated_texts"]
['Hey mondo! mi chiamo Inseq.']

The new argument also works with batched inputs. Setting the target prefix to an empty string will perform the fully unconstrained generation, as if no prefix was provided:

>>> import inseq

>>> model = inseq.load_model("Helsinki-NLP/opus-mt-en-it", "saliency")
>>> src = ["Hello world! My name is Inseq", "I love bugging around code all day"]
>>> prefix = ["Hey mondo!", ""]
>>> out = model.attribute(src, prefix, generate_from_target_prefix=True)
>>> out.info["generated_texts"]
... ['Hey mondo! mi chiamo Inseq', 'Adoro le cimici intorno al codice tutto il giorno']

Type of Change

  • 🚀 New feature (non-breaking change which adds functionality)

@gsarti gsarti merged commit a4a43e2 into main Apr 19, 2023
@gsarti gsarti deleted the gen-from-prefix branch April 19, 2023 11:34
gsarti added a commit that referenced this pull request Apr 19, 2023
* origin/main:
  Target prefix-constrained generation (#172)
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

Successfully merging this pull request may close these issues.

1 participant