1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #include <vector> 17 #include <memory> 18 #include "common/common_test.h" 19 #include "ops/conv2d.h" 20 #include "ir/dtype/type.h" 21 #include "abstract/dshape.h" 22 #include "utils/tensor_construct_utils.h" 23 namespace mindspore { 24 namespace ops { 25 class TestConv2d : public UT::Common { 26 public: 27 TestConv2d() {} 28 void SetUp() {} 29 void TearDown() {} 30 }; 31 32 TEST_F(TestConv2d, test_ops_conv2d) { 33 auto conv_2d = std::make_shared<Conv2D>(); 34 conv_2d->Init(64, {7, 7}); 35 std::vector<int64_t> kernel_size = conv_2d->get_kernel_size(); 36 for (auto item : kernel_size) { 37 EXPECT_EQ(item, 7); 38 } 39 std::vector<int64_t> stride = conv_2d->get_stride(); 40 for (auto item : stride) { 41 EXPECT_EQ(item, 1); 42 } 43 std::vector<int64_t> dilation = conv_2d->get_dilation(); 44 for (auto item : dilation) { 45 EXPECT_EQ(item, 1); 46 } 47 EXPECT_EQ(conv_2d->get_pad_mode(), VALID); 48 std::vector<int64_t> pad = conv_2d->get_pad(); 49 for (auto item : pad) { 50 EXPECT_EQ(item, 0); 51 } 52 EXPECT_EQ(conv_2d->get_mode(), 1); 53 EXPECT_EQ(conv_2d->get_group(), 1); 54 EXPECT_EQ(conv_2d->get_out_channel(), 64); 55 EXPECT_EQ(conv_2d->get_format(), NCHW); 56 auto tensor_x = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{32, 3, 224, 224}); 57 auto tensor_w = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{64, 3, 7, 7}); 58 MS_EXCEPTION_IF_NULL(tensor_x); 59 MS_EXCEPTION_IF_NULL(tensor_w); 60 auto conv_abstract = conv_2d->Infer({tensor_x->ToAbstract(), tensor_w->ToAbstract()}); 61 MS_EXCEPTION_IF_NULL(conv_abstract); 62 EXPECT_EQ(conv_abstract->isa<abstract::AbstractTensor>(), true); 63 auto shape_ptr = conv_abstract->BuildShape(); 64 MS_EXCEPTION_IF_NULL(shape_ptr); 65 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true); 66 auto conv_shape = shape_ptr->cast<abstract::ShapePtr>(); 67 MS_EXCEPTION_IF_NULL(conv_shape); 68 auto shape_vec = conv_shape->shape(); 69 auto type = conv_abstract->BuildType(); 70 MS_EXCEPTION_IF_NULL(type); 71 EXPECT_EQ(type->isa<TensorType>(), true); 72 auto tensor_type = type->cast<TensorTypePtr>(); 73 MS_EXCEPTION_IF_NULL(tensor_type); 74 auto elem_type = tensor_type->element(); 75 EXPECT_EQ(elem_type->type_id(), kNumberTypeFloat32); 76 EXPECT_EQ(shape_vec.size(), 4); 77 EXPECT_EQ(shape_vec[0], 32); 78 EXPECT_EQ(shape_vec[1], 64); 79 EXPECT_EQ(shape_vec[2], 218); 80 EXPECT_EQ(shape_vec[3], 218); 81 } 82 83 } // namespace ops 84 } // namespace mindspore 85