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.

lite_kernel.cc 3.1 kB

5 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 "src/lite_kernel.h"
  17. #include <algorithm>
  18. #include "src/tensor.h"
  19. #include "src/common/utils.h"
  20. #include "src/common/version_manager.h"
  21. namespace mindspore::kernel {
  22. using mindspore::lite::RET_ERROR;
  23. using mindspore::lite::RET_OK;
  24. bool LiteKernel::IsReady(const std::vector<lite::Tensor *> &scope_tensors) {
  25. MS_ASSERT(kernel_ != nullptr);
  26. auto &in_tensors = this->in_tensors();
  27. return std::all_of(in_tensors.begin(), in_tensors.end(), [&](lite::Tensor *in_tensor) {
  28. if (IsContain(scope_tensors, in_tensor)) {
  29. return in_tensor->IsReady();
  30. } else {
  31. return true;
  32. }
  33. });
  34. }
  35. void LiteKernel::InitOutTensorInitRefCount(const std::vector<LiteKernel *> *mask_kernels) {
  36. for (auto *tensor : this->out_tensors()) {
  37. MS_ASSERT(tensor != nullptr);
  38. int init_ref_count = 0;
  39. for (auto *post_kernel : this->out_kernels_) {
  40. if ((mask_kernels == nullptr) ||
  41. std::find(mask_kernels->begin(), mask_kernels->end(), post_kernel) != mask_kernels->end()) {
  42. auto &post_in_tensors = post_kernel->in_tensors();
  43. init_ref_count += std::count_if(
  44. post_in_tensors.begin(), post_in_tensors.end(),
  45. [&tensor](const lite::Tensor *post_kernel_in_tensor) { return post_kernel_in_tensor == tensor; });
  46. }
  47. }
  48. tensor->set_init_ref_count(init_ref_count);
  49. }
  50. }
  51. std::string LiteKernel::ToString() const {
  52. std::ostringstream oss;
  53. oss << "LiteKernel: " << this->name();
  54. oss << ", Type: " << this->type_str();
  55. oss << ", " << this->in_tensors().size() << " InputTensors:";
  56. for (auto tensor : in_tensors()) {
  57. oss << " " << tensor;
  58. }
  59. oss << ", " << this->out_tensors().size() << " OutputTensors:";
  60. for (auto tensor : out_tensors()) {
  61. oss << " " << tensor;
  62. }
  63. oss << ", " << this->in_kernels_.size() << " InputKernels:";
  64. for (auto in_kernel : in_kernels_) {
  65. oss << " " << in_kernel->name();
  66. }
  67. oss << ", " << this->out_kernels_.size() << " OutputKernels:";
  68. for (auto out_kernel : out_kernels_) {
  69. oss << " " << out_kernel->name();
  70. }
  71. return oss.str();
  72. }
  73. int LiteKernel::DoExecute() {
  74. auto ret = kernel_->Execute();
  75. if ((ret == lite::RET_OK) && (desc_.provider != kBuiltin)) {
  76. for (auto *output : out_tensors()) {
  77. MS_ASSERT(output != nullptr);
  78. output->ResetRefCount();
  79. }
  80. for (auto &in_tensor : in_tensors()) {
  81. MS_ASSERT(in_tensor != nullptr);
  82. in_tensor->DecRefCount();
  83. }
  84. }
  85. return ret;
  86. }
  87. } // namespace mindspore::kernel