forked from mindspore-Ecosystem/mindspore
!48044 delete function fill and fills
Merge pull request !48044 from changzherui/del_fill_api
This commit is contained in:
commit
5cc5607898
|
@ -1,6 +0,0 @@
|
|||
mindspore.Tensor.fill
|
||||
=====================
|
||||
|
||||
.. py:method:: mindspore.Tensor.fill(value)
|
||||
|
||||
详情请参考 :func:`mindspore.ops.fill`。
|
|
@ -107,7 +107,6 @@ mindspore.Tensor
|
|||
mindspore.Tensor.expand_as
|
||||
mindspore.Tensor.expand_dims
|
||||
mindspore.Tensor.expm1
|
||||
mindspore.Tensor.fill
|
||||
mindspore.Tensor.flatten
|
||||
mindspore.Tensor.flip
|
||||
mindspore.Tensor.fliplr
|
||||
|
|
|
@ -1773,53 +1773,6 @@ def searchsorted(x, v, side='left', sorter=None):
|
|||
return j
|
||||
|
||||
|
||||
def fill(x, value):
|
||||
"""
|
||||
Fills the array with a scalar value.
|
||||
|
||||
Note:
|
||||
Unlike Numpy, tensor.fill() will always returns a new tensor, instead of
|
||||
filling the original tensor.
|
||||
|
||||
Args:
|
||||
value (Union[None, int, float, bool]): All elements of a will be assigned this value.
|
||||
|
||||
Returns:
|
||||
Tensor, with the original dtype and shape as input tensor.
|
||||
|
||||
Raises:
|
||||
TypeError: If input arguments have types not specified above.
|
||||
ValueError: If `shape` has entries < 0.
|
||||
|
||||
Supported Platforms:
|
||||
``Ascend`` ``GPU`` ``CPU``
|
||||
|
||||
Examples:
|
||||
>>> import numpy as np
|
||||
>>> from mindspore import Tensor
|
||||
>>> a = Tensor(np.arange(4).reshape((2,2)).astype('float32'))
|
||||
>>> print(a.fill(1.0))
|
||||
[[1. 1.]
|
||||
[1. 1.]]
|
||||
"""
|
||||
if value is None:
|
||||
if x.dtype not in (mstype.float16, mstype.float32, mstype.float64):
|
||||
const_utils.raise_type_error(
|
||||
"If None is used as value, the original Tensor's dtype must be float.")
|
||||
value = nan_tensor
|
||||
return F.tile(value, x.shape).astype(x.dtype)
|
||||
if not isinstance(value, (int, float, bool)):
|
||||
const_utils.raise_type_error("input value must be a scalar.")
|
||||
return F.fill(x.dtype, x.shape, value)
|
||||
|
||||
|
||||
def fills(x, value):
|
||||
"""
|
||||
Create a tensor of the same shape and type as the input tensor and fill it with specified value.
|
||||
"""
|
||||
return F.fills(x, value)
|
||||
|
||||
|
||||
def ptp(x, axis=None, keepdims=False):
|
||||
"""
|
||||
The name of the function comes from the acronym for "peak to peak".
|
||||
|
|
|
@ -2184,28 +2184,6 @@ class Tensor(Tensor_):
|
|||
self._init_check()
|
||||
return tensor_operator_registry.get('tensor_scatter_max')()(self, indices, updates)
|
||||
|
||||
def fill(self, value):
|
||||
"""
|
||||
For details, please refer to :func:`mindspore.ops.fill`.
|
||||
"""
|
||||
if value is None:
|
||||
if self.dtype not in (mstype.float16, mstype.float32, mstype.float64):
|
||||
raise TypeError("For 'Tensor.fill', if the argument 'value' is None, the type of the original "
|
||||
"tensor must be float, but got {}.".format(self.dtype))
|
||||
value = Tensor(float('nan')).astype("float32")
|
||||
return tensor_operator_registry.get("tile")()(value, self.shape).astype(self.dtype)
|
||||
if not isinstance(value, (int, float, bool)):
|
||||
raise TypeError("For 'Tensor.fill', the type of the argument 'value' must be int, float or bool, "
|
||||
"but got {}.".format(type(value)))
|
||||
return tensor_operator_registry.get("fill")(self.dtype, self.shape, value)
|
||||
|
||||
def fills(self, value):
|
||||
"""
|
||||
For details, please refer to :func:`mindspore.ops.fills`.
|
||||
"""
|
||||
self._init_check()
|
||||
return tensor_operator_registry.get('fills')(self, value)
|
||||
|
||||
def masked_fill(self, mask, value):
|
||||
"""
|
||||
For details, please refer to :func:`mindspore.ops.masked_fill`.
|
||||
|
|
|
@ -119,7 +119,6 @@ from .array_func import (
|
|||
where,
|
||||
meshgrid,
|
||||
affine_grid,
|
||||
fills,
|
||||
broadcast_to,
|
||||
unsorted_segment_sum,
|
||||
col2im,
|
||||
|
|
|
@ -34,7 +34,6 @@ from mindspore.ops.operations.array_ops import (
|
|||
MatrixDiagV3,
|
||||
MatrixDiagPartV3,
|
||||
MatrixSetDiagV3,
|
||||
Fills,
|
||||
Col2Im,
|
||||
ArgMaxWithValue,
|
||||
ArgMinWithValue,
|
||||
|
@ -58,7 +57,6 @@ from mindspore._c_expression import Tensor as Tensor_
|
|||
|
||||
eye_ = P.Eye()
|
||||
fill_ = P.Fill()
|
||||
fills_ = Fills()
|
||||
ones_ = P.Ones()
|
||||
ones_like_ = P.OnesLike()
|
||||
tile_ = P.Tile()
|
||||
|
@ -672,52 +670,6 @@ def fill(type, shape, value): # pylint: disable=redefined-outer-name
|
|||
return fill_(type, shape, value)
|
||||
|
||||
|
||||
def fills(x, value):
|
||||
"""
|
||||
Create a tensor of the same shape and type as the input tensor and fill it with specified value.
|
||||
|
||||
Args:
|
||||
x (Tensor): Input tensor, used to specify the shape and type of the output tensor. The data type should be
|
||||
int8, int16, int32, float16 or float32.
|
||||
value (Union[int, float, Tensor]): All elements of the output tensor will be assigned this value. The
|
||||
type should be int, float or 0-dimensional tensor.
|
||||
|
||||
Returns:
|
||||
Tensor, with the same shape and type as input tensor.
|
||||
|
||||
Raises:
|
||||
TypeError: If `x` is not a tensor.
|
||||
TypeError: If `value` has types not specified above.
|
||||
RuntimeError: If `value` cannot be converted to the same type as `x`.
|
||||
ValueError: If `value` is a tensor and the length of dimension is not 0.
|
||||
|
||||
Supported Platforms:
|
||||
``GPU``
|
||||
|
||||
Examples:
|
||||
>>> import numpy as np
|
||||
>>> from mindspore import Tensor
|
||||
>>> x = Tensor(np.arange(4).reshape((2, 2)).astype('float32'))
|
||||
>>> output = ops.fills(x, 1)
|
||||
>>> print(output)
|
||||
[[1. 1.]
|
||||
[1. 1.]]
|
||||
"""
|
||||
if isinstance(value, float):
|
||||
value_ = value
|
||||
elif isinstance(value, int):
|
||||
value_ = float(value)
|
||||
elif isinstance(value, Tensor):
|
||||
if value.ndim != 0:
|
||||
raise ValueError("For 'ops.fills', if the argument 'value' is a tensor, the number of its dimension"
|
||||
" should be 0, but got {}".format(value.ndim))
|
||||
value_ = value.astype(mstype.float32)
|
||||
else:
|
||||
raise TypeError("For 'ops.fills', the type of argument 'value' should be int, float or Tensor,"
|
||||
" but got {}".format(type(value)))
|
||||
return fills_(x, value_)
|
||||
|
||||
|
||||
def full(size, fill_value, *, dtype=None): # pylint: disable=redefined-outer-name
|
||||
"""
|
||||
Create a Tensor of the specified shape and fill it with the specified value.
|
||||
|
@ -6415,7 +6367,6 @@ __all__ = [
|
|||
'padding',
|
||||
'fill',
|
||||
'fill_',
|
||||
'fills',
|
||||
'tile',
|
||||
'size',
|
||||
'ger',
|
||||
|
|
|
@ -290,8 +290,6 @@ tensor_operator_registry.register('expand_dims', expand_dims)
|
|||
# support GE backend for no compare operators
|
||||
tensor_operator_registry.register('cast', cast)
|
||||
tensor_operator_registry.register('shape_mul', shape_mul)
|
||||
tensor_operator_registry.register('fill', fill)
|
||||
tensor_operator_registry.register('fills', fills)
|
||||
tensor_operator_registry.register('concatenate', P.Concat)
|
||||
tensor_operator_registry.register('eye', eye)
|
||||
tensor_operator_registry.register('reduce_sum', reduce_sum)
|
||||
|
|
|
@ -1,221 +0,0 @@
|
|||
# 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, Tensor
|
||||
from mindspore import ops as P
|
||||
from mindspore.ops.operations import _inner_ops as inner
|
||||
|
||||
|
||||
class FillsNet(nn.Cell):
|
||||
"""FillsNet."""
|
||||
def __init__(self):
|
||||
super(FillsNet, self).__init__()
|
||||
self.fills = P.fills
|
||||
|
||||
def construct(self, x, value):
|
||||
out = self.fills(x, value)
|
||||
return out
|
||||
|
||||
|
||||
class FillsDynamicNet(nn.Cell):
|
||||
"""Fills in dynamic shape."""
|
||||
def __init__(self):
|
||||
super(FillsDynamicNet, self).__init__()
|
||||
self.test_dynamic = inner.GpuConvertToDynamicShape()
|
||||
|
||||
def construct(self, x, value):
|
||||
x = self.test_dynamic(x)
|
||||
out = P.fills(x, value)
|
||||
return out
|
||||
|
||||
|
||||
def compare_with_numpy(data_shape, data_type, value, out):
|
||||
"""Compare results with numpy."""
|
||||
expect_res = np.zeros(data_shape, dtype=data_type)
|
||||
expect_res.fill(value)
|
||||
ms_res = out.asnumpy()
|
||||
assert np.allclose(expect_res, ms_res)
|
||||
|
||||
|
||||
def gen_np_input(data_shape, data_type):
|
||||
"""Generate input x."""
|
||||
out = np.random.randn(*data_shape)
|
||||
if not data_shape:
|
||||
out = data_type(out)
|
||||
else:
|
||||
out = out.astype(data_type)
|
||||
return out
|
||||
|
||||
|
||||
@pytest.mark.level1
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('run_mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('data_shape', [(), (2,), (2, 3), (2, 2, 3, 3, 4, 4, 5)])
|
||||
@pytest.mark.parametrize('data_type', [np.int8, np.int16, np.int32, np.float16, np.float32])
|
||||
def test_fills_data_type_and_shape(run_mode, data_shape, data_type):
|
||||
"""
|
||||
Feature: Fills
|
||||
Description: test cases for Fills operator with multiple data types and shapes.
|
||||
Expectation: the result match numpy.
|
||||
"""
|
||||
context.set_context(mode=run_mode, device_target='GPU')
|
||||
input_np = gen_np_input(data_shape=data_shape, data_type=data_type)
|
||||
input_x = Tensor(input_np)
|
||||
value = 4.0
|
||||
model = FillsNet()
|
||||
out = model(input_x, value)
|
||||
compare_with_numpy(data_shape, data_type, value, out)
|
||||
|
||||
|
||||
@pytest.mark.level1
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('run_mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('value', [4, 4.0, Tensor(np.float32(4))])
|
||||
def test_fills_with_value_type(run_mode, value):
|
||||
"""
|
||||
Feature: Fills
|
||||
Description: test cases for Fills operator with different value type.
|
||||
Expectation: the result match numpy.
|
||||
"""
|
||||
context.set_context(mode=run_mode, device_target='GPU')
|
||||
data_shape = (2, 3)
|
||||
data_type = np.int32
|
||||
input_np = gen_np_input(data_shape=data_shape, data_type=data_type)
|
||||
input_x = Tensor(input_np)
|
||||
model = FillsNet()
|
||||
out = model(input_x, value)
|
||||
compare_with_numpy(data_shape, data_type, value, out)
|
||||
|
||||
|
||||
@pytest.mark.level1
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('run_mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('data_shape', [(2,), (2, 3), (2, 2, 3, 3, 4, 4, 5)])
|
||||
def test_fills_dyn_with_dynamic_shape(run_mode, data_shape):
|
||||
"""
|
||||
Feature: Fills
|
||||
Description: test cases for Fills operator in dynamic shape case.
|
||||
Expectation: the result match numpy.
|
||||
"""
|
||||
data_type = np.int32
|
||||
context.set_context(mode=run_mode, device_target='GPU')
|
||||
input_np = gen_np_input(data_shape=data_shape, data_type=data_type)
|
||||
input_x = Tensor(input_np)
|
||||
value = 4.0
|
||||
model = FillsDynamicNet()
|
||||
out = model(input_x, value)
|
||||
compare_with_numpy(data_shape, data_type, value, out)
|
||||
|
||||
|
||||
@pytest.mark.level1
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('run_mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('data_type', [np.float16, np.float32])
|
||||
def test_fills_with_nan(run_mode, data_type):
|
||||
"""
|
||||
Feature: Fills
|
||||
Description: test cases for Fills operator when fill with nan.
|
||||
Expectation: the result match numpy.
|
||||
"""
|
||||
context.set_context(mode=run_mode, device_target='GPU')
|
||||
data_shape = (2, 3)
|
||||
value = float('nan')
|
||||
input_np = gen_np_input(data_shape=data_shape, data_type=data_type)
|
||||
input_x = Tensor(input_np)
|
||||
out = input_x.fills(value)
|
||||
assert np.isnan(out.asnumpy()).any()
|
||||
|
||||
|
||||
@pytest.mark.level1
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('data_type', [np.float16, np.float32])
|
||||
@pytest.mark.parametrize('value', [float('inf'), float('-inf')])
|
||||
def test_fills_with_inf(data_type, value):
|
||||
"""
|
||||
Feature: Fills
|
||||
Description: test cases for Fills operator when fill with inf.
|
||||
Expectation: the result match numpy.
|
||||
"""
|
||||
context.set_context(device_target='GPU')
|
||||
data_shape = (2, 3)
|
||||
input_np = gen_np_input(data_shape=data_shape, data_type=data_type)
|
||||
input_x = Tensor(input_np)
|
||||
out = input_x.fills(value)
|
||||
compare_with_numpy(data_shape, data_type, value, out)
|
||||
|
||||
|
||||
@pytest.mark.level1
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('run_mode', [context.GRAPH_MODE, context.PYNATIVE_MODE])
|
||||
@pytest.mark.parametrize('data_type', [np.int8, np.int16, np.int32, np.float16])
|
||||
def test_fills_with_overflow(run_mode, data_type):
|
||||
"""
|
||||
Feature: Fills
|
||||
Description: test cases for Fills operator when overflow happens on value convert.
|
||||
Expectation: the result match numpy.
|
||||
"""
|
||||
context.set_context(mode=run_mode, device_target='GPU')
|
||||
data_shape = (2, 3)
|
||||
input_np = gen_np_input(data_shape=data_shape, data_type=data_type)
|
||||
input_x = Tensor(input_np)
|
||||
value = float(pow(2, 32))
|
||||
model = FillsNet()
|
||||
with pytest.raises(RuntimeError, match='Fills-op'):
|
||||
model(input_x, value)
|
||||
|
||||
|
||||
@pytest.mark.level1
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
@pytest.mark.parametrize('data_type', [np.int8, np.int16, np.int32])
|
||||
@pytest.mark.parametrize('value', [float('inf'), float('-inf'), float('nan')])
|
||||
def test_fills_except_with_inf_nan(data_type, value):
|
||||
"""
|
||||
Feature: Fills
|
||||
Description: test cases for Fills operator when convert inf/nan to int type.
|
||||
Expectation: the result match numpy.
|
||||
"""
|
||||
context.set_context(device_target='GPU')
|
||||
data_shape = (2, 3)
|
||||
input_np = gen_np_input(data_shape=data_shape, data_type=data_type)
|
||||
input_x = Tensor(input_np)
|
||||
with pytest.raises(RuntimeError, match='Fills-op'):
|
||||
input_x.fills(value)
|
||||
|
||||
|
||||
@pytest.mark.level1
|
||||
@pytest.mark.platform_x86_gpu_training
|
||||
@pytest.mark.env_onecard
|
||||
def test_fills_except_with_invalid_type():
|
||||
"""
|
||||
Feature: Fills
|
||||
Description: test cases for Fills operator with invalid type.
|
||||
Expectation: the result match numpy.
|
||||
"""
|
||||
context.set_context(device_target='GPU')
|
||||
data_shape = (2, 3)
|
||||
input_np = gen_np_input(data_shape=data_shape, data_type=np.int)
|
||||
input_x = Tensor(input_np)
|
||||
value = [2]
|
||||
with pytest.raises(TypeError, match='ops.fills'):
|
||||
P.fills(input_x, value)
|
Loading…
Reference in New Issue