!32082 GraphKernel for Mindpsore-Lite

Merge pull request !32082 from DeshiChen/0328_graph_kernel_lite
This commit is contained in:
i-robot 2022-04-08 08:29:29 +00:00 committed by Gitee
commit 51e35f7faf
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
37 changed files with 163 additions and 262 deletions

View File

@ -2,10 +2,6 @@ if(ENABLE_AKG AND ${CMAKE_SYSTEM_NAME} MATCHES "Linux")
file(GLOB_RECURSE _GK_SRC_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
"*.cc"
)
file(GLOB_RECURSE _GK_LITE_LIST RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}
"lite_adapter/*.cc"
)
list(REMOVE_ITEM _GK_SRC_LIST ${_GK_LITE_LIST})
list(APPEND _GRAPH_KERNEL_SRC_FILES ${_GK_SRC_LIST})
add_library(_mindspore_common_graph_kernel_obj OBJECT ${_GRAPH_KERNEL_SRC_FILES})

View File

@ -47,7 +47,7 @@
#include "common/graph_kernel/tsa_atomic_add_to_first_tensor.h"
#include "common/graph_kernel/uss_atomic_add.h"
#include "backend/common/pass/getitem_tuple.h"
#include "common/graph_kernel/adapter/graph_kernel_pass_manager.h"
#include "common/graph_kernel/core/graph_kernel_pass_manager.h"
#include "common/graph_kernel/transform_op_optimizer.h"
#include "common/graph_kernel/rewrite_output_shape.h"
#include "common/graph_kernel/graph_kernel_recompute.h"

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common/graph_kernel/adapter/graph_kernel_pass_manager.h"
#include "common/graph_kernel/core/graph_kernel_pass_manager.h"
#include <iomanip>

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CORE_GRAPH_KERNEL_PASS_MANAGER_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CORE_GRAPH_KERNEL_PASS_MANAGER_H_
#include <utility>
#include <vector>
@ -46,4 +46,4 @@ class GraphKernelPassManager : public PassManager {
const GraphKernelFlags &flags_;
};
} // namespace mindspore::graphkernel
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CORE_GRAPH_KERNEL_PASS_MANAGER_H_

View File

@ -1,26 +0,0 @@
/**
* Copyright 2022 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_COMMON_GRAPH_KERNEL_LITE_ADAPTER_COMMON_GRAPH_KERNEL_OP_PARAMETER_H_
#define MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_COMMON_GRAPH_KERNEL_OP_PARAMETER_H_
#include "nnacl/op_base.h"
typedef struct GraphKernelParameter {
OpParameter op_parameter_;
void *prim_;
} GraphKernelParameter;
#endif // MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_COMMON_GRAPH_KERNEL_OP_PARAMETER_H_

View File

@ -1,75 +0,0 @@
/**
* Copyright 2022 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_kernel/lite_adapter/graph_kernel_pass_manager.h"
#include <iomanip>
#include "ir/anf.h"
#include "ir/func_graph.h"
#include "ir/manager.h"
#include "include/common/debug/anf_ir_dump.h"
namespace mindspore::graphkernel {
void GraphKernelPassManager::Add(const opt::PassPtr &pass, unsigned int pass_level, bool supported_device) {
MS_EXCEPTION_IF_NULL(pass);
auto pass_name = pass->name();
auto pass_id = passes_.size();
auto pass_in_list = [this, pass_id, &pass_name](const std::vector<std::string> &pass_list) {
// config format can be "stage_id.pass_id" or "stage_name.pass_name"
return std::find(pass_list.begin(), pass_list.end(),
std::to_string(this->stage_) + "." + std::to_string(pass_id)) != pass_list.end() ||
std::find(pass_list.begin(), pass_list.end(), this->name_ + "." + pass_name) != pass_list.end();
};
bool enable = supported_device && flags_.opt_level >= pass_level;
if (enable) {
// if it meets the condition to enable, check whether it's in the disabled pass list.
enable = !pass_in_list(flags_.disable_pass);
} else {
// if it doesn't meet the condition to enable, check whether it's in the enabled pass list.
enable = pass_in_list(flags_.enable_pass);
}
enabled_.push_back(enable);
passes_.push_back(pass);
}
bool GraphKernelPassManager::RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const opt::PassPtr &pass) const {
bool changed = pass->Run(func_graph);
return changed;
}
std::string GraphKernelPassManager::GetPassFullname(size_t pass_id, const opt::PassPtr &pass) const {
return "stage" + std::to_string(stage_) + "_" + name() + "_" + std::to_string(pass_id) + "_" + pass->name();
}
bool GraphKernelPassManager::Run(const FuncGraphPtr &func_graph) const {
bool changed = false;
for (size_t i = 0; i < passes_.size(); i++) {
if (enabled_[i]) {
changed = RunPass(func_graph, i, passes_[i]) || changed;
// dump ir to a graph_kernel subdir, and set a global id in front of the filenames
std::ostringstream oss;
constexpr int id_length = 4;
static int g_id = 0;
oss << "graph_kernel/" << std::setfill('0') << std::setw(id_length) << g_id++ << "_"
<< GetPassFullname(i, passes_[i]);
DumpPassIR(func_graph, oss.str());
} else {
MS_LOG(INFO) << "pass " << GetPassFullname(i, passes_[i]) << " is disabled.";
}
}
return changed;
}
} // namespace mindspore::graphkernel

View File

@ -1,50 +0,0 @@
/**
* Copyright 2022 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_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_
#include <utility>
#include <vector>
#include <string>
#include <memory>
#include "backend/common/optimizer/pass_manager.h"
#include "common/graph_kernel/graph_kernel_flags.h"
namespace mindspore::graphkernel {
using opt::PassManager;
class GraphKernelPassManager : public PassManager {
public:
GraphKernelPassManager(size_t stage, const std::string &name)
: PassManager(name, true), stage_(stage), flags_(GraphKernelFlags::GetInstance()) {}
~GraphKernelPassManager() = default;
// Add graph pass for lite, the pass object will be freed when pass manager freed.
void Add(const opt::PassPtr &pass, unsigned int pass_level, bool default_enable = true);
// Run passes for lite on the func_graph
bool Run(const FuncGraphPtr &func_graph) const override;
protected:
bool RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const opt::PassPtr &pass) const override;
std::string GetPassFullname(size_t pass_id, const opt::PassPtr &pass) const override;
size_t stage_;
std::vector<bool> enabled_;
const GraphKernelFlags &flags_;
};
} // namespace mindspore::graphkernel
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_PASS_MANAGER_H_

View File

@ -497,8 +497,9 @@ enum PrimType {
PrimType_Inner_GltextureToOpencl = 10001,
PrimType_Inner_Identity = 10002,
PrimType_Inner_ShapeFusion = 10003,
PrimType_Inner_GraphKernel = 10004,
PrimType_InnerOpMin = PrimType_Inner_ToFormat,
PrimType_InnerOpMax = PrimType_Inner_ShapeFusion + 1
PrimType_InnerOpMax = PrimType_Inner_GraphKernel + 1
};
typedef enum FormatC {

View File

@ -55,6 +55,7 @@ option(MSLITE_ENABLE_BFC_MEMORY "enable distribute BFC memory" off)
option(MSLITE_ENABLE_PARALLEL_INFERENCE "enable parallel inference interface" off)
option(MSLITE_ENABLE_SHARING_MODEL_WEIGHT "enable sharing model weight" off)
option(MSLITE_ENABLE_EXPERIMENTAL_KERNEL "enable experimental kernel" off)
option(MSLITE_ENABLE_GRAPH_KERNEL "enable graph kernel" off)
#Option that can be configured through manually
option(ENABLE_VERBOSE "" off)
@ -97,6 +98,9 @@ endif()
if(DEFINED ENV{MSLITE_ENABLE_CONVERTER})
set(MSLITE_ENABLE_CONVERTER $ENV{MSLITE_ENABLE_CONVERTER})
endif()
if(DEFINED ENV{ENABLE_AKG})
set(MSLITE_ENABLE_GRAPH_KERNEL $ENV{ENABLE_AKG})
endif()
if(DEFINED ENV{MSLITE_ENABLE_TOOLS})
set(MSLITE_ENABLE_TOOLS $ENV{MSLITE_ENABLE_TOOLS})
endif()
@ -398,6 +402,7 @@ message(STATUS "\tMSLITE_ENABLE_BFC_MEMORY = \t${MSLITE_ENABLE
message(STATUS "\tMSLITE_ENABLE_PARALLEL_INFERENCE = \t${MSLITE_ENABLE_PARALLEL_INFERENCE}")
message(STATUS "\tMSLITE_ENABLE_SHARING_MODEL_WEIGHT = \t${MSLITE_ENABLE_SHARING_MODEL_WEIGHT}")
message(STATUS "\tMSLITE_ENABLE_EXPERIMENT_KERNEL = \t${MSLITE_ENABLE_EXPERIMENT_KERNEL}")
message(STATUS "\tMSLITE_ENABLE_GRAPH_KERNEL = \t${MSLITE_ENABLE_GRAPH_KERNEL}")
if((MSLITE_ENABLE_CONVERTER OR MSLITE_ENABLE_TESTCASES) AND (
NOT MSLITE_ENABLE_MINDRT

View File

@ -334,6 +334,14 @@ else()
)
endif()
if(MSLITE_ENABLE_GRAPH_KERNEL)
file(GLOB_RECURSE GRAPH_KERNEL_SRC
${TOOLS_DIR}/graph_kernel/common/*.cc
${TOOLS_DIR}/graph_kernel/runtime/*.cc
)
set(LITE_SRC ${LITE_SRC} ${GRAPH_KERNEL_SRC})
endif()
add_subdirectory(runtime/kernel/arm)
add_library(lite_src_mid OBJECT ${LITE_SRC})

View File

@ -16,6 +16,7 @@
#include "src/ops/populate/populate_register.h"
#include "src/common/log_adapter.h"
#include "src/tensor.h"
#include "nnacl/custom_parameter.h"
using mindspore::schema::PrimitiveType_Custom;
namespace mindspore {
@ -39,6 +40,17 @@ OpParameter *PopulateCustomParameter(const void *prim) {
memset(param, 0, sizeof(OpParameter));
param->type_ = PrimType_Inner_ShapeFusion;
return reinterpret_cast<OpParameter *>(param);
} else if (type == "GraphKernel") {
auto *param = static_cast<CustomParameter *>(malloc(sizeof(CustomParameter)));
if (param == nullptr) {
MS_LOG(ERROR) << "malloc CustomParameter failed.";
return nullptr;
}
memset(param, 0, sizeof(CustomParameter));
param->op_parameter_.type_ = PrimType_Inner_GraphKernel;
// Just use the attr_data pointer to save the prim directly, the inner value is parsed as necessary.
param->attr_data[0] = static_cast<char *>(const_cast<void *>(prim));
return reinterpret_cast<OpParameter *>(param);
} else {
MS_LOG(ERROR) << "Unsupported custom type: " << type;
}

View File

@ -5,6 +5,7 @@ if(MSLITE_ENABLE_MODEL_ENCRYPTION)
add_compile_definitions(ENABLE_OPENSSL)
endif()
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../../src)
set(TOOLS_DIR ${CMAKE_CURRENT_SOURCE_DIR}/..)
set(CCSRC_SRC
${CCSRC_DIR}/backend/common/optimizer/pattern_engine.cc
@ -12,6 +13,26 @@ set(CCSRC_SRC
${CCSRC_DIR}/backend/common/optimizer/optimizer.cc
)
if(MSLITE_ENABLE_GRAPH_KERNEL)
add_compile_definitions(MSLITE_ENABLE_GRAPH_KERNEL)
file(GLOB_RECURSE GRAPH_KERNEL_SRC
${TOOLS_DIR}/graph_kernel/common/*.cc
${TOOLS_DIR}/graph_kernel/converter/*.cc
${CCSRC_DIR}/common/graph_kernel/core/*.cc
${CCSRC_DIR}/common/graph_kernel/expanders/*.cc
${CCSRC_DIR}/common/graph_kernel/model/*.cc
${CCSRC_DIR}/common/graph_kernel/split_model/*.cc
${CCSRC_DIR}/common/graph_kernel/graph_kernel_flags.cc
${CCSRC_DIR}/kernel/akg/akg_kernel_json_generator.cc
${CCSRC_DIR}/backend/common/pass/getitem_tuple.cc
${CCSRC_DIR}/backend/common/optimizer/const_input_to_attr.cc
)
set(CCSRC_SRC
${CCSRC_SRC}
${GRAPH_KERNEL_SRC}
)
endif()
include_directories(${TOP_DIR}/mindspore/ccsrc/plugin/device/cpu/kernel)
file(GLOB_RECURSE CONVERTER_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}

View File

@ -24,6 +24,8 @@
#include "tools/common/meta_graph_serializer.h"
#include "tools/anf_exporter/anf_exporter.h"
#include "include/version.h"
#include "common/graph_kernel/graph_kernel_flags.h"
#include "tools/graph_kernel/converter/graph_kernel_optimization.h"
#ifdef SUPPORT_TRAIN
#include "src/train/train_populate_parameter.h"
#endif
@ -161,6 +163,12 @@ schema::MetaGraphT *Converter::TransferFuncGraph(const std::unique_ptr<converter
return nullptr;
}
#ifdef MSLITE_ENABLE_GRAPH_KERNEL
if (graphkernel::GraphKernelFlags::GetInstance().IsEnableGraphKernel()) {
graphkernel::GraphKernelOptimize(func_graph);
}
#endif
// protobuf -> flatbuffer
auto meta_graph = Export(func_graph, false, false, flag->trainModel);
if (meta_graph == nullptr) {

View File

@ -0,0 +1,5 @@
approvers:
- gaoxiong1
- ckey_dou
- anyrenwei
- dayschan

View File

@ -23,7 +23,7 @@
#include "src/common/utils.h"
#include "nnacl/infer/common_infer.h"
#include "nnacl/infer/infer_register.h"
#include "common/graph_kernel/lite_adapter/common/graph_kernel_op_parameter.h"
#include "nnacl/custom_parameter.h"
namespace mindspore::graphkernel {
namespace {
@ -112,10 +112,11 @@ int SetOutputsType(TensorC **outputs, size_t outputs_size, const std::string &ou
}
} // namespace
int InferShape(const TensorC **inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
int InferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
auto param = reinterpret_cast<GraphKernelParameter *>(parameter);
auto prim = static_cast<schema::Primitive *>(param->prim_)->value_as_Custom();
// in PopulateCustomParameter, the primitive is store in attr_data[0]
auto param = reinterpret_cast<CustomParameter *>(parameter)->attr_data[0];
auto prim = reinterpret_cast<schema::Primitive *>(param)->value_as_Custom();
for (size_t i = 0; i < prim->attr()->size(); i++) {
auto attr = prim->attr()->Get(i);
if (attr->name()->str() == "outputs_shape") {
@ -142,7 +143,7 @@ int InferShape(const TensorC **inputs, size_t inputs_size, TensorC **outputs, si
#ifdef __cplusplus
extern "C" {
#endif
int GraphKernelInferShape(const TensorC **inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
int GraphKernelInferShape(const TensorC *const *inputs, size_t inputs_size, TensorC **outputs, size_t outputs_size,
OpParameter *parameter) {
return mindspore::graphkernel::InferShape(inputs, inputs_size, outputs, outputs_size, parameter);
}

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "common/graph_kernel/lite_adapter/akg_build.h"
#include "tools/graph_kernel/converter/akg/akg_build.h"
#include <sys/wait.h>
#include <dirent.h>
@ -211,7 +211,7 @@ bool AkgKernelBuilder::CompileJsonsInAnfnodes(const AnfNodePtrList &node_list) {
auto res = CompileJsonsInList(dir_path.value(), json_list);
if (res) {
CheckObjFiles(dir_path.value(), json_list);
auto cmd = "g++ -fPIC -shared -o" + dir_path.value() + "/akgkernels.so " + kernels_name;
auto cmd = "g++ -fPIC -shared -o akgkernels.so " + kernels_name;
if (system(cmd.c_str()) == 0) {
return true;
}

View File

@ -1,5 +1,5 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
* Copyright 2021-2022 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.
@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_AKG_BUILD_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_AKG_BUILD_H_
#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_AKG_BUILD_H_
#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_AKG_BUILD_H_
#include <string>
#include "utils/anf_utils.h"
@ -31,4 +31,4 @@ class AkgKernelBuilder {
bool CompileJsonsInAnfnodes(const AnfNodePtrList &node_list);
};
} // namespace mindspore::graphkernel
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_AKG_BUILD_H_
#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_AKG_BUILD_H_

View File

@ -14,16 +14,18 @@
* limitations under the License.
*/
#include "common/graph_kernel/lite_adapter/build_kernel.h"
#define USE_DEPRECATED_API
#include "tools/graph_kernel/converter/akg/kernel_builder.h"
#include <map>
#include <memory>
#include <string>
#include <vector>
#include "common/graph_kernel/core/graph_kernel_callback.h"
#include "common/graph_kernel/lite_adapter/akg_build.h"
#include "tools/graph_kernel/converter/akg/akg_build.h"
#include "ir/anf.h"
#include "ir/func_graph.h"
#include "ops/primitive_c.h"
#include "ops/custom.h"
#include "utils/anf_utils.h"
#include "utils/log_adapter.h"
@ -48,11 +50,8 @@ AnfNodePtr KernelBuilder::CreateCustomOp(const FuncGraphPtr &func_graph, const C
if (func_graph == nullptr || cnode == nullptr) {
return nullptr;
}
auto primc = std::make_shared<ops::Custom>();
if (primc == nullptr) {
return nullptr;
}
primc->set_type("GraphKernel");
auto op = std::make_shared<ops::Custom>();
op->set_type("GraphKernel");
std::map<std::string, std::vector<uint8_t>> custom_attrs;
auto fg = GetCNodeFuncGraph(cnode);
MS_EXCEPTION_IF_NULL(fg);
@ -82,10 +81,10 @@ AnfNodePtr KernelBuilder::CreateCustomOp(const FuncGraphPtr &func_graph, const C
custom_attrs["outputs_shape"] = std::vector<uint8_t>(output_shape_str.begin(), output_shape_str.end());
custom_attrs["outputs_format"] = std::vector<uint8_t>(output_format_str.begin(), output_format_str.end());
custom_attrs["outputs_type"] = std::vector<uint8_t>(output_type_str.begin(), output_type_str.end());
primc->set_attr(custom_attrs);
op->set_attr(custom_attrs);
auto inputs = cnode->inputs();
inputs.erase(inputs.begin());
auto custom_cnode = func_graph->NewCNode(primc, inputs);
inputs[0] = NewValueNode(op->GetPrim());
auto custom_cnode = func_graph->NewCNode(inputs);
custom_cnode->set_fullname_with_scope(cnode->fullname_with_scope());
custom_cnode->set_abstract(cnode->abstract()->Clone());
return custom_cnode;

View File

@ -14,15 +14,15 @@
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_BUILD_KERNEL_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_BUILD_KERNEL_H_
#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_KERNEL_BUILDER_H_
#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_KERNEL_BUILDER_H_
#include "ir/func_graph.h"
#include "backend/common/optimizer/pass.h"
namespace mindspore::graphkernel {
class KernelBuilder : public opt::Pass {
public:
KernelBuilder() : Pass("build_kernel_lite") {}
KernelBuilder() : Pass("akg_kernel_builder") {}
~KernelBuilder() override = default;
AnfNodePtr CreateCustomOp(const FuncGraphPtr &func_graph, const CNodePtr &cnode);
@ -30,4 +30,4 @@ class KernelBuilder : public opt::Pass {
bool Run(const FuncGraphPtr &func_graph) override;
};
} // namespace mindspore::graphkernel
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_BUILD_KERNEL_H_
#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_KERNEL_BUILDER_H_

View File

@ -1,5 +1,5 @@
/**
* Copyright 2021 Huawei Technologies Co., Ltd
* Copyright 2021-2022 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.
@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "common/graph_kernel/lite_adapter/callback_impl.h"
#include "tools/graph_kernel/converter/callback_impl.h"
#include <algorithm>
#include <string>

View File

@ -14,8 +14,8 @@
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CALLBACK_IMPL_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CALLBACK_IMPL_H_
#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CALLBACK_IMPL_H_
#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CALLBACK_IMPL_H_
#include <string>
#include <vector>
#include <utility>
@ -43,4 +43,4 @@ class CallbackImpl : public Callback {
void ResetKernelInfo(const AnfNodePtr &node) override;
};
} // namespace mindspore::graphkernel
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CALLBACK_IMPL_H_
#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CALLBACK_IMPL_H_

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common/graph_kernel/lite_adapter/convert_const_input_to_attr.h"
#include "tools/graph_kernel/converter/convert_const_input_to_attr.h"
#include "backend/common/optimizer/const_input_to_attr.h"
#include "utils/anf_utils.h"

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CONVERT_CONST_INPUT_TO_ATTR_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CONVERT_CONST_INPUT_TO_ATTR_H_
#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CONVERT_CONST_INPUT_TO_ATTR_H_
#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CONVERT_CONST_INPUT_TO_ATTR_H_
#include "ir/func_graph.h"
#include "backend/common/optimizer/pass.h"
@ -27,4 +27,4 @@ class ConvertConstInputToAttr : public opt::Pass {
};
} // namespace mindspore::graphkernel
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_CONVERT_CONST_INPUT_TO_ATTR_H_
#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_CONVERT_CONST_INPUT_TO_ATTR_H_

View File

@ -19,7 +19,7 @@
#include <string>
#include "common/graph_kernel/expanders/expander_factory.h"
#include "common/graph_kernel/lite_adapter/expanders/activation.h"
#include "tools/graph_kernel/converter/expanders/activation.h"
#include "mindapi/base/types.h"
#include "ir/dtype.h"

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_EXPANDERS_ACTIVATION_H_
#define MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_EXPANDERS_ACTIVATION_H_
#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_EXPANDERS_ACTIVATION_H_
#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_EXPANDERS_ACTIVATION_H_
#include <memory>
#include <set>
@ -45,4 +45,4 @@ class CheckActivationType : public Validator {
NodePtr GetActivationExpander(const inner::GraphBuilder &gb, const NodePtrList &inputs, int64_t activation_type);
} // namespace mindspore::graphkernel::expanders
#endif // MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_EXPANDERS_ACTIVATION_H_
#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_EXPANDERS_ACTIVATION_H_

View File

@ -18,7 +18,7 @@
#include <set>
#include "common/graph_kernel/expanders/expander_factory.h"
#include "common/graph_kernel/lite_adapter/expanders/activation.h"
#include "tools/graph_kernel/converter/expanders/activation.h"
#include "mindapi/base/types.h"
#include "ir/dtype.h"

View File

@ -18,7 +18,7 @@
#include <set>
#include "common/graph_kernel/expanders/expander_factory.h"
#include "common/graph_kernel/lite_adapter/expanders/activation.h"
#include "tools/graph_kernel/converter/expanders/activation.h"
#include "mindapi/base/types.h"
#include "ir/dtype.h"

View File

@ -18,7 +18,7 @@
#include <set>
#include "common/graph_kernel/expanders/expander_factory.h"
#include "common/graph_kernel/lite_adapter/expanders/activation.h"
#include "tools/graph_kernel/converter/expanders/activation.h"
#include "mindapi/base/types.h"
#include "ir/dtype.h"

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "common/graph_kernel/lite_adapter/graph_kernel_expander_lite.h"
#include "tools/graph_kernel/converter/graph_kernel_expander_lite.h"
#include <utility>
#include <vector>

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_EXPANDER_LITE_H_
#define MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_EXPANDER_LITE_H_
#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_GRAPH_KERNEL_EXPANDER_LITE_H_
#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_GRAPH_KERNEL_EXPANDER_LITE_H_
#include <memory>
#include <vector>
@ -43,4 +43,4 @@ class GraphKernelExpanderLite : public GraphKernelExpander {
ExpanderPtr GetExpander(const AnfNodePtr &node) override;
};
} // namespace mindspore::graphkernel
#endif // MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_EXPANDER_LITE_H_
#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_GRAPH_KERNEL_EXPANDER_LITE_H_

View File

@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "common/graph_kernel/lite_adapter/graph_kernel_optimization.h"
#include "tools/graph_kernel/converter/graph_kernel_optimization.h"
#include <vector>
#include <string>
@ -27,10 +27,11 @@
#include "common/graph_kernel/core/eliminate_redundant_output.h"
#include "common/graph_kernel/core/shape_ops_splitter.h"
#include "common/graph_kernel/core/update_state_formatter.h"
#include "common/graph_kernel/lite_adapter/build_kernel.h"
#include "common/graph_kernel/lite_adapter/convert_const_input_to_attr.h"
#include "common/graph_kernel/lite_adapter/graph_kernel_expander_lite.h"
#include "common/graph_kernel/lite_adapter/graph_kernel_pass_manager.h"
#include "common/graph_kernel/core/graph_kernel_pass_manager.h"
#include "tools/graph_kernel/converter/akg/kernel_builder.h"
#include "tools/graph_kernel/converter/convert_const_input_to_attr.h"
#include "tools/graph_kernel/converter/graph_kernel_expander_lite.h"
namespace mindspore::graphkernel {
using opt::GetitemTuple;
@ -72,8 +73,8 @@ PassManagerPtr GraphKernelOptimizer::Split() const {
return pm;
}
PassManagerPtr GraphKernelOptimizer::PostProcess() const {
auto pm = std::make_shared<GraphKernelPassManager>(2, "postprocess");
PassManagerPtr GraphKernelOptimizer::BuildKernel() const {
auto pm = std::make_shared<GraphKernelPassManager>(2, "buildkernel");
// build akg and replace graph kernel nodes
pm->Add(std::make_shared<KernelBuilder>(), OptLevel_1);
return pm;
@ -83,7 +84,7 @@ void GraphKernelOptimizer::Run(const FuncGraphPtr &kernel_graph) {
auto optimizer = std::make_shared<GraphOptimizer>("graph_kernel_optimizer");
optimizer->AddPassManager(Cluster());
optimizer->AddPassManager(Split());
optimizer->AddPassManager(PostProcess());
optimizer->AddPassManager(BuildKernel());
auto mng = kernel_graph->manager();
if (mng == nullptr) {

View File

@ -13,8 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_OPTIMIZATION_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_OPTIMIZATION_H_
#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_GRAPH_KERNEL_OPTIMIZATION_H_
#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_GRAPH_KERNEL_OPTIMIZATION_H_
#include "ir/anf.h"
#include "ir/func_graph.h"
@ -33,9 +33,9 @@ class GraphKernelOptimizer {
// Split kernels
PassManagerPtr Split() const;
// Post-process
PassManagerPtr PostProcess() const;
PassManagerPtr BuildKernel() const;
};
void GraphKernelOptimize(const FuncGraphPtr &kernel_graph);
} // namespace mindspore::graphkernel
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_LITE_ADAPTER_GRAPH_KERNEL_OPTIMIZATION_H_
#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_CONVERTER_GRAPH_KERNEL_OPTIMIZATION_H_

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
#include "common/graph_kernel/lite_adapter/runtime/akg_kernel.h"
#include "tools/graph_kernel/runtime/akg_kernel.h"
#include <dlfcn.h>
#include <algorithm>
#include "src/tensor.h"
@ -60,7 +60,7 @@ class AkgCallBack {
} // namespace
void AkgKernel::ExtractKernelName() {
auto prim = static_cast<schema::Primitive *>(params_->prim_)->value_as_Custom();
auto prim = static_cast<schema::Primitive *>(params_)->value_as_Custom();
for (size_t i = 0; i < prim->attr()->size(); i++) {
auto attr = prim->attr()->Get(i);
if (attr->name()->str() == "kernel_name") {

View File

@ -14,12 +14,12 @@
* limitations under the License.
*/
#ifndef MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_RUNTIME_AKG_KERNEL_H_
#define MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_RUNTIME_AKG_KERNEL_H_
#ifndef MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_RUNTIME_AKG_KERNEL_H_
#define MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_RUNTIME_AKG_KERNEL_H_
#include <vector>
#include <string>
#include "src/lite_kernel.h"
#include "common/graph_kernel/lite_adapter/common/graph_kernel_op_parameter.h"
#include "nnacl/custom_parameter.h"
namespace mindspore::kernel {
using AkgParallelLambda = int (*)(int task_id, int num_task, void *cdata);
@ -29,7 +29,8 @@ class AkgKernel : public LiteKernel {
AkgKernel(OpParameter *parameter, const std::vector<lite::Tensor *> &inputs,
const std::vector<lite::Tensor *> &outputs, const lite::InnerContext *ctx)
: LiteKernel(parameter, inputs, outputs, ctx) {
params_ = reinterpret_cast<GraphKernelParameter *>(op_parameter_);
// in PopulateCustomParameter, the primitive is store in attr_data[0]
params_ = static_cast<void *>(reinterpret_cast<CustomParameter *>(op_parameter_)->attr_data[0]);
ExtractKernelName();
}
~AkgKernel() override;
@ -49,7 +50,7 @@ class AkgKernel : public LiteKernel {
protected:
void ExtractKernelName();
GraphKernelParameter *params_{nullptr};
void *params_{nullptr};
void *handle_{nullptr};
void *kernel_func_{nullptr};
std::string kernel_name_;
@ -58,4 +59,4 @@ class AkgKernel : public LiteKernel {
void *cached_runtimeargs_ = nullptr;
};
} // namespace mindspore::kernel
#endif // MINDSPORE_CCSRC_COMMON_GRAPH_KERNEL_LITE_ADAPTER_RUNTIME_AKG_KERNEL_H_
#endif // MINDSPORE_LITE_TOOLS_GRAPH_KERNEL_RUNTIME_AKG_KERNEL_H_

View File

@ -35,11 +35,35 @@ void PassManager::AddPass(const PassPtr &pass) {
}
}
// not implement for lite, just for api compatible
bool PassManager::RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const PassPtr &pass) const { return false; }
bool PassManager::RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const PassPtr &pass) const {
bool changed = false;
#if defined(_WIN32) || defined(_WIN64)
auto start_time = std::chrono::steady_clock::now();
#else
struct timeval start_time {};
struct timeval end_time {};
(void)gettimeofday(&start_time, nullptr);
#endif
if (pass->Run(func_graph)) {
MS_LOG(DEBUG) << "Run pass and find change";
changed = true;
}
#if defined(_WIN32) || defined(_WIN64)
auto end_time = std::chrono::steady_clock::now();
std::chrono::duration<double, std::ratio<1, kUSecondInSecond>> cost = end_time - start_time;
MS_LOG(INFO) << "Run pass " << GetPassFullname(pass_id, pass) << " in " << cost.count() << " us.";
#else
(void)gettimeofday(&end_time, nullptr);
uint64_t cost = kUSecondInSecond * static_cast<uint64_t>(end_time.tv_sec - start_time.tv_sec);
cost += static_cast<uint64_t>(end_time.tv_usec - start_time.tv_usec);
MS_LOG(INFO) << "Run pass " << GetPassFullname(pass_id, pass) << " in " << cost << " us.";
#endif
return changed;
}
// not implement for lite, just for api compatible
std::string PassManager::GetPassFullname(size_t pass_id, const PassPtr &pass) const { return ""; }
std::string PassManager::GetPassFullname(size_t pass_id, const PassPtr &pass) const {
return "hwopt_" + name() + "_" + std::to_string(pass_id) + "_" + pass->name();
}
// not implement for lite, just for api compatible
void PassManager::DumpPassIR(const FuncGraphPtr &func_graph, const std::string &pass_fullname) const {}
@ -52,31 +76,11 @@ bool PassManager::Run(const FuncGraphPtr &func_graph, const std::vector<PassPtr>
size_t num = 0;
for (const auto &pass : passes) {
if (pass != nullptr) {
#if defined(_WIN32) || defined(_WIN64)
auto start_time = std::chrono::steady_clock::now();
#else
struct timeval start_time {};
struct timeval end_time {};
(void)gettimeofday(&start_time, nullptr);
#endif
if (pass->Run(func_graph)) {
MS_LOG(DEBUG) << "Run pass and find change";
changed = true;
}
#if defined(_WIN32) || defined(_WIN64)
auto end_time = std::chrono::steady_clock::now();
std::chrono::duration<double, std::ratio<1, kUSecondInSecond>> cost = end_time - start_time;
MS_LOG(INFO) << "Run pass hwopt_" + name() + "_" << num << "_" + pass->name() + " in " << cost.count() << " us";
#else
(void)gettimeofday(&end_time, nullptr);
uint64_t cost = kUSecondInSecond * static_cast<uint64_t>(end_time.tv_sec - start_time.tv_sec);
cost += static_cast<uint64_t>(end_time.tv_usec - start_time.tv_usec);
MS_LOG(INFO) << "Run pass hwopt_" + name() + "_" << num << "_" + pass->name() + " in " << cost << " us";
#endif
num++;
changed = RunPass(func_graph, num, pass) || changed;
} else {
MS_LOG(INFO) << "pass is null";
}
num++;
}
return changed;
}

View File

@ -220,16 +220,6 @@ list(REMOVE_ITEM MINDSPORE_SRC_LIST
list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/plugin/device/gpu/optimizer/batch_norm_relu_fusion.cc")
list(REMOVE_ITEM MINDSPORE_SRC_LIST
"../../../mindspore/ccsrc/plugin/device/gpu/optimizer/batch_norm_relu_grad_fusion.cc")
list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/akg_build.cc")
list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/build_kernel.cc")
list(REMOVE_ITEM MINDSPORE_SRC_LIST
"../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/callback_impl.cc")
list(REMOVE_ITEM MINDSPORE_SRC_LIST
"../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_optimization.cc")
list(REMOVE_ITEM MINDSPORE_SRC_LIST
"../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/graph_kernel_pass_manager.cc")
list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/runtime/akg_kernel.cc")
list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/common/graph_kernel/lite_adapter/common/infer_shape.cc")
list(REMOVE_ITEM MINDSPORE_SRC_LIST "../../../mindspore/ccsrc/plugin/device/ascend/kernel/tbe/tbe_kernel_compile.cc")
list(REMOVE_ITEM MINDSPORE_SRC_LIST
"../../../mindspore/ccsrc/plugin/device/ascend/kernel/tbe/tiling/op_tiling_adapter.cc")