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

[TFLite Export] Adds TFLite support for LayoutLMv3 #1372

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions optimum/exporters/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,7 @@ class TasksManager:
"text-classification",
"token-classification",
onnx="LayoutLMv3OnnxConfig",
tflite="LayoutLMv3TFLiteConfig",
),
"lilt": supported_tasks_mapping(
"feature-extraction",
Expand Down
11 changes: 10 additions & 1 deletion optimum/exporters/tflite/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
configurations.
"""

from ...utils import DummyTextInputGenerator, DummyVisionInputGenerator, logging
from ...utils import DummyBboxInputGenerator, DummyTextInputGenerator, DummyVisionInputGenerator, logging
from .base import TFLiteConfig


Expand All @@ -40,3 +40,12 @@ class VisionTFLiteConfig(TFLiteConfig):

DUMMY_INPUT_GENERATOR_CLASSES = (DummyVisionInputGenerator,)
MANDATORY_AXES = ("batch_size", "num_channels", "width", "height")


class TextAndVisionTFLiteConfig(TFLiteConfig):
"""
Handles multi-modal text and vision architectures.
"""

DUMMY_INPUT_GENERATOR_CLASSES = (DummyTextInputGenerator, DummyVisionInputGenerator, DummyBboxInputGenerator)
MANDATORY_AXES = ("batch_size", "sequence_length", "num_channels", "width", "height")
16 changes: 14 additions & 2 deletions optimum/exporters/tflite/model_configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

from typing import List

from ...utils.normalized_config import NormalizedConfigManager
from ...utils.normalized_config import NormalizedConfigManager, NormalizedTextConfig
from .base import QuantizationApproach
from .config import TextEncoderTFliteConfig, VisionTFLiteConfig
from .config import TextAndVisionTFLiteConfig, TextEncoderTFliteConfig, VisionTFLiteConfig


class BertTFLiteConfig(TextEncoderTFliteConfig):
Expand Down Expand Up @@ -124,3 +124,15 @@ class ResNetTFLiteConfig(VisionTFLiteConfig):
@property
def inputs(self) -> List[str]:
return ["pixel_values"]


class LayoutLMv3TFLiteConfig(TextAndVisionTFLiteConfig):
SUPPORTED_QUANTIZATION_APPROACHES = (QuantizationApproach.INT8_DYNAMIC, QuantizationApproach.FP16)

NORMALIZED_CONFIG_CLASS = NormalizedTextConfig.with_args(
allow_new=True, MAX_2D_POSITION_EMBEDDINGS="max_2d_position_embeddings", image_size="input_size"
)

@property
def inputs(self) -> List[str]:
return ["input_ids", "attention_mask", "bbox", "pixel_values"]
Loading