-
Notifications
You must be signed in to change notification settings - Fork 6
recipe(layoutlm): add document QA support for impira/layoutlm-document-qa #1093
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
Open
DingmaomaoBJTU
wants to merge
4
commits into
main
Choose a base branch
from
add-impira-layoutlm-document-qa
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ae145f7
Add LayoutLM document QA recipe support
github-actions[bot] b945df3
fix: rename layoutlm fp32 recipe
github-actions[bot] 1a2bc79
recipe(layoutlm): nest CPU recipes, ship fp32+fp16, address review
github-actions[bot] 6a0318e
recipe(layoutlm): revert examples/recipes/README.md per review
github-actions[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
examples/recipes/impira_layoutlm-document-qa/cpu/cpu/question-answering_fp16_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| { | ||
| "export": { | ||
| "opset_version": 17, | ||
| "batch_size": 1, | ||
| "export_params": true, | ||
| "do_constant_folding": true, | ||
| "verbose": false, | ||
| "dynamo": false, | ||
| "enable_hierarchy_tags": true, | ||
| "clean_onnx": false, | ||
| "hierarchy_tag_format": "full", | ||
| "input_tensors": [ | ||
| { | ||
| "name": "input_ids", | ||
| "dtype": "int32", | ||
| "shape": [ | ||
| 1, | ||
| 512 | ||
| ], | ||
| "value_range": [ | ||
| 0, | ||
| 50265 | ||
| ] | ||
| }, | ||
| { | ||
| "name": "bbox", | ||
| "dtype": "int32", | ||
| "shape": [ | ||
| 1, | ||
| 512, | ||
| 4 | ||
| ], | ||
| "value_range": [ | ||
| 0, | ||
| 1000 | ||
| ] | ||
| }, | ||
| { | ||
| "name": "attention_mask", | ||
| "dtype": "int32", | ||
| "shape": [ | ||
| 1, | ||
| 512 | ||
| ], | ||
| "value_range": [ | ||
| 0, | ||
| 2 | ||
| ] | ||
| }, | ||
| { | ||
| "name": "token_type_ids", | ||
| "dtype": "int32", | ||
| "shape": [ | ||
| 1, | ||
| 512 | ||
| ], | ||
| "value_range": [ | ||
| 0, | ||
| 1 | ||
| ] | ||
| } | ||
| ], | ||
| "output_tensors": [ | ||
| { | ||
| "name": "start_logits" | ||
| }, | ||
| { | ||
| "name": "end_logits" | ||
| } | ||
| ] | ||
| }, | ||
| "optim": { | ||
| "clamp_constant_values": true | ||
| }, | ||
| "quant": null, | ||
| "loader": { | ||
| "task": "question-answering", | ||
| "model_class": "LayoutLMForQuestionAnswering", | ||
| "model_type": "layoutlm" | ||
| } | ||
| } |
81 changes: 81 additions & 0 deletions
81
examples/recipes/impira_layoutlm-document-qa/cpu/cpu/question-answering_fp32_config.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| { | ||
| "export": { | ||
| "opset_version": 17, | ||
| "batch_size": 1, | ||
| "export_params": true, | ||
| "do_constant_folding": true, | ||
| "verbose": false, | ||
| "dynamo": false, | ||
| "enable_hierarchy_tags": true, | ||
| "clean_onnx": false, | ||
| "hierarchy_tag_format": "full", | ||
| "input_tensors": [ | ||
| { | ||
| "name": "input_ids", | ||
| "dtype": "int32", | ||
| "shape": [ | ||
| 1, | ||
| 512 | ||
| ], | ||
| "value_range": [ | ||
| 0, | ||
| 50265 | ||
| ] | ||
| }, | ||
| { | ||
| "name": "bbox", | ||
| "dtype": "int32", | ||
| "shape": [ | ||
| 1, | ||
| 512, | ||
| 4 | ||
| ], | ||
| "value_range": [ | ||
| 0, | ||
| 1000 | ||
| ] | ||
| }, | ||
| { | ||
| "name": "attention_mask", | ||
| "dtype": "int32", | ||
| "shape": [ | ||
| 1, | ||
| 512 | ||
| ], | ||
| "value_range": [ | ||
| 0, | ||
| 2 | ||
| ] | ||
| }, | ||
| { | ||
| "name": "token_type_ids", | ||
| "dtype": "int32", | ||
| "shape": [ | ||
| 1, | ||
| 512 | ||
| ], | ||
| "value_range": [ | ||
| 0, | ||
| 1 | ||
| ] | ||
| } | ||
| ], | ||
| "output_tensors": [ | ||
| { | ||
| "name": "start_logits" | ||
| }, | ||
| { | ||
| "name": "end_logits" | ||
| } | ||
| ] | ||
| }, | ||
| "optim": { | ||
| "clamp_constant_values": true | ||
| }, | ||
| "quant": null, | ||
| "loader": { | ||
| "task": "question-answering", | ||
| "model_class": "LayoutLMForQuestionAnswering", | ||
| "model_type": "layoutlm" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # ------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. | ||
| # -------------------------------------------------------------------------- | ||
| """LayoutLM HuggingFace Model Configuration.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any, cast | ||
|
|
||
| from optimum.exporters.onnx.model_configs import LayoutLMOnnxConfig | ||
| from optimum.utils import NormalizedTextConfig | ||
| from optimum.utils.input_generators import DummyBboxInputGenerator, DummyVisionInputGenerator | ||
|
|
||
| from ...export import MaxLengthTextInputGenerator, register_onnx_overwrite | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| import torch | ||
|
|
||
|
|
||
| class ZeroTokenTypeLayoutLMTextInputGenerator(MaxLengthTextInputGenerator): | ||
| """LayoutLM text dummy generator that keeps token_type_ids within type_vocab_size=1.""" | ||
|
|
||
| def generate( | ||
| self, | ||
| input_name: str, | ||
| framework: str = "pt", | ||
| int_dtype: str = "int64", | ||
| float_dtype: str = "fp32", | ||
| ) -> torch.Tensor: | ||
| """Generate LayoutLM text inputs, replacing token_type_ids with zeros.""" | ||
| tensor = cast( | ||
| "torch.Tensor", | ||
| super().generate( | ||
| input_name, | ||
| framework=framework, | ||
| int_dtype=int_dtype, | ||
| float_dtype=float_dtype, | ||
| ), | ||
| ) | ||
| if input_name == "token_type_ids": | ||
| return tensor.new_zeros(tensor.shape) | ||
| return tensor | ||
|
|
||
|
|
||
| @register_onnx_overwrite("layoutlm", "question-answering", library_name="transformers") | ||
| class LayoutLMQAIOConfig(LayoutLMOnnxConfig): # type: ignore[misc] # optimum base is untyped | ||
| """LayoutLM question-answering OnnxConfig with bbox and safe token type IDs.""" | ||
|
|
||
| # sequence_length is bound to the model's max_position_embeddings so | ||
| # MaxLengthTextInputGenerator emits full-length text inputs instead of | ||
| # Optimum's default of 16 (allow_new=True permits adding this mapping). | ||
| # We deliberately do NOT map max_2d_position_embeddings here: Optimum's | ||
| # DummyBboxInputGenerator hardcodes its coordinate range (its | ||
| # normalized_config.max_2d_position_embeddings read is commented out | ||
| # upstream), so such a mapping is inert and never becomes a sequence | ||
| # length. bbox coordinate bounds for the shipped recipe come from the | ||
| # recipe's `value_range` instead. | ||
| NORMALIZED_CONFIG_CLASS = NormalizedTextConfig.with_args( | ||
| sequence_length="max_position_embeddings", | ||
| allow_new=True, | ||
| ) | ||
| DUMMY_INPUT_GENERATOR_CLASSES: tuple[type[Any], ...] = ( | ||
| ZeroTokenTypeLayoutLMTextInputGenerator, | ||
| DummyVisionInputGenerator, | ||
| DummyBboxInputGenerator, | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.