forked from huawei/mindspore2022
!19993 [GraphKernel] Speed up bert performance in ascend for graph kernel
Merge pull request !19993 from TronZhang/bert_ascend_gk
This commit is contained in:
commit
bca7891a70
|
|
@ -24,6 +24,7 @@ class GraphSplitByPattern:
|
|||
"""Graph splitter"""
|
||||
class ReachTable:
|
||||
"""Reachable table"""
|
||||
|
||||
def __init__(self, size):
|
||||
self.map = []
|
||||
self.alive = set(range(size))
|
||||
|
|
@ -61,6 +62,7 @@ class GraphSplitByPattern:
|
|||
|
||||
class StitchInfo:
|
||||
"""StitchInfo"""
|
||||
|
||||
def __init__(self):
|
||||
self.stitch_ops = set()
|
||||
self.stitch_atomic_ops = set()
|
||||
|
|
@ -441,7 +443,6 @@ class GraphSplitByPattern:
|
|||
self.orig_op_map.update(self.recom_area.ori_op_map)
|
||||
self.recom_area.ori_op_map.clear()
|
||||
|
||||
|
||||
def to_subgraph(self, dom):
|
||||
"""Transform area to subgraphs"""
|
||||
ids = self.index_op()
|
||||
|
|
@ -555,6 +556,7 @@ class GraphSplitByPattern:
|
|||
while do_recompute_fuse():
|
||||
self.pattern_fuse()
|
||||
|
||||
|
||||
use_poly_reduce = True
|
||||
|
||||
|
||||
|
|
@ -830,8 +832,17 @@ class GraphSplitAscend(GraphSplitByPattern):
|
|||
REDUCE_FUSE_DEPTH = 10
|
||||
|
||||
def get_default_mode(self, op):
|
||||
if op.prim == "MatMul" or op.prim == "BatchMatMul":
|
||||
return self.Area.MODE_COMPOSITE if op.inputs[0].dtype == "float16" else self.Area.MODE_BASIC
|
||||
"""Get efault mode for op"""
|
||||
def _dtype_same(tensors):
|
||||
dtype = tensors[0].dtype
|
||||
for tensor_ in tensors:
|
||||
if tensor_.dtype != dtype:
|
||||
return False
|
||||
return True
|
||||
|
||||
if op.prim == "MatMul":
|
||||
if op.inputs[0].dtype == "float16" and not _dtype_same(op.inputs):
|
||||
return self.Area.MODE_COMPOSITE
|
||||
if op.prim in ("Tile", "BroadcastTo", "ExpandDims"):
|
||||
return self.Area.MODE_COMPOSITE
|
||||
return self.Area.MODE_BASIC
|
||||
|
|
@ -937,7 +948,10 @@ class GraphSplitAscend(GraphSplitByPattern):
|
|||
return None
|
||||
fused = []
|
||||
for a, _ in dom.out_relations.items():
|
||||
if a.pattern == PrimLib.ELEMWISE and a.check_acyclic(dom):
|
||||
if (((a.dom_op().prim == "AddN" or a.dom_op().prim == "Add" or a.dom_op().prim == "Cast")
|
||||
and dom.dom_op().prim == "MatMul")
|
||||
or (a.pattern == PrimLib.ELEMWISE and dom.dom_op().prim == "BatchMatMul")) \
|
||||
and a.check_acyclic(dom):
|
||||
fused.append(a)
|
||||
return fused, False
|
||||
|
||||
|
|
|
|||
|
|
@ -454,9 +454,7 @@ void AscendBackendUBFusionOptimization(const std::shared_ptr<session::KernelGrap
|
|||
ub_fusion_pm->AddPass(std::make_shared<ConvSingleInFusionPass>(fusion_id_allocator));
|
||||
ub_fusion_pm->AddPass(std::make_shared<BnupdateEltwiseFusionPass>(fusion_id_allocator));
|
||||
ub_fusion_pm->AddPass(std::make_shared<BnupdateEltwiseEltwiseFusionPass>(fusion_id_allocator));
|
||||
if (!context::GraphKernelFlags::GetInstance().IsEnableGraphKernel()) {
|
||||
ub_fusion_pm->AddPass(std::make_shared<MatmulEltwiseFusionPass>(fusion_id_allocator));
|
||||
}
|
||||
ub_fusion_pm->AddPass(std::make_shared<ConvDoubleInFusionPass>(fusion_id_allocator));
|
||||
ub_fusion_pm->AddPass(std::make_shared<ReduceEltwiseFusionPass>(fusion_id_allocator));
|
||||
ub_fusion_pm->AddPass(std::make_shared<SegmentEltwiseFusionPass>(fusion_id_allocator));
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@
|
|||
#include "debug/anf_ir_dump.h"
|
||||
#include "backend/session/anf_runtime_algorithm.h"
|
||||
#include "base/core_ops.h"
|
||||
#include "utils/context/graph_kernel_flags.h"
|
||||
#include "utils/ms_context.h"
|
||||
#include "backend/optimizer/common/fusion_id_allocator.h"
|
||||
|
||||
|
|
@ -56,6 +57,14 @@ void MatmulEltwiseFusionPass::MatchSingleFusionPattern(const session::KernelGrap
|
|||
continue;
|
||||
}
|
||||
auto cnode = node->cast<CNodePtr>();
|
||||
if (context::GraphKernelFlags::GetInstance().IsEnableGraphKernel()) {
|
||||
if (AnfAlgo::GetKernelType(cnode) == KernelType::TBE_KERNEL &&
|
||||
AnfAlgo::GetFusionType(cnode) == kernel::FusionType::ELEMWISE &&
|
||||
AnfAlgo::CheckPrimitiveType(cnode, prim::kPrimAddN)) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
MS_EXCEPTION_IF_NULL(cnode);
|
||||
if (AnfAlgo::GetKernelType(cnode) == KernelType::TBE_KERNEL &&
|
||||
AnfAlgo::GetFusionType(cnode) == kernel::FusionType::ELEMWISE &&
|
||||
|
|
|
|||
|
|
@ -40,63 +40,63 @@ namespace {
|
|||
std::vector<PrimitivePtr> GetClusterableOpList() {
|
||||
std::vector<PrimitivePtr> clusterable_ops = {
|
||||
prim::kPrimAbs,
|
||||
prim::kPrimRound,
|
||||
prim::kPrimNeg,
|
||||
prim::kPrimExp,
|
||||
prim::kPrimAdd,
|
||||
prim::kPrimCast,
|
||||
prim::kPrimMul,
|
||||
prim::kPrimMinimum,
|
||||
prim::kPrimMaximum,
|
||||
prim::kPrimEqual,
|
||||
prim::kPrimExp,
|
||||
prim::kPrimInplaceAssign,
|
||||
prim::kPrimLog,
|
||||
prim::kPrimMaximum,
|
||||
prim::kPrimMinimum,
|
||||
prim::kPrimMul,
|
||||
prim::kPrimNeg,
|
||||
prim::kPrimPow,
|
||||
prim::kPrimSub,
|
||||
prim::kPrimRealDiv,
|
||||
prim::kPrimReciprocal,
|
||||
prim::kPrimReduceSum,
|
||||
prim::kPrimReshape,
|
||||
prim::kPrimRound,
|
||||
prim::kPrimRsqrt,
|
||||
prim::kPrimSqrt,
|
||||
prim::kPrimReciprocal,
|
||||
prim::kPrimSub,
|
||||
prim::kPrimTanh,
|
||||
prim::kPrimReshape,
|
||||
prim::kPrimTranspose,
|
||||
prim::kPrimRealDiv,
|
||||
prim::kPrimReduceSum,
|
||||
prim::kPrimEqual,
|
||||
prim::kPrimAssign,
|
||||
prim::kPrimInplaceAssign,
|
||||
#if ENABLE_D
|
||||
prim::kPrimMatMul,
|
||||
prim::KPrimTransData,
|
||||
prim::kPrimBatchMatMul,
|
||||
#elif ENABLE_GPU
|
||||
prim::kPrimSin,
|
||||
prim::kPrimCos,
|
||||
prim::kPrimAsin,
|
||||
prim::kPrimACos,
|
||||
prim::kPrimSign,
|
||||
prim::kPrimReduceMax,
|
||||
prim::kPrimReduceMin,
|
||||
prim::kPrimGreater,
|
||||
prim::kPrimLess,
|
||||
prim::kPrimGreaterEqual,
|
||||
prim::kPrimLessEqual,
|
||||
prim::kPrimSelect,
|
||||
prim::kPrimAcosh,
|
||||
prim::kPrimAsin,
|
||||
prim::kPrimAsinh,
|
||||
prim::kPrimAssign,
|
||||
prim::kPrimAtan,
|
||||
prim::kPrimAtan2,
|
||||
prim::kPrimExpm1,
|
||||
prim::kPrimAsinh,
|
||||
prim::kPrimAcosh,
|
||||
prim::kPrimCos,
|
||||
prim::kPrimDiv,
|
||||
prim::kPrimFloorDiv,
|
||||
prim::kPrimMod,
|
||||
prim::kPrimFloor,
|
||||
prim::kPrimFloorMod,
|
||||
prim::kPrimErf,
|
||||
prim::kPrimNotEqual,
|
||||
prim::kPrimExpm1,
|
||||
prim::kPrimFloor,
|
||||
prim::kPrimFloorDiv,
|
||||
prim::kPrimFloorMod,
|
||||
prim::kPrimGreater,
|
||||
prim::kPrimGreaterEqual,
|
||||
prim::kPrimIsFinite,
|
||||
prim::kPrimIsInf,
|
||||
prim::kPrimIsNan,
|
||||
prim::kPrimLess,
|
||||
prim::kPrimLessEqual,
|
||||
prim::kPrimLogicalAnd,
|
||||
prim::kPrimLogicalOr,
|
||||
prim::kPrimLogicalNot,
|
||||
prim::kPrimIsNan,
|
||||
prim::kPrimIsInf,
|
||||
prim::kPrimIsFinite,
|
||||
prim::kPrimMod,
|
||||
prim::kPrimNotEqual,
|
||||
prim::kPrimReduceMax,
|
||||
prim::kPrimReduceMin,
|
||||
prim::kPrimSelect,
|
||||
prim::kPrimSign,
|
||||
prim::kPrimSin,
|
||||
#endif
|
||||
};
|
||||
const auto &flags = context::GraphKernelFlags::GetInstance();
|
||||
|
|
|
|||
|
|
@ -46,44 +46,43 @@ constexpr size_t kLambWeightInputIdx = 4;
|
|||
std::vector<PrimitivePtr> GetExpandOps() {
|
||||
std::vector<PrimitivePtr> expand_ops = {
|
||||
prim::kPrimAddN,
|
||||
prim::kPrimSquare,
|
||||
prim::kPrimGeLUGrad,
|
||||
prim::kPrimAssignAdd,
|
||||
prim::kPrimLayerNorm,
|
||||
prim::kPrimLayerNormGrad,
|
||||
prim::kPrimExpandDims,
|
||||
prim::kPrimBiasAddGrad,
|
||||
prim::kPrimGeLU,
|
||||
prim::kPrimSoftmax,
|
||||
prim::kPrimLogSoftmax,
|
||||
prim::kPrimLogSoftmaxGrad,
|
||||
prim::kPrimTile,
|
||||
prim::kPrimMatMul,
|
||||
prim::kPrimBatchMatMul,
|
||||
prim::kPrimErfc,
|
||||
prim::kPrimExpandDims,
|
||||
prim::kPrimGeLU,
|
||||
prim::kPrimGeLUGrad,
|
||||
prim::kPrimSquare,
|
||||
prim::kPrimTile,
|
||||
#if ENABLE_D
|
||||
prim::kPrimSqrtGrad,
|
||||
prim::kPrimClipByNormNoDivSum,
|
||||
prim::kLambApplyOptimizerAssign,
|
||||
prim::kLambApplyWeightAssign,
|
||||
prim::kPrimClipByNormNoDivSum,
|
||||
prim::kPrimSqrtGrad,
|
||||
prim::kSoftmaxGradExt,
|
||||
prim::kSquareSumV1,
|
||||
prim::kFusedMulAdd,
|
||||
#elif ENABLE_GPU
|
||||
prim::kPrimBatchMatMul,
|
||||
prim::kPrimBiasAdd,
|
||||
prim::kPrimFusedAdam,
|
||||
prim::kPrimFusedAdamWeightDecay,
|
||||
prim::kPrimReduceMean,
|
||||
prim::kPrimMaximumGrad,
|
||||
prim::kPrimMinimumGrad,
|
||||
prim::kPrimBiasAddGrad,
|
||||
prim::kPrimDropout,
|
||||
prim::kPrimDropoutGrad,
|
||||
prim::kPrimFusedAdam,
|
||||
prim::kPrimFusedAdamWeightDecay,
|
||||
prim::kPrimMaximumGrad,
|
||||
prim::kPrimMinimumGrad,
|
||||
prim::kPrimLayerNorm,
|
||||
prim::kPrimLayerNormGrad,
|
||||
prim::kPrimLogSoftmax,
|
||||
prim::kPrimLogSoftmaxGrad,
|
||||
prim::kPrimMatMul,
|
||||
prim::kPrimReduceMean,
|
||||
prim::kPrimRelu,
|
||||
prim::kPrimReluGrad,
|
||||
prim::kPrimSigmoid,
|
||||
prim::kPrimSigmoidGrad,
|
||||
prim::kPrimSigmoidCrossEntropyWithLogits,
|
||||
prim::kPrimSigmoidCrossEntropyWithLogitsGrad,
|
||||
prim::kPrimSoftmax,
|
||||
prim::kPrimSoftmaxCrossEntropyWithLogits,
|
||||
prim::kPrimSquaredDifference,
|
||||
prim::kPrimSqueeze,
|
||||
|
|
|
|||
|
|
@ -61,7 +61,7 @@ PassManagerPtr GraphKernelOptimizer::PreProcess() const {
|
|||
pm->AddPass(std::make_shared<CommonSubexpressionElimination>("cse1"), OptLevel_1);
|
||||
|
||||
// Change Assign(p, a, U) to Assign(Depend(p, U), a)
|
||||
pm->AddPass(std::make_shared<SplitAssign>(), OptLevel_1);
|
||||
pm->AddPass(std::make_shared<SplitAssign>(), OptLevel_1, is_gpu);
|
||||
|
||||
// Spread the MakeTuple input of UpdateState
|
||||
pm->AddPass(std::make_shared<SpreadUpdateState>(), OptLevel_1);
|
||||
|
|
|
|||
|
|
@ -62,6 +62,9 @@ def _set_bert_all_reduce_split():
|
|||
context.set_auto_parallel_context(all_reduce_fusion_config=[30, 90, 150, 210, 270, 330, 390, 421])
|
||||
else:
|
||||
context.set_auto_parallel_context(all_reduce_fusion_config=[38, 93, 148, 203, 258, 313, 368, 397])
|
||||
if device_target == 'Ascend' and enable_graph_kernel and device_num == 8:
|
||||
context.set_auto_parallel_context(all_reduce_fusion_config=[
|
||||
0, 1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 35, 40, 50, 70, 93, 148, 203, 258, 313, 368, 397])
|
||||
|
||||
|
||||
def _get_optimizer(args_opt, network):
|
||||
|
|
|
|||
Loading…
Reference in New Issue