fd_scalar.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #pragma once
  15. #include <cstdint>
  16. #include <limits>
  17. #include "ultra_infer/core/fd_type.h"
  18. #include "ultra_infer/core/float16.h"
  19. namespace ultra_infer {
  20. class Scalar {
  21. public:
  22. // Constructor support implicit
  23. Scalar() : Scalar(0) {}
  24. Scalar(double val) : dtype_(FDDataType::FP64) { // NOLINT
  25. data_.f64 = val;
  26. }
  27. Scalar(float val) : dtype_(FDDataType::FP32) { // NOLINT
  28. data_.f32 = val;
  29. }
  30. Scalar(float16 val) : dtype_(FDDataType::FP16) { // NOLINT
  31. data_.f16 = val;
  32. }
  33. Scalar(int64_t val) : dtype_(FDDataType::INT64) { // NOLINT
  34. data_.i64 = val;
  35. }
  36. Scalar(int32_t val) : dtype_(FDDataType::INT32) { // NOLINT
  37. data_.i32 = val;
  38. }
  39. Scalar(int16_t val) : dtype_(FDDataType::INT16) { // NOLINT
  40. data_.i16 = val;
  41. }
  42. Scalar(int8_t val) : dtype_(FDDataType::INT8) { // NOLINT
  43. data_.i8 = val;
  44. }
  45. Scalar(uint8_t val) : dtype_(FDDataType::UINT8) { // NOLINT
  46. data_.ui8 = val;
  47. }
  48. Scalar(bool val) : dtype_(FDDataType::BOOL) { // NOLINT
  49. data_.b = val;
  50. }
  51. // The compatible method for fliud operators,
  52. // and it will be removed in the future.
  53. explicit Scalar(const std::string &str_value) : dtype_(FDDataType::FP64) {
  54. if (str_value == "inf") {
  55. data_.f64 = std::numeric_limits<double>::infinity();
  56. } else if (str_value == "-inf") {
  57. data_.f64 = -std::numeric_limits<double>::infinity();
  58. } else if (str_value == "nan") {
  59. data_.f64 = std::numeric_limits<double>::quiet_NaN();
  60. } else {
  61. data_.f64 = std::stod(str_value);
  62. }
  63. }
  64. template <typename RT> inline RT to() const {
  65. switch (dtype_) {
  66. case FDDataType::FP32:
  67. return static_cast<RT>(data_.f32);
  68. case FDDataType::FP64:
  69. return static_cast<RT>(data_.f64);
  70. case FDDataType::FP16:
  71. return static_cast<RT>(data_.f16);
  72. case FDDataType::INT32:
  73. return static_cast<RT>(data_.i32);
  74. case FDDataType::INT64:
  75. return static_cast<RT>(data_.i64);
  76. case FDDataType::INT16:
  77. return static_cast<RT>(data_.i16);
  78. case FDDataType::INT8:
  79. return static_cast<RT>(data_.i8);
  80. case FDDataType::UINT8:
  81. return static_cast<RT>(data_.ui8);
  82. case FDDataType::BOOL:
  83. return static_cast<RT>(data_.b);
  84. default:
  85. FDASSERT(false, "Invalid enum scalar data type `%s`.",
  86. Str(dtype_).c_str());
  87. }
  88. }
  89. FDDataType dtype() const { return dtype_; }
  90. private:
  91. FDDataType dtype_;
  92. union data {
  93. bool b;
  94. int8_t i8;
  95. int16_t i16;
  96. int32_t i32;
  97. int64_t i64;
  98. uint8_t ui8;
  99. float16 f16;
  100. float f32;
  101. double f64;
  102. } data_;
  103. };
  104. } // namespace ultra_infer