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