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/softmax.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 TestSoftMax : public UT::Common { 28 public: 29 TestSoftMax() {} 30 void SetUp() {} 31 void TearDown() {} 32 }; 33 34 TEST_F(TestSoftMax, test_ops_softmax1) { 35 auto softmax = std::make_shared<Softmax>(); 36 std::vector<std::int64_t> init_data = {-1}; 37 softmax->Init(-1); 38 EXPECT_EQ(softmax->get_axis(), init_data); 39 softmax->set_axis(init_data); 40 EXPECT_EQ(softmax->get_axis(), init_data); 41 auto input1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{1, 2, 3, 4, 5}); 42 MS_EXCEPTION_IF_NULL(input1); 43 auto abstract = softmax->Infer({input1->ToAbstract()}); 44 MS_EXCEPTION_IF_NULL(abstract); 45 EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true); 46 auto shape_ptr = abstract->BuildShape(); 47 MS_EXCEPTION_IF_NULL(shape_ptr); 48 EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true); 49 auto shape = shape_ptr->cast<abstract::ShapePtr>(); 50 MS_EXCEPTION_IF_NULL(shape); 51 auto shape_vec = shape->shape(); 52 auto type = abstract->BuildType(); 53 MS_EXCEPTION_IF_NULL(type); 54 EXPECT_EQ(type->isa<TensorType>(), true); 55 auto tensor_type = type->cast<TensorTypePtr>(); 56 MS_EXCEPTION_IF_NULL(tensor_type); 57 auto data_type = tensor_type->element(); 58 MS_EXCEPTION_IF_NULL(data_type); 59 EXPECT_EQ(data_type->type_id(), kNumberTypeFloat16); 60 EXPECT_EQ(shape_vec.size(), 5); 61 EXPECT_EQ(shape_vec[0], 1); 62 } 63 64 TEST_F(TestSoftMax, test_ops_softmax2) { 65 auto softmax = std::make_shared<Softmax>(); 66 std::vector<std::int64_t> init_data = {-1}; 67 softmax->Init(-1); 68 EXPECT_EQ(softmax->get_axis(), init_data); 69 softmax->set_axis(init_data); 70 EXPECT_EQ(softmax->get_axis(), init_data); 71 auto input1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{1, 2, 3, 4, 5}); 72 MS_EXCEPTION_IF_NULL(input1); 73 auto abstract = softmax->Infer({input1->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(), kNumberTypeFloat32); 90 EXPECT_EQ(shape_vec.size(), 5); 91 EXPECT_EQ(shape_vec[0], 1); 92 } 93 94 TEST_F(TestSoftMax, test_ops_softmax3) { 95 auto softmax = std::make_shared<Softmax>(); 96 std::vector<std::int64_t> init_data = {-1}; 97 softmax->Init(-1); 98 EXPECT_EQ(softmax->get_axis(), init_data); 99 softmax->set_axis(init_data); 100 EXPECT_EQ(softmax->get_axis(), init_data); 101 auto input1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat64, std::vector<int64_t>{1, 2, 3, 4, 5}); 102 MS_EXCEPTION_IF_NULL(input1); 103 auto abstract = softmax->Infer({input1->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(), kNumberTypeFloat64); 120 EXPECT_EQ(shape_vec.size(), 5); 121 EXPECT_EQ(shape_vec[0], 1); 122 } 123 } // namespace ops 124 } // namespace mindspore 125