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.

op_common.h 2.6 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /**
  2. * Copyright 2019 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. #ifndef PREDICT_SRC_OP_COMMON_H_
  17. #define PREDICT_SRC_OP_COMMON_H_
  18. #include <assert.h>
  19. namespace mindspore {
  20. namespace predict {
  21. static inline size_t AlignSize(size_t size, size_t align) { return (size + align - 1) & -align; }
  22. template <typename Tsrc, typename Tdst>
  23. inline void Nchw2Nhwc(const Tsrc *in, Tdst *out, size_t h, size_t w, size_t c) {
  24. MS_ASSERT(in != nullptr && out != nullptr);
  25. const size_t sz = w * h;
  26. for (size_t cc = 0; cc < c; ++cc) {
  27. auto pi = in + sz * cc;
  28. for (size_t el = 0; el < sz; ++el) {
  29. out[cc + el * c] = (Tdst)pi[el];
  30. }
  31. }
  32. }
  33. template <typename Tsrc, typename Tdst>
  34. inline void Nhwc2Nchw(const Tsrc *in, Tdst *out, size_t h, size_t w, size_t c) {
  35. MS_ASSERT(in != nullptr && out != nullptr);
  36. const size_t sz = w * h;
  37. for (auto cc = 0; cc < c; ++cc) {
  38. auto po = out + sz * cc;
  39. for (size_t el = 0; el < sz; ++el) {
  40. po[el] = (Tdst)in[cc + el * c];
  41. }
  42. }
  43. }
  44. template <typename Tsrc, typename Tdst>
  45. inline void InverseQuantization(const Tsrc *srcdata, Tdst *dstdata, size_t datanum, float *parms) {
  46. MS_ASSERT(srcdata != nullptr && dstdata != nullptr);
  47. float scale = parms[2];
  48. float zeroPoint = parms[3];
  49. for (size_t i = 0; i < datanum; ++i) {
  50. dstdata = (scale == 0) ? (0) : (Tdst)((srcdata[i] - zeroPoint) * scale);
  51. }
  52. }
  53. template <typename Tsrc, typename Tdst>
  54. inline void Astype(const Tsrc *srcdata, Tdst *dstdata, size_t datanum) {
  55. MS_ASSERT(srcdata != nullptr && dstdata != nullptr);
  56. for (size_t i = 0; i < datanum; ++i) {
  57. dstdata[i] = (Tdst)srcdata[i];
  58. }
  59. }
  60. #define MSMIN(x, y) ((x) < (y) ? (x) : (y))
  61. #define MSMAX(x, y) ((x) > (y) ? (x) : (y))
  62. #define UP_DIV(x, y) (((x) + (y) - (1)) / (y))
  63. #define DOWN_DIV(x, y) (((x) - (y) + (1)) / (y))
  64. #define ROUND_UP(x, y) (((x) + (y) - (1)) / (y) * (y))
  65. #define ALIGN_UP4(x) ROUND_UP((x), 4)
  66. #define ALIGN_UP8(x) ROUND_UP((x), 8)
  67. #define MAX_MALLOC_SIZE 100 * 1024 * 1024
  68. } // namespace predict
  69. } // namespace mindspore
  70. #endif // PREDICT_SRC_OP_COMMON_H_