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.

trans.h 3.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. #ifndef MINDSPORE_CCSRC_COMMON_TRANS_H
  17. #define MINDSPORE_CCSRC_COMMON_TRANS_H
  18. #include <algorithm>
  19. #include <functional>
  20. #include <map>
  21. #include <memory>
  22. #include <string>
  23. #include <utility>
  24. #include <vector>
  25. #include "ir/dtype.h"
  26. #include "backend/kernel_compiler/kernel.h"
  27. #include "ir/dtype/type.h"
  28. #include "utils/shape_utils.h"
  29. namespace mindspore {
  30. namespace trans {
  31. struct TypeIdArgs {
  32. const void *data;
  33. size_t host_shape_size; // Multiply each dimension elements. [a, b, c, d] => a*b*c*d
  34. TypeId host_data_type;
  35. TypeId device_data_type;
  36. size_t data_size;
  37. };
  38. struct FormatArgs {
  39. const void *data;
  40. const size_t device_size;
  41. std::string host_format;
  42. std::string device_format;
  43. std::vector<size_t> host_shape;
  44. std::vector<size_t> device_shape;
  45. TypeId src_data_type;
  46. };
  47. size_t CubeSizeByType(const TypeId data_type);
  48. std::vector<size_t> PaddingShapeTo4d(const std::vector<size_t> &shape, const std::vector<Axis> &padding_axis = {});
  49. ShapeVector GetRuntimePaddingShape(const AnfNodePtr &node, size_t index);
  50. bool IsNeedPadding(const std::string &format, const size_t shape_size);
  51. std::vector<size_t> TransShapeToDevice(const std::vector<size_t> &shape, const std::string &format);
  52. bool TransDataType(const TypeIdArgs &args, void *result);
  53. bool TransFormat(const FormatArgs &args, void *result);
  54. bool TransFormatFromDeviceToHost(const FormatArgs &args, void *result);
  55. // host to device
  56. bool NchwTo4D(const FormatArgs &args, void *result);
  57. bool NchwToFracZ(const FormatArgs &args, void *result);
  58. bool NchwToFracNz(const FormatArgs &args, void *result);
  59. bool NchwToNc1hwc0(const FormatArgs &args, void *result);
  60. bool NcdhwToFracZ3D(const FormatArgs &args, void *result);
  61. bool NchwToFracZc04(const FormatArgs &args, void *result);
  62. bool NchwToNc1hwc04(const FormatArgs &args, void *result);
  63. bool NchwToC1hwncoc0(const FormatArgs &args, void *result);
  64. bool NcdhwToNdc1hwc0(const FormatArgs &args, void *result);
  65. // device to host
  66. bool ToNchw(const FormatArgs &args, void *result);
  67. bool FracZToNchw(const FormatArgs &args, void *result);
  68. bool FracNzToNchw(const FormatArgs &args, void *result);
  69. bool Nc1hwc0ToNchw(const FormatArgs &args, void *result);
  70. bool Nc1hwc04ToNchw(const FormatArgs &args, void *result);
  71. bool FracZ3DToNcdhw(const FormatArgs &args, void *result);
  72. bool C1hwncoc0ToNchw(const FormatArgs &args, void *result);
  73. bool Ndc1hwc0ToNcdhw(const FormatArgs &args, void *result);
  74. using FormatTransfer = std::function<bool(const FormatArgs &, void *)>;
  75. const std::map<std::string, FormatTransfer> kTransFormatMapOfHostToDevice{
  76. {kOpFormat_FRAC_Z, NchwToFracZ}, {kOpFormat_FRAC_NZ, NchwToFracNz},
  77. {kOpFormat_NC1HWC0, NchwToNc1hwc0}, {kOpFormat_C1HWNCoC0, NchwToC1hwncoc0},
  78. {kOpFormat_FRACTAL_Z_C04, NchwToFracZc04}, {kOpFormat_NC1HWC0_C04, NchwToNc1hwc04},
  79. {kOpFormat_NDC1HWC0, NcdhwToNdc1hwc0}, {kOpFormat_FRACTAL_Z_3D, NcdhwToFracZ3D}};
  80. } // namespace trans
  81. } // namespace mindspore
  82. #endif // MINDSPORE_CCSRC_COMMON_TRANS_H