From 7a314f216acc4e8d84519a6279cbd64eee1c29f3 Mon Sep 17 00:00:00 2001 From: Ilya Churaev Date: Thu, 6 Aug 2020 05:51:05 +0300 Subject: [PATCH] Remove JSON serializer (#1638) --- CMakeLists.txt | 1 - build-instruction.md | 4 - docs/IE_DG/Graph_debug_capabilities.md | 14 - ngraph/CMakeLists.txt | 7 - ngraph/cmake/external_json.cmake | 49 - ngraph/environment_variables.md | 2 - ngraph/licenses/json-license.txt | 24 - ngraph/python/setup.py | 1 - ngraph/python/src/ngraph/impl/__init__.py | 1 - ngraph/python/src/pyngraph/pyngraph.cpp | 2 - ngraph/python/src/pyngraph/serializer.cpp | 32 - ngraph/python/src/pyngraph/serializer.hpp | 23 - ngraph/python/tests/runtime.py | 10 +- ngraph/src/ngraph/CMakeLists.txt | 13 - ngraph/src/ngraph/serializer.cpp | 2731 -------------------- ngraph/src/ngraph/serializer.hpp | 89 - ngraph/src/ngraph/serializer_stub.cpp | 49 - ngraph/test/CMakeLists.txt | 7 - ngraph/test/algebraic_simplification.cpp | 1 - ngraph/test/backend/broadcast.in.cpp | 79 - ngraph/test/backend/comparison.in.cpp | 1 - ngraph/test/backend_api.cpp | 32 - ngraph/test/build_graph.cpp | 1 - ngraph/test/control_dependencies.cpp | 54 - ngraph/test/core.cpp | 36 - ngraph/test/pattern.cpp | 1 - ngraph/test/runtime/interpreter/int_backend.cpp | 31 - ngraph/test/runtime/interpreter/int_backend.hpp | 1 - ngraph/test/runtime/interpreter/int_executable.cpp | 28 - ngraph/test/runtime/interpreter/int_executable.hpp | 4 - ngraph/test/serialize.cpp | 931 ------- ngraph/test/util.cpp | 1 - ngraph/test/util/test_tools.cpp | 11 - ngraph/test/util/test_tools.hpp | 4 - 34 files changed, 1 insertion(+), 4274 deletions(-) delete mode 100644 ngraph/cmake/external_json.cmake delete mode 100644 ngraph/licenses/json-license.txt delete mode 100644 ngraph/python/src/pyngraph/serializer.cpp delete mode 100644 ngraph/python/src/pyngraph/serializer.hpp delete mode 100644 ngraph/src/ngraph/serializer.cpp delete mode 100644 ngraph/src/ngraph/serializer.hpp delete mode 100644 ngraph/src/ngraph/serializer_stub.cpp delete mode 100644 ngraph/test/core.cpp delete mode 100644 ngraph/test/serialize.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3346882..0eff7fe 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -80,7 +80,6 @@ function(build_ngraph) else() ngraph_set(NGRAPH_ONNX_IMPORT_ENABLE FALSE) endif() - ngraph_set(NGRAPH_JSON_ENABLE FALSE) ngraph_set(NGRAPH_INTERPRETER_ENABLE TRUE) if(CMAKE_CXX_COMPILER_ID MATCHES "^(Apple)?Clang$") diff --git a/build-instruction.md b/build-instruction.md index 6c55924..ffbf064 100644 --- a/build-instruction.md +++ b/build-instruction.md @@ -146,7 +146,6 @@ You can use the following additional build options: - nGraph-specific compilation options: `-DNGRAPH_ONNX_IMPORT_ENABLE=ON` enables the building of the nGraph ONNX importer. - `-DNGRAPH_JSON_ENABLE=ON` enables nGraph JSON-based serialization. `-DNGRAPH_DEBUG_ENABLE=ON` enables additional debug prints. ## Build for Raspbian Stretch* OS @@ -325,7 +324,6 @@ You can use the following additional build options: - nGraph-specific compilation options: `-DNGRAPH_ONNX_IMPORT_ENABLE=ON` enables the building of the nGraph ONNX importer. - `-DNGRAPH_JSON_ENABLE=ON` enables nGraph JSON-based serialization. `-DNGRAPH_DEBUG_ENABLE=ON` enables additional debug prints. ## Build on Windows* Systems @@ -428,7 +426,6 @@ cmake -G "Visual Studio 15 2017 Win64" -T "Intel C++ Compiler 18.0" ^ - nGraph-specific compilation options: `-DNGRAPH_ONNX_IMPORT_ENABLE=ON` enables the building of the nGraph ONNX importer. - `-DNGRAPH_JSON_ENABLE=ON` enables nGraph JSON-based serialization. `-DNGRAPH_DEBUG_ENABLE=ON` enables additional debug prints. ### Building Inference Engine with Ninja* Build System @@ -520,7 +517,6 @@ You can use the following additional build options: - nGraph-specific compilation options: `-DNGRAPH_ONNX_IMPORT_ENABLE=ON` enables the building of the nGraph ONNX importer. - `-DNGRAPH_JSON_ENABLE=ON` enables nGraph JSON-based serialization. `-DNGRAPH_DEBUG_ENABLE=ON` enables additional debug prints. ## Build on Android* Systems diff --git a/docs/IE_DG/Graph_debug_capabilities.md b/docs/IE_DG/Graph_debug_capabilities.md index 856bbeb..b7a2486 100644 --- a/docs/IE_DG/Graph_debug_capabilities.md +++ b/docs/IE_DG/Graph_debug_capabilities.md @@ -8,20 +8,6 @@ CNNNetwork. Both representations provide an API to get detailed information abou To receive additional messages about applied graph modifications, rebuild the nGraph library with the `-DNGRAPH_DEBUG_ENABLE=ON` option. -To enable serialization and deserialization of the nGraph function to a JSON file, rebuild the -nGraph library with the `-DNGRAPH_JSON_ENABLE=ON` option. To serialize or deserialize the nGraph -function, call the nGraph function as follows: - -```cpp -#include - -std::shared_ptr nGraph; -... -ngraph::serialize("test_json.json", nGraph); // For graph serialization -std::ifstream file("test_json.json"); // Open a JSON file -nGraph = ngraph::deserialize(file); // For graph deserialization -``` - To visualize the nGraph function to the xDot format or to an image file, use the `ngraph::pass::VisualizeTree` graph transformation pass: ```cpp diff --git a/ngraph/CMakeLists.txt b/ngraph/CMakeLists.txt index c8cee9b..4047cb0 100644 --- a/ngraph/CMakeLists.txt +++ b/ngraph/CMakeLists.txt @@ -120,7 +120,6 @@ option(NGRAPH_CODE_COVERAGE_ENABLE "Enable code coverage data collection" FALSE) option(NGRAPH_LIB_VERSIONING_ENABLE "Enable shared library versioning" FALSE) option(NGRAPH_PYTHON_BUILD_ENABLE "Enable build nGraph python package wheel" FALSE) option(NGRAPH_IE_ENABLE "Enable the Inference Engine backend" FALSE) -option(NGRAPH_JSON_ENABLE "Enable JSON based serialization and tracing features" TRUE) option(NGRAPH_DYNAMIC_COMPONENTS_ENABLE "Enable dynamic loading of components" TRUE) option(NGRAPH_EXPORT_TARGETS_ENABLE "Enable exporting nGraph cmake export targets" TRUE) option(NGRAPH_WARNINGS_AS_ERRORS "Make all nGraph compile-time warnings into errors" FALSE) @@ -158,7 +157,6 @@ NORMALIZE_BOOL(NGRAPH_CODE_COVERAGE_ENABLE) NORMALIZE_BOOL(NGRAPH_LIB_VERSIONING_ENABLE) NORMALIZE_BOOL(NGRAPH_PYTHON_BUILD_ENABLE) NORMALIZE_BOOL(NGRAPH_IE_ENABLE) -NORMALIZE_BOOL(NGRAPH_JSON_ENABLE) NORMALIZE_BOOL(NGRAPH_DYNAMIC_COMPONENTS_ENABLE) NORMALIZE_BOOL(NGRAPH_EXPORT_TARGETS_ENABLE) @@ -177,7 +175,6 @@ message(STATUS "NGRAPH_EXPORT_TARGETS_ENABLE: ${NGRAPH_EXPORT_TARGETS_EN message(STATUS "NGRAPH_IE_ENABLE: ${NGRAPH_IE_ENABLE}") message(STATUS "NGRAPH_IE_STATIC_LIB_ENABLE: ${NGRAPH_IE_STATIC_LIB_ENABLE}") message(STATUS "NGRAPH_INTERPRETER_ENABLE: ${NGRAPH_INTERPRETER_ENABLE}") -message(STATUS "NGRAPH_JSON_ENABLE: ${NGRAPH_JSON_ENABLE}") message(STATUS "NGRAPH_LIB_VERSIONING_ENABLE: ${NGRAPH_LIB_VERSIONING_ENABLE}") message(STATUS "NGRAPH_ONNX_IMPORT_ENABLE: ${NGRAPH_ONNX_IMPORT_ENABLE}") message(STATUS "NGRAPH_PYTHON_BUILD_ENABLE: ${NGRAPH_PYTHON_BUILD_ENABLE}") @@ -486,10 +483,6 @@ if (NGRAPH_ONNX_IMPORT_ENABLE) endif() endif() -if(NGRAPH_JSON_ENABLE) - include(cmake/external_json.cmake) -endif() - add_subdirectory(src) if (NGRAPH_TEST_UTIL_ENABLE) diff --git a/ngraph/cmake/external_json.cmake b/ngraph/cmake/external_json.cmake deleted file mode 100644 index ede3a91..0000000 --- a/ngraph/cmake/external_json.cmake +++ /dev/null @@ -1,49 +0,0 @@ -# ****************************************************************************** -# Copyright 2017-2020 Intel Corporation -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ****************************************************************************** - -# Enable ExternalProject CMake module -include(ExternalProject) - -#------------------------------------------------------------------------------ -# Download json -#------------------------------------------------------------------------------ - -SET(JSON_GIT_REPO_URL https://github.com/nlohmann/json) -if(WIN32) - SET(JSON_GIT_LABEL v3.5.0) -else() - SET(JSON_GIT_LABEL v3.7.3) -endif() - -ExternalProject_Add( - ext_json - PREFIX json - GIT_REPOSITORY ${JSON_GIT_REPO_URL} - GIT_TAG ${JSON_GIT_LABEL} - # Disable install step - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - UPDATE_COMMAND "" - EXCLUDE_FROM_ALL TRUE - ) - -#------------------------------------------------------------------------------ - -ExternalProject_Get_Property(ext_json SOURCE_DIR) -add_library(libjson INTERFACE) -target_include_directories(libjson SYSTEM INTERFACE ${SOURCE_DIR}/include) -add_dependencies(libjson ext_json) diff --git a/ngraph/environment_variables.md b/ngraph/environment_variables.md index 11541f2..9164351 100644 --- a/ngraph/environment_variables.md +++ b/ngraph/environment_variables.md @@ -4,7 +4,6 @@ | ------------------------------------|:---:| --- | | NGRAPH_DISABLED_FUSIONS | | | NGRAPH_ENABLE_REPLACE_CHECK | | -| NGRAPH_ENABLE_SERIALIZE_TRACING | | | NGRAPH_ENABLE_TRACING | | | NGRAPH_ENABLE_VISUALIZE_TRACING | | | NGRAPH_FAIL_MATCH_AT | | @@ -14,7 +13,6 @@ | NGRAPH_PASS_ENABLES | | | NGRAPH_PROFILE_PASS_ENABLE | | | NGRAPH_PROVENANCE_ENABLE | | -| NGRAPH_SERIALIZER_OUTPUT_SHAPES | | | NGRAPH_VISUALIZE_EDGE_JUMP_DISTANCE | | | NGRAPH_VISUALIZE_EDGE_LABELS | | | NGRAPH_VISUALIZE_TRACING_FORMAT | | diff --git a/ngraph/licenses/json-license.txt b/ngraph/licenses/json-license.txt deleted file mode 100644 index 6bd5f8a..0000000 --- a/ngraph/licenses/json-license.txt +++ /dev/null @@ -1,24 +0,0 @@ -https://github.com/nlohmann/json/blob/develop/LICENSE.MIT - -MIT License - -Copyright (c) 2013-2018 Niels Lohmann - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - diff --git a/ngraph/python/setup.py b/ngraph/python/setup.py index 1e5ab2f..73e4f23 100644 --- a/ngraph/python/setup.py +++ b/ngraph/python/setup.py @@ -205,7 +205,6 @@ sources = [ "pyngraph/passes/regmodule_pyngraph_passes.cpp", "pyngraph/partial_shape.cpp", "pyngraph/pyngraph.cpp", - "pyngraph/serializer.cpp", "pyngraph/shape.cpp", "pyngraph/strides.cpp", "pyngraph/tensor_iterator_builder.cpp", diff --git a/ngraph/python/src/ngraph/impl/__init__.py b/ngraph/python/src/ngraph/impl/__init__.py index 314fcf0..283a8f6 100644 --- a/ngraph/python/src/ngraph/impl/__init__.py +++ b/ngraph/python/src/ngraph/impl/__init__.py @@ -46,5 +46,4 @@ from _pyngraph import AxisSet from _pyngraph import AxisVector from _pyngraph import Coordinate -from _pyngraph import serialize from _pyngraph import util diff --git a/ngraph/python/src/pyngraph/pyngraph.cpp b/ngraph/python/src/pyngraph/pyngraph.cpp index 712bc49..c522e38 100644 --- a/ngraph/python/src/pyngraph/pyngraph.cpp +++ b/ngraph/python/src/pyngraph/pyngraph.cpp @@ -34,7 +34,6 @@ #include "pyngraph/ops/util/regmodule_pyngraph_op_util.hpp" #include "pyngraph/partial_shape.hpp" #include "pyngraph/passes/regmodule_pyngraph_passes.hpp" -#include "pyngraph/serializer.hpp" #include "pyngraph/shape.hpp" #include "pyngraph/strides.hpp" #include "pyngraph/types/regmodule_pyngraph_types.hpp" @@ -59,7 +58,6 @@ PYBIND11_MODULE(_pyngraph, m) regclass_pyngraph_Coordinate(m); regmodule_pyngraph_types(m); regclass_pyngraph_Function(m); - regclass_pyngraph_Serializer(m); py::module m_op = m.def_submodule("op", "Package ngraph.impl.op that wraps ngraph::op"); regclass_pyngraph_op_Constant(m_op); regclass_pyngraph_op_Parameter(m_op); diff --git a/ngraph/python/src/pyngraph/serializer.cpp b/ngraph/python/src/pyngraph/serializer.cpp deleted file mode 100644 index a4ddff1..0000000 --- a/ngraph/python/src/pyngraph/serializer.cpp +++ /dev/null @@ -1,32 +0,0 @@ -//***************************************************************************** -// Copyright 2017-2020 Intel Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -//***************************************************************************** - -#include -#include -#include - -#include "ngraph/serializer.hpp" -#include "pyngraph/serializer.hpp" - -namespace py = pybind11; - -void regclass_pyngraph_Serializer(py::module m) -{ - m.def("serialize", - (std::string(*)(std::shared_ptr, size_t)) & ngraph::serialize, - py::arg(), - py::arg("indent") = 0); -} diff --git a/ngraph/python/src/pyngraph/serializer.hpp b/ngraph/python/src/pyngraph/serializer.hpp deleted file mode 100644 index d28fcfc..0000000 --- a/ngraph/python/src/pyngraph/serializer.hpp +++ /dev/null @@ -1,23 +0,0 @@ -//***************************************************************************** -// Copyright 2017-2020 Intel Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -//***************************************************************************** - -#pragma once - -#include - -namespace py = pybind11; - -void regclass_pyngraph_Serializer(py::module m); diff --git a/ngraph/python/tests/runtime.py b/ngraph/python/tests/runtime.py index f06a5e3..b4296b9 100644 --- a/ngraph/python/tests/runtime.py +++ b/ngraph/python/tests/runtime.py @@ -21,7 +21,7 @@ import numpy as np from openvino.inference_engine import IECore, IENetwork from ngraph.exceptions import UserInputError -from ngraph.impl import Function, Node, serialize +from ngraph.impl import Function, Node from ngraph.utils.types import NumericData import tests @@ -111,11 +111,3 @@ class Computation(object): request = self.executable_network.requests[0] request.infer(dict(zip(request._inputs_list, input_values))) return [blob.buffer for blob in request.output_blobs.values()] - - def serialize(self, indent: int = 0) -> str: - """Serialize function (compute graph) to a JSON string. - - :param indent: set indent of serialized output - :return: serialized model - """ - return serialize(self.function, indent) diff --git a/ngraph/src/ngraph/CMakeLists.txt b/ngraph/src/ngraph/CMakeLists.txt index 87d6412..3f546ce 100644 --- a/ngraph/src/ngraph/CMakeLists.txt +++ b/ngraph/src/ngraph/CMakeLists.txt @@ -543,12 +543,6 @@ set (SRC variant.hpp ) -if(NGRAPH_JSON_ENABLE) - list(APPEND SRC serializer.cpp serializer.hpp) -else() - list(APPEND SRC serializer_stub.cpp) -endif() - configure_file(version.in.hpp version.hpp) add_library(ngraph SHARED ${SRC}) @@ -558,10 +552,6 @@ set_target_properties(ngraph PROPERTIES C_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN ON) -if(NOT NGRAPH_JSON_ENABLE) - target_compile_definitions(ngraph PUBLIC NGRAPH_JSON_DISABLE) -endif() - target_link_libraries(ngraph PRIVATE openvino::itt) add_subdirectory(frontend) @@ -581,9 +571,6 @@ if(NGRAPH_LIB_VERSIONING_ENABLE) VERSION ${NGRAPH_VERSION} SOVERSION ${NGRAPH_API_VERSION}) endif() -if(NGRAPH_JSON_ENABLE) - target_link_libraries(ngraph PRIVATE libjson) -endif() target_compile_definitions(ngraph PUBLIC NGRAPH_VERSION="${NGRAPH_VERSION}") if (LINUX) diff --git a/ngraph/src/ngraph/serializer.cpp b/ngraph/src/ngraph/serializer.cpp deleted file mode 100644 index f9c9e37..0000000 --- a/ngraph/src/ngraph/serializer.cpp +++ /dev/null @@ -1,2731 +0,0 @@ -//***************************************************************************** -// Copyright 2017-2020 Intel Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -//***************************************************************************** - -#include -#include -#include -#include - -#include "ngraph/attribute_visitor.hpp" -#include "ngraph/cpio.hpp" -#include "ngraph/env_util.hpp" -#include "ngraph/factory.hpp" -#include "ngraph/file_util.hpp" -#include "ngraph/graph_util.hpp" -#include "ngraph/log.hpp" -#include "ngraph/ops.hpp" -#include "ngraph/provenance.hpp" -#include "ngraph/serializer.hpp" -#include "ngraph/util.hpp" -#include "nlohmann/json.hpp" - -using namespace ngraph; -using namespace std; -using json = nlohmann::json; -using const_data_callback_t = shared_ptr(const string&, const element::Type&, const Shape&); - -static json write_element_type(const ngraph::element::Type& n); -static element::Type read_element_type(json j); -static json write_partial_shape(const PartialShape& s); -static PartialShape read_partial_shape(json j); - -static bool s_serialize_output_shapes_enabled = getenv_bool("NGRAPH_SERIALIZER_OUTPUT_SHAPES"); - -void ngraph::set_serialize_output_shapes(bool enable) -{ - s_serialize_output_shapes_enabled = enable; -} - -bool ngraph::get_serialize_output_shapes() -{ - return s_serialize_output_shapes_enabled; -} - -namespace -{ - // This expands the op list in op_tbl.hpp into a list of enumerations that look like this: - // Abs, - // Acos, - // ... - enum class OP_TYPEID - { -#define VSUF0(NAME) NAME -#define VSUF1(NAME) NAME##_v1 -#define VSUF3(NAME) NAME##_v3 -#define NGRAPH_OP(NAME, NAMESPACE, VERSION) VSUF##VERSION(NAME), -#include "ngraph/op/op_version_tbl.hpp" -#undef NGRAPH_OP - UnknownOp - }; -} - -static OP_TYPEID get_typeid(const NodeTypeInfo& type_info) -{ - // This expands the op list in op_tbl.hpp into a list of enumerations that look like this: - // {Abs::type_info, OP_TYPEID::Abs}, - // {Acos::type_info, OP_TYPEID::Acos}, - // ... - static const map type_info_map{ -#define NGRAPH_OP(NAME, NAMESPACE, VERSION) \ - {NAMESPACE::NAME::type_info, OP_TYPEID::VSUF##VERSION(NAME)}, -#include "ngraph/op/op_version_tbl.hpp" -#undef NGRAPH_OP - }; - OP_TYPEID rc = OP_TYPEID::UnknownOp; - - auto it = type_info_map.find(type_info); - if (it != type_info_map.end()) - { - rc = it->second; - } - return rc; -} - -bool has_key(json j, const std::string& key) -{ - return j.count(key) != 0; -} - -template -T get_or_default(json j, const std::string& key, const T& default_value) -{ - return has_key(j, key) ? j.at(key).get() : default_value; -} - -class JSONAttributeSerializer : public AttributeVisitor -{ -public: - JSONAttributeSerializer(json& j) - : m_json(j) - { - } - void on_adapter(const std::string& name, ValueAccessor& adapter) override - { - NGRAPH_CHECK(false, "Adapter ", adapter.get_type_info().name, " is not handled"); - } - void on_adapter(const std::string& name, ValueAccessor& adapter) override - { - m_json[name] = adapter.get(); - } - void on_adapter(const std::string& name, ValueAccessor& adapter) override - { - m_json[name] = adapter.get(); - } - void on_adapter(const std::string& name, ValueAccessor& adapter) override - { - m_json[name] = adapter.get(); - } - void on_adapter(const std::string& name, ValueAccessor& adapter) override - { - m_json[name] = adapter.get(); - } - void on_adapter(const std::string& name, ValueAccessor>& adapter) override - { - m_json[name] = adapter.get(); - } - void on_adapter(const std::string& name, ValueAccessor>& adapter) override - { - m_json[name] = adapter.get(); - } - void on_adapter(const std::string& name, ValueAccessor>& adapter) override - { - m_json[name] = adapter.get(); - } - void on_adapter(const std::string& name, - ValueAccessor>& adapter) override - { - m_json[name] = adapter.get(); - } - -protected: - json& m_json; -}; - -class JSONSerializer -{ -public: - void set_indent(size_t indent) { m_indent = indent; } - void set_serialize_output_shapes(bool serialize_output_shapes) - { - m_serialize_output_shapes = serialize_output_shapes; - } - - void set_binary_constant_data(bool binary_constant_data) - { - m_binary_constant_data = binary_constant_data; - } - - json serialize_function(const Function& function); - json serialize_output(const Output& output); - json serialize_parameter_vector(const ParameterVector& parameters); - json serialize_output_vector(const OutputVector& output_vector); - json serialize_node(const Node& node); - json serialize_axis_set(const AxisSet& axis_set); - json serialize_tensor_iterator_input_description( - const std::shared_ptr&); - json serialize_tensor_iterator_output_description( - const std::shared_ptr&); - -protected: - size_t m_indent{0}; - bool m_serialize_output_shapes{false}; - bool m_binary_constant_data{false}; - json m_json_nodes; -}; - -class JSONAttributeDeserializer : public AttributeVisitor -{ -public: - JSONAttributeDeserializer(json& j) - : m_json(j) - { - } - void on_adapter(const std::string& name, ValueAccessor& adapter) override - { - NGRAPH_CHECK(false, "Adapter ", adapter.get_type_info().name, " is not handled"); - } - void on_adapter(const std::string& name, ValueAccessor& adapter) override - { - if (has_key(m_json, name)) - { - adapter.set(m_json.at(name).get()); - } - } - void on_adapter(const std::string& name, ValueAccessor& adapter) override - { - if (has_key(m_json, name)) - { - adapter.set(m_json.at(name).get()); - } - } - - void on_adapter(const std::string& name, ValueAccessor& adapter) override - { - if (has_key(m_json, name)) - { - adapter.set(m_json.at(name).get()); - } - } - void on_adapter(const std::string& name, ValueAccessor& adapter) override - { - if (has_key(m_json, name)) - { - adapter.set(m_json.at(name).get()); - } - } - - void on_adapter(const std::string& name, ValueAccessor>& adapter) override - { - if (has_key(m_json, name)) - { - adapter.set(m_json.at(name).get>()); - } - } - void on_adapter(const std::string& name, ValueAccessor>& adapter) override - { - if (has_key(m_json, name)) - { - adapter.set(m_json.at(name).get>()); - } - } - void on_adapter(const std::string& name, ValueAccessor>& adapter) override - { - if (has_key(m_json, name)) - { - adapter.set(m_json.at(name).get>()); - } - } - void on_adapter(const std::string& name, - ValueAccessor>& adapter) override - { - if (has_key(m_json, name)) - { - adapter.set(m_json.at(name).get>()); - } - } - -protected: - json& m_json; -}; - -class JSONDeserializer -{ -public: - void set_const_data_callback(function const_data_callback) - { - m_const_data_callback = const_data_callback; - } - - shared_ptr deserialize_function(json j); - Output deserialize_output(json j); - OutputVector deserialize_output_vector(json j); - ParameterVector deserialize_parameter_vector(json j); - shared_ptr deserialize_node_reference(json j); - shared_ptr deserialize_node(json j); - AxisSet deserialize_axis_set(json j); - shared_ptr - deserialize_tensor_iterator_input_description(json j); - shared_ptr - deserialize_tensor_iterator_output_description(json j); - -protected: - unordered_map> m_node_map; - unordered_map> m_function_map; - function m_const_data_callback; -}; - -static string - serialize(shared_ptr func, size_t indent, bool binary_constant_data); - -static json write_dimension(Dimension d) -{ - if (d.is_dynamic()) - { - return nullptr; - } - else - { - return d.get_length(); - } -} - -static Dimension read_dimension(json j) -{ - if (j.is_null()) - { - return Dimension::dynamic(); - } - else - { - return Dimension(static_cast(j)); - } -} - -static json write_partial_shape(const PartialShape& s) -{ - if (s.rank().is_dynamic()) - { - return nullptr; - } - else - { - std::vector vals(s.rank().get_length()); - for (size_t i = 0; i < vals.size(); i++) - { - vals[i] = write_dimension(s[i]); - } - return move(vals); - } -} - -static PartialShape read_partial_shape(json j) -{ - if (j.is_null()) - { - return PartialShape::dynamic(); - } - else - { - std::vector dims(j.size()); - for (size_t i = 0; i < j.size(); i++) - { - dims[i] = read_dimension(j[i]); - } - return PartialShape(dims); - } -} - -static json write_auto_broadcast(const op::AutoBroadcastSpec& autob) -{ - json j; - j["type"] = autob.m_type; - j["axis"] = autob.m_axis; - return j; -} - -static op::AutoBroadcastSpec - read_auto_broadcast(json js_node, - const std::string& attr, - const op::AutoBroadcastSpec& autob = op::AutoBroadcastSpec()) -{ - if (has_key(js_node, attr)) - { - json j = js_node[attr]; - return op::AutoBroadcastSpec(static_cast(j.at("type")), - j.at("axis").get()); - } - else - { - return autob; - } -} - -static op::PadType read_pad_type(json node_js) -{ - return has_key(node_js, "pad_type") ? static_cast(node_js.at("pad_type")) - : op::PadType::EXPLICIT; -} - -static op::PadMode read_pad_mode(json node_js) -{ - return has_key(node_js, "pad_mode") ? static_cast(node_js.at("pad_mode")) - : op::PadMode::CONSTANT; -} - -static json write_element_type(const ngraph::element::Type& n) -{ - json j; - j = n.c_type_string(); - return j; -} - -static element::Type read_element_type(json j) -{ - size_t bitwidth = 0; - bool is_real = false; - bool is_signed = false; - bool is_quantized = false; - string c_type_string = ""; - if (j.is_object()) - { - bitwidth = j.at("bitwidth").get(); - is_real = j.at("is_real").get(); - is_signed = j.at("is_signed").get(); - is_quantized = j.at("is_quantized").get(); - c_type_string = j.at("c_type_string").get(); - } - else - { - string c_type = j.get(); - for (const element::Type* t : element::Type::get_known_types()) - { - if (t->c_type_string() == c_type) - { - bitwidth = t->bitwidth(); - is_real = t->is_real(); - is_signed = t->is_signed(); - is_quantized = t->is_quantized(); - c_type_string = t->c_type_string(); - break; - } - } - } - return element::Type(bitwidth, is_real, is_signed, is_quantized, c_type_string); -} - -void ngraph::serialize(const string& path, shared_ptr func, size_t indent) -{ - ofstream out(path); - serialize(out, func, indent); -} - -void ngraph::serialize(ostream& out, shared_ptr func, size_t indent) -{ - out << ::serialize(func, indent, false); -} - -#if defined ENABLE_CPIO_FILE -static void serialize_to_cpio(ostream& out, shared_ptr func, size_t indent) -{ - string j = ::serialize(func, indent, true); - cpio::Writer writer(out); - writer.write(func->get_name(), j.c_str(), static_cast(j.size())); - - traverse_nodes(const_cast(func.get()), - [&](shared_ptr node) { - if (auto c = node->as_type()) - { - uint32_t size = - static_cast(shape_size(c->get_output_shape(0)) * - c->get_output_element_type(0).size()); - writer.write(c->get_name(), c->get_data_ptr(), size); - } - }, - true); -} -#endif - -static string serialize(shared_ptr func, size_t indent, bool binary_constant_data) -{ - JSONSerializer serializer; - serializer.set_binary_constant_data(binary_constant_data); - serializer.set_indent(indent); - serializer.set_serialize_output_shapes(s_serialize_output_shapes_enabled); - - json j; - j.push_back(serializer.serialize_function(*func)); - - string rc; - if (indent == 0) - { - rc = j.dump(); - } - else - { - rc = j.dump(static_cast(indent)); - } - return rc; -} - -std::string ngraph::serialize(std::shared_ptr func, size_t indent) -{ - return ::serialize(func, indent, false); -} - -shared_ptr ngraph::deserialize(istream& in) -{ - shared_ptr rc; - if (cpio::is_cpio(in)) - { - cpio::Reader reader(in); - vector file_info = reader.get_file_info(); - if (file_info.size() > 0) - { - // The first file is the model - uint32_t size = static_cast(file_info[0].get_size()); - char* data = new char[size]; - reader.read(file_info[0].get_name(), data, size); - string jstr(data, size); - delete[] data; - json js = json::parse(jstr); - JSONDeserializer deserializer; - deserializer.set_const_data_callback( - [&](const string& const_name, const element::Type& et, const Shape& shape) { - shared_ptr const_node; - for (const cpio::FileInfo& info : file_info) - { - if (info.get_name() == const_name) - { - void* const_data = ngraph_malloc(info.get_size()); - reader.read(const_name, const_data, info.get_size()); - const_node = make_shared(et, shape, const_data); - ngraph_free(const_data); - break; - } - } - return const_node; - }); - for (json func : js) - { - rc = deserializer.deserialize_function(func); - } - } - } - else - { - // json file? - std::stringstream ss; - ss << in.rdbuf(); - rc = deserialize(ss.str()); - } - return rc; -} - -shared_ptr ngraph::deserialize(const string& s) -{ - shared_ptr rc; - if (file_util::exists(s)) - { - // s is a file and not a json string - ifstream in(s, ios_base::binary | ios_base::in); - rc = deserialize(in); - } - else - { - json js = json::parse(s); - JSONDeserializer deserializer; - for (json func : js) - { - rc = deserializer.deserialize_function(func); - } - } - return rc; -} - -json JSONSerializer::serialize_parameter_vector(const ParameterVector& parameters) -{ - json json_parameters = json::array(); - for (auto param : parameters) - { - json_parameters.push_back(param->get_name()); - } - return json_parameters; -} - -json JSONSerializer::serialize_function(const Function& f) -{ - json function; - function["name"] = f.get_name(); - function["parameters"] = serialize_parameter_vector(f.get_parameters()); - - // TODO Functions can return multiple results - for (size_t i = 0; i < f.get_output_size(); ++i) - { - function["result"].push_back(f.get_output_op(i)->get_name()); - } - - json nodes; - for (shared_ptr node : f.get_ordered_ops()) - { - nodes.push_back(serialize_node(*node)); - } - - function["ops"] = nodes; - return function; -} - -template -T get_value(json js, const string& key) -{ - T rc = {}; - auto it = js.find(key); - if (it != js.end()) - { - rc = it->get(); - } - return rc; -} - -shared_ptr JSONDeserializer::deserialize_node_reference(json j) -{ - const string& name = j; - return m_node_map.at(name); -} - -Output JSONDeserializer::deserialize_output(json j) -{ - size_t index; - json json_node_reference; - if (j.is_string()) - { - json_node_reference = j; - index = 0; - } - else if (j.is_object()) - { - json_node_reference = j["node"]; - index = j["index"]; - } - else - { - throw ngraph_error("Expected string or object an output while deserializing"); - } - return Output(deserialize_node_reference(json_node_reference), index); -} - -OutputVector JSONDeserializer::deserialize_output_vector(json j) -{ - OutputVector result; - if (j.is_array()) - { - for (json jelt : j) - { - result.push_back(deserialize_output(jelt)); - } - } - return result; -} - -json JSONSerializer::serialize_axis_set(const AxisSet& axis_set) -{ - return static_cast>(axis_set); -} - -AxisSet JSONDeserializer::deserialize_axis_set(json j) -{ - AxisSet result; - if (j.is_array()) - { - result = j.get>(); - } - return result; -} - -json JSONSerializer::serialize_tensor_iterator_input_description( - const std::shared_ptr& input_description) -{ - json result; - if (auto slice = as_type_ptr(input_description)) - { - result["kind"] = "slice"; - result["input_index"] = slice->m_input_index; - result["body_parameter_index"] = slice->m_body_parameter_index; - result["start"] = slice->m_start; - result["stride"] = slice->m_stride; - result["part_size"] = slice->m_part_size; - result["end"] = slice->m_end; - result["axis"] = slice->m_axis; - } - else if (auto merged = - as_type_ptr(input_description)) - { - result["kind"] = "merged"; - result["input_index"] = merged->m_input_index; - result["body_parameter_index"] = merged->m_body_parameter_index; - result["body_value_index"] = merged->m_body_value_index; - } - else if (auto constant = - as_type_ptr(input_description)) - { - result["kind"] = "constant"; - result["input_index"] = constant->m_input_index; - result["body_parameter_index"] = constant->m_body_parameter_index; - } - else - { - NGRAPH_UNREACHABLE("Unknown input description type"); - } - return result; -} - -shared_ptr - JSONDeserializer::deserialize_tensor_iterator_input_description(json j) -{ - string kind = j["kind"]; - shared_ptr result; - if (kind == "slice") - { - uint64_t input_index = j["input_index"].get(); - uint64_t body_parameter_index = j["body_parameter_index"].get(); - int64_t start = j["start"].get(); - int64_t stride = j["stride"].get(); - uint64_t part_size = j["part_size"].get(); - int64_t end = j["end"].get(); - int64_t axis = j["axis"].get(); - result = make_shared( - input_index, body_parameter_index, start, stride, part_size, end, axis); - } - else if (kind == "merged") - { - uint64_t input_index = j["input_index"].get(); - uint64_t body_parameter_index = j["body_parameter_index"].get(); - uint64_t body_value_index = j["body_value_index"].get(); - result = make_shared( - input_index, body_parameter_index, body_value_index); - } - else if (kind == "constant") - { - uint64_t input_index = j["input_index"].get(); - uint64_t body_parameter_index = j["body_parameter_index"].get(); - result = make_shared(input_index, - body_parameter_index); - } - else - { - NGRAPH_UNREACHABLE("Unknown input description type: ", kind); - } - return result; -} - -json JSONSerializer::serialize_tensor_iterator_output_description( - const std::shared_ptr& output_description) -{ - json result; - if (auto concat = as_type_ptr(output_description)) - { - result["kind"] = "concat"; - result["body_value_index"] = concat->m_body_value_index; - result["output_index"] = concat->m_output_index; - result["start"] = concat->m_start; - result["stride"] = concat->m_stride; - result["part_size"] = concat->m_part_size; - result["end"] = concat->m_end; - result["axis"] = concat->m_axis; - } - else if (auto body_output = - as_type_ptr(output_description)) - { - result["kind"] = "body_output"; - result["body_value_index"] = body_output->m_body_value_index; - result["output_index"] = body_output->m_output_index; - result["iteration"] = body_output->m_iteration; - } - else - { - NGRAPH_UNREACHABLE("Unknown input description type"); - } - return result; -} - -std::shared_ptr - JSONDeserializer::deserialize_tensor_iterator_output_description(json j) -{ - string kind = j["kind"]; - shared_ptr result; - if (kind == "concat") - { - uint64_t body_value_index = j["body_value_index"].get(); - uint64_t output_index = j["output_index"].get(); - int64_t start = j["start"].get(); - int64_t stride = j["stride"].get(); - uint64_t part_size = j["part_size"].get(); - int64_t end = j["end"].get(); - int64_t axis = j["axis"].get(); - result = make_shared( - body_value_index, output_index, start, stride, part_size, end, axis); - } - else if (kind == "body_output") - { - uint64_t body_value_index = j["body_value_index"].get(); - uint64_t output_index = j["output_index"].get(); - int64_t iteration = j["iteration"].get(); - result = make_shared( - body_value_index, output_index, iteration); - } - else - { - NGRAPH_UNREACHABLE("Unknown input description type: ", kind); - } - return result; -} - -ParameterVector JSONDeserializer::deserialize_parameter_vector(json json_parameters) -{ - std::vector> params; - for (auto& param_ref : json_parameters) - { - params.push_back(as_type_ptr(deserialize_node_reference(param_ref))); - } - return params; -} - -shared_ptr JSONDeserializer::deserialize_function(json func_js) -{ - string func_name = func_js.at("name").get(); - vector func_result = func_js.at("result"); - for (json node_js : func_js.at("ops")) - { - deserialize_node(node_js); - } - - // This handles both graphs w/ `op::Result` and legacy graphs w/o it - // If we are dealing w/ a legacy graph, add op::Result for each output node - ResultVector result; - size_t results = 0; - for (auto& result_ref : func_result) - { - auto fr = deserialize_node_reference(result_ref); - if (auto res = as_type_ptr(fr)) - { - result.push_back(res); - // make sure we have `op::Result` on top of all outputs - results++; - } - else - { - result.push_back(std::make_shared(fr)); - } - } - - if (results != 0 && results != func_result.size()) - { - throw ngraph_error( - "Graph serialization is inconsistent. Some op::Results appear to be missing"); - } - - ParameterVector params = deserialize_parameter_vector(func_js.at("parameters")); - - shared_ptr rc{make_shared(result, params, func_name)}; - m_function_map[func_name] = rc; - return rc; -} - -// This helps with conversions to old-style shared-ptr and new-style Output& -// arguments to node constructors. Uses of OutputHelper should be replaced with Output -// when all op constructors use the new style arguments. -struct OutputHelper -{ - OutputHelper(const Output& output) - : m_output(output) - { - } - - operator shared_ptr() const { return get_output_element(m_output); } - operator const Output&() const { return m_output; } - Output m_output; -}; - -// This helps with conversions to old-style shared-ptr and new-style Output& -// arguments to node constructors. Uses of OutputVectorHelper should be replaced with OutputVector -// when all op constructors use the new style arguments. -struct OutputVectorHelper -{ - OutputVectorHelper(const OutputVector& output_vector) - : m_vector(output_vector) - { - } - OutputVectorHelper() = default; - OutputHelper operator[](size_t i) const { return OutputHelper(m_vector[i]); } - void push_back(const Output& output) { m_vector.push_back(output); } - size_t size() const { return m_vector.size(); } - operator vector>() const - { - vector> result; - for (auto& o : m_vector) - { - result.push_back(OutputHelper(o)); - } - return result; - } - operator const OutputVector&() const { return m_vector; } - OutputVector m_vector; -}; - -shared_ptr JSONDeserializer::deserialize_node(json node_js) -{ - auto& factory_registry = FactoryRegistry::get(); - shared_ptr node; - try - { - string node_op = node_js.at("op").get(); - size_t op_version = get_value(node_js, "op_version"); - Node::type_info_t type_info{node_op.c_str(), op_version}; - string node_name = node_js.at("name").get(); - string friendly_name = get_value(node_js, "friendly_name"); - vector control_deps_inputs = get_value>(node_js, "control_deps"); - vector node_outputs = get_value>(node_js, "outputs"); - OutputVectorHelper args(deserialize_output_vector(node_js["inputs"])); - if (has_key(node_js, "attribute_visitor")) - { - if (factory_registry.has_factory(type_info)) - { - node = shared_ptr(factory_registry.create(type_info)); - JSONAttributeDeserializer visitor(node_js); - node->set_arguments(static_cast(args)); - node->visit_attributes(visitor); - for (auto& control_dep : control_deps_inputs) - { - node->add_control_dependency(deserialize_node_reference(control_dep)); - } - - if (!friendly_name.empty()) - { - node->set_friendly_name(friendly_name); - } - else - { - node->set_friendly_name(node_name); - } - if (ngraph::get_provenance_enabled()) - { - std::vector prov_js = node_js.at("provenance_tags"); - for (auto prov_tag : prov_js) - { - node->add_provenance_tag(prov_tag); - } - } - node->constructor_validate_and_infer_types(); - m_node_map[node_name] = node; - return node; - } - } - -#if defined(__GNUC__) && !(__GNUC__ == 4 && __GNUC_MINOR__ == 8) -#pragma GCC diagnostic push -#pragma GCC diagnostic error "-Wswitch" -// #pragma GCC diagnostic error "-Wswitch-enum" -// #pragma GCC diagnostic error "-Wimplicit-fallthrough" -#endif - - switch (get_typeid(type_info)) - { - case OP_TYPEID::Abs: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Acos: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Add: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::Any: - { - auto reduction_axes = deserialize_axis_set(node_js.at("reduction_axes")); - node = make_shared(args[0], reduction_axes); - break; - } - case OP_TYPEID::Asin: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Atan: - { - node = make_shared(args[0]); - break; - } - - case OP_TYPEID::BatchNormInference: - { - auto epsilon = node_js.at("eps").get(); - // Odd order for back-compatibility - node = make_shared( - args[2], args[0], args[1], args[3], args[4], epsilon); - break; - } - case OP_TYPEID::Broadcast: - { - auto shape = node_js.at("shape").get>(); - auto axes = deserialize_axis_set(node_js.at("axes")); - node = make_shared(args[0], shape, axes); - break; - } - case OP_TYPEID::BroadcastLike: - { - auto initial_axes = deserialize_axis_set(node_js.at("initial_axes")); - node = make_shared(args[0], args[1], initial_axes); - break; - } - case OP_TYPEID::Ceiling: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Clamp: - { - const auto clamp_min = node_js.at("min").get(); - const auto clamp_max = node_js.at("max").get(); - node = make_shared(args[0], clamp_min, clamp_max); - break; - } - case OP_TYPEID::Concat: - { - auto axis = node_js.at("axis").get(); - node = make_shared(static_cast(args), axis); - break; - } - case OP_TYPEID::Constant: - { - auto type_node_js = - has_key(node_js, "element_type") ? node_js : node_js.at("value_type"); - auto element_type = read_element_type(type_node_js.at("element_type")); - auto shape = type_node_js.at("shape"); - auto value = node_js.at("value").get>(); - node = make_shared(element_type, shape, value); - break; - } - case OP_TYPEID::Convert: - { - auto target_type = read_element_type(node_js.at("target_type")); - node = make_shared(args[0], target_type); - break; - } - case OP_TYPEID::Cos: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Cosh: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::CumSum: - { - auto exclusive = node_js.at("exclusive"); - auto reverse = node_js.at("reverse"); - node = make_shared(args[0], args[1], exclusive, reverse); - break; - } - case OP_TYPEID::CTCGreedyDecoder: { break; - } - case OP_TYPEID::DeformableConvolution_v1: - { - const auto strides = node_js.at("strides").get>(); - const auto dilations = node_js.at("dilations").get>(); - const auto pads_begin = node_js.at("pads_begin").get>(); - const auto pads_end = node_js.at("pads_end").get>(); - const auto group = node_js.at("group").get(); - const auto deformable_group = node_js.at("deformable_group").get(); - - const op::PadType auto_pad = read_pad_type(node_js); - - node = make_shared(args[0], - args[1], - args[2], - strides, - pads_begin, - pads_end, - dilations, - auto_pad, - group, - deformable_group); - break; - } - case OP_TYPEID::DepthToSpace: - { - auto mode = node_js.at("mode").get(); - auto block_size = node_js.at("block_size").get(); - node = make_shared(args[0], mode, block_size); - break; - } - case OP_TYPEID::DetectionOutput: { break; - } - case OP_TYPEID::Dequantize: - { - auto type = read_element_type(node_js.at("type")); - auto axes = deserialize_axis_set(node_js.at("axes")); - node = make_shared(args[0], args[1], args[2], type, axes); - break; - } - case OP_TYPEID::Divide: - { - bool pythondiv = get_or_default(node_js, "pythondiv", true); - node = make_shared( - args[0], args[1], pythondiv, read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::Dot: - { - // For backwards compatibility, reduction_axes_count is optional. - if (has_key(node_js, "reduction_axes_count")) - { - size_t reduction_axes_count = node_js["reduction_axes_count"].get(); - node = make_shared(args[0], args[1], reduction_axes_count); - } - else - { - node = make_shared(args[0], args[1]); - } - break; - } - case OP_TYPEID::Elu: - { - auto alpha = node_js.at("alpha").get(); - node = make_shared(args[0], alpha); - break; - } - case OP_TYPEID::Equal: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::Erf: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Exp: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::FakeQuantize: - { - size_t levels = node_js.at("levels").get(); - node = - make_shared(args[0], args[1], args[2], args[3], args[4], levels); - break; - } - case OP_TYPEID::Floor: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Gather: - { - auto axis = node_js.at("axis").get(); - node = make_shared(args[0], args[1], axis); - break; - } - case OP_TYPEID::GatherND: - { - node = make_shared(args[0], args[1]); - break; - } - case OP_TYPEID::Gelu: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::GetOutputElement: - { - node = make_shared( - static_cast>(args[0]).get_node_shared_ptr(), - node_js.at("n").get()); - break; - } - case OP_TYPEID::Greater: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::GreaterEq: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::GRN: - { - auto bias = node_js.at("bias").get(); - node = make_shared(args[0], bias); - break; - } - case OP_TYPEID::HardSigmoid: - { - node = make_shared(args[0], args[1], args[2]); - break; - } - case OP_TYPEID::Less: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::LessEq: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::Log: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::LRN: - { - auto alpha = node_js.at("alpha").get(); - auto beta = node_js.at("beta").get(); - auto bias = node_js.at("bias").get(); - auto nsize = node_js.at("nsize").get(); - node = make_shared(args[0], args[1], alpha, beta, bias, nsize); - break; - } - case OP_TYPEID::LSTMCell: - { - auto hidden_size = node_js.at("hidden_size").get(); - auto weights_format = get_or_default( - node_js, "weights_format", op::LSTMWeightsFormat::IFCO); - auto clip = node_js.at("clip").get(); - auto activations = node_js.at("activations").get>(); - auto activations_alpha = node_js.at("activations_alpha").get>(); - auto activations_beta = node_js.at("activations_beta").get>(); - auto input_forget = node_js.at("input_forget").get(); - switch (args.size()) - { - case 7: - node = make_shared(args[0], - args[1], - args[2], - args[3], - args[4], - args[5], - args[6], - hidden_size, - weights_format, - activations, - activations_alpha, - activations_beta, - clip, - input_forget); - break; - case 6: - node = make_shared(args[0], - args[1], - args[2], - args[3], - args[4], - args[5], - hidden_size, - weights_format, - activations, - activations_alpha, - activations_beta, - clip, - input_forget); - break; - case 5: - node = make_shared(args[0], - args[1], - args[2], - args[3], - args[4], - hidden_size, - weights_format, - activations, - activations_alpha, - activations_beta, - clip, - input_forget); - break; - default: throw runtime_error("LSTMCell constructor not supported in serializer"); - } - break; - } - case OP_TYPEID::LSTMSequence: - { - auto hidden_size = node_js.at("hidden_size").get(); - auto clip = node_js.at("clip_threshold").get(); - auto activations = node_js.at("activations").get>(); - auto activations_alpha = node_js.at("activations_alpha").get>(); - auto activations_beta = node_js.at("activations_beta").get>(); - auto input_forget = node_js.at("input_forget").get(); - auto direction = node_js.at("direction").get(); - auto weights_format = get_or_default( - node_js, "weights_format", op::LSTMWeightsFormat::IFCO); - if (args.size() == 8) - { - node = make_shared(args[0], - args[1], - args[2], - args[3], - args[4], - args[5], - args[6], - args[7], - hidden_size, - direction, - weights_format, - activations_alpha, - activations_beta, - activations, - clip, - input_forget); - } - else - { - node = make_shared(args[0], - args[1], - args[2], - args[3], - args[4], - args[5], - args[6], - hidden_size, - direction, - weights_format, - activations_alpha, - activations_beta, - activations, - clip, - input_forget); - } - break; - } - case OP_TYPEID::MatMul: - { - bool transpose_a = node_js.at("transpose_a").get(); - bool transpose_b = node_js.at("transpose_b").get(); - node = make_shared(args[0], args[1], transpose_a, transpose_b); - break; - } - case OP_TYPEID::Max: - { - auto reduction_axes = deserialize_axis_set(node_js.at("reduction_axes")); - node = make_shared(args[0], reduction_axes); - break; - } - case OP_TYPEID::Maximum: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::Min: - { - auto reduction_axes = deserialize_axis_set(node_js.at("reduction_axes")); - node = make_shared(args[0], reduction_axes); - break; - } - case OP_TYPEID::Minimum: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::Multiply: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::MVN: - { - auto normalize_variance = node_js.at("normalize_variance").get(); - AxisSet reduction_axes = deserialize_axis_set(node_js.at("reduction_axes")); - auto eps = node_js.at("eps").get(); - if (reduction_axes.size() > 0) - { - node = make_shared(args[0], reduction_axes, normalize_variance, eps); - } - else - { - node = make_shared(args[0], true, normalize_variance, eps); - } - break; - } - case OP_TYPEID::Negative: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::NonZero_v3: - { - auto target_type = read_element_type(node_js.at("index_element_type")); - node = make_shared(args[0], target_type); - break; - } - case OP_TYPEID::NormalizeL2: - { - float eps = node_js.at("eps").get(); - auto eps_mode = node_js.at("eps_mode").get(); - node = make_shared(args[0], args[1], eps, eps_mode); - break; - } - case OP_TYPEID::NotEqual: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::Not: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::OneHot: - { - if (op_version == 0) - { - auto shape = node_js.at("shape").get>(); - auto one_hot_axis = node_js.at("one_hot_axis").get(); - node = - make_shared(args[0], read_partial_shape(shape), one_hot_axis); - } - if (op_version == 1) - { - auto axis = node_js.at("axis").get(); - node = make_shared(args[0], args[1], args[2], args[3], axis); - } - break; - } - case OP_TYPEID::Or: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::Pad: - { - auto padding_below = node_js.at("padding_below").get>(); - auto padding_above = node_js.at("padding_above").get>(); - - // This is a legacy field whose functionality is no longer supported. The new - // behavior is equivalent to interior padding of 0, so we will accept it under - // those conditions. - auto padding_interior = get_value>(node_js, "padding_interior"); - NGRAPH_CHECK(std::all_of(padding_interior.begin(), - padding_interior.end(), - [](size_t s) { return s == 0; }), - "Legacy padding_interior field must be zero everywhere."); - - auto pad_mode = read_pad_mode(node_js); - - node = - make_shared(args[0], args[1], padding_below, padding_above, pad_mode); - - break; - } - case OP_TYPEID::Parameter: - { - auto type_node_js = - has_key(node_js, "element_type") ? node_js : node_js.at("value_type"); - auto element_type = read_element_type(type_node_js.at("element_type")); - auto shape = type_node_js.at("shape"); - auto cacheable = get_or_default(node_js, "cacheable", false); - node = make_shared(element_type, read_partial_shape(shape), cacheable); - break; - } - case OP_TYPEID::Passthrough: - { - std::vector outputs_js = node_js.at("output_shapes"); - std::vector> outputs; - for (auto output_js : outputs_js) - { - outputs.emplace_back(read_element_type(output_js.at("element_type")), - read_partial_shape(output_js.at("shape"))); - } - node = make_shared(node_js.at("logical_type"), - node_js.at("language"), - node_js.at("function"), - static_cast(args), - std::move(outputs)); - break; - } - case OP_TYPEID::Power: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::PRelu: - { - node = make_shared(args[0], args[1]); - break; - } - case OP_TYPEID::Product: - { - set reduction_axes = - get_or_default>(node_js, "reduction_axes", set()); - if (reduction_axes.empty()) - { - node = make_shared(args[0], args[1]); - } - else - { - node = make_shared(args[0], reduction_axes); - } - break; - } - case OP_TYPEID::PSROIPooling: { break; - } - case OP_TYPEID::PriorBox: { break; - } - case OP_TYPEID::PriorBoxClustered: { break; - } - case OP_TYPEID::Proposal: { break; - } - - case OP_TYPEID::Quantize: - { - auto type = read_element_type(node_js.at("type")); - auto axes = deserialize_axis_set(node_js.at("axes")); - auto round_mode = node_js.at("round_mode").get(); - node = make_shared(args[0], args[1], args[2], type, axes, round_mode); - break; - } - case OP_TYPEID::QuantizedConvolution: - { - auto window_movement_strides = - node_js.at("window_movement_strides").get>(); - auto window_dilation_strides = - node_js.at("window_dilation_strides").get>(); - auto padding_below = node_js.at("padding_below").get>(); - auto padding_above = node_js.at("padding_above").get>(); - auto data_dilation_strides = node_js["data_dilation_strides"]; - auto output_type = read_element_type(node_js.at("output_type")); - auto input_axes = node_js.at("input_axes").get>(); - auto filter_axes = node_js.at("filter_axes").get>(); - auto output_axes = node_js.at("output_axes").get>(); - node = make_shared( - args[0], - args[1], - window_movement_strides, - window_dilation_strides, - padding_below, - padding_above, - data_dilation_strides.get>(), - args[2], - args[3], - args[4], - args[5], - args[6], - args[7], - output_type, - input_axes, - filter_axes, - output_axes); - - break; - } - case OP_TYPEID::QuantizedDot: - { - size_t reduction_axes_count = node_js["reduction_axes_count"].get(); - auto output_type = read_element_type(node_js.at("output_type")); - auto input0_axes = node_js.at("input0_axes").get>(); - auto input1_axes = node_js.at("input1_axes").get>(); - auto output_axes = node_js.at("output_axes").get>(); - - node = make_shared(args[0], - args[1], - reduction_axes_count, - args[2], - args[3], - args[4], - args[5], - args[6], - args[7], - output_type, - input0_axes, - input1_axes, - output_axes); - - break; - } - case OP_TYPEID::Range: - { - node = make_shared(args[0], args[1], args[2]); - break; - } - case OP_TYPEID::Relu: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::ReplaceSlice: - { - auto lower_bounds = node_js.at("lower_bounds").get>(); - auto upper_bounds = node_js.at("upper_bounds").get>(); - auto strides = node_js.at("strides").get>(); - node = make_shared( - args[0], args[1], lower_bounds, upper_bounds, strides); - break; - } - case OP_TYPEID::Reshape: - { - auto input_order = node_js.at("input_order").get>(); - auto output_shape = node_js.at("output_shape").get>(); - node = make_shared(args[0], input_order, output_shape); - break; - } - case OP_TYPEID::Result: - { - auto needs_default_layout = - get_or_default(node_js, "needs_default_layout", false); - node = make_shared(args[0], needs_default_layout); - break; - } - case OP_TYPEID::Reverse: - { - const auto reversed_axes = deserialize_axis_set(node_js.at("reversed_axes")); - node = make_shared(args[0], reversed_axes); - break; - } - case OP_TYPEID::ReverseSequence: - { - auto batch_axis = node_js.at("batch_axis").get(); - auto sequence_axis = node_js.at("sequence_axis").get(); - node = make_shared(args[0], args[1], batch_axis, sequence_axis); - break; - } - case OP_TYPEID::RNNCell: - { - auto hidden_size = node_js.at("hidden_size").get(); - auto clip = node_js.at("clip").get(); - auto activations = node_js.at("activations").get>(); - auto activation_alpha = node_js.at("activations_alpha").get>(); - auto activation_beta = node_js.at("activations_beta").get>(); - switch (args.size()) - { - case 4: - node = make_shared(args[0], - args[1], - args[2], - args[3], - hidden_size, - activations, - activation_alpha, - activation_beta, - clip); - break; - case 5: - node = make_shared(args[0], - args[1], - args[2], - args[3], - args[4], - hidden_size, - activations, - activation_alpha, - activation_beta, - clip); - break; - default: throw runtime_error("GRUCell constructor not supported in serializer"); - } - break; - } - case OP_TYPEID::ROIPooling: { break; - } - case OP_TYPEID::RegionYolo: { break; - } - case OP_TYPEID::ReorgYolo: - { - break; - const auto strides = node_js.at("strides").get>(); - node = make_shared(args[0], strides); - break; - } - case OP_TYPEID::Round: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Select: - { - node = make_shared(args[0], args[1], args[2]); - break; - } - case OP_TYPEID::Selu: - { - node = make_shared(args[0], args[1], args[2]); - break; - } - case OP_TYPEID::ShapeOf: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::ShuffleChannels: - { - const auto axis = node_js.at("axis").get(); - const auto groups = node_js.at("groups").get(); - node = make_shared(args[0], axis, groups); - break; - } - case OP_TYPEID::Sigmoid: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Sign: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Sin: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Sinh: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Slice: - { - auto lower_bounds = node_js.at("lower_bounds").get>(); - auto upper_bounds = node_js.at("upper_bounds").get>(); - auto strides = node_js.at("strides").get>(); - node = make_shared(args[0], lower_bounds, upper_bounds, strides); - break; - } - case OP_TYPEID::Softmax: - { - if (has_key(node_js, "softmax_axes")) - { - auto softmax_axes = deserialize_axis_set(node_js.at("softmax_axes")); - node = make_shared(args[0], softmax_axes); - } - else - { - node = make_shared(args[0], args[1]); - } - - break; - } - case OP_TYPEID::SpaceToDepth: - { - auto block_size = node_js.at("block_size").get(); - auto mode = node_js.at("mode").get(); - node = make_shared(args[0], mode, block_size); - break; - } - case OP_TYPEID::Split: - { - const auto splits = node_js.at("splits").get>(); - node = make_shared(args[0], args[1], splits); - break; - } - case OP_TYPEID::Sqrt: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::SquaredDifference: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::Squeeze: - { - node = make_shared(args[0], args[1]); - break; - } - case OP_TYPEID::Subtract: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - case OP_TYPEID::Sum: - { - set reduction_axes = - get_or_default>(node_js, "reduction_axes", set()); - if (reduction_axes.empty()) - { - node = make_shared(args[0], args[1]); - } - else - { - node = make_shared(args[0], reduction_axes); - } - break; - } - case OP_TYPEID::Tan: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Tanh: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::TensorIterator: - { - auto ti = make_shared(args); - json jbody = node_js["body"]; - // Serializer assumes inputs are available before users sp we - // need to make sure the body nodes are all deserialized before - // referencing them. - json jbody_nodes = jbody["nodes"]; - NodeVector body_nodes; - for (json jnode : jbody_nodes) - { - body_nodes.push_back(deserialize_node(jnode)); - } - json jparams = jbody["parameters"]; - ParameterVector parameters; - for (json jparam : jparams) - { - parameters.push_back(as_type_ptr(deserialize_node(jparam))); - } - json jresults = jbody["results"]; - ResultVector results; - for (json jresult : jresults) - { - results.push_back(as_type_ptr(deserialize_node(jresult))); - } - ti->set_body(make_shared(results, parameters)); - json jins = node_js["input_descriptions"]; - for (json jin : jins) - { - ti->get_input_descriptions().push_back( - deserialize_tensor_iterator_input_description(jin)); - } - json jouts = node_js["output_descriptions"]; - for (json jout : jouts) - { - ti->get_output_descriptions().push_back( - deserialize_tensor_iterator_output_description(jout)); - } - ti->set_output_size(ti->get_output_descriptions().size()); - - node = ti; - break; - } - - case OP_TYPEID::Tile: - { - node = make_shared(args[0], args[1]); - break; - } - case OP_TYPEID::TopK: - { - auto compute_max = node_js.at("compute_max").get(); - auto target_type = read_element_type(node_js.at("index_element_type")); - op::TopKSortType sort = - get_or_default(node_js, "sort", op::TopKSortType::SORT_VALUES); - if (has_key(node_js, "top_k_axis")) - { - auto top_k_axis = node_js.at("top_k_axis").get(); - if (has_key(node_js, "k")) - { - auto k = node_js.at("k").get(); - node = make_shared( - args[0], top_k_axis, target_type, k, compute_max, sort); - } - else - { - node = make_shared( - args[0], args[1], top_k_axis, target_type, compute_max, sort); - } - } - else - { - node = make_shared( - args[0], args[1], args[2], target_type, compute_max, sort); - } - break; - } - case OP_TYPEID::StopGradient: - { - node = make_shared(args[0]); - break; - } - case OP_TYPEID::Unsqueeze: - { - node = make_shared(args[0], args[1]); - break; - } - case OP_TYPEID::Xor: - { - node = make_shared( - args[0], args[1], read_auto_broadcast(node_js, "auto_broadcast")); - break; - } - default: - { - stringstream ss; - ss << "unsupported op " << type_info.name << ":" << type_info.version; - throw runtime_error(ss.str()); - } - } -#if defined(__GNUC__) && !(__GNUC__ == 4 && __GNUC_MINOR__ == 8) -#pragma GCC diagnostic pop -#endif - - for (auto& control_dep : control_deps_inputs) - { - node->add_control_dependency(deserialize_node_reference(control_dep)); - } - - if (!friendly_name.empty()) - { - node->set_friendly_name(friendly_name); - } - else - { - node->set_friendly_name(node_name); - } - if (ngraph::get_provenance_enabled()) - { - if (has_key(node_js, "provenance_tags")) - { - const std::vector prov_js = node_js.at("provenance_tags"); - for (auto prov_tag : prov_js) - { - node->add_provenance_tag(prov_tag); - } - } - } - m_node_map[node_name] = node; - } - catch (exception& err) - { - NGRAPH_INFO << err.what(); - string node_name; - auto it = node_js.find("name"); - if (it != node_js.end()) - { - node_name = it->get(); - } - else - { - node_name = "UNKNOWN"; - } - throw runtime_error("Error parsing json at node '" + node_name + "'"); - } - return node; -} - -json JSONSerializer::serialize_output(const Output& output) -{ - json result; - auto index = output.get_index(); - json json_node_reference = output.get_node()->get_name(); - if (index == 0) - { - result = json_node_reference; - } - else - { - result["node"] = json_node_reference; - result["index"] = index; - } - return result; -} - -json JSONSerializer::serialize_output_vector(const OutputVector& output_vector) -{ - json result; - for (const Output& output : output_vector) - { - result.push_back(serialize_output(output)); - } - return result; -} - -json JSONSerializer::serialize_node(const Node& n) -{ - const NodeTypeInfo& type_info = n.get_type_info(); - json jtype_info = json::object(); - jtype_info["name"] = type_info.name; - jtype_info["version"] = type_info.version; - json node; - node["type_info"] = jtype_info; - node["name"] = n.get_name(); - auto op_version = n.get_version(); - node["op_version"] = op_version; - - if (n.get_name() != n.get_friendly_name()) - { - node["friendly_name"] = n.get_friendly_name(); - } - node["op"] = type_info.name; - // TODO Multiple outputs - json inputs = json::array(); - json control_deps = json::array(); - json outputs = json::array(); - - for (auto& input : n.inputs()) - { - inputs.push_back(serialize_output(input.get_source_output())); - } - for (auto cdep : n.get_control_dependencies()) - { - control_deps.push_back(cdep->get_name()); - } - for (auto& output : n.outputs()) - { - outputs.push_back(output.get_tensor().get_name()); - } - - if (!inputs.empty()) - { - node["inputs"] = inputs; - } - if (!control_deps.empty()) - { - node["control_deps"] = control_deps; - } - if (!outputs.empty()) - { - node["outputs"] = outputs; - } - - if (s_serialize_output_shapes_enabled) - { - json output_shapes = json::array(); - for (size_t i = 0; i < n.get_output_size(); ++i) - { - output_shapes.push_back(n.get_output_shape(i)); - } - node["output_shapes"] = output_shapes; - } - if (ngraph::get_provenance_enabled()) - { - json provenance_tags = json::array(); - for (auto prov_tag : n.get_provenance_tags()) - { - provenance_tags.push_back(prov_tag); - } - node["provenance_tags"] = provenance_tags; - } -#if !(defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 8)) -#pragma GCC diagnostic push -#pragma GCC diagnostic error "-Wswitch" -// #pragma GCC diagnostic error "-Wswitch-enum" -// #pragma GCC diagnostic error "-Wimplicit-fallthrough" -#endif - switch (get_typeid(type_info)) - { - case OP_TYPEID::Abs: { break; - } - case OP_TYPEID::Acos: { break; - } - case OP_TYPEID::Add: - { - const op::util::BinaryElementwiseArithmetic* tmp = nullptr; - tmp = static_cast(&n); - if (tmp != nullptr && tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::Any: - { - auto tmp = static_cast(&n); - node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes()); - break; - } - case OP_TYPEID::Asin: { break; - } - case OP_TYPEID::Atan: { break; - } - case OP_TYPEID::BatchNormInference: - { - auto tmp = static_cast(&n); - node["eps"] = tmp->get_eps_value(); - break; - } - case OP_TYPEID::Broadcast: - { - auto tmp = dynamic_cast(&n); - node["axes"] = serialize_axis_set(tmp->get_broadcast_axes()); - node["shape"] = tmp->get_broadcast_shape(); - break; - } - case OP_TYPEID::BroadcastLike: - { - auto tmp = static_cast(&n); - node["initial_axes"] = serialize_axis_set(tmp->get_initial_broadcast_axes()); - break; - } - case OP_TYPEID::Ceiling: { break; - } - case OP_TYPEID::Clamp: - { - auto tmp = static_cast(&n); - node["min"] = tmp->get_min(); - node["max"] = tmp->get_max(); - break; - } - case OP_TYPEID::Concat: - { - auto tmp = static_cast(&n); - node["axis"] = tmp->get_concatenation_axis(); - break; - } - case OP_TYPEID::Constant: - { - auto tmp = static_cast(&n); - if (tmp->get_all_data_elements_bitwise_identical() && shape_size(tmp->get_shape()) > 0) - { - vector vs; - vs.push_back(tmp->convert_value_to_string(0)); - node["value"] = vs; - } - else - { - node["value"] = tmp->get_value_strings(); - } - node["shape"] = tmp->get_shape(); - node["element_type"] = write_element_type(tmp->get_element_type()); - break; - } - case OP_TYPEID::Convert: - { - auto tmp = static_cast(&n); - node["target_type"] = write_element_type(tmp->get_convert_element_type()); - break; - } - case OP_TYPEID::Cos: { break; - } - case OP_TYPEID::Cosh: { break; - } - case OP_TYPEID::CumSum: - { - auto tmp = static_cast(&n); - node["exclusive"] = tmp->is_exclusive(); - node["reverse"] = tmp->is_reverse(); - break; - } - case OP_TYPEID::CTCGreedyDecoder: { break; - } - case OP_TYPEID::DetectionOutput: { break; - } - case OP_TYPEID::PSROIPooling: { break; - } - case OP_TYPEID::PriorBox: { break; - } - case OP_TYPEID::PriorBoxClustered: { break; - } - case OP_TYPEID::Proposal: { break; - } - case OP_TYPEID::ROIPooling: { break; - } - case OP_TYPEID::RegionYolo: { break; - } - case OP_TYPEID::ReorgYolo: - { - auto tmp = static_cast(&n); - node["strides"] = tmp->get_strides(); - break; - } - case OP_TYPEID::Round: { break; - } - case OP_TYPEID::Dequantize: - { - auto tmp = static_cast(&n); - node["type"] = write_element_type(tmp->get_element_type()); - node["axes"] = serialize_axis_set(tmp->get_axes()); - break; - } - case OP_TYPEID::DepthToSpace: - { - auto tmp = static_cast(&n); - node["type"] = write_element_type(tmp->get_element_type()); - node["mode"] = tmp->get_mode(); - node["block_size"] = tmp->get_block_size(); - break; - } - case OP_TYPEID::Divide: - { - const op::util::BinaryElementwiseArithmetic* bea_node = nullptr; - auto tmp = static_cast(&n); - bea_node = tmp; - node["pythondiv"] = tmp->is_pythondiv(); - if (bea_node != nullptr && bea_node->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(bea_node->get_autob()); - } - break; - } - case OP_TYPEID::Dot: - { - auto tmp = static_cast(&n); - node["reduction_axes_count"] = tmp->get_reduction_axes_count(); - break; - } - case OP_TYPEID::Elu: - { - auto tmp = static_cast(&n); - node["alpha"] = tmp->get_alpha(); - break; - } - case OP_TYPEID::Equal: - { - const op::util::BinaryElementwiseComparison* tmp = nullptr; - tmp = static_cast(&n); - if (tmp != nullptr && tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::Erf: { break; - } - case OP_TYPEID::Exp: { break; - } - case OP_TYPEID::FakeQuantize: - { - auto tmp = static_cast(&n); - node["levels"] = tmp->get_levels(); - break; - } - case OP_TYPEID::Floor: { break; - } - case OP_TYPEID::Gather: - { - auto tmp = static_cast(&n); - node["axis"] = tmp->get_axis(); - break; - } - case OP_TYPEID::GatherND: { break; - } - case OP_TYPEID::GetOutputElement: - { - auto tmp = static_cast(&n); - node["n"] = tmp->get_n(); - break; - } - case OP_TYPEID::Gelu: { break; - } - case OP_TYPEID::Greater: - { - const op::util::BinaryElementwiseComparison* tmp = nullptr; - tmp = static_cast(&n); - if (tmp != nullptr && tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::GreaterEq: - { - const op::util::BinaryElementwiseComparison* tmp = nullptr; - tmp = static_cast(&n); - if (tmp != nullptr && tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::GRN: - { - auto tmp = static_cast(&n); - node["bias"] = tmp->get_bias(); - break; - } - case OP_TYPEID::HardSigmoid: { break; - } - case OP_TYPEID::Less: - { - const op::util::BinaryElementwiseComparison* tmp = nullptr; - tmp = static_cast(&n); - if (tmp != nullptr && tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::LessEq: - { - auto tmp = static_cast(&n); - if (tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::Log: { break; - } - case OP_TYPEID::LRN: - { - auto tmp = static_cast(&n); - node["alpha"] = tmp->get_alpha(); - node["beta"] = tmp->get_beta(); - node["bias"] = tmp->get_bias(); - node["nsize"] = tmp->get_nsize(); - break; - } - case OP_TYPEID::LSTMCell: - { - auto tmp = static_cast(&n); - node["hidden_size"] = tmp->get_hidden_size(); - node["weights_format"] = tmp->get_weights_format(); - node["clip"] = tmp->get_clip(); - node["activations"] = tmp->get_activations(); - node["activations_alpha"] = tmp->get_activations_alpha(); - node["activations_beta"] = tmp->get_activations_beta(); - node["input_forget"] = tmp->get_input_forget(); - break; - } - case OP_TYPEID::LSTMSequence: - { - auto tmp = dynamic_cast(&n); - node["direction"] = tmp->get_direction(); - node["hidden_size"] = tmp->get_hidden_size(); - node["weights_format"] = tmp->get_weights_format(); - node["clip_threshold"] = tmp->get_clip_threshold(); - node["activations"] = tmp->get_activations(); - node["activations_alpha"] = tmp->get_activations_alpha(); - node["activations_beta"] = tmp->get_activations_beta(); - node["input_forget"] = tmp->get_input_forget(); - break; - } - case OP_TYPEID::MatMul: - { - auto tmp = static_cast(&n); - node["transpose_a"] = tmp->get_transpose_a(); - node["transpose_b"] = tmp->get_transpose_b(); - break; - } - case OP_TYPEID::Max: - { - auto tmp = static_cast(&n); - node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes()); - break; - } - case OP_TYPEID::Maximum: - { - const op::util::BinaryElementwiseArithmetic* tmp = nullptr; - tmp = static_cast(&n); - if (tmp != nullptr && tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::Min: - { - auto tmp = static_cast(&n); - node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes()); - break; - } - case OP_TYPEID::Minimum: - { - const op::util::BinaryElementwiseArithmetic* tmp = nullptr; - tmp = static_cast(&n); - if (tmp != nullptr && tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::Multiply: - { - const op::util::BinaryElementwiseArithmetic* tmp = nullptr; - tmp = static_cast(&n); - if (tmp != nullptr && tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::MVN: - { - auto tmp = static_cast(&n); - node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes()); - node["normalize_variance"] = tmp->get_normalize_variance(); - node["eps"] = tmp->get_eps(); - break; - } - case OP_TYPEID::Negative: { break; - } - case OP_TYPEID::NormalizeL2: - { - auto tmp = static_cast(&n); - node["eps"] = tmp->get_eps(); - node["eps_mode"] = tmp->get_eps_mode(); - break; - } - case OP_TYPEID::NotEqual: - { - const op::util::BinaryElementwiseComparison* tmp = nullptr; - tmp = static_cast(&n); - if (tmp != nullptr && tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::Not: { break; - } - case OP_TYPEID::OneHot: - { - if (op_version == 0) - { - auto tmp = static_cast(&n); - node["shape"] = write_partial_shape(tmp->get_output_partial_shape(0)); - node["one_hot_axis"] = tmp->get_one_hot_axis(); - } - if (op_version == 1) - { - auto tmp = static_cast(&n); - node["axis"] = tmp->get_axis(); - } - break; - } - case OP_TYPEID::Or: - { - auto tmp = static_cast(&n); - if (tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::Pad: - { - auto tmp = static_cast(&n); - node["padding_below"] = tmp->get_padding_below(); - node["padding_above"] = tmp->get_padding_above(); - node["pad_mode"] = tmp->get_pad_mode(); - break; - } - case OP_TYPEID::Parameter: - { - auto tmp = static_cast(&n); - node["shape"] = write_partial_shape(tmp->get_output_partial_shape(0)); - node["cacheable"] = tmp->get_cacheable(); - node["element_type"] = write_element_type(tmp->get_element_type()); - break; - } - case OP_TYPEID::Passthrough: - { - auto tmp = static_cast(&n); - node["logical_type"] = tmp->logical_type(); - node["language"] = tmp->language(); - node["function"] = tmp->function(); - std::vector outputs_js; - for (const auto& output_shape : tmp->output_shapes()) - { - json output_js; - output_js["element_type"] = write_element_type(std::get<0>(output_shape)); - output_js["shape"] = write_partial_shape(std::get<1>(output_shape)); - outputs_js.emplace_back(std::move(output_js)); - } - node["output_shapes"] = std::move(outputs_js); - break; - } - case OP_TYPEID::PRelu: { break; - } - case OP_TYPEID::Product: - { - auto tmp = static_cast(&n); - node["reduction_axes"] = tmp->get_reduction_axes(); - break; - } - case OP_TYPEID::Power: - { - const op::util::BinaryElementwiseArithmetic* tmp = nullptr; - tmp = static_cast(&n); - if (tmp != nullptr && tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::Quantize: - { - auto tmp = static_cast(&n); - node["type"] = write_element_type(tmp->get_element_type()); - node["axes"] = serialize_axis_set(tmp->get_axes()); - node["round_mode"] = tmp->get_round_mode(); - break; - } - case OP_TYPEID::QuantizedConvolution: - { - auto tmp = static_cast(&n); - node["window_movement_strides"] = tmp->get_window_movement_strides(); - node["window_dilation_strides"] = tmp->get_window_dilation_strides(); - node["padding_below"] = tmp->get_padding_below(); - node["padding_above"] = tmp->get_padding_above(); - node["data_dilation_strides"] = tmp->get_data_dilation_strides(); - node["output_type"] = write_element_type(tmp->get_element_type()); - node["input_axes"] = tmp->get_input_axes(); - node["filter_axes"] = tmp->get_filter_axes(); - node["output_axes"] = tmp->get_output_axes(); - break; - } - case OP_TYPEID::QuantizedDot: - { - auto tmp = static_cast(&n); - node["reduction_axes_count"] = tmp->get_reduction_axes_count(); - node["output_type"] = write_element_type(tmp->get_element_type()); - node["input0_axes"] = tmp->get_input0_axes(); - node["input1_axes"] = tmp->get_input1_axes(); - node["output_axes"] = tmp->get_output_axes(); - break; - } - case OP_TYPEID::Range: { break; - } - case OP_TYPEID::Relu: { break; - } - case OP_TYPEID::ReplaceSlice: - { - auto tmp = static_cast(&n); - node["lower_bounds"] = tmp->get_lower_bounds(); - node["upper_bounds"] = tmp->get_upper_bounds(); - node["strides"] = tmp->get_strides(); - break; - } - case OP_TYPEID::Reshape: - { - auto tmp = static_cast(&n); - node["input_order"] = tmp->get_input_order(); - node["output_shape"] = tmp->get_output_shape(0); - break; - } - case OP_TYPEID::Result: - { - auto tmp = static_cast(&n); - node["needs_default_layout"] = tmp->needs_default_layout(); - break; - } - case OP_TYPEID::Reverse: - { - const auto tmp = static_cast(&n); - node["reversed_axes"] = serialize_axis_set(tmp->get_reversed_axes()); - break; - } - case OP_TYPEID::ReverseSequence: - { - auto tmp = static_cast(&n); - node["batch_axis"] = tmp->get_origin_batch_axis(); - node["sequence_axis"] = tmp->get_origin_sequence_axis(); - break; - } - case OP_TYPEID::RNNCell: - { - auto tmp = static_cast(&n); - node["hidden_size"] = tmp->get_hidden_size(); - node["clip"] = tmp->get_clip(); - node["activations"] = tmp->get_activations(); - node["activations_alpha"] = tmp->get_activations_alpha(); - node["activations_beta"] = tmp->get_activations_beta(); - break; - } - case OP_TYPEID::Select: { break; - } - case OP_TYPEID::Selu: { break; - } - case OP_TYPEID::ShapeOf: { break; - } - case OP_TYPEID::ShuffleChannels: - { - const auto tmp = static_cast(&n); - node["axis"] = tmp->get_axis(); - node["groups"] = tmp->get_group(); - break; - } - case OP_TYPEID::Sigmoid: { break; - } - case OP_TYPEID::Sign: { break; - } - case OP_TYPEID::Sin: { break; - } - case OP_TYPEID::Sinh: { break; - } - case OP_TYPEID::Slice: - { - auto tmp = static_cast(&n); - node["lower_bounds"] = tmp->get_lower_bounds(); - node["upper_bounds"] = tmp->get_upper_bounds(); - node["strides"] = tmp->get_strides(); - break; - } - case OP_TYPEID::SpaceToDepth: - { - auto tmp = static_cast(&n); - node["type"] = write_element_type(tmp->get_element_type()); - node["mode"] = tmp->get_mode(); - node["block_size"] = tmp->get_block_size(); - break; - } - case OP_TYPEID::Split: - { - const auto tmp = static_cast(&n); - node["splits"] = tmp->get_splits(); - break; - } - case OP_TYPEID::Sqrt: { break; - } - case OP_TYPEID::SquaredDifference: - { - auto tmp = static_cast(&n); - if (tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::Squeeze: { break; - } - case OP_TYPEID::StopGradient: { break; - } - case OP_TYPEID::Subtract: - { - auto tmp = static_cast(&n); - if (tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::Sum: - { - auto tmp = static_cast(&n); - node["reduction_axes"] = tmp->get_reduction_axes(); - break; - } - case OP_TYPEID::Softmax: { break; - } - case OP_TYPEID::Tan: { break; - } - case OP_TYPEID::Tanh: { break; - } - case OP_TYPEID::TensorIterator: - { - auto tmp = static_cast(&n); - json body = json::object(); - { - auto& body_results = tmp->get_body()->get_results(); - // Serializer assumes node inputs are already serialized, so - // we need to capture the body-referenced nodes here. - json body_nodes = json::array(); - for (auto n : topological_sort(body_results)) - { - body_nodes.push_back(serialize_node(*n)); - } - body["nodes"] = body_nodes; - json params = json::array(); - for (auto param : tmp->get_body()->get_parameters()) - { - params.push_back(serialize_node(*param)); - } - body["parameters"] = params; - json results = json::array(); - for (auto result : body_results) - { - results.push_back(serialize_node(*result)); - } - body["results"] = results; - } - node["body"] = body; - json ins = json::array(); - for (auto in : tmp->get_input_descriptions()) - { - ins.push_back(serialize_tensor_iterator_input_description(in)); - } - node["input_descriptions"] = ins; - json outs = json::array(); - for (auto out : tmp->get_output_descriptions()) - { - outs.push_back(serialize_tensor_iterator_output_description(out)); - } - node["output_descriptions"] = outs; - break; - } - case OP_TYPEID::Tile: { break; - } - case OP_TYPEID::TopK: - { - const auto tmp = static_cast(&n); - node["index_element_type"] = write_element_type(tmp->get_index_element_type()); - node["compute_max"] = tmp->get_compute_max(); - node["sort"] = tmp->get_sort(); - switch (tmp->inputs().size()) - { - case 1: - node["k"] = tmp->get_k(); - node["top_k_axis"] = tmp->get_top_k_axis(); - break; - case 2: node["top_k_axis"] = tmp->get_top_k_axis(); break; - case 3: break; - default: throw runtime_error("TopK constructor not supported in serializer"); - } - break; - } - case OP_TYPEID::Unsqueeze: { break; - } - case OP_TYPEID::Xor: - { - auto tmp = static_cast(&n); - if (tmp->get_autob().m_type != op::AutoBroadcastType::NONE) - { - node["auto_broadcast"] = write_auto_broadcast(tmp->get_autob()); - } - break; - } - case OP_TYPEID::UnknownOp: { break; - } - default: - { - auto& factory_registry = FactoryRegistry::get(); - if (factory_registry.has_factory(type_info)) - { - node["attribute_visitor"] = true; - JSONAttributeSerializer visitor(node); - if (!const_cast(n).visit_attributes(visitor)) - { - NGRAPH_ERR << "Cannot serialize: " << node; - } - return node; - } - } - } -#if !(defined(__GNUC__) && (__GNUC__ == 4 && __GNUC_MINOR__ == 8)) -#pragma GCC diagnostic pop -#endif - return node; -} diff --git a/ngraph/src/ngraph/serializer.hpp b/ngraph/src/ngraph/serializer.hpp deleted file mode 100644 index 344c259..0000000 --- a/ngraph/src/ngraph/serializer.hpp +++ /dev/null @@ -1,89 +0,0 @@ -//***************************************************************************** -// Copyright 2017-2020 Intel Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -//***************************************************************************** - -#pragma once - -#include - -#include "ngraph/function.hpp" -#include "ngraph/node.hpp" - -namespace ngraph -{ - /// \brief Serialize a Function to a json string - /// \param func The Function to serialize - /// \param indent If 0 then there is no formatting applied and the resulting string is the - /// most compact representation. If non-zero then the json string is formatted with the - /// indent level specified. - NGRAPH_API - std::string serialize(std::shared_ptr func, size_t indent = 0); - - /// \brief Serialize a Function to a json file - /// \param path The path to the output file - /// \param func The Function to serialize - /// \param indent If 0 then there is no formatting applied and the resulting string is the - /// most compact representation. If non-zero then the json string is formatted with the - /// indent level specified. - NGRAPH_API - void serialize(const std::string& path, - std::shared_ptr func, - size_t indent = 0); - - /// \brief Serialize a Function to a json stream - /// \param out The output stream to which the data is serialized. - /// \param func The Function to serialize - /// \param indent If 0 then there is no formatting applied and the json is the - /// most compact representation. If non-zero then the json is formatted with the - /// indent level specified. - NGRAPH_API - void serialize(std::ostream& out, std::shared_ptr func, size_t indent = 0); - - /// \brief Deserialize a Function - /// \param in An isteam to the input data - NGRAPH_API - std::shared_ptr deserialize(std::istream& in); - - /// \brief Deserialize a Function - /// \param str The json formatted string to deseriailze. - NGRAPH_API - std::shared_ptr deserialize(const std::string& str); - - /// \brief If enabled adds output shapes to the serialized graph - /// \param enable Set to true to enable or false otherwise - /// - /// Option may be enabled by setting the environment variable NGRAPH_SERIALIZER_OUTPUT_SHAPES - NGRAPH_API - void set_serialize_output_shapes(bool enable); - NGRAPH_API - bool get_serialize_output_shapes(); - - class WithSerializeOutputShapesEnabled - { - public: - WithSerializeOutputShapesEnabled(bool enabled = true) - { - m_serialize_output_shapes_enabled = get_serialize_output_shapes(); - set_serialize_output_shapes(enabled); - } - ~WithSerializeOutputShapesEnabled() - { - set_serialize_output_shapes(m_serialize_output_shapes_enabled); - } - - private: - bool m_serialize_output_shapes_enabled; - }; -} diff --git a/ngraph/src/ngraph/serializer_stub.cpp b/ngraph/src/ngraph/serializer_stub.cpp deleted file mode 100644 index fb348c3..0000000 --- a/ngraph/src/ngraph/serializer_stub.cpp +++ /dev/null @@ -1,49 +0,0 @@ -//***************************************************************************** -// Copyright 2017-2020 Intel Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -//***************************************************************************** - -#include "ngraph/serializer.hpp" - -std::string ngraph::serialize(std::shared_ptr func, size_t indent) -{ - throw std::runtime_error("serializer disabled in build"); -} - -void ngraph::serialize(const std::string& path, - std::shared_ptr func, - size_t indent) -{ - throw std::runtime_error("serializer disabled in build"); -} - -void ngraph::serialize(std::ostream& out, std::shared_ptr func, size_t indent) -{ - throw std::runtime_error("serializer disabled in build"); -} - -std::shared_ptr ngraph::deserialize(std::istream& in) -{ - throw std::runtime_error("serializer disabled in build"); -} - -std::shared_ptr ngraph::deserialize(const std::string& str) -{ - throw std::runtime_error("serializer disabled in build"); -} - -void ngraph::set_serialize_output_shapes(bool enable) -{ - throw std::runtime_error("serializer disabled in build"); -} diff --git a/ngraph/test/CMakeLists.txt b/ngraph/test/CMakeLists.txt index a056e40..40f5805 100644 --- a/ngraph/test/CMakeLists.txt +++ b/ngraph/test/CMakeLists.txt @@ -200,10 +200,6 @@ foreach(HEADER ${NGRAPH_HEADER_LIST}) list(APPEND SRC ${OUT_FILE}) endforeach() -if(NGRAPH_JSON_ENABLE) - list(APPEND SRC core.cpp serialize.cpp) -endif() - set_source_files_properties(includes.cpp PROPERTIES COMPILE_DEFINITIONS NGRAPH_INCLUDES="${PROJECT_SOURCE_DIR}/src/ngraph") @@ -398,9 +394,6 @@ if (NGRAPH_ONNX_IMPORT_ENABLE AND NOT NGRAPH_USE_PROTOBUF_LITE) endif() target_compile_definitions(unit-test PRIVATE NGRAPH_VERSION_LABEL="${NGRAPH_VERSION_LABEL}") -if (NGRAPH_JSON_ENABLE) - target_link_libraries(unit-test PRIVATE libjson) -endif() if(NOT WIN32) target_link_libraries(unit-test PRIVATE pthread) endif() diff --git a/ngraph/test/algebraic_simplification.cpp b/ngraph/test/algebraic_simplification.cpp index ea48363..23b4edf 100644 --- a/ngraph/test/algebraic_simplification.cpp +++ b/ngraph/test/algebraic_simplification.cpp @@ -48,7 +48,6 @@ #include "ngraph/pattern/matcher.hpp" #include "ngraph/pattern/op/label.hpp" #include "ngraph/pattern/op/skip.hpp" -#include "ngraph/serializer.hpp" #include "util/all_close.hpp" #include "util/matcher.hpp" #include "util/test_tools.hpp" diff --git a/ngraph/test/backend/broadcast.in.cpp b/ngraph/test/backend/broadcast.in.cpp index d90d5c0..89fb802 100644 --- a/ngraph/test/backend/broadcast.in.cpp +++ b/ngraph/test/backend/broadcast.in.cpp @@ -464,82 +464,3 @@ NGRAPH_TEST(${BACKEND_NAME}, broadcast_matrix_2) read_vector(result), MIN_FLOAT_TOLERANCE_BITS)); } - -#ifndef NGRAPH_JSON_DISABLE -NGRAPH_TEST(${BACKEND_NAME}, constant_broadcast) -{ - const string js = - R"([{ - "name" : "Function_0", - "ops" : [ - { - "element_type" : - {"bitwidth" : 32, "c_type_string" : "float", "is_real" : true, "is_signed" : true, "is_quantized" : false}, - "inputs" : [], - "name" : "Parameter_4", - "op" : "Parameter", - "outputs" : ["Parameter_4"], - "shape" : [ 3, 4 ] - }, - { - "element_type" : - {"bitwidth" : 32, "c_type_string" : "float", "is_real" : true, "is_signed" : true, "is_quantized" : false}, - "inputs" : [], - "name" : "Parameter_0", - "op" : "Parameter", - "outputs" : ["Parameter_0"], - "shape" : [ 3, 4 ] - }, - { - "element_type" : - {"bitwidth" : 32, "c_type_string" : "float", "is_real" : true, "is_signed" : true, "is_quantized" : false}, - "inputs" : [], - "name" : "Constant_1", - "op" : "Constant", - "outputs" : ["Constant_1"], - "shape" : [], - "value" : ["0"] - }, - { - "axes" : [ 0, 1 ], - "element_type" : - {"bitwidth" : 32, "c_type_string" : "float", "is_real" : true, "is_signed" : true, "is_quantized" : false}, - "inputs" : ["Constant_1"], - "name" : "Broadcast_2", - "op" : "Broadcast", - "outputs" : ["Broadcast_2"], - "shape" : [ 3, 4 ] - }, - { - "element_type" : - {"bitwidth" : 32, "c_type_string" : "float", "is_real" : true, "is_signed" : true, "is_quantized" : false}, - "inputs" : [ "Parameter_0", "Broadcast_2" ], - "name" : "Maximum_3", - "op" : "Maximum", - "outputs" : ["Maximum_3"] - }, - { - "element_type" : - {"bitwidth" : 32, "c_type_string" : "float", "is_real" : true, "is_signed" : true, "is_quantized" : false}, - "inputs" : [ "Maximum_3", "Parameter_4" ], - "name" : "Multiply_5", - "op" : "Multiply", - "outputs" : ["Multiply_5"] - } - ], - "parameters" : [ "Parameter_0", "Parameter_4" ], - "result" : ["Multiply_5"], - "result_shape" : [ 3, 4 ], - "result_type" : - {"bitwidth" : 32, "c_type_string" : "float", "is_real" : true, "is_signed" : true, "is_quantized" : false} - }])"; - stringstream ss(js); - - shared_ptr f = ngraph::deserialize(ss); - - // max(x,broadcast(Constant(0))) - auto backend = runtime::Backend::create("${BACKEND_NAME}"); - - // If this compiles it works -} -#endif diff --git a/ngraph/test/backend/comparison.in.cpp b/ngraph/test/backend/comparison.in.cpp index 47bf7a4..a836eab 100644 --- a/ngraph/test/backend/comparison.in.cpp +++ b/ngraph/test/backend/comparison.in.cpp @@ -25,7 +25,6 @@ #include "ngraph/log.hpp" #include "ngraph/ngraph.hpp" #include "ngraph/runtime/tensor.hpp" -#include "ngraph/serializer.hpp" #include "runtime/backend.hpp" #include "util/all_close.hpp" #include "util/all_close_f.hpp" diff --git a/ngraph/test/backend_api.cpp b/ngraph/test/backend_api.cpp index 2b61c0f..f4025af 100644 --- a/ngraph/test/backend_api.cpp +++ b/ngraph/test/backend_api.cpp @@ -58,35 +58,3 @@ TEST(backend_api, DISABLED_config_unsupported) EXPECT_FALSE(backend->set_config(config, error)); EXPECT_FALSE(error == ""); } - -#ifndef NGRAPH_JSON_DISABLE -TEST(backend_api, save_load) -{ - Shape shape{2, 2}; - auto A = make_shared(element::f32, shape); - auto B = make_shared(element::f32, shape); - auto f = make_shared(make_shared(A, B), ParameterVector{A, B}); - - auto backend = runtime::Backend::create("INTERPRETER"); - - // Create some tensors for input/output - shared_ptr a = backend->create_tensor(element::f32, shape); - shared_ptr b = backend->create_tensor(element::f32, shape); - shared_ptr result = backend->create_tensor(element::f32, shape); - - copy_data(a, {1.f, 2.f, 3.f, 4.f}); - copy_data(b, {5.f, 6.f, 7.f, 8.f}); - - stringstream file; - { - auto handle = backend->compile(f); - handle->save(file); - } - { - auto handle = backend->load(file); - ASSERT_NE(handle, nullptr); - handle->call_with_validate({result}, {a, b}); - EXPECT_TRUE(test::all_close_f(read_vector(result), {6.f, 8.f, 10.f, 12.f})); - } -} -#endif diff --git a/ngraph/test/build_graph.cpp b/ngraph/test/build_graph.cpp index 3fc0bff..4df43fa 100644 --- a/ngraph/test/build_graph.cpp +++ b/ngraph/test/build_graph.cpp @@ -18,7 +18,6 @@ #include "ngraph/file_util.hpp" #include "ngraph/ngraph.hpp" -#include "ngraph/serializer.hpp" #include "util/test_tools.hpp" #include diff --git a/ngraph/test/control_dependencies.cpp b/ngraph/test/control_dependencies.cpp index a951b4f..96b011b 100644 --- a/ngraph/test/control_dependencies.cpp +++ b/ngraph/test/control_dependencies.cpp @@ -31,7 +31,6 @@ #include "ngraph/pass/manager.hpp" #include "ngraph/pass/visualize_tree.hpp" #include "ngraph/pattern/matcher.hpp" -#include "ngraph/serializer.hpp" #include "ngraph/util.hpp" #include "util/all_close.hpp" #include "util/ndarray.hpp" @@ -188,56 +187,3 @@ TEST(control_dependencies, replace_node) ASSERT_TRUE(0 == count_control_dependencies(ADD, MUL_AB)); ASSERT_TRUE(1 == count_control_dependencies(ADD, MUL_BA)); } - -#ifndef NGRAPH_JSON_DISABLE -TEST(control_dependencies, serialize_cdop) -{ - auto A = make_shared(element::f32, Shape{}); - auto absn = make_shared(A); - auto cdop = make_shared(A); - cdop->add_control_dependency(absn); - auto f = make_shared(cdop, ParameterVector{A}); - - string js = serialize(f, 4); - shared_ptr clone = deserialize(js); - - auto matcher = std::make_shared(cdop); - auto cdop_clone = clone->get_results().at(0)->input_value(0).get_node_shared_ptr(); - ASSERT_TRUE(matcher->match(cdop_clone)); - auto cloned_deps = cdop_clone->get_control_dependencies(); - ASSERT_EQ(cloned_deps.size(), 1); - auto cloned_abs = *begin(cloned_deps); - ASSERT_TRUE(is_type(cloned_abs)); -} - -TEST(control_dependencies, serialize_cdop_abs) -{ - auto A = make_shared(element::f32, Shape{}); - auto absn = make_shared(A); - auto B = make_shared(element::f32, Shape{}); - auto absn_b = make_shared(B); - auto cdop = make_shared(A); - cdop->add_control_dependency(absn); - cdop->add_control_dependency(absn_b); - auto absn_cdop = make_shared(cdop); - - auto f = make_shared(absn_cdop, ParameterVector{A, B}); - - string js = serialize(f, 4); - shared_ptr clone = deserialize(js); - auto matcher = std::make_shared(cdop); - auto cdop_clone = clone->get_results() - .at(0) - ->input_value(0) - .get_node_shared_ptr() - ->input_value(0) - .get_node_shared_ptr(); - ASSERT_TRUE(matcher->match(cdop_clone)); - auto cloned_deps = cdop_clone->get_control_dependencies(); - ASSERT_EQ(cloned_deps.size(), 2); - for (auto ccdep : cloned_deps) - { - ASSERT_TRUE(is_type(ccdep)); - } -} -#endif diff --git a/ngraph/test/core.cpp b/ngraph/test/core.cpp deleted file mode 100644 index f665eef..0000000 --- a/ngraph/test/core.cpp +++ /dev/null @@ -1,36 +0,0 @@ -//***************************************************************************** -// Copyright 2017-2020 Intel Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -//***************************************************************************** - -#include "gtest/gtest.h" - -#include "ngraph/file_util.hpp" -#include "ngraph/ngraph.hpp" -#include "ngraph/serializer.hpp" - -using namespace ngraph; -using namespace std; - -TEST(core, function_size) -{ - const string m1 = file_util::path_join(SERIALIZED_ZOO, "mxnet/mnist_mlp_forward.json"); - const string m2 = file_util::path_join(SERIALIZED_ZOO, "mxnet/10_bucket_LSTM.json"); - - auto f1 = deserialize(m1); - auto f2 = deserialize(m2); - auto s1 = f1->get_graph_size(); - auto s2 = f2->get_graph_size(); - EXPECT_GT(s2, s1); -} diff --git a/ngraph/test/pattern.cpp b/ngraph/test/pattern.cpp index 478ce00..8828ea0 100644 --- a/ngraph/test/pattern.cpp +++ b/ngraph/test/pattern.cpp @@ -44,7 +44,6 @@ #include "ngraph/pattern/op/or.hpp" #include "ngraph/pattern/op/skip.hpp" #include "ngraph/pattern/op/true.hpp" -#include "ngraph/serializer.hpp" #include "util/matcher.hpp" #include "util/test_tools.hpp" diff --git a/ngraph/test/runtime/interpreter/int_backend.cpp b/ngraph/test/runtime/interpreter/int_backend.cpp index 13400ce..1d0b994 100644 --- a/ngraph/test/runtime/interpreter/int_backend.cpp +++ b/ngraph/test/runtime/interpreter/int_backend.cpp @@ -72,37 +72,6 @@ bool runtime::interpreter::INTBackend::is_supported(const Node& node) const return m_unsupported_op_name_list.find(node.description()) == m_unsupported_op_name_list.end(); } -std::shared_ptr runtime::interpreter::INTBackend::load(istream& in) -{ - shared_ptr exec; - cpio::Reader reader(in); - auto file_info = reader.get_file_info(); - string save_info; - for (const cpio::FileInfo& info : file_info) - { - if (info.get_name() == "save_info") - { - vector buffer = reader.read(info); - save_info = string(buffer.data(), buffer.size()); - break; - } - } - if (save_info == "INTERPRETER Save File 1.0") - { - for (const cpio::FileInfo& info : file_info) - { - if (info.get_name() == "model") - { - vector buffer = reader.read(info); - string model_string = string(buffer.data(), buffer.size()); - exec = shared_ptr(new INTExecutable(model_string)); - break; - } - } - } - return exec; -} - bool runtime::interpreter::INTBackend::set_config(const map& config, string& error) { bool rc = false; diff --git a/ngraph/test/runtime/interpreter/int_backend.hpp b/ngraph/test/runtime/interpreter/int_backend.hpp index 9a89e61..78e0222 100644 --- a/ngraph/test/runtime/interpreter/int_backend.hpp +++ b/ngraph/test/runtime/interpreter/int_backend.hpp @@ -59,7 +59,6 @@ public: std::shared_ptr compile(std::shared_ptr function, bool enable_performance_data = false) override; - std::shared_ptr load(std::istream& input_stream) override; bool is_supported(const Node& node) const override; diff --git a/ngraph/test/runtime/interpreter/int_executable.cpp b/ngraph/test/runtime/interpreter/int_executable.cpp index 6a4a83a..99d27b8 100644 --- a/ngraph/test/runtime/interpreter/int_executable.cpp +++ b/ngraph/test/runtime/interpreter/int_executable.cpp @@ -23,7 +23,6 @@ #include "ngraph/op/util/op_types.hpp" #include "ngraph/ops.hpp" #include "ngraph/pass/manager.hpp" -#include "ngraph/serializer.hpp" #include "ngraph/util.hpp" #include "opset0_downgrade.hpp" #include "opset1_downgrade.hpp" @@ -65,13 +64,7 @@ runtime::interpreter::INTExecutable::INTExecutable(const shared_ptr& f : m_is_compiled{true} , m_performance_counters_enabled{enable_performance_collection} { -#ifdef INTERPRETER_FORCE_SERIALIZE - // To verify that the serializer works correctly let's just run this graph round-trip - string ser = serialize(function); - m_function = deserialize(ser); -#else m_function = clone_function(*function); -#endif auto is_supported = [](const Node& node) { bool retval = false; switch (INTExecutable::get_typeid(node)) @@ -100,18 +93,6 @@ runtime::interpreter::INTExecutable::INTExecutable(const shared_ptr& f set_parameters_and_results(*m_function); } -runtime::interpreter::INTExecutable::INTExecutable(const std::string& model_string) - : m_is_compiled{true} - , m_performance_counters_enabled{false} -{ - m_function = deserialize(model_string); - for (auto node : m_function->get_ordered_ops()) - { - m_nodes.push_back(node); - } - set_parameters_and_results(*m_function); -} - bool runtime::interpreter::INTExecutable::call(const vector>& outputs, const vector>& inputs) { @@ -335,15 +316,6 @@ void runtime::interpreter::INTExecutable::perform_nan_check( } } -void runtime::interpreter::INTExecutable::save(ostream& out) -{ - cpio::Writer writer(out); - string si = "INTERPRETER Save File 1.0"; - writer.write("save_info", si.data(), si.size()); - string model = serialize(m_function, 0); - writer.write("model", model.data(), model.size()); -} - shared_ptr runtime::interpreter::INTExecutable::get_parameter(size_t index) const { diff --git a/ngraph/test/runtime/interpreter/int_executable.hpp b/ngraph/test/runtime/interpreter/int_executable.hpp index d1707d1..12da1af 100644 --- a/ngraph/test/runtime/interpreter/int_executable.hpp +++ b/ngraph/test/runtime/interpreter/int_executable.hpp @@ -128,8 +128,6 @@ public: bool call(const std::vector>& outputs, const std::vector>& inputs) override; - virtual void save(std::ostream& output_stream) override; - void set_nan_check(bool enable); std::vector get_performance_data() const override; @@ -145,8 +143,6 @@ public: create_output_tensor(size_t output_index, size_t pipeline_depth) override; protected: - INTExecutable(const std::string& model_string); - std::shared_ptr get_parameter(size_t index) const; std::shared_ptr get_result(size_t index) const; int get_alignment() const { return 64; } diff --git a/ngraph/test/serialize.cpp b/ngraph/test/serialize.cpp deleted file mode 100644 index f28ab12..0000000 --- a/ngraph/test/serialize.cpp +++ /dev/null @@ -1,931 +0,0 @@ -//***************************************************************************** -// Copyright 2017-2020 Intel Corporation -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -//***************************************************************************** - -#include -#include - -#include "gmock/gmock.h" -#include "gtest/gtest.h" - -#include "ngraph/coordinate_transform.hpp" -#include "ngraph/file_util.hpp" -#include "ngraph/ngraph.hpp" -#include "ngraph/op/constant.hpp" -#include "ngraph/op/get_output_element.hpp" -#include "ngraph/op/interpolate.hpp" -#include "ngraph/op/passthrough.hpp" -#include "ngraph/pass/manager.hpp" -#include "ngraph/pass/visualize_tree.hpp" -#include "ngraph/serializer.hpp" -#include "ngraph/util.hpp" -#include "nlohmann/json.hpp" -#include "util/all_close_f.hpp" -#include "util/test_tools.hpp" -#include "util/visitor.hpp" - -using namespace std; -using namespace ngraph; -using json = nlohmann::json; - -using ::testing::ElementsAre; -using ::testing::NotNull; -using ::testing::StrEq; - -template -T get_or_default(nlohmann::json& j, const std::string& key, const T& default_value) -{ - T rc; - try - { - rc = j.at(key).get(); - } - catch (...) - { - rc = default_value; - } - return rc; -} - -#if defined(NGRAPH_INTERPRETER_ENABLE) -TEST(serialize, main) -{ - // First create "f(A,B,C) = (A+B)*C". - Shape shape{2, 2}; - auto A = make_shared(element::f32, shape); - auto B = make_shared(element::f32, shape); - auto C = make_shared(element::f32, shape); - auto f = make_shared((A + B) * C, ParameterVector{A, B, C}, "f"); - - string js = serialize(f, 4); - - { - ofstream out("serialize_function.js"); - out << js; - } - - istringstream in(js); - shared_ptr sfunc = deserialize(in); - auto backend = runtime::Backend::create("INTERPRETER"); - auto handle = backend->compile(sfunc); - - auto x = backend->create_tensor(element::f32, shape); - copy_data(x, vector{1, 2, 3, 4}); - auto y = backend->create_tensor(element::f32, shape); - copy_data(y, vector{5, 6, 7, 8}); - auto z = backend->create_tensor(element::f32, shape); - copy_data(z, vector{9, 10, 11, 12}); - auto result = backend->create_tensor(element::f32, shape); - - handle->call_with_validate({result}, {x, y, z}); - EXPECT_EQ((vector{54, 80, 110, 144}), read_vector(result)); - - handle->call_with_validate({result}, {y, x, z}); - EXPECT_EQ((vector{54, 80, 110, 144}), read_vector(result)); - - handle->call_with_validate({result}, {x, z, y}); - EXPECT_EQ((vector{50, 72, 98, 128}), read_vector(result)); -} - -TEST(serialize, friendly_name) -{ - // First create "f(A,B,C) = (A+B)*C". - Shape shape{2, 2}; - auto A = make_shared(element::f32, shape); - auto B = make_shared(element::f32, shape); - auto C = make_shared(element::f32, shape); - auto sum = A + B; - auto product = sum * C; - auto f = make_shared(product, ParameterVector{A, B, C}, "f"); - - A->set_friendly_name("A"); - B->set_friendly_name("B"); - C->set_friendly_name("C"); - sum->set_friendly_name("Sum"); - product->set_friendly_name("Product"); - - string js = serialize(f, 4); - ofstream out("serialize_function.js"); - out << js; - - istringstream in(js); - shared_ptr sfunc = deserialize(in); - auto backend = runtime::Backend::create("INTERPRETER"); - auto handle = backend->compile(sfunc); - - auto x = backend->create_tensor(element::f32, shape); - copy_data(x, vector{1, 2, 3, 4}); - auto y = backend->create_tensor(element::f32, shape); - copy_data(y, vector{5, 6, 7, 8}); - auto z = backend->create_tensor(element::f32, shape); - copy_data(z, vector{9, 10, 11, 12}); - auto result = backend->create_tensor(element::f32, shape); - - handle->call_with_validate({result}, {x, y, z}); - EXPECT_EQ((vector{54, 80, 110, 144}), read_vector(result)); - - handle->call_with_validate({result}, {y, x, z}); - EXPECT_EQ((vector{54, 80, 110, 144}), read_vector(result)); - - handle->call_with_validate({result}, {x, z, y}); - EXPECT_EQ((vector{50, 72, 98, 128}), read_vector(result)); -} -#endif - -TEST(serialize, existing_models) -{ - vector models = {"mxnet/mnist_mlp_forward.json", - "mxnet/10_bucket_LSTM.json", - "mxnet/LSTM_backward.json", - "mxnet/LSTM_forward.json"}; - - for (const string& model : models) - { - const string json_path = file_util::path_join(SERIALIZED_ZOO, model); - const string json_string = file_util::read_file_to_string(json_path); - shared_ptr f = ngraph::deserialize(json_string); - } -} - -TEST(serialize, default_value) -{ - json j = {{"test1", 1}, {"test2", 2}}; - - int x1 = j.at("test1").get(); - EXPECT_EQ(x1, 1); - int x2 = get_or_default(j, "test2", 0); - EXPECT_EQ(x2, 2); - int x3 = get_or_default(j, "test3", 3); - EXPECT_EQ(x3, 3); -} - -TEST(serialize, constant) -{ - const string tmp_file = "serialize_constant.cpio"; - Shape shape{2, 2, 2}; - auto A = op::Constant::create(element::f32, shape, {1, 2, 3, 4, 5, 6, 7, 8}); - auto f = make_shared(A, ParameterVector{}); - - EXPECT_EQ((vector{1, 2, 3, 4, 5, 6, 7, 8}), A->get_vector()); - serialize(tmp_file, f); - auto g = deserialize(tmp_file); - ASSERT_NE(g, nullptr); - file_util::remove_file(tmp_file); - bool found = false; - for (shared_ptr node : g->get_ops()) - { - shared_ptr c = as_type_ptr(node); - if (c) - { - found = true; - EXPECT_EQ((vector{1, 2, 3, 4, 5, 6, 7, 8}), c->get_vector()); - break; - } - } - EXPECT_TRUE(found); -} - -MATCHER_P2(IsOutputShape, type, shape, "") -{ - return std::get<0>(arg) == type && std::get<1>(arg).to_shape() == shape; -} - -TEST(serialize, passthrough) -{ - const string tmp_file = "serialize_passthrough.json"; - - using estuple = std::tuple; - - Shape shape{2, 2, 2}; - auto p = make_shared( - "SerializationTest", - "Plain", - "Hello, world!", - OutputVector{}, - std::vector{estuple{element::f32, PartialShape{2, 3}}, - estuple{element::i8, PartialShape{4, 5}}}); - auto f = make_shared(NodeVector{std::make_shared(p, 0), - std::make_shared(p, 1)}, - ParameterVector{}); - serialize(tmp_file, f); - - auto g = deserialize(tmp_file); - file_util::remove_file(tmp_file); - ASSERT_THAT(g, NotNull()); - - std::shared_ptr pt; - for (const auto& op : g->get_ops()) - { - pt = as_type_ptr(op); - if (pt) - { - break; - } - } - ASSERT_THAT(pt.get(), NotNull()); - - EXPECT_THAT(pt->logical_type(), StrEq("SerializationTest")); - EXPECT_THAT(pt->language(), StrEq("Plain")); - EXPECT_THAT(pt->function(), StrEq("Hello, world!")); - EXPECT_THAT(pt->output_shapes(), - ElementsAre(IsOutputShape(element::f32, Shape{2, 3}), - IsOutputShape(element::i8, Shape{4, 5}))); -} - -TEST(serialize, constant_infinity_nan) -{ - vector a_data{123.f, 456.f, INFINITY, -INFINITY, NAN}; - vector b_data{5.f, 5.f, 5.f, 5.f, 5.f, 5.f}; - vector c_data{0.05f, 0.05f, 0.05f, 0.05f, 0.05f, 0.05001f, 0.05f}; - vector d_data{-100, -10, -1, 0, 50, 5000000000001}; - auto A = make_shared(element::f32, Shape{5}, a_data); - auto B = make_shared(element::f32, Shape{6}, b_data); - auto C = make_shared(element::f32, Shape{7}, c_data); - auto D = make_shared(element::i64, Shape{d_data.size()}, d_data); - A->set_friendly_name("A"); - B->set_friendly_name("B"); - C->set_friendly_name("C"); - D->set_friendly_name("D"); - auto f = make_shared(NodeVector{A, B, C, D}, ParameterVector{}); - - string s = serialize(f, 4); - shared_ptr g = deserialize(s); - - shared_ptr a; - shared_ptr b; - shared_ptr c; - shared_ptr d; - for (auto node : g->get_ops()) - { - if (node->get_friendly_name() == "A") - { - a = as_type_ptr(node); - } - else if (node->get_friendly_name() == "B") - { - b = as_type_ptr(node); - } - else if (node->get_friendly_name() == "C") - { - c = as_type_ptr(node); - } - else if (node->get_friendly_name() == "D") - { - d = as_type_ptr(node); - } - } - ASSERT_TRUE(a); - ASSERT_TRUE(b); - ASSERT_TRUE(c); - ASSERT_TRUE(d); - EXPECT_TRUE(test::all_close_f(a->get_vector(), a_data)); - EXPECT_TRUE(test::all_close_f(b->get_vector(), b_data)); - EXPECT_TRUE(test::all_close_f(c->get_vector(), c_data)); - EXPECT_EQ(d->get_vector(), d_data); - - string filename = "constant_infinity_nan_test.dot"; - pass::Manager pass_manager; - pass_manager.register_pass(filename); - pass_manager.run_passes(g); - ifstream file(filename); - ASSERT_TRUE(file); - string str((istreambuf_iterator(file)), istreambuf_iterator()); - EXPECT_NE(str.find(R"(label="A)"), string::npos); - EXPECT_NE(str.find(R"(label="B)"), string::npos); - EXPECT_NE(str.find(R"(label="C)"), string::npos); - EXPECT_NE(str.find(R"(label="D)"), string::npos); -} - -TEST(serialize, non_zero_node_output) -{ - auto arg = make_shared(element::f32, Shape{10}); - auto topk = make_shared(arg, 0, element::i32, 5, true); - auto abs = make_shared(Output(topk, 1)); - auto result = make_shared(abs); - auto f = make_shared(ResultVector{result}, ParameterVector{arg}); - string s = serialize(f); - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_abs = g_result->input_value(0).get_node_shared_ptr(); - auto topk_out = g_abs->input_value(0); - EXPECT_EQ(topk_out.get_index(), 1); - ASSERT_TRUE(is_type(topk_out.get_node())); -} - -TEST(serialize, opset1_softmax) -{ - const auto arg = make_shared(element::f32, Shape{10}); - const auto softmax = make_shared(arg, 0); - const auto result = make_shared(softmax); - const auto f = make_shared(ResultVector{result}, ParameterVector{arg}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - const auto g_result = g->get_results().at(0); - const auto g_softmax = g_result->get_input_node_shared_ptr(0); - EXPECT_TRUE(is_type(g_softmax)); -} - -TEST(serialize, opset1_gather) -{ - auto params = make_shared(element::f32, Shape{5, 6}); - auto indices = make_shared(element::i64, Shape{4}); - auto axis = make_shared(element::i64, Shape{1}); - auto gather_v1 = make_shared(params, indices, axis); - - auto result = make_shared(gather_v1); - auto f = make_shared(ResultVector{result}, ParameterVector{params, indices, axis}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_gather = g_result->get_input_node_shared_ptr(0); - EXPECT_TRUE(is_type(g_gather)); -} - -TEST(serialize, opset1_product) -{ - auto arg = make_shared(element::f32, Shape{1, 2, 3}); - auto keep_dims = true; - auto axes = make_shared(element::i64, Shape{2}, vector{1, 2}); - auto reduce_prod = make_shared(arg, axes, keep_dims); - auto result = make_shared(reduce_prod); - auto f = make_shared(ResultVector{result}, ParameterVector{arg}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_red_prod = g_result->get_input_node_shared_ptr(0); - auto node = as_type_ptr(g_red_prod); - EXPECT_TRUE(node); - EXPECT_EQ(node->get_keep_dims(), 1); - EXPECT_EQ(node->get_reduction_axes(), AxisSet({1, 2})); -} - -TEST(serialize, opset1_sum) -{ - auto arg = make_shared(element::f32, Shape{1, 2, 3}); - auto keep_dims = true; - auto axes = make_shared(element::i64, Shape{2}, vector{1, 2}); - auto reduce_sum = make_shared(arg, axes, keep_dims); - auto result = make_shared(reduce_sum); - auto f = make_shared(ResultVector{result}, ParameterVector{arg}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_red_sum = g_result->get_input_node_shared_ptr(0); - auto node = as_type_ptr(g_red_sum); - EXPECT_TRUE(node); - EXPECT_EQ(node->get_keep_dims(), 1); - EXPECT_EQ(node->get_reduction_axes(), AxisSet({1, 2})); -} - -TEST(serialize, opset1_pad) -{ - auto arg = make_shared(element::f32, Shape{4, 5, 6}); - auto pads_begin = make_shared(element::i64, Shape{1}); - auto pads_end = make_shared(element::i64, Shape{2}); - auto arg_pad_value = make_shared(element::f32, Shape{}); - auto pad_mode = op::PadMode::EDGE; - auto pad = make_shared(arg, pads_begin, pads_end, arg_pad_value, pad_mode); - - auto result = make_shared(pad); - auto f = make_shared(ResultVector{result}, - ParameterVector{arg, pads_begin, pads_end, arg_pad_value}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_pad = as_type_ptr(g_result->input_value(0).get_node_shared_ptr()); - ASSERT_TRUE(g_pad); - EXPECT_EQ(g_pad->get_pad_mode(), pad_mode); -} - -TEST(serialize, tensor_iterator_raw) -{ - // That which we iterate over - auto X = make_shared(element::f32, Shape{32, 40, 10}); - - // Common to all cells - auto WH = make_shared(element::f32, Shape{20, 20}); - auto WX = make_shared(element::f32, Shape{10, 20}); - auto bH = make_shared(element::f32, Shape{20}); - auto WY = make_shared(element::f32, Shape{20, 5}); - auto bY = make_shared(element::f32, Shape{5}); - - // Initial values - auto Hinit = make_shared(element::f32, Shape{32, 1, 20}); - - // Set up the cell body, a function from (Hi, Xi) -> (Ho, Yo) - // Cell parameters - auto Hi = make_shared(element::f32, Shape{32, 1, 20}); - auto Xi = make_shared(element::f32, Shape{32, 1, 10}); - auto WH_body = make_shared(element::f32, Shape{20, 20}); - auto WX_body = make_shared(element::f32, Shape{10, 20}); - auto bH_body = make_shared(element::f32, Shape{20}); - auto WY_body = make_shared(element::f32, Shape{20, 5}); - auto bY_body = make_shared(element::f32, Shape{5}); - - // Body - auto Ho = make_shared( - make_shared( - make_shared(make_shared(Xi, AxisVector{0, 1, 2}, Shape{32, 10}), - WX_body) + - make_shared(make_shared(Hi, AxisVector{0, 1, 2}, Shape{32, 20}), - WH_body) + - make_shared(bH_body, Shape{32, 20}, AxisSet{0})), - AxisVector{0, 1}, - Shape{32, 1, 20}); - auto Yo = make_shared( - make_shared(make_shared(Ho, AxisVector{0, 1, 2}, Shape{32, 20}), - WY_body) + - make_shared(bY_body, Shape{32, 5}, AxisSet{0})); - auto body = make_shared( - OutputVector{Yo, Ho}, ParameterVector{Xi, Hi, WH_body, WX_body, WY_body, bH_body, bY_body}); - - auto tensor_iterator = make_shared(); - tensor_iterator->set_body(body); - // The Xi are the elements of Xseq - // start=0, stride=1, part_size=1, end=39, axis=1 - tensor_iterator->set_sliced_input(Xi, X, 0, 1, 1, 39, 1); - // Hi is Hinit on the first iteration, Ho after that - tensor_iterator->set_merged_input(Hi, Hinit, Ho); - tensor_iterator->set_invariant_input(WH_body, WH); - tensor_iterator->set_invariant_input(WX_body, WX); - tensor_iterator->set_invariant_input(WY_body, WY); - tensor_iterator->set_invariant_input(bH_body, bH); - tensor_iterator->set_invariant_input(bY_body, bY); - - // Output 0 is last Yo - auto out0 = tensor_iterator->get_iter_value(Yo, -1); - // Output 1 is concat of hidden states - // start=0, stride=1, part_size=1, end=39, axis=1 - auto out1 = tensor_iterator->get_concatenated_slices(Ho, 0, 1, 1, 39, 1); - - auto results = ResultVector{make_shared(out0), make_shared(out1)}; - auto f = make_shared(results, ParameterVector{X, Hinit, WH, WX, bH, WY, bY}); - string s = serialize(f); - shared_ptr g = deserialize(s); - - ngraph::test::NodeBuilder builder; - // Uncomment to see serialization - // builder.set_print(true); - builder.save_node(tensor_iterator); - auto g_tensor_iterator = as_type_ptr(builder.create()); - ASSERT_TRUE(g_tensor_iterator); - auto& inputs = tensor_iterator->get_input_descriptions(); - auto& g_inputs = g_tensor_iterator->get_input_descriptions(); - ASSERT_EQ(inputs.size(), g_inputs.size()); - for (size_t i = 0; i < tensor_iterator->get_input_descriptions().size(); ++i) - { - auto& val = inputs[i]; - auto& g_val = g_inputs[i]; - ASSERT_EQ(val->get_type_info(), g_val->get_type_info()); - ASSERT_EQ(val->m_input_index, g_val->m_input_index); - ASSERT_EQ(val->m_body_parameter_index, g_val->m_body_parameter_index); - } - auto& outputs = tensor_iterator->get_output_descriptions(); - auto& g_outputs = g_tensor_iterator->get_output_descriptions(); - ASSERT_EQ(outputs.size(), g_outputs.size()); - for (size_t i = 0; i < tensor_iterator->get_output_descriptions().size(); ++i) - { - auto& val = outputs[i]; - auto& g_val = g_outputs[i]; - ASSERT_EQ(val->get_type_info(), g_val->get_type_info()); - } -} - -TEST(serialize, tensor_iterator_lstm) -{ - // That which we iterate over - const size_t N = 32; // Batch size - const size_t L = 10; // Sequence length - const size_t I = 8; // Input size - const size_t H = 32; // Hidden size - auto SENT = make_shared(element::f32, Shape{N, L, I}); - - auto H_init = make_shared(element::f32, Shape{N, 1, H}); - auto C_init = make_shared(element::f32, Shape{N, 1, H}); - - auto W = make_shared(element::f32, Shape{4 * H, I}); - auto R = make_shared(element::f32, Shape{4 * H, H}); - auto H_t = make_shared(element::f32, Shape{N, 1, H}); - auto C_t = make_shared(element::f32, Shape{N, 1, H}); - - // Body - auto X = make_shared(element::f32, Shape{N, 1, I}); - auto W_body = make_shared(element::f32, Shape{4 * H, I}); - auto R_body = make_shared(element::f32, Shape{4 * H, H}); - auto LSTM_cell = - make_shared(make_shared(X, AxisVector{0, 1, 2}, Shape{N, I}), - make_shared(H_t, AxisVector{0, 1, 2}, Shape{N, H}), - make_shared(C_t, AxisVector{0, 1, 2}, Shape{N, H}), - W_body, - R_body, - H); - auto H_o = make_shared(LSTM_cell->output(0), AxisVector{0, 1}, Shape{N, 1, H}); - auto C_o = make_shared(LSTM_cell->output(1), AxisVector{0, 1}, Shape{N, 1, H}); - auto body = make_shared( - OutputVector{H_o, C_o}, ParameterVector{X, H_t, C_t, W_body, R_body}); - - auto tensor_iterator = make_shared(); - tensor_iterator->set_body(body); - // start=0, stride=1, part_size=1, end=39, axis=1 - tensor_iterator->set_sliced_input(X, SENT, 0, 1, 1, -1, 1); - // H_t is Hinit on the first iteration, Ho after that - tensor_iterator->set_merged_input(H_t, H_init, H_o); - tensor_iterator->set_merged_input(C_t, C_init, C_o); - tensor_iterator->set_invariant_input(W_body, W); - tensor_iterator->set_invariant_input(R_body, R); - - // Output 0 is last Ho, result 0 of body - auto out0 = tensor_iterator->get_iter_value(H_o, -1); - // Output 1 is last Co, result 1 of body - auto out1 = tensor_iterator->get_iter_value(C_o, -1); - - auto results = ResultVector{make_shared(out0), make_shared(out1)}; - auto f = make_shared(results, ParameterVector{SENT, H_init, C_init, W, R}); - string s = serialize(f); - shared_ptr g = deserialize(s); -} - -TEST(serialize, tensor_iterator_2_slice_inputs_part_size_2) -{ - // That which we iterate over - auto X = make_shared(element::f32, Shape{32, 40, 10}); - auto Y = make_shared(element::f32, Shape{32, 40, 10}); - auto M = make_shared(element::f32, Shape{32, 2, 10}); - - // Set up the cell body, a function from (Xi, Yi) -> (Zo) - // Body parameters - auto Xi = make_shared(element::f32, Shape{32, 2, 10}); - auto Yi = make_shared(element::f32, Shape{32, 2, 10}); - auto M_body = make_shared(element::f32, Shape{32, 2, 10}); - - // Body - auto Zo = (Xi + Yi) * M_body; - auto body = make_shared(OutputVector{Zo}, - ParameterVector{Xi, Yi, M_body}); - - auto tensor_iterator = make_shared(); - tensor_iterator->set_body(body); - // The Xi are the elements of Xseq - // start=0, stride=2, part_size=2, end=39, axis=1 - tensor_iterator->set_sliced_input(Xi, X, 0, 2, 2, 39, 1); - // The Yi are the elements of Yseq - // start=0, stride=2, part_size=2, end=-1, axis=1 - tensor_iterator->set_sliced_input(Yi, Y, 0, 2, 2, -1, 1); - tensor_iterator->set_invariant_input(M_body, M); - - // Output 0 is last Zo - auto out0 = tensor_iterator->get_iter_value(Zo, -1); - // Output 1 is concat of Zos - // start=0, stride=2, part_size=2, end=39, axis=1 - auto out1 = tensor_iterator->get_concatenated_slices(Zo, 0, 2, 2, 39, 1); - - auto result0 = make_shared(out0); - auto result1 = make_shared(out1); - Shape out0_shape{32, 2, 10}; - Shape out1_shape{32, 40, 10}; - - auto results = ResultVector{result0, result1}; - auto f = make_shared(results, ParameterVector{X, Y, M}); - EXPECT_EQ(result0->get_output_shape(0), out0_shape); - EXPECT_EQ(result1->get_output_shape(0), out1_shape); - - string s = serialize(f); - shared_ptr g = deserialize(s); -} - -TEST(serialize, tensor_iterator_2_slice_inputs_part_size_2_dynamic) -{ - // That which we iterate over - auto X = make_shared(element::f32, Shape{32, 40, 10}); - auto Y = make_shared(element::f32, Shape{32, 40, 10}); - auto M = make_shared(element::f32, Shape{32, 2, 10}); - - // Set up the cell body, a function from (Xi, Yi) -> (Zo) - // Body parameters - auto Xi = make_shared(element::f32, PartialShape::dynamic()); - auto Yi = make_shared(element::f32, PartialShape::dynamic()); - auto M_body = make_shared(element::f32, PartialShape::dynamic()); - - // Body - auto Zo = (Xi + Yi) * M_body; - auto body = make_shared(OutputVector{Zo}, - ParameterVector{Xi, Yi, M_body}); - - auto tensor_iterator = make_shared(); - tensor_iterator->set_body(body); - // The Xi are the elements of Xseq - // start=0, stride=2, part_size=2, end=38, axis=1 - tensor_iterator->set_sliced_input(Xi, X, 0, 2, 2, 38, 1); - // The Yi are the elements of Yseq - // start=0, stride=2, part_size=2, end=-2, axis=1 - tensor_iterator->set_sliced_input(Yi, Y, 0, 2, 2, -2, 1); - tensor_iterator->set_invariant_input(M_body, M); - - // check input descriptors - for (auto& desc : tensor_iterator->get_input_descriptions()) - { - auto type_info = desc->get_type_info(); - if (std::strcmp(type_info.name, "InvariantInputDescription") == 0) - { - auto input_desc = - as_type_ptr(desc); - EXPECT_NE(input_desc, nullptr); - } - else if (std::strcmp(type_info.name, "SliceInputDescription") == 0) - { - auto input_desc = as_type_ptr(desc); - EXPECT_NE(input_desc, nullptr); - } - else if (std::strcmp(type_info.name, "MergedInputDescription") == 0) - { - auto input_desc = as_type_ptr(desc); - EXPECT_NE(input_desc, nullptr); - } - } - - // Output 0 is last Zo - auto out0 = tensor_iterator->get_iter_value(Zo, -1); - // Output 1 is concat of Zos - // start=0, stride=2, part_size=2, end=38, axis=1 - auto out1 = tensor_iterator->get_concatenated_slices(Zo, 0, 2, 2, 38, 1); - - // check output descriptors - for (auto& desc : tensor_iterator->get_output_descriptions()) - { - auto type_info = desc->get_type_info(); - if (std::strcmp(type_info.name, "ConcatOutputDescription") == 0) - { - auto output_desc = - as_type_ptr(desc); - EXPECT_NE(output_desc, nullptr); - } - else if (std::strcmp(type_info.name, "BodyOutputDescription") == 0) - { - auto output_desc = as_type_ptr(desc); - EXPECT_NE(output_desc, nullptr); - } - } - - auto result0 = make_shared(out0); - auto result1 = make_shared(out1); - Shape out0_shape{32, 2, 10}; - Shape out1_shape{32, 38, 10}; - - auto results = ResultVector{result0, result1}; - auto f = make_shared(results, ParameterVector{X, Y, M}); - EXPECT_EQ(result0->get_output_shape(0), out0_shape); - EXPECT_EQ(result1->get_output_shape(0), out1_shape); - - EXPECT_EQ(body->get_results()[0]->get_output_shape(0), out0_shape); - - string s = serialize(f); - shared_ptr g = deserialize(s); -} - -TEST(serialize, opset1_strided_slice) -{ - auto data = make_shared(element::f32, Shape{2, 4, 6, 8}); - auto begin = make_shared(element::i64, Shape{4}); - auto end = make_shared(element::i64, Shape{4}); - auto strides = make_shared(element::i64, Shape{4}); - - const std::vector begin_mask{1, 0, 1, 0}; - const std::vector end_mask{1, 1, 1, 0}; - const std::vector new_axis_mask{0, 0, 1, 1}; - const std::vector shrink_axis_mask{0, 0, 0, 0}; - const std::vector ellipsis_mask{1, 1, 1, 1}; - - auto strided_slice_in = make_shared(data, - begin, - end, - strides, - begin_mask, - end_mask, - new_axis_mask, - shrink_axis_mask, - ellipsis_mask); - - auto result = make_shared(strided_slice_in); - auto f = - make_shared(ResultVector{result}, ParameterVector{data, begin, end, strides}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_strided_slice_v1 = g_result->get_input_node_shared_ptr(0); - auto strided_slice_out = as_type_ptr(g_strided_slice_v1); - - ASSERT_TRUE(strided_slice_out); - EXPECT_EQ(strided_slice_out->get_begin_mask(), begin_mask); - EXPECT_EQ(strided_slice_out->get_end_mask(), end_mask); - EXPECT_EQ(strided_slice_out->get_new_axis_mask(), new_axis_mask); - EXPECT_EQ(strided_slice_out->get_shrink_axis_mask(), shrink_axis_mask); - EXPECT_EQ(strided_slice_out->get_ellipsis_mask(), ellipsis_mask); -} - -TEST(serialize, opset1_binary_convolution) -{ - auto data = make_shared(element::f32, Shape{1, 2, 2, 2}); - auto filter = make_shared(element::f32, Shape{1, 2, 2, 2}); - const Strides strides{1, 1}; - const CoordinateDiff pads_begin{0, 0}; - const CoordinateDiff pads_end{0, 0}; - const Strides dilations{1, 1}; - auto mode = op::v1::BinaryConvolution::BinaryConvolutionMode::XNOR_POPCOUNT; - const float pad_value = 2.1f; - const auto auto_pad = op::PadType::NOTSET; - - auto binary_conv_in = make_shared( - data, filter, strides, pads_begin, pads_end, dilations, mode, pad_value, auto_pad); - - auto result = make_shared(binary_conv_in); - auto f = make_shared(ResultVector{result}, ParameterVector{data, filter}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_binary_conv = g_result->get_input_node_shared_ptr(0); - auto binary_conv_out = as_type_ptr(g_binary_conv); - ASSERT_TRUE(binary_conv_out); - - EXPECT_EQ(binary_conv_out->get_strides(), strides); - EXPECT_EQ(binary_conv_out->get_pads_begin(), pads_begin); - EXPECT_EQ(binary_conv_out->get_pads_end(), pads_end); - EXPECT_EQ(binary_conv_out->get_dilations(), dilations); - EXPECT_EQ(binary_conv_out->get_mode(), - op::v1::BinaryConvolution::BinaryConvolutionMode::XNOR_POPCOUNT); - EXPECT_EQ(binary_conv_out->get_pad_value(), pad_value); - EXPECT_EQ(binary_conv_out->get_auto_pad(), auto_pad); -} - -TEST(serialize, opset1_interpolate) -{ - auto image = make_shared(element::f32, Shape{2, 2, 33, 65}); - auto output_shape = op::Constant::create(element::i64, Shape{2}, {15, 30}); - op::InterpolateAttrs attrs; - attrs.axes = {2, 3}; - attrs.mode = "linear"; - attrs.align_corners = true; - attrs.antialias = false; - attrs.pads_begin = {0, 0, 0, 0}; - attrs.pads_end = {0, 0, 0, 0}; - - auto op = make_shared(image, output_shape, attrs); - auto result = make_shared(op); - auto f = make_shared(ResultVector{result}, ParameterVector{image}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_interpolate = g_result->get_input_node_shared_ptr(0); - auto g_op = as_type_ptr(g_interpolate); - ASSERT_TRUE(g_op); - op::InterpolateAttrs g_attrs = g_op->get_attrs(); - EXPECT_EQ(g_attrs.axes, attrs.axes); - EXPECT_EQ(g_attrs.mode, attrs.mode); - EXPECT_EQ(g_attrs.align_corners, attrs.align_corners); - EXPECT_EQ(g_attrs.antialias, attrs.antialias); - EXPECT_EQ(g_attrs.pads_begin, attrs.pads_begin); - EXPECT_EQ(g_attrs.pads_end, attrs.pads_end); -} - -TEST(serialize, opset3_interpolate) -{ - using op::v3::Interpolate; - using InterpolateMode = op::v3::Interpolate::InterpolateMode; - using CoordinateTransformMode = op::v3::Interpolate::CoordinateTransformMode; - using InterpolateAttrs = op::v3::Interpolate::InterpolateAttrs; - - auto image = make_shared(element::f32, Shape{2, 2, 33, 65}); - auto output_shape = op::Constant::create(element::i64, Shape{2}, {15, 30}); - InterpolateAttrs attrs; - attrs.axes = {2, 3}; - attrs.mode = InterpolateMode::linear; - attrs.coordinate_transformation_mode = CoordinateTransformMode::half_pixel; - attrs.antialias = false; - attrs.pads_begin = {0, 0, 0, 0}; - attrs.pads_end = {0, 0, 0, 0}; - - auto op = make_shared(image, output_shape, attrs); - auto result = make_shared(op); - auto f = make_shared(ResultVector{result}, ParameterVector{image}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_interpolate = g_result->get_input_node_shared_ptr(0); - auto g_op = as_type_ptr(g_interpolate); - ASSERT_TRUE(g_op); - InterpolateAttrs g_attrs = g_op->get_attrs(); - EXPECT_EQ(g_attrs.axes, attrs.axes); - EXPECT_EQ(g_attrs.mode, attrs.mode); - EXPECT_EQ(g_attrs.coordinate_transformation_mode, attrs.coordinate_transformation_mode); - EXPECT_EQ(g_attrs.antialias, attrs.antialias); - EXPECT_EQ(g_attrs.pads_begin, attrs.pads_begin); - EXPECT_EQ(g_attrs.pads_end, attrs.pads_end); -} - -TEST(serialize, depth_to_space) -{ - auto arg = make_shared(element::f32, Shape{4, 5, 6}); - auto mode = op::DepthToSpace::DepthToSpaceMode::BLOCKS_FIRST; - size_t block_size = 2; - auto depth_to_space_in = make_shared(arg, mode, block_size); - - auto result = make_shared(depth_to_space_in); - auto f = make_shared(ResultVector{result}, ParameterVector{arg}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_depth_to_space = g_result->get_input_node_shared_ptr(0); - auto depth_to_space_out = as_type_ptr(g_depth_to_space); - ASSERT_TRUE(depth_to_space_out); - EXPECT_EQ(depth_to_space_out->get_block_size(), block_size); - EXPECT_EQ(depth_to_space_out->get_mode(), mode); -} - -TEST(serialize, space_to_depth) -{ - auto arg = make_shared(element::f32, Shape{4, 6, 8}); - auto mode = op::SpaceToDepth::SpaceToDepthMode::BLOCKS_FIRST; - size_t block_size = 2; - auto space_to_depth_in = make_shared(arg, mode, block_size); - - auto result = make_shared(space_to_depth_in); - auto f = make_shared(ResultVector{result}, ParameterVector{arg}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_space_to_depth = g_result->get_input_node_shared_ptr(0); - auto depth_to_space_out = as_type_ptr(g_space_to_depth); - ASSERT_TRUE(depth_to_space_out); - EXPECT_EQ(depth_to_space_out->get_block_size(), block_size); - EXPECT_EQ(depth_to_space_out->get_mode(), mode); -} - -TEST(serialize, deformable_psroi_pooling) -{ - auto input = make_shared(element::f32, Shape{1, 2, 3, 4}); - auto coords = make_shared(element::f32, Shape{1, 1}); - auto offsets = make_shared(element::f32, Shape{1, 2, 3, 4}); - const int64_t output_dim = 1; - const int64_t group_size = 2; - const float spatial_scale = 3; - std::string mode = "bilinear_deformable"; - int64_t spatial_bins_x = 4; - int64_t spatial_bins_y = 5; - float trans_std = 6.1f; - int64_t part_size = 7; - - auto def_psroi_pool_in = make_shared(input, - coords, - offsets, - output_dim, - spatial_scale, - group_size, - mode, - spatial_bins_x, - spatial_bins_y, - trans_std, - part_size); - - auto result = make_shared(def_psroi_pool_in); - auto f = make_shared(ResultVector{result}, ParameterVector{input, coords, offsets}); - string s = serialize(f); - - shared_ptr g = deserialize(s); - auto g_result = g->get_results().at(0); - auto g_def_psroi_pool = g_result->get_input_node_shared_ptr(0); - auto def_psroi_pool_out = as_type_ptr(g_def_psroi_pool); - - EXPECT_EQ(def_psroi_pool_out->description(), "DeformablePSROIPooling"); - EXPECT_EQ(def_psroi_pool_out->get_version(), 1); - - EXPECT_EQ(def_psroi_pool_out->get_output_dim(), output_dim); - EXPECT_EQ(def_psroi_pool_out->get_group_size(), group_size); - EXPECT_EQ(def_psroi_pool_out->get_spatial_scale(), spatial_scale); - EXPECT_EQ(def_psroi_pool_out->get_mode(), mode); - EXPECT_EQ(def_psroi_pool_out->get_spatial_bins_x(), spatial_bins_x); - EXPECT_EQ(def_psroi_pool_out->get_spatial_bins_y(), spatial_bins_y); - EXPECT_EQ(def_psroi_pool_out->get_trans_std(), trans_std); - EXPECT_EQ(def_psroi_pool_out->get_part_size(), part_size); -} diff --git a/ngraph/test/util.cpp b/ngraph/test/util.cpp index 46b21b0..563d095 100644 --- a/ngraph/test/util.cpp +++ b/ngraph/test/util.cpp @@ -28,7 +28,6 @@ #include "ngraph/op/util/op_annotations.hpp" #include "ngraph/pass/manager.hpp" #include "ngraph/pass/visualize_tree.hpp" -#include "ngraph/serializer.hpp" #include "util/all_close.hpp" #include "util/ndarray.hpp" diff --git a/ngraph/test/util/test_tools.cpp b/ngraph/test/util/test_tools.cpp index 665ac8a..2887b78 100644 --- a/ngraph/test/util/test_tools.cpp +++ b/ngraph/test/util/test_tools.cpp @@ -209,17 +209,6 @@ string get_results_str(const std::vector& ref_data, return ss.str(); } -#ifndef NGRAPH_JSON_DISABLE -std::shared_ptr make_function_from_file(const std::string& file_name) -{ - const string json_path = file_util::path_join(SERIALIZED_ZOO, file_name); - const string json_string = file_util::read_file_to_string(json_path); - stringstream ss(json_string); - shared_ptr func = ngraph::deserialize(ss); - return func; -} -#endif - ::testing::AssertionResult test_ordered_ops(shared_ptr f, const NodeVector& required_ops) { unordered_set seen; diff --git a/ngraph/test/util/test_tools.hpp b/ngraph/test/util/test_tools.hpp index c433b9a..0eccc85 100644 --- a/ngraph/test/util/test_tools.hpp +++ b/ngraph/test/util/test_tools.hpp @@ -32,7 +32,6 @@ #include "ngraph/op/op.hpp" #include "ngraph/runtime/host_tensor.hpp" #include "ngraph/runtime/tensor.hpp" -#include "ngraph/serializer.hpp" #include "ngraph/type/element_type_traits.hpp" #include "runtime/backend.hpp" @@ -71,9 +70,6 @@ namespace ngraph bool validate_list(const std::vector>& nodes); std::shared_ptr make_test_graph(); -#ifndef NGRAPH_JSON_DISABLE -std::shared_ptr make_function_from_file(const std::string& file_name); -#endif template void copy_data(std::shared_ptr tv, const std::vector& data) -- 2.7.4