Feature/py executor test (#4922)
* Implement FC layer with helper * Update LayerHelper * Add debug string for Python ProtoBuf and Rename `Sync` to `Flush` * Add check of ProtoBuf initialization * Layer wrapper for FC * Fix unittest * Fix CI * Add code generator * AttributeChecker Better error log and speicalize bool Since lots of types can be cast to bool * Complete mlp, fit_a_line * Expose get global scope * Make global scope not thread-safe 1. It is no need to make global scope thread-safe, since it will be invoked in Python main thread. 2. Do not free the global scope when C++ exit. Let the OS free memories, otherwise, we need to handle the destroy dependencies. See https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables * Fix * Implementation of simple conv_2d layer * Stash * Remove private data members in OpRegister * Fix bugs * Stash * Expose FeedFetchList as VarType * Change ProgramDesc not a global variable * Polish code style * Stash * Correct implement BlockDesc destructor * Correct implement BlockDesc destructor * Unify program as parameter name * Fix bugs * Add unittest * Fix unit test error * Remove unused functions * Add clone for Python Program * Working on executor * Stash * Add glog as dependencies of ops * Use VLOG to logging some information is helpful when we debug Paddle * Expose VarDesc::persistable to Python * Test executor * Complete unittest * Polish code * Fix merge error * Follow comment * Polish Python Coderevert-4814-Add_sequence_project_op
parent
63ffe5250a
commit
3db5278301
@ -0,0 +1,59 @@
|
||||
import paddle.v2.framework.core as core
|
||||
from paddle.v2.framework.framework import Block, Program
|
||||
|
||||
|
||||
class Executor(object):
|
||||
def __init__(self, places):
|
||||
if not isinstance(places, list) and not isinstance(places, tuple):
|
||||
places = [places]
|
||||
|
||||
act_places = []
|
||||
for each in places:
|
||||
p = core.Place()
|
||||
p.set_place(each)
|
||||
act_places.append(p)
|
||||
|
||||
self.executor = core.Executor(act_places)
|
||||
|
||||
def run(self,
|
||||
program,
|
||||
feed,
|
||||
fetch_list,
|
||||
feed_var_name='feed',
|
||||
fetch_var_name='fetch'):
|
||||
if not isinstance(program, Program):
|
||||
raise TypeError()
|
||||
|
||||
program = program.clone()
|
||||
global_block = program.global_block()
|
||||
feed_var = global_block.create_var(
|
||||
name=feed_var_name,
|
||||
type=core.VarDesc.VarType.FEED_MINIBATCH,
|
||||
persistable=True)
|
||||
|
||||
for i, name in enumerate(feed):
|
||||
out = global_block.var(name)
|
||||
global_block.prepend_op(
|
||||
'feed',
|
||||
inputs={'X': [feed_var]},
|
||||
outputs={'Out': [out]},
|
||||
attrs={'col': i})
|
||||
# FIXME
|
||||
core.set_feed_variable_float(feed[name], feed_var.name, i)
|
||||
|
||||
fetch_var = global_block.create_var(
|
||||
name=fetch_var_name,
|
||||
type=core.VarDesc.VarType.FETCH_LIST,
|
||||
persistable=True)
|
||||
for i, var in enumerate(fetch_list):
|
||||
global_block.append_op(
|
||||
type='fetch',
|
||||
inputs={'X': [var]},
|
||||
outputs={'Out': [fetch_var]},
|
||||
attrs={'col': i})
|
||||
|
||||
self.executor.run(program.desc, 0)
|
||||
return [
|
||||
core.get_fetch_variable(fetch_var_name, i)
|
||||
for i in xrange(len(fetch_list))
|
||||
]
|
@ -0,0 +1,36 @@
|
||||
import unittest
|
||||
from paddle.v2.framework.layers import mul, data
|
||||
import paddle.v2.framework.core as core
|
||||
from paddle.v2.framework.executor import Executor
|
||||
from paddle.v2.framework.framework import g_program
|
||||
import numpy
|
||||
|
||||
|
||||
class TestExecutor(unittest.TestCase):
|
||||
def test_mul(self):
|
||||
a = data(name='a', shape=[784], data_type='float32')
|
||||
b = data(
|
||||
name='b',
|
||||
shape=[784, 100],
|
||||
data_type='float32',
|
||||
append_batch_size=False)
|
||||
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_program,
|
||||
feed={'a': tensor_a,
|
||||
'b': tensor_b},
|
||||
fetch_list=[out])
|
||||
out = numpy.array(outs[0])
|
||||
self.assertEqual((100, 100), out.shape)
|
||||
self.assertTrue(numpy.allclose(out, numpy.dot(a_np, b_np)))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
Loading…
Reference in new issue