diff --git a/mindspore/ccsrc/CMakeLists.txt b/mindspore/ccsrc/CMakeLists.txt index d2d19ec0af5..1339b4b9ea7 100644 --- a/mindspore/ccsrc/CMakeLists.txt +++ b/mindspore/ccsrc/CMakeLists.txt @@ -60,11 +60,6 @@ if(ENABLE_GPU) add_compile_definitions(ENABLE_GPU) endif () -## make flatuffer files -include_directories("${CMAKE_BINARY_DIR}/predict/schema/inner") -file(GLOB_RECURSE FLATBUFFER_IN RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "predict/schema/*.fbs") -set(FLATBUFFER_OU "${CMAKE_BINARY_DIR}/predict/schema/inner") -ms_build_flatbuffers("${FLATBUFFER_IN}" "${FLATBUFFER_IN}" flat_input "${FLATBUFFER_OU}") ## make protobuf files file(COPY "${ms_onnx_INC}/onnx/onnx.proto" DESTINATION ${CMAKE_BINARY_DIR}/proto) @@ -104,13 +99,9 @@ endif () if (ENABLE_D) include_directories("${CMAKE_BINARY_DIR}/backend/kernel_compiler/aicpu") - include_directories("${CMAKE_BINARY_DIR}/predict/generator/ir") file(GLOB_RECURSE PROTO_IN RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "backend/kernel_compiler/aicpu/proto/*.proto") ms_protobuf_generate(PROTOSRCS PROTOHDRS ${PROTO_IN}) - file(GLOB_RECURSE PROTO_INNER RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "predict/proto/*.proto") - ms_protobuf_generate(PREDICT_PROTOSRCS PREDICT_PROTOHDRS ${PROTO_INNER}) - file(GLOB_RECURSE PROTO_DUMP RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "runtime/device/ascend/dump/proto/*.proto") ms_protobuf_generate(DUMP_PROTOSRCS PROTOHDRS ${PROTO_DUMP}) @@ -139,7 +130,7 @@ set(SUB_COMP frontend/operator pipeline/jit pipeline/pynative - common debug gvar predict pybind_api utils vm + common debug gvar pybind_api utils vm ) foreach (_comp ${SUB_COMP}) @@ -147,7 +138,7 @@ foreach (_comp ${SUB_COMP}) string(REPLACE "/" "_" sub ${_comp}) if (TARGET _mindspore_${sub}_obj) list(APPEND SUB_OBJECTS_SRC $) - add_dependencies(_mindspore_${sub}_obj proto_input flat_input) + add_dependencies(_mindspore_${sub}_obj proto_input ) endif () endforeach () add_subdirectory(${CMAKE_SOURCE_DIR}/mindspore/core/base base) @@ -158,7 +149,7 @@ add_subdirectory(${CMAKE_SOURCE_DIR}/mindspore/core/utils util) list(APPEND SUB_OBJECTS_SRC $) add_subdirectory(${CMAKE_SOURCE_DIR}/mindspore/core/ir ir) list(APPEND SUB_OBJECTS_SRC $) -add_dependencies(_mindspore_core_utils_obj _mindspore_base_obj _mindspore_ir_obj _mindspore_abstract_obj proto_input flat_input) +add_dependencies(_mindspore_core_utils_obj _mindspore_base_obj _mindspore_ir_obj _mindspore_abstract_obj proto_input ) set_property(SOURCE ${SUB_OBJECTS_SRC} PROPERTY COMPILE_DEFINITIONS SUBMODULE_ID=mindspore::SubModuleId::SM_ME) add_library(mindspore STATIC ${SUB_OBJECTS_SRC}) diff --git a/mindspore/ccsrc/backend/session/ascend_session.cc b/mindspore/ccsrc/backend/session/ascend_session.cc index cf2f85ed9f5..39bfd9f9eab 100644 --- a/mindspore/ccsrc/backend/session/ascend_session.cc +++ b/mindspore/ccsrc/backend/session/ascend_session.cc @@ -34,7 +34,6 @@ #include "runtime/device/kernel_adjust.h" #include "runtime/device/ascend/ascend_stream_assign.h" #include "runtime/device/ascend/ascend_label_assign.h" -#include "predict/predict.h" #include "backend/session/anf_runtime_algorithm.h" #include "ir/scalar.h" #include "debug/anf_ir_dump.h" @@ -303,8 +302,6 @@ void AscendSession::CompileChildGraph(const KernelGraphPtr &child_graph) { save_graphs_path + "/" + "select_kernel_after" + "_graph_" + std::to_string(child_graph->graph_id()) + ".ir"; DumpIR(file_path, child_graph); } - // convert kernel Graph to model - predictmodel::StepConvertGraph(child_graph); // optimize graph HardwareOptimize(child_graph); // assign static memory of parameters @@ -333,8 +330,6 @@ void AscendSession::RunGraph(const GraphId &graph_id, const std::vector graph, memo->insert(graph.get()); MS_LOG(INFO) << "Start to do HardwareOptimize in graph: " << graph->graph_id(); - // convert kernel Graph to model - predictmodel::StepConvertGraph(graph.get()); HardwareOptimize(graph.get()); for (auto &child_graph : graph->child_graph_order()) { diff --git a/mindspore/ccsrc/backend/session/cpu_session.cc b/mindspore/ccsrc/backend/session/cpu_session.cc index 94862a0a59d..6230699a3da 100644 --- a/mindspore/ccsrc/backend/session/cpu_session.cc +++ b/mindspore/ccsrc/backend/session/cpu_session.cc @@ -23,7 +23,6 @@ #include "common/utils.h" #include "backend/session/anf_runtime_algorithm.h" #include "runtime/device/kernel_runtime.h" -#include "predict/predict.h" #include "backend/kernel_compiler/cpu/cpu_kernel_factory.h" #include "runtime/device/cpu/kernel_select_cpu.h" #include "backend/optimizer/common/optimizer.h" @@ -79,7 +78,6 @@ GraphId CPUSession::CompileGraph(const AnfNodePtrList &lst, const AnfNodePtrList Optimize(graph); } #endif - predictmodel::StepConvertGraph(graph); MS_LOG(INFO) << "Build kernel"; BuildKernel(graph.get()); MS_LOG(INFO) << "Assign kernel address"; @@ -100,7 +98,6 @@ void CPUSession::RunGraph(const GraphId &graph_id, const std::vector need_sync_outputs; runtime_.BindInputOutput(kernel_graph.get(), inputs, outputs, &need_sync_outputs); MS_LOG(INFO) << "Run graph start"; - predictmodel::StepConvertWeight(inputs); auto execution_order = kernel_graph->execution_order(); Reorder(&execution_order); diff --git a/mindspore/ccsrc/backend/session/gpu_session.cc b/mindspore/ccsrc/backend/session/gpu_session.cc index 9d0a1401086..d608f69edbf 100644 --- a/mindspore/ccsrc/backend/session/gpu_session.cc +++ b/mindspore/ccsrc/backend/session/gpu_session.cc @@ -31,7 +31,6 @@ #include "backend/optimizer/gpu/replace_momentum_cast_fusion.h" #include "backend/optimizer/gpu/replace_addn_fusion.h" #include "runtime/device/kernel_runtime_manager.h" -#include "predict/predict.h" #include "common/utils.h" #include "common/trans.h" #include "utils/context/ms_context.h" @@ -190,8 +189,6 @@ GraphId GPUSession::CompileGraph(const AnfNodePtrList &lst, const AnfNodePtrList // Assign parameter keys. AssignParamKey(graph); #endif - // Convert kernel Graph to model - predictmodel::StepConvertGraph(graph); // Start gpu kernel runtime StartKernelRT(); // Dump .pb graph before hardware optimization @@ -245,8 +242,6 @@ void GPUSession::RunGraph(const GraphId &graph_id, const std::vectorType(); - MS_EXCEPTION_IF_NULL(type_ptr); - if (type_ptr->isa()) { - auto tensor_ptr = type_ptr->cast(); - MS_EXCEPTION_IF_NULL(tensor_ptr); - TypePtr elem = tensor_ptr->element(); - return elem; - } else if (type_ptr->isa()) { - auto tuple_ptr = type_ptr->cast(); - MS_EXCEPTION_IF_NULL(tuple_ptr); - auto tuple_i = (*tuple_ptr)[0]; - MS_EXCEPTION_IF_NULL(tuple_i); - if (tuple_i->isa()) { - auto tensor_ptr = tuple_i->cast(); - MS_EXCEPTION_IF_NULL(tensor_ptr); - TypePtr elem = tensor_ptr->element(); - MS_EXCEPTION_IF_NULL(elem); - return elem; - } else if (tuple_i->isa()) { - return type_ptr; - } else { - MS_LOG(EXCEPTION) << "unsupported type: " << type_ptr->ToString(); - } - } else if (type_ptr->isa()) { - return type_ptr; - } - std::string type_name = type_ptr->ToString(); - MS_LOG(EXCEPTION) - << "The output type of node should be a tensor type a number or a tuple of tensor type, but this is: " - << type_name; -} - -MsDataType GetMSDataType(TypeId ori_data_type) { - MsDataType dst_data_type; - switch (ori_data_type) { - case kNumberTypeFloat16: - dst_data_type = mindspore::predict::DataType_DT_FLOAT16; - return dst_data_type; - case kNumberTypeFloat32: - dst_data_type = mindspore::predict::DataType_DT_FLOAT; - return dst_data_type; - case kNumberTypeInt8: - dst_data_type = mindspore::predict::DataType_DT_INT8; - return dst_data_type; - case kNumberTypeInt32: - dst_data_type = mindspore::predict::DataType_DT_INT32; - return dst_data_type; - case kNumberTypeUInt8: - dst_data_type = mindspore::predict::DataType_DT_UINT8; - return dst_data_type; - case kNumberTypeUInt32: - dst_data_type = mindspore::predict::DataType_DT_UINT32; - return dst_data_type; - case kTypeUnknown: - dst_data_type = mindspore::predict::DataType_DT_UNDEFINED; - return dst_data_type; - default: - MS_LOG(EXCEPTION) << "Ms don't support this DataType"; - } -} - -MsFormat GetMsFormat(const std::string &format_str) { - if (format_str == kOpFormat_DEFAULT) { - MsFormat ms_format = predict::Format_NCHW; - return ms_format; - } else { - // all middle format default to NCHW - return predict::Format_NCHW; - } -} - -TensorPtr GetParaAscendTensor(const AnfNodePtr &anf_node) { - MS_EXCEPTION_IF_NULL(anf_node); - if (!anf_node->isa()) { - return nullptr; - } - auto device_type_id = AnfAlgo::GetOutputDeviceDataType(anf_node, 0); - // device type_ptr - auto device_type_ptr = GetTypePtr(anf_node); - // device shape - auto shape = AnfAlgo::GetOutputDeviceShape(anf_node, 0); - std::vector tensor_shape; - (void)std::transform(shape.begin(), shape.end(), std::back_inserter(tensor_shape), SizeToInt); - // device format - auto format = AnfAlgo::GetOutputFormat(anf_node, 0); - // device tensor - TensorPtr device_tensor = std::make_shared(device_type_id, tensor_shape); - // device info - device_tensor->SetDeviceInfo(format, device_type_ptr); - return device_tensor; -} - -TensorPtr GetParaCpuTensor(const AnfNodePtr &anf_node) { - MS_EXCEPTION_IF_NULL(anf_node); - if (!(anf_node->isa())) { - return nullptr; - } else { - auto ori_type_id = AnfAlgo::GetOutputInferDataType(anf_node, 0); - auto ori_type_ptr = GetTypePtr(anf_node); - auto ori_shape = AnfAlgo::GetOutputInferShape(anf_node, 0); - std::vector tensor_shape; - (void)std::transform(ori_shape.begin(), ori_shape.end(), std::back_inserter(tensor_shape), SizeToInt); - auto ori_format = AnfAlgo::GetOutputFormat(anf_node, 0); - TensorPtr cpu_tensor = std::make_shared(ori_type_id, tensor_shape); - cpu_tensor->SetDeviceInfo(ori_format, ori_type_ptr); - return cpu_tensor; - } -} - -TensorPtr GetValueTensor(const ValueNodePtr &const_node) { - MS_EXCEPTION_IF_NULL(const_node); - auto value_ptr = const_node->value(); - MS_EXCEPTION_IF_NULL(value_ptr); - if (!value_ptr->isa()) { - return nullptr; - } - TensorPtr tensor = value_ptr->cast(); - MS_EXCEPTION_IF_NULL(tensor); - auto data_type = tensor->Dtype(); - MS_EXCEPTION_IF_NULL(data_type); - auto type_id = data_type->type_id(); - auto shape = tensor->shape(); - TensorPtr tensor_constant = std::make_shared(type_id, shape); - tensor_constant->SetDeviceInfo(tensor->device_info().format_, tensor->device_info().data_type_); - return tensor_constant; -} - -TensorPtr GetKernelCpuTensor(const CNodePtr &c_node_ptr, size_t inx) { - if (c_node_ptr == nullptr || inx >= AnfAlgo::GetOutputTensorNum(c_node_ptr)) { - MS_LOG(ERROR) << "GetKernelCpuTensor failed"; - return nullptr; - } - auto ori_shape = AnfAlgo::GetOutputInferShape(c_node_ptr, inx); - auto ori_type_id = AnfAlgo::GetOutputInferDataType(c_node_ptr, inx); - std::vector tensor_shape; - (void)std::transform(ori_shape.begin(), ori_shape.end(), std::back_inserter(tensor_shape), SizeToInt); - auto ori_output_type = GetTypePtr(c_node_ptr); - TensorPtr device_tensor = std::make_shared(ori_type_id, tensor_shape); - auto format = AnfAlgo::GetOutputFormat(c_node_ptr, inx); - device_tensor->SetDeviceInfo(format, ori_output_type); - return device_tensor; -} - -TensorPtr GetKernelAscendTensor(const CNodePtr &c_node_ptr, size_t inx) { - if (c_node_ptr == nullptr || inx >= AnfAlgo::GetOutputTensorNum(c_node_ptr)) { - MS_LOG(ERROR) << "GetKernelAscendTensor failed"; - return nullptr; - } - auto shape = AnfAlgo::GetOutputDeviceShape(c_node_ptr, inx); - std::vector tensor_shape; - (void)std::transform(shape.begin(), shape.end(), std::back_inserter(tensor_shape), SizeToInt); - auto format = AnfAlgo::GetOutputFormat(c_node_ptr, inx); - auto type_id = AnfAlgo::GetOutputDeviceDataType(c_node_ptr, inx); - auto output_type_ptr = GetTypePtr(c_node_ptr); - TensorPtr device_tensor = std::make_shared(type_id, tensor_shape); - device_tensor->SetDeviceInfo(format, output_type_ptr); - return device_tensor; -} - -TensorPtr GetOutputTensor(const AnfNodePtr &out_node, size_t inx) { - MS_EXCEPTION_IF_NULL(out_node); - auto shape = AnfAlgo::GetOutputInferShape(out_node, inx); - std::vector tensor_shape; - (void)std::transform(shape.begin(), shape.end(), std::back_inserter(tensor_shape), SizeToInt); - auto type_id = AnfAlgo::GetOutputInferDataType(out_node, inx); - auto output_type_ptr = GetTypePtr(out_node); - auto format = AnfAlgo::GetOutputFormat(out_node, inx); - TensorPtr output_tensor = std::make_shared(type_id, tensor_shape); - output_tensor->SetDeviceInfo(format, output_type_ptr); - return output_tensor; -} - -bool FindNodeInMap(const std::unordered_map &node_map, const AnfNodePtr &node) { - return std::any_of(node_map.begin(), node_map.end(), - [node](const std::pair &kernel_key) { return kernel_key.first == node.get(); }); -} - -bool SaveDeviceModelUtil(const std::shared_ptr &new_ms_graph_ptr, const std::string &save_path_name, - SubGraphDefT *sub_graph) { - MS_EXCEPTION_IF_NULL(new_ms_graph_ptr); - MS_EXCEPTION_IF_NULL(sub_graph); - // save mindspore schema to file - new_ms_graph_ptr->name = "default_graph"; - std::unique_ptr sub_graph_ptr(sub_graph); - new_ms_graph_ptr->subgraphs.emplace_back(std::move(sub_graph_ptr)); - // get flatbuffer builder - flatbuffers::FlatBufferBuilder builder(1024); - auto offset = mindspore::predict::GraphDef::Pack(builder, new_ms_graph_ptr.get()); - builder.Finish(offset); - auto size = builder.GetSize(); - if (size == 0) { - MS_LOG(ERROR) << "builder has no size"; - return false; - } - auto content = builder.GetBufferPointer(); - std::ofstream output(save_path_name); - if (!output.is_open()) { - MS_LOG(EXCEPTION) << "mindspore.mindspoire output failed"; - } - (void)output.write((const char *)content, size); - output.close(); - return true; -} -} // namespace utils -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/attr_utils/convert_util.h b/mindspore/ccsrc/predict/converter/attr_utils/convert_util.h deleted file mode 100644 index 92fb5f63c94..00000000000 --- a/mindspore/ccsrc/predict/converter/attr_utils/convert_util.h +++ /dev/null @@ -1,60 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_CCSRC_PREDICT_CONVERTER_ATTR_UTILS_CONVERT_UTIL_H_ -#define MINDSPORE_CCSRC_PREDICT_CONVERTER_ATTR_UTILS_CONVERT_UTIL_H_ - -#include -#include -#include -#include -#include -#include -#include -#include "ir/tensor.h" -#include "backend/session/anf_runtime_algorithm.h" -#include "predict/schema/inner/ms_generated.h" - -using TensorPtr = mindspore::tensor::TensorPtr; -using TensorPtrList = std::vector; -using AllOutputTensors = std::unordered_map; -using OpDefT = mindspore::predict::OpDefT; -using GraphDefT = mindspore::predict::GraphDefT; -using TensorDefT = mindspore::predict::TensorDefT; -using SubGraphDefT = mindspore::predict::SubGraphDefT; -using SubGraphPtr = std::unique_ptr; -using MsDataType = mindspore::predict::DataType; -using MsFormat = mindspore::predict::Format; -using MsKernelKey = void *; -namespace mindspore { -namespace predict { -namespace utils { -TypePtr GetTypePtr(const AnfNodePtr &anf_node); -MsDataType GetMSDataType(TypeId ori_data_type); -MsFormat GetMsFormat(const std::string &format_str); -TensorPtr GetParaAscendTensor(const AnfNodePtr &anf_node); -TensorPtr GetParaCpuTensor(const AnfNodePtr &anf_node); -TensorPtr GetValueTensor(const ValueNodePtr &const_node); -TensorPtr GetKernelCpuTensor(const CNodePtr &c_node_ptr, size_t inx); -TensorPtr GetKernelAscendTensor(const CNodePtr &c_node_ptr, size_t inx); -TensorPtr GetOutputTensor(const AnfNodePtr &out_node, size_t inx); -bool FindNodeInMap(const std::unordered_map &Nodemap, const AnfNodePtr &node); -bool SaveDeviceModelUtil(const std::shared_ptr &new_ms_graph_ptr, const std::string &save_path_name, - SubGraphDefT *sub_graph_def_t); -} // namespace utils -} // namespace predict -} // namespace mindspore -#endif // MINDSPORE_CCSRC_PREDICT_CONVERTER_ATTR_UTILS_CONVERT_UTIL_H_ diff --git a/mindspore/ccsrc/predict/converter/attr_utils/op_attr_type.h b/mindspore/ccsrc/predict/converter/attr_utils/op_attr_type.h deleted file mode 100644 index 381da7678c8..00000000000 --- a/mindspore/ccsrc/predict/converter/attr_utils/op_attr_type.h +++ /dev/null @@ -1,65 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_CCSRC_PREDICT_CONVERTER_CPU_ATTR_UTILS_OP_ATTR_TYPE_H_ -#define MINDSPORE_CCSRC_PREDICT_CONVERTER_CPU_ATTR_UTILS_OP_ATTR_TYPE_H_ -namespace mindspore { -namespace predict { -namespace convert { -typedef enum CpuOpType { - CPU_OP_PAD = 0, - CPU_OP_MAXIMUM, - CPU_OP_CONCAT, - CPU_OP_SOFTMAX, - CPU_OP_ACTIVATION, - CPU_OP_CONV2D, - CPU_OP_FUSEDBATCHNORM, - CPU_OP_CAFFEBATCHNORM, - CPU_OP_SQUEEZE, - CPU_OP_BIASADD, - CPU_OP_POOLING, - CPU_OP_DEPTHWISECONV2D, - CPU_OP_DEDEPTHWISECONV2D, - CPU_OP_RESIZE, - CPU_OP_DETECTIONPOSTPROCESS, - CPU_OP_FULLCONNECTION, - CPU_OP_MEAN, - CPU_OP_DECONV2D, - CPU_OP_SCALE, - CPU_OP_ELTWISE, - CPU_OP_ADD, - CPU_OP_SLICE, - CPU_OP_MUL, - CPU_OP_EXP, - CPU_OP_RESHAPE, - CPU_OP_POWER, - CPU_OP_ARGMAX, - CPU_OP_ARGMAX_NETOUTPUT, - CPU_OP_MATMUL, - CPU_OP_CAFFEPRELU, - CPU_OP_STRIDEDSLICE, - CPU_OP_STACK, - CPU_OP_RANGE, - CPU_OP_EXPANDDIMS, - CPU_OP_TILE, - CPU_OP_CAST, - CPU_OP_CAFFECROP, - CPU_OP_PRESERVEED = 37 -} CpuOpType_t; -} // namespace convert -} // namespace predict -} // namespace mindspore -#endif // MINDSPORE_CCSRC_PREDICT_CONVERTER_CPU_ATTR_UTILS_OP_ATTR_TYPE_H_ diff --git a/mindspore/ccsrc/predict/converter/executor_tensor.cc b/mindspore/ccsrc/predict/converter/executor_tensor.cc deleted file mode 100644 index b51496a9b4f..00000000000 --- a/mindspore/ccsrc/predict/converter/executor_tensor.cc +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/executor_tensor.h" - -namespace mindspore { -namespace executor { -int TensorCache::addExTensor(int tensor_key, const TensorPtr &tensor, int refCount, const std::vector &host_shape, - ExTensorType stable, bool inc) { - MS_EXCEPTION_IF_NULL(tensor); - TensorPtr tmp_tensor = tensor; - ExTensorPtr ex_tensor_ptr = - std::make_shared(tensor_key, tmp_tensor, refCount, nodeIndex, host_shape, stable); - int pre_index = ex_tensor_ptr->index_; - if (inc) { - nodeIndex++; - } - // no need to judge,just add to map directly - tensors[tensor_key].push_back(ex_tensor_ptr); - return pre_index; -} - -std::vector TensorCache::findTensor(int key) { - std::vector ex_tensors; - auto iter = tensors.find(key); - if (iter != tensors.end()) { - return iter->second; - } else { - MS_LOG(INFO) << "can not find any tensorlist"; - return ex_tensors; - } -} - -void TensorCache::deleteTensor(int key) { (void)tensors.erase(key); } -} // namespace executor -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/executor_tensor.h b/mindspore/ccsrc/predict/converter/executor_tensor.h deleted file mode 100644 index b33cea030d5..00000000000 --- a/mindspore/ccsrc/predict/converter/executor_tensor.h +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright 2019-2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_CCSRC_PREDICT_CONVERTER_EXECUTOR_TENSOR_H_ -#define MINDSPORE_CCSRC_PREDICT_CONVERTER_EXECUTOR_TENSOR_H_ - -#include -#include -#include -#include -#include "ir/tensor.h" - -namespace mindspore { -namespace executor { -using TensorPtr = tensor::TensorPtr; -static constexpr int MS_MAX_REFCOUNT = 999; -enum ExTensorType { INPUTDATA, WEIGHTS, CONSTANT, KERNEL, OUTPUT }; -class ExTensor { - public: - int key_; - TensorPtr device_tensor_ptr_; - int ref_count_; - int index_; - std::vector host_shape_; - ExTensorType stable_; - ExTensor(int key, TensorPtr tensor_ptr, int ref_count, int index, std::vector host_shape, - ExTensorType ex_tensor_type) - : key_(key), - device_tensor_ptr_(std::move(tensor_ptr)), - ref_count_(ref_count), - index_(index), - host_shape_(std::move(host_shape)), - stable_(ex_tensor_type) {} - ~ExTensor() { host_shape_.clear(); } -}; -using ExTensorPtr = std::shared_ptr; -class TensorCache { - public: - TensorCache() = default; - - ~TensorCache() { tensors.clear(); } - - int addExTensor(int tensor_key, const TensorPtr &tensor, int refCount, const std::vector &host_shape, - ExTensorType stable, bool inc = true); - // just adjust for dynamic tensor - std::vector findTensor(int key); - void deleteTensor(int key); - const std::unordered_map> &GetCachedTensor() const { return tensors; } - - private: - std::unordered_map> tensors; - int nodeIndex = 0; -}; -using TensorCachePtr = std::shared_ptr; -} // namespace executor -} // namespace mindspore -#endif // MINDSPORE_CCSRC_PREDICT_CONVERTER_EXECUTOR_TENSOR_H_ diff --git a/mindspore/ccsrc/predict/converter/kernel2ms.cc b/mindspore/ccsrc/predict/converter/kernel2ms.cc deleted file mode 100644 index 04aceb62eb8..00000000000 --- a/mindspore/ccsrc/predict/converter/kernel2ms.cc +++ /dev/null @@ -1,561 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/kernel2ms.h" -#include -#include "ir/anf.h" -#include "predict/converter/lite_model/op_attr_packer.h" -#include "mindspore/ccsrc/frontend/operator/ops.h" - -namespace mindspore { -namespace executor { -Kernel2Ms &Kernel2Ms::GetInstance() { - static Kernel2Ms instance; - return instance; -} - -bool Kernel2Ms::SetMemResue() const { - MS_LOG(INFO) << "MemResue start"; - return true; -} - -bool Kernel2Ms::SetAllTensors(const TensorCachePtr &tensor_cache, SubGraphDefT *ms_graph) { - if (tensor_cache == nullptr || ms_graph == nullptr) { - return false; - } - const std::unordered_map> &cachedTensors = tensor_cache->GetCachedTensor(); - size_t total_size = 0; - if (cachedTensors.empty()) { - return false; - } - for (auto &iter : cachedTensors) { - auto ex_tensors = iter.second; - total_size += ex_tensors.size(); - } - ms_graph->allTensors.resize(total_size); - for (auto &iter : cachedTensors) { - for (auto &ex_tensor : iter.second) { - std::unique_ptr ms_tensor(new TensorDefT()); - auto device_tensor_tmp = ex_tensor->device_tensor_ptr_; - auto device_d_type = device_tensor_tmp->data_type(); - ms_tensor->dataType = predict::utils::GetMSDataType(device_d_type); - auto device_shape = device_tensor_tmp->shape(); - ms_tensor->dims.clear(); - if (device_shape.empty()) { - ms_tensor->dims.push_back(1); - } else { - ms_tensor->dims.assign(device_shape.begin(), device_shape.end()); - } - std::string format_str = device_tensor_tmp->device_info().format_; - ms_tensor->format = predict::utils::GetMsFormat(format_str); - ms_tensor->offset = 0; - auto stable = ex_tensor->stable_; - if (stable == INPUTDATA || stable == CONSTANT || stable == WEIGHTS) { - ms_tensor->refCount = MS_MAX_REFCOUNT; - } else { - ms_tensor->refCount = 0; - } - ms_graph->allTensors[IntToSize(ex_tensor->index_)] = std::move(ms_tensor); - } - } - return true; -} - -bool Kernel2Ms::SetGraphOutputIdx(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache, - SubGraphDefT *ms_graph, AllOutputTensors *all_output_tensors) { - MS_EXCEPTION_IF_NULL(tensor_cache); - MS_EXCEPTION_IF_NULL(ms_graph); - MS_EXCEPTION_IF_NULL(all_output_tensors); - auto out_nodes = kernel_graph_ptr->outputs(); - if (out_nodes.empty()) { - return false; - } - // maybe need to judge out_nodes is real && output must be CNode - for (size_t i = 0; i < out_nodes.size(); ++i) { - std::vector real_inputs_link; - std::vector real_output_idx_link; - GetRealInpoutsPtr(out_nodes[i], &real_inputs_link, &real_output_idx_link); - if (real_inputs_link.empty()) { - MS_LOG(INFO) << "this graph output node is vitural node, has no real input"; - continue; - } - for (size_t k = 0; k < real_inputs_link.size(); ++k) { - int key = node_indexs_[out_nodes[i].get()]; - auto ex_tensor_list = tensor_cache->findTensor(key); - if (ex_tensor_list.empty()) { - MS_LOG(INFO) << "SetGraphOutputIdx do not add Extensor "; - continue; - } - auto ex_tensor = ex_tensor_list[real_output_idx_link[k]]; - ex_tensor_list.clear(); - ms_graph->outputIndex.push_back(ex_tensor->index_); - } - } - return true; -} - -bool Kernel2Ms::SetOpOutputIdx(const CNodePtr &c_node_ptr, const TensorPtr &output_tensor, - const TensorCachePtr &tensor_cache, int ref_count, size_t order_index, OpDefT *ms_node) { - MS_EXCEPTION_IF_NULL(c_node_ptr); - MS_EXCEPTION_IF_NULL(output_tensor); - MS_EXCEPTION_IF_NULL(ms_node); - MS_EXCEPTION_IF_NULL(tensor_cache); - if (!predict::utils::FindNodeInMap(node_indexs_, c_node_ptr)) { - MS_LOG(ERROR) << "can not find any pk_key in inited node_indexs map"; - return false; - } - int tensor_key = node_indexs_[c_node_ptr.get()]; - auto host_shape = AnfAlgo::GetOutputInferShape(c_node_ptr, order_index); - std::vector tensor_shape; - (void)std::transform(host_shape.begin(), host_shape.end(), std::back_inserter(tensor_shape), SizeToInt); - int outputIndex = tensor_cache->addExTensor(tensor_key, output_tensor, ref_count, tensor_shape, KERNEL); - ms_node->outputIndex.push_back(outputIndex); - return true; -} - -void Kernel2Ms::GetRealInpoutsPtr(const AnfNodePtr &node, std::vector *real_inputs, - std::vector *real_output_idx) { - MS_EXCEPTION_IF_NULL(real_inputs); - MS_EXCEPTION_IF_NULL(real_output_idx); - size_t default_idx = 0; - if (node->isa()) { - auto c_node = node->cast(); - MS_EXCEPTION_IF_NULL(c_node); - std::string c_node_name = GetCNodeFuncName(c_node); - if (c_node_name == prim::kPrimTupleGetItem->name()) { - auto v_node = c_node->inputs()[kTupleGetItemIndex]->cast(); - MS_EXCEPTION_IF_NULL(v_node); - default_idx = IntToSize(GetValue(v_node->value())); - real_inputs->push_back(c_node->inputs()[1]); - real_output_idx->push_back(default_idx); - return; - } else if (c_node_name == prim::kPrimDepend->name()) { - GetRealInpoutsPtr(c_node->inputs()[1], real_inputs, real_output_idx); - return; - } else if (c_node_name == prim::kPrimMakeTuple->name()) { - for (auto &in : c_node->inputs()) { - GetRealInpoutsPtr(in, real_inputs, real_output_idx); - } - return; - } else { - real_inputs->push_back(node); - real_output_idx->push_back(default_idx); - } - } else if (node->isa()) { - real_inputs->push_back(node); - real_output_idx->push_back(default_idx); - } else if (node->isa()) { - real_inputs->push_back(node); - real_output_idx->push_back(default_idx); - } -} - -bool Kernel2Ms::SetOpInputIdx(const CNodePtr &c_node_ptr, const TensorCachePtr &tensor_cache, OpDefT *ms_node) { - MS_EXCEPTION_IF_NULL(c_node_ptr); - MS_EXCEPTION_IF_NULL(tensor_cache); - MS_EXCEPTION_IF_NULL(ms_node); - for (size_t i = 1; i < c_node_ptr->inputs().size(); ++i) { - std::vector real_inputs; - std::vector real_output_idx; - GetRealInpoutsPtr(c_node_ptr->inputs()[i], &real_inputs, &real_output_idx); - if (real_inputs.empty()) { - MS_LOG(INFO) << "kernel has no inputs: " << c_node_ptr.get() << " input size[%lu]" << c_node_ptr->inputs().size(); - continue; - } - for (size_t j = 0; j < real_inputs.size(); ++j) { - int key = node_indexs_[real_inputs[j].get()]; - std::vector ex_tensor_list = tensor_cache->findTensor(key); - if (ex_tensor_list.empty()) { - continue; - } - ExTensorPtr ex_tensor_ptr = ex_tensor_list[real_output_idx[j]]; - ex_tensor_list.clear(); - ms_node->inputIndex.push_back(ex_tensor_ptr->index_); - } - } - return true; -} - -void Kernel2Ms::TransformGraphIndx() { - // transform index && anfnodeptr - if (node_indexs_.empty()) { - MS_LOG(EXCEPTION) << "node_indexs_ not ininted"; - } - for (auto &item : node_indexs_) { - index_nodes_[item.second] = item.first; - } -} - -bool Kernel2Ms::InitGraphInputsIndx(const KernelGraphPtr &kernel_graph_ptr) { - MS_EXCEPTION_IF_NULL(kernel_graph_ptr); - auto input_nodes = kernel_graph_ptr->inputs(); - if (input_nodes.empty()) { - return false; - } - for (const auto &input_node : input_nodes) { - if (input_node->isa()) { - if (!predict::utils::FindNodeInMap(node_indexs_, input_node)) { - // init every parameter node - node_indexs_[input_node.get()] = graph_index_; - graph_index_++; - } - } else { - MS_LOG(INFO) << "This node is anfnode, no need to handle, continue. node info: " << input_node->ToString(); - continue; - } - } - MS_LOG(DEBUG) << "inputs GraphIndex: " << graph_index_; - return true; -} - -bool Kernel2Ms::InitGraphValueNodesIndx(const KernelGraphPtr &kernel_graph_ptr) { - MS_EXCEPTION_IF_NULL(kernel_graph_ptr); - if (kernel_graph_ptr->value_nodes().empty()) { - return false; - } - for (auto &item : kernel_graph_ptr->value_nodes()) { - if (item.first->isa()) { - auto value_node = item.first->cast(); - MS_EXCEPTION_IF_NULL(value_node); - if (value_node == nullptr) { - MS_LOG(WARNING) << "value_node is nullptr"; - return false; - } - if (value_node->value() == nullptr) { - MS_LOG(ERROR) << "Constant value is null."; - return false; - } - if (!value_node->value()->isa()) { - continue; - } - if (!predict::utils::FindNodeInMap(node_indexs_, item.first)) { - // init node - auto node_ptr = item.first; - node_indexs_[node_ptr.get()] = graph_index_; - graph_index_++; - } - } - } - return true; -} - -bool Kernel2Ms::InitGraphOpsIndx(const KernelGraphPtr &kernel_graph_ptr) { - MS_EXCEPTION_IF_NULL(kernel_graph_ptr); - auto kernels = kernel_graph_ptr->execution_order(); - if (kernels.empty()) { - MS_LOG(WARNING) << "this graph has no kernel"; - return false; - } - for (size_t i = 0; i < kernels.size(); ++i) { - // for each kernel's inputs foreach real_input - if (kernels[i]->isa()) { - if (!predict::utils::FindNodeInMap(node_indexs_, kernels[i])) { - // init node - node_indexs_[kernels[i].get()] = graph_index_; - graph_index_++; - } - } - } - return true; -} - -bool Kernel2Ms::InitGraphOutputsIndx(const KernelGraphPtr &kernel_graph_ptr) { - MS_EXCEPTION_IF_NULL(kernel_graph_ptr); - // graph output && their inputs should link together - auto out_nodes = kernel_graph_ptr->outputs(); - if (out_nodes.empty()) { - MS_LOG(ERROR) << "this graph has no outputs"; - return false; - } - for (auto &item : out_nodes) { - if (!predict::utils::FindNodeInMap(node_indexs_, item)) { - node_indexs_[item.get()] = graph_index_; - graph_index_++; - } - } - return true; -} - -bool Kernel2Ms::InitGraphIndx(const KernelGraphPtr &kernel_graph_ptr) { - MS_EXCEPTION_IF_NULL(kernel_graph_ptr); - // only parameter - if (!InitGraphInputsIndx(kernel_graph_ptr)) { - return false; - } - // init value node - if (!InitGraphValueNodesIndx(kernel_graph_ptr)) { - return false; - } - // init op - if (!InitGraphOpsIndx(kernel_graph_ptr)) { - return false; - } - // init Graphoutput attention: out_put nodes have inputs - return InitGraphOutputsIndx(kernel_graph_ptr); -} - -bool Kernel2Ms::SetGraphInputTensors(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache, - SubGraphDefT *ms_graph) { - MS_EXCEPTION_IF_NULL(tensor_cache); - MS_EXCEPTION_IF_NULL(ms_graph); - MS_EXCEPTION_IF_NULL(kernel_graph_ptr); - if (convert_mode_ == kConvertUnused) { - return false; - } - if (kernel_graph_ptr->inputs().empty()) { - return false; - } - for (const auto &input_node : kernel_graph_ptr->inputs()) { - if (input_node->isa()) { - ParameterPtr pk_node = std::dynamic_pointer_cast(input_node); - TensorPtr device_tensor; - if (convert_mode_ == kConvertCpuMode) { - device_tensor = predict::utils::GetParaCpuTensor(input_node); - } else { - device_tensor = predict::utils::GetParaAscendTensor(input_node); - } - if (device_tensor == nullptr) { - return false; - } - ExTensorType node_type; - if (AnfAlgo::IsParameterWeight(pk_node)) { - node_type = WEIGHTS; - } else { - node_type = INPUTDATA; - } - if (!predict::utils::FindNodeInMap(node_indexs_, input_node)) { - MS_LOG(WARNING) << "can not find any pk_key in inited node_indexs map"; - return false; - } - auto pk_key = node_indexs_[input_node.get()]; - all_output_tensors_[pk_key].push_back(device_tensor); - int nodeRefCount = SizeToInt(AnfAlgo::GetOutputTensorNum(input_node)); - int nodeInputIdx = - tensor_cache->addExTensor(pk_key, device_tensor, nodeRefCount, device_tensor->shape(), node_type); - if (!AnfAlgo::IsParameterWeight(pk_node)) { - ms_graph->inputIndex.push_back(nodeInputIdx); - all_input_idxs_.push_back(nodeInputIdx); - } else { - input_weight_idxs_.push_back(nodeInputIdx); - all_input_idxs_.push_back(nodeInputIdx); - } - } - } - return true; -} - -bool Kernel2Ms::SetGraphValueTensors(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache) { - MS_EXCEPTION_IF_NULL(kernel_graph_ptr); - MS_EXCEPTION_IF_NULL(tensor_cache); - for (auto &item : kernel_graph_ptr->value_nodes()) { - if (item.first->isa()) { - auto const_node = item.first->cast(); - auto tensor_constant = predict::utils::GetValueTensor(const_node); - if (tensor_constant == nullptr) { - continue; - } - if (!predict::utils::FindNodeInMap(node_indexs_, item.first)) { - MS_LOG(WARNING) << "can not find any pk_key in inited node_indexs map"; - return false; - } - int constant_key = node_indexs_[(item.first).get()]; - all_output_tensors_[constant_key].push_back(tensor_constant); - auto shape = tensor_constant->shape(); - (void)tensor_cache->addExTensor(constant_key, tensor_constant, 0, shape, CONSTANT); - } - } - return true; -} - -bool Kernel2Ms::SetGraphOpTensors(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache, - SubGraphDefT *ms_graph) { - MS_EXCEPTION_IF_NULL(kernel_graph_ptr); - MS_EXCEPTION_IF_NULL(tensor_cache); - MS_EXCEPTION_IF_NULL(ms_graph); - auto kernels = kernel_graph_ptr->execution_order(); - if (kernels.empty()) { - MS_LOG(ERROR) << "this graph has no kernels"; - return false; - } - for (auto &kernel : kernels) { - if (!predict::utils::FindNodeInMap(node_indexs_, kernel)) { - MS_LOG(ERROR) << "can not find any pk_key in inited node_indexs map"; - return false; - } - auto kernel_key = node_indexs_[kernel.get()]; - std::unique_ptr ms_node(new OpDefT); - ms_node->name = kernel->fullname_with_scope(); - ms_node->fmkType = mindspore::predict::FmkType_CAFFE; - auto c_name = AnfAlgo::GetCNodeName(kernel); - auto fun = predict::convert::OpAttrFactory::GetInstance()->GetPackFun(c_name); - if (fun == nullptr) { - MS_LOG(WARNING) << "get node [" << kernel->fullname_with_scope() << "] attr failed."; - } else if (!fun(kernel, ms_node.get())) { - MS_LOG(ERROR) << "set node [" << kernel->fullname_with_scope() << "] attr failed."; - return false; - } - auto output_size = AnfAlgo::GetOutputTensorNum(kernel); - int nodeRefCount = SizeToInt(output_size); - for (size_t j = 0; j < output_size; ++j) { - TensorPtr device_tensor; - if (convert_mode_ == kConvertCpuMode) { - device_tensor = predict::utils::GetKernelCpuTensor(kernel, j); - } else if (convert_mode_ == kConvertAscendMode) { - device_tensor = predict::utils::GetKernelAscendTensor(kernel, j); - } - if (device_tensor == nullptr) { - return false; - } - all_output_tensors_[kernel_key].push_back(device_tensor); - if (!SetOpOutputIdx(kernel, device_tensor, tensor_cache, nodeRefCount, j, ms_node.get())) { - return false; - } - } - tmp_op_nodes_.emplace_back(ms_node.release()); - } - return true; -} - -bool Kernel2Ms::KernelGraph2MsGraph(const KernelGraphPtr &kernel_graph_ptr) { - MS_EXCEPTION_IF_NULL(kernel_graph_ptr); - graph_index_ = 0; - all_output_tensors_.clear(); - node_indexs_.clear(); - index_nodes_.clear(); - std::unique_ptr sub_ms_graph(new SubGraphDefT()); - if (!InitGraphIndx(kernel_graph_ptr)) { - return false; - } - TransformGraphIndx(); - tensor_cache_ptr_ = std::make_shared(); - // foreach node to init it's real output tensor - if (!SetGraphInputTensors(kernel_graph_ptr, tensor_cache_ptr_, sub_ms_graph.get())) { - return false; - } - // Get KernelGraph value node - if (!SetGraphValueTensors(kernel_graph_ptr, tensor_cache_ptr_)) { - return false; - } - // Get KernelGraph apply_kernel && add opNode - if (!SetGraphOpTensors(kernel_graph_ptr, tensor_cache_ptr_, sub_ms_graph.get())) { - return false; - } - // Get KernelGraph outputs - if (!SetGraphOutputIdx(kernel_graph_ptr, tensor_cache_ptr_, sub_ms_graph.get(), &all_output_tensors_)) { - return false; - } - auto kernels = kernel_graph_ptr->execution_order(); - for (size_t i = 0; i < kernels.size(); ++i) { - auto ms_node = tmp_op_nodes_[i]; - if (!SetOpInputIdx(kernels[i], tensor_cache_ptr_, ms_node)) { - return false; - } - std::unique_ptr ms_node_tmp(ms_node); - sub_ms_graph->nodes.emplace_back(std::move(ms_node_tmp)); - } - if (!SetAllTensors(tensor_cache_ptr_, sub_ms_graph.get())) { - return false; - } - if (!SetMemResue()) { - return false; - } - sub_ms_graph_ = std::move(sub_ms_graph); - sub_ms_graph_->name = "default_sub_graph"; - return true; -} - -bool Kernel2Ms::CheckInputSizes(const std::vector &input_tensors, - const std::vector &all_input_idxs) { - if (input_tensors.size() != all_input_idxs.size()) { - MS_LOG(EXCEPTION) << "real input tensors size:" << input_tensors.size() - << "not equal converted tesnors size:" << all_input_idxs.size() << "the graph has changed"; - } - for (auto in : all_input_idxs) { - if (in < sub_ms_graph_->allTensors.size()) { - auto real_tensor = input_tensors[in]; - auto convert_dims = sub_ms_graph_->allTensors[in]->dims; - auto real_dims = real_tensor->shape(); - if (real_dims.size() != convert_dims.size()) { - return false; - } else { - for (size_t i = 0; i < convert_dims.size(); ++i) { - if (convert_dims[i] != real_dims[i]) { - return false; - } - } - } - } else { - MS_LOG(EXCEPTION) << "index: " << in << "in all_input_idxs is valid"; - } - } - return true; -} - -void Kernel2Ms::ReleaseContextRes() { - tmp_op_nodes_.clear(); - node_indexs_.clear(); - index_nodes_.clear(); - tensor_cache_ptr_ = nullptr; - all_output_tensors_.clear(); -} - -bool Kernel2Ms::KernelInput2MS(const std::vector &input_tensors) { - const std::unordered_map> &cache_tensors = tensor_cache_ptr_->GetCachedTensor(); - if (cache_tensors.empty()) { - return false; - } - auto all_weights_idxs = GetAllInputWeightIdxs(); - auto all_input_idxs = GetAllInputIdxs(); - auto real_input_size = input_tensors.size(); - // check tensor size - bool ret = CheckInputSizes(input_tensors, all_input_idxs); - std::vector match_to_rel_idxs; - // indx order not matched,macth to it - if (!ret) { - for (auto idx : all_weights_idxs) { - auto macth_idx = real_input_size - idx; - match_to_rel_idxs.push_back(macth_idx); - } - } else { - match_to_rel_idxs = all_weights_idxs; - } - if (match_to_rel_idxs.size() == all_weights_idxs.size()) { - for (size_t j = 0; j < all_weights_idxs.size(); ++j) { - auto cache_idx = all_weights_idxs[j]; - auto match_idx = match_to_rel_idxs[j]; - auto real_tensor = input_tensors[match_idx]; - auto real_size = LongToSize(real_tensor->data().nbytes()); - auto real_data = real_tensor->data_c(); - MS_EXCEPTION_IF_NULL(real_data); - if (sub_ms_graph_->allTensors[cache_idx] != nullptr) { - sub_ms_graph_->allTensors[cache_idx]->data.resize(real_size); - } - if (memcpy_s(sub_ms_graph_->allTensors[cache_idx]->data.data(), real_size, real_data, real_size) != 0) { - MS_LOG(ERROR) << "KernelInput2MS memcpy_s failed"; - return false; - } - } - } - ReleaseContextRes(); - return true; -} - -bool Kernel2Ms::SaveDeviceModel(const std::shared_ptr &new_ms_graph_ptr, const std::string &save_path_name) { - MS_EXCEPTION_IF_NULL(new_ms_graph_ptr); - return predict::utils::SaveDeviceModelUtil(new_ms_graph_ptr, save_path_name, sub_ms_graph_.release()); -} -} // namespace executor -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/kernel2ms.h b/mindspore/ccsrc/predict/converter/kernel2ms.h deleted file mode 100644 index 073d618bb20..00000000000 --- a/mindspore/ccsrc/predict/converter/kernel2ms.h +++ /dev/null @@ -1,118 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_CCSRC_PREDICT_CONVERTER_KERNEL_TO_MS_H_ -#define MINDSPORE_CCSRC_PREDICT_CONVERTER_KERNEL_TO_MS_H_ - -#include -#include -#include -#include -#include -#include "backend/session/kernel_graph.h" -#include "predict/converter/executor_tensor.h" -#include "predict/schema/inner/ms_generated.h" -#include "predict/converter/attr_utils/convert_util.h" - -static constexpr size_t kTupleGetItemIndex = 2; -namespace mindspore { -namespace executor { -using KernelGraphPtr = std::shared_ptr; -enum ConvertMode { kConvertCpuMode, kConvertAscendMode, kConvertUnused }; -enum TargetMode { kCPUTarget, kGPUTarget, kUnknowTarget }; -class Kernel2Ms { - public: - static Kernel2Ms &GetInstance(); - - Kernel2Ms(const Kernel2Ms &) = delete; - - Kernel2Ms &operator=(const Kernel2Ms &) = delete; - - bool KernelGraph2MsGraph(const KernelGraphPtr &kernel_graph_ptr); - - bool KernelInput2MS(const std::vector &input_tensors); - - ConvertMode convert_mode() const { return convert_mode_; } - - void set_convert_mode(ConvertMode convert_mode) { convert_mode_ = convert_mode; } - - TargetMode device_target() const { return device_target_; } - - void set_device_target(TargetMode device_target) { device_target_ = device_target; } - - bool SaveDeviceModel(const std::shared_ptr &new_ms_graph_ptr, const std::string &save_path_name); - - private: - Kernel2Ms() : graph_index_(0) {} - - void ReleaseContextRes(); - - ~Kernel2Ms() = default; - - bool SetAllTensors(const TensorCachePtr &tensor_cache, SubGraphDefT *sub_graph_def_t); - - bool SetOpInputIdx(const CNodePtr &c_node_ptr, const TensorCachePtr &tensor_cache, OpDefT *ms_node); - - bool SetOpOutputIdx(const CNodePtr &c_node_ptr, const TensorPtr &output_tensor, const TensorCachePtr &tensor_cache, - int ref_count, size_t order_index, OpDefT *ms_node); - - bool SetGraphOutputIdx(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache, - SubGraphDefT *sub_graph_def_t, AllOutputTensors *all_output_tensors); - - void TransformGraphIndx(); - - void GetRealInpoutsPtr(const AnfNodePtr &node, std::vector *real_inputs, - std::vector *real_output_idx); - - bool InitGraphIndx(const KernelGraphPtr &kernel_graph_ptr); - - bool InitGraphInputsIndx(const KernelGraphPtr &kernel_graph_ptr); - - bool InitGraphValueNodesIndx(const KernelGraphPtr &kernel_graph_ptr); - - bool InitGraphOpsIndx(const KernelGraphPtr &kernel_graph_ptr); - - bool InitGraphOutputsIndx(const KernelGraphPtr &kernel_graph_ptr); - - bool SetGraphInputTensors(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache, - SubGraphDefT *sub_graph_def_t); - - bool SetGraphValueTensors(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache); - - bool SetGraphOpTensors(const KernelGraphPtr &kernel_graph_ptr, const TensorCachePtr &tensor_cache, - SubGraphDefT *sub_graph_def_t); - std::vector GetAllInputWeightIdxs() const { return input_weight_idxs_; } - std::vector GetAllInputIdxs() const { return all_input_idxs_; } - - bool CheckInputSizes(const std::vector &input_tensors, const std::vector &all_input_idxs); - - bool SetMemResue() const; - SubGraphPtr sub_ms_graph_; - AllOutputTensors all_output_tensors_; - std::vector tmp_op_nodes_; - std::unordered_map node_indexs_; - std::unordered_map index_nodes_; - int graph_index_ = 0; - TensorCachePtr tensor_cache_ptr_ = nullptr; - ConvertMode convert_mode_ = kConvertCpuMode; - TargetMode device_target_ = kCPUTarget; - std::vector input_weight_idxs_; - std::vector all_input_idxs_; -}; -using Kernel2MsPtr = std::shared_ptr; -} // namespace executor -} // namespace mindspore -#endif // MINDSPORE_CCSRC_PREDICT_CONVERTER_KERNEL_TO_MS_H_ diff --git a/mindspore/ccsrc/predict/converter/lite_model/op_attr_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/op_attr_packer.cc deleted file mode 100644 index 52648812be7..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/op_attr_packer.cc +++ /dev/null @@ -1,110 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" -#include "./securec.h" - -namespace mindspore { -namespace predict { -namespace convert { -// forward declare -bool Conv2dPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool MatMulPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool BiasAddPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool ReshapePacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool ActivationPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool PoolingPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool FusedBatchNormPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool AddPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool CastPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool MeanPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool SoftmaxPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool ScalePacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool AddFoldPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool ArgMaxPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool BatchNormFoldPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool FakeQuantWithMinMaxPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool FakeQuantWithMinMaxPerChannelPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool MulPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool MulFoldPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); -bool SqueezePacker(const CNodePtr &c_node_ptr, OpDefT *ms_op); - -OpAttrFactory::OpAttrFactory() { - pack_funs_ = {{"Conv2D", Conv2dPacker}, - {"MatMul", MatMulPacker}, - {"BiasAdd", BiasAddPacker}, - {"Reshape", ReshapePacker}, - {"Activation", ActivationPacker}, - {"ReLU", ActivationPacker}, - {"ReLU6", ActivationPacker}, - {"EReLU", ActivationPacker}, - {"LeakyReLU", ActivationPacker}, - {"Sigmoid", ActivationPacker}, - {"Softsign", ActivationPacker}, - {"Softplus", ActivationPacker}, - {"Tanh", ActivationPacker}, - {"HSwish", ActivationPacker}, - {"HSigmoid", ActivationPacker}, - {"MaxPool", PoolingPacker}, - {"MaxPool2D", PoolingPacker}, - {"MeanPool", PoolingPacker}, - {"GlobalPool", PoolingPacker}, - {"FusedBatchNorm", FusedBatchNormPacker}, - {"FusedBatchNormGrad", FusedBatchNormPacker}, - {"Cast", CastPacker}, - {"TensorAdd", AddPacker}, - {"SoftMax", SoftmaxPacker}, - {"SimpleMean", MeanPacker}, - {"ReduceMean", MeanPacker}, - {"AddFold", AddFoldPacker}, - {"ArgMax", ArgMaxPacker}, - {"BatchNorm", BatchNormFoldPacker}, - {"FakeQuantPerLayer", FakeQuantWithMinMaxPacker}, - {"FakeQuantPerChannel", FakeQuantWithMinMaxPerChannelPacker}, - {"Mul", MulPacker}, - {"MulFold", MulFoldPacker}, - {"Squeeze", SqueezePacker}}; -} -OpAttrPackFun OpAttrFactory::GetPackFun(const std::string &opType) { - if (pack_funs_.find(opType) == pack_funs_.end()) { - MS_LOG(WARNING) << "Op Attr pack fun [" << opType << "] not found."; - return nullptr; - } - return pack_funs_[opType]; -} - -mindspore::predict::Format GetAttrFormat(const std::string &format) { - if (format == kOpFormat_NCHW) { - return predict::Format::Format_NCHW; - } else if (format == kOpFormat_NHWC) { - return predict::Format::Format_NHWC; - } else { - return predict::Format::Format_NUM_OF_FORMAT; - } -} - -mindspore::predict::PadMode GetAttrPadMode(const std::string &pad_mode) { - if (pad_mode == "same") { - return mindspore::predict::PadMode::PadMode_SAME; - } else if (pad_mode == "valid") { - return mindspore::predict::PadMode::PadMode_VALID; - } else { - return mindspore::predict::PadMode::PadMode_NOTSET; - } -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/op_attr_packer.h b/mindspore/ccsrc/predict/converter/lite_model/op_attr_packer.h deleted file mode 100644 index 5cb7b1ba218..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/op_attr_packer.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_CCSRC_PREDICT_CONVERTER_OP_ATTR_PACKER_H_ -#define MINDSPORE_CCSRC_PREDICT_CONVERTER_OP_ATTR_PACKER_H_ - -#include -#include -#include -#include "backend/session/anf_runtime_algorithm.h" -#include "predict/schema/inner/ms_generated.h" - -static constexpr size_t kNIndex = 0; -static constexpr size_t kCIndex = 1; -static constexpr size_t kHIndex = 2; -static constexpr size_t kWIndex = 3; -static constexpr size_t kNCHWSize = 4; -namespace mindspore { -namespace predict { -namespace convert { -using OpAttrPackFun = bool (*)(const CNodePtr &c_node_ptr, OpDefT *ms_op); -class OpAttrFactory { - public: - static OpAttrFactory *GetInstance() { - static OpAttrFactory instance; - return &instance; - } - OpAttrFactory(const OpAttrFactory &) = delete; - OpAttrFactory &operator=(const OpAttrFactory &) = delete; - OpAttrPackFun GetPackFun(const std::string &op_type); - ~OpAttrFactory() { pack_funs_.clear(); } - OpAttrFactory(); - - private: - std::unordered_map pack_funs_; -}; - -mindspore::predict::Format GetAttrFormat(const std::string &format); - -mindspore::predict::PadMode GetAttrPadMode(const std::string &pad_mode); -} // namespace convert -} // namespace predict -} // namespace mindspore - -#endif // MINDSPORE_CCSRC_PREDICT_CONVERTER_CPU_OP_INFO_OP_ATTR_FACTORY_H_ diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/activation_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/activation_packer.cc deleted file mode 100644 index 3dc09f70b40..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/activation_packer.cc +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool ActivationPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new ActivationT()); - MS_EXCEPTION_IF_NULL(attr); - if (AnfAlgo::GetCNodeName(c_node_ptr) == "ReLU") { - attr->type = predict::ActivationType::ActivationType_RELU; - } else if (AnfAlgo::GetCNodeName(c_node_ptr) == "Sigmoid") { - attr->type = predict::ActivationType::ActivationType_SIGMOID; - } else if (AnfAlgo::GetCNodeName(c_node_ptr) == "ReLU6") { - attr->type = predict::ActivationType::ActivationType_RELU6; - } else if (AnfAlgo::GetCNodeName(c_node_ptr) == "ELU") { - attr->type = predict::ActivationType::ActivationType_ELU; - } else if (AnfAlgo::GetCNodeName(c_node_ptr) == "Leaky_ReLU") { - attr->type = predict::ActivationType::ActivationType_LEAKY_RELU; - } else if (AnfAlgo::GetCNodeName(c_node_ptr) == "ABS") { - attr->type = predict::ActivationType::ActivationType_ABS; - } else if (AnfAlgo::GetCNodeName(c_node_ptr) == "ReLU1") { - attr->type = predict::ActivationType::ActivationType_RELU1; - } else if (AnfAlgo::GetCNodeName(c_node_ptr) == "Softsign") { - attr->type = predict::ActivationType::ActivationType_SOFTSIGN; - } else if (AnfAlgo::GetCNodeName(c_node_ptr) == "Softplus") { - attr->type = predict::ActivationType::ActivationType_SOFTPLUS; - } else if (AnfAlgo::GetCNodeName(c_node_ptr) == "Tanh") { - attr->type = predict::ActivationType::ActivationType_TANH; - } else { - attr->type = predict::ActivationType::ActivationType_UNKNOW; - MS_LOG(WARNING) << "unknow Activation"; - } - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_Activation; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/add_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/add_packer.cc deleted file mode 100644 index 02a9bda65e8..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/add_packer.cc +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool AddPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new AddT()); - MS_EXCEPTION_IF_NULL(attr); - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_Add; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/addfold_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/addfold_packer.cc deleted file mode 100644 index b6affd5001d..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/addfold_packer.cc +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool AddFoldPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new AddFoldT()); - MS_EXCEPTION_IF_NULL(attr); - ms_op->attr.type = OpT_AddFold; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/argmax_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/argmax_packer.cc deleted file mode 100644 index 4df643704cd..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/argmax_packer.cc +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool ArgMaxPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new ArgMaxT()); - MS_EXCEPTION_IF_NULL(attr); - ms_op->attr.type = OpT_ArgMax; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/batchnormfold_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/batchnormfold_packer.cc deleted file mode 100644 index f05f3894be1..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/batchnormfold_packer.cc +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool BatchNormFoldPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new BatchNormFoldT()); - MS_EXCEPTION_IF_NULL(attr); - ms_op->attr.type = OpT_BatchNormFold; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/biasadd_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/biasadd_packer.cc deleted file mode 100644 index 6fe32c1f6b4..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/biasadd_packer.cc +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool BiasAddPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new BiasAddT()); - MS_EXCEPTION_IF_NULL(attr); - attr->axis = {1}; - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_BiasAdd; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/cast_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/cast_packer.cc deleted file mode 100644 index d0f3f80f6c8..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/cast_packer.cc +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool CastPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new CastT()); - MS_EXCEPTION_IF_NULL(attr); - attr->srcT = 0; - attr->dstT = 0; - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_Cast; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/conv2d_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/conv2d_packer.cc deleted file mode 100644 index 176b235f5f5..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/conv2d_packer.cc +++ /dev/null @@ -1,63 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool Conv2dPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - int kernel_group_value = AnfAlgo::GetNodeAttr(c_node_ptr, "group"); - int kernel_channel_value = AnfAlgo::GetNodeAttr(c_node_ptr, "out_channel"); - std::vector kernel_size_value = AnfAlgo::GetNodeAttr>(c_node_ptr, "kernel_size"); - std::string kernel_pad_mode_value = AnfAlgo::GetNodeAttr(c_node_ptr, "pad_mode"); - int kernel_pad_value = AnfAlgo::GetNodeAttr(c_node_ptr, "pad"); - auto kernel_stride_value = AnfAlgo::GetNodeAttr>(c_node_ptr, "stride"); - auto kernel_dilation_value = AnfAlgo::GetNodeAttr>(c_node_ptr, "dilation"); - std::string kernel_data_format_value = AnfAlgo::GetNodeAttr(c_node_ptr, "data_format"); - std::unique_ptr attr(new Conv2DT()); - MS_EXCEPTION_IF_NULL(attr); - attr->format = GetAttrFormat(kernel_data_format_value); - attr->group = kernel_group_value; - auto in_shape = AnfAlgo::GetPrevNodeOutputInferShape(c_node_ptr, 1); - if (in_shape.size() != kNCHWSize) { - return false; - } - attr->channelIn = SizeToInt(in_shape[1]); - attr->channelOut = kernel_channel_value; - attr->kernelW = kernel_size_value[0]; - attr->kernelH = kernel_size_value[1]; - attr->strideW = kernel_stride_value[0]; - attr->strideH = kernel_stride_value[1]; - attr->padMode = GetAttrPadMode(kernel_pad_mode_value); - attr->padUp = kernel_pad_value; - attr->padDown = kernel_pad_value; - attr->padLeft = kernel_pad_value; - attr->padRight = kernel_pad_value; - attr->dilateW = kernel_dilation_value[0]; - attr->dilateH = kernel_dilation_value[1]; - attr->hasBias = false; - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_Conv2D; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/fakequantwithminmax_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/fakequantwithminmax_packer.cc deleted file mode 100644 index 195a4fde9fa..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/fakequantwithminmax_packer.cc +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool FakeQuantWithMinMaxPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new FakeQuantWithMinMaxT()); - MS_EXCEPTION_IF_NULL(attr); - ms_op->attr.type = OpT_FakeQuantWithMinMax; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/fakequantwithminmaxperchannel_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/fakequantwithminmaxperchannel_packer.cc deleted file mode 100644 index 0074c876467..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/fakequantwithminmaxperchannel_packer.cc +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool FakeQuantWithMinMaxPerChannelPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new FakeQuantWithMinMaxPerChannelT()); - MS_EXCEPTION_IF_NULL(attr); - ms_op->attr.type = OpT_FakeQuantWithMinMaxPerChannel; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/fusedbatchnorm_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/fusedbatchnorm_packer.cc deleted file mode 100644 index e0092820c23..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/fusedbatchnorm_packer.cc +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool FusedBatchNormPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new FusedBatchNormT()); - MS_EXCEPTION_IF_NULL(attr); - auto kernel_epsilon = AnfAlgo::GetNodeAttr(c_node_ptr, "epsilon"); - attr->epsilon = kernel_epsilon; - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_FusedBatchNorm; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/matmul_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/matmul_packer.cc deleted file mode 100644 index a0f82810a7f..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/matmul_packer.cc +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool MatMulPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - bool kernel_transpore_a = AnfAlgo::GetNodeAttr(c_node_ptr, "transpose_a"); - bool kernel_transpore_b = AnfAlgo::GetNodeAttr(c_node_ptr, "transpose_b"); - std::unique_ptr attr(new MatMulT()); - MS_EXCEPTION_IF_NULL(attr); - attr->transposeA = kernel_transpore_a; - attr->transposeB = kernel_transpore_b; - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_MatMul; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/mean_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/mean_packer.cc deleted file mode 100644 index eac3fa88f10..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/mean_packer.cc +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool MeanPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new MeanT()); - MS_EXCEPTION_IF_NULL(attr); - attr->axis = {1}; - attr->keepDims = false; - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_Mean; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/mul_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/mul_packer.cc deleted file mode 100644 index 6c430e79e77..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/mul_packer.cc +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool MulPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new MulT()); - MS_EXCEPTION_IF_NULL(attr); - ms_op->attr.type = OpT_Mul; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/mulflod_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/mulflod_packer.cc deleted file mode 100644 index 1df7204875d..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/mulflod_packer.cc +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool MulFoldPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new MulFoldT()); - MS_EXCEPTION_IF_NULL(attr); - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_MulFold; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/pooling_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/pooling_packer.cc deleted file mode 100644 index edfdcda040e..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/pooling_packer.cc +++ /dev/null @@ -1,61 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool PoolingPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new PoolingT()); - MS_EXCEPTION_IF_NULL(attr); - std::string kernel_format_value = AnfAlgo::GetNodeAttr(c_node_ptr, "data_format"); - attr->format = GetAttrFormat(kernel_format_value); - auto c_name = AnfAlgo::GetCNodeName(c_node_ptr); - if (c_name == "MaxPool") { - ms_op->name = c_node_ptr->fullname_with_scope(); - attr->poolingMode = mindspore::predict::PoolMode::PoolMode_MAX_POOLING; - } else if (c_name == "MeanPool") { - ms_op->name = c_node_ptr->fullname_with_scope(); - attr->poolingMode = mindspore::predict::PoolMode::PoolMode_MEAN_POOLING; - } else if (c_name == "GlobalPool") { - ms_op->name = c_node_ptr->fullname_with_scope(); - } else { - MS_LOG(ERROR) << "unknowed pooling type."; - return false; - } - std::vector kernel_ksize = AnfAlgo::GetNodeAttr>(c_node_ptr, "ksize"); - attr->windowW = kernel_ksize[kHIndex]; - attr->windowH = kernel_ksize[kWIndex]; - std::vector kernel_strides = AnfAlgo::GetNodeAttr>(c_node_ptr, "strides"); - attr->strideW = kernel_strides[kHIndex]; - attr->strideH = kernel_strides[kWIndex]; - std::string kernel_pad_mode_value = AnfAlgo::GetNodeAttr(c_node_ptr, "padding"); - attr->padMode = GetAttrPadMode(kernel_pad_mode_value); - attr->padUp = 0; - attr->padDown = 0; - attr->padLeft = 0; - attr->padRight = 0; - ms_op->attr.type = OpT_Pooling; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/reshape_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/reshape_packer.cc deleted file mode 100644 index a0a263631de..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/reshape_packer.cc +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool ReshapePacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new ReshapeT()); - MS_EXCEPTION_IF_NULL(attr); - attr->format = predict::Format::Format_NCHW; - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_Reshape; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/scale_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/scale_packer.cc deleted file mode 100644 index 356775247d9..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/scale_packer.cc +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool ScalePacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new ScaleT()); - MS_EXCEPTION_IF_NULL(attr); - attr->format = predict::Format::Format_NCHW; - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_Scale; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/softmax_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/softmax_packer.cc deleted file mode 100644 index fe96bae451f..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/softmax_packer.cc +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool SoftmaxPacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new SoftMaxT()); - MS_EXCEPTION_IF_NULL(attr); - attr->axis = {1}; - ms_op->name = c_node_ptr->fullname_with_scope(); - ms_op->attr.type = OpT_SoftMax; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/converter/lite_model/operations/squeeze_packer.cc b/mindspore/ccsrc/predict/converter/lite_model/operations/squeeze_packer.cc deleted file mode 100644 index 7e836fe0215..00000000000 --- a/mindspore/ccsrc/predict/converter/lite_model/operations/squeeze_packer.cc +++ /dev/null @@ -1,38 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/converter/lite_model/op_attr_packer.h" - -namespace mindspore { -namespace predict { -namespace convert { -bool SqueezePacker(const CNodePtr &c_node_ptr, OpDefT *ms_op) { - if (c_node_ptr == nullptr || ms_op == nullptr) { - return false; - } - std::unique_ptr attr(new SqueezeT()); - MS_EXCEPTION_IF_NULL(attr); - - std::vector kernel_axis_value = AnfAlgo::GetNodeAttr>(c_node_ptr, "axis"); - attr->axis = kernel_axis_value; - - ms_op->attr.type = OpT_Squeeze; - ms_op->attr.value = attr.release(); - return true; -} -} // namespace convert -} // namespace predict -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/generator/ir/ir_model.cc b/mindspore/ccsrc/predict/generator/ir/ir_model.cc deleted file mode 100644 index ff465245771..00000000000 --- a/mindspore/ccsrc/predict/generator/ir/ir_model.cc +++ /dev/null @@ -1,31 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/generator/ir/ir_model.h" - -#include -#include - -#include "utils/log_adapter.h" - -namespace mindspore { -namespace generator { -IRModel::~IRModel() { ir_tasks_.clear(); } -void IRModel::SetIrTaskInfos(const std::vector &ir_tasks) { - (void)std::copy(ir_tasks.begin(), ir_tasks.end(), std::back_inserter(ir_tasks_)); -} -} // namespace generator -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/generator/ir/ir_model.h b/mindspore/ccsrc/predict/generator/ir/ir_model.h deleted file mode 100644 index 09e22ff78d2..00000000000 --- a/mindspore/ccsrc/predict/generator/ir/ir_model.h +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef MINDSPORE_CCSRC_EXECUTOR_GENERATOR_IR_IR_MODEL_H_ -#define MINDSPORE_CCSRC_EXECUTOR_GENERATOR_IR_IR_MODEL_H_ -#include -#include -#include -#include "predict/generator/ir/ir_task_info.h" -namespace mindspore { -namespace generator { -class IRModel { - public: - void SetIrTaskInfos(const std::vector &ir_tasks); - IRModel() = default; - ~IRModel(); - - private: - std::vector ir_tasks_; -}; -using IrModelPtr = std::shared_ptr; -} // namespace generator -} // namespace mindspore - -#endif // MINDSPORE_CCSRC_EXECUTOR_GENERATOR_IR_IR_MODEL_H_ diff --git a/mindspore/ccsrc/predict/generator/ir/ir_task_info.cc b/mindspore/ccsrc/predict/generator/ir/ir_task_info.cc deleted file mode 100644 index 1c275ea8ed4..00000000000 --- a/mindspore/ccsrc/predict/generator/ir/ir_task_info.cc +++ /dev/null @@ -1,244 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/generator/ir/ir_task_info.h" -#include "utils/log_adapter.h" - -namespace mindspore { -namespace generator { -bool CceIRTaskInfo::SerializeIRToProto() { - auto cce_task_def_ptr = std::unique_ptr(); - auto kernel_context_ptr = std::unique_ptr(); - MS_EXCEPTION_IF_NULL(cce_task_def_ptr); - MS_EXCEPTION_IF_NULL(kernel_context_ptr); - kernel_context_ptr->set_kernel_type(k_ctx_.kernel_type); - kernel_context_ptr->set_op_id(k_ctx_.op_id); - kernel_context_ptr->set_kernel_func_id(k_ctx_.kernel_func_id); - kernel_context_ptr->set_op_index(k_ctx_.op_index); - kernel_context_ptr->set_is_flowtable(k_ctx_.is_flowtable); - kernel_context_ptr->set_args_count(k_ctx_.args_count); - for (unsigned int i : k_ctx_.origin_op_index) { - kernel_context_ptr->add_origin_op_index(i); - } - void *tmp_args_offset = static_cast((k_ctx_.args_offset).data()); - if (tmp_args_offset == nullptr) { - MS_LOG(WARNING) << "tmp_args_offset have no data"; - return false; - } - kernel_context_ptr->set_args_offset(tmp_args_offset, k_ctx_.args_offset.size()); - cce_task_def_ptr->set_allocated_kernel_context(std::move(kernel_context_ptr).get()); - cce_task_def_ptr->set_stub_func(stub_func_); - cce_task_def_ptr->set_block_dim(block_dim_); - cce_task_def_ptr->set_args_size(args_size_); - void *tmp_sm_desc = static_cast(sm_desc_.data()); - if (tmp_sm_desc == nullptr) { - MS_LOG(WARNING) << "tmp_sm_desc have no data"; - return false; - } - cce_task_def_ptr->set_sm_desc(tmp_sm_desc, sm_desc_.size()); - - void *tmp_flow_table = static_cast(flow_table_.data()); - if (tmp_flow_table == nullptr) { - MS_LOG(WARNING) << "tmp_flow_table have no data"; - return false; - } - cce_task_def_ptr->set_flow_table(tmp_flow_table, flow_table_.size()); - return true; -} - -CceIRTaskInfo::~CceIRTaskInfo() { - args_.clear(); - sm_desc_.clear(); - flow_table_.clear(); -} - -bool TbeIRTaskInfo::SerializeIRToProto() { - auto tbe_task_def_ptr = std::unique_ptr(); - MS_EXCEPTION_IF_NULL(tbe_task_def_ptr); - tbe_task_def_ptr->set_stub_func(stub_func_); - tbe_task_def_ptr->set_block_dim(block_dim_); - tbe_task_def_ptr->set_args_size(args_size_); - void *tmp_args = static_cast(args_.data()); - if (tmp_args == nullptr) { - MS_LOG(WARNING) << "tmp_args have no data"; - return false; - } - tbe_task_def_ptr->set_args(tmp_args, args_.size()); - void *tmp_sm_desc = static_cast(sm_desc_.data()); - if (tmp_sm_desc == nullptr) { - MS_LOG(WARNING) << "tmp_sm_desc have no data"; - return false; - } - tbe_task_def_ptr->set_sm_desc(tmp_sm_desc, sm_desc_.size()); - void *tmp_meta_data = static_cast(meta_data_.data()); - if (tmp_meta_data == nullptr) { - MS_LOG(WARNING) << "tmp_meta_data have no data"; - return false; - } - tbe_task_def_ptr->set_meta_data(tmp_meta_data, meta_data_.size()); - for (auto &in : input_data_addrs_) { - tbe_task_def_ptr->add_input_addrs(in); - } - for (auto &ou : output_data_addrs_) { - tbe_task_def_ptr->add_output_addrs(ou); - } - for (auto &wk : workspace_addrs_) { - tbe_task_def_ptr->add_workspace_addrs(wk); - } - return true; -} - -TbeIRTaskInfo::~TbeIRTaskInfo() { - args_.clear(); - sm_desc_.clear(); - meta_data_.clear(); - input_data_addrs_.clear(); - output_data_addrs_.clear(); - workspace_addrs_.clear(); -} - -bool AicpuIRTaskInfo::SerializeIRToProto() { - auto aicpu_task_def_ptr = std::unique_ptr(); - MS_EXCEPTION_IF_NULL(aicpu_task_def_ptr); - aicpu_task_def_ptr->set_op_type(op_type_); - aicpu_task_def_ptr->set_flag(flag_); - for (auto &shape : input_data_shapes_) { - auto in_shape_ptr = aicpu_task_def_ptr->add_input_shapes(); - for (auto &in_sh : shape) { - in_shape_ptr->add_shape(static_cast(in_sh)); - } - } - for (auto &shape : output_data_shapes_) { - auto ou_shape_ptr = aicpu_task_def_ptr->add_output_shapes(); - for (auto &ou_sh : shape) { - ou_shape_ptr->add_shape(static_cast(ou_sh)); - } - } - for (auto &in_type : input_data_types_) { - aicpu_task_def_ptr->add_input_types(in_type); - } - for (auto &ou_type : output_data_types_) { - aicpu_task_def_ptr->add_output_types(ou_type); - } - for (auto &in_addr : input_data_addrs_) { - aicpu_task_def_ptr->add_input_addrs(in_addr); - } - for (auto &ou_addr : output_data_addrs_) { - aicpu_task_def_ptr->add_output_addrs(ou_addr); - } - void *tmp_node_def = static_cast(node_def_.data()); - if (tmp_node_def == nullptr) { - MS_LOG(WARNING) << "tmp_node_def have no data"; - return false; - } - aicpu_task_def_ptr->set_node_def(tmp_node_def, node_def_.size()); - void *tmp_func_def = static_cast(func_def_.data()); - if (tmp_func_def == nullptr) { - MS_LOG(WARNING) << "tmp_func_def have no data"; - return false; - } - aicpu_task_def_ptr->set_func_def(tmp_func_def, func_def_.size()); - return true; -} - -AicpuIRTaskInfo::~AicpuIRTaskInfo() { - input_data_types_.clear(); - input_data_shapes_.clear(); - input_data_addrs_.clear(); - output_data_types_.clear(); - output_data_shapes_.clear(); - output_data_addrs_.clear(); - node_def_.clear(); - func_def_.clear(); -} - -bool LabelIRTaskInfo::SerializeIRToProto() { - auto label_task_def_ptr = std::unique_ptr(); - MS_EXCEPTION_IF_NULL(label_task_def_ptr); - label_task_def_ptr->set_label_id(label_id_); - return true; -} - -bool EventIRTaskInfo::SerializeIRToProto() { - auto event_task_def_ptr = std::unique_ptr(); - MS_EXCEPTION_IF_NULL(event_task_def_ptr); - event_task_def_ptr->set_event_id(event_id_); - return true; -} - -bool HcclIRTaskInfo::SerializeIRToProto() { - auto hccl_task_def_ptr = std::unique_ptr(); - MS_EXCEPTION_IF_NULL(hccl_task_def_ptr); - hccl_task_def_ptr->set_hccl_type(hccl_type_); - hccl_task_def_ptr->set_input_addr(input_data_addr_); - hccl_task_def_ptr->set_output_addr(output_data_addr_); - auto tmp_wk = static_cast(workspace_.data()); - hccl_task_def_ptr->set_workspace(tmp_wk, workspace_.size()); - hccl_task_def_ptr->set_workspace_num(workspace_num_); - auto tmp_pri_def = static_cast(private_def_.data()); - hccl_task_def_ptr->set_private_def(tmp_pri_def, private_def_.size()); - hccl_task_def_ptr->set_ops_kernel_store(ops_kernel_store_); - hccl_task_def_ptr->set_count(count_); - hccl_task_def_ptr->set_root_id(root_id_); - hccl_task_def_ptr->set_op_type(op_type_); - hccl_task_def_ptr->set_data_type(data_type_); - return true; -} - -HcclIRTaskInfo::~HcclIRTaskInfo() { - workspace_.clear(); - private_def_.clear(); -} - -bool ProfilerIRTaskInfo::SerializeIRToProto() { - auto profiler_task_def_ptr = std::unique_ptr(); - MS_EXCEPTION_IF_NULL(profiler_task_def_ptr); - profiler_task_def_ptr->set_log_id(log_id_); - profiler_task_def_ptr->set_flat(flat_); - profiler_task_def_ptr->set_notify(notify_); - return true; -} - -bool MemcpyAsyncIRTaskInfo::SerializeIRToProto() { - auto mem_task_def_ptr = std::unique_ptr(); - MS_EXCEPTION_IF_NULL(mem_task_def_ptr); - mem_task_def_ptr->set_dst(dst_); - mem_task_def_ptr->set_dst_max(dst_max_); - mem_task_def_ptr->set_src(src_); - mem_task_def_ptr->set_count(count_); - mem_task_def_ptr->set_kind(kind_); - return true; -} - -bool StreamSwitchIRTaskInfo::SerializeIRToProto() { - auto stream_switch_task_def_ptr = std::unique_ptr(); - MS_EXCEPTION_IF_NULL(stream_switch_task_def_ptr); - stream_switch_task_def_ptr->set_true_stream_id(true_stream_id_); - stream_switch_task_def_ptr->set_input_addr(input_addr_); - stream_switch_task_def_ptr->set_value_addr(value_addr_); - stream_switch_task_def_ptr->set_cond(cond_); - stream_switch_task_def_ptr->set_data_type(data_type_); - return true; -} - -bool StreamActiveIRTaskInfo::SerializeIRToProto() { - auto stream_active_task_def_ptr = std::unique_ptr(); - MS_EXCEPTION_IF_NULL(stream_active_task_def_ptr); - stream_active_task_def_ptr->set_active_stream_id(active_stream_id_); - return true; -} -} // namespace generator -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/generator/ir/ir_task_info.h b/mindspore/ccsrc/predict/generator/ir/ir_task_info.h deleted file mode 100644 index ad0df0419a8..00000000000 --- a/mindspore/ccsrc/predict/generator/ir/ir_task_info.h +++ /dev/null @@ -1,295 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_CCSRC_EXECUTOR_GENERATOR_IR_IR_TASK_H_ -#define MINDSPORE_CCSRC_EXECUTOR_GENERATOR_IR_IR_TASK_H_ -#include -#include -#include -#include -#include -#include "proto/ge_runtime_taskinfo.pb.h" - -namespace mindspore { -namespace generator { -using TaskType = ::ge::model_runner::TaskDef_TaskType; -enum TaskTmpType { - CCE_TMP_DEF = 0, - TBE_TMP_DEF = 1, - AICPU_TMP_DEF = 2, - LABEL_TMP_DEF = 3, - EVENT_TMP_DEF = 4, - HCCL_TMP_DEF = 5, - PROFILER_TRACE_TMP_DEF = 6, - MEMCPY_ASYNC_TMP_DEF = 7, - STREAM_SWITCH_TMP_DEF = 8, - STREAM_ACTIVE_TMP_DEF = 9 -}; - -struct KernelContext { - uint32_t kernel_type = 0; - uint32_t op_id = 0; - uint32_t kernel_func_id = 0; - uint32_t op_index = 0; - bool is_flowtable = false; - std::vector args_offset; - uint32_t args_count = 0; - std::vector origin_op_index; -}; - -class IRtaskInfo { - public: - virtual ~IRtaskInfo() = default; - virtual bool SerializeIRToProto() = 0; - - protected: - IRtaskInfo(TaskType task_type, TaskTmpType task_tmp_type, uint64_t stream_id) - : task_type_(task_type), task_tmp_type_(task_tmp_type), stream_id_(stream_id) {} - - public: - uint64_t GetStreamId() const { return stream_id_; } - TaskType GetTaskType() const { return task_type_; } - TaskTmpType GetTaskTmpType() const { return task_tmp_type_; } - - private: - TaskType task_type_; - TaskTmpType task_tmp_type_; - uint64_t stream_id_ = 0; -}; - -using IRtaskInfoPtr = std::shared_ptr; - -class CceIRTaskInfo : public IRtaskInfo { - public: - CceIRTaskInfo(TaskType task_type, uint64_t stream_id, KernelContext k_ctx, std::string stub_func, uint32_t block_dim, - std::vector args, uint32_t args_size, std::vector sm_desc, - std::vector flow_table) - : IRtaskInfo(task_type, CCE_TMP_DEF, stream_id), - k_ctx_(std::move(k_ctx)), - stub_func_(std::move(stub_func)), - block_dim_(block_dim), - args_(std::move(args)), - args_size_(args_size), - sm_desc_(std::move(sm_desc)), - flow_table_(std::move(flow_table)) {} - ~CceIRTaskInfo() override; - bool SerializeIRToProto() override; - - private: - KernelContext k_ctx_; - std::string stub_func_; - uint32_t block_dim_ = 0; - std::vector args_; - // uintptr_t args_addr_; - uint32_t args_size_ = 0; - std::vector sm_desc_; - std::vector flow_table_; -}; - -class TbeIRTaskInfo : public IRtaskInfo { - public: - TbeIRTaskInfo(TaskType task_type, uint64_t stream_id, std::string stub_func, uint32_t block_dim, - std::vector args, uint32_t args_size, std::vector sm_desc, - std::vector meta_data, std::vector input_data_addrs, - std::vector output_data_addrs, std::vector workspace_addrs) - : IRtaskInfo(task_type, TBE_TMP_DEF, stream_id), - stub_func_(std::move(stub_func)), - block_dim_(block_dim), - args_(std::move(args)), - args_size_(args_size), - sm_desc_(std::move(sm_desc)), - meta_data_(std::move(meta_data)), - input_data_addrs_(std::move(input_data_addrs)), - output_data_addrs_(std::move(output_data_addrs)), - workspace_addrs_(std::move(workspace_addrs)) {} - ~TbeIRTaskInfo() override; - bool SerializeIRToProto() override; - - private: - std::string stub_func_; - uint32_t block_dim_ = 0; - std::vector args_; - uint32_t args_size_ = 0; - std::vector sm_desc_; - // uintptr_t binary_; - // uint32_t binary_size_; - std::vector meta_data_; - std::vector input_data_addrs_; - std::vector output_data_addrs_; - std::vector workspace_addrs_; - // std::vector flow_table_; -}; - -class AicpuIRTaskInfo : public IRtaskInfo { - public: - AicpuIRTaskInfo(TaskType task_type, uint64_t stream_id, std::string op_type, uint32_t flag, - std::vector input_data_types, std::vector> input_data_shapes, - std::vector input_data_addrs, std::vector output_data_types, - std::vector> output_data_shapes, std::vector output_data_addrs, - std::vector node_def, std::vector func_def) - : IRtaskInfo(task_type, AICPU_TMP_DEF, stream_id), - op_type_(std::move(op_type)), - flag_(flag), - input_data_types_(std::move(input_data_types)), - input_data_shapes_(std::move(input_data_shapes)), - input_data_addrs_(std::move(input_data_addrs)), - output_data_types_(std::move(output_data_types)), - output_data_shapes_(std::move(output_data_shapes)), - output_data_addrs_(std::move(output_data_addrs)), - node_def_(std::move(node_def)), - func_def_(std::move(func_def)) {} - ~AicpuIRTaskInfo() override; - bool SerializeIRToProto() override; - - private: - std::string op_type_; - uint32_t flag_ = 0; - std::vector input_data_types_; - std::vector> input_data_shapes_; - std::vector input_data_addrs_; - std::vector output_data_types_; - std::vector> output_data_shapes_; - std::vector output_data_addrs_; - std::vector node_def_; - std::vector func_def_; -}; - -class LabelIRTaskInfo : public IRtaskInfo { - public: - LabelIRTaskInfo(TaskType task_type, uint64_t stream_id, uint32_t label_id) - : IRtaskInfo(task_type, LABEL_TMP_DEF, stream_id), label_id_(label_id) {} - ~LabelIRTaskInfo() override {} - bool SerializeIRToProto() override; - - private: - uint32_t label_id_ = 0; -}; - -class EventIRTaskInfo : public IRtaskInfo { - public: - EventIRTaskInfo(TaskType task_type, uint64_t stream_id, uint32_t event_id) - : IRtaskInfo(task_type, EVENT_TMP_DEF, stream_id), event_id_(event_id) {} - ~EventIRTaskInfo() override {} - bool SerializeIRToProto() override; - - private: - uint32_t event_id_ = 0; -}; - -class HcclIRTaskInfo : public IRtaskInfo { - public: - HcclIRTaskInfo(TaskType task_type, uint64_t stream_id, std::string hccl_type, uintptr_t input_data_addr, - uintptr_t output_data_addr, std::vector workspace, int64_t workspace_num, - std::vector private_def, uintptr_t ops_kernel_store, int32_t count, int64_t root_id, - int64_t op_type, int64_t data_type) - : IRtaskInfo(task_type, HCCL_TMP_DEF, stream_id), - hccl_type_(std::move(hccl_type)), - input_data_addr_(input_data_addr), - output_data_addr_(output_data_addr), - workspace_(std::move(workspace)), - workspace_num_(workspace_num), - private_def_(std::move(private_def)), - ops_kernel_store_(ops_kernel_store), - count_(count), - root_id_(root_id), - op_type_(op_type), - data_type_(data_type) {} - ~HcclIRTaskInfo() override; - bool SerializeIRToProto() override; - - private: - std::string hccl_type_; - uintptr_t input_data_addr_ = 0; - uintptr_t output_data_addr_ = 0; - std::vector workspace_; - int64_t workspace_num_ = 0; - std::vector private_def_; - uintptr_t ops_kernel_store_ = 0; - int32_t count_ = 0; - int64_t root_id_ = 0; - int64_t op_type_ = 0; - int64_t data_type_ = 0; -}; - -class ProfilerIRTaskInfo : public IRtaskInfo { - public: - ProfilerIRTaskInfo(TaskType task_type, uint64_t stream_id, uint64_t log_id, bool notify, uint32_t flat) - : IRtaskInfo(task_type, PROFILER_TRACE_TMP_DEF, stream_id), log_id_(log_id), notify_(notify), flat_(flat) {} - ~ProfilerIRTaskInfo() override {} - bool SerializeIRToProto() override; - - private: - uint64_t log_id_ = 0; - bool notify_ = false; - uint32_t flat_ = 0; -}; - -class MemcpyAsyncIRTaskInfo : public IRtaskInfo { - public: - MemcpyAsyncIRTaskInfo(TaskType task_type, uint32_t stream_id, uint64_t dst, uint64_t dst_max, uint64_t src, - uint64_t count, int64_t kind) - : IRtaskInfo(task_type, MEMCPY_ASYNC_TMP_DEF, stream_id), - dst_(dst), - dst_max_(dst_max), - src_(src), - count_(count), - kind_(kind) {} - ~MemcpyAsyncIRTaskInfo() override {} - bool SerializeIRToProto() override; - - private: - uint64_t dst_ = 0; - uint64_t dst_max_ = 0; - uint64_t src_ = 0; - uint64_t count_ = 0; - uint32_t kind_ = 0; -}; - -class StreamSwitchIRTaskInfo : public IRtaskInfo { - public: - StreamSwitchIRTaskInfo(TaskType task_type, uint64_t stream_id, uint32_t true_stream_id, uintptr_t input_addr, - uintptr_t value_addr, uint32_t cond, int64_t data_type) - : IRtaskInfo(task_type, STREAM_SWITCH_TMP_DEF, stream_id), - true_stream_id_(true_stream_id), - input_addr_(input_addr), - value_addr_(value_addr), - cond_(cond), - data_type_(data_type) {} - ~StreamSwitchIRTaskInfo() override {} - bool SerializeIRToProto() override; - - private: - uint32_t true_stream_id_ = 0; - uintptr_t input_addr_ = 0; - uintptr_t value_addr_ = 0; - uint32_t cond_ = 0; - int64_t data_type_ = 0; -}; - -class StreamActiveIRTaskInfo : public IRtaskInfo { - public: - StreamActiveIRTaskInfo(TaskType task_type, uint64_t stream_id, uint32_t active_stream_id) - : IRtaskInfo(task_type, STREAM_ACTIVE_TMP_DEF, stream_id), active_stream_id_(active_stream_id) {} - ~StreamActiveIRTaskInfo() override {} - bool SerializeIRToProto() override; - - private: - uint32_t active_stream_id_ = 0; -}; -}; // namespace generator -} // namespace mindspore - -#endif // MINDSPORE_CCSRC_EXECUTOR_GENERATOR_IR_IR_TASK_H_ diff --git a/mindspore/ccsrc/predict/generator/utils/ir_model_util.cc b/mindspore/ccsrc/predict/generator/utils/ir_model_util.cc deleted file mode 100644 index 8128009472f..00000000000 --- a/mindspore/ccsrc/predict/generator/utils/ir_model_util.cc +++ /dev/null @@ -1,43 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/generator/utils/ir_model_util.h" -namespace mindspore { -namespace generator { -IRModelUtil &IRModelUtil::GetInstance() { - static IRModelUtil instance; - return instance; -} - -void IRModelUtil::Init() { - MS_LOG(INFO) << "IRModel init success"; - version_ = "defaultVersion"; - stream_num_ = 0; - event_num_ = 0; - batch_num_ = 0; - memory_size_ = 0; - weight_size_ = 0; - var_size_ = 0; - logic_mem_base_ = 0; - logic_var_base_ = 0; - logic_var_base_ = 0; - priority_ = 0; - is_enable_save_model_ = false; - min_static_offset_ = 0; - max_dynamic_offset_ = 0; -} -} // namespace generator -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/generator/utils/ir_model_util.h b/mindspore/ccsrc/predict/generator/utils/ir_model_util.h deleted file mode 100644 index 1de50725f27..00000000000 --- a/mindspore/ccsrc/predict/generator/utils/ir_model_util.h +++ /dev/null @@ -1,92 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_CCSRC_PREDICT_GENERATOR_IR_IR_MODEL_UTIL_H_ -#define MINDSPORE_CCSRC_PREDICT_GENERATOR_IR_IR_MODEL_UTIL_H_ -#include -#include -#include -#include -#include -#include "utils/log_adapter.h" - -namespace mindspore { -namespace generator { -class IRModelUtil { - public: - static IRModelUtil &GetInstance(); - IRModelUtil(const IRModelUtil &) = delete; - IRModelUtil &operator=(const IRModelUtil &) = delete; - void Init(); - - void set_version(const std::string &version) { version_ = version; } - void set_stream_num(uint32_t stream_num) { stream_num_ = stream_num; } - void set_event_num(uint32_t event_num) { event_num_ = event_num; } - void set_batch_num(uint32_t batch_num) { batch_num_ = batch_num; } - void set_memory_size(uint32_t memory_size) { memory_size_ = memory_size; } - void set_weight_size(uint32_t weight_size) { weight_size_ = weight_size; } - void set_var_size(uint32_t var_size) { var_size_ = var_size; } - void set_logic_mem_base(uint32_t logic_mem_base) { logic_mem_base_ = logic_mem_base; } - void set_logic_weight_base(uint32_t logic_weight_base) { logic_weight_base_ = logic_weight_base; } - void set_logic_var_base(uint32_t logic_var_base) { logic_var_base_ = logic_var_base; } - void set_priority(uint32_t priority) { priority_ = priority; } - void set_is_enable_save_model(bool is_enable_save_model) { is_enable_save_model_ = is_enable_save_model; } - void set_min_static_offset(uint64_t min_static_offset) { min_static_offset_ = min_static_offset; } - void set_max_dynamic_offset(uint64_t max_dynamic_offset) { max_dynamic_offset_ = max_dynamic_offset; } - void set_max_mem_size(uint64_t max_mem_size) { max_mem_size_ = max_mem_size; } - void set_irmodel_mem_base(uint8_t irmodel_mem_base) { irmodel_mem_base_ = irmodel_mem_base; } - - std::string version() const { return version_; } - uint32_t stream_num() const { return stream_num_; } - uint32_t event_num() const { return event_num_; } - uint32_t batch_num() const { return batch_num_; } - uint64_t memory_size() const { return memory_size_; } - uint64_t weight_size() const { return weight_size_; } - uint64_t var_size() const { return var_size_; } - uint64_t logic_mem_base() const { return logic_mem_base_; } - uint64_t logic_weight_base() const { return logic_weight_base_; } - uint64_t logic_var_base() const { return logic_var_base_; } - uint32_t priority() const { return priority_; } - bool is_enable_save_model() const { return is_enable_save_model_; } - uint64_t min_static_offset() const { return min_static_offset_; } - uint64_t max_dynamic_offset() const { return max_dynamic_offset_; } - uint64_t max_mem_size() const { return max_mem_size_; } - uint8_t irmodel_mem_base() const { return irmodel_mem_base_; } - - private: - IRModelUtil() = default; - ~IRModelUtil() = default; - std::string version_; - uint32_t stream_num_ = 0; - uint32_t event_num_ = 0; - uint32_t batch_num_ = 0; - uint64_t memory_size_ = 0; - uint64_t weight_size_ = 0; - uint64_t var_size_ = 0; - uint64_t logic_mem_base_ = 0; - uint64_t logic_weight_base_ = 0; - uint64_t logic_var_base_ = 0; - uint32_t priority_ = 0; - bool is_enable_save_model_ = false; - uint64_t min_static_offset_ = 0; - uint64_t max_dynamic_offset_ = 0; - uint64_t max_mem_size_ = 0; - uint8_t irmodel_mem_base_ = 0; -}; -} // namespace generator -} // namespace mindspore - -#endif // MINDSPORE_CCSRC_PREDICT_GENERATOR_IR_IR_MODEL_UTIL_H_ diff --git a/mindspore/ccsrc/predict/predict.cc b/mindspore/ccsrc/predict/predict.cc deleted file mode 100644 index bbb12c3787c..00000000000 --- a/mindspore/ccsrc/predict/predict.cc +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright 2020 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "predict/predict.h" - -#include -#include -#include - -namespace mindspore { -namespace predictmodel { -void StepConvertGraph(const KernelGraphPtr &kernel_graph_ptr) { - MS_LOG(INFO) << "start convert_graph step"; - // get kernel_graph. this graph can be origin or device, depends on which steps to persistence - MS_EXCEPTION_IF_NULL(kernel_graph_ptr); - bool save_ms_model = MsContext::GetInstance()->save_ms_model_flag(); - if (save_ms_model) { - if (kernel_graph_ptr->inputs().empty()) { - return; - } - // set convert_mode: convert cpu info or convert Davnici - executor::Kernel2Ms::GetInstance().set_convert_mode(executor::kConvertCpuMode); - // convert kernel_graph to sub_ms_graph - bool ret = executor::Kernel2Ms::GetInstance().KernelGraph2MsGraph(kernel_graph_ptr); - if (!ret) { - MS_LOG(WARNING) << "convert to mindsporeGraph failed"; - } else { - MS_LOG(INFO) << "convert to Graph success"; - } - } -} - -void StepConvertWeight(const std::vector &inputs) { - MS_LOG(INFO) << "start convert_input step"; - // get all inputs tensor - bool save_ms_model = MsContext::GetInstance()->save_ms_model_flag(); - std::string save_path = MsContext::GetInstance()->save_ms_model_path(); - if (save_ms_model) { - if (inputs.empty()) { - return; - } - MS_LOG(INFO) << "save ms model is true to path " << save_path; - if (!executor::Kernel2Ms::GetInstance().KernelInput2MS(inputs)) { - MS_LOG(WARNING) << "convert mindspore kernel input failed"; - } - auto new_ms_graph_ptr = std::make_shared(); - bool ret = executor::Kernel2Ms::GetInstance().SaveDeviceModel(new_ms_graph_ptr, save_path); - if (!ret) { - MS_LOG(WARNING) << "convert to mindsporeGraph failed"; - } else { - MS_LOG(INFO) << "save ms model success"; - } - } -} -} // namespace predictmodel -} // namespace mindspore diff --git a/mindspore/ccsrc/predict/predict.h b/mindspore/ccsrc/predict/predict.h deleted file mode 100644 index 46429fae1b2..00000000000 --- a/mindspore/ccsrc/predict/predict.h +++ /dev/null @@ -1,32 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef MINDSPORE_CCSRC_PREDICT_H_ -#define MINDSPORE_CCSRC_PREDICT_H_ - -#include -#include -#include "backend/session/session_basic.h" -#include "predict/converter/kernel2ms.h" - -namespace mindspore { -namespace predictmodel { -using KernelGraphPtr = std::shared_ptr; -void StepConvertGraph(const KernelGraphPtr &kernel_graph_ptr); -void StepConvertWeight(const std::vector &inputs); -} // namespace predictmodel -} // namespace mindspore -#endif // MINDSPORE_CCSRC_PREDICT_H_ diff --git a/mindspore/ccsrc/predict/proto/DModel_ir.proto b/mindspore/ccsrc/predict/proto/DModel_ir.proto deleted file mode 100644 index 02bfa94df3d..00000000000 --- a/mindspore/ccsrc/predict/proto/DModel_ir.proto +++ /dev/null @@ -1,42 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -syntax = "proto3"; -import public "Graph_ir.proto"; -import public "ge_runtime_taskinfo.proto"; -package ge.model_runner; -option cc_enable_arenas = true; - -message ModelTaskDef { - - string version = 1; - - repeated TaskDef task = 10; - - uint32 stream_num = 11; - uint32 event_num = 12; - uint32 batch_num_ = 13; - - uint64 memory_size = 14; - uint64 weight_size = 15; - uint64 var_size_ = 16; - - uint64 logic_mem_base_ = 17; - uint64 logic_weight_base_ = 18; - uint64 logic_var_base_ = 19; - - uint32 priority_ = 20; -} diff --git a/mindspore/ccsrc/predict/proto/Graph_ir.proto b/mindspore/ccsrc/predict/proto/Graph_ir.proto deleted file mode 100644 index af91ec0917a..00000000000 --- a/mindspore/ccsrc/predict/proto/Graph_ir.proto +++ /dev/null @@ -1,125 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -syntax = "proto3"; - -package mindspore; - -// Data type definition -enum DataType { - DT_UNDEFINED = 0; - // Basic types. - DT_BOOL = 1; // bool - - DT_INT8 = 2; // int8_t - DT_INT16 = 3; // int16_t - DT_INT32 = 4; // int32_t - DT_INT64 = 5; // int64_t - - DT_UINT8 = 6; // uint8_t - DT_UINT16 = 7; // uint16_t - DT_UINT32 = 8; // uint32_t - DT_UINT64 = 9; // uint64_t - - DT_FLOAT16 = 10; // float 16 - DT_FLOAT32 = 11; // float 32 - DT_FLOAT64 = 12; // float 64 - - DT_STRING = 13; // string - DT_TENSOR = 14; // tensor - DT_GRAPH = 15; // graph - - // list type - DT_BOOLS = 16; // list of bool - - DT_INTS8 = 17; // list of int8_t - DT_INTS16 = 18; // list of int16_t - DT_INTS32 = 19; // list of int32_t - DT_INTS64 = 20; // list of int64_t - - DT_UINTS8 = 21; // list of uint8_t - DT_UINTS16 = 22; // list of uint16_t - DT_UINTS32 = 23; // list of uint32_t - DT_UINTS64 = 24; // list of uint64_t - - DT_FLOATS16 = 25; // list of float16 - DT_FLOATS32 = 26; // list of float32 - DT_FLOATS64 = 27; // list of float64 - - DT_STRINGS = 28; // list of string - DT_TENSORS = 29; // list of tensor - DT_GRAPHS = 30; // list of graph - - DT_TUPLE = 31; // tuple - DT_LIST = 32; // list - DT_DICT = 33; // dictionary - - // other types - DT_NONE = 34; // None - DT_SYM_INST = 35; // Symbolic Key Instance - - // type related type - DT_BASE_INT = 36; // type generic int - DT_BASE_UINT = 37; // type generate unsigned int - DT_BASE_FLOAT = 38; // type generate float - DT_TYPE = 39; // type type - DT_ANYTHING = 40; // type anything -}; - -enum MSConst { - DEFAULT_REFCOUNT = 0; - WEIGHT_REFCOUNT = 999; -}; - -message TensorDef { - DataType data_type = 1; - - repeated int64 dims = 2; - - string format = 3; - string layout = 4; - uint32 refCount = 5; - uint64 offset = 6; - uint64 size = 7; - uint64 weight_size = 8; - bytes data = 9; -} - -message OpDef { - string name = 1; - string type = 2; - - string fwk_type = 3; - string opAttr = 4; - repeated int64 input_index = 5; - repeated int64 output_index = 6; -} - -message GraphDef { - string name = 1; - - repeated int64 input_index = 2; - - repeated int64 output_index = 3; - uint64 mempool_size = 4; - - repeated OpDef opdefs = 5; - - repeated TensorDef alltensors = 6; -} - - - diff --git a/mindspore/ccsrc/predict/proto/ge_runtime_taskinfo.proto b/mindspore/ccsrc/predict/proto/ge_runtime_taskinfo.proto deleted file mode 100644 index 3429d065443..00000000000 --- a/mindspore/ccsrc/predict/proto/ge_runtime_taskinfo.proto +++ /dev/null @@ -1,155 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -syntax = "proto3"; - -package ge.model_runner; -option cc_enable_arenas = true; - -message TaskDef { - enum TaskType { - CCE = 0; - TBE = 1; - AICPU = 2; - LABEL_SET = 3; - LABEL_SWITCH = 4; - LABEL_GOTO = 5; - EVENT_RECORD = 6; - EVENT_WAIT = 7; - FUSION_START = 8; - FUSION_END = 9; - HCCL = 10; - PROFILER_TRACE = 11; - MEMCPY_ASYNC = 12; - STREAM_SWITCH = 13; - STREAM_ACTIVE = 14; - // insert new task type here - REVSERVED = 23; - }; - - TaskType task_type = 1; - uint64 stream_id = 2; - oneof subclass { - CceTaskDef cce_task_def = 3; - TbeTaskDef tbe_task_def = 4; - AicpuTaskDef aicpu_task_def = 5; - LabelTaskDef label_task_def = 6; - EventTaskDef event_task_def = 7; - HcclTaskDef hccl_task_def = 8; - ProfilerTaskDef profiler_task_def = 9; - MemcpyAsyncTaskDef memcpy_async_task_def = 10; - StreamSwitchTaskDef stream_switch_task_def = 11; - StreamActiveTaskDef stream_active_task_def = 12; - } -} - -message CceTaskDef { - KernelContext kernel_context = 1; - string stub_func = 2; - uint32 block_dim = 3; - bytes args = 4; - uint32 args_size = 5; - bytes sm_desc = 6; - bytes flow_table = 7; -} - -message TbeTaskDef { - string stub_func = 1; - uint32 block_dim = 2; - bytes args = 3; - uint32 args_size = 4; - bytes sm_desc = 5; - bytes meta_data = 8; - repeated uint64 input_addrs = 9; - repeated uint64 output_addrs = 10; - repeated uint64 workspace_addrs = 11; -} - -message AicpuTaskDef { - string op_type = 1; - uint32 flag = 2; - repeated uint32 input_types = 3; - repeated Shape input_shapes = 4; - repeated uint64 input_addrs = 5; - repeated uint32 output_types = 6; - repeated Shape output_shapes = 7; - repeated uint64 output_addrs = 8; - bytes node_def = 9; - bytes func_def = 10; -} - -message Shape { - repeated uint32 shape = 1; -} - -message LabelTaskDef { - uint32 label_id = 1; -} - -message EventTaskDef { - uint32 event_id = 1; -} - -message HcclTaskDef { - string hccl_type = 1; - uint64 input_addr = 2; - uint64 output_addr = 3; - bytes workspace = 4; - int64 workspace_num = 5; - bytes private_def = 6; - uint64 ops_kernel_store = 7; - int32 count = 8; - int64 root_id = 9; - int64 op_type = 10; - int64 data_type = 11; -} - -message ProfilerTaskDef { - uint64 log_id = 1; - bool notify = 2; - uint32 flat = 3; -} - -message MemcpyAsyncTaskDef { - uint64 dst = 1; - uint64 dst_max = 2; - uint64 src = 3; - uint64 count = 4; - uint32 kind = 5; -} - -message StreamSwitchTaskDef { - uint32 true_stream_id = 1; - uint64 input_addr = 2; - uint64 value_addr = 3; - int64 cond = 4; - int64 data_type = 5; -} - -message StreamActiveTaskDef { - uint32 active_stream_id = 1; -} - -message KernelContext { - uint32 kernel_type = 1; - uint32 op_id = 2; - uint32 kernel_func_id = 3; - uint32 op_index = 4; - bool is_flowtable = 5; - bytes args_offset = 6; - uint32 args_count = 7; - repeated uint32 origin_op_index = 8; -} diff --git a/mindspore/ccsrc/predict/readme.txt b/mindspore/ccsrc/predict/readme.txt deleted file mode 100644 index d75abf257be..00000000000 --- a/mindspore/ccsrc/predict/readme.txt +++ /dev/null @@ -1,17 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -this is a dictory for predict including saving model &&& saving taskinfos. diff --git a/mindspore/ccsrc/predict/schema/inner/readme.txt b/mindspore/ccsrc/predict/schema/inner/readme.txt deleted file mode 100644 index 774f71f602c..00000000000 --- a/mindspore/ccsrc/predict/schema/inner/readme.txt +++ /dev/null @@ -1 +0,0 @@ -this is a dictory for predict to gen fbs headers \ No newline at end of file diff --git a/mindspore/ccsrc/predict/schema/ms.fbs b/mindspore/ccsrc/predict/schema/ms.fbs deleted file mode 100644 index 7c3dcfb4983..00000000000 --- a/mindspore/ccsrc/predict/schema/ms.fbs +++ /dev/null @@ -1,212 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -include "op.fbs"; - -namespace mindspore.predict; - -enum MSCONST: int { - WEIGHT_REFCOUNT = 999 -} - -table QuantParam { - scale: double; - zeroPoint: int; - min: double = 0; - max: double = 0; - narrowRange: bool = true; - numBits: int = 8; -} - -table QuantParamArray { - param: [QuantParam]; //pre-channel -} - -table TensorDef { - // data type - dataType: DataType; - // shape - dims: [int]; - format: Format; - refCount: int; - offset: int; - data: [ubyte]; -} - -union OpT { - Concat, - SoftMax, - Activation, - Conv2D, - FusedBatchNorm, - CaffeBatchNorm, - BiasAdd, - Pooling, - DepthwiseConv2D, - DeDepthwiseConv2D, - Resize, - DetectionPostProcess, - FullConnection, - Mean, - DeConv2D, - Scale, - Reshape, - Eltwise, - NetOutput, - Add, - Sub, - MatMul, - StridedSlice, - Power, - Slice, - Stack, - Mul, - RealDiv, - Pad, - Maximum, - Minimum, - CaffePReLU, - LeakyReLU, - ArgMax, - ArgMin, - Exp, - CaffeCrop, - Range, - Rsqrt, - ExpandDims, - Tile, - Cast, - Shape, - Nchw2Nhwc, - Nhwc2Nchw, - QuantDTypeCast, - Split, - Permute, - FakeQuantWithMinMaxVars, - Equal, - Less, - Greater, - Min, - Floor, - Abs, - Neg, - Cos, - Sin, - Sqrt, - Square, - Constant, - Log, - Tan, - Atan, - Asin, - Clip, - Transpose, - Squeeze, - Unsqueeze, - Upsample, - Dropout, - Broadcast, - Lrn, - Prelu, - ZerosLike, - TopK, - SpaceToDepth, - SpaceToBatch, - SparseToDense, - ReverseSequence, - Rank, - Gather, - GatherNd, - Fill, - Elu, - DepthToSpace, - BatchToSpace, - AddN, - Ceil, - EmbeddingLookup, - EmbeddingLookupSparse, - FloorDiv, - FloorMod, - L2Norm, - LocalResponseNormalization, - MatrixDiag, - Reduce, - Reverse, - Round, - Select, - Scatter, - Unique, - Unstack, - LogicalAnd, - LogicalOr, - LogicalXor, - LogicalNot, - OnnxInt8Quantize, - OnnxInt8Dequantize, - FakeQuantWithMinMax, - FakeQuantWithMinMaxPerChannel, - BatchNormFold, - MulFold, - AddFold, - SquaredDifference -} - -enum QuantType: int { - QUANT_NONE, - AwareTrainning, - WeightQuant, - PostTraining -} - -enum FmkType: int { - TF, - CAFFE, - ONNX, - MS, - TFLITE -} - -table OpDef { - name: string; - fmkType: FmkType; - attr: OpT; - inputIndex: [uint]; - outputIndex: [uint]; - quantType: QuantType = QUANT_NONE; - quantParam: [QuantParamArray]; -} - -table SubGraphDef { - name: string; - inputIndex: [uint]; - outputIndex: [uint]; - mempoolSize: uint; - nodes: [OpDef]; - allTensors: [TensorDef]; // weight + input + output -} - -table MempoolCfg { - size: uint; - shiftFactor: uint; -} - -table GraphDef { - name: string; - mempoolCfg: MempoolCfg; - subgraphs: [SubGraphDef]; -} - -root_type GraphDef; diff --git a/mindspore/ccsrc/predict/schema/op.fbs b/mindspore/ccsrc/predict/schema/op.fbs deleted file mode 100644 index 9286c2b2d31..00000000000 --- a/mindspore/ccsrc/predict/schema/op.fbs +++ /dev/null @@ -1,699 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -namespace mindspore.predict; - -enum ResizeMethod: byte { - UNKNOW = -1, - BILINEAR = 0, - NEAREST_NEIGHBOR = 1 -} - -enum DataType : int { - DT_FLOAT = 0, - DT_FLOAT16 = 1, - DT_INT8 = 2, - DT_INT32 = 3, - DT_UINT8 = 4, - DT_INT16 = 5, - DT_UINT32 = 8, - DT_INT64 = 9, - DT_UINT16 = 10, - DT_UNDEFINED = 16 -} - -enum Format : int { - NCHW = 0, - NHWC, - HWKC, - HWCK, - KCHW, - CKHW, - KHWC, - CHWK, - NC4HW4 = 100, - NUM_OF_FORMAT -} - -enum ActivationType : byte { - NO_ACTIVATION = 0, - RELU = 1, - SIGMOID = 2, - RELU6 = 3, - ELU = 4, - LEAKY_RELU = 5, - ABS = 6, - RELU1 = 7, - SOFTSIGN = 8, - SOFTPLUS = 9, - TANH = 10, - SELU = 11, - HSWISH = 12, - HSIGMOID = 13, - THRESHOLDRELU = 14, - LINEAR = 15, - UNKNOW = 16 -} - -enum ReduceType : byte { - REDUCE_MAX = 0, - REDUCE_MEAN = 1, - REDUCE_ALL = 2, - REDUCE_ANY = 3, - REDUCE_LOG_SUM_EXP = 4, - REDUCE_PROD = 5, - REDUCE_SUM = 6, - UNKNOW = 7 -} - -enum PoolMode : byte { - MAX_POOLING = 0, - MEAN_POOLING = 1, -} - -enum EltwiseMode : byte { - PROD = 0, - SUM = 1, - MAXIMUM = 2, - UNKNOW = 3 -} - -enum PadMode : byte { - NOTSET = 0, - SAME = 1, - VALID = 2, - CAFFE = 4 -} - -enum RoundMode : byte { - FLOOR = 0, - CEIL = 1 -} - -enum PaddingMode : byte { - CONSTANT = 0, - REFLECT = 1, - SYMMETRIC = 2, - MODE_RESERVED = 3 -} - -table Pad { - paddingmode: PaddingMode; - paddings: [int]; -} - -table Maximum { -} - -table Minimum { -} - -table Concat { - axis: int; - n: int; -} - -table SoftMax { - axis: [int]; -} - -table Activation { - type: ActivationType = 0; -} - -table Conv2D { - format: Format = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; - activationType: ActivationType = 0; -} - -table FusedBatchNorm { - epsilon: float = 0.00001; // eg. epsilon=0.001 - momentum: float = 0.9; - spatial: int = 1; -} - -table CaffeBatchNorm { - epsilon: float; // eg. epsilon=0.001 -} - -table Shape { -} - -table Nchw2Nhwc { - -} - -table Nhwc2Nchw { - -} - -table FakeQuantWithMinMaxVars { - narrowRange: bool; - numBits: int; -} - -table BiasAdd { - axis: [int]; -} - -table Pooling { - format: Format = 0; - poolingMode: PoolMode; - global: bool = false; - windowW: int; - windowH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - roundMode: RoundMode; -} - -table DepthwiseConv2D { - format: Format = 0; - channelIn: int; - channelMultiplier: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; - activationType: ActivationType = 0; -} - -table DeDepthwiseConv2D { - format: Format = 0; - channelIn: int; - channelMultiplier: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; - activationType: ActivationType = 0; -} - - -table Resize { - format: Format = 0; - method: ResizeMethod; - newHeight: long; - newWidth: long; - alignCorners: bool = false; - preserveAspectRatio: bool = false; -} - -table DetectionPostProcess { - format: Format = 0; - inputSize: int; - hScale: float; - wScale: float; - xScale: float; - yScale: float; - NmsIouThreshold: float; - NmsScoreThreshold: float; - MaxDetections: long; - DetectionsPreClass: long; - MaxClassesPreDetection: long; - NumClasses: long; - UseRegularNms: bool; -} - -table FullConnection { - hasBias: bool; - axis: int; -} - -// Mean(input_tensor, axis, keep_dims) -table Mean { - axis: [int]; - keepDims: bool = false; -} - -table DeConv2D { - format: Format = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; - activationType: ActivationType = 0; -} - -table Scale { - format: Format = 0; -} - -table Eltwise { - mode: EltwiseMode; -} - -table Add { -} - -table Sub { -} - -table Mul { -} - -table RealDiv { -} - -table Rsqrt { -} - -table Equal { -} - -table Less { -} - -table Greater { -} - -table Min { -} - -table Slice { - format: Format = 0; - begin: [int]; - size: [int]; -} - -table Floor { -} - -table Abs { -} - -table Neg { -} - -table Exp { -} - -table Cos { -} - -table Sin { -} - -table Sqrt { -} - -table Square { -} - -table Ceil { -} - -table Log { -} - -table Tan { -} - -table Atan { -} - -table Asin { -} - -table Reshape { - format: Format = 0; - shape: [long]; -} - -table Power { - power: float; - scale: float; - shift: float; -} - -table ArgMax { - axis: int; - outMaxValue: bool; - topK: int = 1; - keepDims: bool; - axisType: int; -} - -table ArgMin { - axis: int; - outMaxValue: bool; - topK: int = 1; - keepDims: bool; - axisType: int; -} - -table NetOutput { -} - -table MatMul { - transposeA : bool = false; - transposeB : bool = false; -} - -table CaffePReLU { - channelShared : bool = false; -} - -table LeakyReLU { - negativeSlope: float; -} - -table StridedSlice { - beginMask: int; - endMask: int; - ellipsisMask: int; - newAxisMask: int; - shrinkAxisMask: int; - begin: [int]; - end: [int]; - stride: [int]; - isScale: [int]; -} - -table Stack { - axis: int; - n: int; - isScale: [int]; -} - -table Range { - dType: DataType; - start: int; - limit: int; - delta: int; -} - -table ExpandDims { - dim: int; -} - -table Tile { - multiples: [int]; -} - -table Cast { - srcT: int; - dstT: int; -} - -table QuantDTypeCast { - srcT: DataType; - dstT: DataType; -} - -table Split { - numberSplit: int; - sizeSplits: [int]; - splitDim: int; -} - -table CaffeCrop { - axis : long; - offsets : [long]; -} - -table Permute { - order: [long]; -} - -table Clip { - max: float; - min: float; -} - -table Constant { -} - - -table Elu { - alpha: float = 1.0; -} - -table Broadcast { -} - -table Lrn { - alpha: float = 0.0001; - beta: float = 0.75; - bias: float = 1.0; - size: int; -} - -enum ReduceMode : byte { - ReduceMean = 0, - ReduceMax = 1, - ReduceMin = 2, - ReduceProd = 3, - ReduceSum = 4, - ReduceSumSquare = 5 -} - -table Reduce { - axes: [int]; - keepDims: int; - mode: ReduceMode; -} - -table Prelu { - slope: [float]; -} - -table Transpose { - perm: [int]; - conjugate: bool = false; -} - -table Squeeze { - axis: [int]; -} - -table Unsqueeze { - axis: [int]; -} - -table Upsample { - mode: string; - scales: [float]; -} - -table Dropout { - ratio : float = 0.5; -} - -table LocalResponseNormalization { - depth_radius: int; - bias: float; - alpha: float; - beta: float; -} - -table ZerosLike { -} - -table TopK { - k : int; - sorted : bool = true; -} - -table SpaceToDepth { - blockSize : int; - format: Format = 0; -} - -table SpaceToBatch { - blockShape : [int]; - paddings : [int]; -} - -table SparseToDense { - validateIndices: bool; -} - -table ReverseSequence { - seqAxis: int; - batchAxis: int; -} - -table Rank { -} - - -table Gather { - axis: int; - batchDims: int; -} - -table GatherNd { - batchDims: int; -} - -table Fill { - dims: [int]; -} - -table DepthToSpace { - blockSize: int; - format: Format = 0; -} - - -table BatchToSpace { - blockShape: [int]; - crops: [int]; -} - -table AddN { - N: int; -} - - -table EmbeddingLookup { - ids: [int]; - maxNorm: float; -} - -table EmbeddingLookupSparse { - spIds: [int]; - spWeights: [float]; - //combiner: Combiner=0; - maxNortm: float; -} - -table FloorDiv { -} - -table FloorMod { -} - -table L2Norm { - axis: [int]; - epsilon: float; -} - -table LogicalAnd { -} - -table LogicalOr { -} - -table LogicalXor { -} - -table LogicalNot { -} - -table MatrixDiag { - k: int; - numRows: int; - numCols: int; - paddingValue: float; -} - -table Select { -} - -table TfReduce { - type: ReduceType = 7; -} - -table Reverse { - axis: [int]; -} - -table Round { -} - -table Scatter { -} - -table Unique { -} - -table Unstack { - num: int; - axis: int; -} - -table OnnxInt8Quantize { -} - -table OnnxInt8Dequantize { -} - -table FakeQuantWithMinMax { -} - -table FakeQuantWithMinMaxPerChannel { -} - -table BatchNormFold { -} - -table MulFold { -} - -table AddFold { -} - -table SquaredDifference { -} diff --git a/mindspore/ccsrc/runtime/device/ascend/ascend_stream_assign.cc b/mindspore/ccsrc/runtime/device/ascend/ascend_stream_assign.cc index 3835dda0027..9a3327a5f0e 100644 --- a/mindspore/ccsrc/runtime/device/ascend/ascend_stream_assign.cc +++ b/mindspore/ccsrc/runtime/device/ascend/ascend_stream_assign.cc @@ -24,7 +24,6 @@ #include "common/utils.h" #include "backend/session/anf_runtime_algorithm.h" #include "runtime/device/kernel_adjust.h" -#include "predict/generator/utils/ir_model_util.h" #include "backend/optimizer/common/helper.h" #include "utils/utils.h" @@ -53,13 +52,6 @@ void AscendStreamAssign::AssignStream(const NotNull &graph_ptr) GetStreamRelations(); PrintStreamGroups(); FindEventRelations(graph_ptr); - - // Get info for D Model - AscendResourceMng &resource_manager = AscendResourceMng::GetInstance(); - generator::IRModelUtil::GetInstance().set_event_num(resource_manager.get_cur_event_num()); - generator::IRModelUtil::GetInstance().set_stream_num(resource_manager.get_cur_stream_num()); - // Init to 1,temporarily - generator::IRModelUtil::GetInstance().set_batch_num(1); } } diff --git a/mindspore/ccsrc/runtime/device/kernel_runtime.h b/mindspore/ccsrc/runtime/device/kernel_runtime.h index b613ee277a6..419c77e3f80 100644 --- a/mindspore/ccsrc/runtime/device/kernel_runtime.h +++ b/mindspore/ccsrc/runtime/device/kernel_runtime.h @@ -24,7 +24,6 @@ #include "runtime/device/device_address.h" #include "ir/tensor.h" #include "utils/convert_utils.h" -#include "predict/generator/utils/ir_model_util.h" #ifdef ENABLE_DUMP_E2E #include "debug/e2e_dump.h" #endif diff --git a/mindspore/ccsrc/utils/context/ms_context.cc b/mindspore/ccsrc/utils/context/ms_context.cc index 404c50f7968..b3320b03607 100644 --- a/mindspore/ccsrc/utils/context/ms_context.cc +++ b/mindspore/ccsrc/utils/context/ms_context.cc @@ -50,8 +50,6 @@ std::map MsContext::policy_map_ = {{"ge", kMsBacke MsContext::MsContext(const std::string &policy, const std::string &target) { save_graphs_flag_ = false; save_graphs_path_ = "."; - save_ms_model_flag_ = false; - save_ms_model_path_ = "./model.ms"; enable_dump_ = false; save_dump_path_ = "."; tsd_ref_ = 0; diff --git a/mindspore/ccsrc/utils/context/ms_context.h b/mindspore/ccsrc/utils/context/ms_context.h index 19205cccb83..489277f06e1 100644 --- a/mindspore/ccsrc/utils/context/ms_context.h +++ b/mindspore/ccsrc/utils/context/ms_context.h @@ -102,12 +102,6 @@ class MsContext { void set_enable_mem_reuse(bool enable_mem_reuse) { enable_mem_reuse_ = enable_mem_reuse; } bool enable_mem_reuse() const { return enable_mem_reuse_; } - bool save_ms_model_flag() const { return save_ms_model_flag_; } - void set_save_ms_model_flag(bool save_ms_model_flag) { save_ms_model_flag_ = save_ms_model_flag; } - - std::string save_ms_model_path() const { return save_ms_model_path_; } - void set_save_ms_model_path(const std::string &save_ms_model_path) { save_ms_model_path_ = save_ms_model_path; } - void set_enable_gpu_summary(bool enable_gpu_summary) { enable_gpu_summary_ = enable_gpu_summary; } bool enable_gpu_summary() const { return enable_gpu_summary_; } @@ -190,8 +184,6 @@ class MsContext { bool enable_reduce_precision_; bool enable_loop_sink_; bool enable_mem_reuse_; - std::string save_ms_model_path_; - bool save_ms_model_flag_; bool enable_gpu_summary_; bool enable_dump_; std::string save_dump_path_; diff --git a/mindspore/context.py b/mindspore/context.py index 551ec7b79a9..eecdc291bfa 100644 --- a/mindspore/context.py +++ b/mindspore/context.py @@ -234,22 +234,6 @@ class _Context: if not success: raise RuntimeError("Device id set failed!!!") - @property - def save_ms_model(self): - return self._context_handle.get_save_ms_model_flag() - - @save_ms_model.setter - def save_ms_model(self, save_ms_model_flag): - self._context_handle.set_save_ms_model_flag(save_ms_model_flag) - - @property - def save_ms_model_path(self): - return self._context_handle.get_save_ms_model_path() - - @save_ms_model_path.setter - def save_ms_model_path(self, save_ms_model_path): - self._context_handle.set_save_ms_model_path(save_ms_model_path) - @property def enable_auto_mixed_precision(self): return self._context_handle.get_auto_mixed_precision_flag() @@ -541,7 +525,7 @@ def reset_auto_parallel_context(): @args_type_check(mode=int, precompile_only=bool, device_target=str, device_id=int, save_graphs=bool, - save_graphs_path=str, save_ms_model=bool, save_ms_model_path=str, enable_dump=bool, + save_graphs_path=str, enable_dump=bool, save_dump_path=str, enable_reduce_precision=bool, variable_memory_max_size=str, enable_profiling=bool, profiling_options=str, enable_auto_mixed_precision=bool, enable_graph_kernel=bool, check_bprop=bool, max_device_memory=str, print_file_path=str, @@ -569,8 +553,6 @@ def set_context(**kwargs): device_id (int): Id of target device, the value must be in [0, device_num_per_host-1], while device_num_per_host should no more than 4096. Default: 0. save_graphs (bool): Whether to save graphs. Default: False. - save_ms_model (bool): Whether to save lite model converted by graph. Default: False. - save_ms_model_path (str): Path to save converted lite model. Default: "." save_graphs_path (str): Path to save graphs. Default: "." enable_auto_mixed_precision (bool): Whether to enable auto mixed precision. Default: True. enable_graph_kernel (bool): Whether to enable composition of basic primitives. These primitives would be @@ -615,7 +597,6 @@ def set_context(**kwargs): >>> context.set_context(device_id=0) >>> context.set_context(save_graphs=True, save_graphs_path="./model.ms") >>> context.set_context(enable_reduce_precision=True) - >>> context.set_context(save_ms_model=True, save_ms_model_path=".") >>> context.set_context(enable_dump=True, save_dump_path=".") >>> context.set_context(reserve_class_name_in_scope=True) >>> context.set_context(variable_memory_max_size="6GB") diff --git a/mindspore/train/serialization.py b/mindspore/train/serialization.py index fdb6fecb77d..8b54002669c 100644 --- a/mindspore/train/serialization.py +++ b/mindspore/train/serialization.py @@ -20,7 +20,6 @@ from threading import Thread, Lock import numpy as np import mindspore.nn as nn -import mindspore.context as context from mindspore import log as logger from mindspore.train.checkpoint_pb2 import Checkpoint from mindspore.train.print_pb2 import Print @@ -457,18 +456,17 @@ def export(net, *inputs, file_name, file_format='GEIR'): net (Cell): MindSpore network. inputs (Tensor): Inputs of the `net`. file_name (str): File name of model to export. - file_format (str): MindSpore currently supports 'GEIR', 'ONNX' 'LITE' and 'BINARY' format for exported model. + file_format (str): MindSpore currently supports 'GEIR', 'ONNX' and 'BINARY' format for exported model. - GEIR: Graph Engine Intermidiate Representation. An intermidiate representation format of Ascend model. - ONNX: Open Neural Network eXchange. An open format built to represent machine learning models. - - LITE: Huawei model format for mobile. A lite model only for the MindSpore Lite - BINARY: Binary format for model. An intermidiate representation format for models. """ logger.info("exporting model file:%s format:%s.", file_name, file_format) check_input_data(*inputs, data_class=Tensor) - supported_formats = ['GEIR', 'ONNX', 'LITE', 'BINARY'] + supported_formats = ['GEIR', 'ONNX', 'BINARY'] if file_format not in supported_formats: raise ValueError(f'Illegal file format {file_format}, it must be one of {supported_formats}') # switch network mode to infer when it is training @@ -497,9 +495,6 @@ def export(net, *inputs, file_name, file_format='GEIR'): with open(file_name, 'wb') as f: os.chmod(file_name, stat.S_IWUSR | stat.S_IRUSR) f.write(onnx_stream) - elif file_format == 'LITE': # file_format is 'LITE' - context.set_context(save_ms_model=True, save_ms_model_path=file_name) - net(*inputs) # restore network training mode if is_training: net.set_train(mode=True) diff --git a/predict/.gitignore b/predict/.gitignore deleted file mode 100644 index caf7aec495e..00000000000 --- a/predict/.gitignore +++ /dev/null @@ -1,14 +0,0 @@ -# git ignore file for predict - -#flatbuf generated file -schema/*_generated.h -schema/inner/*_generated.h -module/tvm_module/lite/include/*_generated.h - -#tvm fbs files -module/tvm_module/lite/tune/convert/*.fbs - -#doTest dir -test/doTest/ - - diff --git a/predict/CMakeLists.txt b/predict/CMakeLists.txt deleted file mode 100755 index 39ca6b27e8c..00000000000 --- a/predict/CMakeLists.txt +++ /dev/null @@ -1,79 +0,0 @@ -cmake_minimum_required(VERSION 3.12.1) -project (mindspore-predict) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g") -set(CMAKE_BUILD_TYPE "Release") - -set(CMAKE_CXX_STANDARD 11) -set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden") -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden") -set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -s") - -option(ENABLE_ASAN "Enable Google Sanitizer to find memory bugs" OFF) -option(ENABLE_PREDICT_ARM64 "predict arm64" OFF) -option(ENABLE_PREDICT_ARM32 "predict arm32" OFF) - -set(PREDICT_DIR ${CMAKE_CURRENT_SOURCE_DIR}) -set(PREDICT_BUILD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/build) -set(3RD_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../third_party) -set(DOTEST_DIR ${PREDICT_BUILD_DIR}/test/doTest) - -include_directories(${3RD_DIR}) -include_directories(${3RD_DIR}/flatbuffers/include/) -include_directories(${3RD_DIR}/protobuf/build/include/) -include_directories(${3RD_DIR}/googletest/googletest/include/) -include_directories(${3RD_DIR}/googletest/googlemock/include/) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/module/tvm_kernel/lite/include/) -include_directories(${PREDICT_DIR}/module/tvm_kernel/incubator-tvm/3rdparty/dlpack/include) -include_directories(common) - -if(ENABLE_PREDICT_ARM64 OR ENABLE_PREDICT_ARM32) - message("*********************predict compile arm*********************") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DMS_USE_ARM=1") - set(ANDROID_NDK $ENV{ANDROID_NDK}) - if(ANDROID_NDK) - add_subdirectory(${3RD_DIR}/googletest ${CMAKE_BINARY_DIR}/googletest) - link_directories(${PREDICT_BUILD_DIR}/googletest/googlemock/gtest) - - add_subdirectory(${3RD_DIR}/securec ${CMAKE_BINARY_DIR}/securec) - link_directories(${PREDICT_BUILD_DIR}/securec/src) - else() - message(FATAL_ERROR "please set ANDROID_NDK in environment variable for example: export ANDROID_NDK=/root/usr/android-ndk-r16b/") - endif() - - include_directories(${ANDROID_SYSROOT}/usr/include/) - if(${ANDROID_ABI} STREQUAL "armeabi-v7a") - include_directories(${ANDROID_SYSROOT}/usr/include/arm-linux-androideabi) - elseif(${ANDROID_ABI} STREQUAL "arm64-v8a") - include_directories(${ANDROID_SYSROOT}/usr/include/aarch64-linux-android) - else() - include_directories(${ANDROID_SYSROOT}/usr/include/arm-linux-androideabi) - endif() - -else() - # include libsecurec.a x86 - message("*********************predict compile x86*********************") - if(EXISTS "${PREDICT_DIR}/../build/mindspore/securec/src/libsecurec.a") - link_directories(${PREDICT_DIR}/../build/mindspore/securec/src) - else() - include(${PREDICT_DIR}/../cmake/dependency_securec.cmake) - link_directories(${PREDICT_BUILD_DIR}/securec/src) - endif() - - # include libgtest.so x86 - if(EXISTS "${PREDICT_DIR}/../build/googletest/googlemock/gtest/libgtest.so") - link_directories(${PREDICT_DIR}/../build/googletest/googlemock/gtest) - else() - include(${PREDICT_DIR}/../cmake/dependency_gtest.cmake) - link_directories(${PREDICT_BUILD_DIR}/googletest/googlemock/gtest) - endif() -endif() - -if (CODE_COVERAGE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -O0") -endif() - -add_subdirectory(common) -add_subdirectory(src) -add_subdirectory(benchmark) -add_subdirectory(test) -add_subdirectory(module) diff --git a/predict/benchmark/CMakeLists.txt b/predict/benchmark/CMakeLists.txt deleted file mode 100755 index 22f87d8a97e..00000000000 --- a/predict/benchmark/CMakeLists.txt +++ /dev/null @@ -1,38 +0,0 @@ - -cmake_minimum_required(VERSION 3.12) -project(benchmark) - -set(CMAKE_CXX_STANDARD 14) -set(CMAKE_BUILD_TYPE "Debug") - -#include 3rd -include_directories(${3RD_DIR}/protobuf/build/include) -include_directories(${3RD_DIR}/securec/include) -include_directories(${3RD_DIR}/flatbuffers/include) -include_directories(${3RD_DIR}/googletest/googletest/include) -include_directories(${3RD_DIR}/googletest/googlemock/include) -include_directories(${PREDICT_DIR}/module/tvm_kernel/incubator-tvm/3rdparty/dlpack/include) -include_directories(${3RD_DIR}/flatbuffers/include) -include_directories(${3RD_DIR}/securec/include) - -#include ms -include_directories(.) -include_directories(${PREDICT_DIR}) - -set(COMMON_SRC ${PREDICT_DIR}/common/flag_parser.cc - ${PREDICT_DIR}/common/file_utils.cc - ${PREDICT_DIR}/common/func_utils.cc - ${PREDICT_DIR}/common/mslog.cc - ${PREDICT_DIR}/common/utils.cc) - -link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../output/lib/) - -add_executable(benchmark main.cc benchmark.cc ${COMMON_SRC}) - -target_link_libraries(benchmark mspredict libsecurec.a) -add_dependencies(benchmark tvm_kernel) -add_dependencies(benchmark securec) - -add_custom_command(TARGET benchmark POST_BUILD - COMMAND mkdir -pv ${DOTEST_DIR} - COMMAND cp ${PREDICT_BUILD_DIR}/benchmark/benchmark ${DOTEST_DIR}) diff --git a/predict/benchmark/README.md b/predict/benchmark/README.md deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/predict/benchmark/benchmark.cc b/predict/benchmark/benchmark.cc deleted file mode 100644 index c55d03e450e..00000000000 --- a/predict/benchmark/benchmark.cc +++ /dev/null @@ -1,451 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "benchmark/benchmark.h" -#include -#include -#include -#include -#include -#include "include/session.h" - -namespace mindspore { -namespace predict { -STATUS Benchmark::GenerateRandomData(size_t size, void *data) { - MS_ASSERT(data != nullptr); - char *castedData = static_cast(data); - for (size_t i = 0; i < size; i++) { - castedData[i] = static_cast(i); - } - return RET_OK; -} - -STATUS Benchmark::GenerateInputData() { - for (Tensor *tensor : msInputs) { - MS_ASSERT(tensor != nullptr); - auto ret = tensor->MallocData(); - if (ret != RET_OK) { - MS_LOGE("MallocData for inTensor failed %d", ret); - return ret; - } - MS_ASSERT(tensor->GetData() != nullptr); - auto tensorByteSize = tensor->GetDataSize(); - auto status = GenerateRandomData(tensorByteSize, tensor->GetData()); - if (status != RET_OK) { - MS_LOGE("GenerateRandomData for inTensor failed %d", status); - return status; - } - } - return RET_OK; -} - -STATUS Benchmark::LoadInput() { - size_t size = 0; - char *graphBuf = ReadFile(_flags->modelPath.c_str(), &size); - if (graphBuf == nullptr) { - MS_LOGE("Load graph failed, path %s", _flags->modelPath.c_str()); - return RET_ERROR; - } - - this->msInputs = session->GetInput(); - - if (_flags->inDataPath.empty()) { - auto status = GenerateInputData(); - if (status != RET_OK) { - delete graphBuf; - MS_LOGE("Generate input data error %d", status); - return status; - } - } else { - auto status = ReadInputFile(); - if (status != RET_OK) { - delete graphBuf; - MS_LOGE("ReadInputFile error, %d", status); - return status; - } - } - delete graphBuf; - return RET_OK; -} - -STATUS Benchmark::ReadInputFile() { - MS_ASSERT(msInputs.size() <= 1); - if (msInputs.empty()) { - return RET_OK; - } - Tensor *inTensor = msInputs.at(0); - MS_ASSERT(inTensor != nullptr); - - size_t size; - char *binBuf = ReadFile(_flags->inDataPath.c_str(), &size); - if (binBuf == nullptr) { - return RET_ERROR; - } - auto tensorDataSize = inTensor->GetDataSize(); - if (size != tensorDataSize) { - MS_LOGE("Input binary file size error, required: %zu, in fact: %zu", tensorDataSize, size); - delete binBuf; - return RET_ERROR; - } - inTensor->SetData(binBuf); - binBuf = nullptr; - - return RET_OK; -} - -// calibData is FP32 -STATUS Benchmark::ReadCalibData() { - const char *calibDataPath = _flags->calibDataPath.c_str(); - // read calib data - std::ifstream inFile(calibDataPath); - if (!inFile.good()) { - MS_LOGE("file: %s is not exist", calibDataPath); - return RET_PARAM_INVALID; - } - - if (!inFile.is_open()) { - MS_LOGE("file: %s open failed", calibDataPath); - inFile.close(); - return RET_PARAM_INVALID; - } - - std::string line; - MS_LOGI("Start reading calibData file"); - std::string tensorName; - while (!inFile.eof()) { - getline(inFile, line); - std::stringstream stringLine1(line); - size_t dim = 0; - stringLine1 >> tensorName >> dim; - std::vector dims; - size_t shapeSize = 1; - for (size_t i = 0; i < dim; i++) { - size_t tmpDim; - stringLine1 >> tmpDim; - dims.push_back(tmpDim); - shapeSize *= tmpDim; - } - - getline(inFile, line); - std::stringstream stringLine2(line); - std::vector tensorData; - for (size_t i = 0; i < shapeSize; i++) { - float tmpData; - stringLine2 >> tmpData; - tensorData.push_back(tmpData); - } - - std::unique_ptr checkTensor(new CheckTensor(dims, tensorData)); - this->calibData.insert(std::make_pair(tensorName, checkTensor.release())); - } - inFile.close(); - MS_LOGI("Finish reading calibData file"); - return RET_OK; -} - -// tensorData need to be converter first -float Benchmark::CompareData(const std::string &nodeName, std::vector msShape, float *msTensorData) { - auto iter = this->calibData.find(nodeName); - if (iter != this->calibData.end()) { - std::vector castedMSShape; - size_t shapeSize = 1; - for (int64_t dim : msShape) { - castedMSShape.push_back(size_t(dim)); - shapeSize *= dim; - } - - CheckTensor *calibTensor = iter->second; - if (calibTensor->shape != castedMSShape) { - std::ostringstream oss; - oss << "Shape of mslite output("; - for (auto dim : castedMSShape) { - oss << dim << ","; - } - oss << ") and shape source model output("; - for (auto dim : calibTensor->shape) { - oss << dim << ","; - } - oss << ") are different"; - MS_LOGE("%s", oss.str().c_str()); - return -1; - } - - float meanBias = 0; - std::ostringstream outputData; - outputData << "Data of node " << nodeName << " : "; - for (size_t j = 0; j < shapeSize; j++) { - if (j < printNum) { - outputData << msTensorData[j] << " "; - } - if (fabs(calibTensor->data.at(j)) > minFloatThr) { - double bias = fabs(msTensorData[j] - calibTensor->data.at(j)) / fabs(calibTensor->data.at(j)); - meanBias += bias; - } - } - meanBias /= shapeSize; - MS_LOGI("%s", outputData.str().c_str()); - - if (meanBias <= minFloatThr) { - MS_LOGI("Mean bias of node %s : 0%%", nodeName.c_str()); - } else { - MS_LOGI("Mean bias of node %s : %f%%", nodeName.c_str(), meanBias * percentage); - } - return meanBias; - } else { - MS_LOGI("%s is not in Source Model output", nodeName.c_str()); - return -1; - } -} - -STATUS Benchmark::CompareOutput(const std::map> &msOutputs) { - float totalBias = 0; - int totalSize = 0; - bool hasError = false; - for (const auto &msOutput : msOutputs) { - std::string nodeName = msOutput.first; - auto tensors = msOutput.second; - for (auto tensor : tensors) { - MS_ASSERT(tensor->GetData() != nullptr); - float bias = CompareData(nodeName, tensor->GetDims(), static_cast(tensor->GetData())); - if (bias >= 0) { - totalBias += bias; - totalSize++; - } else { - hasError = true; - break; - } - } - } - - if (!hasError) { - float meanBias; - if (totalSize != 0) { - meanBias = totalBias / totalSize * percentage; - } else { - meanBias = 0; - } - - MS_LOGI("Mean bias all node : %f%%", meanBias); - - if (meanBias > 1) { - MS_LOGE("Mean bias of all nodes is too big: %f%%", meanBias); - return RET_ERROR; - } else { - return RET_OK; - } - } else { - MS_LOGE("Error in CompareData"); - return RET_ERROR; - } -} - -STATUS Benchmark::MarkPerformance() { - MS_LOGI("Running warm up loops..."); - for (int i = 0; i < _flags->warmUpLoopCount; i++) { - auto status = session->Run(msInputs); - if (status != RET_OK) { - MS_LOGE("Inference error %d", status); - return status; - } - } - - MS_LOGI("Running benchmark loops..."); - uint64_t timeMin = maxTimeThr; - uint64_t timeMax = 0; - uint64_t timeAvg = 0; - for (int i = 0; i < _flags->loopCount; i++) { - uint64_t start = GetTimeUs(); - auto status = session->Run(msInputs); - if (status != RET_OK) { - MS_LOGE("Inference error %d", status); - return status; - } - - uint64_t end = GetTimeUs(); - uint64_t time = end - start; - timeMin = std::min(timeMin, time); - timeMax = std::max(timeMax, time); - timeAvg += time; - - msOutputs = session->GetAllOutput(); - if (cleanData) { - for (auto &msOutput : msOutputs) { - for (auto &outputTensor : msOutput.second) { - delete outputTensor; - } - } - msOutputs.clear(); - } - } - if (_flags->loopCount > 0) { - timeAvg /= _flags->loopCount; - MS_LOGI("MinRunTime = %f ms, MaxRuntime = %f ms, AvgRunTime = %f ms", timeMin / US2MS, timeMax / US2MS, - timeAvg / US2MS); - } - return RET_OK; -} - -STATUS Benchmark::MarkAccuracy() { - MS_LOGI("MarkAccuracy"); - - auto status = session->Run(msInputs); - if (status != RET_OK) { - MS_LOGE("Inference error %d", status); - return status; - } - msOutputs = session->GetAllOutput(); - - ReadCalibData(); - status = CompareOutput(msOutputs); - if (cleanData) { - for (auto &msOutput : msOutputs) { - for (auto &outputTensor : msOutput.second) { - delete outputTensor; - } - } - msOutputs.clear(); - } - return status; -} - -STATUS Benchmark::CleanData() { - if (cleanData) { - for (auto &msInput : msInputs) { - delete msInput; - } - msInputs.clear(); - for (auto &data : calibData) { - data.second->shape.clear(); - data.second->data.clear(); - delete data.second; - } - calibData.clear(); - } - return RET_OK; -} - -STATUS Benchmark::RunBenchmark() { - // Load graph - std::string comment = modelName; - - MS_LOGI("start reading model file"); - size_t size = 0; - char *graphBuf = ReadFile(_flags->modelPath.c_str(), &size); - if (graphBuf == nullptr) { - MS_LOGE("Load graph failed while running %s", comment.c_str()); - return RET_ERROR; - } - - uint64_t startPrepareTime = GetTimeUs(); - session = CreateSession(graphBuf, size, ctx); - if (session == nullptr) { - delete graphBuf; - MS_LOGE("new session failed while running %s", comment.c_str()); - return RET_ERROR; - } - uint64_t endPrepareTime = GetTimeUs(); - MS_LOGI("PrepareTime = %f ms, ", (endPrepareTime - startPrepareTime) / US2MS); - - // Load input - MS_LOGI("start generate input data"); - auto status = LoadInput(); - if (status != RET_OK) { - delete graphBuf; - MS_LOGE("Generate input data error"); - return status; - } - - if (!_flags->calibDataPath.empty()) { - status = MarkAccuracy(); - if (status != RET_OK) { - delete graphBuf; - MS_LOGE("Run MarkAccuracy error: %d", status); - return status; - } - } else { - status = MarkPerformance(); - if (status != RET_OK) { - delete graphBuf; - MS_LOGE("Run MarkPerformance error: %d", status); - return status; - } - } - - CleanData(); - delete graphBuf; - return RET_OK; -} - -STATUS Benchmark::Init() { - if (this->_flags == nullptr) { - return RET_ERROR; - } - MS_LOGI("ModelPath = %s", this->_flags->modelPath.c_str()); - MS_LOGI("InDataPath = %s", this->_flags->inDataPath.c_str()); - MS_LOGI("TensorDataType = %s", this->_flags->tensorDataTypeIn.c_str()); - MS_LOGI("LoopCount = %d", this->_flags->loopCount); - MS_LOGI("WarmUpLoopCount = %d", this->_flags->warmUpLoopCount); - MS_LOGI("NumThreads = %d", this->_flags->numThreads); - MS_LOGI("calibDataPath = %s", this->_flags->calibDataPath.c_str()); - - this->_flags->inDataType = this->_flags->inDataTypeIn == "img" ? kImage : kBinary; - if (this->_flags->tensorDataTypeIn == "float") { - this->_flags->tensorDataType = DataType_DT_FLOAT; - } - - if (_flags->modelPath.empty()) { - MS_LOGE("modelPath is required"); - return RET_ERROR; - } - - modelName = _flags->modelPath.substr(_flags->modelPath.find_last_of("/") + 1); - - return RET_OK; -} - -int RunBenchmark(int argc, const char **argv) { - BenchmarkFlags flags; - Option err = flags.ParseFlags(argc, argv); - - if (err.IsSome()) { - std::cerr << err.Get() << std::endl; - std::cerr << flags.Usage() << std::endl; - return -1; - } - - if (flags.help) { - std::cerr << flags.Usage() << std::endl; - return 0; - } - - Benchmark mBenchmark(&flags); - auto status = mBenchmark.Init(); - if (status != RET_OK) { - MS_LOGE("Benchmark init Error : %d", status); - return 1; - } - - status = mBenchmark.RunBenchmark(); - if (status != RET_OK) { - MS_LOGE("Run Benchmark Error : %d", status); - return 1; - } - - MS_LOGI("end of benchmark"); - return 0; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/benchmark/benchmark.h b/predict/benchmark/benchmark.h deleted file mode 100644 index 03cd117df0f..00000000000 --- a/predict/benchmark/benchmark.h +++ /dev/null @@ -1,142 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_BENCHMARK_BENCHMARK_H_ -#define PREDICT_BENCHMARK_BENCHMARK_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "common/flag_parser.h" -#include "common/file_utils.h" -#include "common/func_utils.h" -#include "common/mslog.h" -#include "common/utils.h" -#include "include/errorcode.h" -#include "include/session.h" -#include "include/tensor.h" -#include "schema/inner/ms_generated.h" -#include "src/graph.h" -#include "src/graph_execution.h" -#include "src/op.h" - -namespace mindspore { -namespace predict { -enum InDataType { kImage = 0, kBinary = 1 }; - -struct CheckTensor { - CheckTensor(const std::vector &shape, const std::vector &data) { - this->shape = shape; - this->data = data; - } - std::vector shape; - std::vector data; -}; - -class BenchmarkFlags : public virtual FlagParser { - public: - BenchmarkFlags() { - // common - AddFlag(&BenchmarkFlags::modelPath, "modelPath", "Input model path", ""); - AddFlag(&BenchmarkFlags::tensorDataTypeIn, "tensorDataType", "Data type of input Tensor. float", "float"); - AddFlag(&BenchmarkFlags::inDataPath, "inDataPath", "Input data path, if not set, use random input", ""); - // MarkPerformance - AddFlag(&BenchmarkFlags::loopCount, "loopCount", "Run loop count", 10); - AddFlag(&BenchmarkFlags::numThreads, "numThreads", "Run threads number", 2); - AddFlag(&BenchmarkFlags::warmUpLoopCount, "warmUpLoopCount", "Run warm up loop", 3); - // MarkAccuracy - AddFlag(&BenchmarkFlags::calibDataPath, "calibDataPath", "Calibration data file path", ""); - } - - ~BenchmarkFlags() override = default; - - public: - // common - std::string modelPath; - std::string inDataPath; - InDataType inDataType; - std::string inDataTypeIn; - DataType tensorDataType; - std::string tensorDataTypeIn; - // MarkPerformance - int loopCount; - int numThreads; - int warmUpLoopCount; - // MarkAccuracy - std::string calibDataPath; -}; - -class Benchmark { - public: - explicit Benchmark(BenchmarkFlags *flags) : _flags(flags) {} - - virtual ~Benchmark() = default; - - STATUS Init(); - STATUS RunBenchmark(); - - private: - // call GenerateInputData or ReadInputFile to init inputTensors - STATUS LoadInput(); - - // call GenerateRandomData to fill inputTensors - STATUS GenerateInputData(); - - STATUS GenerateRandomData(size_t size, void *data); - - STATUS ReadInputFile(); - - STATUS ReadCalibData(); - - STATUS CleanData(); - - STATUS CompareOutput(const std::map> &msOutputs); - - float CompareData(const std::string &nodeName, std::vector msShape, float *msTensorData); - - STATUS MarkPerformance(); - - STATUS MarkAccuracy(); - - private: - BenchmarkFlags *_flags; - std::shared_ptr session; - Context ctx; - std::vector msInputs; - std::map> msOutputs; - std::unordered_map calibData; - std::string modelName = ""; - bool cleanData = true; - - const float US2MS = 1000.0f; - const float percentage = 100.0f; - const int printNum = 50; - const float minFloatThr = 0.0000001f; - - const uint64_t maxTimeThr = 1000000; -}; - -int RunBenchmark(int argc, const char **argv); -} // namespace predict -} // namespace mindspore -#endif // PREDICT_BENCHMARK_BENCHMARK_H_ diff --git a/predict/benchmark/main.cc b/predict/benchmark/main.cc deleted file mode 100644 index 66e473a42a0..00000000000 --- a/predict/benchmark/main.cc +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include "benchmark/benchmark.h" - -int main(int argc, const char **argv) { - signal(SIGSEGV, mindspore::predict::CoreDumpTraceFunc); - return mindspore::predict::RunBenchmark(argc, argv); -} diff --git a/predict/common/CMakeLists.txt b/predict/common/CMakeLists.txt deleted file mode 100755 index 3734c26bc0a..00000000000 --- a/predict/common/CMakeLists.txt +++ /dev/null @@ -1,17 +0,0 @@ -include_directories(${CMAKE_CURRENT_SOURCE_DIR}) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../include) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../../third_party) - -add_compile_options(-fPIC) - -add_library(common_mid OBJECT - ${CMAKE_CURRENT_SOURCE_DIR}/common.h - ${CMAKE_CURRENT_SOURCE_DIR}/graph_util.cc - ${CMAKE_CURRENT_SOURCE_DIR}/file_utils.cc - ${CMAKE_CURRENT_SOURCE_DIR}/flag_parser.cc - ${CMAKE_CURRENT_SOURCE_DIR}/func_utils.cc - ${CMAKE_CURRENT_SOURCE_DIR}/module_registry.cc - ${CMAKE_CURRENT_SOURCE_DIR}/mslog.cc - ${CMAKE_CURRENT_SOURCE_DIR}/storage.cc - ${CMAKE_CURRENT_SOURCE_DIR}/utils.cc) diff --git a/predict/common/common.h b/predict/common/common.h deleted file mode 100644 index d93139abae9..00000000000 --- a/predict/common/common.h +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_COMMON_H_ -#define PREDICT_COMMON_COMMON_H_ - -#include -#include "schema/inner/ms_generated.h" - -namespace mindspore { -namespace predict { -enum NCHW_SHAPE { NCHW_N = 0, NCHW_C = 1, NCHW_H = 2, NCHW_W = 3 }; -enum NHWC_SHAPE { NHWC_N = 0, NHWC_H = 1, NHWC_W = 2, NHWC_C = 3 }; -enum HWCK_SHAPE { HWCK_H = 0, HWCK_W = 1, HWCK_C = 2, HWCK_K = 3 }; -enum KCHW_SHAPE { KCHW_K = 0, KCHW_C = 1, KCHW_H = 2, KCHW_W = 3 }; -enum CHW_SHAPE { CHW_C = 0, CHW_H = 1, CHW_W = 2 }; -enum HWC_SHAPE { HWC_H = 0, HWC_W = 1, HWC_C = 2 }; - -static constexpr int TENSOR_MAX_REFCOUNT = 999; - -static const char *DELIM_COLON = ":"; -static const char *DELIM_COMMA = ","; -static const char *DELIM_SLASH = "/"; -static const char *DELIM_DOUBLE_BACKSLASH = "\\"; - -// quantization relative -static const char QUANTIZED_UINT8[] = "QUANTIZED_UINT8"; -static const char QUANTIZED_INT8[] = "QUANTIZED_INT8"; -static const char QUANTIZED_INT16[] = "QUANTIZED_INT16"; -static const char QUANTIZED_UINT16[] = "QUANTIZED_UINT16"; -static const char QUANTIZED_FLOAT16[] = "FLOAT16"; -static const char QUANTIZED_FLOAT32[] = "FLOAT32"; -static const char QUANTIZATION_TYPE_DYNAMIC[] = "DYNAMIC"; -static const char QUANTIZATION_TYPE_STATIC[] = "STATIC"; -static const char CALIB_NORM[] = "NORM"; - -// dims -static const int32_t DIM_DEFAULT_SIZE = 4; - -static const Format DEFAULT_FORMAT = Format_NCHW; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_COMMON_COMMON_H_ diff --git a/predict/common/file_utils.cc b/predict/common/file_utils.cc deleted file mode 100644 index 94adf0f7ac4..00000000000 --- a/predict/common/file_utils.cc +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "common/file_utils.h" -#include - -namespace mindspore { -namespace predict { -char *ReadFile(const char *file, size_t *size) { - if (file == nullptr) { - MS_LOGE("file is nullptr"); - return nullptr; - } - MS_ASSERT(size != nullptr); - std::ifstream ifs(RealPath(file)); - if (!ifs.good()) { - MS_LOGE("file: %s is not exist", file); - return nullptr; - } - - if (!ifs.is_open()) { - MS_LOGE("file: %s open failed", file); - return nullptr; - } - - ifs.seekg(0, std::ios::end); - *size = ifs.tellg(); - std::unique_ptr buf(new (std::nothrow) char[*size]); - if (buf == nullptr) { - MS_LOGE("malloc buf failed, file:%s", file); - ifs.close(); - return nullptr; - } - - ifs.seekg(0, std::ios::beg); - ifs.read(buf.get(), *size); - ifs.close(); - - return buf.release(); -} - -std::string RealPath(const char *path) { - if (path == nullptr) { - MS_LOGE("path is nullptr"); - return ""; - } - if ((strlen(path)) >= PATH_MAX) { - MS_LOGE("path is too long"); - return ""; - } - - std::shared_ptr resolvedPath(new (std::nothrow) char[PATH_MAX]{0}); - if (resolvedPath == nullptr) { - MS_LOGE("new resolvedPath failed"); - return ""; - } - - auto ret = realpath(path, resolvedPath.get()); - if (ret == nullptr) { - MS_LOGE("realpath failed"); - return ""; - } - return resolvedPath.get(); -} -} // namespace predict -} // namespace mindspore diff --git a/predict/common/file_utils.h b/predict/common/file_utils.h deleted file mode 100644 index e67c1cf9f11..00000000000 --- a/predict/common/file_utils.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_FILE_UTILS_H_ -#define PREDICT_COMMON_FILE_UTILS_H_ - -#include -#include -#include -#include -#include -#include -#include -#include "common/utils.h" -#include "common/mslog.h" -#include "include/tensor.h" - -namespace mindspore { -namespace predict { -char *ReadFile(const char *file, size_t *size); - -std::string RealPath(const char *path); -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_COMMON_FILE_UTILS_H_ diff --git a/predict/common/flag_parser.cc b/predict/common/flag_parser.cc deleted file mode 100644 index 37482dc4095..00000000000 --- a/predict/common/flag_parser.cc +++ /dev/null @@ -1,179 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "common/flag_parser.h" - -namespace mindspore { -namespace predict { -// parse flags read from command line -Option FlagParser::ParseFlags(int argc, const char *const *argv, bool supportUnknown, - bool supportDuplicate) { - MS_ASSERT(argv != nullptr); - const int FLAG_PREFIX_LEN = 2; - // Get binary name - binName = GetFileName(argv[0]); - - std::multimap> keyValues; - for (int i = 1; i < argc; i++) { - std::string tmp = argv[i]; - Trim(&tmp); - const std::string flagItem(tmp); - - if (flagItem == "--") { - break; - } - - if (flagItem.find("--") == std::string::npos) { - continue; - } - - std::string key; - Option value = Option(None()); - - size_t pos = flagItem.find_first_of("="); - if (pos == std::string::npos && flagItem.find("--no-") != std::string::npos) { - key = flagItem.substr(FLAG_PREFIX_LEN); - } else if (pos == std::string::npos) { - key = flagItem.substr(FLAG_PREFIX_LEN); - } else { - key = flagItem.substr(FLAG_PREFIX_LEN, pos - FLAG_PREFIX_LEN); - value = Option(flagItem.substr(pos + 1)); - } - - keyValues.insert(std::pair>(key, value)); - } - - Option ret = Option(InnerParseFlags(&keyValues)); - if (ret.IsSome()) { - return Option(ret.Get()); - } - - return Option(None()); -} - -bool FlagParser::GetRealFlagName(const std::string &oriFlagName, std::string *flagName) { - MS_ASSERT(flagName != nullptr); - const int BOOL_TYPE_FLAG_PREFIX_LEN = 3; - bool opaque = false; - if (StartsWithPrefix(oriFlagName, "no-")) { - *flagName = oriFlagName.substr(BOOL_TYPE_FLAG_PREFIX_LEN); - opaque = true; - } else { - *flagName = oriFlagName; - } - return opaque; -} - -// Inner parse function -Option FlagParser::InnerParseFlags(std::multimap> *keyValues) { - MS_ASSERT(keyValues != nullptr); - for (auto it = keyValues->begin(); it != keyValues->end(); ++it) { - std::string flagName; - bool opaque = GetRealFlagName((*it).first, &flagName); - Option flagValue = (*it).second; - - auto item = flags.find(flagName); - if (item == flags.end()) { - return Option(std::string(flagName + " is not a valid flag")); - } - FlagInfo *flag = &(item->second); - if (flag == nullptr) { - return Option("Failed: flag is nullptr"); - } - if (flag->isParsed) { - return Option("Failed: already parsed flag: " + flagName); - } - std::string tmpValue; - if (!flag->isBoolean) { - if (opaque) { - return Option(flagName + " is not a boolean type"); - } - if (flagValue.IsNone()) { - return Option("No value provided for non-boolean type: " + flagName); - } - tmpValue = flagValue.Get(); - } else { - if (flagValue.IsNone() || flagValue.Get().empty()) { - tmpValue = !opaque ? "true" : "false"; - } else if (!opaque) { - tmpValue = flagValue.Get(); - } else { - return Option(std::string("Boolean flag can not have non-empty value")); - } - } - // begin to parse value - Option ret = flag->parse(this, tmpValue); - if (ret.IsNone()) { - return Option("Failed to parse value for: " + flag->flagName); - } - flag->isParsed = true; - } - - // to check flags not given in command line but added as in constructor - for (auto &flag : flags) { - if (flag.second.isRequired && !flag.second.isParsed) { - return Option("Error, value of '" + flag.first + "' not provided"); - } - } - - return Option(None()); -} - -void Replaceall(std::string *str, const std::string &oldValue, const std::string &newValue) { - if (str == nullptr) { - MS_LOGE("Input str is nullptr"); - return; - } - while (true) { - std::string::size_type pos(0); - if ((pos = str->find(oldValue)) != std::string::npos) { - str->replace(pos, oldValue.length(), newValue); - } else { - break; - } - } -} - -std::string FlagParser::Usage(const Option &usgMsg) const { - // first line, brief of the usage - std::string usageString = usgMsg.IsSome() ? usgMsg.Get() + "\n" : ""; - // usage of bin name - usageString += usageMsg.IsNone() ? "usage: " + binName + " [options]\n" : usageMsg.Get() + "\n"; - // help line of help message, usageLine:message of parametors - std::string helpLine = ""; - std::string usageLine = ""; - uint32_t i = 0; - for (auto flag = flags.begin(); flag != flags.end(); flag++) { - std::string flagName = flag->second.flagName; - std::string helpInfo = flag->second.helpInfo; - // parameter line - std::string thisLine = flag->second.isBoolean ? " --[no-]" + flagName : " --" + flagName + "=VALUE"; - if (++i < flags.size()) { - // add paramter help message of each line - thisLine += " " + helpInfo; - Replaceall(&helpInfo, "\n\r", "\n"); - usageLine += thisLine + "\n"; - } else { - // brief help message - helpLine = thisLine + " " + helpInfo + "\n"; - } - } - // total usage is brief of usage+ brief of bin + help message + brief of - // paramters - return usageString + helpLine + usageLine; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/common/flag_parser.h b/predict/common/flag_parser.h deleted file mode 100644 index f01b8df71e1..00000000000 --- a/predict/common/flag_parser.h +++ /dev/null @@ -1,291 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_FLAG_PARSER_H_ -#define PREDICT_COMMON_FLAG_PARSER_H_ - -#include -#include -#include -#include - -#include "common/utils.h" -#include "common/option.h" - -namespace mindspore { -namespace predict { -struct FlagInfo; - -struct Nothing {}; - -class FlagParser { - public: - FlagParser() { AddFlag(&FlagParser::help, "help", "print usage message", false); } - - virtual ~FlagParser() = default; - - // only support read flags from command line - virtual Option ParseFlags(int argc, const char *const *argv, bool supportUnknown = false, - bool supportDuplicate = false); - std::string Usage(const Option &usgMsg = Option(None())) const; - - template - void AddFlag(T1 *t1, const std::string &flagName, const std::string &helpInfo, const T2 &t2); - - template - void AddFlag(T1 Flags::*t1, const std::string &flagName, const std::string &helpInfo, const T2 &t2); - - template - void AddFlag(T Flags::*t, const std::string &flagName, const std::string &helpInfo); - - // Option-type fields - template - void AddFlag(Option Flags::*t, const std::string &flagName, const std::string &helpInfo); - bool help; - - protected: - std::string binName; - Option usageMsg; - - private: - struct FlagInfo { - std::string flagName; - bool isRequired; - bool isBoolean; - std::string helpInfo; - bool isParsed; - std::function(FlagParser *, const std::string &)> parse; - }; - - inline void AddFlag(const FlagInfo &flag); - - // construct a temporary flag - template - void ConstructFlag(Option Flags::*t, const std::string &flagName, const std::string &helpInfo, FlagInfo *flag); - - // construct a temporary flag - template - void ConstructFlag(T1 Flags::*t1, const std::string &flagName, const std::string &helpInfo, FlagInfo *flag); - - Option InnerParseFlags(std::multimap> *values); - - bool GetRealFlagName(const std::string &oriFlagName, std::string *flagName); - - std::map flags; -}; - -// convert to std::string -template -Option ConvertToString(T Flags::*t, const FlagParser &baseFlag) { - const Flags *flag = dynamic_cast(&baseFlag); - if (flag != nullptr) { - return std::to_string(flag->*t); - } - - return Option(None()); -} - -// construct for a Option-type flag -template -void FlagParser::ConstructFlag(Option Flags::*t1, const std::string &flagName, const std::string &helpInfo, - FlagInfo *flag) { - if (flag == nullptr) { - MS_LOGE("FlagInfo is nullptr"); - return; - } - flag->flagName = flagName; - flag->helpInfo = helpInfo; - flag->isBoolean = typeid(T) == typeid(bool); - flag->isParsed = false; -} - -// construct a temporary flag -template -void FlagParser::ConstructFlag(T Flags::*t1, const std::string &flagName, const std::string &helpInfo, FlagInfo *flag) { - if (flag == nullptr) { - MS_LOGE("FlagInfo is nullptr"); - return; - } - if (t1 == nullptr) { - MS_LOGE("t1 is nullptr"); - return; - } - flag->flagName = flagName; - flag->helpInfo = helpInfo; - flag->isBoolean = typeid(T) == typeid(bool); - flag->isParsed = false; -} - -inline void FlagParser::AddFlag(const FlagInfo &flagItem) { flags[flagItem.flagName] = flagItem; } - -template -void FlagParser::AddFlag(T Flags::*t, const std::string &flagName, const std::string &helpInfo) { - if (t == nullptr) { - MS_LOGE("t1 is nullptr"); - return; - } - - Flags *flag = dynamic_cast(this); - if (flag == nullptr) { - MS_LOGI("dynamic_cast failed"); - return; - } - - FlagInfo flagItem; - - // flagItem is as a output parameter - ConstructFlag(t, flagName, helpInfo, &flagItem); - flagItem.parse = [t](FlagParser *base, const std::string &value) -> Option { - Flags *flag = dynamic_cast(base); - if (base != nullptr) { - Option ret = Option(GenericParseValue(value)); - if (ret.IsNone()) { - return Option(None()); - } else { - flag->*t = ret.Get(); - } - } - - return Option(Nothing()); - }; - - flagItem.isRequired = true; - flagItem.helpInfo += - !helpInfo.empty() && helpInfo.find_last_of("\n\r") != helpInfo.size() - 1 ? " (default: " : "(default: "; - flagItem.helpInfo += ")"; - - // add this flag to a std::map - AddFlag(flagItem); -} - -template -void FlagParser::AddFlag(T1 *t1, const std::string &flagName, const std::string &helpInfo, const T2 &t2) { - if (t1 == nullptr) { - MS_LOGE("t1 is nullptr"); - return; - } - - FlagInfo flagItem; - - // flagItem is as a output parameter - ConstructFlag(t1, flagName, helpInfo, flagItem); - flagItem.parse = [t1](FlagParser *base, const std::string &value) -> Option { - if (base != nullptr) { - Option ret = Option(GenericParseValue(value)); - if (ret.IsNone()) { - return Option(None()); - } else { - *t1 = ret.Get(); - } - } - - return Option(Nothing()); - }; - - flagItem.isRequired = false; - *t1 = t2; - - flagItem.helpInfo += - !helpInfo.empty() && helpInfo.find_last_of("\n\r") != helpInfo.size() - 1 ? " (default: " : "(default: "; - flagItem.helpInfo += ToString(t2).Get(); - flagItem.helpInfo += ")"; - - // add this flag to a std::map - AddFlag(flagItem); -} - -template -void FlagParser::AddFlag(T1 Flags::*t1, const std::string &flagName, const std::string &helpInfo, const T2 &t2) { - if (t1 == nullptr) { - MS_LOGE("t1 is nullptr"); - return; - } - - Flags *flag = dynamic_cast(this); - if (flag == nullptr) { - MS_LOGI("dynamic_cast failed"); - return; - } - - FlagInfo flagItem; - - // flagItem is as a output parameter - ConstructFlag(t1, flagName, helpInfo, &flagItem); - flagItem.parse = [t1](FlagParser *base, const std::string &value) -> Option { - Flags *flag = dynamic_cast(base); - if (base != nullptr) { - Option ret = Option(GenericParseValue(value)); - if (ret.IsNone()) { - return Option(None()); - } else { - flag->*t1 = ret.Get(); - } - } - - return Option(Nothing()); - }; - - flagItem.isRequired = false; - flag->*t1 = t2; - - flagItem.helpInfo += - !helpInfo.empty() && helpInfo.find_last_of("\n\r") != helpInfo.size() - 1 ? " (default: " : "(default: "; - flagItem.helpInfo += ToString(t2).Get(); - flagItem.helpInfo += ")"; - - // add this flag to a std::map - AddFlag(flagItem); -} - -// option-type add flag -template -void FlagParser::AddFlag(Option Flags::*t, const std::string &flagName, const std::string &helpInfo) { - if (t == nullptr) { - MS_LOGE("t is nullptr"); - return; - } - - Flags *flag = dynamic_cast(this); - if (flag == nullptr) { - MS_LOGE("dynamic_cast failed"); - return; - } - - FlagInfo flagItem; - // flagItem is as a output parameter - ConstructFlag(t, flagName, helpInfo, &flagItem); - flagItem.isRequired = false; - flagItem.parse = [t](FlagParser *base, const std::string &value) -> Option { - Flags *flag = dynamic_cast(base); - if (base != nullptr) { - Option ret = Option(GenericParseValue(value)); - if (ret.IsNone()) { - return Option(None()); - } else { - flag->*t = Option(Some(ret.Get())); - } - } - - return Option(Nothing()); - }; - - // add this flag to a std::map - AddFlag(flagItem); -} -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_COMMON_FLAG_PARSER_H_ diff --git a/predict/common/func_utils.cc b/predict/common/func_utils.cc deleted file mode 100644 index d2aeb8d9418..00000000000 --- a/predict/common/func_utils.cc +++ /dev/null @@ -1,77 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "common/func_utils.h" - -namespace mindspore { -namespace predict { -#if MS_USE_ARM -_Unwind_Reason_Code PrintTraceArm(_Unwind_Context *ctx, void *d) { - MS_ASSERT(ctx != nullptr); - MS_ASSERT(d != nullptr); - Dl_info info; - int *depth = static_cast(d); - auto ipAddr = static_cast(_Unwind_GetIP(ctx)); - if (dladdr(reinterpret_cast(ipAddr), &info)) { - const char *symbol = ""; - const char *dlfile = ""; - if (info.dli_sname) { - symbol = info.dli_sname; - } - if (info.dli_fname) { - dlfile = info.dli_fname; - } - MS_PRINT_ERROR("#%d: (%08lx) %s %s ", *depth, ipAddr, dlfile, symbol); - } - - (*depth)++; - return _URC_NO_REASON; -} -#endif - -void CoreDumpTraceFunc(int iSignum) { - MS_PRINT_ERROR("----- start get backtrace info -----"); -#if MS_USE_ARM - int depth = 0; - _Unwind_Backtrace(&PrintTraceArm, &depth); -#else - const auto maxDeep = 32; - const auto maxStringLen = 100; - void *apBuffer[maxStringLen]; - char **ppStrings; - - auto iStackDepth = backtrace(apBuffer, maxDeep); - if (0 > iStackDepth) { - KillProcess("Get backtrace depth failed"); - return; - } - MS_PRINT_ERROR("Current stack depth is %d", iStackDepth); - ppStrings = backtrace_symbols(apBuffer, iStackDepth); - if (nullptr == ppStrings) { - KillProcess("Get backtrace_symbols failed"); - return; - } - - for (int iLoop = 0; iLoop < iStackDepth; iLoop++) { - MS_PRINT_ERROR("%s \n", ppStrings[iLoop]); - } -#endif - MS_PRINT_ERROR("----- finish get backtrace info -----"); - KillProcess("Exit after core dump"); - return; // try exit 1 -} -} // namespace predict -} // namespace mindspore diff --git a/predict/common/func_utils.h b/predict/common/func_utils.h deleted file mode 100644 index da0389a584f..00000000000 --- a/predict/common/func_utils.h +++ /dev/null @@ -1,35 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_FUNC_UTILS_H_ -#define PREDICT_COMMON_FUNC_UTILS_H_ - -#if MS_USE_ARM -#include -#include -#else -#include -#endif -#include "include/errorcode.h" -#include "common/mslog.h" - -namespace mindspore { -namespace predict { -void CoreDumpTraceFunc(int iSignum); -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_COMMON_FUNC_UTILS_H_ diff --git a/predict/common/graph_util.cc b/predict/common/graph_util.cc deleted file mode 100644 index 6394731bc6d..00000000000 --- a/predict/common/graph_util.cc +++ /dev/null @@ -1,167 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "common/graph_util.h" -#include -#include -#include "common/mslog.h" -#include "include/errorcode.h" - -namespace mindspore { -namespace predict { -OpGraph *OpGraph::Build(const SubGraphDef &subGraphDef) { - auto graph = std::unique_ptr(new OpGraph()); - if (graph == nullptr) { - MS_LOGE("malloc opgraph failed"); - return nullptr; - } - - auto nodeDefs = subGraphDef.nodes(); - if (nodeDefs == nullptr) { - MS_LOGE("nodeDefs from subGraphDef is nullptr"); - return nullptr; - } - - uint32_t opCount = nodeDefs->size(); - for (uint32_t i = 0; i < opCount; i++) { - auto nodeDef = nodeDefs->GetAs(i); - MS_ASSERT(nodeDef != nullptr); - auto ret = graph->AddEdge(*nodeDef, *nodeDefs); - if (ret != RET_OK) { - MS_LOGE("%s add edge failed. ret:%d", nodeDef->opDef()->name()->c_str(), ret); - return nullptr; - } - } - - return graph.release(); -} - -int OpGraph::AddEdge(const NodeDef &srcNodeDef, const flatbuffers::Vector> &nodeDefs) { - MS_ASSERT(srcNodeDef.opDef() != nullptr); - MS_ASSERT(srcNodeDef.opDef()->name() != nullptr); - NODE_ID srcId = std::string(srcNodeDef.opDef()->name()->c_str()); - uint32_t opCount = nodeDefs.size(); - - MS_ASSERT(srcNodeDef.opDef()->outputIndex() != nullptr); - for (auto index : *(srcNodeDef.opDef()->outputIndex())) { - for (uint32_t i = 0; i < opCount; i++) { - auto dstNodeDef = nodeDefs.GetAs(i); - bool find = false; - MS_ASSERT(dstNodeDef != nullptr); - MS_ASSERT(dstNodeDef->opDef() != nullptr); - auto inputIndex = dstNodeDef->opDef()->inputIndex(); - MS_ASSERT(inputIndex != nullptr); - if (std::any_of(inputIndex->begin(), inputIndex->end(), [&index](int i) { return i == index; })) { - find = true; - } - - if (!find) { - continue; - } - MS_ASSERT(dstNodeDef->opDef()->name() != nullptr); - NODE_ID dstId = std::string(dstNodeDef->opDef()->name()->c_str()); - auto ret = AddEdge(srcId, dstId); - if (ret != RET_OK) { - return ret; - } - } - } - - return RET_OK; -} - -int OpGraph::AddEdge(const NODE_ID &srcId, const NODE_ID &dstId) { - auto srcNode = AddNode(srcId); - if (srcNode == nullptr) { - MS_LOGE("add srcNode failed"); - return RET_ERROR; - } - srcNode->AddOutEdge(dstId); - auto dstNode = AddNode(dstId); - if (dstNode == nullptr) { - MS_LOGE("add dstNode failed"); - return RET_ERROR; - } - dstNode->AddInEdge(srcId); - return RET_OK; -} - -OpNode *OpGraph::GetNode(const NODE_ID &nodeId) { - auto node = nodes.find(nodeId); - if (node == nodes.end()) { - return nullptr; - } - return node->second; -} - -OpNode *OpGraph::AddNode(const NODE_ID &nodeId) { - auto node = GetNode(nodeId); - if (node != nullptr) { - return node; - } - node = new (std::nothrow) OpNode(nodeId); - if (node == nullptr) { - MS_LOGE("new node failed"); - return nullptr; - } - nodes[nodeId] = node; - return node; -} - -std::unordered_set OpGraph::GetInputNode() { - std::unordered_set inputNodes; - for (const auto &iter : nodes) { - auto node = iter.second; - MS_ASSERT(node != nullptr); - if (node->GetAllInEdge().empty()) { - inputNodes.insert(node->ID()); - } - } - return inputNodes; -} - -std::unordered_set OpGraph::GetOutputNode() { - std::unordered_set outputNodes; - for (const auto &iter : nodes) { - auto node = iter.second; - MS_ASSERT(node != nullptr); - if (node->GetAllOutEdge().empty()) { - outputNodes.insert(node->ID()); - } - } - return outputNodes; -} - -OpGraph::~OpGraph() { - for (auto iter : nodes) { - if (iter.second != nullptr) { - delete iter.second; - } - } - nodes.clear(); -} - -NODE_ID OpNode::ID() { return id; } - -void OpNode::AddInEdge(const NODE_ID &nodeId) { inEdges.insert(nodeId); } - -void OpNode::AddOutEdge(const NODE_ID &nodeId) { outEdges.insert(nodeId); } - -std::unordered_set OpNode::GetAllInEdge() { return inEdges; } - -std::unordered_set OpNode::GetAllOutEdge() { return outEdges; } -} // namespace predict -} // namespace mindspore diff --git a/predict/common/graph_util.h b/predict/common/graph_util.h deleted file mode 100644 index 9797edadf66..00000000000 --- a/predict/common/graph_util.h +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_GRAPH_UTIL_H_ -#define PREDICT_COMMON_GRAPH_UTIL_H_ - -#include -#include -#include -#include -#include -#include -#include "common/utils.h" -#include "schema/inner/ms_generated.h" - -namespace mindspore { -namespace predict { -using NODE_ID = std::string; - -class OpNode { - public: - explicit OpNode(NODE_ID nodeId) : id(std::move(nodeId)) {} - NODE_ID ID(); - void AddInEdge(const NODE_ID &nodeId); - void AddOutEdge(const NODE_ID &nodeId); - std::unordered_set GetAllInEdge(); - std::unordered_set GetAllOutEdge(); - - protected: - NODE_ID id; - std::unordered_set inEdges; - std::unordered_set outEdges; -}; - -class OpGraph { - public: - OpGraph() = default; - - ~OpGraph(); - - static OpGraph *Build(const SubGraphDef &subGraphDef); - - OpNode *GetNode(const NODE_ID &nodeId); - OpNode *AddNode(const NODE_ID &nodeId); - std::unordered_set GetInputNode(); - std::unordered_set GetOutputNode(); - - private: - int AddEdge(const NODE_ID &srcId, const NODE_ID &dstId); - int AddEdge(const NodeDef &srcNodeDef, const flatbuffers::Vector> &nodeDefs); - - protected: - std::unordered_map nodes; -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_COMMON_GRAPH_UTIL_H_ diff --git a/predict/common/module_registry.cc b/predict/common/module_registry.cc deleted file mode 100644 index da8992bb66f..00000000000 --- a/predict/common/module_registry.cc +++ /dev/null @@ -1,26 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "common/module_registry.h" - -namespace mindspore { -namespace predict { -ModuleRegistry *GetRegistryInstance() { - static ModuleRegistry registry; - return ®istry; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/common/module_registry.h b/predict/common/module_registry.h deleted file mode 100644 index 9d7587e74a7..00000000000 --- a/predict/common/module_registry.h +++ /dev/null @@ -1,97 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_MODULE_REGISTRY_H_ -#define PREDICT_COMMON_MODULE_REGISTRY_H_ -#include -#include -#include -#include "common/mslog.h" - -#define MSPREDICT_API __attribute__((visibility("default"))) - -namespace mindspore { -namespace predict { -class ModuleBase { - public: - virtual ~ModuleBase() = default; -}; - -template -class Module; - -class ModuleRegistry { - public: - ModuleRegistry() = default; - - virtual ~ModuleRegistry() = default; - - template - bool Register(const std::string &name, const T &t) { - modules[name] = &t; - return true; - } - - template - std::shared_ptr Create(const std::string &name) { - auto it = modules.find(name); - if (it == modules.end()) { - return nullptr; - } - auto *module = (Module *)it->second; - if (module == nullptr) { - return nullptr; - } else { - return module->Create(); - } - } - - template - T *GetInstance(const std::string &name) { - auto it = modules.find(name); - if (it == modules.end()) { - return nullptr; - } - auto *module = (Module *)it->second; - if (module == nullptr) { - return nullptr; - } else { - return module->GetInstance(); - } - } - - protected: - std::unordered_map modules; -}; - -ModuleRegistry *GetRegistryInstance() MSPREDICT_API; - -template -class ModuleRegistrar { - public: - ModuleRegistrar(const std::string &name, const T &module) { - auto registryInstance = GetRegistryInstance(); - if (registryInstance == nullptr) { - MS_LOGW("registryInstance is nullptr."); - } else { - registryInstance->Register(name, module); - } - } -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_COMMON_MODULE_REGISTRY_H_ diff --git a/predict/common/mslog.cc b/predict/common/mslog.cc deleted file mode 100644 index a1b61bbc3d6..00000000000 --- a/predict/common/mslog.cc +++ /dev/null @@ -1,47 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "common/mslog.h" -#include -#include -#include -#include -#include "include/errorcode.h" - -namespace mindspore { -namespace predict { -std::string GetEnv(const std::string &envvar) { - const char *value = std::getenv(envvar.c_str()); - if (value == nullptr) { - return std::string(); - } - return std::string(value); -} - -bool IsPrint(int level) { - auto envString = GetEnv("MSLOG"); - static int env = static_cast(std::strtol(!envString.empty() ? envString.c_str() : "3", nullptr, 0)); - if (env == INT_MIN || env == INT_MAX) { - env = WARN; - // enable the SP for binscope checking - std::string errorStr = "env exceeded the value that type int is able to represent"; - MS_LOGE("%s", errorStr.c_str()); - } - - return level >= env; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/common/mslog.h b/predict/common/mslog.h deleted file mode 100644 index a48d87f1fa7..00000000000 --- a/predict/common/mslog.h +++ /dev/null @@ -1,230 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_MSLOG_H_ -#define PREDICT_COMMON_MSLOG_H_ - -#include -#include -#include -#include -#include -#include - -#if defined(__ANDROID__) || defined(ANDROID) -#include -#endif -namespace mindspore { -namespace predict { -constexpr const char *TAG = "MS_PREDICT"; - -constexpr int DEBUG = 1; -constexpr int INFO = 2; -constexpr int WARN = 3; -constexpr int ERROR = 4; - -#define MSPREDICT_API __attribute__((visibility("default"))) - -bool MSPREDICT_API IsPrint(int level); - -#if !defined(__ANDROID__) && !defined(ANDROID) - -#if LOG_TO_FILE -#define MS_LOGD(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::DEBUG)) { \ - syslog(LOG_DEBUG, "%s|%d|%s[%d]|: " #fmt, mindspore::predict::TAG, \getpid(), __func__, __LINE__, ##args); \ - } \ - } -#define MS_LOGI(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::INFO)) { \ - syslog(LOG_INFO, "%s|%d|%s[%d]|: " #fmt, mindspore::predict::TAG, \getpid(), __func__, __LINE__, ##args); \ - } \ - } -#define MS_LOGW(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::WARN)) { \ - syslog(LOG_WARNING, "%s|%d|%s[%d]|: " #fmt, mindspore::predict::TAG, \getpid(), __func__, __LINE__, ##args); \ - } \ - } -#define MS_LOGE(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::ERROR)) { \ - syslog(LOG_ERR, "%s|%d|%s[%d]|: " #fmt, mindspore::predict::TAG, getpid(), __func__, __LINE__, ##args); \ - } \ - } -#else - -#define MS_LOGD(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::DEBUG)) { \ - printf("[DEBUG] %s|%d|%s|%s[%d]|: " #fmt "\r\n", mindspore::predict::TAG, getpid(), __FILE__, __func__, \ - __LINE__, ##args); \ - } \ - } -#define MS_LOGI(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::INFO)) { \ - printf("[INFO] %s|%d|%s|%s[%d]|: " #fmt "\r\n", mindspore::predict::TAG, getpid(), __FILE__, __func__, \ - __LINE__, ##args); \ - } \ - } -#define MS_LOGW(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::WARN)) { \ - printf("[WARN] %s|%d|%s|%s[%d]|: " #fmt "\r\n", mindspore::predict::TAG, getpid(), __FILE__, __func__, \ - __LINE__, ##args); \ - } \ - } -#define MS_LOGE(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::ERROR)) { \ - printf("[ERROR] %s|%d|%s|%s[%d]|: " #fmt "\r\n", mindspore::predict::TAG, getpid(), __FILE__, __func__, \ - __LINE__, ##args); \ - } \ - } -#endif - -#else - -#define MS_LOGD(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::DEBUG)) \ - __android_log_print(ANDROID_LOG_DEBUG, mindspore::predict::TAG, "|%d|%s[%d]|: " fmt, getpid(), __func__, \ - __LINE__, ##args); \ - } - -#define MS_LOGI(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::INFO)) \ - __android_log_print(ANDROID_LOG_INFO, mindspore::predict::TAG, "|%d|%s[%d]|: " fmt, getpid(), __func__, \ - __LINE__, ##args); \ - } - -#define MS_LOGW(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::WARN)) \ - __android_log_print(ANDROID_LOG_WARN, mindspore::predict::TAG, "|%d|%s[%d]|: " fmt, getpid(), __func__, \ - __LINE__, ##args); \ - } - -#define MS_LOGE(fmt, args...) \ - { \ - if (mindspore::predict::IsPrint(mindspore::predict::ERROR)) \ - __android_log_print(ANDROID_LOG_ERROR, mindspore::predict::TAG, "|%d|%s[%d]|: " fmt, getpid(), __func__, \ - __LINE__, ##args); \ - } - -#endif - -#define MS_LOG(severity) std::cout << std::endl -#define MS_DLOG(verboselevel) std::cout << std::endl -// Kill the process for safe exiting. -inline void KillProcess(const std::string &ret) { - MS_LOG(ERROR) << "mindspore Exit Tip:" << ret; - if (raise(SIGKILL) != 0) { - MS_LOGE("Send SIGKILL to kill process failed"); - } -} -} // namespace predict -} // namespace mindspore - -#define MS_ASSERT(expression) \ - do { \ - if (!(expression)) { \ - std::stringstream ss; \ - ss << "Assertion failed: " << #expression << ", file: " << __FILE__ << ", line: " << __LINE__; \ - mindspore::predict::KillProcess(ss.str()); \ - } \ - } while (0) - -#define MS_EXIT(ret) \ - do { \ - std::stringstream ss; \ - ss << (ret) << " ( file: " << __FILE__ << ", line: " << __LINE__ << " )."; \ - mindspore::predict::KillProcess(ss.str()); \ - } while (0) - -#define MS_PRINT_ERROR(fmt, args...) \ - printf(#fmt "\n", ##args); \ - MS_LOGE(fmt, ##args); - -#define MS_PRINT_INFO(fmt, args...) \ - printf(fmt "\n", ##args); \ - MS_LOGI(fmt, ##args); - -constexpr int LOG_CHECK_EVERY_FIRSTNUM = 10; -constexpr int LOG_CHECK_EVERY_NUM1 = 10; -constexpr int LOG_CHECK_EVERY_NUM2 = 100; -constexpr int LOG_CHECK_EVERY_NUM3 = 1000; -constexpr int LOG_CHECK_EVERY_NUM4 = 10000; - -#define LOG_CHECK_ID_CONCAT(word1, word2) word1##word2 - -#define LOG_CHECK_ID LOG_CHECK_ID_CONCAT(__FUNCTION__, __LINE__) - -#define LOG_CHECK_FIRST_N \ - [](uint32_t firstNum) { \ - static uint32_t LOG_CHECK_ID = 0; \ - ++LOG_CHECK_ID; \ - return (LOG_CHECK_ID <= firstNum); \ - } - -#define LOG_CHECK_EVERY_N1 \ - [](uint32_t firstNum, uint32_t num) { \ - static uint32_t LOG_CHECK_ID = 0; \ - ++LOG_CHECK_ID; \ - return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID % num == 0)); \ - } - -#define LOG_CHECK_EVERY_N2 \ - [](uint32_t firstNum, uint32_t num1, uint32_t num2) { \ - static uint32_t LOG_CHECK_ID = 0; \ - ++LOG_CHECK_ID; \ - return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \ - (LOG_CHECK_ID % num2 == 0)); \ - } - -#define LOG_CHECK_EVERY_N3 \ - [](uint32_t firstNum, uint32_t num1, uint32_t num2, uint32_t num3) { \ - static uint32_t LOG_CHECK_ID = 0; \ - ++LOG_CHECK_ID; \ - return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \ - (LOG_CHECK_ID < num3 && LOG_CHECK_ID % num2 == 0) || (LOG_CHECK_ID % num3 == 0)); \ - } - -#define LOG_CHECK_EVERY_N4 \ - [](uint32_t firstNum, uint32_t num1, uint32_t num2, uint32_t num3, uint32_t num4) { \ - static uint32_t LOG_CHECK_ID = 0; \ - ++LOG_CHECK_ID; \ - return ((LOG_CHECK_ID <= firstNum) || (LOG_CHECK_ID < num2 && LOG_CHECK_ID % num1 == 0) || \ - (LOG_CHECK_ID < num3 && LOG_CHECK_ID % num2 == 0) || (LOG_CHECK_ID < num4 && LOG_CHECK_ID % num3 == 0) || \ - (LOG_CHECK_ID % num4 == 0)); \ - } - -#define LOG_CHECK_EVERY_N \ - []() { \ - static uint32_t LOG_CHECK_ID = 0; \ - ++LOG_CHECK_ID; \ - return ((LOG_CHECK_ID <= LOG_CHECK_EVERY_FIRSTNUM) || \ - (LOG_CHECK_ID < LOG_CHECK_EVERY_NUM2 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM1 == 0) || \ - (LOG_CHECK_ID < LOG_CHECK_EVERY_NUM3 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM2 == 0) || \ - (LOG_CHECK_ID < LOG_CHECK_EVERY_NUM4 && LOG_CHECK_ID % LOG_CHECK_EVERY_NUM3 == 0) || \ - (LOG_CHECK_ID % LOG_CHECK_EVERY_NUM4 == 0)); \ - } - -#endif // PREDICT_COMMON_MSLOG_H_ diff --git a/predict/common/op_utils.h b/predict/common/op_utils.h deleted file mode 100644 index 35f01edce39..00000000000 --- a/predict/common/op_utils.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_OP_UTILS_H_ -#define PREDICT_COMMON_OP_UTILS_H_ - -#include -#include -#include "schema/inner/ms_generated.h" - -namespace mindspore { -namespace predict { -inline OpT GetOpType(const OpDef &opDef) { return opDef.attr_type(); } - -inline OpT GetOpType(const NodeDef &nodeDef) { return GetOpType(*(nodeDef.opDef())); } - -inline std::string GetOpTypeName(const NodeDef &nodeDef) { return EnumNameOpT(GetOpType(nodeDef)); } - -inline std::string GetOpTypeName(const OpDef &opDef) { return EnumNameOpT(GetOpType(opDef)); } - -inline OpT GetOpType(const OpDefT &opDefT) { return opDefT.attr.type; } - -inline OpT GetOpType(const NodeDefT &nodeDefT) { return GetOpType(*(nodeDefT.opDef.get())); } - -inline std::string GetOpTypeName(const NodeDefT &nodeDefT) { return EnumNameOpT(GetOpType(nodeDefT)); } - -inline std::string GetOpTypeName(const OpDefT &opDefT) { return EnumNameOpT(GetOpType(opDefT)); } -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_COMMON_OP_UTILS_H_ diff --git a/predict/common/option.h b/predict/common/option.h deleted file mode 100644 index ca72dde29b2..00000000000 --- a/predict/common/option.h +++ /dev/null @@ -1,119 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_OPTION_H_ -#define PREDICT_COMMON_OPTION_H_ - -#include -#include -#include "common/mslog.h" - -namespace mindspore { -namespace predict { -template -struct InnerSome { - explicit InnerSome(const T &t) : _t(std::move(t)) {} - - T _t; -}; - -template -InnerSome::type> Some(T &&t) { - return InnerSome::type>(std::forward(t)); -} - -struct None {}; - -template -class Option { - public: - Option() : state(NONE) {} - - explicit Option(const T &t) : data(t), state(SOME) {} - - explicit Option(T &&t) : data(std::move(t)), state(SOME) {} - - explicit Option(const InnerSome &some) : data(some._t), state(SOME) {} - - explicit Option(const None &none) : state(NONE) {} - - Option(const Option &that) : state(that.state) { - if (that.IsSome()) { - new (&data) T(that.data); - } - } - - virtual ~Option() = default; - - bool IsNone() const { return state == NONE; } - - bool IsSome() const { return state == SOME; } - - const T &Get() const & { - MS_ASSERT(IsSome()); - return data; - } - - T &Get() & { - MS_ASSERT(IsSome()); - return data; - } - - T &&Get() && { - MS_ASSERT(IsSome()); - return std::move(data); - } - - const T &&Get() const && { - MS_ASSERT(IsSome()); - return std::move(data); - } - - // oprerator override - Option &operator=(const Option &that) { - if (&that != this) { - if (IsSome()) { - data.~T(); - } - state = that.state; - if (that.IsSome()) { - new (&data) T(that.data); - } - } - - return *this; - } - - bool operator==(const Option &that) const { - return (IsNone() && that.IsNone()) || (IsSome() && that.IsSome() && data == that.data); - } - - bool operator!=(const Option &that) const { return !(*this == that); } - - bool operator==(const T &that) const { return IsSome() && data == that; } - - bool operator!=(const T &that) const { return !(*this == that); } - - private: - enum State { NONE = 0, SOME = 1 }; - - T data; - State state; -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_COMMON_OPTION_H_ diff --git a/predict/common/storage.cc b/predict/common/storage.cc deleted file mode 100644 index ade5861c746..00000000000 --- a/predict/common/storage.cc +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "common/storage.h" -#include "flatbuffers/flatbuffers.h" -#include "common/mslog.h" -#include "common/file_utils.h" - -namespace mindspore { -namespace predict { -int Storage::Save(const GraphDefT &graph, const std::string &outputPath) { - flatbuffers::FlatBufferBuilder builder(flatSize); - auto offset = GraphDef::Pack(builder, &graph); - builder.Finish(offset); - int size = builder.GetSize(); - auto content = builder.GetBufferPointer(); - if (content == nullptr) { - MS_LOGE("GetBufferPointer nullptr"); - return RET_ERROR; - } - std::string realPath = RealPath(outputPath.c_str()); - if (realPath.empty()) { - MS_LOGE("Output file path '%s' is not valid", outputPath.c_str()); - return RET_ERROR; - } - - std::ofstream output(realPath, std::ofstream::binary); - if (!output.is_open()) { - MS_LOGE("ofstream open failed"); - return RET_ERROR; - } - output.write((const char *)content, size); - output.close(); - return RET_OK; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/common/storage.h b/predict/common/storage.h deleted file mode 100644 index fc612ffb6b7..00000000000 --- a/predict/common/storage.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_STORAGE_H_ -#define PREDICT_COMMON_STORAGE_H_ - -#include -#include -#include "include/errorcode.h" -#include "flatbuffers/flatbuffers.h" -#include "schema/inner/ms_generated.h" - -namespace mindspore { -namespace predict { -class Storage { - public: - int Save(const GraphDefT &graph, const std::string &outputPath); - const int flatSize = 1024; -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_COMMON_STORAGE_H_ diff --git a/predict/common/utils.cc b/predict/common/utils.cc deleted file mode 100644 index b186a4b8d84..00000000000 --- a/predict/common/utils.cc +++ /dev/null @@ -1,228 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "common/utils.h" - -namespace mindspore { -namespace predict { -uint64_t GetTimeUs() { - struct timespec ts = {0, 0}; - if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) { - return 0; - } - // USECS_IN_SEC *NSECS_IN_USEC; - auto retval = static_cast((ts.tv_sec * USEC) + (ts.tv_nsec / MSEC)); - return retval; -} - -static const unsigned int FP32_BIT_SIZE = 32; -static const unsigned int FP32_EXPONENT_BIAS = 127; -static const unsigned int FP32_SIGNIFICAND = 23; - -static const unsigned int FP32_EXPONENT_MAX = 255; - -static const unsigned int FP16_BIT_SIZE = 16; -static const unsigned int FP16_EXPONENT_BIAS = 15; -static const unsigned int FP16_SIGNIFICAND = 10; - -static const int FP16_EXPONENT_MAX = 30; -static const int FP16_EXPONENT_MIN = -10; - -float ShortToFloat32(int16_t srcValue) { - uint16_t expHalf16 = srcValue & 0x7C00; - int exp1 = static_cast(expHalf16); - uint16_t mantissa16 = srcValue & 0x03FF; - int mantissa1 = static_cast(mantissa16); - int sign = static_cast(srcValue & 0x8000); - sign = sign << FP16_BIT_SIZE; - - // nan or inf - if (expHalf16 == 0x7C00) { - // nan - if (mantissa16 > 0) { - int res = (0x7FC00000 | sign); - int *iRes = &res; - MS_ASSERT(iRes != nullptr); - auto fres = static_cast(*iRes); - return fres; - } - // inf - int res = (0x7F800000 | sign); - int *iRes = &res; - MS_ASSERT(iRes != nullptr); - auto fres = static_cast(*iRes); - return fres; - } - if (expHalf16 != 0) { - exp1 += ((FP32_EXPONENT_BIAS - FP16_EXPONENT_BIAS) << FP16_SIGNIFICAND); // exponents converted to float32 bias - int res = (exp1 | mantissa1); - res = res << (FP32_SIGNIFICAND - FP16_SIGNIFICAND); - res = (res | sign); - int *iRes = &res; - - auto fres = static_cast(*iRes); - return fres; - } - - int xmm1 = exp1 > (1 << FP16_SIGNIFICAND) ? exp1 : (1 << FP16_SIGNIFICAND); - xmm1 = (xmm1 << (FP32_SIGNIFICAND - FP16_SIGNIFICAND)); - xmm1 += ((FP32_EXPONENT_BIAS - FP16_EXPONENT_BIAS - FP16_SIGNIFICAND) - << FP32_SIGNIFICAND); // add the bias difference to xmm1 - xmm1 = xmm1 | sign; // Combine with the sign mask - - auto res = static_cast(mantissa1); // Convert mantissa to float - res *= static_cast(xmm1); - - return res; -} - -int16_t Float32ToShort(float srcValue) { - auto srcValueBit = static_cast(srcValue); - int sign = srcValueBit >> (FP32_BIT_SIZE - 1); - int mantissa = srcValueBit & 0x007FFFFF; - // exponent - int exp = ((srcValueBit & 0x7F800000) >> FP32_SIGNIFICAND) + FP16_EXPONENT_BIAS - FP32_EXPONENT_BIAS; - int16_t res; - if (exp > 0 && exp < FP16_EXPONENT_MAX) { - // use rte rounding mode, round the significand, combine sign, exponent and significand into a short. - res = (sign << (FP16_BIT_SIZE - 1)) | (exp << FP16_SIGNIFICAND) | - ((mantissa + 0x00001000) >> (FP32_SIGNIFICAND - FP16_SIGNIFICAND)); - } else if (srcValueBit == 0) { - res = 0; - } else { - if (exp <= 0) { - if (exp < FP16_EXPONENT_MIN) { - // value is less than min half float point - res = 0; - } else { - // normalized single, magnitude is less than min normal half float point. - mantissa = (mantissa | 0x00800000) >> (1 - exp); - // round to nearest - if ((mantissa & 0x00001000) > 0) { - mantissa = mantissa + 0x00002000; - } - // combine sign & mantissa (exp is zero to get denormalized number) - res = (sign << FP16_EXPONENT_BIAS) | (mantissa >> (FP32_SIGNIFICAND - FP16_SIGNIFICAND)); - } - } else if (exp == (FP32_EXPONENT_MAX - FP32_EXPONENT_BIAS + FP16_EXPONENT_BIAS)) { - if (mantissa == 0) { - // input float is infinity, return infinity half - res = (sign << FP16_EXPONENT_BIAS) | 0x7C00; - } else { - // input float is NaN, return half NaN - res = (sign << FP16_EXPONENT_BIAS) | 0x7C00 | (mantissa >> (FP32_SIGNIFICAND - FP16_SIGNIFICAND)); - } - } else { - // exp > 0, normalized single, round to nearest - if ((mantissa & 0x00001000) > 0) { - mantissa = mantissa + 0x00002000; - if ((mantissa & 0x00800000) > 0) { - mantissa = 0; - exp = exp + 1; - } - } - if (exp > FP16_EXPONENT_MAX) { - // exponent overflow - return infinity half - res = (sign << FP16_EXPONENT_BIAS) | 0x7C00; - } else { - // combine sign, exp and mantissa into normalized half - res = (sign << FP16_EXPONENT_BIAS) | (exp << FP16_SIGNIFICAND) | - (mantissa >> (FP32_SIGNIFICAND - FP16_SIGNIFICAND)); - } - } - } - return res; -} -std::string Remove(const std::string &from, const std::string &subStr, Mode mode) { - std::string result = from; - if (mode == PREFIX) { - if (from.substr(0, subStr.length()) == subStr) { - result = from.substr(subStr.size()); - } - } else if (mode == SUFFIX) { - if (from.rfind(subStr) == from.size() - subStr.size()) { - result = from.substr(0, from.size() - subStr.size()); - } - } else { - size_t index; - while ((index = result.find(subStr)) != std::string::npos) { - result = result.erase(index, subStr.size()); - } - } - - return result; -} - -std::vector StrSplit(const std::string &str, const std::string &pattern) { - std::string::size_type pos; - std::vector result; - std::string tmpStr(str + pattern); - std::string::size_type size = tmpStr.size(); - - for (std::string::size_type i = 0; i < size; i++) { - pos = tmpStr.find(pattern, i); - if (pos < size) { - std::string s = tmpStr.substr(i, pos - i); - result.push_back(s); - i = pos + pattern.size() - 1; - } - } - return result; -} - -std::vector Tokenize(const std::string &src, const std::string &delimiters, - const Option &maxTokenNum) { - if (maxTokenNum.IsSome() && maxTokenNum.Get() == 0) { - return {}; - } - - std::vector tokens; - size_t offset = 0; - - while (true) { - size_t nonDelimiter = src.find_first_not_of(delimiters, offset); - if (nonDelimiter == std::string::npos) { - break; - } - size_t delimiter = src.find_first_of(delimiters, nonDelimiter); - if (delimiter == std::string::npos || (maxTokenNum.IsSome() && tokens.size() == maxTokenNum.Get() - 1)) { - tokens.push_back(src.substr(nonDelimiter)); - break; - } - - tokens.push_back(src.substr(nonDelimiter, delimiter - nonDelimiter)); - offset = delimiter; - } - return tokens; -} - -void ShortToFloat32(const int16_t *srcdata, float *dstdata, size_t elementSize) { - MS_ASSERT(srcdata != nullptr); - MS_ASSERT(dstdata != nullptr); - for (size_t i = 0; i < elementSize; i++) { - dstdata[i] = ShortToFloat32(srcdata[i]); - } -} - -void Float32ToShort(const float *srcdata, int16_t *dstdata, size_t elementSize) { - MS_ASSERT(srcdata != nullptr); - MS_ASSERT(dstdata != nullptr); - for (size_t i = 0; i < elementSize; i++) { - dstdata[i] = Float32ToShort(srcdata[i]); - } -} -} // namespace predict -} // namespace mindspore diff --git a/predict/common/utils.h b/predict/common/utils.h deleted file mode 100644 index e7d44fe9820..00000000000 --- a/predict/common/utils.h +++ /dev/null @@ -1,154 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_COMMON_UTILS_H_ -#define PREDICT_COMMON_UTILS_H_ - -#include -#include -#include -#include -#include -#include "common/mslog.h" -#include "common/option.h" -#include "include/errorcode.h" - -namespace mindspore { -namespace predict { -const int USEC = 1000000; -const int MSEC = 1000; - -uint64_t GetTimeUs(); - -int16_t Float32ToShort(float srcValue); - -float ShortToFloat32(int16_t srcValue); - -void ShortToFloat32(const int16_t *srcData, float *dstData, size_t elementSize); - -void Float32ToShort(const float *srcData, int16_t *dstData, size_t elementSize); - -template -bool IsContain(const std::vector &vec, T element) { - for (auto iter = vec.begin(); iter != vec.end(); iter++) { - if (*iter == element) { - return true; - } - } - return false; -} - -const char WHITESPACE[] = "\t\n\v\f\r "; -const char STR_TRUE[] = "true"; -const char STR_FALSE[] = "false"; - -template -Option ToString(T t) { - std::ostringstream out; - out << t; - if (!out.good()) { - return Option(None()); - } - - return Option(out.str()); -} - -template <> -inline Option ToString(bool value) { - return value ? Option(STR_TRUE) : Option(STR_FALSE); -} - -// get the file name from a given path -// for example: "/usr/bin", we will get "bin" -inline std::string GetFileName(const std::string &path) { - char delim = '/'; - - size_t i = path.rfind(delim, path.length()); - if (i != std::string::npos) { - return (path.substr(i + 1, path.length() - i)); - } - - return ""; -} - -// trim the white space character in a string -// see also: macro WHITESPACE defined above -inline void Trim(std::string *input) { - if (input == nullptr) { - return; - } - if (input->empty()) { - return; - } - - input->erase(0, input->find_first_not_of(WHITESPACE)); - input->erase(input->find_last_not_of(WHITESPACE) + 1); -} - -// to judge whether a string is starting with prefix -// for example: "hello world" is starting with "hello" -inline bool StartsWithPrefix(const std::string &source, const std::string &prefix) { - if (source.length() < prefix.length()) { - return false; - } - - return (source.compare(0, prefix.length(), prefix) == 0); -} - -// split string -std::vector StrSplit(const std::string &str, const std::string &pattern); - -// tokenize string -std::vector Tokenize(const std::string &src, const std::string &delimiters, - const Option &maxTokenNum = Option(None())); - -enum Mode { PREFIX, SUFFIX, ANY }; - -// remove redundant character -std::string Remove(const std::string &from, const std::string &subStr, Mode mode = ANY); - -template -inline Option GenericParseValue(const std::string &value) { - T ret; - std::istringstream input(value); - input >> ret; - - if (input && input.eof()) { - return Option(ret); - } - - return Option(None()); -} - -template <> -inline Option GenericParseValue(const std::string &value) { - return Option(value); -} - -template <> -inline Option GenericParseValue(const std::string &value) { - if (value == "true") { - return Option(true); - } else if (value == "false") { - return Option(false); - } - - return Option(None()); -} -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_COMMON_UTILS_H_ diff --git a/predict/include/context.h b/predict/include/context.h deleted file mode 100644 index cb30f1f8c29..00000000000 --- a/predict/include/context.h +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_INCLUDE_CONTEXT_H_ -#define PREDICT_INCLUDE_CONTEXT_H_ - -#include -#include "dlpack/dlpack.h" -#include "include/tensor.h" - -#define MSPREDICT_API __attribute__((visibility("default"))) - -namespace mindspore { -namespace predict { -///\brief Resource management definition of MindSpore predict. -class MSPREDICT_API Context { - public: - ///\brief Constructor of MindSpore predict context using default value for parameters. - /// - ///\return Instance of MindSpore predict context. - Context(); - - ///\brief Custum constructor of MindSpore predict context using input value for parameters. - /// - ///\param[in] threadNum The number of thread during the runtime. - ///\param[in] allocator The memory management during the runtime - ///\param[in] deviceCtx The device information during the runtime. - /// - ///\return Instance of MindSpore predict context. - Context(int threadNum, std::shared_ptr allocator, DLContext deviceCtx); - - ///\brief Destructor of MindSpore predict context. - virtual ~Context(); - - public: - DLContext deviceCtx; - int threadNum = 1; - std::shared_ptr allocator; -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_INCLUDE_CONTEXT_H_ diff --git a/predict/include/errorcode.h b/predict/include/errorcode.h deleted file mode 100755 index 5487673f162..00000000000 --- a/predict/include/errorcode.h +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_INCLUDE_ERRORCODE_H_ -#define PREDICT_INCLUDE_ERRORCODE_H_ - -namespace mindspore { -namespace predict { -using STATUS = int; - -/* Success */ -constexpr int RET_OK = 0; /**< No error occurs. */ - -/* Common error code, range: [-1, -100]*/ -constexpr int RET_ERROR = -1; /**< Common error code. */ -constexpr int RET_NULL_PTR = -2; /**< NULL pointer returned.*/ -constexpr int RET_PARAM_INVALID = -3; /**< Invalid parameter.*/ -constexpr int RET_NO_CHANGE = -4; /**< No change. */ - -/* Executor error code, range: [-101,-200] */ -constexpr int RET_OUT_OF_TENSOR_RANGE = -101; /**< Failed to checking range. */ -constexpr int RET_INPUT_TENSOR_ERROR = -102; /**< Failed to checking input tensor. */ -constexpr int RET_REENTRANT_ERROR = -103; /**< Exist executor running. */ - -/* Graph error code, range: [-201,-300] */ -constexpr int RET_GRAPH_FILE_ERR = -201; /**< Failed to verify graph file. */ - -/* Node error code, range: [-301,-400] */ -constexpr int RET_NOT_FIND_OP = -301; /**< Failed to find OP. */ -constexpr int RET_INVALID_OP_NAME = -302; /**< Invalid OP name. */ -constexpr int RET_INVALID_OP_ATTR = -303; /**< Invalid OP attr. */ -constexpr int RET_OP_EXECUTE_FAILURE = -304; /**< Failed to execution OP. */ - -/* Tensor error code, range: [-401,-500] */ -constexpr int RET_FORMAT_ERR = -401; /**< Failed to checking tensor format. */ -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_INCLUDE_ERRORCODE_H_ diff --git a/predict/include/session.h b/predict/include/session.h deleted file mode 100644 index 76fa2c4d6e2..00000000000 --- a/predict/include/session.h +++ /dev/null @@ -1,139 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_INCLUDE_SESSION_H_ -#define PREDICT_INCLUDE_SESSION_H_ - -#include -#include -#include -#include -#include -#include "include/context.h" -#include "include/tensor.h" - -#define MSPREDICT_API __attribute__((visibility("default"))) - -namespace mindspore { -namespace predict { -using NODE_ID = std::string; - -///\brief Graph defined by MindSpore predict. -/// -///\note -/// The caller does not need to care about detailed implementation of this class, so just list the class name here. -class Graph; - -///\brief GraphExecution defined by MindSpore predict. -/// -///\note -/// The caller does not need to care about detailed implementation of this class, so just list the class name here. -class GraphExecution; - -///\brief MindSpore predict session. -/// -/// This class represents session of MindSpore predict. -/// -///\note -/// The caller needs to allocate and free memory of inputs and outputs. -/// New Session is not suggested, please use CreateSession function to create new session class. -class MSPREDICT_API Session { - public: - ///\brief Constructor of MindSpore predict session. - /// - ///\param[in] ctx The context of the session. - /// - ///\return Instance of MindSpore predict session. - explicit Session(const Context &ctx); - - ///\brief Destructor of MindSpore predict session. - ~Session(); - - ///\brief Init the session. - /// - ///\param[in] ctx The context of the session. - ///\param[in] size The size of the session. - ///\param[in] graphBuf The buffer of the graph, used for build session. - /// - ///\return Return RET_OK if the initialization is success, otherwhise return RET_ERROR. - int Init(const char *graphBuf, size_t size); - - ///\brief Get the input of session. - /// - ///\return Input node's input tensors if found, empty vector otherwise. - /// - ///\note - /// The caller needs to allocate and free memory of inputs. - std::vector GetInput(); - - ///\brief Run the session. - /// - ///\param[in] inputs The input of the session. - /// - ///\return Return RET_OK if run success, otherwhise return RET_ERROR. - ///\note - /// Currently input tensors' data format only support FORMAT_NCHW. - /// Currently input tensors' data type only support FLOAT. - int Run(const std::vector &inputs); - - ///\brief Get the output of session. - /// - ///\param[in] nodeName Given output node name. - /// - ///\return Output node's output tensors if found, empty vector otherwise. - /// - ///\note - /// The caller needs to free memory of outputs. - std::vector GetOutput(const std::string &nodeName); - - ///\brief Get the all output of session. - /// - ///\return Every output node's output tensors. - /// - ///\note - /// The caller needs to free memory of outputs. - std::map> GetAllOutput(); - - protected: - ///\brief Init the executor. - /// - ///\return Return RET_OK if the initialization is success, otherwhise return RET_ERROR. - int InitExecutor(); - - const Context &_ctx; - Graph *_graph = nullptr; - GraphExecution *_executor = nullptr; - bool reinitExecutor = true; -}; - -///\brief MindSpore predict neural network session create function -/// -/// This function used to create MindSpore predict neural network session, which will be used to run the neural network. -/// -///\param[in] sessionName The name of the session. -///\param[in] graphBuf The buffer of the graph, used for build session. -///\param[in] size The size of the session. -///\param[in] ctx The context of the session. -/// -///\return Instance of MindSpore predict session. -/// -///\note -/// The caller needs to allocate and free memory of graph buffer. -std::shared_ptr MSPREDICT_API CreateSession(const char *graphBuf, size_t size, const Context &ctx); -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_INCLUDE_SESSION_H_ diff --git a/predict/include/tensor.h b/predict/include/tensor.h deleted file mode 100644 index 8a608b486d1..00000000000 --- a/predict/include/tensor.h +++ /dev/null @@ -1,259 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_INCLUDE_TENSOR_H_ -#define PREDICT_INCLUDE_TENSOR_H_ - -#include -#include -#include "dlpack/dlpack.h" -#include "schema/inner/ms_generated.h" - -#define MSPREDICT_API __attribute__((visibility("default"))) - -namespace mindspore { -namespace predict { -///\brief Allocator definition of MindSpore predict. -class Allocator; - -///\brief Tensor definition of MindSpore predict. -class MSPREDICT_API Tensor { - public: - ///\brief Constructor of MindSpore predict tensor. - /// - ///\param[in] tensor Define the parameters of the tensor. - ///\param[in] copyData Malloc data for the tensor, and copy origin data from - /// input tensor. - /// - ///\return Instance of MindSpore predict tensor. - Tensor(const Tensor &tensor, bool copyData = false); - - ///\brief Constructor of MindSpore predict tensor. - /// - ///\param[in] dt Data Type of the tensor, see introduction to 'enum DataType' - /// for supported type. - ///\param[in] dims Dimension Values such as height and width, which defined - /// the shape of the tensor. - ///\param[in] format Tensor format, see introduction to 'enum Format' for - /// supported format. - ///\param[in] data Data of the tensor. - /// - ///\return Instance of MindSpore predict tensor. - /// - ///\note - /// Length of data should align with dt, format and dims, otherwise the - /// application might run into unexpected error, - /// such as segment fault. - /// For example, dt is DT_FLOAT, format is FORMAT_NCHW, dims is [1,3,300,300], - /// then minimum length of data should - /// be 1 * 3 * 300 * 300 * sizeof(float). - Tensor(DataType dt, const std::vector &dims, Format format, void *data); - - ///\brief Destructor of MindSpore predict tensor. - ~Tensor(); - - ///\brief Get MindSpore predict tensor. - /// - ///\param[in] Definition of the tensor. - /// - ///\return Address of MindSpore predict tensor. - static Tensor *CopyFromTensorDef(const TensorDef &tensordef); - - ///\brief Get dtype of MindSpore predict tensor. - /// - ///\return Dtype of MindSpore predict tensor. - DLDataType GetTensorDtype() const; - - ///\brief Get data of MindSpore predict tensor. - /// - ///\return Address of MindSpore predict tensor data. - void *GetData() const; - - ///\brief Set data of MindSpore predict tensor. - /// - ///\param[in] data Address for data of the MindSpore predict tensor instance. - /// - ///\note - /// Length of data should align with dt, format and dims, otherwise the - /// application might run into unexpected error, - /// such as segment fault. - /// For example, dt is DT_FLOAT, format is FORMAT_NCHW, dims is [1,3,300,300], - /// then minimum length of data should - /// be 1 * 3 * 300 * 300 * sizeof(float). - void SetData(void *data); - - ///\brief Get data type of MindSpore predict tensor. - /// - ///\return Data Type of the tensor. - DataType GetDataType() const; - - ///\brief Set data type of MindSpore predict tensor. - /// - ///\param[in] dt Data Type of the tensor, see introduction to 'enum DataType' - /// for supported type. - void SetDataType(DataType dt); - - ///\brief Get number of dimension of MindSpore predict tensor. - /// - ///\return Number of dimension of the MindSpore predict tensor. - int GetNDim() const; - - ///\brief Get dimension of MindSpore predict tensor. - /// - ///\return Dimension of the MindSpore predict tensor. - std::vector GetDims() const; - - ///\brief Set dimension of MindSpore predict tensor. - /// - ///\param[in] dims Vector that has values of dimension. - void SetDims(const std::vector &dims); - - ///\brief Get format of MindSpore predict tensor. - /// - ///\return Format of the MindSpore predict tensor. - Format GetFormat() const { return format; } - - ///\brief Set format of MindSpore predict tensor. - /// - ///\param[in] format Format of the tensor. - void SetFormat(Format format) { this->format = format; } - - ///\brief Get reference count of MindSpore predict tensor. - /// - ///\return Reference count of the MindSpore predict tensor. - int RefCount() { return refCount; } - - ///\brief Increase reference count of MindSpore predict tensor. - /// - ///\param[in] ref The increase of the reference count. - void AddRef(int ref) { refCount += ref; } - - ///\brief Decrease reference count of MindSpore predict tensor. - /// - ///\param[in] ref The decrease of the reference count. - void DefRef(int ref) { refCount -= ref; } - - ///\brief Get element size of MindSpore predict tensor. - /// - ///\return Element size of MindSpore predict tensor. - size_t GetElementSize() const; - - ///\brief Get data size of MindSpore predict tensor. - /// - ///\return Data size of MindSpore predict tensor. - size_t GetDataSize() const; - - ///\brief Get element size of MindSpore predict tensor in NC4HW4 format. - /// - ///\param[in] isNhwc Whether the current format is NHWC. - /// - ///\return Element size of MindSpore predict tensor in NC4HW4 format. - size_t GetNC4HW4ElementSize(bool isNhwc); - - ///\brief Get data size of MindSpore predict tensor in NC4HW4 format. - /// - ///\param[in] isNhwc Whether the current format is NHWC. - /// - ///\return Data size of MindSpore predict tensor in NC4HW4 format. - size_t GetNC4HW4DataSize(bool isNhwc); - - ///\brief Malloc data for the MindSpore predict tensor. - /// - ///\param[in] allocator The malloc source for data. - ///\param[in] refCount The reference count of the data. - /// - ///\return Return RET_OK if the data is successfully allocated, otherwhise return RET_ERROR. - int MallocData(std::shared_ptr allocator = nullptr, int refCount = 0); - - ///\brief Free the MindSpore predict tensor. - void FreeTensor(); - - ///\brief Free the data of MindSpore predict tensor. - void ForceFreeData(); - - ///\brief Free the data of MindSpore predict tensor. - void FreeData(); - - ///\brief Compare data size of MindSpore predict tensor in NC4HW4 format. - /// - ///\param[in] dst The compare tensor. - /// - ///\return The result of fuction. - bool CompareShape(const Tensor &dst); - - ///\brief Compare shape of MindSpore predict tensor with another shape. - /// - ///\param[in] other The compare shape information. - /// - ///\return The result of function. - bool CompareShape(const std::vector &other); - - ///\brief Get instance of MindSpore predict tensor. - /// - ///\return Instance of MindSpore predict dlTensor. - DLTensor *GetDLTensor() { return &dlTensor; } - - ///\brief Get height of MindSpore predict tensor. - /// - ///\return Height of MindSpore predict tensor. - int64_t Height() const; - - ///\brief Get width of MindSpore predict tensor. - /// - ///\return Width of MindSpore predict tensor. - int64_t Width() const; - - ///\brief Get channel of MindSpore predict tensor. - /// - ///\return Channel of MindSpore predict tensor. - int64_t Channel() const; - - ///\brief Get batch of MindSpore predict tensor. - /// - ///\return Batch of MindSpore predict tensor. - int64_t Batch() const; - - ///\brief Get stride of MindSpore predict tensor. - /// - ///\param[in] index the index of stride. - /// - ///\return Stride of MindSpore predict tensor. - int64_t Stride(int index) const; - - ///\brief Set stride of MindSpore predict tensor by input. - /// - ///\param[in] index Index of stride - ///\param[in] stride The stride to set - void SetStride(int index, int64_t stride); - - ///\brief Set stride of MindSpore predict tensor by dims. - void SetStride(); - void SetScale(bool isScale = true); - - private: - bool isScale = false; - int refCount = 0; - int isConst; - Format format; - DLTensor dlTensor; - std::shared_ptr allocator = nullptr; - std::vector scale; - std::vector zeroPoint; -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_INCLUDE_TENSOR_H_ diff --git a/predict/module/CMakeLists.txt b/predict/module/CMakeLists.txt deleted file mode 100644 index 9b978f1172a..00000000000 --- a/predict/module/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(tvm_kernel) diff --git a/predict/module/tvm_kernel/.gitignore b/predict/module/tvm_kernel/.gitignore deleted file mode 100644 index 3b552d75dab..00000000000 --- a/predict/module/tvm_kernel/.gitignore +++ /dev/null @@ -1,27 +0,0 @@ -# Created by .ignore support plugin -# - -# filter python -*.pyc - -# filter build -*.so -*.o - -# filter coverage -coverage/ - -# filter report -*.xml - -# filter tvm -3rdparty/ - -# filter build -build/ -cmake-build-debug/ -.idea/ -TFLite_Detection_PostProcess_CI -app_run -output -tvm diff --git a/predict/module/tvm_kernel/.gitmodules b/predict/module/tvm_kernel/.gitmodules deleted file mode 100644 index 8a987193a4e..00000000000 --- a/predict/module/tvm_kernel/.gitmodules +++ /dev/null @@ -1,4 +0,0 @@ -[submodule "3rdparty/incubator-tvm"] - path = 3rdparty/incubator-tvm - url = https://github.com/dmlc/tvm.git - branch = v0.5 diff --git a/predict/module/tvm_kernel/CMakeLists.txt b/predict/module/tvm_kernel/CMakeLists.txt deleted file mode 100755 index b99e257c3eb..00000000000 --- a/predict/module/tvm_kernel/CMakeLists.txt +++ /dev/null @@ -1,25 +0,0 @@ -cmake_minimum_required(VERSION 3.12.1) -project(autotensor LANGUAGES CXX) -set (MINDSPORE "${PROJECT_SOURCE_DIR}/../../..") -set (TVM_KERNEL_LITE "${PROJECT_SOURCE_DIR}/lite") -set (THIRDPARTY "${MINDSPORE}/third_party") -set (TVM_CLEAN_SOURCE "${THIRDPARTY}/incubator-tvm") -set (TVM_BUILD_SOURCE "${PROJECT_SOURCE_DIR}/incubator-tvm") -set (BUILD_DIR "${PROJECT_SOURCE_DIR}") -set (TVM_KERNEL_OUTPUT_DIR ${CMAKE_CURRENT_BINARY_DIR}) -set (TVM_OUTPUT_DIR ${TVM_KERNEL_OUTPUT_DIR}/incubator-tvm) - -set (LLVM_CONFIG $ENV{LLVM_PATH}) -if (NOT LLVM_CONFIG) - message(FATAL_ERROR "please set LLVM_PATH in env") -endif() -set (CMAKE_BUILD_TYPE "Release") - -include(${TVM_BUILD_SOURCE}/cmake/util/Util.cmake) -include(${TVM_BUILD_SOURCE}/cmake/util/FindLLVM.cmake) -if(EXISTS ${TVM_BUILD_SOURCE}/cmake/config.cmake) - include(${TVM_BUILD_SOURCE}/cmake/config.cmake) -endif() -add_subdirectory(${TVM_KERNEL_LITE}) -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - diff --git a/predict/module/tvm_kernel/lite/CMakeLists.txt b/predict/module/tvm_kernel/lite/CMakeLists.txt deleted file mode 100755 index 50e2bf5c9b9..00000000000 --- a/predict/module/tvm_kernel/lite/CMakeLists.txt +++ /dev/null @@ -1,140 +0,0 @@ -cmake_minimum_required(VERSION 3.12) -set(CMAKE_CXX_STANDARD 14) - -if(ENABLE_PREDICT_ARM64) - set(TARGS "arm64") -elseif(ENABLE_PREDICT_ARM32) - set(TARGS "arm32") -else() - set(TARGS "x86") -endif() -message("TARGET is set to ${TARGS}") - -set(CMAKE_VERBOSE_MAKEFILE ON) -set(CMAKE_SKIP_RPATH TRUE) - -if(MSVC) - message("not support MSVC") -else(MSVC) - include(CheckCXXCompilerFlag) - check_cxx_compiler_flag("-std=c++11" SUPPORT_CXX11) - if ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") - message("Build in Debug mode") - set(CMAKE_C_FLAGS "-O0 -g -Wall -Werror -fPIC [${CMAKE_C_FLAGS} -rdynamic") - set(CMAKE_CXX_FLAGS "-O0 -g -Wall -Werror -fPIC -std=c++11 ${CMAKE_CXX_FLAGS} -rdynamic") - else() - set(CMAKE_C_FLAGS "-D_FORTIFY_SOURCE=2 -O2 -fno-rtti -fvisibility=hidden -Wall -Werror -fPIC -fstack-protector-strong ${CMAKE_C_FLAGS}") - set(CMAKE_CXX_FLAGS "-D_FORTIFY_SOURCE=2 -O2 -fno-rtti -fvisibility=hidden -Wall -Werror -fPIC -fstack-protector-strong -std=c++11 ${CMAKE_CXX_FLAGS}") - set(CMAKE_EXE_LINKER_FLAGS "-Wl,-z,relro,-z,now -Wl,-z,noexecstack") - endif () - if (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND - CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 7.0) - set(CMAKE_CXX_FLAGS "-Wall -Werror -faligned-new ${CMAKE_CXX_FLAGS}") - endif() - if (CODE_COVERAGE) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror -fprofile-arcs -ftest-coverage -O0") - endif() -endif(MSVC) - - -if("${TARGS}" STREQUAL "x86") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -D__x86_64__ -fno-strict-aliasing") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -D__x86_64__ -fno-strict-aliasing") -endif() - - -set(PRJ_SRC_DIR "${PROJECT_SOURCE_DIR}") -set(PRJ_KLIB_DIR "${PROJECT_SOURCE_DIR}") -set(PRJ_LITE_DIR "${PROJECT_SOURCE_DIR}/lite") - -# include directories -message("current PRJ DIR: ${PROJECT_SOURCE_DIR}") -message("current SUB_PRJ DIR: ${PRJ_SRC_DIR}") -message("current KLIB DIR: ${PRJ_KLIB_DIR}") -message("current PRJ_LITE_DIR: ${PRJ_LITE_DIR}") -message("CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}") -set(DMLC_CORE "${TVM_BUILD_SOURCE}/3rdparty/dmlc-core") -set(DLPACK "${TVM_BUILD_SOURCE}/3rdparty/dlpack") -set(PREDICT "${PRJ_SRC_DIR}/../../") -set(SECUREC "${PRJ_SRC_DIR}/../../../third_party/securec") -message("include dir: ${DLPACK}/include") -include_directories(${DLPACK}/include) -include_directories(${DMLC_CORE}/include) -include_directories(${TVM_BUILD_SOURCE}/include) -include_directories(${TVM_BUILD_SOURCE}/src/pass) -include_directories(${PRJ_LITE_DIR}) -include_directories(${PRJ_LITE_DIR}/include) -include_directories(${PRJ_LITE_DIR}/../../..) -include_directories(${PRJ_LITE_DIR}/../../../include) -include_directories(${PRJ_LITE_DIR}/../../../src/runtime) -include_directories(${PRJ_LITE_DIR}/../../../common) -include_directories(${SECUREC}) -message("SECUREC: " "${SECUREC}/build/src") -include_directories(${PREDICT}) -include_directories(${PREDICT}/src) -include_directories(${PRJ_SRC_DIR}/../../../third_party/flatbuffers/include) -include_directories(${PRJ_SRC_DIR}/../../../third_party) -# Source file lists -file(GLOB_RECURSE TVM_KERNEL_SRC - src/api/*.cc - src/tflite/TFLite_Detection_PostProcess.cc) - -set (TVM_RUNTIME_FLG $ENV{TVM_RUNTIME_ON}) -if ("${TVM_RUNTIME_FLG}" STREQUAL "true") - message("Using TVM runtime function") - file(GLOB TVM_RUNTIME_SRCS - ${TVM_ROOT}/apps/howto_deploy/tvm_runtime_pack.cc) -else() - message("Using LITE runtime function") - set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DLITE_RUNTIME_ON -DTVM_RUNTIME_HEADER_ONLY -DLITE_THREAD_POOL_SHARED") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLITE_RUNTIME_ON -DTVM_RUNTIME_HEADER_ONLY -DLITE_THREAD_POOL_SHARED") - file(GLOB_RECURSE TVM_RUNTIME_SRCS - ${PREDICT}/src/runtime/*.cc) -endif() - -if("${TARGS}" STREQUAL "arm32" OR "${TARGS}" STREQUAL "arm64") - set(CMAKE_SKIP_BUILD_RPATH TRUE) - set(CMAKE_BUILD_WITH_INSTALL_RPATH TRUE) - set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE) -endif() - -set(LIB_X86_PATH "${PRJ_KLIB_DIR}/build/lib_x86") -set(LIB_ARM64_PATH "${PRJ_KLIB_DIR}/build/lib_arm64") -set(LIB_ARM32_PATH "${PRJ_KLIB_DIR}/build/lib_arm32") -if("${TARGS}" STREQUAL "x86") - set(KLIBS_PATH "${LIB_X86_PATH}") -elseif("${TARGS}" STREQUAL "arm64") - set(KLIBS_PATH "${LIB_ARM64_PATH}") -elseif("${TARGS}" STREQUAL "arm32") - set(KLIBS_PATH "${LIB_ARM32_PATH}") -else() - message(ERROR " not suport ${TARGS}") -endif() - -file(GLOB_RECURSE KERNEL_LIBS "${KLIBS_PATH}/*.o") -message("KERNEL_PATH= ${KLIBS_PATH}") - -add_compile_options(-DTVM_CUDA_RUNTIM=0) -add_compile_options(-DTVM_METAL_RUNTIM=0) -add_compile_options(-DTVM_OPENCL_RUNTIM=0) - -link_directories(${KLIBS_PATH}) - -add_library(tvm_runtime_pack STATIC ${TVM_RUNTIME_SRCS}) -add_library(kernel_manager STATIC ${TVM_KERNEL_SRC}) -add_library(tvm_kernel_static STATIC ${TVM_KERNEL_SRC} ${KERNEL_LIBS}) -add_library(tvm_kernel SHARED ${TVM_KERNEL_SRC} ${KERNEL_LIBS}) -set_target_properties(tvm_kernel PROPERTIES LINK_FLAGS "-Wl,-z,relro,-z,now -Wl,-z,noexecstack") - -set(KERNEL_LD_LIB tvm_runtime_pack dl) - -if("${TARGS}" STREQUAL "x86") - set(KERNEL_LD_LIB ${KERNEL_LD_LIB} pthread) -else() - set(ANDROID_ALLOW_UNDEFINED_SYMBOLS TRUE) -endif() - -target_link_libraries(tvm_kernel ${KERNEL_LD_LIB} libsecurec.a) -target_link_libraries(tvm_kernel_static OBJECT tvm_runtime_pack libsecurec.a) - -add_dependencies(tvm_kernel securec) diff --git a/predict/module/tvm_kernel/lite/include/lite/api/km_api.h b/predict/module/tvm_kernel/lite/include/lite/api/km_api.h deleted file mode 100644 index 7ccd4964cbc..00000000000 --- a/predict/module/tvm_kernel/lite/include/lite/api/km_api.h +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this ${file} except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef PREDICT_MODULE_TVM_KERNEL_LITE_INCLUDE_LITE_API_KM_API_H_ -#define PREDICT_MODULE_TVM_KERNEL_LITE_INCLUDE_LITE_API_KM_API_H_ - -#include -#include -#include -#include -#include "schema/inner/ms_generated.h" -#include "schema/inner/op_generated.h" - -#define PUBLIC __attribute__((visibility("default"))) - -/*! - * \brief Call tvm kernel. - * \param fid tvm kernel id. - * \param tensors tvm kernel arguments. - * \return 0 if SUCCESS. - */ -PUBLIC int CallKernel(const std::string &fid, const std::vector &tensors); - -/*! - * \brief Get tvm kernel by id. - * \param fid tvm kernel id. - * \return std::function if SUCCESS else nullptr. - */ -PUBLIC std::function &)> GetKernel(const std::string &fid); - -/*! - * \brief Get tvm kernel by OpDef. - * \param opdef defined by predict schema. - * \param tensors. - * \param option. - * \return std::function if SUCCESS else nullptr. - */ -struct PUBLIC KernelOption { - int numThreads = 0; - std::string device; -}; - -PUBLIC std::function &)> GetKernel(const mindspore::predict::OpDef &opdef, - const std::vector &tensors, - const KernelOption &option); - -/*! - * \brief load TVM Kernel lib - * \param mode 0 indicate shared lib - * \param fname shared lib path when mode equals 0 - * \return 0 if SUCCESS - */ -PUBLIC void InitKernelManager(int mode, const std::string &fname); - -/* - * \brief config ThreadPool using mode - * \param mode: -1 using mid speed cpu first, 1 using higher speed cpu first - * \param nthreads: threads num to be used, can't exceed cpu num - * if mode==-1 bind mid cpu first - * if mode==1 bind higher cpu first - * if mode==0 no bind - * \param execute_self: cur thread do arithmetic or not - * execute_self: true cur thread do arithmetic work - * execute_self: false cur thread not do arithmetic work - */ -PUBLIC void ConfigThreadPool(int mode = -1, int nthreads = 2, bool execute_self = true); - -/* - * \brief provid simple api for mslite, mslite not care mode - */ -inline void CfgThreadPool(int nthread) { ConfigThreadPool(-1, nthread, true); } - -/* - * the Callback function to do cpu bind for master thread. - */ -PUBLIC void DoMasterThreadBind(bool bindflg); - -PUBLIC void DoAllThreadBind(bool ifBind); - -#undef PUBLIC - -#endif // PREDICT_MODULE_TVM_KERNEL_LITE_INCLUDE_LITE_API_KM_API_H_ diff --git a/predict/module/tvm_kernel/lite/python/__init__.py b/predict/module/tvm_kernel/lite/python/__init__.py deleted file mode 100644 index 5a51943fbe5..00000000000 --- a/predict/module/tvm_kernel/lite/python/__init__.py +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -"""Neural network operators""" -# from . import arm_cpu -# from . import at_ops diff --git a/predict/module/tvm_kernel/lite/python/arm_cpu/__init__.py b/predict/module/tvm_kernel/lite/python/arm_cpu/__init__.py deleted file mode 100644 index dce9d5e96c7..00000000000 --- a/predict/module/tvm_kernel/lite/python/arm_cpu/__init__.py +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -"""Schedule for ARM CPU""" - -from . import conv2d diff --git a/predict/module/tvm_kernel/lite/python/arm_cpu/conv2d.py b/predict/module/tvm_kernel/lite/python/arm_cpu/conv2d.py deleted file mode 100644 index ded792f689a..00000000000 --- a/predict/module/tvm_kernel/lite/python/arm_cpu/conv2d.py +++ /dev/null @@ -1,470 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -"""Conv2D schedule for ARM CPU""" -from __future__ import absolute_import as _abs - -import functools - -import tvm -from tvm import autotvm -import tvm.contrib.nnpack - -from topi.generic import schedule_conv2d_nchw -from topi.util import traverse_inline, get_const_tuple -from topi.nn import pad, conv2d -from topi.nn.util import get_const_int, get_pad_tuple - - -@autotvm.register_topi_compute(conv2d, "arm_cpu", ["asm"]) -def conv2d_arm_cpu(cfg, data, kernel, strides, padding, dilation, out_dtype): - """TOPI compute callback for conv2d - - Parameters - ---------- - cfg: ConfigEntity - The config for this template - - data : tvm.Tensor - 4-D with shape [batch, in_channel, in_height, in_width] - - kernel : tvm.Tensor - 4-D with shape [num_filter, in_channel, filter_height, filter_width] or - pre-packed 5-D with shape [num_filter_chunk, in_channel, filter_height, - filter_width, num_filter_block] - - strides : list of two ints - [stride_height, stride_width] - - padding : list of two ints - [pad_height, pad_width] - - dilation : list of two ints - [dilation_height, dilation_width] - - out_dtype: str - The output type. This is used for mixed precision. - - Returns - ------- - output : tvm.Tensor - 4-D with shape [batch, out_channel, out_height, out_width] - """ - args = _gen_cfg(cfg, data, kernel, strides, padding, dilation, num_tile=2) - return _conv_spatial_pack_asm( - args, data, kernel, strides, padding, dilation, out_dtype - ) - - -@autotvm.register_topi_schedule(schedule_conv2d_nchw, "arm_cpu", ["asm"]) -def schedule_conv2d_nchw_arm_cpu(outs): - """TOPI schedule callback for conv2d - - Parameters - ---------- - outs: Array of Tensor - The computation graph description of conv2d - in the format of an array of tensors. - - Returns - ------- - s: Schedule - The computation schedule for conv2d. - """ - s = _conv_schedule_asm(outs) - return s - - -def _gen_cfg(cfg, data, kernel, strides, padding, dilation, num_tile): - """_gen_cfg""" - if len(kernel.shape) == 4: - co_, _, kh_, kw_ = get_const_tuple(kernel.shape) - else: # kernel tensor is pre packed - co_, _, kh_, kw_, vc_ = get_const_tuple(kernel.shape) - co_ = co_ * vc_ - - if isinstance(dilation, int): - dilation_h = dilation_w = dilation - else: - dilation_h, dilation_w = dilation - - n_, ci_, ih_, iw_ = get_const_tuple(data.shape) - - dilated_kernel_h = (kh_ - 1) * dilation_h + 1 - dilated_kernel_w = (kw_ - 1) * dilation_w + 1 - pad_top, pad_left, pad_bottom, pad_right = get_pad_tuple( - padding, (dilated_kernel_h, dilated_kernel_w) - ) - hstr, wstr = strides if isinstance(strides, (tuple, list)) else (strides, strides) - oh_ = (ih_ + pad_top + pad_bottom - dilated_kernel_h) // hstr + 1 - ow_ = (iw_ + pad_left + pad_right - dilated_kernel_w) // wstr + 1 - - n, co, oh, ow = cfg.axis(n_), cfg.axis(co_), cfg.axis(oh_), cfg.axis(ow_) - ci, kh, kw = cfg.reduce_axis(ci_), cfg.reduce_axis(kh_), cfg.reduce_axis(kw_) - - if num_tile == 2: # for arm cpu - candidate_vc = [] - for iv in range(3, co_): - if co_ % iv == 0: - candidate_vc.append([co_ // iv, iv]) - candidate_vc.append([1, co_]) - co, vc = cfg.define_split( - "tile_co", co, num_outputs=2, policy="candidate", candidate=candidate_vc - ) - oh, vh = cfg.define_split("tile_oh", oh, num_outputs=2) - ow, vw = cfg.define_split("tile_ow", ow, num_outputs=2) - elif num_tile == 3: # for mali gpu - co, _, vc = cfg.define_split("tile_co", co, num_outputs=3) - oh, _, vh = cfg.define_split("tile_oh", oh, num_outputs=3) - ow, _, vw = cfg.define_split("tile_ow", ow, num_outputs=3) - else: - raise RuntimeError("Invalid num_tile") - - cfg.define_reorder( - "reorder_0", - [n, co, oh, ow, ci, kh, kw, vh, vw, vc], - policy="candidate", - candidate=[[n, co, oh, ow, ci, kh, kw, vh, vw, vc],], - ) - - vc_ = cfg["tile_co"].size[-1] - vh_ = cfg["tile_oh"].size[-1] - vw_ = cfg["tile_ow"].size[-1] - is_var = False - return (is_var, vh_, vw_, vc_) - -def _conv_spatial_pack_asm(args, data, kernel, strides, padding, - dilation, out_dtype): - """_conv_spatial_pack_asm""" - is_var, vh_, vw_, vc_ = args - - # create workload according to raw arguments - out_dtype = out_dtype or data.dtype - n_, ci_, ih_, iw_ = data.shape if is_var else get_const_tuple(data.shape) - - if isinstance(dilation, int): - dilation_h = dilation_w = dilation - else: - dilation_h, dilation_w = dilation - - if len(kernel.shape) == 4: - pre_packed = False - co_, _, kh_, kw_ = kernel.shape if is_var else get_const_tuple(kernel.shape) - else: # kernel tensor is pre packed - pre_packed = True - co_, _, kh_, kw_, vc_ = kernel.shape if is_var else get_const_tuple(kernel.shape) - co_ = co_ * vc_ - - dilated_kernel_h = (kh_ - 1) * dilation_h + 1 - dilated_kernel_w = (kw_ - 1) * dilation_w + 1 - pad_top, pad_left, pad_bottom, pad_right = get_pad_tuple( - padding, (dilated_kernel_h, dilated_kernel_w) - ) - hstr, wstr = strides if isinstance(strides, (tuple, list)) else (strides, strides) - oh_ = (ih_ + pad_top + pad_bottom - dilated_kernel_h) // hstr + 1 - ow_ = (iw_ + pad_left + pad_right - dilated_kernel_w) // wstr + 1 - data_pad = pad(data, [0, 0, pad_top, pad_left], [0, 0, pad_bottom, pad_right]) - - oh_div = oh_ // vh_ - ow_div = ow_ // vw_ - kvshape = (co_ // vc_, ci_, kh_, kw_, vc_) - ovshape = (n_, co_ // vc_, oh_div, ow_div, vh_, vw_, vc_) - oshape = (n_, co_, oh_div * vh_, ow_div * vw_) - - if dilation_h != 1 or dilation_w != 1: - # undilate input data - dvshape = (n_, oh_ // vh_, ow_ // vw_, kh_, kw_, vh_, vw_, ci_) - data_vec = tvm.compute( - dvshape, - lambda n, h, w, kh, kw, vh, vw, ci: data_pad[n][ci][ - (h * vh_ + vh) * hstr + kh * dilation_h - ][(w * vw_ + vw) * wstr + kw * dilation_w], - name="data_vec_undilated", - ) - else: - dvshape = ( - n_, - oh_ // vh_, - ow_ // vw_, - (vh_ - 1) * hstr + kh_, - (vw_ - 1) * wstr + kw_, - ci_, - ) - data_vec = tvm.compute( - dvshape, - lambda n, h, w, vh, vw, ci: data_pad[n][ci][h * vh_ * hstr + vh][ - w * vw_ * wstr + vw - ], - name="data_vec", - ) - - if pre_packed: - kernel_vec = kernel - else: - kernel_vec = tvm.compute( - kvshape, - lambda co, ci, kh, kw, vc: kernel[co * vc_ + vc][ci][kh][kw], - name="kernel_vec", - ) - - ci = tvm.reduce_axis((0, ci_), name="ci") - kh = tvm.reduce_axis((0, kh_), name="kh") - kw = tvm.reduce_axis((0, kw_), name="kw") - - # asm begin---- - type_map = { - "int8": "int32", - "uint8": "uint32", - "float32": "float32", - "float16": "float16", - } - acum_dtype = type_map[data.dtype] - attrs = { - "SH": hstr, - "SW": wstr, - "PH": pad_top, - "PW": pad_left, - "DILA_H": dilation_h, - "DILA_W": dilation_w, - "VH": vh_, - "VW": vw_, - "VC": vc_, - "ACUM_DTYPE": acum_dtype, - } - # asm end---- - - if dilation_h != 1 or dilation_w != 1: - conv = tvm.compute( - ovshape, - lambda n, co, h, w, vh, vw, vc: tvm.sum( - data_vec[n, h, w, kh, kw, vh, vw, ci].astype(out_dtype) - * kernel_vec[co, ci, kh, kw, vc].astype(out_dtype), - axis=[ci, kh, kw], - ), - name="conv", - attrs=attrs, - ) - else: - conv = tvm.compute( - ovshape, - lambda n, co, h, w, vh, vw, vc: tvm.sum( - data_vec[n, h, w, vh * hstr + kh, vw * wstr + kw, ci].astype(out_dtype) - * kernel_vec[co, ci, kh, kw, vc].astype(out_dtype), - axis=[ci, kh, kw], - ), - name="conv", - attrs=attrs, - ) - - output = tvm.compute( - oshape, - lambda n, co, h, w: conv[n][co // vc_][h // vh_][w // vw_][h % vh_][w % vw_][ - co % vc_ - ], - name="output_unpack", - tag="asm_conv2d_output", - ) - - return output - - -def intrin_conv(args): - """intrin_conv""" - ( - ci_, - vh_, - vw_, - vc_, - kh_, - kw_, - sh_, - sw_, - dila_h, - dila_w, - dtype, - acum_dtype, - opname, - core_id, - ) = args - hstr, wstr = sh_, sw_ - ci_ = tvm.var("ci_") if ci_ is None else ci_ - kvshape = (ci_, kh_, kw_, vc_) - ovshape = (vh_, vw_, vc_) - if dila_h != 1 or dila_w != 1: - dvshape = (kh_, kw_, vh_, vw_, ci_) - else: - dvshape = ((vh_ - 1) * hstr + kh_, (vw_ - 1) * wstr + kw_, ci_) - - data_vec = tvm.placeholder(dvshape, name="a", dtype=dtype) - kernel_vec = tvm.placeholder(kvshape, name="b", dtype=dtype) - ci = tvm.reduce_axis((0, ci_), name="ci") - kh = tvm.reduce_axis((0, kh_), name="kh") - kw = tvm.reduce_axis((0, kw_), name="kw") - if dila_h != 1 or dila_w != 1: - conv = tvm.compute( - ovshape, - lambda vh, vw, vc: tvm.sum( - data_vec[kh, kw, vh, vw, ci].astype(acum_dtype) - * kernel_vec[ci, kh, kw, vc].astype(acum_dtype), - axis=[ci, kh, kw], - ), - name="conv", - ) - else: - conv = tvm.compute( - ovshape, - lambda vh, vw, vc: tvm.sum( - data_vec[vh * hstr + kh, vw * wstr + kw, ci].astype(acum_dtype) - * kernel_vec[ci, kh, kw, vc].astype(acum_dtype), - axis=[ci, kh, kw], - ), - name="conv", - ) - - stride_a = [ - functools.reduce(lambda x, y: x * y, dvshape[i + 1: len(dvshape)]) - for i in range(0, len(dvshape) - 1) - ] - stride_a.append(1) - stride_b = [ - functools.reduce(lambda x, y: x * y, kvshape[i + 1: len(kvshape)]) - for i in range(0, len(kvshape) - 1) - ] - stride_b.append(1) - stride_c = [ - functools.reduce(lambda x, y: x * y, ovshape[i + 1: len(ovshape)]) - for i in range(0, len(ovshape) - 1) - ] - stride_c.append(1) - - a_buffer = tvm.decl_buffer( - data_vec.shape, data_vec.dtype, name="A", offset_factor=1, strides=stride_a - ) - b_buffer = tvm.decl_buffer( - kernel_vec.shape, kernel_vec.dtype, name="B", offset_factor=1, strides=stride_b - ) - c_buffer = tvm.decl_buffer( - conv.shape, conv.dtype, name="C", offset_factor=1, strides=stride_c - ) - - def intrin_func(ins, outs): - aa, bb = ins - cc = outs[0] - - def _body(): - ib = tvm.ir_builder.create() - ib.emit( - tvm.call_extern( - "int32", - opname, - cc.access_ptr("w"), - aa.access_ptr("r"), - bb.access_ptr("r"), - ci_, - vh_, - vw_, - vc_, - kh_, - sh_, - core_id, - ) - ) - return ib.get() - - return _body() - - return tvm.decl_tensor_intrin( - conv.op, intrin_func, binds={data_vec: a_buffer, kernel_vec: b_buffer, conv: c_buffer} - ) - - -def _schedule_asm(s, data_vec, kernel_vec, conv, output, last): - """schedule implementation""" - n, co, oh, ow, vh, vw, vc = s[conv].op.axis - - axis_extent = [] - for i in (vh, vw, vc): - axis_extent.append(get_const_int(i.dom.extent)) - reduce_extent = [] - for i in s[conv].op.reduce_axis[1:]: - reduce_extent.append(get_const_int(i.dom.extent)) - vh_, vw_, vc_ = axis_extent - - # schedule fusion - n, co, h, w = s[last].op.axis - co, vc = s[last].split(co, vc_) - oh, vh = s[last].split(h, vh_) - ow, vw = s[last].split(w, vw_) - s[last].reorder(n, co, oh, ow, vh, vw, vc) - if last != output: - s[output].compute_inline() - - s[conv].compute_at(s[last], ow) - - # mark parallel - s[last].parallel(co) - - if data_vec.op.name == "data_vec_undilated": - _, h, _, _, _, _, _, _ = s[data_vec].op.axis - else: - _, h, _, _, _, _ = s[data_vec].op.axis - s[data_vec].parallel(h) - - if kernel_vec.op.name == "kernel_vec": - co, _, _, _, _ = s[kernel_vec].op.axis - if autotvm.GLOBAL_SCOPE.in_tuning: - # kernel packing will be pre-computed during compilation, so we skip - # this part to make tuning records correct - s[kernel_vec].pragma(co, "debug_skip_region") - else: - s[kernel_vec].parallel(co) - elif kernel_vec.op.name == "kernel_vec_conv2d_transpose": # for conv2d transpose - co, _, _, _, _ = s[kernel_vec].op.axis - s[kernel_vec].parallel(co) - - return s - - -def _conv_schedule_asm(outs): - """_conv_schedule_asm""" - s = tvm.create_schedule([x.op for x in outs]) - - def _callback(op): - if "asm_conv2d_output" in op.tag: - # schedule conv2d - output = op.output(0) - conv = op.input_tensors[0] - - sidx = 0 - if conv.op.input_tensors[0].name == "attr": - sidx = 1 - data_vec = conv.op.input_tensors[sidx] - data_pad = data_vec.op.input_tensors[0] - s[data_pad].compute_inline() - - kernel_vec = conv.op.input_tensors[sidx + 1] - if kernel_vec.op.name == "kernel_vec": - kernel = kernel_vec.op.input_tensors[0] - else: - kernel = kernel_vec - if (isinstance(kernel.op, tvm.tensor.ComputeOp) and "dilate" in kernel.op.tag): - s[kernel].compute_inline() - - if conv.op.input_tensors[0].name == "attr": - _schedule_asm(s, data_vec, kernel_vec, conv, output, outs[0]) - else: - _schedule_asm(s, data_vec, kernel_vec, conv, output, outs[0]) - - traverse_inline(s, outs[0].op, _callback) - return s diff --git a/predict/module/tvm_kernel/lite/python/arm_cpu/deconv.py b/predict/module/tvm_kernel/lite/python/arm_cpu/deconv.py deleted file mode 100644 index 4ed29900a25..00000000000 --- a/predict/module/tvm_kernel/lite/python/arm_cpu/deconv.py +++ /dev/null @@ -1,477 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -"""Conv2D_transpose of stride=2, kernel=2*2 schedule for ARM CPU""" -from __future__ import absolute_import as _abs - -import functools - -import tvm -from tvm import autotvm -import tvm.contrib.nnpack - -from topi.generic import schedule_conv2d_nchw -from topi.util import traverse_inline, get_const_tuple -from topi.nn import conv2d - - -@autotvm.register_topi_compute(conv2d, "arm_cpu", ["deconv"]) -def conv2d_arm_cpu_deconv(cfg, data, kernel, out_dtype): - """TOPI compute callback for conv2d - - Parameters - ---------- - cfg: ConfigEntity - The config for this template - - data : tvm.Tensor - 4-D with shape [batch, in_channel, in_height, in_width] - - kernel : tvm.Tensor - 4-D with shape [num_filter, in_channel, filter_height, filter_width] or - pre-packed 5-D with shape [num_filter_chunk, in_channel, filter_height, - filter_width, num_filter_block] - - out_dtype: str - The output type. This is used for mixed precision. - - Returns - ------- - output : tvm.Tensor - 4-D with shape [batch, out_channel, out_height, out_width] - """ - args = _gen_cfg_deconv(cfg, data, kernel, num_tile=2) - return _conv_spatial_pack_deconv( - args, data, kernel, out_dtype - ) - - -@autotvm.register_topi_schedule(schedule_conv2d_nchw, "arm_cpu", ["deconv"]) -def schedule_conv2d_nchw_arm_cpu_deconv(cfg, outs): - """TOPI schedule callback for conv2d - - Parameters - ---------- - cfg: ConfigEntity - The config for this template - - outs: Array of Tensor - The computation graph description of conv2d - in the format of an array of tensors. - - Returns - ------- - s: Schedule - The computation schedule for conv2d. - """ - s = _conv_schedule_deconv(cfg, outs) - return s - - -def _gen_cfg_deconv(cfg, data, kernel, num_tile): - """generation config from input args""" - if len(kernel.shape) == 4: - co_, _, _, _ = get_const_tuple(kernel.shape) - else: # kernel tensor is pre packed - co_, _, _, _, vc_ = get_const_tuple(kernel.shape) - co_ = co_ * vc_ - - if len(data.shape) == 4: - _, ci_, ih_, iw_ = get_const_tuple(data.shape) - c4 = 4 - ci_ = ci_ // 4 - else: - _, ci_, ih_, iw_, c4 = get_const_tuple(data.shape) - - oh_ = ih_ * 2 - ow_ = iw_ * 2 - - co, oh, ow = cfg.axis(co_), cfg.axis(oh_), cfg.axis(ow_) - ci, ki = cfg.reduce_axis(ci_), cfg.reduce_axis(c4) - - if num_tile == 2: # for arm cpu - candidate_vc = [[co_ // c4, c4]] - co, vc = cfg.define_split( - "tile_co", co, num_outputs=2, policy="candidate", candidate=candidate_vc - ) - candidate_vw = [] - for iv in range(4, ow_ + 1): # [4, 6, 8, 12, 16, 24, 32, 40]: - if iv % 4 == 0 and (ow_ % iv == 0): - candidate_vw.append([ow_ // iv, iv]) - ow, vw = cfg.define_split( - "tile_ow", ow, num_outputs=2, policy="candidate", candidate=candidate_vw - ) - candidate_vh = [[1, 2]] - oh, vh = cfg.define_split( - "tile_oh", oh, num_outputs=2, policy="candidate", candidate=candidate_vh - ) - elif num_tile == 3: # for mali gpu - co, _, vc = cfg.define_split("tile_co", co, num_outputs=3) - oh, _, vh = cfg.define_split("tile_oh", oh, num_outputs=3) - ow, _, vw = cfg.define_split("tile_ow", ow, num_outputs=3) - else: - raise RuntimeError("Invalid num_tile") - - cfg.define_annotate("ann_reduce", [ci, ki], policy="try_unroll") - cfg.define_annotate("ann_spatial", [vh, vw, vc], policy="try_unroll_vec") - - vc_ = cfg["tile_co"].size[-1] - vh_ = cfg["tile_oh"].size[-1] - vw_ = cfg["tile_ow"].size[-1] - is_var = False - return (is_var, vh_, vw_, vc_) - - -def _conv_spatial_pack_deconv(args, data, kernel, out_dtype): - """conv2d_arm_cpu_deconv inner implement""" - is_var, vh_, vw_, vc_ = args - # create workload according to raw arguments - out_dtype = out_dtype or data.dtype - if len(data.shape) == 4: - n_, ci_, ih_, iw_ = data.shape if is_var else get_const_tuple(data.shape) - c4 = 4 - ci_ = ci_ // c4 - else: - n_, ci_, ih_, iw_, c4 = data.shape if is_var else get_const_tuple(data.shape) - - if len(kernel.shape) == 4: - pre_packed = False - _, co_, kh_, kw_ = kernel.shape if is_var else get_const_tuple(kernel.shape) - else: # kernel tensor is pre packed - pre_packed = True - _, co_, kh_, kw_, vc_ = kernel.shape if is_var else get_const_tuple(kernel.shape) - co_ = co_ * c4 - - oh_ = ih_ * 2 - ow_ = iw_ * 2 - ow_div = ow_ // vw_ - oh_div = oh_ // vh_ - kvshape = (co_ // vc_, kh_, kw_, ci_, c4, c4) - ovshape = (n_, co_ // vc_, oh_div, ow_div, vh_, vw_, c4) - - dvshape = (n_, ih_ // (vh_ // 2), iw_ // (vw_ // 2), vh_ // 2, ci_, vw_ // 2, c4) - if len(data.shape) == 4: - data_vec = tvm.compute( - dvshape, - lambda n, h, w, vh, ci, vw, ki: data[n][ci * c4 + ki][h * vh_ // 2 + vh][ - w * vw_ // 2 + vw - ], - name="data_vec", - ) - else: - data_vec = tvm.compute( - dvshape, - lambda n, h, w, vh, ci, vw, ki: data[n][ci][h * vh_ // 2 + vh][ - w * vw_ // 2 + vw - ][ki], - name="data_vec", - ) - - if pre_packed: - kernel_vec = kernel - else: - kernel_vec = tvm.compute( - kvshape, - lambda co, kh, kw, ci, ki, vc: kernel[ci * c4 + ki][co * vc_ + vc][kh][kw], - name="kernel_vec", - ) - - ci = tvm.reduce_axis((0, ci_), name="ci") - ki = tvm.reduce_axis((0, c4), name="ki") - - type_map = { - "int8": "int32", - "uint8": "uint32", - "float32": "float32", - "float16": "float16", - } - acum_dtype = type_map[data.dtype] - attrs = { - "SH": 2, - "SW": 2, - "PH": 0, - "PW": 0, - "DILA_H": 1, - "DILA_W": 1, - "VH": vh_, - "VW": vw_, - "VC": vc_, - "ACUM_DTYPE": acum_dtype, - } - - conv = tvm.compute( - ovshape, - lambda n, co, h, w, vh, vw, vc: tvm.sum( - data_vec[n, h, w, vh // 2, ci, vw // 2, ki].astype(out_dtype) - * kernel_vec[co, (h * vh_ + vh) % 2, (w * vw_ + vw) % 2, ci, ki, vc].astype( - out_dtype - ), - axis=[ci, ki], - ), - name="conv", - attrs=attrs, - ) - if len(data.shape) == 4: - osshape = (n_, co_, oh_, ow_div * vw_) - output = tvm.compute( - osshape, - lambda n, co, h, w: conv[n][co // c4][h][w // vw_][w % vw_][co % c4], - name="output_unpack", - tag="deconv_conv2d_output", - ) - else: - osshape = (n_, co_ // c4, oh_, ow_div * vw_, c4) - output = tvm.compute( - osshape, - lambda n, co, h, w, vc: conv[n][co][h // vh_][w // vw_][h % vh_][w % vw_][vc], - name="output_unpack", - tag="deconv_conv2d_output", - ) - - return output - - -def intrin_deconv(args): - """deconv inner implement""" - ( - ci_, - vh_, - vw_, - vc_, - kh_, - kw_, - sh_, - sw_, - dila_h, - dila_w, - dtype, - acum_dtype, - opname, - core_id, - ) = args - hstr, wstr = sh_, sw_ - ci_ = tvm.var("ci_") if ci_ is None else ci_ - kvshape = (ci_, kh_, kw_, vc_) - ovshape = (vh_, vw_, vc_) - if dila_h != 1 or dila_w != 1: - dvshape = (kh_, kw_, vh_, vw_, ci_) - else: - dvshape = ((vh_ - 1) * hstr + kh_, (vw_ - 1) * wstr + kw_, ci_) - - data_vec = tvm.placeholder(dvshape, name="a", dtype=dtype) - kernel_vec = tvm.placeholder(kvshape, name="b", dtype=dtype) - ci = tvm.reduce_axis((0, ci_), name="ci") - kh = tvm.reduce_axis((0, kh_), name="kh") - kw = tvm.reduce_axis((0, kw_), name="kw") - if DILA_H != 1 or dila_w != 1: - conv = tvm.compute( - ovshape, - lambda vh, vw, vc: tvm.sum( - data_vec[kh, kw, vh, vw, ci].astype(acum_dtype) - * kernel_vec[ci, kh, kw, vc].astype(acum_dtype), - axis=[ci, kh, kw], - ), - name="conv", - ) - else: - conv = tvm.compute( - ovshape, - lambda vh, vw, vc: tvm.sum( - data_vec[vh * hstr + kh, vw * wstr + kw, ci].astype(acum_dtype) - * kernel_vec[ci, kh, kw, vc].astype(acum_dtype), - axis=[ci, kh, kw], - ), - name="conv", - ) - - stride_a = [ - functools.reduce(lambda x, y: x * y, dvshape[i + 1: len(dvshape)]) - for i in range(0, len(dvshape) - 1) - ] - stride_a.append(1) - stride_b = [ - functools.reduce(lambda x, y: x * y, kvshape[i + 1: len(kvshape)]) - for i in range(0, len(kvshape) - 1) - ] - stride_b.append(1) - stride_c = [ - functools.reduce(lambda x, y: x * y, ovshape[i + 1: len(ovshape)]) - for i in range(0, len(ovshape) - 1) - ] - stride_c.append(1) - - a_buffer = tvm.decl_buffer( - data_vec.shape, data_vec.dtype, name="A", offset_factor=1, strides=stride_a - ) - b_buffer = tvm.decl_buffer( - kernel_vec.shape, kernel_vec.dtype, name="B", offset_factor=1, strides=stride_b - ) - c_buffer = tvm.decl_buffer( - conv.shape, conv.dtype, name="C", offset_factor=1, strides=stride_c - ) - - def intrin_func(ins, outs): - aa, bb = ins - cc = outs[0] - - def _body(): - ib = tvm.ir_builder.create() - ib.emit( - tvm.call_extern( - "int32", - opname, - cc.access_ptr("w"), - aa.access_ptr("r"), - bb.access_ptr("r"), - ci_, - vh_, - vw_, - vc_, - kh_, - sh_, - core_id, - ) - ) - return ib.get() - - return _body() - - return tvm.decl_tensor_intrin( - conv.op, intrin_func, binds={data_vec: a_buffer, kernel_vec: b_buffer, conv: c_buffer} - ) - - -def _schedule_deconv(cfg, s, data_vec, kernel_vec, conv, output, last): - """schedule implementation""" - is_tune = bool(isinstance(cfg, (tvm.autotvm.ConfigEntity, tvm.autotvm.ConfigSpace))) - if is_tune: - vh_ = cfg["tile_oh"].size[-1] - vw_ = cfg["tile_ow"].size[-1] - vc_ = cfg["tile_co"].size[-1] - cfg = { - "ci_": tvm.var("ci_"), - "VH": vh_, - "VW": vw_, - "VC": vc_, - "tile_oh": vh_, - "tile_ow": vw_, - "tile_co": vc_, - "tile_ci": 4, - "ann_reduce": cfg["ann_reduce"].anns, - "ann_spatial": cfg["ann_spatial"].anns, - } # ,'reorder_0':cfg['reorder_0'].perm} - else: - pass - n, co, oh, ow, vh, vw, vc = s[conv].op.axis - ci, ki = s[conv].op.reduce_axis - s[conv].reorder(n, co, oh, ow, ci, vw, ki, vc) - if cfg["ann_reduce"][0] == "unroll": - s[conv].unroll(ci) - elif cfg["ann_reduce"][0] == "vec": - s[conv].vectorize(ci) - if cfg["ann_reduce"][1] == "unroll": - s[conv].unroll(ki) - elif cfg["ann_reduce"][1] == "vec": - s[conv].vectorize(ki) - if cfg["ann_spatial"][0] == "vec": - s[conv].vectorize(vh) - elif cfg["ann_spatial"][0] == "unroll": - s[conv].unroll(vh) - if cfg["ann_spatial"][1] == "vec": - s[conv].vectorize(vw) - elif cfg["ann_spatial"][1] == "unroll": - s[conv].unroll(vw) - if cfg["ann_spatial"][2] == "vec": - s[conv].vectorize(vc) - elif cfg["ann_spatial"][2] == "unroll": - s[conv].unroll(vc) - - # schedule conv - attrs = conv.op.attrs - vh_, vw_, vc_ = (attrs["VH"].value, attrs["VW"].value, attrs["VC"].value) - - # schedule fusion - if len(s[last].op.axis) == 4: - n, co, h, w = s[last].op.axis - co, vc = s[last].split(co, vc_) - ow, vw = s[last].split(w, vw_) - oh, vh = s[last].split(h, vh_) - s[last].reorder(n, co, oh, ow, vh, vw, vc) - else: - n, co, h, w, vc = s[last].op.axis - oh, vh = s[last].split(h, vh_) - ow, vw = s[last].split(w, vw_) - s[last].reorder(n, co, oh, ow, vh, vw, vc) - if last != output and isinstance(output.op, tvm.tensor.ComputeOp): - s[output].compute_inline() - if cfg["ann_spatial"][0] == "vec": - s[last].vectorize(vh) - elif cfg["ann_spatial"][0] == "unroll": - s[last].unroll(vh) - if cfg["ann_spatial"][1] == "vec": - s[last].vectorize(vw) - elif cfg["ann_spatial"][1] == "unroll": - s[last].unroll(vw) - if cfg["ann_spatial"][2] == "vec": - s[last].vectorize(vc) - elif cfg["ann_spatial"][2] == "unroll": - s[last].unroll(vc) - - s[conv].compute_at(s[last], ow) - - # mark parallel - s[last].parallel(co) - - if data_vec.op.name == "data_vec_undilated": - _, h, _, _, _, _, _, _, _ = s[data_vec].op.axis - else: - _, h, _, _, _, _, _ = s[data_vec].op.axis - s[data_vec].parallel(h) - - co, _, _, _, _, vc = s[kernel_vec].op.axis - s[kernel_vec].parallel(co) - if cfg["ann_spatial"][2] == "vec": - s[kernel_vec].vectorize(vc) - elif cfg["ann_spatial"][2] == "unroll": - s[kernel_vec].unroll(vc) - return s - - -def _conv_schedule_deconv(cfg, outs): - """schedule_conv2d_nchw_arm_cpu_deconv inner implementation""" - s = tvm.create_schedule([x.op for x in outs]) - - def _callback(op): - if "deconv_conv2d_output" in op.tag: - # schedule conv2d - output = op.output(0) - conv = op.input_tensors[0] - - sidx = 0 - if conv.op.input_tensors[0].name == "attr": - sidx = 1 - data_vec = conv.op.input_tensors[sidx] - - kernel_vec = conv.op.input_tensors[sidx + 1] - if kernel_vec.op.name == "kernel_vec": - kernel = kernel_vec.op.input_tensors[0] - else: - kernel = kernel_vec - if (isinstance(kernel.op, tvm.tensor.ComputeOp) and "dilate" in kernel.op.tag): - s[kernel].compute_inline() - - _schedule_deconv(cfg, s, data_vec, kernel_vec, conv, output, outs[0]) - - traverse_inline(s, outs[0].op, _callback) - return s diff --git a/predict/module/tvm_kernel/lite/python/arm_cpu/depthwise_conv2d.py b/predict/module/tvm_kernel/lite/python/arm_cpu/depthwise_conv2d.py deleted file mode 100644 index f54076eb849..00000000000 --- a/predict/module/tvm_kernel/lite/python/arm_cpu/depthwise_conv2d.py +++ /dev/null @@ -1,289 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -"""Depthwise convolution schedule for ARM CPU""" - -import tvm -from tvm import autotvm - -from topi.generic import schedule_depthwise_conv2d_nchw -from topi.nn import depthwise_conv2d_nchw, pad -from topi.util import traverse_inline, get_const_tuple -from topi.nn.util import get_pad_tuple - -# register customized schedule for arm cpu. -@autotvm.register_topi_schedule( - schedule_depthwise_conv2d_nchw, ["arm_cpu", "cpu"], ["custom"] -) -def schedule_depthwise_conv2d_nchw_arm(cfg, outs): - """Schedule depthwise conv2d - - Parameters - ---------- - cfg: ConfigEntity - The configuration of this template - outs: Array of Tensor - The computation graph description of depthwise convolution2d - in the format of an array of tensors. - - Returns - ------- - s: Schedule - The computation schedule for depthwise_conv2d nchw. - """ - s = _depthwise_schedule_spatial_pack(cfg, outs) - return s - - -@autotvm.register_topi_compute(depthwise_conv2d_nchw, ["arm_cpu", "cpu"], ["custom"]) -def depthwise_conv2d_arm_cpu(cfg, data, kernel, strides, padding, dilation, out_dtype): - """TOPI compute callback for depthwise_conv2d nchw - - Parameters - ---------- - cfg: ConfigEntity - The config for this template - - data : tvm.Tensor - 4-D with shape [batch, in_channel, in_height, in_width] - - kernel : tvm.Tensor - 4-D with shape [num_filter, multiplier, filter_height, filter_width] or - pre-packed 5-D with shape [num_filter_chunk, multiplier, filter_height, - filter_width, num_filter_block] - - strides : list of two ints - [stride_height, stride_width] - - padding : list of two ints - [pad_height, pad_width] - - dilation : list of two ints - [dilation_height, dilation_width] - - out_dtype: str - The output type. This is used for mixed precision. - - Returns - ------- - output : tvm.Tensor - 4-D with shape [batch, out_channel, out_height, out_width] - """ - - return _depthwise_spatial_pack( - cfg, data, kernel, strides, padding, dilation, out_dtype - ) - - -def _depthwise_spatial_pack(args, data, kernel, strides, padding, dilation, out_dtype): - """depthwise_conv2d_arm_cpu's inner implement""" - is_var, u_vh, u_vw, u_vc = args - out_dtype = out_dtype or data.dtype - - u_n, u_c, ih, iw = data.shape if is_var else get_const_tuple(data.shape) - - if isinstance(dilation, int): - dilation_h = dilation_w = dilation - else: - dilation_h, dilation_w = dilation - - if len(kernel.shape) == 4: - pre_packed = False - u_c, um, ukh, ukw = kernel.shape if is_var else get_const_tuple(kernel.shape) - else: # kernel tensor is pre packed - pre_packed = True - u_c, um, ukh, ukw, u_vc = kernel.shape if is_var else get_const_tuple(kernel.shape) - u_c = u_c * u_vc - - dilated_kernel_h = (ukh - 1) * dilation_h + 1 - dilated_kernel_w = (ukw - 1) * dilation_w + 1 - - pad_top, pad_left, pad_down, pad_right = get_pad_tuple( - padding, (dilated_kernel_h, dilated_kernel_w) - ) - hstr, wstr = strides if isinstance(strides, (tuple, list)) else (strides, strides) - u_oh = (ih + pad_top + pad_down - dilated_kernel_h) // hstr + 1 - u_ow = (iw + pad_left + pad_right - dilated_kernel_w) // wstr + 1 - # pack data - hpad = pad_top + pad_down - wpad = pad_left + pad_right - dopad = hpad != 0 or wpad != 0 - if dopad: - data_pad = pad( - data, - (0, 0, pad_top, pad_left), - (0, 0, pad_down, pad_right), - name="data_pad", - ) - else: - data_pad = data - - oh_div = u_oh // u_vh - ow_div = u_ow // u_vw - kvshape = (u_c // u_vc, um, ukh, ukw, u_vc) - ovshape = (u_n, u_c * um // u_vc, oh_div, u_ow // u_vw, u_vh, u_vw, u_vc) - oshape = (u_n, u_c * um, oh_div * u_vh, ow_div * u_vw) - - if dilation_h != 1 or dilation_w != 1: - # undilate input data - dvshape = (u_n, oh_div, ow_div, u_c, ukh, ukw, u_vh, u_vw) - data_vec = tvm.compute( - dvshape, - lambda n, h, w, c, kh, kw, vh, vw: data_pad[n][c][ - (h * u_vh + vh) * hstr + kh * dilation_h - ][(w * u_vw + vw) * wstr + kw * dilation_w], - name="data_vec_undilated", - ) - else: - dvshape = (u_n, oh_div, ow_div, u_c, u_vh * hstr + ukh - 1, u_vw * wstr + ukw - 1) - data_vec = tvm.compute( - dvshape, - lambda n, h, w, c, vh, vw: data_pad[n][c][h * u_vh * hstr + vh][ - w * u_vw * wstr + vw - ], - name="data_vec", - ) - - if pre_packed: - kernel_vec = kernel - else: - kernel_vec = tvm.compute( - kvshape, - lambda co, m, kh, kw, vc: kernel[co * u_vc + vc][m][kh][kw], - name="kernel_vec", - ) - - kh = tvm.reduce_axis((0, ukh), name="kh") - kw = tvm.reduce_axis((0, ukw), name="kw") - - if dilation_h != 1 or dilation_w != 1: - conv = tvm.compute( - ovshape, - lambda n, co, h, w, vh, vw, vc: tvm.sum( - data_vec[n, h, w, (co * u_vc + vc) // um, kh, kw, vh, vw].astype(out_dtype) - * kernel_vec[co // um, co % um, kh, kw, vc].astype(out_dtype), - axis=[kh, kw], - ), - name="depthwise_conv", - ) - else: - conv = tvm.compute( - ovshape, - lambda n, co, h, w, vh, vw, vc: tvm.sum( - data_vec[ - n, h, w, (co * u_vc + vc) // um, vh * hstr + kh, vw * wstr + kw - ].astype(out_dtype) - * kernel_vec[co // um, co % um, kh, kw, vc].astype(out_dtype), - axis=[kh, kw], - ), - name="depthwise_conv", - ) - - output = tvm.compute( - oshape, - lambda n, co, h, w: conv[n][co // u_vc][h // u_vh][w // u_vw][h % u_vh][w % u_vw][ - co % u_vc - ], - name="output_unpack", - tag="spatial_depthwise_conv_nchw_output", - ) - return output - - -def _schedule_spatial_pack(cfg, s, data_vec, kernel_vec, conv, output, last): - """schedule implementation""" - u_vc = cfg["tile_co"].size[-1] if not isinstance(cfg, dict) else cfg["VC"] - u_vh = cfg["tile_oh"].size[-1] if not isinstance(cfg, dict) else cfg["VH"] - u_vw = cfg["tile_ow"].size[-1] if not isinstance(cfg, dict) else cfg["VW"] - - n, co, oh, ow, vh, vw, vc = s[conv].op.axis - kh, kw = s[conv].op.reduce_axis - - if data_vec.op.name == "data_vec_undilated": - _, _, dv_ow, _, _, _, _, _ = s[data_vec].op.axis - else: - _, _, dv_ow, _, _, _ = s[data_vec].op.axis - - data_pad = data_vec.op.input_tensors[0] - - if isinstance(data_pad.op, tvm.tensor.ComputeOp): - s[data_pad].vectorize(list(s[data_pad].op.axis)[-1]) - s[data_pad].compute_at(s[data_vec], dv_ow) - - s[data_vec].vectorize(list(s[data_vec].op.axis)[-1]) - s[data_vec].compute_at(s[conv], ow) - - # schedule conv - s[conv].reorder(n, co, oh, ow, kh, kw, vh, vw, vc) - s[conv].unroll(kh) - s[conv].unroll(vh) - s[conv].vectorize(vw) - s[conv].unroll(vc) - s[conv].parallel(co) - - n, co, h, w = s[last].op.axis - co, vc = s[last].split(co, u_vc) - oh, vh = s[last].split(h, u_vh) - ow, vw = s[last].split(w, u_vw) - if last != output: - s[output].compute_inline() - s[last].vectorize(vw) - s[last].unroll(vc) - else: - s[last].vectorize(vw) - s[conv].compute_at(s[last], oh) - - # mark parallel - s[last].parallel(co) - - if data_vec.op.name == "data_vec_undilated": - _, h, _, _, _, _, _, _ = s[data_vec].op.axis - else: - _, h, _, _, _, _ = s[data_vec].op.axis - s[data_vec].parallel(h) - - if kernel_vec.op.name == "kernel_vec": - co, _, _, _, _ = s[kernel_vec].op.axis - if autotvm.GLOBAL_SCOPE.in_tuning: - # kernel packing will be pre-computed during compliation, so we skip - # this part to make tuning records correct - s[kernel_vec].pragma(co, "debug_skip_region") - else: - s[kernel_vec].parallel(co) - - return s - - -def _depthwise_schedule_spatial_pack(cfg, outs): - """schedule_depthwise_conv2d_nchw_arm's inner implement""" - outs = [outs] if isinstance(outs, tvm.tensor.Tensor) else outs - s = tvm.create_schedule([x.op for x in outs]) - - def _callback(op): - if op.tag == "spatial_depthwise_conv_nchw_output": - output = op.output(0) - conv = op.input_tensors[0] - data_vec = conv.op.input_tensors[0] - kernel_vec = conv.op.input_tensors[1] - if kernel_vec.op.name == "kernel_vec": - kernel = kernel_vec.op.input_tensors[0] - else: - kernel = kernel_vec - if isinstance(kernel.op, tvm.tensor.ComputeOp) and "dilate" in kernel.op.tag: - s[kernel].compute_inline() - - _schedule_spatial_pack(cfg, s, data_vec, kernel_vec, conv, output, outs[0]) - - traverse_inline(s, outs[0].op, _callback) - return s diff --git a/predict/module/tvm_kernel/lite/python/arm_cpu/matmul.py b/predict/module/tvm_kernel/lite/python/arm_cpu/matmul.py deleted file mode 100644 index 6430f24f6f2..00000000000 --- a/predict/module/tvm_kernel/lite/python/arm_cpu/matmul.py +++ /dev/null @@ -1,472 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -"""Conv2D schedule for ARM CPU""" -from __future__ import absolute_import as _abs - -import functools - -import tvm -from tvm import autotvm -import tvm.contrib.nnpack - -from topi.generic import schedule_conv2d_nchw -from topi.util import traverse_inline -from topi.nn import conv2d - - -@autotvm.register_topi_compute(conv2d, "arm_cpu", ["matmul"]) -def matmul_arm_cpu(cfg, a_, b_, layout, out_dtype): - """TOPI compute callback for - - Parameters - ---------- - cfg: ConfigEntity - The config for this template - - a_ : tvm.Tensor - 2-D with shape [M, k_] - - b_ : tvm.Tensor - 2-D with shape [k_, N] - - out_dtype: str - The output type. This is used for mixed precision. - - Returns - ------- - output : tvm.Tensor - 4-D with shape [batch, out_channel, out_height, out_width] - """ - args = _gen_cfg(cfg, a_, b_) - return _matmul_spatial_pack_asm(args, a_, b_, layout, out_dtype) - - -@autotvm.register_topi_schedule(schedule_conv2d_nchw, "arm_cpu", ["matmul"]) -def schedule_matmul_arm_cpu(cfg, outs): - """TOPI schedule callback for conv2d - - Parameters - ---------- - cfg: ConfigEntity - The config for this template - - outs: Array of Tensor - The computation graph description of conv2d - in the format of an array of tensors. - - Returns - ------- - s: Schedule - The computation schedule for conv2d. - """ - s = _matmul_schedule_asm(cfg, outs) - return s - - -def _gen_cfg(cfg, a_, b_): - """get best loginfo from cfg""" - if len(a_.shape) == 2: - w_, ci_ = get_const_tuple(a_.shape) - h_ = 1 - elif len(a_.shape) == 3: - _, ci_, w_ = get_const_tuple(a_.shape) - h_ = 1 - elif len(a_.shape) == 4: - _, ci_, h_, w_ = get_const_tuple(a_.shape) - else: - raise ValueError("not support shape: " + a_.shape) - - co_, k_ = get_const_tuple(b_.shape) - - oh, ow = cfg.axis(h_), cfg.axis(w_) - co = cfg.axis(co_) - k = cfg.reduce_axis(k_) - - oh, vh = cfg.define_split("tile_oh", oh, num_outputs=2) - ow, vw = cfg.define_split("tile_ow", ow, num_outputs=2) - oc, vc = cfg.define_split("tile_co", co, num_outputs=2) - - cfg.define_reorder( - "reorder_0", - [oc, oh, ow, k, vh, vw, vc], - policy="candidate", - candidate=[[oc, oh, ow, k, vh, vw, vc],], - ) - - vh_ = cfg["tile_oh"].size[-1] - vw_ = cfg["tile_ow"].size[-1] - vc_ = cfg["tile_co"].size[-1] - is_var = False - is_transpose = False - return (is_var, is_transpose, ci_, vh_, vw_, vc_) - - -def _matmul_spatial_pack_asm(args, a_, b_, layout, out_dtype): - """matmul_spatial_pack_asm's inner interace""" - is_var, is_transpose, ci_, vh_, vw_, vc_ = args - - # create workload according to raw arguments - out_dtype = out_dtype or a_.dtype - if layout == "NCHW": - batch, k_, h_, w_ = a_.shape if is_var else get_const_tuple(a_.shape) - n_, _ = b_.shape if is_var else get_const_tuple(b_.shape) - elif layout == "NCH": - batch, k_, h_ = a_.shape if is_var else get_const_tuple(a_.shape) - n_, _ = b_.shape if is_var else get_const_tuple(b_.shape) - w_ = 1 - elif layout == "NC": - w_, k_ = a_.shape if is_var else get_const_tuple(a_.shape) - n_, _ = b_.shape if is_var else get_const_tuple(b_.shape) - h_ = 1 - else: - raise ValueError("not support layout: " + layout) - - ki = tvm.reduce_axis((0, k_), name="ki") - type_map = { - "int8": "int32", - "uint8": "uint32", - "float32": "float32", - "float16": "float16", - } - acum_dtype = type_map[a_.dtype] - attrs = {"ci_": ci_, "vh_": vh_, "vw_": vw_, "vc_": vc_, "ACUM_DTYPE": acum_dtype} - - if layout == "NCHW": - h_div = h_ // vh_ - w_div = w_ // vw_ - n_div = n_ // vc_ - avshape = (batch, h_div, w_div, vh_, vw_, k_) - bvshape = (n_div, k_, vc_) - ovshape = (batch, n_div, h_div, w_div, vh_, vw_, vc_) - - a_vec = tvm.compute( - avshape, - lambda n, oh, ow, vh, vw, ci: a_[n][ci][oh * vh_ + vh][ow * vw_ + vw], - name="a_vec", - ) - b_vec = tvm.compute( - bvshape, lambda oc, ci, vc: b_[oc * vc_ + vc][ci], name="b_vec" - ) - - ma = tvm.compute( - ovshape, - lambda n, oc, oh, ow, vh, vw, vc: tvm.sum( - a_vec[n, oh, ow, vh, vw, ki].astype(out_dtype) - * b_vec[oc, ki, vc].astype(out_dtype), - axis=[ki], - ), - name="matmul", - attrs=attrs, - ) - - if is_transpose: - oshape = (batch, h_div * vh_, w_div * vw_, n_div * vc_) - - output = tvm.compute( - oshape, - lambda n, h, w, c: ma[n][c // vc_][h // vh_][w // vw_][h % vh_][w % vw_][ - c % vc_ - ], - name="output_unpack", - tag="asm_matmul_output", - ) - else: - oshape = (batch, n_div * vc_, h_div * vh_, w_div * vw_) - output = tvm.compute( - oshape, - lambda n, c, h, w: ma[n][c // vc_][h // vh_][w // vw_][h % vh_][w % vw_][ - c % vc_ - ], - name="output_unpack", - tag="asm_matmul_output", - ) - elif layout == "NCH": - w_div = w_ // vw_ - n_div = n_ // vc_ - avshape = (batch, w_div, vw_, k_) - bvshape = (n_div, k_, vc_) - ovshape = (batch, n_div, w_div, vw_, vc_) - oshape = (batch, n_div * vc_, w_div * vw_) - - a_vec = tvm.compute( - avshape, lambda b, om, vw, ci: a_[b][ci][om * vw_ + vw], name="a_vec" - ) - b_vec = tvm.compute( - bvshape, lambda on, ci, vc: b_[on * vc_ + vc][ci], name="b_vec" - ) - - ma = tvm.compute( - ovshape, - lambda b, on, om, vm, vn: tvm.sum( - a_vec[b, om, vm, ki].astype(out_dtype) - * b_vec[on, ki, vn].astype(out_dtype), - axis=[ki], - ), - name="matmul", - attrs=attrs, - ) - - output = tvm.compute( - oshape, - lambda b, n, m: ma[b][n // vc_][m // vw_][m % vw_][n % vc_], - name="output_unpack", - tag="asm_matmul_output", - ) - elif layout == "NC": - w_div = w_ // vw_ - n_div = n_ // vc_ - avshape = (w_div, vw_, k_) - bvshape = (n_div, k_, vc_) - ovshape = (w_div, n_div, vw_, vc_) - oshape = (w_div * vw_, n_div * vc_) - - a_vec = tvm.compute( - avshape, lambda om, vw, ci: a_[om * vw_ + vw][ci], name="a_vec" - ) - b_vec = tvm.compute( - bvshape, lambda on, ci, vc: b_[on * vc_ + vc][ci], name="b_vec" - ) - - ma = tvm.compute( - ovshape, - lambda om, on, vm, vn: tvm.sum( - a_vec[om, vm, ki].astype(out_dtype) - * b_vec[on, ki, vn].astype(out_dtype), - axis=[ki], - ), - name="matmul", - attrs=attrs, - ) - - output = tvm.compute( - oshape, - lambda m, n: ma[m // vw_][n // vc_][m % vw_][n % vc_], - name="output_unpack", - tag="asm_matmul_output", - ) - else: - raise ValueError("not support layout: " + layout) - - return output - - -def intrin_conv(args): - """intrin_conv is a conv inner interface""" - ( - ndim, - ci_, - vh_, - vw_, - vc_, - _, - _, - _, - _, - _, - _, - _, - _, - dtype, - acum_dtype, - opname, - core_id, - ) = args - ci_ = tvm.var("ci_") if ci_ is None else ci_ - kvshape = (ci_, vc_) - if ndim == 2: - dvshape = (vw_, ci_) - ovshape = (vw_, vc_) - - data_vec = tvm.placeholder(dvshape, name="a", dtype=dtype) - kernel_vec = tvm.placeholder(kvshape, name="b", dtype=dtype) - ci = tvm.reduce_axis((0, ci_), name="ci") - conv = tvm.compute( - ovshape, - lambda vw, vc: tvm.sum( - data_vec[vw, ci].astype(acum_dtype) - * kernel_vec[ci, vc].astype(acum_dtype), - axis=[ci], - ), - name="conv", - ) - else: - dvshape = (vh_, vw_, ci_) - ovshape = (vh_, vw_, vc_) - - data_vec = tvm.placeholder(dvshape, name="a", dtype=dtype) - kernel_vec = tvm.placeholder(kvshape, name="b", dtype=dtype) - ci = tvm.reduce_axis((0, ci_), name="ci") - conv = tvm.compute( - ovshape, - lambda vh, vw, vc: tvm.sum( - data_vec[vh, vw, ci].astype(acum_dtype) - * kernel_vec[ci, vc].astype(acum_dtype), - axis=[ci], - ), - name="conv", - ) - - stride_a = [ - functools.reduce(lambda x, y: x * y, dvshape[i + 1: len(dvshape)]) - for i in range(0, len(dvshape) - 1) - ] - stride_a.append(1) - stride_b = [ - functools.reduce(lambda x, y: x * y, kvshape[i + 1: len(kvshape)]) - for i in range(0, len(kvshape) - 1) - ] - stride_b.append(1) - stride_c = [ - functools.reduce(lambda x, y: x * y, ovshape[i + 1: len(ovshape)]) - for i in range(0, len(ovshape) - 1) - ] - stride_c.append(1) - - ab_ = tvm.decl_buffer( - data_vec.shape, data_vec.dtype, name="a_", offset_factor=1, strides=stride_a - ) - bb_ = tvm.decl_buffer( - kernel_vec.shape, kernel_vec.dtype, name="b_", offset_factor=1, strides=stride_b - ) - cb_ = tvm.decl_buffer( - conv.shape, conv.dtype, name="C", offset_factor=1, strides=stride_c - ) - - def intrin_func(ins, outs): - aa, bb = ins - cc = outs[0] - - def _body(): - b_ = tvm.ir_builder.create() - b_.emit( - tvm.call_extern( - "int32", - opname, - cc.access_ptr("w"), - aa.access_ptr("r"), - bb.access_ptr("r"), - ci_, - vh_, - vw_, - vc_, - core_id, - ) - ) - return b_.get() - - return _body() - - return tvm.decl_tensor_intrin( - conv.op, intrin_func, binds={data_vec: ab_, kernel_vec: bb_, conv: cb_} - ) - - -def _schedule_asm(cfg, s, a_vec, b_vec, mat, output, last): - """schedule implementation""" - is_transpose = 0 if not isinstance(cfg, dict) else cfg["is_transpose"] - attrs = mat.op.attrs - vh_, vw_, vc_ = (attrs["vh_"].value, attrs["vw_"].value, attrs["vc_"].value) - - # axis split and reorder - if len(a_vec.shape) == 3: - ow, oc = s[last].op.axis - oc, vc = s[last].split(oc, vc_) - ow, vw = s[last].split(ow, vw_) - s[last].reorder(ow, oc, vw, vc) - s[last].vectorize(vc) - oh = ow = oc - elif len(a_vec.shape) == 4: - n, oc, ow, vw, vc = s[last].op.axis - oc, vc = s[last].split(oc, vc_) - ow, vw = s[last].split(ow, vw_) - s[last].reorder(n, oc, ow, vw, vc) - elif len(a_vec.shape) == 6: - if is_transpose: - n, oh, ow, oc = s[last].op.axis - else: - n, oc, oh, ow = s[last].op.axis - oc, vc = s[last].split(oc, vc_) - oh, vh = s[last].split(oh, vh_) - ow, vw = s[last].split(ow, vw_) - s[last].reorder(n, oc, oh, ow, vh, vw, vc) - else: - raise ValueError("not support a_vec: " + str(len(a_vec.shape))) - if last != output and isinstance(output.op, tvm.tensor.ComputeOp): - s[output].compute_inline() - - s[mat].compute_at(s[last], ow) - s[mat].vectorize(s[mat].op.axis[-1]) - - # mark parallel - s[last].parallel(oh) - - if len(a_vec.shape) == 3: - om, _, _ = s[a_vec].op.axis - s[a_vec].compute_at(s[last], ow) - s[a_vec].parallel(om) - elif len(a_vec.shape) == 4: - _, om, _, _ = s[a_vec].op.axis - s[a_vec].compute_at(s[last], ow) - s[a_vec].parallel(om) - else: - _, oh, _, _, _, _ = s[a_vec].op.axis - s[a_vec].parallel(oh) - s[a_vec].vectorize(s[a_vec].op.axis[-1]) - s[a_vec].compute_inline() - - oc, _, _ = s[b_vec].op.axis - s[b_vec].parallel(oc) - s[b_vec].vectorize(s[b_vec].op.axis[-1]) - s[b_vec].compute_inline() - return s - - -def _matmul_schedule_asm(cfg, outs): - """schedule_conv2d_nchw schedule implementation""" - s = tvm.create_schedule([x.op for x in outs]) - - def _callback(op): - if "asm_matmul_output" in op.tag: - # schedule conv2d - output = op.output(0) - mat = op.input_tensors[0] - - sidx = 0 - if mat.op.input_tensors[0].name == "attr": - sidx = 1 - a_vec = mat.op.input_tensors[sidx] - b_vec = mat.op.input_tensors[sidx + 1] - - def recurs_inline(a_): - if a_.op.input_tensors: - a1 = a_.op.input_tensors[0] - if a1.shape == a_.shape: - s[a1].compute_inline() - recurs_inline(a1) - - def recurs_inline_(a_): - if isinstance(a_, tvm.tensor.ComputeOp): - if a_.op.input_tensors: - a1 = a_.op.input_tensors[0] - s[a1].compute_inline() - recurs_inline_(a1) - - recurs_inline_(a_vec) - recurs_inline_(b_vec) - - _schedule_asm(cfg, s, a_vec, b_vec, mat, output, outs[0]) - - traverse_inline(s, outs[0].op, _callback) - return s diff --git a/predict/module/tvm_kernel/lite/python/at_ops/__init__.py b/predict/module/tvm_kernel/lite/python/at_ops/__init__.py deleted file mode 100644 index 274ad9a7e51..00000000000 --- a/predict/module/tvm_kernel/lite/python/at_ops/__init__.py +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -"""Neural network operators""" -# from .at_lib import * -# from .at_gen import * diff --git a/predict/module/tvm_kernel/lite/python/at_ops/at_gen_strip.py b/predict/module/tvm_kernel/lite/python/at_ops/at_gen_strip.py deleted file mode 100644 index 519740c6fe2..00000000000 --- a/predict/module/tvm_kernel/lite/python/at_ops/at_gen_strip.py +++ /dev/null @@ -1,516 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -""" -This module is rule to generation tvm operate. you can use it like: -python3 at_gen_strip.py [x86:arm64:arm32] -""" -import os -import sys -import itertools -from functools import partial -from at_ops.at_lib import Deconv, tvm, ConvVar, BatchNorm, Eltwise, Resize, CaffeCrop, CaffePReLU -from at_ops.at_lib import FullConnection, Power, ArgMax, Concat, Pad, Pooling, Mean, MatMul, Softmax -from at_ops.at_lib import Activation, Exp, Split, Cast, ExpandDims, Tile, Range -from at_rt import at_runtime_reset - - -check_correctness = False -ARCH_TYPE = sys.argv[1] - -dtypes = ("float32",) # "float16", "uint8", "int8", "uint32", "int32" - -device_map = { - "x86": "llvm", - "arm64": "llvm -device=arm_cpu -model=kirin970 -target=arm64-linux-android", - "arm32": "llvm -device=arm_cpu -model=kirin970 -target=armv7a-linux-eabi -mfloat-abi=soft", -} - -lib_path_map = { - "x86": "../../../build/lib_x86/", - "arm64": "../../../build/lib_arm64/", - "arm32": "../../../build/lib_arm32/", -} - -best_log_map = { - "x86": None, - "arm64": None, - "arm32": None, -} - -lib_path = lib_path_map[ARCH_TYPE] -device = device_map[ARCH_TYPE] -if ARCH_TYPE == "arm64": - if dtypes[0] == "float16": - device += " -mattr=+fp16fml" - else: - device += " -mattr=+neon" -best_log = best_log_map[ARCH_TYPE] - -kwargs = { - "device": device, - "lib_path": lib_path, - "check_correctness": check_correctness, -} - -use_arm32 = ARCH_TYPE == "arm32" - -MAX_DIMS = 5 -const_op_list = [ - ( - "Deconvolution", - partial(Deconv, optype="Deconvolution"), - { - "ndim": (5,), - "dtype": dtypes, - "kernels": ((2, 2),), - "strides": ((2, 2),), - "pad": ((0, 0, 0, 0),), - "dilations": ((1, 1),), - "hasbias": (False, True), - "activation_type": ("NO_ACTIVATION",), - "cfg": [ - { - "CI": tvm.var("CI"), - "VH": 2, - "VW": 12, - "VC": 4, - "VI": 4, - "tile_oh": 2, - "tile_ow": 12, - "tile_co": 4, - "ann_reduce": ["none", "unroll"], - "ann_spatial": ["unroll", "unroll", "vec"], - }, - { - "CI": tvm.var("CI"), - "VH": 2, - "VW": 10, - "VC": 4, - "VI": 4, - "tile_oh": 2, - "tile_ow": 10, - "tile_co": 4, - "ann_reduce": ["none", "unroll"], - "ann_spatial": ["unroll", "unroll", "vec"], - }, - { - "CI": tvm.var("CI"), - "VH": 2, - "VW": 16, - "VC": 4, - "VI": 4, - "tile_oh": 2, - "tile_ow": 16, - "tile_co": 4, - "ann_reduce": ["none", "unroll"], - "ann_spatial": ["unroll", "unroll", "vec"], - }, - { - "CI": tvm.var("CI"), - "VH": 2, - "VW": 8, - "VC": 4, - "VI": 4, - "tile_oh": 2, - "tile_ow": 8, - "tile_co": 4, - "ann_reduce": ["none", "unroll"], - "ann_spatial": ["unroll", "unroll", "vec"], - }, - { - "CI": tvm.var("CI"), - "VH": 2, - "VW": 4, - "VC": 4, - "VI": 4, - "tile_oh": 2, - "tile_ow": 4, - "tile_co": 4, - "ann_reduce": ["none", "unroll"], - "ann_spatial": ["unroll", "unroll", "vec"], - }, - { - "CI": tvm.var("CI"), - "VH": 2, - "VW": 2, - "VC": 4, - "VI": 4, - "tile_oh": 2, - "tile_ow": 2, - "tile_co": 4, - "ann_reduce": ["none", "unroll"], - "ann_spatial": ["unroll", "unroll", "vec"], - }, - ], - }, - ), - ( - "Convolution", - partial(ConvVar, optype="Convolution"), - { - "ndim": (4,), - "layout": ("NCHW",), - "dtype": dtypes, - "kernels": ((1, 1), (3, 3), (5, 5),), - "strides": ((1, 1), (2, 2)), - "pad": ((1, 1, 1, 1), (0, 0, 0, 0), (2, 2, 2, 2)), - "dilations": ((1, 1),), - "hasbias": (False, True), - "activation_type": ("NO_ACTIVATION", "RELU"), - "cfg": [ - { - "CI": tvm.var("CI"), - "VH": 1, - "VW": 1, - "VC": 1, - "VI": 1, - "tile_oh": 1, - "tile_ow": 1, - "tile_co": 1, - "ann_reduce": ["none", "unroll"], - "ann_spatial": ["unroll", "unroll", "vec"], - "core_id": 0, - }, - ], - }, - ), - ( - "ConvolutionDepthwise", - partial(ConvVar, optype="ConvolutionDepthwise"), - { - "ndim": (4,), - "layout": ("NCHW",), - "dtype": dtypes, - "kernels": ((2, 2), (3, 3),), - "strides": ((1, 1),), - "pad": ((0, 0, 0, 0), (0, 1, 0, 1), (1, 0, 1, 0), (1, 1, 1, 1),), - "dilations": ((1, 1),), - "hasbias": (False, True), - "activation_type": ("NO_ACTIVATION", "RELU"), - "channel_multiplier": (1,), - "cfg": [ - { - "CI": tvm.var("CI"), - "VH": 1, - "VW": 1, - "VC": 1, - "VI": 1, - "tile_oh": 1, - "tile_ow": 1, - "tile_co": 1, - "ann_reduce": ["none", "unroll"], - "ann_spatial": ["unroll", "unroll", "vec"], - "core_id": 0, - }, - ], - }, - ), - ( - "DeConvolutionDepthwise", - partial(ConvVar, optype="DeConvolutionDepthwise"), - { - "ndim": (4,), - "layout": ("NCHW",), - "dtype": dtypes, - "kernels": ((1, 1), (2, 2), (3, 3),), - "strides": ((1, 1), (2, 2),), - "pad": ((0, 0, 0, 0), (1, 0, 1, 0), (1, 1, 1, 1),), - "dilations": ((1, 1),), - "hasbias": (False, True), - "activation_type": ("NO_ACTIVATION", "RELU"), - "channel_multiplier": (1,), - "cfg": [ - { - "CI": tvm.var("CI"), - "VH": 1, - "VW": 1, - "VC": 1, - "VI": 1, - "tile_oh": 1, - "tile_ow": 1, - "tile_co": 1, - "ann_reduce": ["none", "unroll"], - "ann_spatial": ["unroll", "unroll", "vec"], - "core_id": 0, - }, - ], - }, - ), - ( - "BatchNorm", - BatchNorm, - {"ndim": (4,), "dtype": dtypes, "optype": ("TFBatchNorm",), "axis": (1, 3,)}, - ), - ( - "BiasAdd", - BatchNorm, - {"ndim": (2, 4), "dtype": dtypes, "optype": ("TFBiasAdd",), "axis": (1, 3)}, - ), - ( - "CaffeBatchNorm", - BatchNorm, - {"ndim": (2, 4), "dtype": dtypes, "optype": ("CaffeBatchNorm",), "axis": (1, 3)}, - ), - ( - "Scale", - BatchNorm, - {"ndim": (2, 4), "dtype": dtypes, "optype": ("CaffeScale",), "axis": (1,)}, - ), - ( - "Eltwise", - Eltwise, - { - "ndim_a": tuple(range(0, MAX_DIMS + 1)), - "ndim_b": tuple(range(0, MAX_DIMS + 1)), - "dtype": dtypes, - "mode": ("add", "subtract", "multiply", "divide", "maximum"), - }, - ), - ( - "Add", - Eltwise, - { - "ndim_a": tuple(range(0, MAX_DIMS + 1)), - "ndim_b": tuple(range(0, MAX_DIMS + 1)), - "dtype": dtypes, - "mode": ("add",), - }, - ), - ( - "Sub", - Eltwise, - { - "ndim_a": tuple(range(0, MAX_DIMS + 1)), - "ndim_b": tuple(range(0, MAX_DIMS + 1)), - "dtype": dtypes, - "mode": ("subtract",), - }, - ), - ( - "Mul", - Eltwise, - { - "ndim_a": tuple(range(0, MAX_DIMS + 1)), - "ndim_b": tuple(range(0, MAX_DIMS + 1)), - "dtype": dtypes, - "mode": ("multiply",), - }, - ), - ( - "RealDiv", - Eltwise, - { - "ndim_a": tuple(range(0, MAX_DIMS + 1)), - "ndim_b": tuple(range(0, MAX_DIMS + 1)), - "dtype": dtypes, - "mode": ("divide",), - }, - ), - ( - "Maximum", - Eltwise, - { - "ndim_a": tuple(range(0, MAX_DIMS + 1)), - "ndim_b": tuple(range(0, MAX_DIMS + 1)), - "dtype": dtypes, - "mode": ("maximum",), - }, - ), - ( - "ResizeBilinear", - Resize, - { - "ndim": (4,), - "dtype": dtypes, - "method": ("bilinear",), # "bicubic" - "align_corners": (True, False), - }, - ), - ( - "ResizeNearestNeighbor", - Resize, - { - "ndim": (4,), - "dtype": dtypes, - "method": ("nearest_neighbor",), # "bicubic" - "align_corners": (True, False), - }, - ), - ( - "CaffeCrop", - CaffeCrop, - {"ndim": (4,), "dtype": dtypes, "axis": tuple(range(0, 4))}, - ), - ( - "CaffePReLU", - CaffePReLU, - {"ndim": (2, 4), "dtype": dtypes, "channel_shared": (True, False)}, - ), - ( - "FullConnection", - FullConnection, - {"ndim_a": (2, 4), "dtype": dtypes, "has_bias": (True, False)}, - ), - ("Power", Power, {"ndim": tuple(range(1, MAX_DIMS + 1)), "dtype": dtypes}), - ( - "ArgMax", - ArgMax, - { - "ndim": tuple(range(1, MAX_DIMS + 1)), - "dtype": dtypes, - "axis": tuple(range(0, MAX_DIMS)), # not support None - "keep_dims": (True, False), - "top_k": (1,), - "out_dtype": dtypes, - }, - ), - ( - "Concat", - Concat, - { - "ndim": tuple(range(1, MAX_DIMS + 1)), - "dtype": dtypes, - "input_num": tuple(range(2, 6 + 1)), - "axis": tuple(range(0, MAX_DIMS)), - }, - ), - ( - "Pad", - Pad, - { - "ndim": tuple(range(2, MAX_DIMS + 1)), - "dtype": dtypes, - "paddingmode": ("CONSTANT", "REFLECT", "SYMMETRIC"), - }, - ), - ( - "Pooling", - Pooling, - { - "ndim": (4,), - "dtype": dtypes, - "pooling_mode": ("max", "avg"), - "caffe_mode": (True, False), - "kernel": ((1, 1), (2, 2), (3, 3), (5, 5)), - "stride": ((1, 1), (2, 2), (3, 3)), - "pad": ((0, 0, 0, 0), (0, 1, 0, 1), (1, 1, 1, 1)), - "use_global": (True, False), - }, - ), - ( - "Mean", - Mean, - { - "ndim": (4,), - "dtype": dtypes, - "axis": ( - (0,), - (1,), - (2,), - (3,), - (0, 1), - (0, 2), - (0, 3), - (1, 2), - (1, 3), - (2, 3), - (0, 1, 2), - (0, 1, 3), - (0, 2, 3), - (1, 2, 3), - (0, 1, 2, 3), - ), - "keep_dims": (True, False), - }, - ), - ( - "MatMul", - MatMul, - { - "ndim_a": (2,), - "ndim_b": (2,), - "dtype": dtypes, - "transpose_a": (True, False), - "transpose_b": (True, False), - }, - ), - ( - "Softmax", - Softmax, - { - "ndim": tuple(range(1, MAX_DIMS + 1)), - "dtype": dtypes, - "axis": tuple(range(0, MAX_DIMS)), - }, - ), - ( - "Activation", - Activation, - { - "ndim": tuple(range(1, MAX_DIMS + 1)), - "dtype": dtypes, - "optype": ("NO_ACTIVATION", "RELU", "RELU6", "SIGMOID"), - }, - ), - ("Exp", Exp, {"ndim": tuple(range(1, MAX_DIMS + 1)), "dtype": dtypes}), - ( - "Split", - Split, - { - "ndim": tuple(range(1, MAX_DIMS + 1)), - "dtype": dtypes, - "output_num": tuple(range(1, 5)), - "axis": tuple(range(0, MAX_DIMS)), - }, - ), - ( - "Cast", - Cast, - { - "ndim": tuple(range(1, MAX_DIMS + 1)), - "src_dtype": dtypes, - "dst_dtype": dtypes, - }, - ), - ( - "ExpandDims", - ExpandDims, - { - "ndim": tuple(range(1, MAX_DIMS + 1)), - "dtype": dtypes, - "axis": tuple(range(0, MAX_DIMS)), - }, - ), - ("Tile", Tile, {"ndim": tuple(range(1, MAX_DIMS + 1)), "dtype": dtypes}), - ("Range", Range, {"out_dtype": ("float32", "uint32", "int32")}), -] - - -def gen_const_libs(some_op=None): - for optype, func, attr in const_op_list: - if some_op and some_op != optype: - continue - for values in itertools.product(*attr.values()): - args = dict((k, v) for k, v in zip(attr.keys(), values)) - func(device=device, lib_path=lib_path, **args) - - -if __name__ == "__main__": - if not os.path.exists(lib_path): - os.makedirs(lib_path) - # skip best_history log: - with tvm.target.create(device): - with at_runtime_reset.AtRuntimeReset(): - gen_const_libs() diff --git a/predict/module/tvm_kernel/lite/python/at_ops/at_lib.py b/predict/module/tvm_kernel/lite/python/at_ops/at_lib.py deleted file mode 100644 index 655064b29e1..00000000000 --- a/predict/module/tvm_kernel/lite/python/at_ops/at_lib.py +++ /dev/null @@ -1,1193 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -""" -This module is rule to generation tvm operate, call by at_gen_strip.py -""" -import numpy as np -import tvm -import topi -from topi.image import resize -from topi.nn import mirror_pad -from topi import tag -import topi.testing - -from arm_cpu.deconv import _conv_spatial_pack_deconv, schedule_conv2d_nchw_arm_cpu_deconv -from arm_cpu.conv2d import _conv_spatial_pack_asm, schedule_conv2d_nchw_arm_cpu -from arm_cpu.matmul import _matmul_spatial_pack_asm, _matmul_schedule_asm -from arm_cpu.depthwise_conv2d import _depthwise_spatial_pack, schedule_depthwise_conv2d_nchw_arm -from config_tool import activation_enum_map - -map_conv = { - 'Convolution': "Conv2D", - 'ConvolutionDepthwise': "DepthwiseConv2D", - 'Deconvolution': "DeConv2D", - 'DeConvolutionDepthwise': "DeDepthwiseConv2D", -} - - -def Genlib(sch, tensor_list, device, opname, lib_path, print_lower=False): - if print_lower: - print(tvm.lower(sch, tensor_list, simple_mode=True)) - ctx = tvm.context(device, 0) - func_o = tvm.build(sch, tensor_list, device + " --system-lib", name=opname) - func_so = tvm.build(sch, tensor_list, device, name=opname) - func_o.save(lib_path + opname + ".o", "o") - return func_o, func_so, ctx - - -def AsType(as_input, dtype): - if as_input.dtype == dtype: - return as_input - return tvm.compute(as_input.shape, - lambda *i: as_input(*i).astype(dtype), - tag="injective") - - -@tvm.tag_scope(tag=tag.ELEMWISE) -def TopiNNrelu6(x): - return tvm.compute(x.shape, lambda *i: tvm.min(tvm.max(x(*i), tvm.const(0, x.dtype)), tvm.const(6, x.dtype))) - - -def TopiActivation(in_tensor, a_type, memcpy=False): - ''' - activativation - Args: - in_tensor: - a_type: - memcpy: - - Returns: - ''' - if a_type == 'NO_ACTIVATION': - if memcpy: - return tvm.compute(in_tensor.shape, lambda *i: in_tensor[i], tag=tag.ELEMWISE) - return in_tensor - if a_type == 'RELU': - return topi.nn.relu(in_tensor) - if a_type == 'RELU6': - return TopiNNrelu6(in_tensor) - if a_type == 'SIGMOID': - if in_tensor.dtype in ["uint8", "int8", "uint32", "int32"]: - a_fp32 = AsType(in_tensor, 'float32') - out_tensor = topi.sigmoid(a_fp32) - return AsType(out_tensor, in_tensor.dtype) - return topi.sigmoid(in_tensor) - raise ValueError("not support activation type" + a_type) - - -def Deconv(device="llvm", lib_path="./", optype=None, - ndim=None, dtype=None, kernels=None, - strides=None, pad=None, dilations=None, - hasbias=None, activation_type=None, - config_entity=None, impl_dtype=None, - use_arm32=False, cfg=None): - ''' - Deconvolution - Args: - device: - lib_path: - optype: - ndim: - dtype: - kernels: - strides: - pad: - dilations: - hasbias: - activationType: - configEntity: - impl_dtype: - use_arm32: - cfg: - - Returns: - ''' - if cfg is None: - cfg = {'CI': tvm.var('ci'), 'VH': 2, 'VW': 2, 'VC': 4, 'VI': 4, - 'tile_oh': 2, 'tile_ow': 2, 'tile_co': 4, - 'ann_reduce': ['none', 'none'], - "ann_spatial": ['none', 'none', 'none'] - } - has_bias = hasbias - batch = tvm.var("batch") - in_channel = tvm.var("in_channel") - in_height, in_width = tvm.var("in_height"), tvm.var("in_width") - kh, kw = kernels - ow = cfg['VW'] - oh = cfg['VH'] - oc = cfg['VC'] - op_name = "%s_ndim%d_%s_k%d_s%d_p%d%d%d%d_d%d_act%d_vc%d_vh%d_vw%d_hasbias%d" % (\ - map_conv[optype], ndim, dtype,\ - kh, strides[0], pad[0], pad[1], pad[2], pad[3], dilations[0],\ - activation_enum_map[activation_type], oc, oh, ow, hasbias) - opname = op_name - print("DEconv", opname, config_entity) - - if impl_dtype is None: - impl_dtype = dtype - - out_channel = tvm.var("out_channel") - - # define placeholder - input_tensor = in_tensor = tvm.placeholder((batch, in_channel, in_height, in_width, 4), \ - dtype=dtype, name='in_tensor') - temp_tensor = kernel_tensor = tvm.placeholder((in_channel*4, out_channel, kh, kw), dtype=dtype, \ - name='kernel_tensor') - if has_bias: - bias = tvm.placeholder((out_channel,), dtype=dtype, name='bias') - bias1 = topi.reshape(bias, (out_channel, 1, 1)) - - if impl_dtype != dtype: - input_tensor = AsType(input_tensor, impl_dtype) - temp_tensor = AsType(temp_tensor, impl_dtype) - if has_bias: - bias1 = AsType(bias1, impl_dtype) - - # define compute & schedule - cfg1 = (True, 1, 1, 1) if cfg is None else (True, cfg["tile_oh"], cfg["tile_ow"], cfg["tile_co"]) - out_tensor = _conv_spatial_pack_deconv(cfg1, input_tensor, temp_tensor, out_dtype=impl_dtype) - - if has_bias: - out_tensor = tvm.compute(out_tensor.shape, lambda n, co, h, w, c4: \ - out_tensor[n, co, h, w, c4] + bias1[co*4 + c4][0][0], tag="injective") - out_tensor = TopiActivation(out_tensor, activation_type) - if impl_dtype != dtype: - out_tensor = AsType(out_tensor, dtype) - - # create schedule - if use_arm32: - s = tvm.create_schedule(out_tensor.op) - else: - s = schedule_conv2d_nchw_arm_cpu_deconv(cfg, [out_tensor]) - - attr = [batch, in_channel, in_height, in_width, out_channel, in_tensor, kernel_tensor] - if has_bias: attr.append(bias) - attr.append(out_tensor) - tensor_list = attr - - Genlib(s, tensor_list, device, opname, lib_path) - - -def ConvVar(device="llvm", lib_path="./", optype=None,\ - ndim=None, layout=None, dtype=None, kernels=None,\ - strides=None, pad=None, dilations=None,\ - hasbias=None, activation_type=None,\ - config_entity=None, impl_dtype=None, channel_multiplier=None,\ - use_arm32=False, cfg=None): - ''' - convolution - Args: - device: - lib_path: - optype: - ndim: - layout: - dtype: - kernels: - strides: - pad: - dilations: - hasbias: - activationType: - configEntity: - impl_dtype: - channel_multiplier: - use_arm32: - cfg: - - Returns: - ''' - use_depthwise = optype == 'ConvolutionDepthwise' - use_deconv = optype == 'Deconvolution' - use_deconv_depthwise = optype == 'DeConvolutionDepthwise' - has_bias = hasbias - - ow = 1 if cfg is None else cfg['VW'] - oh = 1 if cfg is None else cfg['VH'] - oc = 1 if cfg is None else cfg['VC'] - kh, kw = kernels - op_name = "%s_ndim%d_%s_k%d_s%d_p%d%d%d%d_d%d_act%d_vc%d_vh%d_vw%d_hasbias%d" % ( \ - map_conv[optype], ndim, dtype, \ - kh, strides[0], pad[0], pad[1], pad[2], pad[3], dilations[0], \ - activation_enum_map[activation_type], oc, oh, ow, hasbias) - batch = tvm.var("batch") - in_channel = tvm.var("in_channel") - in_height, in_width = tvm.var("in_height"), tvm.var("in_width") - pad_up, pad_down, pad_left, pad_right = pad - opname = op_name - - print("Conv", opname, config_entity) - - if impl_dtype is None: - impl_dtype = dtype - - if use_depthwise: - multiplier = channel_multiplier - out_channel = in_channel * multiplier - elif use_deconv_depthwise: - multiplier = channel_multiplier - out_channel = in_channel * multiplier - else: - out_channel = tvm.var("out_channel") - - # define placeholder - input_tensor = in_tensor = tvm.placeholder((batch, in_channel, in_height, in_width), dtype=dtype, name='in_tensor') - - if use_depthwise: - temp_tensor = kernel_tensor = tvm.placeholder((in_channel, multiplier, kh, kw), dtype=dtype,\ - name='kernel_tensor') - elif use_deconv: - temp_tensor = kernel_tensor = tvm.placeholder((in_channel, out_channel, kh, kw), dtype=dtype,\ - name='kernel_tensor') - elif use_deconv_depthwise: - temp_tensor = kernel_tensor = tvm.placeholder((in_channel, multiplier, kh, kw), dtype=dtype,\ - name='kernel_tensor') - else: - temp_tensor = kernel_tensor = tvm.placeholder((out_channel, in_channel, kh, kw), dtype=dtype,\ - name='kernel_tensor') - if has_bias: - bias = tvm.placeholder((out_channel,), dtype=dtype, name='bias') - bias1 = topi.reshape(bias, (out_channel, 1, 1)) - - if impl_dtype != dtype: - input_tensor = AsType(input_tensor, impl_dtype) - temp_tensor = AsType(temp_tensor, impl_dtype) - if has_bias: - bias1 = AsType(bias1, impl_dtype) - - # define compute & schedule - if pad_up != pad_down or pad_left != pad_right: - input_tensor = topi.nn.pad(input_tensor, [0, 0, pad_up, pad_left], [0, 0, pad_down, pad_right], name='data_pad') - padding = 0, 0 - else: - padding = pad_up, pad_left - if use_depthwise: - cfg1 = (True, 1, 1, 1) if cfg is None else (True, cfg["tile_oh"], cfg["tile_ow"], cfg["tile_co"]) - out_tensor = _depthwise_spatial_pack(cfg1, input_tensor, temp_tensor, strides, padding, dilations,\ - out_dtype=impl_dtype) - elif use_deconv: - - def GetInput(input_tensor, temp_tensor, padding): - _, out_c, filter_h, filter_w = temp_tensor.shape - if out_c is None: - print("temp_tensor.shape err") - stride_h, stride_w = strides - # dilate stage - dilated_input = topi.nn.dilate(input_tensor, [1, 1, stride_h, stride_w], - name='DilatedInput') - # padding stage - fpad_top, fpad_left, fpad_bottom, fpad_right = topi.nn.get_pad_tuple(padding, ( - filter_h, filter_w)) - bpad_top = filter_h - 1 - fpad_top - bpad_bottom = filter_h - 1 - fpad_bottom - bpad_left = filter_w - 1 - fpad_left - bpad_right = filter_w - 1 - fpad_right - padded_input = topi.nn.pad(dilated_input, \ - [0, 0, bpad_top, bpad_left], \ - [0, 0, bpad_bottom, bpad_right], \ - name='PaddedInput') - return padded_input - - special_deconv = kh == 2 and kw == 2 and strides[0] == 2 and strides[1] == 2 - # special_deconv = False - if special_deconv: - out_tensor = OptimalOut(input_tensor, temp_tensor, in_channel) - else: - out_tensor = BaseImplementation(input_tensor, temp_tensor, GetInput, layout, padding) - elif use_deconv_depthwise: - def GetInput(input_tensor, temp_tensor, padding): - _, out_c, filter_h, filter_w = temp_tensor.shape - if out_c is None: - print("temp_tensor.shape err") - stride_h, stride_w = strides - # dilate stage - dilated_input = topi.nn.dilate(input_tensor, [1, 1, stride_h, stride_w], - name='DilatedInput') - # padding stage - fpad_top, fpad_left, fpad_bottom, fpad_right = topi.nn.get_pad_tuple(padding, ( - filter_h, filter_w)) - bpad_top = filter_h - 1 - fpad_top - bpad_bottom = filter_h - 1 - fpad_bottom - bpad_left = filter_w - 1 - fpad_left - bpad_right = filter_w - 1 - fpad_right - padded_input = topi.nn.pad(dilated_input, \ - [0, 0, bpad_top, bpad_left], \ - [0, 0, bpad_bottom, bpad_right], \ - name='PaddedInput') - return padded_input - - temp_tensor = topi.flip(temp_tensor, axis=-1) - temp_tensor = topi.flip(temp_tensor, axis=-2) - out_tensor = topi.nn.depthwise_conv2d_nchw(GetInput(input_tensor, temp_tensor, padding), temp_tensor, (1, 1), \ - padding, (1, 1), out_dtype=input_tensor.dtype) - else: - cfg1 = (True, 1, 1, 1) if cfg is None else (True, cfg["tile_oh"], cfg["tile_ow"], cfg["tile_co"]) - out_tensor = _conv_spatial_pack_asm(cfg1, input_tensor, temp_tensor, strides, padding, dilations,\ - out_dtype=impl_dtype) - - if has_bias: - out_tensor = tvm.compute(out_tensor.shape, lambda n, co, h, w: out_tensor[n, co, h, w] + bias1[co][0][0],\ - tag="injective") - out_tensor = TopiActivation(out_tensor, activation_type) - if impl_dtype != dtype: - out_tensor = AsType(out_tensor, dtype) - - # create schedule - if use_arm32: - s = tvm.create_schedule(out_tensor.op) - elif use_depthwise: - s = schedule_depthwise_conv2d_nchw_arm(cfg, [out_tensor]) - elif use_deconv: - if special_deconv: - s = tvm.create_schedule([out_tensor.op]) - else: - s = topi.generic.schedule_conv2d_nchw([out_tensor]) - elif use_deconv_depthwise: - s = tvm.create_schedule([out_tensor.op]) - else: - s = schedule_conv2d_nchw_arm_cpu([out_tensor]) - - # generate lib - attr = [batch, in_channel, in_height, in_width, out_channel, in_tensor, kernel_tensor] - tensor_list = [*attr, bias, out_tensor] if has_bias else [*attr, out_tensor] - Genlib(s, tensor_list, device, opname, lib_path) - - -def BaseImplementation(input_tensor, temp_tensor, get_input, layout, padding): - temp_tensor = topi.flip(temp_tensor, axis=-1) - temp_tensor = topi.flip(temp_tensor, axis=-2) - temp_tensor = topi.transpose(temp_tensor, axes=(1, 0, 2, 3)) - out_tensor = topi.nn.conv2d(get_input(input_tensor, temp_tensor, padding), temp_tensor, (1, 1), padding, (1, 1), - layout=layout, out_dtype=input_tensor.dtype) - return out_tensor - - -def OptimalOut(input_tensor, temp_tensor, in_channel): - ''' - deconv compute - Args: - input_tensor: - temp_tensor: - in_channel: - - Returns: - ''' - temp_tensor = topi.transpose(temp_tensor, axes=(1, 0, 2, 3)) - out_shape = [] - for i in range(len(input_tensor.shape)): - if i == 0: - out_shape.append(input_tensor.shape[i]) - continue - if i == 1: - out_shape.append(temp_tensor.shape[0]) - continue - out_shape.append(2 * input_tensor.shape[i]) - rc = tvm.reduce_axis((0, in_channel), name='rc') - return tvm.compute(out_shape, lambda i, j, k, l:\ - tvm.sum(input_tensor[i, rc, k // 2, l // 2].astype(input_tensor.dtype) *\ - temp_tensor[j, rc, k % 2, l % 2].astype(input_tensor.dtype), axis=[rc])) - - -def Concat(device="llvm", lib_path="./", - ndim=None, dtype=None, input_num=None, axis=None): - ''' - concat - Args: - device: - lib_path: - all_tensors: - ndim: - dtype: - input_num: - axis: - - Returns: - ''' - if axis >= ndim: - return - shapes = [] - for i in range(input_num): - shape = [] - for j in range(ndim): - if j == axis: - shape.append(tvm.var("axis" + str(i))) - else: - shape.append(tvm.var("n" + str(j))) - shapes.append(shape) - in_tensor = [tvm.placeholder(shape, dtype=dtype, name='in_tensor%d' % i) for i, shape in enumerate(shapes)] - opname = "Concat_ndim%d_%s_input_num%d_axis%d" % (ndim, dtype, input_num, axis) - print(opname) - - # define compute - out_tensor = topi.concatenate(tuple(in_tensor), axis) - tensor_list = in_tensor + [out_tensor] - if ndim < 5: - s = topi.generic.schedule_concatenate(out_tensor) - else: - s = tvm.create_schedule(out_tensor.op) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Activation(device="llvm", lib_path="./", - ndim=None, dtype=None, optype=None): - ''' - activation - Args: - device: - lib_path: - ndim: - dtype: - optype: - - Returns: - ''' - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - opname = "Activation_ndim%d_%s_%s" % (ndim, dtype, optype) - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - out_tensor = TopiActivation(in_tensor, optype, memcpy=True) - tensor_list = [in_tensor, out_tensor] - s = tvm.create_schedule(out_tensor.op) - Genlib(s, tensor_list, device, opname, lib_path) - - -def BatchNorm(device="llvm", lib_path="./", - ndim=None, dtype=None, optype=False, axis=None): - ''' - batchnorm - Args: - device: - lib_path: - ndim: - dtype: - optype: - axis: - - Returns: - ''' - if axis >= ndim: - return - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - channel = shape[axis] - eps = tvm.var("epsilon", dtype="float32") - opname = optype + ("_ndim%d_%s_axis%d" % (ndim, dtype, axis)) - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - mean = tvm.placeholder((channel,), dtype=dtype, name='mean') - variance = tvm.placeholder((channel,), dtype=dtype, name='var') - scale = tvm.placeholder((channel,), dtype=dtype, name='scale') - offset = tvm.placeholder((channel,), dtype=dtype, name='offset') - - variance_sqrt = tvm.compute((channel,), lambda i: tvm.sqrt(variance[i] + eps.astype(dtype))) - if optype == "TFBatchNorm": - out_tensor = tvm.compute(shape, lambda *idx: ((in_tensor[idx] - mean[idx[axis]]) / variance_sqrt[idx[axis]]) *\ - scale[idx[axis]] + offset[idx[axis]]) - tensor_list = [eps, in_tensor, scale, offset, mean, variance, out_tensor] - elif optype == "CaffeBatchNorm": - out_tensor = tvm.compute(shape, lambda *idx: (in_tensor[idx] - mean[idx[axis]]) / variance_sqrt[idx[axis]]) - tensor_list = [eps, in_tensor, mean, variance, out_tensor] - elif optype == "CaffeScale": - out_tensor = tvm.compute(shape, lambda *idx: in_tensor[idx] * scale[idx[axis]] + offset[idx[axis]]) - tensor_list = [in_tensor, scale, offset, out_tensor] - elif optype == "TFBiasAdd": - out_tensor = tvm.compute(shape, lambda *idx: in_tensor[idx] + offset[idx[axis]]) - tensor_list = [in_tensor, offset, out_tensor] - else: - raise RuntimeError("no support for {}".format(optype)) - - # define schedule & generate lib - s = tvm.create_schedule(out_tensor.op) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Pooling(device="llvm", lib_path="./", - ndim=None, dtype=None, pooling_mode=None, kernel=None, stride=None, pad=None, caffe_mode=None, - use_global=False): - ''' - pooling - Args: - device: - lib_path: - ndim: - dtype: - pooling_mode: - kernel: - stride: - pad: - caffe_mode: - use_global: - - Returns: - ''' - shape = [tvm.var("n" + str(i)) for i in range(0, ndim)] - layout = 'NCHW' - if use_global: - opname = "GlobalPooling_ndim%d_%s_%s" % (ndim, dtype, pooling_mode) - else: - kernel_h, kernel_w = kernel - stride_h, stride_w = stride - pad_up, pad_down, pad_left, pad_right = pad - if pad_up == 0 and pad_down == 0 and pad_left == 0 and pad_right == 0 and caffe_mode: - caffe_mode = False - opname = "Pooling_ndim%d_%s_%s_kernel%d%d_stride%d%d_pad%d%d%d%d%s" \ - % (ndim, dtype, pooling_mode, kernel_h, kernel_w, stride_h, stride_w, - pad_up, pad_down, pad_left, pad_right, "_caffe" if caffe_mode else "") - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - if use_global: - out_tensor = topi.nn.global_pool(in_tensor, pool_type=pooling_mode, layout=layout) - sch = topi.generic.schedule_adaptive_pool(out_tensor) - else: - out_tensor = topi.nn.pool(in_tensor, - kernel=(kernel_h, kernel_w), - stride=(stride_h, stride_w), - padding=(pad_up, pad_left, pad_down, pad_right), - pool_type=pooling_mode, - ceil_mode=False, - layout=layout, - count_include_pad=False) - sch = topi.generic.schedule_pool(out_tensor, layout) - tensor_list = [in_tensor, out_tensor] - Genlib(sch, tensor_list, device, opname, lib_path, print_lower=False) - - -def Eltwise(device="llvm", lib_path="./", - ndim_a=None, ndim_b=None, dtype=None, mode=None): - ''' - eltwise - Args: - device: - lib_path: - ndim_a: - ndim_b: - dtype: - mode: - - Returns: - ''' - ndim_max = max(ndim_a, ndim_b) - shape = [tvm.var("n" + str(i)) for i in range(ndim_max)] - shape_b1 = [dim if i == 1 else 1 for i, dim in enumerate(shape)] - shape_a = shape[ndim_max - ndim_a:] if ndim_a else (1,) - shape_b = shape[ndim_max - ndim_b:] if ndim_b == ndim_a else shape_b1 if ndim_b == 1 else (1,) - opname = "Eltwise_%s_ndimA%d_ndimB%d_%s" % (mode, ndim_a, ndim_b, dtype) - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape_a, dtype=dtype, name='in_tensor') - b_tensor = tvm.placeholder(shape_b, dtype=dtype, name='b_tensor') - - topi_funs = { - 'add': topi.add, - 'subtract': topi.subtract, - 'multiply': topi.multiply, - 'divide': topi.divide, - 'maximum': topi.maximum, - 'minimum': topi.minimum, - } - - out_tensor = topi_funs[mode](in_tensor, b_tensor) - tensor_list = [in_tensor, b_tensor, out_tensor] - s = topi.generic.schedule_elemwise(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Softmax(device="llvm", lib_path="./", - ndim=None, dtype=None, axis=None): - ''' - softmax - Args: - device: - lib_path: - ndim: - dtype: - axis: - - Returns: - ''' - if axis >= ndim: - return - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - opname = "Softmax_ndim%d_%s_axis%s" % (ndim, dtype, axis) - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - out_tensor = topi.nn.softmax(in_tensor, axis) - tensor_list = [in_tensor, out_tensor] - s = topi.generic.schedule_elemwise(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Resize(device="llvm", lib_path="./", - ndim=None, dtype=None, method=None, align_corners=None): - ''' - resize - Args: - device: - lib_path: - ndim: - dtype: - method: - align_corners: - - Returns: - ''' - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - new_height = tvm.var("newHeight") - new_width = tvm.var("new_width") - opname = "Resize_ndim%d_%s_%s_%s" % (ndim, dtype, method, "Align" if align_corners else "NotAlign") - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - out_tensor = resize(in_tensor, [new_height, new_width], align_corners=align_corners, method=method) - tensor_list = [new_height, new_width, in_tensor, out_tensor] - s = topi.generic.schedule_injective(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Mean(device="llvm", lib_path="./", - ndim=None, dtype=None, axis=None, keep_dims=None): - ''' - mean - Args: - device: - lib_path: - ndim: - dtype: - axis: - keepDims: - - Returns: - ''' - if axis[-1] >= ndim: - return - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - axis_str = "" - for dim in axis: - axis_str += str(dim) - opname = "Mean_ndim%d_%s_axis%s_%s" % (ndim, dtype, axis_str, "keepDims" if keep_dims else "notkeepDims") - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - c_shape = shape[:] - reduced_num = 1 - for dim in axis: - c_shape[dim] = 1 - reduced_num *= shape[dim] - - def _ComputeSum(*b_idx): - reduce_axis = [tvm.reduce_axis((0, shape[dim])) for dim in axis] - a_idx = list(b_idx) - for i, dim in enumerate(axis): - a_idx[dim] = reduce_axis[i] - a_idx = tuple(a_idx) - return tvm.sum(in_tensor[a_idx], axis=reduce_axis) - - out_tensor = tvm.compute(c_shape, _ComputeSum) - out_tensor = tvm.compute(c_shape, lambda *i: out_tensor(*i) / reduced_num) - if not keep_dims: - out_tensor = topi.squeeze(out_tensor, axis) - - # define schedule & generate lib - tensor_list = [in_tensor, out_tensor] - s = tvm.create_schedule(out_tensor.op) - Genlib(s, tensor_list, device, opname, lib_path) - - -def CaffeCrop(device="llvm", lib_path="./", - ndim=None, dtype=None, axis=None): - ''' - caffe crop op - Args: - device: - lib_path: - ndim: - dtype: - axis: - - Returns: - ''' - shape = [tvm.var("n" + str(i)) for i in range(axis)] - shape_a = shape[:] - shape_b = shape[:] - offsets = [] - for i in range(axis, ndim): - shape_a.append(tvm.var("nA" + str(i))) - shape_b.append(tvm.var("nB" + str(i))) - offsets.append(tvm.var("offset" + str(i))) - opname = "CaffeCrop_ndim%d_%s_axis%d" % (ndim, dtype, axis) - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape_a, dtype=dtype, name='in_tensor') - b_tensor = tvm.placeholder(shape_b, dtype=dtype, name='b_tensor') - begin = [0] * axis + offsets - end = shape_a[:] - for i in range(axis, len(shape_a)): - end[i] = offsets[i - axis] + shape_b[i] - shape_c = [end[i] - begin[i] for i in range(ndim)] - - def _Compute(*C_idx): - a_idx = [idx + begin[i] for i, idx in enumerate(list(C_idx))] - a_idx = tuple(a_idx) - return in_tensor[a_idx] - - out_tensor = tvm.compute(shape_c, _Compute) - tensor_list = offsets + [in_tensor, b_tensor, out_tensor] - - s = tvm.create_schedule(out_tensor.op) - Genlib(s, tensor_list, device, opname, lib_path) - - -def FullConnection(device="llvm", lib_path="./", - ndim_a=None, dtype=None, has_bias=None): - ''' - full connection - Args: - device: - lib_path: - ndim_a: - dtype: - hasBias: - - Returns: - ''' - n_dim, ci, h_dim, kernel_tensor = (tvm.var("n_dim"), tvm.var("out_tensor"), tvm.var("h_dim"), \ - tvm.var("kernel_tensor")) - co = tvm.var("co") - if ndim_a == 4: - shape_a = (n_dim, ci, h_dim, kernel_tensor) - chw = ci * h_dim * kernel_tensor - else: - shape_a = (n_dim, ci) - chw = ci - shape_w = (co, chw) - opname = "FullConnection_ndimA%d_%s_%s" % (ndim_a, dtype, "hasBias" if has_bias else "notHasBias") - is_var = True - vh, vw, vc = 1, 1, 1 - print(opname) - - in_tensor = tvm.placeholder(shape_a, dtype=dtype, name='in_tensor') - kernel_tensor = tvm.placeholder(shape_w, dtype=dtype, name='kernel_tensor') - input_tensor = topi.reshape(in_tensor, (n_dim, chw)) if len(shape_a) == 4 else in_tensor - - out_tensor = _matmul_spatial_pack_asm((is_var, 0, ci, vh, vw, vc), input_tensor, kernel_tensor, \ - layout='NC', out_dtype=dtype) - if has_bias: - bias = tvm.placeholder((co,), dtype=dtype, name='bias') - out_tensor = tvm.compute((n_dim, co), lambda n, co: out_tensor[n, co] + bias[co], tag='injective') - - tensor_list = [in_tensor, kernel_tensor, bias, out_tensor] if has_bias else [in_tensor, kernel_tensor, out_tensor] - cfg = {'is_var': is_var, 'is_transpose': 0, 'core_id': 0, 'CI': ci, 'VH': vh, 'VW': vw, 'VC': vc} - s = _matmul_schedule_asm(cfg, [out_tensor]) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Power(device="llvm", lib_path="./", - ndim=None, dtype=None): - ''' - power - Args: - device: - lib_path: - ndim: - dtype: - - Returns: - ''' - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - power = tvm.var("power", dtype="float32") - scale = tvm.var("scale", dtype="float32") - shift = tvm.var("shift", dtype="float32") - opname = "Power_ndim%d_%s" % (ndim, dtype) - print(opname) - - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - out_tensor = tvm.compute(shape, lambda *i: tvm.power(in_tensor[i] * scale.astype(in_tensor.dtype) + \ - shift.astype(in_tensor.dtype), \ - power.astype(in_tensor.dtype))) - tensor_list = [power, scale, shift, in_tensor, out_tensor] - - s = tvm.create_schedule(out_tensor.op) - Genlib(s, tensor_list, device, opname, lib_path) - - -def CaffePReLU(device="llvm", lib_path="./", - ndim=None, dtype=None, channel_shared=None): - ''' - caffe prelu - Args: - device: - lib_path: - ndim: - dtype: - channel_shared: - - Returns: - ''' - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - channel = 1 if channel_shared else shape[1] - opname = "CaffePReLU_ndim%d_%s_%s" % (ndim, dtype, - "channelShared" if channel_shared else "channelNotShared") - print(opname) - - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - slope = tvm.placeholder((channel,), dtype=dtype, name='slope') - if channel_shared: - out_tensor = tvm.compute(shape, lambda *idx: tvm.if_then_else(in_tensor[idx] >= 0, in_tensor[idx],\ - in_tensor[idx] * slope[0])) - else: - out_tensor = tvm.compute(shape, lambda *idx: tvm.if_then_else(in_tensor[idx] >= 0, in_tensor[idx],\ - in_tensor[idx] * slope[idx[1]])) - - tensor_list = [in_tensor, slope, out_tensor] - s = tvm.create_schedule(out_tensor.op) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Pad(device="llvm", lib_path="./", - ndim=None, dtype=None, paddingmode=None): - ''' - pad - Args: - device: - lib_path: - ndim: - dtype: - paddingmode: - - Returns: - ''' - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - pad_before = [tvm.var("pad_before" + str(i)) for i in range(ndim)] - pad_after = [tvm.var("pad_after" + str(i)) for i in range(ndim)] - pad_before_const = [0, 0] + pad_before[2:] - pad_after_const = [0, 0] + pad_after[2:] - paddings = [None] * 2 * len(shape) - paddings[0:: 2] = pad_before - paddings[1:: 2] = pad_after - pad_value = 0 - opname = "Pad_ndim%d_%s_%s" % (ndim, dtype, paddingmode) - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - if paddingmode == "CONSTANT": - out_tensor = topi.nn.pad(in_tensor, pad_before_const, pad_after_const, pad_value=pad_value, name='out_tensor') - else: - out_tensor = mirror_pad(in_tensor, pad_before_const, pad_after_const, mode=paddingmode, name='out_tensor') - tensor_list = paddings + [in_tensor, out_tensor] - def SchedulePad(inputs): - s = tvm.create_schedule(inputs.op) - if s[inputs].op.axis: - s[inputs].parallel(s[inputs].op.axis[1]) - return s - - s = SchedulePad(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) - - -def MatMul(device="llvm", lib_path="./", - ndim_a=None, ndim_b=None, dtype=None, transpose_a=None, transpose_b=None): - ''' - matmul - Args: - device: - lib_path: - ndim_a: - ndim_b: - dtype: - transpose_a: - transpose_b: - - Returns: - ''' - m, k, n_dim = tvm.var("m"), tvm.var("k"), tvm.var("n_dim") - a_shape = (m, k) if not transpose_a else (k, m) - b_shape = (k, n_dim) if not transpose_b else (n_dim, k) - opname = "MatMul_ndimA%d_ndimB%d_%s_%d_%d" % (ndim_a, ndim_b, dtype, transpose_a, transpose_b) - print(opname) - - # define compute - in_tensor = tvm.placeholder(a_shape, dtype=dtype, name='in_tensor') - b_tensor = tvm.placeholder(b_shape, dtype=dtype, name='b_tensor') - out_tensor = topi.matmul(in_tensor, b_tensor, transpose_a, transpose_b) - tensor_list = [in_tensor, b_tensor, out_tensor] - s = topi.generic.schedule_elemwise(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Stack(device="llvm", lib_path="./", - ndim=None, dtype=None, input_num=None, axis=None): - ''' - stack - Args: - device: - lib_path: - ndim: - dtype: - input_num: - axis: - - Returns: - ''' - if axis > ndim: - return - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - shapes = [shape] * input_num - in_tensor = [tvm.placeholder(shape, dtype=dtype, name='in_tensor%d' % i) for i, shape in enumerate(shapes)] - opname = "Stack_ndim%d_%s_input_num%d_axis%d" % (ndim, dtype, input_num, axis) - print(opname) - - input_tensor = [topi.expand_dims(ai, axis) for ai in in_tensor] - out_tensor = topi.concatenate(tuple(input_tensor), axis=axis) - tensor_list = in_tensor + [out_tensor] - if ndim < 4: - s = topi.generic.schedule_concatenate(out_tensor) - else: - s = tvm.create_schedule(out_tensor.op) - Genlib(s, tensor_list, device, opname, lib_path) - - -def ArgMax(device="llvm", lib_path="./", - ndim=None, dtype=None, axis=None, keep_dims=None, top_k=None, - out_dtype=None): - ''' - argmax - Args: - device: - lib_path: - ndim: - dtype: - axis: - keepDims: - top_k: - out_dtype: - - Returns: - ''' - if axis >= ndim: - return - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - opname = "ArgMax_ndim%d_%s_axis%d_%s_top%d_%s" \ - % (ndim, dtype, axis, "keepDims" if keep_dims else "notKeepDims", top_k, out_dtype) - print(opname) - - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - out_tensor = topi.argmax(in_tensor, axis=axis, keepdims=keep_dims) - out_tensor = AsType(out_tensor, out_dtype) - tensor_list = [in_tensor, out_tensor] - s = tvm.create_schedule(out_tensor.op) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Exp(device="llvm", lib_path="./", - ndim=None, dtype=None): - ''' - exp - Args: - device: - lib_path: - ndim: - dtype: - - Returns: - ''' - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - opname = "Exp_ndim%d_%s" % (ndim, dtype) - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - if 'int' in dtype: - input_tensor = AsType(in_tensor, 'float32') - out_tensor = topi.exp(input_tensor) - out_tensor = AsType(out_tensor, in_tensor.dtype) - else: - out_tensor = topi.exp(in_tensor) - tensor_list = [in_tensor, out_tensor] - s = topi.generic.schedule_injective(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Cast(device="llvm", lib_path="./", - ndim=None, src_dtype=None, dst_dtype=None): - ''' - cast - Args: - device: - lib_path: - ndim: - src_dtype: - dst_dtype: - - Returns: - ''' - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - opname = "Cast_ndim%d_%s_%s" % (ndim, src_dtype, dst_dtype) - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape, dtype=src_dtype, name='in_tensor') - out_tensor = topi.cast(in_tensor, dst_dtype) - tensor_list = [in_tensor, out_tensor] - s = topi.generic.schedule_injective(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) - - -def ExpandDims(device="llvm", lib_path="./", - ndim=None, axis=None, dtype=None): - ''' - expand dims - Args: - device: - lib_path: - ndim: - axis: - dtype: - - Returns: - ''' - if axis > ndim: - return - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - opname = "ExpandDim_ndim%d_%s_axis%d" % (ndim, dtype, axis) - print(opname) - - # define compute - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') - out_tensor = topi.expand_dims(in_tensor, axis=axis) - tensor_list = [in_tensor, out_tensor] - s = topi.generic.schedule_injective(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Tile(device="llvm", lib_path="./", - ndim=None, dtype=None): - ''' - tile - Args: - device: - lib_path: - ndim: - dtype: - - Returns: - ''' - shape = [tvm.var("n" + str(i)) for i in range(ndim)] - multiples = [tvm.var("k" + str(i)) for i in range(ndim)] - opname = "Tile_ndim%d_%s" % (ndim, dtype) - print(opname) - - def _Compute(*C_idx): - a_idx = [tvm.floordiv(idx, multiples[i]) for i, idx in enumerate(list(C_idx))] - a_idx = tuple(a_idx) - return in_tensor[a_idx] - - # define compute - in_tensor = tvm.placeholder(shape, dtype=dtype, name='in_tensor') # tvm 0.6-dev: topi.tile - shape_c = (np.array(shape) * np.array(multiples)).tolist() - out_tensor = tvm.compute(shape_c, _Compute) - - tensor_list = multiples + [in_tensor, out_tensor] - s = topi.generic.schedule_injective(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Range(device="llvm", lib_path="./", - out_dtype=None): - ''' - range - Args: - device: - lib_path: - out_dtype: - - Returns: - ''' - start = tvm.var("start") - delta = tvm.var("delta") - opname = "Range_ndim_" + out_dtype - print(opname) - - out_tensor = tvm.compute((tvm.var("n0"),), lambda i: start.astype(out_dtype) + delta.astype(out_dtype) * i, \ - name='out_tensor') - out_tensor = AsType(out_tensor, out_dtype) - tensor_list = [start, delta, out_tensor] - s = topi.generic.schedule_injective(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) - - -def Split(device="llvm", lib_path="./", - ndim=None, dtype=None, output_num=None, axis=None): - ''' - split - Args: - device: - lib_path: - ndim: - dtype: - output_num: - axis: - - Returns: - ''' - if axis >= ndim: - return - size_splits = [tvm.var("split" + str(i)) for i in range(output_num)] - a_shape = [tvm.var("n" + str(i)) for i in range(axis)] \ - + [np.sum(size_splits)] \ - + [tvm.var("n" + str(i)) for i in range(axis + 1, ndim)] - c_shapes = [] - for i in range(output_num): - c_shape = [] - for j in range(ndim): - if j == axis: - c_shape.append(tvm.var("split" + str(i))) - else: - c_shape.append(tvm.var("n" + str(j))) - c_shapes.append(c_shape) - indices_or_sections = np.cumsum(size_splits).tolist()[:-1] - opname = "Split_ndim%d_%s_output_num%d_axis%d" % (ndim, dtype, output_num, axis) - print(opname) - - # define compute - in_tensor = tvm.placeholder(a_shape, dtype=dtype, name='in_tensor') - - def _Compute(*C_idx): - a_idx = list(C_idx) - a_idx[axis] += idx_shift - a_idx = tuple(a_idx) - return in_tensor[a_idx] - - indices_or_sections_add0 = [0] + indices_or_sections - out_tensor = [] - for i in range(output_num): - idx_shift = indices_or_sections_add0[i] - ci = tvm.compute(c_shapes[i], _Compute) - out_tensor.append(ci) - tensor_list = size_splits + [in_tensor] + out_tensor - - s = topi.generic.schedule_injective(out_tensor) - Genlib(s, tensor_list, device, opname, lib_path) diff --git a/predict/module/tvm_kernel/lite/python/at_ops/config_tool.py b/predict/module/tvm_kernel/lite/python/at_ops/config_tool.py deleted file mode 100644 index b3006b1174d..00000000000 --- a/predict/module/tvm_kernel/lite/python/at_ops/config_tool.py +++ /dev/null @@ -1,211 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -""" -This module is define some data struct for tvm kernel. -""" -import tvm -import topi - -format_map = {"NCHW": 0, "NHWC": 1} - -pool_map = {"max_pool": 0, "avg_pool": 1, "global_pool": 2} - -activation_map = { - "no_activation": 0, - "relu": 1, - "sigmoid": 2, - "relu6": 3, - "elu": 4, - "leaky_relu": 5, - "abs": 6, - "relu1": 7, - "softsign": 8, - "softplus": 9, - "tanh ": 10, -} -activation_enum_map = { - "NO_ACTIVATION": 0, - "RELU": 1, - "SIGMOID": 2, - "RELU6": 3, - "elu": 4, - "leaky_relu": 5, - "abs": 6, - "relu1": 7, - "softsign": 8, - "softplus": 9, - "tanh ": 10, -} - -padmode_map = {"NOTSET": 0, "SAME": 1, "VALID": 2} - -mslite_datatype_map = { - "float16": 1, - "float32": 0, - "double": 11, - "int8": 2, - "int16": 6, - "int32": 3, - "int64": 9, - "uint8": 4, - "uint16": 7, - "uint32": 8, - "uint64": 10, -} - - -def get_key_by_value(dicts, value): - for k, v in dicts.items(): - if v == value: - return k - return None - - -def relu6(x): - return tvm.compute( - x.shape, - lambda *i: tvm.min( - tvm.max(x(*i), tvm.const(0, x.dtype)), tvm.const(6, x.dtype) - ), - ) - - -activation_topi_funs = {"NO_ACTIVATION": None, "RELU": topi.nn.relu, "RELU6": relu6} - -name_funcs = { - "Concat": ( - lambda opname, x: ( - opname + "_%d_%d" + "_%d" + "_%d" * x["ndim"] + "_%d" * len(x["shapeAxis"]) - ) - % ( - format_map[x["format"]], - x["ndim"], - x["axis"], - *x["shapeOut"], - *x["shapeAxis"], - ) - ), - "Softmax": ( - lambda opname, x: (opname + "_%d_%d" + "_%d" * x["ndim"] + "_%d") - % (format_map[x["format"]], x["ndim"], *x["shape"], x["axis"]) - ), - "Activation": ( - lambda opname, x: (opname + "_%d_%d" + "_%d" + "_%d" * x["ndim"]) - % (format_map[x["format"]], x["ndim"], activation_map[x["type"]], *x["shape"]) - ), - "Add": ( - lambda opname, x: (opname + "_%d_%d" + "_%d" * x["ndim"]) - % (format_map[x["format"]], x["ndim"], *x["shape"]) - ), - "Convolution": ( - lambda opname, x: ( - opname + "_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d" - ) - % ( - format_map[x["format"]], - x["ndim"], - x["batch"], - x["in_channel"], - *x["in_size"], - x["num_filter"], - *x["filter_size"], - *x["pad"], - *x["stride"], - x["dilation"], - x["hasbias"], - activation_map[x["activation_type"]], - ) - ), - "Identity": ( - lambda opname, x: (opname + "_%d_%d" + "_%d" * x["ndim"]) - % (format_map[x["format"]], x["ndim"], *x["shape"]) - ), - "BatchNorm": ( - lambda opname, x: (opname + "_%d_%d" + "_%d" * x["ndim"] + "_%d") - % (format_map[x["format"]], x["ndim"], *x["shape"], x["epsilon"]) - ), - "Squeeze": ( - lambda opname, x: ( - opname + "_%d_%d" + "_%d" * x["ndim"] + "_%d" * len(x["axis"]) - ) - % (format_map[x["format"]], x["ndim"], *x["shape"], *x["axis"]) - ), - "BiasAdd": ( - lambda opname, x: (opname + "_%d_%d" + "_%d" * x["ndim"] + "_%d") - % (format_map[x["format"]], x["ndim"], *x["shape"], x["axis"]) - ), - "Pooling": ( - lambda opname, x: (opname + "_%d_%d_%d" + "_%d" * x["ndim"] + "_%d_%d_%d") - % ( - format_map[x["format"]], - x["ndim"], - pool_map[x["type"]], - *x["shape"], - x["kernel"], - x["stride"], - x["pad"], - ) - ), - "ConvolutionDepthwise": ( - lambda opname, x: ( - opname + "_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d_%d" - ) - % ( - format_map[x["format"]], - x["ndim"], - x["batch"], - x["in_channel"], - *x["in_size"], - x["in_channel"] * x["channel_multiplier"], - *x["filter_size"], - *x["pad"], - *x["stride"], - x["dilation"], - x["hasbias"], - activation_map[x["activation_type"]], - ) - ), - "Reshape": ( - lambda opname, x: ( - opname + "_%d_%d" + "_%d" * x["ndimA"] + "_%d" * len(x["shapeB"]) - ) - % (format_map[x["format"]], x["ndimA"], *x["shapeA"], *x["shapeB"]) - ), - "Shape": ( - lambda opname, x: (opname + "_%d_%d" + "_%d" * x["ndim"]) - % (format_map[x["format"]], x["ndim"], *x["shape"]) - ), - "RealDiv": ( - lambda opname, x: ( - opname + "_%d_%d" + "_%d" * x["ndim"] + "_%d" * len(x["shapeB"]) - ) - % (format_map[x["format"]], x["ndim"], *x["shapeA"], *x["shapeB"]) - ), - "ResizeBilinear": (lambda opname, x: "ResizeBilinear"), - "TFLite_Detection_PostProcess": (lambda opname, x: "TFLite_Detection_PostProcess"), -} - -config_dict = {op_type: [] for op_type in name_funcs} - - -def config_dict_append(op_type, config, opname=None): - if opname is None: - config["opname"] = name_funcs[op_type](op_type, config) - else: - config["opname"] = opname - duplicate = [True for x in config_dict[op_type] if config == x] - - if duplicate: - config_dict[op_type].append(config) diff --git a/predict/module/tvm_kernel/lite/python/at_rt/at_runtime_reset.py b/predict/module/tvm_kernel/lite/python/at_rt/at_runtime_reset.py deleted file mode 100644 index cc0cc728859..00000000000 --- a/predict/module/tvm_kernel/lite/python/at_rt/at_runtime_reset.py +++ /dev/null @@ -1,62 +0,0 @@ -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -""" -This module is Using to make Lite Runtime funcitons instead TVM Runtime funcitons while codegen. -""" - -import os -from tvm import codegen - -class AtRuntimeReset(): - """Using this class to make Lite Runtime funcitons instead TVM Runtime funcitons while codegen - Usage like: - with at_runtime_reset.AtRuntimeReset(): - fadd = tvm.build(s, [A, B], tgt, target_host = tgt_host, name = "myadd") - then the module fadd will using Lite runtime functions. - """ - - def __enter__(self): - if os.getenv("TVM_RUNTIME_ON") is not None: - return - codegen.SetRTFuncTransPair( - "TVMBackendAllocWorkspace", "LiteBackendAllocWorkspace" - ) - codegen.SetRTFuncTransPair( - "TVMBackendFreeWorkspace", "LiteBackendFreeWorkspace" - ) - codegen.SetRTFuncTransPair("TVMAPISetLastError", "LiteAPISetLastError") - codegen.SetRTFuncTransPair( - "TVMBackendParallelLaunch", "LiteBackendParallelLaunch" - ) - codegen.SetRTFuncTransPair( - "TVMBackendParallelBarrier", "LiteBackendParallelBarrier" - ) - codegen.SetRTFuncTransPair( - "TVMBackendRegisterSystemLibSymbol", "LiteBackendRegisterSystemLibSymbol" - ) - codegen.SetRTFuncTransPair("TVMFuncCall", "LiteFuncCall") - codegen.SetRTFuncTransPair( - "TVMBackendGetFuncFromEnv", "LiteBackendGetFuncFromEnv" - ) - - def __exit__(self, ptype, value, trace): - codegen.DelRTFuncTransPair("TVMBackendAllocWorkspace") - codegen.DelRTFuncTransPair("TVMBackendFreeWorkspace") - codegen.DelRTFuncTransPair("TVMAPISetLastError") - codegen.DelRTFuncTransPair("TVMBackendParallelLaunch") - codegen.DelRTFuncTransPair("TVMBackendParallelBarrier") - codegen.DelRTFuncTransPair("TVMBackendRegisterSystemLibSymbol") - codegen.DelRTFuncTransPair("TVMFuncCall") - codegen.DelRTFuncTransPair("TVMBackendGetFuncFromEnv") diff --git a/predict/module/tvm_kernel/lite/src/api/kernel_manager.cc b/predict/module/tvm_kernel/lite/src/api/kernel_manager.cc deleted file mode 100644 index b349ae60198..00000000000 --- a/predict/module/tvm_kernel/lite/src/api/kernel_manager.cc +++ /dev/null @@ -1,1772 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this ${file} except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef LITE_RUNTIME_ON - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "common/mslog.h" - -const char *LIB_INFO = "libtvm_kernel version: master (c66c6b28dc991c9d705e1b983aab7385c337128d)"; -namespace km { -class KernelManager { - public: - int CallKernel(const std::string &fid, TVMArgs args) { - tvm::runtime::Module *mod = this->GetModule(); - CHECK(mod != nullptr) << "Failed to get Module!"; - const std::string name = fid; - tvm::runtime::PackedFunc f = mod->GetFunction(name, false); - CHECK(f != nullptr) << "Can't find kernel func " << fid; - TVMRetValue rv; - f.CallPacked(args, &rv); - return 0; - } - - void InitKernelManager(int mode, const std::string &fname) { return this->Init(mode, fname); } - - static KernelManager *Global() { - static KernelManager inst; - return &inst; - } - - tvm::runtime::Module *GetModule() const { return &g_modLib; } - - private: - KernelManager() = default; - - ~KernelManager() = default; - - void Init(int mode, std::string fpath) { - std::call_once(init_flag, &KernelManager::InitLib, mode, fpath); - return; - } - - static void InitLib(int mode, std::string fpath) { - if (mode) { - const PackedFunc *ptr = tvm::runtime::Registry::Get("module._GetSystemLib"); - CHECK(ptr != nullptr) << "Failed to get systemlib"; - g_modLib = (*ptr)(); - } else { - g_modLib = tvm::runtime::Module::LoadFromFile(fpath); - } - } - static tvm::runtime::Module g_modLib; - std::once_flag init_flag; -}; - -tvm::runtime::Module KernelManager::g_modLib; -} // namespace km - -std::function &)> GetKernel(const std::string &fid) { - km::KernelManager *inst = km::KernelManager::Global(); - CHECK(inst != nullptr) << "Failed to get KernelManager instance!"; - tvm::runtime::Module *mod = inst->GetModule(); - CHECK(mod != nullptr) << "Failed to get Module!"; - tvm::runtime::PackedFunc f = mod->GetFunction(fid, false); - if (f == nullptr) { - MS_LOGE("GetFunction return nullptr"); - return nullptr; - } - auto runner = [f](const std::vector &tensors) -> int { - int argLen = tensors.size(); - CHECK(argLen) << "Input tensors num=0 !"; - std::vector values(argLen); - std::vector codes(argLen); - tvm::runtime::TVMArgsSetter setter(values.data(), codes.data()); - for (int i = 0; i < argLen; ++i) { - setter(i, tensors.at(i)); - } - tvm::runtime::TVMArgs targs(values.data(), codes.data(), argLen); - TVMRetValue rv; - f.CallPacked(targs, &rv); - return 0; - }; - return runner; -} - -int CallKernel(const std::string &fid, const std::vector &tensors) { - km::KernelManager *inst = km::KernelManager::Global(); - CHECK(inst != nullptr) << "Failed to get KernelManager instance!"; - int argLen = tensors.size(); - CHECK(argLen) << "Input tensors num=0 !"; - std::vector values(argLen); - std::vector codes(argLen); - tvm::runtime::TVMArgsSetter setter(values.data(), codes.data()); - for (int i = 0; i < argLen; ++i) { - setter(i, tensors.at(i)); - } - tvm::runtime::TVMArgs targs(values.data(), codes.data(), argLen); - inst->CallKernel(fid, targs); - return 0; -} - -int InitKernelManager(int mode, const std::string &fname) { - km::KernelManager *inst = km::KernelManager::Global(); - CHECK(inst != nullptr) << "Failed to get KernelManager instance!"; - inst->InitKernelManager(mode, fname); - return 0; -} - -// just for api compatible, tvm/lite has same api -void ConfigThreadPool(int mode = 1, int nthreads = 0, bool execute_self = true) {} - -#else - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "flatbuffers/flatbuffers.h" -#include "schema/inner/ms_generated.h" -#include "include/securec.h" -#include "src/runtime/runtime_api.h" -#include "common/mslog.h" - -using runnerType = std::function &)>; - -const char *LIB_INFO = "libtvm_kernel version: master (c66c6b28dc991c9d705e1b983aab7385c337128d)"; - -namespace lite { -namespace runtime { -extern "C" { -// Function signature for generated packed function in shared library -typedef int (*BackendPackedCFunc)(const void *args, int *type_codes, int num_args); -} // extern "C" - -class LiteFuncPool { - public: - LiteFuncPool() = default; - - ~LiteFuncPool() = default; - - void GetFunction(const std::string &name, void **func_addr) { - auto it = tbl_.find(name); - if (func_addr == nullptr) { - MS_LOGW("input func_addr is nullptr"); - return; - } - *func_addr = (it != tbl_.end() ? it->second : nullptr); - } - - void RegisterSymbol(const std::string &name, void *ptr) { - std::lock_guard lock(mutex_); - auto it = tbl_.find(name); - if (it != tbl_.end() && ptr != it->second) { - MS_LOGW("Lite symbol %s get overriden to a different address %p->%p", name.c_str(), ptr, it->second); - } - tbl_[name] = ptr; - } - - static LiteFuncPool *Global() { - static LiteFuncPool inst; - return &inst; - } - - private: - // Internal mutex - std::mutex mutex_; - // Internal symbol table - std::unordered_map tbl_; -}; -} // namespace runtime -} // namespace lite - -using LiteFuncPool = lite::runtime::LiteFuncPool; -using BackendPackedCFunc = lite::runtime::BackendPackedCFunc; - -int LiteBackendRegisterSystemLibSymbol(const char *name, void *ptr) { - MS_ASSERT(LiteFuncPool::Global() != nullptr); - LiteFuncPool::Global()->RegisterSymbol(name, ptr); - return 0; -} - -// do nothing, api compatible with TVM_RUNTIME_ON API -void InitKernelManager(int mode, const std::string &fname) { return; } - -static inline void *GetFunction(const std::string &fid) { - void *f = nullptr; - MS_ASSERT(LiteFuncPool::Global() != nullptr); - LiteFuncPool::Global()->GetFunction(fid, &f); - if (f == nullptr) { - return nullptr; - } - return f; -} - -runnerType __attribute__((noinline)) GetKernel(const std::string &fid) { - auto f = GetFunction(fid); - if (f == nullptr) { - return nullptr; - } - auto runner = [f](const std::vector &tensors) -> int { - if (tensors.empty()) { - MS_LOGE("Input tensors num = 0 !"); - return -1; - } - std::vector values(tensors.size()); - std::vector codes(tensors.size()); - tvm::runtime::TVMArgsSetter setter(values.data(), codes.data()); - for (size_t i = 0; i < tensors.size(); ++i) { - setter(i, tensors.at(i)); - } - auto passfunc = reinterpret_cast(f); - return passfunc(values.data(), codes.data(), tensors.size()); - }; - return runner; -} - -namespace auto_tensor { -constexpr int TENSOR_NUM_MAX = 10; -constexpr bool STORE_MODE = true; -constexpr bool RESUME_MODE = false; -const char *NOT_SUPPORT = "NOT SUPPORT"; -const int NCHW_N = 0; -const int NCHW_C = 1; -const int NCHW_H = 2; -const int NCHW_W = 3; -const int tile = 4; - -void store_shape(const std::vector &tensors, int (&ndim)[TENSOR_NUM_MAX], int64_t *(&shape)[TENSOR_NUM_MAX], - int64_t *(&strides)[TENSOR_NUM_MAX], bool mode = STORE_MODE) { - if (mode == STORE_MODE) { - for (size_t i = 0; i < tensors.size(); ++i) { - ndim[i] = tensors[i]->ndim; - shape[i] = tensors[i]->shape; - strides[i] = tensors[i]->strides; - } - } else { - for (size_t i = 0; i < tensors.size(); ++i) { - tensors[i]->ndim = ndim[i]; - tensors[i]->shape = shape[i]; - tensors[i]->strides = strides[i]; - } - } -} - -static std::string get_dtype(const DLTensor &tensor) { - auto dtype = tensor.dtype; - if (dtype.code == kDLFloat) { - if (dtype.bits == 16) - return "float16"; - else if (dtype.bits == 32) - return "float32"; - else if (dtype.bits == 64) - return "float64"; - } else if (dtype.code == kDLInt) { - if (dtype.bits == 8) - return "int8"; - else if (dtype.bits == 16) - return "int16"; - else if (dtype.bits == 32) - return "int32"; - else if (dtype.bits == 64) - return "int64"; - } else if (dtype.code == kDLUInt) { - if (dtype.bits == 8) - return "uint8"; - else if (dtype.bits == 16) - return "uint16"; - else if (dtype.bits == 32) - return "uint32"; - else if (dtype.bits == 64) - return "uint64"; - } - return std::string(NOT_SUPPORT); -} - -struct OpCommonAttr { - std::string optype = ""; - std::string fid = ""; - uint32_t ndim = 0; - std::string dtype = "float32"; - - OpCommonAttr(const mindspore::predict::OpDef &opdef, const std::vector &tensors) { - auto opT = mindspore::predict::EnumNameOpT(opdef.attr_type()); - this->optype = opT; - MS_ASSERT(opdef.name() != nullptr); - this->fid = opdef.name()->str(); - if (!tensors.empty()) { - MS_ASSERT(tensors.front() != nullptr); - ndim = tensors.front()->ndim; - dtype = get_dtype(*tensors.front()); - } - } -}; - -template -static void NCHW2NHWC(DLTensor *src) { - if (src == nullptr) { - MS_LOGW("input src is nullptr"); - return; - } - T *src_data = static_cast(src->data); - std::unique_ptr tmp(new (std::nothrow) - T[src->shape[NCHW_N] * src->shape[NCHW_C] * src->shape[NCHW_H] * src->shape[NCHW_W]]); - if (tmp == nullptr) { - MS_LOGW("new tmp buf failed"); - return; - } - int N = src->shape[NCHW_N]; - int C = src->shape[NCHW_C]; - int H = src->shape[NCHW_H]; - int W = src->shape[NCHW_W]; - - // NCHW -> NHWC - int k = 0; - for (int n = 0; n < N; n++) - for (int h = 0; h < H; h++) - for (int w = 0; w < W; w++) - for (int c = 0; c < C; c++) { - tmp[k++] = src_data[n * C * H * W + c * H * W + h * W + w]; - } - - int sizes = N * C * H * W * sizeof(T); - errno_t ret = memcpy_s(src_data, sizes, tmp.get(), sizes); - if (ret != 0) { - MS_LOGW("memcpy_s failed: %d", ret); - return; - } -} - -static void transpose_shape(DLTensor *tensor, std::vector axis) { - if (tensor == nullptr) { - MS_LOGW("input tensor is nullptr"); - return; - } - int ndim = tensor->ndim; - std::vector origin_shape(tensor->shape, tensor->shape + ndim); - - for (int i = ndim - 1; i >= 0; --i) { - tensor->shape[i] = origin_shape[axis[i]]; - } -} - -static runnerType Pack_NCHW2NHWC(runnerType fun) { - if (fun == nullptr) { - MS_LOGE("input fun is nullptr"); - return nullptr; - } - auto runner = [fun](const std::vector &tensors) -> int { - if (tensors.back() == nullptr) { - MS_LOGE("tensors.back() is nullptr"); - return 1; - } - transpose_shape(tensors.back(), {0, 3, 1, 2}); // NHWC -> NCHW - fun(tensors); - - auto output = tensors.back(); - if (output == nullptr) { - MS_LOGE("tensors.back() after func is nullptr"); - return 1; - } - if (output->dtype.bits == 8) { - NCHW2NHWC(output); - } else if (output->dtype.bits == 16) { - NCHW2NHWC(output); - } else if (output->dtype.bits == 32) { - NCHW2NHWC(output); - } else if (output->dtype.bits == 64) { - NCHW2NHWC(output); - } else { - MS_LOGE("conv NCHW2NHWC output.dtype.bits=%d invalid, only support (8, 16, 32, 64)", output->dtype.bits); - return 1; - } - - if (tensors.back() == nullptr) { - MS_LOGE("tensors.back() is nullptr"); - return 1; - } - transpose_shape(tensors.back(), {0, 2, 3, 1}); // NCHW -> NHWC - return 0; - }; - return runner; -} - -runnerType __attribute__((noinline)) GetKernel_Insert_vector_int32(const std::string &fid, - const std::vector &vec) { - auto f = GetFunction(fid); - if (f == nullptr) { - MS_LOGE("GetFunction return nullptr"); - return nullptr; - } - auto runner = [f, vec](const std::vector &tensors) -> int { - std::vector values(vec.size() + tensors.size()); - std::vector codes(values.size()); - tvm::runtime::TVMArgsSetter setter(values.data(), codes.data()); - for (size_t i = 0; i < vec.size(); ++i) { - setter(i, vec.at(i)); - } - for (size_t i = 0; i < tensors.size(); ++i) { - setter(i + vec.size(), tensors.at(i)); - } - auto passfunc = reinterpret_cast(f); - return passfunc(values.data(), codes.data(), values.size()); - }; - return runner; -} - -runnerType __attribute__((noinline)) GetKernel_Insert_vector_float(const std::string &fid, - const std::vector &vec) { - auto f = GetFunction(fid); - if (f == nullptr) { - MS_LOGE("GetFunction return nullptr"); - return nullptr; - } - auto runner = [f, vec](const std::vector &tensors) -> int { - std::vector values(vec.size() + tensors.size()); - std::vector codes(values.size()); - tvm::runtime::TVMArgsSetter setter(values.data(), codes.data()); - for (size_t i = 0; i < vec.size(); ++i) { - setter(i, vec.at(i)); - } - for (size_t i = 0; i < tensors.size(); ++i) { - setter(i + vec.size(), tensors.at(i)); - } - auto passfunc = reinterpret_cast(f); - return passfunc(values.data(), codes.data(), values.size()); - }; - return runner; -} - -static runnerType GetKernel_Conv(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - if (tensors.at(0) == nullptr) { - MS_LOGE("input tensors.at(0) is nullptr"); - return nullptr; - } - int n = tensors.at(0)->shape[NCHW_N]; - int ci = tensors.at(0)->shape[NCHW_C]; - int h = tensors.at(0)->shape[NCHW_H]; - int w = tensors.at(0)->shape[NCHW_W]; - std::vector arg_const{n, ci, h, w}; - const OpCommonAttr opAttr(opdef, tensors); - - std::string fid; - if (opdef.attr_as_Conv2D() != nullptr) { - auto op = opdef.attr_as_Conv2D(); - fid = std::string(mindspore::predict::EnumNameOpT(opdef.attr_type())) + "_ndim" + std::to_string(opAttr.ndim) + - "_" + opAttr.dtype + "_k" + std::to_string(op->kernelH()) + "_s" + std::to_string(op->strideH()) + "_p" + - std::to_string(op->padUp()) + std::to_string(op->padDown()) + std::to_string(op->padLeft()) + - std::to_string(op->padRight()) + "_d" + std::to_string(op->dilateH()) + "_act" + - std::to_string(static_cast(op->activationType())) + "_vc" + std::to_string(1) + "_vh" + - std::to_string(1) + "_vw" + std::to_string(1) + "_hasbias" + std::to_string(op->hasBias()); - if (tensors.at(1) == nullptr) { - MS_LOGE("input tensors.at(1) is nullptr"); - return nullptr; - } - int co = tensors.at(1)->shape[NCHW_N]; - arg_const.push_back(co); - } else if (opdef.attr_as_DepthwiseConv2D() != nullptr) { - auto op = opdef.attr_as_DepthwiseConv2D(); - fid = std::string(mindspore::predict::EnumNameOpT(opdef.attr_type())) + "_ndim" + std::to_string(opAttr.ndim) + - "_" + opAttr.dtype + "_k" + std::to_string(op->kernelH()) + "_s" + std::to_string(op->strideH()) + "_p" + - std::to_string(op->padUp()) + std::to_string(op->padDown()) + std::to_string(op->padLeft()) + - std::to_string(op->padRight()) + "_d" + std::to_string(op->dilateH()) + "_act" + - std::to_string(static_cast(op->activationType())) + "_vc" + std::to_string(1) + "_vh" + - std::to_string(1) + "_vw" + std::to_string(1) + "_hasbias" + std::to_string(op->hasBias()); - int co = tensors.at(0)->shape[NCHW_C] * op->channelMultiplier(); - arg_const.push_back(co); - } else if (opdef.attr_as_DeDepthwiseConv2D() != nullptr) { - auto op = opdef.attr_as_DeDepthwiseConv2D(); - fid = std::string(mindspore::predict::EnumNameOpT(opdef.attr_type())) + "_ndim" + std::to_string(opAttr.ndim) + - "_" + opAttr.dtype + "_k" + std::to_string(op->kernelH()) + "_s" + std::to_string(op->strideH()) + "_p" + - std::to_string(op->padUp()) + std::to_string(op->padDown()) + std::to_string(op->padLeft()) + - std::to_string(op->padRight()) + "_d" + std::to_string(op->dilateH()) + "_act" + - std::to_string(static_cast(op->activationType())) + "_vc" + std::to_string(1) + "_vh" + - std::to_string(1) + "_vw" + std::to_string(1) + "_hasbias" + std::to_string(op->hasBias()); - int co = tensors.at(0)->shape[NCHW_C] * op->channelMultiplier(); - arg_const.push_back(co); - } - auto fun = GetKernel(fid); - if (fun == nullptr) { - MS_LOGE("GetKernel return nullptr"); - return nullptr; - } - - auto f = GetFunction(fid); - if (f == nullptr) { - MS_LOGE("GetFunction return nullptr"); - return nullptr; - } - auto runner = [f, arg_const](const std::vector &tensors) -> int { - int ndim[TENSOR_NUM_MAX]; - int64_t *shapes[TENSOR_NUM_MAX]; - int64_t *strides[TENSOR_NUM_MAX]; - store_shape(tensors, ndim, shapes, strides, STORE_MODE); - - std::vector values(arg_const.size() + tensors.size()); - std::vector codes(values.size()); - tvm::runtime::TVMArgsSetter setter(values.data(), codes.data()); - for (size_t i = 0; i < arg_const.size(); ++i) { - setter(i, arg_const.at(i)); - } - for (size_t i = 0; i < tensors.size(); ++i) { - setter(i + arg_const.size(), tensors.at(i)); - } - auto passfunc = reinterpret_cast(f); - passfunc(values.data(), codes.data(), values.size()); - store_shape(tensors, ndim, shapes, strides, RESUME_MODE); - return 0; - }; - fun = runner; - - if (opdef.isLastConv()) { - return Pack_NCHW2NHWC(fun); - } - return fun; -} - -void update_shape_NC4HW4(const std::vector &tensors, int64_t (&shapeA)[TENSOR_NUM_MAX], - int64_t (&shapeC)[TENSOR_NUM_MAX]) { - auto inputA = tensors.front(); - auto output = tensors.back(); - if (inputA == nullptr) { - MS_LOGW("input tensors.front() is nullptr"); - return; - } - if (output == nullptr) { - MS_LOGW("input tensors.back() is nullptr"); - return; - } - shapeA[inputA->ndim] = tile; - for (int32_t i = 0; i < inputA->ndim; ++i) { - if (i == 1) { - shapeA[i] = inputA->shape[i] >> 2; - } else { - shapeA[i] = inputA->shape[i]; - } - } - { - inputA->ndim = inputA->ndim + 1; - inputA->shape = shapeA; - inputA->strides = nullptr; - } - shapeC[output->ndim] = tile; - for (int32_t i = 0; i < output->ndim; ++i) { - if (i == 1) { - shapeC[i] = output->shape[i] >> 2; - } else { - shapeC[i] = output->shape[i]; - } - } - { - output->ndim = output->ndim + 1; - output->shape = shapeC; - output->strides = nullptr; - } -} - -static runnerType GetKernel_Conv_var(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto fun = GetKernel(opAttr.fid); - if (tensors.at(0) == nullptr) { - MS_LOGE("input tensors.at(0) is nullptr"); - return nullptr; - } - std::string fid = opAttr.fid.substr(0, opAttr.fid.find('_')); - int n = tensors.at(0)->shape[NCHW_N]; - int ci = tensors.at(0)->shape[NCHW_C]; - int h = tensors.at(0)->shape[NCHW_H]; - int w = tensors.at(0)->shape[NCHW_W]; - int co = tensors.at(1)->shape[NCHW_C]; - std::vector arg_const{n, ci >> 2, h, w, co}; - if (fun == nullptr) { - auto fd = [](int h, std::vector &res) { - for (int i = 2; i <= h; i += 2) { - if ((h % i) == 0) res.emplace_back(i); - } - }; - int outidx = tensors.size() - 1; - std::vector vw; - if (tensors.at(outidx) == nullptr) { - MS_LOGE("input tensors.at(%d) is nullptr", outidx); - return nullptr; - } - fd(tensors.at(outidx)->shape[NCHW_W], vw); - - auto op = opdef.attr_as_DeConv2D(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_DeConv2D() is nullptr"); - return nullptr; - } - std::string fids; - for (auto iter = vw.rbegin(); iter != vw.rend(); iter++) { - fids = fid + "_ndim" + std::to_string(opAttr.ndim + 1) + "_" + opAttr.dtype + "_k" + - std::to_string(op->kernelH()) + "_s" + std::to_string(op->strideH()) + "_p" + std::to_string(op->padUp()) + - std::to_string(op->padDown()) + std::to_string(op->padLeft()) + std::to_string(op->padRight()) + "_d" + - std::to_string(op->dilateH()) + "_act" + std::to_string(static_cast(op->activationType())) + "_vc" + - std::to_string(4) + "_vh" + std::to_string(2) + "_vw" + std::to_string(*iter) + "_hasbias" + - std::to_string(op->hasBias()); - fun = GetKernel(fids); - if (fun != nullptr) { - break; - } - } - fid = fids; - if (fun == nullptr) { - MS_LOGE("fun is nullptr"); - return nullptr; - } - auto f = GetFunction(fid); - if (f == nullptr) { - MS_LOGE("GetFunction return nullptr"); - return nullptr; - } - auto runner = [f, arg_const](const std::vector &tensors) -> int { - int ndim[TENSOR_NUM_MAX]; - int64_t *shapes[TENSOR_NUM_MAX]; - int64_t *strides[TENSOR_NUM_MAX]; - int64_t shapeA[TENSOR_NUM_MAX]; - int64_t shapeC[TENSOR_NUM_MAX]; - store_shape(tensors, ndim, shapes, strides, STORE_MODE); - update_shape_NC4HW4(tensors, shapeA, shapeC); - - std::vector values(arg_const.size() + tensors.size()); - std::vector codes(values.size()); - tvm::runtime::TVMArgsSetter setter(values.data(), codes.data()); - for (size_t i = 0; i < arg_const.size(); ++i) { - setter(i, arg_const.at(i)); - } - for (size_t i = 0; i < tensors.size(); ++i) { - setter(i + arg_const.size(), tensors.at(i)); - } - auto passfunc = reinterpret_cast(f); - passfunc(values.data(), codes.data(), values.size()); - store_shape(tensors, ndim, shapes, strides, RESUME_MODE); - return 0; - }; - fun = runner; - } - - if (opdef.isLastConv()) { - return Pack_NCHW2NHWC(fun); - } - return fun; -} - -enum reahpeCHW_Mode { FusedCHW, ExpandCHW }; - -void update_shape_reahpeCHW(const std::vector &tensors, reahpeCHW_Mode mode, int64_t (&shape)[4], - int64_t (&strides)[4], bool reahpe_output = false) { - auto input = tensors.front(); - auto output = tensors.back(); - if (input == nullptr) { - MS_LOGW("input tensors.front() is nullptr"); - return; - } - if (output == nullptr) { - MS_LOGW("input tensors.back() is nullptr"); - return; - } - int ndim; - if (mode == FusedCHW) { - ndim = 2; - int64_t CHW = 1; - for (int32_t i = 1; i < input->ndim; ++i) { - CHW *= input->shape[i]; - } - shape[NCHW_N] = input->shape[NCHW_N]; - shape[NCHW_C] = CHW; - strides[1] = 1; - strides[0] = CHW; - } else { - ndim = 4; - shape[NCHW_N] = input->shape[NCHW_N]; - shape[NCHW_C] = input->shape[NCHW_C]; - shape[NCHW_H] = 1; - shape[NCHW_W] = 1; - strides[3] = 1; - strides[2] = 1; - strides[1] = 1; - strides[0] = input->shape[NCHW_C]; - } - - input->ndim = ndim; - input->shape = shape; - input->strides = strides; - if (reahpe_output) { - output->ndim = ndim; - output->shape = shape; - output->strides = strides; - } -} - -static runnerType Pack_reahpeCHW(const runnerType &fun, const std::vector &tensors, reahpeCHW_Mode mode, - bool reahpe_output = false) { - if (fun == nullptr) { - MS_LOGE("input fun is nullptr"); - return nullptr; - } - if (tensors.front() == nullptr) { - MS_LOGE("input tensors.front() is nullptr"); - return nullptr; - } - if ((tensors.front()->ndim == 2 && mode == FusedCHW) || (tensors.front()->ndim == 4 && mode == ExpandCHW)) { - return fun; - } - - auto runner = [fun, mode, reahpe_output](const std::vector &tensors) -> int { - int ndim[TENSOR_NUM_MAX]; - int64_t *shape[TENSOR_NUM_MAX]; - int64_t *strides[TENSOR_NUM_MAX]; - int64_t shape_R[4]; - int64_t strides_R[4]; - store_shape(tensors, ndim, shape, strides, STORE_MODE); - update_shape_reahpeCHW(tensors, mode, shape_R, strides_R, reahpe_output); - fun(tensors); - store_shape(tensors, ndim, shape, strides, RESUME_MODE); - return 0; - }; - return runner; -} - -static runnerType GetKernel_BatchNorm(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - std::string fid; - std::vector epsilon(1, 0.001); - if (opAttr.optype == "BatchNorm") { - fid = "TFBatchNorm_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype + "_axis1"; - epsilon.front() = opdef.attr_as_FusedBatchNorm()->epsilon(); - return GetKernel_Insert_vector_float(fid, epsilon); - } else if (opAttr.optype == "CaffeBatchNorm") { - fid = "CaffeBatchNorm_ndim4_" + opAttr.dtype + "_axis1"; - epsilon.front() = opdef.attr_as_CaffeBatchNorm()->epsilon(); - auto fun = GetKernel_Insert_vector_float(fid, epsilon); - if (fun == nullptr) { - MS_LOGE("GetKernel_Insert_vector_float return nullptr"); - return nullptr; - } - bool reahpe_output = true; - return Pack_reahpeCHW(fun, tensors, ExpandCHW, reahpe_output); - } else if (opAttr.optype == "BiasAdd") { - auto op = opdef.attr_as_BiasAdd(); - fid = "TFBiasAdd_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype + "_axis" + - std::to_string(op->axis()->Get(0)); - return GetKernel(fid); - } else if (opAttr.optype == "Scale") { - fid = "CaffeScale_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype + "_axis1"; - return GetKernel(fid); - } - return nullptr; -} - -void update_shape_flatten(const std::vector &tensors, int64_t *shape, int64_t *strides) { - auto inputA = tensors.back(); - if (inputA == nullptr) { - MS_LOGW("input tensors.back() is nullptr"); - return; - } - for (int32_t i = 0; i < inputA->ndim; ++i) { - *shape *= inputA->shape[i]; - } - for (size_t i = 0; i < tensors.size(); ++i) { - tensors[i]->ndim = 1; - tensors[i]->shape = shape; - tensors[i]->strides = strides; - } -} - -std::string GetEltwiseMode(const OpCommonAttr &opAttr, const mindspore::predict::OpDef &opdef) { - const auto optype = opAttr.optype; - std::string mode = "add"; - if (optype == "Eltwise") { - auto op_mode = opdef.attr_as_Eltwise()->mode(); - if (mindspore::predict::EltwiseMode_PROD == op_mode) { - mode = "multiply"; - } else if (mindspore::predict::EltwiseMode_SUM == op_mode) { - mode = "add"; - } else if (mindspore::predict::EltwiseMode_MAXIMUM == op_mode) { - mode = "maximum"; - } - } else { - if ("Add" == optype) { - mode = "add"; - } else if ("Sub" == optype) { - mode = "subtract"; - } else if ("Mul" == optype) { - mode = "multiply"; - } else if ("RealDiv" == optype) { - mode = "divide"; - } else if ("Maximum" == optype) { - mode = "maximum"; - } - } - return mode; -} - -bool IsSwap(const std::vector &tensors) { - auto CalShape = [](DLTensor *tensor) -> int { - int res = 1; - if (tensor == nullptr) { - MS_LOGE("input DLTensor is nullptr"); - return -1; - } - for (int i = 0; i < tensor->ndim; ++i) { - res *= tensor->shape[i]; - } - return res; - }; - - MS_ASSERT(tensors[0] != nullptr); - MS_ASSERT(tensors[1] != nullptr); - auto ndimA = tensors[0]->ndim; - auto ndimB = tensors[1]->ndim; - bool isSwap = false; - - if (ndimA <= ndimB) { - auto AShape = CalShape(tensors[0]); - auto BShape = CalShape(tensors[1]); - if (AShape < BShape) { - isSwap = true; - } - } - return isSwap; -} - -static runnerType GetKernel_Eltwise(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - std::string mode = GetEltwiseMode(opAttr, opdef); - - // make fid - int indexA = 0; - int indexB = 1; - MS_ASSERT(tensors[0] != nullptr); - MS_ASSERT(tensors[1] != nullptr); - auto ndimA = tensors[0]->ndim; - auto ndimB = tensors[1]->ndim; - - bool isSwap = IsSwap(tensors); - if (isSwap) { - std::swap(ndimA, ndimB); - std::swap(indexA, indexB); - } - - MS_ASSERT(tensors[indexA] != nullptr); - MS_ASSERT(tensors[indexB] != nullptr); - if (ndimA == 1 && tensors[indexA]->shape[NCHW_N] == 1) { - ndimA = 0; - } - if (ndimB == 1 && tensors[indexB]->shape[NCHW_N] == 1) { - ndimB = 0; - } - bool is_same = ndimA == ndimB && ndimA > 1; - for (int i = 0; i < tensors[indexB]->ndim && is_same; ++i) { - if (tensors[indexB]->shape[i] != tensors[indexA]->shape[i]) { - is_same = false; - } - } - for (int i = 0; i < tensors[indexB]->ndim && ndimB > 1 && is_same == false; ++i) { - if (tensors[indexB]->shape[i] == 1) { - ndimB--; - } - } - - if (ndimA == ndimB && ndimA >= 1) { - std::string fid = "Eltwise_" + mode + "_ndimA1_ndimB1" + "_" + opAttr.dtype; - auto fun = GetKernel(fid); - if (fun == nullptr) { - MS_LOGE("GetKernel return nullptr"); - return nullptr; - } - auto runner = [fun, isSwap](const std::vector &tensors) -> int { - std::vector tensorsCopy(tensors); - if (isSwap) { - iter_swap(tensorsCopy.begin(), tensorsCopy.begin() + 1); - } - int ndim[TENSOR_NUM_MAX]; - int64_t *shapes[TENSOR_NUM_MAX]; - int64_t *strides[TENSOR_NUM_MAX]; - int64_t shape = 1; - int64_t stride = 1; - - store_shape(tensorsCopy, ndim, shapes, strides, STORE_MODE); - update_shape_flatten(tensorsCopy, &shape, &stride); - fun(tensorsCopy); - store_shape(tensorsCopy, ndim, shapes, strides, RESUME_MODE); - return 0; - }; - return runner; - } else { - std::string fid = - "Eltwise_" + mode + "_ndimA" + std::to_string(ndimA) + "_ndimB" + std::to_string(ndimB) + "_" + opAttr.dtype; - auto fun = GetKernel(fid); - if (fun == nullptr) { - MS_LOGE("GetKernel return nullptr"); - return nullptr; - } - auto runner = [fun, isSwap](const std::vector &tensors) -> int { - std::vector tensorsCopy(tensors); - if (isSwap) { - iter_swap(tensorsCopy.begin(), tensorsCopy.begin() + 1); - } - - fun(tensorsCopy); - return 0; - }; - return runner; - } -} - -static runnerType GetKernel_Resize(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - if (tensors.size() != 2) { - MS_LOGE("Input tensors num should be 2 !"); - return nullptr; - } - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_Resize(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_Resize() is nullptr"); - return nullptr; - } - std::string fid = "Resize_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype; - if (op->method() == mindspore::predict::ResizeMethod::ResizeMethod_NEAREST_NEIGHBOR) { - fid += "_nearest_neighbor"; - } else if (op->method() == mindspore::predict::ResizeMethod::ResizeMethod_BILINEAR) { - fid += "_bilinear"; - } - fid += (op->alignCorners()) ? "_Align" : "_NotAlign"; - std::vector HeightWidth = { - static_cast(op->newHeight()), - static_cast(op->newWidth()), - }; - return GetKernel_Insert_vector_int32(fid, HeightWidth); -} - -static runnerType GetKernel_DataCarry(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - auto runner = [](const std::vector &tensors) -> int { - auto input = tensors.front(); - auto output = tensors.back(); - if (input == nullptr) { - MS_LOGE("input tensors.front() is nullptr"); - return 1; - } - if (output == nullptr) { - MS_LOGE("input tensors.back() is nullptr"); - return 1; - } - uint64_t input_num = 1; - for (int i = 0; i < input->ndim; ++i) { - input_num *= input->shape[i]; - } - uint64_t input_byte_num = input_num * input->dtype.lanes * input->dtype.bits / 8; - - uint64_t output_num = 1; - for (int i = 0; i < output->ndim; ++i) { - output_num *= output->shape[i]; - } - uint64_t output_byte_num = output_num * output->dtype.lanes * output->dtype.bits / 8; - - errno_t ret = memcpy_s(output->data, output_byte_num, input->data, input_byte_num); - if (ret != 0) { - MS_LOGE("memset_s failed."); - return ret; - } - return 0; - }; - return runner; -} - -static runnerType GetKernel_Shape(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - auto runner = [](const std::vector &tensors) -> int { - auto input = tensors.front(); - auto output = tensors.back(); - if (input == nullptr) { - MS_LOGE("input tensors.front() is nullptr"); - return 1; - } - if (output == nullptr) { - MS_LOGE("input tensors.back() is nullptr"); - return 1; - } - for (int i = 0; i < input->ndim; ++i) { - reinterpret_cast(output->data)[i] = static_cast(input->shape[i]); - } - return 0; - }; - return runner; -} - -void StridedSliceArgs(const std::vector &input_shape, std::vector *begin, std::vector *end, - std::vector *stride, uint32_t begin_mask, uint32_t end_mask, uint32_t ellipsis_mask, - uint32_t new_axis_mask, uint32_t shrink_axis_mask) { - MS_ASSERT(begin != nullptr); - MS_ASSERT(end != nullptr); - MS_ASSERT(stride != nullptr); - constexpr int support_dims = 8; - std::bitset begin_list(begin_mask); - std::bitset end_list(end_mask); - std::bitset ellipsis_list(ellipsis_mask); - std::bitset new_axis_list(new_axis_mask); - std::bitset shrink_list(shrink_axis_mask); - - std::string begin_list_s = begin_list.to_string().substr(support_dims - begin->size()); - reverse(begin_list_s.begin(), begin_list_s.end()); - - std::string end_list_s = end_list.to_string().substr(support_dims - end->size()); - reverse(end_list_s.begin(), end_list_s.end()); - - std::string ellipsis_list_s = ellipsis_list.to_string().substr(support_dims - end->size()); - reverse(ellipsis_list_s.begin(), ellipsis_list_s.end()); - - std::string new_axis_list_s = new_axis_list.to_string().substr(support_dims - end->size()); - reverse(new_axis_list_s.begin(), new_axis_list_s.end()); - - std::string shrink_list_s = shrink_list.to_string().substr(support_dims - end->size()); - reverse(shrink_list_s.begin(), shrink_list_s.end()); - - int new_axis_count = new_axis_list.count(); - if (ellipsis_list.any()) { - auto idx = 0; // ellipsis_list._Find_first(); - // the 1 is ellipsis - int ellipsis_length = input_shape.size() - (begin->size() - 1 - new_axis_count); - begin->erase(begin->begin() + idx); - end->erase(end->begin() + idx); - stride->erase(stride->begin() + idx); - - begin_list_s.erase(idx, 1); - end_list_s.erase(idx, 1); - ellipsis_list_s.erase(idx, 1); - new_axis_list_s.erase(idx, 1); - shrink_list_s.erase(idx, 1); - - if (ellipsis_length > 0) { - begin->insert(begin->begin() + idx, ellipsis_length, 0); - end->insert(end->begin() + idx, ellipsis_length, 0); - stride->insert(stride->begin() + idx, ellipsis_length, 1); - begin_list_s.insert(idx, ellipsis_length, '1'); - end_list_s.insert(idx, ellipsis_length, '1'); - ellipsis_list_s.insert(idx, ellipsis_length, '0'); - new_axis_list_s.insert(idx, ellipsis_length, '0'); - shrink_list_s.insert(idx, ellipsis_length, '0'); - } - } - - if (new_axis_count) { - for (int i = static_cast(new_axis_list_s.size()) - 1; i >= 0; i--) { - if (new_axis_list_s[i] == '1') { - begin->erase(begin->begin() + i); - end->erase(end->begin() + i); - stride->erase(stride->begin() + i); - begin_list_s.erase(i, 1); - end_list_s.erase(i, 1); - shrink_list_s.erase(i, 1); - } - } - } - - unsigned int size = begin->size(); - for (unsigned int i = 0; i < size; i++) { - if (shrink_list_s[i] == '1') { - auto beginItr = (begin->begin() + i); - auto endItr = (end->begin() + i); - auto strideItr = (stride->begin() + i); - *endItr = *beginItr + 1; - *strideItr = 1; - continue; - } - if (begin_list_s[i] == '1') { - auto beginItr = (begin->begin() + i); - *beginItr = 0; - } - if (end_list_s[i] == '1') { - auto endItr = (end->begin() + i); - *endItr = input_shape[i]; - } - } -} - -#define MAXDIMS 10 -template -int StridedSlice(const std::vector &input_shape, T *input, T *output, int *start, int *end, int *stride, - const int &output_size) { - MS_ASSERT(input != nullptr); - MS_ASSERT(output != nullptr); - MS_ASSERT(start != nullptr); - MS_ASSERT(end != nullptr); - MS_ASSERT(stride != nullptr); - int dimension = input_shape.size(); - if (dimension == 1) { - if (*stride == 1) { - int sizes = (*end - *start) * sizeof(T); - errno_t ret = memcpy_s(output, output_size * sizeof(T), input + *start, sizes); - if (ret != 0) { - MS_LOGE("memset_s failed: %d", ret); - return ret; - } - return 0; - } - for (int j = *start, i = 0; j < *end; j += (*stride), i++) { - output[i] = input[j]; - } - return 0; - } - - // adapt higher dimension - int dimensionArray[MAXDIMS]; - int factorArray[MAXDIMS]; - int totalElement = 0; - - for (int i = 0; i < dimension; i++) { - dimensionArray[i] = input_shape[i]; - factorArray[i] = i ? factorArray[i - 1] * dimensionArray[i] : dimensionArray[i]; - totalElement = i ? totalElement * dimensionArray[i] : dimensionArray[i]; - } - - int j = 0; - for (int k = 0; k < totalElement; k++) { - bool isValid = true; - for (int i = 0; i < dimension; i++) { - int tmp = (k / (totalElement / factorArray[i])) % dimensionArray[i]; - if (tmp < start[i] || tmp >= end[i]) { - isValid = false; - break; - } - isValid = isValid && ((tmp - start[i]) % stride[i] == 0); - } - if (isValid) { - output[j++] = input[k]; - } - } - - return 0; -} - -static runnerType GetKernel_StridedSlice(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto ndim = opAttr.ndim; - - auto op = opdef.attr_as_StridedSlice(); - if (op == nullptr) { - MS_LOGE("op is nullptr"); - return nullptr; - } - uint32_t begin_mask = op->beginMask(); - uint32_t end_mask = op->endMask(); - uint32_t ellipsis_mask = op->ellipsisMask(); - uint32_t new_axis_mask = op->newAxisMask(); - uint32_t shrink_axis_mask = op->shrinkAxisMask(); - std::vector begin; - std::vector end; - std::vector stride; - for (uint32_t i = 0; i < ndim; ++i) { - begin.push_back(op->begin()->Get(i)); - end.push_back(op->end()->Get(i)); - stride.push_back(op->stride()->Get(i)); - } - - auto runner = [begin_mask, end_mask, ellipsis_mask, new_axis_mask, shrink_axis_mask, begin, end, - stride](const std::vector &tensors) mutable -> int { - auto input = tensors.front(); - auto output = tensors.back(); - std::vector input_shape; - for (int i = 0; i < input->ndim; ++i) { - input_shape.push_back(input->shape[i]); - } - - int output_size = 1; - for (int i = 0; i < output->ndim; ++i) { - output_size *= output->shape[i]; - } - - StridedSliceArgs(input_shape, &begin, &end, &stride, begin_mask, end_mask, ellipsis_mask, new_axis_mask, - shrink_axis_mask); - - if (input->dtype.lanes != 1) { - MS_LOGE("StridedSlice input.dtype.lanes=%d invalid, only support 1", input->dtype.lanes); - return 1; - } - - if (input->dtype.bits == 16) { - StridedSlice(input_shape, reinterpret_cast(input->data), reinterpret_cast(output->data), - begin.data(), end.data(), stride.data(), output_size); - } else if (input->dtype.bits == 32) { - StridedSlice(input_shape, reinterpret_cast(input->data), reinterpret_cast(output->data), - begin.data(), end.data(), stride.data(), output_size); - } else { - MS_LOGE("StridedSlice input.dtype.bits=%d invalid, only support (16, 32)", input->dtype.bits); - return 1; - } - return 0; - }; - return runner; -} - -template -static void Permute4d(DLTensor *src, DLTensor *dst, const std::vector &shape, - const std::vector &strides) { - MS_ASSERT(src != nullptr); - MS_ASSERT(dst != nullptr); - int64_t N = shape[NCHW_N]; - int64_t C = shape[NCHW_C]; - int64_t H = shape[NCHW_H]; - int64_t W = shape[NCHW_W]; - auto src_data = reinterpret_cast(src->data); - auto dst_data = reinterpret_cast(dst->data); - int k = 0; - for (int n = 0; n < N; n++) - for (int c = 0; c < C; c++) - for (int h = 0; h < H; h++) - for (int w = 0; w < W; w++) { - dst_data[k++] = src_data[n * strides[0] + c * strides[1] + h * strides[2] + w * strides[3]]; - } -} - -static runnerType GetKernel_CaffeCrop(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_CaffeCrop(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_CaffeCrop() is nullptr"); - return nullptr; - } - std::string fid = - "CaffeCrop_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype + "_axis" + std::to_string(op->axis()); - - std::vector offsets(op->offsets()->size()); - for (size_t i = 0; i < offsets.size(); ++i) { - offsets[i] = op->offsets()->Get(i); - } - return GetKernel_Insert_vector_int32(fid, offsets); -} - -static runnerType GetKernel_CaffePReLU(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_CaffePReLU(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_CaffePReLU() is nullptr"); - return nullptr; - } - std::string fid = "CaffePReLU_ndim4_" + opAttr.dtype; - fid += (op->channelShared()) ? "_channelShared" : "_channelNotShared"; - auto fun = GetKernel(fid); - if (fun == nullptr) { - return nullptr; - } - bool reahpe_output = true; - return Pack_reahpeCHW(fun, tensors, ExpandCHW, reahpe_output); -} - -static runnerType GetKernel_FullConnection(const mindspore::predict::OpDef &opdef, - const std::vector &tensors, const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_FullConnection(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_FullConnection() is nullptr"); - return nullptr; - } - std::string fid = "FullConnection_ndimA2_" + opAttr.dtype; - fid += (op->hasBias()) ? "_hasBias" : "_notHasBias"; - auto fun = GetKernel(fid); - if (fun == nullptr) { - return nullptr; - } - bool reahpe_output = false; - return Pack_reahpeCHW(fun, tensors, FusedCHW, reahpe_output); -} - -static runnerType GetKernel_Power(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_Power(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_Power() is nullptr"); - return nullptr; - } - std::string fid = "Power_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype; - std::vector pss; - pss.push_back(op->power()); - pss.push_back(op->scale()); - pss.push_back(op->shift()); - return GetKernel_Insert_vector_float(fid, pss); -} - -static runnerType GetKernel_ArgMax(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_ArgMax(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_ArgMax() is nullptr"); - return nullptr; - } - std::string fid = - "ArgMax_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype + "_axis" + std::to_string(op->axis()); - fid += (op->keepDims()) ? "_keepDims" : "_notKeepDims"; - fid += "_top1"; - if (tensors.back() == nullptr) { - MS_LOGE("tensors.back() is nullptr"); - return nullptr; - } - fid += "_" + get_dtype(*tensors.back()); - return GetKernel(fid); -} - -void update_shape_Concat(const std::vector &tensors, int32_t axis, int64_t (&shape)[TENSOR_NUM_MAX][3], - int64_t (&strides)[TENSOR_NUM_MAX][3]) { - int64_t shape_low_dim = 1; - int64_t shape_high_dim = 1; - auto output = tensors.back(); - if (output == nullptr) { - MS_LOGW("tensors.back() is nullptr"); - return; - } - for (int32_t i = 0; i < axis; ++i) { - shape_high_dim *= output->shape[i]; - } - for (int32_t i = axis + 1; i < output->ndim; ++i) { - shape_low_dim *= output->shape[i]; - } - for (size_t i = 0; i < tensors.size(); ++i) { - shape[i][0] = shape_high_dim; - shape[i][1] = tensors[i]->shape[axis]; - shape[i][2] = shape_low_dim; - - strides[i][2] = 1; - strides[i][1] = shape[i][2]; - strides[i][0] = shape[i][2] * shape[i][1]; - - tensors[i]->ndim = 3; - tensors[i]->shape = shape[i]; - tensors[i]->strides = strides[i]; - } -} - -static runnerType GetKernel_Concat(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - if (tensors.size() < 2) { - MS_LOGE("Concat should have at least two tensors"); - return nullptr; - } - if (tensors.at(0) == nullptr) { - MS_LOGE("0th tensors of Concat is nullptr"); - return nullptr; - } - auto ndim = tensors.at(0)->ndim; - if (opdef.attr_as_Concat() == nullptr) { - MS_LOGE("opdef.attr_as_Concat() is nullptr"); - return nullptr; - } - auto axis = opdef.attr_as_Concat()->axis(); - if (axis < 0) { - axis += ndim; - } - std::string fid = - "Concat_ndim3_" + opAttr.dtype + "_input_num" + std::to_string(static_cast(tensors.size()) - 1) + "_axis1"; - auto fun = GetKernel(fid); - if (fun == nullptr) { - MS_LOGE("GetKernel return nullptr"); - return nullptr; - } - auto runner = [fun, axis](const std::vector &tensors) -> int { - int ndim[TENSOR_NUM_MAX]; - int64_t *shape[TENSOR_NUM_MAX]; - int64_t *strides[TENSOR_NUM_MAX]; - int64_t shape_C[TENSOR_NUM_MAX][3]; - int64_t strides_C[TENSOR_NUM_MAX][3]; - store_shape(tensors, ndim, shape, strides, STORE_MODE); - update_shape_Concat(tensors, axis, shape_C, strides_C); - fun(tensors); - store_shape(tensors, ndim, shape, strides, RESUME_MODE); - return 0; - }; - return runner; -} - -template -void Stack_ScaleNumber(const std::vector &tensors) { - if (tensors.empty()) { - MS_LOGW("input tensors is nullptr"); - return; - } - auto output = tensors.back(); - if (output != nullptr) { - MS_LOGW("tensors.back() is nullptr"); - return; - } - for (int i = 0; i < static_cast(tensors.size()) - 1; i++) { - reinterpret_cast(output->data)[i] = reinterpret_cast(tensors.at(i)->data)[0]; - } -} - -static runnerType GetKernel_Stack(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_Stack(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_Stack() is nullptr"); - return nullptr; - } - if (op->isScale()->Get(0)) { - auto runner = [](const std::vector &tensors) -> int { - auto input = tensors.front(); - if (input->dtype.bits == 8) { - Stack_ScaleNumber(tensors); - } else if (input->dtype.bits == 16) { - Stack_ScaleNumber(tensors); - } else if (input->dtype.bits == 32) { - Stack_ScaleNumber(tensors); - } else if (input->dtype.bits == 64) { - Stack_ScaleNumber(tensors); - } else { - MS_LOGE("StridedSlice input.dtype.bits=%d invalid, only support (8, 16, 32, 64)", input->dtype.bits); - return 1; - } - return 0; - }; - return runner; - } - std::string fid = "Stack_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype + "_input_num" + - std::to_string(static_cast(tensors.size()) - 1) + "_axis" + std::to_string(op->axis()); - return GetKernel(fid); -} - -static runnerType GetKernel_Pad(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_Pad(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_Pad() is nullptr"); - return nullptr; - } - std::string fid = "Pad_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype + "_" + - mindspore::predict::EnumNamePaddingMode(op->paddingmode()); - std::vector paddings(op->paddings()->size()); - for (size_t i = 0; i < paddings.size(); ++i) { - paddings[i] = op->paddings()->Get(i); - } - return GetKernel_Insert_vector_int32(fid, paddings); -} - -static runnerType GetKernel_Pooling(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_Pooling(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_Pooling() is nullptr"); - return nullptr; - } - auto H = tensors.front()->shape[NCHW_H]; - auto W = tensors.front()->shape[NCHW_W]; - auto padUp = op->padUp(); - auto padDown = op->padDown(); - auto padLeft = op->padLeft(); - auto padRight = op->padRight(); - bool useGlobal = false; - if (H == op->windowH() && W == op->windowW()) { - useGlobal = true; - } - if (op->padMode() != mindspore::predict::PadMode_VALID) { - int64_t outputHeight = tensors.back()->shape[NCHW_H]; - int64_t outputWidth = tensors.back()->shape[NCHW_W]; - - int64_t dHeight = (outputHeight - 1) * op->strideH() + op->windowH() - H; - int64_t dWidth = (outputWidth - 1) * op->strideW() + op->windowW() - W; - padUp = dHeight / 2; - padDown = dHeight - dHeight / 2; - padLeft = dWidth / 2; - padRight = dWidth - dWidth / 2; - if (padDown < 0) { - padDown = 0; - } - if (padRight < 0) { - padRight = 0; - } - } - std::string poolingMode = mindspore::predict::EnumNamesPoolMode()[op->poolingMode()]; - if (poolingMode != "MAX_POOLING" && poolingMode != "MEAN_POOLING") { - MS_LOGE("Pooling op not support poolingMode=%s", poolingMode.c_str()); - return nullptr; - } - - std::string fid; - fid += useGlobal ? "GlobalPooling" : "Pooling"; - fid += "_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype; - fid += (poolingMode == "MAX_POOLING") ? "_max" : "_avg"; - if (!useGlobal) { - fid += "_kernel" + std::to_string(op->windowH()) + std::to_string(op->windowW()); - fid += "_stride" + std::to_string(op->strideH()) + std::to_string(op->strideW()); - fid += - "_pad" + std::to_string(padUp) + std::to_string(padDown) + std::to_string(padLeft) + std::to_string(padRight); - if (op->caffeMode() && (padUp || padDown || padLeft || padRight)) fid += "_caffe"; - } - auto fun = GetKernel(fid); - if (fun == nullptr) { - MS_LOGE("GetKernel return nullptr"); - return nullptr; - } - return (opdef.isLastConv()) ? Pack_NCHW2NHWC(fun) : fun; -} - -static runnerType GetKernel_Mean(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_Mean(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_Mean() is nullptr"); - return nullptr; - } - std::string axis_str = ""; - for (uint32_t i = 0; i < op->axis()->size(); ++i) { - axis_str += std::to_string(op->axis()->Get(i)); - } - std::string fid = "Mean_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype + "_axis" + axis_str; - fid += (op->keepDims()) ? "_keepDims" : "_notkeepDims"; - return GetKernel(fid); -} - -static runnerType GetKernel_MatMul(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_MatMul(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_MatMul() is nullptr"); - return nullptr; - } - std::string fid = "MatMul_ndimA2_ndimB2_" + opAttr.dtype; - fid += (op->transposeA()) ? "_1" : "_0"; - fid += (op->transposeB()) ? "_1" : "_0"; - return GetKernel(fid); -} - -static runnerType GetKernel_Softmax(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_SoftMax(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_SoftMax() is nullptr"); - return nullptr; - } - std::string fid = - "Softmax_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype + "_axis" + std::to_string(op->axis()->Get(0)); - return GetKernel(fid); -} - -void update_shape_Activation(const std::vector &tensors, int64_t *shape, int64_t *strides) { - auto input = tensors.front(); - MS_ASSERT(input != nullptr); - for (int32_t i = 0; i < input->ndim; ++i) { - *shape *= input->shape[i]; - } - for (size_t i = 0; i < tensors.size(); ++i) { - MS_ASSERT(tensors[i] != nullptr); - tensors[i]->ndim = 1; - tensors[i]->shape = shape; - tensors[i]->strides = strides; - } -} - -static runnerType GetKernel_Activation(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_Activation(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_Activation() is nullptr"); - return nullptr; - } - std::string fid = - "Activation_ndim1_" + opAttr.dtype + "_" + std::string(mindspore::predict::EnumNameActivationType(op->type())); - - auto fun = GetKernel(fid); - if (fun == nullptr) { - MS_LOGE("GetKernel return nullptr"); - return nullptr; - } - auto runner = [fun](const std::vector &tensors) -> int { - int ndim[TENSOR_NUM_MAX]; - int64_t *shapes[TENSOR_NUM_MAX]; - int64_t *strides[TENSOR_NUM_MAX]; - int64_t shape = 1; - int64_t stride = 1; - - store_shape(tensors, ndim, shapes, strides, STORE_MODE); - update_shape_Activation(tensors, &shape, &stride); - fun(tensors); - store_shape(tensors, ndim, shapes, strides, RESUME_MODE); - return 0; - }; - return runner; -} - -static runnerType GetKernel_Exp(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - std::string fid = "Exp_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype; - return GetKernel(fid); -} - -static runnerType GetKernel_Cast(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - MS_ASSERT(tensors.front() != nullptr); - MS_ASSERT(tensors.back() != nullptr); - auto src_dtype = get_dtype(*tensors.front()); - auto dst_dtype = get_dtype(*tensors.back()); - std::string fid = "Cast_ndim" + std::to_string(tensors.front()->ndim) + "_" + src_dtype + "_" + dst_dtype; - return GetKernel(fid); -} - -static runnerType GetKernel_ExpandDims(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_ExpandDims(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_ExpandDims() is nullptr"); - return nullptr; - } - std::string fid = - "ExpandDims_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype + "_axis" + std::to_string(op->dim()); - return GetKernel(fid); -} - -static runnerType GetKernel_Tile(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_Tile(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_Tile() is nullptr"); - return nullptr; - } - std::string fid = "Tile_ndim" + std::to_string(opAttr.ndim) + "_" + opAttr.dtype; - std::vector multiples; - for (size_t i = 0; i < op->multiples()->size(); ++i) { - multiples.push_back(op->multiples()->Get(i)); - } - return GetKernel_Insert_vector_int32(fid, multiples); -} - -static runnerType GetKernel_Range(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - const OpCommonAttr opAttr(opdef, tensors); - auto op = opdef.attr_as_Range(); - if (op == nullptr) { - MS_LOGE("opdef.attr_as_Range() is nullptr"); - return nullptr; - } - std::string fid = "Range_ndim_" + opAttr.dtype; - std::vector vec = {static_cast(op->start()), static_cast(op->delta())}; - - auto f = GetFunction(fid); - if (f == nullptr) { - MS_LOGE("GetFunction returu nullptr"); - return nullptr; - } - auto runner = [f, vec](const std::vector &tensors_origin) -> int { - // remove 3 input, only remain output - const std::vector tensors = {tensors_origin.back()}; - std::vector values(vec.size() + tensors.size()); - std::vector codes(values.size()); - tvm::runtime::TVMArgsSetter setter(values.data(), codes.data()); - for (size_t i = 0; i < vec.size(); ++i) { - setter(i, vec.at(i)); - } - for (size_t i = 0; i < tensors.size(); ++i) { - setter(i + vec.size(), tensors.at(i)); - } - auto passfunc = reinterpret_cast(f); - return passfunc(values.data(), codes.data(), values.size()); - }; - return runner; -} - -using GetKernelFunType = std::function &tensors, const KernelOption &option)>; - -static const std::unordered_map g_kernel_op_list = { - {"Conv2D", GetKernel_Conv}, - {"DepthwiseConv2D", GetKernel_Conv}, - {"DeDepthwiseConv2D", GetKernel_Conv}, - {"DeConv2D", GetKernel_Conv_var}, - {"BatchNorm", GetKernel_BatchNorm}, - {"CaffeBatchNorm", GetKernel_BatchNorm}, - {"BiasAdd", GetKernel_BatchNorm}, - {"Scale", GetKernel_BatchNorm}, - {"Eltwise", GetKernel_Eltwise}, - {"Add", GetKernel_Eltwise}, - {"Sub", GetKernel_Eltwise}, - {"Mul", GetKernel_Eltwise}, - {"RealDiv", GetKernel_Eltwise}, - {"Maximum", GetKernel_Eltwise}, - {"ResizeBilinear", GetKernel_Resize}, - {"ResizeNearestNeighbor", GetKernel_Resize}, - {"Squeeze", GetKernel_DataCarry}, - {"Reshape", GetKernel_DataCarry}, - {"Shape", GetKernel_Shape}, - {"StridedSlice", GetKernel_StridedSlice}, - {"CaffeCrop", GetKernel_CaffeCrop}, - {"CaffePReLU", GetKernel_CaffePReLU}, - {"FullConnection", GetKernel_FullConnection}, - {"Power", GetKernel_Power}, - {"ArgMax", GetKernel_ArgMax}, - {"Concat", GetKernel_Concat}, - {"Stack", GetKernel_Stack}, - {"Pad", GetKernel_Pad}, - {"Pooling", GetKernel_Pooling}, - {"Mean", GetKernel_Mean}, - {"MatMul", GetKernel_MatMul}, - {"SoftMax", GetKernel_Softmax}, - {"Activation", GetKernel_Activation}, - {"Exp", GetKernel_Exp}, - {"Cast", GetKernel_Cast}, - {"ExpandDims", GetKernel_ExpandDims}, - {"Tile", GetKernel_Tile}, - {"Range", GetKernel_Range}, -}; - -GetKernelFunType Get_GetKernelFun(const std::string &optype) { - auto it = g_kernel_op_list.find(optype); - return (it != g_kernel_op_list.end() ? it->second : nullptr); -} -} // namespace auto_tensor - -runnerType GetKernel(const mindspore::predict::OpDef &opdef, const std::vector &tensors, - const KernelOption &option) { - std::string optype = mindspore::predict::EnumNameOpT(opdef.attr_type()); - auto GetKernelFun = auto_tensor::Get_GetKernelFun(optype); - if (GetKernelFun != nullptr) { - return GetKernelFun(opdef, tensors, option); - } else { - return nullptr; - } -} - -int CallKernel(const std::string &fid, const std::vector &tensors) { - auto runner = GetKernel(fid); - return runner(tensors); -} - -#endif // LITE_RUNTIME_ON diff --git a/predict/module/tvm_kernel/lite/src/api/tvm_op_module.cc b/predict/module/tvm_kernel/lite/src/api/tvm_op_module.cc deleted file mode 100644 index c6a6fc93f5d..00000000000 --- a/predict/module/tvm_kernel/lite/src/api/tvm_op_module.cc +++ /dev/null @@ -1,94 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this ${file} except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include "src/api/tvm_op_module.h" -#include "common/op_utils.h" -#include "lite/api/km_api.h" -#include "src/op.h" -namespace mindspore { -namespace predict { -using OpFunc = std::function &)>; - -class TVMOperator : public OpBase { - public: - explicit TVMOperator(OpFunc func) : opfunc(std::move(func)) {} - ~TVMOperator() override = default; - int Init(const std::vector &inputs, const std::vector &outputs) override { return 0; } - int Execute(const std::vector &inputs, const std::vector &outputs) override { - std::vector dlT; - for (auto input : inputs) { - MS_ASSERT(input != nullptr); - dlT.push_back(input->GetDLTensor()); - } - - for (auto output : outputs) { - MS_ASSERT(output != nullptr); - dlT.push_back(output->GetDLTensor()); - } - return opfunc(dlT); - } - - static OpBase *CreateOp(const std::vector &inputs, const std::vector &outputs, const OpDef &opDef, - const Context &ctx, const OpDesc &desc) { - std::vector dlT; - for (auto input : inputs) { - MS_ASSERT(input != nullptr); - dlT.push_back(input->GetDLTensor()); - } - - for (auto output : outputs) { - MS_ASSERT(output != nullptr); - dlT.push_back(output->GetDLTensor()); - } - - KernelOption option; - option.numThreads = ctx.threadNum; - OpFunc opFunc = GetKernel(opDef, dlT, option); - if (opFunc != nullptr) { - auto op = std::unique_ptr(new (std::nothrow) TVMOperator(opFunc)); - if (op == nullptr) { - MS_LOGE("new TVMOperator failed"); - } - return op.release(); - } - return nullptr; - } - - private: - OpFunc opfunc; - std::vector dltensors; -}; - -TVMOpRegistry::TVMOpRegistry() = default; - -mindspore::predict::OpCreator TVMOpRegistry::GetOpCreator(const mindspore::predict::OpDesc &desc) { - return TVMOperator::CreateOp; -} - -OpRegistry *TVMOpModule::GetInstance() { - static TVMOpRegistry tvmOpRegistry; - return &tvmOpRegistry; -} - -static TVMOpModule tvmOpModule; - -static ModuleRegistrar> g_tvmOpReg(MODULE_REG_NAME_OP_REGISTRY, tvmOpModule); -} // namespace predict -} // namespace mindspore diff --git a/predict/module/tvm_kernel/lite/src/api/tvm_op_module.h b/predict/module/tvm_kernel/lite/src/api/tvm_op_module.h deleted file mode 100644 index 3f0f33142dd..00000000000 --- a/predict/module/tvm_kernel/lite/src/api/tvm_op_module.h +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this ${file} except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef PREDICT_MODULE_TVM_KERNEL_LITE_SRC_API_TVM_OP_MODULE_H_ -#define PREDICT_MODULE_TVM_KERNEL_LITE_SRC_API_TVM_OP_MODULE_H_ - -#include "src/op_registry.h" -namespace mindspore { -namespace predict { -class TVMOpRegistry : public OpRegistry { - public: - TVMOpRegistry(); - OpCreator GetOpCreator(const OpDesc &desc) override; -}; - -class TVMOpModule : public Module { - public: - OpRegistry *GetInstance() override; -}; -} // namespace predict -} // namespace mindspore -#endif // PREDICT_MODULE_TVM_KERNEL_LITE_SRC_API_TVM_OP_MODULE_H_ diff --git a/predict/module/tvm_kernel/lite/src/codegen/llvm/lite_rtfunc_reset.cc b/predict/module/tvm_kernel/lite/src/codegen/llvm/lite_rtfunc_reset.cc deleted file mode 100644 index df19676b406..00000000000 --- a/predict/module/tvm_kernel/lite/src/codegen/llvm/lite_rtfunc_reset.cc +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this ${file} except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include "tvm/api_registry.h" - -namespace tvm { -namespace codegen { -class LiteRTFuncReseter { - public: - LiteRTFuncReseter() {} - ~LiteRTFuncReseter() {} - int InsertFuncPair(std::string sfunc, std::string dfunc) { - CHECK_NE(sfunc.size(), 0); - CHECK_NE(dfunc.size(), 0); - func_map_[sfunc] = dfunc; - return 0; - } - - /* - * the llvm::Function::Create need a longe life scope const char* as input - * so here not return block life scopte tmp std::string. - */ - const char* GetResetFunc(std::string sfunc) { - CHECK_NE(sfunc.size(), 0); - auto it_dfunc = func_map_.find(sfunc); - if (it_dfunc != func_map_.end()) { - return it_dfunc->second.c_str(); - } else { - func_map_[sfunc] = sfunc; - return func_map_[sfunc].c_str(); - } - } - - /* - * not real delete item paire, just set orig function pair - */ - int DeleteFuncPair(std::string sfunc) { - CHECK_NE(sfunc.size(), 0); - func_map_[sfunc] = sfunc; - return 0; - } - static LiteRTFuncReseter* GetRTFuncReseter() { - static LiteRTFuncReseter inst; - return &inst; - } - - private: - std::map func_map_; -}; - -TVM_REGISTER_API("codegen.SetRTFuncTransPair").set_body([](const TVMArgs& targs, TVMRetValue* rv) { - *rv = LiteRTFuncReseter::GetRTFuncReseter()->InsertFuncPair(targs[0], targs[1]); -}); - -TVM_REGISTER_API("codegen.DelRTFuncTransPair").set_body([](const TVMArgs& targs, TVMRetValue* rv) { - *rv = LiteRTFuncReseter::GetRTFuncReseter()->DeleteFuncPair(targs[0]); -}); - -/* - * now no operator=(const char* ) provide for TVMRetValue - * here using explicit operator call function to make sure not using operator=(int) - */ -TVM_REGISTER_API("codegen.GetTransRTFunc").set_body([](const TVMArgs& targs, TVMRetValue* rv) { - (*rv).operator=( - reinterpret_cast(const_cast(LiteRTFuncReseter::GetRTFuncReseter()->GetResetFunc(targs[0])))); -}); -} // namespace codegen -} // namespace tvm diff --git a/predict/schema/inner/README b/predict/schema/inner/README deleted file mode 100644 index f397efe4c57..00000000000 --- a/predict/schema/inner/README +++ /dev/null @@ -1 +0,0 @@ -for flatbuf auto generated file diff --git a/predict/schema/ms.fbs b/predict/schema/ms.fbs deleted file mode 100644 index f66abf8a866..00000000000 --- a/predict/schema/ms.fbs +++ /dev/null @@ -1,153 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -include "op.fbs"; - -namespace mindspore.predict; - -enum DataType : int { - DT_FLOAT = 0, - DT_FLOAT16 = 1, - DT_INT8 = 2, - DT_INT32 = 3, - DT_UINT8 = 4, - DT_UINT32 = 8, - DT_UNDEFINED = 16 -} - -enum Format : int { - NCHW = 0, - NHWC, - NC4HW4 = 100, - NUM_OF_FORMAT -} - -enum MSConst: int { - WEIGHT_REFCOUNT = 999 -} - -table QuantizationDef { - // Quantized value q, corresponding float value r: - // r = scale * (q - zero_point), where scale = (rmax - rmin) / (qmax - qmin) - min: [float]; - max: [float]; - scale: [float]; - zero_point: [long]; - - // Tensor shape of the specifies dimension. - dimension: int; -} - -table TensorDef { - // data type - dataType: DataType; - // shape - dims: [int]; - format: Format; - refCount: int; - offset: int; - data: [ubyte]; - quantization: QuantizationDef; -} - -union OpT { - Concat, - SoftMax, - Activation, - Conv2D, - FusedBatchNorm, - CaffeBatchNorm, - Squeeze, - BiasAdd, - Pooling, - DepthwiseConv2D, - DeDepthwiseConv2D, - Resize, - DetectionPostProcess, - FullConnection, - Mean, - DeConv2D, - Scale, - Reshape, - Eltwise, - NetOutput, - Add, - MatMul, - StridedSlice, - Power, - Slice, - Stack, - Mul, - Pad, - Maximum, - CaffePReLU, - ArgMax, - Exp, - CaffeCrop, - Range, - ExpandDims, - Tile, - Cast -// Split -} - -enum QuantType: int { - QUANT_NONE, - QUANT_INT8 -} - -table OpDef { - name: string; - attr: OpT; - inputIndex: [uint]; - outputIndex: [uint]; - isLastConv: bool; - quantType: QuantType = QUANT_NONE; -} - - -enum FmkType: int { - TF, - CAFFE -} - -table NodeDef { - fmkType: FmkType; - opDef: OpDef; -} - - -table SubGraphDef { - name: string; - inputIndex: [uint]; - outputIndex: [uint]; - mempoolSize: uint; - nodes: [NodeDef]; - allTensors: [TensorDef]; // weight + input + output -} - -table MempoolCfg { - size: uint; - shiftFactor: uint; -} - -table GraphDef { - name: string; - mempoolCfg: MempoolCfg; - subgraphs: [SubGraphDef]; -} - -root_type GraphDef; diff --git a/predict/schema/op.fbs b/predict/schema/op.fbs deleted file mode 100755 index 4d01bb9c3bc..00000000000 --- a/predict/schema/op.fbs +++ /dev/null @@ -1,351 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -namespace mindspore.predict; - -enum ResizeMethod: byte { - UNKNOW = -1, - BILINEAR = 0, - NEAREST_NEIGHBOR = 1 -} - -enum DataFormatType : byte { - UNKNOW = -1, - NCHW = 0, - NHWC = 1, - HWC = 2, // for input image or resize - CHW = 3, // for input image or resize -} - -enum ActivationType : byte { - NO_ACTIVATION = 0, - RELU = 1, - SIGMOID = 2, - RELU6 = 3, - ELU = 4, - LEAKY_RELU = 5, - ABS = 6, - RELU1 = 7, - SOFTSIGN = 8, - SOFTPLUS = 9, - TANH = 10, - UNKNOW = 11 -} - -enum PoolMode : byte { - MAX_POOLING = 0, - MEAN_POOLING = 1, - GLOBAL_POOING = 2 -} - -enum EltwiseMode : byte { - PROD = 0, - SUM = 1, - MAXIMUM = 2 -} - -enum PadMode : byte { - NOTSET=0, - SAME=1, - VALID=2, - CAFFE_CEIL_NEW=4 -} - -enum PaddingMode : byte { - CONSTANT = 0, - REFLECT = 1, - SYMMETRIC = 2, - MODE_RESERVED = 3 -} - -table Pad { - paddingmode: PaddingMode; - paddings: [int]; -} - -table Maximum { - format: DataFormatType = 0; -} - -table Concat { - axis: int; - n: int; -} - -table SoftMax { - axis: [int]; -} - -table Activation { - type: ActivationType = 0; -} - -table Conv2D { - format: DataFormatType = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; - activationType: ActivationType = 0; -} - -table FusedBatchNorm { - epsilon: float; // eg. epsilon=0.001 -} - -table CaffeBatchNorm { - epsilon: float; // eg. epsilon=0.001 -} - -table Squeeze { - axis: [int]; -} - -table BiasAdd { - axis: [int]; -} - -table Pooling { - format: DataFormatType = 0; - poolingMode: PoolMode; - windowW: int; - windowH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - caffeMode: bool = false; -} - -table DepthwiseConv2D { - format: DataFormatType = 0; - channelIn: int; - channelMultiplier: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; - activationType: ActivationType = 0; -} - -table DeDepthwiseConv2D { - format: DataFormatType = 0; - channelIn: int; - channelMultiplier: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; - activationType: ActivationType = 0; -} - - -table Resize { - format: DataFormatType = 0; - method: ResizeMethod; - newHeight: long; - newWidth: long; - alignCorners: bool = false; - preserveAspectRatio: bool = false; -} - -table DetectionPostProcess { - format: DataFormatType = 0; - inputSize: int; - hScale: float; - wScale: float; - xScale: float; - yScale: float; - NmsIouThreshold: float; - NmsScoreThreshold: float; - MaxDetections: long; - DetectionsPreClass: long; - MaxClassesPreDetection: long; - NumClasses: long; - UseRegularNms: bool; -} - -table FullConnection { - format: DataFormatType = 0; - hasBias: bool; - axis: int; -} - -// Mean(input_tensor, axis, keep_dims) -table Mean { - axis: [int]; - keepDims: bool = false; -} - -table DeConv2D { - format: DataFormatType = 0; - group: int; - channelIn: int; - channelOut: int; - kernelW: int; - kernelH: int; - strideW: int; - strideH: int; - padMode: PadMode; - padUp: int; - padDown: int; - padLeft: int; - padRight: int; - dilateW: int; - dilateH: int; - hasBias: bool = false; - activationType: ActivationType = 0; -} - -table Scale { - format: DataFormatType = 0; -} - -table Eltwise { - format: DataFormatType = 0; - mode: EltwiseMode; -} - -table Add { - format: DataFormatType = 0; -} - -table Slice { - format: DataFormatType = 0; - begin: [int]; - end: [int]; - stride: [int]; -} - -table Mul { -} - -table Exp { -} - -table Reshape { - format: DataFormatType = 0; - shape: [long]; -} - -table Power { - power: float; - scale: float; - shift: float; -} - -table ArgMax { - axis: int; - outMaxValue: bool; - topK: int; - keepDims: bool; - axisType: int; -} - -table NetOutput { - format: DataFormatType = 0; -} - -table MatMul { - transposeA : bool = false; - transposeB : bool = false; -} - -table CaffePReLU { - channelShared : bool = false; -} - -table StridedSlice { - beginMask: int; - endMask: int; - ellipsisMask: int; - newAxisMask: int; - shrinkAxisMask: int; - begin: [int]; - end: [int]; - stride: [int]; - isScale: [int]; -} - -table Stack { - axis: int; - n: int; - isScale: [int]; -} - -table Range { - start: int; - limit: int; - delta: int; -} - -table ExpandDims { - dim: int; -} - -table Tile { - multiples: [int]; -} - -table Cast { - srcT: int; - dstT: int; -} - -table Split { - numberSplit: int; - sizeSplits: [int]; - splitDim: int; -} - -table CaffeCrop { - axis : long; - offsets : [long]; -} - -table Permute { - order: [long]; -} diff --git a/predict/src/CMakeLists.txt b/predict/src/CMakeLists.txt deleted file mode 100644 index 92c45473d7d..00000000000 --- a/predict/src/CMakeLists.txt +++ /dev/null @@ -1,69 +0,0 @@ -cmake_minimum_required(VERSION 3.12) -project(mspredict) - -set(CMAKE_CXX_STANDARD 11) - -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/..) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../module/tvm_kernel/incubator-tvm/3rdparty/dlpack/include/) - -link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../output/lib/) - -if (ENABLE_ASAN) - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -o0 -fsanitize=address -fno-omit-frame-pointer -fsanitize=undefined") -endif() - -set(MSPREDICT_SRC - runtime/allocator.cc - runtime/allocator.h - runtime/thread_pool.cc - runtime/thread_pool.h - runtime/workspace_pool.cc - runtime/workspace_pool.h - runtime/runtime_api.cc - runtime/runtime_api.h - context.cc - graph.cc - graph.h - graph_execution.cc - graph_execution.h - node.cc - node.h - op.cc - op.h - op_factory.cc - op_factory.h - op_registry.cc - op_registry.h - session.cc - tensor.cc - ${CMAKE_CURRENT_SOURCE_DIR}/operator/cpu/common/op_func_comm.cc) - -set(MSPREDICT_SRC ${MSPREDICT_SRC} - ${CMAKE_CURRENT_SOURCE_DIR}/../common/graph_util.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../common/utils.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../common/mslog.cc - ${CMAKE_CURRENT_SOURCE_DIR}/../common/module_registry.cc) - -add_library(mspredict SHARED ${MSPREDICT_SRC}) - -if(ENABLE_PREDICT_ARM64 OR ENABLE_PREDICT_ARM32) - target_link_libraries(mspredict android log tvm_kernel libsecurec.a) -else() - target_link_libraries(mspredict pthread tvm_kernel libsecurec.a) -endif() - -add_dependencies(mspredict tvm_kernel) -add_dependencies(mspredict securec) -add_dependencies(mspredict gtest) - -add_custom_command(TARGET mspredict POST_BUILD - COMMAND mkdir -pv ${PREDICT_DIR}/output/lib - COMMAND cp ${PREDICT_BUILD_DIR}/src/libmspredict.so ${PREDICT_DIR}/output/lib/ - COMMAND cp ${PREDICT_BUILD_DIR}/module/tvm_kernel/lite/libtvm_kernel.so ${PREDICT_DIR}/output/lib/ - COMMAND mkdir -pv ${PREDICT_DIR}/output/include - COMMAND cp -r ${PREDICT_DIR}/include/* ${PREDICT_DIR}/output/include - COMMAND mkdir -pv ${PREDICT_DIR}/output/include/schema/inner - COMMAND cp ${PREDICT_DIR}/schema/ms_generated.h ${PREDICT_DIR}/output/include/schema/inner - COMMAND cp ${PREDICT_DIR}/schema/op_generated.h ${PREDICT_DIR}/output/include/schema/inner - COMMAND mkdir -pv ${PREDICT_DIR}/output/include/dlpack/ - COMMAND cp ${PREDICT_DIR}/module/tvm_kernel/incubator-tvm/3rdparty/dlpack/include/dlpack/dlpack.h ${PREDICT_DIR}/output/include/dlpack/) diff --git a/predict/src/context.cc b/predict/src/context.cc deleted file mode 100644 index 45ec719db6a..00000000000 --- a/predict/src/context.cc +++ /dev/null @@ -1,33 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "include/context.h" -#include "include/tensor.h" -#include "src/runtime/allocator.h" - -namespace mindspore { -namespace predict { -Context::Context() { allocator = Allocator::Create(); } - -Context::~Context() {} - -Context::Context(int threadNum, std::shared_ptr allocator, DLContext deviceCtx) { - this->allocator = allocator; - this->threadNum = threadNum; - this->deviceCtx = deviceCtx; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/src/graph.cc b/predict/src/graph.cc deleted file mode 100644 index 5e1aab6a56e..00000000000 --- a/predict/src/graph.cc +++ /dev/null @@ -1,378 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/graph.h" -#include -#include -#include -#include -#include "schema/ms_generated.h" -#include "common/graph_util.h" -#include "common/mslog.h" -#include "include/errorcode.h" -#include "src/graph_execution.h" - -namespace mindspore { -namespace predict { -static const uint32_t G_MAX_OP_COUNT = 10000; - -Graph *Graph::CreateFromBuf(const char *buf, size_t size, const Context &ctx) { - if (buf == nullptr) { - MS_LOGE("the input buffer is nullptr"); - return nullptr; - } - - flatbuffers::Verifier verify((const uint8_t *)buf, size); - if (!VerifyGraphDefBuffer(verify)) { - MS_LOGE("the buffer is invalid and fail to create graph"); - return nullptr; - } - - auto graphDef = GetGraphDef(buf); - std::unique_ptr graph(new (std::nothrow) Graph()); - if (graph == nullptr) { - MS_LOGE("graph malloc fail"); - return nullptr; - } - auto ret = graph->Build(*graphDef, ctx); - if (ret != RET_OK) { - MS_LOGE("build graph fail"); - return nullptr; - } - return graph.release(); -} - -Graph::Graph() = default; - -Graph::~Graph() { - for (auto &subgraph : subgraphs) { - delete subgraph; - } - subgraphs.clear(); -} - -int Graph::Build(const GraphDef &graphDef, const Context &ctx) { - MS_ASSERT(graphDef.subgraphs() != nullptr); - for (size_t i = 0; i < graphDef.subgraphs()->size(); i++) { - MS_ASSERT(graphDef.subgraphs()->GetAs(i) != nullptr); - SubGraph *subGraph = SubGraph::CreateSubGraph(*(graphDef.subgraphs()->GetAs(i)), ctx); - if (subGraph == nullptr) { - MS_LOGE("converter subgraph failed"); - return RET_ERROR; - } - subgraphs.push_back(subGraph); - auto subDepends = subGraph->GetDepends(); - depends.insert(subDepends.begin(), subDepends.end()); - } - - auto iter = depends.begin(); - while (iter != depends.end()) { - if (iter->second.empty()) { - readyQue.push_back(iter->first); - iter = depends.erase(iter); - } else { - iter++; - } - } - - return RET_OK; -} - -std::vector Graph::GetInputs() { - MS_ASSERT(subgraphs.front() != nullptr); - return subgraphs.front()->GetInputs(); -} - -std::vector Graph::GetOutputs() { - MS_ASSERT(subgraphs.back() != nullptr); - return subgraphs.back()->GetOutputs(); -} - -std::map> &Graph::GetOutputsMap() { - MS_ASSERT(subgraphs.back() != nullptr); - return subgraphs.back()->GetOutputsMap(); -} - -void Graph::FreeAllTensors() { - for (auto iter : subgraphs) { - iter->FreeAllTensors(); - } -} - -std::vector *Graph::Subgraphs() { return &subgraphs; } - -SubGraph::SubGraph() = default; - -SubGraph::~SubGraph() { - for (auto iter = nodes.begin(); iter != nodes.end();) { - if (iter->second != nullptr) { - delete iter->second; - } - iter = nodes.erase(iter); - } - nodes.clear(); - - for (auto &allTensor : allTensors) { - if (allTensor != nullptr) { - delete allTensor; - } - } - allTensors.clear(); -} - -SubGraph *SubGraph::CreateSubGraph(const SubGraphDef &subGraphDef, const Context &ctx) { - std::unique_ptr subGraph(new (std::nothrow) SubGraph()); - if (subGraph == nullptr) { - MS_LOGE("subGraph malloc fail"); - return nullptr; - } - - auto ret = subGraph->Build(subGraphDef, ctx); - if (ret != RET_OK) { - MS_LOGE("subGraph Build fail"); - return nullptr; - } - - return subGraph.release(); -} - -int SubGraph::Build(const SubGraphDef &subGraphDef, const Context &ctx) { - int ret; - MS_ASSERT(subGraphDef.inputIndex() != nullptr); - ret = ConverterIndex(*(subGraphDef.inputIndex()), &inputIndices); - if (ret != RET_OK) { - MS_LOGE("ConverterIndex failed: %d", ret); - return ret; - } - MS_LOGD("converter inputIndex succ"); - - MS_ASSERT(subGraphDef.outputIndex() != nullptr); - ret = ConverterIndex(*(subGraphDef.outputIndex()), &outputIndices); - if (ret != RET_OK) { - MS_LOGE("ConverterIndex failed: %d", ret); - return ret; - } - MS_LOGD("converter outputIndex succ"); - MS_ASSERT(subGraphDef.allTensors() != nullptr); - ret = ConverterAllTensor(*(subGraphDef.allTensors())); - if (ret != RET_OK) { - MS_LOGE("ConverterAllTensor failed: %d", ret); - return ret; - } - MS_LOGD("converter AllTensor succ"); - MS_ASSERT(subGraphDef.nodes() != nullptr); - ret = ConverterNodes(*(subGraphDef.nodes()), ctx); - if (ret != RET_OK) { - MS_LOGE("ConverterNodes failed: %d", ret); - return ret; - } - MS_LOGD("converter nodes succ"); - - ret = ConverterEdges(subGraphDef); - if (ret != RET_OK) { - MS_LOGE("ConverterEdges failed: %d", ret); - return ret; - } - MS_LOGD("converter edges succ"); - - ret = InitOutputsMap(); - if (ret != RET_OK) { - MS_LOGE("InitOutputsMap failed: %d", ret); - return ret; - } - MS_LOGD("init outputs map succ"); - - MS_LOGD("build graph succ"); - return RET_OK; -} - -int SubGraph::ConverterIndex(const flatbuffers::Vector &srcIndex, std::vector *dstIndex) { - if (dstIndex == nullptr) { - MS_LOGE("input dstIndex is nullptr"); - return RET_PARAM_INVALID; - } - dstIndex->resize(srcIndex.size()); - std::copy(srcIndex.begin(), srcIndex.end(), dstIndex->begin()); - return RET_OK; -} - -int SubGraph::ConverterAllTensor(const flatbuffers::Vector> &srcTensors) { - uint32_t tensorsSize = srcTensors.size(); - - allTensors.clear(); - allTensors.reserve(tensorsSize); - for (uint32_t i = 0; i < tensorsSize; i++) { - auto tensorDef = srcTensors.GetAs(i); - if (tensorDef == nullptr) { - MS_LOGE("%ud th tensordef is null", i); - return RET_ERROR; - } - auto tensor = Tensor::CopyFromTensorDef(*tensorDef); - if (tensor == nullptr) { - return RET_ERROR; - } - allTensors.push_back(tensor); - } - - return RET_OK; -} - -int SubGraph::ConverterNodes(const flatbuffers::Vector> &nodeDefs, const Context &ctx) { - uint32_t opCount = nodeDefs.size(); - // for dfx - if (opCount > G_MAX_OP_COUNT) { - MS_LOGE("opCount(%u) bigger than maxOpCount(%u)", opCount, G_MAX_OP_COUNT); - return RET_ERROR; - } - - nodes.clear(); - - for (uint32_t i = 0; i < opCount; i++) { - auto nodeDef = nodeDefs.GetAs(i); - MS_ASSERT(nodeDef != nullptr); - auto node = std::unique_ptr(new (std::nothrow) Node(nodeDef)); - if (node == nullptr) { - MS_LOGE("new node failed"); - return RET_NULL_PTR; - } - - node->SetTensors(*nodeDef, allTensors); - - auto ret = node->InitOp(*(nodeDef->opDef()), ctx); - if (ret != RET_OK) { - MS_LOGE("node (%s) InitOP failed. ret:%d", node->ID().c_str(), ret); - return ret; - } - - auto nodeId = node->ID(); - nodes[nodeId] = node.release(); - MS_LOGD("add node succ, id:%s", nodeId.c_str()); - } - - return RET_OK; -} - -int SubGraph::ConverterEdges(const SubGraphDef &subGraphDef) { - auto opGraph = OpGraph::Build(subGraphDef); - if (opGraph == nullptr) { - MS_LOGE("opGraph Build fail"); - return RET_ERROR; - } - - for (auto nodeIter : nodes) { - auto node = opGraph->GetNode(nodeIter.first); - if (node == nullptr) { - MS_LOGI("node %s not found", nodeIter.first.c_str()); - continue; - } - for (const auto &edge : node->GetAllInEdge()) { - MS_ASSERT(nodeIter.second != nullptr); - nodeIter.second->AddInEdge(GetNode(edge)); - } - for (const auto &edge : node->GetAllOutEdge()) { - MS_ASSERT(nodeIter.second != nullptr); - nodeIter.second->AddOutEdge(GetNode(edge)); - } - } - delete opGraph; - return RET_OK; -} - -int SubGraph::InitOutputsMap() { - if (nodes.empty()) { - MS_LOGE("nodes are empty"); - return RET_ERROR; - } - for (auto node : nodes) { - NODE_ID realNodeName = node.second->ID(); - MS_ASSERT(node.second != nullptr); - if (node.second->GetAllOutEdges().empty()) { - auto nodeType = node.second->Type(); - if (nodeType == "Nhwc2Nchw" || nodeType == "Nchw2Nhwc") { - auto dependNode = *(this->GetDepends().at(this->GetNode(realNodeName)).begin()); - realNodeName = dependNode->ID(); - } - this->outputsMap.emplace( - std::pair>(realNodeName, node.second->GetOutputTensors())); - } - } - return RET_OK; -} - -std::unordered_map> SubGraph::GetDepends() { - std::unordered_map> depends; - for (auto nodeIter : nodes) { - MS_ASSERT(nodeIter.second != nullptr); - depends[nodeIter.second] = nodeIter.second->GetAllInEdges(); - } - return depends; -} - -Node *SubGraph::GetNode(const NODE_ID &id) { - auto node = nodes.find(id); - if (node == nodes.end()) { - return nullptr; - } - return node->second; -} - -std::vector SubGraph::GetInputs() { - std::vector inputTensor; - inputTensor.resize(inputIndices.size()); - std::transform(inputIndices.begin(), inputIndices.end(), inputTensor.begin(), - [this](int i) { return this->allTensors[i]; }); - - return inputTensor; -} - -std::vector SubGraph::GetOutputs() { - std::vector outputTensor; - outputTensor.resize(outputIndices.size()); - std::transform(outputIndices.begin(), outputIndices.end(), outputTensor.begin(), - [this](int i) { return this->allTensors[i]; }); - - return outputTensor; -} - -std::map> &SubGraph::GetOutputsMap() { return outputsMap; } - -void SubGraph::FreeAllTensors() { - for (auto &allTensor : allTensors) { - if (allTensor != nullptr) { - auto refcount = allTensor->RefCount(); - if (refcount != MSConst_WEIGHT_REFCOUNT) { - allTensor->DefRef(refcount); - allTensor->FreeData(); - } - } - } -} - -const std::vector *SubGraph::GetInputIndices() const { return &inputIndices; } - -const std::vector *SubGraph::GetOutputIndices() const { return &outputIndices; } - -bool SubGraph::IsInputIndex(uint32_t i) { - auto iter = std::find(inputIndices.begin(), inputIndices.end(), i); - return !(iter == inputIndices.end()); -} - -bool SubGraph::IsOutputIndex(uint32_t i) { - auto iter = std::find(outputIndices.begin(), outputIndices.end(), i); - return !(iter == outputIndices.end()); -} -} // namespace predict -} // namespace mindspore diff --git a/predict/src/graph.h b/predict/src/graph.h deleted file mode 100755 index f02c46f94ea..00000000000 --- a/predict/src/graph.h +++ /dev/null @@ -1,101 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_GRAPH_H_ -#define PREDICT_SRC_GRAPH_H_ - -#include -#include -#include -#include -#include -#include -#include "common/utils.h" -#include "common/graph_util.h" -#include "include/tensor.h" -#include "src/node.h" - -#define MSPREDICT_API __attribute__((visibility("default"))) - -namespace mindspore { -namespace predict { -class SubGraph { - public: - SubGraph(); - ~SubGraph(); - static SubGraph *CreateSubGraph(const SubGraphDef &subGraphDef, const Context &ctx); - int Build(const SubGraphDef &subGraphDef, const Context &ctx); - bool IsInputIndex(uint32_t i); - bool IsOutputIndex(uint32_t i); - - const std::vector *GetInputIndices() const; - const std::vector *GetOutputIndices() const; - - std::vector GetInputs(); - std::vector GetOutputs(); - std::map> &GetOutputsMap(); - void FreeAllTensors(); - - Node *GetNode(const NODE_ID &id); - - std::unordered_map> GetDepends(); - - private: - int ConverterIndex(const flatbuffers::Vector &srcIndex, std::vector *dstIndex); - - int ConverterAllTensor(const flatbuffers::Vector> &srcTensors); - - int ConverterNodes(const flatbuffers::Vector> &opDefs, const Context &ctx); - - int ConverterEdges(const SubGraphDef &subGraphDef); - - int InitOutputsMap(); - - protected: - std::unordered_map nodes; - std::vector inputIndices; - std::vector outputIndices; - std::vector allTensors; // weight + input + output - std::map> outputsMap; -}; - -class MSPREDICT_API Graph { - public: - Graph(); - ~Graph(); - static Graph *CreateFromBuf(const char *buf, size_t size, const Context &ctx); - - std::vector GetInputs(); - std::vector GetOutputs(); - - std::map> &GetOutputsMap(); - - void FreeAllTensors(); - - int Build(const GraphDef &def, const Context &ctx); - std::vector *Subgraphs(); - - protected: - friend class GraphExecution; - - std::vector subgraphs; - std::unordered_map> depends; // records the dependencies - std::deque readyQue; // the nodes which can execute without any dependencies -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_SRC_GRAPH_H_ diff --git a/predict/src/graph_execution.cc b/predict/src/graph_execution.cc deleted file mode 100644 index 4a71d4359ae..00000000000 --- a/predict/src/graph_execution.cc +++ /dev/null @@ -1,293 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/graph_execution.h" -#include -#include -#include - -namespace mindspore { -namespace predict { -GraphExecution::GraphExecution(const Context &ctx) : graph(nullptr), _ctx(ctx) {} -GraphExecution::GraphExecution(const Context &ctx, Graph *staticGraph) : _ctx(ctx) { - graph = staticGraph; - if (graph != nullptr) { - depends = graph->depends; - readyQue = graph->readyQue; - outputTensors = graph->GetOutputs(); - inputTensors = graph->GetInputs(); - } -} - -GraphExecution::~GraphExecution() = default; - -int GraphExecution::TransInputDataToNc4hw4(const Tensor &src, Tensor *dst) { - MS_ASSERT(dst != nullptr); - if (dst->GetData() == nullptr) { - auto ret = dst->MallocData(nullptr, MSConst_WEIGHT_REFCOUNT); - if (ret != RET_OK) { - MS_LOGE("Malloc inputTensors failed: %d", ret); - return ret; - } - } - auto ret = NchwToNc4hw4(&src, dst); - if (ret != RET_OK) { - MS_LOGE("NchwToNc4hw4 failed"); - return ret; - } - return RET_OK; -} - -int GraphExecution::SetInputTensors(const std::vector &inputs) { - size_t num = inputs.size(); - if (num != inputTensors.size()) { - MS_LOGE("input num %zu != model input num %zu", num, inputTensors.size()); - return RET_INPUT_TENSOR_ERROR; - } - - for (size_t i = 0; i < num; i++) { - MS_ASSERT(inputs[i] != nullptr); - // The input Tensor desc must be equivalent with the model tensor - if (inputs[i]->GetData() == nullptr) { - MS_LOGE("input tensor data is null!"); - return RET_INPUT_TENSOR_ERROR; - } - if (inputTensors[i] == nullptr) { - MS_LOGE("inputTensors[%zu] is nullptr", i); - return RET_ERROR; - } - - if (!inputs[i]->CompareShape(*inputTensors[i])) { - MS_LOGE("tensor shape in graph and executor are different!"); - return RET_INPUT_TENSOR_ERROR; - } - - if (inputs[i]->GetDataType() != inputTensors[i]->GetDataType()) { - MS_LOGE("tensor datatype in graph and executor are different!"); - return RET_INPUT_TENSOR_ERROR; - } - - if (inputs[i]->GetFormat() != Format_NCHW) { - MS_LOGE("input format not support. only nchw is supported now"); - return RET_INPUT_TENSOR_ERROR; - } - - if (inputs[i]->GetFormat() == inputTensors[i]->GetFormat()) { - auto data = inputs[i]->GetData(); - if (data == nullptr) { - MS_LOGE("data of input tensor is null!"); - return RET_INPUT_TENSOR_ERROR; - } - inputTensors[i]->SetData(data); - } else if (inputTensors[i]->GetFormat() == Format_NC4HW4) { - auto ret = TransInputDataToNc4hw4(*inputs[i], inputTensors[i]); - if (ret != RET_OK) { - MS_LOGE("TransInputDataToNc4hw4 failed"); - return ret; - } - } else { - MS_LOGE("graphDef inputTensors format is invalid: %d", inputTensors[i]->GetFormat()); - return RET_ERROR; - } - } - return RET_OK; -} - -int GraphExecution::MallocOutput() { - for (auto tensor : outputTensors) { - auto ret = tensor->MallocData(); - if (ret != RET_OK) { - MS_LOGE("malloc output data failed"); - return RET_ERROR; - } - } - return RET_OK; -} - -void GraphExecution::FreeTensors(std::vector *tensors) { - for (auto &tensor : (*tensors)) { - delete tensor; - } - tensors->clear(); -} - -void GraphExecution::FreeOutputMap(std::map> *map) { - MS_ASSERT(map != nullptr); - for (auto &m : *map) { - FreeTensors(&(m.second)); - } - map->clear(); -} - -int GraphExecution::CopyOutputTensors(const std::vector &refOutputs, std::vector *outputs) { - for (auto tensor : refOutputs) { - if (tensor == nullptr) { - MS_LOGE("tensor in refOutputs is nullptr"); - return RET_INPUT_TENSOR_ERROR; - } - std::unique_ptr t(new Tensor(*tensor)); - if (t == nullptr) { - MS_LOGE("new Tensor failed."); - if (outputs != nullptr) { - FreeTensors(outputs); - } - return RET_ERROR; - } - - if (tensor->GetFormat() == Format_NC4HW4) { - t->SetFormat(Format_NCHW); - auto ret = t->MallocData(); - if (ret != RET_OK) { - MS_LOGE("malloc data failed.") - FreeTensors(outputs); - return ret; - } - - ret = Nc4hw4ToNchw(tensor, t.get()); - if (ret != RET_OK) { - MS_LOGE("Nc4hw4ToNchw failed"); - return ret; - } - tensor->FreeData(); - } else { - t->SetData(tensor->GetData()); - tensor->SetData(nullptr); - } - outputs->push_back(t.release()); - } - return RET_OK; -} - -std::map> GraphExecution::GetAllOutput() { - std::map> outputs{}; - for (auto &outputNode : graph->GetOutputsMap()) { - std::vector outputNodeTensors{}; - auto ret = this->CopyOutputTensors(outputNode.second, &outputNodeTensors); - if (ret != RET_OK) { - MS_LOGE("copy output failed."); - FreeOutputMap(&outputs); - return outputs; - } - outputs.emplace(std::pair>(outputNode.first, outputNodeTensors)); - } - return outputs; -} - -std::vector GraphExecution::GetOutput(const NODE_ID &nodeName) { - std::vector outputNodeTensors{}; - auto iter = graph->GetOutputsMap().find(nodeName); - if (iter == graph->GetOutputsMap().end()) { - MS_LOGE("node name is not in output."); - return outputNodeTensors; - } - auto ret = this->CopyOutputTensors(iter->second, &outputNodeTensors); - if (ret != RET_OK) { - MS_LOGE("copy output failed."); - } - return outputNodeTensors; -} - -std::vector GraphExecution::GetInput() { - std::vector inputs{}; - for (auto refInput : graph->GetInputs()) { - if (refInput == nullptr) { - MS_LOGE("tensor from graph->GetInputs() is nullptr"); - return inputs; - } - std::unique_ptr t(new Tensor(refInput->GetDataType(), refInput->GetDims(), Format_NCHW, nullptr)); - if (t == nullptr) { - MS_LOGE("new Tensor failed.") - FreeTensors(&inputs); - return inputs; - } - inputs.push_back(t.release()); - } - return inputs; -} - -void GraphExecution::ResetInputData() { - for (auto tensor : inputTensors) { - if (tensor == nullptr) { - MS_LOGW("tensor in inputTensors is nullptr"); - continue; - } - if (tensor->GetFormat() == Format_NC4HW4) { - if (tensor->GetData() != nullptr) { - free(tensor->GetData()); - tensor->SetData(nullptr); - } - continue; - } - tensor->SetData(nullptr); - } -} - -void GraphExecution::FreeAllTensors() { graph->FreeAllTensors(); } - -int GraphExecution::Run(const std::vector &inputs) { - if (inputs.empty()) { - MS_LOGE("input is empty"); - return RET_ERROR; - } - - int ret; - - if (readyQue.empty()) { - MS_LOGE("readyQue is empty"); - return RET_ERROR; - } - - ret = SetInputTensors(inputs); - if (ret != RET_OK) { - MS_LOGE("SetInputTensors failed: %d", ret); - ResetInputData(); - return ret; - } - ret = MallocOutput(); - if (ret != RET_OK) { - MS_LOGE("MallocOutput failed: %d", ret); - ResetInputData(); - return ret; - } - - while (!readyQue.empty()) { - auto *node = readyQue.front(); - readyQue.pop_front(); - - ret = node->Run(_ctx); - if (ret != RET_OK) { - MS_LOGE("node (%s) failed to run op (%s). error code:%d", node->ID().c_str(), node->Type().c_str(), ret); - ResetInputData(); - FreeAllTensors(); - return ret; - } - - for (auto outNode : node->GetAllOutEdges()) { - auto nodeDepend = depends.find(outNode); - nodeDepend->second.erase(node); - if (nodeDepend->second.empty()) { - depends.erase(nodeDepend); - readyQue.push_back(outNode); - } - } - } - - ResetInputData(); - - return RET_OK; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/src/graph_execution.h b/predict/src/graph_execution.h deleted file mode 100644 index 022be218654..00000000000 --- a/predict/src/graph_execution.h +++ /dev/null @@ -1,70 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_GRAPH_EXECUTION_H_ -#define PREDICT_SRC_GRAPH_EXECUTION_H_ - -#include -#include -#include -#include -#include -#include -#include -#include "common/mslog.h" -#include "src/graph.h" -#include "include/errorcode.h" -#include "schema/inner/ms_generated.h" -#include "src/operator/cpu/include/op_func_comm.h" -#include "src/node.h" - -namespace mindspore { -namespace predict { -class GraphExecution { - public: - explicit GraphExecution(const Context &ctx); - GraphExecution(const Context &ctx, Graph *staticGraph); - virtual ~GraphExecution(); - - virtual std::vector GetInput(); - virtual int SetInputTensors(const std::vector &inputs); - - virtual int Run(const std::vector &inputs); - - virtual std::map> GetAllOutput(); - virtual std::vector GetOutput(const NODE_ID &nodeName); - - private: - void ResetInputData(); - int MallocOutput(); - void FreeTensors(std::vector *tensors); - int TransInputDataToNc4hw4(const Tensor &src, Tensor *dst); - int CopyOutputTensors(const std::vector &refOutputs, std::vector *outputs); - void FreeOutputMap(std::map> *map); - void FreeAllTensors(); - - protected: - Graph *graph; - const Context &_ctx; - std::vector inputTensors; - std::vector outputTensors; - std::unordered_map> depends; // records the dependencies - std::deque readyQue; // the nodes which can execute without any dependencies -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_SRC_GRAPH_EXECUTION_H_ diff --git a/predict/src/node.cc b/predict/src/node.cc deleted file mode 100644 index 7128dde209f..00000000000 --- a/predict/src/node.cc +++ /dev/null @@ -1,148 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/node.h" -#include -#include -#include -#include -#include -#include "schema/inner/ms_generated.h" -#include "common/mslog.h" -#include "common/op_utils.h" -#include "include/errorcode.h" -#include "src/op_factory.h" - -namespace mindspore { -namespace predict { -Node::Node(const NodeDef *nodeDef) - : id(std::string(nodeDef->opDef()->name()->c_str())), type(GetOpTypeName(*nodeDef)) {} - -Node::~Node() { - if (op != nullptr) { - delete op; - } -} - -NODE_ID Node::ID() { return id; } - -std::string Node::Type() { return type; } - -void Node::SetTensors(const NodeDef &nodeDef, const std::vector &allTensors) { - if (nodeDef.opDef() == nullptr) { - MS_LOGE("nodeDef is null"); - return; - } - - auto inputIndex = nodeDef.opDef()->inputIndex(); - MS_ASSERT(inputIndex != nullptr); - inputs.resize(inputIndex->size()); - std::transform(inputIndex->begin(), inputIndex->end(), inputs.begin(), [allTensors](int i) { return allTensors[i]; }); - - auto outputIndex = nodeDef.opDef()->outputIndex(); - MS_ASSERT(outputIndex != nullptr); - outputs.resize(outputIndex->size()); - std::transform(outputIndex->begin(), outputIndex->end(), outputs.begin(), - [allTensors](int i) { return allTensors[i]; }); -} - -void Node::SetDepends(const std::unordered_set &deps) { depends = deps; } - -std::unordered_set Node::GetDepends() { return depends; } - -void Node::AddInEdge(Node *node) { - if (node == nullptr) { - MS_LOGE("node is null"); - return; - } - inEdges.insert(node); -} - -void Node::AddOutEdge(Node *node) { - if (node == nullptr) { - MS_LOGE("node is null"); - return; - } - outEdges.insert(node); -} - -std::unordered_set &Node::GetAllInEdges() { return inEdges; } - -std::unordered_set &Node::GetAllOutEdges() { return outEdges; } - -std::vector &Node::GetOutputTensors() { return outputs; } -std::vector &Node::GetInputTensors() { return inputs; } - -int Node::InitOp(const OpDef &opDef, const Context &ctx) { - OpDesc dst; - dst.type = GetOpType(opDef); - dst.arch = X86_FP32; - MS_ASSERT(OpFactory::GetInstance() != nullptr); - op = OpFactory::GetInstance()->GetOp(inputs, outputs, opDef, ctx, dst); - if (op == nullptr) { - MS_LOGE("Can't find opName: %s, type: %s ", id.c_str(), type.c_str()); - return RET_ERROR; - } - return RET_OK; -} - -int Node::Run(const Context &ctx) { - MS_LOGD("%s run start", id.c_str()); - auto ret = MallocOutput(ctx); - if (ret != RET_OK) { - MS_LOGE("MallocOutput failed: %d", ret); - return ret; - } - if (op == nullptr) { - MS_LOGE("op is nullptr."); - return RET_ERROR; - } - ret = op->Execute(inputs, outputs); - if (ret != RET_OK) { - return ret; - } - FreeInput(); - return RET_OK; -} - -int Node::MallocOutput(const Context &ctx) { - size_t refCount = outEdges.size(); - for (auto tensor : outputs) { - if (tensor == nullptr) { - MS_LOGE("tensor in outputs is nullptr"); - return RET_ERROR; - } - auto ret = tensor->MallocData(ctx.allocator, refCount); - if (ret != RET_OK) { - return ret; - } - } - return RET_OK; -} - -void Node::FreeInput() { - for (auto tensor : inputs) { - if (tensor == nullptr) { - MS_LOGW("tensor in inputs is nullptr"); - return; - } - if (tensor->RefCount() != MSConst_WEIGHT_REFCOUNT) { - tensor->FreeData(); - } - } -} -} // namespace predict -} // namespace mindspore diff --git a/predict/src/node.h b/predict/src/node.h deleted file mode 100644 index eebb1b4321c..00000000000 --- a/predict/src/node.h +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_NODE_H_ -#define PREDICT_SRC_NODE_H_ - -#include -#include -#include -#include "include/session.h" -#include "src/op.h" - -namespace mindspore { -namespace predict { -using NODE_ID = std::string; - -class Node { - public: - Node() = default; - explicit Node(const NodeDef *nodeDef); - virtual ~Node(); - NODE_ID ID(); - std::string Type(); - void SetTensors(const NodeDef &nodeDef, const std::vector &allTensors); - void SetDepends(const std::unordered_set &deps); - std::unordered_set GetDepends(); - - void AddInEdge(Node *node); - void AddOutEdge(Node *node); - std::unordered_set &GetAllOutEdges(); - std::unordered_set &GetAllInEdges(); - - std::vector &GetOutputTensors(); - std::vector &GetInputTensors(); - - int InitOp(const OpDef &opDef, const Context &ctx); - int Run(const Context &ctx); - int MallocOutput(const Context &ctx); - void FreeInput(); - - protected: - friend class GraphExecution; - NODE_ID id; - std::string type; - OpBase *op{}; - std::vector inputs; - std::vector outputs; - std::unordered_set depends; - std::unordered_set inEdges; - std::unordered_set outEdges; -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_SRC_NODE_H_ diff --git a/predict/src/op.cc b/predict/src/op.cc deleted file mode 100644 index ca99b7fdff6..00000000000 --- a/predict/src/op.cc +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/op.h" - -namespace mindspore { -namespace predict { -OpBase::OpBase() : desc(nullptr) {} - -OpBase::~OpBase() = default; -} // namespace predict -} // namespace mindspore diff --git a/predict/src/op.h b/predict/src/op.h deleted file mode 100644 index a07ce21952f..00000000000 --- a/predict/src/op.h +++ /dev/null @@ -1,58 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_OP_H_ -#define PREDICT_SRC_OP_H_ - -#include -#include -#include "include/context.h" -#include "include/tensor.h" -#include "include/errorcode.h" -#include "schema/inner/ms_generated.h" - -#define MSPREDICT_API __attribute__((visibility("default"))) - -namespace mindspore { -namespace predict { -enum OP_ARCH { X86_FP32, X86_INT8, ARM_FP32, ARM_FP16, ARM_INT8, GPU }; - -struct MSPREDICT_API OpDesc { - OP_ARCH arch; - OpT type; - - bool operator<(const OpDesc &dst) const { return (arch < dst.arch) || (type < dst.type); } -}; - -class MSPREDICT_API OpBase { - public: - OpBase(); - virtual ~OpBase(); - - virtual int Execute(const std::vector &inputs, const std::vector &outputs) = 0; - virtual int Init(const std::vector &inputs, const std::vector &outputs) = 0; - - protected: - const OpDesc *desc; - std::string name; -}; - -typedef OpBase *(*OpCreator)(const std::vector &inputs, const std::vector &outputs, - const OpDef &opDef, const Context &ctx, const OpDesc &desc); -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_SRC_OP_H_ diff --git a/predict/src/op_common.h b/predict/src/op_common.h deleted file mode 100644 index c5eb69bd578..00000000000 --- a/predict/src/op_common.h +++ /dev/null @@ -1,83 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_OP_COMMON_H_ -#define PREDICT_SRC_OP_COMMON_H_ -#include - -namespace mindspore { -namespace predict { -static inline size_t AlignSize(size_t size, size_t align) { return (size + align - 1) & -align; } - -template -inline void Nchw2Nhwc(const Tsrc *in, Tdst *out, size_t h, size_t w, size_t c) { - MS_ASSERT(in != nullptr && out != nullptr); - const size_t sz = w * h; - - for (size_t cc = 0; cc < c; ++cc) { - auto pi = in + sz * cc; - - for (size_t el = 0; el < sz; ++el) { - out[cc + el * c] = (Tdst)pi[el]; - } - } -} - -template -inline void Nhwc2Nchw(const Tsrc *in, Tdst *out, size_t h, size_t w, size_t c) { - MS_ASSERT(in != nullptr && out != nullptr); - const size_t sz = w * h; - - for (auto cc = 0; cc < c; ++cc) { - auto po = out + sz * cc; - - for (size_t el = 0; el < sz; ++el) { - po[el] = (Tdst)in[cc + el * c]; - } - } -} - -template -inline void InverseQuantization(const Tsrc *srcdata, Tdst *dstdata, size_t datanum, float *parms) { - MS_ASSERT(srcdata != nullptr && dstdata != nullptr); - float scale = parms[2]; - float zeroPoint = parms[3]; - for (size_t i = 0; i < datanum; ++i) { - dstdata = (scale == 0) ? (0) : (Tdst)((srcdata[i] - zeroPoint) * scale); - } -} - -template -inline void Astype(const Tsrc *srcdata, Tdst *dstdata, size_t datanum) { - MS_ASSERT(srcdata != nullptr && dstdata != nullptr); - for (size_t i = 0; i < datanum; ++i) { - dstdata[i] = (Tdst)srcdata[i]; - } -} -#define MSMIN(x, y) ((x) < (y) ? (x) : (y)) -#define MSMAX(x, y) ((x) > (y) ? (x) : (y)) - -#define UP_DIV(x, y) (((x) + (y) - (1)) / (y)) -#define DOWN_DIV(x, y) (((x) - (y) + (1)) / (y)) -#define ROUND_UP(x, y) (((x) + (y) - (1)) / (y) * (y)) -#define ALIGN_UP4(x) ROUND_UP((x), 4) -#define ALIGN_UP8(x) ROUND_UP((x), 8) - -#define MAX_MALLOC_SIZE 100 * 1024 * 1024 -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_SRC_OP_COMMON_H_ diff --git a/predict/src/op_factory.cc b/predict/src/op_factory.cc deleted file mode 100644 index 2506db68d8e..00000000000 --- a/predict/src/op_factory.cc +++ /dev/null @@ -1,48 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/op_factory.h" - -namespace mindspore { -namespace predict { -OpFactory::OpFactory() { InitKernelManager(0, ""); } - -OpFactory::~OpFactory() = default; - -OpFactory *OpFactory::GetInstance() { - static OpFactory instance; - return &instance; -} - -OpBase *OpFactory::GetOp(const std::vector &inputs, const std::vector &outputs, const OpDef &opDef, - const Context &ctx, const OpDesc &desc) { - MS_ASSERT(GetRegistryInstance() != nullptr); - auto *reg = GetRegistryInstance()->GetInstance(MODULE_REG_NAME_OP_REGISTRY); - if (reg != nullptr) { - auto creator = reg->GetOpCreator(desc); - if (creator) { - return creator(inputs, outputs, opDef, ctx, desc); - } - } - MS_ASSERT(OpRegistry::GetInstance() != nullptr); - auto creator = OpRegistry::GetInstance()->GetOpCreator(desc); - if (creator) { - return creator(inputs, outputs, opDef, ctx, desc); - } - return nullptr; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/src/op_factory.h b/predict/src/op_factory.h deleted file mode 100644 index 583a605d8c0..00000000000 --- a/predict/src/op_factory.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_OP_FACTORY_H_ -#define PREDICT_SRC_OP_FACTORY_H_ - -#include -#include "lite/api/km_api.h" -#include "src/op.h" -#include "src/op_registry.h" - -namespace mindspore { -namespace predict { -class OpFactory { - public: - OpFactory(); - virtual ~OpFactory(); - - static OpFactory *GetInstance(); - OpBase *GetOp(const std::vector &inputs, const std::vector &outputs, const OpDef &opDef, - const Context &ctx, const OpDesc &desc); -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_SRC_OP_FACTORY_H_ diff --git a/predict/src/op_registry.cc b/predict/src/op_registry.cc deleted file mode 100644 index 14cd810d547..00000000000 --- a/predict/src/op_registry.cc +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/op_registry.h" - -namespace mindspore { -namespace predict { -OpRegistry::OpRegistry() = default; - -OpRegistry::~OpRegistry() = default; - -OpRegistry *OpRegistry::GetInstance() { - static OpRegistry instance; - return &instance; -} - -OpCreator OpRegistry::GetOpCreator(const OpDesc &desc) { - auto it = creators.find(desc); - if (it != creators.end()) { - return it->second; - } - return nullptr; -} - -void OpRegistry::RegOp(const OpDesc desc, OpCreator creator) { creators[desc] = creator; } - -void OpRegistry::RegOp(const OP_ARCH arch, const OpT type, OpCreator creator) { - OpDesc desc = {arch, type}; - creators[desc] = creator; -} - -bool OpRegistry::Merge(const std::unordered_map &newCreators) { return false; } - -const std::map &OpRegistry::GetOpCreators() { return creators; } -} // namespace predict -} // namespace mindspore diff --git a/predict/src/op_registry.h b/predict/src/op_registry.h deleted file mode 100644 index bb1d957fec9..00000000000 --- a/predict/src/op_registry.h +++ /dev/null @@ -1,71 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_OP_REGISTRY_H_ -#define PREDICT_SRC_OP_REGISTRY_H_ - -#include -#include -#include -#include "common/mslog.h" -#include "common/module_registry.h" -#include "src/op.h" - -#define MSPREDICT_API __attribute__((visibility("default"))) - -namespace mindspore { -namespace predict { -class MSPREDICT_API OpRegistry { - public: - OpRegistry(); - virtual ~OpRegistry(); - - static OpRegistry *GetInstance(); - virtual OpCreator GetOpCreator(const OpDesc &desc); - - const std::map &GetOpCreators(); - - void RegOp(OpDesc desc, OpCreator creator); - void RegOp(OP_ARCH arch, OpT type, OpCreator creator); - static bool Merge(const std::unordered_map &newCreators); - - protected: - std::map creators; -}; - -template <> -class Module : public ModuleBase { - public: - virtual OpRegistry *GetInstance() = 0; -}; - -const char MODULE_REG_NAME_OP_REGISTRY[] = "op_registry"; - -class OpRegistrar { - public: - OpRegistrar(const OpDesc &desc, OpCreator creator) { OpRegistry::GetInstance()->RegOp(desc, creator); } - - OpRegistrar(const OP_ARCH arch, const OpT type, OpCreator creator) { - MS_ASSERT(OpRegistry::GetInstance() != nullptr); - OpRegistry::GetInstance()->RegOp(arch, type, creator); - } -}; - -#define REG_OP(arch, type, opCreater) static OpRegistrar g_##arch##type##OpReg(arch, type, opCreater); -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_SRC_OP_REGISTRY_H_ diff --git a/predict/src/operator/cpu/common/op_func_comm.cc b/predict/src/operator/cpu/common/op_func_comm.cc deleted file mode 100755 index f05e224ec3d..00000000000 --- a/predict/src/operator/cpu/common/op_func_comm.cc +++ /dev/null @@ -1,422 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/operator/cpu/include/op_func_comm.h" -#include "include/errorcode.h" -#include "include/tensor.h" -#include "common/mslog.h" -#include "securec/include/securec.h" - -namespace mindspore { -namespace predict { -#ifndef MS_USE_NEON -#ifndef MS_USE_SSE - -void MSAddBias(float *srcPtr, const float *bias, size_t unitSize, size_t count) { - if (srcPtr == nullptr || bias == nullptr) { - MS_LOGW("srcPtr or bias is nullptr"); - return; - } - for (size_t stepU = 0; stepU < count; stepU++) { - float *tmpPtr = srcPtr + unitSize * CAL_STEP * stepU; - const float *biasPtr = bias + CAL_STEP * stepU; - for (size_t step = 0; step < unitSize; step++) { - float *dstPtr = tmpPtr + CAL_STEP * step; - for (int i = 0; i < CAL_STEP; i++) { - dstPtr[i] += biasPtr[i]; - } - } - } -} - -void MSAddBiasRelu(float *srcPtr, const float *bias, size_t unitSize, size_t count) { - if (srcPtr == nullptr || bias == nullptr) { - MS_LOGW("srcPtr or bias is nullptr"); - return; - } - for (size_t stepU = 0; stepU < count; stepU++) { - float *tmpPtr = srcPtr + unitSize * CAL_STEP * stepU; - const float *biasPtr = bias + CAL_STEP * stepU; - for (size_t step = 0; step < unitSize; step++) { - float *dstPtr = tmpPtr + CAL_STEP * step; - for (int i = 0; i < CAL_STEP; i++) { - dstPtr[i] += biasPtr[i]; - dstPtr[i] = (dstPtr[i] < 0) ? 0 : dstPtr[i]; - } - } - } -} - -void MSAddBiasRelu6(float *srcPtr, const float *bias, size_t unitSize, size_t count) { - if (srcPtr == nullptr || bias == nullptr) { - MS_LOGW("srcPtr or bias is nullptr"); - return; - } - for (size_t stepU = 0; stepU < count; stepU++) { - float *tmpPtr = srcPtr + unitSize * CAL_STEP * stepU; - const float *biasPtr = bias + CAL_STEP * stepU; - for (size_t step = 0; step < unitSize; step++) { - float *dstPtr = tmpPtr + CAL_STEP * step; - for (int i = 0; i < CAL_STEP; i++) { - dstPtr[i] += biasPtr[i]; - dstPtr[i] = (dstPtr[i] < 0) ? 0 : dstPtr[i]; - dstPtr[i] = (dstPtr[i] > 6.0f) ? 6.0f : dstPtr[i]; - } - } - } -} - -void MSCopyC4WithStride(const float *srcPtr, float *dstPtr, size_t srcStride, size_t dstStride, size_t count) { - if (srcPtr == nullptr || dstPtr == nullptr) { - MS_LOGW("srcPtr or dstPtr is nullptr"); - return; - } - for (size_t stepU = 0; stepU < count; stepU++) { - auto sPtr = srcPtr + stepU * srcStride; - auto dPtr = dstPtr + stepU * dstStride; - int tmpC = 0; - while (tmpC < CAL_STEP) { - dPtr[tmpC] = sPtr[tmpC]; - tmpC++; - } - } -} -#endif // MS_USE_SSE - -int MSPackC4(float *dstPtr, const float *srcPtr, size_t area, size_t depth) { - if (dstPtr == nullptr || srcPtr == nullptr) { - MS_LOGE("srcPtr or dstPtr is nullptr"); - return RET_ERROR; - } - int cur = 0; - size_t size = area * UP_DIV(depth, CAL_STEP) * CAL_STEP * sizeof(float); - auto ret = memset_s(dstPtr, size, 0, size); - if (ret != EOK) { - MS_LOGE("memset_s failed!"); - return RET_ERROR; - } - for (size_t step = 0; step < depth; step++) { - auto plane = step / CAL_STEP; - auto offset = step % CAL_STEP; - auto dstPlane = plane * area * CAL_STEP + dstPtr; - for (size_t i = 0; i < area; i++) { - dstPlane[CAL_STEP * i + offset] = srcPtr[cur++]; - } - } - return RET_OK; -} - -void MSUnpackC4(float *dstPtr, const float *srcPtr, size_t area, size_t depth) { - if (dstPtr == nullptr || srcPtr == nullptr) { - MS_LOGW("srcPtr or dstPtr is nullptr"); - return; - } - int cur = 0; - for (size_t step = 0; step < depth; step++) { - auto plane = step / CAL_STEP; - auto offset = step % CAL_STEP; - auto srcPlane = plane * area * CAL_STEP + srcPtr; - for (size_t i = 0; i < area; i++) { - dstPtr[cur++] = srcPlane[CAL_STEP * i + offset]; - } - } -} - -void MSUInt8ToInt16WithOffsetC4Common(int16_t *dstPtr, const uint8_t *srcPtr, size_t zeroPoint, size_t sizeQuad, - size_t dstStride, size_t srcStride) { - if (dstPtr == nullptr || srcPtr == nullptr) { - MS_LOGW("srcPtr or dstPtr is nullptr"); - return; - } - for (size_t step = 0; step < sizeQuad; step++) { - auto dstZ = dstPtr + (dstStride / sizeof(int16_t)) * step; - auto srcZ = srcPtr + (srcStride / sizeof(uint8_t)) * step; - for (int i = 0; i < CAL_STEP; i++) { - dstZ[i] = (int16_t)((int32_t)srcZ[i] - (int32_t)zeroPoint); - } - } -} - -void MSUInt8ToInt16WithOffsetC4Fast(int16_t *colAddr, const uint8_t *srcStart, size_t zeroPoint, size_t sizeQuad, - size_t depthQuad, size_t dstZStep, size_t srcZStep) { - if (colAddr == nullptr || srcStart == nullptr) { - MS_LOGW("colAddr or srcStart is nullptr"); - return; - } - for (size_t step = 0; step < depthQuad; step++) { - auto dstZ = colAddr + (dstZStep / sizeof(int16_t)) * step; - auto srcZ = srcStart + (srcZStep / sizeof(uint8_t)) * step; - MSUInt8ToInt16WithOffsetC4Common(dstZ, srcZ, zeroPoint, sizeQuad, CAL_STEP * sizeof(int16_t), - CAL_STEP * sizeof(uint8_t)); - } -} -#endif - -void MSPackC4Uint8(uint8_t *dstPtr, const uint8_t *srcPtr, size_t area, size_t depth) { - if (dstPtr == nullptr || srcPtr == nullptr) { - MS_LOGW("srcPtr or dstPtr is nullptr"); - return; - } - int cur = 0; - size_t size = area * UP_DIV(depth, CAL_STEP) * CAL_STEP * sizeof(uint8_t); - auto ret = memset_s(dstPtr, size, 0, size); - if (ret != EOK) { - MS_LOGE("memset_s failed!"); - return; - } - for (size_t step = 0; step < depth; step++) { - auto plane = step / CAL_STEP; - auto offset = step % CAL_STEP; - auto dstPlane = plane * area * CAL_STEP + dstPtr; - for (size_t x = 0; x < area; ++x) { - dstPlane[CAL_STEP * x + offset] = srcPtr[cur++]; - } - } -} - -void MSUnpackC4Uint8(uint8_t *dstPtr, const uint8_t *srcPtr, size_t area, size_t depth) { - if (dstPtr == nullptr || srcPtr == nullptr) { - MS_LOGW("srcPtr or dstPtr is nullptr"); - return; - } - int cur = 0; - for (size_t step = 0; step < depth; step++) { - auto srcPlane = (step / CAL_STEP) * area * CAL_STEP + srcPtr; - for (size_t i = 0; i < area; i++) { - dstPtr[cur++] = srcPlane[CAL_STEP * i + (step % CAL_STEP)]; - } - } -} - -#ifdef MS_USE_NEON -static void MSTensorConvertNCHWToNC4HW4Depth(float *dst, const float *src, size_t area, size_t depth) { - if (dstPtr == nullptr || srcPtr == nullptr) { - MS_LOGW("srcPtr or dstPtr is nullptr"); - return; - } - if (1 == depth) { - auto zeroValue = vmovq_n_f32(0.0f); - int areaC4 = static_cast(area / CAL_STEP); - int remain = areaC4 * CAL_STEP; - for (int i = 0; i < areaC4; ++i) { - auto srcCur = src + CAL_STEP * i; - auto dstCur = dst + CAL_STEP * CAL_STEP * i; - auto srcValue = vld1q_f32(srcCur); - float32x4x4_t dstValue; - dstValue.val[0] = srcValue; - dstValue.val[1] = zeroValue; - dstValue.val[2] = zeroValue; - dstValue.val[3] = zeroValue; - vst4q_f32(dstCur, dstValue); - } - for (int i = remain; i < area; ++i) { - dst[CAL_STEP * i + 0] = src[i]; - dst[CAL_STEP * i + 1] = 0.0f; - dst[CAL_STEP * i + 2] = 0.0f; - dst[CAL_STEP * i + 3] = 0.0f; - } - } else if (3 == depth) { - auto zeroValue = vmovq_n_f32(0.0f); - int areaC4 = static_cast(area / CAL_STEP); - int remain = areaC4 * CAL_STEP; - for (int i = 0; i < areaC4; ++i) { - auto srcCur = src + 12 * i; - auto dstCur = dst + 16 * i; - auto srcValue = vld3q_f32(srcCur); - float32x4x4_t dstValue; - dstValue.val[0] = srcValue.val[0]; - dstValue.val[1] = srcValue.val[1]; - dstValue.val[2] = srcValue.val[2]; - dstValue.val[3] = zeroValue; - vst4q_f32(dstCur, dstValue); - } - for (int i = remain; i < area; ++i) { - dst[CAL_STEP * i + 0] = src[3 * i + 0]; - dst[CAL_STEP * i + 1] = src[3 * i + 1]; - dst[CAL_STEP * i + 2] = src[3 * i + 2]; - dst[CAL_STEP * i + 3] = 0.0f; - } - } -} -#endif - -void MSTensorConvertNHWCToNC4HW4(float *dst, const float *src, size_t area, size_t depth) { - if (dst == nullptr || src == nullptr) { - MS_LOGW("srcPtr or dstPtr is nullptr"); - return; - } -#ifdef MS_USE_NEON - MSTensorConvertNCHWToNC4HW4Depth(dst, src, area, depth); - return; -#endif - int c = static_cast(depth); - int cDiv4 = c / CAL_STEP; - int cMod4 = c % CAL_STEP; - int cAlign = cDiv4 * CAL_STEP; - for (int hi = 0; hi < area; ++hi) { - auto srcHeight = src + hi * c; - auto dstHeight = dst + hi * CAL_STEP; - for (int ci = 0; ci < cDiv4; ++ci) { -#ifdef MS_USE_NEON - vst1q_f32(dstHeight + CAL_STEP * ci * area, vld1q_f32(srcHeight + CAL_STEP * ci)); -#else - for (int i = 0; i < CAL_STEP; ++i) { - dstHeight[ci * area * CAL_STEP + i] = srcHeight[CAL_STEP * ci + i]; - } -#endif - } - } - - if (cMod4 == 0) { - MS_LOGW("depth should be multiple of four"); - return; - } - - auto srcAlign = src + cAlign; - auto dstAlign = dst + area * cAlign; - -#ifdef MS_USE_NEON - auto zeroVector = vdupq_n_f32(0.0f); -#endif - - for (int hi = 0; hi < area; ++hi) { - auto srcHeight = srcAlign + hi * c; - auto dstHeight = dstAlign + hi * CAL_STEP; -#ifdef MS_USE_NEON - vst1q_f32(dstHeight, zeroVector); -#else - for (int i = 0; i < CAL_STEP; ++i) { - dstHeight[i] = 0; - } -#endif - for (int ci = 0; ci < cMod4; ++ci) { - dstHeight[ci] = srcHeight[ci]; - } - } -} - -void MSTensorConvertNC4HW4ToNHWC(float *dst, const float *src, size_t area, size_t depth) { - if (dst == nullptr || src == nullptr) { - MS_LOGW("srcPtr or dstPtr is nullptr"); - return; - } - int c = static_cast(depth); - int cDiv4 = c / CAL_STEP; - int cMod4 = c % CAL_STEP; - int cAlign = cDiv4 * CAL_STEP; - for (int hi = 0; hi < area; ++hi) { - auto srcHeight = src + hi * CAL_STEP; - auto dstHeight = dst + hi * c; - for (int ci = 0; ci < cDiv4; ++ci) { -#ifdef MS_USE_NEON - vst1q_f32(dstHeight + CAL_STEP * ci, vld1q_f32(srcHeight + CAL_STEP * ci * area)); -#else - for (int i = 0; i < CAL_STEP; ++i) { - dstHeight[ci * CAL_STEP + i] = srcHeight[CAL_STEP * ci * area + i]; - } -#endif - } - } - - if (cMod4 == 0) { - MS_LOGW("depth should be multiple of four"); - return; - } - - auto srcAlign = src + area * cAlign; - auto dstAlign = dst + cAlign; - - for (int hi = 0; hi < area; ++hi) { - auto srcHeight = srcAlign + hi * CAL_STEP; - auto dstHeight = dstAlign + hi * c; - - for (int ci = 0; ci < cMod4; ++ci) { - dstHeight[ci] = srcHeight[ci]; - } - } -} - -int NchwToNc4hw4(const Tensor *input, Tensor *output) { - if (input == nullptr || output == nullptr) { - MS_LOGE("input or output is nullptr"); - return RET_ERROR; - } - int batch = static_cast(input->Batch()); - int channel = static_cast(input->Channel()); - MS_ASSERT(batch > 0); - MS_ASSERT(channel > 0); - int area = static_cast(input->Width()) * static_cast(input->Height()); - int inputStride = input->GetElementSize() / batch; - int outputStride = output->GetElementSize() / batch; - DataType dt = input->GetDataType(); - - MS_ASSERT(input->GetData()); - MS_ASSERT(output->GetData()); - - if (dt == DataType_DT_FLOAT) { - for (int i = 0; i < batch; ++i) { - auto ret = MSPackC4(reinterpret_cast(output->GetData()) + outputStride * i, - (const float *)input->GetData() + inputStride * i, area, channel); - if (ret != RET_OK) { - MS_LOGE("MSPackC4 failed: %d", ret); - return RET_ERROR; - } - } - } else if (dt == DataType_DT_UINT8) { - for (int i = 0; i < batch; ++i) { - MSPackC4Uint8(reinterpret_cast(output->GetData()) + outputStride * i, - (const uint8_t *)input->GetData() + inputStride * i, area, channel); - } - } else { - MS_LOGE("Unsupported dataType: %d", dt); - return RET_ERROR; - } - return RET_OK; -} - -int Nc4hw4ToNchw(const Tensor *input, Tensor *output) { - if (input == nullptr || output == nullptr) { - MS_LOGE("input tensor or output tensor is nullptr"); - return RET_ERROR; - } - - int batch = static_cast(input->Batch()); - int channel = static_cast(input->Channel()); - MS_ASSERT(batch > 0); - MS_ASSERT(channel > 0); - int area = static_cast(input->Width()) * static_cast(input->Height()); - int inputStride = input->GetElementSize() / batch; - int outputStride = output->GetElementSize() / batch; - DataType dt = input->GetDataType(); - if (dt == DataType_DT_FLOAT) { - for (int i = 0; i < batch; ++i) { - MSUnpackC4(reinterpret_cast(output->GetData()) + outputStride * i, - (const float *)input->GetData() + inputStride * i, area, channel); - } - } else if (dt == DataType_DT_UINT8) { - for (int i = 0; i < batch; ++i) { - MSUnpackC4Uint8(reinterpret_cast(output->GetData()) + outputStride * i, - (const uint8_t *)input->GetData() + inputStride * i, area, channel); - } - } else { - MS_LOGE("Unsupported dataType: %d", dt); - return RET_ERROR; - } - - return RET_OK; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/src/operator/cpu/include/op_func_comm.h b/predict/src/operator/cpu/include/op_func_comm.h deleted file mode 100644 index 884803d6693..00000000000 --- a/predict/src/operator/cpu/include/op_func_comm.h +++ /dev/null @@ -1,62 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_OPERATOR_CPU_INCLUDE_OP_FUNC_COMM_H_ -#define PREDICT_SRC_OPERATOR_CPU_INCLUDE_OP_FUNC_COMM_H_ - -#include -#include -#include -#include -#include -#include "src/op_common.h" -#include "include/tensor.h" - -#ifdef MS_USE_NEON -#include -#endif // MS_USE_NEON - -namespace mindspore { -namespace predict { -#ifdef __cplusplus -extern "C" { -#endif -#define CAL_STEP 4 -void MSAddBias(float *dst, const float *bias, size_t planeNumber, size_t biasNumber); -void MSAddBiasRelu(float *dst, const float *bias, size_t planeNumber, size_t biasNumber); -void MSAddBiasRelu6(float *dst, const float *bias, size_t planeNumber, size_t biasNumber); -void MSPackC4Uint8(uint8_t *dst, const uint8_t *src, size_t area, size_t depth); -void MSUnpackC4(float *dst, const float *src, size_t area, size_t depth); -void MSUnpackC4Uint8(uint8_t *dst, const uint8_t *src, size_t area, size_t depth); -void MSTensorConvertNHWCToNC4HW4(float *dst, const float *src, size_t area, size_t depth); -void MSTensorConvertNC4HW4ToNHWC(float *dst, const float *src, size_t area, size_t depth); -void MSUnpackC4(float *dst, const float *src, size_t area, size_t depth); -void MSCopyC4WithStride(const float *source, float *dest, size_t srcStride, size_t dstStride, size_t count); -void MSUInt8ToInt16WithOffsetC4Common(int16_t *dst, const uint8_t *src, size_t zeroPoint, size_t sizeQuad, - size_t dstStride, size_t srcStride); -void MSUInt8ToInt16WithOffsetC4Fast(int16_t *dst, const uint8_t *src, size_t zeroPoint, size_t sizeQuad, - size_t depthQuad, size_t dstZStep, size_t srcZStep); - -int MSPackC4(float *dst, const float *src, size_t area, size_t depth); -int NchwToNc4hw4(const Tensor *input, Tensor *output); -int Nc4hw4ToNchw(const Tensor *input, Tensor *output); -#ifdef __cplusplus -} -#endif -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_SRC_OPERATOR_CPU_INCLUDE_OP_FUNC_COMM_H_ diff --git a/predict/src/runtime/allocator.cc b/predict/src/runtime/allocator.cc deleted file mode 100644 index cb94af5df9c..00000000000 --- a/predict/src/runtime/allocator.cc +++ /dev/null @@ -1,135 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/runtime/allocator.h" -#include "common/module_registry.h" -#include "src/op_common.h" - -namespace mindspore { -namespace predict { -std::shared_ptr Allocator::Create() { - auto alloc = GetRegistryInstance()->Create(MODULE_REG_NAME_ALLOCATOR); - if (alloc != nullptr) { - return alloc; - } - - // default allocator - return std::shared_ptr(new DefaultAllocator()); -} - -DefaultAllocator::DefaultAllocator() = default; - -DefaultAllocator::~DefaultAllocator() { Clear(); } - -void DefaultAllocator::SetContext(const AllocatorContext &ctx) { - lockFlag = ctx.lockFlag; - shiftFactor = ctx.shiftFactor; -} - -void DefaultAllocator::Lock() { - if (lockFlag) { - lock.lock(); - } -} - -void DefaultAllocator::UnLock() { - if (lockFlag) { - lock.unlock(); - } -} - -void *DefaultAllocator::Malloc(size_t size) { - if (size > MAX_MALLOC_SIZE) { - return nullptr; - } - Lock(); - auto it = freeList.begin(); - for (; it != freeList.end(); it++) { - auto membuf = *it; - - if ((membuf->size >= size) && (membuf->size < (size << shiftFactor))) { - freeList.erase(it); - allocatedList.push_back(membuf); - UnLock(); - return membuf->buf; - } - } - std::unique_ptr membuf(reinterpret_cast(malloc(sizeof(MemBuf) + size))); - if (membuf == nullptr) { - UnLock(); - return nullptr; - } - membuf->size = size; - membuf->buf = reinterpret_cast(membuf.get()) + sizeof(MemBuf); - auto bufPtr = membuf->buf; - allocatedList.push_back(membuf.release()); - UnLock(); - return bufPtr; -} - -void DefaultAllocator::Free(void *buf) { - if (buf == nullptr) { - return; - } - Lock(); - auto it = allocatedList.begin(); - for (; it != allocatedList.end(); it++) { - auto membuf = *it; - - if (membuf->buf == buf) { - allocatedList.erase(it); - freeList.push_back(membuf); - UnLock(); - return; - } - } - UnLock(); - free(buf); -} - -size_t DefaultAllocator::GetTotalSize() { - Lock(); - size_t totalSize = 0; - auto it = allocatedList.begin(); - for (; it != allocatedList.end(); it++) { - auto membuf = *it; - totalSize += membuf->size; - } - it = freeList.begin(); - for (; it != freeList.end(); it++) { - auto membuf = *it; - totalSize += membuf->size; - } - UnLock(); - return totalSize; -} - -void DefaultAllocator::Clear() { - Lock(); - auto it = allocatedList.begin(); - for (; it != allocatedList.end(); it++) { - free(*it); - } - allocatedList.clear(); - it = freeList.begin(); - for (; it != freeList.end(); it++) { - free(*it); - } - freeList.clear(); - UnLock(); -} -} // namespace predict -} // namespace mindspore diff --git a/predict/src/runtime/allocator.h b/predict/src/runtime/allocator.h deleted file mode 100644 index a9d72fbc9d7..00000000000 --- a/predict/src/runtime/allocator.h +++ /dev/null @@ -1,81 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_RUNTIME_ALLOCATOR_H_ -#define PREDICT_SRC_RUNTIME_ALLOCATOR_H_ - -#include -#include -#include -#include -#include "common/module_registry.h" - -namespace mindspore { -namespace predict { -struct AllocatorContext { - int shiftFactor; - bool lockFlag; -}; - -class Allocator { - public: - Allocator() : name("default") {} - virtual ~Allocator() {} - virtual void *Malloc(size_t size) = 0; - virtual void Free(void *ptr) = 0; - virtual void SetContext(const AllocatorContext &ctx) {} - virtual size_t GetTotalSize() { return 0; } - virtual void Clear() {} - static std::shared_ptr Create(); - std::string name; -}; - -class DefaultAllocator : public Allocator { - public: - DefaultAllocator(); - ~DefaultAllocator() override; - void SetContext(const AllocatorContext &ctx) override; - void *Malloc(size_t size) override; - void Free(void *ptr) override; - size_t GetTotalSize() override; - void Clear() override; - - private: - void Lock(); - void UnLock(); - struct MemBuf { - size_t size; - void *buf; - }; - - std::mutex lock; - std::vector allocatedList; - std::vector freeList; - int shiftFactor = 0; - bool lockFlag = false; -}; - -// these declaration are for module integration, refer to sample_allocator -const char MODULE_REG_NAME_ALLOCATOR[] = "allocator"; - -template <> class Module : public ModuleBase { - public: - virtual std::shared_ptr Create() = 0; -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_SRC_RUNTIME_ALLOCATOR_H_ diff --git a/predict/src/runtime/runtime_api.cc b/predict/src/runtime/runtime_api.cc deleted file mode 100644 index 2091c808fff..00000000000 --- a/predict/src/runtime/runtime_api.cc +++ /dev/null @@ -1,79 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/runtime/runtime_api.h" -#include -#include -#include "src/runtime/workspace_pool.h" -#include "src/runtime/thread_pool.h" -#include "common/mslog.h" - -static std::mutex gWorkspaceMutex; -#ifdef __cplusplus -extern "C" { -#endif -void LiteAPISetLastError(const char *msg) { MS_LOGE("The lite api set last error is %s.", msg); } - -void *LiteBackendAllocWorkspace(int deviceType, int deviceId, uint64_t size, int dtypeCode, int dtypeBits) { - std::lock_guard lock(gWorkspaceMutex); - auto p = mindspore::predict::WorkspacePool::GetInstance(); - if (p == nullptr) { - MS_LOGE("get ThreadPool install failed"); - return nullptr; - } - return p->AllocWorkSpaceMem(size); -} - -int LiteBackendFreeWorkspace(int deviceType, int deviceId, void *ptr) { - std::lock_guard lock(gWorkspaceMutex); - auto p = mindspore::predict::WorkspacePool::GetInstance(); - if (p == nullptr) { - MS_LOGE("get ThreadPool install failed"); - return -1; - } - p->FreeWorkSpaceMem(ptr); - return 0; -} - -void ConfigThreadPool(int mode, int nthreads) { - auto p = mindspore::predict::ThreadPool::GetInstance(); - if (p == nullptr) { - MS_LOGE("get ThreadPool install failed"); - return; - } - p->ConfigThreadPool(mode, nthreads); -} - -int LiteBackendParallelLaunch(FTVMParallelLambda flambda, void *cdata, int num_task) { - auto p = mindspore::predict::ThreadPool::GetInstance(); - if (p == nullptr) { - MS_LOGE("get ThreadPool install failed"); - return -1; - } - if (!p->LaunchThreadPoolTask()) { - MS_LOGE("get ThreadPool or thread bind failed"); - return -1; - } - if (!p->AddTask(flambda, cdata, num_task)) { - MS_LOGE("AddTask failed"); - return -1; - } - return 0; -} - -#ifdef __cplusplus -} -#endif diff --git a/predict/src/runtime/runtime_api.h b/predict/src/runtime/runtime_api.h deleted file mode 100644 index 01aa782cf8a..00000000000 --- a/predict/src/runtime/runtime_api.h +++ /dev/null @@ -1,39 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#ifndef PREDICT_SRC_RUNTIME_RUNTIME_API_H_ -#define PREDICT_SRC_RUNTIME_RUNTIME_API_H_ -#include - -#ifdef __cplusplus -extern "C" { -#endif - -typedef struct { - void *sync_handle; - int32_t num_task; -} TVMParallelGroupEnv; -typedef int (*FTVMParallelLambda)(int task_id, TVMParallelGroupEnv *penv, void *cdata); -void LiteAPISetLastError(const char *msg); -void *LiteBackendAllocWorkspace(int deviceType, int deviceId, uint64_t size, int dtypeCode, int dtypeBits); -int LiteBackendFreeWorkspace(int deviceType, int deviceId, void *ptr); -void ConfigThreadPool(int mode, int nthreads); -int LiteBackendParallelLaunch(FTVMParallelLambda flambda, void *cdata, int num_task); -int LiteBackendRegisterSystemLibSymbol(const char *name, void *ptr); - -#ifdef __cplusplus -} -#endif -#endif // PREDICT_SRC_RUNTIME_RUNTIME_API_H_ diff --git a/predict/src/runtime/thread_pool.cc b/predict/src/runtime/thread_pool.cc deleted file mode 100644 index 6018927a182..00000000000 --- a/predict/src/runtime/thread_pool.cc +++ /dev/null @@ -1,447 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "src/runtime/thread_pool.h" -#include -#include "common/mslog.h" - -namespace mindspore { -namespace predict { -static constexpr int kThreadPoolMaxThreads = 8; -static const int kCoreNumThr = 4; -static const int kMidCoreNum = 2; -static const int kBigCoreNum = 2; -bool LiteQueue::Enqueue(const ThreadPoolTask &task) { - const int tailIndex = tail.load(std::memory_order_relaxed); - // queue full - auto next = (tailIndex + 1) % kSingleThreadMaxTask; - if (next == head.load(std::memory_order_acquire)) { - return false; - } - buffer[tailIndex] = task; - tail.store(next, std::memory_order_release); - taskSize.fetch_add(1); - return true; -} - -bool LiteQueue::Dequeue(ThreadPoolTask *out) { - if (out == nullptr) { - MS_LOGE("ThreadPoolTask is nullptr"); - return false; - } - if (taskSize.load() == 0) { - return false; - } - // queue empty - const int headIndex = head.load(std::memory_order_relaxed); - if (headIndex == tail.load(std::memory_order_acquire)) { - return false; - } - *out = buffer[headIndex]; - head.store((headIndex + 1) % kSingleThreadMaxTask, std::memory_order_release); - return true; -} - -bool LiteThreadBind::Bind(int numThreads, int mode) { - InitSortedCpuId(); - if (numThreads > static_cast(sortedCpuIds.size())) { - MS_LOGE("thread num %d is larger than cores %lu in the system", numThreads, sortedCpuIds.size()); - return false; - } - threadNums = numThreads + 1; - bindModel = static_cast(mode); - if (bindModel == NO_BIND) { - if (!BindAllThread(false)) { - MS_LOGE("unbind %d threads failed", threadNums); - return false; - } - MS_LOGD("unbind %d threads successful", threadNums); - } else { - if (!BindAllThread(true)) { - MS_LOGE("bind %d threads failed", threadNums); - return false; - } - MS_LOGD("bind %d threads successful", threadNums); - } - return true; -} - -void LiteThreadBind::InitSortedCpuId() { - int numCores = static_cast(std::thread::hardware_concurrency()); - if (numCores < kCoreNumThr) { - bigCore = 0; - midCore = numCores; - } else { - bigCore = kBigCoreNum; - midCore = kMidCoreNum; - } - if (numCores > kCoreNumThr) { - numCores = bigCore + midCore; - } - sortedCpuIds.resize(numCores); - sortedCpuIds.clear(); - for (int i = numCores - 1; i >= 0; --i) { - sortedCpuIds.emplace_back(i); - } -} - -bool LiteThreadBind::BindAllThread(bool bindFlag) { - if (threadNums <= 0) { - MS_LOGE("no thread pool find, current threadNums %d", threadNums); - return false; - } - if (!BindThreads(bindFlag)) { - MS_LOGE("bind threads failed"); - return false; - } - return true; -} - -bool LiteThreadBind::BindMasterThread(bool bindFlag, int mode) { - std::vector cpu; - cpu.resize(sortedCpuIds.size()); - cpu.clear(); - if (bindFlag) { - int cpuIndex = (mode == MID_CORE) ? (threadNums - 1) : 0; - auto materCpuId = sortedCpuIds.at(cpuIndex); - cpu.emplace_back(materCpuId); - } else { - // unbind master - cpu.assign(sortedCpuIds.begin(), sortedCpuIds.end()); - } - cpu_set_t cpuSet; - CPU_ZERO(&cpuSet); - for (auto coreId : cpu) { - CPU_SET(coreId, &cpuSet); - } - if (!SetCPUBind(pthread_self(), cpuSet)) { - MS_LOGE("do master bind failed. mode: %d", mode); - return false; - } - return true; -} - -bool LiteThreadBind::BindThreads(bool bindFlag) { - if (bindFlag) { - if (bindModel != NO_BIND) { - size_t bindNums = std::min(sortedCpuIds.size(), threadIdList.size()); - size_t coreIndex; - cpu_set_t cpuSet; - for (size_t i = 0; i < bindNums; ++i) { - if (bindModel == MID_CORE) { - coreIndex = sortedCpuIds.size() - i - 1; - } else { - coreIndex = i; - } - CPU_ZERO(&cpuSet); - CPU_SET(sortedCpuIds[coreIndex], &cpuSet); - if (!threadIdList[i].second) { - MS_LOGD("threadIdList[%lu]=%lu, sortedCpuIds[%lu]=%d", i, threadIdList[i].first, coreIndex, - sortedCpuIds[coreIndex]); - if (!SetCPUBind(threadIdList[i].first, cpuSet)) { - MS_LOGE("do SetCPUBind failed"); - return false; - } - } - threadIdList[i].second = true; - } - } - } else { - // unbind - size_t bindNums = std::min(sortedCpuIds.size(), threadIdList.size()); - cpu_set_t cpuSet; - CPU_ZERO(&cpuSet); - for (auto coreId : sortedCpuIds) { - CPU_SET(coreId, &cpuSet); - } - for (size_t i = 0; i < bindNums; ++i) { - if (!SetCPUBind(threadIdList[i].first, cpuSet)) { - MS_LOGE("do SetCPUBind failed"); - return false; - } - threadIdList[i].second = false; - } - } - return true; -} - -bool LiteThreadBind::SetCPUBind(pthread_t threadId, const cpu_set_t &cpuSet) { -#if defined(__ANDROID__) -#if __ANDROID_API__ >= 21 - int ret = sched_setaffinity(pthread_gettid_np(threadId), sizeof(cpu_set_t), &cpuSet); - if (ret != 0) { - MS_LOGE("bind thread %ld to cpu failed.ERROR %d", threadId, ret); - } -#endif -#else - int ret = pthread_setaffinity_np(threadId, sizeof(cpu_set_t), &cpuSet); - if (ret != 0) { - MS_LOGE("bind thread %ld to cpu failed.ERROR %d", threadId, ret); - return false; - } -#endif - return true; -} - -LiteThreadPool::LiteThreadPool(int numThreads) { - queueList.resize(kThreadPoolMaxThreads); - queueList.clear(); - AddNewThread(numThreads); -} - -void LiteThreadPool::AddNewThread(int newNums) { - for (int i = curThreadNums, j = 0; j < newNums; ++j, ++i) { - queueList.push_back(std::unique_ptr(new LiteQueue())); - threadList.emplace_back([this, i]() { - ThreadPoolTask task; - while (!destroy) { - while (running != 0) { - MS_LOGD("i = %d, thread id = %lu, taskSize = %d", i, pthread_self(), queueList[i]->taskSize.load()); - while (queueList[i]->taskSize.load() > 0 && queueList[i]->Dequeue(&task)) { - auto ret = task.first(task.second.taskId, task.second.tvmParam, task.second.cdata); - if (ret != 0) { - errorInfo.emplace_back(std::make_pair(task.second.taskId, std::make_pair(false, ret))); - } - queueList[i]->taskSize.fetch_sub(1); - } - std::this_thread::yield(); - } - std::unique_lock queueLock(tMutex); - queueReady.wait(queueLock, [this] { return destroy || running != 0; }); - } - }); - } - MS_LOGI("%d new thread create", newNums); - curThreadNums += newNums; -} - -bool LiteThreadPool::DistributeTask(ThreadPoolTask task, int numTask) { - // wake up - errorInfo.clear(); - if (!AddRunReference()) { - MS_LOGE("add reference failed"); - return false; - } - bool kSuccFlag; - for (int i = 1; i < numTask; ++i) { - task.second.taskId = i; - do { - kSuccFlag = false; - for (auto &queue : queueList) { - MS_ASSERT(queue != nullptr); - if (queue->Enqueue(task)) { - kSuccFlag = true; - break; - } - } - std::this_thread::yield(); - } while (!kSuccFlag); - } - MS_LOGI("add %d task successful", numTask); - // master thread - int ret = task.first(0, task.second.tvmParam, task.second.cdata); - if (ret != 0) { - errorInfo.emplace_back(std::make_pair(0, std::make_pair(false, ret))); - } - kSuccFlag = false; - while (!kSuccFlag) { - kSuccFlag = true; - for (auto iter = queueList.begin(); iter != queueList.end(); ++iter) { - if ((*iter)->taskSize.load() != 0) { - kSuccFlag = false; - break; - } - } - std::this_thread::yield(); - } - // hibernate - if (!SubRunReference()) { - MS_LOGE("sub reference failed"); - return false; - } - MS_LOGI("finish %d task successful", numTask); - return CheckResult(); -} - -bool LiteThreadPool::AddRunReference() { - running.fetch_add(1); - std::lock_guard queueLock(tMutex); - queueReady.notify_all(); - return true; -} - -bool LiteThreadPool::SubRunReference() { - running.fetch_sub(1); - return true; -} - -bool LiteThreadPool::CheckResult() { - bool kSuccFlag = true; - for (auto result : errorInfo) { - if (result.second.first) { - MS_LOGE("task %d failed, error code is %d", result.first, result.second.second); - kSuccFlag = false; - } - } - return kSuccFlag; -} - -int ThreadPool::GetThreadNum(int numThreads) { - if (numThreads <= 0 || numThreads > kThreadPoolMaxThreads) { - MS_LOGE("numThreads %d, must be greater than 0 or less than or equal to %d", numThreads, kThreadPoolMaxThreads); - return -1; - } else { - if (numThreads > totalThreadNum) { - return (numThreads - totalThreadNum); - } else { - MS_LOGD("%d threads have been already created", numThreads); - return 0; - } - } -} - -void ThreadPool::GetThreadIdList() { - if (gThreadPool != nullptr) { - for (int i = 0; i < totalThreadNum; ++i) { - bool kSuccFlag = false; - pthread_t threadHandle; - do { - kSuccFlag = false; - threadHandle = gThreadPool->threadList[i].native_handle(); - if (threadHandle != 0) { - kSuccFlag = true; - } - std::this_thread::yield(); - } while (!kSuccFlag); - - auto iter = std::find_if(std::begin(gThreadBind->threadIdList), std::end(gThreadBind->threadIdList), - [threadHandle](std::pair id) { return id.first == threadHandle; }); - if (iter == std::end(gThreadBind->threadIdList)) { - gThreadBind->threadIdList.emplace_back(std::make_pair(threadHandle, false)); - } - } - } - MS_ASSERT(gThreadBind != nullptr); - gThreadBind->threadIdList.emplace_back(std::make_pair(pthread_self(), false)); -} - -bool ThreadPool::SetThreadCpulBind(int mode) { - if (totalThreadNum <= 0) { - MS_LOGE("no threads need to be bind, totalThreadNum : %d", totalThreadNum); - return false; - } - std::lock_guard bMutex(gPoolMutex); - if (gThreadBind == nullptr) { - gThreadBind = std::unique_ptr(new (std::nothrow) LiteThreadBind()); - if (gThreadBind == nullptr) { - MS_LOGE("new LiteThreadBind failed"); - return false; - } - gThreadBind->threadIdList.resize(kThreadPoolMaxThreads + 1); - gThreadBind->threadIdList.clear(); - } - GetThreadIdList(); - - if (!gThreadBind->Bind(totalThreadNum, mode)) { - MS_LOGE("BindCore failed"); - return false; - } - return true; -} - -bool ThreadPool::SetThreadPool(int numThreads) { - std::lock_guard Lock(gPoolMutex); - int realNums = GetThreadNum(numThreads); - if (realNums < -1) { - return false; - } - if (realNums == 0) { - return true; - } - if (gThreadPool == nullptr) { - gThreadPool = std::unique_ptr(new (std::nothrow) LiteThreadPool(realNums)); - if (gThreadPool == nullptr) { - MS_LOGE("%d threads create failed", realNums); - return false; - } - } else { - gThreadPool->AddNewThread(realNums); - } - MS_LOGD("%d threads create successful", realNums); - return true; -} - -ThreadPool *ThreadPool::GetInstance() { - static ThreadPool instance; - return &instance; -} - -void ThreadPool::ConfigThreadPool(int mode, int numThreads) { - bindMode = mode; - totalThreadNum = numThreads; -} - -bool ThreadPool::LaunchThreadPoolTask() { - if (gThreadPool == nullptr) { - if (!SetThreadPool(totalThreadNum)) { - MS_LOGE("create %d threads failed", totalThreadNum); - return false; - } - } - - if (gThreadBind == nullptr) { - if (!SetThreadCpulBind(bindMode)) { - MS_LOGE("create bind mode %d failed", bindMode); - return false; - } - } - return true; -} - -bool ThreadPool::AddTask(const WorkFun &worker, void *cdata, int numTask) { - if (numTask <= 0) { - numTask = totalThreadNum; - } - // single task, run master thread - if (numTask <= 1) { - TvmEnv env{}; - env.num_task = numTask; - int ret = worker(0, &env, cdata); - if (ret != 0) { - MS_LOGE("task 0 failed, error code is %d", ret); - return false; - } - MS_LOGD("task 0 successful"); - return true; - } - ThreadPoolTask task; - task.first = worker; - task.second.cdata = cdata; - return gThreadPool->DistributeTask(task, numTask); -} - -LiteThreadPool::~LiteThreadPool() { - destroy.store(true); - running.store(0); - queueReady.notify_all(); - for (auto &thread : threadList) { - if (thread.joinable()) { - thread.join(); - } - } -} -} // namespace predict -} // namespace mindspore diff --git a/predict/src/runtime/thread_pool.h b/predict/src/runtime/thread_pool.h deleted file mode 100644 index 53e4c1ec88e..00000000000 --- a/predict/src/runtime/thread_pool.h +++ /dev/null @@ -1,129 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_RUNTIME_THREAD_POOL_H_ -#define PREDICT_SRC_RUNTIME_THREAD_POOL_H_ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "src/runtime/runtime_api.h" - -namespace mindspore { -namespace predict { -constexpr int kSingleThreadMaxTask = 4; -using TvmEnv = TVMParallelGroupEnv; -using WorkFun = FTVMParallelLambda; -using TaskParam = struct Param { - void *cdata; - int32_t taskId; - TvmEnv *tvmParam; -}; -using ThreadPoolTask = std::pair; - -class LiteQueue { - public: - LiteQueue() = default; - ~LiteQueue() = default; - - bool Enqueue(const ThreadPoolTask &task); - bool Dequeue(ThreadPoolTask *out); - std::atomic taskSize{0}; - - private: - std::atomic head{0}; - std::atomic tail{0}; - ThreadPoolTask buffer[kSingleThreadMaxTask]{}; -}; - -class LiteThreadBind { - public: - LiteThreadBind() = default; - ~LiteThreadBind() = default; - bool Bind(int numThreads, int mode); - std::vector> threadIdList; - - private: - enum AffinityMode : int { BIG_CORE = 1, MID_CORE = -1, NO_BIND = 0 }; - void InitSortedCpuId(); - bool BindAllThread(bool bindFlag); - bool BindMasterThread(bool bindFlag, int mode = MID_CORE); - bool BindThreads(bool bindFlag); - bool SetCPUBind(pthread_t threadId, const cpu_set_t &cpuSet); - int bigCore{0}; - int midCore{0}; - int threadNums{0}; - std::vector sortedCpuIds{}; - AffinityMode bindModel{MID_CORE}; -}; - -class LiteThreadPool { - public: - LiteThreadPool() = default; - explicit LiteThreadPool(int numThreads); - ~LiteThreadPool(); - - void AddNewThread(int newNums); - bool DistributeTask(ThreadPoolTask task, int numTask); - std::vector threadList{}; - - private: - using errCode = std::pair; - bool AddRunReference(); - bool SubRunReference(); - bool CheckResult(); - int curThreadNums{0}; - std::vector> queueList; - std::atomic_int running{0}; - std::mutex tMutex; - std::condition_variable queueReady; - std::atomic destroy = {false}; - std::vector> errorInfo{}; -}; - -class ThreadPool { - public: - static ThreadPool *GetInstance(); - void ConfigThreadPool(int mode, int numThreads); - bool LaunchThreadPoolTask(); - bool AddTask(const WorkFun &worker, void *cdata, int numTask); - - ThreadPool(const ThreadPool &) = delete; - ThreadPool &operator=(const ThreadPool &) = delete; - - private: - ThreadPool() = default; - ~ThreadPool() = default; - int GetThreadNum(int numThreads); - void GetThreadIdList(); - bool SetThreadPool(int numThreads = 1); - bool SetThreadCpulBind(int mode); - std::unique_ptr gThreadPool{nullptr}; - std::unique_ptr gThreadBind{nullptr}; - std::mutex gPoolMutex; - int totalThreadNum{1}; - int bindMode{-1}; -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_SRC_RUNTIME_THREAD_POOL_H_ diff --git a/predict/src/runtime/workspace_pool.cc b/predict/src/runtime/workspace_pool.cc deleted file mode 100644 index 6cafe7482e3..00000000000 --- a/predict/src/runtime/workspace_pool.cc +++ /dev/null @@ -1,113 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -#include "src/runtime/workspace_pool.h" -#include -#include -#include "common/mslog.h" - -namespace mindspore { -namespace predict { -static constexpr size_t kWorkspacePageSize = 4096; -static constexpr int kTempAllocaAlignment = 64; -WorkspacePool *WorkspacePool::GetInstance() { - static WorkspacePool instance; - return &instance; -} - -void *WorkspacePool::AllocWorkSpaceMem(size_t size) { - size_t nbytes = (size + (kWorkspacePageSize - 1)) / kWorkspacePageSize * kWorkspacePageSize; - if (nbytes == 0) { - nbytes = kWorkspacePageSize; - } - std::pair alloc; - // fist alloc - if (freeList.empty()) { - alloc.first = nbytes; - alloc.second = memalign(kTempAllocaAlignment, nbytes); - } else if (freeList.size() == 1) { // one element - alloc = *(freeList.begin()); - freeList.erase(freeList.begin()); - if (alloc.first < nbytes) { - free(alloc.second); - alloc.first = nbytes; - alloc.second = memalign(kTempAllocaAlignment, nbytes); - } - } else { - if ((*(freeList.begin())).first >= nbytes) { - auto iter = freeList.begin(); - for (; iter != freeList.end(); ++iter) { - if ((*iter).first < size) { - alloc = *(--iter); - freeList.erase(iter); - break; - } - } - if (iter == freeList.end()) { - alloc = *(freeList.rbegin()); - freeList.erase(--freeList.end()); - } - } else { - alloc = *(freeList.begin()); - freeList.erase(freeList.begin()); - free(alloc.second); - alloc.first = nbytes; - alloc.second = memalign(kTempAllocaAlignment, nbytes); - } - } - allocList.emplace_back(alloc); - return alloc.second; -} - -void WorkspacePool::FreeWorkSpaceMem(void *ptr) { - if (ptr == nullptr) { - return; - } - std::pair alloc; - if (allocList.empty()) { - MS_LOGE("no mem have been alloc"); - return; - } else if (allocList.back().second == ptr) { - alloc = allocList.back(); - allocList.pop_back(); - } else { - auto iter = allocList.begin(); - for (; iter != allocList.end(); ++iter) { - if ((*iter).second == ptr) { - alloc = *iter; - allocList.erase(iter); - break; - } - } - if (iter == allocList.end()) { - MS_LOGE("no value ptr have been alloc"); - return; - } - } - freeList.insert(alloc); -} - -WorkspacePool::~WorkspacePool() { - for (auto &a : allocList) { - free(a.second); - } - allocList.clear(); - for (auto &f : freeList) { - free(f.second); - } - freeList.clear(); -} -} // namespace predict -} // namespace mindspore diff --git a/predict/src/runtime/workspace_pool.h b/predict/src/runtime/workspace_pool.h deleted file mode 100644 index ce8a5ca3ab7..00000000000 --- a/predict/src/runtime/workspace_pool.h +++ /dev/null @@ -1,44 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_SRC_RUNTIME_WORKSPACE_POOL_H_ -#define PREDICT_SRC_RUNTIME_WORKSPACE_POOL_H_ -#include -#include -#include -#include -#include -#include - -namespace mindspore { -namespace predict { -class WorkspacePool { - public: - WorkspacePool() = default; - ~WorkspacePool(); - WorkspacePool(const WorkspacePool &) = delete; - WorkspacePool &operator=(const WorkspacePool &) = delete; - static WorkspacePool *GetInstance(); - void *AllocWorkSpaceMem(size_t size); - void FreeWorkSpaceMem(void *ptr); - - private: - std::vector> allocList{}; - std::set, std::greater>> freeList{}; -}; -} // namespace predict -} // namespace mindspore -#endif // PREDICT_SRC_RUNTIME_WORKSPACE_POOL_H_ diff --git a/predict/src/session.cc b/predict/src/session.cc deleted file mode 100644 index b808ec7c6bd..00000000000 --- a/predict/src/session.cc +++ /dev/null @@ -1,154 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "include/session.h" -#include -#include -#include "include/errorcode.h" -#include "common/mslog.h" -#include "src/graph.h" -#include "src/graph_execution.h" - -namespace mindspore { -namespace predict { -Context m_ctx; -bool m_isConfig = false; - -// In 32bits, this evaluates to 2GB - 1 -static constexpr auto MAX_BUFFER_SIZE = ((1ULL << (sizeof(int32_t) * 8 - 1)) - 1); - -std::shared_ptr CreateSession(const char *graphBuf, size_t size, const Context &ctx) { - if (graphBuf == nullptr) { - MS_LOGE("the graphBuf is nullptr"); - return nullptr; - } - if (size > MAX_BUFFER_SIZE) { - MS_LOGE("the size is invalid"); - return nullptr; - } - auto session = std::make_shared(ctx); - MS_ASSERT(session != nullptr); - auto ret = session->Init(graphBuf, size); - if (ret != RET_OK) { - MS_LOGE("Init session failed."); - return nullptr; - } - return session; -} -Session::Session(const Context &ctx) : _ctx(ctx) { - Context cfgCtx; - cfgCtx = ctx; - if (cfgCtx.threadNum > m_ctx.threadNum) { - cfgCtx.threadNum = m_ctx.threadNum; - } -} - -int Session::Init(const char *graphBuf, size_t size) { - _graph = Graph::CreateFromBuf(graphBuf, size, _ctx); - if (_graph == nullptr) { - MS_LOGE("Graph create from buf failed."); - return RET_NULL_PTR; - } - - auto ret = this->InitExecutor(); - if (ret != RET_OK) { - MS_LOGE("Init Executor failed"); - return ret; - } - return ret; -} - -int Session::InitExecutor() { - if (_executor != nullptr) { - delete _executor; - _executor = nullptr; - } - if (_graph != nullptr) { - _executor = new (std::nothrow) GraphExecution(_ctx, _graph); - if (_executor == nullptr) { - MS_LOGE("new GraphExecution fail"); - return RET_ERROR; - } - return RET_OK; - } else { - MS_LOGE("the graph is nullptr"); - return RET_ERROR; - } -} - -Session::~Session() { - if (_executor != nullptr) { - delete _executor; - } - if (_graph != nullptr) { - delete _graph; - } -} - -int Session::Run(const std::vector &inputs) { - auto ret = RET_OK; - if (reinitExecutor) { - ret = this->InitExecutor(); - if (ret != RET_OK) { - MS_LOGE("Init Executor failed"); - return ret; - } - } - if (_executor == nullptr) { - MS_LOGE("_executor is nullptr"); - return ret; - } - ret = _executor->Run(inputs); - return ret; -} - -std::vector Session::GetInput() { - if (_executor == nullptr) { - MS_LOGE("_executor is nullptr"); - return std::vector{}; - } - auto inputs = _executor->GetInput(); - if (inputs.empty()) { - MS_LOGI("output is empty."); - } - return inputs; -} - -std::vector Session::GetOutput(const std::string &nodeName) { - if (_executor == nullptr) { - MS_LOGE("graph's executor is nullptr."); - return std::vector{}; - } - auto outputs = _executor->GetOutput(nodeName); - if (outputs.empty()) { - MS_LOGI("output is empty."); - } - return outputs; -} - -std::map> Session::GetAllOutput() { - if (_executor == nullptr) { - MS_LOGE("graph's executor is nullptr."); - return std::map>{}; - } - auto outputs = _executor->GetAllOutput(); - if (outputs.empty()) { - MS_LOGI("outputs is empty."); - } - return outputs; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/src/tensor.cc b/predict/src/tensor.cc deleted file mode 100644 index de758f3407c..00000000000 --- a/predict/src/tensor.cc +++ /dev/null @@ -1,517 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "include/tensor.h" -#include "common/mslog.h" -#include "src/op_common.h" -#include "include/errorcode.h" -#include "securec/include/securec.h" -#include "common/common.h" -#include "src/runtime/allocator.h" - -namespace mindspore { -namespace predict { -Tensor *Tensor::CopyFromTensorDef(const TensorDef &tensorDef) { - std::vector dims; - - if (tensorDef.dims() == nullptr) { - MS_LOGD("tensorDef->dims is nullptr"); - } else { - MS_ASSERT(tensorDef.dims()->data() != nullptr); - for (uint32_t j = 0; j < tensorDef.dims()->size(); j++) { - dims.push_back(tensorDef.dims()->data()[j]); - } - } - auto tensor = - std::unique_ptr(new (std::nothrow) Tensor(tensorDef.dataType(), dims, tensorDef.format(), nullptr)); - if (tensor == nullptr) { - MS_LOGE("new Tensor failed"); - return nullptr; - } - - if (tensorDef.refCount() == MSConst_WEIGHT_REFCOUNT && tensorDef.data() != nullptr && tensorDef.data()->size() > 0) { - if (dims.size() < 1) { - tensor->SetDims({1}); - } - auto ret = tensor->MallocData(); - if (ret != RET_OK) { - MS_LOGE("malloc data fail,datasize %zu", tensor->GetDataSize()); - return nullptr; - } - auto tensorData = tensorDef.data()->data(); - ret = memcpy_sp(tensor->GetData(), tensor->GetDataSize(), tensorData, tensorDef.data()->size()); - if (ret != RET_OK) { - MS_LOGE("copy data fail,dst size %zu, src size %u", tensor->GetDataSize(), tensorDef.data()->size()); - return nullptr; - } - } - tensor->refCount = tensorDef.refCount(); - return tensor.release(); -} - -Tensor::Tensor(const Tensor &tensor, bool copyData) { - format = tensor.format; - dlTensor.data = nullptr; - dlTensor.ctx.device_type = tensor.dlTensor.ctx.device_type; - dlTensor.ctx.device_id = tensor.dlTensor.ctx.device_id; - dlTensor.strides = nullptr; - dlTensor.byte_offset = tensor.dlTensor.byte_offset; - dlTensor.dtype.code = tensor.dlTensor.dtype.code; - dlTensor.dtype.bits = tensor.dlTensor.dtype.bits; - dlTensor.dtype.lanes = tensor.dlTensor.dtype.lanes; - - dlTensor.ndim = tensor.dlTensor.ndim; - if (dlTensor.ndim > 0) { - dlTensor.shape = new (std::nothrow) int64_t[dlTensor.ndim]; - if (dlTensor.shape != nullptr) { - for (int i = 0; i < dlTensor.ndim; i++) { - dlTensor.shape[i] = tensor.dlTensor.shape[i]; - } - } else { - MS_LOGW("new shape fail,ndim %d", dlTensor.ndim); - } - } else { - dlTensor.shape = nullptr; - } - if (copyData) { - allocator = tensor.allocator; - refCount = tensor.refCount; - auto ret = MallocData(); - if (ret != RET_OK) { - return; - } - size_t datasize = GetDataSize(); - ret = memcpy_sp(dlTensor.data, datasize, tensor.dlTensor.data, datasize); - if (ret != RET_OK) { - return; - } - } -} - -Tensor::Tensor(DataType dt, const std::vector &dims, Format format, void *data) { - this->format = format; - dlTensor.data = data; - dlTensor.ctx.device_type = DLDeviceType::kDLCPU; - dlTensor.ctx.device_id = 0; - dlTensor.strides = nullptr; - dlTensor.byte_offset = 0; - - dlTensor.ndim = static_cast(dims.size()); - if (dlTensor.ndim > 0) { - dlTensor.shape = new (std::nothrow) int64_t[dlTensor.ndim]; - if (dlTensor.shape != nullptr) { - for (int i = 0; i < dlTensor.ndim; i++) { - dlTensor.shape[i] = dims[i]; - } - } else { - MS_LOGW("new shape fail,ndim %d", dlTensor.ndim); - } - } else { - dlTensor.shape = nullptr; - } - - SetDataType(dt); -} - -Tensor::~Tensor() { FreeTensor(); } - -DLDataType Tensor::GetTensorDtype() const { return dlTensor.dtype; } - -void *Tensor::GetData() const { return dlTensor.data; } - -void Tensor::SetData(void *data) { dlTensor.data = data; } - -DataType Tensor::GetDataType() const { - DataType dataType = DataType_DT_UNDEFINED; - switch (dlTensor.dtype.code) { - case kDLFloat: - if (dlTensor.dtype.bits == 32) { - dataType = DataType_DT_FLOAT; - } else if (dlTensor.dtype.bits == 16) { - dataType = DataType_DT_FLOAT16; - } - break; - case kDLInt: - if (dlTensor.dtype.bits == 32) { - dataType = DataType_DT_INT32; - } else if (dlTensor.dtype.bits == 8) { - dataType = DataType_DT_INT8; - } - break; - case kDLUInt: - if (dlTensor.dtype.bits == 32) { - dataType = DataType_DT_UINT32; - } else if (dlTensor.dtype.bits == 8) { - dataType = DataType_DT_UINT8; - } - break; - default: - break; - } - return dataType; -} - -void Tensor::SetDataType(DataType dt) { - switch (dt) { - case DataType_DT_FLOAT: - dlTensor.dtype.code = kDLFloat; - dlTensor.dtype.bits = 32; - dlTensor.dtype.lanes = 1; - break; - case DataType_DT_FLOAT16: - dlTensor.dtype.code = kDLFloat; - dlTensor.dtype.bits = 16; - dlTensor.dtype.lanes = 1; - break; - case DataType_DT_INT8: - dlTensor.dtype.code = kDLInt; - dlTensor.dtype.bits = 8; - dlTensor.dtype.lanes = 1; - break; - case DataType_DT_UINT8: - dlTensor.dtype.code = kDLUInt; - dlTensor.dtype.bits = 8; - dlTensor.dtype.lanes = 1; - break; - case DataType_DT_INT32: - dlTensor.dtype.code = kDLInt; - dlTensor.dtype.bits = 32; - dlTensor.dtype.lanes = 1; - break; - case DataType_DT_UINT32: - dlTensor.dtype.code = kDLUInt; - dlTensor.dtype.bits = 32; - dlTensor.dtype.lanes = 1; - break; - default: - MS_LOGW(" DataType %d is not implemented.", dt); - MS_LOGW(" DataType DT_FLOAT is used."); - dlTensor.dtype.code = kDLFloat; - dlTensor.dtype.bits = 32; - dlTensor.dtype.lanes = 1; - return; - } -} - -int Tensor::GetNDim() const { return dlTensor.ndim; } - -std::vector Tensor::GetDims() const { - std::vector dims; - for (int i = 0; i < dlTensor.ndim; i++) { - dims.push_back(dlTensor.shape[i]); - } - return dims; -} - -size_t Tensor::GetElementSize() const { - const int tile = 4; - if (format == Format_NC4HW4) { - size_t size = 1; - for (int i = 0; i < dlTensor.ndim; i++) { - auto var = static_cast(dlTensor.shape[i]); - if (i == 1) { - var = UP_DIV(var, tile) * tile; - } - size *= var; - } - return size; - } else { - size_t size = 1; - for (int i = 0; i < dlTensor.ndim; i++) { - size *= static_cast(dlTensor.shape[i]); - } - - return size; - } -} - -size_t Tensor::GetDataSize() const { - size_t size = GetElementSize(); - - const int BYTES = 8; - const int GAP = 7; - size *= (dlTensor.dtype.bits * dlTensor.dtype.lanes + GAP) / BYTES; - return size; -} - -int Tensor::MallocData(std::shared_ptr allocator, int refCount) { - if (dlTensor.data != nullptr) { - this->refCount += refCount; - return RET_OK; - } - this->refCount = refCount; - - size_t size = GetDataSize(); - if (allocator) { - this->allocator = allocator; - dlTensor.data = allocator->Malloc(size); - } else { - if (size > MAX_MALLOC_SIZE) { - return RET_ERROR; - } - dlTensor.data = malloc(size); - } - if (dlTensor.data == nullptr) { - return RET_ERROR; - } - return RET_OK; -} - -void Tensor::ForceFreeData() { - if (allocator) { - allocator->Free(dlTensor.data); - } else { - free(dlTensor.data); - } - dlTensor.data = nullptr; -} - -void Tensor::FreeData() { - --refCount; - if (refCount <= 0) { - ForceFreeData(); - } -} - -bool Tensor::CompareShape(const Tensor &dst) { - if (dlTensor.ndim != dst.dlTensor.ndim || dlTensor.shape == nullptr || dst.dlTensor.shape == nullptr) { - MS_LOGE("param error, one.ndim: %d, other.ndim: %d, one shape %p,other shape %p", dlTensor.ndim, dst.dlTensor.ndim, - dlTensor.shape, dst.dlTensor.shape); - return false; - } - - for (int i = 0; i < dlTensor.ndim; i++) { - if (dlTensor.shape[i] != dst.dlTensor.shape[i]) { - MS_LOGE("one.shape[%d]: %ld, other.shape[%d]: %ld", i, dlTensor.shape[i], i, dst.dlTensor.shape[i]); - return false; - } - } - return true; -} - -bool Tensor::CompareShape(const std::vector &other) { - if (dlTensor.ndim != other.size() || dlTensor.shape == nullptr) { - return false; - } - - for (int i = 0; i < dlTensor.ndim; i++) { - if (dlTensor.shape[i] != other[i]) { - return false; - } - } - return true; -} - -int64_t Tensor::Height() const { - if (dlTensor.shape == nullptr) { - MS_LOGE("shape is null"); - } - if (dlTensor.ndim != DIM_DEFAULT_SIZE) { - MS_LOGE("Tensor should be 4 dimensional."); - return -1; - } - switch (this->format) { - case Format_NCHW: - case Format_NC4HW4: - return dlTensor.shape[NCHW_H]; - case Format_NHWC: - return dlTensor.shape[NHWC_H]; - default: - MS_LOGE("Unsupported format: %d", this->format); - return -1; - } -} - -int64_t Tensor::Width() const { - if (dlTensor.shape == nullptr) { - MS_LOGE("shape is null"); - } - if (dlTensor.ndim != DIM_DEFAULT_SIZE) { - MS_LOGE("Tensor should be 4 dimensional."); - return -1; - } - switch (this->format) { - case Format_NCHW: - case Format_NC4HW4: - return dlTensor.shape[NCHW_W]; - case Format_NHWC: - return dlTensor.shape[NHWC_W]; - default: - MS_LOGE("Unsupported format: %d", this->format); - return -1; - } -} - -int64_t Tensor::Channel() const { - if (dlTensor.shape == nullptr) { - MS_LOGE("shape is null"); - } - if (dlTensor.ndim != DIM_DEFAULT_SIZE) { - MS_LOGE("Tensor should be 4 dimensional."); - return -1; - } - switch (this->format) { - case Format_NCHW: - case Format_NC4HW4: - return dlTensor.shape[NCHW_C]; - case Format_NHWC: - return dlTensor.shape[NHWC_C]; - default: - MS_LOGE("Unsupported format: %d", this->format); - return -1; - } -} - -int64_t Tensor::Batch() const { - if (dlTensor.shape == nullptr) { - MS_LOGE("shape is null"); - } - if (dlTensor.ndim != DIM_DEFAULT_SIZE) { - MS_LOGE("Tensor should be 4 dimensional."); - return -1; - } - switch (this->format) { - case Format_NCHW: - case Format_NC4HW4: - case Format_NHWC: - return dlTensor.shape[NCHW_N]; - default: - MS_LOGE("Unsupported format: %d", this->format); - return -1; - } -} - -int64_t Tensor::Stride(int index) const { - if (dlTensor.strides) { - return dlTensor.strides[index]; - } - if (dlTensor.shape == nullptr) { - MS_LOGE("shape is null"); - return -1; - } - int64_t stride = 1; - for (int i = index + 1; i < dlTensor.ndim; i++) { - stride *= dlTensor.shape[i]; - } - return stride; -} - -void Tensor::SetStride() { - if (dlTensor.strides == nullptr) { - if (dlTensor.ndim < 1) { - MS_LOGE("dims of dlTensor is empty."); - return; - } - dlTensor.strides = new (std::nothrow) int64_t[dlTensor.ndim - 1]; - if (dlTensor.strides == nullptr) { - MS_LOGW("new stride fail, ndim %d.", dlTensor.ndim); - return; - } - } - - for (int idx = 0; idx < dlTensor.ndim - 1; idx++) { - int64_t stride = 1; - if (dlTensor.ndim <= idx + 1) { - MS_LOGE("out of for loop upper limit."); - return; - } - for (int i = idx + 1; i < dlTensor.ndim; i++) { - stride *= dlTensor.shape[i]; - } - dlTensor.strides[idx] = stride; - } -} -void Tensor::SetScale(bool isScale) { this->isScale = isScale; } - -void Tensor::SetStride(int index, int64_t stride) { - if (index >= dlTensor.ndim) { - return; - } - - if (dlTensor.strides == nullptr) { - SetStride(); - } - - dlTensor.strides[index] = stride; - return; -} - -void Tensor::SetDims(const std::vector &dims) { - if (dlTensor.shape != nullptr) { - delete[] dlTensor.shape; - } - dlTensor.ndim = static_cast(dims.size()); - if (dlTensor.ndim > 0) { - dlTensor.shape = new (std::nothrow) int64_t[dlTensor.ndim]; - if (dlTensor.shape != nullptr) { - for (int i = 0; i < dlTensor.ndim; i++) { - dlTensor.shape[i] = dims[i]; - } - } else { - MS_LOGW("new shape fail,ndim %d", dlTensor.ndim); - } - } else { - dlTensor.shape = nullptr; - } -} - -void Tensor::FreeTensor() { - if (dlTensor.shape != nullptr) { - delete[] dlTensor.shape; - dlTensor.shape = nullptr; - } - - if (dlTensor.strides != nullptr) { - delete[] dlTensor.strides; - dlTensor.strides = nullptr; - } - - dlTensor.ndim = 0; - - if (allocator != nullptr) { - allocator->Free(dlTensor.data); - } else { - free(dlTensor.data); - } - dlTensor.data = nullptr; -} - -size_t Tensor::GetNC4HW4ElementSize(bool isNhwc) { - int alignIndex = 1; - if (isNhwc) { - alignIndex = 3; - } - - size_t size = 1; - for (int i = 0; i < dlTensor.ndim; i++) { - auto var = static_cast(dlTensor.shape[i]); - if (i == alignIndex) { - var = ALIGN_UP4(var); - } - size *= var; - } - return size; -} - -size_t Tensor::GetNC4HW4DataSize(bool isNhwc) { - size_t size = GetNC4HW4ElementSize(isNhwc); - const int BYTES = 8; - const int GAP = 7; - size *= (dlTensor.dtype.bits * dlTensor.dtype.lanes + GAP) / BYTES; - return size; -} -} // namespace predict -} // namespace mindspore diff --git a/predict/test/CMakeLists.txt b/predict/test/CMakeLists.txt deleted file mode 100755 index 9370ff7ce0f..00000000000 --- a/predict/test/CMakeLists.txt +++ /dev/null @@ -1,49 +0,0 @@ -cmake_minimum_required(VERSION 3.12) -project(ms-test) - -set(CMAKE_CXX_STANDARD 11) - -#include 3rd -include_directories(${3RD_DIR}/securec/include) -include_directories(${3RD_DIR}/flatbuffers/include) -include_directories(${3RD_DIR}/googletest/googletest/include) -include_directories(${3RD_DIR}/googletest/googlemock/include) -include_directories(${3RD_DIR}/securec/include) - -#include ms -include_directories(.) -include_directories(..) - -link_directories(${CMAKE_CURRENT_SOURCE_DIR}/../output/lib/) - -set(COMMON_SRC ${PREDICT_DIR}/common/flag_parser.cc - ${PREDICT_DIR}/common/file_utils.cc - ${PREDICT_DIR}/common/mslog.cc - ${PREDICT_DIR}/common/storage.cc - ${PREDICT_DIR}/common/utils.cc) - -#tools src -file(GLOB_RECURSE TOOLS_SRC ../tools/*.cpp) - -add_executable(ms-test - ${COMMON_SRC} - ${TOOLS_SRC} - src/graph_tests.cc - benchmark/benchmark_tests.cc - ${CMAKE_SOURCE_DIR}/benchmark/benchmark.cc - ${TF_PROTO_SRC} - ${MS_CONVERTER_SRC} - test_context.h - test_context.cc - main.cc) - -target_link_libraries(ms-test mspredict gtest libsecurec.a) -add_dependencies(ms-test securec) -add_dependencies(ms-test gtest) - -# copy test file -add_custom_command(TARGET ms-test POST_BUILD - COMMAND mkdir -pv ${DOTEST_DIR} - COMMAND cp ${PREDICT_BUILD_DIR}/test/ms-test ${DOTEST_DIR} - COMMAND cp ${PREDICT_DIR}/test/run_tests.sh ${PREDICT_BUILD_DIR}/test/ - COMMAND cp -r ${PREDICT_DIR}/test/data/ ${PREDICT_BUILD_DIR}/test/doTest/) diff --git a/predict/test/benchmark/benchmark_tests.cc b/predict/test/benchmark/benchmark_tests.cc deleted file mode 100644 index e1e218e8518..00000000000 --- a/predict/test/benchmark/benchmark_tests.cc +++ /dev/null @@ -1,69 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include "test/test_context.h" -#include "benchmark/benchmark.h" - -#define LENET_ARGS 2 -#define MS_ARGS 4 - -namespace mindspore { -namespace predict { -class BenchmarkTest : public ::testing::Test { - protected: - void SetUp() {} - - void TearDown() {} - std::string root; -}; - -TEST_F(BenchmarkTest, BenchmarkRun) { - const char* args[LENET_ARGS]; - args[0] = "./benchmark"; - args[1] = "--modelPath=./data/lenet/lenet.ms"; - - int errorcode = mindspore::predict::RunBenchmark(LENET_ARGS, args); - EXPECT_EQ(0, errorcode); -} - -TEST_F(BenchmarkTest, LenetRun) { - const char* args[MS_ARGS]; - args[0] = "./benchmark"; - args[1] = "--modelPath=./data/ms/mindspore.ms"; - args[2] = "--inDataPath=./data/ms/mindspore.bin"; - args[3] = "--calibDataPath=./data/ms/mindspore.out"; - - int errorcode = mindspore::predict::RunBenchmark(MS_ARGS, args); - EXPECT_EQ(0, errorcode); -} - -TEST_F(BenchmarkTest, MindSporeRun) { - const char* args[4]; - args[0] = "./benchmark"; - args[1] = "--modelPath=./data/lenet/lenet.ms"; - args[2] = "--inDataPath=./data/lenet/lenet.bin"; - args[3] = "--calibDataPath=./data/lenet/lenet.out"; - - int errorcode = mindspore::predict::RunBenchmark(4, args); - EXPECT_EQ(0, errorcode); -} -} // namespace predict -} // namespace mindspore diff --git a/predict/test/data/lenet/lenet.bin b/predict/test/data/lenet/lenet.bin deleted file mode 100755 index 6aef53bd64b..00000000000 Binary files a/predict/test/data/lenet/lenet.bin and /dev/null differ diff --git a/predict/test/data/lenet/lenet.ms b/predict/test/data/lenet/lenet.ms deleted file mode 100755 index c66948dd677..00000000000 Binary files a/predict/test/data/lenet/lenet.ms and /dev/null differ diff --git a/predict/test/data/lenet/lenet.out b/predict/test/data/lenet/lenet.out deleted file mode 100644 index 31a1d61b8b2..00000000000 --- a/predict/test/data/lenet/lenet.out +++ /dev/null @@ -1,2 +0,0 @@ -prob 2 1 10 -0.0 0.9999994 5.4061115e-07 0.0 0.0 0.0 0.0 5.690875e-08 1.1269122e-34 0.0 diff --git a/predict/test/data/ms/mindspore.bin b/predict/test/data/ms/mindspore.bin deleted file mode 100755 index 77981f8b7ef..00000000000 --- a/predict/test/data/ms/mindspore.bin +++ /dev/null @@ -1,5 +0,0 @@ -p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پkI<6,?6,?6,?6,?6,?BL>p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پy>b??<4@<4@<4@<4@@L?p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ`9?>`9?>`9?>`9?>`9?>`9?>`9?>N?<4@<4@<4@!2@@Yy @ -f>p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ?<4@<4@<4@<4@<4@<4@<4@<4@<4@<4@!2@R@lnp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ?<4@<4@<4@<4@<4@<4@<4@<4@<4@<4@@lnҾp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پh?Yy @w#@<4@<4@<4@<4@<4@<4@6,?`9?>`9?>`9?>dד=p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پYy @<4@<4@<4@<4@p@b?b?b?=ٽp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پYy @<4@<4@<4@@;?p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پYy @<4@<4@<4@,?p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ ?5 @<4@<4@@b??ݜp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ ?w#@<4@<4@<4@:)@T>`9?>`9?> p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ?b>-@<4@<4@<4@<4@<4@<4@4?p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ4?<4@<4@<4@<4@<4@<4@b>-@?p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ `9?>`9?>T>:)@<4@<4@<4@w#@ ?p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پݜ?b?@<4@<4@5 @ ?p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پL?BA@<4@<4@w#@?p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ?<4@<4@<4@'@ap2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ -f>b?kI-@@!2@<4@<4@<4@<4@<4@4@y>~žp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ -f>@<4@<4@<4@<4@<4@<4@<4@<4@<4@<4@@ -f>p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پL?6,?6,?6,?6,?6,?6,?6,?6,?6,?6,?L?p2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پp2پ \ No newline at end of file diff --git a/predict/test/data/ms/mindspore.ms b/predict/test/data/ms/mindspore.ms deleted file mode 100755 index 654e4060aaf..00000000000 Binary files a/predict/test/data/ms/mindspore.ms and /dev/null differ diff --git a/predict/test/data/ms/mindspore.out b/predict/test/data/ms/mindspore.out deleted file mode 100644 index a7325da0e39..00000000000 --- a/predict/test/data/ms/mindspore.out +++ /dev/null @@ -1,2 +0,0 @@ -Default/fc3-Dense/BiasAdd-op14 2 1 10 --2.1191406 -8.4140625 -13.625 2.8222656 -11.4453125 30.734375 7.515625 -9.921875 2.5371094 2.9238281 diff --git a/predict/test/main.cc b/predict/test/main.cc deleted file mode 100644 index 7e18f9a10d0..00000000000 --- a/predict/test/main.cc +++ /dev/null @@ -1,50 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "test/test_context.h" -#include "common/mslog.h" - -int main(int argc, char **argv) { - // Initialize Google Test. - testing::InitGoogleTest(&argc, argv); - - for (size_t i = 0; i < argc; i++) { - std::string arg = std::string(argv[i]); - if (arg.find("--testRoot") != std::string::npos) { - auto testContext = - std::shared_ptr(new (std::nothrow) mindspore::predict::TestContext()); - if (testContext == nullptr) { - MS_LOGE("new testContext failed"); - return 1; - } - testContext->SetTestRoot(arg.substr(arg.find("--testRoot=") + 11)); - break; - } - } - - int result = RUN_ALL_TESTS(); - - return result; -} diff --git a/predict/test/run_tests.sh b/predict/test/run_tests.sh deleted file mode 100755 index e5a94e70f71..00000000000 --- a/predict/test/run_tests.sh +++ /dev/null @@ -1,28 +0,0 @@ -#!/bin/bash -# Copyright 2019 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ - -set -e - -CUR_DIR=$(dirname "$(readlink -f "$0")") -echo "$CUR_DIR" -DOTEST_DIR="$CUR_DIR"/doTest - -cd "$DOTEST_DIR" -./ms-test -if [ $? -ne 0 ]; then - echo "run ./ms-test failed !" - exit 1 -fi diff --git a/predict/test/src/graph_tests.cc b/predict/test/src/graph_tests.cc deleted file mode 100644 index 8fbaf689f3a..00000000000 --- a/predict/test/src/graph_tests.cc +++ /dev/null @@ -1,148 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include -#include -#include -#include "schema/inner/ms_generated.h" -#include "src/graph.h" -#include "common/file_utils.h" -#include "test/test_context.h" -#include "include/session.h" - -namespace mindspore { -namespace predict { -class GraphTest : public ::testing::Test { - protected: - void SetUp() {} - - void TearDown() {} - - std::string root; -}; - -void InitMsGraphAllTensor(SubGraphDefT *msSubgraph) { - ASSERT_NE(msSubgraph, nullptr); - std::unique_ptr tensor (new (std::nothrow) TensorDefT); - ASSERT_NE(tensor, nullptr); - tensor->refCount = MSConst_WEIGHT_REFCOUNT; - tensor->format = Format_NCHW; - tensor->dataType = DataType_DT_FLOAT; - tensor->dims = {1, 1, 1, 2}; - tensor->offset = -1; - tensor->data.resize(0); - msSubgraph->allTensors.emplace_back(std::move(tensor)); - - std::unique_ptr tensor2(new (std::nothrow) TensorDefT); - ASSERT_NE(tensor2, nullptr); - tensor2->refCount = MSConst_WEIGHT_REFCOUNT; - tensor2->format = Format_NCHW; - tensor2->dataType = DataType_DT_FLOAT; - tensor2->dims = {1, 1, 1, 2}; - tensor2->offset = -1; - tensor2->data.resize(0); - msSubgraph->allTensors.emplace_back(std::move(tensor2)); - - std::unique_ptr tensor3(new (std::nothrow) TensorDefT); - ASSERT_NE(tensor3, nullptr); - tensor3->refCount = 0; - tensor3->format = Format_NCHW; - tensor3->dataType = DataType_DT_FLOAT; - tensor3->dims = {1, 1, 1, 2}; - tensor3->offset = -1; - tensor3->data.resize(0); - msSubgraph->allTensors.emplace_back(std::move(tensor3)); -} - -void FreeOutputs(std::map> *outputs) { - for (auto &output : (*outputs)) { - for (auto &outputTensor : output.second) { - delete outputTensor; - } - } - outputs->clear(); -} - -void FreeInputs(std::vector *inputs) { - for (auto &input : *inputs) { - input->SetData(nullptr); - delete input; - } - inputs->clear(); - return; -} - -TEST_F(GraphTest, CreateFromFileAdd) { - auto msGraph = std::unique_ptr(new (std::nothrow) GraphDefT()); - ASSERT_NE(msGraph, nullptr); - msGraph->name = "test1"; - auto msSubgraph = std::unique_ptr(new (std::nothrow) SubGraphDefT()); - ASSERT_NE(msSubgraph, nullptr); - msSubgraph->name = msGraph->name + "_1"; - msSubgraph->inputIndex = {0, 1}; - msSubgraph->outputIndex = {2}; - - std::unique_ptr node(new (std::nothrow) NodeDefT); - ASSERT_NE(node, nullptr); - std::unique_ptr opDef(new (std::nothrow) OpDefT); - ASSERT_NE(opDef, nullptr); - node->opDef = std::move(opDef); - node->opDef->isLastConv = false; - node->opDef->inputIndex = {static_cast(0), 1}; - node->opDef->outputIndex = {static_cast(2)}; - node->opDef->name = msSubgraph->name + std::to_string(0); - node->fmkType = FmkType_CAFFE; - - auto attr = std::unique_ptr(new (std::nothrow) AddT()); - ASSERT_NE(attr, nullptr); - attr->format = DataFormatType_NCHW; - node->opDef->attr.type = OpT_Add; - node->opDef->attr.value = attr.release(); - - msSubgraph->nodes.emplace_back(std::move(node)); - - InitMsGraphAllTensor(msSubgraph.get()); - msGraph->subgraphs.emplace_back(std::move(msSubgraph)); - - flatbuffers::FlatBufferBuilder builder(1024); - auto offset = mindspore::predict::GraphDef::Pack(builder, msGraph.get()); - builder.Finish(offset); - int size = builder.GetSize(); - void *content = builder.GetBufferPointer(); - - Context ctx; - auto session = CreateSession(static_cast(content), size, ctx); - - std::vector tmpT = {1, 2}; - void *in1Data = tmpT.data(); - std::vector tmpT2 = {3, 5}; - void *in2Data = tmpT2.data(); - - auto inputs = session->GetInput(); - inputs[0]->SetData(in1Data); - inputs[1]->SetData(in2Data); - - auto ret = session->Run(inputs); - EXPECT_EQ(0, ret); - auto outputs = session->GetAllOutput(); - EXPECT_EQ(4, reinterpret_cast(outputs.begin()->second.front()->GetData())[0]); - EXPECT_EQ(7, reinterpret_cast(outputs.begin()->second.front()->GetData())[1]); - - FreeOutputs(&outputs); - FreeInputs(&inputs); -} -} // namespace predict -} // namespace mindspore diff --git a/predict/test/test_context.cc b/predict/test/test_context.cc deleted file mode 100644 index ca8f36d3a87..00000000000 --- a/predict/test/test_context.cc +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "test/test_context.h" - -namespace mindspore { -namespace predict { -std::string TestContext::GetTestRoot() { return this->testRoot; } - -void TestContext::SetTestRoot(const std::string &testRoot) { this->testRoot = testRoot; } -} // namespace predict -} // namespace mindspore diff --git a/predict/test/test_context.h b/predict/test/test_context.h deleted file mode 100644 index 16f439d6e6c..00000000000 --- a/predict/test/test_context.h +++ /dev/null @@ -1,36 +0,0 @@ -/** - * Copyright 2019 Huawei Technologies Co., Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef PREDICT_TEST_TEST_CONTEXT_H_ -#define PREDICT_TEST_TEST_CONTEXT_H_ - -#include - -namespace mindspore { -namespace predict { -class TestContext { - public: - TestContext() = default; - std::string GetTestRoot(); - void SetTestRoot(const std::string &testRoot); - - private: - std::string testRoot = "./"; -}; -} // namespace predict -} // namespace mindspore - -#endif // PREDICT_TEST_TEST_CONTEXT_H_ diff --git a/tests/ut/python/predict/test_predict_save_model.py b/tests/ut/python/predict/test_predict_save_model.py deleted file mode 100644 index f57875d073d..00000000000 --- a/tests/ut/python/predict/test_predict_save_model.py +++ /dev/null @@ -1,93 +0,0 @@ -# Copyright 2020 Huawei Technologies Co., Ltd -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. -# ============================================================================ -""" -Function: - test network -Usage: - python test_predict_save_model.py --path ./ -""" - -import argparse -import os -import numpy as np - -import mindspore.context as context -import mindspore.nn as nn -import mindspore.ops.operations as P -from mindspore.common.tensor import Tensor -from mindspore.train.serialization import export, load_checkpoint, load_param_into_net - - -class LeNet(nn.Cell): - def __init__(self): - super(LeNet, self).__init__() - self.relu = P.ReLU() - self.batch_size = 32 - - self.conv1 = nn.Conv2d(1, 6, kernel_size=5, stride=1, padding=0, has_bias=False, pad_mode='valid') - self.conv2 = nn.Conv2d(6, 16, kernel_size=5, stride=1, padding=0, has_bias=False, pad_mode='valid') - self.pool = nn.MaxPool2d(kernel_size=2, stride=2) - self.reshape = P.Reshape() - self.fc1 = nn.Dense(400, 120) - self.fc2 = nn.Dense(120, 84) - self.fc3 = nn.Dense(84, 10) - - def construct(self, input_x): - output = self.conv1(input_x) - output = self.relu(output) - output = self.pool(output) - output = self.conv2(output) - output = self.relu(output) - output = self.pool(output) - output = self.reshape(output, (self.batch_size, -1)) - output = self.fc1(output) - output = self.relu(output) - output = self.fc2(output) - output = self.relu(output) - output = self.fc3(output) - return output - - -parser = argparse.ArgumentParser(description='MindSpore Model Save') -parser.add_argument('--path', default='./lenet_model.ms', type=str, help='model save path') - -if __name__ == '__main__': - context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") - - print("test lenet predict start") - seed = 0 - np.random.seed(seed) - batch = 32 - channel = 1 - input_h = 32 - input_w = 32 - origin_data = np.random.uniform(low=0, high=255, size=(batch, channel, input_h, input_w)).astype(np.float32) - origin_data.tofile("lenet_input_data.bin") - - input_data = Tensor(origin_data) - print(input_data.asnumpy()) - net = LeNet() - ckpt_file_path = "./tests/ut/python/predict/checkpoint_lenet.ckpt" - predict_args = parser.parse_args() - model_path_name = predict_args.path - - is_ckpt_exist = os.path.exists(ckpt_file_path) - if is_ckpt_exist: - param_dict = load_checkpoint(ckpt_file_name=ckpt_file_path) - load_param_into_net(net, param_dict) - export(net, input_data, file_name=model_path_name, file_format='LITE') - print("test lenet predict success.") - else: - print("checkpoint file is not exist.") diff --git a/third_party/patch/predict/0001-RetBugFix-CustomRuntime_v06.patch b/third_party/patch/predict/0001-RetBugFix-CustomRuntime_v06.patch deleted file mode 100644 index 5977c943ef9..00000000000 --- a/third_party/patch/predict/0001-RetBugFix-CustomRuntime_v06.patch +++ /dev/null @@ -1,203 +0,0 @@ -diff --git a/include/tvm/runtime/registry.h b/include/tvm/runtime/registry.h -index d668984..3676a61 100644 ---- a/include/tvm/runtime/registry.h -+++ b/include/tvm/runtime/registry.h -@@ -319,6 +319,19 @@ class Registry { - #define TVM_REGISTER_EXT_TYPE(T) \ - TVM_STR_CONCAT(TVM_TYPE_REG_VAR_DEF, __COUNTER__) = \ - ::tvm::runtime::ExtTypeVTable::Register_() -+/* -+ * Macro transfer TVM runtime API to custom runtime API -+ */ -+#define TVM_RT_FUNC_TRANS(OrigFuncStr) ({ \ -+ const runtime::PackedFunc* trans_func = runtime::Registry::Get("codegen.GetTransRTFunc");\ -+ const char* dst_func_str = nullptr; \ -+ if( trans_func != nullptr){ \ -+ dst_func_str = ((*trans_func)(OrigFuncStr)).ptr(); \ -+ }else{ \ -+ dst_func_str = OrigFuncStr; \ -+ } \ -+ dst_func_str; \ -+}) - - } // namespace runtime - } // namespace tvm -diff --git a/src/codegen/llvm/codegen_cpu.cc b/src/codegen/llvm/codegen_cpu.cc -index 0ba0c58..2850ad4 100644 ---- a/src/codegen/llvm/codegen_cpu.cc -+++ b/src/codegen/llvm/codegen_cpu.cc -@@ -99,26 +99,26 @@ void CodeGenCPU::Init(const std::string& module_name, - // We will need this in environment for backward registration. - f_tvm_register_system_symbol_ = llvm::Function::Create( - llvm::FunctionType::get(t_int_, {t_char_->getPointerTo(), t_void_p_}, false), -- llvm::Function::ExternalLinkage, "TVMBackendRegisterSystemLibSymbol", module_.get()); -+ llvm::Function::ExternalLinkage, TVM_RT_FUNC_TRANS("TVMBackendRegisterSystemLibSymbol"), module_.get()); - } else { - f_tvm_register_system_symbol_ = nullptr; - } - if (dynamic_lookup || system_lib) { - f_tvm_func_call_ = llvm::Function::Create( - ftype_tvm_func_call_, -- llvm::Function::ExternalLinkage, "TVMFuncCall", module_.get()); -+ llvm::Function::ExternalLinkage, TVM_RT_FUNC_TRANS("TVMFuncCall"), module_.get()); - f_tvm_get_func_from_env_ = llvm::Function::Create( - ftype_tvm_get_func_from_env_, - llvm::Function::ExternalLinkage, "TVMBackendGetFuncFromEnv", module_.get()); - f_tvm_api_set_last_error_ = llvm::Function::Create( - ftype_tvm_api_set_last_error_, -- llvm::Function::ExternalLinkage, "TVMAPISetLastError", module_.get()); -+ llvm::Function::ExternalLinkage, TVM_RT_FUNC_TRANS("TVMAPISetLastError"), module_.get()); - f_tvm_parallel_launch_ = llvm::Function::Create( - ftype_tvm_parallel_launch_, -- llvm::Function::ExternalLinkage, "TVMBackendParallelLaunch", module_.get()); -+ llvm::Function::ExternalLinkage, TVM_RT_FUNC_TRANS("TVMBackendParallelLaunch"), module_.get()); - f_tvm_parallel_barrier_ = llvm::Function::Create( - ftype_tvm_parallel_barrier_, -- llvm::Function::ExternalLinkage, "TVMBackendParallelBarrier", module_.get()); -+ llvm::Function::ExternalLinkage, TVM_RT_FUNC_TRANS("TVMBackendParallelBarrier"), module_.get()); - } - this->InitGlobalContext(dynamic_lookup); - } -@@ -461,11 +461,14 @@ void CodeGenCPU::CreateComputeScope(const AttrStmt* op) { - } - std::swap(function_, fcompute); - std::swap(new_vmap, var_map_); -+ std::stack br_ret_flg; -+ std::swap(br_ret_flg, br_ret_flg_); - BasicBlock *compute_entry = BasicBlock::Create(*ctx_, "entry", function_); - builder_->SetInsertPoint(compute_entry); - this->VisitStmt(op->body); - builder_->CreateRet(ConstInt32(0)); - // swap the var map back, now we are back on track. -+ std::swap(br_ret_flg, br_ret_flg_); - std::swap(new_vmap, var_map_); - std::swap(function_, fcompute); - builder_->SetInsertPoint(compute_call_end); -@@ -542,9 +545,12 @@ void CodeGenCPU::CreateParallelLaunch(const Stmt& body, int num_task) { - std::swap(function_, f); - std::swap(parallel_env_, par_env); - std::swap(var_map_, new_vmap); -+ std::stack br_ret_flg; -+ std::swap(br_ret_flg, br_ret_flg_); - this->VisitStmt(body); - builder_->CreateRet(ConstInt32(0)); - // swap the var map back, now we are back on track. -+ std::swap(br_ret_flg, br_ret_flg_); - std::swap(var_map_, new_vmap); - std::swap(parallel_env_, par_env); - std::swap(function_, f); -@@ -794,7 +800,9 @@ llvm::Value* CodeGenCPU::CreateIntrinsic(const Call* op) { - } else if (op->is_intrinsic(intrinsic::tvm_static_handle)) { - return CreateStaticHandle(); - } else if (op->is_intrinsic(intrinsic::tvm_throw_last_error)) { -- builder_->CreateRet(ConstInt32(-1)); -+ llvm::Value* pRetCode = (op->args.size() == 0) ? ConstInt32(-1) : MakeValue(op->args[0]); -+ builder_->CreateRet(pRetCode); -+ CodeGenLLVM::SetRetTrFlg(true); - return ConstInt32(-1); - } else if (op->is_intrinsic(intrinsic::tvm_struct_get)) { - CHECK_EQ(op->args.size(), 3U); -diff --git a/src/codegen/llvm/codegen_llvm.cc b/src/codegen/llvm/codegen_llvm.cc -index 2cff88b..e26812d 100644 ---- a/src/codegen/llvm/codegen_llvm.cc -+++ b/src/codegen/llvm/codegen_llvm.cc -@@ -1110,23 +1110,37 @@ void CodeGenLLVM::VisitStmt_(const IfThenElse* op) { - *ctx_, "if_then", function_); - BasicBlock* end_block = BasicBlock::Create( - *ctx_, "if_end", function_); -+ // define ret terminitor exist flg for this Stmt -+ bool cur_br_ret_flg = false; -+ br_ret_flg_.push(&cur_br_ret_flg); - if (op->else_case.defined()) { - BasicBlock* else_block = BasicBlock::Create( - *ctx_, "if_else", function_); - builder_->CreateCondBr(cond, then_block, else_block); - builder_->SetInsertPoint(then_block); -+ cur_br_ret_flg = false; - this->VisitStmt(op->then_case); - builder_->CreateBr(end_block); -+ if ( !cur_br_ret_flg ){ -+ builder_->CreateBr(end_block); -+ } - builder_->SetInsertPoint(else_block); -+ cur_br_ret_flg = false; - this->VisitStmt(op->else_case); -- builder_->CreateBr(end_block); -+ if ( !cur_br_ret_flg ){ -+ builder_->CreateBr(end_block); -+ } - } else { - builder_->CreateCondBr(cond, then_block, end_block, md_very_likely_branch_); - builder_->SetInsertPoint(then_block); -+ cur_br_ret_flg = false; - this->VisitStmt(op->then_case); -- builder_->CreateBr(end_block); -+ if ( !cur_br_ret_flg ){ -+ builder_->CreateBr(end_block); -+ } - } - builder_->SetInsertPoint(end_block); -+ br_ret_flg_.pop(); - } - - -diff --git a/src/codegen/llvm/codegen_llvm.h b/src/codegen/llvm/codegen_llvm.h -index b7d091b..6fba863 100644 ---- a/src/codegen/llvm/codegen_llvm.h -+++ b/src/codegen/llvm/codegen_llvm.h -@@ -143,6 +143,11 @@ class CodeGenLLVM : - void VisitStmt_(const Block* op) override; - void VisitStmt_(const Evaluate* op) override; - void VisitStmt_(const ProducerConsumer* op) override; -+ //Set IfThelElse branch exist Return flg -+ void SetRetTrFlg(bool RetFlg){ -+ if( !br_ret_flg_.empty() ) -+ *(br_ret_flg_.top()) = RetFlg; -+ } - - protected: - /*! \brief The storage information */ -@@ -304,6 +309,12 @@ class CodeGenLLVM : - * initializes file and compilation_unit_ to TVM defaults. - */ - static std::unique_ptr CreateDebugInfo(llvm::Module* module); -+ -+ /* -+ * IfThenElse stmt branch return flg store stack -+ * if a branch already return, can't add br terminator again -+ */ -+ std::stack br_ret_flg_; - }; - } // namespace codegen - } // namespace tvm -diff --git a/src/pass/lower_tvm_builtin.cc b/src/pass/lower_tvm_builtin.cc -index e73956c..3a7b46c 100644 ---- a/src/pass/lower_tvm_builtin.cc -+++ b/src/pass/lower_tvm_builtin.cc -@@ -104,7 +104,7 @@ class BuiltinLower : public IRMutator { - CHECK(device_type_.defined()) << "Unknown device type in current IR"; - CHECK(device_id_.defined()) << "Unknown device id in current IR"; - Stmt throw_last_error = Evaluate::make(Call::make(Int(32), -- intrinsic::tvm_throw_last_error, {}, -+ intrinsic::tvm_throw_last_error, {(Int(32), 1001)}, - Call::Intrinsic)); - - Stmt body = Block::make( -@@ -117,7 +117,7 @@ class BuiltinLower : public IRMutator { - Stmt alloca = LetStmt::make( - op->buffer_var, - Call::make(op->buffer_var.type(), -- "TVMBackendAllocWorkspace", -+ TVM_RT_FUNC_TRANS("TVMBackendAllocWorkspace"), - {cast(Int(32), device_type_), - cast(Int(32), device_id_), - cast(UInt(64), total_bytes), -@@ -127,7 +127,7 @@ class BuiltinLower : public IRMutator { - body); - - Expr free_op = Call::make(Int(32), -- "TVMBackendFreeWorkspace", -+ TVM_RT_FUNC_TRANS("TVMBackendFreeWorkspace"), - {cast(Int(32), device_type_), - cast(Int(32), device_id_), - op->buffer_var},