Skip to content

Commit

Permalink
fix 4 apis test=develop (#25529)
Browse files Browse the repository at this point in the history
  • Loading branch information
yaoxuefeng6 authored Aug 7, 2020
1 parent beb0ca5 commit 07f509a
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 81 deletions.
116 changes: 110 additions & 6 deletions python/paddle/fluid/tests/unittests/test_addmm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,104 @@ class TestAddMMOpError(unittest.TestCase):
def test_errors(self):
with program_guard(Program(), Program()):
# The input type of addmm_op must be Variable.

input = fluid.create_lod_tensor(
np.array([[-1]]), [[1]], fluid.CPUPlace())
np.array([[-1, -1], [-1, -1]]), [[2]], fluid.CPUPlace())
x1 = fluid.create_lod_tensor(
np.array([[-1]]), [[1]], fluid.CPUPlace())
np.array([[-1, -1], [-1, -1]]), [[2]], fluid.CPUPlace())
x2 = fluid.create_lod_tensor(
np.array([[-1]]), [[1]], fluid.CPUPlace())
np.array([[-1, -1], [-1, -1]]), [[2]], fluid.CPUPlace())
self.assertRaises(TypeError, paddle.addmm, input, x1, x2)

# The input dtype of mul_op must be float32 or float64.
input = fluid.layers.data(name='input', shape=[4], dtype="int32")
x3 = fluid.layers.data(name='x3', shape=[4], dtype="int32")
x4 = fluid.layers.data(name='x4', shape=[4], dtype="int32")
input = fluid.layers.data(
name='input',
shape=[4, 4],
dtype="int32",
append_batch_size=False)
x3 = fluid.layers.data(
name='x3', shape=[4, 4], dtype="int32", append_batch_size=False)
x4 = fluid.layers.data(
name='x4', shape=[4, 4], dtype="int32", append_batch_size=False)
self.assertRaises(TypeError, paddle.addmm, input, x3, x4)
# x and y dimension mismatch
x5 = fluid.layers.data(
name='x5',
shape=[4, 5],
dtype="float32",
append_batch_size=False)
x6 = fluid.layers.data(
name='x6',
shape=[4, 4],
dtype="float32",
append_batch_size=False)
self.assertRaises(ValueError, paddle.addmm, input, x5, x6)
# input and x are not broadcastable
x7 = fluid.layers.data(
name='x7',
shape=[4, 4],
dtype="float32",
append_batch_size=False)
x8 = fluid.layers.data(
name='x8',
shape=[4, 4],
dtype="float32",
append_batch_size=False)
input1 = fluid.layers.data(
name='input1',
shape=[2, 4],
dtype="float32",
append_batch_size=False)
self.assertRaises(ValueError, paddle.addmm, input1, x7, x8)
# input and x are not broadcastable
x9 = fluid.layers.data(
name='x9',
shape=[4, 4],
dtype="float32",
append_batch_size=False)
x10 = fluid.layers.data(
name='x10',
shape=[4, 4],
dtype="float32",
append_batch_size=False)
input2 = fluid.layers.data(
name='input2',
shape=[1, 2],
dtype="float32",
append_batch_size=False)
self.assertRaises(ValueError, paddle.addmm, input2, x9, x10)
x11 = fluid.layers.data(
name='x11',
shape=[4, 4],
dtype="float32",
append_batch_size=False)
x12 = fluid.layers.data(
name='x12',
shape=[4, 4],
dtype="float32",
append_batch_size=False)
input3 = fluid.layers.data(
name='input3',
shape=[4, 2],
dtype="float32",
append_batch_size=False)
self.assertRaises(ValueError, paddle.addmm, input3, x11, x12)
x13 = fluid.layers.data(
name='x13',
shape=[4, 4],
dtype="float32",
append_batch_size=False)
x14 = fluid.layers.data(
name='x14',
shape=[4, 4],
dtype="float32",
append_batch_size=False)
input4 = fluid.layers.data(
name='input4',
shape=[3, 1],
dtype="float32",
append_batch_size=False)
self.assertRaises(ValueError, paddle.addmm, input4, x13, x14)


class TestAddMMOp2(TestAddMMOp):
Expand Down Expand Up @@ -147,5 +233,23 @@ def test_api_with_dygraph(self):
assert np.allclose(np_input + np.dot(np_x, np_y), out.numpy())


'''
class TestAddMMAPI(unittest.TestCase):
def test_api_error(self):
data_x = np.ones((2, 2)).astype(np.float32)
data_y = np.ones((2, 2)).astype(np.float32)
data_input = np.ones((2, 2)).astype(np.float32)
paddle.enable_imperative()
def test_error1():
data_x_wrong = np.ones((2, 3)).astype(np.float32)
x = paddle.imperative.to_variable(data_x_wrong)
y = paddle.imperative.to_variable(data_y)
input = paddle.imperative.to_variable(data_input)
out = paddle.tensor.addmm( input=input, x=x, y=y, beta=0.5, alpha=5.0 )
self.assertRaises(ValueError, test_error1)
'''

if __name__ == "__main__":
unittest.main()
10 changes: 10 additions & 0 deletions python/paddle/fluid/tests/unittests/test_bmm_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,15 @@ def test_out(self):
self.assertTrue(np.allclose(expected_result, out_np))


class TestBmmAPIError(unittest.TestCase):
def test_api_error(self):
x_data = np.arange(24, dtype='float32').reshape((2, 3, 4))
y_data = np.arange(16, dtype='float32').reshape((2, 4, 2))
y_data_wrong1 = np.arange(16, dtype='float32').reshape((2, 2, 4))
y_data_wrong2 = np.arange(16, dtype='float32').reshape((2, 2, 2, 2))
self.assertRaises(ValueError, paddle.bmm, x_data, y_data_wrong1)
self.assertRaises(ValueError, paddle.bmm, x_data, y_data_wrong2)


if __name__ == "__main__":
unittest.main()
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/test_tril_triu_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,15 +63,15 @@ def case_generator(op_type, Xshape, diagonal, expected):
"diagonal: TypeError":
"diagonal in {} must be a python Int".format(op_type),
"input: ValueError":
"input shape in {} must be at least 2-D".format(op_type),
"x shape in {} must be at least 2-D".format(op_type),
}

class FailureCase(unittest.TestCase):
def test_failure(self):
data = fluid.data(shape=Xshape, dtype='float64', name=cls_name)
with self.assertRaisesRegexp(
eval(expected.split(':')[-1]), errmsg[expected]):
getattr(tensor, op_type)(input=data, diagonal=diagonal)
getattr(tensor, op_type)(x=data, diagonal=diagonal)

class SuccessCase(TrilTriuOpDefaultTest):
def initTestCase(self):
Expand Down
75 changes: 31 additions & 44 deletions python/paddle/tensor/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -490,14 +490,13 @@ def _tril_triu_op(helper):
"""Base op of tril_op and triu_op
"""
op_type = helper.layer_type
x = helper.kwargs.get('input', None)
x = helper.kwargs.get('x', None)

assert x is not None, 'x cannot be None in {}'.format(op_type)
check_variable_and_dtype(x, 'x', ['float32', 'float64', 'int32', 'int64'],
op_type)
if len(x.shape) < 2:
raise ValueError("input shape in {} must be at least 2-D".format(
op_type))
raise ValueError("x shape in {} must be at least 2-D".format(op_type))
diagonal = helper.kwargs.get('diagonal', 0)
if not isinstance(diagonal, (int, )):
raise TypeError("diagonal in {} must be a python Int".format(op_type))
Expand All @@ -521,18 +520,18 @@ def _tril_triu_op(helper):
return out


def tril(input, diagonal=0, name=None):
def tril(x, diagonal=0, name=None):
"""
:alias_main: paddle.tril
:alias: paddle.tril,paddle.tensor.tril,paddle.tensor.creation.tril
This op returns the lower triangular part of a matrix (2-D tensor) or batch
of matrices :attr:`input`, the other elements of the result tensor are set
of matrices :attr:`x`, the other elements of the result tensor are set
to 0. The lower triangular part of the matrix is defined as the elements
on and below the diagonal.
Args:
input (Variable): The input variable which is a Tensor.
x (Variable): The input variable x which is a Tensor.
Support data types: ``float64``, ``float32``, ``int32``, ``int64``.
diagonal (int, optional): The diagonal to consider, default value is 0.
If :attr:`diagonal` = 0, all elements on and below the main diagonal are
Expand All @@ -545,71 +544,65 @@ def tril(input, diagonal=0, name=None):
user to set this property. For more information, please refer to :ref:`api_guide_Name`.
Returns:
Variable: Tensor, results of lower triangular operation by the specified diagonal of input tensor,
it's data type is the same as input's Tensor.
Variable: Tensor, results of lower triangular operation by the specified diagonal of input tensor x,
it's data type is the same as x's Tensor.
Raises:
TypeError: diagonal is not a int type.
ValueError: dimension of :attr:`input` is less than 2.
ValueError: dimension of :attr:`x` is less than 2.
Examples:
.. code-block:: python
import numpy as np
import paddle.tensor as tensor
import paddle.fluid as fluid
import paddle
data = np.arange(1, 13, dtype="int64").reshape(3,-1)
# array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]])
x = fluid.data(shape=(-1, 4), dtype='int64', name='x')
exe = fluid.Executor(fluid.CPUPlace())
# example 1, default diagonal
tril = tensor.tril(x)
tril_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[tril], return_numpy=True)
paddle.enable_imperative()
x = paddle.imperative.to_variable(data)
tril1 = paddle.tensor.tril(x)
# array([[ 1, 0, 0, 0],
# [ 5, 6, 0, 0],
# [ 9, 10, 11, 0]])
# example 2, positive diagonal value
tril = tensor.tril(x, diagonal=2)
tril_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[tril], return_numpy=True)
tril2 = paddle.tensor.tril(x, diagonal=2)
# array([[ 1, 2, 3, 0],
# [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]])
# example 3, negative diagonal value
tril = tensor.tril(x, diagonal=-1)
tril_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[tril], return_numpy=True)
tril3 = paddle.tensor.tril(x, diagonal=-1)
# array([[ 0, 0, 0, 0],
# [ 5, 0, 0, 0],
# [ 9, 10, 0, 0]])
"""
if in_dygraph_mode():
op = getattr(core.ops, 'tril_triu')
return op(input, 'diagonal', diagonal, "lower", True)
return op(x, 'diagonal', diagonal, "lower", True)

return _tril_triu_op(LayerHelper('tril', **locals()))


def triu(input, diagonal=0, name=None):
def triu(x, diagonal=0, name=None):
"""
:alias_main: paddle.triu
:alias: paddle.triu,paddle.tensor.triu,paddle.tensor.creation.triu
This op returns the upper triangular part of a matrix (2-D tensor) or batch of matrices
:attr:`input`, the other elements of the result tensor are set to 0.
:attr:`x`, the other elements of the result tensor are set to 0.
The upper triangular part of the matrix is defined as the elements on and
above the diagonal.
Args:
input (Variable): The input variable which is a Tensor.
x (Variable): The input variable x which is a Tensor.
Support data types: ``float64``, ``float32``, ``int32``, ``int64``.
diagonal (int, optional): The diagonal to consider, default value is 0.
If :attr:`diagonal` = 0, all elements on and above the main diagonal are
Expand All @@ -622,55 +615,49 @@ def triu(input, diagonal=0, name=None):
user to set this property. For more information, please refer to :ref:`api_guide_Name`.
Returns:
Variable: Tensor, results of upper triangular operation by the specified diagonal of input tensor,
it's data type is the same as input's Tensor.
Variable: Tensor, results of upper triangular operation by the specified diagonal of input tensor x,
it's data type is the same as x's Tensor.
Raises:
TypeError: diagonal is not a int type.
ValueError: dimension of :attr:`input` is less than 2.
ValueError: dimension of :attr:`x` is less than 2.
Examples:
.. code-block:: python
import numpy as np
import paddle.fluid as fluid
import paddle.tensor as tensor
import paddle
data = np.arange(1, 13, dtype="int64").reshape(3,-1)
# array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8],
# [ 9, 10, 11, 12]])
x = fluid.data(shape=(-1, 4), dtype='int64', name='x')
exe = fluid.Executor(fluid.CPUPlace())
paddle.enable_imperative()
# example 1, default diagonal
triu = tensor.triu(x)
triu_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[triu], return_numpy=True)
x = paddle.imperative.to_variable(data)
triu1 = paddle.tensor.triu(x)
# array([[ 1, 2, 3, 4],
# [ 0, 6, 7, 8],
# [ 0, 0, 11, 12]])
# example 2, positive diagonal value
triu = tensor.triu(x, diagonal=2)
triu_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[triu], return_numpy=True)
triu2 = paddle.tensor.triu(x, diagonal=2)
# array([[0, 0, 3, 4],
# [0, 0, 0, 8],
# [0, 0, 0, 0]])
# example 3, negative diagonal value
triu = tensor.triu(x, diagonal=-1)
triu_out, = exe.run(fluid.default_main_program(), feed={"x": data},
fetch_list=[triu], return_numpy=True)
triu3 = paddle.tensor.triu(x, diagonal=-1)
# array([[ 1, 2, 3, 4],
# [ 5, 6, 7, 8],
# [ 0, 10, 11, 12]])
"""
if in_dygraph_mode():
op = getattr(core.ops, 'tril_triu')
return op(input, 'diagonal', diagonal, "lower", False)
return op(x, 'diagonal', diagonal, "lower", False)

return _tril_triu_op(LayerHelper('triu', **locals()))

Expand Down
Loading

0 comments on commit 07f509a

Please sign in to comment.