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

Add LayoutLMv2 to models exportable with ONNX #14555

Closed
wants to merge 5 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[format] files reformatted with black
  • Loading branch information
fadi212 committed Nov 29, 2021
commit b1b50fdc7a8b225f456260ee390f51508c4d03d0
2 changes: 1 addition & 1 deletion src/transformers/models/layoutlmv2/__init__.py
Original file line number Diff line number Diff line change
@@ -25,7 +25,7 @@
"configuration_layoutlmv2": [
"LAYOUTLMV2_PRETRAINED_CONFIG_ARCHIVE_MAP",
"LayoutLMv2Config",
"LayoutLMv2OnnxConfig"
"LayoutLMv2OnnxConfig",
],
"tokenization_layoutlmv2": ["LayoutLMv2Tokenizer"],
}
Original file line number Diff line number Diff line change
@@ -231,12 +231,7 @@ def get_detectron2_config(self):


class LayoutLMv2OnnxConfig(OnnxConfig):
def __init__(
self,
config: PretrainedConfig,
task: str = "default",
patching_specs: List[PatchingSpec] = None
):
def __init__(self, config: PretrainedConfig, task: str = "default", patching_specs: List[PatchingSpec] = None):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do not need to override this as you are simply calling the base clase __init__ method.

super().__init__(config, task=task, patching_specs=patching_specs)

@property
@@ -274,6 +269,7 @@ def generate_dummy_inputs(
"""

import torch

if not framework == TensorType.PYTORCH:
raise NotImplementedError("Exporting LayoutLM to ONNX is currently only supported for PyTorch.")

1 change: 1 addition & 0 deletions src/transformers/onnx/convert.py
Original file line number Diff line number Diff line change
@@ -113,6 +113,7 @@ def export(
raise ValueError("Model and config inputs doesn't match")

if isinstance(model.base_model, LayoutLMv2Model):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is problematic because export should be somehow agnostic to the model to export.
If I understand correctly you want to replace nn.AdaptiveAvgPool2d by nn.AvgPool2d.
What you can do instead of patching the modules (also is it wanted to have hardcoded values for kernel size and stride?), is to patch the underlying op: torch.nn.functional.adaptive_avg_pool2d -> torch.nn.functional.avg_pool2d.

You can do this by providing a PatchingSpec in the LayoutLMv2OnnxConfig.__init__ method.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will look into it.


class PoolerLayer(nn.Module):
def __init__(self):
super(PoolerLayer, self).__init__()
2 changes: 1 addition & 1 deletion src/transformers/onnx/features.py
Original file line number Diff line number Diff line change
@@ -102,7 +102,7 @@ class FeaturesManager:
"sequence-classification",
"token-classification",
onnx_config_cls=LayoutLMv2OnnxConfig,
)
),
}

AVAILABLE_FEATURES = sorted(reduce(lambda s1, s2: s1 | s2, (v.keys() for v in _SUPPORTED_MODEL_KIND.values())))