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.

space_to_batch.c 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #include "nnacl/fp32/space_to_batch.h"
  17. #include "nnacl/arithmetic_common.h"
  18. void DoSpaceToBatchNHWC(const float *input, float *output, int *block_sizes, int *in_shape, int *out_shape) {
  19. int out_dim0 = out_shape[0];
  20. int out_dim1 = out_shape[1];
  21. int out_dim2 = out_shape[2];
  22. int copy_num = out_shape[3];
  23. int block_w = block_sizes[1];
  24. int block_h = block_sizes[0];
  25. int in_strides[4];
  26. ComputeStrides(in_shape, in_strides, 4);
  27. int out_strides[4];
  28. ComputeStrides(out_shape, out_strides, 4);
  29. size_t copy_size = copy_num * sizeof(float);
  30. size_t out_offset = 0;
  31. for (int n = 0; n < out_dim0; ++n) {
  32. int in_n = n % in_shape[0];
  33. int32_t stride_w = (n / in_shape[0]) % block_w;
  34. int32_t stride_h = (n / in_shape[0]) / block_w;
  35. size_t in_offset0 = in_n * in_strides[0];
  36. for (int h = 0; h < out_dim1; ++h) {
  37. size_t in_offset1 = in_offset0 + (h * block_h + stride_h) * in_strides[1];
  38. for (int w = 0; w < out_dim2; ++w) {
  39. size_t in_offset2 = in_offset1 + (w * block_w + stride_w) * in_strides[2];
  40. memcpy(output + out_offset, input + in_offset2, copy_size);
  41. out_offset += copy_num;
  42. }
  43. }
  44. }
  45. }
  46. void DoSpaceToBatchPaddingNHWC(const float *input, float *output, int *in_shape, int *padding, int *out_shape) {
  47. int in_h = in_shape[1];
  48. int in_w = in_shape[2];
  49. int in_c = in_shape[3];
  50. int out_w = out_shape[2];
  51. int out_c = out_shape[3];
  52. size_t ped_h_num = out_w * out_c;
  53. size_t ped_h_size = ped_h_num * sizeof(float);
  54. size_t ped_w_size = out_c * sizeof(float);
  55. size_t out_offset = 0;
  56. int in_strides[4];
  57. ComputeStrides(in_shape, in_strides, 4);
  58. int out_strides[4];
  59. ComputeStrides(out_shape, out_strides, 4);
  60. size_t copy_size = in_c * sizeof(float);
  61. for (int i = 0; i < in_shape[0]; ++i) {
  62. size_t in_offset0 = i * in_strides[0];
  63. for (int pad_h_top = 0; pad_h_top < padding[0]; ++pad_h_top) {
  64. memset(output + out_offset, 0, ped_h_size);
  65. out_offset += ped_h_num;
  66. }
  67. for (int j = 0; j < in_h; ++j) {
  68. size_t in_offset1 = in_offset0 + j * in_strides[1];
  69. for (int pad_w_left = 0; pad_w_left < padding[2]; ++pad_w_left) {
  70. memset(output + out_offset, 0, ped_w_size);
  71. out_offset += out_c;
  72. }
  73. for (int k = 0; k < in_w; ++k) {
  74. size_t in_offset2 = in_offset1 + k * in_strides[2];
  75. memcpy(output + out_offset, input + in_offset2, copy_size);
  76. out_offset += in_c;
  77. }
  78. for (int pad_w_right = 0; pad_w_right < padding[3]; ++pad_w_right) {
  79. memset(output + out_offset, 0, ped_w_size);
  80. out_offset += out_c;
  81. }
  82. }
  83. for (int pad_h_bottom = 0; pad_h_bottom < padding[1]; ++pad_h_bottom) {
  84. memset(output + out_offset, 0, ped_h_size);
  85. out_offset += ped_h_num;
  86. }
  87. }
  88. }