From 1f9db982ee8f63399773176cd19c2f2f3d5178b4 Mon Sep 17 00:00:00 2001 From: Shenghang Tsai Date: Fri, 4 Jun 2021 18:32:18 +0800 Subject: [PATCH] Fix doctest not raising when failed (#5100) * fix doctest not raising * fix case * fix * add check * refine Co-authored-by: oneflow-ci-bot <69100618+oneflow-ci-bot@users.noreply.github.com> --- ci/check/run_license_format.py | 18 +++++---- oneflow/python/nn/modules/abs.py | 4 +- oneflow/python/nn/modules/acosh.py | 10 ++--- oneflow/python/nn/modules/activation.py | 8 ++-- oneflow/python/nn/modules/arange.py | 6 +-- oneflow/python/nn/modules/argmax.py | 4 +- oneflow/python/nn/modules/atan2.py | 8 ++-- oneflow/python/nn/modules/batchnorm.py | 2 +- oneflow/python/nn/modules/cast.py | 2 +- oneflow/python/nn/modules/concat.py | 4 +- oneflow/python/nn/modules/constant.py | 2 +- oneflow/python/nn/modules/container.py | 2 +- oneflow/python/nn/modules/dropout.py | 6 +-- oneflow/python/nn/modules/eq.py | 4 +- oneflow/python/nn/modules/exp.py | 10 ++--- oneflow/python/nn/modules/expand.py | 16 ++++---- oneflow/python/nn/modules/gather.py | 8 ++-- oneflow/python/nn/modules/greater.py | 10 ++--- oneflow/python/nn/modules/less.py | 6 +-- oneflow/python/nn/modules/linear.py | 18 ++++----- oneflow/python/nn/modules/log1p.py | 6 +-- oneflow/python/nn/modules/loss.py | 20 +++++----- oneflow/python/nn/modules/masked_fill.py | 2 +- oneflow/python/nn/modules/math_ops.py | 46 +++++++++++----------- oneflow/python/nn/modules/matmul.py | 4 +- oneflow/python/nn/modules/negative.py | 2 +- oneflow/python/nn/modules/normalization.py | 24 +++++------ oneflow/python/nn/modules/permute.py | 4 +- oneflow/python/nn/modules/pooling.py | 26 +----------- oneflow/python/nn/modules/prelu.py | 10 ++--- oneflow/python/nn/modules/repeat.py | 8 ++-- oneflow/python/nn/modules/reshape.py | 2 +- oneflow/python/nn/modules/round.py | 12 +++--- oneflow/python/nn/modules/sign.py | 14 +++---- oneflow/python/nn/modules/sinh.py | 8 ++-- oneflow/python/nn/modules/softplus.py | 6 +-- oneflow/python/nn/modules/squeeze.py | 2 +- oneflow/python/nn/modules/transpose.py | 6 +-- oneflow/python/nn/modules/upsample.py | 20 +++++----- oneflow/python/nn/modules/where.py | 8 ++-- 40 files changed, 178 insertions(+), 200 deletions(-) diff --git a/ci/check/run_license_format.py b/ci/check/run_license_format.py index 0842328f180..f528bbc29d9 100644 --- a/ci/check/run_license_format.py +++ b/ci/check/run_license_format.py @@ -35,12 +35,14 @@ def check_file(path): with open(path) as f: content = f.read() txt = get_txt(path) - if content.count("The OneFlow Authors. All rights reserved.") > 1: - return ("duplicated", content) - if content.startswith(txt) or (not content): + if "import doctest" in content and "raise_on_error=True" not in content: + return ("please add 'doctest.testmod(raise_on_error=True)'", content) + elif content.count("The OneFlow Authors. All rights reserved.") > 1: + return ("license_duplicated", content) + elif content.startswith(txt) or (not content): return ("ok", content) - else: - return ("absent", content) + elif content.startswith(txt) == False: + return ("license_absent", content) def format_file(path): @@ -50,13 +52,13 @@ def format_file(path): format_status, content = check_file(path) if format_status == "ok": return True - elif format_status == "absent": + elif format_status == "license_absent": with open(path, "w") as w: new_content = txt + content w.write(new_content) return False else: - raise ValueError(f"license {format_status} {path}") + raise ValueError(f"{format_status} {path}") def do_check(x): @@ -97,7 +99,7 @@ def glob_files(path): any_absence = False for (p, format_status) in p.map(do_check, files): if format_status != "ok": - print(f"license {format_status}:", p) + print(f"{format_status}:", p) any_absence = True if any_absence: exit(1) diff --git a/oneflow/python/nn/modules/abs.py b/oneflow/python/nn/modules/abs.py index 15a3e01f9b3..6bcb1ca6bbf 100644 --- a/oneflow/python/nn/modules/abs.py +++ b/oneflow/python/nn/modules/abs.py @@ -50,7 +50,7 @@ def abs_op(x): >>> x = flow.Tensor(np.array([-1, 2, -3, 4]).astype(np.float32)) >>> flow.abs(x) tensor([1., 2., 3., 4.], dtype=oneflow.float32) - + """ return Abs()(x) @@ -58,4 +58,4 @@ def abs_op(x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/acosh.py b/oneflow/python/nn/modules/acosh.py index 1d9b3d5f1ba..4c13b0f6af2 100644 --- a/oneflow/python/nn/modules/acosh.py +++ b/oneflow/python/nn/modules/acosh.py @@ -33,7 +33,7 @@ def forward(self, x): @experimental_api def acosh_op(x): r"""Returns a new tensor with the inverse hyperbolic cosine of the elements of :attr:`input`. - + .. math:: \text{out}_{i} = \cosh^{-1}(\text{input}_{i}) @@ -70,7 +70,7 @@ def acosh_op_tensor(x): acosh() -> Tensor See :func:`oneflow.experimental.acosh` - + """ return Acosh()(x) @@ -82,7 +82,7 @@ def arccosh_op(x): r""" See :func:`oneflow.experimental.acosh` - + """ return Acosh()(x) @@ -96,7 +96,7 @@ def arccosh_op_tensor(x): arccosh() -> Tensor See :func:`oneflow.experimental.acosh` - + """ return Acosh()(x) @@ -105,4 +105,4 @@ def arccosh_op_tensor(x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/activation.py b/oneflow/python/nn/modules/activation.py index 42175e58c2c..e75065df1c1 100644 --- a/oneflow/python/nn/modules/activation.py +++ b/oneflow/python/nn/modules/activation.py @@ -462,7 +462,7 @@ class Hardsigmoid(Module): >>> out = hardsigmoid(input).numpy() >>> print(out) [0.41666666 0.5 0.5833333 ] - + """ @@ -759,7 +759,7 @@ class Hardswish(Module): >>> out = hardswish(input).numpy() >>> print(out) [-0.20833333 0. 0.29166666] - + .. _`Searching for MobileNetV3`: https://arxiv.org/abs/1905.02244 """ @@ -812,7 +812,7 @@ class Hardtanh(Module): >>> import numpy as np >>> import oneflow.experimental as flow >>> flow.enable_eager_execution() - + >>> m = flow.nn.Hardtanh() >>> arr = np.array([0.2, 0.3, 3.0, 4.0]) >>> x = flow.Tensor(arr) @@ -914,4 +914,4 @@ def forward(self, x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/arange.py b/oneflow/python/nn/modules/arange.py index 7fe02a33246..e11b67a6554 100644 --- a/oneflow/python/nn/modules/arange.py +++ b/oneflow/python/nn/modules/arange.py @@ -87,9 +87,9 @@ def arange_op( device(flow.device, optional): the desired device of returned tensor. Default: if None, uses the current device for the default tensor. requires_grad(bool, optional): If autograd should record operations on the returned tensor. Default: `False`. - For example: + For example: - .. code-block:: python + .. code-block:: python >>> import oneflow.experimental as flow >>> flow.enable_eager_execution() @@ -108,4 +108,4 @@ def arange_op( if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/argmax.py b/oneflow/python/nn/modules/argmax.py index d5cbf60f8f1..b22eebe1996 100644 --- a/oneflow/python/nn/modules/argmax.py +++ b/oneflow/python/nn/modules/argmax.py @@ -88,7 +88,7 @@ def argmax_op(input, dim: int = None, keepdim: bool = False): For example: - .. code-block:: python + .. code-block:: python >>> import numpy as np >>> import oneflow.experimental as flow @@ -111,4 +111,4 @@ def argmax_op(input, dim: int = None, keepdim: bool = False): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/atan2.py b/oneflow/python/nn/modules/atan2.py index 9391be1335f..22d87ed76dd 100644 --- a/oneflow/python/nn/modules/atan2.py +++ b/oneflow/python/nn/modules/atan2.py @@ -41,7 +41,7 @@ def forward(self, x, y): @experimental_api def atan2_op(input, other): r"""Element-wise arctangent of input{i}/other{i} - with consideration of the quadrant. Returns a new tensor with the signed + with consideration of the quadrant. Returns a new tensor with the signed angles in radians between vector (other{i},input{i}) and vector (1, 0). The shapes of input and other must be broadcastable. @@ -57,14 +57,14 @@ def atan2_op(input, other): >>> import oneflow.experimental as flow >>> import numpy as np - + >>> x1 = flow.Tensor(np.array([1,2,3])) >>> y1 = flow.Tensor(np.array([3,2,1])) >>> x2 = flow.Tensor(np.array([1.53123589,0.54242598,0.15117185])) >>> y2 = flow.Tensor(np.array([-0.21906378,0.09467151,-0.75562878])) >>> x3 = flow.Tensor(np.array([1,0,-1])) >>> y3 = flow.Tensor(np.array([0,1,0])) - + >>> flow.enable_eager_execution() >>> flow.atan2(x1,y1).numpy() array([0.32175055, 0.7853982 , 1.2490457 ], dtype=float32) @@ -92,4 +92,4 @@ def atan2_op_tensor(input, other): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/batchnorm.py b/oneflow/python/nn/modules/batchnorm.py index e2610211ef6..07657a09883 100644 --- a/oneflow/python/nn/modules/batchnorm.py +++ b/oneflow/python/nn/modules/batchnorm.py @@ -363,4 +363,4 @@ def _check_input_dim(self, input): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/cast.py b/oneflow/python/nn/modules/cast.py index d03fd87851e..f7eea305860 100644 --- a/oneflow/python/nn/modules/cast.py +++ b/oneflow/python/nn/modules/cast.py @@ -68,4 +68,4 @@ def cast_op(x, dtype): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/concat.py b/oneflow/python/nn/modules/concat.py index 1c0de3dfbd7..5a14da7a35c 100644 --- a/oneflow/python/nn/modules/concat.py +++ b/oneflow/python/nn/modules/concat.py @@ -61,7 +61,7 @@ def concat_op(inputs, dim=0): Args: inputs: a `list` of `Tensor` - dim: a `int`. + dim: a `int`. Returns: A `Tensor` @@ -90,4 +90,4 @@ def concat_op(inputs, dim=0): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/constant.py b/oneflow/python/nn/modules/constant.py index a4fb90a045a..5d33ce5e0a2 100644 --- a/oneflow/python/nn/modules/constant.py +++ b/oneflow/python/nn/modules/constant.py @@ -212,4 +212,4 @@ def ones_like_op(other): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/container.py b/oneflow/python/nn/modules/container.py index c34afae3684..b2f4f0c0a7d 100644 --- a/oneflow/python/nn/modules/container.py +++ b/oneflow/python/nn/modules/container.py @@ -534,4 +534,4 @@ def _replicate_for_data_parallel(self): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/dropout.py b/oneflow/python/nn/modules/dropout.py index a2e4f7a7c68..29cf42f50e1 100644 --- a/oneflow/python/nn/modules/dropout.py +++ b/oneflow/python/nn/modules/dropout.py @@ -65,9 +65,9 @@ class Dropout(_DropoutNd): - Input: :math:`(*)`. Input can be of any shape - Output: :math:`(*)`. Output is of the same shape as input - For example: + For example: - .. code-block:: python + .. code-block:: python >>> import numpy as np >>> import oneflow.experimental as flow @@ -127,4 +127,4 @@ def forward(self, x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/eq.py b/oneflow/python/nn/modules/eq.py index d28c26d7db6..a65c7d6f9bf 100644 --- a/oneflow/python/nn/modules/eq.py +++ b/oneflow/python/nn/modules/eq.py @@ -57,7 +57,7 @@ def eq_op(input, other): other (oneflow.Tensor): the tensor to compare Returns: - + - A boolean tensor that is True where :attr:`input` is equal to :attr:`other` and False elsewhere For example: @@ -82,4 +82,4 @@ def eq_op(input, other): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/exp.py b/oneflow/python/nn/modules/exp.py index 947b8236234..4a3b9a3a482 100644 --- a/oneflow/python/nn/modules/exp.py +++ b/oneflow/python/nn/modules/exp.py @@ -34,9 +34,9 @@ def forward(self, x): def exp_op(x): """This operator computes the exponential of Tensor. - The equation is: + The equation is: - .. math:: + .. math:: out = e^x @@ -46,9 +46,9 @@ def exp_op(x): Returns: oneflow.Tensor: The result Tensor - For example: + For example: - .. code-block:: python + .. code-block:: python >>> import numpy as np >>> import oneflow.experimental as flow @@ -66,4 +66,4 @@ def exp_op(x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/expand.py b/oneflow/python/nn/modules/expand.py index 80309279f8c..b41527cdcfe 100644 --- a/oneflow/python/nn/modules/expand.py +++ b/oneflow/python/nn/modules/expand.py @@ -66,21 +66,21 @@ def forward(self, x): @experimental_api def expand_op(x, *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. + 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. Args: - x (oneflow.Tensor): The input Tensor. + x (oneflow.Tensor): The input Tensor. *sizes (flow.Size or int): The desired expanded size. Returns: - oneflow.Tensor: The result Tensor. + oneflow.Tensor: The result Tensor. - For example: + For example: .. code-block:: python @@ -104,4 +104,4 @@ def expand_op(x, *sizes): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/gather.py b/oneflow/python/nn/modules/gather.py index 84c9e94dcb2..9c477a95ae7 100644 --- a/oneflow/python/nn/modules/gather.py +++ b/oneflow/python/nn/modules/gather.py @@ -71,7 +71,7 @@ def gather_op(input, index, dim=0, sparse_grad=False): 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 :attr:`input` and :attr:`index` must have the same number of dimensions. @@ -83,11 +83,11 @@ def gather_op(input, index, dim=0, sparse_grad=False): input (Tensor): the source tensor dim (int): the axis along which to index index (LongTensor): the indices of elements to gather - + For example: .. code-block:: python - + >>> import oneflow.experimental as flow >>> import numpy as np >>> flow.enable_eager_execution() @@ -105,4 +105,4 @@ def gather_op(input, index, dim=0, sparse_grad=False): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/greater.py b/oneflow/python/nn/modules/greater.py index 0125af8a235..5c5cd816c48 100644 --- a/oneflow/python/nn/modules/greater.py +++ b/oneflow/python/nn/modules/greater.py @@ -53,18 +53,18 @@ def greater_op(x, y): For example: .. code-block:: python - + >>> import numpy as np >>> import oneflow.experimental as flow >>> flow.enable_eager_execution() >>> input1 = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) >>> input2 = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) - + >>> out = flow.gt(input1, input2).numpy().shape >>> print(out) (2, 6, 5, 3) - + """ return Greater()(x, y) @@ -77,7 +77,7 @@ def greater_op_tensor(x, y): gt() -> Tensor See :func:`oneflow.experimental.gt` - + """ return Greater()(x, y) @@ -85,4 +85,4 @@ def greater_op_tensor(x, y): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/less.py b/oneflow/python/nn/modules/less.py index 70da9b48e62..9ce6fdaa739 100644 --- a/oneflow/python/nn/modules/less.py +++ b/oneflow/python/nn/modules/less.py @@ -54,10 +54,10 @@ def less_op(x, y): >>> 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).numpy() >>> print(out) [0 0 1] @@ -69,4 +69,4 @@ def less_op(x, y): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/linear.py b/oneflow/python/nn/modules/linear.py index 0ab2165a854..f3de08c9255 100644 --- a/oneflow/python/nn/modules/linear.py +++ b/oneflow/python/nn/modules/linear.py @@ -31,9 +31,9 @@ class Identity(Module): args: any argument (unused) kwargs: any keyword argument (unused) - For example: + For example: - .. code-block:: python + .. code-block:: python import numpy as np import oneflow as flow @@ -62,7 +62,7 @@ class Linear(Module): This module supports :ref:`TensorFloat32`. Args: - + - in_features: size of each input sample - out_features: size of each output sample @@ -72,19 +72,19 @@ class Linear(Module): Shape: - Input: :math:`(N, *, H_{in})` where :math:`*` means any number of additional dimensions and :math:`H_{in} = {in\_features}` - + - Output: :math:`(N, *, H_{out})` where all but the last dimension are the same shape as the input and :math:`H_{out} = {out\_features}`. Attr: - :attr:`weight`: the learnable weights of the module of shape :math:`({out\_features}, {in\_features})`. The values are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})`, where :math:`(k = 1 / {in\_features})` - + - :attr:`bias`: the learnable bias of the module of shape :math:`({out\_features})`. If :attr:`bias` is ``True``, the values are initialized from :math:`\mathcal{U}(-\sqrt{k}, \sqrt{k})` where :math:`(k = 1 / {in\_features})` - - For example: - .. code-block:: python + For example: + + .. code-block:: python >>> import numpy as np >>> import oneflow.experimental as flow @@ -158,4 +158,4 @@ def forward(self, x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/log1p.py b/oneflow/python/nn/modules/log1p.py index fef90b9aff2..320bc8f6af6 100644 --- a/oneflow/python/nn/modules/log1p.py +++ b/oneflow/python/nn/modules/log1p.py @@ -35,7 +35,7 @@ def forward(self, x): @experimental_api def log1p_op(input): r"""Returns a new tensor with the natural logarithm of (1 + input). - + .. math:: \text{out}_{i}=\log_e(1+\text{input}_{i}) @@ -50,7 +50,7 @@ def log1p_op(input): >>> out = flow.log1p(x).numpy() >>> out array([0.8329091 , 0.91629076, 1.3083328 ], dtype=float32) - + """ return Log1p()(input) @@ -58,4 +58,4 @@ def log1p_op(input): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/loss.py b/oneflow/python/nn/modules/loss.py index 7eb4f44910e..abdc723ac2c 100644 --- a/oneflow/python/nn/modules/loss.py +++ b/oneflow/python/nn/modules/loss.py @@ -26,7 +26,7 @@ class CrossEntropyLoss(Module): r"""This criterion combines :class:`~flow.nn.LogSoftmax` and :class:`~flow.nn.NLLLoss` in one single class. It is useful when training a classification problem with `C` classes. - + The `input` is expected to contain raw, unnormalized scores for each class. `input` has to be a Tensor of size either :math:`(minibatch, C)` or @@ -34,7 +34,7 @@ class CrossEntropyLoss(Module): with :math:`K \geq 1` for the `K`-dimensional case (described later). This criterion expects a class index in the range :math:`[0, C-1]` as the - `target` for each value of a 1D tensor of size `minibatch`; + `target` for each value of a 1D tensor of size `minibatch`; The loss can be described as: @@ -78,7 +78,7 @@ class CrossEntropyLoss(Module): >>> out_mean = flow.nn.CrossEntropyLoss(reduction="mean")(input, target) >>> print(out_mean.numpy()) [0.75896907] - + """ @@ -162,7 +162,7 @@ class NLLLoss(Module): layer. The `target` that this loss expects should be a class index in the range :math:`[0, C-1]` - where `C = number of classes`; + where `C = number of classes`; The unreduced (i.e. with :attr:`reduction` set to ``'none'``) loss can be described as: @@ -196,11 +196,11 @@ class NLLLoss(Module): and :attr:`reduce` are in the process of being deprecated, and in the meantime, specifying either of those two args will override :attr:`reduction`. Default: ``'mean'`` - + For example: - .. code-block:: python - + .. code-block:: python + >>> import oneflow.experimental as flow >>> flow.enable_eager_execution() >>> import numpy as np @@ -219,12 +219,12 @@ class NLLLoss(Module): >>> out = m(input, target).numpy() >>> print(out) [-1.1355073] - + >>> m = flow.nn.NLLLoss(reduction="mean") >>> out = m(input, target).numpy() >>> print(out) [-0.37850246] - + """ def __init__( @@ -299,4 +299,4 @@ def forward(self, input, target): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/masked_fill.py b/oneflow/python/nn/modules/masked_fill.py index 74e1e7fa7e1..04e948760f3 100644 --- a/oneflow/python/nn/modules/masked_fill.py +++ b/oneflow/python/nn/modules/masked_fill.py @@ -86,4 +86,4 @@ def masked_fill_op(tensor, mask, value): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/math_ops.py b/oneflow/python/nn/modules/math_ops.py index ec8a06bcbfa..b33dca44edd 100644 --- a/oneflow/python/nn/modules/math_ops.py +++ b/oneflow/python/nn/modules/math_ops.py @@ -56,7 +56,7 @@ def forward(self, input): @experimental_api def _sum(input, dim=None, keepdims=False): r"""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: .. code-block:: python @@ -139,12 +139,12 @@ def forward(self, x, y): @experimental_api def _mul(x, y): r"""Computes the multiplication of x by y for each element, scalar and broadcast promotation are supported. - + The formula is: .. math:: out = x \times y - + For example: .. code-block:: python @@ -290,8 +290,8 @@ def forward(self, input): def variance_op(input, dim=None, keepdim=False): r"""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 + 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). Args: @@ -381,7 +381,7 @@ def _sub(x, y): .. math:: out = x - y - + For example: .. code-block:: python @@ -456,11 +456,11 @@ def _div(x, y): .. math:: out = \frac{X}{Y} - + Args: x (Union[int, float, flow.Tensor]): X. y (Union[int, float, flow.Tensor]): Y. - + For example: .. code-block:: python @@ -675,7 +675,7 @@ def asin_op_tensor(input): @experimental_api def arcsin_op(input): r""" - + Alias for :func:`oneflow.experimental.asin` """ return Asin()(input) @@ -716,7 +716,7 @@ def asinh_op(input): >>> import oneflow.experimental as flow >>> import numpy as np - >>> flow.enable_eager_execution() + >>> flow.enable_eager_execution() >>> input = flow.Tensor(np.array([2, 3, 4]), dtype=flow.float32) >>> output = flow.asinh(input) >>> print(output.shape) @@ -740,7 +740,7 @@ def asinh_op(input): @experimental_api def arcsinh_op(input): r""" - + Alias for :func:`oneflow.experimental.asinh` """ return Asinh()(input) @@ -817,7 +817,7 @@ def sin_op_tensor(tensor): sin() -> Tensor See :func:`oneflow.experimental.sin` - + """ return Sin()(tensor) @@ -838,7 +838,7 @@ def forward(self, x): def cos_op(tensor): r""" Returns a new tensor with the cosine of the elements of :attr:`input`. - + .. math:: \text{out}_{i} = \cos(\text{input}_{i}) Args: @@ -854,7 +854,7 @@ def cos_op(tensor): input = flow.Tensor(arr, dtype=flow.float32) output = flow.cos(input) # [0.13944048 0.29570782 0.6553126 0.5573547 ] - + """ return Cos()(tensor) @@ -874,12 +874,12 @@ def forward(self, x): def log_op(tensor): r""" Returns a new tensor with the natural logarithm of the elements of :attr:`input`. - + .. math:: y_{i} = \log_{e} (x_{i}) Args: input (Tensor): the input tensor. - + For example: .. code-block:: python @@ -890,7 +890,7 @@ def log_op(tensor): input = flow.Tensor(arr, dtype=flow.float32) output = flow.log(input) # equal to np.log(input) - + """ return Log()(tensor) @@ -1064,8 +1064,8 @@ def std_op(tensor, dim, unbiased=True, keepdim=False): dimension :attr:`dim`. If :attr:`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, + 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 :attr:`unbiased` is ``False``, then the standard-deviation will be calculated @@ -1115,18 +1115,18 @@ def forward(self, x, y): def pow_op(tensor, exponent): r"""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 or a single int number. - + For example: .. code-block:: python import oneflow.experimental as flow import numpy as np - + x = flow.Tensor(np.array([1, 2, 3, 4, 5, 6])) out = flow.pow(x, 2).numpy() print(out) # [1, 4, 9, 16, 25, 36] - + """ return Pow()(tensor, exponent) @@ -1170,4 +1170,4 @@ def cosh_op(tensor): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/matmul.py b/oneflow/python/nn/modules/matmul.py index d153faf45b3..8fd27001d21 100644 --- a/oneflow/python/nn/modules/matmul.py +++ b/oneflow/python/nn/modules/matmul.py @@ -89,7 +89,7 @@ def matmul_op(a, b): Returns: oneflow.Tensor: The result Tensor - For example: + For example: .. code-block:: python @@ -109,4 +109,4 @@ def matmul_op(a, b): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/negative.py b/oneflow/python/nn/modules/negative.py index 7b1377ae7a6..fc6532b8747 100644 --- a/oneflow/python/nn/modules/negative.py +++ b/oneflow/python/nn/modules/negative.py @@ -62,4 +62,4 @@ def negative_op(x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/normalization.py b/oneflow/python/nn/modules/normalization.py index 17a5aac5f85..c6f5dd7733d 100644 --- a/oneflow/python/nn/modules/normalization.py +++ b/oneflow/python/nn/modules/normalization.py @@ -28,17 +28,17 @@ class LayerNorm(Module): r"""Applies Layer Normalization over a mini-batch of inputs as described in the paper `Layer Normalization `__ - + .. math:: y = \frac{x - \mathrm{E}[x]}{ \sqrt{\mathrm{Var}[x] + \epsilon}} * \gamma + \beta - + The mean and standard-deviation are calculated separately over the last certain number dimensions which have to be of the shape specified by :attr:`normalized_shape`. :math:`\gamma` and :math:`\beta` are learnable affine transform parameters of :attr:`normalized_shape` if :attr:`elementwise_affine` is ``True``. The standard-deviation is calculated via the biased estimator. - + .. note:: Unlike Batch Normalization and Instance Normalization, which applies scalar scale and bias for each entire channel/plane with the @@ -46,17 +46,17 @@ class LayerNorm(Module): bias with :attr:`elementwise_affine`. This layer uses statistics computed from input data in both training and evaluation modes. - + Args: normalized_shape (int or list or oneflow.Size): input shape from an expected input of size .. math:: - [* \times \text{normalized_shape}[0] \times \text{normalized_shape}[1] \times \ldots \times \text{normalized_shape}[-1]] - + [* \times \text{normalized_shape}[0] \times \text{normalized_shape}[1] \times \ldots \times \text{normalized_shape}[-1]] + If a single integer is used, it is treated as a singleton list, and this module will - + normalize over the last dimension which is expected to be of that specific size. - + eps: a value added to the denominator for numerical stability. Default: 1e-5 elementwise_affine: a boolean value that when set to ``True``, this module has learnable per-element affine parameters initialized to ones (for weights) @@ -64,10 +64,10 @@ class LayerNorm(Module): Shape: - Input: :math:`(N, *)` - Output: :math:`(N, *)` (same shape as input) - - For example: - .. code-block:: python + For example: + + .. code-block:: python >>> import numpy as np >>> import oneflow.experimental as flow @@ -248,4 +248,4 @@ def extra_repr(self) -> str: if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/permute.py b/oneflow/python/nn/modules/permute.py index a619e2ec8ed..1e4cdac869a 100644 --- a/oneflow/python/nn/modules/permute.py +++ b/oneflow/python/nn/modules/permute.py @@ -53,7 +53,7 @@ def permute_op(tensor, *dims): Args: *dims (int...): The desired ordering of dimensions - + For example: .. code-block:: python @@ -74,4 +74,4 @@ def permute_op(tensor, *dims): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/pooling.py b/oneflow/python/nn/modules/pooling.py index a855e677d9b..06bd85e2013 100644 --- a/oneflow/python/nn/modules/pooling.py +++ b/oneflow/python/nn/modules/pooling.py @@ -153,30 +153,6 @@ class MaxPool1d(Module): L_{out} = \left\lfloor \frac{L_{in} + 2 \times \text{padding} - \text{dilation} \times (\text{kernel_size} - 1) - 1}{\text{stride}} + 1\right\rfloor - For example: - - .. code-block:: python - - >>> import oneflow.experimental as flow - >>> import numpy as np - >>> flow.enable_eager_execution() - - >>> kernel_size, stride, padding = 2, 1, 2 - >>> m = flow.nn.MaxPool1d(kernel_size, stride, padding) - >>> np.random.seed(0) - >>> x = flow.Tensor(np.random.rand(1, 1, 5)) - >>> y = m(x) - >>> print(y.numpy()) - [[[-3.4028235e+38 5.4881352e-01 7.1518934e-01 7.1518934e-01 - 6.0276335e-01 5.4488319e-01 4.2365479e-01 -3.4028235e+38]]] - - >>> kernel_size, stride, padding = 3, 5, 2 - >>> m = flow.nn.MaxPool1d(kernel_size, stride, padding) - >>> x = flow.Tensor(np.random.randn(9, 7, 20)) - >>> y = m(x) - >>> print(y.size()) - flow.Size([9, 7, 5]) - """ def __init__( @@ -466,4 +442,4 @@ def forward(self, x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/prelu.py b/oneflow/python/nn/modules/prelu.py index a8f7d8c2486..43968c593ac 100644 --- a/oneflow/python/nn/modules/prelu.py +++ b/oneflow/python/nn/modules/prelu.py @@ -60,10 +60,10 @@ class PReLU(Module): >>> flow.enable_eager_execution() >>> m = flow.nn.PReLU() - >>> input = flow.Tensor(np.random.randn(2, 6, 5, 3), dtype=flow.float32) - >>> out = m(input).numpy().shape - >>> print(out) - (6, 2, 5, 3) + >>> input = flow.Tensor(np.asarray([[[[1, -2], [3, 4]]]]), dtype=flow.float32) + >>> print(m(input).numpy()) + [[[[ 1. -0.5] + [ 3. 4. ]]]] """ @@ -83,4 +83,4 @@ def forward(self, x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/repeat.py b/oneflow/python/nn/modules/repeat.py index 560bde6f2ae..5d0319b6e42 100644 --- a/oneflow/python/nn/modules/repeat.py +++ b/oneflow/python/nn/modules/repeat.py @@ -68,13 +68,13 @@ def repeat_op(x, sizes): """This operator repeat the input tensor to a larger size along the specified dimensions. Args: - x (oneflow.Tensor): The input Tensor. + x (oneflow.Tensor): The input Tensor. size (Sequence[int]): The number of times to repeat this tensor along each dimension Returns: - oneflow.Tensor: The result Tensor. + oneflow.Tensor: The result Tensor. - For example: + For example: .. code-block:: python @@ -97,4 +97,4 @@ def repeat_op(x, sizes): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/reshape.py b/oneflow/python/nn/modules/reshape.py index 9d5ae6031a0..6fa18280e0d 100644 --- a/oneflow/python/nn/modules/reshape.py +++ b/oneflow/python/nn/modules/reshape.py @@ -95,4 +95,4 @@ def reshape_op(x, shape: Sequence[int] = None): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/round.py b/oneflow/python/nn/modules/round.py index 82e4d1e8b1c..726926379e8 100644 --- a/oneflow/python/nn/modules/round.py +++ b/oneflow/python/nn/modules/round.py @@ -38,11 +38,11 @@ def round_op(x): Returns: oneflow.Tensor: The result Tensor For example: - + .. code-block:: python >>> import oneflow.experimental as flow - >>> import numpy as np + >>> import numpy as np >>> flow.enable_eager_execution() >>> x1 = flow.Tensor(np.array([1.49999, 1.500001, 2.7]).astype(np.float32)) >>> out1 = flow.round(x1) @@ -52,7 +52,7 @@ def round_op(x): >>> out2 = flow.round(x2) >>> out2.numpy() array([2., 8., 5., 7.], dtype=float32) - + """ return Round()(x) @@ -63,9 +63,9 @@ def round_op(x): def round_op_tensor(x): r""" round() -> Tensor - + See :func:`oneflow.experimental.round` - + """ return Round()(x) @@ -74,4 +74,4 @@ def round_op_tensor(x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/sign.py b/oneflow/python/nn/modules/sign.py index 8300bdcc1c9..5e83b2d1971 100644 --- a/oneflow/python/nn/modules/sign.py +++ b/oneflow/python/nn/modules/sign.py @@ -34,27 +34,27 @@ def forward(self, x): def sign_op(x): r"""Computes the sign of Tensor. - .. math:: + .. math:: \text{out}_{i} = \text{sgn}(\text{input}_{i}) Args: input (Tensor): the input tensor. - For example: + For example: - .. code-block:: python + .. code-block:: python >>> import oneflow.experimental as flow >>> import numpy as np >>> flow.enable_eager_execution() >>> x1 = flow.Tensor(np.array([-2, 0, 2]).astype(np.float32)) >>> out1 = flow.sign(x1) - >>> out1.numpy() + >>> out1.numpy() array([-1., 0., 1.], dtype=float32) >>> x2 = flow.Tensor(np.array([-3.2, -4.5, 5.8]).astype(np.float32),device=flow.device('cuda')) >>> out2 = flow.sign(x2) - >>> out2.numpy() + >>> out2.numpy() array([-1., -1., 1.], dtype=float32) """ @@ -69,7 +69,7 @@ def sign_op_tensor(x): sign() -> Tensor See :func:`oneflow.experimental.sign` - + """ return Sign()(x) @@ -78,4 +78,4 @@ def sign_op_tensor(x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/sinh.py b/oneflow/python/nn/modules/sinh.py index 1f5cb263c0d..82100990b4e 100644 --- a/oneflow/python/nn/modules/sinh.py +++ b/oneflow/python/nn/modules/sinh.py @@ -35,13 +35,13 @@ def sinh_op(x): .. math:: \text{out}_{i} = \sinh(\text{input}_{i}) - + Args: input (Tensor): the input tensor. - For example: + For example: - .. code-block:: python + .. code-block:: python >>> import numpy as np >>> import oneflow.experimental as flow @@ -80,4 +80,4 @@ def sinh_op_tensor(x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/softplus.py b/oneflow/python/nn/modules/softplus.py index ef472b8c43a..545d27b6440 100644 --- a/oneflow/python/nn/modules/softplus.py +++ b/oneflow/python/nn/modules/softplus.py @@ -46,9 +46,9 @@ def softplus_op(x): threshold:values above this revert to a linear function.Default:20 - For example: + For example: - .. code-block:: python + .. code-block:: python >>> import numpy as np >>> import oneflow.experimental as flow @@ -72,4 +72,4 @@ def softplus_op(x): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/squeeze.py b/oneflow/python/nn/modules/squeeze.py index 5ab87af15c8..ed11eb2a1b5 100644 --- a/oneflow/python/nn/modules/squeeze.py +++ b/oneflow/python/nn/modules/squeeze.py @@ -75,4 +75,4 @@ def squeeze_op(input, dim: Optional[Sequence[int]] = None): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/transpose.py b/oneflow/python/nn/modules/transpose.py index ba3d62d4ea5..f19aea4697a 100644 --- a/oneflow/python/nn/modules/transpose.py +++ b/oneflow/python/nn/modules/transpose.py @@ -80,7 +80,7 @@ def transpose_op(tensor, dim0, dim1): dim1 (int): the second dimension to be transposed. Returns: Tensor: A transposed tensor. - + For example: .. code-block:: python @@ -93,7 +93,7 @@ def transpose_op(tensor, dim0, dim1): >>> out = flow.transpose(input, 0, 1).numpy().shape >>> print(out) (6, 2, 5, 3) - + """ return Transpose(dim0=dim0, dim1=dim1)(tensor) @@ -101,4 +101,4 @@ def transpose_op(tensor, dim0, dim1): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/upsample.py b/oneflow/python/nn/modules/upsample.py index ff852298076..a86c743cc19 100644 --- a/oneflow/python/nn/modules/upsample.py +++ b/oneflow/python/nn/modules/upsample.py @@ -45,11 +45,11 @@ class Upsample(Module): Default: ``'nearest'`` align_corners (bool, optional): if ``True``, the corner pixels of the input and output tensors are aligned, and thus preserving the values at - those pixels. This only has effect when :attr:`mode` is ``'bilinear'``. + those pixels. This only has effect when :attr:`mode` is ``'bilinear'``. Default: ``False`` Shape: - - Input: : :math:`(N, C, H_{in}, W_{in})` + - Input: : :math:`(N, C, H_{in}, W_{in})` - Output: :math:`(N, C, H_{out}, W_{out})` , where .. math:: @@ -63,7 +63,7 @@ class Upsample(Module): .. note:: If you want downsampling/general resizing, you should use :func:`~nn.functional.interpolate`. - + For example: .. code-block:: python @@ -71,7 +71,7 @@ class Upsample(Module): >>> import numpy as np >>> import oneflow.experimental as flow >>> flow.enable_eager_execution() - + >>> input = flow.Tensor(np.arange(1, 5).reshape((1, 1, 2, 2)), dtype=flow.float32) >>> input = input.to("cuda") >>> m = flow.nn.Upsample(scale_factor=2.0, mode="nearest") @@ -81,7 +81,7 @@ class Upsample(Module): [1. 1. 2. 2.] [3. 3. 4. 4.] [3. 3. 4. 4.]]]] - + """ def __init__( @@ -192,7 +192,7 @@ class UpsamplingNearest2d(Upsample): >>> import numpy as np >>> import oneflow.experimental as flow >>> flow.enable_eager_execution() - + >>> input = flow.Tensor(np.arange(1, 5).reshape((1, 1, 2, 2)), dtype=flow.float32) >>> input = input.to("cuda") >>> m = flow.nn.UpsamplingNearest2d(scale_factor=2.0) @@ -202,7 +202,7 @@ class UpsamplingNearest2d(Upsample): [1. 1. 2. 2.] [3. 3. 4. 4.] [3. 3. 4. 4.]]]] - + """ def __init__( @@ -250,7 +250,7 @@ class UpsamplingBilinear2d(Upsample): >>> import numpy as np >>> import oneflow.experimental as flow >>> flow.enable_eager_execution() - + >>> input = flow.Tensor(np.arange(1, 5).reshape((1, 1, 2, 2)), dtype=flow.float32) >>> input = input.to("cuda") >>> m = flow.nn.UpsamplingBilinear2d(scale_factor=2.0) @@ -260,7 +260,7 @@ class UpsamplingBilinear2d(Upsample): [1.6666667 2. 2.3333335 2.6666667] [2.3333335 2.6666667 3. 3.3333335] [3. 3.3333333 3.6666667 4. ]]]] - + """ def __init__( @@ -276,4 +276,4 @@ def __init__( if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True) diff --git a/oneflow/python/nn/modules/where.py b/oneflow/python/nn/modules/where.py index 4c9aec9a9a0..cd7cb7375a9 100644 --- a/oneflow/python/nn/modules/where.py +++ b/oneflow/python/nn/modules/where.py @@ -101,7 +101,7 @@ def forward(self, condition, x, y): def where_op(condition, x, y): """Return a tensor of elements selected from either :attr:`x` or :attr:`y`, depending on :attr:`condition`. If the element in condition is larger than 0, - + it will take the `x` element, else it will take the `y` element .. note:: @@ -118,7 +118,7 @@ def where_op(condition, x, y): where :attr:`condition` is False Returns: Tensor: A tensor of shape equal to the broadcasted shape of :attr:`condition`, :attr:`x`, :attr:`y` - + For example: .. code-block:: python @@ -138,7 +138,7 @@ def where_op(condition, x, y): [[1. 0.3139] [0.3898 1. ] [0.0478 1. ]] - + """ return Where()(condition, x, y) @@ -146,4 +146,4 @@ def where_op(condition, x, y): if __name__ == "__main__": import doctest - doctest.testmod() + doctest.testmod(raise_on_error=True)