Merge pull request #4022 from QiJune/refine_op_py_tests
refine all operator python tests using new test frameworkenforce_failed
commit
d71190f04c
File diff suppressed because it is too large
Load Diff
@ -1,82 +0,0 @@
|
||||
import numpy
|
||||
import paddle.v2.framework.core as core
|
||||
from paddle.v2.framework.op import Operator
|
||||
|
||||
|
||||
class OpTestMeta(type):
|
||||
"""
|
||||
Operator Test ClassMeta.
|
||||
|
||||
It injects `test_all` method into user's OperatorTest class, to make Python
|
||||
unittest module run that method.
|
||||
|
||||
The `test_all` read what value is stored in `self`. It use self's values to
|
||||
create and run a operator, and check whether that op is OK or not.
|
||||
|
||||
See `test_add_two_op` for example usage.
|
||||
"""
|
||||
|
||||
def __new__(cls, name, bases, attrs):
|
||||
obj = super(OpTestMeta, cls).__new__(cls, name, bases, attrs)
|
||||
|
||||
def test_all(self):
|
||||
scope = core.Scope()
|
||||
kwargs = dict()
|
||||
places = [core.CPUPlace()]
|
||||
if core.is_compile_gpu():
|
||||
places.append(core.GPUPlace(0))
|
||||
|
||||
for place in places:
|
||||
for in_name, in_dup in Operator.get_op_inputs(self.type):
|
||||
if hasattr(self, 'inputs') and in_name in self.inputs:
|
||||
kwargs[in_name] = []
|
||||
if in_dup:
|
||||
arrays = self.inputs[in_name]
|
||||
for index, arr in enumerate(arrays):
|
||||
var = scope.new_var(in_name + str(index))
|
||||
tensor = var.get_tensor()
|
||||
tensor.set_dims(arr.shape)
|
||||
tensor.set(arr, place)
|
||||
kwargs[in_name].append(in_name + str(index))
|
||||
else:
|
||||
kwargs[in_name] = in_name
|
||||
var = scope.new_var(in_name).get_tensor()
|
||||
arr = self.inputs[in_name]
|
||||
var.set_dims(arr.shape)
|
||||
var.set(arr, place)
|
||||
else:
|
||||
kwargs[in_name] = "@EMPTY@"
|
||||
|
||||
for out_name, out_dup in Operator.get_op_outputs(self.type):
|
||||
if not hasattr(self, "outputs"):
|
||||
raise ValueError(
|
||||
"The test op must set self.outputs dict.")
|
||||
if out_name not in self.outputs:
|
||||
raise ValueError("The %s is not in self.outputs dict." %
|
||||
(out_name))
|
||||
kwargs[out_name] = out_name
|
||||
scope.new_var(out_name).get_tensor()
|
||||
|
||||
for attr_name in Operator.get_op_attr_names(self.type):
|
||||
if hasattr(self, "attrs") and attr_name in self.attrs:
|
||||
kwargs[attr_name] = self.attrs[attr_name]
|
||||
|
||||
op = Operator(self.type, **kwargs)
|
||||
if isinstance(place, core.GPUPlace) and not op.support_gpu():
|
||||
return
|
||||
|
||||
op.infer_shape(scope)
|
||||
|
||||
ctx = core.DeviceContext.create(place)
|
||||
op.run(scope, ctx)
|
||||
|
||||
for out_name, out_dup in Operator.get_op_outputs(self.type):
|
||||
actual = numpy.array(scope.find_var(out_name).get_tensor())
|
||||
expect = self.outputs[out_name]
|
||||
self.assertTrue(
|
||||
numpy.allclose(
|
||||
actual, expect, atol=1e-05),
|
||||
"output name: " + out_name + " has diff")
|
||||
|
||||
obj.test_all = test_all
|
||||
return obj
|
@ -1,23 +1,20 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
import numpy
|
||||
import paddle.v2.framework.core as core
|
||||
from paddle.v2.framework.op import Operator
|
||||
|
||||
from op_test_util import OpTestMeta
|
||||
|
||||
|
||||
class TestAddOp(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class TestAddOp(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "add"
|
||||
self.op_type = "add"
|
||||
self.inputs = {
|
||||
'X': numpy.random.random((102, 105)).astype("float32"),
|
||||
'Y': numpy.random.random((102, 105)).astype("float32")
|
||||
'X': np.random.random((102, 105)).astype("float32"),
|
||||
'Y': np.random.random((102, 105)).astype("float32")
|
||||
}
|
||||
self.outputs = {'Out': self.inputs['X'] + self.inputs['Y']}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -1,22 +1,22 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from gradient_checker import GradientChecker, create_op
|
||||
from op_test_util import OpTestMeta
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestConcatOp(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class TestConcatOp(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "concat"
|
||||
self.op_type = "concat"
|
||||
x0 = np.random.random((2, 3, 2, 5)).astype('float32')
|
||||
x1 = np.random.random((2, 3, 3, 5)).astype('float32')
|
||||
x2 = np.random.random((2, 3, 4, 5)).astype('float32')
|
||||
axis = 2
|
||||
self.inputs = {'X': [x0, x1, x2]}
|
||||
self.inputs = {'X': [('x0', x0), ('x1', x1), ('x2', x2)]}
|
||||
self.attrs = {'axis': axis}
|
||||
self.outputs = {'Out': np.concatenate((x0, x1, x2), axis=axis)}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
|
@ -1,16 +1,17 @@
|
||||
import unittest
|
||||
from op_test_util import OpTestMeta
|
||||
import numpy
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestFillZerosLikeOp(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class TestFillZerosLikeOp(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "fill_zeros_like"
|
||||
self.inputs = {'Src': numpy.random.random((219, 232)).astype("float32")}
|
||||
self.outputs = {'Dst': numpy.zeros_like(self.inputs['Src'])}
|
||||
self.op_type = "fill_zeros_like"
|
||||
self.inputs = {'Src': np.random.random((219, 232)).astype("float32")}
|
||||
self.outputs = {'Dst': np.zeros_like(self.inputs["Src"])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -1,31 +1,22 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test_util import OpTestMeta
|
||||
from gradient_checker import GradientChecker, create_op
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestLookupTableOp(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class TestLookupTableOp(OpTest):
|
||||
def setUp(self):
|
||||
self.type = 'lookup_table'
|
||||
table = np.random.random((17, 31)).astype('float32')
|
||||
ids = np.random.randint(0, 17, 4).astype('int32')
|
||||
self.op_type = "lookup_table"
|
||||
table = np.random.random((17, 31)).astype("float32")
|
||||
ids = np.random.randint(0, 17, 4).astype("int32")
|
||||
self.inputs = {'W': table, 'Ids': ids}
|
||||
self.outputs = {'Out': table[ids]}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
class TestLookupTableGradOp(GradientChecker):
|
||||
def test_grad(self):
|
||||
op = create_op('lookup_table')
|
||||
table = np.random.random((17, 31)).astype('float32')
|
||||
ids = np.random.randint(0, 17, 4).astype('int32')
|
||||
inputs = {'W': table, 'Ids': ids}
|
||||
# comapre gradients
|
||||
self.compare_grad(op, inputs, set(['Ids']))
|
||||
# check gradients
|
||||
self.check_grad(op, inputs, set('W'), 'Out')
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['W'], 'Out', no_grad_set=set('Ids'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -1,24 +1,20 @@
|
||||
import unittest
|
||||
from op_test_util import OpTestMeta
|
||||
from gradient_checker import GradientChecker, create_op
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestMeanOp(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class TestMeanOp(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "mean"
|
||||
self.inputs = {'X': np.random.random((32, 784)).astype("float32")}
|
||||
self.outputs = {'Out': np.mean(self.inputs['X'])}
|
||||
self.op_type = "mean"
|
||||
self.inputs = {'X': np.random.random((10, 10)).astype("float32")}
|
||||
self.outputs = {'Out': np.mean(self.inputs["X"])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
class MeanGradOpTest(GradientChecker):
|
||||
def test_normal(self):
|
||||
op = create_op("mean")
|
||||
inputs = {"X": np.random.random((10, 10)).astype("float32")}
|
||||
self.check_grad(op, inputs, set("X"), "Out")
|
||||
def test_checkout_grad(self):
|
||||
self.check_grad(['X'], 'Out')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -1,30 +1,23 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from gradient_checker import GradientChecker, create_op
|
||||
from op_test_util import OpTestMeta
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class MinusOpTest(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class MinusOpTest(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "minus"
|
||||
self.op_type = "minus"
|
||||
self.inputs = {
|
||||
'X': np.random.random((32, 84)).astype("float32"),
|
||||
'Y': np.random.random((32, 84)).astype("float32")
|
||||
}
|
||||
self.outputs = {'Out': (self.inputs['X'] - self.inputs['Y'])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
class MinusGradTest(GradientChecker):
|
||||
def test_left(self):
|
||||
op = create_op("minus")
|
||||
inputs = {
|
||||
"X": np.random.random((10, 10)).astype("float32"),
|
||||
"Y": np.random.random((10, 10)).astype("float32")
|
||||
}
|
||||
self.check_grad(op, inputs, ["X", 'Y'], "Out")
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X', 'Y'], 'Out')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -1,68 +1,51 @@
|
||||
import unittest
|
||||
import numpy as np
|
||||
from op_test_util import OpTestMeta
|
||||
from gradient_checker import GradientChecker, create_op
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestRowwiseAddOp(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
def setUp(self):
|
||||
self.type = "rowwise_add"
|
||||
self.inputs = {
|
||||
'X': np.random.random((32, 84)).astype("float32"),
|
||||
'b': np.random.random(84).astype("float32")
|
||||
}
|
||||
self.outputs = {'Out': np.add(self.inputs['X'], self.inputs['b'])}
|
||||
|
||||
|
||||
class TestRowwiseAddOp2(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class TestRowwiseAddOp(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "rowwise_add"
|
||||
self.op_type = "rowwise_add"
|
||||
self.inputs = {
|
||||
'X': np.random.random((13, 6, 7, 8)).astype("float32"),
|
||||
'b': np.random.random((7, 8)).astype("float32")
|
||||
'X': np.random.uniform(0.1, 1, [5, 10]).astype("float32"),
|
||||
'b': np.random.uniform(0.1, 1, [10]).astype("float32")
|
||||
}
|
||||
self.outputs = {'Out': np.add(self.inputs['X'], self.inputs['b'])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
class TestRowwiseAddGradOp(GradientChecker):
|
||||
def setUp(self):
|
||||
self.op = create_op("rowwise_add")
|
||||
self.inputs = {
|
||||
"X": np.random.uniform(0.1, 1, [5, 10]).astype("float32"),
|
||||
"b": np.random.uniform(0.1, 1, [10]).astype("float32")
|
||||
}
|
||||
def test_check_grad_normal(self):
|
||||
self.check_grad(['X', 'b'], 'Out')
|
||||
|
||||
def test_normal(self):
|
||||
self.check_grad(self.op, self.inputs, ["X", "b"], "Out")
|
||||
def test_check_grad_ingore_b(self):
|
||||
self.check_grad(['X'], 'Out', no_grad_set=set('b'))
|
||||
|
||||
def test_ignore_b(self):
|
||||
self.check_grad(self.op, self.inputs, ["X"], "Out", no_grad_set={"b"})
|
||||
def test_check_grad_ingore_x(self):
|
||||
self.check_grad(['b'], 'Out', no_grad_set=set('X'))
|
||||
|
||||
def test_ignore_x(self):
|
||||
self.check_grad(self.op, self.inputs, ["b"], "Out", no_grad_set={"X"})
|
||||
|
||||
|
||||
class TestRowwiseAddGradOp2(GradientChecker):
|
||||
class TestRowwiseAddOp2(OpTest):
|
||||
def setUp(self):
|
||||
self.op = create_op("rowwise_add")
|
||||
self.op_type = "rowwise_add"
|
||||
self.inputs = {
|
||||
"X": np.random.uniform(0.1, 1, [2, 3, 2, 5]).astype("float32"),
|
||||
"b": np.random.uniform(0.1, 1, [2, 5]).astype("float32")
|
||||
'X': np.random.uniform(0.1, 1, [2, 3, 2, 5]).astype("float32"),
|
||||
'b': np.random.uniform(0.1, 1, [2, 5]).astype("float32")
|
||||
}
|
||||
self.outputs = {'Out': np.add(self.inputs['X'], self.inputs['b'])}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
def test_normal(self):
|
||||
self.check_grad(self.op, self.inputs, ["X", "b"], "Out")
|
||||
def test_check_grad_normal(self):
|
||||
self.check_grad(['X', 'b'], 'Out')
|
||||
|
||||
def test_ignore_b(self):
|
||||
self.check_grad(self.op, self.inputs, ["X"], "Out", no_grad_set={"b"})
|
||||
def test_check_grad_ignore_b(self):
|
||||
self.check_grad(['X'], 'Out', no_grad_set=set('b'))
|
||||
|
||||
def test_ignore_x(self):
|
||||
self.check_grad(self.op, self.inputs, ["b"], "Out", no_grad_set={"X"})
|
||||
def test_check_grad_ignore_x(self):
|
||||
self.check_grad(['b'], 'Out', no_grad_set=set('X'))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -1,43 +1,34 @@
|
||||
import unittest
|
||||
from op_test_util import OpTestMeta
|
||||
from gradient_checker import GradientChecker, create_op
|
||||
import numpy as np
|
||||
from paddle.v2.framework.op import Operator
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class IdentityTest(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class IdentityTest(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "identity"
|
||||
self.inputs = {'X': np.random.random((32, 784)).astype("float32")}
|
||||
self.op_type = "identity"
|
||||
self.inputs = {'X': np.random.random((10, 10)).astype("float32")}
|
||||
self.outputs = {'Out': self.inputs['X']}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
class IdentityGradOpTest(GradientChecker):
|
||||
def test_normal(self):
|
||||
op = create_op("identity")
|
||||
inputs = {"X": np.random.random((10, 10)).astype("float32")}
|
||||
self.check_grad(op, inputs, set("X"), "Out")
|
||||
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Out')
|
||||
|
||||
class ScaleTest(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class ScaleTest(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "scale"
|
||||
self.inputs = {'X': np.random.random((32, 784)).astype("float32")}
|
||||
self.op_type = "scale"
|
||||
self.inputs = {'X': np.random.random((10, 10)).astype("float32")}
|
||||
self.attrs = {'scale': -2.3}
|
||||
self.outputs = {'Out': self.inputs['X'] * self.attrs['scale']}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
class ScaleGradTest(GradientChecker):
|
||||
def test_normal(self):
|
||||
op = Operator("scale", X="X", Out="Out", scale=3.2)
|
||||
self.check_grad(op,
|
||||
{"X": np.random.random((10, 10)).astype("float32")},
|
||||
set("X"), "Out")
|
||||
def test_check_grad(self):
|
||||
self.check_grad(['X'], 'Out')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
@ -1,21 +1,22 @@
|
||||
import unittest
|
||||
import numpy
|
||||
from op_test_util import OpTestMeta
|
||||
import numpy as np
|
||||
from op_test import OpTest
|
||||
|
||||
|
||||
class TestSGD(unittest.TestCase):
|
||||
__metaclass__ = OpTestMeta
|
||||
|
||||
class TestSGD(OpTest):
|
||||
def setUp(self):
|
||||
self.type = "sgd"
|
||||
w = numpy.random.random((102, 105)).astype("float32")
|
||||
g = numpy.random.random((102, 105)).astype("float32")
|
||||
self.op_type = "sgd"
|
||||
w = np.random.random((102, 105)).astype("float32")
|
||||
g = np.random.random((102, 105)).astype("float32")
|
||||
lr = 0.1
|
||||
|
||||
self.inputs = {'param': w, 'grad': g}
|
||||
self.attrs = {'learning_rate': lr}
|
||||
self.outputs = {'param_out': w - lr * g}
|
||||
|
||||
def test_check_output(self):
|
||||
self.check_output()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue