forked from mindspore-Ecosystem/mindspore
!33177 add lpNorm implement
Merge pull request !33177 from zhuzhongrui/pub_master3
This commit is contained in:
commit
fc7f6a384c
|
@ -0,0 +1,111 @@
|
|||
/**
|
||||
* 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 "plugin/device/cpu/kernel/mkldnn/reduction_cpu_kernel.h"
|
||||
#include <map>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <algorithm>
|
||||
#include "utils/ms_utils.h"
|
||||
#include "plugin/device/cpu/hal/device/cpu_device_address.h"
|
||||
#include "mindspore/core/ops/lp_norm.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace kernel {
|
||||
namespace {
|
||||
struct ReductionDescParam {
|
||||
dnnl::algorithm algorithm{dnnl::algorithm::undef};
|
||||
float p_{2.0f};
|
||||
float eps_{0.0f};
|
||||
};
|
||||
} // namespace
|
||||
|
||||
dnnl::reduction::desc ReductionCpuKernelMod::GetReductionDesc(const dnnl::memory::desc &src_desc,
|
||||
const dnnl::memory::desc &dst_desc) {
|
||||
static const std::map<std::string, ReductionDescParam> reduction_op_desc_map{
|
||||
{prim::kPrimLpNorm->name(), ReductionDescParam{dnnl::algorithm::reduction_norm_lp_sum, p_, eps_}}};
|
||||
const auto desc_pair = reduction_op_desc_map.find(kernel_name_);
|
||||
if (desc_pair == reduction_op_desc_map.end()) {
|
||||
MS_LOG(EXCEPTION) << "ReductionCpuKernelMod does not support " << kernel_name_;
|
||||
}
|
||||
auto desc = CreateDesc<dnnl::reduction::desc>(desc_pair->second.algorithm, src_desc, dst_desc, desc_pair->second.p_,
|
||||
desc_pair->second.eps_);
|
||||
return desc;
|
||||
}
|
||||
|
||||
void ReductionCpuKernelMod::InitKernel(const CNodePtr &kernel_node) {
|
||||
kernel_name_ = common::AnfAlgo::GetCNodeName(kernel_node);
|
||||
const std::string p = "p";
|
||||
if (!common::AnfAlgo::HasNodeAttr(p, kernel_node)) {
|
||||
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "' has no kernel attribute: " << p;
|
||||
}
|
||||
p_ = LongToFloat(common::AnfAlgo::GetNodeAttr<int64_t>(kernel_node, p));
|
||||
const std::string eps = "epsilon";
|
||||
if (!common::AnfAlgo::HasNodeAttr(eps, kernel_node)) {
|
||||
MS_LOG(EXCEPTION) << "For '" << kernel_name_ << "' has no kernel attribute: " << eps;
|
||||
}
|
||||
eps_ = common::AnfAlgo::GetNodeAttr<float>(kernel_node, eps);
|
||||
auto kernel_attr = GetKernelAttrFromNode(kernel_node);
|
||||
auto [is_match, index] = MatchKernelAttr(kernel_attr, GetOpSupport());
|
||||
if (!is_match) {
|
||||
MS_LOG(EXCEPTION) << kernel_name_ << " does not support this kernel data type: " << kernel_attr;
|
||||
}
|
||||
kernel_func_ = func_list_[index].second;
|
||||
std::vector<size_t> input_shape = common::AnfAlgo::GetPrevNodeOutputInferShape(kernel_node, kIndex0);
|
||||
std::vector<size_t> output_shape = common::AnfAlgo::GetOutputInferShape(kernel_node, kIndex0);
|
||||
// For Reduction kernel required at least 4d data shape, extend it to 4d.
|
||||
while (input_shape.size() < kIndex4) {
|
||||
input_shape.insert(input_shape.begin(), 1);
|
||||
}
|
||||
while (output_shape.size() < kIndex4) {
|
||||
output_shape.insert(output_shape.begin(), 1);
|
||||
}
|
||||
dnnl::memory::desc src_desc = GetDefaultMemDesc(input_shape);
|
||||
dnnl::memory::desc dst_desc = GetDefaultMemDesc(output_shape);
|
||||
auto desc = GetReductionDesc(src_desc, dst_desc);
|
||||
auto prim_desc = CreateDesc<dnnl::reduction::primitive_desc>(desc, engine_);
|
||||
primitive_ = CreatePrimitive<dnnl::reduction>(prim_desc);
|
||||
AddArgument(DNNL_ARG_SRC, src_desc);
|
||||
AddArgument(DNNL_ARG_DST, dst_desc);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool ReductionCpuKernelMod::LaunchKernel(const std::vector<kernel::AddressPtr> &inputs,
|
||||
const std::vector<kernel::AddressPtr> &outputs) {
|
||||
auto input = reinterpret_cast<T *>(inputs.at(kIndex0)->addr);
|
||||
auto output = reinterpret_cast<T *>(outputs.at(kIndex0)->addr);
|
||||
SetArgumentHandle(DNNL_ARG_SRC, input);
|
||||
SetArgumentHandle(DNNL_ARG_DST, output);
|
||||
ExecutePrimitive();
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<std::pair<KernelAttr, ReductionCpuKernelMod::ReductionFunc>> ReductionCpuKernelMod::func_list_ = {
|
||||
{KernelAttr().AddInputAttr(kNumberTypeFloat32).AddOutputAttr(kNumberTypeFloat32),
|
||||
&ReductionCpuKernelMod::LaunchKernel<float>}};
|
||||
|
||||
std::vector<KernelAttr> ReductionCpuKernelMod::GetOpSupport() {
|
||||
std::vector<KernelAttr> support_list;
|
||||
(void)std::transform(func_list_.begin(), func_list_.end(), std::back_inserter(support_list),
|
||||
[](const std::pair<KernelAttr, ReductionFunc> &pair) { return pair.first; });
|
||||
return support_list;
|
||||
}
|
||||
|
||||
MS_KERNEL_FACTORY_REG_BY_CREATOR(NativeCpuKernelMod, LpNorm,
|
||||
[]() { return std::make_shared<ReductionCpuKernelMod>(prim::kPrimLpNorm->name()); });
|
||||
} // namespace kernel
|
||||
} // namespace mindspore
|
|
@ -0,0 +1,61 @@
|
|||
/**
|
||||
* 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_REDUCTION_CPU_KERNEL_H_
|
||||
#define MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_REDUCTION_CPU_KERNEL_H_
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include "plugin/device/cpu/kernel/mkldnn/mkl_cpu_kernel.h"
|
||||
|
||||
namespace mindspore {
|
||||
namespace kernel {
|
||||
class ReductionCpuKernelMod : public MKLCpuKernelMod {
|
||||
public:
|
||||
ReductionCpuKernelMod() = default;
|
||||
explicit ReductionCpuKernelMod(const std::string &kernel_type) : kernel_type_(kernel_type) {}
|
||||
~ReductionCpuKernelMod() override = default;
|
||||
|
||||
// TO be Deprecated API.
|
||||
void InitKernel(const CNodePtr &kernel_node) override;
|
||||
|
||||
bool Launch(const std::vector<AddressPtr> &inputs, const std::vector<AddressPtr> &workspace,
|
||||
const std::vector<AddressPtr> &outputs) override {
|
||||
return kernel_func_(this, inputs, outputs);
|
||||
}
|
||||
|
||||
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 ReductionFunc = std::function<bool(ReductionCpuKernelMod *, const std::vector<kernel::AddressPtr> &,
|
||||
const std::vector<kernel::AddressPtr> &)>;
|
||||
dnnl::reduction::desc GetReductionDesc(const dnnl::memory::desc &src_desc, const dnnl::memory::desc &dst_desc);
|
||||
|
||||
ReductionFunc kernel_func_;
|
||||
float p_{2.0};
|
||||
float eps_{1e-12};
|
||||
static std::vector<std::pair<KernelAttr, ReductionFunc>> func_list_;
|
||||
std::string kernel_type_{};
|
||||
};
|
||||
} // namespace kernel
|
||||
} // namespace mindspore
|
||||
|
||||
#endif // MINDSPORE_CCSRC_BACKEND_KERNEL_COMPILER_CPU_REDUCTION_CPU_KERNEL_H_
|
|
@ -14,7 +14,6 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
|
|
@ -101,7 +101,7 @@ inline float SizeToFloat(size_t v) { return static_cast<float>(v); }
|
|||
|
||||
inline double LongToDouble(int64_t v) { return static_cast<double>(v); }
|
||||
|
||||
inline double LongToFloat(int64_t v) { return static_cast<float>(v); }
|
||||
inline float LongToFloat(int64_t v) { return static_cast<float>(v); }
|
||||
|
||||
inline double FloatToDouble(float v) { return static_cast<double>(v); }
|
||||
|
||||
|
|
|
@ -1320,7 +1320,7 @@ class LpNorm(Primitive):
|
|||
- **input** (Tensor) - Input tensor.
|
||||
|
||||
Outputs:
|
||||
Tensor, has the same dtype as `input`, which shape depends on the args axis.For example, if the size of input
|
||||
Tensor, has the same dtype as `input`, which shape depends on the args axis. For example, if the size of input
|
||||
is (2, 3, 4), axis is [0, 1], Outputs' shape will be (4,).
|
||||
|
||||
Raises:
|
||||
|
@ -1334,7 +1334,7 @@ class LpNorm(Primitive):
|
|||
ValueError: If the length of shape of `axis` is bigger than the length of shape of `input`.
|
||||
|
||||
Supported Platforms:
|
||||
``Ascend``
|
||||
``Ascend`` ``GPU`` ``CPU``
|
||||
|
||||
Examples:
|
||||
>>> input_x = Tensor(np.array([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]).astype(np.float32))
|
||||
|
@ -1347,6 +1347,7 @@ class LpNorm(Primitive):
|
|||
@prim_attr_register
|
||||
def __init__(self, axis, p=2, keep_dims=False, epsilon=1e-12):
|
||||
"""Initialize LpNorm"""
|
||||
super().__init__("LpNorm")
|
||||
validator.check_value_type("p", p, [int], self.name)
|
||||
validator.check_value_type("axis", axis, [int, tuple, list], self.name)
|
||||
validator.check_value_type("keep_dims", keep_dims, [bool], self.name)
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
# 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.
|
||||
# ============================================================================
|
||||
|
||||
import numpy as np
|
||||
import pytest
|
||||
import mindspore.nn as nn
|
||||
from mindspore import Tensor, context
|
||||
from mindspore.ops import operations as P
|
||||
|
||||
|
||||
class LpNormNet(nn.Cell):
|
||||
def __init__(self, axis, p=2, keep_dims=False, epsilon=1e-12):
|
||||
super(LpNormNet, self).__init__()
|
||||
self.lp_norm = P.LpNorm(axis, p, keep_dims, epsilon)
|
||||
|
||||
def construct(self, input_x):
|
||||
output = self.lp_norm(input_x)
|
||||
return output
|
||||
|
||||
|
||||
def lp_norm_np_bencmark(data_type):
|
||||
"""
|
||||
Feature: generate a LpNorm numpy benchmark.
|
||||
Description: The input shape need to match input shape.
|
||||
Expectation: match to np mindspore LpNorm.
|
||||
"""
|
||||
result = np.array([9.165152, 10.954452]).astype(data_type)
|
||||
return result
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.platform_x86_cpu
|
||||
@pytest.mark.parametrize("data_type", [np.float32, np.float16])
|
||||
def test_lp_norm_op(data_type):
|
||||
"""
|
||||
Feature: Test LpNorm.
|
||||
Description: The input shape need match to output shape.
|
||||
Expectation: match to np benchmark.
|
||||
"""
|
||||
context.set_context(mode=context.GRAPH_MODE)
|
||||
input_x = np.array([[[1.0, 2.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]]).astype(data_type)
|
||||
error = 1e-6
|
||||
if data_type == np.float16:
|
||||
error = 1e-3
|
||||
benchmark_output = lp_norm_np_bencmark(data_type)
|
||||
axis = [0, 1]
|
||||
p = 2
|
||||
keep_dims = False
|
||||
lp_norm = LpNormNet(axis, p, keep_dims)
|
||||
output = lp_norm(Tensor(input_x))
|
||||
np.testing.assert_allclose(output.asnumpy(), benchmark_output, rtol=error)
|
||||
context.set_context(mode=context.PYNATIVE_MODE)
|
||||
output = lp_norm(Tensor(input_x))
|
||||
np.testing.assert_allclose(output.asnumpy(), benchmark_output, rtol=error)
|
Loading…
Reference in New Issue