[Python API] Bind test_utils (#8549)

* [Python API] Bind test_utils

* fix building

* fix building

* fix codestyle

* fix codestyle

* try to fix merging

* Update __init__.py

* fix azure jobs

* fixes after master merge

* fix importing

* try to fix import

* Fix array out-of-bounds error (#8966)

* try to fix

* fix building

* fix code style

* undo some changes after rebase

Co-authored-by: Mikhail Nosov <mikhail.nosov@intel.com>
This commit is contained in:
Anastasia KuporosovaandMikhail Nosov authored and GitHub committed 2021-12-09 15:07:12 +03:00
1 parent d2df884274
commit 8453b6395a
17 files changed
+84 -144

No files matched your search

+1 -1
View File
@@ -232,7 +232,7 @@ jobs:
- script: |
export DATA_PATH=$(MODELS_PATH)
export MODELS_PATH=$(MODELS_PATH)
. $(SETUPVARS) -pyver 3.8 && python3 -m pytest -s $(INSTALL_TEST_DIR)/pyngraph $(PYTHON_STATIC_ARGS) --junitxml=TEST-Pyngraph.xml --ignore=$(INSTALL_TEST_DIR)/pyngraph/tests/test_onnx/test_zoo_models.py --ignore=$(INSTALL_TEST_DIR)/pyngraph/tests/test_onnx/test_backend.py
. $(SETUPVARS) -pyver 3.8 && python3 -m pytest -s $(INSTALL_TEST_DIR)/pyngraph $(PYTHON_STATIC_ARGS) --junitxml=TEST-Pyngraph.xml --ignore=$(INSTALL_TEST_DIR)/pyngraph/tests/test_utils/test_utils.py --ignore=$(INSTALL_TEST_DIR)/pyngraph/tests/test_onnx/test_zoo_models.py --ignore=$(INSTALL_TEST_DIR)/pyngraph/tests/test_onnx/test_backend.py
displayName: 'nGraph Python Bindings Tests'
continueOnError: false
@@ -66,10 +66,6 @@ set (PYTHON_BRIDGE_SRC_ROOT ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory (src/openvino/inference_engine)
add_subdirectory (src/openvino/offline_transformations)
if (ENABLE_TESTS)
add_subdirectory(src/openvino/test_utils)
endif()
if(ENABLE_WHEEL)
add_subdirectory(wheel)
endif()
@@ -86,7 +82,7 @@ install(FILES requirements.txt
DESTINATION ${PYTHON_BRIDGE_CPACK_PATH}/${PYTHON_VERSION}
COMPONENT ${PYTHON_COMPONENT})
install(PROGRAMS ${PYTHON_BRIDGE_OUTPUT_DIRECTORY}/__init__.py
install(PROGRAMS ${PYTHON_BRIDGE_SRC_ROOT}/src/openvino/__init__.py
DESTINATION ${PYTHON_BRIDGE_CPACK_PATH}/${PYTHON_VERSION}/openvino
COMPONENT ${PYTHON_COMPONENT})
@@ -1,26 +0,0 @@
#!/usr/bin/env python3
# Copyright (C) 2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input_file', type=str, required=True)
parser.add_argument('-o', '--output_file', type=str, required=True)
args = parser.parse_args()
mode = 'a' if os.path.exists(args.output_file) else 'w'
with open(args.input_file) as input_file, open(args.output_file, mode) as output_file:
lines = input_file.readlines()
count = 0
copied_lines = ["\n"]
for idx, line in enumerate(lines):
if line[0] == '#' and idx <= 2:
count += 1
if idx > 2 and count == 3:
copied_lines.append(line)
output_file.writelines(copied_lines)
@@ -63,15 +63,12 @@ endif()
find_package(Python COMPONENTS Interpreter Development)
# perform copy
# merge __init__.py files of new and old python api
add_custom_command(TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/__init__.py ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/__init__.py
COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/../__init__.py ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../__init__.py
COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_BRIDGE_SRC_ROOT}/requirements.txt ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../../requirements.txt
COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_BRIDGE_SRC_ROOT}/requirements.txt ${PYTHON_BRIDGE_OUTPUT_DIRECTORY}/../../requirements.txt
COMMAND ${Python_EXECUTABLE} ${PYTHON_BRIDGE_SRC_ROOT}/cmake/utils/merge_init_files.py
-i ${PYTHON_BRIDGE_SRC_ROOT}/src/openvino/__init__.py
-o ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/../__init__.py
)
# install
@@ -1,7 +0,0 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from .test_utils_api import *
__all__ = ['CompareNetworks']
@@ -1,15 +0,0 @@
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from .cimport test_utils_api_impl_defs as C
from ..inference_engine.ie_api cimport IENetwork
from libcpp cimport bool
from libcpp.string cimport string
from libcpp.pair cimport pair
def CompareNetworks(IENetwork lhs, IENetwork rhs):
cdef pair[bool, string] c_pair
c_pair = C.CompareNetworks(lhs.impl, rhs.impl)
return c_pair
@@ -1,13 +0,0 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include "test_utils_api_impl.hpp"
#include <common_test_utils/ngraph_test_utils.hpp>
#include <string>
std::pair<bool, std::string> InferenceEnginePython::CompareNetworks(InferenceEnginePython::IENetwork lhs,
InferenceEnginePython::IENetwork rhs) {
return compare_functions(lhs.actual->getFunction(), rhs.actual->getFunction(), true, true, false, true, true);
}
@@ -1,14 +0,0 @@
// Copyright (C) 2018-2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#pragma once
#include "Python.h"
#include "ie_api_impl.hpp"
namespace InferenceEnginePython {
std::pair<bool, std::string> CompareNetworks(InferenceEnginePython::IENetwork, InferenceEnginePython::IENetwork);
}; // namespace InferenceEnginePython
@@ -1,11 +0,0 @@
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from libcpp cimport bool
from libcpp.string cimport string
from libcpp.pair cimport pair
from ..inference_engine.ie_api_impl_defs cimport IENetwork
cdef extern from "test_utils_api_impl.hpp" namespace "InferenceEnginePython":
cdef pair[bool, string] CompareNetworks(IENetwork lhs, IENetwork rhs)
@@ -1,29 +0,0 @@
# Copyright (C) 2018-2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from openvino.inference_engine import IECore, IENetwork
import ngraph as ng
from ngraph.impl.op import Parameter
from ngraph.impl import Function, Shape, Type
def get_test_cnnnetwork():
element_type = Type.f32
param = Parameter(element_type, Shape([1, 3, 22, 22]))
relu = ng.relu(param)
func = Function([relu], [param], 'test')
caps = Function.to_capsule(func)
cnnNetwork = IENetwork(caps)
assert cnnNetwork != None
return cnnNetwork
def test_compare_networks():
try:
from openvino.test_utils import CompareNetworks
net = get_test_cnnnetwork()
status, msg = CompareNetworks(net, net)
assert status
except:
print("openvino.test_utils.CompareNetworks is not available")
+2
View File
@@ -2,5 +2,7 @@
# SPDX-License-Identifier: Apache-2.0
#
set(PYTHON_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
add_subdirectory(compatibility)
add_subdirectory(pyopenvino)
@@ -0,0 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from .test_utils_api import compare_functions
@@ -44,9 +44,13 @@ if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
add_compile_options(-Wno-deprecated-declarations -Wno-undef)
endif()
if(ENABLE_TESTS)
add_subdirectory(test_utils)
endif()
# create target
file(GLOB_RECURSE SOURCES *.cpp)
file(GLOB_RECURSE SOURCES core/*.cpp graph/*.cpp frontend/*.cpp pyopenvino.cpp)
pybind11_add_module(${PROJECT_NAME} MODULE ${SOURCES})
@@ -1,4 +1,4 @@
# Copyright (C) 2018-2021 Intel Corporation
# Copyright (C) 2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
#
@@ -8,25 +8,23 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PYTHON_BRIDGE_OUTPUT_DIRECTORY}/test_utils)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PYTHON_BRIDGE_OUTPUT_DIRECTORY}/test_utils)
set(CMAKE_COMPILE_PDB_OUTPUT_DIRECTORY ${PYTHON_BRIDGE_OUTPUT_DIRECTORY}/test_utils)
set(CMAKE_PDB_OUTPUT_DIRECTORY ${PYTHON_BRIDGE_OUTPUT_DIRECTORY}/test_utils)
set(PYTHON_BRIDGE_CPACK_PATH "python")
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/test_utils_api.pyx
${CMAKE_CURRENT_SOURCE_DIR}/test_utils_api_impl.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test_utils_api_impl.hpp
${CMAKE_CURRENT_SOURCE_DIR}/test_utils_api_impl_defs.pxd)
file(GLOB SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/test_utils.cpp)
set_source_files_properties(${CMAKE_CURRENT_SOURCE_DIR}/test_utils_api.pyx
PROPERTIES CYTHON_IS_CXX ON)
if(NOT DEFINED OpenVINO_SOURCE_DIR)
find_package(InferenceEngineDeveloperPackage REQUIRED)
endif()
# create target
cython_add_module(${TARGET_NAME} ${SOURCES})
pybind11_add_module(${TARGET_NAME} MODULE ${SOURCES})
add_dependencies(${TARGET_NAME} ie_api)
ov_python_disable_intel_warnings(${TARGET_NAME})
add_dependencies(${TARGET_NAME} pyopenvino)
if(COMMAND ie_add_vs_version_file)
ie_add_vs_version_file(NAME ${TARGET_NAME}
FILEDESCRIPTION "Test Utils Python library")
FILEDESCRIPTION "Test Utils Python library")
endif()
if(InferenceEngineDeveloperPackage_FOUND)
@@ -36,7 +34,6 @@ else()
endif()
target_include_directories(${TARGET_NAME} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_include_directories(${TARGET_NAME} SYSTEM PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/../inference_engine")
target_link_libraries(${TARGET_NAME} PRIVATE ${link_libraries})
# Compatibility with python 2.7 which has deprecated "register" specifier
@@ -46,12 +43,12 @@ endif()
# perform copy
add_custom_command(TARGET ${TARGET_NAME}
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_BRIDGE_SRC_ROOT}/src/openvino/test_utils/__init__.py ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/__init__.py
)
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy ${PYTHON_SOURCE_DIR}/openvino/test_utils/__init__.py ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/__init__.py
)
add_clang_format_target(${TARGET_NAME}_clang FOR_TARGETS ${TARGET_NAME}
EXCLUDE_PATTERNS ".*\\.cxx;.*\\.pxd;.*\\.pyx")
EXCLUDE_PATTERNS ".*\\.cxx")
# install
@@ -61,7 +58,7 @@ install(TARGETS ${TARGET_NAME}
LIBRARY DESTINATION ${PYTHON_BRIDGE_CPACK_PATH}/${PYTHON_VERSION}/openvino/test_utils
COMPONENT tests EXCLUDE_FROM_ALL)
install(PROGRAMS __init__.py
install(PROGRAMS ${PYTHON_SOURCE_DIR}/openvino/test_utils/__init__.py
DESTINATION ${PYTHON_BRIDGE_CPACK_PATH}/${PYTHON_VERSION}/openvino/test_utils
COMPONENT tests
EXCLUDE_FROM_ALL)
@@ -0,0 +1,28 @@
// Copyright (C) 2021 Intel Corporation
// SPDX-License-Identifier: Apache-2.0
//
#include <pybind11/pybind11.h>
#include <string>
#include "common_test_utils/ngraph_test_utils.hpp"
namespace py = pybind11;
PYBIND11_MODULE(test_utils_api, m) {
m.def(
"compare_functions",
[](const ov::Function& lhs, const ov::Function& rhs) {
const auto lhs_ptr = std::const_pointer_cast<ov::Function>(lhs.shared_from_this());
const auto rhs_ptr = std::const_pointer_cast<ov::Function>(rhs.shared_from_this());
const auto fc = FunctionsComparator::with_default()
.enable(FunctionsComparator::ATTRIBUTES)
.enable(FunctionsComparator::CONST_VALUES);
const auto results = fc.compare(lhs_ptr, rhs_ptr);
return std::make_pair(results.valid, results.message);
},
py::arg("lhs"),
py::arg("rhs"));
}
@@ -0,0 +1,26 @@
# Copyright (C) 2021 Intel Corporation
# SPDX-License-Identifier: Apache-2.0
from openvino.runtime import Function
from openvino.runtime.impl import Shape, Type
from openvino.runtime.impl.op import Parameter
import openvino.runtime.opset8 as ops
def get_test_function():
element_type = Type.f32
param = Parameter(element_type, Shape([1, 3, 22, 22]))
relu = ops.relu(param)
func = Function([relu], [param], "test")
assert func is not None
return func
def test_compare_functions():
try:
from openvino.test_utils import compare_functions
func = get_test_function()
status, msg = compare_functions(func, func)
assert status
except RuntimeError:
print("openvino.test_utils.compare_functions is not available")
+1 -1
View File
@@ -20,7 +20,7 @@ commands=
flake8 {posargs:src/ setup.py}
flake8 --ignore=D100,D101,D102,D103,D104,D105,D107,W503 tests/ tests_compatibility/ # ignore lack of docs in tests
mypy --config-file=tox.ini {posargs:src/}
pytest --backend={env:NGRAPH_BACKEND} tests -v -k 'not _cuda' --ignore=tests/test_onnx/test_zoo_models.py --ignore=tests/test_inference_engine
pytest --backend={env:NGRAPH_BACKEND} tests -v -k 'not _cuda' --ignore=tests/test_onnx/test_zoo_models.py --ignore=tests/test_utils --ignore=tests/test_inference_engine
pytest --backend={env:NGRAPH_BACKEND} tests_compatibility -v -k 'not _cuda' --ignore=tests_compatibility/test_onnx/test_zoo_models.py
[testenv:zoo_models]