1 /**
2 * Copyright 2024 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/litert/executor.h"
18 #include <queue>
19 #include "include/errorcode.h"
20 #include "src/common/tensor_util.h"
21
22 namespace mindspore::lite {
Run(const std::vector<Tensor * > & in_tensors,const std::vector<Tensor * > & out_tensors,const std::vector<kernel::KernelExec * > & kernels,const KernelCallBack & before,const KernelCallBack & after)23 int Executor::Run(const std::vector<Tensor *> &in_tensors, const std::vector<Tensor *> &out_tensors,
24 const std::vector<kernel::KernelExec *> &kernels, const KernelCallBack &before,
25 const KernelCallBack &after) {
26 // init the max spin count.
27 CHECK_NULL_RETURN(ctx_);
28 auto thread_pool = ctx_->thread_pool_;
29 CHECK_NULL_RETURN(thread_pool);
30 thread_pool->SetSpinCountMaxValue();
31
32 // clear ref_count
33 for (auto *kernel : kernels) {
34 CHECK_NULL_RETURN(kernel);
35 for (auto *tensor : kernel->in_tensors()) {
36 CHECK_NULL_RETURN(tensor);
37 tensor->set_ref_count(0);
38 }
39 }
40
41 // clear output ref_couont
42 for (auto output_tensor : out_tensors) {
43 CHECK_NULL_RETURN(output_tensor);
44 if (output_tensor->allocator() != nullptr) {
45 output_tensor->DecRefCount();
46 } else {
47 /* user set graph->-output-tensor from outside */
48 output_tensor->set_own_data(false);
49 output_tensor->set_allocator(nullptr);
50 }
51 }
52
53 for (auto kernel : kernels) {
54 CHECK_NULL_RETURN(kernel);
55 int ret = kernel->Execute(before, after);
56 if (ret != RET_OK) {
57 MS_LOG(ERROR) << "run kernel failed, name: " << kernel->name();
58 return ret;
59 }
60 }
61
62 thread_pool->SetSpinCountMinValue();
63 return RET_OK;
64 }
65 } // namespace mindspore::lite
66