Commit 57fdd798 authored by tsocha's avatar tsocha Committed by Scott Cyphers

[Py] Update serialization (#750)

* [Py] Update serialization

* Update docstring

* Add UT for serialize model
parent 211b70f8
......@@ -93,9 +93,14 @@ class Computation:
result_arr = result_arr.reshape(result_shape)
return result_arr
def serialize(self): # type: () -> str
"""Serialize function (compute graph) to a JSON string."""
return serialize(self.function)
def serialize(self, indent=0, bin_const_data=False): # type: (int, bool) -> str
"""Serialize function (compute graph) to a JSON string.
:param indent: set indent of serialized output
:param bin_const_data: constant data should be binary or not
:return: serialized model
"""
return serialize(self.function, indent, bin_const_data)
@staticmethod
def _get_buffer_size(element_type, element_count): # type: (TensorViewType, int) -> int
......
......@@ -16,6 +16,7 @@
import numpy as np
import pytest
import json
import ngraph as ng
......@@ -39,3 +40,21 @@ def test_simple_computation_on_ndarrays(dtype):
value_c = np.array([[9, 10], [11, 12]], dtype=dtype)
result = computation(value_a, value_b, value_c)
assert np.allclose(result, np.array([[54, 80], [110, 144]], dtype=dtype))
def test_serialization():
dtype = np.float32
manager_name = pytest.config.getoption('backend', default='CPU')
shape = [2, 2]
parameter_a = ng.parameter(shape, dtype=dtype, name='A')
parameter_b = ng.parameter(shape, dtype=dtype, name='B')
parameter_c = ng.parameter(shape, dtype=dtype, name='C')
model = (parameter_a + parameter_b) * parameter_c
runtime = ng.runtime(manager_name=manager_name)
computation = runtime.computation(model, parameter_a, parameter_b, parameter_c)
serialized = computation.serialize(2)
serial_json = json.loads(serialized)
assert serial_json[0]["name"] != ''
assert 10 == len(serial_json[0]["ops"])
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment