Exemple #1
0
def InterpretScope(session, function_desc, config_proto):
    job_conf = function_desc.job_config_proto
    job_conf.set_job_name(function_desc.job_func.__name__)
    placement_scope = function_desc.function_attribute.default_placement_scope
    if placement_scope is None:
        tag_and_dev_ids = placement_util.GetDefaultMachineDeviceIds(
            session.resource)
        hierarchy = None
    else:
        assert isinstance(placement_scope, placement_ctx.EmptyPlacementScope)
        tag_and_dev_ids = (
            placement_scope.device_tag,
            placement_scope.machine_device_ids,
        )
        hierarchy = placement_scope.hierarchy
    distribute_strategy = function_desc.function_attribute.default_distribute_strategy
    if distribute_strategy is None:
        distribute_strategy = distribute_util.DistributeConsistentStrategy()
    is_mirrored = isinstance(distribute_strategy,
                             distribute_util.DistributeMirroredStrategy)
    assert isinstance(hierarchy, (list, tuple)) or hierarchy is None
    if hierarchy is not None:
        hierarchy = oneflow._oneflow_internal.Size(tuple(hierarchy))
    scope = scope_util.MakeInitialScope(job_conf, *tag_and_dev_ids, hierarchy,
                                        is_mirrored)
    with _JobBuildAndInferCtx(job_conf.job_name()), distribute_strategy:
        c_api_util.CurJobBuildAndInferCtx_SetJobConf(job_conf)
        with runtime_mode.ModeScope(runtime_mode.GLOBAL_MODE):
            with scope_util.ScopeContext(scope):
                yield
Exemple #2
0
def name_scope(name: str) -> None:
    """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.compatible.single_client.scope.namespace("scope1"):
            ...
            with oneflow.compatible.single_client.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()
Exemple #3
0
    def __init__(self, is_mirrored):
        self.is_mirrored_ = is_mirrored
        self.scope_context_ = None
        sess = session_ctx.GetDefaultSession()
        if sess.is_running and (
                not sess.has_empty_is_mirrored_strategy_enabled_stack()):

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

            self.scope_context_ = scope_util.ScopeContext(
                scope_util.MakeScope(BuildScope))
Exemple #4
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._oneflow_internal.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)
Exemple #5
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)
Exemple #6
0
 def open(self, job_name, signature=None, batch_size=None):
     self._check_status(self.SessionStatus.OPEN)
     c_api_util.JobBuildAndInferCtx_Open(job_name)
     if signature is not None:
         self.set_job_signature(job_name, signature)
     if isinstance(batch_size, int):
         self.set_job_batch_size(job_name, batch_size)
     job_conf = self._get_job_conf(job_name)
     c_api_util.CurJobBuildAndInferCtx_SetJobConf(job_conf)
     tag_and_dev_ids = placement_util.GetDefaultMachineDeviceIds(
         self.config_proto_.resource)
     scope = scope_util.MakeInitialScope(job_conf, *tag_and_dev_ids, None,
                                         self.is_mirrored_)
     with runtime_mode.ModeScope(runtime_mode.GLOBAL_MODE):
         with scope_util.ScopeContext(scope):
             self.cur_job_name_ = job_name
             yield self
             self.cur_job_name_ = None
     oneflow._oneflow_internal.JobBuildAndInferCtx_Close()
Exemple #7
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"]
Exemple #8
0
def _EagerRunModelInit(var_op_conf):
    (op_conf, _) = _GenModelInitOpConfAndRetLbi(var_op_conf)
    bn_in_op2blob_object = oneflow._oneflow_internal.deprecated.BnInOp2BlobObject(
    )

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

    sess = session_ctx.GetDefaultSession()
    with scope_util.ScopeContext(scope_util.MakeScope(_BuildNotMirroredScope)):
        oneflow._oneflow_internal.deprecated.LogicalRun(
            BuildModelInitInstruction)
    return bn_in_op2blob_object["out_0"]
Exemple #9
0
def _EagerRunModelSave(var_blobs, snapshot_path):
    (path_input_op_conf, path_lbi) = _GenModelIOPathInputOpConfAndRetLbi()
    path_input_blob_objects = oneflow._oneflow_internal.deprecated.BnInOp2BlobObject(
    )
    (
        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._oneflow_internal.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._oneflow_internal.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._oneflow_internal.deprecated.LogicalRun(
            BuildModelIOPathInputInstruction)
        oneflow._oneflow_internal.deprecated.LogicalRun(
            BuildFeedPathInstruction)
        oneflow._oneflow_internal.deprecated.LogicalRun(
            BuildModelSaveInstruction)