parent
b79b613acc
commit
a500a57c72
@ -0,0 +1,62 @@
|
||||
/**
|
||||
* 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/graph_kernel_cse.h"
|
||||
|
||||
#include <memory>
|
||||
#include "backend/session/anf_runtime_algorithm.h"
|
||||
#include "runtime/device/kernel_info.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace opt {
|
||||
|
||||
bool GraphKernelBackendCSE::CheckEqualKernelBuildInfo(const AnfNodePtr &main, const AnfNodePtr &node) const {
|
||||
MS_EXCEPTION_IF_NULL(main);
|
||||
MS_EXCEPTION_IF_NULL(node);
|
||||
auto main_kernel_info = dynamic_cast<device::KernelInfo *>(main->kernel_info());
|
||||
auto node_kernel_info = dynamic_cast<device::KernelInfo *>(node->kernel_info());
|
||||
if (main_kernel_info == nullptr && node_kernel_info == nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (main_kernel_info != nullptr && node_kernel_info != nullptr) {
|
||||
auto main_build_info = main_kernel_info->GetMutableSelectKernelBuildInfo();
|
||||
auto node_build_info = node_kernel_info->GetMutableSelectKernelBuildInfo();
|
||||
if (main_build_info == nullptr && node_build_info == nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (main_build_info == nullptr || node_build_info == nullptr) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (main_build_info->fusion_type() != node_build_info->fusion_type() ||
|
||||
main_build_info->processor() != node_build_info->processor()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return main_build_info->IsSimilarityKernelBuildInfo(*node_build_info);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GraphKernelCSE::Run(const FuncGraphPtr &func_graph) {
|
||||
MS_EXCEPTION_IF_NULL(func_graph);
|
||||
auto graphkernel_backend_cse = std::make_shared<GraphKernelBackendCSE>();
|
||||
return graphkernel_backend_cse->Cse(func_graph, func_graph->manager());
|
||||
}
|
||||
} // namespace opt
|
||||
} // namespace mindspore
|
@ -0,0 +1,38 @@
|
||||
/**
|
||||
* 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_CSE_H_
|
||||
#define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CSE_H_
|
||||
|
||||
#include "backend/optimizer/pass/common_subexpression_elimination.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace opt {
|
||||
class GraphKernelCSE : public Pass {
|
||||
public:
|
||||
GraphKernelCSE() : Pass("graph_kernel_cse") {}
|
||||
~GraphKernelCSE() override = default;
|
||||
bool Run(const FuncGraphPtr &func_graph) override;
|
||||
};
|
||||
|
||||
class GraphKernelBackendCSE : public BackendCSE {
|
||||
public:
|
||||
GraphKernelBackendCSE() = default;
|
||||
~GraphKernelBackendCSE() override = default;
|
||||
bool CheckEqualKernelBuildInfo(const AnfNodePtr &main, const AnfNodePtr &node) const override;
|
||||
};
|
||||
} // namespace opt
|
||||
} // namespace mindspore
|
||||
#endif // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_GRAPH_KERNEL_CSE_H_
|
@ -0,0 +1,51 @@
|
||||
# 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.
|
||||
# ============================================================================
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
import mindspore.context as context
|
||||
from mindspore import Tensor
|
||||
from mindspore.nn import Cell
|
||||
import mindspore.ops.operations as P
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
|
||||
|
||||
|
||||
class Net(Cell):
|
||||
def __init__(self):
|
||||
super(Net, self).__init__()
|
||||
self.add = P.TensorAdd()
|
||||
self.mul = P.Mul()
|
||||
|
||||
def construct(self, x):
|
||||
mul_res = self.mul(x, x)
|
||||
square_res = P.Square()(x)
|
||||
return self.add(mul_res, square_res)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_basic():
|
||||
input_x = np.random.normal(0, 1, [2, 3, 4, 3]).astype(np.float32)
|
||||
mul_res = input_x * input_x
|
||||
square_res = np.square(input_x)
|
||||
expect = mul_res + square_res
|
||||
|
||||
net = Net()
|
||||
result = net(Tensor(input_x))
|
||||
|
||||
res = np.allclose(expect, result.asnumpy(), rtol=1.e-4, atol=1.e-7, equal_nan=True)
|
||||
assert res
|
Loading…
Reference in new issue