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_full_connection.cc 5.0 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. namespace mindspore {
  25. namespace ops {
  26. class TestFullConnection : public UT::Common {
  27. public:
  28. TestFullConnection() {}
  29. void SetUp() {}
  30. void TearDown() {}
  31. };
  32. TEST_F(TestFullConnection, test_full_connection_1) {
  33. auto op = std::make_shared<FullConnection>();
  34. bool has_bias = false;
  35. bool use_axis = false;
  36. int64_t axis = 3;
  37. op->Init(has_bias, axis, use_axis, NO_ACTIVATION);
  38. auto tensor_1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3});
  39. auto tensor_2 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3});
  40. auto abstract = op->Infer({tensor_1->ToAbstract(), tensor_2->ToAbstract()});
  41. MS_EXCEPTION_IF_NULL(abstract);
  42. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  43. auto shape_ptr = abstract->BuildShape();
  44. MS_EXCEPTION_IF_NULL(shape_ptr);
  45. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  46. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  47. MS_EXCEPTION_IF_NULL(shape);
  48. auto shape_vec = shape->shape();
  49. auto type = abstract->BuildType();
  50. MS_EXCEPTION_IF_NULL(type);
  51. EXPECT_EQ(type->isa<TensorType>(), true);
  52. auto tensor_type = type->cast<TensorTypePtr>();
  53. MS_EXCEPTION_IF_NULL(tensor_type);
  54. auto data_type = tensor_type->element();
  55. MS_EXCEPTION_IF_NULL(data_type);
  56. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat16);
  57. EXPECT_EQ(shape_vec.size(), 2);
  58. EXPECT_EQ(shape_vec[0], 2);
  59. EXPECT_EQ(shape_vec[1], 2);
  60. }
  61. TEST_F(TestFullConnection, test_full_connection_2) {
  62. auto op = std::make_shared<FullConnection>();
  63. bool has_bias = true;
  64. bool use_axis = false;
  65. int64_t axis = 1;
  66. op->Init(has_bias, axis, use_axis, NO_ACTIVATION);
  67. auto tensor_1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3});
  68. auto tensor_2 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3});
  69. auto tensor_3 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 2});
  70. auto abstract = op->Infer({tensor_1->ToAbstract(), tensor_2->ToAbstract(), tensor_3->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(), kNumberTypeFloat16);
  87. EXPECT_EQ(shape_vec.size(), 2);
  88. EXPECT_EQ(shape_vec[0], 2);
  89. EXPECT_EQ(shape_vec[1], 2);
  90. }
  91. TEST_F(TestFullConnection, test_full_connection_3) {
  92. auto op = std::make_shared<FullConnection>();
  93. bool has_bias = false;
  94. bool use_axis = true;
  95. int64_t axis = 1;
  96. op->Init(has_bias, axis, use_axis, NO_ACTIVATION);
  97. auto tensor_1 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3});
  98. auto tensor_2 = TensorConstructUtils::CreateOnesTensor(kNumberTypeFloat16, std::vector<int64_t>{2, 3});
  99. auto abstract = op->Infer({tensor_1->ToAbstract(), tensor_2->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(), kNumberTypeFloat16);
  116. EXPECT_EQ(shape_vec.size(), 2);
  117. EXPECT_EQ(shape_vec[0], 2);
  118. EXPECT_EQ(shape_vec[1], 2);
  119. }
  120. } // namespace ops
  121. } // namespace mindspore