-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Allow callers to pass tokenizer_options in Text2TextGenerationPipeline #1675
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -72,24 +72,31 @@ export class Text2TextGenerationPipeline | |
| } | ||
|
|
||
| const tokenizer = this.tokenizer; | ||
|
|
||
| // Callers may pass tokenizer_options as a top-level key inside generate_kwargs to | ||
| // control tokenization (e.g. max_length, truncation side) without touching generation | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment says callers can control So |
||
| // parameters. We extract it here so it never leaks into model.generate(). | ||
| const { tokenizer_options: caller_tokenizer_options, ...rest_generate_kwargs } = generate_kwargs; | ||
| const tokenizer_options = { | ||
| padding: true, | ||
| truncation: true, | ||
| ...caller_tokenizer_options, | ||
| }; | ||
|
|
||
| let inputs; | ||
| if (this.task === 'translation' && '_build_translation_inputs' in tokenizer) { | ||
| // TODO: move to Translation pipeline? | ||
| // Currently put here to avoid code duplication | ||
| // @ts-ignore | ||
| inputs = tokenizer._build_translation_inputs(texts, tokenizer_options, generate_kwargs); | ||
| inputs = tokenizer._build_translation_inputs(texts, tokenizer_options, rest_generate_kwargs); | ||
| } else { | ||
| inputs = tokenizer(texts, tokenizer_options); | ||
| } | ||
|
|
||
| const outputTokenIds = await this.model.generate({ | ||
| ...inputs, | ||
| ...this._default_generation_config, | ||
| ...generate_kwargs, | ||
| ...rest_generate_kwargs, | ||
| }); | ||
| return tokenizer | ||
| .batch_decode(/** @type {Tensor} */ (outputTokenIds), { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,24 @@ export default () => { | |
| }, | ||
| MAX_TEST_EXECUTION_TIME, | ||
| ); | ||
|
|
||
| it( | ||
| "tokenizer_options are forwarded to the tokenizer", | ||
| async () => { | ||
| const text = "This is a test."; | ||
| // max_length=3 forces aggressive truncation via tokenizer_options; | ||
| // the model should still run without error and return a string result. | ||
| // tokenizer_options is extracted before generate() is called so it | ||
| // never leaks into GenerationFunctionParameters. | ||
| const output = await pipe(text, { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test would still pass if |
||
| max_new_tokens: 5, | ||
| tokenizer_options: { truncation: true, max_length: 3 }, | ||
| }); | ||
| expect(Array.isArray(output)).toBe(true); | ||
| expect(typeof output[0].generated_text).toBe("string"); | ||
| }, | ||
| MAX_TEST_EXECUTION_TIME, | ||
| ); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The public callback type still describes
optionsonly as generation parameters:If
tokenizer_optionsremains part of the pipeline API, it should get its own typedef, similar to howTextGenerationPipelinedefines text-generation-specific params.