forked from mindspore-Ecosystem/mindspore
!38050 [MSLITE][Fix] add st and fix unique consecutive
Merge pull request !38050 from 赵英灼/fixunique714
This commit is contained in:
commit
15de1ca7ad
|
@ -98,8 +98,10 @@ constexpr auto kMaxPoolV1 = "MaxPoolV1";
|
|||
constexpr auto kMaxPoolGradV1 = "MaxPoolGradV1";
|
||||
constexpr auto kAvgPoolV1 = "AvgPoolV1";
|
||||
constexpr auto kAvgPoolGradV1 = "AvgPoolGradV1";
|
||||
const std::set<std::string> kCpuKernelOps{kIdentity, kMaskedSelect, kMaskedSelectGrad, kDynamicStitch,
|
||||
kSearchSorted, kResizeBilinear, kResizeBilinearGrad, kTensorScatterElements};
|
||||
constexpr auto kUniqueConsecutive = "UniqueConsecutive";
|
||||
const std::set<std::string> kCpuKernelOps{kIdentity, kMaskedSelect, kMaskedSelectGrad,
|
||||
kDynamicStitch, kSearchSorted, kResizeBilinear,
|
||||
kResizeBilinearGrad, kTensorScatterElements, kUniqueConsecutive};
|
||||
const std::set<std::string> kCacheKernelOps{kUpdateCache, kCacheSwapTable, kSubAndFilter, kPadAndShift, kDropout3D,
|
||||
kDropout2D, kNonMaxSuppressionV3, kGetNext, kInitData, kPrint};
|
||||
const std::set<std::string> kCpuKernelBaseOps{kRandomChoiceWithMask,
|
||||
|
|
|
@ -29,6 +29,7 @@ namespace mindspore {
|
|||
namespace ops {
|
||||
namespace {
|
||||
constexpr int64_t kUniqueConsecutiveInputNum = 1;
|
||||
// For aicpu, if axis is 1000, that represents None.
|
||||
constexpr int64_t kAxisIsNone = 1000;
|
||||
abstract::BaseShapePtr UniqueConsecutiveInferShape(const PrimitivePtr &primitive,
|
||||
const std::vector<AbstractBasePtr> &input_args) {
|
||||
|
@ -61,8 +62,8 @@ abstract::BaseShapePtr UniqueConsecutiveInferShape(const PrimitivePtr &primitive
|
|||
int64_t axis = GetValue<int64_t>(axis_ptr);
|
||||
int64_t ndims = SizeToLong(input_shape_vec.size());
|
||||
if (axis >= ndims || axis < -ndims) {
|
||||
MS_LOG(EXCEPTION) << "node:" << op_name << " axis must be in the range [-" << ndims << "," << ndims << ")"
|
||||
<< "but got" << axis << ".";
|
||||
MS_LOG(EXCEPTION) << "For " << op_name << ", the axis must be in the range [-" << ndims << "," << ndims << ")"
|
||||
<< "but got " << axis << ".";
|
||||
}
|
||||
if (axis < 0) {
|
||||
axis = axis + ndims;
|
||||
|
|
|
@ -25,7 +25,6 @@ unique_consecutive_op_info = AiCPURegOp("UniqueConsecutive") \
|
|||
.attr("return_idx", "bool") \
|
||||
.attr("return_counts", "bool") \
|
||||
.attr("axis", "int") \
|
||||
.attr("cust_aicpu", "str") \
|
||||
.dtype_format(DataType.I32_Default, DataType.I32_Default, DataType.I32_Default, DataType.I32_Default) \
|
||||
.dtype_format(DataType.I64_Default, DataType.I64_Default, DataType.I32_Default, DataType.I32_Default) \
|
||||
.dtype_format(DataType.F16_Default, DataType.F16_Default, DataType.I32_Default, DataType.I32_Default) \
|
||||
|
|
|
@ -0,0 +1,344 @@
|
|||
# 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 mindspore.nn as nn
|
||||
import mindspore.ops as ops
|
||||
from mindspore import context, Tensor
|
||||
|
||||
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
|
||||
|
||||
|
||||
class NetUniqueConsecutive(nn.Cell):
|
||||
def __init__(self, return_idx=False, return_counts=False, axis=None):
|
||||
super(NetUniqueConsecutive, self).__init__()
|
||||
self.return_idx = return_idx
|
||||
self.return_counts = return_counts
|
||||
self.axis = axis
|
||||
|
||||
def construct(self, x):
|
||||
return ops.unique_consecutive(x, self.return_idx, self.return_counts, self.axis)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([1, 1, 2, 2, 3, 1, 1, 2]).astype(np.int32))
|
||||
net = NetUniqueConsecutive()
|
||||
out = net(x)
|
||||
exp_out = np.array([1, 2, 3, 1, 2]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_return_idx():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator that returns idx.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([1, 1, 2, 2, 3, 1, 1, 2]).astype(np.int32))
|
||||
net = NetUniqueConsecutive(return_idx=True)
|
||||
out, idx = net(x)
|
||||
exp_out = np.array([1, 2, 3, 1, 2]).astype(np.int32)
|
||||
exp_idx = np.array([0, 0, 1, 1, 2, 3, 3, 4]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_return_counts():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator that returns counts.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([1, 1, 2, 2, 3, 1, 1, 2]).astype(np.int32))
|
||||
net = NetUniqueConsecutive(return_counts=True)
|
||||
out, counts = net(x)
|
||||
exp_out = np.array([1, 2, 3, 1, 2]).astype(np.int32)
|
||||
exp_counts = np.array([2, 2, 1, 2, 1]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_set_axis_0():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator with axis.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([[[1, 2, 3], [3, 2, 4]], [[1, 2, 3], [3, 2, 4]]]).astype(np.int32))
|
||||
net = NetUniqueConsecutive(return_idx=True, return_counts=True, axis=0)
|
||||
out, idx, counts = net(x)
|
||||
exp_out = np.array([[[1, 2, 3], [3, 2, 4]]]).astype(np.int32)
|
||||
exp_idx = np.array([0, 0]).astype(np.int32)
|
||||
exp_counts = np.array([2]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_set_axis_1():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator with axis.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([[[1, 2, 3], [3, 2, 4]], [[1, 2, 3], [3, 2, 4]]]).astype(np.int32))
|
||||
net = NetUniqueConsecutive(return_idx=True, return_counts=True, axis=1)
|
||||
out, idx, counts = net(x)
|
||||
exp_out = np.array([[[1, 2, 3], [3, 2, 4]], [[1, 2, 3], [3, 2, 4]]]).astype(np.int32)
|
||||
exp_idx = np.array([0, 1]).astype(np.int32)
|
||||
exp_counts = np.array([1, 1]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_1d_int32():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator with int32 data.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([1, 2, 3, 3, 1, 2, 2]).astype(np.int32))
|
||||
net = NetUniqueConsecutive(return_idx=True, return_counts=True, axis=0)
|
||||
out, idx, counts = net(x)
|
||||
exp_out = np.array([1, 2, 3, 1, 2]).astype(np.int32)
|
||||
exp_idx = np.array([0, 1, 2, 2, 3, 4, 4]).astype(np.int32)
|
||||
exp_counts = np.array([1, 1, 2, 1, 2]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_1d_int64():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator with int64 data.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([1, 2, 3, 3, 1, 2, 2]).astype(np.int64))
|
||||
net = NetUniqueConsecutive(return_idx=True, return_counts=True, axis=0)
|
||||
out, idx, counts = net(x)
|
||||
exp_out = np.array([1, 2, 3, 1, 2]).astype(np.int64)
|
||||
exp_idx = np.array([0, 1, 2, 2, 3, 4, 4]).astype(np.int64)
|
||||
exp_counts = np.array([1, 1, 2, 1, 2]).astype(np.int64)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_1d_half():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator with half data.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([0.4, 0.5, 2.2, 2.2, 12.43, 12.43, 0.4, 0.5]).astype(np.float16))
|
||||
net = NetUniqueConsecutive(return_idx=True, return_counts=True, axis=0)
|
||||
out, idx, counts = net(x)
|
||||
exp_out = np.array([0.4, 0.5, 2.2, 12.43, 0.4, 0.5]).astype(np.float16)
|
||||
exp_idx = np.array([0, 1, 2, 2, 3, 3, 4, 5]).astype(np.int32)
|
||||
exp_counts = np.array([1, 1, 2, 2, 1, 1]).astype(np.int32)
|
||||
assert np.allclose(out.asnumpy(), exp_out, rtol=1.e-5, atol=1.e-6)
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_1d_float():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator with float data.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([0.5, 0.5, 1.2, 1.3, 6.5, 1.2, 0.5]).astype(np.float32))
|
||||
net = NetUniqueConsecutive(return_idx=True, return_counts=True, axis=0)
|
||||
out, idx, counts = net(x)
|
||||
exp_out = np.array([0.5, 1.2, 1.3, 6.5, 1.2, 0.5]).astype(np.float32)
|
||||
exp_idx = np.array([0, 0, 1, 2, 3, 4, 5]).astype(np.int32)
|
||||
exp_counts = np.array([2, 1, 1, 1, 1, 1]).astype(np.int32)
|
||||
assert np.allclose(out.asnumpy(), exp_out, rtol=1.e-5, atol=1.e-6)
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_0d():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator with 0-dimensional data.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(5)
|
||||
net = NetUniqueConsecutive(return_idx=True, return_counts=True, axis=None)
|
||||
out, idx, counts = net(x)
|
||||
exp_out = np.array([5])
|
||||
exp_idx = 0
|
||||
exp_counts = np.array([1])
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_3d():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator with 3-dimensional data.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([[[1, 2, 3], [3, 2, 4], [3, 2, 4], [1, 2, 3]], \
|
||||
[[1, 2, 3], [3, 2, 4], [3, 2, 4], [1, 2, 3]]]).astype(np.int32))
|
||||
net = NetUniqueConsecutive(return_idx=True, return_counts=True, axis=0)
|
||||
out, idx, counts = net(x)
|
||||
exp_out = np.array([[[1, 2, 3], [3, 2, 4], [3, 2, 4], [1, 2, 3]]]).astype(np.int32)
|
||||
exp_idx = np.array([0, 0]).astype(np.int32)
|
||||
exp_counts = np.array([2]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_unique_consecutive_3d_axis():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive operator with 3-dimensional data.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([[[1, 2, 3], [3, 2, 4], [3, 2, 4], [1, 2, 3]], \
|
||||
[[1, 2, 3], [3, 2, 4], [3, 2, 4], [1, 2, 3]]]).astype(np.int32))
|
||||
net = NetUniqueConsecutive(return_idx=True, return_counts=True, axis=1)
|
||||
out, idx, counts = net(x)
|
||||
exp_out = np.array([[[1, 2, 3], [3, 2, 4], [1, 2, 3]], \
|
||||
[[1, 2, 3], [3, 2, 4], [1, 2, 3]]]).astype(np.int32)
|
||||
exp_idx = np.array([0, 1, 1, 2]).astype(np.int32)
|
||||
exp_counts = np.array([1, 2, 1]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
class NetTensor(nn.Cell):
|
||||
def construct(self, x, return_idx, return_counts, axis):
|
||||
return x.unique_consecutive(return_idx, return_counts, axis)
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_tensor_return_output():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive tensor api that only return output.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([1, 1, 2, 2, 3, 1, 1, 2]).astype(np.int32))
|
||||
net = NetTensor()
|
||||
out = net(x, False, False, 0)
|
||||
exp_out = np.array([1, 2, 3, 1, 2]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_tensor_return_idx():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive tensor api that only return output.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([1, 1, 2, 2, 3, 1, 1, 2]).astype(np.int32))
|
||||
net = NetTensor()
|
||||
out, idx = net(x, True, False, 0)
|
||||
exp_out = np.array([1, 2, 3, 1, 2]).astype(np.int32)
|
||||
exp_idx = np.array([0, 0, 1, 1, 2, 3, 3, 4]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_tensor_return_counts():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive tensor api that only return output.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([1, 1, 2, 2, 3, 1, 1, 2]).astype(np.int32))
|
||||
net = NetTensor()
|
||||
out, counts = net(x, False, True, 0)
|
||||
exp_out = np.array([1, 2, 3, 1, 2]).astype(np.int32)
|
||||
exp_counts = np.array([2, 2, 1, 2, 1]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
||||
|
||||
|
||||
@pytest.mark.level0
|
||||
@pytest.mark.platform_x86_ascend_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_tensor_return_all():
|
||||
"""
|
||||
Feature: UniqueConsecutive operator.
|
||||
Description: Test UniqueConsecutive tensor api that return all.
|
||||
Expectation: No exception.
|
||||
"""
|
||||
x = Tensor(np.array([1, 1, 2, 2, 3, 1, 1, 2]).astype(np.int32))
|
||||
net = NetTensor()
|
||||
out, idx, counts = net(x, True, True, 0)
|
||||
exp_out = np.array([1, 2, 3, 1, 2]).astype(np.int32)
|
||||
exp_idx = np.array([0, 0, 1, 1, 2, 3, 3, 4]).astype(np.int32)
|
||||
exp_counts = np.array([2, 2, 1, 2, 1]).astype(np.int32)
|
||||
assert (out.asnumpy() == exp_out).all()
|
||||
assert (idx.asnumpy() == exp_idx).all()
|
||||
assert (counts.asnumpy() == exp_counts).all()
|
Loading…
Reference in New Issue