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.

complex.cc 4.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * Copyright 2020-2021 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 "ops/complex.h"
  17. #include <complex>
  18. #include <map>
  19. #include <string>
  20. #include <set>
  21. #include "abstract/primitive_infer_map.h"
  22. #include "ops/op_utils.h"
  23. namespace mindspore {
  24. namespace ops {
  25. namespace {
  26. template <typename T>
  27. void ImpleComplex(void *real, void *imag, void *target, size_t size) {
  28. MS_EXCEPTION_IF_NULL(real);
  29. MS_EXCEPTION_IF_NULL(imag);
  30. MS_EXCEPTION_IF_NULL(target);
  31. auto real_data = reinterpret_cast<T *>(real);
  32. auto imag_data = reinterpret_cast<T *>(imag);
  33. auto target_data = reinterpret_cast<std::complex<T> *>(target);
  34. MS_EXCEPTION_IF_NULL(real_data);
  35. MS_EXCEPTION_IF_NULL(imag_data);
  36. MS_EXCEPTION_IF_NULL(target_data);
  37. for (size_t i = 0; i < size; ++i) {
  38. target_data[i] = std::complex<T>(real_data[i], imag_data[i]);
  39. }
  40. }
  41. abstract::ShapePtr ComplexInferShape(const PrimitivePtr &primitive, const std::vector<AbstractBasePtr> &input_args) {
  42. auto shape_map = CheckAndConvertUtils::ConvertShapePtrToShapeMap(input_args[kInputIndex0]->BuildShape());
  43. auto in_shape = shape_map[kShape];
  44. auto min_shape = shape_map[kMinShape];
  45. auto max_shape = shape_map[kMaxShape];
  46. return std::make_shared<abstract::Shape>(in_shape, min_shape, max_shape);
  47. }
  48. TypePtr ComplexInferType(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
  49. std::map<std::string, TypePtr> types;
  50. auto real_input_type = input_args[kInputIndex0]->BuildType();
  51. auto imag_input_type = input_args[kInputIndex1]->BuildType();
  52. (void)types.emplace("real_input", real_input_type);
  53. (void)types.emplace("imag_input", imag_input_type);
  54. (void)CheckAndConvertUtils::CheckTensorTypeSame(types, std::set<TypePtr>{kFloat32, kFloat64}, prim->name());
  55. auto real_input_tensor = real_input_type->cast<TensorTypePtr>();
  56. TypeId real_input_tensor_id = real_input_tensor->element()->type_id();
  57. return real_input_tensor_id == kNumberTypeFloat32 ? kComplex64 : kComplex128;
  58. }
  59. AbstractBasePtr ComplexInfer(const abstract::AnalysisEnginePtr &, const PrimitivePtr &primitive,
  60. const std::vector<AbstractBasePtr> &input_args) {
  61. MS_EXCEPTION_IF_NULL(primitive);
  62. const int64_t input_num = 2;
  63. CheckAndConvertUtils::CheckInputArgs(input_args, kEqual, input_num, primitive->name());
  64. return abstract::MakeAbstract(ComplexInferShape(primitive, input_args), ComplexInferType(primitive, input_args));
  65. }
  66. ValuePtr ComplexInferValue(const PrimitivePtr &prim, const std::vector<AbstractBasePtr> &input_args) {
  67. if (input_args.empty()) {
  68. return nullptr;
  69. }
  70. auto real = input_args[kInputIndex0]->BuildValue();
  71. if (real == nullptr) {
  72. return nullptr;
  73. }
  74. auto real_tensor = real->cast<tensor::TensorPtr>();
  75. if (real_tensor == nullptr) {
  76. return nullptr;
  77. }
  78. auto imag = input_args[kInputIndex1]->BuildValue();
  79. if (imag == nullptr) {
  80. return nullptr;
  81. }
  82. auto imag_tensor = imag->cast<tensor::TensorPtr>();
  83. if (imag_tensor == nullptr) {
  84. return nullptr;
  85. }
  86. if (real_tensor->data_type() != imag_tensor->data_type()) {
  87. MS_EXCEPTION(TypeError) << "Inputs of Complex should be same, but got " << real_tensor->data_type() << "and "
  88. << imag_tensor->data_type();
  89. }
  90. auto data_size = real_tensor->DataSize();
  91. auto dtype = real_tensor->data_type();
  92. auto shape = ComplexInferShape(prim, input_args)->shape();
  93. auto output_type = (dtype == kNumberTypeFloat32 ? kNumberTypeComplex64 : kNumberTypeComplex128);
  94. auto result_tensor = std::make_shared<tensor::Tensor>(output_type, shape);
  95. auto real_datac = real_tensor->data_c();
  96. auto imag_datac = imag_tensor->data_c();
  97. auto result_datac = result_tensor->data_c();
  98. switch (dtype) {
  99. case kNumberTypeFloat32: {
  100. ImpleComplex<float>(real_datac, imag_datac, result_datac, data_size);
  101. break;
  102. }
  103. case kNumberTypeFloat64: {
  104. ImpleComplex<double>(real_datac, imag_datac, result_datac, data_size);
  105. break;
  106. }
  107. default: {
  108. MS_EXCEPTION(TypeError) << "Complex unsupported data type: " << real_tensor->ToString();
  109. }
  110. }
  111. return result_tensor;
  112. }
  113. } // namespace
  114. REGISTER_PRIMITIVE_EVAL_IMPL(Complex, prim::kPrimComplex, ComplexInfer, ComplexInferValue, true);
  115. } // namespace ops
  116. } // namespace mindspore