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_DATA_PREPARE_ACTOR_H_ 18 #define MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_DATA_PREPARE_ACTOR_H_ 19 20 #include <vector> 21 #include <string> 22 #include <memory> 23 #include <utility> 24 #include <unordered_map> 25 #include <map> 26 #include "runtime/framework/graph_compiler.h" 27 #include "runtime/framework/actor/actor_common.h" 28 #include "runtime/framework/actor/data_source_actor.h" 29 #include "runtime/framework/actor/debug_aware_actor.h" 30 #include "runtime/framework/device_tensor_store.h" 31 #include "runtime/hardware/device_context.h" 32 33 namespace mindspore { 34 namespace runtime { 35 using mindspore::device::DeviceContext; 36 37 // The data prepare actor is used to prepare data for device tensor store and host tensor queue to represent the begin 38 // of one step. 39 class DataPrepareActor : public DebugAwareActor { 40 public: DataPrepareActor(const std::string & name,const AID & memory_manager_aid,const AID * debug_aid,const GraphCompilerInfo * graph_compiler_info,const HostQueueDSActorPtr & host_data_source_actor,const HostTensorQueuePtr & host_tensor_queue)41 DataPrepareActor(const std::string &name, const AID &memory_manager_aid, const AID *debug_aid, 42 const GraphCompilerInfo *graph_compiler_info, const HostQueueDSActorPtr &host_data_source_actor, 43 const HostTensorQueuePtr &host_tensor_queue) 44 : DebugAwareActor(name, KernelTransformType::kDataPrepareActor, nullptr, memory_manager_aid, debug_aid), 45 graph_compiler_info_(graph_compiler_info), 46 strategy_(GraphExecutionStrategy::kPipeline), 47 host_data_source_actor_(host_data_source_actor), 48 host_tensor_queue_(host_tensor_queue), 49 loop_count_aid_(nullptr) {} 50 ~DataPrepareActor() override = default; 51 52 void Init() override; 53 54 // The process entry of data prepare. 55 void PrepareData(const std::vector<std::vector<TensorPtr>> &input_tensors, OpContext<DeviceTensor> *const context); 56 57 // The debug related operation interface. 58 void SendDebugReq(OpContext<DeviceTensor> *const context) override; 59 void OnDebugFinish(OpContext<DeviceTensor> *const context) override; 60 61 // The continuous memory related operation interface. 62 void SendMemoryAllocReq(OpContext<DeviceTensor> *const context) override; 63 void OnMemoryAllocFinish(OpContext<DeviceTensor> *const context) override; 64 65 private: 66 friend class GraphScheduler; 67 68 // Send output controls when finish data prepare. 69 void SendOutput(OpContext<DeviceTensor> *const context); 70 71 void PrepareDataForDeviceTensorStore(const std::vector<std::vector<TensorPtr>> &input_tensors, 72 OpContext<DeviceTensor> *const context); 73 void PrepareDataForHostTensorQueue(const std::vector<std::vector<TensorPtr>> &input_tensors, 74 OpContext<DeviceTensor> *const context); 75 void PrepareDataForStepMode(const std::vector<std::vector<TensorPtr>> &input_tensors, 76 OpContext<DeviceTensor> *const context); 77 78 // Prepare the device data for persistent device tensor of weight node from host tensor. 79 void PrepareDataForWeightNode(const AnfNodePtr &backend_node, const AnfNodePtr &front_node, const TensorPtr &tensor, 80 const DeviceContext *device_context, OpContext<DeviceTensor> *const context); 81 // Prepare the device data for persistent device tensor of value node. 82 void PrepareDataForValueNode(const ValueNodePtr &node, const DeviceContext *device_context, 83 OpContext<DeviceTensor> *const context); 84 // The branch processing of PrepareDataForValueNode that value type is tensor. 85 void PrepareDataForValueNodeTensor(const ValueNodePtr &node, const ValuePtr &node_value, 86 const DeviceContext *device_context, OpContext<DeviceTensor> *const context); 87 88 // The data prepare in the control flow scene. 89 void PrepareDeviceTensorStoreForControlNode(const ControlNodeParserPtr &control_node_parser, 90 const std::vector<TensorPtr> &tensors, 91 OpContext<DeviceTensor> *const context); 92 void PrepareHostTensorQueueForControlNode(const std::vector<TensorPtr> &tensors, 93 std::vector<TensorPtr> *const host_tensors, 94 OpContext<DeviceTensor> *const context); 95 // In control flow, all weight nodes associated with the host weight parameter need to use the same device tensor. 96 void PrepareDataForControlWeightNode( 97 const AnfNodePtr &node, const AnfNodePtr &front_node, const TensorPtr &tensor, const DeviceContext *device_context, 98 const std::unordered_map<AnfNodePtr, std::vector<AnfNodePtr>> &host_parameter_to_weights, 99 OpContext<DeviceTensor> *const context); 100 101 const GraphCompilerInfo *graph_compiler_info_; 102 GraphExecutionStrategy strategy_; 103 HostQueueDSActorPtr host_data_source_actor_; 104 HostTensorQueuePtr host_tensor_queue_; 105 106 // The output controls contain the data source actors and the no input kernel actors. 107 std::vector<AID> data_source_aids_; 108 std::vector<AID> no_input_kernel_aids_; 109 // If has no data source actor and kernel actor, then need send to loop count actor. 110 const AID *loop_count_aid_; 111 112 // The nodes need continuous memory, which must allocate in the begin of step running. The first bool of pair 113 // expresses the inputs of node need continuous memory, the second bool of pair expresses the outputs of node need 114 // continuous memory. 115 std::map<std::pair<CNodePtr, DeviceContext *>, std::pair<bool, bool>> continuous_memory_nodes_; 116 // The members for continuous memory alloc fetched by continuous_memory_nodes_. 117 std::vector<std::vector<DeviceTensorPtr>> continuous_memory_alloc_list_list_; 118 std::vector<std::vector<size_t>> size_list_list_; 119 std::vector<size_t> total_size_list_; 120 std::vector<const DeviceContext *> continuous_memory_device_contexts_; 121 }; // namespace runtime 122 123 using DataPrepareActorPtr = std::shared_ptr<DataPrepareActor>; 124 } // namespace runtime 125 } // namespace mindspore 126 127 #endif // MINDSPORE_CCSRC_RUNTIME_FRAMEWORK_ACTOR_DATA_PREPARE_ACTOR_H_ 128