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