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_topk.cc 2.9 kB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/topk.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 TestTopK : public UT::Common {
  27. public:
  28. TestTopK() {}
  29. void SetUp() {}
  30. void TearDown() {}
  31. };
  32. TEST_F(TestTopK, test_topk) {
  33. auto topk = std::make_shared<TopK>();
  34. bool sorted = true;
  35. topk->Init(sorted);
  36. EXPECT_EQ(topk->get_sorted(), true);
  37. auto tensor_x = std::make_shared<tensor::Tensor>(kNumberTypeFloat16, std::vector<int64_t>{5});
  38. MS_EXCEPTION_IF_NULL(tensor_x);
  39. auto tensor_x_data = reinterpret_cast<int *>(tensor_x->data_c());
  40. *tensor_x_data = 1;
  41. tensor_x_data++;
  42. *tensor_x_data = 2;
  43. tensor_x_data++;
  44. *tensor_x_data = 3;
  45. tensor_x_data++;
  46. *tensor_x_data = 4;
  47. tensor_x_data++;
  48. *tensor_x_data = 5;
  49. tensor_x_data++;
  50. auto k = MakeValue(3);
  51. MS_EXCEPTION_IF_NULL(k);
  52. auto abstract = topk->Infer({tensor_x->ToAbstract(), k->ToAbstract()});
  53. MS_EXCEPTION_IF_NULL(abstract);
  54. EXPECT_EQ(abstract->isa<abstract::AbstractTuple>(), true);
  55. auto shape_ptr = abstract->BuildShape();
  56. MS_EXCEPTION_IF_NULL(shape_ptr);
  57. EXPECT_EQ(shape_ptr->isa<abstract::TupleShape>(), true);
  58. auto shape = shape_ptr->cast<abstract::TupleShapePtr>();
  59. MS_EXCEPTION_IF_NULL(shape);
  60. auto shape_vec = shape->shape();
  61. EXPECT_EQ(shape_vec.size(), 2);
  62. auto shape1 = shape_vec[0]->cast<abstract::ShapePtr>()->shape();
  63. EXPECT_EQ(shape1.size(), 1);
  64. EXPECT_EQ(shape1[0], 3);
  65. auto shape2 = shape_vec[1]->cast<abstract::ShapePtr>()->shape();
  66. EXPECT_EQ(shape2.size(), 1);
  67. EXPECT_EQ(shape2[0], 3);
  68. auto type_ptr = abstract->BuildType();
  69. MS_EXCEPTION_IF_NULL(type_ptr);
  70. auto type = type_ptr->cast<TuplePtr>();
  71. auto type_vec = type->elements();
  72. EXPECT_EQ(type_vec.size(), 2);
  73. MS_EXCEPTION_IF_NULL(type_vec[0]);
  74. auto data_type1 = type_vec[0]->cast<TensorTypePtr>()->element();
  75. MS_EXCEPTION_IF_NULL(data_type1);
  76. EXPECT_EQ(data_type1->type_id(), kNumberTypeFloat16);
  77. auto data_type2 = type_vec[1]->cast<TensorTypePtr>()->element();
  78. MS_EXCEPTION_IF_NULL(data_type2);
  79. EXPECT_EQ(data_type2->type_id(), kNumberTypeInt32);
  80. }
  81. } // namespace ops
  82. } // namespace mindspore