예제 #1
0
파일: data_ops.py 프로젝트: zzk0/oneflow
def decode_random(
    shape: Sequence[int],
    dtype: flow.dtype,
    batch_size: int = 1,
    initializer: Optional[initializer_conf_util.InitializerConf] = None,
    tick: Optional[oneflow._oneflow_internal.BlobDesc] = None,
    name: Optional[str] = None,
) -> oneflow._oneflow_internal.BlobDesc:
    op_conf = op_conf_util.OperatorConf()
    if name is None:
        name = id_util.UniqueStr("DecodeRandom_")
    assert isinstance(name, str)
    op_conf.name = name
    assert isinstance(shape, (list, tuple))
    op_conf.decode_random_conf.shape.dim.extend(shape)
    assert dtype is not None
    setattr(
        op_conf.decode_random_conf,
        "data_type",
        oneflow._oneflow_internal.deprecated.GetProtoDtype4OfDtype(dtype),
    )
    op_conf.decode_random_conf.batch_size = batch_size
    if initializer is not None:
        op_conf.decode_random_conf.data_initializer.CopyFrom(initializer)
    else:
        op_conf.decode_random_conf.data_initializer.CopyFrom(
            flow.random_uniform_initializer())
    if tick:
        op_conf.decode_random_conf.tick = tick.unique_name
    op_conf.decode_random_conf.out = "out"
    lbi = logical_blob_id_util.LogicalBlobId()
    lbi.op_name = op_conf.name
    lbi.blob_name = "out"
    interpret_util.ConsistentForward(op_conf)
    return remote_blob_util.RemoteBlob(lbi)
예제 #2
0
def EagerReturnRemoteBlob(remote_blob, allow_cpu_return_op=True):
    if not hob.is_trainable(None):
        return remote_blob
    (op_conf, lbi,
     scope) = _GetReturnOpConfAndOutLbiAndScope(remote_blob,
                                                allow_cpu_return_op)
    if remote_blob.blob_object.op_arg_parallel_attr.is_mirrored():
        add_and_infer = compile_context.CurJobAddMirroredOp
    else:
        add_and_infer = compile_context.CurJobAddConsistentOp
    op_attribute = add_and_infer(op_conf, scope)

    def BuildInstruction(builder):
        get_blob_scope = blob_register_util.BnInOp2BlobObjectScope
        with get_blob_scope(blob_register,
                            op_attribute) as bn_in_op2blob_object:
            cfg_op_attribute = oneflow._oneflow_internal.deprecated.MakeOpAttributeByString(
                str(op_attribute))
            builder.StatelessCall(
                cfg_op_attribute,
                remote_blob.blob_object.parallel_desc_symbol.parallel_conf,
                bn_in_op2blob_object,
                boxing_util.BoxingTo,
            )

    oneflow._oneflow_internal.deprecated.LogicalRun(BuildInstruction)
    return remote_blob_util.RemoteBlob(lbi)
예제 #3
0
def InputOpByArgBlobDef(blob_def):
    assert isinstance(blob_def, input_blob_util.ArgBlobDef)
    op_conf = op_conf_util.OperatorConf()
    op_conf.name = blob_def.op_name
    op_conf.input_conf.out = blob_def.blob_name
    op_conf.input_conf.blob_conf.CopyFrom(blob_def.ToInterfaceBlobConf())
    blob_def.AddAndInferOp(op_conf)
    lbi = logical_blob_id_util.LogicalBlobId()
    lbi.op_name = blob_def.op_name
    lbi.blob_name = blob_def.blob_name
    return remote_blob_util.RemoteBlob(lbi)
예제 #4
0
def LazyReturnRemoteBlob(remote_blob, allow_cpu_return_op=True):
    assert isinstance(
        remote_blob,
        (
            oneflow._oneflow_internal.LazyMirroredBlob,
            oneflow._oneflow_internal.LazyConsistentBlob,
        ),
    )
    (op_conf, lbi,
     scope) = _GetReturnOpConfAndOutLbiAndScope(remote_blob,
                                                allow_cpu_return_op)
    compile_context.CurJobAddOp(op_conf, scope)
    return remote_blob_util.RemoteBlob(lbi)
예제 #5
0
파일: data_ops.py 프로젝트: zzk0/oneflow
def image_decoder_random_crop_resize(
    input_blob: oneflow._oneflow_internal.BlobDesc,
    target_width: int,
    target_height: int,
    num_attempts: Optional[int] = None,
    seed: Optional[int] = None,
    random_area: Optional[Sequence[float]] = None,
    random_aspect_ratio: Optional[Sequence[float]] = None,
    num_workers: Optional[int] = None,
    warmup_size: Optional[int] = None,
    max_num_pixels: Optional[int] = None,
    name: Optional[str] = None,
) -> Tuple[oneflow._oneflow_internal.BlobDesc]:
    if name is None:
        name = id_util.UniqueStr("ImageDecoderRandomCropResize_")
    op_conf = op_conf_util.OperatorConf()
    op_conf.name = name
    setattr(op_conf.image_decoder_random_crop_resize_conf, "in",
            input_blob.unique_name)
    op_conf.image_decoder_random_crop_resize_conf.out = "out"
    op_conf.image_decoder_random_crop_resize_conf.target_width = target_width
    op_conf.image_decoder_random_crop_resize_conf.target_height = target_height
    if num_attempts is not None:
        op_conf.image_decoder_random_crop_resize_conf.num_attempts = num_attempts
    if seed is not None:
        op_conf.image_decoder_random_crop_resize_conf.seed = seed
    if random_area is not None:
        assert len(random_area) == 2
        op_conf.image_decoder_random_crop_resize_conf.random_area_min = random_area[
            0]
        op_conf.image_decoder_random_crop_resize_conf.random_area_max = random_area[
            1]
    if random_aspect_ratio is not None:
        assert len(random_aspect_ratio) == 2
        op_conf.image_decoder_random_crop_resize_conf.random_aspect_ratio_min = random_aspect_ratio[
            0]
        op_conf.image_decoder_random_crop_resize_conf.random_aspect_ratio_max = random_aspect_ratio[
            1]
    if num_workers is not None:
        op_conf.image_decoder_random_crop_resize_conf.num_workers = num_workers
    if warmup_size is not None:
        op_conf.image_decoder_random_crop_resize_conf.warmup_size = warmup_size
    if max_num_pixels is not None:
        op_conf.image_decoder_random_crop_resize_conf.max_num_pixels = max_num_pixels
    interpret_util.Forward(op_conf)
    lbi = logical_blob_id_util.LogicalBlobId()
    lbi.op_name = op_conf.name
    lbi.blob_name = "out"
    return remote_blob_util.RemoteBlob(lbi)
예제 #6
0
def distribute_add(xs, name=None):
    assert flow.current_scope(
    ).device_parallel_desc_symbol.parallel_num == len(xs)
    if name is None:
        name = id_util.UniqueStr("DistributeAdd_")
    op_conf = op_conf_util.OperatorConf()
    op_conf.name = name
    getattr(op_conf.distribute_add_conf,
            "in").extend([_SoleConsistentLbn(x) for x in xs])
    op_conf.distribute_add_conf.out = "out"
    interpret_util.ConsistentForward(op_conf)
    lbi = logical_blob_id_util.LogicalBlobId()
    lbi.op_name = op_conf.name
    lbi.blob_name = "out"
    return remote_blob_util.RemoteBlob(lbi)
예제 #7
0
def distribute_clone(x, name=None):
    if name is None:
        name = id_util.UniqueStr("DistributeClone_")
    op_conf = op_conf_util.OperatorConf()
    op_conf.name = name
    setattr(op_conf.distribute_clone_conf, "in", x.unique_name)
    parallel_size = flow.current_scope(
    ).device_parallel_desc_symbol.parallel_num
    op_conf.distribute_clone_conf.out.extend(
        ["out_%d" % i for i in range(parallel_size)])
    interpret_util.ConsistentForward(op_conf)
    ret = []
    for i in range(parallel_size):
        out = "out_%d" % i
        lbi = logical_blob_id_util.LogicalBlobId()
        lbi.op_name = op_conf.name
        lbi.blob_name = out
        ret.append(remote_blob_util.RemoteBlob(lbi))
    return tuple(ret)
예제 #8
0
 def MakeRemoteBlob(self, lbi):
     return remote_blob_util.RemoteBlob(lbi)
예제 #9
0
def _CreateVariableBlob(op_conf):
    compile_context.CurJobAddConsistentOp(op_conf)
    lbi = logical_blob_id_util.LogicalBlobId()
    lbi.op_name = op_conf.name
    lbi.blob_name = op_conf.variable_conf.out
    return remote_blob_util.RemoteBlob(lbi)
예제 #10
0
def constant_like(
    like: oneflow._oneflow_internal.BlobDesc,
    value: Union[int, float],
    dtype: Optional[flow.dtype] = None,
    name: Optional[str] = None,
) -> oneflow._oneflow_internal.BlobDesc:
    """This operator creates a constant Blob that has the same shape as `like`.

    Args:
        like (oneflow._oneflow_internal.BlobDesc): A Blob.
        value (Union[int, float]): The constant value of Blob.
        dtype (Optional[flow.dtype], optional): The data type of Blob. Defaults to None.
        name (Optional[str], optional): The name for the operation. Defaults to None.

    Raises:
        NotImplementedError: The data type of value should be int or float.

    Returns:
        oneflow._oneflow_internal.BlobDesc: The result Blob.

    For example:

    .. code-block:: python

        import oneflow.compatible.single_client as flow
        import numpy as np
        import oneflow.compatible.single_client.typing as tp


        @flow.global_function()
        def constant_like_Job() -> tp.Numpy:
            constant_blob = flow.constant(value=1.5,
                                        shape=(1, 3, 3),
                                        dtype=flow.float)
            constant_like_blob = flow.constant_like(like=constant_blob,
                                                    value=5.5,
                                                    dtype=flow.float)
            return constant_like_blob


        out = constant_like_Job()

        # out [[[5.5 5.5 5.5]
        #       [5.5 5.5 5.5]
        #       [5.5 5.5 5.5]]]

    """
    op_conf = op_conf_util.OperatorConf()
    setattr(
        op_conf,
        "name",
        name if name is not None else id_util.UniqueStr("ConstantLike_"),
    )
    setattr(op_conf.constant_like_conf, "like", like.unique_name)
    if isinstance(value, int):
        op_conf.constant_like_conf.int_operand = value
    elif isinstance(value, float):
        op_conf.constant_like_conf.float_operand = value
    else:
        raise NotImplementedError
    if dtype is not None:
        setattr(
            op_conf.constant_like_conf,
            "data_type",
            oneflow._oneflow_internal.deprecated.GetProtoDtype4OfDtype(dtype),
        )
    setattr(op_conf.constant_like_conf, "out", "out")
    interpret_util.Forward(op_conf)
    out_lbi = logical_blob_id_util.LogicalBlobId()
    setattr(out_lbi, "op_name", op_conf.name)
    setattr(out_lbi, "blob_name", "out")
    return remote_blob_util.RemoteBlob(out_lbi)