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

Update Paddle API for ovc tools #23250

Open
wants to merge 8 commits into
base: master
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
3 changes: 2 additions & 1 deletion .github/workflows/job_python_unit_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ jobs:
if [[ "${{ runner.os }}" == "Linux" ]] && [[ "${{ runner.arch }}" == "ARM64" ]]; then
# Find gomp lib
GOMP_LIB=$(find "${PIP_INSTALL_PATH}/torch/lib/../../torch.libs/" -name '*libgomp-*so*')
export LD_PRELOAD=${GOMP_LIB}
GOMP_LIB_GLOBAL=$(ldconfig -p | grep libgomp | grep AArch64 | awk '{print $4}')
export LD_PRELOAD=${GOMP_LIB}:${GOMP_LIB_GLOBAL}
fi

python3 -m pytest ${LAYER_TESTS_INSTALL_DIR}/ovc_python_api_tests --junitxml=${INSTALL_TEST_DIR}/TEST-test_ovc_convert.xml
Expand Down
2 changes: 1 addition & 1 deletion tests/constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tensorflow>=2.5,<2.17.0
test-generator==0.1.2
requests>=2.25.1
opencv-python>=4.5
paddlepaddle==2.5.0
paddlepaddle==2.6.0
protobuf>=3.18.1,<4.0.0
py>=1.9.0
pytest>=5.0,<7.5
Expand Down
1 change: 0 additions & 1 deletion tests/layer_tests/ovc_python_api_tests/test_paddle.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class TestMoConvertPaddle(CommonMOConvertTest):
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):
Expand Down
2 changes: 1 addition & 1 deletion tests/layer_tests/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
-c ../constraints.txt
# paddlepaddle # ticket 95904
paddlepaddle
numpy
onnxruntime
requests
Expand Down
6 changes: 4 additions & 2 deletions tools/ovc/openvino/tools/ovc/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ def convert_model(

PaddlePaddle
paddle.hapi.model.Model
paddle.fluid.dygraph.layers.Layer
paddle.fluid.executor.Executor
paddle.nn.layer.layers.Layer
paddle.fluid.dygraph.layers.Layer # deprecated since paddle v2.5
paddle.base.Executor
paddle.fluid.executor.Executor # deprecated since paddle v2.6

:param input:
Information of model input required for model conversion.
Expand Down
10 changes: 3 additions & 7 deletions tools/ovc/openvino/tools/ovc/convert_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
from openvino.tools.ovc.telemetry_utils import send_params_info, send_conversion_result, \
init_mo_telemetry
from openvino.tools.ovc.moc_frontend.pytorch_frontend_utils import get_pytorch_decoder, extract_input_info_from_example
from openvino.tools.ovc.moc_frontend.paddle_frontend_utils import paddle_frontend_converter
from openvino.tools.ovc.moc_frontend.paddle_frontend_utils import paddle_frontend_converter, is_paddle_model

# pylint: disable=no-name-in-module,import-error
from openvino.frontend import FrontEndManager, OpConversionFailure, TelemetryExtension
Expand Down Expand Up @@ -222,12 +222,8 @@ def check_model_object(argv):
if isinstance(model, io.BytesIO):
return 'onnx'

if 'paddle' in sys.modules:
import paddle
if isinstance(model, paddle.hapi.model.Model) or isinstance(model,
paddle.fluid.dygraph.layers.Layer) or isinstance(
model, paddle.fluid.executor.Executor):
return "paddle"
if 'paddle' in sys.modules and is_paddle_model(model):
return "paddle"

raise Error('Unknown model type: {}'.format(type(model)))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,26 @@
import os
import sys
import tempfile
# pylint: disable=no-name-in-module,import-error

def paddle_class_check(class_str):
try:
import paddle
return eval(class_str)
except:
return None

def paddle_model_check_instance(model, class_str):
cls = paddle_class_check(class_str)
if cls and isinstance(model, cls):
return True
else:
return False

def is_paddle_model(model):
possible_instances = ["paddle.hapi.model.Model", "paddle.nn.layer.layers.Layer", "paddle.fluid.dygraph.layers.Layer", "paddle.base.Executor", "paddle.fluid.executor.Executor"]
is_instance = [paddle_model_check_instance(model, possible_instance) for possible_instance in possible_instances]
return any(is_instance)

class paddle_frontend_converter:
def __init__(self, model, inputs=None, outputs=None):
Expand Down Expand Up @@ -49,26 +68,30 @@ def convert_paddle_to_pdmodel(self):
self.pdiparams = "{}.pdiparams".format(self.model_name)
self.pdiparams_info = "{}.pdiparams.info".format(self.model_name)

import paddle # pylint: disable=import-error
if isinstance(self.model, paddle.hapi.model.Model):
import paddle
if paddle_model_check_instance(self.model, "paddle.hapi.model.Model"):
self.model.save(self.model_name, False)
else:
if self.inputs is None:
raise RuntimeError(
"Saving inference model needs 'inputs' before saving. Please specify 'example_input'"
)
if isinstance(self.model, paddle.fluid.dygraph.layers.Layer):
with paddle.fluid.framework._dygraph_guard(None):
paddle.jit.save(self.model, self.model_name, input_spec=self.inputs, output_spec=self.outputs)
elif isinstance(self.model, paddle.fluid.executor.Executor):
if paddle_model_check_instance(self.model, "paddle.nn.layer.layers.Layer") or paddle_model_check_instance(self.model, "paddle.fluid.dygraph.layers.Layer"):
if paddle_class_check("paddle.base.framework._dygraph_guard"):
with paddle.base.framework._dygraph_guard(None):
paddle.jit.save(self.model, self.model_name, input_spec=self.inputs, output_spec=self.outputs)
else:
with paddle.fluid.framework._dygraph_guard(None):
paddle.jit.save(self.model, self.model_name, input_spec=self.inputs, output_spec=self.outputs)
elif paddle_model_check_instance(self.model, "paddle.base.Executor") or paddle_model_check_instance(self.model, "paddle.fluid.executor.Executor"):
if self.outputs is None:
raise RuntimeError(
"Model is static. Saving inference model needs 'outputs' before saving. Please specify 'output' for this model"
)
paddle.static.save_inference_model(self.model_name, self.inputs, self.outputs, self.model)
else:
raise RuntimeError(
"Conversion just support paddle.hapi.model.Model, paddle.fluid.dygraph.layers.Layer and paddle.fluid.executor.Executor"
"Conversion just support paddle.hapi.model.Model, paddle.nn.layer.layers.Layer, paddle.fluid.dygraph.layers.Layer, paddle.base.Executor and paddle.fluid.executor.Executor"
)

if not os.path.exists(self.pdmodel):
Expand Down
Loading