From 8c97652d82ab922a49992c9fd8bfec44736f4b03 Mon Sep 17 00:00:00 2001 From: YaoChi Date: Wed, 15 Dec 2021 21:05:06 +0800 Subject: [PATCH 01/10] 80% Sbp signature left to finish --- oneflow/core/functional/functional_api.yaml | 18 +-- oneflow/core/functional/impl/eye_functor.cpp | 121 ++++++++++++++++++ oneflow/core/functional/impl/math_functor.cpp | 49 ------- python/oneflow/__init__.py | 2 +- python/oneflow/nn/modules/eye.py | 56 +++++--- 5 files changed, 169 insertions(+), 77 deletions(-) create mode 100644 oneflow/core/functional/impl/eye_functor.cpp diff --git a/oneflow/core/functional/functional_api.yaml b/oneflow/core/functional/functional_api.yaml index dab46b79186..db7c44731bd 100755 --- a/oneflow/core/functional/functional_api.yaml +++ b/oneflow/core/functional/functional_api.yaml @@ -529,18 +529,20 @@ bind_python: False - name: "eye" - signature: - [ - "Tensor (Scalar n, Scalar m=None, *, DataType dtype=None, Device device=None) => Eye", + signature: [ + "Tensor (Scalar n, Scalar m=None, *, DataType dtype=kFloat, Device device=None, Bool requires_grad=False) => Eye", + "Tensor (Scalar n, Scalar m=None, *, DataType dtype=kFloat, String device, Bool requires_grad=False) => Eye", + "Tensor (Scalar n, Scalar m=None, *, DataType dtype=kFloat, Bool requires_grad=False, Placement placement=None, SbpList sbp=None) => Eye", ] bind_python: True +- name: "physical_eye" + signature: "Tensor (Scalar n, Scalar m=None, *, DataType dtype=None, Device device=None) => PhysicalEye" + bind_python: false + - name: "consistent_eye" - signature: - [ - "Tensor (Scalar n, Scalar m=None, *, DataType dtype=None, Placement placement, SbpList sbp) => ConsistentEye", - ] - bind_python: True + signature: "Tensor (Scalar n, Scalar m=None, *, DataType dtype=None, Placement placement, SbpList sbp) => ConsistentEye" + bind_python: false - name: "arange" signature: [ diff --git a/oneflow/core/functional/impl/eye_functor.cpp b/oneflow/core/functional/impl/eye_functor.cpp new file mode 100644 index 00000000000..ec18775f773 --- /dev/null +++ b/oneflow/core/functional/impl/eye_functor.cpp @@ -0,0 +1,121 @@ +/* +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 + + http://www.apache.org/licenses/LICENSE-2.0 + +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. +*/ + +#include "oneflow/core/common/scalar.h" +#include "oneflow/core/framework/attr_map.h" +#include "oneflow/core/framework/nd_sbp.h" +#include "oneflow/core/framework/op_builder.h" +#include "oneflow/core/framework/op_expr.h" +#include "oneflow/core/framework/op_interpreter/op_interpreter_util.h" +#include "oneflow/core/framework/tensor.h" +#include "oneflow/core/framework/tensor_tuple.h" +#include "oneflow/core/functional/functional.h" +#include "oneflow/core/functional/function_library.h" +#include "oneflow/core/functional/functional_api.yaml.h" +#include "oneflow/core/functional/impl/common.h" +#include "oneflow/core/functional/impl/unary_functor.h" +#include "oneflow/core/job/lazy_mode.h" +#include "oneflow/core/job/sbp_parallel.h" +#include "oneflow/core/functional/tensor_processor.h" +#include "oneflow/api/common/device.h" + +namespace oneflow { +namespace one { +namespace functional { + +namespace impl { + +class EyeDevcieFunctor { + public: + EyeDevcieFunctor() { op_ = CHECK_JUST(one::OpBuilder("eye").Output("out").Build()); } + Maybe operator()(const Scalar& rows, const Optional& cols, + const Symbol& dtype, const Optional>& device, + const bool& requies_grad) const { + MutableAttrMap attrs; + JUST(attrs.SetAttr("rows", JUST(rows.As()))); + JUST(attrs.SetAttr("cols", JUST(cols.value_or(rows).As()))); + JUST(attrs.SetAttr("dtype", dtype->data_type())); + OpExprInterpContext ctx(attrs); + ctx.device = device; + return OpInterpUtil::Dispatch(*op_, {}, ctx); + } + + private: + std::shared_ptr op_; +}; + +class EyeDeviceStrFunctor { + public: + Maybe operator()(const Scalar& rows, const Optional& cols, + const Symbol& dtype, const std::string& device, + const bool& requires_grad) const { + const Symbol& dev = JUST(DeviceExportUtil::ParseAndNew(device)); + return JUST(functional::Eye(rows, cols, dtype, dev, requires_grad)); + } +}; + +class ConsistentEyeSbpListFunctor { + public: + ConsistentEyeSbpListFunctor() { op_ = CHECK_JUST(one::OpBuilder("eye").Output("out").Build()); } + Maybe operator()(const Scalar& rows, const Optional& cols, + const Symbol& dtype, const bool& requires_grad, + const Symbol& placement, + const std::vector>& sbp_tuple) const { + MutableAttrMap attrs; + JUST(attrs.SetAttr("rows", JUST(rows.As()))); + JUST(attrs.SetAttr("cols", JUST(cols.value_or(rows).As()))); + JUST(attrs.SetAttr("dtype", dtype->data_type())); + if (LazyMode::is_enabled()) { + std::vector nd_sbp(sbp_tuple.size()); + { + for (int i = 0; i < sbp_tuple.size(); ++i) { + nd_sbp.at(i) = SbpParallelToString(*sbp_tuple.at(i)); + } + } + JUST(attrs.SetAttr>("nd_sbp", nd_sbp)); + } + const auto& nd_sbp = JUST(GetNdSbp(sbp_tuple)); + return OpInterpUtil::Dispatch(*op_, {}, OpExprInterpContext(attrs, placement, nd_sbp)); + } + + private: + std::shared_ptr op_; +}; + +// class ConsistentEyeSbpFunctor { +// public: +// Maybe operator()(const Scalar& rows, const Optional& cols, +// const Symbol& dtype, +// const bool& requires_grad, +// const Symbol& placement, +// const Symbol& sbp) const { +// std::vector> sbp_tuple; +// sbp_tuple.push_back(sbp); +// return JUST(functional::Eye(rows, cols, dtype, requires_grad, placement, sbp_tuple)); +// } +// }; + +} // namespace impl + +using namespace impl; + +ONEFLOW_FUNCTION_LIBRARY(m) { + m.add_functor("Eye"); +}; + +} // namespace functional +} // namespace one +} // namespace oneflow diff --git a/oneflow/core/functional/impl/math_functor.cpp b/oneflow/core/functional/impl/math_functor.cpp index d6873390857..c4b7819ea62 100644 --- a/oneflow/core/functional/impl/math_functor.cpp +++ b/oneflow/core/functional/impl/math_functor.cpp @@ -596,53 +596,6 @@ class TransposeFunctor { std::shared_ptr op_; }; -class EyeFunctor { - public: - EyeFunctor() { op_ = CHECK_JUST(one::OpBuilder("eye").Output("out").Build()); } - Maybe operator()(const Scalar& rows, const Optional& cols, - const Optional>& dtype, - const Optional>& device) const { - MutableAttrMap attrs; - JUST(attrs.SetAttr("rows", JUST(rows.As()))); - JUST(attrs.SetAttr("cols", JUST(cols.value_or(rows).As()))); - JUST(attrs.SetAttr("dtype", dtype ? JUST(dtype)->data_type() : DataType::kFloat)); - OpExprInterpContext ctx(attrs); - ctx.device = device; - return OpInterpUtil::Dispatch(*op_, {}, ctx); - } - - private: - std::shared_ptr op_; -}; - -class ConsistentEyeFunctor { - public: - ConsistentEyeFunctor() { op_ = CHECK_JUST(one::OpBuilder("eye").Output("out").Build()); } - Maybe operator()(const Scalar& rows, const Optional& cols, - const Optional>& dtype, - const Symbol& placement, - const std::vector>& sbp_tuple) const { - MutableAttrMap attrs; - JUST(attrs.SetAttr("rows", JUST(rows.As()))); - JUST(attrs.SetAttr("cols", JUST(cols.value_or(rows).As()))); - JUST(attrs.SetAttr("dtype", dtype ? JUST(dtype)->data_type() : DataType::kFloat)); - if (LazyMode::is_enabled()) { - std::vector nd_sbp(sbp_tuple.size()); - { - for (int i = 0; i < sbp_tuple.size(); ++i) { - nd_sbp.at(i) = SbpParallelToString(*sbp_tuple.at(i)); - } - } - JUST(attrs.SetAttr>("nd_sbp", nd_sbp)); - } - const auto& nd_sbp = JUST(GetNdSbp(sbp_tuple)); - return OpInterpUtil::Dispatch(*op_, {}, OpExprInterpContext(attrs, placement, nd_sbp)); - } - - private: - std::shared_ptr op_; -}; - class Transpose2dimFunctor { public: Transpose2dimFunctor() { @@ -1741,8 +1694,6 @@ ONEFLOW_FUNCTION_LIBRARY(m) { m.add_functor("ReduceMaxGlobalStageGrad"); m.add_functor("Transpose"); m.add_functor("Permute"); - m.add_functor("Eye"); - m.add_functor("ConsistentEye"); m.add_functor("Transpose2dim"); m.add_functor("Arange"); m.add_functor("ConsistentArange"); diff --git a/python/oneflow/__init__.py b/python/oneflow/__init__.py index fcd19b92ff7..5c2df72e901 100755 --- a/python/oneflow/__init__.py +++ b/python/oneflow/__init__.py @@ -164,6 +164,7 @@ def is_deprecated(func_or_class): from oneflow._C import read_onerec from oneflow._C import decode_onerec from oneflow._C import dot +from oneflow._C import eye from . import sbp @@ -337,7 +338,6 @@ def atexit_hook(hook): from oneflow.nn.modules.slice import slice_update_op as slice_update from oneflow.nn.modules.slice import logical_slice_assign_op as logical_slice_assign from oneflow.nn.modules.sort import sort_op as sort -from oneflow.nn.modules.eye import eye_op as eye from oneflow.nn.modules.tensor_buffer import gen_tensor_buffer from oneflow.nn.modules.tensor_buffer import ( tensor_buffer_to_tensor_op as tensor_buffer_to_tensor, diff --git a/python/oneflow/nn/modules/eye.py b/python/oneflow/nn/modules/eye.py index 490f2564685..dc73a7c52bc 100644 --- a/python/oneflow/nn/modules/eye.py +++ b/python/oneflow/nn/modules/eye.py @@ -53,27 +53,45 @@ def eye_op( [0., 0., 1.]], dtype=oneflow.float32) """ - if placement is None: - if isinstance(device, str): - device = flow.device(device) - res = flow._C.eye(n, m, dtype=dtype, device=device) + if isinstance(sbp, flow.sbp.sbp): + assert sbp == flow.sbp.broadcast + sbp = (sbp,) else: - assert isinstance( - placement, flow._oneflow_internal.placement - ), "placement should be oneflow._oneflow_internal.placement type." - assert isinstance(sbp, (flow.sbp.sbp, tuple, list)), "sbp: %s" % sbp - if isinstance(sbp, flow.sbp.sbp): - assert sbp == flow.sbp.broadcast - sbp = (sbp,) - else: - for elem in sbp: - assert isinstance(elem, flow.sbp.sbp), "sbp: %s" % sbp - assert elem == flow.sbp.broadcast - assert len(sbp) == len(placement.hierarchy) - res = flow._C.consistent_eye(n, m, dtype=dtype, placement=placement, sbp=sbp) + for elem in sbp: + assert isinstance(elem, flow.sbp.sbp), "sbp: %s" % sbp + assert elem == flow.sbp.broadcast + assert len(sbp) == len(placement.hierarchy) - res.requires_grad = requires_grad - return res + return flow._C.eye( + n, + m, + dtype=dtype, + device=device, + requires_grad=requires_grad, + placement=placement, + sbp=sbp, + ) + # if placement is None: + # if isinstance(device, str): + # device = flow.device(device) + # res = flow._C.eye(n, m, dtype=dtype, device=device) + # else: + # assert isinstance( + # placement, flow._oneflow_internal.placement + # ), "placement should be oneflow._oneflow_internal.placement type." + # assert isinstance(sbp, (flow.sbp.sbp, tuple, list)), "sbp: %s" % sbp + # if isinstance(sbp, flow.sbp.sbp): + # assert sbp == flow.sbp.broadcast + # sbp = (sbp,) + # else: + # for elem in sbp: + # assert isinstance(elem, flow.sbp.sbp), "sbp: %s" % sbp + # assert elem == flow.sbp.broadcast + # assert len(sbp) == len(placement.hierarchy) + # res = flow._C.consistent_eye(n, m, dtype=dtype, placement=placement, sbp=sbp) + + # res.requires_grad = requires_grad + # return res if __name__ == "__main__": From a78df26ee9ea34be338c64c33a01c488939e1a23 Mon Sep 17 00:00:00 2001 From: YaoChi Date: Wed, 15 Dec 2021 21:08:23 +0800 Subject: [PATCH 02/10] refine functional_api.yaml --- oneflow/core/functional/functional_api.yaml | 8 -------- 1 file changed, 8 deletions(-) diff --git a/oneflow/core/functional/functional_api.yaml b/oneflow/core/functional/functional_api.yaml index db7c44731bd..13e6f2962f4 100755 --- a/oneflow/core/functional/functional_api.yaml +++ b/oneflow/core/functional/functional_api.yaml @@ -536,14 +536,6 @@ ] bind_python: True -- name: "physical_eye" - signature: "Tensor (Scalar n, Scalar m=None, *, DataType dtype=None, Device device=None) => PhysicalEye" - bind_python: false - -- name: "consistent_eye" - signature: "Tensor (Scalar n, Scalar m=None, *, DataType dtype=None, Placement placement, SbpList sbp) => ConsistentEye" - bind_python: false - - name: "arange" signature: [ "Tensor (Scalar start, Scalar end, Scalar step=1, *, DataType dtype=None, From d0f19ccb902e80aa0f2707bdc30ac2a5be8fffc7 Mon Sep 17 00:00:00 2001 From: YaoChi Date: Wed, 15 Dec 2021 22:19:10 +0800 Subject: [PATCH 03/10] 90% docstr left to update --- oneflow/core/functional/functional_api.yaml | 3 +- oneflow/core/functional/impl/eye_functor.cpp | 45 +++++---- python/oneflow/framework/docstr/math_ops.py | 29 ++++++ python/oneflow/nn/modules/eye.py | 100 ------------------- 4 files changed, 54 insertions(+), 123 deletions(-) delete mode 100644 python/oneflow/nn/modules/eye.py diff --git a/oneflow/core/functional/functional_api.yaml b/oneflow/core/functional/functional_api.yaml index 13e6f2962f4..f1ca4993fd6 100755 --- a/oneflow/core/functional/functional_api.yaml +++ b/oneflow/core/functional/functional_api.yaml @@ -532,7 +532,8 @@ signature: [ "Tensor (Scalar n, Scalar m=None, *, DataType dtype=kFloat, Device device=None, Bool requires_grad=False) => Eye", "Tensor (Scalar n, Scalar m=None, *, DataType dtype=kFloat, String device, Bool requires_grad=False) => Eye", - "Tensor (Scalar n, Scalar m=None, *, DataType dtype=kFloat, Bool requires_grad=False, Placement placement=None, SbpList sbp=None) => Eye", + "Tensor (Scalar n, Scalar m=None, *, DataType dtype=kFloat, Bool requires_grad=False, Placement placement, SbpList sbp) => Eye", + "Tensor (Scalar n, Scalar m=None, *, DataType dtype=kFloat, Bool requires_grad=False, Placement placement, Sbp sbp) => Eye", ] bind_python: True diff --git a/oneflow/core/functional/impl/eye_functor.cpp b/oneflow/core/functional/impl/eye_functor.cpp index ec18775f773..4ec6c2e4119 100644 --- a/oneflow/core/functional/impl/eye_functor.cpp +++ b/oneflow/core/functional/impl/eye_functor.cpp @@ -14,6 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ +#include "oneflow/core/common/just.h" #include "oneflow/core/common/scalar.h" #include "oneflow/core/framework/attr_map.h" #include "oneflow/core/framework/nd_sbp.h" @@ -26,10 +27,8 @@ limitations under the License. #include "oneflow/core/functional/function_library.h" #include "oneflow/core/functional/functional_api.yaml.h" #include "oneflow/core/functional/impl/common.h" -#include "oneflow/core/functional/impl/unary_functor.h" #include "oneflow/core/job/lazy_mode.h" #include "oneflow/core/job/sbp_parallel.h" -#include "oneflow/core/functional/tensor_processor.h" #include "oneflow/api/common/device.h" namespace oneflow { @@ -43,14 +42,16 @@ class EyeDevcieFunctor { EyeDevcieFunctor() { op_ = CHECK_JUST(one::OpBuilder("eye").Output("out").Build()); } Maybe operator()(const Scalar& rows, const Optional& cols, const Symbol& dtype, const Optional>& device, - const bool& requies_grad) const { + const bool& requires_grad) const { MutableAttrMap attrs; JUST(attrs.SetAttr("rows", JUST(rows.As()))); JUST(attrs.SetAttr("cols", JUST(cols.value_or(rows).As()))); JUST(attrs.SetAttr("dtype", dtype->data_type())); OpExprInterpContext ctx(attrs); ctx.device = device; - return OpInterpUtil::Dispatch(*op_, {}, ctx); + auto res = JUST(OpInterpUtil::Dispatch(*op_, {}, ctx)); + JUST(res->set_requires_grad(requires_grad)); + return res; } private: @@ -80,40 +81,40 @@ class ConsistentEyeSbpListFunctor { JUST(attrs.SetAttr("dtype", dtype->data_type())); if (LazyMode::is_enabled()) { std::vector nd_sbp(sbp_tuple.size()); - { - for (int i = 0; i < sbp_tuple.size(); ++i) { - nd_sbp.at(i) = SbpParallelToString(*sbp_tuple.at(i)); - } + for (int i = 0; i < sbp_tuple.size(); ++i) { + nd_sbp.at(i) = SbpParallelToString(*sbp_tuple.at(i)); } JUST(attrs.SetAttr>("nd_sbp", nd_sbp)); } const auto& nd_sbp = JUST(GetNdSbp(sbp_tuple)); - return OpInterpUtil::Dispatch(*op_, {}, OpExprInterpContext(attrs, placement, nd_sbp)); + auto res = JUST( + OpInterpUtil::Dispatch(*op_, {}, OpExprInterpContext(attrs, placement, nd_sbp))); + JUST(res->set_requires_grad(requires_grad)); + return Maybe(res); } private: std::shared_ptr op_; }; -// class ConsistentEyeSbpFunctor { -// public: -// Maybe operator()(const Scalar& rows, const Optional& cols, -// const Symbol& dtype, -// const bool& requires_grad, -// const Symbol& placement, -// const Symbol& sbp) const { -// std::vector> sbp_tuple; -// sbp_tuple.push_back(sbp); -// return JUST(functional::Eye(rows, cols, dtype, requires_grad, placement, sbp_tuple)); -// } -// }; +class ConsistentEyeSbpFunctor { + public: + Maybe operator()(const Scalar& rows, const Optional& cols, + const Symbol& dtype, const bool& requires_grad, + const Symbol& placement, + const Symbol& sbp) const { + std::vector> sbp_tuple{sbp}; + return JUST(functional::Eye(rows, cols, dtype, requires_grad, placement, sbp_tuple)); + } +}; } // namespace impl using namespace impl; ONEFLOW_FUNCTION_LIBRARY(m) { - m.add_functor("Eye"); + m.add_functor("Eye"); }; } // namespace functional diff --git a/python/oneflow/framework/docstr/math_ops.py b/python/oneflow/framework/docstr/math_ops.py index 8dc6c91de65..d140aa7840e 100644 --- a/python/oneflow/framework/docstr/math_ops.py +++ b/python/oneflow/framework/docstr/math_ops.py @@ -1337,3 +1337,32 @@ oneflow.Size([3, 4, 2, 5]) """, ) + +add_docstr( + oneflow.eye, + """This operator creates a 2-D Tensor with ones on the diagonal and zeros elsewhere. + + Args: + n (int): the number of rows. + m (Optional[int], optional): the number of colums with default being n. Defaults to None. + + Keyword args: + 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`. + + Returns: + oneflow.Tensor: The result Blob with ones on the diagonal and zeros elsewhere. + + For example: + + .. code-block:: python + + >>> import oneflow as flow + >>> out = flow.eye(3, 3) + >>> out + tensor([[1., 0., 0.], + [0., 1., 0.], + [0., 0., 1.]], dtype=oneflow.float32) + + """ +) \ No newline at end of file diff --git a/python/oneflow/nn/modules/eye.py b/python/oneflow/nn/modules/eye.py deleted file mode 100644 index dc73a7c52bc..00000000000 --- a/python/oneflow/nn/modules/eye.py +++ /dev/null @@ -1,100 +0,0 @@ -""" -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 - - http://www.apache.org/licenses/LICENSE-2.0 - -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. -""" -from typing import Union, List - -import oneflow as flow -from oneflow.framework.tensor import register_tensor_op - - -def eye_op( - n, - m=None, - dtype: flow.dtype = flow.float, - device: Union[str, flow.device] = None, - placement: flow.placement = None, - sbp: Union[flow.sbp.sbp, List[flow.sbp.sbp]] = None, - requires_grad: bool = False, -): - """This operator creates a 2-D Tensor with ones on the diagonal and zeros elsewhere. - - Args: - n (int): the number of rows. - m (Optional[int], optional): the number of colums with default being n. Defaults to None. - - Keyword args: - 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`. - - Returns: - oneflow.Tensor: The result Blob with ones on the diagonal and zeros elsewhere. - - For example: - - .. code-block:: python - - >>> import oneflow as flow - >>> out = flow.eye(3, 3) - >>> out - tensor([[1., 0., 0.], - [0., 1., 0.], - [0., 0., 1.]], dtype=oneflow.float32) - - """ - if isinstance(sbp, flow.sbp.sbp): - assert sbp == flow.sbp.broadcast - sbp = (sbp,) - else: - for elem in sbp: - assert isinstance(elem, flow.sbp.sbp), "sbp: %s" % sbp - assert elem == flow.sbp.broadcast - assert len(sbp) == len(placement.hierarchy) - - return flow._C.eye( - n, - m, - dtype=dtype, - device=device, - requires_grad=requires_grad, - placement=placement, - sbp=sbp, - ) - # if placement is None: - # if isinstance(device, str): - # device = flow.device(device) - # res = flow._C.eye(n, m, dtype=dtype, device=device) - # else: - # assert isinstance( - # placement, flow._oneflow_internal.placement - # ), "placement should be oneflow._oneflow_internal.placement type." - # assert isinstance(sbp, (flow.sbp.sbp, tuple, list)), "sbp: %s" % sbp - # if isinstance(sbp, flow.sbp.sbp): - # assert sbp == flow.sbp.broadcast - # sbp = (sbp,) - # else: - # for elem in sbp: - # assert isinstance(elem, flow.sbp.sbp), "sbp: %s" % sbp - # assert elem == flow.sbp.broadcast - # assert len(sbp) == len(placement.hierarchy) - # res = flow._C.consistent_eye(n, m, dtype=dtype, placement=placement, sbp=sbp) - - # res.requires_grad = requires_grad - # return res - - -if __name__ == "__main__": - import doctest - - doctest.testmod(raise_on_error=True) From f03a33f0d8809761cd891fb1b0bae35c8efc4bca Mon Sep 17 00:00:00 2001 From: YaoChi Date: Wed, 15 Dec 2021 22:42:26 +0800 Subject: [PATCH 04/10] refine --- oneflow/core/functional/impl/eye_functor.cpp | 5 +++++ python/oneflow/framework/docstr/math_ops.py | 16 +++++++++++----- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/oneflow/core/functional/impl/eye_functor.cpp b/oneflow/core/functional/impl/eye_functor.cpp index 4ec6c2e4119..8522736a109 100644 --- a/oneflow/core/functional/impl/eye_functor.cpp +++ b/oneflow/core/functional/impl/eye_functor.cpp @@ -76,6 +76,11 @@ class ConsistentEyeSbpListFunctor { const Symbol& placement, const std::vector>& sbp_tuple) const { MutableAttrMap attrs; + CHECK_EQ_OR_RETURN(sbp_tuple.size(), placement->hierarchy()->NumAxes()) + << "len(sbp) == len(placement.hierarchy) required, but " + << "len(sbp)==" << sbp_tuple.size() << ", " + << "len(placement.hierarchy)==" << placement->hierarchy()->NumAxes(); + JUST(attrs.SetAttr("rows", JUST(rows.As()))); JUST(attrs.SetAttr("cols", JUST(cols.value_or(rows).As()))); JUST(attrs.SetAttr("dtype", dtype->data_type())); diff --git a/python/oneflow/framework/docstr/math_ops.py b/python/oneflow/framework/docstr/math_ops.py index d140aa7840e..4665240aa1c 100644 --- a/python/oneflow/framework/docstr/math_ops.py +++ b/python/oneflow/framework/docstr/math_ops.py @@ -1347,11 +1347,13 @@ m (Optional[int], optional): the number of colums with default being n. Defaults to None. Keyword args: - device(flow.device, optional): the desired device of returned tensor. Default: if None, uses the current device for the default tensor. + device(Union[flow.device, str], 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`. + placement(Optional[oneflow._oneflow_internal.placement]): The placement attribute allows you to specify which physical device the tensor is stored on. + sbp(Optional[Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]]]): When creating a consistent tensor, specify the SBP of the tensor. Returns: - oneflow.Tensor: The result Blob with ones on the diagonal and zeros elsewhere. + oneflow.Tensor: The result tensor with ones on the diagonal and zeros elsewhere. For example: @@ -1363,6 +1365,10 @@ tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]], dtype=oneflow.float32) - - """ -) \ No newline at end of file + >>> out = flow.eye(3, 3, device="cuda") + >>> out + tensor([[1., 0., 0.], + [0., 1., 0.], + [0., 0., 1.]], device='cuda:0', dtype=oneflow.float32) + """, +) From 9d2edda9bad9f46b5b50f64e57a5651821f8d3b6 Mon Sep 17 00:00:00 2001 From: YaoChi Date: Wed, 15 Dec 2021 22:56:24 +0800 Subject: [PATCH 05/10] add sbp check --- oneflow/core/functional/impl/eye_functor.cpp | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/oneflow/core/functional/impl/eye_functor.cpp b/oneflow/core/functional/impl/eye_functor.cpp index 8522736a109..dc7840586c4 100644 --- a/oneflow/core/functional/impl/eye_functor.cpp +++ b/oneflow/core/functional/impl/eye_functor.cpp @@ -15,7 +15,10 @@ limitations under the License. */ #include "oneflow/core/common/just.h" +#include "oneflow/core/common/maybe.h" #include "oneflow/core/common/scalar.h" +#include "oneflow/core/common/throw.h" +#include "oneflow/core/common/util.h" #include "oneflow/core/framework/attr_map.h" #include "oneflow/core/framework/nd_sbp.h" #include "oneflow/core/framework/op_builder.h" @@ -80,14 +83,20 @@ class ConsistentEyeSbpListFunctor { << "len(sbp) == len(placement.hierarchy) required, but " << "len(sbp)==" << sbp_tuple.size() << ", " << "len(placement.hierarchy)==" << placement->hierarchy()->NumAxes(); + + FOR_RANGE(int32_t, i, 0, sbp_tuple.size()){ + CHECK_OR_RETURN(sbp_tuple.at(i)->has_broadcast_parallel()) << "sbp of eye should be broadcast only"; + } JUST(attrs.SetAttr("rows", JUST(rows.As()))); JUST(attrs.SetAttr("cols", JUST(cols.value_or(rows).As()))); JUST(attrs.SetAttr("dtype", dtype->data_type())); if (LazyMode::is_enabled()) { std::vector nd_sbp(sbp_tuple.size()); - for (int i = 0; i < sbp_tuple.size(); ++i) { - nd_sbp.at(i) = SbpParallelToString(*sbp_tuple.at(i)); + { + for (int i = 0; i < sbp_tuple.size(); ++i) { + nd_sbp.at(i) = SbpParallelToString(*sbp_tuple.at(i)); + } } JUST(attrs.SetAttr>("nd_sbp", nd_sbp)); } From 4df34183090d7181bba308d2c14c2e0ced5e822e Mon Sep 17 00:00:00 2001 From: YaoChi Date: Wed, 15 Dec 2021 23:02:12 +0800 Subject: [PATCH 06/10] refine docs --- python/oneflow/framework/docstr/math_ops.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/python/oneflow/framework/docstr/math_ops.py b/python/oneflow/framework/docstr/math_ops.py index 4665240aa1c..818fd6ede0f 100644 --- a/python/oneflow/framework/docstr/math_ops.py +++ b/python/oneflow/framework/docstr/math_ops.py @@ -1340,7 +1340,9 @@ add_docstr( oneflow.eye, - """This operator creates a 2-D Tensor with ones on the diagonal and zeros elsewhere. + """oneflow.eye(n, m, *, device=None, requires_grad=False, placement=None, sbp) -> Tensor + + This operator creates a 2-D Tensor with ones on the diagonal and zeros elsewhere. Args: n (int): the number of rows. From 0ee52c5f58ccda1fc9d0a043db1ee1ca10946ed6 Mon Sep 17 00:00:00 2001 From: oneflow-ci-bot Date: Wed, 15 Dec 2021 15:07:20 +0000 Subject: [PATCH 07/10] auto format by CI --- oneflow/core/functional/impl/eye_functor.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/oneflow/core/functional/impl/eye_functor.cpp b/oneflow/core/functional/impl/eye_functor.cpp index dc7840586c4..1561b3000aa 100644 --- a/oneflow/core/functional/impl/eye_functor.cpp +++ b/oneflow/core/functional/impl/eye_functor.cpp @@ -83,9 +83,10 @@ class ConsistentEyeSbpListFunctor { << "len(sbp) == len(placement.hierarchy) required, but " << "len(sbp)==" << sbp_tuple.size() << ", " << "len(placement.hierarchy)==" << placement->hierarchy()->NumAxes(); - - FOR_RANGE(int32_t, i, 0, sbp_tuple.size()){ - CHECK_OR_RETURN(sbp_tuple.at(i)->has_broadcast_parallel()) << "sbp of eye should be broadcast only"; + + FOR_RANGE(int32_t, i, 0, sbp_tuple.size()) { + CHECK_OR_RETURN(sbp_tuple.at(i)->has_broadcast_parallel()) + << "sbp of eye should be broadcast only"; } JUST(attrs.SetAttr("rows", JUST(rows.As()))); @@ -95,7 +96,7 @@ class ConsistentEyeSbpListFunctor { std::vector nd_sbp(sbp_tuple.size()); { for (int i = 0; i < sbp_tuple.size(); ++i) { - nd_sbp.at(i) = SbpParallelToString(*sbp_tuple.at(i)); + nd_sbp.at(i) = SbpParallelToString(*sbp_tuple.at(i)); } } JUST(attrs.SetAttr>("nd_sbp", nd_sbp)); From 3a72419976532ca9a97c2075820c4b50db8d8351 Mon Sep 17 00:00:00 2001 From: YaoChi Date: Wed, 15 Dec 2021 23:09:20 +0800 Subject: [PATCH 08/10] refine --- oneflow/core/functional/impl/eye_functor.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/oneflow/core/functional/impl/eye_functor.cpp b/oneflow/core/functional/impl/eye_functor.cpp index dc7840586c4..a36b9decaff 100644 --- a/oneflow/core/functional/impl/eye_functor.cpp +++ b/oneflow/core/functional/impl/eye_functor.cpp @@ -104,7 +104,7 @@ class ConsistentEyeSbpListFunctor { auto res = JUST( OpInterpUtil::Dispatch(*op_, {}, OpExprInterpContext(attrs, placement, nd_sbp))); JUST(res->set_requires_grad(requires_grad)); - return Maybe(res); + return res; } private: From 3ca3a5f2d29713744d08ac1baee4dc54f2738416 Mon Sep 17 00:00:00 2001 From: YaoChi Date: Thu, 16 Dec 2021 09:51:28 +0800 Subject: [PATCH 09/10] refine docstr --- python/oneflow/framework/docstr/math_ops.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/oneflow/framework/docstr/math_ops.py b/python/oneflow/framework/docstr/math_ops.py index 818fd6ede0f..e3315e6572c 100644 --- a/python/oneflow/framework/docstr/math_ops.py +++ b/python/oneflow/framework/docstr/math_ops.py @@ -1346,13 +1346,13 @@ Args: n (int): the number of rows. - m (Optional[int], optional): the number of colums with default being n. Defaults to None. + m (int, optional): the number of colums with default being n. Defaults to None. Keyword args: device(Union[flow.device, str], 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`. - placement(Optional[oneflow._oneflow_internal.placement]): The placement attribute allows you to specify which physical device the tensor is stored on. - sbp(Optional[Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]]]): When creating a consistent tensor, specify the SBP of the tensor. + placement(oneflow._oneflow_internal.placement, optional): The placement attribute allows you to specify which physical device the tensor is stored on. + sbp(Union[oneflow._oneflow_internal.sbp.sbp, List[oneflow._oneflow_internal.sbp.sbp]], optional): When creating a consistent tensor, specify the SBP of the tensor. Returns: oneflow.Tensor: The result tensor with ones on the diagonal and zeros elsewhere. From 163d573e6d2367c19c5acc6d20d6468940f36a92 Mon Sep 17 00:00:00 2001 From: oneflow-ci-bot Date: Thu, 16 Dec 2021 14:05:42 +0000 Subject: [PATCH 10/10] auto format by CI --- oneflow/core/functional/impl/math_functor.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/oneflow/core/functional/impl/math_functor.cpp b/oneflow/core/functional/impl/math_functor.cpp index 7e0ff3a6171..e52528ebc01 100644 --- a/oneflow/core/functional/impl/math_functor.cpp +++ b/oneflow/core/functional/impl/math_functor.cpp @@ -596,7 +596,6 @@ class TransposeFunctor { std::shared_ptr op_; }; - class Transpose2dimFunctor { public: Transpose2dimFunctor() {