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.

base_ref_utils.cc 2.2 kB

5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. #include <vector>
  17. #include <memory>
  18. #include "utils/base_ref_utils.h"
  19. #include "include/ms_tensor.h"
  20. #include "ir/tensor.h"
  21. namespace mindspore {
  22. void IterateFindTensor(std::vector<std::shared_ptr<inference::MSTensor>> *msTensors, const VectorRef &ref_list) {
  23. for (size_t i = 0; i < ref_list.size(); ++i) {
  24. if (utils::isa<tensor::TensorPtr>(ref_list[i])) {
  25. auto tensor_ptr = utils::cast<std::shared_ptr<tensor::Tensor>>(ref_list[i]);
  26. MS_EXCEPTION_IF_NULL(tensor_ptr);
  27. auto tensor = new inference::Tensor(tensor_ptr);
  28. msTensors->emplace_back(std::shared_ptr<inference::MSTensor>(tensor));
  29. } else if (utils::isa<VectorRef>(ref_list[i])) {
  30. auto ref_iter = utils::cast<VectorRef>(ref_list[i]);
  31. IterateFindTensor(msTensors, ref_iter);
  32. } else {
  33. MS_LOG(EXCEPTION) << "The output is not a tensor";
  34. }
  35. }
  36. }
  37. std::vector<std::shared_ptr<inference::MSTensor>> TransformVectorRefToMultiTensor(const VectorRef &base_ref) {
  38. std::vector<std::shared_ptr<inference::MSTensor>> msTensors;
  39. if (utils::isa<VectorRef>(base_ref)) {
  40. auto ref_list = utils::cast<VectorRef>(base_ref);
  41. IterateFindTensor(&msTensors, ref_list);
  42. } else if (utils::isa<tensor::Tensor>(base_ref)) {
  43. auto tensor_ptr = utils::cast<std::shared_ptr<tensor::Tensor>>(base_ref);
  44. MS_EXCEPTION_IF_NULL(tensor_ptr);
  45. auto tensor = new inference::Tensor(tensor_ptr);
  46. msTensors.emplace_back(std::shared_ptr<inference::MSTensor>(tensor));
  47. } else {
  48. MS_LOG(EXCEPTION) << "The output is not a base ref list or a tensor!";
  49. }
  50. return msTensors;
  51. }
  52. } // namespace mindspore