• 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/gather.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 TestGather : public UT::Common {
28  public:
29   TestGather() {}
30   void SetUp() {}
31   void TearDown() {}
32 };
33 
34 TEST_F(TestGather, test_gather) {
35   auto gather = std::make_shared<Gather>();
36   gather->Init();
37   auto tensor_x = std::make_shared<tensor::Tensor>(kNumberTypeInt32, std::vector<int64_t>{2, 2});
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   auto index = std::make_shared<tensor::Tensor>(kNumberTypeInt32, std::vector<int64_t>{2, 2});
49   MS_EXCEPTION_IF_NULL(index);
50   auto index_data = reinterpret_cast<int *>(index->data_c());
51   *index_data = 0;
52   index_data++;
53   *index_data = 0;
54   index_data++;
55   *index_data = 1;
56   index_data++;
57   *index_data = 0;
58   index_data++;
59   auto dim = MakeValue(1);
60   MS_EXCEPTION_IF_NULL(dim);
61   auto abstract = gather->Infer({tensor_x->ToAbstract(), dim->ToAbstract(), index->ToAbstract()});
62   MS_EXCEPTION_IF_NULL(abstract);
63   EXPECT_EQ(abstract->isa<abstract::AbstractTensor>(), true);
64   auto shape_ptr = abstract->BuildShape();
65   MS_EXCEPTION_IF_NULL(shape_ptr);
66   EXPECT_EQ(shape_ptr->isa<abstract::Shape>(), true);
67   auto shape = shape_ptr->cast<abstract::ShapePtr>();
68   MS_EXCEPTION_IF_NULL(shape);
69   auto shape_vec = shape->shape();
70   auto type = abstract->BuildType();
71   MS_EXCEPTION_IF_NULL(type);
72   EXPECT_EQ(type->isa<TensorType>(), true);
73   auto tensor_type = type->cast<TensorTypePtr>();
74   MS_EXCEPTION_IF_NULL(tensor_type);
75   auto data_type = tensor_type->element();
76   MS_EXCEPTION_IF_NULL(data_type);
77   EXPECT_EQ(data_type->type_id(), kNumberTypeInt32);
78   EXPECT_EQ(shape_vec.size(), 2);
79   EXPECT_EQ(shape_vec[0], 2);
80   EXPECT_EQ(shape_vec[1], 2);
81 }
82 }  // namespace ops
83 }  // namespace mindspore
84