1 /** 2 * Copyright 2021 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/fusion/full_connection.h" 20 #include "ir/dtype/type.h" 21 #include "ir/value.h" 22 #include "abstract/dshape.h" 23 #include "utils/tensor_construct_utils.h" 24 25 namespace mindspore { 26 namespace ops { 27 class TestFullConnection : public UT::Common { 28 public: 29 TestFullConnection() {} 30 void SetUp() {} 31 void TearDown() {} 32 }; 33 34 TEST_F(TestFullConnection, test_full_connection_1) { 35 auto op = std::make_shared<FullConnection>(); 36 bool has_bias = false; 37 bool use_axis = false; 38 int64_t axis = 3; 39 op->Init(has_bias, axis, use_axis, NO_ACTIVATION); 40 auto tensor_1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3}); 41 auto tensor_2 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3}); 42 auto abstract = op->Infer({tensor_1->ToAbstract(), tensor_2->ToAbstract()}); 43 MS_EXCEPTION_IF_NULL(abstract); 44 EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true); 45 auto shape_ptr = abstract->BuildShape(); 46 MS_EXCEPTION_IF_NULL(shape_ptr); 47 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true); 48 auto shape = shape_ptr->cast<abstract::ShapePtr>(); 49 MS_EXCEPTION_IF_NULL(shape); 50 auto shape_vec = shape->shape(); 51 auto type = abstract->BuildType(); 52 MS_EXCEPTION_IF_NULL(type); 53 EXPECT_EQ(type->isa<TensorType>(), true); 54 auto tensor_type = type->cast<TensorTypePtr>(); 55 MS_EXCEPTION_IF_NULL(tensor_type); 56 auto data_type = tensor_type->element(); 57 MS_EXCEPTION_IF_NULL(data_type); 58 EXPECT_EQ(data_type->type_id(), kNumberTypeFloat16); 59 EXPECT_EQ(shape_vec.size(), 2); 60 EXPECT_EQ(shape_vec[0], 2); 61 EXPECT_EQ(shape_vec[1], 2); 62 } 63 64 TEST_F(TestFullConnection, test_full_connection_2) { 65 auto op = std::make_shared<FullConnection>(); 66 bool has_bias = true; 67 bool use_axis = false; 68 int64_t axis = 1; 69 op->Init(has_bias, axis, use_axis, NO_ACTIVATION); 70 auto tensor_1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3}); 71 auto tensor_2 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3}); 72 auto tensor_3 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 2}); 73 auto abstract = op->Infer({tensor_1->ToAbstract(), tensor_2->ToAbstract(), tensor_3->ToAbstract()}); 74 MS_EXCEPTION_IF_NULL(abstract); 75 EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true); 76 auto shape_ptr = abstract->BuildShape(); 77 MS_EXCEPTION_IF_NULL(shape_ptr); 78 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true); 79 auto shape = shape_ptr->cast<abstract::ShapePtr>(); 80 MS_EXCEPTION_IF_NULL(shape); 81 auto shape_vec = shape->shape(); 82 auto type = abstract->BuildType(); 83 MS_EXCEPTION_IF_NULL(type); 84 EXPECT_EQ(type->isa<TensorType>(), true); 85 auto tensor_type = type->cast<TensorTypePtr>(); 86 MS_EXCEPTION_IF_NULL(tensor_type); 87 auto data_type = tensor_type->element(); 88 MS_EXCEPTION_IF_NULL(data_type); 89 EXPECT_EQ(data_type->type_id(), kNumberTypeFloat16); 90 EXPECT_EQ(shape_vec.size(), 2); 91 EXPECT_EQ(shape_vec[0], 2); 92 EXPECT_EQ(shape_vec[1], 2); 93 } 94 95 TEST_F(TestFullConnection, test_full_connection_3) { 96 auto op = std::make_shared<FullConnection>(); 97 bool has_bias = false; 98 bool use_axis = true; 99 int64_t axis = 1; 100 op->Init(has_bias, axis, use_axis, NO_ACTIVATION); 101 auto tensor_1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3}); 102 auto tensor_2 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3}); 103 auto abstract = op->Infer({tensor_1->ToAbstract(), tensor_2->ToAbstract()}); 104 MS_EXCEPTION_IF_NULL(abstract); 105 EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true); 106 auto shape_ptr = abstract->BuildShape(); 107 MS_EXCEPTION_IF_NULL(shape_ptr); 108 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true); 109 auto shape = shape_ptr->cast<abstract::ShapePtr>(); 110 MS_EXCEPTION_IF_NULL(shape); 111 auto shape_vec = shape->shape(); 112 auto type = abstract->BuildType(); 113 MS_EXCEPTION_IF_NULL(type); 114 EXPECT_EQ(type->isa<TensorType>(), true); 115 auto tensor_type = type->cast<TensorTypePtr>(); 116 MS_EXCEPTION_IF_NULL(tensor_type); 117 auto data_type = tensor_type->element(); 118 MS_EXCEPTION_IF_NULL(data_type); 119 EXPECT_EQ(data_type->type_id(), kNumberTypeFloat16); 120 EXPECT_EQ(shape_vec.size(), 2); 121 EXPECT_EQ(shape_vec[0], 2); 122 EXPECT_EQ(shape_vec[1], 2); 123 } 124 } // namespace ops 125 } // namespace mindspore 126