From e13c70bddd1db2fe449c4465aff09c22f5af272c Mon Sep 17 00:00:00 2001 From: yanglf1121 Date: Wed, 12 May 2021 11:33:38 +0800 Subject: [PATCH] fix numpy native bugs --- mindspore/_extends/parse/standard_method.py | 2 +- mindspore/common/tensor.py | 2 +- mindspore/numpy/math_ops.py | 10 +++++----- .../ops/composite/multitype_ops/_compile_utils.py | 5 ++++- tests/st/numpy_native/test_array_ops.py | 2 +- tests/st/numpy_native/test_math_ops.py | 8 ++++---- 6 files changed, 16 insertions(+), 13 deletions(-) diff --git a/mindspore/_extends/parse/standard_method.py b/mindspore/_extends/parse/standard_method.py index 036f3724cf..daad8ed5a3 100644 --- a/mindspore/_extends/parse/standard_method.py +++ b/mindspore/_extends/parse/standard_method.py @@ -510,7 +510,7 @@ def cumsum(x, axis=None, dtype=None): original_dtype = x.dtype # If original tensor is int, and has precision less then int32, convert # to int32 - if x.dtype in (mstype.int8, mstype.int16, mstype.uint8, mstype.int16): + if x.dtype in (mstype.bool_, mstype.int8, mstype.int16, mstype.uint8, mstype.int16): x = x.astype(mstype.int32) if axis is None: x = x.ravel() diff --git a/mindspore/common/tensor.py b/mindspore/common/tensor.py index ffe3a82f15..cb48748044 100644 --- a/mindspore/common/tensor.py +++ b/mindspore/common/tensor.py @@ -796,7 +796,7 @@ class Tensor(Tensor_): x = self original_dtype = x.dtype # If original tensor is int, and has precision less then int32, convert to int32 - if mstype.issubclass_(x.dtype, mstype.int_) and x.itemsize < 4: + if x.dtype in (mstype.bool_, mstype.int8, mstype.int16, mstype.uint8, mstype.int16): x = x.astype(mstype.int32) if axis is None: x = x.ravel() diff --git a/mindspore/numpy/math_ops.py b/mindspore/numpy/math_ops.py index 44448d3416..f1c8dba5a4 100644 --- a/mindspore/numpy/math_ops.py +++ b/mindspore/numpy/math_ops.py @@ -4175,7 +4175,7 @@ def argmax(a, axis=None): 5 >>> print(np.argmax(a, axis=0)) [1 1 1] - >>> print(np.argmax(a, axis=0)) + >>> print(np.argmax(a, axis=1)) [2 2] """ a = _to_tensor(a) @@ -4211,7 +4211,7 @@ def argmin(a, axis=None): 0 >>> print(np.argmin(a, axis=0)) [0 0 0] - >>> print(np.argmin(a, axis=0)) + >>> print(np.argmin(a, axis=1)) [0 0] """ a = _to_tensor(a) @@ -4652,7 +4652,7 @@ def histogram(a, bins=10, range=None, weights=None, density=False): # pylint: di if density: count = F.cast(count, mstype.float32) count = count/diff(bin_edges)/F.reduce_sum(count) - return count.astype(mstype.int32), bin_edges + return count, bin_edges @constexpr @@ -4791,7 +4791,7 @@ def histogramdd(sample, bins=10, range=None, weights=None, density=False): # pyl shape = _expanded_shape(ndim, dedges[i].size, i) count /= _to_tensor(dedges[i]).reshape(shape) count /= s - return count.astype(mstype.int32), bin_edges + return count, bin_edges def histogram2d(x, y, bins=10, range=None, weights=None, density=False): # pylint: disable=redefined-builtin @@ -4855,7 +4855,7 @@ def histogram2d(x, y, bins=10, range=None, weights=None, density=False): # pylin 5.33333349e+00, 6.00000000e+00])) """ count, bin_edges = histogramdd((x, y), bins=bins, range=range, weights=weights, density=density) - return count.astype(mstype.int32), bin_edges[0], bin_edges[1] + return count, bin_edges[0], bin_edges[1] def matrix_power(a, n): diff --git a/mindspore/ops/composite/multitype_ops/_compile_utils.py b/mindspore/ops/composite/multitype_ops/_compile_utils.py index 8caf9f3e7f..e5dce19429 100644 --- a/mindspore/ops/composite/multitype_ops/_compile_utils.py +++ b/mindspore/ops/composite/multitype_ops/_compile_utils.py @@ -908,7 +908,10 @@ def reduce_(a, reduce_fn, cmp_fn=None, axis=None, keepdims=False, initial=None, const_utils.raise_value_error('zero-size tensors are not supported.') if initial is not None: - initial = F.fill(dtype, shape, initial) + if isinstance(initial, Tensor): + initial = F.tile(initial, shape).astype(dtype) + else: + initial = F.fill(dtype, shape, initial) a = cmp_fn(a, initial) if isinstance(where, Tensor): diff --git a/tests/st/numpy_native/test_array_ops.py b/tests/st/numpy_native/test_array_ops.py index ce27a983a3..fd1e8fb472 100644 --- a/tests/st/numpy_native/test_array_ops.py +++ b/tests/st/numpy_native/test_array_ops.py @@ -1641,7 +1641,7 @@ def test_apply_over_axes(): match_array(actual.asnumpy(), expected, error=5) -@pytest.mark.level1 +@pytest.mark.level2 @pytest.mark.platform_arm_ascend_training @pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_gpu_training diff --git a/tests/st/numpy_native/test_math_ops.py b/tests/st/numpy_native/test_math_ops.py index 46840c1a25..f925580488 100644 --- a/tests/st/numpy_native/test_math_ops.py +++ b/tests/st/numpy_native/test_math_ops.py @@ -1372,7 +1372,7 @@ def test_negative(): match_array(mnp_neg.asnumpy(), onp_neg, 1e-5) -@pytest.mark.level1 +@pytest.mark.level0 @pytest.mark.platform_arm_ascend_training @pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_gpu_training @@ -2170,7 +2170,7 @@ def test_bincount(): match_res(mnp.bincount, onp.bincount, x, weights, minlength=25, error=3) -@pytest.mark.level1 +@pytest.mark.level2 @pytest.mark.platform_arm_ascend_training @pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_gpu_training @@ -2193,7 +2193,7 @@ def test_histogram(): match_all_arrays(mnp_res, onp_res, error=1) -@pytest.mark.level1 +@pytest.mark.level2 @pytest.mark.platform_arm_ascend_training @pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_gpu_training @@ -2236,7 +2236,7 @@ def test_histogramdd(): match_all_arrays(mnp_res[1], onp_res[1], error=3) -@pytest.mark.level1 +@pytest.mark.level2 @pytest.mark.platform_arm_ascend_training @pytest.mark.platform_x86_ascend_training @pytest.mark.platform_x86_gpu_training