Unverified Commit 79587d93 authored by Scott Cyphers's avatar Scott Cyphers Committed by GitHub

More serialization preparation for iteration ops (#3119)

* More serialization preparation for iteration ops

* const arg

* const json& leads to memory issues
parent fccaa49c
...@@ -191,10 +191,15 @@ static OP_TYPEID get_typeid(const string& s) ...@@ -191,10 +191,15 @@ static OP_TYPEID get_typeid(const string& s)
return rc; return rc;
} }
bool has_key(json j, const std::string& key)
{
return j.count(key) != 0;
}
template <typename T> template <typename T>
T get_or_default(nlohmann::json& j, const std::string& key, const T& default_value) T get_or_default(json j, const std::string& key, const T& default_value)
{ {
return j.count(key) != 0 ? j.at(key).get<T>() : default_value; return has_key(j, key) ? j.at(key).get<T>() : default_value;
} }
class JSONSerializer class JSONSerializer
...@@ -213,8 +218,11 @@ public: ...@@ -213,8 +218,11 @@ public:
json serialize_function(const Function& function); json serialize_function(const Function& function);
json serialize_output(const Output<Node>& output); json serialize_output(const Output<Node>& output);
json serialize_parameter_vector(const ParameterVector& parameters);
json serialize_output_vector(const OutputVector& output_vector);
json serialize_node_reference(const Node& node); json serialize_node_reference(const Node& node);
json serialize_node(const Node& node); json serialize_node(const Node& node);
json serialize_axis_set(const AxisSet& axis_set);
protected: protected:
size_t m_indent{0}; size_t m_indent{0};
...@@ -233,10 +241,13 @@ public: ...@@ -233,10 +241,13 @@ public:
m_const_data_callback = const_data_callback; m_const_data_callback = const_data_callback;
} }
shared_ptr<Function> deserialize_function(json& j); shared_ptr<Function> deserialize_function(json j);
Output<Node> deserialize_output(json& j); Output<Node> deserialize_output(json j);
shared_ptr<Node> deserialize_node_reference(json& j); OutputVector deserialize_output_vector(json j);
shared_ptr<Node> deserialize_node(json& j); ParameterVector deserialize_parameter_vector(json j);
shared_ptr<Node> deserialize_node_reference(json j);
shared_ptr<Node> deserialize_node(json j);
AxisSet deserialize_axis_set(json j);
protected: protected:
unordered_map<string, shared_ptr<Node>> m_node_map; unordered_map<string, shared_ptr<Node>> m_node_map;
...@@ -259,7 +270,7 @@ static json write_dimension(Dimension d) ...@@ -259,7 +270,7 @@ static json write_dimension(Dimension d)
} }
} }
static Dimension read_dimension(const json& j) static Dimension read_dimension(json j)
{ {
if (j.is_null()) if (j.is_null())
{ {
...@@ -288,7 +299,7 @@ static json write_partial_shape(const PartialShape& s) ...@@ -288,7 +299,7 @@ static json write_partial_shape(const PartialShape& s)
} }
} }
static PartialShape read_partial_shape(const json& j) static PartialShape read_partial_shape(json j)
{ {
if (j.is_null()) if (j.is_null())
{ {
...@@ -313,19 +324,32 @@ static json write_auto_broadcast(const op::AutoBroadcastSpec& autob) ...@@ -313,19 +324,32 @@ static json write_auto_broadcast(const op::AutoBroadcastSpec& autob)
return j; return j;
} }
static op::AutoBroadcastSpec read_auto_broadcast(const json& j) static op::AutoBroadcastSpec read_auto_broadcast(json js_node, const std::string& attr)
{ {
if (!j.is_object()) if (has_key(js_node, attr))
{ {
return op::AutoBroadcastSpec(); json j = js_node[attr];
return op::AutoBroadcastSpec(static_cast<op::AutoBroadcastType>(j.at("type")),
j.at("axis").get<size_t>());
} }
else else
{ {
return op::AutoBroadcastSpec(static_cast<op::AutoBroadcastType>(j.at("type")), return op::AutoBroadcastSpec();
j.at("axis").get<size_t>());
} }
} }
static op::PadType read_pad_type(json node_js)
{
return has_key(node_js, "pad_type") ? static_cast<op::PadType>(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<op::PadMode>(node_js.at("pad_mode"))
: op::PadMode::CONSTANT;
}
static json write_element_type(const ngraph::element::Type& n) static json write_element_type(const ngraph::element::Type& n)
{ {
json j; json j;
...@@ -333,7 +357,7 @@ static json write_element_type(const ngraph::element::Type& n) ...@@ -333,7 +357,7 @@ static json write_element_type(const ngraph::element::Type& n)
return j; return j;
} }
static element::Type read_element_type(const json& j) static element::Type read_element_type(json j)
{ {
size_t bitwidth = 0; size_t bitwidth = 0;
bool is_real = false; bool is_real = false;
...@@ -493,21 +517,24 @@ shared_ptr<ngraph::Function> ngraph::deserialize(const string& s) ...@@ -493,21 +517,24 @@ shared_ptr<ngraph::Function> ngraph::deserialize(const string& s)
rc = deserializer.deserialize_function(func); rc = deserializer.deserialize_function(func);
} }
} }
return rc; return rc;
} }
json JSONSerializer::serialize_parameter_vector(const ParameterVector& parameters)
{
json json_parameters = json::array();
for (auto param : parameters)
{
json_parameters.push_back(serialize_node_reference(*param));
}
return json_parameters;
}
json JSONSerializer::serialize_function(const Function& f) json JSONSerializer::serialize_function(const Function& f)
{ {
json function; json function;
function["name"] = f.get_name(); function["name"] = f.get_name();
function["parameters"] = serialize_parameter_vector(f.get_parameters());
vector<string> parameter_list;
for (auto param : f.get_parameters())
{
parameter_list.push_back(serialize_node_reference(*param));
}
function["parameters"] = parameter_list;
// TODO Functions can return multiple results // TODO Functions can return multiple results
for (size_t i = 0; i < f.get_output_size(); ++i) for (size_t i = 0; i < f.get_output_size(); ++i)
...@@ -519,7 +546,7 @@ json JSONSerializer::serialize_function(const Function& f) ...@@ -519,7 +546,7 @@ json JSONSerializer::serialize_function(const Function& f)
} }
template <typename T> template <typename T>
T get_value(nlohmann::json js, const string& key) T get_value(json js, const string& key)
{ {
T rc; T rc;
auto it = js.find(key); auto it = js.find(key);
...@@ -530,13 +557,13 @@ T get_value(nlohmann::json js, const string& key) ...@@ -530,13 +557,13 @@ T get_value(nlohmann::json js, const string& key)
return rc; return rc;
} }
shared_ptr<Node> JSONDeserializer::deserialize_node_reference(json& j) shared_ptr<Node> JSONDeserializer::deserialize_node_reference(json j)
{ {
const string& name = j; const string& name = j;
return m_node_map.at(name); return m_node_map.at(name);
} }
Output<Node> JSONDeserializer::deserialize_output(json& j) Output<Node> JSONDeserializer::deserialize_output(json j)
{ {
size_t index; size_t index;
json json_node_reference; json json_node_reference;
...@@ -557,10 +584,48 @@ Output<Node> JSONDeserializer::deserialize_output(json& j) ...@@ -557,10 +584,48 @@ Output<Node> JSONDeserializer::deserialize_output(json& j)
return Output<Node>(deserialize_node_reference(json_node_reference), index); return Output<Node>(deserialize_node_reference(json_node_reference), index);
} }
shared_ptr<Function> JSONDeserializer::deserialize_function(json& func_js) 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<set<size_t>>(axis_set);
}
AxisSet JSONDeserializer::deserialize_axis_set(json j)
{
AxisSet result;
if (j.is_array())
{
result = j.get<set<size_t>>();
}
return result;
}
ParameterVector JSONDeserializer::deserialize_parameter_vector(json json_parameters)
{
std::vector<std::shared_ptr<op::Parameter>> params;
for (auto& param_ref : json_parameters)
{
params.push_back(
dynamic_pointer_cast<op::Parameter>(deserialize_node_reference(param_ref)));
}
return params;
}
shared_ptr<Function> JSONDeserializer::deserialize_function(json func_js)
{ {
string func_name = func_js.at("name").get<string>(); string func_name = func_js.at("name").get<string>();
vector<json> func_parameters = func_js.at("parameters");
vector<json> func_result = func_js.at("result"); vector<json> func_result = func_js.at("result");
for (json node_js : func_js.at("ops")) for (json node_js : func_js.at("ops"))
{ {
...@@ -592,12 +657,7 @@ shared_ptr<Function> JSONDeserializer::deserialize_function(json& func_js) ...@@ -592,12 +657,7 @@ shared_ptr<Function> JSONDeserializer::deserialize_function(json& func_js)
"Graph serialization is inconsistent. Some op::Results appear to be missing"); "Graph serialization is inconsistent. Some op::Results appear to be missing");
} }
std::vector<std::shared_ptr<op::Parameter>> params; ParameterVector params = deserialize_parameter_vector(func_js.at("parameters"));
for (auto& param_ref : func_parameters)
{
params.push_back(
dynamic_pointer_cast<op::Parameter>(deserialize_node_reference(param_ref)));
}
shared_ptr<Function> rc{make_shared<Function>(result, params, func_name)}; shared_ptr<Function> rc{make_shared<Function>(result, params, func_name)};
m_function_map[func_name] = rc; m_function_map[func_name] = rc;
...@@ -630,7 +690,12 @@ struct OutputHelper ...@@ -630,7 +690,12 @@ struct OutputHelper
// when all op constructors use the new style arguments. // when all op constructors use the new style arguments.
struct OutputVectorHelper struct OutputVectorHelper
{ {
const OutputHelper& operator[](size_t i) const { return m_vector[i]; } 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<Node>& output) { m_vector.push_back(output); } void push_back(const Output<Node>& output) { m_vector.push_back(output); }
size_t size() const { return m_vector.size(); } size_t size() const { return m_vector.size(); }
operator vector<shared_ptr<Node>>() const operator vector<shared_ptr<Node>>() const
...@@ -638,14 +703,15 @@ struct OutputVectorHelper ...@@ -638,14 +703,15 @@ struct OutputVectorHelper
vector<shared_ptr<Node>> result; vector<shared_ptr<Node>> result;
for (auto& o : m_vector) for (auto& o : m_vector)
{ {
result.push_back(o); result.push_back(OutputHelper(o));
} }
return result; return result;
} }
vector<OutputHelper> m_vector; operator const OutputVector&() const { return m_vector; }
OutputVector m_vector;
}; };
shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) shared_ptr<Node> JSONDeserializer::deserialize_node(json node_js)
{ {
shared_ptr<Node> node; shared_ptr<Node> node;
try try
...@@ -653,14 +719,9 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -653,14 +719,9 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
string node_name = node_js.at("name").get<string>(); string node_name = node_js.at("name").get<string>();
string node_op = node_js.at("op").get<string>(); string node_op = node_js.at("op").get<string>();
string friendly_name = get_value<string>(node_js, "friendly_name"); string friendly_name = get_value<string>(node_js, "friendly_name");
vector<json> node_inputs = get_value<vector<json>>(node_js, "inputs");
vector<json> control_deps_inputs = get_value<vector<json>>(node_js, "control_deps"); vector<json> control_deps_inputs = get_value<vector<json>>(node_js, "control_deps");
vector<string> node_outputs = get_value<vector<string>>(node_js, "outputs"); vector<string> node_outputs = get_value<vector<string>>(node_js, "outputs");
OutputVectorHelper args; OutputVectorHelper args(deserialize_output_vector(node_js["inputs"]));
for (auto& node_input : node_inputs)
{
args.push_back(deserialize_output(node_input));
}
#if !(defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 8) #if !(defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ == 8)
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic error "-Wswitch" #pragma GCC diagnostic error "-Wswitch"
...@@ -681,12 +742,12 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -681,12 +742,12 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::Add: case OP_TYPEID::Add:
{ {
node = make_shared<op::Add>(args[0], args[1], read_auto_broadcast(node_js["autob"])); node = make_shared<op::Add>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::All: case OP_TYPEID::All:
{ {
auto reduction_axes = node_js.at("reduction_axes").get<set<size_t>>(); auto reduction_axes = deserialize_axis_set(node_js.at("reduction_axes"));
node = make_shared<op::All>(args[0], reduction_axes); node = make_shared<op::All>(args[0], reduction_axes);
break; break;
} }
...@@ -697,12 +758,12 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -697,12 +758,12 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::And: case OP_TYPEID::And:
{ {
node = make_shared<op::And>(args[0], args[1], read_auto_broadcast(node_js["autob"])); node = make_shared<op::And>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::Any: case OP_TYPEID::Any:
{ {
auto reduction_axes = node_js.at("reduction_axes").get<set<size_t>>(); auto reduction_axes = deserialize_axis_set(node_js.at("reduction_axes"));
node = make_shared<op::Any>(args[0], reduction_axes); node = make_shared<op::Any>(args[0], reduction_axes);
break; break;
} }
...@@ -739,12 +800,8 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -739,12 +800,8 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
auto padding_above = node_js.at("padding_above").get<vector<size_t>>(); auto padding_above = node_js.at("padding_above").get<vector<size_t>>();
auto include_padding_in_avg_computation = auto include_padding_in_avg_computation =
node_js.at("include_padding_in_avg_computation").get<bool>(); node_js.at("include_padding_in_avg_computation").get<bool>();
op::PadType pad_type = node_js["pad_type"].empty() op::PadType pad_type = read_pad_type(node_js);
? op::PadType::EXPLICIT bool ceil_mode = get_or_default<bool>(node_js, "ceil_mode", false);
: static_cast<op::PadType>(node_js.at("pad_type"));
bool ceil_mode =
node_js["ceil_mode"].empty() ? false : node_js.at("ceil_mode").get<bool>();
;
node = make_shared<op::AvgPool>(args[0], node = make_shared<op::AvgPool>(args[0],
window_shape, window_shape,
window_movement_strides, window_movement_strides,
...@@ -806,7 +863,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -806,7 +863,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
case OP_TYPEID::Broadcast: case OP_TYPEID::Broadcast:
{ {
auto shape = node_js.at("shape").get<vector<size_t>>(); auto shape = node_js.at("shape").get<vector<size_t>>();
auto axes = node_js.at("axes").get<set<size_t>>(); auto axes = deserialize_axis_set(node_js.at("axes"));
node = make_shared<op::Broadcast>(args[0], shape, axes); node = make_shared<op::Broadcast>(args[0], shape, axes);
break; break;
} }
...@@ -817,7 +874,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -817,7 +874,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::BroadcastLike: case OP_TYPEID::BroadcastLike:
{ {
auto initial_axes = node_js.at("initial_axes").get<set<size_t>>(); auto initial_axes = deserialize_axis_set(node_js.at("initial_axes"));
node = make_shared<op::BroadcastLike>(args[0], args[1], initial_axes); node = make_shared<op::BroadcastLike>(args[0], args[1], initial_axes);
break; break;
} }
...@@ -836,13 +893,13 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -836,13 +893,13 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
case OP_TYPEID::Concat: case OP_TYPEID::Concat:
{ {
auto axis = node_js.at("axis").get<size_t>(); auto axis = node_js.at("axis").get<size_t>();
node = make_shared<op::Concat>(args, axis); node = make_shared<op::Concat>(static_cast<OutputVector>(args), axis);
break; break;
} }
case OP_TYPEID::Constant: case OP_TYPEID::Constant:
{ {
auto type_node_js = auto type_node_js =
node_js.count("element_type") == 0 ? node_js.at("value_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 element_type = read_element_type(type_node_js.at("element_type"));
auto shape = type_node_js.at("shape"); auto shape = type_node_js.at("shape");
auto value = node_js.at("value").get<vector<string>>(); auto value = node_js.at("value").get<vector<string>>();
...@@ -866,17 +923,19 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -866,17 +923,19 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
// For backwards compatibility, we accept "image_dilation_strides" in place of // For backwards compatibility, we accept "image_dilation_strides" in place of
// "data_dilation_strides", and we also allow it to be omitted altogether. // "data_dilation_strides", and we also allow it to be omitted altogether.
auto data_dilation_strides_maybe = node_js["data_dilation_strides"]; json data_dilation_strides;
if (data_dilation_strides_maybe.empty()) if (has_key(node_js, "data_dilation_strides"))
{ {
data_dilation_strides_maybe = node_js["image_dilation_strides"]; data_dilation_strides = node_js["data_dilation_strides"];
}
else if (has_key(node_js, "image_dilation_strides"))
{
data_dilation_strides = node_js["image_dilation_strides"];
} }
op::PadType pad_type = node_js["pad_type"].empty() op::PadType pad_type = read_pad_type(node_js);
? op::PadType::EXPLICIT
: static_cast<op::PadType>(node_js.at("pad_type"));
if (data_dilation_strides_maybe.empty()) if (data_dilation_strides.empty())
{ {
node = make_shared<op::Convolution>(args[0], node = make_shared<op::Convolution>(args[0],
args[1], args[1],
...@@ -887,15 +946,15 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -887,15 +946,15 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
else else
{ {
node = make_shared<op::Convolution>( node =
args[0], make_shared<op::Convolution>(args[0],
args[1], args[1],
window_movement_strides, window_movement_strides,
window_dilation_strides, window_dilation_strides,
padding_below, padding_below,
padding_above, padding_above,
data_dilation_strides_maybe.get<std::vector<size_t>>(), data_dilation_strides.get<std::vector<size_t>>(),
pad_type); pad_type);
} }
break; break;
} }
...@@ -1031,33 +1090,28 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1031,33 +1090,28 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
case OP_TYPEID::Dequantize: case OP_TYPEID::Dequantize:
{ {
auto type = read_element_type(node_js.at("type")); auto type = read_element_type(node_js.at("type"));
auto axes = node_js.at("axes").get<set<size_t>>(); auto axes = deserialize_axis_set(node_js.at("axes"));
node = make_shared<op::Dequantize>(args[0], args[1], args[2], type, axes); node = make_shared<op::Dequantize>(args[0], args[1], args[2], type, axes);
break; break;
} }
case OP_TYPEID::Divide: case OP_TYPEID::Divide:
{ {
bool pythondiv = true; bool pythondiv = get_or_default(node_js, "pythondiv", true);
if (node_js["pythondiv"].is_object())
{
pythondiv = node_js.at("pythondiv").get<bool>();
}
node = make_shared<op::Divide>( node = make_shared<op::Divide>(
args[0], args[1], pythondiv, read_auto_broadcast(node_js["autob"])); args[0], args[1], pythondiv, read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::Dot: case OP_TYPEID::Dot:
{ {
// For backwards compatibility, reduction_axes_count is optional. // For backwards compatibility, reduction_axes_count is optional.
auto obj = node_js["reduction_axes_count"]; if (has_key(node_js, "reduction_axes_count"))
if (obj.empty())
{ {
node = make_shared<op::Dot>(args[0], args[1]); size_t reduction_axes_count = node_js["reduction_axes_count"].get<size_t>();
node = make_shared<op::Dot>(args[0], args[1], reduction_axes_count);
} }
else else
{ {
size_t reduction_axes_count = obj.get<size_t>(); node = make_shared<op::Dot>(args[0], args[1]);
node = make_shared<op::Dot>(args[0], args[1], reduction_axes_count);
} }
break; break;
} }
...@@ -1093,7 +1147,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1093,7 +1147,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::Equal: case OP_TYPEID::Equal:
{ {
node = make_shared<op::Equal>(args[0], args[1], read_auto_broadcast(node_js["autob"])); node = make_shared<op::Equal>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::Erf: case OP_TYPEID::Erf:
...@@ -1158,13 +1212,13 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1158,13 +1212,13 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
case OP_TYPEID::Greater: case OP_TYPEID::Greater:
{ {
node = node =
make_shared<op::Greater>(args[0], args[1], read_auto_broadcast(node_js["autob"])); make_shared<op::Greater>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::GreaterEq: case OP_TYPEID::GreaterEq:
{ {
node = node =
make_shared<op::GreaterEq>(args[0], args[1], read_auto_broadcast(node_js["autob"])); make_shared<op::GreaterEq>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::GRN: case OP_TYPEID::GRN:
...@@ -1191,10 +1245,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1191,10 +1245,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
auto data_dilation_strides = node_js.at("data_dilation_strides").get<vector<size_t>>(); auto data_dilation_strides = node_js.at("data_dilation_strides").get<vector<size_t>>();
auto groups = node_js.at("groups").get<size_t>(); auto groups = node_js.at("groups").get<size_t>();
op::PadType pad_type = node_js["pad_type"].empty() op::PadType pad_type = read_pad_type(node_js);
? op::PadType::EXPLICIT
: static_cast<op::PadType>(node_js.at("pad_type"));
node = make_shared<op::GroupConvolution>(args[0], node = make_shared<op::GroupConvolution>(args[0],
args[1], args[1],
window_movement_strides, window_movement_strides,
...@@ -1214,9 +1265,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1214,9 +1265,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
auto padding_end = node_js.at("padding_end").get<vector<ptrdiff_t>>(); auto padding_end = node_js.at("padding_end").get<vector<ptrdiff_t>>();
auto output_padding = node_js.at("output_padding").get<vector<ptrdiff_t>>(); auto output_padding = node_js.at("output_padding").get<vector<ptrdiff_t>>();
auto groups = node_js.at("groups").get<size_t>(); auto groups = node_js.at("groups").get<size_t>();
op::PadType pad_type = node_js["pad_type"].empty() op::PadType pad_type = read_pad_type(node_js);
? op::PadType::EXPLICIT
: static_cast<op::PadType>(node_js.at("pad_type"));
auto output_shape = node_js.at("output_shape").get<vector<size_t>>(); auto output_shape = node_js.at("output_shape").get<vector<size_t>>();
node = make_shared<op::GroupConvolutionTranspose>(args[0], node = make_shared<op::GroupConvolutionTranspose>(args[0],
...@@ -1238,12 +1287,12 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1238,12 +1287,12 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::Less: case OP_TYPEID::Less:
{ {
node = make_shared<op::Less>(args[0], args[1], read_auto_broadcast(node_js["autob"])); node = make_shared<op::Less>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::LessEq: case OP_TYPEID::LessEq:
{ {
node = make_shared<op::LessEq>(args[0], args[1], read_auto_broadcast(node_js["autob"])); node = make_shared<op::LessEq>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::Log: case OP_TYPEID::Log:
...@@ -1285,7 +1334,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1285,7 +1334,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::Max: case OP_TYPEID::Max:
{ {
auto reduction_axes = node_js.at("reduction_axes").get<set<size_t>>(); auto reduction_axes = deserialize_axis_set(node_js.at("reduction_axes"));
node = make_shared<op::Max>(args[0], reduction_axes); node = make_shared<op::Max>(args[0], reduction_axes);
break; break;
} }
...@@ -1296,11 +1345,9 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1296,11 +1345,9 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
node_js.at("window_movement_strides").get<vector<size_t>>(); node_js.at("window_movement_strides").get<vector<size_t>>();
// For backwards compatibility, both (but not just one) of the padding_ fields may be // For backwards compatibility, both (but not just one) of the padding_ fields may be
// omitted. // omitted.
auto padding_below_maybe = node_js["padding_below"]; auto padding_below_maybe = get_or_default(node_js, "padding_below", json{});
auto padding_above_maybe = node_js["padding_above"]; auto padding_above_maybe = get_or_default(node_js, "padding_above", json{});
op::PadType pad_type = node_js["pad_type"].empty() op::PadType pad_type = read_pad_type(node_js);
? op::PadType::EXPLICIT
: static_cast<op::PadType>(node_js.at("pad_type"));
if (padding_below_maybe.empty() && !padding_above_maybe.empty()) if (padding_below_maybe.empty() && !padding_above_maybe.empty())
{ {
throw runtime_error( throw runtime_error(
...@@ -1359,31 +1406,31 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1359,31 +1406,31 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
case OP_TYPEID::Maximum: case OP_TYPEID::Maximum:
{ {
node = node =
make_shared<op::Maximum>(args[0], args[1], read_auto_broadcast(node_js["autob"])); make_shared<op::Maximum>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::Min: case OP_TYPEID::Min:
{ {
auto reduction_axes = node_js.at("reduction_axes").get<set<size_t>>(); auto reduction_axes = deserialize_axis_set(node_js.at("reduction_axes"));
node = make_shared<op::Min>(args[0], reduction_axes); node = make_shared<op::Min>(args[0], reduction_axes);
break; break;
} }
case OP_TYPEID::Minimum: case OP_TYPEID::Minimum:
{ {
node = node =
make_shared<op::Minimum>(args[0], args[1], read_auto_broadcast(node_js["autob"])); make_shared<op::Minimum>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::Multiply: case OP_TYPEID::Multiply:
{ {
node = node =
make_shared<op::Multiply>(args[0], args[1], read_auto_broadcast(node_js["autob"])); make_shared<op::Multiply>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::MVN: case OP_TYPEID::MVN:
{ {
auto normalize_variance = node_js.at("normalize_variance").get<bool>(); auto normalize_variance = node_js.at("normalize_variance").get<bool>();
auto reduction_axes = node_js.at("reduction_axes").get<set<size_t>>(); auto reduction_axes = deserialize_axis_set(node_js.at("reduction_axes"));
auto eps = node_js.at("eps").get<double>(); auto eps = node_js.at("eps").get<double>();
node = make_shared<op::MVN>(args[0], normalize_variance, normalize_variance, eps); node = make_shared<op::MVN>(args[0], normalize_variance, normalize_variance, eps);
break; break;
...@@ -1405,7 +1452,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1405,7 +1452,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
case OP_TYPEID::NotEqual: case OP_TYPEID::NotEqual:
{ {
node = node =
make_shared<op::NotEqual>(args[0], args[1], read_auto_broadcast(node_js["autob"])); make_shared<op::NotEqual>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::Not: case OP_TYPEID::Not:
...@@ -1422,7 +1469,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1422,7 +1469,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::Or: case OP_TYPEID::Or:
{ {
node = make_shared<op::Or>(args[0], args[1], read_auto_broadcast(node_js["autob"])); node = make_shared<op::Or>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::Pad: case OP_TYPEID::Pad:
...@@ -1439,9 +1486,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1439,9 +1486,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
[](size_t s) { return s == 0; }), [](size_t s) { return s == 0; }),
"Legacy padding_interior field must be zero everywhere."); "Legacy padding_interior field must be zero everywhere.");
auto pad_mode = node_js.count("pad_mode") == 0 auto pad_mode = read_pad_mode(node_js);
? op::PadMode::CONSTANT
: static_cast<op::PadMode>(node_js.at("pad_mode"));
node = make_shared<op::Pad>(args[0], args[1], padding_below, padding_above, pad_mode); node = make_shared<op::Pad>(args[0], args[1], padding_below, padding_above, pad_mode);
break; break;
...@@ -1449,7 +1494,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1449,7 +1494,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
case OP_TYPEID::Parameter: case OP_TYPEID::Parameter:
{ {
auto type_node_js = auto type_node_js =
node_js.count("element_type") == 0 ? node_js.at("value_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 element_type = read_element_type(type_node_js.at("element_type"));
auto shape = type_node_js.at("shape"); auto shape = type_node_js.at("shape");
auto cacheable = get_or_default<bool>(node_js, "cacheable", false); auto cacheable = get_or_default<bool>(node_js, "cacheable", false);
...@@ -1474,7 +1519,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1474,7 +1519,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::Power: case OP_TYPEID::Power:
{ {
node = make_shared<op::Power>(args[0], args[1], read_auto_broadcast(node_js["autob"])); node = make_shared<op::Power>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::PRelu: case OP_TYPEID::PRelu:
...@@ -1484,14 +1529,14 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1484,14 +1529,14 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::Product: case OP_TYPEID::Product:
{ {
auto reduction_axes = node_js.at("reduction_axes").get<set<size_t>>(); auto reduction_axes = deserialize_axis_set(node_js.at("reduction_axes"));
node = make_shared<op::Product>(args[0], reduction_axes); node = make_shared<op::Product>(args[0], reduction_axes);
break; break;
} }
case OP_TYPEID::Quantize: case OP_TYPEID::Quantize:
{ {
auto type = read_element_type(node_js.at("type")); auto type = read_element_type(node_js.at("type"));
auto axes = node_js.at("axes").get<set<size_t>>(); auto axes = deserialize_axis_set(node_js.at("axes"));
auto round_mode = node_js.at("round_mode").get<op::Quantize::RoundMode>(); auto round_mode = node_js.at("round_mode").get<op::Quantize::RoundMode>();
node = make_shared<op::Quantize>(args[0], args[1], args[2], type, axes, round_mode); node = make_shared<op::Quantize>(args[0], args[1], args[2], type, axes, round_mode);
break; break;
...@@ -1550,8 +1595,8 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1550,8 +1595,8 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
node_js.at("window_movement_strides").get<vector<size_t>>(); node_js.at("window_movement_strides").get<vector<size_t>>();
// For backwards compatibility, both (but not just one) of the padding_ fields may be // For backwards compatibility, both (but not just one) of the padding_ fields may be
// omitted. // omitted.
auto padding_below_maybe = node_js["padding_below"]; auto padding_below_maybe = get_or_default(node_js, "padding_below", json{});
auto padding_above_maybe = node_js["padding_above"]; auto padding_above_maybe = get_or_default(node_js, "padding_above", json{});
auto padding_below = padding_below_maybe.get<vector<size_t>>(); auto padding_below = padding_below_maybe.get<vector<size_t>>();
auto padding_above = padding_above_maybe.get<vector<size_t>>(); auto padding_above = padding_above_maybe.get<vector<size_t>>();
node = make_shared<op::QuantizedMaxPool>( node = make_shared<op::QuantizedMaxPool>(
...@@ -1599,7 +1644,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1599,7 +1644,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::Reverse: case OP_TYPEID::Reverse:
{ {
auto reversed_axes = node_js.at("reversed_axes").get<set<size_t>>(); auto reversed_axes = deserialize_axis_set(node_js.at("reversed_axes"));
node = make_shared<op::Reverse>(args[0], reversed_axes); node = make_shared<op::Reverse>(args[0], reversed_axes);
break; break;
} }
...@@ -1683,7 +1728,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1683,7 +1728,7 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
} }
case OP_TYPEID::Softmax: case OP_TYPEID::Softmax:
{ {
auto softmax_axes = node_js.at("softmax_axes").get<set<size_t>>(); auto softmax_axes = deserialize_axis_set(node_js.at("softmax_axes"));
node = make_shared<op::Softmax>(args[0], softmax_axes); node = make_shared<op::Softmax>(args[0], softmax_axes);
break; break;
} }
...@@ -1718,12 +1763,12 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js) ...@@ -1718,12 +1763,12 @@ shared_ptr<Node> JSONDeserializer::deserialize_node(json& node_js)
case OP_TYPEID::Subtract: case OP_TYPEID::Subtract:
{ {
node = node =
make_shared<op::Subtract>(args[0], args[1], read_auto_broadcast(node_js["autob"])); make_shared<op::Subtract>(args[0], args[1], read_auto_broadcast(node_js, "autob"));
break; break;
} }
case OP_TYPEID::Sum: case OP_TYPEID::Sum:
{ {
auto reduction_axes = node_js.at("reduction_axes").get<set<size_t>>(); auto reduction_axes = deserialize_axis_set(node_js.at("reduction_axes"));
node = make_shared<op::Sum>(args[0], reduction_axes); node = make_shared<op::Sum>(args[0], reduction_axes);
break; break;
} }
...@@ -1859,6 +1904,16 @@ json JSONSerializer::serialize_output(const Output<Node>& output) ...@@ -1859,6 +1904,16 @@ json JSONSerializer::serialize_output(const Output<Node>& output)
return result; return result;
} }
json JSONSerializer::serialize_output_vector(const OutputVector& output_vector)
{
json result;
for (const Output<Node>& output : output_vector)
{
result.push_back(serialize_output(output));
}
return result;
}
json JSONSerializer::serialize_node(const Node& n) json JSONSerializer::serialize_node(const Node& n)
{ {
m_nodes_serialized.insert(&n); m_nodes_serialized.insert(&n);
...@@ -1958,7 +2013,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -1958,7 +2013,7 @@ json JSONSerializer::serialize_node(const Node& n)
case OP_TYPEID::All: case OP_TYPEID::All:
{ {
auto tmp = dynamic_cast<const op::All*>(&n); auto tmp = dynamic_cast<const op::All*>(&n);
node["reduction_axes"] = tmp->get_reduction_axes(); node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes());
break; break;
} }
case OP_TYPEID::AllReduce: { break; case OP_TYPEID::AllReduce: { break;
...@@ -1975,7 +2030,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -1975,7 +2030,7 @@ json JSONSerializer::serialize_node(const Node& n)
case OP_TYPEID::Any: case OP_TYPEID::Any:
{ {
auto tmp = dynamic_cast<const op::Any*>(&n); auto tmp = dynamic_cast<const op::Any*>(&n);
node["reduction_axes"] = tmp->get_reduction_axes(); node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes());
break; break;
} }
case OP_TYPEID::Asin: { break; case OP_TYPEID::Asin: { break;
...@@ -2031,7 +2086,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -2031,7 +2086,7 @@ json JSONSerializer::serialize_node(const Node& n)
case OP_TYPEID::Broadcast: case OP_TYPEID::Broadcast:
{ {
auto tmp = dynamic_cast<const op::Broadcast*>(&n); auto tmp = dynamic_cast<const op::Broadcast*>(&n);
node["axes"] = tmp->get_broadcast_axes(); node["axes"] = serialize_axis_set(tmp->get_broadcast_axes());
node["shape"] = tmp->get_broadcast_shape(); node["shape"] = tmp->get_broadcast_shape();
break; break;
} }
...@@ -2040,7 +2095,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -2040,7 +2095,7 @@ json JSONSerializer::serialize_node(const Node& n)
case OP_TYPEID::BroadcastLike: case OP_TYPEID::BroadcastLike:
{ {
auto tmp = dynamic_cast<const op::BroadcastLike*>(&n); auto tmp = dynamic_cast<const op::BroadcastLike*>(&n);
node["initial_axes"] = tmp->get_initial_broadcast_axes(); node["initial_axes"] = serialize_axis_set(tmp->get_initial_broadcast_axes());
break; break;
} }
case OP_TYPEID::Ceiling: { break; case OP_TYPEID::Ceiling: { break;
...@@ -2154,7 +2209,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -2154,7 +2209,7 @@ json JSONSerializer::serialize_node(const Node& n)
{ {
auto tmp = dynamic_cast<const op::Dequantize*>(&n); auto tmp = dynamic_cast<const op::Dequantize*>(&n);
node["type"] = write_element_type(tmp->get_element_type()); node["type"] = write_element_type(tmp->get_element_type());
node["axes"] = tmp->get_axes(); node["axes"] = serialize_axis_set(tmp->get_axes());
break; break;
} }
case OP_TYPEID::DepthToSpace: case OP_TYPEID::DepthToSpace:
...@@ -2347,7 +2402,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -2347,7 +2402,7 @@ json JSONSerializer::serialize_node(const Node& n)
case OP_TYPEID::Max: case OP_TYPEID::Max:
{ {
auto tmp = dynamic_cast<const op::Max*>(&n); auto tmp = dynamic_cast<const op::Max*>(&n);
node["reduction_axes"] = tmp->get_reduction_axes(); node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes());
break; break;
} }
case OP_TYPEID::MaxPool: case OP_TYPEID::MaxPool:
...@@ -2381,7 +2436,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -2381,7 +2436,7 @@ json JSONSerializer::serialize_node(const Node& n)
case OP_TYPEID::Min: case OP_TYPEID::Min:
{ {
auto tmp = dynamic_cast<const op::Min*>(&n); auto tmp = dynamic_cast<const op::Min*>(&n);
node["reduction_axes"] = tmp->get_reduction_axes(); node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes());
break; break;
} }
case OP_TYPEID::Minimum: case OP_TYPEID::Minimum:
...@@ -2405,7 +2460,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -2405,7 +2460,7 @@ json JSONSerializer::serialize_node(const Node& n)
case OP_TYPEID::MVN: case OP_TYPEID::MVN:
{ {
auto tmp = dynamic_cast<const op::MVN*>(&n); auto tmp = dynamic_cast<const op::MVN*>(&n);
node["reduction_axes"] = tmp->get_reduction_axes(); node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes());
node["normalize_variance"] = tmp->get_normalize_variance(); node["normalize_variance"] = tmp->get_normalize_variance();
node["eps"] = tmp->get_eps(); node["eps"] = tmp->get_eps();
break; break;
...@@ -2485,7 +2540,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -2485,7 +2540,7 @@ json JSONSerializer::serialize_node(const Node& n)
case OP_TYPEID::Product: case OP_TYPEID::Product:
{ {
auto tmp = dynamic_cast<const op::Product*>(&n); auto tmp = dynamic_cast<const op::Product*>(&n);
node["reduction_axes"] = tmp->get_reduction_axes(); node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes());
break; break;
} }
case OP_TYPEID::Power: case OP_TYPEID::Power:
...@@ -2501,7 +2556,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -2501,7 +2556,7 @@ json JSONSerializer::serialize_node(const Node& n)
{ {
auto tmp = dynamic_cast<const op::Quantize*>(&n); auto tmp = dynamic_cast<const op::Quantize*>(&n);
node["type"] = write_element_type(tmp->get_element_type()); node["type"] = write_element_type(tmp->get_element_type());
node["axes"] = tmp->get_axes(); node["axes"] = serialize_axis_set(tmp->get_axes());
node["round_mode"] = tmp->get_round_mode(); node["round_mode"] = tmp->get_round_mode();
break; break;
} }
...@@ -2576,7 +2631,7 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -2576,7 +2631,7 @@ json JSONSerializer::serialize_node(const Node& n)
case OP_TYPEID::Reverse: case OP_TYPEID::Reverse:
{ {
auto tmp = dynamic_cast<const op::Reverse*>(&n); auto tmp = dynamic_cast<const op::Reverse*>(&n);
node["reversed_axes"] = tmp->get_reversed_axes(); node["reversed_axes"] = serialize_axis_set(tmp->get_reversed_axes());
break; break;
} }
case OP_TYPEID::ReverseSequence: case OP_TYPEID::ReverseSequence:
...@@ -2663,13 +2718,13 @@ json JSONSerializer::serialize_node(const Node& n) ...@@ -2663,13 +2718,13 @@ json JSONSerializer::serialize_node(const Node& n)
case OP_TYPEID::Sum: case OP_TYPEID::Sum:
{ {
auto tmp = dynamic_cast<const op::Sum*>(&n); auto tmp = dynamic_cast<const op::Sum*>(&n);
node["reduction_axes"] = tmp->get_reduction_axes(); node["reduction_axes"] = serialize_axis_set(tmp->get_reduction_axes());
break; break;
} }
case OP_TYPEID::Softmax: case OP_TYPEID::Softmax:
{ {
auto tmp = dynamic_cast<const op::Softmax*>(&n); auto tmp = dynamic_cast<const op::Softmax*>(&n);
node["softmax_axes"] = tmp->get_axes(); node["softmax_axes"] = serialize_axis_set(tmp->get_axes());
break; break;
} }
case OP_TYPEID::Tan: { break; case OP_TYPEID::Tan: { break;
......
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