!48423 fix level1 testcase

Merge pull request !48423 from caifubi/master-pynative-testcase
This commit is contained in:
i-robot 2023-02-07 07:20:13 +00:00 committed by Gitee
commit 8978cf0a97
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 16 additions and 4 deletions

View File

@ -24,6 +24,7 @@ from mindspore.ops.operations import _inner_ops
from mindspore import ops as P from mindspore import ops as P
from mindspore.ops import composite as C from mindspore.ops import composite as C
class Net(nn.Cell): class Net(nn.Cell):
def __init__(self): def __init__(self):
super(Net, self).__init__() super(Net, self).__init__()
@ -32,15 +33,18 @@ class Net(nn.Cell):
def construct(self, input_x, update, begin, end, strides): def construct(self, input_x, update, begin, end, strides):
return self.copy_slices(input_x, update, begin, end, strides) return self.copy_slices(input_x, update, begin, end, strides)
class GradNet(nn.Cell): class GradNet(nn.Cell):
def __init__(self): def __init__(self):
super(GradNet, self).__init__() super(GradNet, self).__init__()
self.input_x = Parameter(Tensor([[2, 3, 4, 5], [2, 3, 4, 5]], mstype.float32)) self.input_x = Parameter(Tensor([[2, 3, 4, 5], [2, 3, 4, 5]], mstype.float32))
self.relu = P.ReLU() self.relu = P.ReLU()
self.copy_slices = _inner_ops.TensorCopySlices()
def construct(self, input_y): def construct(self, input_y):
self.input_x[0:1] /= input_y output = self.copy_slices(self.input_x, input_y, (0, 0), (1, 1), (1, 1))
return self.relu(self.input_x) return self.relu(output)
def convert_begin_end_strides_to_slice(begin, end, strides): def convert_begin_end_strides_to_slice(begin, end, strides):
result = [] result = []
@ -48,6 +52,7 @@ def convert_begin_end_strides_to_slice(begin, end, strides):
result.append(slice(x, y, z)) result.append(slice(x, y, z))
return tuple(result) return tuple(result)
def test_tensor_copy_slices_net(input_shape, update_shape, begin, end, strides, dtype): def test_tensor_copy_slices_net(input_shape, update_shape, begin, end, strides, dtype):
input_np = np.zeros(input_shape, dtype) input_np = np.zeros(input_shape, dtype)
update_np = np.ones(update_shape, dtype) update_np = np.ones(update_shape, dtype)
@ -59,12 +64,14 @@ def test_tensor_copy_slices_net(input_shape, update_shape, begin, end, strides,
input_np[slices] = update_np input_np[slices] = update_np
assert (output.asnumpy() == input_np).all() assert (output.asnumpy() == input_np).all()
def test_tensor_copy_slices_net_many_dtype(input_shape, update_shape, begin, end, strides, dtypes): def test_tensor_copy_slices_net_many_dtype(input_shape, update_shape, begin, end, strides, dtypes):
for dtype in dtypes: for dtype in dtypes:
test_tensor_copy_slices_net(input_shape, update_shape, begin, end, strides, dtype) test_tensor_copy_slices_net(input_shape, update_shape, begin, end, strides, dtype)
support_dtype = (np.int64, np.int32, np.float64, np.float32) support_dtype = (np.int64, np.int32, np.float64, np.float32)
def test_tensor_copy_slices(): def test_tensor_copy_slices():
test_tensor_copy_slices_net_many_dtype((10,), (5,), (0,), (5,), (1,), support_dtype) test_tensor_copy_slices_net_many_dtype((10,), (5,), (0,), (5,), (1,), support_dtype)
test_tensor_copy_slices_net_many_dtype((10,), (5,), (5,), (10,), (1,), support_dtype) test_tensor_copy_slices_net_many_dtype((10,), (5,), (5,), (10,), (1,), support_dtype)
@ -75,7 +82,7 @@ def test_tensor_copy_slices():
test_tensor_copy_slices_net_many_dtype((10, 10, 10), (5, 10), (9, 5,), (10, 10,), (1, 1,), support_dtype) test_tensor_copy_slices_net_many_dtype((10, 10, 10), (5, 10), (9, 5,), (10, 10,), (1, 1,), support_dtype)
@pytest.mark.level1 @pytest.mark.level0
@pytest.mark.platform_x86_gpu_training @pytest.mark.platform_x86_gpu_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
def test_tensor_copy_slices_bprop(): def test_tensor_copy_slices_bprop():
@ -86,7 +93,7 @@ def test_tensor_copy_slices_bprop():
output = net(input_y) output = net(input_y)
grad_output = grad_net(input_y, output) grad_output = grad_net(input_y, output)
assert np.allclose(grad_output[0].asnumpy(), np.array([-6.75])) assert np.allclose(grad_output[0].asnumpy(), np.array([2.0]))
@pytest.mark.level1 @pytest.mark.level1
@ -97,6 +104,7 @@ def test_tensor_copy_slices_ascend_graph():
context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
test_tensor_copy_slices() test_tensor_copy_slices()
@pytest.mark.level1 @pytest.mark.level1
@pytest.mark.platform_arm_ascend_training @pytest.mark.platform_arm_ascend_training
@pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_ascend_training
@ -105,6 +113,7 @@ def test_tensor_copy_slices_ascend_pynative():
context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend") context.set_context(mode=context.PYNATIVE_MODE, device_target="Ascend")
test_tensor_copy_slices() test_tensor_copy_slices()
@pytest.mark.level1 @pytest.mark.level1
@pytest.mark.platform_x86_gpu_training @pytest.mark.platform_x86_gpu_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
@ -112,6 +121,7 @@ def test_tensor_copy_slices_gpu_graph():
context.set_context(mode=context.GRAPH_MODE, device_target="GPU") context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
test_tensor_copy_slices() test_tensor_copy_slices()
@pytest.mark.level1 @pytest.mark.level1
@pytest.mark.platform_x86_gpu_training @pytest.mark.platform_x86_gpu_training
@pytest.mark.env_onecard @pytest.mark.env_onecard
@ -119,6 +129,7 @@ def test_tensor_copy_slices_gpu_pynative():
context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU") context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
test_tensor_copy_slices() test_tensor_copy_slices()
@pytest.mark.level0 @pytest.mark.level0
@pytest.mark.platform_x86_cpu @pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard @pytest.mark.env_onecard
@ -126,6 +137,7 @@ def test_tensor_copy_slices_cpu_graph():
context.set_context(mode=context.GRAPH_MODE, device_target="CPU") context.set_context(mode=context.GRAPH_MODE, device_target="CPU")
test_tensor_copy_slices() test_tensor_copy_slices()
@pytest.mark.level0 @pytest.mark.level0
@pytest.mark.platform_x86_cpu @pytest.mark.platform_x86_cpu
@pytest.mark.env_onecard @pytest.mark.env_onecard