def protobuf_to_op(pb_op):
"""
This will convert a protobuf Op object into its corresponding Python object. But this cannot
setup links to other ops (such as args, control_deps) since those ops may not
exist yet.
We have to wait until all ops are created before connecting them back up together in a second
pass, so args, etc will be uninitialized.
"""
cls = get_ngraph_op_cls(pb_op.op_type)
# Skip the class constructor but we'll use the generic op constructor because it sets a lot of
# helpful defaults
py_op = cls.__new__(cls)
op_graph.Op.__init__(py_op)
py_op.name = str(pb_op.name)
if 'valfun_value' in pb_op.attrs:
valfun_value = pb_to_tensor(pb_op.attrs['valfun_value'].tensor)
py_op.valfun = lambda x: valfun_value
# op.uuid
py_op.uuid = uuid.UUID(bytes=pb_op.uuid.uuid)
# op.metadata and remaining keys
ignored_keys = {'valfun_value', 'dtype', 'metadata'}
remaining_keys = set(pb_op.attrs.keys()).difference(ignored_keys)
for key in remaining_keys:
if key == '_ngraph_ser_handle':
py_op._ngraph_ser_handle = True
if key.startswith('_ngraph_metadata_'):
value = pb_op.attrs[key]
py_op.metadata[key[17:]] = protobuf_attr_to_python(value)
elif not key.startswith('_is_') and key not in EXCEPTION_ATTRIBUTES and \
key.startswith('_'):
continue
else:
value = pb_op.attrs[key]
setattr(py_op, key, protobuf_attr_to_python(value))
return py_op
评论列表
文章目录