From e6e6973b06d2611c04bcac3d57e1b680f50acadc Mon Sep 17 00:00:00 2001 From: BBuf <1182563586@qq.com> Date: Tue, 22 Mar 2022 19:15:03 +0800 Subject: [PATCH 1/9] fix reduce_sum scalar check bug --- oneflow/core/common/shape.cpp | 3 ++- oneflow/user/ops/reduce_ops.cpp | 18 +++++++++++------- python/oneflow/test/modules/test_sum.py | 5 +++++ 3 files changed, 18 insertions(+), 8 deletions(-) diff --git a/oneflow/core/common/shape.cpp b/oneflow/core/common/shape.cpp index 16bdb1df15d..3838a59d5df 100644 --- a/oneflow/core/common/shape.cpp +++ b/oneflow/core/common/shape.cpp @@ -21,7 +21,8 @@ limitations under the License. namespace oneflow { Shape CreateReducedShape(const ShapeView& shape, const AxisVector& axis_vec) { - CHECK_EQ(axis_vec.empty(), false); + // For 0-dim Tensor + if (axis_vec.empty()) { return Shape({}); } DimVector dim_vec; shape.ToDimVector(&dim_vec); for (int64_t axis : axis_vec) { dim_vec.at(ShiftNegativeAxis(axis, shape.NumAxes())) = 1; } diff --git a/oneflow/user/ops/reduce_ops.cpp b/oneflow/user/ops/reduce_ops.cpp index 610c01a5aaf..f0080b98849 100644 --- a/oneflow/user/ops/reduce_ops.cpp +++ b/oneflow/user/ops/reduce_ops.cpp @@ -23,15 +23,19 @@ namespace oneflow { Maybe InferTensorDescFn(user_op::InferContext* ctx) { const Shape& input_shape = ctx->InputShape("input_tensor", 0); const auto& reduce_axes = ctx->Attr>("axis"); - CHECK_OR_RETURN(!reduce_axes.empty()); - const AxisVector reduce_axes_vec = {reduce_axes.begin(), reduce_axes.end()}; - const Shape& reduce_shape = CreateReducedShape(input_shape, reduce_axes_vec); - const bool keepdims = ctx->Attr("keepdims"); Shape* output_shape = ctx->OutputShape("output_tensor", 0); - if (keepdims) { - *output_shape = reduce_shape; + // For 0-dim Tensor + if (reduce_axes.empty()) { + *output_shape = input_shape; } else { - *output_shape = reduce_shape.RemoveOnes(reduce_axes_vec); + const AxisVector reduce_axes_vec = {reduce_axes.begin(), reduce_axes.end()}; + const Shape& reduce_shape = CreateReducedShape(input_shape, reduce_axes_vec); + const bool keepdims = ctx->Attr("keepdims"); + if (keepdims) { + *output_shape = reduce_shape; + } else { + *output_shape = reduce_shape.RemoveOnes(reduce_axes_vec); + } } return Maybe::Ok(); } diff --git a/python/oneflow/test/modules/test_sum.py b/python/oneflow/test/modules/test_sum.py index 62ff5dc3f8b..b1b818d4ce4 100644 --- a/python/oneflow/test/modules/test_sum.py +++ b/python/oneflow/test/modules/test_sum.py @@ -63,6 +63,11 @@ def _test_sum_impl(test_case, device, data_type): np_grad = np.ones((4, 5, 6)) test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, 1e-05, 1e-05)) + # For 0-dim tensor test + input = flow.tensor(1.0) + of_out = input.sum() + test_case.assertTrue(np.allclose(input.numpy(), of_out.numpy(), 1e-05, 1e-05)) + @flow.unittest.skip_unless_1n1d() class TestSumModule(flow.unittest.TestCase): From 3d0572b7a333c3c3716b8272a96fbe9ce1b8d26f Mon Sep 17 00:00:00 2001 From: BBuf <1182563586@qq.com> Date: Thu, 24 Mar 2022 19:28:47 +0800 Subject: [PATCH 2/9] fix bug --- oneflow/core/functional/functional_api.yaml | 18 ++++---- .../OneFlow/Transform/ConstantFolding.h | 16 +++++++ .../lib/OneFlow/Transform/ConstantFolding.cpp | 44 +++++++++++++++++++ python/oneflow/test/modules/test_conv1d.py | 9 ++++ python/oneflow/test/modules/test_conv2d.py | 11 ++++- python/oneflow/test/modules/test_conv3d.py | 11 +++++ 6 files changed, 99 insertions(+), 10 deletions(-) create mode 100644 oneflow/ir/include/OneFlow/Transform/ConstantFolding.h create mode 100644 oneflow/ir/lib/OneFlow/Transform/ConstantFolding.cpp diff --git a/oneflow/core/functional/functional_api.yaml b/oneflow/core/functional/functional_api.yaml index 2a61301d70c..d8e4e65c7ce 100755 --- a/oneflow/core/functional/functional_api.yaml +++ b/oneflow/core/functional/functional_api.yaml @@ -696,23 +696,23 @@ - name: "conv1d" signature: - "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[1] stride, - Int32List[1] padding, Int32List[1] dilation, Int32 groups=1, - String channel_pos) => Conv1d" + "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[1] stride={1}, + Int32List[1] padding={0}, Int32List[1] dilation={1}, Int32 groups=1, + String channel_pos=\"channels_first\") => Conv1d" bind_python: True - name: "conv2d" signature: - "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[2] stride, - Int32List[2] padding, Int32List[2] dilation, Int32 groups=1, - String channel_pos) => Conv2d" + "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[2] stride={1}, + Int32List[2] padding={0}, Int32List[2] dilation={1}, Int32 groups=1, + String channel_pos=\"channels_first\") => Conv2d" bind_python: True - name: "conv3d" signature: - "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[3] stride, - Int32List[3] padding, Int32List[3] dilation, Int32 groups=1, - String channel_pos) => Conv3d" + "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[3] stride={1}, + Int32List[3] padding={0}, Int32List[3] dilation={1}, Int32 groups=1, + String channel_pos=\"channels_first\") => Conv3d" bind_python: True - name: "fake_quantization" diff --git a/oneflow/ir/include/OneFlow/Transform/ConstantFolding.h b/oneflow/ir/include/OneFlow/Transform/ConstantFolding.h new file mode 100644 index 00000000000..7bdb2e5fceb --- /dev/null +++ b/oneflow/ir/include/OneFlow/Transform/ConstantFolding.h @@ -0,0 +1,16 @@ +#ifndef ONEFLOW_IR_INCLUDE_ONEFLOW_TRANSFORM_CONSTANTFOLDING_H_ +#define ONEFLOW_IR_INCLUDE_ONEFLOW_TRANSFORM_CONSTANTFOLDING_H_ + +#include "mlir/Pass/Pass.h" + +namespace mlir { + +namespace oneflow { + +std::unique_ptr createConstantFoldingPass(); + +} // namespace oneflow + +} // namespace mlir + +#endif // ONEFLOW_IR_INCLUDE_ONEFLOW_TRANSFORM_CONSTANTFOLDING_H_ diff --git a/oneflow/ir/lib/OneFlow/Transform/ConstantFolding.cpp b/oneflow/ir/lib/OneFlow/Transform/ConstantFolding.cpp new file mode 100644 index 00000000000..706014ee46a --- /dev/null +++ b/oneflow/ir/lib/OneFlow/Transform/ConstantFolding.cpp @@ -0,0 +1,44 @@ +/* +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 +#include +#include "OneFlow/Passes.h" +#include "mlir/Pass/Pass.h" +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" + +using namespace mlir; + +namespace { + +class ConstantFoldingPass : public ConstantFoldingPassBase { + void runOnOperation() override { + Operation* op = getOperation(); + RewritePatternSet patterns(op->getContext()); + oneflow::populateConstantFolding(patterns); + (void)applyPatternsAndFoldGreedily(op, std::move(patterns)); + } +}; + +} // namespace + +namespace mlir { + +namespace oneflow { + +std::unique_ptr createConstantFoldingPass() { + return std::make_unique(); +} + +} // namespace oneflow + +} // namespace mlir diff --git a/python/oneflow/test/modules/test_conv1d.py b/python/oneflow/test/modules/test_conv1d.py index 1e86281aae8..10f6a946d68 100644 --- a/python/oneflow/test/modules/test_conv1d.py +++ b/python/oneflow/test/modules/test_conv1d.py @@ -435,6 +435,15 @@ def test_conv1d(test_case): for arg in GenArgList(arg_dict): arg[0](test_case, *arg[1:]) + def test_nn_functional_conv1d(test_case): + img = flow.ones(1,3,224) + kernel = flow.randn(3,1,3) + y = flow.nn.functional.conv1d(img, kernel, groups=3) + assert(y.shape[0] == img.shape[0]) + assert(y.shape[1] == kernel.shape[0]) + # default padding is 1. + assert(y.shape[2] == img.shape[2] - 2) + @autotest() def test_conv1d_with_random_data(test_case): channels = random(1, 6) diff --git a/python/oneflow/test/modules/test_conv2d.py b/python/oneflow/test/modules/test_conv2d.py index 6c89ccf4647..4d7d1739c3a 100644 --- a/python/oneflow/test/modules/test_conv2d.py +++ b/python/oneflow/test/modules/test_conv2d.py @@ -1581,6 +1581,16 @@ def test_conv2d_default_init(test_case): ) ) + def test_nn_functional_conv2d(test_case): + img = flow.ones(1,3,224,224) + kernel = flow.randn(3,1,3,3) + y = flow.nn.functional.conv2d(img, kernel, groups=3) + assert(y.shape[0] == img.shape[0]) + assert(y.shape[1] == kernel.shape[0]) + # default padding is 1. + assert(y.shape[2] == img.shape[2] - 2) + assert(y.shape[3] == img.shape[3] - 2) + def test_conv2d(test_case): arg_dict = OrderedDict() arg_dict["device"] = ["cuda", "cpu"] @@ -1866,6 +1876,5 @@ def test_conv2d_group_with_random_data(test_case): y = m(x) return y - if __name__ == "__main__": unittest.main() diff --git a/python/oneflow/test/modules/test_conv3d.py b/python/oneflow/test/modules/test_conv3d.py index 345e1e05d0c..cffd10dec91 100644 --- a/python/oneflow/test/modules/test_conv3d.py +++ b/python/oneflow/test/modules/test_conv3d.py @@ -22,6 +22,17 @@ @flow.unittest.skip_unless_1n1d() class TestConv3DModule(flow.unittest.TestCase): + def test_nn_functional_conv3d(test_case): + img = flow.ones(1, 3, 224, 224, 224) + kernel = flow.randn(6, 3, 3, 3, 3) + y = flow.nn.functional.conv3d(img, kernel) + assert(y.shape[0] == img.shape[0]) + assert(y.shape[1] == kernel.shape[0]) + # default padding is 1. + assert(y.shape[2] == img.shape[2] - 2) + assert(y.shape[3] == img.shape[3] - 2) + assert(y.shape[3] == img.shape[3] - 2) + @autotest(n=10) def test_conv3d_with_random_data(test_case): channels = random(1, 6) From bc8f2fd417bfc16afc1954d25d9e083f103ff544 Mon Sep 17 00:00:00 2001 From: BBuf <1182563586@qq.com> Date: Thu, 24 Mar 2022 19:29:13 +0800 Subject: [PATCH 3/9] fix bug --- .../OneFlow/Transform/ConstantFolding.h | 16 ------- .../lib/OneFlow/Transform/ConstantFolding.cpp | 44 ------------------- 2 files changed, 60 deletions(-) delete mode 100644 oneflow/ir/include/OneFlow/Transform/ConstantFolding.h delete mode 100644 oneflow/ir/lib/OneFlow/Transform/ConstantFolding.cpp diff --git a/oneflow/ir/include/OneFlow/Transform/ConstantFolding.h b/oneflow/ir/include/OneFlow/Transform/ConstantFolding.h deleted file mode 100644 index 7bdb2e5fceb..00000000000 --- a/oneflow/ir/include/OneFlow/Transform/ConstantFolding.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef ONEFLOW_IR_INCLUDE_ONEFLOW_TRANSFORM_CONSTANTFOLDING_H_ -#define ONEFLOW_IR_INCLUDE_ONEFLOW_TRANSFORM_CONSTANTFOLDING_H_ - -#include "mlir/Pass/Pass.h" - -namespace mlir { - -namespace oneflow { - -std::unique_ptr createConstantFoldingPass(); - -} // namespace oneflow - -} // namespace mlir - -#endif // ONEFLOW_IR_INCLUDE_ONEFLOW_TRANSFORM_CONSTANTFOLDING_H_ diff --git a/oneflow/ir/lib/OneFlow/Transform/ConstantFolding.cpp b/oneflow/ir/lib/OneFlow/Transform/ConstantFolding.cpp deleted file mode 100644 index 706014ee46a..00000000000 --- a/oneflow/ir/lib/OneFlow/Transform/ConstantFolding.cpp +++ /dev/null @@ -1,44 +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. -*/ -#include -#include -#include "OneFlow/Passes.h" -#include "mlir/Pass/Pass.h" -#include "mlir/Transforms/GreedyPatternRewriteDriver.h" - -using namespace mlir; - -namespace { - -class ConstantFoldingPass : public ConstantFoldingPassBase { - void runOnOperation() override { - Operation* op = getOperation(); - RewritePatternSet patterns(op->getContext()); - oneflow::populateConstantFolding(patterns); - (void)applyPatternsAndFoldGreedily(op, std::move(patterns)); - } -}; - -} // namespace - -namespace mlir { - -namespace oneflow { - -std::unique_ptr createConstantFoldingPass() { - return std::make_unique(); -} - -} // namespace oneflow - -} // namespace mlir From 0e8d32a5d98e1e3f8b9541aea27f62e76140bb77 Mon Sep 17 00:00:00 2001 From: BBuf <1182563586@qq.com> Date: Thu, 24 Mar 2022 19:34:19 +0800 Subject: [PATCH 4/9] revert --- oneflow/core/common/shape.cpp | 2 -- oneflow/user/ops/reduce_ops.cpp | 18 +++++++----------- python/oneflow/test/modules/test_sum.py | 5 ----- 3 files changed, 7 insertions(+), 18 deletions(-) diff --git a/oneflow/core/common/shape.cpp b/oneflow/core/common/shape.cpp index 3838a59d5df..7c41beb0982 100644 --- a/oneflow/core/common/shape.cpp +++ b/oneflow/core/common/shape.cpp @@ -21,8 +21,6 @@ limitations under the License. namespace oneflow { Shape CreateReducedShape(const ShapeView& shape, const AxisVector& axis_vec) { - // For 0-dim Tensor - if (axis_vec.empty()) { return Shape({}); } DimVector dim_vec; shape.ToDimVector(&dim_vec); for (int64_t axis : axis_vec) { dim_vec.at(ShiftNegativeAxis(axis, shape.NumAxes())) = 1; } diff --git a/oneflow/user/ops/reduce_ops.cpp b/oneflow/user/ops/reduce_ops.cpp index f0080b98849..610c01a5aaf 100644 --- a/oneflow/user/ops/reduce_ops.cpp +++ b/oneflow/user/ops/reduce_ops.cpp @@ -23,19 +23,15 @@ namespace oneflow { Maybe InferTensorDescFn(user_op::InferContext* ctx) { const Shape& input_shape = ctx->InputShape("input_tensor", 0); const auto& reduce_axes = ctx->Attr>("axis"); + CHECK_OR_RETURN(!reduce_axes.empty()); + const AxisVector reduce_axes_vec = {reduce_axes.begin(), reduce_axes.end()}; + const Shape& reduce_shape = CreateReducedShape(input_shape, reduce_axes_vec); + const bool keepdims = ctx->Attr("keepdims"); Shape* output_shape = ctx->OutputShape("output_tensor", 0); - // For 0-dim Tensor - if (reduce_axes.empty()) { - *output_shape = input_shape; + if (keepdims) { + *output_shape = reduce_shape; } else { - const AxisVector reduce_axes_vec = {reduce_axes.begin(), reduce_axes.end()}; - const Shape& reduce_shape = CreateReducedShape(input_shape, reduce_axes_vec); - const bool keepdims = ctx->Attr("keepdims"); - if (keepdims) { - *output_shape = reduce_shape; - } else { - *output_shape = reduce_shape.RemoveOnes(reduce_axes_vec); - } + *output_shape = reduce_shape.RemoveOnes(reduce_axes_vec); } return Maybe::Ok(); } diff --git a/python/oneflow/test/modules/test_sum.py b/python/oneflow/test/modules/test_sum.py index b1b818d4ce4..62ff5dc3f8b 100644 --- a/python/oneflow/test/modules/test_sum.py +++ b/python/oneflow/test/modules/test_sum.py @@ -63,11 +63,6 @@ def _test_sum_impl(test_case, device, data_type): np_grad = np.ones((4, 5, 6)) test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, 1e-05, 1e-05)) - # For 0-dim tensor test - input = flow.tensor(1.0) - of_out = input.sum() - test_case.assertTrue(np.allclose(input.numpy(), of_out.numpy(), 1e-05, 1e-05)) - @flow.unittest.skip_unless_1n1d() class TestSumModule(flow.unittest.TestCase): From 9d68a2f912a63b6c888cddda7fc0898357db0a56 Mon Sep 17 00:00:00 2001 From: BBuf <1182563586@qq.com> Date: Thu, 24 Mar 2022 19:38:07 +0800 Subject: [PATCH 5/9] revert --- oneflow/core/common/shape.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/oneflow/core/common/shape.cpp b/oneflow/core/common/shape.cpp index 7c41beb0982..16bdb1df15d 100644 --- a/oneflow/core/common/shape.cpp +++ b/oneflow/core/common/shape.cpp @@ -21,6 +21,7 @@ limitations under the License. namespace oneflow { Shape CreateReducedShape(const ShapeView& shape, const AxisVector& axis_vec) { + CHECK_EQ(axis_vec.empty(), false); DimVector dim_vec; shape.ToDimVector(&dim_vec); for (int64_t axis : axis_vec) { dim_vec.at(ShiftNegativeAxis(axis, shape.NumAxes())) = 1; } From 0cce7da003ef8d76f7166fbaad02edd0d8c82243 Mon Sep 17 00:00:00 2001 From: oneflow-ci-bot Date: Thu, 24 Mar 2022 12:53:16 +0000 Subject: [PATCH 6/9] auto format by CI --- python/oneflow/test/modules/test_conv1d.py | 10 +++++----- python/oneflow/test/modules/test_conv2d.py | 13 +++++++------ python/oneflow/test/modules/test_conv3d.py | 10 +++++----- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/python/oneflow/test/modules/test_conv1d.py b/python/oneflow/test/modules/test_conv1d.py index 10f6a946d68..351882cc639 100644 --- a/python/oneflow/test/modules/test_conv1d.py +++ b/python/oneflow/test/modules/test_conv1d.py @@ -436,13 +436,13 @@ def test_conv1d(test_case): arg[0](test_case, *arg[1:]) def test_nn_functional_conv1d(test_case): - img = flow.ones(1,3,224) - kernel = flow.randn(3,1,3) + img = flow.ones(1, 3, 224) + kernel = flow.randn(3, 1, 3) y = flow.nn.functional.conv1d(img, kernel, groups=3) - assert(y.shape[0] == img.shape[0]) - assert(y.shape[1] == kernel.shape[0]) + assert y.shape[0] == img.shape[0] + assert y.shape[1] == kernel.shape[0] # default padding is 1. - assert(y.shape[2] == img.shape[2] - 2) + assert y.shape[2] == img.shape[2] - 2 @autotest() def test_conv1d_with_random_data(test_case): diff --git a/python/oneflow/test/modules/test_conv2d.py b/python/oneflow/test/modules/test_conv2d.py index 4d7d1739c3a..96921c4502b 100644 --- a/python/oneflow/test/modules/test_conv2d.py +++ b/python/oneflow/test/modules/test_conv2d.py @@ -1582,14 +1582,14 @@ def test_conv2d_default_init(test_case): ) def test_nn_functional_conv2d(test_case): - img = flow.ones(1,3,224,224) - kernel = flow.randn(3,1,3,3) + img = flow.ones(1, 3, 224, 224) + kernel = flow.randn(3, 1, 3, 3) y = flow.nn.functional.conv2d(img, kernel, groups=3) - assert(y.shape[0] == img.shape[0]) - assert(y.shape[1] == kernel.shape[0]) + assert y.shape[0] == img.shape[0] + assert y.shape[1] == kernel.shape[0] # default padding is 1. - assert(y.shape[2] == img.shape[2] - 2) - assert(y.shape[3] == img.shape[3] - 2) + assert y.shape[2] == img.shape[2] - 2 + assert y.shape[3] == img.shape[3] - 2 def test_conv2d(test_case): arg_dict = OrderedDict() @@ -1876,5 +1876,6 @@ def test_conv2d_group_with_random_data(test_case): y = m(x) return y + if __name__ == "__main__": unittest.main() diff --git a/python/oneflow/test/modules/test_conv3d.py b/python/oneflow/test/modules/test_conv3d.py index cffd10dec91..8f6d7b0203c 100644 --- a/python/oneflow/test/modules/test_conv3d.py +++ b/python/oneflow/test/modules/test_conv3d.py @@ -26,12 +26,12 @@ def test_nn_functional_conv3d(test_case): img = flow.ones(1, 3, 224, 224, 224) kernel = flow.randn(6, 3, 3, 3, 3) y = flow.nn.functional.conv3d(img, kernel) - assert(y.shape[0] == img.shape[0]) - assert(y.shape[1] == kernel.shape[0]) + assert y.shape[0] == img.shape[0] + assert y.shape[1] == kernel.shape[0] # default padding is 1. - assert(y.shape[2] == img.shape[2] - 2) - assert(y.shape[3] == img.shape[3] - 2) - assert(y.shape[3] == img.shape[3] - 2) + assert y.shape[2] == img.shape[2] - 2 + assert y.shape[3] == img.shape[3] - 2 + assert y.shape[3] == img.shape[3] - 2 @autotest(n=10) def test_conv3d_with_random_data(test_case): From 31ac967bba876a9bf4c8bfb8b67eb027b392f11b Mon Sep 17 00:00:00 2001 From: BBuf <1182563586@qq.com> Date: Thu, 24 Mar 2022 22:10:26 +0800 Subject: [PATCH 7/9] fix commnet --- python/oneflow/test/modules/test_conv1d.py | 13 ++++++------- python/oneflow/test/modules/test_conv2d.py | 14 ++++++-------- python/oneflow/test/modules/test_conv3d.py | 15 ++++++--------- 3 files changed, 18 insertions(+), 24 deletions(-) diff --git a/python/oneflow/test/modules/test_conv1d.py b/python/oneflow/test/modules/test_conv1d.py index 10f6a946d68..29e8e6724f3 100644 --- a/python/oneflow/test/modules/test_conv1d.py +++ b/python/oneflow/test/modules/test_conv1d.py @@ -435,14 +435,13 @@ def test_conv1d(test_case): for arg in GenArgList(arg_dict): arg[0](test_case, *arg[1:]) + @autotest(n=3) def test_nn_functional_conv1d(test_case): - img = flow.ones(1,3,224) - kernel = flow.randn(3,1,3) - y = flow.nn.functional.conv1d(img, kernel, groups=3) - assert(y.shape[0] == img.shape[0]) - assert(y.shape[1] == kernel.shape[0]) - # default padding is 1. - assert(y.shape[2] == img.shape[2] - 2) + device = random_device() + img = torch.ones((1,3,224), requires_grad=True).to(device) + kernel = torch.ones((3,1,3), requires_grad=True).to(device) + y = torch.nn.functional.conv1d(img, kernel, groups=3) + return y @autotest() def test_conv1d_with_random_data(test_case): diff --git a/python/oneflow/test/modules/test_conv2d.py b/python/oneflow/test/modules/test_conv2d.py index 4d7d1739c3a..f6083a1b155 100644 --- a/python/oneflow/test/modules/test_conv2d.py +++ b/python/oneflow/test/modules/test_conv2d.py @@ -1581,15 +1581,13 @@ def test_conv2d_default_init(test_case): ) ) + @autotest(n=3) def test_nn_functional_conv2d(test_case): - img = flow.ones(1,3,224,224) - kernel = flow.randn(3,1,3,3) - y = flow.nn.functional.conv2d(img, kernel, groups=3) - assert(y.shape[0] == img.shape[0]) - assert(y.shape[1] == kernel.shape[0]) - # default padding is 1. - assert(y.shape[2] == img.shape[2] - 2) - assert(y.shape[3] == img.shape[3] - 2) + device = random_device() + img = torch.ones((1,3,224,224), requires_grad=True).to(device) + kernel = torch.ones((3,1,3,3), requires_grad=True).to(device) + y = torch.nn.functional.conv2d(img, kernel, groups=3) + return y def test_conv2d(test_case): arg_dict = OrderedDict() diff --git a/python/oneflow/test/modules/test_conv3d.py b/python/oneflow/test/modules/test_conv3d.py index cffd10dec91..01701005166 100644 --- a/python/oneflow/test/modules/test_conv3d.py +++ b/python/oneflow/test/modules/test_conv3d.py @@ -22,16 +22,13 @@ @flow.unittest.skip_unless_1n1d() class TestConv3DModule(flow.unittest.TestCase): + @autotest(n=3) def test_nn_functional_conv3d(test_case): - img = flow.ones(1, 3, 224, 224, 224) - kernel = flow.randn(6, 3, 3, 3, 3) - y = flow.nn.functional.conv3d(img, kernel) - assert(y.shape[0] == img.shape[0]) - assert(y.shape[1] == kernel.shape[0]) - # default padding is 1. - assert(y.shape[2] == img.shape[2] - 2) - assert(y.shape[3] == img.shape[3] - 2) - assert(y.shape[3] == img.shape[3] - 2) + device = random_device() + img = torch.ones((1, 3, 224, 224, 224), requires_grad=True).to(device) + kernel = torch.ones((6, 3, 3, 3, 3), requires_grad=True).to(device) + y = torch.nn.functional.conv3d(img, kernel) + return y @autotest(n=10) def test_conv3d_with_random_data(test_case): From 4dfc5d96a49f75becbdbff2c525cdbd679cb0950 Mon Sep 17 00:00:00 2001 From: oneflow-ci-bot Date: Sat, 26 Mar 2022 02:18:53 +0000 Subject: [PATCH 8/9] auto format by CI --- python/oneflow/test/modules/test_conv1d.py | 4 ++-- python/oneflow/test/modules/test_conv2d.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/python/oneflow/test/modules/test_conv1d.py b/python/oneflow/test/modules/test_conv1d.py index 29e8e6724f3..fa1c9984ef3 100644 --- a/python/oneflow/test/modules/test_conv1d.py +++ b/python/oneflow/test/modules/test_conv1d.py @@ -438,8 +438,8 @@ def test_conv1d(test_case): @autotest(n=3) def test_nn_functional_conv1d(test_case): device = random_device() - img = torch.ones((1,3,224), requires_grad=True).to(device) - kernel = torch.ones((3,1,3), requires_grad=True).to(device) + img = torch.ones((1, 3, 224), requires_grad=True).to(device) + kernel = torch.ones((3, 1, 3), requires_grad=True).to(device) y = torch.nn.functional.conv1d(img, kernel, groups=3) return y diff --git a/python/oneflow/test/modules/test_conv2d.py b/python/oneflow/test/modules/test_conv2d.py index 600fc56edb3..7e58a552fcb 100644 --- a/python/oneflow/test/modules/test_conv2d.py +++ b/python/oneflow/test/modules/test_conv2d.py @@ -1584,8 +1584,8 @@ def test_conv2d_default_init(test_case): @autotest(n=3) def test_nn_functional_conv2d(test_case): device = random_device() - img = torch.ones((1,3,224,224), requires_grad=True).to(device) - kernel = torch.ones((3,1,3,3), requires_grad=True).to(device) + img = torch.ones((1, 3, 224, 224), requires_grad=True).to(device) + kernel = torch.ones((3, 1, 3, 3), requires_grad=True).to(device) y = torch.nn.functional.conv2d(img, kernel, groups=3) return y From c793e5a484798ae66f7661ffad3fdf8e025ccdbc Mon Sep 17 00:00:00 2001 From: BBuf <1182563586@qq.com> Date: Wed, 30 Mar 2022 17:34:00 +0800 Subject: [PATCH 9/9] fix clang check error --- oneflow/core/functional/functional_api.yaml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/oneflow/core/functional/functional_api.yaml b/oneflow/core/functional/functional_api.yaml index d8c2d024ce2..0151c0dfd23 100755 --- a/oneflow/core/functional/functional_api.yaml +++ b/oneflow/core/functional/functional_api.yaml @@ -696,22 +696,22 @@ - name: "conv1d" signature: - "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[1] stride={1}, - Int32List[1] padding={0}, Int32List[1] dilation={1}, Int32 groups=1, + "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[1] stride=1, + Int32List[1] padding=0, Int32List[1] dilation=1, Int32 groups=1, String channel_pos=\"channels_first\") => Conv1d" bind_python: True - name: "conv2d" signature: - "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[2] stride={1}, - Int32List[2] padding={0}, Int32List[2] dilation={1}, Int32 groups=1, + "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[2] stride=1, + Int32List[2] padding=0, Int32List[2] dilation=1, Int32 groups=1, String channel_pos=\"channels_first\") => Conv2d" bind_python: True - name: "conv3d" signature: - "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[3] stride={1}, - Int32List[3] padding={0}, Int32List[3] dilation={1}, Int32 groups=1, + "Tensor (Tensor x, Tensor weight, Tensor bias=None, Int32List[3] stride=1, + Int32List[3] padding=0, Int32List[3] dilation=1, Int32 groups=1, String channel_pos=\"channels_first\") => Conv3d" bind_python: True