From 9ef4331f56c168b0392c78d8e26825f29b114553 Mon Sep 17 00:00:00 2001 From: alex-yuyue Date: Thu, 29 Apr 2021 11:47:13 -0400 Subject: [PATCH] Add eager tests for transform ops Signed-off-by: alex-yuyue --- tests/ut/cpp/dataset/c_api_transforms_test.cc | 11 ++- tests/ut/cpp/dataset/common/common.h | 5 +- .../python/dataset/test_eager_transforms.py | 78 +++++++++++++++++++ 3 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 tests/ut/python/dataset/test_eager_transforms.py diff --git a/tests/ut/cpp/dataset/c_api_transforms_test.cc b/tests/ut/cpp/dataset/c_api_transforms_test.cc index d314895c04e..a0afab85847 100644 --- a/tests/ut/cpp/dataset/c_api_transforms_test.cc +++ b/tests/ut/cpp/dataset/c_api_transforms_test.cc @@ -2238,15 +2238,20 @@ TEST_F(MindDataTestPipeline, TestSliceSuccess5) { u_int32_t curr_seed = GlobalContext::config_manager()->seed(); GlobalContext::config_manager()->set_seed(246); std::shared_ptr schema = Schema(); - ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {2, 2})); + // Generate a ds of 4 tensors, each tensor has 3 rows and 2 columns + ASSERT_OK(schema->add_column("col1", mindspore::DataType::kNumberTypeInt16, {3, 2})); std::shared_ptr ds = RandomData(4, schema); EXPECT_NE(ds, nullptr); ds = ds->SetNumWorkers(2); EXPECT_NE(ds, nullptr); - // Apply Slice op on ds, get the first and third elements in each row. + // Apply two SliceOptions on ds which includes 4 tensors with shape 3*2 + // The first SliceOption is to slice the first and the second row of each tensor + // The shape of result tensor changes to 2*2 std::vector indices1 = {0, 1}; SliceOption slice_option1 = SliceOption(indices1); + // The second SliceOption is to slice the last column of each tensor + // The shape of result tensor changes to 2*1 std::vector indices2 = {-1}; SliceOption slice_option2 = SliceOption(indices2); @@ -2270,7 +2275,7 @@ TEST_F(MindDataTestPipeline, TestSliceSuccess5) { while (row.size() != 0) { auto ind = row["col1"]; std::shared_ptr de_expected_tensor; - ASSERT_OK(Tensor::CreateFromVector(expected[i], TensorShape({1, 2}), &de_expected_tensor)); + ASSERT_OK(Tensor::CreateFromVector(expected[i], TensorShape({2, 1}), &de_expected_tensor)); mindspore::MSTensor expected_tensor = mindspore::MSTensor(std::make_shared(de_expected_tensor)); EXPECT_MSTENSOR_EQ(ind, expected_tensor); diff --git a/tests/ut/cpp/dataset/common/common.h b/tests/ut/cpp/dataset/common/common.h index cd0dba569eb..fb9133aa82a 100644 --- a/tests/ut/cpp/dataset/common/common.h +++ b/tests/ut/cpp/dataset/common/common.h @@ -63,10 +63,13 @@ using mindspore::StatusCode; } \ } while (false) -// Macro to compare 2 MSTensors; compare shape-size and data +// Macro to compare 2 MSTensors; compare shape-size, shape and data #define EXPECT_MSTENSOR_EQ(_mstensor1, _mstensor2) \ do { \ EXPECT_EQ(_mstensor1.Shape().size(), _mstensor2.Shape().size()); \ + for (int i = 0; i < _mstensor1.Shape().size(); i++) { \ + EXPECT_EQ(_mstensor1.Shape()[i], _mstensor2.Shape()[i]); \ + } \ EXPECT_EQ(_mstensor1.DataSize(), _mstensor2.DataSize()); \ EXPECT_EQ(std::memcmp((const void *)_mstensor1.Data().get(), (const void *)_mstensor2.Data().get(), \ _mstensor2.DataSize()), 0); \ diff --git a/tests/ut/python/dataset/test_eager_transforms.py b/tests/ut/python/dataset/test_eager_transforms.py new file mode 100644 index 00000000000..2a57f88f55e --- /dev/null +++ b/tests/ut/python/dataset/test_eager_transforms.py @@ -0,0 +1,78 @@ +# Copyright 2021 Huawei Technologies Co., Ltd +# +# 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. +# ============================================================================== +""" +Eager Tests for Transform Tensor ops +""" + +import numpy as np +import mindspore.common.dtype as mstype +import mindspore.dataset.transforms.c_transforms as data_trans + + +def test_eager_concatenate(): + """ + Test Concatenate op is callable + """ + prepend_tensor = np.array([1.4, 2., 3., 4., 4.5], dtype=np.float) + append_tensor = np.array([9., 10.3, 11., 12.], dtype=np.float) + concatenate_op = data_trans.Concatenate(0, prepend_tensor, append_tensor) + expected = np.array([1.4, 2., 3., 4., 4.5, 5., 6., 7., 8., 9., 10.3, + 11., 12.]) + assert np.array_equal(concatenate_op([5., 6., 7., 8.]), expected) + + +def test_eager_fill(): + """ + Test Fill op is callable + """ + fill_op = data_trans.Fill(3) + expected = np.array([3, 3, 3, 3]) + assert np.array_equal(fill_op([4, 5, 6, 7]), expected) + + +def test_eager_mask(): + """ + Test Mask op is callable + """ + mask_op = data_trans.Mask(data_trans.Relational.EQ, 3, mstype.bool_) + expected = np.array([False, False, True, False, False]) + assert np.array_equal(mask_op([1, 2, 3, 4, 5]), expected) + + +def test_eager_pad_end(): + """ + Test PadEnd op is callable + """ + pad_end_op = data_trans.PadEnd([3], -1) + expected = np.array([1, 2, -1]) + assert np.array_equal(pad_end_op([1, 2]), expected) + + +def test_eager_slice(): + """ + Test Slice op is callable + """ + indexing = [[0], [0, 3]] + slice_op = data_trans.Slice(*indexing) + expected = np.array([[1, 4]]) + assert np.array_equal(slice_op([[1, 2, 3, 4, 5]]), expected) + + +if __name__ == "__main__": + test_eager_concatenate() + test_eager_fill() + test_eager_mask() + test_eager_pad_end() + test_eager_slice()