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_adapter_util.h 2.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 TRANSFORM_OP_ADAPTER_UTIL_H_
  17. #define TRANSFORM_OP_ADAPTER_UTIL_H_
  18. #include <string>
  19. #include <vector>
  20. #include "transform/op_adapter_base.h"
  21. namespace mindspore {
  22. namespace transform {
  23. template <typename P, typename Q>
  24. static Q ConvertAnyUtil(const ValuePtr &value, const AnyTraits<P> &, const AnyTraits<Q> &) {
  25. return static_cast<Q>(GetValue<P>(value));
  26. }
  27. GeTensor ConvertAnyUtil(const ValuePtr &value, const AnyTraits<mindspore::tensor::Tensor> &traits);
  28. std::vector<int64_t> ConvertAnyUtil(const ValuePtr &value, const std::string &name,
  29. const AnyTraits<std::vector<int64_t>>);
  30. std::string ConvertAnyUtil(const ValuePtr &value, const AnyTraits<std::vector<int64_t>>, const AnyTraits<std::string>);
  31. std::vector<float> ConvertAnyUtil(const ValuePtr &value, const AnyTraits<std::vector<float>>, const AnyTraits<float>);
  32. std::vector<int64_t> ConvertAnyUtil(const ValuePtr &value, const std::string &format,
  33. const AnyTraits<std::vector<int64_t>>, const AnyTraits<int64_t>);
  34. GeDataType ConvertAnyUtil(const ValuePtr &value, const AnyTraits<GEType>);
  35. template <typename P, typename Q>
  36. std::vector<Q> ConvertAnyUtil(const ValuePtr &value, AnyTraits<P>, const AnyTraits<std::vector<Q>>) {
  37. if (!value->isa<ValueTuple>() && !value->isa<ValueList>()) {
  38. MS_LOG(EXCEPTION) << "error convert Value to vector for value: " << value->ToString()
  39. << ", type: " << value->type_name() << ", value should be a tuple or list";
  40. }
  41. auto vec = value->isa<ValueTuple>() ? value->cast<ValueTuplePtr>()->value() : value->cast<ValueListPtr>()->value();
  42. std::vector<Q> data;
  43. for (auto &it : vec) {
  44. data.push_back(ConvertAnyUtil(it, AnyTraits<P>(), AnyTraits<Q>()));
  45. }
  46. return data;
  47. }
  48. GeTensor ConvertAnyUtil(const ValuePtr &value, const AnyTraits<AnyValue>);
  49. bool IsCustomPrim(const PrimitivePtr &prim);
  50. bool IsCustomCNode(const AnfNodePtr &node);
  51. } // namespace transform
  52. } // namespace mindspore
  53. #endif // TRANSFORM_OP_ADAPTER_UTIL_H_