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 dy2static support of LayoutLMv2 SER and LayoutXLM SER #2695

Merged
merged 6 commits into from
Jul 5, 2022

Conversation

WenmuZhou
Copy link
Contributor

PR types

New features

PR changes

Models

Description

add dy2static support of LayoutLMv2 and LayoutxLM

Use the following commands to complete the dynamic and static transformation of the model

  • LayoutXLM
import paddlenlp as ppnlp
model = ppnlp.transformers.LayoutXLMForTokenClassification.from_pretrained("layoutxlm-base-uncased")
model.eval()
model = paddle.jit.to_static(
        model,
        input_spec=[
            paddle.static.InputSpec(
                shape=[None, 512], dtype="int64"),  # input_ids
            paddle.static.InputSpec(
                shape=[None, 512, 4], dtype="int64"),  # bbox
            paddle.static.InputSpec(
                shape=[None, 3, 224, 224], dtype="int64"),  # image
            paddle.static.InputSpec(
                shape=[None, 512], dtype="int64"),  # attention_mask
        ])

# Save in static graph model.
save_path = os.path.join(args.output_path, "inference")
paddle.jit.save(model, 'layoutxlm')
  • LayoutLMv2
import paddlenlp as ppnlp
model = ppnlp.transformers.LayoutXLMForTokenClassification.from_pretrained("layoutlmv2-base-uncased")
model.eval()
model = paddle.jit.to_static(
        model,
        input_spec=[
            paddle.static.InputSpec(
                shape=[None, 512], dtype="int64"),  # input_ids
            paddle.static.InputSpec(
                shape=[None, 512, 4], dtype="int64"),  # bbox
            paddle.static.InputSpec(
                shape=[None, 3, 224, 224], dtype="int64"),  # image
            paddle.static.InputSpec(
                shape=[None, 512], dtype="int64"),  # attention_mask
        ])

# Save in static graph model.
save_path = os.path.join(args.output_path, "inference")
paddle.jit.save(model,'layoutlmv2')

@@ -339,7 +339,7 @@ def __init__(self, config):
self.dropout = nn.Dropout(config["attention_probs_dropout_prob"])

def transpose_for_scores(self, x):
new_x_shape = x.shape[:-1] + [
Copy link
Contributor

Choose a reason for hiding this comment

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

直接用paddle.shape是否就ok,避免list

Copy link
Contributor Author

@WenmuZhou WenmuZhou Jun 30, 2022

Choose a reason for hiding this comment

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

使用paddle.shape后使用concat扩充维度会报错,暂时只能这么操作

attention_probs = F.softmax(attention_scores, axis=-1)
# This is actually dropping out entire tokens to attend to, which might
# seem a bit unusual, but is taken from the original Transformer paper.
attention_probs = self.dropout(attention_probs)
context_layer = paddle.matmul(attention_probs, value_layer)
context_layer = context_layer.transpose([0, 2, 1, 3])
new_context_layer_shape = context_layer.shape[:-2] + [
new_context_layer_shape = list(context_layer.shape[:-2]) + [
Copy link
Contributor

Choose a reason for hiding this comment

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

同上,paddle.shape直接试下?

@@ -335,9 +335,10 @@ def __init__(self, config):
self.dropout = nn.Dropout(config["attention_probs_dropout_prob"])

def transpose_for_scores(self, x):
new_x_shape = x.shape[:-1] + [
new_x_shape = list(x.shape[:-1]) + [
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

attention_scores)
attention_probs = F.softmax(attention_scores, axis=-1)
# This is actually dropping out entire tokens to attend to, which might
# seem a bit unusual, but is taken from the original Transformer paper.
attention_probs = self.dropout(attention_probs)
context_layer = paddle.matmul(attention_probs, value_layer)
context_layer = context_layer.transpose([0, 2, 1, 3])
new_context_layer_shape = context_layer.shape[:-2] + [
new_context_layer_shape = list(context_layer.shape[:-2]) + [
Copy link
Contributor

Choose a reason for hiding this comment

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

同上

@WenmuZhou WenmuZhou changed the title add dy2static support of LayoutLMv2 and LayoutXLM add dy2static support of LayoutLMv2 SER and LayoutXLM SER Jul 2, 2022
@yingyibiao yingyibiao self-requested a review July 4, 2022 08:33
Copy link
Contributor

@yingyibiao yingyibiao left a comment

Choose a reason for hiding this comment

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

LGTM

@yingyibiao yingyibiao merged commit 7eba091 into PaddlePaddle:develop Jul 5, 2022
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.

3 participants