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.

convert_utils.h 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_H_
  17. #define MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_H_
  18. #include <limits>
  19. #include <memory>
  20. #include "pybind11/pybind11.h"
  21. #include "utils/any.h"
  22. #include "ir/base.h"
  23. #include "ir/anf.h"
  24. #include "utils/base_ref.h"
  25. namespace py = pybind11;
  26. namespace mindspore {
  27. py::object AnyToPyData(const Any &value);
  28. py::object BaseRefToPyData(const BaseRef &value);
  29. bool BaseRefToBool(const BaseRef &in, bool *out);
  30. bool ValueToBool(const ValuePtr &in, bool *out);
  31. py::object ValuePtrToPyData(const ValuePtr &value);
  32. inline int SizeToInt(size_t u) {
  33. if (u > static_cast<size_t>((std::numeric_limits<int>::max)())) {
  34. MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int.";
  35. }
  36. return static_cast<int>(u);
  37. }
  38. inline uint32_t SizeToUint(size_t u) {
  39. if (u > static_cast<size_t>((std::numeric_limits<uint32_t>::max)())) {
  40. MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of uint32_t.";
  41. }
  42. return static_cast<uint32_t>(u);
  43. }
  44. inline int64_t SizeToLong(size_t u) {
  45. if (u > static_cast<size_t>((std::numeric_limits<int64_t>::max)())) {
  46. MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of int64_t.";
  47. }
  48. return static_cast<int64_t>(u);
  49. }
  50. inline size_t IntToSize(int u) {
  51. if (u < 0) {
  52. MS_LOG(EXCEPTION) << "The int value(" << u << ") is less than 0.";
  53. }
  54. return static_cast<size_t>(u);
  55. }
  56. inline size_t LongToSize(int64_t u) {
  57. if (u < 0) {
  58. MS_LOG(EXCEPTION) << "The int64_t value(" << u << ") is less than 0.";
  59. }
  60. return static_cast<size_t>(u);
  61. }
  62. inline size_t FloatToSize(float u) {
  63. if (u < 0) {
  64. MS_LOG(EXCEPTION) << "The float value(" << u << ") is less than 0.";
  65. }
  66. if (u > static_cast<float>((std::numeric_limits<size_t>::max)())) {
  67. MS_LOG(EXCEPTION) << "The float value(" << u << ") exceeds the maximum value of size_t.";
  68. }
  69. return static_cast<size_t>(u);
  70. }
  71. inline float IntToFloat(int32_t v) { return static_cast<float>(v); }
  72. inline uint32_t IntToUint(int32_t u) {
  73. if (u < 0) {
  74. MS_LOG(EXCEPTION) << "The int32_t value(" << u << ") is less than 0.";
  75. }
  76. return static_cast<uint32_t>(u);
  77. }
  78. inline int32_t UintToInt(uint32_t u) {
  79. if (u > static_cast<uint32_t>((std::numeric_limits<int32_t>::max)())) {
  80. MS_LOG(EXCEPTION) << "The uint32_t value(" << u << ") exceeds the maximum value of int32_t.";
  81. }
  82. return static_cast<int32_t>(u);
  83. }
  84. inline unsigned int UlongToUint(size_t u) {
  85. if (u > static_cast<size_t>((std::numeric_limits<unsigned int>::max)())) {
  86. MS_LOG(EXCEPTION) << "The size_t value(" << u << ") exceeds the maximum value of unsigned int.";
  87. }
  88. return static_cast<unsigned int>(u);
  89. }
  90. inline void IntMulWithOverflowCheck(int a, int b, int *c) {
  91. int out = a * b;
  92. if (a != 0) {
  93. bool ok = ((out / a) != b);
  94. if (ok) {
  95. MS_LOG(EXCEPTION) << "Mul: a(" << a << ") * b(" << b << ") result is overflow";
  96. }
  97. }
  98. *c = out;
  99. }
  100. inline uint8_t *AddressOffset(void *address, size_t offset) {
  101. MS_EXCEPTION_IF_NULL(address);
  102. return static_cast<uint8_t *>(address) + offset;
  103. }
  104. AbstractBasePtr PyListDtype2AbstractTensor(const py::object &shape_obj, const py::object &type_obj);
  105. bool IsGraphOutputValueNodeOrParameter(const AnfNodePtr &output, const py::tuple &args,
  106. const std::shared_ptr<py::object> &ret_val);
  107. } // namespace mindspore
  108. #endif // MINDSPORE_CCSRC_UTILS_CONVERT_UTILS_H_