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_matmul.cc 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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/mat_mul.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 TestMatMul : public UT::Common {
  27. public:
  28. TestMatMul() {}
  29. void SetUp() {}
  30. void TearDown() {}
  31. };
  32. TEST_F(TestMatMul, test_ops_matmul1) {
  33. auto matmul = std::make_shared<MatMul>();
  34. matmul->Init();
  35. EXPECT_EQ(matmul->get_transpose_a(), false);
  36. EXPECT_EQ(matmul->get_transpose_b(), false);
  37. matmul->set_transpose_a(false);
  38. matmul->set_transpose_b(false);
  39. auto tensor_x = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{1, 3});
  40. auto tensor_y = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{3, 4});
  41. auto abstract = matmul->Infer({tensor_x->ToAbstract(), tensor_y->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(), kNumberTypeFloat32);
  58. EXPECT_EQ(shape_vec.size(), 2);
  59. EXPECT_EQ(shape_vec[0], 1);
  60. EXPECT_EQ(shape_vec[1], 4);
  61. EXPECT_EQ(matmul->get_transpose_a(), false);
  62. EXPECT_EQ(matmul->get_transpose_b(), false);
  63. }
  64. TEST_F(TestMatMul, test_ops_matmul2) {
  65. auto matmul = std::make_shared<MatMul>();
  66. matmul->Init(true, false);
  67. EXPECT_EQ(matmul->get_transpose_a(), true);
  68. EXPECT_EQ(matmul->get_transpose_b(), false);
  69. matmul->set_transpose_a(true);
  70. matmul->set_transpose_b(false);
  71. auto tensor_x = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{3, 1});
  72. auto tensor_y = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{3, 4});
  73. auto abstract = matmul->Infer({tensor_x->ToAbstract(), tensor_y->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(), 2);
  91. EXPECT_EQ(shape_vec[0], 1);
  92. EXPECT_EQ(shape_vec[1], 4);
  93. EXPECT_EQ(matmul->get_transpose_a(), true);
  94. EXPECT_EQ(matmul->get_transpose_b(), false);
  95. }
  96. TEST_F(TestMatMul, test_ops_matmul3) {
  97. auto matmul = std::make_shared<MatMul>();
  98. matmul->Init(false, true);
  99. EXPECT_EQ(matmul->get_transpose_a(), false);
  100. EXPECT_EQ(matmul->get_transpose_b(), true);
  101. matmul->set_transpose_a(false);
  102. matmul->set_transpose_b(true);
  103. auto tensor_x = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{1, 3});
  104. auto tensor_y = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{4, 3});
  105. auto abstract = matmul->Infer({tensor_x->ToAbstract(), tensor_y->ToAbstract()});
  106. MS_EXCEPTION_IF_NULL(abstract);
  107. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  108. auto shape_ptr = abstract->BuildShape();
  109. MS_EXCEPTION_IF_NULL(shape_ptr);
  110. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  111. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  112. MS_EXCEPTION_IF_NULL(shape);
  113. auto shape_vec = shape->shape();
  114. auto type = abstract->BuildType();
  115. MS_EXCEPTION_IF_NULL(type);
  116. EXPECT_EQ(type->isa<TensorType>(), true);
  117. auto tensor_type = type->cast<TensorTypePtr>();
  118. MS_EXCEPTION_IF_NULL(tensor_type);
  119. auto data_type = tensor_type->element();
  120. MS_EXCEPTION_IF_NULL(data_type);
  121. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
  122. EXPECT_EQ(shape_vec.size(), 2);
  123. EXPECT_EQ(shape_vec[0], 1);
  124. EXPECT_EQ(shape_vec[1], 4);
  125. EXPECT_EQ(matmul->get_transpose_a(), false);
  126. EXPECT_EQ(matmul->get_transpose_b(), true);
  127. }
  128. } // namespace ops
  129. } // namespace mindspore