Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"add asnumpy interface" #5620

Merged
merged 6 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 81 additions & 3 deletions python/paddle/v2/fluid/executor.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
import numpy as np
import paddle.v2.fluid.core as core
from paddle.v2.fluid.framework import Block, Program, g_main_program

g_scope = core.Scope()


def as_numpy(tensor):
if isinstance(tensor, list):
return [as_numpy(t) for t in tensor]
assert isinstance(tensor, core.LoDTensor)
lod = tensor.lod()
tensor_data = np.array(tensor)
if len(lod) == 0:
ans = tensor_data
else:
raise RuntimeError("LoD Calculate lacks unit tests and buggy")
# elif len(lod) == 1:
# ans = []
# idx = 0
# while idx < len(lod) - 1:
# ans.append(tensor_data[lod[idx]:lod[idx + 1]])
# idx += 1
# else:
# for l in reversed(lod):
# ans = []
# idx = 0
# while idx < len(l) - 1:
# ans.append(tensor_data[l[idx]:l[idx + 1]])
# idx += 1
# tensor_data = ans
# ans = tensor_data
return ans


class Executor(object):
def __init__(self, places):
if not isinstance(places, list) and not isinstance(places, tuple):
Expand All @@ -16,14 +45,56 @@ def __init__(self, places):
act_places.append(p)

self.executor = core.Executor(act_places)
self.places = places

def aslodtensor(self, data):
def accumulate(data):
if not isinstance(data, list):
return 1
return sum([accumulate(sub) for sub in data])

def parselod(data):
seq_lens = [accumulate(seq) for seq in data]
cur_len = 0
lod = [cur_len]
for l in seq_lens:
cur_len += l
lod.append(cur_len)
return lod

assert len(self.places) != 0
if not isinstance(data, list):
# pure tensor case
tensor = core.LoDTensor()
tensor.set(data, self.places[0])
return tensor
else:
raise RuntimeError("Current implementation lacks unittests")
# lodtensor case
lod = []
if not isinstance(data[0], list):
lod.append(parselod(data))
flattened_data = np.concatenate(data, axis=0).astype("int64")
else:
while isinstance(data[0], list):
lod.append(parselod(seq))
flattened_data = [item for seq in data for item in seq]
data = flattened_data
flattened_data = np.concatenate(data, axis=0).astype("int64")
flattened_data = flattened_data.reshape([len(flattened_data), 1])
tensor = core.LoDTensor()
tensor.set(flattened_data, self.places[0])
tensor.set_lod(lod)
return tensor

def run(self,
program=None,
feed=None,
fetch_list=None,
feed_var_name='feed',
fetch_var_name='fetch',
scope=None):
scope=None,
return_numpy=True):
if feed is None:
feed = {}
if fetch_list is None:
Expand Down Expand Up @@ -52,7 +123,10 @@ def run(self,
inputs={'X': [feed_var]},
outputs={'Out': [out]},
attrs={'col': i})
core.set_feed_variable(scope, feed[name], feed_var.name, i)
cur_feed = feed[name]
if not isinstance(cur_feed, core.LoDTensor):
cur_feed = self.aslodtensor(cur_feed)
core.set_feed_variable(scope, cur_feed, feed_var.name, i)

fetch_var = global_block.create_var(
name=fetch_var_name,
Expand All @@ -66,7 +140,11 @@ def run(self,
attrs={'col': i})

self.executor.run(program.desc, scope, 0, True)
return [
outs = [
core.get_fetch_variable(scope, fetch_var_name, i)
for i in xrange(len(fetch_list))
]

if return_numpy:
outs = as_numpy(outs)
return outs
1 change: 1 addition & 0 deletions python/paddle/v2/fluid/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
image/
fit_a_line.model/
tmp
10 changes: 7 additions & 3 deletions python/paddle/v2/fluid/tests/op_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,10 @@ def check_output_with_place(self, place, atol):
feed_map = self.feed_var(inputs, place)

exe = Executor(place)
outs = exe.run(program, feed=feed_map, fetch_list=fetch_list)
outs = exe.run(program,
feed=feed_map,
fetch_list=fetch_list,
return_numpy=False)

for out_name, out_dup in Operator.get_op_outputs(self.op_type):
if out_name not in self.outputs:
Expand Down Expand Up @@ -501,5 +504,6 @@ def _get_gradient(self, input_to_check, place, output_names, no_grad_set):

fetch_list = [g for p, g in param_grad_list]
executor = Executor(place)
result = executor.run(prog, feed_dict, fetch_list)
return map(np.array, result)
return map(
np.array,
executor.run(prog, feed_dict, fetch_list, return_numpy=False))
27 changes: 12 additions & 15 deletions python/paddle/v2/fluid/tests/test_array_read_write_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,13 @@ def test_read_write(self):

exe = Executor(cpu)

tensor = core.LoDTensor()
tensor.set(numpy.random.random(size=(100, 100)).astype('float32'), cpu)

outs = map(numpy.array,
exe.run(feed={'x0': tensor,
'x1': tensor,
'x2': tensor},
fetch_list=[a_sum, x_sum],
scope=scope))
tensor = numpy.random.random(size=(100, 100)).astype('float32')

outs = exe.run(feed={'x0': tensor,
'x1': tensor,
'x2': tensor},
fetch_list=[a_sum, x_sum],
scope=scope)
self.assertEqual(outs[0], outs[1])

total_sum = layers.sums(input=[a_sum, x_sum])
Expand All @@ -72,12 +70,11 @@ def test_read_write(self):
[each_x.name + "@GRAD" for each_x in x])
g_out = [
item.sum()
for item in map(
numpy.array,
exe.run(feed={'x0': tensor,
'x1': tensor,
'x2': tensor},
fetch_list=g_vars))
for item in exe.run(
feed={'x0': tensor,
'x1': tensor,
'x2': tensor},
fetch_list=g_vars)
]
g_out_sum = numpy.array(g_out).sum()

Expand Down
13 changes: 5 additions & 8 deletions python/paddle/v2/fluid/tests/test_conditional_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,15 @@ def test_forward(self):
exe = Executor(cpu)
exe.run(g_startup_program)

x = core.LoDTensor()
x.set(numpy.random.random(size=(10, 1)).astype('float32'), cpu)
x = numpy.random.random(size=(10, 1)).astype('float32')

outs = map(numpy.array, exe.run(feed={'X': x}, fetch_list=[out]))[0]
outs = exe.run(feed={'X': x}, fetch_list=[out])[0]
print outs
loss = layers.mean(x=out)
append_backward_ops(loss=loss)
outs = map(numpy.array,
exe.run(feed={'X': x},
fetch_list=[
g_main_program.block(0).var(data.name + "@GRAD")
]))[0]
outs = exe.run(
feed={'X': x},
fetch_list=[g_main_program.block(0).var(data.name + "@GRAD")])[0]
print outs


Expand Down
12 changes: 4 additions & 8 deletions python/paddle/v2/fluid/tests/test_executor_and_mul.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import unittest
from paddle.v2.fluid.layers import mul, data
from paddle.v2.fluid.layers import mul, data, sequence_pool
import paddle.v2.fluid.core as core
from paddle.v2.fluid.executor import Executor
from paddle.v2.fluid.framework import g_main_program
Expand All @@ -17,17 +17,13 @@ def test_mul(self):
out = mul(x=a, y=b)
place = core.CPUPlace()
a_np = numpy.random.random((100, 784)).astype('float32')
tensor_a = core.LoDTensor()
tensor_a.set(a_np, place)
b_np = numpy.random.random((784, 100)).astype('float32')
tensor_b = core.LoDTensor()
tensor_b.set(b_np, place)
exe = Executor(place)
outs = exe.run(g_main_program,
feed={'a': tensor_a,
'b': tensor_b},
feed={'a': a_np,
'b': b_np},
fetch_list=[out])
out = numpy.array(outs[0])
out = outs[0]
self.assertEqual((100, 100), out.shape)
self.assertTrue(numpy.allclose(out, numpy.dot(a_np, b_np)))

Expand Down
33 changes: 14 additions & 19 deletions python/paddle/v2/fluid/tests/test_inference_model_io.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import paddle.v2 as paddle
import paddle.v2.fluid.layers as layers
import unittest

import numpy as np
import paddle.v2.fluid.core as core
import paddle.v2.fluid.optimizer as optimizer

import paddle.v2.fluid.executor as executor
import paddle.v2.fluid.layers as layers
import paddle.v2.fluid.optimizer as optimizer
from paddle.v2.fluid.framework import Program
from paddle.v2.fluid.io import save_inference_model, load_inference_model
import paddle.v2.fluid.executor as executor
import unittest
import numpy as np


class TestBook(unittest.TestCase):
Expand Down Expand Up @@ -44,33 +44,28 @@ def test_fit_line_inference_model(self):
x=cost, main_program=program, startup_program=init_program)

sgd_optimizer = optimizer.SGDOptimizer(learning_rate=0.001)
opts = sgd_optimizer.minimize(avg_cost, init_program)
sgd_optimizer.minimize(avg_cost, init_program)

place = core.CPUPlace()
exe = executor.Executor(place)

exe.run(init_program, feed={}, fetch_list=[])

for i in xrange(100):
x_data = np.array(
tensor_x = np.array(
[[1, 1], [1, 2], [3, 4], [5, 2]]).astype("float32")
y_data = np.array([[-2], [-3], [-7], [-7]]).astype("float32")
tensor_y = np.array([[-2], [-3], [-7], [-7]]).astype("float32")

tensor_x = core.LoDTensor()
tensor_x.set(x_data, place)
tensor_y = core.LoDTensor()
tensor_y.set(y_data, place)
exe.run(program,
feed={'x': tensor_x,
'y': tensor_y},
fetch_list=[avg_cost])

save_inference_model(MODEL_DIR, ["x", "y"], [avg_cost], exe, program)
outs = exe.run(program,
feed={'x': tensor_x,
'y': tensor_y},
fetch_list=[avg_cost])
expected = np.array(outs[0])
expected = exe.run(program,
feed={'x': tensor_x,
'y': tensor_y},
fetch_list=[avg_cost])[0]

reload(executor) # reload to build a new scope
exe = executor.Executor(place)
Expand All @@ -83,7 +78,7 @@ def test_fit_line_inference_model(self):
feed={feed_var_names[0]: tensor_x,
feed_var_names[1]: tensor_y},
fetch_list=fetch_vars)
actual = np.array(outs[0])
actual = outs[0]

self.assertEqual(feed_var_names, ["x", "y"])
self.assertEqual(len(fetch_vars), 1)
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/v2/fluid/tests/test_lod_array_length_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_array_length(self):
arr_len = layers.array_length(arr)
cpu = core.CPUPlace()
exe = Executor(cpu)
result = numpy.array(exe.run(fetch_list=[arr_len])[0])
result = exe.run(fetch_list=[arr_len])[0]
self.assertEqual(11, result[0])


Expand Down
9 changes: 5 additions & 4 deletions python/paddle/v2/fluid/tests/test_lod_tensor_array_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,11 @@ def test_grad(self):

exe = Executor(place)
g_out = [
item.sum()
for item in map(
numpy.array,
exe.run(program, feed={'x': tensor}, fetch_list=[g_vars]))
numpy.array(item).sum()
for item in exe.run(program,
feed={'x': tensor},
fetch_list=[g_vars],
return_numpy=False)
]
g_out_sum = numpy.array(g_out).sum()

Expand Down
32 changes: 9 additions & 23 deletions python/paddle/v2/fluid/tests/test_mnist_if_else_op.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,10 @@ def test_raw_api(self):
y_data = np.array(map(lambda x: x[1], data)).astype("int64")
y_data = np.expand_dims(y_data, axis=1)

tensor_x = core.LoDTensor()
tensor_x.set(x_data, place)

tensor_y = core.LoDTensor()
tensor_y.set(y_data, place)

outs = map(np.array,
exe.run(kwargs['main_program'],
feed={'x': tensor_x,
'y': tensor_y},
fetch_list=[avg_loss]))
outs = exe.run(kwargs['main_program'],
feed={'x': x_data,
'y': y_data},
fetch_list=[avg_loss])
print outs[0]
if outs[0] < 1.0:
return
Expand Down Expand Up @@ -131,19 +124,12 @@ def test_ifelse(self):
for data in train_reader():
x_data = np.array(map(lambda x: x[0], data)).astype("float32")
y_data = np.array(map(lambda x: x[1], data)).astype("int64")
y_data = np.expand_dims(y_data, axis=1)

tensor_x = core.LoDTensor()
tensor_x.set(x_data, place)

tensor_y = core.LoDTensor()
tensor_y.set(y_data, place)
y_data = y_data.reshape((y_data.shape[0], 1))

outs = map(np.array,
exe.run(kwargs['main_program'],
feed={'x': tensor_x,
'y': tensor_y},
fetch_list=[avg_loss]))
outs = exe.run(kwargs['main_program'],
feed={'x': x_data,
'y': y_data},
fetch_list=[avg_loss])
print outs[0]
if outs[0] < 1.0:
return
Expand Down
2 changes: 1 addition & 1 deletion python/paddle/v2/fluid/tests/test_parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_param(self):
self.assertEqual(0, param.block.idx)
exe = Executor(core.CPUPlace())
p = exe.run(g_main_program, fetch_list=[param])[0]
self.assertTrue(np.allclose(np.array(p), np.ones(shape) * val))
self.assertTrue(np.allclose(p, np.ones(shape) * val))
p = io.get_parameter_value_by_name('fc.w', exe, g_main_program)
self.assertTrue(np.allclose(np.array(p), np.ones(shape) * val))

Expand Down
Loading