Exemplo n.º 1
0
def _EagerRunModelSave(var_blobs, snapshot_path):
    path_input_op_conf, path_lbi = _GenModelIOPathInputOpConfAndRetLbi()
    path_input_blob_objects = {}
    (
        BuildModelIOPathInputInstruction,
        BuildFeedPathInstruction,
    ) = _MakeModelIOPathInputBuilds(path_input_op_conf, snapshot_path,
                                    path_input_blob_objects)

    model_save_op_conf = _GenModelSaveOpConf(var_blobs, path_lbi)
    model_save_blob_objects = {}

    def BuildModelSaveInstruction(builder):
        path_blob_object = path_input_blob_objects["out"]
        model_save_blob_objects["path"] = path_blob_object
        for i, blob in enumerate(var_blobs):
            model_save_blob_objects["in_{}".format(i)] = blob.blob_object

        op_attribute = op_infer_util.Infer(
            model_save_op_conf, ibn2blob_object=model_save_blob_objects)
        parallel_conf = path_blob_object.parallel_desc_symbol.parallel_conf
        builder.StatelessCall(op_attribute,
                              parallel_conf,
                              bn_in_op2blob_object=model_save_blob_objects)

    sess = session_ctx.GetDefaultSession()
    with scope_util.ScopeContext(scope_util.MakeScope(_BuildNotMirroredScope)):
        vm_util.LogicalRun(BuildModelIOPathInputInstruction)
        vm_util.LogicalRun(BuildFeedPathInstruction)
        vm_util.LogicalRun(BuildModelSaveInstruction)
Exemplo n.º 2
0
def name_scope(name: str) -> None:
    r"""Create a namespace. All variables within the namespace will have a prefix `[SCOPE NAME]-`. This is for convenience only and has no other effect on the system.
    Usage::

        with oneflow.scope.namespace("scope1"):
            ...
            with oneflow.scope.namespace("scope2"):
                ...

    Args:
        name: Name of this namespace

    """
    assert isinstance(name, str)
    name_scope_stack_push(name)

    def BuildScope(old_scope, builder):
        return builder.BuildScopeWithNewScopeName(old_scope, name)

    sess = session_context.GetDefaultSession()
    try:
        with scope_util.ScopeContext(scope_util.MakeScope(BuildScope)):
            yield
    finally:
        name_scope_stack_pop()
Exemplo n.º 3
0
def _EagerRunModelLoad(var_op_conf, snapshot_path):
    assert isinstance(snapshot_path, str)
    assert os.path.basename(snapshot_path) == "out"
    snapshot_path = os.path.dirname(snapshot_path)
    assert os.path.basename(snapshot_path) == var_op_conf.name
    snapshot_path = os.path.dirname(snapshot_path)

    path_input_op_conf, path_lbi = _GenModelIOPathInputOpConfAndRetLbi()
    path_input_blob_objects = {}
    (
        BuildModelIOPathInputInstruction,
        BuildFeedPathInstruction,
    ) = _MakeModelIOPathInputBuilds(path_input_op_conf, snapshot_path,
                                    path_input_blob_objects)

    model_load_op_conf, _ = _GenModelLoadOpConfAndRetLbi(var_op_conf, path_lbi)
    model_load_blob_objects = {}

    def BuildModelLoadInstruction(builder):
        path_blob_object = path_input_blob_objects["out"]
        model_load_blob_objects["path"] = path_blob_object
        op_attribute = op_infer_util.Infer(
            model_load_op_conf, ibn2blob_object=model_load_blob_objects)
        parallel_conf = path_blob_object.parallel_desc_symbol.parallel_conf
        builder.StatelessCall(op_attribute,
                              parallel_conf,
                              bn_in_op2blob_object=model_load_blob_objects)

    sess = session_ctx.GetDefaultSession()
    with scope_util.ScopeContext(scope_util.MakeScope(_BuildNotMirroredScope)):
        vm_util.LogicalRun(BuildModelIOPathInputInstruction)
        vm_util.LogicalRun(BuildFeedPathInstruction)
        vm_util.LogicalRun(BuildModelLoadInstruction)

    return model_load_blob_objects["out_0"]
Exemplo n.º 4
0
def _EagerRunModelSave(var_blobs, snapshot_path):
    path_input_op_conf, path_lbi = _GenModelIOPathInputOpConfAndRetLbi()
    path_input_blob_objects = {}
    (
        BuildModelIOPathInputInstruction,
        BuildFeedPathInstruction,
    ) = _MakeModelIOPathInputBuilds(path_input_op_conf, snapshot_path,
                                    path_input_blob_objects)

    model_save_op_conf = _GenModelSaveOpConf(var_blobs, path_lbi)
    model_save_blob_objects = oneflow_api.deprecated.BnInOp2BlobObject()

    def BuildModelSaveInstruction(builder):
        path_blob_object = path_input_blob_objects["out"]
        model_save_blob_objects["path"] = path_blob_object
        for i, blob in enumerate(var_blobs):
            model_save_blob_objects["in_{}".format(i)] = blob.blob_object

        op_attribute = op_infer_util.Infer(
            model_save_op_conf, ibn2blob_object=model_save_blob_objects)
        parallel_conf = path_blob_object.parallel_desc_symbol.parallel_conf
        cfg_op_attribute = oneflow_api.deprecated.MakeOpAttributeByString(
            str(op_attribute))
        builder.StatelessCall(
            cfg_op_attribute,
            parallel_conf,
            model_save_blob_objects,
            boxing_util.BoxingTo,
        )

    sess = session_ctx.GetDefaultSession()
    with scope_util.ScopeContext(scope_util.MakeScope(_BuildNotMirroredScope)):
        oneflow_api.deprecated.LogicalRun(BuildModelIOPathInputInstruction)
        oneflow_api.deprecated.LogicalRun(BuildFeedPathInstruction)
        oneflow_api.deprecated.LogicalRun(BuildModelSaveInstruction)
Exemplo n.º 5
0
def _EagerRunModelInit(var_op_conf):
    op_conf, _ = _GenModelInitOpConfAndRetLbi(var_op_conf)
    bn_in_op2blob_object = oneflow_api.deprecated.BnInOp2BlobObject()

    def BuildModelInitInstruction(builder):
        upstream_signature = op_node_signature_pb.OpNodeSignature()
        op_conf.scope_symbol_id = oneflow.current_scope().symbol_id
        op_attribute = c_api_util.InferOpConf(op_conf, upstream_signature)
        parallel_conf = (
            oneflow.current_scope().device_parallel_desc_symbol.parallel_conf)
        cfg_op_attribute = oneflow_api.deprecated.MakeOpAttributeByString(
            str(op_attribute))
        builder.StatelessCall(
            cfg_op_attribute,
            parallel_conf,
            bn_in_op2blob_object,
            boxing_util.BoxingTo,
            vm_util._FindOrCreateDelegateBlobObject,
        )

    sess = session_ctx.GetDefaultSession()
    with scope_util.ScopeContext(scope_util.MakeScope(_BuildNotMirroredScope)):
        vm_util.LogicalRun(BuildModelInitInstruction)

    return bn_in_op2blob_object["out_0"]
Exemplo n.º 6
0
def GetNormalModePlacementScope(device_tag, machine_device_ids):
    sess = session_ctx.GetDefaultSession()
    scope = scope_util.MakeScope(
        lambda old_scope, builder: old_scope.BuildWithNewParallelDesc(
            builder, device_tag, machine_device_ids
        )
    )
    return scope_util.ScopeContext(scope)
Exemplo n.º 7
0
def GetNormalModePlacementScope(device_tag, machine_device_ids):
    if isinstance(machine_device_ids, tuple):
        machine_device_ids = list(machine_device_ids)
    if not isinstance(machine_device_ids, list):
        machine_device_ids = [machine_device_ids]
    sess = session_ctx.GetDefaultSession()
    scope = scope_util.MakeScope(
        lambda old_scope, builder: builder.BuildScopeWithNewParallelDesc(
            old_scope, device_tag, machine_device_ids))
    return scope_util.ScopeContext(scope)
Exemplo n.º 8
0
def GetGlobalModePlacementScope(device_tag, machine_device_ids):
    if isinstance(machine_device_ids, (list, tuple)) == False:
        machine_device_ids = [machine_device_ids]
    sess = session_ctx.GetDefaultSession()

    def BuildScope(old_scope, builder):
        return old_scope.BuildWithNewParallelDesc(builder, device_tag,
                                                  machine_device_ids)

    scope_ctx = scope_util.ScopeContext(scope_util.MakeScope(BuildScope))
    return placement_ctx.GlobalModePlacementScope(scope_ctx)
Exemplo n.º 9
0
    def __init__(self, is_mirrored):
        self.is_mirrored_ = is_mirrored
        self.scope_context_ = None
        sess = session_ctx.GetDefaultSession()
        # bypass the first DistributeStrategy for avoiding None old_scope
        if sess.is_running and len(sess.is_mirrored_strategy_enabled_stack) > 0:

            def BuildScope(old_scope, builder):
                return old_scope.BuildWithNewIsMirrored(builder, is_mirrored)

            self.scope_context_ = scope_util.ScopeContext(
                scope_util.MakeScope(BuildScope)
            )
Exemplo n.º 10
0
def GetGlobalModePlacementScope(device_tag, machine_device_ids, hierarchy=None):
    if isinstance(machine_device_ids, (list, tuple)) == False:
        machine_device_ids = [machine_device_ids]
    sess = session_ctx.GetDefaultSession()
    if hierarchy is not None:
        hierarchy = oneflow_api.Size(tuple(hierarchy))

    def BuildScope(old_scope, builder):
        return builder.BuildScopeWithNewParallelDesc(
            old_scope, device_tag, machine_device_ids, hierarchy
        )

    scope_ctx = scope_util.ScopeContext(scope_util.MakeScope(BuildScope))
    return placement_ctx.GlobalModePlacementScope(scope_ctx)
Exemplo n.º 11
0
def GetNormalModePlacementScope(device_tag,
                                machine_device_ids,
                                hierarchy=None):
    if isinstance(machine_device_ids, tuple):
        machine_device_ids = list(machine_device_ids)
    if not isinstance(machine_device_ids, list):
        machine_device_ids = [machine_device_ids]
    sess = session_ctx.GetDefaultSession()
    if hierarchy is not None:
        hierarchy = oneflow._oneflow_internal.Size(tuple(hierarchy))
    scope = scope_util.MakeScope(
        lambda old_scope, builder: builder.BuildScopeWithNewParallelDesc(
            old_scope, device_tag, machine_device_ids, hierarchy))
    return scope_util.ScopeContext(scope)
Exemplo n.º 12
0
def _EagerRunModelLoad(var_op_conf, snapshot_path):
    assert isinstance(snapshot_path, str)
    assert os.path.basename(snapshot_path) == "out"
    snapshot_path = os.path.dirname(snapshot_path)
    assert os.path.basename(snapshot_path) == var_op_conf.name
    snapshot_path = os.path.dirname(snapshot_path)

    path_input_op_conf, path_lbi = _GenModelIOPathInputOpConfAndRetLbi()
    path_input_blob_objects = {}
    (
        BuildModelIOPathInputInstruction,
        BuildFeedPathInstruction,
    ) = _MakeModelIOPathInputBuilds(path_input_op_conf, snapshot_path,
                                    path_input_blob_objects)

    model_load_op_conf, _ = _GenModelLoadOpConfAndRetLbi(var_op_conf, path_lbi)
    model_load_blob_objects = oneflow._oneflow_internal.deprecated.BnInOp2BlobObject(
    )

    def BuildModelLoadInstruction(builder):
        path_blob_object = path_input_blob_objects["out"]
        model_load_blob_objects["path"] = path_blob_object
        op_attribute = op_infer_util.Infer(
            model_load_op_conf, ibn2blob_object=model_load_blob_objects)
        parallel_conf = path_blob_object.parallel_desc_symbol.parallel_conf
        cfg_op_attribute = oneflow._oneflow_internal.deprecated.MakeOpAttributeByString(
            str(op_attribute))
        builder.StatelessCall(
            cfg_op_attribute,
            parallel_conf,
            model_load_blob_objects,
            boxing_util.BoxingTo,
        )

    sess = session_ctx.GetDefaultSession()
    with scope_util.ScopeContext(scope_util.MakeScope(_BuildNotMirroredScope)):
        oneflow._oneflow_internal.deprecated.LogicalRun(
            BuildModelIOPathInputInstruction)
        oneflow._oneflow_internal.deprecated.LogicalRun(
            BuildFeedPathInstruction)
        oneflow._oneflow_internal.deprecated.LogicalRun(
            BuildModelLoadInstruction)

    return model_load_blob_objects["out_0"]
Exemplo n.º 13
0
def _EagerRunModelInit(var_op_conf):
    op_conf, _ = _GenModelInitOpConfAndRetLbi(var_op_conf)
    bn_in_op2blob_object = {}

    def BuildModelInitInstruction(builder):
        upstream_signature = op_attribute_pb.OpNodeSignature()
        op_conf.scope_symbol_id = oneflow.current_scope().symbol_id
        op_attribute = c_api_util.InferOpConf(op_conf, upstream_signature)
        parallel_conf = (
            oneflow.current_scope().device_parallel_desc_symbol.parallel_conf)
        builder.StatelessCall(op_attribute,
                              parallel_conf,
                              bn_in_op2blob_object=bn_in_op2blob_object)

    sess = session_ctx.GetDefaultSession()
    with scope_util.ScopeContext(scope_util.MakeScope(_BuildNotMirroredScope)):
        vm_util.LogicalRun(BuildModelInitInstruction)

    return bn_in_op2blob_object["out_0"]
Exemplo n.º 14
0
def _GetReturnOpConfAndOutLbiAndScope(remote_blob, allow_cpu_return_op=True):
    op_conf = op_conf_util.OperatorConf()
    op_conf.name = id_util.UniqueStr("Return_")
    setattr(op_conf.return_conf, "in", remote_blob.unique_name)
    op_conf.return_conf.out = "out"
    if allow_cpu_return_op:
        op_conf.device_tag = "cpu"

    lbi = logical_blob_id_util.LogicalBlobId()
    lbi.op_name = op_conf.name
    lbi.blob_name = "out"

    parallel_conf = placement_proto_pb.ParallelConf()
    parallel_conf.CopyFrom(remote_blob.parallel_conf)

    def BuildScope(old_scope, builder):
        return old_scope.BuildWithNewParallelConf(builder, parallel_conf)

    sess = session_ctx.GetDefaultSession()
    scope = scope_util.MakeScope(BuildScope)

    return op_conf, lbi, scope