|
|
|
@ -47,11 +47,22 @@ class TestSumOp(OpTest):
|
|
|
|
|
class TestSelectedRowsSumOp(OpTest):
|
|
|
|
|
def check_with_place(self, place):
|
|
|
|
|
scope = core.Scope()
|
|
|
|
|
|
|
|
|
|
self.height = 10
|
|
|
|
|
self.row_numel = 12
|
|
|
|
|
self.rows = [0, 1, 2, 3, 4, 5, 6]
|
|
|
|
|
|
|
|
|
|
self.check_input_and_optput(scope, place, True, True, True)
|
|
|
|
|
self.check_input_and_optput(scope, place, False, True, True)
|
|
|
|
|
self.check_input_and_optput(scope, place, False, False, True)
|
|
|
|
|
self.check_input_and_optput(scope, place, False, False, False)
|
|
|
|
|
|
|
|
|
|
def _get_array(self, row_num, row_numel):
|
|
|
|
|
array = np.ones((row_num, row_numel)).astype("float32")
|
|
|
|
|
for i in range(row_num):
|
|
|
|
|
array[i] *= i
|
|
|
|
|
return array
|
|
|
|
|
|
|
|
|
|
def check_input_and_optput(self,
|
|
|
|
|
scope,
|
|
|
|
|
place,
|
|
|
|
@ -71,28 +82,36 @@ class TestSelectedRowsSumOp(OpTest):
|
|
|
|
|
sum_op.run(scope, place)
|
|
|
|
|
|
|
|
|
|
has_data_w_num = 0
|
|
|
|
|
for w in [w1_has_data, w2_has_data, w3_has_data]:
|
|
|
|
|
if not w:
|
|
|
|
|
for has_data in [w1_has_data, w2_has_data, w3_has_data]:
|
|
|
|
|
if has_data:
|
|
|
|
|
has_data_w_num += 1
|
|
|
|
|
|
|
|
|
|
self.assertEqual(7 * has_data_w_num, len(out.rows()))
|
|
|
|
|
if has_data_w_num > 0:
|
|
|
|
|
self.assertEqual(len(out.rows()), 7)
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
np.array_equal(
|
|
|
|
|
np.array(out.get_tensor()),
|
|
|
|
|
self._get_array(len(self.rows), self.row_numel) *
|
|
|
|
|
has_data_w_num))
|
|
|
|
|
else:
|
|
|
|
|
self.assertEqual(len(out.rows()), 0)
|
|
|
|
|
self.assertTrue(
|
|
|
|
|
np.array_equal(
|
|
|
|
|
np.array(out.get_tensor()),
|
|
|
|
|
self._get_array(0, self.row_numel) * has_data_w_num))
|
|
|
|
|
|
|
|
|
|
def create_selected_rows(self, scope, place, var_name, isEmpty):
|
|
|
|
|
def create_selected_rows(self, scope, place, var_name, has_data):
|
|
|
|
|
# create and initialize W Variable
|
|
|
|
|
if not isEmpty:
|
|
|
|
|
rows = [0, 1, 2, 3, 4, 5, 6]
|
|
|
|
|
row_numel = 12
|
|
|
|
|
if has_data:
|
|
|
|
|
rows = self.rows
|
|
|
|
|
else:
|
|
|
|
|
rows = []
|
|
|
|
|
row_numel = 12
|
|
|
|
|
|
|
|
|
|
var = scope.var(var_name)
|
|
|
|
|
w_selected_rows = var.get_selected_rows()
|
|
|
|
|
w_selected_rows.set_height(len(rows))
|
|
|
|
|
w_selected_rows.set_height(self.height)
|
|
|
|
|
w_selected_rows.set_rows(rows)
|
|
|
|
|
w_array = np.ones((len(rows), row_numel)).astype("float32")
|
|
|
|
|
for i in range(len(rows)):
|
|
|
|
|
w_array[i] *= i
|
|
|
|
|
w_array = self._get_array(len(rows), self.row_numel)
|
|
|
|
|
w_tensor = w_selected_rows.get_tensor()
|
|
|
|
|
w_tensor.set(w_array, place)
|
|
|
|
|
|
|
|
|
|