• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2020 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 
17 #include "nnacl/base/depth_to_space_base.h"
18 #include "nnacl/errorcode.h"
19 
DepthToSpaceForNHWC(const void * input,void * output,const int * in_shape,const DepthToSpaceArgs * param)20 void DepthToSpaceForNHWC(const void *input, void *output, const int *in_shape, const DepthToSpaceArgs *param) {
21   int32_t block_size = param->block_size_;
22   int32_t in_shape_dim2 = in_shape[2];
23   int32_t in_shape_dim1 = in_shape[1];
24   size_t copy_size = (size_t)block_size * param->out_stride_dim2_ * param->data_type_size_;
25   for (int i = 0; i < in_shape[0]; ++i) {
26     int64_t in_offset_n = i * param->in_stride_dim0_;
27     int64_t out_offset_n = i * param->out_stride_dim0_;
28     for (int j = 0; j < in_shape_dim1; ++j) {
29       int64_t in_offset_h = in_offset_n + j * param->in_stride_dim1_;
30       int64_t out_offset_h = out_offset_n + j * block_size * param->out_stride_dim1_;
31       for (int k = 0; k < in_shape_dim2; ++k) {
32         int64_t in_offset_w = in_offset_h + k * param->in_stride_dim2_;
33         int64_t out_offset_w = out_offset_h + k * block_size * param->out_stride_dim2_;
34         for (int l = 0; l < block_size; ++l) {
35           int64_t out_offset = (out_offset_w + l * param->out_stride_dim1_) * param->data_type_size_;
36           int64_t in_offset = (in_offset_w + l * block_size * param->out_stride_dim2_) * param->data_type_size_;
37           memcpy((int8_t *)output + out_offset, (int8_t *)input + in_offset, copy_size);
38         }
39       }
40     }
41   }
42 }
43 
DepthToSpaceCRDForNHWC(const void * input,void * output,const int * in_shape,const DepthToSpaceArgs * param)44 void DepthToSpaceCRDForNHWC(const void *input, void *output, const int *in_shape, const DepthToSpaceArgs *param) {
45   int32_t block_size = param->block_size_;
46   int32_t in_shape_dim3 = in_shape[3];
47   int32_t in_shape_dim2 = in_shape[2];
48   int32_t in_shape_dim1 = in_shape[1];
49   size_t copy_size = param->data_type_size_;
50   for (int i = 0; i < in_shape[0]; ++i) {
51     int64_t in_offset_n = i * param->in_stride_dim0_;
52     int64_t out_offset_n = i * param->out_stride_dim0_;
53     for (int j = 0; j < in_shape_dim1; ++j) {
54       int64_t in_offset_h = in_offset_n + j * param->in_stride_dim1_;
55       int64_t out_offset_h = out_offset_n + j * block_size * param->out_stride_dim1_;
56       for (int k = 0; k < in_shape_dim2; ++k) {
57         int64_t in_offset_w = in_offset_h + k * param->in_stride_dim2_;
58         int64_t out_offset_w = out_offset_h + k * block_size * param->out_stride_dim2_;
59         for (int l = 0; l < in_shape_dim3; ++l) {
60           int64_t offset = l % (block_size * block_size);
61           int64_t out_offset_c =
62             out_offset_w +
63             offset / block_size * block_size * in_shape_dim2 * in_shape_dim3 / (block_size * block_size) +
64             offset % block_size * in_shape_dim3 / (block_size * block_size);
65           int64_t out_offset = (out_offset_c + l / (block_size * block_size)) * param->data_type_size_;
66           int64_t in_offset = (in_offset_w + l) * param->data_type_size_;
67           memcpy((int8_t *)output + out_offset, (int8_t *)input + in_offset, copy_size);
68         }
69       }
70     }
71   }
72 }
73