Skip to content

动转静代码片段

骑马小猫 edited this page Oct 25, 2022 · 1 revision

介绍

动转静之后可以最大程度上提升模型的运行速度,以下为不同模型的动转静的代码片段。

模型列表

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')

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')
Clone this wiki locally