refactor lrn_grad for cpu backend.

This commit is contained in:
z00512249 2022-05-06 07:23:51 +08:00
parent 0828efe02e
commit fe07ad8002
4 changed files with 239 additions and 28 deletions

View File

@ -13,50 +13,63 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "plugin/device/cpu/kernel/lrn_grad_cpu_kernel.h"
#include "plugin/device/cpu/kernel/mkldnn/lrn_grad_cpu_kernel.h"
#include <vector>
#include <algorithm>
#include <utility>
#include <memory>
#include <string>
#include <map>
#include "mindspore/core/ops/grad/lrn_grad.h"
namespace mindspore {
namespace kernel {
void LrnGradCpuKernelMod::GetLrnAttr(const CNodePtr &kernel_node) {
const std::string depth_radius = "depth_radius";
if (!common::AnfAlgo::HasNodeAttr(depth_radius, kernel_node)) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "' has no kernel attribute: " << depth_radius;
bool LrnGradCpuKernelMod::GetLrnGradAttr(const BaseOperatorPtr &base_operator) {
if (kernel_name_ != ops::kNameLRNGrad) {
MS_LOG(ERROR) << "For 'LRNGrad' kernel name get failed, but got " << kernel_name_;
return false;
}
depth_radius_ = common::AnfAlgo::GetNodeAttr<int64_t>(kernel_node, "depth_radius");
const std::string bias = "bias";
if (!common::AnfAlgo::HasNodeAttr(bias, kernel_node)) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "' has no kernel attribute: " << bias;
}
bias_ = common::AnfAlgo::GetNodeAttr<float>(kernel_node, "bias");
const std::string alpha = "alpha";
if (!common::AnfAlgo::HasNodeAttr(alpha, kernel_node)) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "' has no kernel attribute: " << alpha;
}
alpha_ = common::AnfAlgo::GetNodeAttr<float>(kernel_node, "alpha");
const std::string beta = "beta";
if (!common::AnfAlgo::HasNodeAttr(beta, kernel_node)) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "' has no kernel attribute: " << beta;
}
beta_ = common::AnfAlgo::GetNodeAttr<float>(kernel_node, "beta");
auto kernel_ptr = std::make_shared<ops::LRNGrad>(base_operator->GetPrim());
depth_radius_ = kernel_ptr->get_depth_radius();
bias_ = kernel_ptr->get_bias();
alpha_ = kernel_ptr->get_alpha();
beta_ = kernel_ptr->get_beta();
dnnl_algorithm_ = dnnl::algorithm::lrn_across_channels;
return true;
}
void LrnGradCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
kernel_name_ = common::AnfAlgo::GetCNodeName(kernel_node);
auto kernel_attr = GetKernelAttrFromNode(kernel_node);
bool LrnGradCpuKernelMod::Init(const BaseOperatorPtr &base_operator, const std::vector<KernelTensorPtr> &inputs,
const std::vector<KernelTensorPtr> &outputs) {
kernel_name_ = base_operator->name();
if (inputs.empty() || outputs.empty()) {
MS_LOG(ERROR) << "For '" << kernel_name_ << "' got empty inputs or outputs, which is invalid.";
return false;
}
if (!GetLrnGradAttr(base_operator)) {
MS_LOG(ERROR) << "For '" << kernel_name_ << "' got GetReductionAttr failed.";
return false;
}
auto kernel_attr = GetKernelAttrFromTensors(inputs, outputs);
auto [is_match, index] = MatchKernelAttr(kernel_attr, GetOpSupport());
if (!is_match) {
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "' does not support this kernel data type: " << kernel_attr;
MS_LOG(ERROR) << "For '" << kernel_name_ << "' does not support this kernel type: " << kernel_attr;
return false;
}
kernel_func_ = func_list_[index].second;
GetLrnAttr(kernel_node);
input_shape_ = common::AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, kIndex0);
return true;
}
int LrnGradCpuKernelMod::Resize(const BaseOperatorPtr &base_operator, const std::vector<KernelTensorPtr> &inputs,
const std::vector<KernelTensorPtr> &outputs,
const std::map<uint32_t, tensor::TensorPtr> &) {
int ret = KRET_OK;
if ((ret = NativeCpuKernelMod::Resize(base_operator, inputs, outputs)) != KRET_OK) {
return ret;
}
std::vector<size_t> input_shape_;
auto input_shape = inputs.at(kIndex0)->GetShapeVector();
(void)std::transform(input_shape.begin(), input_shape.end(), std::back_inserter(input_shape_), LongToSize);
dnnl::memory::desc src_desc = GetDefaultMemDesc(input_shape_);
const auto lrn_multiple = 2;
dnnl::memory::dim local_size = lrn_multiple * depth_radius_ + 1;
@ -73,6 +86,7 @@ void LrnGradCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
AddArgument(DNNL_ARG_DST, src_desc);
AddArgument(DNNL_ARG_DIFF_SRC, src_desc);
AddArgument(DNNL_ARG_DIFF_DST, src_desc);
return ret;
}
template <typename T>
@ -94,6 +108,7 @@ bool LrnGradCpuKernelMod::LaunchKernel(const std::vector<kernel::AddressPtr> &in
std::vector<std::pair<KernelAttr, LrnGradCpuKernelMod::LrnGradFunc>> LrnGradCpuKernelMod::func_list_ = {
// For kNumberTypeFloat16 input data type will cast to kNumberTypeFloat32 from frontend to backend.
{KernelAttr()
.AddAllSameAttr(true)
.AddInputAttr(kNumberTypeFloat32)
.AddInputAttr(kNumberTypeFloat32)
.AddInputAttr(kNumberTypeFloat32)

View File

@ -0,0 +1,63 @@
/**
* 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_KERNEL_COMPILER_CPU_LRN_GRAD_CPU_KERNEL_H_
#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_LRN_GRAD_CPU_KERNEL_H_
#include <string>
#include <vector>
#include <utility>
#include <map>
#include "plugin/device/cpu/kernel/mkldnn/mkl_cpu_kernel.h"
#include "plugin/factory/ms_factory.h"
namespace mindspore {
namespace kernel {
class LrnGradCpuKernelMod : public MKLCpuKernelMod {
public:
LrnGradCpuKernelMod() = default;
~LrnGradCpuKernelMod() override = default;
bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &,
const std::vector<AddressPtr> &outputs) override {
return kernel_func_(this, inputs, outputs);
}
bool Init(const BaseOperatorPtr &base_operator, const std::vector<KernelTensorPtr> &inputs,
const std::vector<KernelTensorPtr> &outputs) override;
int Resize(const BaseOperatorPtr &base_operator, const std::vector<KernelTensorPtr> &inputs,
const std::vector<KernelTensorPtr> &outputs, const std::map<uint32_t, tensor::TensorPtr> &) override;
protected:
std::vector<KernelAttr> GetOpSupport() override;
private:
template <typename T>
bool LaunchKernel(const std::vector<kernel::AddressPtr> &inputs, const std::vector<kernel::AddressPtr> &outputs);
using LrnGradFunc = std::function<bool(LrnGradCpuKernelMod *, const std::vector<kernel::AddressPtr> &,
const std::vector<kernel::AddressPtr> &)>;
bool GetLrnGradAttr(const BaseOperatorPtr &base_operator);
int64_t depth_radius_{1};
float bias_{0.0};
float alpha_{0.0};
float beta_{0.0};
LrnGradFunc kernel_func_;
dnnl::algorithm dnnl_algorithm_{};
static std::vector<std::pair<KernelAttr, LrnGradFunc>> func_list_;
};
} // namespace kernel
} // namespace mindspore
#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_LRN_GRAD_CPU_KERNEL_H_

View File

@ -0,0 +1,69 @@
/**
* 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 "ops/grad/lrn_grad.h"
#include <string>
#include <algorithm>
#include <memory>
#include "ops/op_utils.h"
#include "utils/check_convert_utils.h"
#include "abstract/ops/primitive_infer_map.h"
#include "mindapi/src/helper.h"
namespace mindspore {
namespace ops {
void LRNGrad::set_depth_radius(const int64_t depth_radius) {
(void)CheckAndConvertUtils::CheckInteger(kDepthRadius, depth_radius, kGreaterEqual, 0, this->name());
(void)this->AddAttr(kDepthRadius, api::MakeValue(depth_radius));
}
int64_t LRNGrad::get_depth_radius() const {
auto value_ptr = GetAttr(kDepthRadius);
return GetValue<int64_t>(value_ptr);
}
void LRNGrad::set_bias(const float bias) { (void)this->AddAttr(kBias, api::MakeValue(bias)); }
float LRNGrad::get_bias() const {
auto value_ptr = GetAttr(kBias);
return GetValue<float>(value_ptr);
}
void LRNGrad::set_alpha(const float alpha) { (void)this->AddAttr(kAlpha, api::MakeValue(alpha)); }
float LRNGrad::get_alpha() const {
auto value_ptr = GetAttr(kAlpha);
return GetValue<float>(value_ptr);
}
void LRNGrad::set_beta(const float beta) { (void)this->AddAttr(kBeta, api::MakeValue(beta)); }
float LRNGrad::get_beta() const {
auto value_ptr = GetAttr(kBeta);
return GetValue<float>(value_ptr);
}
void LRNGrad::Init(const int64_t depth_radius, const float bias, const float alpha, const float beta) {
this->set_depth_radius(depth_radius);
this->set_bias(bias);
this->set_alpha(alpha);
this->set_beta(beta);
}
MIND_API_OPERATOR_IMPL(LRNGrad, BaseOperator);
REGISTER_PRIMITIVE_C(kNameLRNGrad, LRNGrad);
} // namespace ops
} // namespace mindspore

View File

@ -0,0 +1,64 @@
/**
* 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_CORE_OPS_LRN_GRAD_H_
#define MINDSPORE_CORE_OPS_LRN_GRAD_H_
#include <map>
#include <vector>
#include <string>
#include <memory>
#include "ops/base_operator.h"
#include "mindapi/base/types.h"
namespace mindspore {
namespace ops {
constexpr auto kNameLRNGrad = "LRNGrad";
/// \brief Local Response Normalization's Grad. Refer to Python API @ref mindspore.ops.LRNGrad for more details.
class MIND_API LRNGrad : public BaseOperator {
public:
MIND_API_BASE_MEMBER(LRNGrad);
/// \brief Constructor.
LRNGrad() : BaseOperator(kNameLRNGrad) { InitIOName({"x"}, {"y"}); }
/// \brief Init. Refer to the parameters of Python API @ref mindspore.ops.LRNGrad for the inputs.
void Init(const int64_t depth_radius = 5, const float bias = 1.0, const float alpha = 1.0, const float beta = 0.5);
/// \brief Set depth_radius.
void set_depth_radius(const int64_t depth_radius);
/// \brief Set bias.
void set_bias(const float bias);
/// \brief Set alpha.
void set_alpha(const float alpha);
/// \brief Set beta.
void set_beta(const float beta);
/// \brief Get depth_radius.
///
/// \return depth_radius.
int64_t get_depth_radius() const;
/// \brief Get bias.
///
/// \return bias.
float get_bias() const;
/// \brief Get alpha.
///
/// \return alpha.
float get_alpha() const;
/// \brief Get beta.
///
/// \return beta.
float get_beta() const;
};
} // namespace ops
} // namespace mindspore
#endif // MINDSPORE_CORE_OPS_LRN_GRAD_H_