!35654 Add testcases for ScatterUpdateof and SetItem on dynamic shape tensors

Merge pull request !35654 from zhengzuohe/shrink_axis_mask
This commit is contained in:
i-robot 2022-06-10 11:39:00 +00:00 committed by Gitee
commit e7a32b1f53
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 482 additions and 0 deletions

View File

@ -326,6 +326,7 @@ def get_scatter_nd_vmap_rule(prim, axis_size):
@vmap_rules_getters.register(P.ScatterNdMin)
@vmap_rules_getters.register(P.ScatterNdMax)
@vmap_rules_getters.register(P.ScatterNdDiv)
@vmap_rules_getters.register(P.ScatterNdUpdate)
@vmap_rules_getters.register(P.ScatterUpdate)
def get_scatter_op_vmap_rule(prim, axis_size):
"""
@ -344,6 +345,7 @@ def get_scatter_op_vmap_rule(prim, axis_size):
"ScatterNdMin": P.ScatterNdMin,
"ScatterNdMax": P.ScatterNdMax,
"ScatterNdDiv": P.ScatterNdDiv,
"ScatterNdUpdate": P.ScatterNdUpdate,
"ScatterUpdate": P.ScatterNdUpdate
}
sactter_func_list = ["ScatterAdd", "ScatterMin", "ScatterMax", "ScatterDiv", "ScatterUpdate"]

View File

@ -246,6 +246,19 @@ class VmapNet(nn.Cell):
return vmap(self.net, self.in_axes, self.out_axes)(self.inputx, indices, updates)
class NestVmapNet(nn.Cell):
def __init__(self, net, inputx, in_axes, out_axes):
super(NestVmapNet, self).__init__()
self.net = net
self.in_axes = in_axes
self.out_axes = out_axes
self.inputx = Parameter(inputx, name="inputx")
def construct(self, indices, updates):
return vmap(vmap(self.net, self.in_axes, self.out_axes), self.in_axes, self.out_axes)(
self.inputx, indices, updates)
def scatter_func_indices_vmap():
inputx = Parameter(Tensor(np.array(
[[[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, 4, 5]], [[0, 1, 2], [3, 4, 5]]]
@ -277,6 +290,33 @@ def scatter_func_updates_vmap():
np.testing.assert_array_almost_equal(output.asnumpy(), expected)
def scatter_func_updates_nest_vmap():
inputx = Parameter(Tensor(np.array(
[
[[0.1, 1.0, 2.2], [3.0, 4.3, 5.5]],
[[0.1, 1.0, 2.2], [3.0, 4.3, 5.5]]
]
).astype(np.float32)), name="inputx")
indices = Tensor(np.array([0, 1]).astype(np.int32))
updates = Tensor(np.array(
[
[[1.0, 0.1], [1.2, 1.3]],
[[1.0, 0.1], [1.2, 1.3]]
]
).astype(np.float32))
expected = np.array(
[
[[1.0, 0.1, 2.2], [1.2, 1.3, 5.5]],
[[1.0, 0.1, 2.2], [1.2, 1.3, 5.5]]
]
).astype(np.float32)
# scatter_update
output = NestVmapNet(ScatterFuncVmapNet("update"), inputx,
(0, None, 0), 0)(indices, updates)
np.testing.assert_array_almost_equal(output.asnumpy(), expected)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@ -435,3 +475,19 @@ def test_scatter_func_updates_vmap():
scatter_func_updates_vmap()
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
scatter_func_updates_vmap()
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_scatter_func_updates_nest_vmap():
"""
Feature: test scatter_func nest vmap.
Description: in_axes: (0, None, 0).
Expectation: the result match with numpy result
"""
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
scatter_func_updates_nest_vmap()
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
scatter_func_updates_nest_vmap()

View File

@ -0,0 +1,424 @@
# 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
from mindspore import context, nn
from mindspore import Tensor
import mindspore.common.dtype as mstype
class NumpySetItem():
def __init__(self, index, value):
super(NumpySetItem, self).__init__()
self.index = index
self.value = value
def __call__(self, tensor1, tensor2):
tensor1[self.index] = self.value
tensor2[self.index] = self.value
return tensor1, tensor2
class TensorSetItem(nn.Cell):
def __init__(self, index, value):
super(TensorSetItem, self).__init__()
self.index = index
self.value = value
def construct(self, tensor1, tensor2):
tensor1[self.index] = self.value
tensor2[self.index] = self.value
return tensor1, tensor2
def common_func(ms_net, np_net):
x = Tensor(shape=[8, None, 3], dtype=mstype.float32)
y = Tensor(shape=[None, 32, 3], dtype=mstype.float32)
ms_net.set_inputs(x, y)
input_np1 = np.arange(8 * 16 * 3).reshape(8, 16, 3).astype(np.float32)
input_np2 = np.arange(16 * 32 * 3).reshape(16, 32, 3).astype(np.float32)
out0, out1 = ms_net(Tensor(input_np1), Tensor(input_np2))
out_np0, out_np1 = np_net(input_np1, input_np2)
assert np.all(out0.asnumpy() == out_np0)
assert np.all(out1.asnumpy() == out_np1)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_int_number():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is int, value is a number.
Expectation: Assert the result is equal the numpy result.
"""
index = 2
value = 88.0
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_int_tensor():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is int, value is a tensor.
Expectation: Assert the result is equal the numpy result.
"""
index = 2
value = Tensor(np.arange(3).reshape(
(1 * 3)).astype(np.float32), mstype.float32)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value.asnumpy())
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_int_sequence():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is int, value is a sequence.
Expectation: Assert the result is equal the numpy result.
"""
index = 2
value = (1.0, Tensor(5, mstype.float32), 8.0)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_tensor_number():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is tensor, value is a number.
Expectation: Assert the result is equal the numpy result.
"""
index = Tensor(
np.array([[2, 0, 2], [0, 2, 0], [0, 2, 0]], np.int32), mstype.int32)
value = 88.0
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index.asnumpy(), value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_tensor_tensor():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is tensor, value is a tensor.
Expectation: Assert the result is equal the numpy result.
"""
index = Tensor(
np.array([[2, 0, 2], [0, 2, 0], [0, 2, 0]], np.int32), mstype.int32)
value = Tensor(np.arange(3).reshape(
(1 * 3)).astype(np.float32), mstype.float32)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index.asnumpy(), value.asnumpy())
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_tensor_sequence():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is tensor, value is a sequence.
Expectation: Assert the result is equal the numpy result.
"""
index = Tensor(
np.array([[2, 0, 2], [0, 2, 0], [0, 2, 0]], np.int32), mstype.int32)
value = (1.0, Tensor(5, mstype.float32), 8.0)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index.asnumpy(), value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_none_number():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is None, value is a number.
Expectation: Assert the result is equal the numpy result.
"""
index = None
value = 88.0
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_none_tensor():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is None, value is a tensor.
Expectation: Assert the result is equal the numpy result.
"""
index = None
value = Tensor(np.arange(3).reshape(
(1 * 3)).astype(np.float32), mstype.float32)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value.asnumpy())
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_none_sequence():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is None, value is a sequence.
Expectation: Assert the result is equal the numpy result.
"""
index = None
value = (1.0, Tensor(5, mstype.float32), 8.0)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_ellipsis_number():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is ..., value is a number.
Expectation: Assert the result is equal the numpy result.
"""
index = ...
value = 88.0
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_ellipsis_tensor():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is ..., value is a tensor.
Expectation: Assert the result is equal the numpy result.
"""
index = ...
value = Tensor(np.arange(3).reshape(
(1 * 3)).astype(np.float32), mstype.float32)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value.asnumpy())
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_ellipsis_sequence():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is ..., value is a sequence.
Expectation: Assert the result is equal the numpy result.
"""
index = ...
value = (1.0, Tensor(5, mstype.float32), 8.0)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_bool_number():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is bool(True), value is a number.
Expectation: Assert the result is equal the numpy result.
"""
index = True
value = 88.0
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_bool_tensor():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is bool(True), value is a tensor.
Expectation: Assert the result is equal the numpy result.
"""
index = True
value = Tensor(np.arange(3).reshape(
(1 * 3)).astype(np.float32), mstype.float32)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value.asnumpy())
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_bool_sequence():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is bool(True), value is a sequence.
Expectation: Assert the result is equal the numpy result.
"""
index = True
value = (1.0, Tensor(5, mstype.float32), 8.0)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_list_number():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is a list of int, value is a number.
Expectation: Assert the result is equal the numpy result.
"""
index = [0, 1]
value = 88.0
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_list_tensor():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is al ist of bool and int, value is a tensor.
Expectation: Assert the result is equal the numpy result.
"""
index = [True, 5]
value = Tensor(np.arange(3).reshape(
(1 * 3)).astype(np.float32), mstype.float32)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value.asnumpy())
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)
@pytest.mark.level0
@pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training
@pytest.mark.env_onecard
def test_dynamic_setitem_list_sequence():
"""
Feature: Test index value assignment for dynamic shape Tensor in feed mode.
Description: The input shape is dynamic, the tensor index is a list of int, value is a sequence.
Expectation: Assert the result is equal the numpy result.
"""
index = [0, 1]
value = (1.0, Tensor(5, mstype.float32), 8.0)
ms_net = TensorSetItem(index, value)
np_net = NumpySetItem(index, value)
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
common_func(ms_net, np_net)
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
common_func(ms_net, np_net)