oneflow

Copyright 2020 The OneFlow Authors. All rights reserved.

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

class oneflow.BuiltinOp(op_type_name, op_name=None)
Attr(attr_name, attr_value, attr_type_name=None)

Set value of op’s attribute.

Parameters
  • attr_name (str) – attribute name of op

  • attr_value (Any) – attribute value of op

Raises

ValueError – raised when value is not idential to op’s attribute type.

Returns

[description]

Return type

[type]

Build()

Explicitly complete the construction of the builtin op

Returns

the completed builtin op

Input(input_name, num=1)

Set input blob of op

Parameters
  • input_name (str) – input name of blob

  • num (int, optional) – Defaults to 1.

Returns

self

Output(output_name, num=1)

Set output blob of op

Parameters
  • output_name (str) – name of output blob

  • num (int, optional) – Defaults to 1.

Returns

self

property op

access the builtin op

Returns

the builtin op

class oneflow.ConfigProto
DESCRIPTOR = <google.protobuf.pyext._message.MessageDescriptor object>
load_lib_path

Field oneflow.ConfigProto.load_lib_path

profiler_conf

Field oneflow.ConfigProto.profiler_conf

resource

Field oneflow.ConfigProto.resource

session_id

Field oneflow.ConfigProto.session_id

class oneflow.DeprecatedFixedTensorDef(*args, **kwargs)
class oneflow.DeprecatedMirroredTensorDef(*args, **kwargs)
oneflow.ExecutionConfig

alias of oneflow.python.framework.function_util.FunctionConfig

oneflow.FixedTensorDef

alias of oneflow.python.framework.input_blob_def.DeprecatedFixedTensorDef

class oneflow.FunctionConfig

OneFlow function’s configurations.

property all_reduce_fp16
property all_reduce_group_min_mbyte
property all_reduce_group_num
property all_reduce_group_size_warmup
property all_reduce_lazy_ratio
property allow_cpu_return_op
property concurrency_width
property cudnn_buf_limit_mbyte
property cudnn_conv_enable_pseudo_half
property cudnn_conv_enable_true_half
property cudnn_conv_force_bwd_data_algo
property cudnn_conv_force_bwd_filter_algo
property cudnn_conv_force_fwd_algo
property cudnn_conv_heuristic_search_algo
property cudnn_conv_use_deterministic_algo_only
property default_data_type
property default_distribute_strategy
property default_initializer_conf
property default_logical_view
property default_placement_scope
property disable_all_reduce_sequence
property do_parallel_cast_before_widening_type_cast
property enable_all_reduce_group
property enable_auto_mixed_precision
property enable_cudnn
property enable_cudnn_conv_pseudo_half
property enable_cudnn_fused_normalization_add_relu
property enable_float_compute_for_half_gemm
property enable_fuse_add_to_output
property enable_fuse_cast_scale
property enable_fuse_model_update_ops
property enable_gradients_stats_aggregation
property enable_inplace
property enable_inplace_in_reduce_struct
property enable_keep_header_only
property enable_nccl
property enable_non_distributed_optimizer
property enable_qat
property enable_quantization_aware_training
property enable_reused_mem
property enable_true_half_config_when_conv
property exp_run_conf
property indexed_slices_optimizer_conf
property non_distributed_optimizer_group_size_mbyte
property optimizer_placement_optimization_mode
property optimizer_placement_optimization_threshold
property prune_amp_white_identity_ops
property prune_cast_to_static_shape_ops
property prune_parallel_cast_ops
property qat
property static_mem_alloc_algo_white_list
property static_mem_alloc_policy_white_list
property tensorrt
property train
property use_boxing_v2
property use_nccl_inter_node_communication
property use_tensorrt
property use_xla_jit
oneflow.Generator(device=None)
oneflow.InitEagerGlobalSession()
class oneflow.JobConfigProto
DESCRIPTOR = <google.protobuf.pyext._message.MessageDescriptor object>
class FlagName2flagValueEntry
DESCRIPTOR = <google.protobuf.pyext._message.MessageDescriptor object>
key

Field oneflow.JobConfigProto.FlagName2flagValueEntry.key

value

Field oneflow.JobConfigProto.FlagName2flagValueEntry.value

concurrency_width

Field oneflow.JobConfigProto.concurrency_width

cudnn_buf_limit_mbyte

Field oneflow.JobConfigProto.cudnn_buf_limit_mbyte

cudnn_conv_enable_pseudo_half

Field oneflow.JobConfigProto.cudnn_conv_enable_pseudo_half

cudnn_conv_force_bwd_data_algo

Field oneflow.JobConfigProto.cudnn_conv_force_bwd_data_algo

cudnn_conv_force_bwd_filter_algo

Field oneflow.JobConfigProto.cudnn_conv_force_bwd_filter_algo

cudnn_conv_force_fwd_algo

Field oneflow.JobConfigProto.cudnn_conv_force_fwd_algo

cudnn_conv_heuristic_search_algo

Field oneflow.JobConfigProto.cudnn_conv_heuristic_search_algo

cudnn_conv_use_deterministic_algo_only

Field oneflow.JobConfigProto.cudnn_conv_use_deterministic_algo_only

default_data_type

Field oneflow.JobConfigProto.default_data_type

default_initialize_with_snapshot_path

Field oneflow.JobConfigProto.default_initialize_with_snapshot_path

default_initializer_conf

Field oneflow.JobConfigProto.default_initializer_conf

do_parallel_cast_before_widening_type_cast

Field oneflow.JobConfigProto.do_parallel_cast_before_widening_type_cast

enable_auto_mixed_precision

Field oneflow.JobConfigProto.enable_auto_mixed_precision

enable_cudnn

Field oneflow.JobConfigProto.enable_cudnn

enable_cudnn_fused_normalization_add_relu

Field oneflow.JobConfigProto.enable_cudnn_fused_normalization_add_relu

enable_fuse_add_to_output

Field oneflow.JobConfigProto.enable_fuse_add_to_output

enable_fuse_cast_scale

Field oneflow.JobConfigProto.enable_fuse_cast_scale

enable_fuse_model_update_ops

Field oneflow.JobConfigProto.enable_fuse_model_update_ops

enable_gradients_stats_aggregation

Field oneflow.JobConfigProto.enable_gradients_stats_aggregation

enable_inplace

Field oneflow.JobConfigProto.enable_inplace

enable_inplace_in_reduce_struct

Field oneflow.JobConfigProto.enable_inplace_in_reduce_struct

enable_quantization_aware_training

Field oneflow.JobConfigProto.enable_quantization_aware_training

enable_reuse_mem

Field oneflow.JobConfigProto.enable_reuse_mem

flag_name2flag_value

Field oneflow.JobConfigProto.flag_name2flag_value

indexed_slices_optimizer_conf

Field oneflow.JobConfigProto.indexed_slices_optimizer_conf

job_name

Field oneflow.JobConfigProto.job_name

logical_object_id

Field oneflow.JobConfigProto.logical_object_id

memory_allocation_algorithm_conf

Field oneflow.JobConfigProto.memory_allocation_algorithm_conf

num_gradient_accumulation_steps

Field oneflow.JobConfigProto.num_gradient_accumulation_steps

optimizer_placement_optimization_mode

Field oneflow.JobConfigProto.optimizer_placement_optimization_mode

optimizer_placement_optimization_threshold

Field oneflow.JobConfigProto.optimizer_placement_optimization_threshold

predict_conf

Field oneflow.JobConfigProto.predict_conf

prune_amp_white_identity_ops

Field oneflow.JobConfigProto.prune_amp_white_identity_ops

prune_cast_to_static_shape_ops

Field oneflow.JobConfigProto.prune_cast_to_static_shape_ops

prune_parallel_cast_ops

Field oneflow.JobConfigProto.prune_parallel_cast_ops

qat_config

Field oneflow.JobConfigProto.qat_config

signature

Field oneflow.JobConfigProto.signature

train_conf

Field oneflow.JobConfigProto.train_conf

xrt_config

Field oneflow.JobConfigProto.xrt_config

oneflow.MirroredTensorDef

alias of oneflow.python.framework.input_blob_def.DeprecatedMirroredTensorDef

class oneflow.Model(*args, **kwargs)

A high level API for model training and validation.

configure_optimizers()

Choose what optimizers and learning-rate schedulers to use in your optimization. Normally you’d need one. But in the case of GANs or similar you might have multiple.

fit(training_config: Optional[oneflow.python.framework.model.TrainingConfig] = None, validation_config: Optional[oneflow.python.framework.model.ValidationConfig] = None, checkpoint_config: Optional[oneflow.python.framework.model.CheckpointConfig] = None, callbacks: Union[oneflow.python.framework.model.Callback, List[oneflow.python.framework.model.Callback], None] = None, max_steps: int = 100)

Runs the full training and validation routine.

method_overrided(method_name: str = None) → bool
training_step(*args, **kwargs)

Operates on a single batch of data from the training set and return loss.

validation_step(*args, **kwargs)

Operates on a single batch of data from the validation set.

class oneflow.MultiClientSession(sess_id)
AnyGlobalFunctionDefined()
class Status

An enumeration.

CLOSED = 3
CREATED = 1
INITED = 2
TryClose()
TryInit()
property config_proto
property function_flag_name2default_val
property id
property is_running
property resource
property scope_attr_name2default_val
property status
class oneflow.Session(sess_id)
AddInfo4InterfaceOpName(interface_op_name, op_attribute)
AddJob(function_desc)
AnyGlobalFunctionDefined()
AsyncPull(op_name, pull_data_cb)
AsyncPush(op_name, push_data_cb)
Close()
CurrentEagerGlobalFunctionDesc()
EagerRun(function_desc, *arg)
FindOrCreateLazyBlob(op_name, Create)
ForceReleaseEagerBlobs()
GetFunctionDesc(job_name)
GetJobConfigProto(job_name)
GetLazyFunctionDesc(job_name)
HasAnyCallbackAfterFunctionReturn()
Init()
Job(job_name)
JobName4InterfaceOpName(interface_op_name)
LaunchJob(job_instance)
LaunchUserJob(job_func, *arg)
LazyRun(job_func, *arg)
OpAttribute4InterfaceOpName(interface_op_name)
ParallelConf4LazyInterfaceOpName(interface_op_name)
ReleaseLazyRefBlob()
StashJob(job_name=None, key=None)
StashVariableBlob4Job(job_name, var_name, var_blob)
Sync()
TryClose()
TryGetVariableBlobOfJobFromStash(job_name, var_name)
TryInit()
UpdateInfo4InterfaceOp()
property backward_blob_register
property config_proto
property function_flag_name2default_val
has_empty_is_mirrored_strategy_enabled_stack()
property id
property inter_user_job_info
property interface_ops
is_consistent_strategy_enabled()
is_mirrored_strategy_enabled()
property is_running
property job_name2name_scope_stack
pop_mirrored_strategy_enabled()
push_mirrored_strategy_enabled(val)
property resource
property scope_attr_name2default_val
property snapshot_mgr
property status
property uuid2watch_handler
property var_name2var_blob
class oneflow.Size
count(self: oneflow._oneflow_internal.Size, arg0: int) → int
index(self: oneflow._oneflow_internal.Size, arg0: int, start: int = 0, end: int = 20) → int
numel(self: oneflow._oneflow_internal.Size) → int
class oneflow.Tensor(*args, dtype=None, device=None, requires_grad=False, placement=None, sbp=None, is_consistent=False, is_lazy=False, data_initializer=None, determining_initializer=None)
abs()

Return the absolute value of each element in input tensor:math:y = |x| element-wise.

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> x = flow.Tensor(np.array([-1, 2, -3, 4]).astype(np.float32))
>>> flow.abs(x)
tensor([1., 2., 3., 4.], dtype=oneflow.float32)
acos()

Returns a new tensor with the inverse cosine of the elements of input.

\[\text{out}_{i} = \arccos(\text{input}_{i})\]
Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> arr = np.array([0.5, 0.6, 0.7])
>>> input = flow.Tensor(arr, dtype=flow.float32)
>>> output = flow.acos(input)
>>> print(output.numpy())
[1.0471976  0.9272952  0.79539883]
acosh() → Tensor

See oneflow.experimental.acosh()

add(y)

Computes the addition of x by y for each element, scalar and broadcast promotation are supported. The formula is:

\[out = x + y\]

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

# element-wise add
>>> x = flow.Tensor(np.random.randn(2,3))
>>> y = flow.Tensor(np.random.randn(2,3))
>>> out = flow.add(x, y).numpy()
>>> out.shape
(2, 3)

# scalar add
>>> x = 5
>>> y = flow.Tensor(np.random.randn(2,3))
>>> out = flow.add(x, y).numpy()
>>> out.shape
(2, 3)

# broadcast add
>>> x = flow.Tensor(np.random.randn(1,1))
>>> y = flow.Tensor(np.random.randn(2,3))
>>> out = flow.add(x, y).numpy()
>>> out.shape
(2, 3)
add_(y)

In-place version of oneflow.experimental.Tensor.add().

addmm(mat1, mat2, alpha=1, beta=1)

See oneflow.experimental.addmm()

arccosh() → Tensor

See oneflow.experimental.acosh()

arcsin()

See oneflow.experimental.asin()

arcsinh()

See oneflow.experimental.asinh()

arctan()

See oneflow.experimental.arctan()

arctanh()

Alias for oneflow.experimental.atanh()

argmax(dim: int = None, keepdim: bool = False)

The op computes the index with the largest value of a Tensor at specified axis.

Parameters
  • input (oneflow.Tensor) – Input Tensor

  • dim (int, optional) – dimension to be calculated. Defaults to the last dim (-1)

  • keepdim (bool optional) – whether the output tensor has dim retained or not. Ignored if dim=None.

Returns

A Tensor(dtype=int32) contains the index with the largest value of input

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = np.array([[1, 3, 8, 7, 2],
...            [1, 9, 4, 3, 2]], dtype=np.float32)

>>> out = flow.argmax(flow.Tensor(x))
>>> out
tensor([6], dtype=oneflow.int32)
>>> out = flow.argmax(flow.Tensor(x), dim=1)
>>> out
tensor([2, 1], dtype=oneflow.int32)
argsort(dim: int = -1, descending: bool = False)

This operator sorts the input Tensor at specified dim and return the indices of the sorted Tensor.

Parameters
  • input (oneflow.Tensor) – The input Tensor.

  • dim (int, optional) – dimension to be sorted. Defaults to the last dim (-1).

  • descending (bool, optional) – controls the sorting order (ascending or descending).

Returns

The indices of the sorted Tensor.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = np.array([[10, 2, 9, 3, 7],
...               [1, 9, 4, 3, 2]]).astype("float32")
>>> input = flow.Tensor(x)
>>> output = flow.argsort(input)
>>> output
tensor([[1, 3, 4, 2, 0],
        [0, 4, 3, 2, 1]], dtype=oneflow.int32)
>>> output = flow.argsort(input, descending=True)
>>> output
tensor([[0, 2, 4, 3, 1],
        [1, 2, 3, 4, 0]], dtype=oneflow.int32)
>>> output = flow.argsort(input, dim=0)
>>> output
tensor([[1, 0, 1, 0, 1],
        [0, 1, 0, 1, 0]], dtype=oneflow.int32)
argwhere() → Tensor

See oneflow.experimental.argwhere()

asin()

See oneflow.experimental.asin()

asinh()

See oneflow.experimental.asinh()

atan()

See oneflow.experimental.atan()

atan2(other) → Tensor

See oneflow.experimental.atan2()

atanh() → Tensor

See oneflow.experimental.atanh()

backward(**kwargs)
bmm() → Tensor

See oneflow.experimental.bmm()

cast(dtype)

The operation takes input tensor x and casts it to the output with dtype

Parameters
  • x (oneflow.Tensor) – A Tensor

  • dtype (flow.dtype) – Data type of the output tensor

Returns

A Tensor with specific dtype.

Return type

oneflow.Tensor

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> np_arr = np.random.randn(2, 3, 4, 5).astype(np.float32)
>>> input = flow.Tensor(np_arr, dtype=flow.float32)
>>> output = flow.cast(input, flow.int8)
>>> np.array_equal(output.numpy(), np_arr.astype(np.int8))
True
ceil()

See oneflow.experimental.ceil()

chunk(chunks, dim)

Splits a tensor into a specific number of chunks. Each chunk is a view of the input tensor. Last chunk will be smaller if the tensor size along the given dimension dim is not divisible by chunks.

Parameters
  • input (oneflow.experimental.Tensor) – The tensor to split.

  • chunks (int) – Number of chunks to return.

  • dim (int) – Dimension along which to split the tensor.

Returns

List of Tensors.

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> np_arr = np.random.randn(5, 3, 6, 9).astype(np.float32)
>>> input = flow.Tensor(np_arr)
>>> of_out = []
>>> of_out = flow.chunk(input, chunks=3, dim=2)
>>> chunks = 3
>>> of_out_shape = []
>>> for i in range(0, chunks):
...     of_out_shape.append(of_out[i].numpy().shape)
>>> of_out_shape
[(5, 3, 2, 9), (5, 3, 2, 9), (5, 3, 2, 9)]

>>> np_arr = np.random.randn(5, 3, 6, 9).astype(np.float32)
>>> input = flow.Tensor(np_arr)
>>> of_out = []
>>> of_out = flow.chunk(input, chunks=4, dim=3)
>>> chunks = 4
>>> of_out_shape = []
>>> for i in range(0, chunks):
...     of_out_shape.append(of_out[i].numpy().shape)
>>> of_out_shape
[(5, 3, 6, 2), (5, 3, 6, 2), (5, 3, 6, 2), (5, 3, 6, 3)]
clamp(min=None, max=None)

See oneflow.experimental.clamp()

clip(min=None, max=None)

See oneflow.experimental.clamp()

clone(**kwargs)
copy_(**kwargs)
cos()

Returns a new tensor with the cosine of the elements of input.

\[\text{out}_{i} = \cos(\text{input}_{i})\]
Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()
>>> arr = np.array([1.4309,  1.2706, -0.8562,  0.9796])
>>> input = flow.Tensor(arr, dtype=flow.float32)
>>> output = flow.cos(input).numpy()
cosh()

Returns a new tensor with the hyperbolic cosine of the elements of input.

\[\text{out}_{i} = \cosh(\text{input}_{i})\]
Parameters

input (Tensor) – the input tensor.

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> arr = np.array([ 0.1632,  1.1835, -0.6979, -0.7325])
>>> input = flow.Tensor(arr, dtype=flow.float32)
>>> output = flow.cosh(input).numpy()
>>> output
array([1.0133467, 1.7859949, 1.2535787, 1.2804903], dtype=float32)
property data
data_ptr()
detach(**kwargs)
determine(determining_initializer=None)
property device
diag() → Tensor

See oneflow.experimental.diag()

dim()
div(other)

Computes the division of input by other for each element, scalar and broadcast promotation are supported. The formula is:

\[out = \frac{input}{other}\]
Parameters
  • input (Union[int, float, flow.Tensor]) – input.

  • other (Union[int, float, flow.Tensor]) – other.

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

# element-wise divide
>>> input = flow.Tensor(np.random.randn(2,3))
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.div(input,other).numpy()
>>> out.shape
(2, 3)

# scalar divide
>>> input = 5
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.div(input,other).numpy()
>>> out.shape
(2, 3)

# broadcast divide
>>> input = flow.Tensor(np.random.randn(1,1))
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.div(input,other).numpy()
>>> out.shape
(2, 3)
property dtype
element_size()
eq(other)

Computes element-wise equality. The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters
Returns

  • A boolean tensor that is True where input is equal to other and False elsewhere

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> input = flow.Tensor(np.array([2, 3, 4, 5]), dtype=flow.float32)
>>> other = flow.Tensor(np.array([2, 3, 4, 1]), dtype=flow.float32)

>>> y = flow.eq(input, other)
>>> y
tensor([1, 1, 1, 0], dtype=oneflow.int8)
erf()

See oneflow.experimental.erf()

erfc()

See oneflow.experimental.erfc()

exp()

This operator computes the exponential of Tensor.

The equation is:

\[out = e^x\]
Parameters

x (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = flow.Tensor(np.array([1, 2, 3]).astype(np.float32))
>>> y = x.exp()
>>> y
tensor([ 2.7183,  7.3891, 20.0855], dtype=oneflow.float32)
expand(*sizes)

This operator expand the input tensor to a larger size.

Passing -1 as the size for a dimension means not changing the size of that dimension.

Tensor can be also expanded to a larger number of dimensions and the new ones will be appended at the front.

For the new dimensions, the size cannot be set to -1.

Parameters
  • x (oneflow.Tensor) – The input Tensor.

  • *sizes (flow.Size or int) – The desired expanded size.

Returns

The result Tensor.

Return type

oneflow.Tensor

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> x = np.array([[[[0, 1]],
...               [[2, 3]],
...               [[4, 5]]]]).astype(np.int32)

>>> input = flow.Tensor(x)

>>> out = input.expand(1, 3, 2, 2)
>>> out.shape
flow.Size([1, 3, 2, 2])
expm1()

See oneflow.experimental.expm1()

fill_(value)
flatten(start_dim: int = 0, end_dim: int = -1)

Flattens a contiguous range of dims into a tensor.

Parameters
  • start_dim – first dim to flatten (default = 0).

  • end_dim – last dim to flatten (default = -1).

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> input = flow.Tensor(32, 1, 5, 5)
>>> output = input.flatten(start_dim=1)
>>> output.size()
flow.Size([32, 25])
flip(dims)

See oneflow.experimental.flip()

floor()

See oneflow.experimental.floor()

gather(index, dim=0, sparse_grad=False)

Gathers values along an axis specified by dim.

For a 3-D tensor the output is specified by:

out[i][j][k] = input[index[i][j][k]][j][k] # if dim == 0 out[i][j][k] = input[i][index[i][j][k]][k] # if dim == 1 out[i][j][k] = input[i][j][index[i][j][k]] # if dim == 2

input and index must have the same number of dimensions. It is also required that index.size(d) <= input.size(d) for all dimensions d != dim. out will have the same shape as index. Note that input and index do not broadcast against each other.

Parameters
  • input (Tensor) – the source tensor

  • dim (int) – the axis along which to index

  • index (LongTensor) – the indices of elements to gather

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> input = np.random.randn(3, 4, 3, 5)
>>> index = np.random.choice(np.arange(3), size=180, replace=True).reshape((3, 4, 3, 5))
>>> output = flow.gather(flow.Tensor(input), flow.Tensor(index, dtype=flow.int), dim=1)
>>> output.shape
flow.Size([3, 4, 3, 5])
ge() → Tensor

See oneflow.experimental.ge()

gelu()

Gelu activation operator.

The equation is:

\[out = 0.5 * x * (1 + tanh(\sqrt{\frac{2}{\pi}} * (x + 0.044715x^{3})))\]
Parameters

x (oneflow.Tensor) – Input Tensor

Returns

A Tensor.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = np.array([-0.5, 0, 0.5]).astype(np.float32)
>>> input = flow.Tensor(x)
>>> gelu = flow.nn.GELU()

>>> out = gelu(input)
>>> out
tensor([-0.1543,  0.    ,  0.3457], dtype=oneflow.float32)
get_device()
property grad
property grad_fn
gt() → Tensor

See oneflow.experimental.gt()

in_top_k() → Tensor

See oneflow.experimental.in_top_k()

property is_consistent
is_contiguous()
property is_cuda
property is_determined
property is_lazy
property is_leaf
kaiming_normal_(a=0, mode='fan_in', nonlinearity='leaky_relu', *, data_format='NCHW')
kaiming_uniform_(a=0, mode='fan_in', nonlinearity='leaky_relu', *, data_format='NCHW')
le(y)

Returns the truth value of \(x <= y\) element-wise.

Parameters
Returns

A Tensor with int8 type.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> input1 = flow.Tensor(np.array([1, 2, 3]).astype(np.float32), dtype=flow.float32)
>>> input2 = flow.Tensor(np.array([1, 1, 4]).astype(np.float32), dtype=flow.float32)

>>> out = flow.le(input1, input2)
>>> out
tensor([1, 0, 1], dtype=oneflow.int8)
log()

Returns a new tensor with the natural logarithm of the elements of input.

\[y_{i} = \log_{e} (x_{i})\]
Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()
>>> arr = np.random.randn(2, 3, 4, 5)
>>> input = flow.Tensor(arr, dtype=flow.float32)
>>> output = flow.log(input)
log1p()

Returns a new tensor with the natural logarithm of (1 + input).

\[\text{out}_{i}=\log_e(1+\text{input}_{i})\]

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()
>>> x = flow.Tensor(np.array([1.3, 1.5, 2.7]))
>>> out = flow.log1p(x).numpy()
>>> out
array([0.8329091 , 0.91629076, 1.3083328 ], dtype=float32)
long()

Tensor.long() is equivalent to Tensor.to(flow.int64). See to().

Parameters

input (Tensor) – the input tensor.

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> input = flow.Tensor(np.random.randn(1, 2, 3), dtype=flow.float32)
>>> input = input.long()
>>> input.dtype
oneflow.int64
lt(y)

Returns the truth value of \(x < y\) element-wise.

Parameters
Returns

A Tensor with int8 type.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> input1 = flow.Tensor(np.array([1, 2, 3]).astype(np.float32), dtype=flow.float32)
>>> input2 = flow.Tensor(np.array([1, 2, 4]).astype(np.float32), dtype=flow.float32)

>>> out = flow.lt(input1, input2)
>>> out
tensor([0, 0, 1], dtype=oneflow.int8)
masked_fill(mask, value)

Fills elements of self tensor with value where mask is True. The shape of mask must be broadcastable with the shape of the underlying tensor.

Parameters
  • mask (BoolTensor) – the boolean mask

  • value (float) – the value to fill in with

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()
>>> in_arr = np.array(
...     [[[-0.13169311,  0.97277078,  1.23305363,  1.56752789],
...     [-1.51954275,  1.87629473, -0.53301206,  0.53006478],
...     [-1.38244183, -2.63448052,  1.30845795, -0.67144869]],
...     [[ 0.41502161,  0.14452418,  0.38968   , -1.76905653],
...     [ 0.34675095, -0.7050969 , -0.7647731 , -0.73233418],
...     [-1.90089858,  0.01262963,  0.74693893,  0.57132389]]]
... )
>>> fill_value = 8.7654321 # random value e.g. -1e9 3.1415
>>> input = flow.Tensor(in_arr, dtype=flow.float32)
>>> mask = flow.Tensor((in_arr > 0).astype(np.int8), dtype=flow.int)
>>> output = flow.masked_fill(input, mask, fill_value)

# tensor([[[-0.1317,  8.7654,  8.7654,  8.7654],
#  [-1.5195,  8.7654, -0.533 ,  8.7654],
#  [-1.3824, -2.6345,  8.7654, -0.6714]],

# [[ 8.7654,  8.7654,  8.7654, -1.7691],
#  [ 8.7654, -0.7051, -0.7648, -0.7323],
#  [-1.9009,  8.7654,  8.7654,  8.7654]]], dtype=oneflow.float32)
masked_select(mask)

See oneflow.experimental.masked_select()

matmul(other)

This operator applies matrix multiplication to two Tensor.

Parameters
Returns

The result Tensor

Return type

oneflow.Tensor

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()
>>> input1 = flow.Tensor(np.random.randn(2, 6), dtype=flow.float32)
>>> input2 = flow.Tensor(np.random.randn(6, 5), dtype=flow.float32)
>>> of_out = flow.matmul(input1, input2)
>>> of_out.shape
flow.Size([2, 5])
max(dim=None, keepdim=False)

Computes the maximum value of all elements in the input tensor.

For example:

>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()
>>> input = flow.Tensor([[4, 1, 5], [2, 6, 3]])
>>> flow.max(input)
tensor([6.], dtype=oneflow.float32)
>>> flow.max(input, dim=0)
tensor([4., 6., 5.], dtype=oneflow.float32)
>>> flow.max(input, dim=1)
tensor([5., 6.], dtype=oneflow.float32)
mean(dim=None, keepdim=False)

Computes the mean of row of elements in a tensor in the given axis, if the axis is None, mean of all elements will be caculated.

For example:

>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()
>>> input = flow.Tensor([[1, 2, 3], [4, 5, 6]])
>>> flow.mean(input)
tensor([3.5], dtype=oneflow.float32)
>>> flow.mean(input, dim=0)
tensor([2.5, 3.5, 4.5], dtype=oneflow.float32)
>>> flow.mean(input, dim=1)
tensor([2., 5.], dtype=oneflow.float32)
min(dim=None, keepdim=False)

Computes the minimum value of all elements in the input tensor.

For example:

>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()
>>> input = flow.Tensor([[4, 1, 5], [2, 6, 3]])
>>> flow.min(input)
tensor([1.], dtype=oneflow.float32)
>>> flow.min(input, dim=0)
tensor([2., 1., 3.], dtype=oneflow.float32)
>>> flow.min(input, dim=1)
tensor([1., 2.], dtype=oneflow.float32)
mish() → Tensor

See oneflow.experimental.mish()

mul(other)

Computes the multiplication of input by other for each element, scalar and broadcast promotation are supported.

The formula is:

\[out = input \times other\]

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

# element-wise multiply
>>> input = flow.Tensor(np.random.randn(2,3))
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.mul(input,other).numpy()
>>> out.shape
(2, 3)

# scalar mutiply
>>> input = 5
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.mul(input,other).numpy()
>>> out.shape
(2, 3)

# broadcast mutiply
>>> input = flow.Tensor(np.random.randn(1,1))
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.mul(input,other).numpy()
>>> out.shape
(2, 3)
property ndim
ndimension()
ne(other)

Computes element-wise not equality. The second argument can be a number or a tensor whose shape is broadcastable with the first argument.

Parameters
Returns

  • A boolean tensor that is True where input is not equal to other and False elsewhere

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> input = flow.Tensor(np.array([2, 3, 4, 5]), dtype=flow.float32)
>>> other = flow.Tensor(np.array([2, 3, 4, 1]), dtype=flow.float32)

>>> y = flow.ne(input, other)
>>> y
tensor([0, 0, 0, 1], dtype=oneflow.int8)
negative()

This operator computes the negative value of Tensor.

Parameters

x (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> input = flow.Tensor(
...    np.array([1.0, -1.0, 2.3]).astype(np.float32), dtype=flow.float32
... )
>>> out = flow.negative(input)
>>> out
tensor([-1. ,  1. , -2.3], dtype=oneflow.float32)
nelement()
new_ones(size=None, dtype=None, device=None, requires_grad=False)

Returns a Tensor of size size filled with 1. By default, the returned Tensor has the same torch.dtype and torch.device as this tensor.

Parameters
  • size (int...) – a list, tuple, or flow.Size of integers defining the shape of the output tensor.

  • dtype (flow.dtype, optional) – the desired type of returned tensor. Default: if None, same flow.dtype as this tensor.

  • device (flow.device, optional) – the desired device of returned tensor. Default: if None, same flow.device as this tensor.

  • requires_grad (bool, optional) – If autograd should record operations on the returned tensor. Default: False.

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = flow.Tensor(np.ones((1, 2, 3)))
>>> y = x.new_ones((2, 2))
>>> y
tensor([[1., 1.],
        [1., 1.]], dtype=oneflow.float32)
norm(ord=None, dim=None, keepdim=False)

See oneflow.experimental.linalg.norm()

normal_(mean=0, std=1)
numel()
numpy(**kwargs)
permute(*dims)

Returns a view of the original tensor with its dimensions permuted.

Parameters

*dims (int...) – The desired ordering of dimensions

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> input = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)
>>> out = input.permute(1, 0, 2, 3).shape
>>> out
flow.Size([6, 2, 5, 3])
property placement
pow(exponent)
Takes the power of each element in input with exponent and returns a tensor with the result. Exponent can be either a single float number, a single int number, or a tensor with the same shape as input.

When exponent is a scalar value, the operation applied is:

\[\text{out}_i = x_i ^ \text{exponent}\]

When exponent is a tensor, the operation applied is:

\[\text{out}_i = x_i ^ {\text{exponent}_i}\]
Args:
  • input (Tensor): the input tensor.

  • exponent (int, float, Tensor): the exponent.

Returns:

Tensor: The result of variance on the specified axis of input Tensor

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> x = flow.Tensor(np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]))
>>> out = flow.pow(x, 2).numpy()
>>> out
array([ 1.,  4.,  9., 16., 25., 36.], dtype=float32)

>>> x = flow.Tensor(np.array([1.0, 2.0, 3.0, 4.0]))
>>> y = flow.Tensor(np.array([1.0, 2.0, 3.0, 4.0]))
>>> out = flow.pow(x, y).numpy()
>>> out
array([  1.,   4.,  27., 256.], dtype=float32)
reciprocal()

Computes the safe reciprocal of x. If x is zero, the reciprocal will be also set to zero.

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = flow.Tensor(np.array([[1, 2, 3], [4, 5, 6]]))
>>> out = flow.reciprocal(x)
>>> out.numpy()
array([[1.        , 0.5       , 0.33333334],
       [0.25      , 0.2       , 0.16666667]], dtype=float32)
register_hook(**kwargs)
repeat(sizes)

This operator repeat the input tensor to a larger size along the specified dimensions.

Parameters
  • x (oneflow.Tensor) – The input Tensor.

  • size (Sequence[int]) – The number of times to repeat this tensor along each dimension

Returns

The result Tensor.

Return type

oneflow.Tensor

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> x = np.array([[[[0, 1]],
...               [[2, 3]],
...               [[4, 5]]]]).astype(np.int32)

>>> input = flow.Tensor(x)
>>> out = input.repeat(sizes=(1, 1, 2, 2))
>>> out.shape
flow.Size([1, 3, 2, 4])
property requires_grad
requires_grad_(requires_grad=True)
reshape(shape: Sequence[int] = None)

This operator reshapes a Tensor.

We can set one dimension in shape as -1, the operator will infer the complete shape.

Parameters
  • x – A Tensor.

  • shape – Shape of the output tensor.

Returns

A Tensor has the same type as x.

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = np.array(
...    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
... ).astype(np.float32)
>>> input = flow.Tensor(x)

>>> y = flow.reshape(input, shape=[2, 2, 2, -1]).shape
>>> y
flow.Size([2, 2, 2, 2])
retain_grad()
round() → Tensor

See oneflow.experimental.round()

rsqrt()

Returns a new tensor with the reciprocal of the square-root of each of the elements of input.

\[\text{out}_{i} = \frac{1}{\sqrt{\text{input}_{i}}}\]
Parameters

input

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> a = flow.Tensor(np.array([1.0, 2.0, 3.0]))
>>> out = flow.rsqrt(a).numpy()
>>> out
array([1.        , 0.70710677, 0.57735026], dtype=float32)
property sbp
set_data_initializer(data_initializer)
set_is_consistent(is_consistent)
set_is_lazy(is_lazy)
set_placement(placement)
set_sbp(sbp)
property shape
sigmoid()

Applies the element-wise function:

\[\text{Sigmoid}(x) = \sigma(x) = \frac{1}{1 + \exp(-x)}\]
Shape:
  • Input: \((N, *)\) where * means, any number of additional dimensions

  • Output: \((N, *)\), same shape as the input

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = flow.Tensor(np.array([0.81733328, 0.43621480, 0.10351428]))
>>> out = flow.sigmoid(x)
>>> out
tensor([0.6937, 0.6074, 0.5259], dtype=oneflow.float32)
sign() → Tensor

See oneflow.experimental.sign()

sin() → Tensor

See oneflow.experimental.sin()

sin_()

In-place version of oneflow.experimental.sin()

sinh() → Tensor

See oneflow.experimental.sinh()

size(idx=None)
softmax(dim=None)

Applies the Softmax function to an n-dimensional input Tensor rescaling them so that the elements of the n-dimensional output Tensor lie in the range [0,1] and sum to 1.

Softmax is defined as:

\[\text{Softmax}(x_{i}) = \frac{\exp(x_i)}{\sum_j \exp(x_j)}\]

When the input Tensor is a sparse tensor then the unspecifed values are treated as -inf.

Shape:
  • Input: \((*)\) where * means, any number of additional dimensions

  • Output: \((*)\), same shape as the input

Returns

a Tensor of the same dimension and shape as the input with values in the range [0, 1]

Parameters

dim (int) – A dimension along which Softmax will be computed (so every slice along dim will sum to 1).

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> m = flow.nn.Softmax(dim = 2)
>>> x = flow.Tensor(
...    np.array(
...        [[[-0.46716809,  0.40112534,  0.61984003],
...        [-1.31244969, -0.42528763,  1.47953856]]]
...    )
... )
>>> out = m(x)
>>> out
tensor([[[0.1575, 0.3754, 0.4671],
         [0.0507, 0.123 , 0.8263]]], dtype=oneflow.float32)
softplus()

Applies the element-wise function:

\[Softplus(x)= \frac{1}{β}*log(1+exp(β∗x))\]

SoftPlus is a smooth approximation to the ReLU function and can be used to constrain the output of a machine to always be positive.

For numerical stability the implementation reverts to the linear function when input X β > threshold.

Parameters
  • beta – the value for the Softplus formulation.Default:1

  • threshold – values above this revert to a linear function.Default:20

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow

>>> x1 = flow.Tensor(np.array([1, 2, 3]))
>>> x2 = flow.Tensor(np.array([1.53123589,0.54242598,0.15117185]))
>>> x3 = flow.Tensor(np.array([1,0,-1]))

>>> flow.enable_eager_execution()
>>> flow.softplus(x1).numpy()
array([1.3132616, 2.126928 , 3.0485873], dtype=float32)
>>> flow.softplus(x2).numpy()
array([1.7270232, 1.0006962, 0.771587 ], dtype=float32)
>>> flow.softplus(x3).numpy()
array([1.3132616 , 0.6931472 , 0.31326166], dtype=float32)
sort(dim: int = -1, descending: bool = False)

Sorts the elements of the input tensor along a given dimension in ascending order by value.

Parameters
  • input (oneflow.Tensor) – The input Tensor.

  • dim (int, optional) – dimension to be sorted. Defaults to the last dim (-1).

  • descending (bool, optional) – controls the sorting order (ascending or descending).

Returns

A tuple of (values, indices), where where the values are the sorted values and the indices are the indices of the elements in the original input tensor.

Return type

Tuple(oneflow.Tensor, oneflow.Tensor(dtype=int32))

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> x = np.array([[1, 3, 8, 7, 2], [1, 9, 4, 3, 2]], dtype=np.float32)
>>> input = flow.Tensor(x)
>>> (values, indices) = flow.sort(input)
>>> values
tensor([[1., 2., 3., 7., 8.],
        [1., 2., 3., 4., 9.]], dtype=oneflow.float32)
>>> indices
tensor([[0, 4, 1, 3, 2],
        [0, 4, 3, 2, 1]], dtype=oneflow.int32)
>>> (values, indices) = flow.sort(input, descending=True)
>>> values
tensor([[8., 7., 3., 2., 1.],
        [9., 4., 3., 2., 1.]], dtype=oneflow.float32)
>>> indices
tensor([[2, 3, 1, 4, 0],
        [1, 2, 3, 4, 0]], dtype=oneflow.int32)
>>> (values, indices) = flow.sort(input, dim=0)
>>> values
tensor([[1., 3., 4., 3., 2.],
        [1., 9., 8., 7., 2.]], dtype=oneflow.float32)
>>> indices
tensor([[0, 0, 1, 1, 0],
        [1, 1, 0, 0, 1]], dtype=oneflow.int32)
sqrt()

Returns a new tensor with the square-root of the elements of input.

\[\text{out}_{i} = \sqrt{\text{input}_{i}}\]
Parameters

input – the input tensor.

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> arr = np.array([1.0, 2.0, 3.0])
>>> input = flow.Tensor(arr)
>>> output = flow.sqrt(input).numpy()
>>> output
array([1.       , 1.4142135, 1.7320508], dtype=float32)
square()

Returns a new tensor with the square of the elements of input.

\[\text{out}_{i} = \sqrt{\text{input}_{i}}\]
Parameters

input – the input tensor.

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> arr = np.array([1.0, 2.0, 3.0])
>>> input = flow.Tensor(arr)
>>> output = flow.square(input).numpy()
>>> output
array([1., 4., 9.], dtype=float32)
squeeze(dim: Optional[Sequence[int]] = None)

This operator removes the specified dimention which size is 1 of the input Tensor. If the dim is not specified, this operator will remove all the dimention which size is 1 of the input Tensor.

The amount of element in return value is the same as Tensor input.

Parameters
  • input (oneflow.Tensor) – The input Tensor.

  • dim (Optional[Sequence[int]]) – The dim. Defaults to None.

Returns

The result Tensor.

Return type

Tensor

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> input = flow.Tensor(np.array([[[[1, 1, 1]]]]).astype(np.int32))
>>> out = flow.squeeze(input, dim=[1, 2]).shape
>>> out
flow.Size([1, 3])
stack(dim: int = 0) → None

Concatenates a sequence of tensors along a new dimension. The returned tensor shares the same underlying data with input tensors.

A dim value within the range [-input.ndimension() - 1, input.ndimension() + 1] can be used. Negative dim will correspond to stack() applied at dim = dim + input.ndimension() + 1.

Parameters
  • inputs (List[oneflow.Tensor]) – the list of input tensors. Each tensor should have the same shape.

  • dim (int) – the index at which to insert the concatenated dimension.

Returns

A Tensor

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()
>>> x = flow.Tensor(np.random.rand(1, 3, 5))
>>> y = flow.Tensor(np.random.rand(1, 3, 5))
>>> out = flow.stack([x, y], dim = -1)
>>> out.shape
flow.Size([1, 3, 5, 2])
std(dim, unbiased=True, keepdim=False)

Returns the standard-deviation of each row of the input tensor in the dimension dim. If dim is a list of dimensions, reduce over all of them.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed, resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

If unbiased is False, then the standard-deviation will be calculated via the biased estimator. Otherwise, Bessel’s correction will be used.

Parameters
  • input (Tensor) – the input tensor.

  • (int or tuple of python (dim) – ints): the dimension or dimensions to reduce.

  • unbiased (bool) – whether to use the unbiased estimation or not

  • keepdim (bool) – whether the output tensor has dim retained or not.

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> arr = np.array([1.0, 2.0, 3.0])
>>> input = flow.Tensor(arr)
>>> output = flow.std(input, dim=0).numpy()
>>> output
array([0.8164968], dtype=float32)
storage_offset()
stride()
sub(other)

Computes the subtraction of input by other for each element, scalar and broadcast promotation are supported. The formula is:

\[out = input - other\]

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

# element-wise subtract
>>> input = flow.Tensor(np.random.randn(2,3))
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.sub(input,other).numpy()
>>> out.shape
(2, 3)

# scalar subtract
>>> input = 5
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.sub(input,other).numpy()
>>> out.shape
(2, 3)

# broadcast subtract
>>> input = flow.Tensor(np.random.randn(1,1))
>>> other = flow.Tensor(np.random.randn(2,3))
>>> out = flow.sub(input,other).numpy()
>>> out.shape
(2, 3)
sum(dim=None, keepdim=False)

Computes the sum of row of elements in a tensor in the given axis, if the axis is None, sum of all elements will be caculated.

For example:

>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()
>>> input = flow.Tensor([[1, 2, 3], [4, 5, 6]])
>>> flow.sum(input)
tensor([21.], dtype=oneflow.float32)
>>> flow.sum(input, dim=0)
tensor([5., 7., 9.], dtype=oneflow.float32)
>>> flow.sum(input, dim=1)
tensor([ 6., 15.], dtype=oneflow.float32)
tan() → Tensor

See oneflow.experimental.tan()

tanh()

This operator computes the hyperbolic tangent value of Tensor.

The equation is:

\[out = \frac{e^x-e^{-x}}{e^x+e^{-x}}\]
Parameters

x (oneflow.Tensor) – A Tensor

Returns

The result Tensor

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = np.array([-1, 0, 1]).astype(np.float32)
>>> input = flow.Tensor(x)
>>> tanh = flow.nn.Tanh()
>>> out = tanh(input)
>>> out
tensor([-0.7616,  0.    ,  0.7616], dtype=oneflow.float32)
tile(reps)

The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.tile.html

Constructs a tensor by repeating the elements of input. The reps argument specifies the number of repetitions in each dimension.

If reps specifies fewer dimensions than input has, then ones are prepended to reps until all dimensions are specified. For example, if input has shape (8, 6, 4, 2) and reps is (2, 2), then reps is treated as (1, 1, 2, 2).

Analogously, if input has fewer dimensions than reps specifies, then input is treated as if it were unsqueezed at dimension zero until it has as many dimensions as reps specifies. For example, if input has shape (4, 2) and reps is (3, 3, 2, 2), then input is treated as if it had the shape (1, 1, 4, 2).

Note

This function is similar to NumPy’s tile function.

Parameters
  • input (oneflow.Tensor) – the tensor whose elements to repeat.

  • reps (tuple) – the number of repetitions per dimension.

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> x = np.array([1, 2]).astype(np.int32)
>>> input = flow.Tensor(x, dtype=flow.int32)
>>> out = input.tile(reps=(2,))
>>> out
tensor([1, 2, 1, 2], dtype=oneflow.int32)

>>> x = np.random.randn(5, 2, 1)
>>> input = flow.Tensor(x)
>>> out = input.tile(reps=(3, 4))
>>> out.size()
flow.Size([5, 6, 4])
to(*args, **kwargs)
Performs Tensor dtype and/or device conversion.

A flow.dtype and flow.device are inferred from the arguments of input.to(*args, **kwargs).

Note

If the input Tensor already has the correct flow.dtype and flow.device, then input is returned. Otherwise, the returned tensor is a copy of input with the desired.

Parameters
Returns

A Tensor.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> arr = np.random.randint(1, 9, size=(1, 2, 3, 4))
>>> input = flow.Tensor(arr)
>>> output = input.to(dtype=flow.float32)
>>> np.array_equal(arr.astype(np.float32), output.numpy())
True
tolist()
topk(k, dim: int = None, largest: bool = True, sorted: bool = True)

Finds the values and indices of the k largest entries at specified axis.

Parameters
  • input (oneflow.Tensor) – Input Tensor

  • dim (int, optional) – the dimension to sort along. Defaults to the last dim (-1)

  • largest (bool, optional) – controls whether to return largest or smallest elements

  • sorted (bool, optional) – controls whether to return the elements in sorted order

Returns

A tuple of (values, indices), where the indices are the indices of the elements in the original input tensor.

Return type

Tuple(oneflow.Tensor, oneflow.Tensor(dtype=int32))

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()
>>> x = np.array([[1, 3, 8, 7, 2], [1, 9, 4, 3, 2]], dtype=np.float32)
>>> (values, indices) = flow.topk(flow.Tensor(x), k=3, dim=1)
>>> values
tensor([[8., 7., 3.],
        [9., 4., 3.]], dtype=oneflow.float32)
>>> indices
tensor([[2, 3, 1],
        [1, 2, 3]], dtype=oneflow.int32)
>>> values.shape
flow.Size([2, 3])
>>> indices.shape
flow.Size([2, 3])
>>> (values, indices) = flow.topk(flow.Tensor(x), k=2, dim=1, largest=False)
>>> values
tensor([[1., 2.],
        [1., 2.]], dtype=oneflow.float32)
>>> indices
tensor([[0, 4],
        [0, 4]], dtype=oneflow.int32)
>>> values.shape
flow.Size([2, 2])
>>> indices.shape
flow.Size([2, 2])
transpose(dim0, dim1)

Returns a tensor that is a transposed version of input. The given dimensions dim0 and dim1 are swapped.

The resulting out tensor shares its underlying storage with the input tensor, so changing the content of one would change the content of the other.

Parameters
  • tensor (oneflow.Tensor) – The input tensor.

  • dim0 (int) – the first dimension to be transposed.

  • dim1 (int) – the second dimension to be transposed.

Returns

A transposed tensor.

Return type

Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> input = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32)
>>> out = flow.transpose(input, 0, 1).shape
>>> out
flow.Size([6, 2, 5, 3])
triu(diagonal=0)

Returns the upper triangular part of a matrix (2-D tensor) or batch of matrices input, the other elements of the result tensor out are set to 0.

Parameters
  • input (Tensor) – the input tensor.

  • diagonal (int, optional) – the diagonal to consider

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> x = flow.Tensor(np.ones(shape=(3, 3)).astype(np.float32))
>>> flow.triu(x)
tensor([[1., 1., 1.],
        [0., 1., 1.],
        [0., 0., 1.]], dtype=oneflow.float32)
type_as(target)
Returns this tensor cast to the type of the given tensor.

This is a no-op if the tensor is already of the correct type.

Parameters
  • input (Tensor) – the input tensor.

  • target (Tensor) – the tensor which has the desired type.

For example:

>>> import oneflow.experimental as flow
>>> import numpy as np
>>> flow.enable_eager_execution()

>>> input = flow.Tensor(np.random.randn(1, 2, 3), dtype=flow.float32)
>>> target = flow.Tensor(np.random.randn(4, 5, 6), dtype = flow.int32)
>>> input = input.type_as(target)
>>> input.dtype
oneflow.int32
uniform_(a=0, b=1)
unsqueeze(dim)

Returns a new tensor with a dimension of size one inserted at the specified position.

The returned tensor shares the same underlying data with this tensor.

A dim value within the range [-input.ndimension() - 1, input.ndimension() + 1) can be used. Negative dim will correspond to unsqueeze() applied at dim = dim + input.ndimension() + 1.

Parameters
  • input (Tensor) – the input tensor.

  • dim (int) – the index at which to insert the singleton dimension

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = flow.Tensor(np.random.rand(2, 3, 4))
>>> y = x.unsqueeze(2)
>>> y.shape
flow.Size([2, 3, 1, 4])
var(dim=None, keepdim=False)

Returns the variance of each row of the input tensor in the given dimension dim.

If keepdim is True, the output tensor is of the same size as input except in the dimension(s) dim where it is of size 1. Otherwise, dim is squeezed (see flow.squeeze()), resulting in the output tensor having 1 (or len(dim)) fewer dimension(s).

Parameters
  • input (Tensor) – the input tensor.

  • (int or tuple of python (dim) – ints): the dimension or dimensions to reduce. Defaults to None.

  • keepdim (bool, optional) – whether the output tensor has dim retained or not. Defaults to False.

Returns

The result of variance on the specified axis of input Tensor

Return type

Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> np_arr = np.random.randn(2,3,4,5)
>>> input = flow.Tensor(np_arr)
>>> output = flow.var(input, 1, True)
view(shape: Sequence[int] = None)

The interface is consistent with PyTorch. The documentation is referenced from: https://pytorch.org/docs/stable/generated/torch.Tensor.view.html

Returns a new tensor with the same data as the self tensor but of a different shape.

The returned tensor shares the same data and must have the same number of elements, but may have a different size. For a tensor to be viewed, the new view size must be compatible with its original size and stride, i.e., each new view dimension must either be a subspace of an original dimension, or only span across original dimensions \(d, d+1, \dots, d+k\) that satisfy the following contiguity-like condition that \(\forall i = d, \dots, d+k-1\),

\[\text{stride}[i] = \text{stride}[i+1] \times \text{size}[i+1]\]

Otherwise, it will not be possible to view self tensor as shape without copying it (e.g., via contiguous()). When it is unclear whether a view() can be performed, it is advisable to use reshape(), which returns a view if the shapes are compatible, and copies (equivalent to calling contiguous()) otherwise.

Parameters
  • x – A Tensor.

  • shape – Shape of the output tensor.

Returns

A Tensor has the same type as x.

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = np.array(
...    [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
... ).astype(np.float32)
>>> input = flow.Tensor(x)

>>> y = flow.view(input, shape=[2, 2, 2, -1]).numpy().shape
>>> y
(2, 2, 2, 2)
where(x, y)

Return a tensor of elements selected from either x or y, depending on condition. If the element in condition is larger than 0,

it will take the x element, else it will take the y element

Note

The tensors condition, x, y must be broadcastable. It will take the x element, else it will take the y element.

Parameters
  • condition (IntTensor) – When 1 (nonzero), yield x, otherwise yield y

  • x (Tensor or Scalar) – value (if :attr:x is a scalar) or values selected at indices where condition is True

  • y (Tensor or Scalar) – value (if :attr:x is a scalar) or values selected at indices where condition is False

Returns

A tensor of shape equal to the broadcasted shape of condition, x, y

Return type

Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> x = flow.Tensor(
...    np.array([[-0.4620, 0.3139], [0.3898, -0.7197], [0.0478, -0.1657]]),
...    dtype=flow.float32,
... )
>>> y = flow.Tensor(np.ones(shape=(3, 2)), dtype=flow.float32)
>>> condition = flow.Tensor(np.array([[0, 1], [1, 0], [1, 0]]), dtype=flow.int32)
>>> out = condition.where(x, y)
>>> out 
tensor([[1.    , 0.3139],
        ...
        [0.0478, 1.    ]], dtype=oneflow.float32)
xavier_normal_(gain=1.0, *, data_format='NCHW')
xavier_uniform_(gain=1.0, *, data_format='NCHW')
zeros_(**kwargs)
oneflow.acc(one: oneflow._oneflow_internal.BlobDesc, max_acc_num: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.amp_white_identity(x: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.argsort(input: oneflow._oneflow_internal.BlobDesc, axis: int = -1, direction: str = 'ASCENDING', name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator sorts the input Blob at specified axis and return the indices of the sorted Blob.

Parameters
  • input (oneflow._oneflow_internal.BlobDesc) – A Blob

  • axis (int, optional) – dimension to be sorted. Defaults to the last dim (-1)

  • direction (str, optional) – The direction in which to sort the Blob values. If the direction is “ASCENDING”, The order of input will be sorted as ascending, else, the order of input will be sorted as descending. Defaults to “ASCENDING”.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The indices of the sorted Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def argsort_Job(x: tp.Numpy.Placeholder((5, ))
) -> tp.Numpy:
    return flow.argsort(input=x)

x = np.array([10, 2, 9, 3, 7]).astype("float32")
out = argsort_Job(x)

# out [1 3 4 2 0]
oneflow.argwhere(condition: oneflow._oneflow_internal.BlobDesc, dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator finds the indices of input Blob condition elements that are non-zero. It returns a List. Each element in the output is a coordinate that points to a non-zero element in the condition.

Parameters
  • condition (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • dtype (Optional[flow.dtype], optional) – The data type of output. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob. Its type is ListNumpy.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def argwhere_Job(x: tp.Numpy.Placeholder(shape=(2, 3), dtype=flow.float32),
) -> tp.ListNumpy:
    return flow.argwhere(x)


x = np.array([[0, 1, 0],
            [2, 0, 2]]).astype(np.float32)
out = argwhere_Job(x)

# out [array([[0, 1],
#             [1, 0],
#             [1, 2]], dtype=int32)]
oneflow.assign(ref, value, dtype=None, name=None)
oneflow.broadcast_like(x: oneflow._oneflow_internal.BlobDesc, like: oneflow._oneflow_internal.BlobDesc, broadcast_axes: Optional[Sequence[int]] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator broadcast the input Blob x on the specified axis with input Blob like.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • like (oneflow._oneflow_internal.BlobDesc) – A Blob.

  • broadcast_axes (Optional[Sequence[int]], optional) – The broadcast axis. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises

ValueError – The length of broadcast_axes must be greater than 0 and less than or equal to number of axes of like shape.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

Example 1:

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


@flow.global_function()
def broadcast_like_Job(x: tp.Numpy.Placeholder(shape=(3, 1), dtype=flow.float32)
) -> tp.Numpy:
    like_tensor = flow.constant(value=1.0,
                                dtype=flow.float32,
                                shape=(3, 3))
    return flow.broadcast_like(x=x,
                            like=like_tensor,
                            broadcast_axes=(1, ))


x = np.array([[1], [1], [1]]).astype(np.float32)
out = broadcast_like_Job(x)

# out [[[1 1 1]
#       [1 1 1]
#       [1 1 1]]]

# out.shape (3, 3)

Example 2:

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


@flow.global_function()
def broadcast_like_Job(x: tp.Numpy.Placeholder(shape=(3, 1, 1), dtype=flow.float32)
) -> tp.Numpy:
    like_tensor = flow.constant(value=1.0,
                                dtype=flow.float32,
                                shape=(3, 3, 3))
    return flow.broadcast_like(x=x,
                            like=like_tensor,
                            broadcast_axes=(1, 2))


x = np.random.randn(3, 1, 1).astype(np.float32)
out = broadcast_like_Job(x)

# out.shape (3, 3, 3)
oneflow.broadcast_to_compatible_with(x: oneflow._oneflow_internal.BlobDesc, compatible: Sequence[oneflow._oneflow_internal.BlobDesc], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Returns a ‘Blob’ with the shape can be broadcasted by other shapes

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – a ‘Blob’

  • compatible (Sequence[oneflow._oneflow_internal.BlobDesc]) – Sequence of different shape

  • name (Optional[str], optional) – This operator’s name. Defaults to None.

Returns

A ‘Blob’ with the biggest shape

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def broadcast_to_compatible_with_Job(x: tp.Numpy.Placeholder((4, 1, 1))
)->tp.Numpy:
    blob_a = flow.constant(value=1, dtype=flow.float32, shape=(1, 2, 1))
    blob_b = flow.constant(value=1, dtype=flow.float32, shape=(1, 1, 3))

    return flow.math.broadcast_to_compatible_with(x, [blob_a, blob_b])

x = np.ones(shape=(4, 1, 1), dtype=np.float32)

out = broadcast_to_compatible_with_Job(x)

# out.shape (4, 2, 3)
oneflow.builtin_op

alias of oneflow.python.ops.builtin_ops.BuiltinOp

oneflow.cast(x: oneflow._oneflow_internal.BlobDesc, dtype: oneflow._oneflow_internal.dtype, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

The op takes input x and casts it to the output with dtype

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – Input Blob

  • dtype (flow.dtype) – Data type of the output

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def cast_Job(x: tp.Numpy.Placeholder((3, ), dtype=flow.float32)
)->tp.Numpy:
    return flow.cast(x, dtype=flow.int32)

x = np.array([1, 2, 3]).astype(np.float32)
out = cast_Job(x)

# out.dtype = "int32"
oneflow.cast_to_current_logical_view(x: oneflow._oneflow_internal.BlobDesc) → oneflow._oneflow_internal.BlobDesc
oneflow.cast_to_static_shape(x: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator returns a Blob that has identical content and data type to input Blob, and whose shape is converted from dynamic to static

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob which has dynamic shape.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob which is identical to input blob but has static shape.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def cast_to_static_shape_func(
    x: tp.ListNumpy.Placeholder(shape=(3, 3), dtype=flow.float32),
) -> tp.Numpy:
    return flow.cast_to_static_shape(x)

x = np.array([[1, 1, 1],
              [2, 2, 2],
              [3, 3, 3]]).astype(np.float32)

out = cast_to_static_shape_func(x)

# out [[1 1 1]
#      [2 2 2]
#      [3 3 3]]
oneflow.categorical_ordinal_encode(table: oneflow._oneflow_internal.BlobDesc, size: oneflow._oneflow_internal.BlobDesc, input_tensor: oneflow._oneflow_internal.BlobDesc, hash_precomputed: bool = True, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator maintains a hash table to encode the categorical ordinal Blob. It converts a discrete input value into a continuous integer ID.

Parameters
  • table (oneflow._oneflow_internal.BlobDesc) – The hash table, you can assign it as a variable.

  • size (oneflow._oneflow_internal.BlobDesc) – The size of hash table.

  • input_tensor (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • hash_precomputed (bool, optional) – We currently only support the ‘True’ mode. The internal hash value will no longer be computed. Defaults to True.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def categorical_ordinal_encode_Job(x: tp.Numpy.Placeholder((3, 3), dtype=flow.int32)
) -> tp.Numpy:
    dtype = x.dtype
    with flow.scope.namespace("categorical_ordinal_encode"):
        table = flow.get_variable(
            name="Table",
            shape=(16,),
            dtype=dtype,
            initializer=flow.constant_initializer(0, dtype=dtype),
            trainable=False,
            reuse=False,
        )
        size = flow.get_variable(
            name="Size",
            shape=(1,),
            dtype=dtype,
            initializer=flow.constant_initializer(0, dtype=dtype),
            trainable=False,
            reuse=False,
        )
        return flow.categorical_ordinal_encode(
            table=table, size=size, input_tensor=x, name="Encode",
        )

x = np.array([[7, 0, 2],
            [1, 7, 2],
            [0, 1, 7]]).astype(np.int32)

out = categorical_ordinal_encode_Job(x)

# out [[1 0 2]
#      [3 1 2]
#      [0 3 1]]
oneflow.clamp(values: oneflow._oneflow_internal.BlobDesc, min_value: Union[int, float, None] = None, max_value: Union[int, float, None] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This op clips Blob values to a specified min value and max value.

The equation is:

\[out = MIN(MAX(x, min), max)\]
Parameters
  • values (oneflow._oneflow_internal.BlobDesc) – Input Blob

  • min_value (Optional[Union[int, float]], optional) – The minimum value to clip by. Defaults to None.

  • max_value (Optional[Union[int, float]], optional) – The maximum value to clip by. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises

ValueError – min_value and max_value cannot be None at the same time

Returns

A clipped Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def clip_by_value_Job(x: tp.Numpy.Placeholder((4, ))
)->tp.Numpy:
    return flow.math.clip_by_value(x, min_value=-1, max_value=5)

x = np.array([-2, 1, 4, 7], dtype=np.float32)

out = clip_by_value_Job(x)

# out [-1. 1. 4. 5.]
oneflow.clear_default_session() → None

Clear the default session. All compiled OneFlow functions will be deleted.

oneflow.clip(values: oneflow._oneflow_internal.BlobDesc, min_value: Union[int, float, None] = None, max_value: Union[int, float, None] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This op clips Blob values to a specified min value and max value.

The equation is:

\[out = MIN(MAX(x, min), max)\]
Parameters
  • values (oneflow._oneflow_internal.BlobDesc) – Input Blob

  • min_value (Optional[Union[int, float]], optional) – The minimum value to clip by. Defaults to None.

  • max_value (Optional[Union[int, float]], optional) – The maximum value to clip by. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises

ValueError – min_value and max_value cannot be None at the same time

Returns

A clipped Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def clip_by_value_Job(x: tp.Numpy.Placeholder((4, ))
)->tp.Numpy:
    return flow.math.clip_by_value(x, min_value=-1, max_value=5)

x = np.array([-2, 1, 4, 7], dtype=np.float32)

out = clip_by_value_Job(x)

# out [-1. 1. 4. 5.]
oneflow.clip_by_scalar(values: oneflow._oneflow_internal.BlobDesc, min_value: Union[int, float, None] = None, max_value: Union[int, float, None] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This op clips Blob values to a specified min value and max value.

The equation is:

\[out = MIN(MAX(x, min), max)\]
Parameters
  • values (oneflow._oneflow_internal.BlobDesc) – Input Blob

  • min_value (Optional[Union[int, float]], optional) – The minimum value to clip by. Defaults to None.

  • max_value (Optional[Union[int, float]], optional) – The maximum value to clip by. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises

ValueError – min_value and max_value cannot be None at the same time

Returns

A clipped Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def clip_by_value_Job(x: tp.Numpy.Placeholder((4, ))
)->tp.Numpy:
    return flow.math.clip_by_value(x, min_value=-1, max_value=5)

x = np.array([-2, 1, 4, 7], dtype=np.float32)

out = clip_by_value_Job(x)

# out [-1. 1. 4. 5.]
oneflow.clip_by_value(values: oneflow._oneflow_internal.BlobDesc, min_value: Union[int, float, None] = None, max_value: Union[int, float, None] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This op clips Blob values to a specified min value and max value.

The equation is:

\[out = MIN(MAX(x, min), max)\]
Parameters
  • values (oneflow._oneflow_internal.BlobDesc) – Input Blob

  • min_value (Optional[Union[int, float]], optional) – The minimum value to clip by. Defaults to None.

  • max_value (Optional[Union[int, float]], optional) – The maximum value to clip by. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises

ValueError – min_value and max_value cannot be None at the same time

Returns

A clipped Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def clip_by_value_Job(x: tp.Numpy.Placeholder((4, ))
)->tp.Numpy:
    return flow.math.clip_by_value(x, min_value=-1, max_value=5)

x = np.array([-2, 1, 4, 7], dtype=np.float32)

out = clip_by_value_Job(x)

# out [-1. 1. 4. 5.]
oneflow.combined_margin_loss(x: oneflow._oneflow_internal.BlobDesc, label: oneflow._oneflow_internal.BlobDesc, m1: float = 1, m2: float = 0, m3: float = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.concat(inputs: Optional[Sequence[oneflow._oneflow_internal.BlobDesc]] = None, axis: int = 0, max_dim_size: Optional[int] = None, name: Optional[str] = None, values: Optional[Sequence[oneflow._oneflow_internal.BlobDesc]] = None) → oneflow._oneflow_internal.BlobDesc

Concatenate two or more Blob s at specified axis.

Analogous to numpy.concatenate

Parameters
  • inputs – a list of Blob

  • axis – a int. 0 by default

  • max_dim_size – hint of max dimension size along the given axis

  • name – name of this operator. None by default

  • values – deprecated param, use inputs instead

Returns

A Blob

For example:

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


@flow.global_function()
def concat_Job() -> tp.Numpy:
    constant_blob_1 = flow.constant(value=1.5,
                                    shape=(1, 3, 3, 4),
                                    dtype=flow.float,
                                    name="blob1")
    constant_blob_2 = flow.constant(value=2.5,
                                    shape=(1, 3, 3, 4),
                                    dtype=flow.float,
                                    name="blob2")
    return flow.concat(inputs=[constant_blob_1, constant_blob_2],
                    axis=3)


out = concat_Job()

# out.shape (1, 3, 3, 8)
oneflow.consistent_user_op_builder(op_name)
oneflow.consistent_user_op_module_builder(op_type_name)
oneflow.constant(value: Union[int, float], dtype: Optional[oneflow._oneflow_internal.dtype] = None, shape: Optional[Sequence[int]] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator creates a constant Blob.

Parameters
  • value (Union[int, float]) – The constant value of Blob.

  • dtype (Optional[flow.dtype], optional) – The data type of Blob. Defaults to None.

  • shape (Optional[Sequence[int]], optional) – The shape 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

The result blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


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


out = constant_Job()

# out [[[1.5 1.5 1.5]
#       [1.5 1.5 1.5]
#       [1.5 1.5 1.5]]]
oneflow.constant_initializer(value: float = 0, dtype: oneflow._oneflow_internal.dtype = oneflow.float32) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates blob with constant values.

Parameters
  • value (float, optional) – A Python scalar. All elements of the initialized variable . Defaults to 0.

  • dtype (flow.dtype, optional) – Default data type. Defaults to flow.float.

Raises

NotImplementedError – Do not support such data type.

Returns

An InitializerConf object.

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def constant_Job() -> None:
    init = flow.constant_initializer(2.5)
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
constant_Job()

# out [2.5 2.5 2.5]

Example 2:

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


@flow.global_function()
def conv2d_constant_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.constant_initializer(0.01)
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_constant_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.constant_like(like: oneflow._oneflow_internal.BlobDesc, value: Union[int, float], dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator creates a constant Blob that has the same shape as like.

Parameters
  • 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

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import oneflow as flow
import numpy as np
import oneflow.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]]]
oneflow.constant_pad2d(x: oneflow._oneflow_internal.BlobDesc, padding: Union[int, tuple, list], constant_value: Union[int, float] = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Pads the input tensor using an input constant value.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – input blob, only support “NCHW” format.

  • padding (Union[int, oneflow._oneflow_internal.BlobDesc]) – The size or bundary of padding, if is int uses the same padding in all dimension;

  • 4-dims tuple, uses (if) –

  • constant_value (Union[int, float]) – The constant value used for padding. Defaults to Zero.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

[description]

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def pad_Job(x: tp.Numpy.Placeholder((1, 2, 3, 3), const_value)
) -> tp.Numpy:
    return flow.constant_pad2d(x, padding=[2, 2, 1, 1], const_value)


x = np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)
const_value = 1.5
out = pad_Job(x, const_value)

# out [[[[ 1.5  1.5  1.5  1.5  1.5  1.5  1.5]
#    [ 1.5  1.5  0.  1.  2.  1.5  1.5]
#    [ 1.5  1.5  3.  4.  5.  1.5  1.5]
#    [ 1.5  1.5  6.  7.  8.  1.5  1.5]
#    [ 1.5  1.5  1.5  1.5  1.5  1.5  1.5]]

#   [[ 1.5  1.5  1.5  1.5  1.5  1.5  1.5.]
#    [ 1.5  1.5  9.  10.  11.  1.5  1.5]
#    [ 1.5  1.5  12.  13.  14.  1.5  1.5]
#    [ 1.5  1.5  15.  16.  17.  1.5  1.5]
#    [ 1.5  1.5  1.5  1.5  1.5  1.5  1.5]]]]
oneflow.constant_scalar(value: Union[int, float], dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator creates a constant scalar Blob.

Parameters
  • 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.

Returns

The result blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def constant_scalar_Job() -> tp.Numpy:
    constant_scalar = flow.constant_scalar(value=2.5,
                                        dtype=flow.float)
    return constant_scalar


out = constant_scalar_Job()

# out [2.5]
oneflow.convert_oneflow_dtype_to_numpy_dtype(oneflow_dtype: oneflow._oneflow_internal.dtype)
oneflow.count_not_finite(x: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.ctc_loss(log_probs: oneflow._oneflow_internal.BlobDesc, targets: oneflow._oneflow_internal.BlobDesc, input_lengths: oneflow._oneflow_internal.BlobDesc, target_lengths: oneflow._oneflow_internal.BlobDesc, blank: int = 0, reduction: str = 'mean', zero_infinity: bool = False, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Computes the CTC(Connectionist Temporal Classification) loss. This operator implements the CTC loss as presented in (Graves et al., 2006).

Parameters
  • log_probs (oneflow._oneflow_internal.BlobDesc) – A Blob of shape [input_length, batch_size, num_labels]. The logarithmized probabilities of the outputs (e.g. obtained with flow.nn.logsoftmax()).

  • targets (oneflow._oneflow_internal.BlobDesc) – A Blob of shape [batch_size, max_target_length]. It represent the target sequences. Each element in the target sequence is a class index. And the target index cannot be blank (default=0).

  • input_lengths (oneflow._oneflow_internal.BlobDesc) – A Blob of shape [batch_size]. It represent the lengths of the inputs. And the lengths are specified for each sequence to achieve masking under the assumption that sequences are padded to equal lengths.

  • target_lengths (oneflow._oneflow_internal.BlobDesc) – A Blob of shape [batch_size]. It represent lengths of the targets. Lengths are specified for each sequence to achieve masking under the assumption that sequences are padded to equal lengths.

  • blank (int, optional) – Blank label. Defaults to 0.

  • reduction (str, optional) – The reduce type, it can be the one of “none”, “mean”, “sum”. “none”: no reduction will be applied, “mean”: the output losses will be divided by the target lengths and then the mean over the batch is taken, “sum”: the output will be summed. Defaults to “mean”.

  • zero_infinity (bool, optional) – Whether to zero infinite losses and the associated gradients. Infinite losses mainly occur when the inputs are too short to be aligned to the targets. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def ctc_loss_job(
    log_probs: tp.Numpy.Placeholder(shape=(5, 2, 3)),
    targets: tp.Numpy.Placeholder(shape=(2, 3), dtype=flow.int32),
    input_lengths: tp.Numpy.Placeholder(shape=(2,), dtype=flow.int32),
    target_lengths: tp.Numpy.Placeholder(shape=(2,), dtype=flow.int32),
) -> tp.Numpy:
    loss = flow.ctc_loss(
        log_probs, targets, input_lengths, target_lengths, blank=0, reduction="none"
    )
    return loss


log_probs = np.array(
    [
        [[-1.1031, -0.7998, -1.5200], [-0.9808, -1.1363, -1.1908]],
        [[-1.2258, -1.0665, -1.0153], [-1.1135, -1.2331, -0.9671]],
        [[-1.3348, -0.6611, -1.5118], [-0.9823, -1.2355, -1.0941]],
        [[-1.3850, -1.3273, -0.7247], [-0.8235, -1.4783, -1.0994]],
        [[-0.9049, -0.8867, -1.6962], [-1.4938, -1.3630, -0.6547]],
    ]
).astype(np.float32)
targets = np.array([[1, 2, 2], [1, 2, 2]]).astype("int32")
input_lengths = np.array([5, 5]).astype("int32")
target_lengths = np.array([3, 3]).astype("int32")
loss = ctc_loss_job(log_probs, targets, input_lengths, target_lengths)

# loss [3.918017 2.907672]
oneflow.current_global_function_desc() → oneflow.python.framework.function_desc.FunctionDesc
oneflow.current_machine_id()

Get machine id of current machine/node

Returns

[description]

Return type

[type]

oneflow.current_resource() → oneflow.core.job.resource_pb2.Resource
Get current resources, such as:machine nums, cpu/gpu device nums,

epoch network threed num, rdma params…

Returns

[description]

Return type

resource_util.Resource

oneflow.current_scope()

Return current scope

oneflow.default_generator(device=None)
class oneflow.device
property index
property type
oneflow.diag(input: oneflow._oneflow_internal.BlobDesc, diagonal: Optional[int] = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator compute diagonal.

If input is a vector, then returns a square matrix with the elements of input as the diagonal. If input is a matrix, then returns a vector with the diagonal elements of input.

Parameters
  • input (remote_blob_util.BlobDef) – The input Blob

  • diagonal (Optional[int], 0) – The diagonal to consider. If diagonal = 0, it is the main diagonal. If diagonal > 0, it is above the main diagonal. If diagonal < 0, it is below the main diagonal. Defaults to 0.

Returns

The result Blob.

Return type

remote_blob_util.BlobDef

For example:

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


@flow.global_function()
def Diag_Job(input: tp.Numpy.Placeholder((3, 3), dtype=flow.float32),) -> tp.Numpy:
    return flow.diag(input)


input = np.array([[1.0, 2.0, 3.0],
                  [4.0, 5.0, 6.0],
                  [7.0, 8.0, 9.0],], dtype=np.float32)
out = Diag_Job(input)
# out [1. 5. 9.]
oneflow.dim_gather(input: oneflow._oneflow_internal.BlobDesc, dim: int, index: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator gathers elements from input according to index along with the axis dim.

Take a 3-D blob as example, the output is specified by:

output[i][j][k] = input[index[i][j][k]][j][k]  # if dim == 0
output[i][j][k] = input[i][index[i][j][k]][k]  # if dim == 1
output[i][j][k] = input[i][j][index[i][j][k]]  # if dim == 2

The shape of input and index should be the same except in the dim dimension.

That is, if input is a n-dimension blob with shape \((x_0, x_1, \dots, x_{i-1}, x_i, x_{i+1}, \dots, x_n)\), and dim = i, then index must be a n-dimension blob with shape \((x_0, x_1, \dots, x_{i-1}, k, x_{i+1}, \dots, x_n)\) where \(k \geq 1\).

The return Blob output will have the same shape with index.

Parameters
  • input (oneflow._oneflow_internal.BlobDesc) – The input blob

  • dim (int) – The axis along which to index

  • index (oneflow._oneflow_internal.BlobDesc) – The index blob of elements to gather

  • name (Optional[str], optional) – The name of the operation. Defaults to None.

Returns

The elements gathered from input will be returned as the output Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def dim_gather_Job(input: tp.Numpy.Placeholder((2, 2), dtype=flow.float64),
                index:tp.Numpy.Placeholder((2, 2), dtype=flow.int32))->tp.Numpy:
    return flow.dim_gather(input, 1, index)

input = np.array([[1, 2], [3, 4]]).astype(np.float64)
index = np.array([[1, 0], [0, 1]]).astype(np.int32)

out = dim_gather_Job(input, index)
# output
# [[2. 1.]
#  [3. 4.]]
oneflow.distributed_partial_fc_sample(weight: oneflow._oneflow_internal.BlobDesc, label: oneflow._oneflow_internal.BlobDesc, num_sample: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
class oneflow.dtype
property bytes
property is_complex
property is_floating_point
property is_signed
oneflow.dtypes()
oneflow.dynamic_reshape(x: oneflow._oneflow_internal.BlobDesc, shape: Sequence[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator reshapes a dynamic blob.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • shape (Sequence[int]) – The output shape.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def dynamic_reshape_Job(x: tp.Numpy.Placeholder(shape=(1, 3, 64, 64), dtype=flow.float32)
) -> tp.Numpy:
    reshape_out1 = flow.dynamic_reshape(x, (-1, 64))
    variable1 = flow.get_variable(
        "var1",
        shape=(64, 32),
        dtype=flow.float,
        initializer=flow.random_uniform_initializer(minval=-10, maxval=10),
        trainable=True,
    )
    matmul_tensor = flow.matmul(reshape_out1, variable1)
    reshape_out2 = flow.dynamic_reshape(matmul_tensor, (-1, 8, 4))
    return reshape_out2

x = np.random.rand(1, 3, 64, 64).astype(np.float32)
out = dynamic_reshape_Job(x)

# out.shape (192, 8, 4)
oneflow.eager_execution_enabled() → bool

Get current setting of the job, if enable eager execution mode ,then return True

Returns

[description]

Return type

bool

oneflow.eager_nccl_all_reduce(x: oneflow._oneflow_internal.BlobDesc, parallel_conf: str, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.elem_cnt(inputs: oneflow._oneflow_internal.BlobDesc, dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator returns the amount of elements in input Blob.

Parameters
  • inputs (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • dtype (Optional[flow.dtype], optional) – The data type. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob. Its type is ListNumpy.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def elem_cnt_Job(x: tp.Numpy.Placeholder(shape=(5, ), dtype=flow.float32),
) -> tp.ListNumpy:
    return flow.elem_cnt(inputs=x, dtype=flow.int32)

x = np.array([10, 20, -30, 40, 50]).astype(np.float32)
out = elem_cnt_Job(x)

# [array([5], dtype=int32)]
oneflow.empty_initializer(dtype: oneflow._oneflow_internal.dtype = oneflow.float32) → oneflow.core.job.initializer_conf_pb2.InitializerConf
oneflow.enable_eager_execution(val: bool = True) → None

If True, job will execute in eager mode, else use lazy mode(static graph).

Parameters

val (bool, optional) – Whether eager execution or not. Defaults to True.

oneflow.expand(x: oneflow._oneflow_internal.BlobDesc, expand_size: Sequence[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator expand the input Blob to a larger size.

Passing -1 as the size for a dimension means not changing the size of that dimension.

Blob can be also expanded to a larger number of dimensions and the new ones will be appended at the front.

For the new dimensions, the size cannot be set to -1.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • expand_size (Sequence[int]) – The desired expanded size.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def expandJob(x: tp.Numpy.Placeholder(shape=(1, 3, 1, 2), dtype=flow.int32),
) -> tp.Numpy:
    return flow.expand(input=x,
                        expand_size=[1, 3, 2, 2])

x = np.array([[[[0, 1]],
               [[2, 3]],
               [[4, 5]]]]).astype(np.int32)

out = expandJob(x)
# out shape: [1, 3, 2, 2]
# [[[[0, 1],
#    [0, 1]],
#   [[2, 3],
#    [2, 3]],
#   [[4, 5],
#    [4, 5]]]]
oneflow.expand_dims(input: oneflow._oneflow_internal.BlobDesc, axis: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator inserts a dimention at the specified axis in the input Blob. The size of new dimension can only be 1, and the amount of element in return value is the same as Blob input.

Parameters
  • input (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • axis (int) – The specified dimension index.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def expand_dim_Job(x: tp.Numpy.Placeholder(shape=(1, 3, 3), dtype=flow.int32),
) -> tp.Numpy:
    return flow.expand_dims(input=x,
                            axis=2)


x = np.array([[[1, 1, 1],
            [1, 1, 1],
            [1, 1, 1]]]).astype(np.int32)
out = expand_dim_Job(x)

# out.shape (1, 3, 1, 3)
oneflow.find_or_create_module(module_name: str, create: Callable[[], None], reuse: bool = False)
oneflow.flatten(input: oneflow._oneflow_internal.BlobDesc, start_dim: int = 0, end_dim: int = -1, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Flattens a contiguous range of dims in a Blob.

Parameters
  • input – A Blob.

  • start_dim – The first dim to flatten.

  • end_dim – The last dim to flatten.

  • name – A name for the operation (optional).

Returns

A Blob, has the same type as input.

For example:

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

@flow.global_function()
def flatten_Job(input: tp.Numpy.Placeholder(shape=(4, 4, 3, 2), dtype=flow.float32)
) -> tp.Numpy:
    flatten_blob = flow.flatten(input, start_dim=1, end_dim=-1)
    return flatten_blob


input = np.zeros((4, 4, 3, 2)).astype(np.float32)
out = flatten_Job(input)

# out.shape (4, 24)
oneflow.function_config

alias of oneflow.python.framework.function_util.FunctionConfig

oneflow.gather(params: oneflow._oneflow_internal.BlobDesc, indices: oneflow._oneflow_internal.BlobDesc, validate_indices: Optional[oneflow._oneflow_internal.BlobDesc] = None, axis: Optional[int] = None, batch_dims: int = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator gathers slices from params axis according to indices.

Parameters
  • params – A Blob. The blob from which to gather values. Must be at least rank axis + 1.

  • indices – A Blob. Index blob. Must be in range [0, params.shape[axis]).

  • axis – A int. The axis in params to gather indices from. Defaults to the first dimension. Supports negative indexes.

  • batch_dims – An optional int. Defaults to 0.

  • name – A name for the operation (optional).

Returns

A blob. Has the same type as params.

For example:

Example 1:

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


@flow.global_function()
def gather_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
            indice: tp.Numpy.Placeholder(shape=(2, ), dtype=flow.int32)
) -> tp.Numpy:
    gather_blob = flow.gather(params=x,
                            indices=indice,
                            axis=1)
    return gather_blob


x = np.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]).astype(np.float32)
indice = np.array([0, 2]).astype(np.int32)
out = gather_Job(x, indice)

# out [[1. 3.]
#      [4. 6.]
#      [7. 9.]]

Example 2:

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


@flow.global_function()
def gather_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
            indice: tp.Numpy.Placeholder(shape=(2, ), dtype=flow.int32)
) -> tp.Numpy:
    gather_blob = flow.gather(params=x,
                            indices=indice,
                            axis=0)
    return gather_blob


x = np.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]).astype(np.float32)
indice = np.array([0, 2]).astype(np.int32)
out = gather_Job(x, indice)

# out [[1. 2. 3.]
#      [7. 8. 9.]]
oneflow.gather_nd(params: oneflow._oneflow_internal.BlobDesc, indices: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator is a high-dimensional extension of gather, indices is a K-dimensional tensor, which is regarded as a index of input Blob params.

Each element defines a slice of params:

\[output[(i_0,i_1,...,i_{K-2})] = param[indices(i_{0},i_{1},...,i_{K-2})]\]
Parameters
  • params (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • indices (oneflow._oneflow_internal.BlobDesc) – The slice indices.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

Example 1:

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


@flow.global_function()
def gather_nd_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
                indice: tp.Numpy.Placeholder(shape=(2, 1), dtype=flow.int32)
) -> tp.Numpy:
    gather_nd_blob = flow.gather_nd(params=x,
                                    indices=indice)
    return gather_nd_blob


x = np.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]).astype(np.float32)
indice = np.array([[0], [2]]).astype(np.int32)
out = gather_nd_Job(x, indice)

# out [[1. 2. 3.]
#      [7. 8. 9.]]

Example 2:

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


@flow.global_function()
def gather_nd_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
                indice: tp.Numpy.Placeholder(shape=(2, 2), dtype=flow.int32)
) -> tp.Numpy:
    gather_nd_blob = flow.gather_nd(params=x,
                                    indices=indice)
    return gather_nd_blob


x = np.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]).astype(np.float32)
indice = np.array([[0, 2], [2, 1]]).astype(np.int32)
out = gather_nd_Job(x, indice)

# out [3. 8.]

Example3:

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


@flow.global_function()
def gather_nd_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
                indice: tp.Numpy.Placeholder(shape=(3, 2), dtype=flow.int32)
) -> tp.Numpy:
    gather_nd_blob = flow.gather_nd(params=x,
                                    indices=indice)
    return gather_nd_blob


x = np.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]).astype(np.float32)
indice = np.array([[0, 1], [1, 0], [2, 2]]).astype(np.int32)
out = gather_nd_Job(x, indice)

# out [2. 4. 9.]
oneflow.gen_tensor_buffer(shape: Sequence[int], shape_list: Sequence[Sequence[int]], value_list: Sequence[float], data_type: Optional[oneflow._oneflow_internal.dtype] = oneflow.float32, dynamic_out: Optional[bool] = False, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator generates a tensor buffer blob.

Parameters
  • shape (Sequence[int]) – shape of output blob

  • shape_list (Sequence[Sequence[int]]) – shapes for tensor buffer in output blob

  • value_list (Sequence[float]) – values for tensor buffer in output blob

  • data_type (Optional[flow.dtype]) – data type for tensor buffer in output blob

  • dynamic_out (Optional[bool]) – if output is a dynamic blob

  • name (Optional[str]) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

BlobDesc

For example:

import oneflow as flow

@flow.global_function(function_config=func_config)
def GenTensorBufferJob():
    with flow.scope.placement("cpu", "0:0"):
        x = flow.gen_tensor_buffer([(2,)], [(2, 1), (1, 2)], [0.0, 1.0])
        y = flow.tensor_buffer_to_list_of_tensors(x, (100, 100), flow.float, True)
        return y

# y_0.shape (2, 1), y_1.shape (1. 2)
oneflow.get_all_variables() → Dict[str, oneflow._oneflow_internal.EagerConsistentBlob]

Get all variables of all jobs as a dict.

oneflow.get_variable(name: str, shape: Optional[Sequence[int]] = None, dtype: Optional[oneflow._oneflow_internal.dtype] = oneflow.float32, initializer: Optional[oneflow.core.job.initializer_conf_pb2.InitializerConf] = None, regularizer: Optional[oneflow.core.job.regularizer_conf_pb2.RegularizerConf] = None, trainable: Optional[bool] = None, model_name: Optional[str] = None, random_seed: Optional[int] = None, parallel_distribution: Union[Sequence[oneflow._oneflow_internal.distribute.Distribute], Sequence[str], str, None] = None, distribute: Optional[oneflow._oneflow_internal.distribute.Distribute] = None, reuse: bool = True) → oneflow._oneflow_internal.BlobDesc

Create a variable or retrieve an existing one.

Parameters
  • name – Name of this variable. One variable could be shared by multiple OneFlow functions. None by default

  • shape – Shape of the variable. None by default

  • dtype – Data type of the variable. None by default

  • initializer – A initializer object. For instance, a ones_initializer(). None by default

  • trainable – A bool to indicate if this variable is trainable. True by default

  • model_name – A string. ‘weight’ or ‘bias’. None by default

  • random_seed – Random seed for random initializers. None by default

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def variable_Job() -> None:
    init = flow.constant_initializer(1.25)
    variable = flow.get_variable(
        "variable-weight",
        shape=(1, 3, 2, 2),
        initializer=init,
        trainable=True
    )
    flow.watch(variable, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
variable_Job()

# out [[[[1.25 1.25]
#        [1.25 1.25]]

#       [[1.25 1.25]
#        [1.25 1.25]]

#       [[1.25 1.25]
#        [1.25 1.25]]]]

Example 2:

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


def conv2d(input, filters, kernel_size, strides, padding, name):
    input_shape = input.shape
    weight_initializer = flow.truncated_normal(0.1)
    weight_regularizer = flow.regularizers.l2(0.0005)
    weight_shape = (filters,
                    input_shape[1],
                    kernel_size[0],
                    kernel_size[1])

    weight = flow.get_variable(
        name + "-weight",
        shape=weight_shape,
        initializer=weight_initializer,
        regularizer=weight_regularizer,
    )
    return flow.nn.conv2d(input, weight, strides, padding, name=name)


@flow.global_function()
def conv2d_Job(x: tp.Numpy.Placeholder((1, 64, 32, 32))
) -> tp.Numpy:
    conv = conv2d(x,
                filters=128,
                kernel_size=[3, 3],
                strides=2,
                padding='SAME',
                name="ConvLayer")
    return conv


x = np.random.randn(1, 64, 32, 32).astype(np.float32)
out = conv2d_Job(x)

# out.shape (1, 128, 16, 16)
oneflow.global_function(type: str = 'predict', function_config: oneflow.python.framework.function_util.FunctionConfig = None) → Callable[[Callable], Callable]

Creates a callable OneFlow global function from a Python function.

For instance:

@oneflow.global_function(flow.FunctionConfig())
def train():
    # your model
Parameters

function_config (FunctionConfig, optional) – a FunctionConfig object. Defaults to FunctionConfig().

Returns

a callable which is called to execute the compiled function

Return type

Callable[[Callable], Callable]

oneflow.glorot_normal_initializer(data_format: str = '') → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a Xavier normal distribution.

It also can be called as oneflow.glorot_normal_initializer.

The equation is:

\[W\sim N(0, \sqrt{\frac{{2}}{{n_j+n_{j+1}}}})\]

\(N\) means normal distribution

\(n_j\) means the amount of Nth layer parameters

Parameters

data_format (str, optional) – The data format. Defaults to “”.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def xavier_normal_Job() -> None:
    init = flow.xavier_normal_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
xavier_normal_Job()

# out [[ 0.5908121  -0.10804518 -0.6148571 ]
#      [ 1.4007381  -0.08172473  0.36579943]
#      [-0.6461796  -0.15923311  0.33653972]]

Example 2:

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


@flow.global_function()
def conv2d_xavier_normal_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.xavier_normal_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_xavier_normal_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.glorot_uniform_initializer(data_format: str = '') → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a Xavier uniform distribution.

It also can be called as oneflow.glorot_uniform_initializer.

The equation is:

\[W\sim U(-\sqrt{\frac{{6}}{{n_j+n_{j+1}}}},\sqrt{\frac{{6}}{{n_j+n_{j+1}}}})\]

\(U\) means uniform distribution

\(n_j\) means the amount of Nth layer parameters

Parameters

data_format (str, optional) – The data format. Defaults to “”.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def xavier_uniform_Job() -> None:
    init = flow.xavier_uniform_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
xavier_uniform_Job()

# out [[-0.14424723 -0.9532095  -0.08723891]
#      [-0.8011227  -0.29729813 -0.26769108]
#      [ 0.9208976  -0.5971756  -0.15077025]]

Example 2:

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


@flow.global_function()
def conv2d_xavier_uniform_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.xavier_uniform_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_xavier_uniform_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.hierarchical_parallel_cast(input: oneflow._oneflow_internal.BlobDesc, parallel_distribution: Sequence[str], grad_mode: Optional[str] = None, grad_parallel_distribution: Sequence[str] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.identity(x: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator returns a Blob that has identical content and data type to input Blob.

Analogous to tf.identity

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def identity_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.int32),
) -> tp.Numpy:
    return flow.identity(x)

x = np.array([[1, 1, 1],
            [2, 2, 2],
            [3, 3, 3]]).astype(np.int32)
out = identity_Job(x)

# out [[1 1 1]
#      [2 2 2]
#      [3 3 3]]
oneflow.identity_n(inputs: Sequence[oneflow._oneflow_internal.BlobDesc], name: Optional[str] = None) → List[oneflow._oneflow_internal.BlobDesc]

This operator is similar to oneflow.identity. The difference is that the input and output of identity_n is List.

Parameters
  • inputs (Iterable[oneflow._oneflow_internal.BlobDesc]) – A List of input Blob.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A list of result Blob.

Return type

List[oneflow._oneflow_internal.BlobDesc]

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp
from typing import List


@flow.global_function()
def identity_Job(x: tp.Numpy.Placeholder(shape=(1, 3), dtype=flow.int32),
                y: tp.Numpy.Placeholder(shape=(1, 3), dtype=flow.int32),
                z: tp.Numpy.Placeholder(shape=(1, 3), dtype=flow.int32)
) -> List[tp.Numpy]:
    return flow.identity_n([x, y, z])


x = np.array([[1, 1, 1]]).astype(np.int32)
y = np.array([[2, 2, 2]]).astype(np.int32)
z = np.array([[3, 3, 3]]).astype(np.int32)
out = identity_Job(x, y, z)

# out[0] [[1, 1, 1]]
# out[1] [[2, 2, 2]]
# out[2] [[3, 3, 3]]
oneflow.image_batch_align(images: oneflow._oneflow_internal.BlobDesc, shape: Sequence[int], dtype: oneflow._oneflow_internal.dtype, alignment: int, dynamic_out: bool = True, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator aligns the shape for a batch of images.

The aligned shape is computed as:

\[ \begin{align}\begin{aligned}& shape_{width} = int(\frac{(shape_{width}+alignment-1)}{alignment})*alignment\\& shape_{height} = int(\frac{(shape_{height}+alignment-1)}{alignment})*alignment\end{aligned}\end{align} \]
Parameters
  • images (oneflow._oneflow_internal.BlobDesc) – The images.

  • shape (Sequence[int]) – The maximum static shape of input images.

  • dtype (flow.dtype) – The data type.

  • alignment (int) – The align factor.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import cv2
import numpy as np
import oneflow as flow
import oneflow.typing as tp


def _of_image_batch_align(images, input_shape, output_shape, alignment):
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_batch_align_job(
        images_def: tp.ListListNumpy.Placeholder(shape=input_shape, dtype=flow.float)
    ) -> tp.ListNumpy:
        # Convert to tensor buffer
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)
        image = flow.image_batch_align(
            images_buffer, shape=output_shape[1:], dtype=flow.float, alignment=alignment
        )
        return image

    image = image_batch_align_job([images])
    return image[0]


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    assert image_static_shape[0] == 1, str(image_static_shape)
    image_static_shape[0] = len(image_shapes)
    return image_static_shape

def _roundup(x, n):
    # compute the aligned shape
    return int((x + n - 1) / n) * n

if __name__ == "__main__":
    img = _read_images_by_cv(['./img/1.jpg', './img/2.jpg', './img/3.jpg'])
    img_shape = _get_images_static_shape(img) # In example is [3, 349, 367, 3]
    alignment = 16 # alignment factor
    aligned_image_shape = [
        img_shape[0],
        _roundup(img_shape[1], alignment),
        _roundup(img_shape[2], alignment),
        img_shape[3],
    ]
    image = _of_image_batch_align(img, tuple(img_shape), aligned_image_shape, alignment)
oneflow.image_decode(images_bytes_buffer: oneflow._oneflow_internal.BlobDesc, dtype: oneflow._oneflow_internal.dtype = oneflow.uint8, color_space: str = 'BGR', name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator decode the image.

Parameters
  • images_bytes_buffer (oneflow._oneflow_internal.BlobDesc) – The input Blob. Its type should be kTensorBuffer. More details please refer to the code example.

  • dtype (flow.dtype, optional) – The data type. Defaults to flow.uint8.

  • color_space (str, optional) – The color space. Defaults to “BGR”.

  • name (Optional[str], optional) – The name for the opreation. Defaults to None.

Returns

The decoded image list.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import oneflow as flow
import oneflow.typing as tp
import numpy as np
from PIL import Image


def _of_image_decode(images):
    image_files = [open(im, "rb") for im in images]
    images_bytes = [imf.read() for imf in image_files]
    static_shape = (len(images_bytes), max([len(bys) for bys in images_bytes]))
    for imf in image_files:
        imf.close()

    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_decode_job(
        images_def: tp.ListListNumpy.Placeholder(shape=static_shape, dtype=flow.int8)
    )->tp.ListListNumpy:
        # convert to tensor buffer
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)
        decoded_images_buffer = flow.image_decode(images_buffer)
        # Remember to set a shape
        # convert back to tensor list
        return flow.tensor_buffer_to_tensor_list(
            decoded_images_buffer, shape=(640, 640, 3), dtype=flow.uint8
        )

    images_np_arr = [
        np.frombuffer(bys, dtype=np.byte).reshape(1, -1) for bys in images_bytes
    ]
    decoded_images = image_decode_job([images_np_arr])
    return decoded_images[0]


if __name__ == "__main__":
    img = _of_image_decode(['./img/1.jpg'])
    print(img[0].shape) # Our image shape is (1, 349, 367, 3)
oneflow.image_flip(image: oneflow._oneflow_internal.BlobDesc, flip_code: Union[int, oneflow._oneflow_internal.BlobDesc], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator flips the images.

The flip code corresponds to the different flip mode:

0 (0x00): Non Flip

1 (0x01): Horizontal Flip

16 (0x10): Vertical Flip

17 (0x11): Both Horizontal and Vertical Flip

Parameters
  • image (oneflow._oneflow_internal.BlobDesc) – The input images.

  • flip_code (Union[int, oneflow._oneflow_internal.BlobDesc]) – The flip code.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import cv2
import numpy as np
import oneflow as flow
import oneflow.typing as tp


def _of_image_flip(images, image_shape, flip_code):
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_flip_job(
        images_def: tp.ListListNumpy.Placeholder(shape=image_shape, dtype=flow.float)
    ) -> tp.ListListNumpy:
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)
        flip_images = flow.image_flip(images_buffer, flip_code)
        return flow.tensor_buffer_to_tensor_list(
            flip_images, shape=image_shape[1:], dtype=flow.float
        )

    image_tensor = image_flip_job([images])
    return image_tensor[0]


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    assert image_static_shape[0] == 1, str(image_static_shape)
    image_static_shape[0] = len(image_shapes)
    return image_static_shape

if __name__ == "__main__":
    img = _read_images_by_cv(['./img/1.jpg', './img/2.jpg', './img/3.jpg'])
    img_shape = _get_images_static_shape(img) # In example is [3, 349, 367, 3]
    image = _of_image_flip(img,
                   tuple(img_shape),
                   flip_code=1)
oneflow.image_normalize(image: oneflow._oneflow_internal.BlobDesc, std: Sequence[float], mean: Sequence[float], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator normalizes the image.

Parameters
  • image (oneflow._oneflow_internal.BlobDesc) – The input image.

  • std (Sequence[float]) – The standard deviation of the images.

  • mean (Sequence[float]) – The mean value of the images.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import cv2
import numpy as np
import oneflow as flow
import oneflow.typing as tp


def _of_image_normalize(images, image_shape, std, mean):
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_normalize_job(
        images_def: tp.ListListNumpy.Placeholder(shape=image_shape, dtype=flow.float)
    ) -> tp.ListListNumpy:
        # Convert to tensor buffer
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)
        # Normalize the imagess
        norm_images = flow.image_normalize(images_buffer, std, mean)
        # Convert back to tensor list
        return flow.tensor_buffer_to_tensor_list(
            norm_images, shape=image_shape[1:], dtype=flow.float
        )

    image_tensor = image_normalize_job([images])
    return image_tensor[0]


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    assert image_static_shape[0] == 1, str(image_static_shape)
    image_static_shape[0] = len(image_shapes)
    return image_static_shape

if __name__ == "__main__":
    img = _read_images_by_cv(['./img/1.jpg', './img/2.jpg', './img/3.jpg'])
    img_shape = _get_images_static_shape(img) # In example is [3, 349, 367, 3]
    image = _of_image_normalize(img,
                                tuple(img_shape),
                                std=(102.9801, 115.9465, 122.7717),
                                mean=(1.0, 1.0, 1.0))
oneflow.image_random_crop(input_blob: oneflow._oneflow_internal.BlobDesc, num_attempts: int = 10, seed: Optional[int] = None, random_area: Sequence[float] = None, random_aspect_ratio: Sequence[float] = None, name: str = 'ImageRandomCrop') → oneflow._oneflow_internal.BlobDesc

This operator crops the input image randomly.

Parameters
  • input_blob (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • num_attempts (int, optional) – The maximum number of random cropping attempts. Defaults to 10.

  • seed (Optional[int], optional) – The random seed. Defaults to None.

  • random_area (Sequence[float], optional) – The random cropping area. Defaults to None.

  • random_aspect_ratio (Sequence[float], optional) – The random scaled ratio. Defaults to None.

  • name (str, optional) – The name for the operation. Defaults to “ImageRandomCrop”.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import oneflow as flow
import oneflow.typing as tp
import numpy as np
import cv2


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    assert image_static_shape[0] == 1, str(image_static_shape)
    image_static_shape[0] = len(image_shapes)
    return image_static_shape

def _of_image_random_crop(images, image_static_shape):
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_random_crop_job(images_def: tp.ListListNumpy.Placeholder(shape=image_static_shape, dtype=flow.float)
    ) -> tp.ListListNumpy:
        # The input Blob type should be "kTensorBuffer"
        # So we use oneflow.tensor_list_to_tensor_buffer to convert
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)
        # Do the random crop
        random_crop_buffer = flow.image.random_crop(
            images_buffer,
            random_area=[0.15, 0.80],
            random_aspect_ratio=[0.75, 1.55],
        )
        # We convert back to "tensorlist" type
        random_crop_images = flow.tensor_buffer_to_tensor_list(
            random_crop_buffer,
            shape=(image_static_shape[1], image_static_shape[2], image_static_shape[-1]),
            dtype=flow.float,
        )
        return random_crop_images

    random_crop_images = image_random_crop_job([images])

    return random_crop_images

if __name__ == "__main__":
    img = _read_images_by_cv(['./img/1.jpg'])
    img_shape = _get_images_static_shape(img) # In example is (1, 234, 346, 3)
    random_crop_images = _of_image_random_crop(img, tuple(img_shape))
    # random_crop_images.shape is (234, 346, 3)
oneflow.image_resize(image: oneflow._oneflow_internal.BlobDesc, target_size: Union[int, Sequence[int]] = None, min_size: Optional[int] = None, max_size: Optional[int] = None, keep_aspect_ratio: bool = False, resize_side: str = 'shorter', channels: int = 3, dtype: Optional[oneflow._oneflow_internal.dtype] = None, interpolation_type: str = 'auto', name: Optional[str] = None, color_space: Optional[str] = None, interp_type: Optional[str] = None, resize_shorter: int = 0, resize_x: int = 0, resize_y: int = 0) → Union[oneflow._oneflow_internal.BlobDesc, Sequence[oneflow._oneflow_internal.BlobDesc]]

Resize images to target size.

Parameters
  • image – A Tensor consists of images to be resized.

  • target_size – A list or tuple when keep_aspect_ratio is false or an int when keep_aspect_ratio is true. When keep_aspect_ratio is false, target_size has a form of (target_width, target_height) that image will resize to. When keep_aspect_ratio is true, the longer side or shorter side of the image will be resized to target size.

  • min_size – An int, optional. Only works when keep_aspect_ratio is true and resize_side is “longer”. If min_size is not None, the shorter side must be greater than or equal to min_size. Default is None.

  • max_size – An int, optional. Only works when keep_aspect_ratio is true and resize_side is “shorter”. If max_size is not None, the longer side must be less than or equal to max_size. Default is None.

  • keep_aspect_ratio – A bool. If is false, indicate that image will be resized to fixed width and height, otherwise image will be resized keeping aspect ratio.

  • resize_side – A str of “longer” or “shorter”. Only works when keep_aspect_ratio is True. If resize_side is “longer”, the longer side of image will be resized to target_size. If resize_side is “shorter”, the shorter side of image will be resized to target_size.

  • channels – An int. how many channels an image has

  • dtypeoneflow.dtype. Indicate output resized image data type.

  • interpolation_type – A str of “auto”, “bilinear”, “nearest_neighbor”, “bicubic” or “area”. Indicate interpolation method used to resize image.

  • name – A str, optional. Name for the operation.

  • color_space – Deprecated, a str of “RGB”, “BGR” or “GRAY”. Please use channels instead.

  • interp_type – Deprecated, s str of “Linear”, “Cubic” or “NN”. Please use interpolation_type instead.

  • resize_shorter – Deprecated, a int. Indicate target size that the shorter side of image will resize to. Please use target_size and resize_side instead.

  • resize_x – Deprecated, a int. Indicate the target size that the width of image will resize to. Please use target_size instead.

  • resize_y – Deprecated, a int. Indicate the target size that the height of image will resize to. Please use target_size instead.

Returns

Tuple of resized images Blob, width and height scales Blob and new width and height Blob (new width and height Blob will be None when keep_aspect_ratio is false). If deprecated params are used, a single resized images Blob will be returned.

For example:

import oneflow as flow
import oneflow.typing as tp
from typing import Tuple


@flow.global_function(type="predict")
def ofrecord_reader_job() -> Tuple[tp.Numpy, tp.Numpy]:
    batch_size = 16
    color_space = "RGB"
    # our ofrecord file path is "./dataset/part-0"
    ofrecord = flow.data.ofrecord_reader(
        "./imgdataset",
        batch_size=batch_size,
        data_part_num=1,
        part_name_suffix_length=-1,
        part_name_prefix='part-',
        random_shuffle=True,
        shuffle_after_epoch=True,
    )
    image = flow.data.OFRecordImageDecoderRandomCrop(
            ofrecord, "encoded", color_space=color_space
        )
    res_image, scale, new_size = flow.image.Resize(
            image, target_size=(224, 224)
        )
    label = flow.data.OFRecordRawDecoder(
        ofrecord, "class/label", shape=(1, ), dtype=flow.int32
    )

    return res_image, label

if __name__ == "__main__":
    images, labels = ofrecord_reader_job()
    # image.shape (16, 224, 224, 3)
oneflow.image_target_resize(images: oneflow._oneflow_internal.BlobDesc, target_size: int, min_size: Optional[int] = None, max_size: Optional[int] = None, resize_side: str = 'shorter', interpolation_type: str = 'auto', name: Optional[str] = None) → Sequence[oneflow._oneflow_internal.BlobDesc]

This operator resizes image to target size.

Parameters
  • images (oneflow._oneflow_internal.BlobDesc) – The input Blob. Its type should be kTensorBuffer. More details please refer to the code example.

  • target_size (int) – An int, the target size.

  • min_size (Optional[int], optional) – If min_size is not None, the shorter side must be greater than or equal to min_size. Default is None. Defaults to None.

  • max_size (Optional[int], optional) – If max_size is not None, the longer side must be less than or equal to max_size. Defaults to None.

  • resize_side (str, optional) – A str of “longer” or “shorter”. Only works when keep_aspect_ratio is True. If resize_side is “longer”, the longer side of image will be resized to target_size. If resize_side is “shorter”, the shorter side of image will be resized to target_size. Defaults to “shorter”.

  • interpolation_type (str, optional) – A str of “auto”, “bilinear”, “nearest_neighbor”, “bicubic” or “area”. Indicate interpolation method used to resize image. Defaults to “auto”.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Sequence includes the result Blob.

Return type

Sequence[oneflow._oneflow_internal.BlobDesc]

For example:

import oneflow as flow
import oneflow.typing as tp
from typing import Tuple
import numpy as np
import cv2


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    assert image_static_shape[0] == 1, str(image_static_shape)
    image_static_shape[0] = len(image_shapes)
    return image_static_shape

def _of_image_target_resize(images, image_static_shape, target_size, max_size):
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def image_target_resize_job(images_def: tp.ListListNumpy.Placeholder(shape=image_static_shape, dtype=flow.float)
    ) -> Tuple[tp.ListListNumpy, tp.ListNumpy, tp.ListNumpy]:
        # The input Blob type should be "kTensorBuffer"
        # So we use oneflow.tensor_list_to_tensor_buffer to convert
        images_buffer = flow.tensor_list_to_tensor_buffer(images_def)

        resized_images_buffer, size, scale = flow.image_target_resize(
            images_buffer,
            target_size=target_size,
            max_size=max_size,
            resize_side="shorter",
        )
        # We convert back to "tensorlist" type
        resized_images = flow.tensor_buffer_to_tensor_list(
            resized_images_buffer,
            shape=(target_size, max_size, image_static_shape[-1]),
            dtype=flow.float,
        )
        return resized_images, size, scale

    resized_images, size, scale = image_target_resize_job([images])
    resized_image = resized_images[0]
    size = size[0]
    scale = scale[0]

    return resized_images, size, scale

if __name__ == "__main__":
    img = _read_images_by_cv(['./img/1.jpg'])
    img_shape = _get_images_static_shape(img) # In example is [1, 349, 367, 3]
    target_size = 256
    max_size = 512
    resized_images, size, scale = _of_image_target_resize(img, tuple(img_shape), target_size, max_size)
    # Here the shorter side is "349", we resize it to target_size(256)
    # The scale is 256 / 349 = 0.73
    # The longer side will be resized to 367 * scale = 269
    # get the first element from the resized_images (its type is `list.list`)
    print(resized_images[0][0].shape) # (1, 256, 269, 3)
oneflow.in_top_k(targets: oneflow._oneflow_internal.BlobDesc, predictions: oneflow._oneflow_internal.BlobDesc, k: Optional[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Says whether the targets are in the top K predictions.

Parameters
  • targets (oneflow._oneflow_internal.BlobDesc) – A Blob of type int32 or int64.

  • predictions (oneflow._oneflow_internal.BlobDesc) – A Blob of type float32.

  • k (Optional[int], optional) – Number of top elements to look at for computing precision.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob of type bool. Computed Precision at k as a bool Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def intopk_Job(
    targets: tp.Numpy.Placeholder((2,), dtype=flow.int32),
    predictions: tp.Numpy.Placeholder((2, 4), dtype=flow.float32),
) -> tp.Numpy:
    return flow.math.in_top_k(targets, predictions, 1)

targets = np.array([3, 1], dtype=np.int32)
predictions = np.array([[0.0, 1.0, 2.0, 3.0], [3.0, 2.0, 1.0, 0.0],], dtype=np.float32)
out = intopk_Job(targets, predictions)

# out [1 0]
oneflow.inter_job_reuse_mem_strategy(strategy_str: str, job_set: Optional[oneflow.core.job.job_set_pb2.JobSet] = None, **kwargs: _VT) → None

Set memory sharing strategy for job set.

Parameters
  • strategy_str – An optional string from: mem_sharing_priority, parallelism_priority

  • custom_parallelism. (or) –

  • job_set – A JobSet object. If None, set default job set.

oneflow.is_deprecated(func_or_class)
oneflow.kaiming_initializer(shape: Sequence[int], distribution: str = 'random_normal', mode: str = 'fan_in', nonlinearity: str = 'leaky_relu', negative_slope: float = 0.0, data_format: str = 'NCHW') → None

Initialize weight according to the method described in Delving deep into rectifiers: Surpassing human-level performance on ImageNet classification - He, K. et al. (2015), using a normal or uniform distribution.

When distribution is “random_normal”

The equation is:

\[W \sim N(0, \sqrt{\frac{{2}}{{n}}})\]

When distribution is “random_uniform”

The equation is:

\[W \sim U(-\sqrt{\frac{{6}}{{n}}}, \sqrt{\frac{{6}}{{n}}})\]

If mode is “fan_in”, the “n” is the number of input units in the weight Blob.

If mode is “fan_out”, the “n” is the number of output units in the weight Blob.

if mode is “fan_avg”, the “n” is the average of the number of input and output units in the weight Blob

Parameters
  • shape (Sequence[int]) – Blob shape.

  • distribution (str, optional) – ‘random_normal’ or ‘random_uniform’. Defaults to “random_normal”.

  • mode (str, optional) – ‘fan_in’, ‘fan_out’ or ‘fan_avg’. Defaults to “fan_in”.

  • nonlinearity (str, optional) – None, ‘tanh’, ‘sigmoid’, ‘relu’ or ‘leaky_relu’. Defaults to “leaky_relu”.

  • negative_slope (float, optional) – The negative slope of leaky_relu. Defaults to 0.0.

  • data_format (str, optional) – ‘NCHW’, ‘NHWC’. Defaults to “NCHW”.

Raises

NotImplementedError – Only support normal and uniform distribution

Returns

flow.random_normal_initializer or flow.random_uniform_initializer

Return type

[type]

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def kaiming_Job() -> None:
    init = flow.kaiming_initializer(shape=(3, 3),
                                    mode="fan_avg",
                                    nonlinearity="relu")
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
kaiming_Job()

# out [[ 0.54521346  0.32585594  1.3474437 ]
#      [ 0.30729076 -0.19158769  0.2709008 ]
#      [-0.95830524 -0.05093324  0.28178614]]

Example 2:

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


@flow.global_function()
def conv2d_kaiming_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.kaiming_initializer(shape=(1, 256, 32, 32))
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_kaiming_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.load(path: str) → Union[Dict[str, oneflow.python.framework.check_point_v2.FileBackendVariableBlob], oneflow.python.framework.check_point_v2.FileBackendVariableBlob]
oneflow.load_variables(value_dict: Dict[str, Union[oneflow._oneflow_internal.EagerBlobTrait, oneflow.python.framework.check_point_v2.FileBackendVariableBlob, numpy.ndarray, oneflow.Tensor]], ignore_mismatch: bool = True)

Load value in value_dict into oneflow variables. For example, if value_dict is {‘x’, np.ones(x_shape)}, the value of variable “x” will all ones. If ignore_mismatch is False, an exception will be raised when there is a name in value_dict not belonging to any variable.

oneflow.manual_seed(seed)
oneflow.masked_fill(x: oneflow._oneflow_internal.BlobDesc, mask: oneflow._oneflow_internal.BlobDesc, value: Union[float, int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Fill a blob with a given value according to the given mask.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – Input Blob.

  • mask (oneflow._oneflow_internal.BlobDesc) – Composed with 0 and 1, the input blob ‘x’ will be filled with the given value where the mask is 1.

  • value (Union[int, int]) – The value to use for filling the input blob.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Attention

x and mask must be broadcastable to each other. mask must be int type (int8/int32/int64).

Returns

The value-filled Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def masked_fill_Job(x: tp.Numpy.Placeholder((4, ), mask: tp.Numpy.Placeholder((4, ),
                    dtype = flow.int8))->tp.Numpy:
    return flow.masked_fill(x, mask, value=5)

x = np.array([1, 2, 3, 4], dtype=np.float32)
mask = np.array([1, 0, 0, 1], dtype=np.int8)

out = masked_fill_Job(x, mask)

# output [5 2 3 5]
oneflow.matmul(a: oneflow._oneflow_internal.BlobDesc, b: oneflow._oneflow_internal.BlobDesc, transpose_a: bool = False, transpose_b: bool = False, alpha: float = 1.0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator applies matrix multiplication to two Blobs.

Parameters
  • a (oneflow._oneflow_internal.BlobDesc) – A Blob

  • b (oneflow._oneflow_internal.BlobDesc) – A Blob

  • transpose_a (bool, optional) – Whether to transpose A Blob. Defaults to False.

  • transpose_b (bool, optional) – Whether to transpose B Blob. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def matmul_Job(A: tp.Numpy.Placeholder((3, 3)),
            B: tp.Numpy.Placeholder((3, 3))
) -> tp.Numpy:
    return flow.linalg.matmul(A, B)


A = np.array([[1, 0, 0],
            [0, 1, 1],
            [0, 0, 1]]).astype(np.float32)
B = np.array([[3, 4, 5],
            [6, 7, 8],
            [9, 10, 11]]).astype(np.float32)
out = matmul_Job(A, B)

# output [[ 3.  4.  5.]
#         [15. 17. 19.]
#         [ 9. 10. 11.]]
oneflow.multi_count_not_finite(x: Optional[Sequence[oneflow._oneflow_internal.BlobDesc]] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
class oneflow.no_grad
oneflow.nonzero(a: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator finds the indices of input Blob condition elements that are non-zero.

Parameters
  • a (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

oneflow.object_bbox_flip(bbox: oneflow._oneflow_internal.BlobDesc, image_size: oneflow._oneflow_internal.BlobDesc, flip_code: Union[int, oneflow._oneflow_internal.BlobDesc], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator flips the object bounding box.

The flip code corresponds to the different flip mode:

0 (0x00): Non Flip

1 (0x01): Horizontal Flip

16 (0x10): Vertical Flip

17 (0x11): Both Horizontal and Vertical Flip

Parameters
  • bbox (oneflow._oneflow_internal.BlobDesc) – The bounding box.

  • image_size (oneflow._oneflow_internal.BlobDesc) – The size of input image.

  • flip_code (Union[int, oneflow._oneflow_internal.BlobDesc]) – The flip code.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


def _of_object_bbox_flip(bbox_list, image_size, flip_code):
    bbox_shape = _get_bbox_static_shape(bbox_list)
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def object_bbox_flip_job(
        bbox_def: tp.ListListNumpy.Placeholder(
            shape=tuple(bbox_shape), dtype=flow.float
        ),
        image_size_def: tp.ListNumpy.Placeholder(
            shape=image_size.shape, dtype=flow.int32
        ),
    ) -> tp.ListListNumpy:
        bbox_buffer = flow.tensor_list_to_tensor_buffer(bbox_def)
        flip_bbox = flow.object_bbox_flip(bbox_buffer, image_size_def, flip_code)
        return flow.tensor_buffer_to_tensor_list(
            flip_bbox, shape=bbox_shape[1:], dtype=flow.float
        )

    input_bbox_list = [np.expand_dims(bbox, axis=0) for bbox in bbox_list]
    bbox_tensor = object_bbox_flip_job([input_bbox_list], [image_size])
    return bbox_tensor[0]


def _get_bbox_static_shape(bbox_list):
    bbox_shapes = [bbox.shape for bbox in bbox_list]
    bbox_static_shape = np.amax(bbox_shapes, axis=0)
    assert isinstance(
        bbox_static_shape, np.ndarray
    ), "bbox_shapes: {}, bbox_static_shape: {}".format(
        str(bbox_shapes), str(bbox_static_shape)
    )
    bbox_static_shape = bbox_static_shape.tolist()
    bbox_static_shape.insert(0, len(bbox_list))
    return bbox_static_shape

if __name__ == "__main__":
    bbox = np.array([[[20.0, 40.0, 80.0, 160.0],
                    [30.0, 50.0, 70.0, 100.0]]]).astype(np.single) # [x1, y1, x2, y2]
    image_size = np.array([[480, 620]]).astype(np.int32)
    bbox_flip =  _of_object_bbox_flip(bbox,
                                    image_size,
                                    flip_code=1) # Horizontal Flip
    print(bbox_flip[0][0])

    # [[399.  40. 459. 160.]
    #  [409.  50. 449. 100.]]
oneflow.object_bbox_scale(bbox: oneflow._oneflow_internal.BlobDesc, scale: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator scales the input image and the corresponding bounding box. It returns the scaled bounding box.

Parameters
  • bbox (oneflow._oneflow_internal.BlobDesc) – The bounding box.

  • scale (oneflow._oneflow_internal.BlobDesc) – The scale factor.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import numpy as np
import oneflow as flow
import oneflow.typing as tp
import cv2
from typing import Tuple


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return images


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    image_static_shape.insert(0, len(image_shapes))
    return image_static_shape


def _get_bbox_static_shape(bbox_list):
    bbox_shapes = [bbox.shape for bbox in bbox_list]
    bbox_static_shape = np.amax(bbox_shapes, axis=0)
    assert isinstance(
        bbox_static_shape, np.ndarray
    ), "bbox_shapes: {}, bbox_static_shape: {}".format(
        str(bbox_shapes), str(bbox_static_shape)
    )
    bbox_static_shape = bbox_static_shape.tolist()
    bbox_static_shape.insert(0, len(bbox_list))
    return bbox_static_shape


def _of_target_resize_bbox_scale(images, bbox_list, target_size, max_size):
    image_shape = _get_images_static_shape(images)
    bbox_shape = _get_bbox_static_shape(bbox_list)

    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def target_resize_bbox_scale_job(
        image_def: tp.ListListNumpy.Placeholder(
            shape=tuple(image_shape), dtype=flow.float
        ),
        bbox_def: tp.ListListNumpy.Placeholder(
            shape=tuple(bbox_shape), dtype=flow.float
        ),
    ) -> Tuple[tp.ListListNumpy, tp.ListNumpy]:
        images_buffer = flow.tensor_list_to_tensor_buffer(image_def)
        resized_images_buffer, new_size, scale = flow.image_target_resize(
            images_buffer, target_size=target_size, max_size=max_size
        )
        bbox_buffer = flow.tensor_list_to_tensor_buffer(bbox_def)
        scaled_bbox = flow.object_bbox_scale(bbox_buffer, scale)
        scaled_bbox_list = flow.tensor_buffer_to_tensor_list(
            scaled_bbox, shape=bbox_shape[1:], dtype=flow.float
        )
        return scaled_bbox_list, new_size

    input_image_list = [np.expand_dims(image, axis=0) for image in images]
    input_bbox_list = [np.expand_dims(bbox, axis=0) for bbox in bbox_list]
    output_bbox_list, output_image_size = target_resize_bbox_scale_job(
        [input_image_list], [input_bbox_list]
    )
    return output_bbox_list[0], output_image_size[0]


if __name__ == "__main__":
    images = _read_images_by_cv(['./img/1.jpg', './img/2.jpg'])
    bbox = np.array([[[20.0, 40.0, 80.0, 160.0],
                    [30.0, 50.0, 70.0, 100.0]],
                    [[26.0, 40.0, 86.0, 160.0],
                    [36.0, 56.0, 76.0, 106.0]]]).astype(np.single) # [x1, y1, x2, y2]
    bbox, size = _of_target_resize_bbox_scale(images, bbox, 280, 350)
    print(bbox[0])
    print(bbox[1])

    # [[[ 16.0218    32.09169   64.0872   128.36676 ]
    #   [ 24.032698  40.114613  56.076298  80.229225]]]

    # [[[ 24.186047  37.170418  80.       148.68167 ]
    #   [ 33.488373  52.038586  70.69768   98.5016  ]]]
oneflow.object_segmentation_polygon_flip(poly: oneflow._oneflow_internal.BlobDesc, image_size: oneflow._oneflow_internal.BlobDesc, flip_code: Union[int, oneflow._oneflow_internal.BlobDesc], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator flips the segmentation points in image.

The flip code corresponds to the different flip mode:

0 (0x00): Non Flip

1 (0x01): Horizontal Flip

16 (0x10): Vertical Flip

17 (0x11): Both Horizontal and Vertical Flip

Parameters
  • poly (oneflow._oneflow_internal.BlobDesc) – The poly segmentation points.

  • image_size (oneflow._oneflow_internal.BlobDesc) – The image size.

  • flip_code (Union[int, oneflow._oneflow_internal.BlobDesc]) – The filp code.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import numpy as np
import oneflow as flow
import oneflow.typing as tp
import cv2


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return [np.expand_dims(image, axis=0) for image in images]


def _of_object_segm_poly_flip(poly_list, image_size, flip_code):
    poly_shape = _get_segm_poly_static_shape(poly_list)

    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def object_segm_poly_flip_job(
        poly_def: tp.ListListNumpy.Placeholder(
            shape=tuple(poly_shape), dtype=flow.float
        ),
        image_size_def: tp.ListNumpy.Placeholder(
            shape=image_size.shape, dtype=flow.int32
        ),
    ) -> tp.ListListNumpy:
        poly_buffer = flow.tensor_list_to_tensor_buffer(poly_def)
        flip_poly = flow.object_segmentation_polygon_flip(
            poly_buffer, image_size_def, flip_code
        )
        return flow.tensor_buffer_to_tensor_list(
            flip_poly, shape=poly_shape[1:], dtype=flow.float
        )

    input_poly_list = [np.expand_dims(poly, axis=0) for poly in poly_list]
    poly_tensor = object_segm_poly_flip_job([input_poly_list], [image_size])
    return poly_tensor[0]


def _get_segm_poly_static_shape(poly_list):
    poly_shapes = [poly.shape for poly in poly_list]
    poly_static_shape = np.amax(poly_shapes, axis=0)
    assert isinstance(
        poly_static_shape, np.ndarray
    ), "poly_shapes: {}, poly_static_shape: {}".format(
        str(poly_shapes), str(poly_static_shape)
    )
    poly_static_shape = poly_static_shape.tolist()
    poly_static_shape.insert(0, len(poly_list))
    return poly_static_shape

if __name__ == "__main__":
    segm_poly_list = []
    segmentations = [[[20.0, 40.0], [80.0, 160.0], [100.0, 210.0]], # Image 1 segmentation point
                    [[25.0, 45.0], [85.0, 165.0], [105.0, 215.0]]] # Image 2 segmentation point
    for segmentation in segmentations:
        polygon = []
        for seg in segmentation:
            polygon.extend(seg)
        poly_array = np.array(polygon, dtype=np.single).reshape(-1, 2) # Reshape it
        segm_poly_list.append(poly_array)

    image_size = np.array([[480, 620], # Image 1 size
                        [640, 640]]).astype(np.int32) # Image 2 size
    of_segm_poly_list = _of_object_segm_poly_flip(
        segm_poly_list, image_size, flip_code=1
    ) # Horizontal Flip
    print(of_segm_poly_list[0])
    print(of_segm_poly_list[1])

    # of_segm_poly_list[0]
    # [[[460.  40.]
    #   [400. 160.]
    #   [380. 210.]]]

    # of_segm_poly_list[1]
    # [[[615.  45.]
    #   [555. 165.]
    #   [535. 215.]]]
oneflow.object_segmentation_polygon_scale(poly: oneflow._oneflow_internal.BlobDesc, scale: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator scales the segmentation points in the images.

Parameters
  • poly (oneflow._oneflow_internal.BlobDesc) – The poly segmentation points.

  • scale (oneflow._oneflow_internal.BlobDesc) – The image scale.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import numpy as np
import oneflow as flow
import oneflow.typing as tp
import cv2
from typing import Tuple


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return images


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    image_static_shape.insert(0, len(image_shapes))
    return image_static_shape


def _get_segm_poly_static_shape(poly_list):
    poly_shapes = [poly.shape for poly in poly_list]
    poly_static_shape = np.amax(poly_shapes, axis=0)
    assert isinstance(
        poly_static_shape, np.ndarray
    ), "poly_shapes: {}, poly_static_shape: {}".format(
        str(poly_shapes), str(poly_static_shape)
    )
    poly_static_shape = poly_static_shape.tolist()
    poly_static_shape.insert(0, len(poly_list))
    return poly_static_shape


def _get_bbox_static_shape(bbox_list):
    bbox_shapes = [bbox.shape for bbox in bbox_list]
    bbox_static_shape = np.amax(bbox_shapes, axis=0)
    assert isinstance(
        bbox_static_shape, np.ndarray
    ), "bbox_shapes: {}, bbox_static_shape: {}".format(
        str(bbox_shapes), str(bbox_static_shape)
    )
    bbox_static_shape = bbox_static_shape.tolist()
    bbox_static_shape.insert(0, len(bbox_list))
    return bbox_static_shape


def _of_object_segm_poly_scale(images, poly_list, target_size, max_size):
    image_shape = _get_images_static_shape(images)
    print(image_shape)
    poly_shape = _get_segm_poly_static_shape(poly_list)
    print("Poly shape is ", poly_shape)
    func_config = flow.FunctionConfig()
    func_config.default_data_type(flow.float)
    func_config.default_logical_view(flow.scope.mirrored_view())

    @flow.global_function(function_config=func_config)
    def object_segm_poly_scale_job(
        image_def: tp.ListListNumpy.Placeholder(
            shape=tuple(image_shape), dtype=flow.float
        ),
        poly_def: tp.ListListNumpy.Placeholder(
            shape=tuple(poly_shape), dtype=flow.float
        ),
    ) -> Tuple[tp.ListListNumpy, tp.ListNumpy]:
        images_buffer = flow.tensor_list_to_tensor_buffer(image_def)
        resized_images_buffer, new_size, scale = flow.image_target_resize(
            images_buffer, target_size=target_size, max_size=max_size
        )
        poly_buffer = flow.tensor_list_to_tensor_buffer(poly_def)
        scaled_poly = flow.object_segmentation_polygon_scale(poly_buffer, scale)
        scaled_poly_list = flow.tensor_buffer_to_tensor_list(
            scaled_poly, shape=poly_shape[1:], dtype=flow.float
        )
        return scaled_poly_list, new_size

    input_image_list = [np.expand_dims(image, axis=0) for image in images]
    input_poly_list = [np.expand_dims(poly, axis=0) for poly in poly_list]

    output_poly_list, output_image_size = object_segm_poly_scale_job(
        [input_image_list], [input_poly_list]
    )

    return output_poly_list[0], output_image_size

if __name__ == "__main__":
    images = _read_images_by_cv(['./img/1.jpg', './img/2.jpg'])
    segm_poly_list = []
    segmentations = [[[20.0, 40.0], [80.0, 160.0], [100.0, 210.0]], # Image 1 segmentation point
                    [[25.0, 45.0], [85.0, 165.0], [105.0, 215.0]]] # Image 2 segmentation point

    for segmentation in segmentations:
        polygon = []
        for seg in segmentation:
            polygon.extend(seg)
        poly_array = np.array(polygon, dtype=np.single).reshape(-1, 2) # Reshape it
        segm_poly_list.append(poly_array)

    bbox, size = _of_object_segm_poly_scale(images, segm_poly_list, 280, 350)
oneflow.object_segmentation_polygon_to_mask(poly: oneflow._oneflow_internal.BlobDesc, poly_index: oneflow._oneflow_internal.BlobDesc, image_size: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator converts the poly segment points to the segment mask array.

Parameters
  • poly (oneflow._oneflow_internal.BlobDesc) – The poly segment points.

  • poly_index (oneflow._oneflow_internal.BlobDesc) – The poly segment index.

  • image_size (oneflow._oneflow_internal.BlobDesc) – The input image size.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

import numpy as np
import oneflow as flow
import oneflow.typing as tp
import cv2
from typing import Tuple


def _read_images_by_cv(image_files):
    images = [cv2.imread(image_file).astype(np.single) for image_file in image_files]
    return images


def _get_images_static_shape(images):
    image_shapes = [image.shape for image in images]
    image_static_shape = np.amax(image_shapes, axis=0)
    assert isinstance(
        image_static_shape, np.ndarray
    ), "image_shapes: {}, image_static_shape: {}".format(
        str(image_shapes), str(image_static_shape)
    )
    image_static_shape = image_static_shape.tolist()
    image_static_shape.insert(0, len(image_shapes))
    return image_static_shape


def _get_segm_poly_static_shape(poly_list, poly_index_list):
    assert len(poly_list) == len(poly_index_list)
    num_images = len(poly_list)
    max_poly_elems = 0
    for poly, poly_index in zip(poly_list, poly_index_list):
        assert len(poly.shape) == 2
        assert len(poly_index.shape) == 2, str(poly_index.shape)
        assert poly.shape[0] == poly_index.shape[0]
        assert poly.shape[1] == 2
        assert poly_index.shape[1] == 3
        max_poly_elems = max(max_poly_elems, poly.shape[0])
    return [num_images, max_poly_elems, 2], [num_images, max_poly_elems, 3]

def _segm_poly_to_tensor(img_segm_poly_list):
    poly_array_list = []
    poly_index_array_list = []
    for img_idx, segm_poly_list in enumerate(img_segm_poly_list):
        img_poly_elem_list = []
        img_poly_index_list = []

        for obj_idx, poly_list in enumerate(segm_poly_list):
            for poly_idx, poly in enumerate(poly_list):
                img_poly_elem_list.extend(poly)
                for pt_idx, pt in enumerate(poly):
                    if pt_idx % 2 == 0:
                        img_poly_index_list.append([pt_idx / 2, poly_idx, obj_idx])

        img_poly_array = np.array(img_poly_elem_list, dtype=np.single).reshape(-1, 2)
        assert img_poly_array.size > 0, segm_poly_list
        poly_array_list.append(img_poly_array)

        img_poly_index_array = np.array(img_poly_index_list, dtype=np.int32)
        assert img_poly_index_array.size > 0, segm_poly_list
        poly_index_array_list.append(img_poly_index_array)

    return poly_array_list, poly_index_array_list


def _of_poly_to_mask_pipline(
    images, poly_list, poly_index_list, num_segms_list, target_size, max_size
):
    print(len(images))
    print(len(poly_list))

    assert len(images) == len(poly_list)
    assert len(poly_list) == len(poly_index_list)
    image_shape = _get_images_static_shape(images)
    poly_shape, poly_index_shape = _get_segm_poly_static_shape(
        poly_list, poly_index_list
    )
    max_num_segms = max(num_segms_list)

    func_config = flow.FunctionConfig()
    func_config.default_logical_view(flow.scope.mirrored_view())
    func_config.default_data_type(flow.float)


    @flow.global_function(function_config=func_config)
    def poly_to_mask_job(
        image_def: tp.ListListNumpy.Placeholder(
            shape=tuple(image_shape), dtype=flow.float
        ),
        poly_def: tp.ListListNumpy.Placeholder(
            shape=tuple(poly_shape), dtype=flow.float
        ),
        poly_index_def: tp.ListListNumpy.Placeholder(
            shape=tuple(poly_index_shape), dtype=flow.int32
        ),
    ) -> Tuple[tp.ListListNumpy, tp.ListListNumpy]:
        images_buffer = flow.tensor_list_to_tensor_buffer(image_def)
        resized_images_buffer, new_size, scale = flow.image_target_resize(
            images_buffer, target_size=target_size, max_size=max_size
        )
        poly_buffer = flow.tensor_list_to_tensor_buffer(poly_def)
        poly_index_buffer = flow.tensor_list_to_tensor_buffer(poly_index_def)
        scaled_poly_buffer = flow.object_segmentation_polygon_scale(poly_buffer, scale)
        mask_buffer = flow.object_segmentation_polygon_to_mask(
            scaled_poly_buffer, poly_index_buffer, new_size
        )
        mask_list = flow.tensor_buffer_to_tensor_list(
            mask_buffer, shape=(max_num_segms, target_size, max_size), dtype=flow.int8
        )
        scaled_poly_list = flow.tensor_buffer_to_tensor_list(
            scaled_poly_buffer, shape=poly_shape[1:], dtype=flow.float
        )
        return mask_list, scaled_poly_list

    input_image_list = [np.expand_dims(image, axis=0) for image in images]
    input_poly_list = [np.expand_dims(poly, axis=0) for poly in poly_list]
    input_poly_index_list = [
        np.expand_dims(poly_index, axis=0) for poly_index in poly_index_list
    ]

    output_mask_list, output_poly_list = poly_to_mask_job(
        [input_image_list], [input_poly_list], [input_poly_index_list]
    )

    return output_mask_list[0], output_poly_list[0]

if __name__ == "__main__":
    images = _read_images_by_cv(['./img/1.jpg', './img/2.jpg'])
    segm_poly_list = []

    segmentations = [[[20.0, 40.0, 80.0, 160.0, 100.0, 210.0, 120.0, 215.0]], # Image 1 segmentation point
                    [[24.0, 42.0, 86.0, 168.0, 103.0, 223.0, 125.0, 235.0]]] # Image 2 segmentation point

    for segmentation in segmentations:
        polygon = []
        for seg in segmentation:
            polygon.extend(seg)

        poly_array = np.array(polygon, dtype=np.single).reshape(-1, 2) # Reshape it
        segm_poly_list.append([poly_array])

    poly_list, poly_index_list = _segm_poly_to_tensor(segm_poly_list)
    num_segms_list = [len(segm_poly_list) for segm_poly_list in segm_poly_list]
    target_size = 280
    max_size = 350
    of_mask_list, of_scaled_poly_list = _of_poly_to_mask_pipline(
        images, poly_list, poly_index_list, num_segms_list, target_size, max_size
    )
    of_mask_list = [
        mask_array.reshape(-1, mask_array.shape[-2], mask_array.shape[-1])
        for mask_array in of_mask_list
    ] # reshape it
oneflow.one_hot(indices: oneflow._oneflow_internal.BlobDesc, depth: int, on_value: Union[int, float] = 1, off_value: Union[int, float] = 0, axis: int = -1, dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator generates a onehot Blob from input Blob.

If input Blob’s rank is N, the corresponding onehot Blob’s rank is N+1. The new axis is generated on the specified dimension according to the parameter axis.

The locations represented by indices take value on_value, while other locations take off_value

Parameters
  • indices (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • depth (int) – The length of onehot Blob.

  • on_value (Union[int, float], optional) – The fill value when indices[i] == i. Defaults to 1.

  • off_value (Union[int, float], optional) – The fill value when indice[i] != i. Defaults to 0.

  • axis (int, optional) – The specified dimension that the new axis is generated on. Defaults to -1.

  • dtype (Optional[flow.dtype], optional) – The output data type, it can be “oneflow.int32”, “oneflow.int64”, “oneflow.float”, “oneflow.double”. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Note

The data type of input blob should be int32 or int64

For example:

Example 1:

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


@flow.global_function()
def onehot_Job(x: tp.Numpy.Placeholder((4, ), dtype=flow.int32)
) -> tp.Numpy:
    return flow.one_hot(indices=x,
                        depth=5,
                        axis=-1,
                        dtype=flow.int32)


x = np.array([0, 3, 1, 2]).astype(np.int32)
out = onehot_Job(x)

# out [[1 0 0 0 0]
#      [0 0 0 1 0]
#      [0 1 0 0 0]
#      [0 0 1 0 0]]

Example 2:

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


@flow.global_function()
def onehot_Job(x: tp.Numpy.Placeholder((4, ), dtype=flow.int32)
) -> tp.Numpy:
    return flow.one_hot(indices=x,
                        depth=5,
                        axis=0,
                        dtype=flow.int32)


x = np.array([0, 3, 1, 2]).astype(np.int32)
out = onehot_Job(x)

# out [[1 0 0 0]
#      [0 0 1 0]
#      [0 0 0 1]
#      [0 1 0 0]
#      [0 0 0 0]]
Returns

[description]

Return type

oneflow._oneflow_internal.BlobDesc

oneflow.ones(shape: Sequence[int], dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator creates a Tensor filled with the scalar value 1.

Parameters
  • shape (Sequence[int]) – The shape of the Tensor.

  • dtype (Optional[flow.dtype], optional) – The data type. Defaults to None.

  • name (Optional[str], optional) – The name for the operator. Defaults to None.

Returns

The result Blob filled with value 1

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import oneflow as flow
import oneflow.typing as tp


@flow.global_function()
def ones_job() -> tp.Numpy:
    return flow.ones(shape=(2, 3), dtype=flow.float32)


out = ones_job()

# output: [[1. 1. 1.]
#          [1. 1. 1.]]
oneflow.ones_initializer(dtype: oneflow._oneflow_internal.dtype = oneflow.float32) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates blobs initialized to 1.

Parameters

dtype (flow.dtype, optional) – Default data type. Defaults to flow.float.

Returns

constant_initializer

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def ones_Job() -> None:
    init = flow.ones_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
ones_Job()

# out [1. 1. 1.]

Example 2:

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


@flow.global_function()
def conv2d_one_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.ones_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_one_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.ones_like(like: oneflow._oneflow_internal.BlobDesc, dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator creates a Blob with all elements set to 1 that has the same shape as like.

Parameters
  • like (oneflow._oneflow_internal.BlobDesc) – A 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.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def ones_like_Job() -> tp.Numpy:
    constant_blob = flow.constant(value=1.5,
                                shape=(1, 3, 3),
                                dtype=flow.float)
    ones_like_blob = flow.ones_like(like=constant_blob,
                                    dtype=flow.float)
    return ones_like_blob


out = ones_like_Job()

# out [[[1. 1. 1.]
#       [1. 1. 1.]
#       [1. 1. 1.]]]
oneflow.pack(input: oneflow._oneflow_internal.BlobDesc, pack_num: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.pad(x: oneflow._oneflow_internal.BlobDesc, paddings: Sequence[int], constant_value: Union[int, float] = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator pads the input blob with constant value that user specifies. User can set the amount of padding by setting the parameter paddings.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob

  • paddings (Sequence[int]) – A list of integers to specify the padding width, its length must equal with the length of x.shape.

  • constant_value (Union[int, float], optional) – The constant value to pad. Defaults to 0.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises

ValueError – The parameter paddings must be a tuple or a list.

Returns

The Blob after padding.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def pad_Job(x: tp.Numpy.Placeholder((3, 3))
) -> tp.Numpy:
    return flow.pad(x,
                    paddings=((2, 2), (1, 1)),
                    constant_value=5)


x = np.array([[1, 1, 1],
            [1, 1, 1],
            [1, 1, 1]]).astype(np.float32)
out = pad_Job(x)

# out [[5. 5. 5. 5. 5.]
#      [5. 5. 5. 5. 5.]
#      [5. 1. 1. 1. 5.]
#      [5. 1. 1. 1. 5.]
#      [5. 1. 1. 1. 5.]
#      [5. 5. 5. 5. 5.]
#      [5. 5. 5. 5. 5.]]
oneflow.pad_grad(x: oneflow._oneflow_internal.BlobDesc, paddings: Sequence[int], constant_value: Union[int, float] = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.parallel_cast(input: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None, distribute: Optional[oneflow._oneflow_internal.distribute.Distribute] = None, gradient_distribute: Optional[oneflow._oneflow_internal.distribute.Distribute] = None) → oneflow._oneflow_internal.BlobDesc
class oneflow.placement
property device_type
property hierarchy
oneflow.random_normal_initializer(mean: float = 0.0, stddev: float = 1.0, seed: Optional[int] = None, dtype: Optional[oneflow._oneflow_internal.dtype] = None) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates blob with a normal distribution.

Parameters
  • mean (float, optional) – A python scalar. Mean of the random values to generate.. Defaults to 0.0.

  • stddev (float, optional) – A python scalar. Standard deviation of the random values to generate. Defaults to 1.0.

  • seed (Optional[int], optional) – None. Not support yet. Defaults to None.

  • dtype (Optional[flow.dtype], optional) – . Defaults to None.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def random_normal_Job() -> None:
    init = flow.random_normal_initializer(mean=1, stddev=1)
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
random_normal_Job()

# out [1.4190257 2.7663114 1.7114428]

Example 2:

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


@flow.global_function()
def conv2d_random_normal_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.random_normal_initializer(mean=0, stddev=1)

    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_random_normal_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.random_uniform_initializer(minval: float = 0, maxval: float = 1, dtype: oneflow._oneflow_internal.dtype = oneflow.float32) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates blobs with a uniform distribution.

Parameters
  • minval (float, optional) – A python scalar. Lower bound of the range of random values to generate. Defaults to 0.

  • maxval (float, optional) – A python scalar. Upper bound of the range of random values to generate. Defaults to 1.

  • dtype (flow.dtype, optional) – Default data type. Defaults to flow.float.

Raises

NotImplementedError – Do not support such data type.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def random_uniform_Job() -> None:
    init = flow.random_uniform_initializer(minval=0, maxval=0.5)
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
random_uniform_Job()

# out [0.07557311 0.3943565  0.31875622]

Example 2:

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


@flow.global_function()
def conv2d_random_uniform_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.random_uniform_initializer(minval=0, maxval=0.5)

    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_random_uniform_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.range(start, limit=None, delta=1, dtype=None, name='range') → oneflow._oneflow_internal.BlobDesc

This operator is similar to python range, the difference is that oneflow.range generates a Blob.

Parameters
  • start ([type]) – The start of interval. Its type should be int.

  • limit ([type], optional) – The limit of interval. Its type should be int.

  • delta (int, optional) – The numerical spacing between elements. Defaults to 1.

  • dtype ([type], optional) – The output’s data type. Currently we only support oneflow.int64. Defaults to None.

  • name (str, optional) – The name for the operation. Defaults to “range”.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


@flow.global_function()
def range_job()->tp.Numpy:
    with flow.scope.placement("cpu", "0:0"):
        out = flow.range(10, dtype=flow.int64)

    return out

out = range_job()

# out [0 1 2 3 4 5 6 7 8 9]

Example2:

import oneflow as flow
import oneflow.typing as tp


@flow.global_function()
def range_job()->tp.Numpy:
    with flow.scope.placement("cpu", "0:0"):
        out = flow.range(1, 10, 3, dtype=flow.int64)

    return out

out = range_job()

# out [1 4 7]
oneflow.reflection_pad2d(x: oneflow._oneflow_internal.BlobDesc, padding: Union[int, tuple, list], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Pads the input tensor using the reflection of the input boundary.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – input blob, only support “NCHW” format.

  • padding (Union[int, oneflow._oneflow_internal.BlobDesc]) – The size or bundary of padding, if is int uses the same padding in all dimension;

  • 4-dims tuple, uses (if) –

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

[description]

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def pad_Job(x: tp.Numpy.Placeholder((1, 2, 3, 3))
) -> tp.Numpy:
    return flow.reflection_pad2d(x, padding=[2, 2, 1, 1])


x = np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)
out = pad_Job(x)

# out [[[[ 5.  4.  3.  4.  5.  4.  3.]
#    [ 2.  1.  0.  1.  2.  1.  0.]
#    [ 5.  4.  3.  4.  5.  4.  3.]
#    [ 8.  7.  6.  7.  8.  7.  6.]
#    [ 5.  4.  3.  4.  5.  4.  3.]]

#   [[ 14. 13. 12. 13. 14. 13. 12.]
#    [ 11. 10.  9. 10. 11. 10.  9.]
#    [ 14. 13. 12. 13. 14. 13. 12.]
#    [ 17. 16. 15. 16. 17. 16. 15.]
#    [ 14. 13. 12. 13. 14. 13. 12.]]]]
oneflow.repeat(input: oneflow._oneflow_internal.BlobDesc, repeat_num: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.replication_pad2d(x: oneflow._oneflow_internal.BlobDesc, padding: Union[int, tuple, list], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Pads the input tensor using the replication of the input boundary.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – input blob, only support “NCHW” format.

  • padding (Union[int, oneflow._oneflow_internal.BlobDesc]) – The size or bundary of padding, if is int uses the same padding in all dimension;

  • 4-dims tuple, uses (if) –

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

[description]

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def pad_Job(x: tp.Numpy.Placeholder((1, 2, 3, 3))
) -> tp.Numpy:
    return flow.reflection_pad2d(x, padding=[2, 2, 1, 1])


x = np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)
out = pad_Job(x)

# out [[[[ 0.  0.  0.  1.  2.  2.  2.]
#    [ 0.  0.  0.  1.  2.  2.  2.]
#    [ 3.  3.  3.  4.  5.  5.  5.]
#    [ 6.  6.  6.  7.  8.  8.  8.]
#    [ 6.  6.  6.  7.  8.  8.  8.]]

#   [[ 9.  9.  9.  10.  11.  11.  11.]
#    [ 9.  9.  9.  10.  11.  11.  11.]
#    [ 12.  12.  12.  13.  14.  14.  14.]
#    [ 15.  15.  15.  16.  17.  17.  17.]
#    [ 15.  15.  15.  16.  17.  17.  17.]]]]
oneflow.reshape(x: oneflow._oneflow_internal.BlobDesc, shape: Sequence[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator reshapes a Blob. If the Blob is dynamic, it will call flow.dynamic_reshape automatically

We can set one dimension in shape as -1, the operator will infer the complete shape.

Parameters
  • x – A Blob.

  • shape – Shape of the output blob.

  • name – A name for the operation (optional).

Returns

A Blob, has the same type as x.

For example:

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


@flow.global_function()
def reshape_Job(x: tp.Numpy.Placeholder(shape=(4, 4), dtype=flow.float32)
) -> tp.Numpy:
    reshape_blob = flow.reshape(x,
                                shape=[2, 2, 2, -1])
    return reshape_blob


x = np.array([[1, 2, 3, 4],
            [5, 6, 7, 8],
            [9, 10, 11, 12],
            [13, 14, 15, 16]]).astype(np.float32)
out = reshape_Job(x)

# out.shape (2, 2, 2, 2)
oneflow.reshape_like(x: oneflow._oneflow_internal.BlobDesc, like: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator reshapes the Blob x to be the same as Blob like .

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • like (oneflow._oneflow_internal.BlobDesc) – A Blob.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def reshape_like_Job(x: tp.Numpy.Placeholder(shape=(4, 4), dtype=flow.float32)
) -> tp.Numpy:
    like_blob = flow.constant(value=1,
                            dtype=flow.int8,
                            shape=(2, 2, 4))
    reshape_like_blob = flow.reshape_like(x,
                                        like=like_blob)
    return reshape_like_blob


x = np.array([[1, 2, 3, 4],
            [5, 6, 7, 8],
            [9, 10, 11, 12],
            [13, 14, 15, 16]]).astype(np.float32)
out = reshape_like_Job(x)

# out.shape (2, 2, 4)
oneflow.reverse(input: oneflow._oneflow_internal.BlobDesc, axis: Union[int, Sequence[int]], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator reverses the elements on the assigned axis.

Parameters
  • input (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • axis (Union[int, Sequence[int]]) – The reverse axis.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises
  • ValueError – The name must be a string.

  • ValueError – The axis must be a int or a list/tuple of int.

  • ValueError – The axis is out of range.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def reverse_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32)) -> tp.Numpy:
    reverse_blob = flow.reverse(x,
                                axis=0)
    return reverse_blob


x = np.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]).astype(np.float32)
out = reverse_Job(x)

# out [[7. 8. 9.]
#      [4. 5. 6.]
#      [1. 2. 3.]]
oneflow.same_padding(x: oneflow._oneflow_internal.BlobDesc, padding: Sequence[int], data_format: str, kernel_size: Sequence[int], strides: Sequence[int], dilation_rate: Sequence[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator do the padding in “SAME” mode, It can computes the pad width according to the kernel_size and strides to keep the size of feature map unchanged after convolution or other operations.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – The input blob.

  • padding (Sequence[int]) – The padding mode. It should be “SAME_UPPER” or “SAME_LOWER”

  • data_format ([type]) – The data format of input Blob. If the string starts with “NC”, it means the data format is channel first, else the data format is channel last.

  • kernel_size (Sequence[int]) – The kernel size of operations. Its type should be tuple or list.

  • strides (Sequence[int]) – The strides of operations. Its type should be tuple or list.

  • dilation_rate (Sequence[int]) – The dilation rate of operations. Its type should be tuple or list.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The Blob after padding.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def same_pad_Job(x: tp.Numpy.Placeholder((1, 1, 3, 3))
) -> tp.Numpy:
    return flow.same_padding(x,
                            padding="SAME_UPPER",
                            data_format="NCHW",
                            kernel_size=(3, 3),
                            strides=(1, 1),
                            dilation_rate=(1, 1))


x = np.ones(shape=(1, 1, 3, 3)).astype(np.float32)
out = same_pad_Job(x)

# out [[[[0. 0. 0. 0. 0.]
#        [0. 1. 1. 1. 0.]
#        [0. 1. 1. 1. 0.]
#        [0. 1. 1. 1. 0.]
#        [0. 0. 0. 0. 0.]]]]
oneflow.save(obj, save_dir)
oneflow.scatter_nd(indices: oneflow._oneflow_internal.BlobDesc, updates: oneflow._oneflow_internal.BlobDesc, shape: Sequence[int], name: Optional[str] = None)

This operator inserts the elements in updates according to the indices and create a new Blob.

Parameters
  • indices (oneflow._oneflow_internal.BlobDesc) – The indice of updates. Its type should be flow.int.

  • updates (oneflow._oneflow_internal.BlobDesc) – The update Blob.

  • shape (Sequence[int]) – The constant tensor shape, the constant tensor elements are all zero.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

Example 1:

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


@flow.global_function()
def scatter_nd_Job(indice: tp.Numpy.Placeholder(shape=(3, 1), dtype=flow.int32),
                update: tp.Numpy.Placeholder(shape=(3, ), dtype=flow.float32),
) -> tp.Numpy:
    scatter_blob = flow.scatter_nd(indices=indice,
                                updates=update,
                                shape=[8])
    return scatter_blob


indice_array = np.array([[1], [6], [4]]).astype(np.int32)
update_array = np.array([10.2, 5.1, 12.7]).astype(np.float32)
out = scatter_nd_Job(indice_array, update_array)

# [ 0.  10.2  0.   0.  12.7  0.   5.1  0. ]

Example 2:

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


@flow.global_function()
def scatter_nd_Job(indice: tp.Numpy.Placeholder(shape=(3, 1), dtype=flow.int32),
                update: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
) -> tp.Numpy:
    scatter_blob = flow.scatter_nd(indices=indice,
                                updates=update,
                                shape=[5, 3])
    return scatter_blob


indice_array = np.array([[0], [4], [2]]).astype(np.int32)
update_array = np.array([[1, 1, 1],
                        [2, 2, 2],
                        [3, 3, 3]]).astype(np.float32)
out = scatter_nd_Job(indice_array, update_array)

# out [[1. 1. 1.]
#      [0. 0. 0.]
#      [3. 3. 3.]
#      [0. 0. 0.]
#      [2. 2. 2.]]
oneflow.slice(x: oneflow._oneflow_internal.BlobDesc, begin: Sequence[int], size: Sequence[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Extracts a slice from a tensor.

Parameters
  • x – A Blob.

  • begin – A list or a tuple, indicate each dimension slice begin, whose length must be equal to x’s number of dimensions, the first element of begin must be set to None. (Because the internal op of OneFlow does not support 0-dimension slice at present.)

  • size – A list or a tuple, indicate each dimension slice size, whose length must be equal to x’s number of dimensions, the first element of beign must be set to None.

  • name – A name for the operation (optional).

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def slice_Job(x: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32)
) -> tp.Numpy:
    slice_blob = flow.slice(x,
                            begin=[None, 0],
                            size=[None, 2])
    return slice_blob

x = np.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]).astype(np.float32)
out = slice_Job(x)

# out [[1. 2.]
#      [4. 5.]
#      [7. 8.]]
oneflow.slice_update(x: oneflow._oneflow_internal.BlobDesc, update: oneflow._oneflow_internal.BlobDesc, slice_tup_list: Sequence[Tuple[int, int, int]], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Update a slice of tensor x. Like x[start:stop:step] = update.

Parameters
  • x – A Blob, whose slice will be updated.

  • update – A Blob, indicate the update content.

  • slice_tup_list – A list of slice tuple, indicate each dimension slice (start, stop, step).

  • name – A name for the operation (optional).

For example:

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


@flow.global_function()
def slice_update_job(x: tp.Numpy.Placeholder(shape=(5, )),
                    update: tp.Numpy.Placeholder(shape=(3, )))->tp.Numpy:
    out = flow.slice_update(x=x,
                            update=update,
                            slice_tup_list=[[1, 4, 1]])

    return out

x = np.array([1, 1, 1, 1, 1]).astype(np.float32)
update = np.array([2, 3, 4]).astype(np.float32)
out = slice_update_job(x, update)

# out [1. 2. 3. 4. 1.]
oneflow.slice_v2(x: oneflow._oneflow_internal.BlobDesc, slice_tup_list: Sequence[Tuple[int, int, int]], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Extracts a slice from a tensor. The slice_tup_list assigns the slice indices in each dimension, the format is (start, stop, step). The operator will slice the Blob according to the slice_top_list.

Parameters
  • x – A Blob.

  • slice_tup_list – A list of slice tuple, indicate each dimension slice (start, stop, step).

  • name – A name for the operation (optional).

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

Note: Because the internal op of OneFlow does not support 0-dimension slice at present, we should set the zero element in slice_tup_list as None.

For example:

import oneflow as flow
import numpy as np
import oneflow.typing as tp
@flow.global_function()
def slicev2_Job(x: tp.Numpy.Placeholder(shape=(3, 6, 9), dtype=flow.float32)
) -> tp.Numpy:
    slicev2_blob = flow.slice_v2(x,
                                slice_tup_list=[[None, None, None],
                                                [0, 5, 2], # slice in dimension 1, extract [0, 2, 4]
                                                [0, 6, 3]]) # slice in dimension 2, extract [0, 3]
    return slicev2_blob
x = np.random.randn(3, 6, 9).astype(np.float32)
out = slicev2_Job(x)

# out.shape (3, 3, 2)
oneflow.smooth_l1_loss(prediction: oneflow._oneflow_internal.BlobDesc, label: oneflow._oneflow_internal.BlobDesc, beta: float = 1.0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator computes the smooth l1 loss.

The equation is:

\[ \begin{align}\begin{aligned}& out = \frac{(\beta*x)^2}{2}, \left|x\right|<\frac{1}{{\beta}^2}\\& out = \left|x\right|-\frac{0.5}{{\beta}^2}, otherwise\end{aligned}\end{align} \]
Parameters
  • prediction (oneflow._oneflow_internal.BlobDesc) – The prediction Blob

  • label (oneflow._oneflow_internal.BlobDesc) – The label Blob

  • beta (float, optional) – The \(\beta\) in the equation. Defaults to 1.0.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def smooth_l1_loss_Job(prediction: tp.Numpy.Placeholder((5, )),
                    label: tp.Numpy.Placeholder((5, ))
) -> tp.Numpy:
    return flow.smooth_l1_loss(prediction=prediction,
                            label=label)


prediction = np.array([0.1, 0.4, 0.3, 0.5, 0.9]).astype(np.float32)
label = np.array([0.3, 0.9, 2.5, 0.4, 0.3]).astype(np.float32)
out = smooth_l1_loss_Job(prediction, label)

# out [0.02       0.12499999 1.7        0.005      0.17999998]
oneflow.sort(input: oneflow._oneflow_internal.BlobDesc, axis: int = -1, direction: str = 'ASCENDING', name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator sorts the input Blob at specified axis.

Parameters
  • input (oneflow._oneflow_internal.BlobDesc) – A Blob

  • axis (int, optional) – dimension to be sorted. Defaults to the last dim (-1)

  • direction (str, optional) – The direction in which to sort the Blob values. If the direction is “ASCENDING”, The order of input will be sorted as ascending, else, the order of input will be sorted as descending. Defaults to “ASCENDING”.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The sorted Blob

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def sort_Job(x: tp.Numpy.Placeholder((5, ))
) -> tp.Numpy:
    return flow.sort(input=x)

x = np.array([10, 2, 9, 3, 7]).astype("float32")
out = sort_Job(x)

# out [ 2.  3.  7.  9. 10.]
oneflow.squeeze(input: oneflow._oneflow_internal.BlobDesc, axis: Optional[Sequence[int]] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator removes the specified dimention which size is 1 of the input Blob. If the axis is not specified, this operator will remove all the dimention which size is 1 of the input Blob.

The amount of element in return value is the same as Blob input.

Parameters
  • input (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • axis (Optional[Sequence[int]], optional) – The axis. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

Example 1:

Example 2:

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


@flow.global_function()
def squeeze_Job(x: tp.Numpy.Placeholder(shape=(1, 1, 1, 3), dtype=flow.int32),
) -> tp.Numpy:
    return flow.squeeze(x, axis=[1, 2])


x = np.array([[[[1, 1, 1]]]]).astype(np.int32)
out = squeeze_Job(x)

# out.shape (1, 3)
oneflow.stack(inputs: Sequence[oneflow._oneflow_internal.BlobDesc], axis: int = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator stacks the multiple Blobs on the specified axis.

Parameters
  • inputs (Sequence[oneflow._oneflow_internal.BlobDesc]) – A list of input Blob.

  • axis (int) – The stack axis.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

For example:

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


@flow.global_function()
def stack_job(x: tp.Numpy.Placeholder(shape=(2, 4, 6)),
            y: tp.Numpy.Placeholder(shape=(2, 4, 6)))->tp.Numpy:
    out = flow.stack([x, y], axis=2)
    return out

x = np.ones(shape=(2, 4, 6), dtype=np.float32)
y = np.ones(shape=(2, 4, 6), dtype=np.float32)

out = stack_job(x, y)

# output.shape (2, 4, 2, 6)
Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

oneflow.sync_default_session() → None

Synchronize the default session. Block until every synchronous OneFlow function and its callback finishes running.

oneflow.sync_dynamic_resize(inputs: oneflow._oneflow_internal.BlobDesc, size: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
Parameters
  • inputs (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • size (oneflow._oneflow_internal.BlobDesc) – The size of new Blob.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob. Its type is ListNumpy.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def sync_dynamic_resize_Job(x: tp.Numpy.Placeholder(shape=(4, 3), dtype=flow.float32),
                            size: tp.Numpy.Placeholder(shape=(1, ), dtype=flow.int32),
) -> tp.ListNumpy:
    resize_Blob = flow.sync_dynamic_resize(inputs=x,
                                        size=size)
    return resize_Blob

x = np.array([[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9],
            [10, 11, 12]]).astype(np.float32)
size = np.array([2]).astype(np.int32)
out = sync_dynamic_resize_Job(x, size)

# out [array([[1., 2., 3.],
#             [4., 5., 6.]], dtype=float32)]
oneflow.tensor(data, dtype=None, device=None, requires_grad=False, placement=None, sbp=None, is_consistent=False, is_lazy=False)
oneflow.tensor_buffer_to_list_of_tensors(x: oneflow._oneflow_internal.BlobDesc, out_shape: Sequence[int], out_dtype: oneflow._oneflow_internal.dtype, dynamic_out: Optional[bool] = False, name: Optional[str] = None) → List[oneflow._oneflow_internal.BlobDesc]

This operator converts the Blob of TensorBuffer to list of Tensors. Every element in x will be converted to a Tensor and output will be flatten to a list.

Parameters
  • x (BlobDesc) – Input Blob, data type must be tensor buffer.

  • out_shape (Sequence[int]) – max shape for a tensor buffer in x

  • out_dtype (flow.dtype,) – output data type

  • dynamic_out (Optional[bool]) – if output is dynamic blob. Default to False.

  • name (Optional[str]) – The name for the operation. Default to None.

Returns

result blobs

Return type

List[BlobDesc]

For example:

# the same with `gen_tensor_buffer` op
oneflow.tensor_buffer_to_tensor(x: oneflow._oneflow_internal.BlobDesc, dtype: oneflow._oneflow_internal.dtype, instance_shape: Sequence[int], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator converts the Blob’s type from TensorBuffer to Tensor. Some operator’s output data type is TensorBuffer, you can use this operator to convert back to Tensor.

Refer to Concept Explanation for more about TensorBuffer.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – Input Blob.

  • dtype (flow.dtype) – The data dtype.

  • instance_shape (Sequence[int]) – The shape of each TensorBuffer instance.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def tensor_buffer_to_tensor_Job(x: tp.Numpy.Placeholder(shape=(4, 16, 64, 64), dtype=flow.float32),
) -> tp.Numpy:
    x = flow.tensor_to_tensor_buffer(x,
                                    instance_dims=2)
    return flow.tensor_buffer_to_tensor(x,
                                        instance_shape=(64, 64),
                                        dtype=flow.float)

x = np.random.randn(4, 16, 64, 64).astype(np.float32)
out = tensor_buffer_to_tensor_Job(x)

# out.shape (4, 16, 64, 64)
oneflow.tensor_scatter_nd_add(params: oneflow._oneflow_internal.BlobDesc, indices: oneflow._oneflow_internal.BlobDesc, updates: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator adds elements from ‘updates’ to Blob ‘params’ based on the indices.

Parameters
  • params (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • indices (oneflow._oneflow_internal.BlobDesc) – The indice of updates. Its type should be flow.int32.

  • updates (oneflow._oneflow_internal.BlobDesc) – The update Blob.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def tensor_scatter_nd_add_Job(x: tp.Numpy.Placeholder(shape=(5, 3), dtype=flow.float32),
                            indice: tp.Numpy.Placeholder(shape=(3, 1), dtype=flow.int32),
                            update: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
) -> tp.Numpy:
    scatter_blob = flow.tensor_scatter_nd_add(params=x,
                                            indices=indice,
                                            updates=update)
    return scatter_blob

x = np.array([[1, 2, 3],
            [1, 2, 3],
            [1, 2, 3],
            [1, 2, 3],
            [1, 2, 3]]).astype(np.float32)
indice_array = np.array([[0], [4], [2]]).astype(np.int32)
update_array = np.array([[1, 1, 1],
                        [2, 2, 2],
                        [3, 3, 3]]).astype(np.float32)
out = tensor_scatter_nd_add_Job(x, indice_array, update_array)

# out [[2. 3. 4.]
#      [1. 2. 3.]
#      [4. 5. 6.]
#      [1. 2. 3.]
#      [3. 4. 5.]]
oneflow.tensor_scatter_nd_update(params: oneflow._oneflow_internal.BlobDesc, indices: oneflow._oneflow_internal.BlobDesc, updates: oneflow._oneflow_internal.BlobDesc, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator inserts the elements in updates according to the indices into the Blob params.

Parameters
  • params (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • indices (oneflow._oneflow_internal.BlobDesc) – The indice of updates. Its type should be flow.int32.

  • updates (oneflow._oneflow_internal.BlobDesc) – The update Blob.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def tensor_scatter_nd_Job(x: tp.Numpy.Placeholder(shape=(5, 3), dtype=flow.float32),
                        indice: tp.Numpy.Placeholder(shape=(3, 1), dtype=flow.int32),
                        update: tp.Numpy.Placeholder(shape=(3, 3), dtype=flow.float32),
) -> tp.Numpy:
    scatter_blob = flow.tensor_scatter_nd_update(params=x,
                                                indices=indice,
                                                updates=update)
    return scatter_blob

x = np.array([[1, 2, 3],
            [1, 2, 3],
            [1, 2, 3],
            [1, 2, 3],
            [1, 2, 3]]).astype(np.float32)
indice_array = np.array([[0], [4], [2]]).astype(np.int32)
update_array = np.array([[1, 1, 1],
                        [2, 2, 2],
                        [3, 3, 3]]).astype(np.float32)
out = tensor_scatter_nd_Job(x, indice_array, update_array)

# out [[1. 1. 1.]
#      [1. 2. 3.]
#      [3. 3. 3.]
#      [1. 2. 3.]
#      [2. 2. 2.]]
oneflow.tensor_to_tensor_buffer(x: oneflow._oneflow_internal.BlobDesc, instance_dims: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator converts the Blob’s type from Tensor to TensorBuffer.

Refer to Concept Explanation for more about TensorBuffer.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – Input Blob.

  • instance_dims (int) – The dimensions of dynamic tensor instance.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def tensor_buffer_to_tensor_Job(x: tp.Numpy.Placeholder(shape=(4, 16, 64, 64), dtype=flow.float32),
) -> tp.Numpy:
    x = flow.tensor_to_tensor_buffer(x,
                                    instance_dims=2)
    return flow.tensor_buffer_to_tensor(x,
                                        instance_shape=(64, 64),
                                        dtype=flow.float)

x = np.random.randn(4, 16, 64, 64).astype(np.float32)
out = tensor_buffer_to_tensor_Job(x)

# out.shape (4, 16, 64, 64)
oneflow.to(input, *args, **kwargs)
Performs Tensor dtype and/or device conversion.

A flow.dtype and flow.device are inferred from the arguments of input.to(*args, **kwargs).

Note

If the input Tensor already has the correct flow.dtype and flow.device, then input is returned. Otherwise, the returned tensor is a copy of input with the desired.

Parameters
Returns

A Tensor.

Return type

oneflow.Tensor

For example:

>>> import numpy as np
>>> import oneflow.experimental as flow
>>> flow.enable_eager_execution()

>>> arr = np.random.randint(1, 9, size=(1, 2, 3, 4))
>>> input = flow.Tensor(arr)
>>> output = input.to(dtype=flow.float32)
>>> np.array_equal(arr.astype(np.float32), output.numpy())
True
oneflow.transpose(a: oneflow._oneflow_internal.BlobDesc, perm: Sequence[int] = None, conjugate: bool = False, batch_axis_non_change: bool = False, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator transposes the specified axis of input Blob.

Parameters
  • a (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • perm (Sequence[int], optional) – The list of dimension permutation. Defaults to None.

  • conjugate (bool, optional) – Still Unavailable. Defaults to False.

  • batch_axis_non_change (bool, optional) – deprecated. Defaults to False.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises

NotImplementedError – The attribute conjugate still unavailable.

Returns

A transposed blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def transpose_Job(x: tp.Numpy.Placeholder(shape=(1, 2, 3), dtype=flow.float32)
) -> tp.Numpy:
    transpose_blob = flow.transpose(x,
                                    perm=[2, 0, 1])
    return transpose_blob

x = np.random.randn(1, 2, 3).astype(np.float32)
out = transpose_Job(x)

# out.shape (3, 1, 2)
oneflow.truncated_normal(stddev: float = 1.0) → oneflow.core.job.initializer_conf_pb2.InitializerConf
oneflow.truncated_normal_initializer(mean: float = 0.0, stddev: float = 1.0) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a truncated normal distribution.

Parameters
  • mean (float, optional) – A scalar (float). Defaults to 0.0.

  • stddev (float, optional) – A scalar (float). Defaults to 1.0.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def truncated_normal_Job() -> None:
    init = flow.truncated_normal_initializer(mean=1, stddev=1)
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
truncated_normal_Job()

# out [1.8303236  0.09787154 0.83049864]

Example 2:

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


@flow.global_function()
def conv2d_truncated_normal_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.truncated_normal_initializer(mean=0, stddev=1)

    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_truncated_normal_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.unpack(input: oneflow._oneflow_internal.BlobDesc, unpack_num: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc
oneflow.unsorted_batch_segment_sum(data: oneflow._oneflow_internal.BlobDesc, segment_ids: oneflow._oneflow_internal.BlobDesc, num_segments: int, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

It is similar with unsorted_segment_sum, the difference is that unsorted_batch_segment_sum brings a batch axis. We can do the segment sum in different batch of data.

For example, the segment id is like:

[[0 0 0 1 2 2 3 3],
 [0 0 1 1 2 3 3 3]]
Parameters
  • data (oneflow._oneflow_internal.BlobDesc) – Input Blob

  • segment_ids (oneflow._oneflow_internal.BlobDesc) – A Blob with shape (d0, d1). The d0, d1 are the first and second dimension of data.

  • num_segments (int) – num_segments should equal the number of distinct segment IDs.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def unsorted_batch_segment_sum_Job(data: tp.Numpy.Placeholder((3, 4)),
                                segment_ids: tp.Numpy.Placeholder((3, 4), dtype=flow.int32)
)->tp.Numpy:
    return flow.math.unsorted_batch_segment_sum(data, segment_ids, 2)

input_blob = np.array([[1, 2, 3, 4],
                    [1, 2, 3 ,4],
                    [1, 2, 3, 4]]).astype(np.float32)
segment_ids = np.array([[0, 0, 0, 1],
                        [0, 0, 1, 0],
                        [0, 1, 0, 0]]).astype(np.int32)
out = unsorted_batch_segment_sum_Job(input_blob, segment_ids)

# out [[6. 4.]
#      [7. 3.]
#      [8. 2.]]
oneflow.unsorted_segment_sum(data: oneflow._oneflow_internal.BlobDesc, segment_ids: oneflow._oneflow_internal.BlobDesc, num_segments: int, axis: int = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Computes the sum along segments of a Blob.

Parameters
  • data (oneflow._oneflow_internal.BlobDesc) – Input Blob

  • segment_ids (oneflow._oneflow_internal.BlobDesc) – A Blob should be the size of the first dimension, with consecutive IDs in the range 0 to k (k < d0).

  • num_segments (int) – num_segments should equal the number of distinct segment IDs.

  • axis (int, optional) – The axis of data. Defaults to 0.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob with the same type of data.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

# Example 1:
import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def unsorted_segment_sumJob(data: tp.Numpy.Placeholder((3, 4)),
                            segment_ids: tp.Numpy.Placeholder((4, ), dtype=flow.int32)
)->tp.Numpy:
    return flow.math.unsorted_segment_sum(data, segment_ids, num_segments=2, axis=1)

input_blob = np.array([[1, 2, 3, 4],
                       [5, 6, 7 ,8],
                       [9, 10, 11, 12]]).astype(np.float32)
segment_ids = np.array([0, 1, 0, 1]).astype(np.int32)
out = unsorted_segment_sumJob(input_blob, segment_ids)

# out [[ 4.  6.]
#      [12. 14.]
#      [20. 22.]]

# Example 2
import oneflow as flow
import numpy as np
import oneflow.typing as tp

@flow.global_function()
def unsorted_segment_sumJob(data: tp.Numpy.Placeholder((3, 4)),
                            segment_ids: tp.Numpy.Placeholder((3, ), dtype=flow.int32)
)->tp.Numpy:
    return flow.math.unsorted_segment_sum(data, segment_ids, num_segments=2, axis=0)

input_blob = np.array([[1, 2, 3, 4],
                       [5, 6, 7 ,8],
                       [9, 10, 11, 12]]).astype(np.float32)
segment_ids = np.array([0, 1, 0]).astype(np.int32)
out = unsorted_segment_sumJob(input_blob, segment_ids)

#  out [[10. 12. 14. 16.]
#       [ 5.  6.  7.  8.]]
oneflow.unsorted_segment_sum_like(data: oneflow._oneflow_internal.BlobDesc, segment_ids: oneflow._oneflow_internal.BlobDesc, like: oneflow._oneflow_internal.BlobDesc, axis: int = 0, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Computes the sum along segments of a Blob, the output shape is the same as the like Blob.

Parameters
  • data (oneflow._oneflow_internal.BlobDesc) – Input Blob

  • segment_ids (oneflow._oneflow_internal.BlobDesc) – A Blob should be the size of the first dimension, with consecutive IDs in the range 0 to k (k < d0).

  • like (oneflow._oneflow_internal.BlobDesc) – The input Blob which specifies shape

  • axis (int, optional) – The axis of data. Defaults to 0.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

A Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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

@flow.global_function()
def unsorted_segment_sum_like_Job(data: tp.Numpy.Placeholder((3, 4)),
                                segment_ids: tp.Numpy.Placeholder((3, ), dtype=flow.int32),
                                like: tp.Numpy.Placeholder((2, 4), dtype=flow.float32)
)->tp.Numpy:
    return flow.math.unsorted_segment_sum_like(data, segment_ids, like, axis=0)

input_blob = np.array([[1, 2, 3, 4],
                    [5, 6, 7 ,8],
                    [9, 10, 11, 12]]).astype(np.float32)
segment_ids = np.array([0, 1, 0]).astype(np.int32)
like = np.zeros(shape=(2, 4), dtype=np.float32)

out = unsorted_segment_sum_like_Job(input_blob, segment_ids, like)

# out [[10. 12. 14. 16.]
#      [ 5.  6.  7.  8.]]
oneflow.user_op_builder(op_name)

Build a wrapper of user op.

For instance::
def myargmax(

input: oneflow._oneflow_internal.BlobDesc) -> oneflow._oneflow_internal.BlobDesc: return ( flow.user_op_builder(“myargmax”) .Op(“argmax”) .Input(“in”, [input]) .Output(“out”) .Build() .InferAndTryRun() .RemoteBlobList()[0] )

Parameters

op_name (str) – name of new user op

Returns

UserOpConfBuilder object used to build a wrapper of user op.

Return type

UserOpConfBuilder

oneflow.user_op_module_builder(op_type_name)
oneflow.variance_scaling_initializer(scale: float = 1.0, mode: str = 'fan_in', distribution: str = 'truncated_normal', data_format: str = '') → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a truncated normal distribution or a random normal distribution or a random uniform distribution with a scale adapting to it.

When the distribution is “truncated_normal”

The equation is:

\[W\sim N(0, \sqrt{\frac{{scale}}{{n}}})\]

If mode is “fan_in”, the “n” is the number of input units in the weight Blob.

If mode is “fan_out”, the “n” is the number of output units in the weight Blob.

if mode is “fan_avg”, the “n” is the average of the number of input and output units in the weight Blob

Parameters
  • scale (float, optional) – Scaling factor (positive float). Defaults to 1.0.

  • mode (str, optional) – One of “fan_in”, “fan_out”, “fan_avg”. Defaults to “fan_in”.

  • distribution (str, optional) – Random distribution to use. One of “truncated_normal”,. Defaults to “truncated_normal”.

  • data_format (str, optional) – A string be one of “N…C” or “NC…”. Defaults to “”.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def variance_scale_Job() -> None:
    init = flow.variance_scaling_initializer(scale=2.0, mode="fan_avg")
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
variance_scale_Job()

# out [[-0.13931477  0.12266728 -0.9434968 ]
#      [-0.49665168  0.10231158 -0.19194333]
#      [-0.7902896  -1.7034698  -0.38695997]]

Example 2:

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


@flow.global_function()
def conv2d_variance_scaling_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.variance_scaling_initializer(mode="fan_out")
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_variance_scaling_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.watch(blob_watched: oneflow._oneflow_internal.BlobDesc, handler_or_prompt: Union[Callable, str, None] = None) → None

Register callback for a blob. The callback function will be called after the computation produce the blob finishes. We can use it to watch the values of Blob.

Parameters
  • blob_watched – a Blob

  • handler_or_prompt – a function has an argument of a Blob

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def watch_Job() -> None:
    init = flow.constant_initializer(2.5)
    variable = flow.get_variable(
        "variable-weight",
        shape=(5, ),
        initializer=init,
        trainable=True
    )
    flow.watch(variable, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
watch_Job()

# out [2.5 2.5 2.5 2.5 2.5]

Example 2:

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

def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def watch_Job(x: tp.Numpy.Placeholder((1, 3, 2, 2))
) -> None:
    initializer = flow.truncated_normal(0.1)
    conv2d = flow.layers.conv2d(
        x,
        filters=3,
        kernel_size=1,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )

    flow.watch(conv2d, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
x = np.ones(shape=(1, 3, 2, 2)).astype(np.float32)
watch_Job(x)

# out [[[[ 0.03757111  0.03757111]
#        [ 0.03757111  0.03757111]]

#       [[-0.36131713 -0.36131713]
#        [-0.36131713 -0.36131713]]

#       [[-0.12266113 -0.12266113]
#        [-0.12266113 -0.12266113]]]]
oneflow.watch_diff(blob_watched: oneflow._oneflow_internal.BlobDesc, handler_or_prompt: Union[Callable, str, None] = None) → None

Register callback for gradient of a blob. The callback will be called after the computation produce the gradient blob finishes.

Parameters
  • blob_watched – a Blob

  • handler_or_prompt – a function has an argument of a Blob

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


BATCH_SIZE = 20

def watch_diff_handler(blob: tp.Numpy):
    print("watch_diff_handler:", blob, blob.shape, blob.dtype)

@flow.global_function(type="train")
def train_job(
    images: tp.Numpy.Placeholder((BATCH_SIZE, 1, 28, 28), dtype=flow.float),
    labels: tp.Numpy.Placeholder((BATCH_SIZE,), dtype=flow.int32),
) -> tp.Numpy:
    initializer = flow.truncated_normal(0.1)
    with flow.scope.placement("gpu", "0:0"):
        reshape = flow.reshape(images, [images.shape[0], -1])
        hidden = flow.layers.dense(
            reshape,
            512,
            activation=flow.nn.relu,
            kernel_initializer=initializer,
            name="hidden",
        )
        logits = flow.layers.dense(
            hidden, 10, kernel_initializer=initializer, name="output"
        )
        loss = flow.nn.sparse_softmax_cross_entropy_with_logits(labels, logits, name="softmax_loss")

    lr_scheduler = flow.optimizer.PiecewiseConstantScheduler([], [0.1])
    flow.optimizer.SGD(lr_scheduler, momentum=0).minimize(loss)
    flow.watch_diff(logits, watch_diff_handler)
    return loss


if __name__ == "__main__":
    checkpoint = flow.train.CheckPoint()
    checkpoint.init()
    (train_images, train_labels), (test_images, test_labels) = flow.data.load_mnist(
            BATCH_SIZE
    )
    for i, (images, labels) in enumerate(zip(train_images, train_labels)):
        loss = train_job(images, labels)


# watch_diff_handler: [[-1.88834548e-01  2.71021971e-03  2.28271242e-02  7.17673637e-03
#                       4.10183379e-03  8.93106461e-02  2.23669074e-02  3.86103359e-03
#                       3.12465224e-02  5.23346756e-03] .....

Example 2:

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


BATCH_SIZE = 20

def watch_diff_handler(blob: tp.Numpy):
    print("watch_diff_handler:", blob)


@flow.global_function(type="train")
def watch_matmul_diff_job(
    images: tp.Numpy.Placeholder((3, 3), dtype=flow.float),
) -> None:
    with flow.scope.placement("cpu", "0:0"):
        weight_initializer = flow.constant_initializer(2)
        weight_shape = (3, BATCH_SIZE)
        weight = flow.get_variable(
            "matmultest-weight",
            shape=weight_shape,
            initializer=weight_initializer)
        output = flow.linalg.matmul(images, weight)

    lr_scheduler = flow.optimizer.PiecewiseConstantScheduler([], [0.1])
    flow.optimizer.SGD(lr_scheduler, momentum=0.9).minimize(output)
    flow.watch_diff(weight, watch_diff_handler)


if __name__ == "__main__":
    check_point = flow.train.CheckPoint()
    check_point.init()

    x = np.array([[1, 1, 1],
                [1, 1, 1],
                [1, 1, 1]]).astype(np.float32)
    watch_matmul_diff_job(x)

# watch_diff_handler: [[3. 3. 3.]
#                      [3. 3. 3.]
#                      [3. 3. 3.]]

Example 3:

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


def watch_diff_handler(blob: tp.Numpy):
    print("watch_diff_handler:", blob, blob.shape, blob.dtype)


@flow.global_function(type="train")
def watch_conv_diff_job(
    images: tp.Numpy.Placeholder((1, 1, 4, 4), dtype=flow.float),
) -> None:
    with flow.scope.placement("gpu", "0:0"):
        weight_shape = (1, 1, 3, 3)
        weight_initializer = flow.truncated_normal(0.1)
        weight = flow.get_variable(
            name="conv-weight",
            shape=weight_shape,
            initializer=weight_initializer
        )
        output = flow.nn.conv2d(images, weight, strides=1, padding="VALID")

    lr_scheduler = flow.optimizer.PiecewiseConstantScheduler([], [0.1])
    flow.optimizer.SGD(lr_scheduler, momentum=0.9).minimize(output)
    flow.watch_diff(weight, watch_diff_handler)


if __name__ == "__main__":
    check_point = flow.train.CheckPoint()
    check_point.init()

    x = np.array([[[[ 1.,  2.,  3.,  4.],
                    [ 5.,  6.,  7.,  8.],
                    [ 9., 10., 11., 12.],
                    [13., 14., 15., 16.]]]]).astype(np.float32)

    watch_conv_diff_job(x)

# watch_diff_handler: [[[[14. 18. 22.]
#                        [30. 34. 38.]
#                        [46. 50. 54.]]]]
oneflow.where(condition: oneflow._oneflow_internal.BlobDesc, x: Optional[oneflow._oneflow_internal.BlobDesc] = None, y: Optional[oneflow._oneflow_internal.BlobDesc] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator returns the elements where condition is larger than 0.

If x and y is None, this operator is equal to oneflow.argwhere.

If x and y both are not None, If the element in condition is larger than 0, it will take the x element, else it will take the y element.

Parameters
  • condition (oneflow._oneflow_internal.BlobDesc) – The input Blob.

  • x (Optional[oneflow._oneflow_internal.BlobDesc], optional) – A Blob. Defaults to None.

  • y (Optional[oneflow._oneflow_internal.BlobDesc], optional) – A Blob. Defaults to None.

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Raises

ValueError – It is not supported when exactly one of x or y is non-None

Returns

The result Blob. Its type is ListNumpy.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

Example 1:

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


@flow.global_function()
def where_Job(condition: tp.Numpy.Placeholder(shape=(5, ), dtype=flow.int32),
            x: tp.Numpy.Placeholder(shape=(5, ), dtype=flow.float32),
            y: tp.Numpy.Placeholder(shape=(5, ), dtype=flow.float32),
) -> tp.ListNumpy:
    return flow.where(condition=condition,
                    x=x,
                    y=y)


condition = np.array([3, 0, 1, 0, 1]).astype(np.int32)
x = np.array([10, 20, 30, 40, 50]).astype(np.float32)
y = np.array([100, 200, 300, 400, 500]).astype(np.float32)
out = where_Job(condition, x, y)

# out [array([ 10., 200.,  30., 400.,  50.], dtype=float32)]

Example 2:

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


@flow.global_function()
def where_Job(condition: tp.Numpy.Placeholder(shape=(5, ), dtype=flow.int32),
) -> tp.ListNumpy:
    return flow.where(condition=condition)


condition = np.array([3, 0, 1, 0, 1]).astype(np.int32)
out = where_Job(condition)

# out [array([[0],
#             [2],
#             [4]], dtype=int32)]
oneflow.xavier_normal_initializer(data_format: str = '') → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a Xavier normal distribution.

It also can be called as oneflow.glorot_normal_initializer.

The equation is:

\[W\sim N(0, \sqrt{\frac{{2}}{{n_j+n_{j+1}}}})\]

\(N\) means normal distribution

\(n_j\) means the amount of Nth layer parameters

Parameters

data_format (str, optional) – The data format. Defaults to “”.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def xavier_normal_Job() -> None:
    init = flow.xavier_normal_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
xavier_normal_Job()

# out [[ 0.5908121  -0.10804518 -0.6148571 ]
#      [ 1.4007381  -0.08172473  0.36579943]
#      [-0.6461796  -0.15923311  0.33653972]]

Example 2:

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


@flow.global_function()
def conv2d_xavier_normal_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.xavier_normal_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_xavier_normal_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.xavier_uniform_initializer(data_format: str = '') → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates a Xavier uniform distribution.

It also can be called as oneflow.glorot_uniform_initializer.

The equation is:

\[W\sim U(-\sqrt{\frac{{6}}{{n_j+n_{j+1}}}},\sqrt{\frac{{6}}{{n_j+n_{j+1}}}})\]

\(U\) means uniform distribution

\(n_j\) means the amount of Nth layer parameters

Parameters

data_format (str, optional) – The data format. Defaults to “”.

Returns

Initial configuration

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def xavier_uniform_Job() -> None:
    init = flow.xavier_uniform_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, 3),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
xavier_uniform_Job()

# out [[-0.14424723 -0.9532095  -0.08723891]
#      [-0.8011227  -0.29729813 -0.26769108]
#      [ 0.9208976  -0.5971756  -0.15077025]]

Example 2:

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


@flow.global_function()
def conv2d_xavier_uniform_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.xavier_uniform_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_xavier_uniform_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.zero_pad2d(x: oneflow._oneflow_internal.BlobDesc, padding: Union[int, tuple, list], name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

Pads the input tensor using zeros.

Parameters
  • x (oneflow._oneflow_internal.BlobDesc) – input blob, only support “NCHW” format.

  • padding (Union[int, oneflow._oneflow_internal.BlobDesc]) – The size or bundary of padding, if is int uses the same padding in all dimension;

  • 4-dims tuple, uses (if) –

  • name (Optional[str], optional) – The name for the operation. Defaults to None.

Returns

[description]

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def pad_Job(x: tp.Numpy.Placeholder((1, 2, 3, 3), const_value)
) -> tp.Numpy:
    return flow.constant_pad2d(x, padding=[2, 2, 1, 1], const_value)


x = np.arange(18).reshape((1, 2, 3, 3)).astype(np.float32)
const_value = 1.5
out = pad_Job(x, const_value)

# out [[[[ 0.  0.  0.  0.  0.  0.  0.]
#    [ 0.  0.  0.  1.  2.  0.  0.]
#    [ 0.  0.  3.  4.  5.  0.  0.]
#    [ 0.  0.  6.  7.  8.  0.  0.]
#    [ 0.  0.  0.  0.  0.  0.  0.]]

#   [[ 0.  0.  0.  0.  0.  0.  0.]
#    [ 0.  0.  9.  10.  11.  0.  0.]
#    [ 0.  0.  12.  13.  14.  0.  0.]
#    [ 0.  0.  15.  16.  17.  0.  0.]
#    [ 0.  0.  0.  0.  0.  0.  0.]]]]
oneflow.zeros(shape: Sequence[int], dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator creates a Tensor filled with the scalar value 0.

Parameters
  • shape (Sequence[int]) – The shape of the Tensor.

  • dtype (Optional[flow.dtype], optional) – The data type. Defaults to None.

  • name (Optional[str], optional) – The name for the operator. Defaults to None.

Returns

The result Tensor filled with value 0

Return type

oneflow._oneflow_internal.BlobDesc

For example:

import oneflow as flow
import oneflow.typing as tp


@flow.global_function()
def zeros_job() -> tp.Numpy:
    return flow.zeros(shape=(2, 3), dtype=flow.float32)


out = zeros_job()

# output: [[0. 0. 0.]
#          [0. 0. 0.]]
oneflow.zeros_initializer(dtype: oneflow._oneflow_internal.dtype = oneflow.float32) → oneflow.core.job.initializer_conf_pb2.InitializerConf

Initializer that generates blobs initialized to 0

Parameters

dtype (flow.dtype, optional) – Default data type. Defaults to flow.float.

Returns

constant_initializer

Return type

initializer_conf_util.InitializerConf

For example:

Example 1:

import oneflow as flow
import oneflow.typing as tp


def watch_handler(y: tp.Numpy):
    print("out", y)


@flow.global_function()
def zeros_Job() -> None:
    init = flow.zeros_initializer()
    blob = flow.get_variable(
        "blob-weight",
        shape=(3, ),
        initializer=init,
        trainable=True
    )
    flow.watch(blob, watch_handler)


checkpoint = flow.train.CheckPoint()
checkpoint.init()
zeros_Job()

# out [0. 0. 0.]

Example 2:

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


@flow.global_function()
def conv2d_zero_Job(x: tp.Numpy.Placeholder((1, 256, 32, 32))
) -> tp.Numpy:
    initializer = flow.zeros_initializer()
    conv2d = flow.layers.conv2d(
        x,
        filters=128,
        kernel_size=3,
        strides=1,
        padding='SAME',
        kernel_initializer=initializer,
        name="Conv2d"
    )
    return conv2d


x = np.random.randn(1, 256, 32, 32).astype(np.float32)
out = conv2d_zero_Job(x)

# out.shape (1, 128, 32, 32)
oneflow.zeros_like(like: oneflow._oneflow_internal.BlobDesc, dtype: Optional[oneflow._oneflow_internal.dtype] = None, name: Optional[str] = None) → oneflow._oneflow_internal.BlobDesc

This operator creates a Blob that has the same shape as like whose all elements are set to 0.

Parameters
  • like (oneflow._oneflow_internal.BlobDesc) – A 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.

Returns

The result Blob.

Return type

oneflow._oneflow_internal.BlobDesc

For example:

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


@flow.global_function()
def zeros_like_Job() -> tp.Numpy:
    constant_blob = flow.constant(value=1.5,
                                shape=(1, 3, 3),
                                dtype=flow.float)
    zeros_like_blob = flow.zeros_like(like=constant_blob,
                                    dtype=flow.float)
    return zeros_like_blob


out = zeros_like_Job()

# out [[[0. 0. 0.]
#       [0. 0. 0.]
#       [0. 0. 0.]]]

Types

oneflow.double

oneflow.float

oneflow.float32

oneflow.float64

oneflow.int32

oneflow.int64

oneflow.int8