Exemplo n.º 1
0
def _default_initializer_for_determining(tensor):
    assert not tensor.is_determined
    undetermined_tensor = tensor._undetermined_tensor
    variable_name = id_util.UniqueStr("tensor_")

    blob = None

    @global_function_or_identity()
    def job():
        nonlocal blob
        with tensor._placement_scope():
            blob = flow.get_variable(
                name=variable_name,
                shape=tuple(undetermined_tensor.shape),
                dtype=undetermined_tensor.dtype,
                initializer=undetermined_tensor.data_initializer,
            )

    job()
    if undetermined_tensor.is_consistent:
        determined_tensor = oneflow_api.ConsistentTensor(
            undetermined_tensor.shape,
            undetermined_tensor.dtype,
            undetermined_tensor.sbp,
            undetermined_tensor.placement,
            undetermined_tensor.is_lazy,
            undetermined_tensor.requires_grad,
            True,
            undetermined_tensor.retain_grad,
        )
    else:
        determined_tensor = oneflow_api.LocalTensor(
            undetermined_tensor.shape,
            undetermined_tensor.dtype,
            undetermined_tensor.device,
            undetermined_tensor.is_lazy,
            undetermined_tensor.requires_grad,
            True,
            undetermined_tensor.retain_grad,
        )
    determined_tensor._set_blob_object(blob.blob_object)
    return determined_tensor
Exemplo n.º 2
0
def _numpy_initializer_for_determining(tensor):
    assert not tensor.is_determined
    undetermined_tensor = tensor._undetermined_tensor
    assert undetermined_tensor.numpy_data is not None
    variable_name = id_util.UniqueStr("tensor_")

    @global_function_or_identity()
    def set_numpy_data():
        with tensor._placement_scope():
            flow.get_variable(
                name=variable_name,
                shape=tuple(undetermined_tensor.shape),
                dtype=undetermined_tensor.dtype,
                initializer=undetermined_tensor.data_initializer,
            )

    set_numpy_data()
    flow.load_variables({variable_name: undetermined_tensor.numpy_data})
    blob = flow.get_all_variables()[variable_name]
    if undetermined_tensor.is_consistent:
        determined_tensor = oneflow_api.ConsistentTensor(
            undetermined_tensor.shape,
            undetermined_tensor.dtype,
            undetermined_tensor.sbp,
            undetermined_tensor.placement,
            undetermined_tensor.is_lazy,
            undetermined_tensor.requires_grad,
            True,
            undetermined_tensor.retain_grad,
        )
    else:
        determined_tensor = oneflow_api.LocalTensor(
            undetermined_tensor.shape,
            undetermined_tensor.dtype,
            undetermined_tensor.device,
            undetermined_tensor.is_lazy,
            undetermined_tensor.requires_grad,
            True,
            undetermined_tensor.retain_grad,
        )
    determined_tensor._set_blob_object(blob.blob_object)
    return determined_tensor
Exemplo n.º 3
0
def _default_initializer_for_determining(undetermined_tensor):
    assert not undetermined_tensor.is_consistent
    variable_name = id_util.UniqueStr("tensor_")
    blob = flow.get_variable(
        name=variable_name,
        shape=tuple(undetermined_tensor.shape),
        dtype=undetermined_tensor.dtype,
        initializer=undetermined_tensor.data_initializer,
    )
    determined_tensor = oneflow_api.LocalTensor(
        undetermined_tensor.shape,
        undetermined_tensor.dtype,
        undetermined_tensor.device,
        undetermined_tensor.is_lazy,
        undetermined_tensor.requires_grad,
        True,
        undetermined_tensor.retain_grad,
    )
    determined_tensor._set_blob_object(blob.blob_object)
    return determined_tensor, variable_name
Exemplo n.º 4
0
def _initialized_job(
    shape=None,
    dtype=None,
    device=None,
    requires_grad=None,
    retain_grad=None,
    is_lazy=False,
    numpy_data=None,
):
    assert numpy_data is not None
    variable_name = id_util.UniqueStr("tensor_")

    @global_function_or_identity()
    def set_data():
        flow.get_variable(
            name=variable_name,
            shape=tuple(shape),
            dtype=dtype,
            initializer=flow.zeros_initializer(dtype=dtype),
        )

    if not is_lazy:
        set_data()
    flow.load_variables({variable_name: numpy_data})
    blob = flow.get_all_variables()[variable_name]
    determined_tensor = oneflow_api.LocalTensor(
        shape,
        dtype,
        device,
        is_lazy,
        requires_grad,
        True,
        retain_grad,
    )
    determined_tensor._set_blob_object(blob.blob_object)
    return determined_tensor