serde.py 文件源码

python
阅读 30 收藏 0 点赞 0 评论 0

项目:ngraph 作者: NervanaSystems 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号