Skip to content

Commit

Permalink
add exception catching trial
Browse files Browse the repository at this point in the history
  • Loading branch information
kexinzhao committed Jun 15, 2018
1 parent 6b11525 commit 051d7e6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 22 deletions.
7 changes: 4 additions & 3 deletions paddle/fluid/pybind/pybind.cc
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,9 @@ PYBIND11_PLUGIN(core) {
recursive_sequence_lengths.end(),
std::back_inserter(new_lod));
LoD new_offset_lod = ConvertToOffsetBasedLoD(new_lod);
PADDLE_ENFORCE(CheckLoD(new_offset_lod, -1),
"the provided lod info is invalid");
PADDLE_ENFORCE(
CheckLoD(new_offset_lod, -1),
"the provided recursive_sequence_lengths info is invalid");
new (&instance) LoDTensor(new_offset_lod);
})
.def("__init__", [](LoDTensor &instance) { new (&instance) LoDTensor(); })
Expand Down Expand Up @@ -184,7 +185,7 @@ PYBIND11_PLUGIN(core) {
LoD new_offset_lod = ConvertToOffsetBasedLoD(new_lod);
PADDLE_ENFORCE(
CheckLoD(new_offset_lod, vectorize(self.dims()).front()),
"the provided lod info is invalid");
"the provided recursive_sequence_lengths info is invalid");
self.set_lod(new_offset_lod);
})
.def("lod",
Expand Down
9 changes: 3 additions & 6 deletions python/paddle/fluid/tests/test_lod_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,10 @@ def test_pybind_lod(self):
tensor = fluid.LoDTensor()
lod = []
tensor.set_recursive_sequence_lengths(lod)
self.assertTrue(tensor.has_valid_recursive_sequence_lengths())
lod = [[], [1], [3]]
tensor.set_recursive_sequence_lengths(lod)
self.assertFalse(tensor.has_valid_recursive_sequence_lengths())
self.assertRaises(Exception, tensor.set_recursive_sequence_lengths, lod)
lod = [[0], [2], [3]]
tensor.set_recursive_sequence_lengths(lod)
self.assertFalse(tensor.has_valid_recursive_sequence_lengths())
self.assertRaises(Exception, tensor.set_recursive_sequence_lengths, lod)

lod = [[1, 2, 3]]
tensor.set_recursive_sequence_lengths(lod)
Expand All @@ -41,7 +38,7 @@ def test_pybind_lod(self):

# Each level's sum should be equal to the number of items in the next level
# Moreover, last level's sum should be equal to the tensor height
lod = [[2, 1], [1, 3, 1, 2, 1]]
lod = [[2, 3], [1, 3, 1, 2, 2]]
tensor.set_recursive_sequence_lengths(lod)
self.assertEqual(tensor.recursive_sequence_lengths(), lod)
tensor.set(np.random.random([8, 1]), fluid.CPUPlace())
Expand Down
8 changes: 4 additions & 4 deletions python/paddle/fluid/tests/unittests/test_edit_distance_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ class TestEditDistanceOp(OpTest):
def setUp(self):
self.op_type = "edit_distance"
normalized = False
x1 = np.array([[0, 12, 3, 5, 8, 2]]).astype("int64")
x2 = np.array([[0, 12, 4, 7, 8]]).astype("int64")
x1 = np.array([[12, 3, 5, 8, 2]]).astype("int64")
x2 = np.array([[12, 4, 7, 8]]).astype("int64")
x1 = np.transpose(x1)
x2 = np.transpose(x2)
x1_lod = [1, 4]
Expand Down Expand Up @@ -87,8 +87,8 @@ class TestEditDistanceOpNormalized(OpTest):
def setUp(self):
self.op_type = "edit_distance"
normalized = True
x1 = np.array([[0, 10, 3, 6, 5, 8, 2]]).astype("int64")
x2 = np.array([[0, 10, 4, 6, 7, 8]]).astype("int64")
x1 = np.array([[10, 3, 6, 5, 8, 2]]).astype("int64")
x2 = np.array([[10, 4, 6, 7, 8]]).astype("int64")
x1 = np.transpose(x1)
x2 = np.transpose(x2)
x1_lod = [1, 2, 3]
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/test_lod_tensor_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ def test_get_set(self):

t = core.LoDTensor()
t.set(numpy.array([i + 10], dtype='float32'), cpu)
t.set_recursive_sequence_lengths([[2]])
t.set_recursive_sequence_lengths([[1]])
tensor_array[i] = t
t = tensor_array[i]
self.assertEqual(
numpy.array(t), numpy.array(
[i + 10], dtype='float32'))
self.assertEqual([[2]], t.recursive_sequence_lengths())
self.assertEqual([[1]], t.recursive_sequence_lengths())


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions python/paddle/fluid/tests/unittests/test_print_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def setUp(self):
self.x_tensor = core.LoDTensor()
tensor_np = np.random.random(size=(2, 3)).astype('float32')
self.x_tensor.set(tensor_np, self.place)
self.x_tensor.set_recursive_sequence_lengths([[1, 0]])
self.x_tensor.set_recursive_sequence_lengths([[1, 1]])

def build_network(self, only_forward, **kargs):
x = layers.data('x', shape=[3], dtype='float32', lod_level=1)
Expand Down Expand Up @@ -62,7 +62,7 @@ def setUp(self):
self.x_tensor = core.LoDTensor()
tensor_np = np.random.random(size=(2, 3)).astype('float32')
self.x_tensor.set(tensor_np, self.place)
self.x_tensor.set_recursive_sequence_lengths([[1, 0]])
self.x_tensor.set_recursive_sequence_lengths([[1, 1]])


if __name__ == '__main__':
Expand Down
10 changes: 5 additions & 5 deletions python/paddle/fluid/tests/unittests/test_sequence_expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def set_data(self):
x_data = np.random.uniform(0.1, 1, [1, 2, 2]).astype('float32')
x_lod = [[1]]
y_data = np.random.uniform(0.1, 1, [2, 2, 2]).astype('float32')
y_lod = [[2], [2]]
y_lod = [[2], [1, 1]]
self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}
self.attrs = {'ref_level': 0}

Expand All @@ -100,8 +100,8 @@ class TestSequenceExpandCase3(TestSequenceExpand):
def set_data(self):
x_data = np.random.uniform(0.1, 1, [4, 1]).astype('float32')
x_lod = [[1, 1, 1, 1]]
y_data = np.random.uniform(0.1, 1, [6, 1]).astype('float32')
y_lod = [[2, 2, 0, 2]]
y_data = np.random.uniform(0.1, 1, [8, 1]).astype('float32')
y_lod = [[2, 2, 2, 2]]
self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}


Expand All @@ -110,8 +110,8 @@ def set_data(self):
data = np.random.uniform(0.1, 1, [5 * 2, 1])
x_data = np.array(data).reshape([5, 2]).astype('float32')
x_lod = [[2, 3]]
y_data = np.random.uniform(0.1, 1, [3, 1]).astype('float32')
y_lod = [[1, 2], [1, 2]]
y_data = np.random.uniform(0.1, 1, [5, 1]).astype('float32')
y_lod = [[2], [2, 3]]
self.inputs = {'X': (x_data, x_lod), 'Y': (y_data, y_lod)}


Expand Down

0 comments on commit 051d7e6

Please sign in to comment.