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_unsqueeze.cc 3.8 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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/unsqueeze.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 TestUnsqueeze : public UT::Common {
  27. public:
  28. TestUnsqueeze() {}
  29. void SetUp() {}
  30. void TearDown() {}
  31. };
  32. /*TEST_F(TestUnsqueeze, test_unsqueeze_1) {
  33. auto unsqueeze = std::make_shared<Unsqueeze>();
  34. std::vector<int64_t> axis = {};
  35. unsqueeze->Init(axis);
  36. auto tensor_x = std::make_shared<tensor::Tensor>(kNumberTypeFloat16, std::vector<int64_t>{1, 3, 2});
  37. MS_EXCEPTION_IF_NULL(tensor_x);
  38. auto tensor_x_data = reinterpret_cast<int *>(tensor_x->data_c());
  39. *tensor_x_data = 1;
  40. tensor_x_data++;
  41. *tensor_x_data = 2;
  42. tensor_x_data++;
  43. *tensor_x_data = 3;
  44. tensor_x_data++;
  45. *tensor_x_data = 4;
  46. tensor_x_data++;
  47. *tensor_x_data = 5;
  48. tensor_x_data++;
  49. *tensor_x_data = 6;
  50. tensor_x_data++;
  51. auto abstract = unsqueeze->Infer({tensor_x->ToAbstract()});
  52. MS_EXCEPTION_IF_NULL(abstract);
  53. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  54. auto shape_ptr = abstract->BuildShape();
  55. MS_EXCEPTION_IF_NULL(shape_ptr);
  56. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  57. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  58. MS_EXCEPTION_IF_NULL(shape);
  59. auto shape_vec = shape->shape();
  60. EXPECT_EQ(shape_vec.size(), 2);
  61. EXPECT_EQ(shape_vec[0], 3);
  62. EXPECT_EQ(shape_vec[1], 2);
  63. auto type = abstract->BuildType();
  64. MS_EXCEPTION_IF_NULL(type);
  65. EXPECT_EQ(type->isa<TensorType>(), true);
  66. auto tensor_type = type->cast<TensorTypePtr>();
  67. MS_EXCEPTION_IF_NULL(tensor_type);
  68. auto data_type = tensor_type->element();
  69. MS_EXCEPTION_IF_NULL(data_type);
  70. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat16);
  71. }
  72. */
  73. TEST_F(TestUnsqueeze, test_unsqueeze_1) {
  74. auto unsqueeze = std::make_shared<Unsqueeze>();
  75. std::vector<int64_t> axis = {1, 2};
  76. unsqueeze->Init(axis);
  77. auto tensor_x = std::make_shared<tensor::Tensor>(kNumberTypeFloat32, std::vector<int64_t>{1, 3});
  78. MS_EXCEPTION_IF_NULL(tensor_x);
  79. auto tensor_x_data = reinterpret_cast<int *>(tensor_x->data_c());
  80. *tensor_x_data = 1;
  81. tensor_x_data++;
  82. *tensor_x_data = 2;
  83. tensor_x_data++;
  84. *tensor_x_data = 3;
  85. tensor_x_data++;
  86. auto abstract = unsqueeze->Infer({tensor_x->ToAbstract()});
  87. MS_EXCEPTION_IF_NULL(abstract);
  88. EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
  89. auto shape_ptr = abstract->BuildShape();
  90. MS_EXCEPTION_IF_NULL(shape_ptr);
  91. EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
  92. auto shape = shape_ptr->cast<abstract::ShapePtr>();
  93. MS_EXCEPTION_IF_NULL(shape);
  94. auto shape_vec = shape->shape();
  95. EXPECT_EQ(shape_vec.size(), 4);
  96. EXPECT_EQ(shape_vec[0], 1);
  97. EXPECT_EQ(shape_vec[1], 1);
  98. EXPECT_EQ(shape_vec[2], 1);
  99. EXPECT_EQ(shape_vec[3], 3);
  100. auto type = abstract->BuildType();
  101. MS_EXCEPTION_IF_NULL(type);
  102. EXPECT_EQ(type->isa<TensorType>(), true);
  103. auto tensor_type = type->cast<TensorTypePtr>();
  104. MS_EXCEPTION_IF_NULL(tensor_type);
  105. auto data_type = tensor_type->element();
  106. MS_EXCEPTION_IF_NULL(data_type);
  107. EXPECT_EQ(data_type->type_id(), kNumberTypeFloat32);
  108. }
  109. } // namespace ops
  110. } // namespace mindspore