From fe75596454c463c63d025f121a03ff06582e0137 Mon Sep 17 00:00:00 2001 From: Nishant Prabhu <33579638+nishprabhu@users.noreply.github.com> Date: Wed, 22 Sep 2021 01:09:37 +0530 Subject: [PATCH] Layoutlm onnx support (Issue #13300) (#13562) * Add support for exporting PyTorch LayoutLM to ONNX * Added tests for converting LayoutLM to ONNX * Add support for exporting PyTorch LayoutLM to ONNX * Added tests for converting LayoutLM to ONNX * cleanup * Removed regression/ folder * Add support for exporting PyTorch LayoutLM to ONNX * Added tests for converting LayoutLM to ONNX * cleanup * Fixed import error * Remove unnecessary import statements * Changed max_2d_positions from class variable to instance variable of the config class * Add support for exporting PyTorch LayoutLM to ONNX * Added tests for converting LayoutLM to ONNX * cleanup * Add support for exporting PyTorch LayoutLM to ONNX * cleanup * Fixed import error * Changed max_2d_positions from class variable to instance variable of the config class * Use super class generate_dummy_inputs method Co-authored-by: Michael Benayoun * Add support for Masked LM, sequence classification and token classification Co-authored-by: Michael Benayoun * Removed uncessary import and method * Fixed code styling * Raise error if PyTorch is not installed * Remove unnecessary import statement Co-authored-by: Michael Benayoun --- .../models/layoutlm/configuration_layoutlm.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/transformers/models/layoutlm/configuration_layoutlm.py b/src/transformers/models/layoutlm/configuration_layoutlm.py index 913a6bf792512..4c23cde9a5b86 100644 --- a/src/transformers/models/layoutlm/configuration_layoutlm.py +++ b/src/transformers/models/layoutlm/configuration_layoutlm.py @@ -71,6 +71,8 @@ class LayoutLMConfig(BertConfig): The standard deviation of the truncated_normal_initializer for initializing all weight matrices. layer_norm_eps (:obj:`float`, `optional`, defaults to 1e-12): The epsilon used by the layer normalization layers. + gradient_checkpointing (:obj:`bool`, `optional`, defaults to :obj:`False`): + If True, use gradient checkpointing to save memory at the expense of slower backward pass. max_2d_position_embeddings (:obj:`int`, `optional`, defaults to 1024): The maximum value that the 2D position embedding might ever used. Typically set this to something large just in case (e.g., 1024). @@ -106,6 +108,7 @@ def __init__( initializer_range=0.02, layer_norm_eps=1e-12, pad_token_id=0, + gradient_checkpointing=False, max_2d_position_embeddings=1024, **kwargs ): @@ -123,6 +126,7 @@ def __init__( initializer_range=initializer_range, layer_norm_eps=layer_norm_eps, pad_token_id=pad_token_id, + gradient_checkpointing=gradient_checkpointing, **kwargs, ) self.max_2d_position_embeddings = max_2d_position_embeddings @@ -183,6 +187,11 @@ def generate_dummy_inputs( raise ValueError("Cannot generate dummy inputs without PyTorch installed.") import torch - batch_size, seq_length = input_dict["input_ids"].shape - input_dict["bbox"] = torch.tensor([*[box] * seq_length]).tile(batch_size, 1, 1) + input_dict["bbox"] = torch.tensor( + [ + [0] * 4, + *[box] * seq_length, + [self.max_2d_positions] * 4, + ] + ).tile(batch_size, 1, 1) return input_dict