[MO][PDPD] Convert Paddle models from memory (#17005)

* support convert_model in paddle runtime

* add convert runtime paddle test

* fix a pylint error

* fix ci error

* skip test_mo_convert_paddle.py # Ticket: 95904

* auto remove tmp file

* add docs for PDFE

* enable paddle mo test in ci

* fix docs

* fix docs

* fix the docs
This commit is contained in:
Xiuchuan Zhai
2023-05-20 18:13:21 +04:00
committed by GitHub
parent 1b24e15e1e
commit 0b72998631
9 changed files with 315 additions and 6 deletions
@@ -0,0 +1,103 @@
# Copyright (C) 2018-2023 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import numpy as np
import pytest
from common.mo_convert_test_class import CommonMOConvertTest
import openvino.runtime as ov
from openvino.runtime import PartialShape, Model
def make_pd_dynamic_graph_model():
import paddle
paddle.disable_static()
class NeuralNetwork(paddle.nn.Layer):
def __init__(self):
super(NeuralNetwork, self).__init__()
self.relu_sigmoid_stack = paddle.nn.Sequential(
paddle.nn.ReLU(),
paddle.nn.Sigmoid())
def forward(self, input):
return self.relu_sigmoid_stack(input)
return NeuralNetwork()
def make_pd_static_graph_model(shape):
import paddle
import paddle.nn
paddle.enable_static()
x = paddle.static.data(name="x", shape=shape)
y = paddle.static.data(name="y", shape=shape)
relu = paddle.nn.ReLU()
sigmoid = paddle.nn.Sigmoid()
y = sigmoid(relu(x))
exe = paddle.static.Executor(paddle.CPUPlace())
exe.run(paddle.static.default_startup_program())
return exe, x, y
def make_pd_hapi_graph_model(shape):
import paddle
paddle.disable_static()
from paddle.static import InputSpec
net = paddle.nn.Sequential(
paddle.nn.ReLU(),
paddle.nn.Sigmoid())
input = InputSpec(shape, 'float32', 'x')
label = InputSpec(shape, 'float32', 'label')
model = paddle.Model(net, input, label)
optim = paddle.optimizer.SGD(learning_rate=1e-3,
parameters=model.parameters())
model.prepare(optim, paddle.nn.CrossEntropyLoss(), paddle.metric.Accuracy())
return model
def make_ref_graph_model(shape, dtype=np.float32):
shape = PartialShape(shape)
param = ov.opset8.parameter(shape, name="x", dtype=dtype)
relu = ov.opset8.relu(param)
sigm = ov.opset8.sigmoid(relu)
model = Model([sigm], [param], "test")
return model
def create_paddle_dynamic_module(tmp_dir):
import paddle
shape = [2,3,4]
pd_model = make_pd_dynamic_graph_model()
ref_model = make_ref_graph_model(shape)
x = paddle.static.InputSpec(shape=shape, dtype='float32', name='x')
return pd_model, ref_model, {"example_input": [x]}
def create_paddle_static_module(tmp_dir):
shape = [2,3,4]
pd_model, x, y = make_pd_static_graph_model(shape)
ref_model = make_ref_graph_model(shape)
return pd_model, ref_model, {"example_input": [x], "example_output": [y]}
def create_paddle_hapi_module(tmp_dir):
shape = [2,3,4]
pd_model = make_pd_hapi_graph_model(shape)
ref_model = make_ref_graph_model(shape)
return pd_model, ref_model, {}
class TestMoConvertPaddle(CommonMOConvertTest):
test_data = [
create_paddle_dynamic_module,
create_paddle_static_module,
create_paddle_hapi_module
]
@pytest.mark.skip(reason="Paddlepaddle has incompatible protobuf. Ticket: 95904")
@pytest.mark.parametrize("create_model", test_data)
def test_mo_import_from_memory_paddle_fe(self, create_model, ie_device, precision, ir_version,
temp_dir):
fw_model, graph_ref, mo_params = create_model(temp_dir)
test_params = {'input_model': fw_model, 'use_new_frontend': True}
if mo_params is not None:
test_params.update(mo_params)
self._test_by_ref_graph(temp_dir, test_params, graph_ref, compare_tensor_names=False)
@@ -24,7 +24,7 @@ class TestSubprocessMoConvert(unittest.TestCase):
# We don't expect PyTorch specific parameters to be in help message of the MO tool.
for group in mo_convert_params:
if group == 'Pytorch-specific parameters:':
if group == 'Pytorch-specific parameters:' or group == 'PaddlePaddle-specific parameters:':
continue
for param_name in group:
assert param_name in mo_output
@@ -36,4 +36,4 @@ class TestSubprocessMoConvert(unittest.TestCase):
for group in mo_convert_params:
for param_name in group:
assert param_name in mo_output
assert param_name in mo_output