• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2022 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 gather_dress or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "nnacl/kernel/gather_d.h"
18 #include "nnacl/gather_parameter.h"
19 #include "nnacl/tensor_c_utils.h"
20 #include "nnacl/op_base.h"
21 #include "nnacl/base/gather_d_base.h"
22 #include "nnacl/kernel/default_kernel_base.h"
23 
24 typedef struct GatherDStru {
25   KernelBase base;
26 } GatherDStru;
27 
GatherDPrepare(struct KernelBase * self)28 int GatherDPrepare(struct KernelBase *self) {
29   GatherDStru *gather_d = (GatherDStru *)self;
30   NNACL_CHECK_NULL_RETURN_ERR(gather_d);
31   GatherParameter *param = (GatherParameter *)gather_d->base.param_;
32   NNACL_CHECK_NULL_RETURN_ERR(param);
33   NNACL_CHECK_FALSE(self->in_size_ < kInputSize2 || self->out_size_ < 1, NNACL_TENSOR_SIZE_INVALID);
34 
35   param->axis_ = ((int *)(gather_d->base.in_[1]->data_))[0];
36   return NNACL_OK;
37 }
38 
GatherDResize(struct KernelBase * self)39 int GatherDResize(struct KernelBase *self) {
40   GatherDStru *gather_d = (GatherDStru *)self;
41   NNACL_CHECK_NULL_RETURN_ERR(gather_d);
42   GatherParameter *param = (GatherParameter *)gather_d->base.param_;
43   NNACL_CHECK_NULL_RETURN_ERR(param);
44   int input_rank = (int)gather_d->base.in_[0]->shape_size_;
45   NNACL_CHECK_FALSE(param->axis_ >= input_rank || param->axis_ < -input_rank, NNACL_GATHER_D_AXIS_INVALID);
46 
47   if (param->axis_ < 0) {
48     param->axis_ = param->axis_ + input_rank;
49   }
50   return NNACL_OK;
51 }
52 
GatherDCompute(struct KernelBase * self)53 int GatherDCompute(struct KernelBase *self) {
54   GatherDStru *gather_d_stru = (GatherDStru *)self;
55   NNACL_CHECK_NULL_RETURN_ERR(gather_d_stru);
56   GatherParameter *param = (GatherParameter *)gather_d_stru->base.param_;
57   NNACL_CHECK_NULL_RETURN_ERR(param);
58 
59   TensorC *input = gather_d_stru->base.in_[0];
60   NNACL_CHECK_NULL_RETURN_ERR(input);
61   TensorC *output = gather_d_stru->base.out_[0];
62   NNACL_CHECK_NULL_RETURN_ERR(output);
63   const void *input_data = input->data_;
64   NNACL_CHECK_NULL_RETURN_ERR(input_data);
65   const void *index_data = gather_d_stru->base.in_[2]->data_;
66   NNACL_CHECK_NULL_RETURN_ERR(index_data);
67   void *output_data = output->data_;
68   NNACL_CHECK_NULL_RETURN_ERR(output_data);
69 
70   size_t input_shape[MAX_SHAPE_SIZE];
71   for (size_t i = 0; i < input->shape_size_; i++) {
72     input_shape[i] = input->shape_[i];
73   }
74   size_t output_shape[MAX_SHAPE_SIZE];
75   for (size_t i = 0; i < output->shape_size_; i++) {
76     output_shape[i] = output->shape_[i];
77   }
78 
79   int input_dtype = input->data_type_;
80   int index_dtype = gather_d_stru->base.in_[THIRD_INPUT]->data_type_;
81   int status = NNACL_ERR;
82   if (index_dtype == kNumberTypeInt32) {
83     if (input_dtype == kNumberTypeFloat32) {
84       status = GATHER_D(float, int32_t, (float *)output_data, (float *)input_data, (int32_t *)index_data, input_shape,
85                         input->shape_size_, output_shape, output->shape_size_, param->axis_);
86     } else if (input_dtype == kNumberTypeFloat16) {
87 #ifdef ENABLE_FP16
88       status = GATHER_D(float16_t, int32_t, (float16_t *)output_data, (float16_t *)input_data, (int32_t *)index_data,
89                         input_shape, input->shape_size_, output_shape, output->shape_size_, param->axis_);
90 #endif
91     } else if (input_dtype == kNumberTypeInt32) {
92       status = GATHER_D(int32_t, int32_t, (int32_t *)output_data, (int32_t *)input_data, (int32_t *)index_data,
93                         input_shape, input->shape_size_, output_shape, output->shape_size_, param->axis_);
94     }
95   } else if (index_dtype == kNumberTypeInt64) {
96     if (input_dtype == kNumberTypeFloat32) {
97       status = GATHER_D(float, int64_t, (float *)output_data, (float *)input_data, (int64_t *)index_data, input_shape,
98                         input->shape_size_, output_shape, output->shape_size_, param->axis_);
99     } else if (input_dtype == kNumberTypeFloat16) {
100 #ifdef ENABLE_FP16
101       status = GATHER_D(float16_t, int64_t, (float16_t *)output_data, (float16_t *)input_data, (int64_t *)index_data,
102                         input_shape, input->shape_size_, output_shape, output->shape_size_, param->axis_);
103 #endif
104     } else if (input_dtype == kNumberTypeInt32) {
105       status = GATHER_D(int32_t, int64_t, (int32_t *)output_data, (int32_t *)input_data, (int64_t *)index_data,
106                         input_shape, input->shape_size_, output_shape, output->shape_size_, param->axis_);
107     }
108   }
109   return status;
110 }
111 
CreateGatherD(OpParameter * param,int data_type)112 KernelBase *CreateGatherD(OpParameter *param, int data_type) {
113   GatherDStru *gather_d = (GatherDStru *)malloc(sizeof(GatherDStru));
114   NNACL_MALLOC_CHECK_NULL_RETURN_NULL(gather_d);
115   gather_d->base.Prepare = GatherDPrepare;
116   gather_d->base.Resize = GatherDResize;
117   gather_d->base.Release = DefaultRelease;
118   gather_d->base.Compute = GatherDCompute;
119   return (KernelBase *)gather_d;
120 }
121 
122 REG_KERNEL_CREATOR(PrimType_GatherD, kNumberTypeFloat32, CreateGatherD);
123 REG_KERNEL_CREATOR(PrimType_GatherD, kNumberTypeInt32, CreateGatherD);
124 REG_KERNEL_CREATOR(PrimType_GatherD, kNumberTypeFloat16, CreateGatherD);
125