Browse Source

!15071 [GraphKernel] Make code meek the criterion.

From: @tronzhang
Reviewed-by: @ckey_dou,@gaoxiong1
Signed-off-by: @gaoxiong1
pull/15071/MERGE
mindspore-ci-bot Gitee 4 years ago
parent
commit
4686e734aa
5 changed files with 4 additions and 159 deletions
  1. +3
    -1
      mindspore/ccsrc/backend/optimizer/graph_kernel/add_atomic_clean.cc
  2. +0
    -123
      mindspore/ccsrc/backend/optimizer/graph_kernel/clean_all_in_once.cc
  3. +0
    -34
      mindspore/ccsrc/backend/optimizer/graph_kernel/clean_all_in_once.h
  4. +0
    -1
      mindspore/ccsrc/backend/optimizer/graph_kernel/graph_kernel_optimization.cc
  5. +1
    -0
      mindspore/ccsrc/backend/optimizer/graph_kernel/parallel_cost_model.h

+ 3
- 1
mindspore/ccsrc/backend/optimizer/graph_kernel/add_atomic_clean.cc View File

@@ -411,13 +411,15 @@ CNodePtr AtomicCleanInsertter::CreateAtomicCleanCompositeNode(const KernelGraphP
// Create composite op's sub-graph.
auto new_sub_graph = std::make_shared<FuncGraph>();

AnfNodePtr broadcast_input_node = value_node;
AnfNodePtr broadcast_input_node;
if (dst_type == kNumberTypeFloat16) {
AnfNodePtrList cast_inputs = {NewValueNode(prim::kPrimCast), value_node};
auto cast_node_inner =
CreateCNode(cast_inputs, new_sub_graph, {.format = format, .shape = {1}, .type = TypeIdToType(dst_type)});
SetNodeAttrSafely("dst_type", MakeValue("float32"), cast_node_inner);
broadcast_input_node = cast_node_inner;
} else {
broadcast_input_node = value_node;
}

// Create broadcast basic op.


+ 0
- 123
mindspore/ccsrc/backend/optimizer/graph_kernel/clean_all_in_once.cc View File

@@ -1,123 +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 "backend/optimizer/graph_kernel/clean_all_in_once.h"
#include <algorithm>
#include <map>
#include <string>
#include <utility>
#include <vector>
#include "backend/session/anf_runtime_algorithm.h"
#include "backend/kernel_compiler/common_utils.h"
#include "backend/optimizer/graph_kernel/graph_kernel_helper.h"

namespace mindspore {
namespace opt {
namespace {
ShapeVector GetValidShape(const AnfNodePtr &node) {
// Shape will not contain 1 in head.
auto shape = GetShape(node);
ShapeVector valid_shape;
bool valid = false;
for (auto s : shape) {
if (!valid && s == 1) {
continue;
}
valid = true;
valid_shape.push_back(s);
}
return valid_shape;
}

bool IsAtomicCleanNode(const AnfNodePtr &node) {
MS_EXCEPTION_IF_NULL(node);
auto cnode = node->cast<CNodePtr>();
MS_EXCEPTION_IF_NULL(cnode);
auto func_graph = GetValueNode<FuncGraphPtr>(cnode->input(kAnfPrimitiveIndex));
MS_EXCEPTION_IF_NULL(func_graph);
if (!func_graph->has_attr("composite_type")) {
return false;
}

auto ctype_value = func_graph->get_attr("composite_type");
if (!ctype_value->isa<StringImm>()) {
MS_LOG(EXCEPTION) << "Attribute composite_type should be a string!";
}
auto ctype = GetValue<std::string>(ctype_value);
return ctype == "atomic_clean";
}

std::vector<AnfNodePtrList> SplitVectorByWidth(const AnfNodePtrList &nodes, int width) {
std::vector<AnfNodePtrList> splitted_nodes;
if (nodes.empty()) {
return splitted_nodes;
}

int num = (nodes.size() - 1) / width + 1;
splitted_nodes.resize(num);
for (size_t i = 0; i < nodes.size(); ++i) {
splitted_nodes[i / width].push_back(nodes[i]);
}
return splitted_nodes;
}
} // namespace

bool CleanAllInOnce::Run(const FuncGraphPtr &func_graph) {
MS_EXCEPTION_IF_NULL(func_graph);
auto mng = func_graph->manager();
if (mng == nullptr) {
mng = Manage(func_graph, true);
func_graph->set_manager(mng);
}
auto todos = TopoSort(func_graph->get_return());
std::map<ShapeVector, AnfNodePtrList> clean_map;
std::for_each(todos.cbegin(), todos.cend(), [&clean_map](const AnfNodePtr &node) {
if (AnfAlgo::IsGraphKernel(node) && IsAtomicCleanNode(node)) {
auto valid_shape = GetValidShape(node);
auto iter = clean_map.find(valid_shape);
if (iter != clean_map.end()) {
iter->second.push_back(node);
} else {
clean_map.insert({valid_shape, {node}});
}
}
});

bool changed = false;
if (!clean_map.empty()) {
for (auto iter : clean_map) {
// Do all in once is not good, so do ten in once.
auto splitted_nodes = SplitVectorByWidth(iter.second, 10);
for (auto &snodes : splitted_nodes) {
if (snodes.size() < 2) {
continue;
}
AnfNodePtr clean_all_node;
std::tie(clean_all_node, std::ignore) = FuseNodesToSubGraph(snodes, func_graph, "clean_all");
MS_LOG(INFO) << "Add node to clean batch buffers in once(" << clean_all_node->fullname_with_scope()
<< ") for atomic add!";
changed = true;
}
}
}

if (changed) {
mng->RemoveRoots();
mng->KeepRoots({func_graph});
}
return changed;
}
} // namespace opt
} // namespace mindspore

+ 0
- 34
mindspore/ccsrc/backend/optimizer/graph_kernel/clean_all_in_once.h View File

@@ -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.
*/
#ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CLEAN_ALL_IN_ONCE_H_
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CLEAN_ALL_IN_ONCE_H_
#include <map>
#include <memory>
#include "backend/optimizer/common/pass.h"
#include "ir/func_graph.h"

namespace mindspore {
namespace opt {
class CleanAllInOnce : public Pass {
public:
CleanAllInOnce() : Pass("clean_all_in_once") {}
~CleanAllInOnce() override = default;
bool Run(const FuncGraphPtr &func_graph);
};
using CleanAllInOncePtr = std::shared_ptr<CleanAllInOnce>;
} // namespace opt
} // namespace mindspore
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CLEAN_ALL_IN_ONCE_H_

+ 0
- 1
mindspore/ccsrc/backend/optimizer/graph_kernel/graph_kernel_optimization.cc View File

@@ -25,7 +25,6 @@
#include "backend/optimizer/graph_kernel/add_stitch_atomic_clean_gpu.h"
#include "backend/optimizer/graph_kernel/arithmetic_simplify.h"
#include "backend/optimizer/graph_kernel/basic_ops_fusion.h"
#include "backend/optimizer/graph_kernel/clean_all_in_once.h"
#include "backend/optimizer/graph_kernel/eliminate_redundant_output.h"
#include "backend/optimizer/graph_kernel/tensor_promotion.h"
#include "backend/optimizer/graph_kernel/graph_kernel_splitter.h"


+ 1
- 0
mindspore/ccsrc/backend/optimizer/graph_kernel/parallel_cost_model.h View File

@@ -114,6 +114,7 @@ class ParellelCostModelWarehouse {

private:
ParellelCostModelWarehouse() { cost_model_ = std::make_shared<ParallelCostModel>(); }
~ParellelCostModelWarehouse() = default;
ParallelCostModelPtr cost_model_;
};
} // namespace opt


Loading…
Cancel
Save