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

Conversation

fadi212
Copy link

@fadi212 fadi212 commented Nov 28, 2021

This PR adds the code for converting LayoutLMv2 model to onnx format.

@fadi212
Copy link
Author

fadi212 commented Nov 28, 2021

@michaelbenayoun Can you please review this PR and highlight why these circleci tests are failing ?

Copy link
Member

@michaelbenayoun michaelbenayoun left a comment

Choose a reason for hiding this comment

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

Great contribution, thanks!



class LayoutLMv2OnnxConfig(OnnxConfig):
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.

]
)

def generate_dummy_inputs(
Copy link
Member

@michaelbenayoun michaelbenayoun Nov 29, 2021

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 rewrite everything, I think you can inspire from what was done for LayoutLM

Copy link
Author

Choose a reason for hiding this comment

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

@michaelbenayoun input for LayoutLMv2 requires image too and bboxes tile so I had to write this method. If I am missing anything please highlight what changes can I make,

Copy link
Member

Choose a reason for hiding this comment

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

Yes, you need to, but you can use the base class generate_dummy_inputs method, and then complete the method with the additional stuff, just as it was done for LayoutLM.

@@ -855,10 +855,10 @@ def forward(

device = input_ids.device if input_ids is not None else inputs_embeds.device

visual_shape = list(input_shape)
visual_shape = list(torch.empty(size=input_shape).size())
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for this?

Choose a reason for hiding this comment

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

image
image

Copy link
Author

Choose a reason for hiding this comment

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

Yes @linchpinlin exactly. Thanks
@michaelbenayoun I get tensor size error if I don't apply these changes

visual_shape[1] = self.config.image_feature_pool_shape[0] * self.config.image_feature_pool_shape[1]
visual_shape = torch.Size(visual_shape)
final_shape = list(input_shape)
final_shape = list(torch.empty(size=input_shape).size())
Copy link
Member

Choose a reason for hiding this comment

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

Same question

@@ -110,6 +112,19 @@ def export(
if not inputs_match:
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.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the contributing guidelines are likely to be ignored.

@malcolmgreaves
Copy link

Hi! Thanks for working on this @fadi212 🙏 I am interested in this work, but I see it has been quite some time since you've had an opportunity to work on it. Are you still working on this? If you no longer have the time or resources to do so, would you be able to provide any next steps or advice on what is necessary for completion? Thank you for your time and effort 🤗

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.

4 participants