1 /** 2 * Copyright 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 #ifndef MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_ACTOR_COMMON_H_ 18 #define MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_ACTOR_COMMON_H_ 19 20 #include <string> 21 #include <vector> 22 #include <unordered_map> 23 #include <utility> 24 #include <thread> 25 #include <algorithm> 26 #include "mindrt/include/actor/op_actor.h" 27 #include "runtime/device/device_address.h" 28 #include "backend/session/anf_runtime_algorithm.h" 29 #include "backend/session/kernel_graph.h" 30 #include "utils/log_adapter.h" 31 #include "ir/tensor.h" 32 33 namespace mindspore { 34 namespace runtime { 35 using mindspore::session::KernelWithIndex; 36 using tensor::TensorPtr; 37 using DeviceTensor = mindspore::device::DeviceAddress; 38 39 // The execution result of actor. 40 constexpr int kSuccess = 0; 41 constexpr int kFailure = 1; 42 43 enum class GraphExecutionStrategy { 44 kPipeline, // The actor running is triggered only by data. 45 kStep // The actor running need be triggered by control in addition. 46 }; 47 48 enum class KernelTransformType { 49 kUnknown, 50 kDataPrepareActor, 51 kDeviceDataSourceActor, 52 kHostDataSourceActor, 53 kKernelActor, 54 kCopyActor, 55 kLoopCountActor, 56 kOutputActor, 57 kDeviceTensorStore, 58 // Internal parameter is the output of previous kernel graph which is related to the input of next kernel graph. 59 kInternalParameter 60 }; 61 62 #define SET_OPCONTEXT_FAIL_RET_WITH_ERROR(op_context, message) \ 63 { \ 64 MS_LOG(ERROR) << message; \ 65 op_context.SetFailed(kFailure); \ 66 return; \ 67 } 68 69 #define SET_OPCONTEXT_SUCCESS_RET(op_context) \ 70 { \ 71 op_context.SetSuccess(kSuccess); \ 72 return; \ 73 } 74 75 #define SET_OPCONTEXT_FAIL_RET_WITH_ERROR_BY_STRATEGY(strategy, op_context, message) \ 76 { \ 77 if (strategy == GraphExecutionStrategy::kStep) { \ 78 MS_LOG(EXCEPTION) << message; \ 79 } \ 80 MS_LOG(ERROR) << message; \ 81 op_context.SetFailed(kFailure); \ 82 return; \ 83 } 84 85 #define SET_OPCONTEXT_MEMORY_ALLOC_FAIL_BY_STRATEGY(strategy, op_context, device_context, kernel_name, alloc_size) \ 86 { \ 87 std::string message = "Device(id:" + std::to_string((device_context).device_context_key().device_id_) + \ 88 ") memory isn't enough and alloc failed, kernel name: " + kernel_name + \ 89 ", alloc size: " + std::to_string(alloc_size) + "B."; \ 90 if (strategy == GraphExecutionStrategy::kStep) { \ 91 MS_LOG(EXCEPTION) << message; \ 92 } \ 93 MS_LOG(ERROR) << message; \ 94 (op_context).SetFailed(kFailure); \ 95 return; \ 96 } 97 98 void ComputeThreadNums(size_t *actor_thread_num, size_t *OMP_thread_num, size_t *max_thread_num); 99 100 bool IsDeviceQueueDSActor(const AnfNodePtr &node, GraphExecutionStrategy strategy = GraphExecutionStrategy::kPipeline); 101 102 // Host parameters are parameters of root funcgraph, in control flow, only the parameters of the root funcgraph are 103 // in the host data source. 104 bool IsHostQueueDSActor(const AnfNodePtr &node, const KernelGraphPtr &graph = nullptr, 105 const std::vector<AnfNodePtr> &host_parameters = {}, 106 GraphExecutionStrategy strategy = GraphExecutionStrategy::kPipeline); 107 108 bool IsKernelActor(const AnfNodePtr &node, GraphExecutionStrategy strategy = GraphExecutionStrategy::kPipeline); 109 110 bool IsSwitchActor(const AnfNodePtr &node); 111 112 // The skip kernel doesn't run, it exists in the inplace optimizer. 113 bool IsSkippedKernelActor(const AnfNodePtr &node); 114 115 // Internal parameter is not the origin parameter of func graph, it is the output of previous kernel graph which is 116 // related to the input of this kernel graph. 117 bool IsInternalParameter(const AnfNodePtr &node, const KernelGraphPtr &graph); 118 119 // Judge whether the device tensor of the node is persistent or not. 120 bool IsPersistentDeviceTensor(const AnfNodePtr &node); 121 122 // Judge whether the front node is in a gather actor. 123 bool IsGatherActor(const AnfNodePtr &front_node, 124 const std::unordered_map<std::string, OpActor<DeviceTensor> *> &actor_name_to_actor); 125 126 // Copy data from src_device_tensor to dst_device_tensor. 127 bool Copy(const DeviceTensor *dst_device_tensor, const DeviceTensor *src_device_tensor); 128 129 void UpdateRefCount(DeviceTensor *const device_tensor, bool is_max_ref_count = false); 130 // Update the reference count of device tensor by the output index of node. 131 void UpdateRefCount(const AnfNodePtr &node, size_t output_idx, bool is_max_ref_count = false); 132 133 // Get front node by backend node. 134 AnfNodePtr FetchFrontNodeByBackendNode(const AnfNodePtr &backend_node, const KernelGraphPtr &graph); 135 KernelWithIndex FetchFrontNodeWithIndexByGraphOutput(const KernelWithIndex &output_with_index, 136 const KernelGraphPtr &graph); 137 } // namespace runtime 138 } // namespace mindspore 139 140 #endif // MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_ACTOR_COMMON_H_ 141