clean code

This commit is contained in:
kswang 2021-07-24 11:48:35 +08:00
parent 438cbefaf4
commit 9e8156b020
10 changed files with 83 additions and 19 deletions

View File

@ -205,7 +205,11 @@ void AscendKernelRuntime::ClearGraphRuntimeResource(uint32_t graph_id, const std
}
}
void AscendKernelRuntime::ClearGlobalIdleMem() { mem_manager_->ClearGlobalIdleMem(); }
void AscendKernelRuntime::ClearGlobalIdleMem() {
if (mem_manager_ != nullptr) {
mem_manager_->ClearGlobalIdleMem();
}
}
bool AscendKernelRuntime::NeedDestroyHccl() {
auto context_ptr = MsContext::GetInstance();
@ -346,6 +350,7 @@ bool AscendKernelRuntime::LoadData(mindspore::session::KernelGraph *graph) {
}
bool AscendKernelRuntime::KernelMemNotReuse(const AnfNodePtr &node) {
MS_EXCEPTION_IF_NULL(node);
bool need_dump = false;
auto &dump_json_parser = DumpJsonParser::GetInstance();
if (dump_json_parser.e2e_dump_enabled() && dump_json_parser.dump_mode() == 1) {
@ -571,6 +576,9 @@ void AscendKernelRuntime::TaskFailCallback(rtExceptionInfo *task_fail_info) {
}
CNodePtr AscendKernelRuntime::GetErrorNodeName(uint32_t streamid, uint32_t taskid) {
if (current_graph_ == nullptr) {
return nullptr;
}
auto runtime_info_map = ModelRunner::Instance().GetRuntimeInfoMap(current_graph_->graph_id());
for (const auto &iter : runtime_info_map) {
auto task_id = std::get<kTupleTaskId>(*iter.second);
@ -655,6 +663,7 @@ bool AscendKernelRuntime::RunDynamicKernelAsync(const session::KernelGraph *grap
auto dynamic_kernels = iter->second;
for (const auto &dynamic_kernel : dynamic_kernels) {
MS_EXCEPTION_IF_NULL(dynamic_kernel);
if (dynamic_kernel->have_depends() || dynamic_kernel->GetKernelType() == KernelType::HCCL_KERNEL) {
MS_LOG(INFO) << "Match Dynamic Kernel, Start SyncStream";
if (!SyncStream()) {
@ -898,6 +907,7 @@ bool AscendKernelRuntime::DestroyHccl() {
}
bool AscendKernelRuntime::GraphWithEmptyTaskList(const session::KernelGraph *graph) const {
MS_EXCEPTION_IF_NULL(graph);
auto iter = task_map_.find(graph->graph_id());
if (iter == task_map_.end()) {
MS_LOG(EXCEPTION) << "Unknown graph ptr";
@ -941,6 +951,7 @@ std::shared_ptr<DeviceEvent> AscendKernelRuntime::CreateDeviceEvent() {
uint64_t AscendKernelRuntime::GetAvailableMemMaxSize() const {
auto ascend_mem_manager = std::dynamic_pointer_cast<AscendMemoryManager>(mem_manager_);
MS_EXCEPTION_IF_NULL(ascend_mem_manager);
return ascend_mem_manager->GetDeviceMemSize();
}

View File

@ -18,6 +18,9 @@
namespace mindspore {
namespace device {
void HalfToFloat(void *dst, const void *src, size_t elem_num) {
if (dst == nullptr || src == nullptr) {
return;
}
auto half_data = static_cast<const float16 *>(src);
auto float_data = static_cast<float *>(dst);
for (size_t i = 0; i < elem_num; ++i) {
@ -27,6 +30,9 @@ void HalfToFloat(void *dst, const void *src, size_t elem_num) {
}
void FloatToHalf(void *dst, const void *src, size_t elem_num) {
if (dst == nullptr || src == nullptr) {
return;
}
auto float_data = static_cast<const float *>(src);
auto half_data = static_cast<float16 *>(dst);
for (size_t i = 0; i < elem_num; ++i) {
@ -35,6 +41,9 @@ void FloatToHalf(void *dst, const void *src, size_t elem_num) {
}
void DoubleToFloat(void *dst, const void *src, size_t elem_num) {
if (dst == nullptr || src == nullptr) {
return;
}
auto double_data = static_cast<const double *>(src);
auto float_data = static_cast<float *>(dst);
for (size_t i = 0; i < elem_num; ++i) {
@ -43,6 +52,9 @@ void DoubleToFloat(void *dst, const void *src, size_t elem_num) {
}
void FloatToDouble(void *dst, const void *src, size_t elem_num) {
if (dst == nullptr || src == nullptr) {
return;
}
auto float_data = static_cast<const float *>(src);
auto double_data = static_cast<double *>(dst);
for (size_t i = 0; i < elem_num; ++i) {
@ -51,6 +63,9 @@ void FloatToDouble(void *dst, const void *src, size_t elem_num) {
}
void ShortToInt(void *dst, const void *src, size_t elem_num) {
if (dst == nullptr || src == nullptr) {
return;
}
auto half_data = static_cast<const int16_t *>(src);
auto int_data = static_cast<int *>(dst);
for (size_t i = 0; i < elem_num; ++i) {
@ -59,6 +74,9 @@ void ShortToInt(void *dst, const void *src, size_t elem_num) {
}
void IntToShort(void *dst, const void *src, size_t elem_num) {
if (dst == nullptr || src == nullptr) {
return;
}
auto int_data = static_cast<const int *>(src);
auto half_data = static_cast<int16_t *>(dst);
for (size_t i = 0; i < elem_num; ++i) {
@ -67,6 +85,9 @@ void IntToShort(void *dst, const void *src, size_t elem_num) {
}
void LongToInt(void *dst, const void *src, size_t elem_num) {
if (dst == nullptr || src == nullptr) {
return;
}
auto long_data = static_cast<const int64_t *>(src);
auto int_data = static_cast<int *>(dst);
for (size_t i = 0; i < elem_num; ++i) {
@ -75,6 +96,9 @@ void LongToInt(void *dst, const void *src, size_t elem_num) {
}
void IntToLong(void *dst, const void *src, size_t elem_num) {
if (dst == nullptr || src == nullptr) {
return;
}
auto int_data = static_cast<const int *>(src);
auto long_data = static_cast<int64_t *>(dst);
for (size_t i = 0; i < elem_num; ++i) {
@ -83,6 +107,9 @@ void IntToLong(void *dst, const void *src, size_t elem_num) {
}
void ConvertSameType(void *dst, const void *src, size_t size, TypeId type) {
if (dst == nullptr || src == nullptr) {
return;
}
if (type == kNumberTypeFloat16) {
auto dst_data = static_cast<float16 *>(dst);
auto src_data = static_cast<const float16 *>(src);

View File

@ -35,6 +35,9 @@ void ConvertSameType(void *dst, const void *src, size_t size, TypeId type);
template <typename T>
void ConvertSameType(T *dst, const T *src, size_t elem_num) {
if (dst == nullptr || src == nullptr) {
return;
}
for (size_t i = 0; i < elem_num; ++i) {
dst[i] = src[i];
}

View File

@ -713,6 +713,7 @@ bool KernelAdjust::StepLoadCtrlInputs(const std::shared_ptr<session::KernelGraph
// deal four ctrl nodes.
for (size_t i = 0; i < inputs.size(); ++i) {
auto tensor = inputs[i];
MS_EXCEPTION_IF_NULL(tensor);
size_t deal_index = input_nodes.size() - input_ctrl_size + i;
if (deal_index >= input_nodes.size()) {
MS_LOG(EXCEPTION) << "deal_index[" << deal_index << "] out of range";
@ -722,7 +723,6 @@ bool KernelAdjust::StepLoadCtrlInputs(const std::shared_ptr<session::KernelGraph
MS_EXCEPTION_IF_NULL(input_node);
if (input_node->isa<Parameter>()) {
auto pk_node = input_node->cast<ParameterPtr>();
MS_EXCEPTION_IF_NULL(tensor);
MS_EXCEPTION_IF_NULL(pk_node);
if (tensor->NeedSyncHostToDevice() || !pk_node->has_default()) {
need_sync = true;

View File

@ -51,7 +51,7 @@ bool KernelInfo::SetOutputAddr(const DeviceAddressPtr &output_address, size_t in
for (size_t i = output_address_list_.size(); i <= index; i++) {
output_address_list_.emplace_back(nullptr);
}
} else if (output_address_list_.empty()) {
} else if (kernel_mod_ != nullptr && output_address_list_.empty()) {
// set cnode
for (size_t i = 0; i < kernel_mod_->GetOutputSizeList().size(); i++) {
output_address_list_.emplace_back(nullptr);

View File

@ -54,6 +54,7 @@ std::vector<AnfNodePtr> GetGraphInputs(const session::KernelGraph *graph) {
for (size_t i = 0; i < input_num; ++i) {
auto input_node = kernel->input(i + 1);
auto input_real_node = AnfAlgo::VisitKernelWithReturnType(input_node, 0).first;
MS_EXCEPTION_IF_NULL(input_real_node);
if (input_real_node->isa<Parameter>() && inputs_set.find(input_real_node) == inputs_set.end()) {
(void)inputs_set.insert(input_real_node);
(void)result.emplace_back(input_real_node);
@ -175,9 +176,9 @@ void KernelRuntime::RunOpAssignInputMemory(const std::vector<tensor::TensorPtr>
}
auto output_size = AnfAlgo::GetOutputTensorNum(item);
for (size_t index = 0; index < output_size; index++) {
MS_EXCEPTION_IF_NULL(input_tensors[input_index]);
auto output_address =
std::dynamic_pointer_cast<device::DeviceAddress>(input_tensors[input_index]->device_address());
auto current_tensor = input_tensors[input_index];
MS_EXCEPTION_IF_NULL(current_tensor);
auto output_address = std::dynamic_pointer_cast<device::DeviceAddress>(current_tensor->device_address());
if (output_address != nullptr && output_address->DeviceType() == GetTargetDeviceAddressType()) {
AnfAlgo::SetOutputAddr(output_address, index, item.get());
continue;
@ -266,16 +267,18 @@ void KernelRuntime::RunOpAssignOutputNodeMemory(const ValuePtr &pre_output_value
// share output address with pre output tensors
for (size_t i = 0; i < output_nodes.size(); ++i) {
auto output_node_with_index = AnfAlgo::VisitKernel(output_nodes[i], 0);
if (!output_node_with_index.first->isa<CNode>()) {
if (output_node_with_index.first->isa<Parameter>()) {
auto param = output_node_with_index.first->cast<ParameterPtr>();
if (!param->has_default()) {
auto output_node = output_node_with_index.first;
MS_EXCEPTION_IF_NULL(output_node);
if (!output_node->isa<CNode>()) {
if (output_node->isa<Parameter>()) {
auto param = output_node->cast<ParameterPtr>();
if (param != nullptr && !param->has_default()) {
MS_LOG(EXCEPTION) << "The output parameter should be real parameter!";
}
}
continue;
}
auto real_output_cnode = output_node_with_index.first->cast<CNodePtr>();
auto real_output_cnode = output_node->cast<CNodePtr>();
MS_EXCEPTION_IF_NULL(real_output_cnode);
MS_EXCEPTION_IF_NULL(pre_output_tensors[i]);
if (pre_output_tensors[i]->device_address() == nullptr) {
@ -305,6 +308,7 @@ void KernelRuntime::AssignStaticMemoryInput(const session::KernelGraph *graph) {
graph_inputs.insert(graph_inputs.end(), graph->child_graph_result().begin(), graph->child_graph_result().end());
std::vector<AnfNodePtr> need_alloc_nodes;
auto add_need_alloc_nodes = [&need_alloc_nodes, graph, this](const AnfNodePtr &node) {
MS_EXCEPTION_IF_NULL(node);
if (!node->isa<Parameter>()) {
return;
}
@ -312,7 +316,7 @@ void KernelRuntime::AssignStaticMemoryInput(const session::KernelGraph *graph) {
return;
}
auto input_param = node->cast<ParameterPtr>();
if (!input_param->IsUsedByRealKernelInGraph(graph->graph_id())) {
if (input_param != nullptr && !input_param->IsUsedByRealKernelInGraph(graph->graph_id())) {
return;
}
need_alloc_nodes.push_back(node);
@ -337,6 +341,7 @@ void KernelRuntime::AssignStaticMemoryInput(const session::KernelGraph *graph) {
bool ps_cache_check = false;
#endif
for (auto &item : need_alloc_nodes) {
MS_EXCEPTION_IF_NULL(item);
auto output_size = AnfAlgo::GetOutputTensorNum(item);
for (size_t index = 0; index < output_size; index++) {
TypeId output_type_id = AnfAlgo::GetOutputDeviceDataType(item, index);
@ -398,6 +403,7 @@ void KernelRuntime::AssignStaticMemoryOutput(const session::KernelGraph *graph)
}
for (const auto &item_with_index : non_communication_op) {
MS_EXCEPTION_IF_NULL(item_with_index.first);
MS_LOG(DEBUG) << "AssignNodeOutputMem for " << item_with_index.first->fullname_with_scope();
AssignNodeOutputMem(kStaticMem, item_with_index.first, SizeToInt(item_with_index.second));
}
@ -461,6 +467,8 @@ void KernelRuntime::GenKernelEvents(const session::KernelGraph *graph) {
}
auto pre_event = CreateDeviceEvent();
auto post_event = CreateDeviceEvent();
MS_EXCEPTION_IF_NULL(pre_event);
MS_EXCEPTION_IF_NULL(post_event);
pre_event->set_wait_stream(communication_stream_);
pre_event->set_record_stream(stream_);
post_event->set_wait_stream(stream_);
@ -557,6 +565,7 @@ DeviceAddressPtr KernelRuntime::PreAssignCNodeMemory(const AnfNodePtr &anf_node,
MS_LOG(EXCEPTION) << "anf_node should be a cnode";
}
auto cnode = anf_node->cast<CNodePtr>();
MS_EXCEPTION_IF_NULL(cnode);
if (opt::IsNopNode(cnode)) {
const size_t kNopNodeInputSize = 2;
if (cnode->size() != kNopNodeInputSize) {
@ -589,6 +598,7 @@ void KernelRuntime::AssignCommunicationNodeInputMem(MemType type, const AnfNodeP
for (size_t i = 0; i < input_num; ++i) {
auto input_node_with_index = AnfAlgo::GetPrevNodeOutput(node, i, true);
auto input_node = input_node_with_index.first;
MS_EXCEPTION_IF_NULL(input_node);
if (AnfAlgo::OutputAddrExist(input_node, input_node_with_index.second)) {
MS_LOG(INFO) << "Communication op " << input_node->fullname_with_scope() << " has input device address";
return;
@ -727,8 +737,6 @@ void KernelRuntime::AssignValueNodeTensor(const ValueNodePtr &value_node, const
<< "node dtype is " << AnfAlgo::GetOutputInferDataType(value_node, output_idx);
}
}
return;
}
void KernelRuntime::AssignStaticMemoryValueNode(session::KernelGraph *graph) {
@ -740,6 +748,7 @@ void KernelRuntime::AssignStaticMemoryValueNode(session::KernelGraph *graph) {
// order the value nodes
std::map<std::string, ValueNodePtr> value_nodes_map;
for (auto &node : graph->graph_value_nodes()) {
MS_EXCEPTION_IF_NULL(node);
value_nodes_map[node->fullname_with_scope()] = node;
}
@ -901,6 +910,8 @@ void KernelRuntime::GenLaunchArgs(const mindspore::kernel::KernelMod &kernel_mod
}
void KernelRuntime::GenAddrCleanLaunchArgs(const CNodePtr &cnode, AddressPtrList *kernel_inputs) {
MS_EXCEPTION_IF_NULL(cnode);
MS_EXCEPTION_IF_NULL(kernel_inputs);
if (cnode->inputs().size() != 2) {
MS_LOG(EXCEPTION) << "Atomic Addr clean Node Input nodes not equal 2.";
}
@ -986,6 +997,7 @@ bool KernelRuntime::LaunchKernelMod(const session::KernelGraph &graph) {
dynamic_kernel_list[i]->PostExecute();
} else {
auto &kernel = kernels[i];
MS_EXCEPTION_IF_NULL(kernel);
auto kernel_mod = AnfAlgo::GetKernelMod(kernel);
MS_EXCEPTION_IF_NULL(kernel_mod);
@ -1014,7 +1026,7 @@ bool KernelRuntime::LaunchKernelMod(const session::KernelGraph &graph) {
MS_LOG(ERROR) << "Launch kernel failed.";
return false;
}
KernelLaunchProfiling(kernels[i]->fullname_with_scope());
KernelLaunchProfiling(kernel->fullname_with_scope());
}
LaunchKernelEvent(kernel_post_run_events, i);
}
@ -1113,6 +1125,7 @@ void KernelRuntime::GetFirstPSEmbeddingCache(const session::KernelGraph *graph,
}
auto cnode =
AnfAlgo::IsGraphKernel(input_index.first) ? AnfAlgo::GetOutputOfGraphkernel(input_index) : input_index.first;
MS_EXCEPTION_IF_NULL(cnode);
if (!cnode->isa<CNode>()) {
MS_LOG(EXCEPTION) << "The embeddingLookup whose input index should be a CNode but got "
<< cnode->fullname_with_scope();
@ -1139,6 +1152,7 @@ void KernelRuntime::GetFirstPSEmbeddingCache(const session::KernelGraph *graph,
void KernelRuntime::CheckSparsePSEmbeddingCache(const CNodePtr &node) {
MS_EXCEPTION_IF_NULL(node);
auto pre_node = AnfAlgo::GetPrevNodeOutput(node, 1, true);
MS_EXCEPTION_IF_NULL(pre_node.first);
while (pre_node.first->isa<CNode>() && (AnfAlgo::GetCNodeName(pre_node.first) != kUniqueOpName)) {
pre_node = AnfAlgo::GetPrevNodeOutput(pre_node.first, 0, true);
MS_EXCEPTION_IF_NULL(pre_node.first);
@ -1148,6 +1162,7 @@ void KernelRuntime::CheckSparsePSEmbeddingCache(const CNodePtr &node) {
}
pre_node = AnfAlgo::GetPrevNodeOutput(pre_node.first, 0, true);
MS_EXCEPTION_IF_NULL(pre_node.first);
while (pre_node.first->isa<CNode>() && (AnfAlgo::GetCNodeName(pre_node.first) == kCastOpName)) {
pre_node = AnfAlgo::GetPrevNodeOutput(pre_node.first, 0, true);
MS_EXCEPTION_IF_NULL(pre_node.first);
@ -1187,6 +1202,7 @@ void KernelRuntime::CheckIfSupportPSEmbeddingCache(const session::KernelGraph *g
}
auto cnode =
AnfAlgo::IsGraphKernel(input_index.first) ? AnfAlgo::GetOutputOfGraphkernel(input_index) : input_index.first;
MS_EXCEPTION_IF_NULL(cnode);
if (cnode == first_cache_input_index) {
if (!ps::ps_cache_instance.IsHashTable(param_name)) {
MS_LOG(ERROR) << "The embeddingLookup(" << kernel->fullname_with_scope() << ") doesn't enable cache.";

View File

@ -72,7 +72,7 @@ KernelRuntime *KernelRuntimeManager::GetSingleKernelRuntime(const std::string &d
auto runtime_iter = runtime_map_.find(runtime_key);
if (runtime_iter != runtime_map_.end()) {
return runtime_iter->second.get();
} else if (runtime_map_.size() > 0) {
} else if (!runtime_map_.empty()) {
auto cur_runtime_key = runtime_map_.begin()->first;
auto find_pos = cur_runtime_key.rfind('_');
if (find_pos != std::string::npos) {

View File

@ -22,7 +22,11 @@
namespace mindspore::device {
std::vector<kernel::AddressPtr> LaunchKernel::ObtainKernelAddress(const std::vector<size_t> &list,
std::vector<uint8_t *> *addr) {
MS_EXCEPTION_IF_NULL(addr);
std::vector<kernel::AddressPtr> kernel_address;
if (addr->size() < list.size()) {
MS_LOG_EXCEPTION << "Error addr size!";
}
for (size_t i = 0; i < list.size(); ++i) {
auto size = AlignSizeForLaunchKernel(list[i]);
(*addr)[i] = AllocDeviceMem(size);

View File

@ -66,7 +66,7 @@ void LaunchMul::ObtainMulInputsAddr() {
if (device_num == 0) {
MS_LOG(ERROR) << "device num can't be zero";
}
input2_value_ = 1.0 / device_num;
input2_value_ = 1.0f / device_num;
auto size = abstract::TypeIdSize(dtype_);
auto input_size = AlignSizeForLaunchKernel(size * 1);
// alloc memory

View File

@ -143,6 +143,7 @@ uint8_t *MemoryManager::MallocMem(MemType type, size_t size, const DeviceAddress
uint8_t *MemoryManager::MallocDynamicMem(size_t size, bool communication_mem) { return nullptr; }
bool MemoryManager::MallocMemFromMemPool(const DeviceAddressPtr address, size_t size) {
MS_EXCEPTION_IF_NULL(address);
auto device_ptr = MallocMemFromMemPool(size);
if (!device_ptr) {
return false;
@ -176,7 +177,7 @@ void MemoryManager::FreeMemFromMemPool(void *device_ptr) {
bool MemoryManager::MallocContinuousMemFromMemPool(const DeviceAddressPtrList addr_list, size_t total_size,
std::vector<size_t> size_list) {
auto device_ptr_list = MallocContinuousMemFromMemPool(total_size, size_list);
if (device_ptr_list.size() == 0) {
if (device_ptr_list.empty()) {
return false;
}
if (addr_list.size() != device_ptr_list.size()) {
@ -197,7 +198,9 @@ std::vector<void *> MemoryManager::MallocContinuousMemFromMemPool(size_t total_s
MS_LOG(ERROR) << "MallocContinuousMemFromMemPool total_size is 0.";
}
std::vector<void *> device_ptr_list;
device_ptr_list.emplace_back(nullptr);
for (size_t i = 0; i < size_list.size(); ++i) {
device_ptr_list.emplace_back(nullptr);
}
return device_ptr_list;
}
} // namespace device