diff --git a/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc b/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc index 348d39360df..950c9aa97a2 100644 --- a/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc +++ b/mindspore/ccsrc/runtime/device/ascend/ascend_kernel_runtime.cc @@ -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(*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 AscendKernelRuntime::CreateDeviceEvent() { uint64_t AscendKernelRuntime::GetAvailableMemMaxSize() const { auto ascend_mem_manager = std::dynamic_pointer_cast(mem_manager_); + MS_EXCEPTION_IF_NULL(ascend_mem_manager); return ascend_mem_manager->GetDeviceMemSize(); } diff --git a/mindspore/ccsrc/runtime/device/convert_tensor_utils.cc b/mindspore/ccsrc/runtime/device/convert_tensor_utils.cc index 6f800ab383f..d5b3dd99665 100644 --- a/mindspore/ccsrc/runtime/device/convert_tensor_utils.cc +++ b/mindspore/ccsrc/runtime/device/convert_tensor_utils.cc @@ -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(src); auto float_data = static_cast(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(src); auto half_data = static_cast(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(src); auto float_data = static_cast(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(src); auto double_data = static_cast(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(src); auto int_data = static_cast(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(src); auto half_data = static_cast(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(src); auto int_data = static_cast(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(src); auto long_data = static_cast(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(dst); auto src_data = static_cast(src); diff --git a/mindspore/ccsrc/runtime/device/convert_tensor_utils.h b/mindspore/ccsrc/runtime/device/convert_tensor_utils.h index 496cfae44e4..e9d209586eb 100644 --- a/mindspore/ccsrc/runtime/device/convert_tensor_utils.h +++ b/mindspore/ccsrc/runtime/device/convert_tensor_utils.h @@ -35,6 +35,9 @@ void ConvertSameType(void *dst, const void *src, size_t size, TypeId type); template 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]; } diff --git a/mindspore/ccsrc/runtime/device/kernel_adjust.cc b/mindspore/ccsrc/runtime/device/kernel_adjust.cc index e714b14608f..851aefbbe72 100644 --- a/mindspore/ccsrc/runtime/device/kernel_adjust.cc +++ b/mindspore/ccsrc/runtime/device/kernel_adjust.cc @@ -713,6 +713,7 @@ bool KernelAdjust::StepLoadCtrlInputs(const std::shared_ptr= input_nodes.size()) { MS_LOG(EXCEPTION) << "deal_index[" << deal_index << "] out of range"; @@ -722,7 +723,6 @@ bool KernelAdjust::StepLoadCtrlInputs(const std::shared_ptrisa()) { auto pk_node = input_node->cast(); - MS_EXCEPTION_IF_NULL(tensor); MS_EXCEPTION_IF_NULL(pk_node); if (tensor->NeedSyncHostToDevice() || !pk_node->has_default()) { need_sync = true; diff --git a/mindspore/ccsrc/runtime/device/kernel_info.cc b/mindspore/ccsrc/runtime/device/kernel_info.cc index f85579221e9..ae47a00233e 100644 --- a/mindspore/ccsrc/runtime/device/kernel_info.cc +++ b/mindspore/ccsrc/runtime/device/kernel_info.cc @@ -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); diff --git a/mindspore/ccsrc/runtime/device/kernel_runtime.cc b/mindspore/ccsrc/runtime/device/kernel_runtime.cc index 53d58ecf080..7d2af553466 100644 --- a/mindspore/ccsrc/runtime/device/kernel_runtime.cc +++ b/mindspore/ccsrc/runtime/device/kernel_runtime.cc @@ -54,6 +54,7 @@ std::vector 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() && 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 } 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(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(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()) { - if (output_node_with_index.first->isa()) { - auto param = output_node_with_index.first->cast(); - if (!param->has_default()) { + auto output_node = output_node_with_index.first; + MS_EXCEPTION_IF_NULL(output_node); + if (!output_node->isa()) { + if (output_node->isa()) { + auto param = output_node->cast(); + 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(); + auto real_output_cnode = output_node->cast(); 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 need_alloc_nodes; auto add_need_alloc_nodes = [&need_alloc_nodes, graph, this](const AnfNodePtr &node) { + MS_EXCEPTION_IF_NULL(node); if (!node->isa()) { return; } @@ -312,7 +316,7 @@ void KernelRuntime::AssignStaticMemoryInput(const session::KernelGraph *graph) { return; } auto input_param = node->cast(); - 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(); + 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 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()) { 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() && (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() && (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."; diff --git a/mindspore/ccsrc/runtime/device/kernel_runtime_manager.cc b/mindspore/ccsrc/runtime/device/kernel_runtime_manager.cc index ac2f807a582..903b4e672df 100644 --- a/mindspore/ccsrc/runtime/device/kernel_runtime_manager.cc +++ b/mindspore/ccsrc/runtime/device/kernel_runtime_manager.cc @@ -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) { diff --git a/mindspore/ccsrc/runtime/device/launch_kernel.cc b/mindspore/ccsrc/runtime/device/launch_kernel.cc index cf0101b86cf..74f8e435faf 100644 --- a/mindspore/ccsrc/runtime/device/launch_kernel.cc +++ b/mindspore/ccsrc/runtime/device/launch_kernel.cc @@ -22,7 +22,11 @@ namespace mindspore::device { std::vector LaunchKernel::ObtainKernelAddress(const std::vector &list, std::vector *addr) { + MS_EXCEPTION_IF_NULL(addr); std::vector 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); diff --git a/mindspore/ccsrc/runtime/device/launch_mul.cc b/mindspore/ccsrc/runtime/device/launch_mul.cc index cd60dc5ca9a..ff3b157d1f4 100644 --- a/mindspore/ccsrc/runtime/device/launch_mul.cc +++ b/mindspore/ccsrc/runtime/device/launch_mul.cc @@ -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 diff --git a/mindspore/ccsrc/runtime/device/memory_manager.cc b/mindspore/ccsrc/runtime/device/memory_manager.cc index d6d08882eec..ac1029fef9a 100644 --- a/mindspore/ccsrc/runtime/device/memory_manager.cc +++ b/mindspore/ccsrc/runtime/device/memory_manager.cc @@ -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_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 MemoryManager::MallocContinuousMemFromMemPool(size_t total_s MS_LOG(ERROR) << "MallocContinuousMemFromMemPool total_size is 0."; } std::vector 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