You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_ops_softmax.cc 4.7 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. namespace mindspore {
  25. namespace ops {
  26. class TestSoftMax : public UT::Common {
  27. public:
  28. TestSoftMax() {}
  29. void SetUp() {}
  30. void TearDown() {}
  31. };
  32. TEST_F(TestSoftMax, test_ops_softmax1) {
  33. auto softmax = std::make_shared<Softmax>();
  34. std::vector<std::int64_t> init_data = {-1};
  35. softmax->Init(-1);
  36. EXPECT_EQ(softmax->get_axis(), init_data);
  37. softmax->set_axis(init_data);
  38. EXPECT_EQ(softmax->get_axis(), init_data);
  39. auto input1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{1, 2, 3, 4, 5});
  40. MS_EXCEPTION_IF_NULL(input1);
  41. auto abstract = softmax->Infer({input1->ToAbstract()});
  42. MS_EXCEPTION_IF_NULL(abstract);
  43. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  44. auto shape_ptr = abstract->BuildShape();
  45. MS_EXCEPTION_IF_NULL(shape_ptr);
  46. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  47. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  48. MS_EXCEPTION_IF_NULL(shape);
  49. auto shape_vec = shape->shape();
  50. auto type = abstract->BuildType();
  51. MS_EXCEPTION_IF_NULL(type);
  52. EXPECT_EQ(type->isa<TensorType>(), true);
  53. auto tensor_type = type->cast<TensorTypePtr>();
  54. MS_EXCEPTION_IF_NULL(tensor_type);
  55. auto data_type = tensor_type->element();
  56. MS_EXCEPTION_IF_NULL(data_type);
  57. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat16);
  58. EXPECT_EQ(shape_vec.size(), 5);
  59. EXPECT_EQ(shape_vec[0], 1);
  60. }
  61. TEST_F(TestSoftMax, test_ops_softmax2) {
  62. auto softmax = std::make_shared<Softmax>();
  63. std::vector<std::int64_t> init_data = {-1};
  64. softmax->Init(-1);
  65. EXPECT_EQ(softmax->get_axis(), init_data);
  66. softmax->set_axis(init_data);
  67. EXPECT_EQ(softmax->get_axis(), init_data);
  68. auto input1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat32, std::vector<int64_t>{1, 2, 3, 4, 5});
  69. MS_EXCEPTION_IF_NULL(input1);
  70. auto abstract = softmax->Infer({input1->ToAbstract()});
  71. MS_EXCEPTION_IF_NULL(abstract);
  72. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  73. auto shape_ptr = abstract->BuildShape();
  74. MS_EXCEPTION_IF_NULL(shape_ptr);
  75. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  76. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  77. MS_EXCEPTION_IF_NULL(shape);
  78. auto shape_vec = shape->shape();
  79. auto type = abstract->BuildType();
  80. MS_EXCEPTION_IF_NULL(type);
  81. EXPECT_EQ(type->isa<TensorType>(), true);
  82. auto tensor_type = type->cast<TensorTypePtr>();
  83. MS_EXCEPTION_IF_NULL(tensor_type);
  84. auto data_type = tensor_type->element();
  85. MS_EXCEPTION_IF_NULL(data_type);
  86. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
  87. EXPECT_EQ(shape_vec.size(), 5);
  88. EXPECT_EQ(shape_vec[0], 1);
  89. }
  90. TEST_F(TestSoftMax, test_ops_softmax3) {
  91. auto softmax = std::make_shared<Softmax>();
  92. std::vector<std::int64_t> init_data = {-1};
  93. softmax->Init(-1);
  94. EXPECT_EQ(softmax->get_axis(), init_data);
  95. softmax->set_axis(init_data);
  96. EXPECT_EQ(softmax->get_axis(), init_data);
  97. auto input1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat64, std::vector<int64_t>{1, 2, 3, 4, 5});
  98. MS_EXCEPTION_IF_NULL(input1);
  99. auto abstract = softmax->Infer({input1->ToAbstract()});
  100. MS_EXCEPTION_IF_NULL(abstract);
  101. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  102. auto shape_ptr = abstract->BuildShape();
  103. MS_EXCEPTION_IF_NULL(shape_ptr);
  104. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  105. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  106. MS_EXCEPTION_IF_NULL(shape);
  107. auto shape_vec = shape->shape();
  108. auto type = abstract->BuildType();
  109. MS_EXCEPTION_IF_NULL(type);
  110. EXPECT_EQ(type->isa<TensorType>(), true);
  111. auto tensor_type = type->cast<TensorTypePtr>();
  112. MS_EXCEPTION_IF_NULL(tensor_type);
  113. auto data_type = tensor_type->element();
  114. MS_EXCEPTION_IF_NULL(data_type);
  115. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat64);
  116. EXPECT_EQ(shape_vec.size(), 5);
  117. EXPECT_EQ(shape_vec[0], 1);
  118. }
  119. } // namespace ops
  120. } // namespace mindspore